FoxSemanticChecker.Mod 392 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. replacements*: Replacement;
  93. cellsAreObjects: BOOLEAN;
  94. variableAccessed: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. error := FALSE;
  103. NEW(typeFixes);
  104. NEW(pointerFixes);
  105. resolvedType := NIL;
  106. resolvedExpression := NIL;
  107. resolvedStatement := NIL;
  108. currentScope := NIL;
  109. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  110. SELF.importCache := importCache;
  111. arrayBaseImported := FALSE;
  112. SELF.VerboseErrorMessage := verboseErrorMessage;
  113. global := NIL;
  114. phase := UndefinedPhase;
  115. currentIsRealtime := FALSE;
  116. currentIsUnreachable := FALSE;
  117. currentIsCellNet := FALSE;
  118. currentIsBodyProcedure := FALSE;
  119. currentIsExclusive := FALSE;
  120. withEntries := NIL;
  121. SELF.cellsAreObjects := system.cellsAreObjects;
  122. END InitChecker;
  123. (** report error **)
  124. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  125. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  126. BEGIN
  127. IF diagnostics # NIL THEN
  128. Basic.GetErrorMessage(code,message,errorMessage);
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  132. END;
  133. error := TRUE;
  134. END Error;
  135. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  136. VAR errModule: SyntaxTree.Module;
  137. BEGIN
  138. IF diagnostics # NIL THEN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  141. END;
  142. END Warning;
  143. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  144. VAR errorMessage: ARRAY 256 OF CHAR;
  145. BEGIN
  146. IF diagnostics # NIL THEN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  149. END;
  150. error := TRUE;
  151. END ErrorSS;
  152. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  153. VAR msg, msg2: ARRAY 256 OF CHAR;
  154. BEGIN
  155. IF diagnostics # NIL THEN
  156. COPY(msg1, msg);
  157. Strings.Append(msg, " = ");
  158. Basic.GetString(s, msg2);
  159. Strings.Append(msg, msg2);
  160. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  161. END;
  162. END InfoSS;
  163. (*** symbol lookup ***)
  164. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  165. **)
  166. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  167. VAR
  168. scope,baseScope: SyntaxTree.Scope;
  169. symbol, s: SyntaxTree.Symbol;
  170. ownerRecord,base: SyntaxTree.RecordType;
  171. BEGIN
  172. scope := inScope;
  173. symbol := NIL;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. s := NIL;
  177. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  178. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  179. END;
  180. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  181. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  182. ELSE
  183. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  184. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  185. symbol.MarkUsed;
  186. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  187. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  188. base := RecordBase(ownerRecord);
  189. IF (base # NIL) THEN
  190. baseScope := base.recordScope;
  191. symbol := Find(baseScope,name,FALSE);
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. ELSE
  196. symbol := NIL;
  197. END;
  198. END;
  199. END;
  200. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  201. END;
  202. IF (symbol # NIL) THEN
  203. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  204. ASSERT(phase = DeclarationPhase);
  205. ResolveSymbol(symbol)
  206. END;
  207. symbol.MarkUsed;
  208. END;
  209. RETURN symbol
  210. END Find;
  211. (*** types ***)
  212. (** find type declaration with name qualifiedIdentifier and return resolved type
  213. - check qualified identifier prefix, set scope to module scope if appropriate
  214. - check suffix in scope
  215. **)
  216. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  217. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  218. BEGIN
  219. result := NIL;
  220. prevScope := currentScope;
  221. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  222. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  223. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  224. IF symbol(SyntaxTree.Import).module = NIL THEN
  225. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  226. result := SyntaxTree.invalidType;
  227. symbol := NIL;
  228. ELSE
  229. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  230. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  231. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  232. IF VerboseErrorMessage THEN
  233. Printout.Info("scope", currentScope);
  234. Printout.Info("symbol", symbol);
  235. END;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  237. END;
  238. END;
  239. ELSE
  240. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  241. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  242. symbol := NIL;
  243. END;
  244. ELSE
  245. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  246. IF symbol = NIL THEN
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  248. IF VerboseErrorMessage THEN
  249. Printout.Info("Qualident",qualifiedIdentifier);
  250. Printout.Info("in scope",currentScope) ;
  251. END;
  252. END;
  253. END;
  254. IF symbol = NIL THEN (* error already handled *)
  255. typeDeclaration := NIL;
  256. result := SyntaxTree.invalidType;
  257. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  258. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSE
  262. currentScope := symbol.scope;
  263. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  264. result := ResolveType(typeDeclaration.declaredType);
  265. symbol.MarkUsed;
  266. ASSERT(result # NIL);
  267. END;
  268. currentScope := prevScope;
  269. RETURN result
  270. END ResolveNamedType;
  271. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  272. If node is currently being resolved then emit a cyclic definition error.
  273. Return TRUE only if node is fully resolved.
  274. **)
  275. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  276. VAR result: BOOLEAN;
  277. BEGIN
  278. IF SyntaxTree.Resolved IN x.state THEN
  279. result := FALSE
  280. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  281. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  282. result := FALSE;
  283. ELSE
  284. result := TRUE;
  285. x.SetState(SyntaxTree.BeingResolved)
  286. END;
  287. RETURN result
  288. END TypeNeedsResolution;
  289. (** Return invalid type if x is currently being resolved, return x otherwise**)
  290. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  291. BEGIN
  292. IF SyntaxTree.Resolved IN x.state THEN
  293. RETURN x
  294. ELSE
  295. RETURN SyntaxTree.invalidType
  296. END;
  297. END ResolvedType;
  298. PROCEDURE VisitType(x: SyntaxTree.Type);
  299. BEGIN
  300. ASSERT(x = SyntaxTree.invalidType);
  301. END VisitType;
  302. (** resolve basic type **)
  303. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  304. BEGIN
  305. IF TypeNeedsResolution(x) THEN
  306. x.SetState(SyntaxTree.Resolved);
  307. END;
  308. resolvedType := ResolvedType(x)
  309. END VisitBasicType;
  310. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitByteType;
  314. (** resolve character type **)
  315. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitCharacterType;
  319. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitBooleanType;
  323. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitSetType;
  327. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitAddressType;
  331. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitSizeType;
  335. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitAnyType;
  339. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitObjectType;
  343. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitNilType;
  347. (** resolve integer type **)
  348. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitIntegerType;
  352. (** resolve real type **)
  353. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitFloatType;
  357. (** resolve complex type **)
  358. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  359. BEGIN
  360. VisitBasicType(x);
  361. END VisitComplexType;
  362. (**
  363. resolve string type: nothing to be done
  364. **)
  365. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  366. BEGIN
  367. IF TypeNeedsResolution(x) THEN
  368. x.SetState(SyntaxTree.Resolved);
  369. END;
  370. resolvedType := ResolvedType(x)
  371. END VisitStringType;
  372. (**
  373. check enumeration scope: enter symbols and check for duplicate names
  374. **)
  375. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  376. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  377. BEGIN
  378. prevScope := currentScope;
  379. currentScope := x;
  380. e := x.firstConstant;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  408. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: LONGINT;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. x.SetArrayBase(pointerType);
  479. END;
  480. IF x.length # NIL THEN
  481. variableAccessed := FALSE;
  482. e := ResolveExpression(x.length);
  483. IF (e.resolved = NIL) THEN
  484. IF variableAccessed THEN
  485. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  486. END;
  487. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  488. ELSE
  489. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  490. END;
  491. END;
  492. IF arrayBase IS SyntaxTree.ArrayType THEN
  493. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  494. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  495. END;
  496. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  497. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  498. END;
  499. x.SetHasPointers(arrayBase.hasPointers);
  500. x.SetState(SyntaxTree.Resolved);
  501. END;
  502. resolvedType := ResolvedType(x);
  503. END VisitArrayType;
  504. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  505. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  506. BEGIN
  507. module := currentScope.ownerModule;
  508. IF module.name=name THEN
  509. (* do nothing *)
  510. ELSE
  511. moduleScope := module.moduleScope;
  512. import := moduleScope.FindImport(name);
  513. IF import = NIL THEN
  514. import := SyntaxTree.NewImport(position,name,name,TRUE);
  515. moduleScope.AddImport(import);
  516. Register(import,moduleScope,FALSE);
  517. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  518. VisitImport(import);
  519. ELSIF import.direct=FALSE THEN
  520. import.SetScope(module.moduleScope);
  521. import.SetDirect(TRUE);
  522. IF moduleScope.FindSymbol(import.name) = NIL THEN
  523. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  524. duplicate.SetContext(import.context);
  525. duplicate.SetModule(import.module);
  526. Register(duplicate,moduleScope,TRUE);
  527. VisitImport(duplicate);
  528. END;
  529. END;
  530. import.MarkUsed
  531. END;
  532. END ImportModule;
  533. (**
  534. resolve math array type
  535. - check base type
  536. - open math array of array forbidden
  537. - math array of tensor forbidden
  538. - static array of open array forbidden
  539. **)
  540. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  541. VAR arrayBase: SyntaxTree.Type;
  542. BEGIN
  543. IF TypeNeedsResolution(x) THEN
  544. x.SetArrayBase(ResolveType(x.arrayBase));
  545. IF x.length # NIL THEN
  546. x.SetLength(ConstantIntegerGeq0(x.length));
  547. END;
  548. arrayBase := x.arrayBase;
  549. IF arrayBase # NIL THEN
  550. arrayBase := arrayBase.resolved;
  551. IF arrayBase = SyntaxTree.invalidType THEN
  552. (* error already handled *)
  553. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  554. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  555. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  556. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  557. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  558. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  559. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  560. END;
  561. END;
  562. IF x.form = SyntaxTree.Static THEN
  563. x.SetIncrement(system.SizeOf(arrayBase));
  564. END;
  565. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  566. END;
  567. x.SetState(SyntaxTree.Resolved);
  568. END;
  569. resolvedType := ResolvedType(x);
  570. END VisitMathArrayType;
  571. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  572. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  573. (1) Rec = RECORD ... END; Ptr <---> Rec
  574. Ptr = POINTER TO Rec; ^ |
  575. | |
  576. TypeDesc TypeDesc
  577. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  578. ^ /
  579. | /
  580. TypeDesc <-- /
  581. *)
  582. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  583. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  584. BEGIN
  585. Strings.IntToStr(x.position,number);
  586. COPY(prefix,name);
  587. Strings.Append(name,"@");
  588. Strings.Append(name,number);
  589. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  590. typeDeclaration.SetDeclaredType(x);
  591. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  592. x.SetTypeDeclaration(typeDeclaration);
  593. currentScope.AddTypeDeclaration(typeDeclaration);
  594. typeDeclaration.SetScope(currentScope);
  595. END AnonymousTypeDeclaration;
  596. (**
  597. deferred pointer type resolving
  598. - resolve base type
  599. - check that base type is a record or array type
  600. - if error then set base type to invalid type
  601. **)
  602. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  603. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  604. BEGIN
  605. ASSERT(type.pointerBase # NIL);
  606. position := type.pointerBase.position;
  607. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  608. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  609. (* not for pointers, a type is needed for the records only
  610. IF type.typeDeclaration = NIL THEN
  611. AnonymousTypeDeclaration(type);
  612. END;
  613. *)
  614. END;
  615. resolved := ResolveType(type.pointerBase);
  616. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  617. type.SetPointerBase(resolved);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  619. recordType := resolved.resolved(SyntaxTree.RecordType);
  620. IF recordType.isObject & (recordType.baseType # NIL) THEN
  621. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  622. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  623. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  624. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  625. END;
  626. END;
  627. END;
  628. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  629. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  630. END;
  631. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  632. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  633. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  634. ELS
  635. *)
  636. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  638. END;
  639. END;
  640. ELSE
  641. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  642. type.SetPointerBase(SyntaxTree.invalidType)
  643. END
  644. END FixPointerType;
  645. (**
  646. resolve pointer type
  647. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  648. **)
  649. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  650. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  651. modifiers: SyntaxTree.Modifier; position: LONGINT;
  652. BEGIN
  653. IF TypeNeedsResolution(x) THEN
  654. modifiers := x.modifiers;
  655. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  656. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  657. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  658. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  659. (* inheritance cycle check
  660. example:
  661. A=POINTER TO RECORD(B) END;
  662. B=POINTER TO RECORD(A) END;
  663. *)
  664. IF x.pointerBase IS SyntaxTree.RecordType THEN
  665. recordType := x.pointerBase(SyntaxTree.RecordType);
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  685. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  686. x.SetSize(value)
  687. ELSE
  688. x.SetSize(system.SizeOf(system.longintType));
  689. END;
  690. x.SetState(SyntaxTree.Resolved);
  691. END;
  692. resolvedType := ResolvedType(x)
  693. END VisitPortType;
  694. (**
  695. deferred procedure type resolving
  696. - resolve return type
  697. - traverse and resolve parameters
  698. **)
  699. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  700. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  701. BEGIN
  702. resolved := ResolveType(procedureType.returnType);
  703. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  704. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  705. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  706. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  707. END;
  708. procedureType.SetReturnType(resolved);
  709. IF (resolved # NIL) THEN
  710. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  711. parameter.SetType(procedureType.returnType);
  712. parameter.SetAccess(SyntaxTree.Hidden);
  713. parameter.SetUntraced(procedureType.hasUntracedReturn);
  714. VisitParameter(parameter);
  715. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  716. END;
  717. (* process parameters *)
  718. parameter :=procedureType.firstParameter;
  719. WHILE (parameter # NIL) DO
  720. VisitParameter(parameter);
  721. parameter := parameter.nextParameter;
  722. END;
  723. END FixProcedureType;
  724. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  725. VAR prev,this: SyntaxTree.Modifier;
  726. BEGIN
  727. this := modifiers;prev := NIL;
  728. WHILE (this # NIL) & (this.identifier # name) DO
  729. prev := this; this := this.nextModifier;
  730. END;
  731. IF this # NIL THEN
  732. IF this.expression # NIL THEN
  733. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  734. END;
  735. this.Resolved;
  736. position := this.position;
  737. RETURN TRUE
  738. ELSE
  739. RETURN FALSE
  740. END;
  741. END HasFlag;
  742. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  743. VAR prev,this: SyntaxTree.Modifier;
  744. BEGIN
  745. this := modifiers;prev := NIL;
  746. WHILE (this # NIL) & (this.identifier # name) DO
  747. prev := this; this := this.nextModifier;
  748. END;
  749. IF this # NIL THEN
  750. IF this.expression = NIL THEN
  751. Error(this.position,Diagnostics.Invalid,"expected expression value");
  752. ELSE
  753. this.SetExpression(ConstantExpression(this.expression));
  754. IF CheckIntegerValue(this.expression,value) THEN END;
  755. END;
  756. this.Resolved;
  757. position := this.position;
  758. RETURN TRUE
  759. ELSE RETURN FALSE
  760. END;
  761. END HasValue;
  762. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  763. VAR this: SyntaxTree.Modifier;
  764. BEGIN
  765. this := modifiers;
  766. WHILE this # NIL DO
  767. IF ~this.resolved THEN
  768. IF checkUse OR (this.expression = NIL) THEN
  769. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  770. ELSE
  771. this.SetExpression(ResolveExpression(this.expression));
  772. this.Resolved;
  773. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  774. END;
  775. END;
  776. this := this.nextModifier
  777. END;
  778. END CheckModifiers;
  779. (**
  780. resolve procedure type
  781. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  782. **)
  783. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  784. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  785. BEGIN
  786. IF TypeNeedsResolution(procedureType) THEN
  787. modifiers := procedureType.modifiers;
  788. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  789. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  790. procedureType.SetInterrupt(TRUE);
  791. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  792. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  793. IF useDarwinCCalls THEN (*fld*)
  794. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  795. ELSE
  796. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  797. END
  798. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  799. procedureType.SetNoReturn(TRUE);
  800. END;
  801. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  802. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  803. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  804. CheckModifiers(modifiers, TRUE);
  805. modifiers := procedureType.returnTypeModifiers;
  806. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  807. CheckModifiers(modifiers, TRUE);
  808. typeFixes.Add(procedureType,currentScope);
  809. procedureType.SetHasPointers(procedureType.isDelegate);
  810. procedureType.SetState(SyntaxTree.Resolved);
  811. END;
  812. resolvedType := ResolvedType(procedureType)
  813. END VisitProcedureType;
  814. (** check and resolve record type
  815. - check base type: must be record, math array or array-structured object type
  816. - check declarations
  817. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  818. **)
  819. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  820. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  821. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  822. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  823. hasPointers: BOOLEAN;
  824. modifiers: SyntaxTree.Modifier;
  825. value: LONGINT;
  826. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  827. BEGIN
  828. type := type.resolved;
  829. IF (type IS SyntaxTree.PointerType) &
  830. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  831. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  832. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  833. RETURN TRUE
  834. ELSE
  835. RETURN FALSE
  836. END;
  837. END IsPointerToRecord;
  838. BEGIN
  839. IF TypeNeedsResolution(x) THEN
  840. hasPointers := FALSE;
  841. modifiers := x.modifiers;
  842. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  843. CheckModifiers(modifiers, TRUE);
  844. IF x.baseType # NIL THEN
  845. position := x.baseType.position;
  846. baseType := ResolveType(x.baseType);
  847. resolved := baseType.resolved;
  848. hasPointers := hasPointers OR resolved.hasPointers;
  849. IF x.isObject THEN (* object *)
  850. ASSERT(x.pointerType # NIL);
  851. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  852. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  853. baseType := NIL
  854. ELSIF IsPointerToRecord(resolved,recordType) THEN
  855. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  856. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  857. ELSE
  858. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  859. END;
  860. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  861. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  862. ELSIF IsPointerToRecord(resolved,recordType) THEN
  863. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  864. ELSIF resolved IS SyntaxTree.RecordType THEN
  865. ELSE
  866. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  867. END;
  868. ELSE
  869. IF resolved IS SyntaxTree.RecordType THEN
  870. ELSE
  871. Error(position, Diagnostics.Invalid,"record does not extend record")
  872. END;
  873. END;
  874. x.SetBaseType(baseType);
  875. IF x.Level() > 15 THEN
  876. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  877. (* note:
  878. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  879. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  880. inheritance history of a type.
  881. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  882. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  883. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  884. *)
  885. END;
  886. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  887. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  888. END;
  889. END;
  890. Declarations(x.recordScope);
  891. ResolveArrayStructure(x);
  892. (* computation of sizes and offsets skipped -> done in backend / system *)
  893. recordBase := x.GetBaseRecord();
  894. IF recordBase = NIL THEN numberMethods := 0
  895. ELSE numberMethods := recordBase.recordScope.numberMethods
  896. END;
  897. isRealtime := TRUE;
  898. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  899. WHILE symbol # NIL DO
  900. IF symbol IS SyntaxTree.Variable THEN
  901. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  902. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  903. END;
  904. IF symbol IS SyntaxTree.Procedure THEN
  905. procedure := symbol(SyntaxTree.Procedure);
  906. IF procedure.super # NIL THEN
  907. procedure.SetMethodNumber(procedure.super.methodNumber)
  908. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  909. procedure.SetMethodNumber(numberMethods);
  910. INC(numberMethods);
  911. END;
  912. END;
  913. symbol := symbol.nextSymbol;
  914. END;
  915. IF isRealtime THEN x.SetRealtime(TRUE) END;
  916. x.recordScope.SetNumberMethods(numberMethods);
  917. (* TODO: is this needed anymore? *)
  918. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  919. Error(x.position,Diagnostics.Invalid,"object extends a record")
  920. END;
  921. IF (x.typeDeclaration = NIL) THEN
  922. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  923. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  924. (*
  925. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  926. AnonymousTypeDeclaration(x,name);
  927. *)
  928. ELSE
  929. AnonymousTypeDeclaration(x,"Anonymous");
  930. END;
  931. END;
  932. x.SetHasPointers(hasPointers);
  933. x.SetState(SyntaxTree.Resolved);
  934. END;
  935. resolvedType := ResolvedType(x);
  936. END VisitRecordType;
  937. (** check and resolve cell type
  938. - check base type: must be cell
  939. - check declarations
  940. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  941. **)
  942. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  943. VAR
  944. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  945. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  946. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  947. recordBase: SyntaxTree.RecordType;
  948. numberMethods, int: LONGINT;
  949. variable: SyntaxTree.Variable;
  950. v: SyntaxTree.Expression;
  951. str: Scanner.StringType;
  952. atype: SyntaxTree.ArrayType;
  953. prev: SyntaxTree.Scope;
  954. BEGIN
  955. IF TypeNeedsResolution(x) THEN
  956. recordBase := NIL;
  957. IF cellsAreObjects THEN
  958. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  959. ImportModule(qualifiedIdentifier.prefix, x.position);
  960. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  961. x.SetBaseType(ResolveType(x.baseType));
  962. recordBase := x.GetBaseRecord();
  963. IF recordBase = NIL THEN
  964. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  965. END;
  966. END;
  967. IF recordBase = NIL THEN numberMethods := 0
  968. ELSE numberMethods := recordBase.recordScope.numberMethods
  969. END;
  970. modifier := x.modifiers;
  971. (*IF ~x.isCellNet THEN*)
  972. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  973. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  974. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  975. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  976. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  977. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  978. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  979. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  980. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  981. symbol := system.activeCellsCapabilities;
  982. WHILE symbol # NIL DO
  983. IF HasFlag(modifier, symbol.name, position) THEN END;
  984. symbol := symbol.nextSymbol;
  985. END;
  986. modifier := x.modifiers;
  987. WHILE (modifier # NIL) DO
  988. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  989. IF modifier.expression # NIL THEN
  990. v := ConstantExpression(modifier.expression);
  991. property.SetValue(v);
  992. IF IsIntegerValue(modifier.expression, int) THEN
  993. (*property.SetValue(modifier.expression);*)
  994. property.SetType(system.longintType);
  995. ELSIF IsStringValue(modifier.expression, str) THEN
  996. (*property.SetValue(modifier.expression);*)
  997. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  998. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  999. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1000. property.SetType(atype);
  1001. ELSE
  1002. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1003. END;
  1004. ELSE (* flag property *)
  1005. ASSERT(modifier.resolved);
  1006. property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));
  1007. property.SetType(system.booleanType);
  1008. END;
  1009. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1010. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1011. x.AddProperty(property);
  1012. modifier := modifier.nextModifier;
  1013. END;
  1014. (*ELSE
  1015. (* no: this should not be allowed on cell net types or check against global scope ...
  1016. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1017. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1018. END;
  1019. END
  1020. *)
  1021. END;*)
  1022. CheckModifiers(modifier, FALSE);
  1023. Declarations(x.cellScope);
  1024. (* process parameters *)
  1025. prev := currentScope;
  1026. currentScope := x.cellScope;
  1027. parameter :=x.firstParameter;
  1028. WHILE (parameter # NIL) DO
  1029. VisitParameter(parameter);
  1030. type := parameter.type.resolved;
  1031. IF ~(type IS SyntaxTree.PortType) THEN
  1032. WHILE IsStaticArray(type, type, len) DO
  1033. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1034. END;
  1035. WHILE IsDynamicArray(type, type) DO
  1036. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1037. END;
  1038. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1039. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1040. END;
  1041. END;
  1042. parameter := parameter.nextParameter;
  1043. END;
  1044. currentScope := prev;
  1045. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1046. WHILE symbol # NIL DO
  1047. IF symbol IS SyntaxTree.Variable THEN
  1048. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1049. END;
  1050. symbol := symbol.nextSymbol;
  1051. END;
  1052. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1053. IF (x.typeDeclaration = NIL) THEN
  1054. AnonymousTypeDeclaration(x,"Anonymous");
  1055. END;
  1056. x.SetState(SyntaxTree.Resolved);
  1057. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1058. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1059. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1060. Warning(x.position, "Non-empty body for an engine?");
  1061. END;
  1062. END;
  1063. resolvedType := ResolvedType(x);
  1064. END VisitCellType;
  1065. (* check if an object is an array-structured object type
  1066. - determine the array structure
  1067. - collect operators from top to bottom in the inheritance hierarchy
  1068. - check if LEN operator is declared
  1069. - determine number of possible index operators
  1070. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1071. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1072. *)
  1073. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1074. VAR
  1075. indexOperatorCount, i: LONGINT;
  1076. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1077. isTensor: BOOLEAN;
  1078. BEGIN
  1079. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1080. (* determine array structure *)
  1081. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1082. END;
  1083. IF recordType.HasArrayStructure() THEN
  1084. (* the object is an ASOT *)
  1085. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1086. (* reset array access operators *)
  1087. arrayAccessOperators.len := NIL;
  1088. arrayAccessOperators.generalRead := NIL;
  1089. arrayAccessOperators.generalWrite := NIL;
  1090. IF isTensor THEN
  1091. (* all operators of dimensionalities 1 to max *)
  1092. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1093. ELSE
  1094. (* all operators of certain dimensionality *)
  1095. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1096. END;
  1097. NEW(arrayAccessOperators.read, indexOperatorCount);
  1098. NEW(arrayAccessOperators.write, indexOperatorCount);
  1099. FOR i := 0 TO indexOperatorCount - 1 DO
  1100. arrayAccessOperators.read[i] := NIL;
  1101. arrayAccessOperators.write[i] := NIL
  1102. END;
  1103. (* collect access operators in the record scope *)
  1104. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1105. IF arrayAccessOperators.len = NIL THEN
  1106. (* TODO: think about making this operator optional for static array structures *)
  1107. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1108. END;
  1109. (* show error messages *)
  1110. IF isTensor THEN
  1111. (* require ARRAY [*] OF RANGE *)
  1112. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1113. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1114. ELSE
  1115. (* forbid ARRAY [*] OF RANGE *)
  1116. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1117. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1118. (* require RANGE, RANGE, ... RANGE *)
  1119. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1120. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1121. END;
  1122. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1123. ELSE
  1124. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1125. IF recordType.recordScope.firstOperator # NIL THEN
  1126. RETURN;
  1127. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1128. END
  1129. END
  1130. END ResolveArrayStructure;
  1131. (** collect array access operators in a record scope **)
  1132. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1133. VAR
  1134. baseType: SyntaxTree.Type;
  1135. operator: SyntaxTree.Operator;
  1136. isReadOperator, isGeneralOperator: BOOLEAN;
  1137. indexListSize, indexListKind, hashValue: LONGINT;
  1138. BEGIN
  1139. (* if a parent record scope exists, collect the operators there first *)
  1140. baseType := recordScope.ownerRecord.baseType;
  1141. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1142. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1143. END;
  1144. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1145. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1146. END;
  1147. (* go through all operators in the current record scope *)
  1148. operator := recordScope.firstOperator;
  1149. WHILE operator # NIL DO
  1150. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1151. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1152. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1153. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1154. IF isGeneralOperator THEN
  1155. IF isReadOperator THEN
  1156. arrayAccessOperators.generalRead := operator
  1157. ELSE
  1158. arrayAccessOperators.generalWrite := operator
  1159. END
  1160. ELSE
  1161. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1162. IF isReadOperator THEN
  1163. arrayAccessOperators.read[hashValue] := operator
  1164. ELSE
  1165. arrayAccessOperators.write[hashValue] := operator
  1166. END
  1167. END
  1168. END
  1169. ELSE
  1170. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1171. END;
  1172. operator := operator.nextOperator
  1173. END
  1174. END CollectArrayAccessOperators;
  1175. (** the hash value of an index operator **)
  1176. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1177. VAR result: LONGINT;
  1178. BEGIN
  1179. IF isTensor THEN
  1180. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1181. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1182. ELSE
  1183. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1184. END
  1185. ELSE
  1186. result := indexListKind
  1187. END;
  1188. RETURN result
  1189. END IndexOperatorHash;
  1190. (** 2 to the power of exponent **)
  1191. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1192. VAR result, i: LONGINT;
  1193. BEGIN
  1194. result := 1;
  1195. FOR i := 1 TO exponent DO
  1196. result := result * 2;
  1197. END;
  1198. RETURN result
  1199. END TwoToThePowerOf;
  1200. (** check if a LEN operator has a correct signature. i.e.
  1201. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1202. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1203. **)
  1204. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1205. VAR
  1206. procedureType: SyntaxTree.ProcedureType;
  1207. returnedArrayType: SyntaxTree.MathArrayType;
  1208. result: BOOLEAN;
  1209. BEGIN
  1210. result := FALSE;
  1211. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1212. IF (procedureType.numberParameters = 0) THEN
  1213. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1214. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1215. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1216. IF returnedArrayType.form = SyntaxTree.Open THEN
  1217. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1218. result := TRUE
  1219. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1220. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1221. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1222. result := TRUE
  1223. END
  1224. END
  1225. END
  1226. END
  1227. END;
  1228. IF result THEN
  1229. (* export symbol automatically *)
  1230. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1231. ELSE
  1232. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1233. END;
  1234. RETURN result
  1235. END CheckLenOperator;
  1236. (** check if an index operator has a correct signature. i.e.
  1237. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1238. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1239. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1240. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1241. - determine if it is a read or write operator (existance of return type)
  1242. - check index parameters
  1243. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1244. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1245. [LONGINT] -> binary 0 -> 0
  1246. [RANGE] -> binary 1 -> 1
  1247. [LONGINT, LONGINT] -> binary 00 -> 0
  1248. [LONGINT, RANGE] -> binary 01 -> 1
  1249. [RANGE, LONGINT] -> binary 10 -> 2
  1250. [RANGE, RANGE] -> binary 11 -> 3
  1251. etc.
  1252. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1253. - for read operators, check if return type matches the type of data that is read
  1254. - for write operators, check if last parameter type matches the type of data that is written
  1255. **)
  1256. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1257. VAR
  1258. elementType, otherElementType, dataType: SyntaxTree.Type;
  1259. procedureType: SyntaxTree.ProcedureType;
  1260. mathArrayType: SyntaxTree.MathArrayType;
  1261. parameter: SyntaxTree.Parameter;
  1262. parameterCount, rangeCount, i: LONGINT;
  1263. hasTypeError: BOOLEAN;
  1264. BEGIN
  1265. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1266. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1267. (* determine if it is a read or write operator *)
  1268. isReadOperator := (procedureType.returnType # NIL);
  1269. IF isReadOperator THEN
  1270. indexListSize := parameterCount;
  1271. ELSE
  1272. indexListSize := parameterCount - 1;
  1273. END;
  1274. IF indexListSize < 1 THEN
  1275. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1276. RETURN FALSE
  1277. END;
  1278. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1279. (* general operator *)
  1280. isGeneralOperator := TRUE;
  1281. IF indexListSize > 1 THEN
  1282. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1283. RETURN FALSE
  1284. END;
  1285. (* ARRAY [*] OF RANGE*)
  1286. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1287. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1288. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1289. RETURN FALSE
  1290. END;
  1291. parameter := procedureType.firstParameter.nextParameter
  1292. ELSE
  1293. (* fixed-dim. operator *)
  1294. isGeneralOperator := FALSE;
  1295. (* check number of index parameters *)
  1296. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1297. (* for tensors, limited to a certain size *)
  1298. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1299. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1300. RETURN FALSE
  1301. END
  1302. ELSE
  1303. (* for non-tensors, depends on dimensionality *)
  1304. IF indexListSize # arrayStructure.Dimensionality() THEN
  1305. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1306. RETURN FALSE
  1307. END
  1308. END;
  1309. (* go through all index parameters
  1310. - count the number of ranges
  1311. - determine the index list kind number
  1312. *)
  1313. indexListKind := 0;
  1314. rangeCount := 0;
  1315. parameter := procedureType.firstParameter;
  1316. FOR i := 1 TO indexListSize DO
  1317. indexListKind := indexListKind * 2;
  1318. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1319. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1320. INC(indexListKind);
  1321. INC(rangeCount)
  1322. ELSE
  1323. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1324. RETURN FALSE
  1325. END;
  1326. parameter := parameter.nextParameter
  1327. END;
  1328. END;
  1329. (*
  1330. - for read operators: check type of last parameter
  1331. - for write operators: check return type
  1332. *)
  1333. IF isReadOperator THEN
  1334. dataType := procedureType.returnType (* the return type *)
  1335. ELSE
  1336. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1337. END;
  1338. elementType := arrayStructure.ElementType();
  1339. hasTypeError := FALSE;
  1340. IF isGeneralOperator THEN
  1341. (* ARRAY [?] OF <Element> *)
  1342. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1343. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1344. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1345. hasTypeError := TRUE
  1346. END
  1347. ELSE
  1348. hasTypeError := TRUE
  1349. END
  1350. ELSE
  1351. IF rangeCount = 0 THEN
  1352. (* <Element> *)
  1353. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1354. ELSE
  1355. (* ARRAY [*, *, ..., *] OF <Element> *)
  1356. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1357. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1358. IF mathArrayType.IsFullyDynamic() THEN
  1359. IF mathArrayType.Dimensionality() = rangeCount THEN
  1360. otherElementType := mathArrayType.ElementType();
  1361. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1362. ELSE
  1363. hasTypeError := TRUE
  1364. END
  1365. ELSE
  1366. hasTypeError := TRUE
  1367. END
  1368. ELSE
  1369. hasTypeError := TRUE
  1370. END
  1371. END
  1372. END;
  1373. IF hasTypeError THEN
  1374. IF isReadOperator THEN
  1375. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1376. ELSE
  1377. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1378. END;
  1379. RETURN FALSE
  1380. END;
  1381. (* export symbol automatically *)
  1382. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1383. RETURN TRUE
  1384. END CheckIndexOperator;
  1385. (** resolve all pending types (late resolving).
  1386. - type fixes are resolved at the end of the declaration phase
  1387. - type fixes may imply new type fixes that are also entered at the end of the list
  1388. **)
  1389. PROCEDURE FixTypes;
  1390. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1391. BEGIN
  1392. prevScope := currentScope;
  1393. p := typeFixes.Get(currentScope);
  1394. WHILE p # NIL DO
  1395. ASSERT(currentScope # NIL);
  1396. ASSERT(p IS SyntaxTree.Type);
  1397. IF p IS SyntaxTree.PointerType THEN
  1398. FixPointerType(p(SyntaxTree.PointerType))
  1399. ELSIF p IS SyntaxTree.ProcedureType THEN
  1400. FixProcedureType(p(SyntaxTree.ProcedureType))
  1401. ELSE
  1402. HALT(100);
  1403. END;
  1404. p := typeFixes.Get(currentScope);
  1405. END;
  1406. currentScope :=prevScope;
  1407. END FixTypes;
  1408. (**
  1409. resolve type x
  1410. - if x is nil then return nil
  1411. - if x cannot be resolved then the result is invalidType else the result is x
  1412. - the resolved type is entered into x.resolved
  1413. **)
  1414. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1415. VAR prev,resolved: SyntaxTree.Type;
  1416. BEGIN
  1417. prev := resolvedType;
  1418. resolvedType := SyntaxTree.invalidType;
  1419. IF x = NIL THEN resolvedType := NIL
  1420. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1421. END;
  1422. resolved := resolvedType;
  1423. resolvedType := prev;
  1424. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1425. RETURN resolved
  1426. END ResolveType;
  1427. (*** compatibility rules ***)
  1428. (**
  1429. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1430. **)
  1431. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1432. VAR result: SyntaxTree.Type;
  1433. BEGIN
  1434. result := SyntaxTree.invalidType;
  1435. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1436. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1437. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1438. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1439. ELSE result := type.resolved
  1440. END;
  1441. RETURN result
  1442. END RegularType;
  1443. (** returns signature compatibility of procedure types this and to
  1444. - if not compatible then error is reported
  1445. - compatibility means type equality
  1446. **)
  1447. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1448. VAR result: BOOLEAN;
  1449. BEGIN
  1450. result := SameType(to,this);
  1451. IF ~result THEN
  1452. Error(position,Diagnostics.Invalid,"signature incompatible");
  1453. IF VerboseErrorMessage THEN
  1454. Printout.Info("this",this);
  1455. Printout.Info("to",to);
  1456. END;
  1457. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1458. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1459. END;
  1460. RETURN result
  1461. END SignatureCompatible;
  1462. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1463. - for var parameters compatibility means same type except for
  1464. - formal is of open array of system byte
  1465. - formal is of record type
  1466. - formal is of open array type
  1467. - formal is of open math array type
  1468. - for value parameters compatibllity means assignment compatibility except for
  1469. - formal is of open array type
  1470. if compatible the return true else report error and return false
  1471. **)
  1472. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1473. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1474. BEGIN
  1475. formalType := RegularType(formal.position,formal.type);
  1476. actualType := RegularType(actual.position,actual.type);
  1477. error := FALSE;
  1478. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1479. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1480. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1481. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1482. END;
  1483. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1484. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1485. ELSIF ~IsVariable(actual) THEN
  1486. result := FALSE; error := TRUE;
  1487. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1488. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1489. ELSE
  1490. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1491. END;
  1492. IF VerboseErrorMessage THEN
  1493. Printout.Info("actual",actual);
  1494. Printout.Info("formal",formal);
  1495. END;
  1496. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1497. result := CompatibleTo(system,actualType,formalType);
  1498. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1499. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1500. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1501. IF IsArrayStructuredObjectType(actualType) THEN
  1502. actualType := MathArrayStructureOfType(actualType)
  1503. END;
  1504. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1505. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1506. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1507. END;
  1508. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1509. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1510. END;
  1511. ELSE
  1512. result := SameType(actualType,formalType)
  1513. END
  1514. ELSE
  1515. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1516. actualType := system.characterType;
  1517. END;
  1518. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1519. result := TRUE; (* special rule for WINAPI parameters *)
  1520. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1521. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1522. ELSE
  1523. result := CompatibleTo(system,actualType,formalType);
  1524. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1525. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1526. END;
  1527. END;
  1528. END;
  1529. IF ~result & ~error THEN
  1530. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1531. IF VerboseErrorMessage THEN
  1532. Printout.Info("actual",actual);
  1533. Printout.Info("formal",formal);
  1534. END;
  1535. END;
  1536. RETURN result
  1537. END ParameterCompatible;
  1538. (** check compatibility for expressions of the form left := right
  1539. - if compatible then return true else error report and return false
  1540. - check if left is variable
  1541. - check compatiblity
  1542. **)
  1543. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1544. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1545. BEGIN
  1546. result := FALSE;
  1547. leftType := RegularType(left.position,left.type);
  1548. rightType := RegularType(right.position,right.type);
  1549. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1550. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1551. END;
  1552. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1553. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1554. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1555. (* error already handled *)
  1556. result := TRUE;
  1557. ELSIF ~IsVariable(left) THEN
  1558. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1559. IF VerboseErrorMessage THEN
  1560. Printout.Info("left",left);
  1561. Printout.Info("right",right);
  1562. END;
  1563. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1564. result := TRUE;
  1565. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1566. result := TRUE
  1567. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1568. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1569. IF VerboseErrorMessage THEN
  1570. Printout.Info("left",left);
  1571. Printout.Info("right",right);
  1572. END;
  1573. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1574. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1575. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1576. ELSE
  1577. result := TRUE
  1578. END;
  1579. RETURN result
  1580. END AssignmentCompatible;
  1581. (*** values ***)
  1582. (** check and resolve integer value **)
  1583. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1584. VAR hugeint: HUGEINT;
  1585. BEGIN
  1586. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1587. value.SetType(Global.GetIntegerType(system,hugeint));
  1588. resolvedExpression := value
  1589. END VisitIntegerValue;
  1590. (** check and resolve real value **)
  1591. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1592. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1593. BEGIN
  1594. subtype := value(SyntaxTree.RealValue).subtype;
  1595. IF subtype = Scanner.Real THEN
  1596. type := system.realType
  1597. ELSIF subtype = Scanner.Longreal THEN
  1598. type := system.longrealType
  1599. ELSE
  1600. HALT(100)
  1601. END;
  1602. value.SetType(type);
  1603. resolvedExpression := value
  1604. END VisitRealValue;
  1605. (** check and resolve complex value **)
  1606. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1607. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1608. BEGIN
  1609. subtype := value(SyntaxTree.ComplexValue).subtype;
  1610. IF subtype = Scanner.Real THEN
  1611. type := system.complexType
  1612. ELSIF subtype = Scanner.Longreal THEN
  1613. type := system.longcomplexType
  1614. ELSE
  1615. HALT(100)
  1616. END;
  1617. value.SetType(type);
  1618. resolvedExpression := value
  1619. END VisitComplexValue;
  1620. (** check and resolve set value **)
  1621. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1622. BEGIN
  1623. value.SetType(system.setType);
  1624. resolvedExpression := value
  1625. END VisitSetValue;
  1626. (** check and resolve set value **)
  1627. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1628. BEGIN
  1629. value.SetType(SyntaxTree.invalidType);
  1630. resolvedExpression := value
  1631. END VisitMathArrayValue;
  1632. (** check and resolve boolean value **)
  1633. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1634. BEGIN
  1635. value.SetType(system.booleanType);
  1636. resolvedExpression := value
  1637. END VisitBooleanValue;
  1638. (** check and resolve string value **)
  1639. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1640. BEGIN
  1641. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1642. resolvedExpression := value
  1643. END VisitStringValue;
  1644. (** check and resolve character value **)
  1645. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1646. BEGIN
  1647. value.SetType(system.characterType);
  1648. resolvedExpression := value
  1649. END VisitCharacterValue;
  1650. (** check and resolve nil value **)
  1651. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1652. BEGIN
  1653. value.SetType(system.nilType);
  1654. resolvedExpression := value
  1655. END VisitNilValue;
  1656. (** check and resolve enumerator value **)
  1657. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1658. BEGIN
  1659. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1660. ASSERT(value.type # NIL);
  1661. resolvedExpression := value
  1662. END VisitEnumerationValue;
  1663. (*** expressions ***)
  1664. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1665. - check all elements on integer type
  1666. - if element range is constant, then check lower and upper bound
  1667. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1668. if an error occurs then report error and return invalidExpression
  1669. **)
  1670. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1671. VAR
  1672. i: LONGINT;
  1673. element: SyntaxTree.Expression;
  1674. constant: BOOLEAN;
  1675. elements: SyntaxTree.ExpressionList;
  1676. s: SET;
  1677. result: SyntaxTree.Expression;
  1678. value: SyntaxTree.Value;
  1679. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1680. VAR
  1681. left, right: SyntaxTree.Expression;
  1682. elementResult: SyntaxTree.Expression;
  1683. leftInteger, rightInteger: LONGINT;
  1684. BEGIN
  1685. (* set context of range *)
  1686. IF element IS SyntaxTree.RangeExpression THEN
  1687. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1688. END;
  1689. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1690. IF elementResult = SyntaxTree.invalidExpression THEN
  1691. (* error already reported *)
  1692. constant := FALSE
  1693. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1694. (* the element is a range expression *)
  1695. (* extract left and right hand side of range *)
  1696. left := elementResult(SyntaxTree.RangeExpression).first;
  1697. right := elementResult(SyntaxTree.RangeExpression).last;
  1698. (* guaranteed by VisitRangeExpression: *)
  1699. ASSERT((left # NIL) & (right # NIL));
  1700. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1701. ELSE
  1702. (* the element is not a range expression *)
  1703. (* check type and add conversion if needed *)
  1704. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1705. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1706. ELSE
  1707. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1708. elementResult := SyntaxTree.invalidExpression;
  1709. constant := FALSE
  1710. END;
  1711. left := elementResult;
  1712. right := elementResult
  1713. END;
  1714. IF elementResult # SyntaxTree.invalidExpression THEN
  1715. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1716. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1717. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1718. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1719. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1720. END
  1721. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1722. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1723. ELSE
  1724. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1725. s := s + {leftInteger..rightInteger};
  1726. (* same but more explicit:
  1727. WHILE (leftInteger <= rightInteger) DO
  1728. INCL(s,leftInteger);
  1729. INC(leftInteger);
  1730. END;
  1731. *)
  1732. END;
  1733. ELSE
  1734. constant := FALSE;
  1735. END
  1736. END;
  1737. RETURN elementResult
  1738. END CheckElement;
  1739. BEGIN
  1740. result := set; constant := TRUE; s := {}; elements := set.elements;
  1741. IF elements # NIL THEN
  1742. FOR i := 0 TO elements.Length()-1 DO
  1743. element := elements.GetExpression(i);
  1744. element := CheckElement(element);
  1745. IF element = SyntaxTree.invalidExpression THEN
  1746. result := SyntaxTree.invalidExpression
  1747. END;
  1748. elements.SetExpression(i,element);
  1749. END;
  1750. END;
  1751. IF constant THEN
  1752. value := SyntaxTree.NewSetValue(set.position,s);
  1753. value.SetType(system.setType);
  1754. result.SetResolved(value);
  1755. END;
  1756. (* optimization possible
  1757. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1758. left this to the programmer...
  1759. *)
  1760. result.SetType(system.setType);
  1761. resolvedExpression := result;
  1762. END VisitSet;
  1763. (*
  1764. old variant: quite generic but needs better conversion handling, do this?
  1765. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1766. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1767. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1768. BEGIN
  1769. type := NIL;
  1770. numberElements := x.elements.Length();
  1771. FOR i := 0 TO numberElements-1 DO
  1772. expression := x.elements.GetExpression(i);
  1773. position := expression.position;
  1774. expression := ResolveExpression(x.elements.GetExpression(i));
  1775. x.elements.SetExpression(i,de);
  1776. IF type = NIL THEN
  1777. type := expression.type;
  1778. ELSIF CompatibleTo(system,expression.type,type) THEN
  1779. (* ok *)
  1780. ELSIF CompatibleTo(system,type,expression.type) THEN
  1781. type := expression.type
  1782. ELSE
  1783. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1784. type := SyntaxTree.invalidType;
  1785. END;
  1786. END;
  1787. isValue := TRUE;
  1788. FOR i := 0 TO numberElements-1 DO
  1789. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1790. x.elements.SetExpression(i,expression);
  1791. isValue := isValue & (expression.resolved # NIL);
  1792. END;
  1793. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1794. arrayType.SetArrayBase(type);
  1795. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1796. IF isValue THEN
  1797. value := SyntaxTree.NewMathArrayValue(position);
  1798. value.SetElements(x.elements);
  1799. x.SetResolved(value);
  1800. END;
  1801. x.SetType(arrayType);
  1802. resolvedExpression := x;
  1803. END VisitMathArrayExpression;
  1804. *)
  1805. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1806. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1807. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1808. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1809. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1810. BEGIN
  1811. numberElements := x.elements.Length();
  1812. FOR i := 0 TO numberElements-1 DO
  1813. expression := x.elements.GetExpression(i);
  1814. IF expression IS SyntaxTree.MathArrayExpression THEN
  1815. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1816. ELSE
  1817. position := expression.position;
  1818. expression := ResolveExpression(x.elements.GetExpression(i));
  1819. x.elements.SetExpression(i,expression);
  1820. IF type = NIL THEN
  1821. type := expression.type;
  1822. ELSIF CompatibleTo(system,expression.type,type) THEN
  1823. (* ok *)
  1824. ELSIF CompatibleTo(system,type,expression.type) THEN
  1825. type := expression.type
  1826. ELSE
  1827. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1828. type := SyntaxTree.invalidType;
  1829. END;
  1830. END;
  1831. END;
  1832. END RecursivelyFindType;
  1833. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1834. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1835. BEGIN
  1836. numberElements := x.elements.Length();
  1837. FOR i := 0 TO numberElements-1 DO
  1838. expression := x.elements.GetExpression(i);
  1839. IF expression IS SyntaxTree.MathArrayExpression THEN
  1840. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1841. ELSE
  1842. position := expression.position;
  1843. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1844. x.elements.SetExpression(i,expression);
  1845. isValue := isValue & (expression.resolved # NIL);
  1846. END;
  1847. END;
  1848. END RecursivelySetExpression;
  1849. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1850. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1851. arrayType: SyntaxTree.MathArrayType;
  1852. BEGIN
  1853. numberElements := x.elements.Length();
  1854. baseType := NIL;
  1855. gsize := 0;
  1856. FOR i := 0 TO numberElements-1 DO
  1857. expression := x.elements.GetExpression(i);
  1858. IF expression IS SyntaxTree.MathArrayExpression THEN
  1859. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1860. IF i=0 THEN
  1861. gsize := size;
  1862. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1863. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1864. ELSE expression.SetType(baseType)
  1865. END;
  1866. ELSIF baseType = NIL THEN baseType := type;
  1867. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1868. END;
  1869. END;
  1870. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1871. arrayType.SetArrayBase(baseType);
  1872. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1873. RETURN ResolveType(arrayType);
  1874. END RecursivelySetType;
  1875. BEGIN
  1876. type := NIL;
  1877. RecursivelyFindType(x);
  1878. isValue := TRUE;
  1879. RecursivelySetExpression(x);
  1880. arrayType := RecursivelySetType(x);
  1881. x.SetType(arrayType);
  1882. IF isValue THEN
  1883. value := SyntaxTree.NewMathArrayValue(x.position);
  1884. value.SetArray(x);
  1885. x.SetResolved(value);
  1886. value.SetType(arrayType);
  1887. END;
  1888. x.SetType(arrayType);
  1889. resolvedExpression := x;
  1890. END VisitMathArrayExpression;
  1891. (** check and resolve unary expression **)
  1892. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1893. VAR
  1894. left: SyntaxTree.Expression;
  1895. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1896. bool: BOOLEAN;
  1897. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1898. value: SyntaxTree.Value;
  1899. BEGIN
  1900. type := SyntaxTree.invalidType;
  1901. left := ResolveExpression(unaryExpression.left);
  1902. unaryExpression.SetLeft(left);
  1903. operator := unaryExpression.operator;
  1904. result := unaryExpression;
  1905. IF ~system.operatorDefined[operator] THEN
  1906. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1907. RETURN
  1908. ELSIF left.type = NIL THEN
  1909. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1910. resolvedExpression := SyntaxTree.invalidExpression;
  1911. RETURN
  1912. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1913. RETURN
  1914. END;
  1915. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1916. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1917. END;
  1918. IF operatorCall # NIL THEN
  1919. result := operatorCall;
  1920. type := operatorCall.type;
  1921. (* admissible operators
  1922. Minus number, set
  1923. Not boolean
  1924. *)
  1925. ELSE
  1926. CASE unaryExpression.operator OF
  1927. |Scanner.Minus:
  1928. IF IsIntegerType(left.type.resolved) THEN
  1929. IF left.resolved # NIL THEN
  1930. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1931. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1932. result.SetResolved(value);
  1933. type := Global.GetIntegerType(system,int);
  1934. value.SetType(type);
  1935. ELSE
  1936. type := left.type
  1937. END
  1938. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1939. IF IsRealValue(left,real) THEN
  1940. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1941. result.SetResolved(value);
  1942. type := left.type;
  1943. value.SetType(type);
  1944. ELSE
  1945. type := left.type;
  1946. END;
  1947. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1948. IF IsSetValue(left,set) THEN
  1949. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1950. result.SetResolved(value);
  1951. type := left.type;
  1952. value.SetType(type);
  1953. ELSE
  1954. type := left.type;
  1955. END;
  1956. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1957. IF IsComplexValue(left, real, imaginary) THEN
  1958. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1959. result.SetResolved(value);
  1960. type := left.type;
  1961. value.SetType(type);
  1962. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1963. ELSE
  1964. type := left.type;
  1965. END
  1966. ELSE
  1967. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1968. END;
  1969. |Scanner.Not:
  1970. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1971. IF IsBooleanValue(left,bool) THEN
  1972. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1973. result.SetResolved(value);
  1974. type := system.booleanType;
  1975. value.SetType(type);
  1976. ELSE
  1977. type := system.booleanType;
  1978. END;
  1979. ELSE
  1980. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1981. END;
  1982. |Scanner.Plus:
  1983. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  1984. result := left; type := left.type;
  1985. ELSE
  1986. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1987. END;
  1988. (* ADDRESS OF *)
  1989. |Scanner.Address:
  1990. IF HasAddress(left) THEN
  1991. type := system.addressType;
  1992. ELSE
  1993. type := SyntaxTree.invalidType;
  1994. Error(left.position,Diagnostics.Invalid,"has no address");
  1995. Printout.Info("par", left);
  1996. END;
  1997. (* SIZE OF *)
  1998. |Scanner.Size:
  1999. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2000. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2001. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2002. value := SyntaxTree.NewIntegerValue(left.position, int);
  2003. result.SetResolved(value);
  2004. type := Global.GetIntegerType(system,int);
  2005. value.SetType(type)
  2006. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2007. ELSE
  2008. (* for variables, system sizeof could represent the physically occupied size
  2009. determined via the type descriptor, implement that ? *)
  2010. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2011. END
  2012. (* ALIAS OF *)
  2013. |Scanner.Alias:
  2014. type := left.type.resolved;
  2015. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2016. type := SyntaxTree.invalidType;
  2017. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2018. END;
  2019. ELSE
  2020. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2021. END;
  2022. END;
  2023. result.SetType(type);
  2024. resolvedExpression := result
  2025. END VisitUnaryExpression;
  2026. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2027. VAR
  2028. result: SyntaxTree.Expression;
  2029. array: SyntaxTree.MathArrayExpression;
  2030. value: SyntaxTree.MathArrayValue;
  2031. isValue: BOOLEAN;
  2032. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2033. BEGIN
  2034. type := type.resolved;
  2035. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2036. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2037. END;
  2038. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2039. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2040. END;
  2041. RETURN type
  2042. END BaseType;
  2043. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2044. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2045. BEGIN
  2046. numberElements := x.elements.Length();
  2047. FOR i := 0 TO numberElements-1 DO
  2048. expression := x.elements.GetExpression(i);
  2049. IF expression IS SyntaxTree.MathArrayExpression THEN
  2050. array := SyntaxTree.NewMathArrayExpression(position);
  2051. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2052. expression := array;
  2053. ELSE
  2054. position := expression.position;
  2055. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2056. isValue := isValue & (expression.resolved # NIL);
  2057. END;
  2058. to.elements.AddExpression(expression);
  2059. END;
  2060. END RecursivelyConvert;
  2061. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2062. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2063. arrayType: SyntaxTree.MathArrayType;
  2064. BEGIN
  2065. numberElements := x.elements.Length();
  2066. baseType := NIL;
  2067. gsize := 0;
  2068. FOR i := 0 TO numberElements-1 DO
  2069. expression := x.elements.GetExpression(i);
  2070. IF expression IS SyntaxTree.MathArrayExpression THEN
  2071. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2072. IF i=0 THEN
  2073. gsize := size;
  2074. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2075. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2076. ELSE expression.SetType(baseType)
  2077. END;
  2078. ELSIF baseType = NIL THEN baseType := type;
  2079. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2080. END;
  2081. END;
  2082. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2083. arrayType.SetArrayBase(baseType);
  2084. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2085. RETURN ResolveType(arrayType);
  2086. END RecursivelySetType;
  2087. BEGIN
  2088. result := SyntaxTree.invalidExpression;
  2089. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2090. result := expression (* do not convert *)
  2091. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2092. isValue := TRUE;
  2093. type := BaseType(type);
  2094. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2095. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2096. value := SyntaxTree.NewMathArrayValue(array.position);
  2097. value.SetArray(array);
  2098. value.SetType(RecursivelySetType(array));
  2099. result := value;
  2100. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2101. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2102. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2103. IF result = NIL THEN
  2104. result := SyntaxTree.invalidExpression;
  2105. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2106. IF VerboseErrorMessage THEN
  2107. Printout.Info("expression",expression);
  2108. Printout.Info("type",type);
  2109. END;
  2110. END;
  2111. END;
  2112. RETURN result
  2113. END MathArrayConversion;
  2114. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2115. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2116. BEGIN
  2117. result := expression; type := type.resolved;
  2118. IF (expression IS SyntaxTree.IntegerValue) THEN
  2119. int := expression(SyntaxTree.IntegerValue).hvalue;
  2120. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2121. int := Global.ConvertSigned(int,system.SizeOf(type));
  2122. result := SyntaxTree.NewIntegerValue(position,int);
  2123. result.SetType(type);
  2124. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2125. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2126. result := SyntaxTree.NewIntegerValue(position,int);
  2127. result.SetType(type);
  2128. ELSIF (type IS SyntaxTree.FloatType) THEN
  2129. result := SyntaxTree.NewRealValue(expression.position,int);
  2130. result.SetType(type);
  2131. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2132. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2133. result.SetType(type);
  2134. ELSIF (type IS SyntaxTree.SetType) THEN
  2135. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2136. result.SetType(type);
  2137. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2138. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2139. result.SetType(type);
  2140. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2141. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2142. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2143. END;
  2144. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2145. result.SetType(type);
  2146. ELSIF (type IS SyntaxTree.PortType) THEN
  2147. result := ConvertValue(position, expression, system.integerType);
  2148. ELSE
  2149. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2150. result := SyntaxTree.invalidExpression;
  2151. IF VerboseErrorMessage THEN
  2152. Printout.Info("expression",expression);
  2153. Printout.Info("type",type);
  2154. END;
  2155. END;
  2156. ELSIF IsRealValue(expression,real) THEN
  2157. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2158. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2159. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2160. result.SetType(type);
  2161. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2162. int := ENTIERH(real);
  2163. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2164. result.SetType(type);
  2165. ELSIF (type IS SyntaxTree.FloatType) THEN
  2166. result := SyntaxTree.NewRealValue(position,real);
  2167. result.SetType(type);
  2168. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2169. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2170. result.SetType(type);
  2171. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2172. ELSIF (type IS SyntaxTree.PortType) THEN
  2173. result := ConvertValue(position, expression, system.integerType);
  2174. ELSE
  2175. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2176. result := SyntaxTree.invalidExpression;
  2177. END
  2178. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2179. IF (type IS SyntaxTree.ComplexType) THEN
  2180. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2181. result.SetType(type);
  2182. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2183. ELSE
  2184. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2185. result := SyntaxTree.invalidExpression;
  2186. END
  2187. ELSIF IsSetValue(expression,set) THEN
  2188. IF (type IS SyntaxTree.IntegerType) THEN
  2189. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2190. result.SetType(type);
  2191. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2192. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2193. result.SetType(type);
  2194. ELSIF (type IS SyntaxTree.PortType) THEN
  2195. result := ConvertValue(position, expression, system.integerType);
  2196. ELSE
  2197. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2198. result := SyntaxTree.invalidExpression;
  2199. END;
  2200. ELSIF IsStringValue(expression,string) THEN
  2201. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2202. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2203. result.SetType(type);
  2204. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2205. ELSE
  2206. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2207. result := SyntaxTree.invalidExpression;
  2208. END;
  2209. ELSIF IsCharacterValue(expression,char) THEN
  2210. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2211. string[0] := char; string[1] := 0X;
  2212. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2213. result := SyntaxTree.NewStringValue(expression.position,string);
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.ByteType) THEN
  2216. (* do not simply set the new type as this could invalidate types of constants *)
  2217. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2218. result.SetType(type)
  2219. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2220. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2221. result.SetType(type);
  2222. ELSIF (type IS SyntaxTree.SetType) THEN
  2223. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2226. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.PortType) THEN
  2229. result := ConvertValue(position, expression, system.integerType);
  2230. ELSE
  2231. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2232. result := SyntaxTree.invalidExpression;
  2233. END;
  2234. ELSIF expression IS SyntaxTree.NilValue THEN
  2235. IF type IS SyntaxTree.AddressType THEN
  2236. result := SyntaxTree.NewIntegerValue(position,0);
  2237. result.SetType(type);
  2238. ELSE
  2239. result := expression;
  2240. END;
  2241. (* nothing to be done *)
  2242. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2243. result := MathArrayConversion(position, expression,type);
  2244. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2245. int := expression(SyntaxTree.EnumerationValue).value;
  2246. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2247. int := Global.ConvertSigned(int,system.SizeOf(type));
  2248. result := SyntaxTree.NewIntegerValue(position,int);
  2249. result.SetType(type);
  2250. ELSE
  2251. result := expression;
  2252. END;
  2253. (* nothing to be done *)
  2254. ELSE
  2255. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2256. IF VerboseErrorMessage THEN
  2257. Printout.Info("expression",expression);
  2258. Printout.Info("type",type);
  2259. END;
  2260. result := SyntaxTree.invalidExpression;
  2261. END;
  2262. RETURN result
  2263. END ConvertValue;
  2264. (**
  2265. return a conversion of an expression to a given type
  2266. - if expression is already of same type then return expression
  2267. - if incompatible conversion then report error and return invalidExpression
  2268. **)
  2269. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2270. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2271. BEGIN
  2272. type := type.resolved;
  2273. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2274. result := expression;
  2275. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2276. ELSIF expression = NIL THEN (* NIL expression *)
  2277. ELSIF expression.type = NIL THEN
  2278. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2279. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2280. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2281. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2282. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2283. ELSIF expression.resolved # NIL THEN (* value *)
  2284. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2285. IF value IS SyntaxTree.Value THEN
  2286. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2287. result.SetResolved(value(SyntaxTree.Value));
  2288. result.SetType(value.type);
  2289. ELSE
  2290. result := value
  2291. END;
  2292. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2293. expressionList := SyntaxTree.NewExpressionList();
  2294. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2295. typeDeclaration.SetDeclaredType(type);
  2296. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2297. typeSymbol.SetType(typeDeclaration.type);
  2298. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2299. expressionList.AddExpression(expression);
  2300. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2301. result.SetType(type);
  2302. ELSIF IsArrayStructuredObjectType(type) THEN
  2303. (* no type can be converted to an array-structured object type *)
  2304. HALT(100)
  2305. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2306. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2307. result := MathArrayConversion(position, expression,type);
  2308. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2309. expression := ConvertToMathArray(expression);
  2310. type := MathArrayStructureOfType(type);
  2311. result := MathArrayConversion(position, expression, type)
  2312. ELSE
  2313. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2314. END;
  2315. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2316. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2317. OR ~(type IS SyntaxTree.ArrayType) THEN
  2318. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2319. END;
  2320. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2321. (*skip, no conversion*)
  2322. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2323. (* skip, no conversion *)
  2324. ELSE
  2325. ASSERT(~(type IS SyntaxTree.RangeType));
  2326. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2327. ASSERT(type # NIL);
  2328. END;
  2329. RETURN result
  2330. END NewConversion;
  2331. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2332. BEGIN
  2333. IF CompatibleTo(system,expression.type, type) THEN
  2334. RETURN NewConversion(position, expression, type, NIL);
  2335. ELSE
  2336. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2337. RETURN SyntaxTree.invalidExpression
  2338. END;
  2339. END CompatibleConversion;
  2340. (**
  2341. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2342. **)
  2343. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2344. VAR leftType,rightType: SyntaxTree.Type;
  2345. BEGIN
  2346. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2347. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2348. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2349. (* error already handled *)
  2350. ELSE
  2351. leftType := left.type.resolved; rightType := right.type.resolved;
  2352. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2353. right := NewConversion(right.position, right, leftType, NIL);
  2354. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2355. left := NewConversion(left.position,left,rightType,NIL);
  2356. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2357. right := NewConversion(right.position, right, leftType, NIL);
  2358. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2359. left := NewConversion(left.position,left,rightType,NIL);
  2360. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2361. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2362. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2363. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2364. ELSIF
  2365. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2366. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2367. (* must be the case LONGREAL / COMPLEX ) *)
  2368. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2369. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2370. ELSE
  2371. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2372. END;
  2373. END;
  2374. END ConvertOperands;
  2375. (** find and return best operator matching to parameter list (nil, if none)
  2376. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2377. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2378. **)
  2379. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2380. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2381. identifier: SyntaxTree.Identifier;
  2382. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2383. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2384. BEGIN
  2385. operator := scope.firstOperator;
  2386. WHILE(operator # NIL) DO
  2387. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2388. procedureType := operator.type(SyntaxTree.ProcedureType);
  2389. distance := Distance(system, procedureType,actualParameters);
  2390. IF (distance < Infinity) THEN
  2391. IF returnType # NIL THEN
  2392. IF procedureType.returnType = NIL THEN
  2393. distance := Infinity
  2394. ELSE
  2395. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2396. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2397. END;
  2398. END;
  2399. END;
  2400. (*
  2401. IF distance < Infinity THEN
  2402. TRACE(distance, operator);
  2403. Printout.Info("potential operator",operator);
  2404. ELSE
  2405. Printout.Info("operator not possible",operator);
  2406. END;
  2407. *)
  2408. IF distance < bestDistance THEN
  2409. bestDistance := distance;
  2410. bestOperator := operator;
  2411. END;
  2412. END;
  2413. operator := operator.nextOperator;
  2414. END;
  2415. (*
  2416. Printout.Info("taken operator",bestOperator);
  2417. *)
  2418. END FindInScope;
  2419. BEGIN
  2420. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2421. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2422. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2423. import := currentScope.ownerModule.moduleScope.firstImport;
  2424. WHILE (bestDistance > 0) & (import # NIL) DO
  2425. IF import.module # NIL THEN
  2426. identifier := Global.GetIdentifier(operator,import.module.case);
  2427. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2428. END;
  2429. import := import.nextImport;
  2430. END;
  2431. RETURN bestOperator
  2432. END FindOperator;
  2433. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2434. BEGIN
  2435. currentScope := scope;
  2436. END SetCurrentScope;
  2437. (**
  2438. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2439. - handle LEN and DIM operator for array-structured object types
  2440. - find operator, if found then
  2441. - if in other module then add import designator
  2442. - create symbol designator for operator
  2443. - if error then return invalidExpression, if no operator then return NIL
  2444. **)
  2445. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2446. VAR
  2447. operator: SyntaxTree.Operator;
  2448. import: SyntaxTree.Import;
  2449. expression, result: SyntaxTree.Expression;
  2450. designator: SyntaxTree.Designator;
  2451. actualParameters, tempList: SyntaxTree.ExpressionList;
  2452. recordType: SyntaxTree.RecordType;
  2453. castReturnType : SyntaxTree.MathArrayType;
  2454. BEGIN
  2455. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2456. result := SyntaxTree.invalidExpression
  2457. ELSIF leftExpression = NIL THEN
  2458. result := NIL
  2459. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2460. (* LEN or DIM operator on array-structured object type *)
  2461. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2462. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2463. IF recordType.arrayAccessOperators.len = NIL THEN
  2464. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2465. result := SyntaxTree.invalidExpression
  2466. ELSE
  2467. ASSERT(leftExpression IS SyntaxTree.Designator);
  2468. designator := leftExpression(SyntaxTree.Designator);
  2469. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2470. ASSERT(expression IS SyntaxTree.Designator);
  2471. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2472. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2473. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2474. result := designator
  2475. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2476. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2477. tempList := SyntaxTree.NewExpressionList();
  2478. tempList.AddExpression(rightExpression);
  2479. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2480. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2481. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2482. tempList := SyntaxTree.NewExpressionList();
  2483. tempList.AddExpression(designator);
  2484. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2485. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2486. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2487. END
  2488. END;
  2489. ELSE
  2490. (* import OCArrayBase if needed *)
  2491. IF ~arrayBaseImported THEN
  2492. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2493. (* operators on complex numbers *)
  2494. ImportModule(Global.ArrayBaseName,position);
  2495. arrayBaseImported := TRUE;
  2496. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2497. IF op = Global.Dim THEN
  2498. (* not existing in OCArrayBase *)
  2499. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2500. (* not existing in OCArrayBase *)
  2501. ELSE
  2502. ImportModule(Global.ArrayBaseName,position);
  2503. arrayBaseImported := TRUE;
  2504. END
  2505. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2506. ImportModule(Global.ArrayBaseName,position);
  2507. arrayBaseImported := TRUE
  2508. END;
  2509. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2510. (* LEN(RANGE) *)
  2511. ImportModule(Global.ArrayBaseName,position);
  2512. arrayBaseImported := TRUE;
  2513. END;
  2514. END;
  2515. actualParameters := SyntaxTree.NewExpressionList();
  2516. actualParameters.AddExpression(leftExpression);
  2517. IF rightExpression # NIL THEN
  2518. actualParameters.AddExpression(rightExpression)
  2519. END;
  2520. operator := FindOperator(system,op,actualParameters,resultType);
  2521. IF operator # NIL THEN
  2522. designator := NIL;
  2523. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2524. import := currentScope.ownerModule.moduleScope.firstImport;
  2525. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2526. import := import.nextImport;
  2527. END;
  2528. expression := NewSymbolDesignator(position,NIL,import);
  2529. designator := expression(SyntaxTree.Designator);
  2530. END;
  2531. expression := NewSymbolDesignator(position,designator,operator);
  2532. designator := expression(SyntaxTree.Designator);
  2533. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2534. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2535. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2536. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2537. result.SetType(castReturnType);
  2538. END;
  2539. ELSE
  2540. result := NIL;
  2541. END;
  2542. END;
  2543. RETURN result
  2544. END NewOperatorCall;
  2545. (** check and resolve binary expression **)
  2546. (*! clean up *)
  2547. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2548. VAR left,right,result: SyntaxTree.Expression;
  2549. leftType, rightType: SyntaxTree.Type;
  2550. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2551. cl,cr: CHAR;
  2552. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2553. type: SyntaxTree.Type;
  2554. value: SyntaxTree.Value;
  2555. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2556. integerConstantFolding: BOOLEAN;
  2557. PROCEDURE NewBool(v: BOOLEAN);
  2558. BEGIN
  2559. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2560. value.SetType(system.booleanType);
  2561. result.SetResolved(value);
  2562. type := system.booleanType
  2563. END NewBool;
  2564. PROCEDURE NewSet(v: SET);
  2565. BEGIN
  2566. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2567. value.SetType(system.setType);
  2568. result.SetResolved(value);
  2569. type := system.setType;
  2570. END NewSet;
  2571. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2572. BEGIN
  2573. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2574. (* type cast to "larger" type only if the value is still in the range *)
  2575. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2576. value.SetType(t);
  2577. END;
  2578. result.SetResolved(value);
  2579. type := value.type;
  2580. END NewInteger;
  2581. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2582. BEGIN
  2583. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2584. value.SetType(t);
  2585. result.SetResolved(value);
  2586. type := t;
  2587. END NewReal;
  2588. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2589. BEGIN
  2590. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2591. value.SetType(t);
  2592. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2593. result.SetResolved(value);
  2594. type := t;
  2595. END NewComplex;
  2596. BEGIN
  2597. type := SyntaxTree.invalidType;
  2598. left := ResolveExpression(binaryExpression.left);
  2599. right := ResolveExpression(binaryExpression.right);
  2600. binaryExpression.SetLeft(left);
  2601. binaryExpression.SetRight(right);
  2602. result := binaryExpression;
  2603. operator := binaryExpression.operator;
  2604. IF ~system.operatorDefined[operator] THEN
  2605. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2606. result := SyntaxTree.invalidExpression;
  2607. RETURN
  2608. END;
  2609. IF left.type = NIL THEN
  2610. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2611. result := SyntaxTree.invalidExpression;
  2612. RETURN;
  2613. END;
  2614. IF right.type = NIL THEN
  2615. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2616. result := SyntaxTree.invalidExpression;
  2617. RETURN;
  2618. END;
  2619. leftType := left.type.resolved; rightType := right.type.resolved;
  2620. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2621. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2622. END;
  2623. IF operatorCall # NIL THEN
  2624. result := operatorCall;
  2625. type := operatorCall.type;
  2626. (* admissible operators:
  2627. Times, Plus, Minus numeric numeric numeric
  2628. set set set
  2629. Slash numeric numeric real /complex
  2630. set set set
  2631. Div , Mod integer integer integer
  2632. And, Or bool bool bool
  2633. Equal, Unequal basic basic bool
  2634. pointer pointer bool
  2635. object object bool
  2636. record record bool
  2637. string string bool
  2638. enumerator enumerator bool
  2639. Less, LessEqual,
  2640. Greater, GreaterEqual integer/real integer/real bool
  2641. enumerator enumerator bool
  2642. In integer set bool
  2643. Is pointer type bool
  2644. object type bool
  2645. record type bool
  2646. Upto: special abbreviation for a..b
  2647. *)
  2648. ELSIF (left.type = NIL) THEN
  2649. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2650. D.Str("nil type in "); D.Type(left); D.Ln;
  2651. result := SyntaxTree.invalidExpression;
  2652. ELSIF (right.type = NIL) THEN
  2653. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2654. result := SyntaxTree.invalidExpression;
  2655. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2656. result := SyntaxTree.invalidExpression;
  2657. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2658. HALT(100);
  2659. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2660. type := system.booleanType;
  2661. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2662. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2663. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2664. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2665. IF VerboseErrorMessage THEN
  2666. Printout.Info("left",left);
  2667. Printout.Info("right",right);
  2668. END;
  2669. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2670. NewBool(TRUE)
  2671. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2672. NewBool(TRUE);
  2673. ELSIF IsUnextensibleRecord(left) THEN
  2674. NewBool(FALSE)
  2675. END
  2676. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2677. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2678. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2679. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2680. ELSIF operator = Scanner.In THEN (* left IN right *)
  2681. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2682. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2683. NewBool(il IN sr);
  2684. ELSE
  2685. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2686. left := NewConversion(left.position, left, system.longintType,NIL);
  2687. binaryExpression.SetLeft(left)
  2688. END;
  2689. type := system.booleanType;
  2690. END
  2691. ELSE
  2692. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2693. END
  2694. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2695. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2696. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2697. END;
  2698. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2699. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2700. END
  2701. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2702. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2703. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2704. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2705. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2706. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2707. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2708. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2709. END;
  2710. type := system.booleanType;
  2711. ELSE
  2712. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2713. END
  2714. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2715. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2716. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2717. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2718. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2719. CASE operator OF
  2720. |Scanner.Equal: NewBool(strl^=strr^);
  2721. |Scanner.Unequal:NewBool(strl^#strr^);
  2722. |Scanner.Less: NewBool(strl^<strr^);
  2723. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2724. |Scanner.Greater: NewBool(strl^>strr^);
  2725. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2726. ELSE
  2727. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2728. END;
  2729. END;
  2730. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2731. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2732. type := system.booleanType
  2733. ELSE
  2734. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2735. END;
  2736. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2737. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2738. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2739. type := system.booleanType;
  2740. ELSE
  2741. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2742. END
  2743. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2744. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2745. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2746. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2747. type := system.booleanType
  2748. ELSE
  2749. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2750. END;
  2751. ELSE
  2752. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2753. END;
  2754. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2755. type := system.booleanType;
  2756. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2757. type := system.booleanType;
  2758. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2759. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2760. THEN
  2761. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2762. IF (leftType # rightType) THEN
  2763. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2764. ConvertOperands(left,right); (* operands must be of the same type here *)
  2765. END;
  2766. binaryExpression.SetLeft(left);
  2767. binaryExpression.SetRight(right);
  2768. leftType := left.type.resolved;
  2769. rightType := right.type.resolved;
  2770. END;
  2771. type := leftType;
  2772. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2773. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2774. IF VerboseErrorMessage THEN
  2775. Printout.Info("left",left);
  2776. Printout.Info("right",right);
  2777. END;
  2778. ELSIF IsIntegerType(leftType) THEN
  2779. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2780. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2781. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2782. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2783. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2784. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2785. END;
  2786. END;
  2787. (* constant folding *)
  2788. (* bootstrap64
  2789. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2790. CASE operator OF
  2791. |Scanner.Plus: NewInteger(il+ir,left.type);
  2792. |Scanner.Minus: NewInteger(il-ir,left.type);
  2793. |Scanner.Times: NewInteger(il*ir,left.type);
  2794. |Scanner.Slash:
  2795. IF ir # 0 THEN
  2796. NewReal(il/ir, system.realType);
  2797. END;
  2798. |Scanner.Mod:
  2799. IF ir > 0 THEN
  2800. NewInteger(il MOD ir,left.type);
  2801. END;
  2802. |Scanner.Div:
  2803. IF ir > 0 THEN
  2804. NewInteger(il DIV ir,left.type);
  2805. END;
  2806. |Scanner.Equal: NewBool(il=ir);
  2807. |Scanner.Unequal:NewBool(il#ir);
  2808. |Scanner.Less: NewBool(il<ir);
  2809. |Scanner.LessEqual: NewBool(il<=ir);
  2810. |Scanner.Greater: NewBool(il>ir);
  2811. |Scanner.GreaterEqual: NewBool(il>=ir);
  2812. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2813. END;
  2814. ELS*)
  2815. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2816. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2817. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2818. CASE operator OF
  2819. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2820. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2821. |Scanner.Times: NewInteger(hl*hr,left.type);
  2822. |Scanner.Slash:
  2823. IF hr = 0 THEN
  2824. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2825. ELSE
  2826. IF type.sizeInBits = 64 THEN
  2827. NewReal(hl/hr,system.longrealType);
  2828. ELSE
  2829. NewReal(hl/hr,system.realType)
  2830. END
  2831. END;
  2832. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2833. |Scanner.Mod:
  2834. IF hr = 0 THEN
  2835. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2836. ELSE
  2837. NewInteger(hl MOD hr, left.type);
  2838. (* bootstrap64
  2839. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2840. *)
  2841. END;
  2842. |Scanner.Div:
  2843. IF hr = 0 THEN
  2844. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2845. ELSE
  2846. NewInteger(hl DIV hr, left.type);
  2847. (* bootstrap64
  2848. NewInteger(Machine.DivH(hl,hr),left.type);
  2849. *)
  2850. END;
  2851. (* *)
  2852. |Scanner.Equal: NewBool(hl=hr);
  2853. |Scanner.Unequal: NewBool(hl#hr);
  2854. |Scanner.Less: NewBool(hl<hr);
  2855. |Scanner.LessEqual: NewBool(hl<=hr);
  2856. |Scanner.Greater: NewBool(hl>hr);
  2857. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2858. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2859. END;
  2860. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2861. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2862. type := left.type
  2863. ELSIF (operator = Scanner.Slash) THEN
  2864. left := NewConversion(left.position,left,system.realType,NIL);
  2865. right := NewConversion(right.position,right,system.realType,NIL);
  2866. binaryExpression.SetLeft(left);
  2867. binaryExpression.SetRight(right);
  2868. type := system.realType
  2869. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2870. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2871. type := system.booleanType
  2872. ELSE
  2873. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2874. END;
  2875. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2876. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2877. CASE operator OF
  2878. |Scanner.Plus: NewReal(rl+rr,leftType);
  2879. |Scanner.Minus: NewReal(rl-rr,leftType);
  2880. |Scanner.Times:NewReal(rl*rr,leftType);
  2881. |Scanner.Slash:
  2882. IF rr = 0 THEN
  2883. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2884. ELSE
  2885. NewReal(rl/rr,leftType);
  2886. END
  2887. |Scanner.Equal: NewBool(rl=rr);
  2888. |Scanner.Unequal: NewBool(rl#rr);
  2889. |Scanner.Less: NewBool(rl<rr);
  2890. |Scanner.LessEqual: NewBool(rl<=rr);
  2891. |Scanner.Greater: NewBool(rl>rr);
  2892. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2893. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2894. END;
  2895. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2896. type := left.type
  2897. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2898. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2899. type := system.booleanType
  2900. ELSE
  2901. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2902. IF VerboseErrorMessage THEN
  2903. Printout.Info("left",left);
  2904. Printout.Info("right",right);
  2905. END;
  2906. END;
  2907. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2908. CASE operator OF
  2909. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2910. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2911. ELSE
  2912. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2913. IF VerboseErrorMessage THEN
  2914. Printout.Info("left", left);
  2915. Printout.Info("right", right)
  2916. END;
  2917. END;
  2918. IF ~error THEN
  2919. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2920. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2921. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2922. (* do constant folding *)
  2923. CASE operator OF
  2924. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2925. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2926. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2927. |Scanner.Slash:
  2928. divisor := c * c + d * d;
  2929. ASSERT(divisor # 0);
  2930. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2931. |Scanner.Equal: NewBool((a = c) & (b = d))
  2932. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2933. END
  2934. END
  2935. END
  2936. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2937. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2938. CASE operator OF
  2939. |Scanner.And: NewBool(bl & br);
  2940. |Scanner.Or: NewBool(bl OR br);
  2941. |Scanner.Equal: NewBool(bl = br);
  2942. |Scanner.Unequal: NewBool(bl # br);
  2943. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2944. END;
  2945. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2946. type := system.booleanType
  2947. ELSE
  2948. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2949. END;
  2950. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2951. (* constant folding *)
  2952. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2953. IF operator = Scanner.Equal THEN
  2954. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2955. ELSIF operator = Scanner.Unequal THEN
  2956. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2957. END;
  2958. END;
  2959. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2960. type := system.booleanType;
  2961. ELSE
  2962. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2963. END;
  2964. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2965. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2966. CASE operator OF
  2967. |Scanner.Plus: NewSet(sl + sr);
  2968. |Scanner.Minus: NewSet(sl - sr);
  2969. |Scanner.Times: NewSet(sl * sr);
  2970. |Scanner.Slash: NewSet(sl / sr);
  2971. |Scanner.Equal: NewBool(sl=sr);
  2972. |Scanner.Unequal: NewBool(sl#sr);
  2973. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2974. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2975. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2976. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2977. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  2978. END;
  2979. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2980. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2981. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2982. THEN
  2983. type := system.booleanType
  2984. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2985. type := left.type
  2986. ELSE
  2987. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  2988. END;
  2989. ELSIF IsCharacterType(left.type) THEN
  2990. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  2991. CASE operator OF
  2992. |Scanner.Equal: NewBool(cl=cr);
  2993. |Scanner.Unequal: NewBool(cl#cr);
  2994. |Scanner.Less: NewBool(cl<cr);
  2995. |Scanner.LessEqual: NewBool(cl<=cr);
  2996. |Scanner.Greater: NewBool(cl>cr);
  2997. |Scanner.GreaterEqual: NewBool(cl>=cr);
  2998. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  2999. END;
  3000. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3001. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3002. type := system.booleanType
  3003. ELSE
  3004. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3005. END;
  3006. ELSE
  3007. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3008. END;
  3009. ELSE
  3010. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3011. END;
  3012. IF type = SyntaxTree.invalidType THEN
  3013. result := SyntaxTree.invalidExpression
  3014. ELSE
  3015. result.SetType(type)
  3016. END;
  3017. resolvedExpression := result
  3018. END VisitBinaryExpression;
  3019. (** resolve a range expression of the from <<first .. last BY step>>
  3020. - depending on the context different things are checked:
  3021. ArrayIndex:
  3022. - components must be integers
  3023. - replace missing lower bound with 0
  3024. - replace missing upper bound with MAX(LONGINT)
  3025. - replace missing step size with 1
  3026. SetElement:
  3027. - components must be integers
  3028. - replace missing lower bound with 0
  3029. - replace missing upper bound with MAX(SET)
  3030. - must not have step size
  3031. CaseGuard:
  3032. - components must be constant
  3033. - components must be integers or characters
  3034. - must have lower and upper bound present
  3035. - components are made compatible
  3036. - must not have step size
  3037. - if error: return invalidExpression
  3038. **)
  3039. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3040. VAR
  3041. hasError: BOOLEAN;
  3042. first, last, step: SyntaxTree.Expression;
  3043. BEGIN
  3044. hasError := FALSE;
  3045. first := x.first;
  3046. last := x.last;
  3047. step := x.step;
  3048. (* check lower bound *)
  3049. IF x.context = SyntaxTree.CaseGuard THEN
  3050. IF first = NIL THEN
  3051. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3052. hasError := TRUE
  3053. ELSE
  3054. first := ResolveExpression(first);
  3055. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3056. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3057. hasError := TRUE
  3058. ELSE
  3059. IF first IS SyntaxTree.StringValue THEN
  3060. (* add conversion from string to character *)
  3061. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3062. END
  3063. END;
  3064. (* check if expression is constant *)
  3065. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3066. (* error already reported *)
  3067. hasError := TRUE
  3068. END
  3069. END
  3070. ELSE (* ArrayIndex, SetElement *)
  3071. IF first = NIL THEN
  3072. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3073. END;
  3074. first := ResolveExpression(first);
  3075. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3076. first := NewConversion(first.position, first, system.longintType, NIL)
  3077. ELSE
  3078. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3079. hasError := TRUE
  3080. END
  3081. END;
  3082. (* check upper bound *)
  3083. IF x.context = SyntaxTree.CaseGuard THEN
  3084. IF last = NIL THEN
  3085. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3086. hasError := TRUE
  3087. ELSE
  3088. last := ResolveExpression(last);
  3089. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3090. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3091. hasError := TRUE
  3092. ELSE
  3093. IF last IS SyntaxTree.StringValue THEN
  3094. (* add conversion from string to character *)
  3095. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3096. END
  3097. END;
  3098. (* check if expression is constant *)
  3099. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3100. (* error already reported *)
  3101. hasError := TRUE
  3102. ELSE
  3103. (* try to make lower and upper bound compatible *)
  3104. ConvertOperands(first, last);
  3105. IF first.type.resolved # last.type.resolved THEN
  3106. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3107. hasError := TRUE
  3108. END
  3109. END
  3110. END
  3111. ELSE (* ArrayIndex, SetElement *)
  3112. IF last = NIL THEN
  3113. IF x.context = SyntaxTree.ArrayIndex THEN
  3114. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3115. ELSE
  3116. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3117. END
  3118. END;
  3119. last := ResolveExpression(last);
  3120. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3121. last := NewConversion(last.position, last, system.longintType, NIL)
  3122. ELSE
  3123. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3124. hasError := TRUE
  3125. END
  3126. END;
  3127. (* check step size *)
  3128. IF x.context = SyntaxTree.ArrayIndex THEN
  3129. IF step = NIL THEN
  3130. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3131. END;
  3132. step := ResolveExpression(step);
  3133. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3134. step := NewConversion(step.position, step, system.longintType, NIL)
  3135. ELSE
  3136. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3137. hasError := TRUE
  3138. END
  3139. ELSE (* SetElement, CaseGuard *)
  3140. IF step # NIL THEN
  3141. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3142. hasError := TRUE
  3143. END
  3144. END;
  3145. IF hasError THEN
  3146. resolvedExpression := SyntaxTree.invalidExpression
  3147. ELSE
  3148. x.SetFirst(first);
  3149. x.SetLast(last);
  3150. x.SetStep(step);
  3151. x.SetType(system.rangeType);
  3152. resolvedExpression := x;
  3153. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3154. END
  3155. END VisitRangeExpression;
  3156. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3157. BEGIN
  3158. x.SetType(NIL);
  3159. resolvedExpression := x;
  3160. END VisitTensorRangeExpression;
  3161. (** resolve the expression d and return result as designator
  3162. - resolve expression
  3163. - if expression is a designator then return designator else error message and return invalidDesignator
  3164. **)
  3165. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3166. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3167. BEGIN
  3168. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3169. resolved := ResolveExpression(d);
  3170. IF resolved = SyntaxTree.invalidExpression THEN
  3171. (* error should already have been reported *)
  3172. result := SyntaxTree.invalidDesignator;
  3173. ELSIF resolved IS SyntaxTree.Designator THEN
  3174. result := resolved(SyntaxTree.Designator);
  3175. ELSE
  3176. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3177. result := SyntaxTree.invalidDesignator;
  3178. END;
  3179. (* result.type might be nil. *)
  3180. RETURN result
  3181. END ResolveDesignator;
  3182. (**
  3183. symbol designator generated in this module
  3184. nothing to be resolved
  3185. **)
  3186. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3187. BEGIN
  3188. resolvedExpression := x;
  3189. END VisitSymbolDesignator;
  3190. (**
  3191. self designator generated in this module
  3192. nothing to be resolved
  3193. **)
  3194. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3195. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3196. BEGIN
  3197. (* check if in record scope *)
  3198. scope := currentScope;
  3199. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3200. scope := scope.outerScope;
  3201. END;
  3202. IF scope = NIL THEN (* in module scope *)
  3203. x.SetType(system.anyType);
  3204. ELSIF scope IS SyntaxTree.CellScope THEN
  3205. cell := scope(SyntaxTree.CellScope).ownerCell;
  3206. x.SetType(cell);
  3207. ELSE (* in record scope *)
  3208. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3209. IF (record # NIL) & (record.pointerType # NIL) THEN
  3210. type := ResolveType(record.pointerType);
  3211. x.SetType(type);
  3212. ELSE
  3213. x.SetType(record);
  3214. END;
  3215. END;
  3216. resolvedExpression := x;
  3217. END VisitSelfDesignator;
  3218. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3219. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3220. BEGIN
  3221. scope := currentScope;
  3222. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3223. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3224. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3225. returnType := procedureType.returnType;
  3226. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3227. THEN
  3228. x.SetType(returnType);
  3229. ELSE
  3230. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3231. x.SetType(SyntaxTree.invalidType);
  3232. END;
  3233. ELSE
  3234. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3235. x.SetType(SyntaxTree.invalidType);
  3236. END;
  3237. x.SetAssignable(TRUE);
  3238. resolvedExpression := x;
  3239. END VisitResultDesignator;
  3240. (**
  3241. return symbol designator as an expression
  3242. - if symbol is a constant then return the constant value expression
  3243. - else
  3244. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3245. - if symbol is a guarded variable then return a TypeGuardDesignator
  3246. - else return a symbol designator
  3247. **)
  3248. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3249. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3250. guardType: SyntaxTree.Type;
  3251. BEGIN
  3252. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3253. result := SyntaxTree.invalidExpression;
  3254. ASSERT(symbol # NIL);
  3255. (*
  3256. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3257. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3258. Error(position,Diagnostics.Invalid,"type not allowed here");
  3259. ELS *)
  3260. (* not needed any more as values are stored in the expression
  3261. IF symbol IS SyntaxTree.Constant THEN
  3262. result := symbol(SyntaxTree.Constant).value
  3263. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3264. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3265. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3266. ELSE
  3267. result := symbol(SyntaxTree.Constant).value
  3268. END;
  3269. ELSE
  3270. *)
  3271. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3272. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3273. THEN
  3274. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3275. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3276. left := NewDereferenceDesignator(position,left);
  3277. left.SetHidden(TRUE);
  3278. END;
  3279. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3280. scope := currentScope;
  3281. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3282. scope := scope.outerScope;
  3283. END;
  3284. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3285. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3286. END;
  3287. END;
  3288. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3289. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3290. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3291. ELSE
  3292. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3293. END;
  3294. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3295. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3296. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3297. result.SetType(symbol.type);
  3298. result.SetAssignable(assignable);
  3299. symbol.MarkUsed;
  3300. IF symbol IS SyntaxTree.Constant THEN
  3301. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3302. END;
  3303. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3304. variableAccessed := TRUE
  3305. END;
  3306. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3307. IF GetGuard(symbol,guardType) THEN
  3308. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType);
  3309. END;
  3310. END;
  3311. ASSERT(result.type # NIL);
  3312. RETURN result
  3313. END NewSymbolDesignator;
  3314. (** check and resolve an identifier designator "identifier"
  3315. - if identifier = self then return SelfDesignator
  3316. - else find symbol in current scope
  3317. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3318. **)
  3319. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3320. VAR symbol: SyntaxTree.Symbol;
  3321. BEGIN
  3322. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3323. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3324. IF symbol # NIL THEN
  3325. ResolveSymbol(symbol);
  3326. ASSERT(symbol.type # NIL);
  3327. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3328. ELSE
  3329. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3330. IF VerboseErrorMessage THEN
  3331. Printout.Info("undeclared identifier designator",identifierDesignator);
  3332. END;
  3333. resolvedExpression := SyntaxTree.invalidDesignator;
  3334. END;
  3335. END VisitIdentifierDesignator;
  3336. (** check and resolve a selector designator of the form left.designator
  3337. - if left is a pointer type then do auto dereferenciation
  3338. - left denotes a search scope:
  3339. - if left type is import type then set search scope to respective module
  3340. - if left type is enumeration type then set search scope to respective enumeration scope
  3341. - elsif left type is record type then set search scope to record scope
  3342. - search symbol in computed scope
  3343. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3344. **)
  3345. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3346. VAR
  3347. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3348. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3349. BEGIN
  3350. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3351. left := ResolveDesignator(selectorDesignator.left);
  3352. result := SyntaxTree.invalidDesignator;
  3353. IF left # NIL THEN
  3354. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3355. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3356. END;
  3357. scope := NIL;
  3358. IF left.type = NIL THEN
  3359. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3360. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3361. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3362. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3363. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3364. module := symbol(SyntaxTree.Import).module;
  3365. IF module # NIL THEN
  3366. scope := module.moduleScope
  3367. ELSE
  3368. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3369. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3370. END;
  3371. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3372. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3373. ASSERT(scope # NIL)
  3374. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3375. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3376. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3377. IF type IS SyntaxTree.EnumerationType THEN
  3378. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3379. ELSE
  3380. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3381. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3382. END;
  3383. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3384. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3385. ELSE
  3386. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3387. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3388. END;
  3389. symbol := NIL;
  3390. IF scope # NIL THEN
  3391. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3392. IF symbol # NIL THEN
  3393. ResolveSymbol(symbol);
  3394. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3395. symbol.MarkUsed
  3396. ELSE
  3397. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3398. IF VerboseErrorMessage THEN
  3399. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3400. Printout.Info("scope", scope);
  3401. Printout.Info("left", left);
  3402. Printout.Info("undeclared identifier",selectorDesignator);
  3403. Printout.Info("left resolved designator",left);
  3404. END
  3405. END;
  3406. END;
  3407. END;
  3408. resolvedExpression := result;
  3409. END VisitSelectorDesignator;
  3410. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3411. VAR len,idx: LONGINT;
  3412. BEGIN
  3413. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3414. IF idx < 0 THEN
  3415. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3416. ELSE
  3417. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3418. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3419. END;
  3420. END;
  3421. END;
  3422. END IndexCheck;
  3423. (*
  3424. - if index designator has not type, use newBaseType as its type
  3425. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3426. - special rule: if static array of dynamic array occurs, make it all dynamic
  3427. index designator type: new base type: new index designator type:
  3428. NIL z z
  3429. ARRAY [x, y] z ARRAY [x, y] OF z
  3430. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3431. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3432. *)
  3433. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3434. VAR
  3435. mathArrayType: SyntaxTree.MathArrayType;
  3436. makeDynamic: BOOLEAN;
  3437. BEGIN
  3438. IF indexDesignator.type = NIL THEN
  3439. indexDesignator.SetType(newBaseType)
  3440. ELSE
  3441. (* index designator must be a of math array type *)
  3442. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3443. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3444. (* determine if all arrays have to be made dynamic *)
  3445. makeDynamic :=
  3446. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3447. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3448. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3449. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3450. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3451. END;
  3452. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3453. mathArrayType.SetArrayBase(newBaseType)
  3454. END
  3455. END SetIndexBaseType;
  3456. (** check and append index list element to index designator of math array
  3457. - check validity of single index or array range
  3458. - compute new type
  3459. - if range then create new array type (calculate length of resulting array)
  3460. - otherwise take sourceArray.arrayBase as new type
  3461. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3462. **)
  3463. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3464. VAR
  3465. targetArray: SyntaxTree.MathArrayType;
  3466. first, last, step: SyntaxTree.Expression;
  3467. firstValue, lastValue, stepValue, length: LONGINT;
  3468. rangeExpression: SyntaxTree.RangeExpression;
  3469. BEGIN
  3470. IF indexListItem.type = SyntaxTree.invalidType THEN
  3471. (* error already handled *)
  3472. indexDesignator.parameters.AddExpression(indexListItem)
  3473. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3474. indexDesignator.HasRange;
  3475. indexDesignator.HasTensorRange;
  3476. indexDesignator.parameters.AddExpression(indexListItem);
  3477. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3478. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3479. IndexCheck(indexListItem, sourceArray.length);
  3480. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3481. indexDesignator.parameters.AddExpression(indexListItem)
  3482. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3483. indexDesignator.HasRange;
  3484. (* if the range is given as an array range expression, check the validity of its components *)
  3485. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3486. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3487. first := rangeExpression.first;
  3488. last := rangeExpression.last;
  3489. step := rangeExpression.step;
  3490. (* perform static checks on range components *)
  3491. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3492. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3493. END;
  3494. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3495. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3496. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3497. END
  3498. END;
  3499. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3500. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3501. END;
  3502. (* add conversions to size type *)
  3503. (* TODO: needed? *)
  3504. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3505. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3506. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3507. END;
  3508. IF indexDesignator.hasTensorRange THEN
  3509. (* the index designator's base type is a tensor: leave it as is *)
  3510. ELSE
  3511. (* append a new math array to the index designator's base type *)
  3512. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3513. IF ~error THEN
  3514. (*
  3515. (* optimization: calculate length of target array for static ranges *)
  3516. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3517. IF IsStaticallyOpenRange(rangeExpression) THEN
  3518. (* range is open ('*'): reuse source array length as target array length *)
  3519. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3520. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3521. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3522. IF lastValue = MAX(LONGINT) THEN
  3523. IF IsIntegerValue(sourceArray.length, length) THEN
  3524. lastValue := length - 1;
  3525. isStaticTargetArrayLength := TRUE
  3526. ELSE
  3527. isStaticTargetArrayLength := FALSE
  3528. END
  3529. ELSE
  3530. isStaticTargetArrayLength := TRUE
  3531. END;
  3532. IF isStaticTargetArrayLength THEN
  3533. (* calculate static target array length *)
  3534. IF firstValue > lastValue THEN
  3535. length := 0
  3536. ELSE
  3537. length := 1 + lastValue - firstValue;
  3538. IF length MOD stepValue = 0 THEN
  3539. length := length DIV stepValue
  3540. ELSE
  3541. length := length DIV stepValue + 1
  3542. END
  3543. END;
  3544. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3545. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3546. ASSERT(targetArray.form = SyntaxTree.Static)
  3547. END
  3548. END
  3549. END
  3550. *)
  3551. END;
  3552. SetIndexBaseType(indexDesignator, targetArray)
  3553. END;
  3554. indexDesignator.parameters.AddExpression(indexListItem)
  3555. ELSE
  3556. Error(position, Diagnostics.Invalid,"invalid index list item");
  3557. END;
  3558. END AppendMathIndex;
  3559. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3560. VAR parameters: SyntaxTree.ExpressionList;
  3561. BEGIN
  3562. parameters := index.parameters;
  3563. IF (expression.type = NIL) THEN
  3564. Error(position,Diagnostics.Invalid,"invalid index");
  3565. ELSIF IsIntegerType(expression.type.resolved) THEN
  3566. IF over IS SyntaxTree.ArrayType THEN
  3567. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3568. ELSIF over IS SyntaxTree.StringType THEN
  3569. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3570. END;
  3571. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3572. parameters.AddExpression(expression);
  3573. ELSE
  3574. Error(position,Diagnostics.Invalid,"invalid index");
  3575. END;
  3576. END AppendIndex;
  3577. (** convert an expression to math array type
  3578. if expression is of math array type: return expression itself
  3579. if expression is of array-structured object type: return an index operator call on it
  3580. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3581. otherwise: return invalid expression
  3582. **)
  3583. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3584. VAR
  3585. result: SyntaxTree.Expression;
  3586. mathArrayType: SyntaxTree.MathArrayType;
  3587. BEGIN
  3588. IF expression.type = NIL THEN
  3589. result := SyntaxTree.invalidExpression
  3590. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3591. (* expression of math array type *)
  3592. result := expression
  3593. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3594. (* expression of array-structured object type *)
  3595. mathArrayType := MathArrayStructureOfType(expression.type);
  3596. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3597. ELSE
  3598. result := SyntaxTree.invalidExpression
  3599. END;
  3600. RETURN result
  3601. END ConvertToMathArray;
  3602. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3603. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3604. VAR
  3605. result: SyntaxTree.ExpressionList;
  3606. i: LONGINT;
  3607. BEGIN
  3608. result := SyntaxTree.NewExpressionList();
  3609. FOR i := 1 TO itemCount DO
  3610. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3611. END;
  3612. RETURN result
  3613. END ListOfOpenRanges;
  3614. (** create a procedure call designator for an index operator call on an array-structured object type
  3615. - use given index list as actual parameters
  3616. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3617. **)
  3618. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3619. VAR
  3620. operator: SyntaxTree.Operator;
  3621. expression: SyntaxTree.Expression;
  3622. actualParameters, tempList: SyntaxTree.ExpressionList;
  3623. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3624. result, tempDesignator: SyntaxTree.Designator;
  3625. recordType: SyntaxTree.RecordType;
  3626. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3627. i, hashValue, indexListSize, indexListKind: LONGINT;
  3628. castReturnType: SyntaxTree.MathArrayType;
  3629. BEGIN
  3630. ASSERT(IsArrayStructuredObjectType(left.type));
  3631. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3632. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3633. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3634. indexListSize := indexList.Length();
  3635. indexListKind := 0;
  3636. containsNonRange := FALSE;
  3637. FOR i := 0 TO indexList.Length() - 1 DO
  3638. indexListKind := indexListKind * 2;
  3639. expression := indexList.GetExpression(i);
  3640. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3641. INC(indexListKind)
  3642. ELSE
  3643. containsNonRange := TRUE
  3644. END
  3645. END;
  3646. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3647. (* select applicable index operator
  3648. - try to look up optimal index operator
  3649. - if not present, use operator on ranges
  3650. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3651. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3652. *)
  3653. usesGeneralOperator := FALSE;
  3654. IF rhs # NIL THEN
  3655. (* write operator *)
  3656. IF hashValue = -1 THEN
  3657. operator := NIL
  3658. ELSE
  3659. operator := recordType.arrayAccessOperators.write[hashValue];
  3660. END;
  3661. IF operator = NIL THEN
  3662. usesPureRangeOperator := TRUE;
  3663. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3664. operator := recordType.arrayAccessOperators.generalWrite;
  3665. usesGeneralOperator := TRUE
  3666. ELSE
  3667. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3668. operator := recordType.arrayAccessOperators.write[hashValue];
  3669. END
  3670. END
  3671. ELSE
  3672. (* read operator *)
  3673. IF hashValue = -1 THEN
  3674. operator := NIL
  3675. ELSE
  3676. operator := recordType.arrayAccessOperators.read[hashValue];
  3677. END;
  3678. IF operator = NIL THEN
  3679. usesPureRangeOperator := TRUE;
  3680. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3681. operator := recordType.arrayAccessOperators.generalRead;
  3682. usesGeneralOperator := TRUE
  3683. ELSE
  3684. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3685. operator := recordType.arrayAccessOperators.read[hashValue];
  3686. END
  3687. END
  3688. END;
  3689. IF operator = NIL THEN
  3690. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3691. result := SyntaxTree.invalidDesignator;
  3692. ELSE
  3693. (* determine if reshaping is needed *)
  3694. needsReshaping := containsNonRange & usesPureRangeOperator;
  3695. (* import OCArrayBase if reshaping is needed *)
  3696. IF needsReshaping & ~arrayBaseImported THEN
  3697. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3698. arrayBaseImported := TRUE
  3699. END;
  3700. (* add the index list item to the list of actual parameters
  3701. - for general operators: add a single inline array containing the index list items as parameter
  3702. - otherwise: add all index list items as individual parameters
  3703. *)
  3704. actualParameters := SyntaxTree.NewExpressionList();
  3705. IF usesGeneralOperator THEN
  3706. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3707. END;
  3708. FOR i := 0 TO indexListSize - 1 DO
  3709. expression := indexList.GetExpression(i);
  3710. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3711. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3712. tempList := SyntaxTree.NewExpressionList();
  3713. tempList.AddExpression(expression);
  3714. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3715. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3716. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3717. END;
  3718. IF usesGeneralOperator THEN
  3719. tempMathArrayExpression.elements.AddExpression(expression);
  3720. ELSE
  3721. actualParameters.AddExpression(expression)
  3722. END
  3723. END;
  3724. IF usesGeneralOperator THEN
  3725. actualParameters.AddExpression(tempMathArrayExpression)
  3726. END;
  3727. IF rhs # NIL THEN
  3728. (* add actual parameter for RHS *)
  3729. IF needsReshaping THEN
  3730. (* reshape using OCArrayBase.ExpandDimensions *)
  3731. tempList := SyntaxTree.NewExpressionList();
  3732. (* source array *)
  3733. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3734. tempList.AddExpression(rhs);
  3735. ELSE
  3736. (* convert scalar to one-dimensional array *)
  3737. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3738. tempMathArrayExpression.elements.AddExpression(rhs);
  3739. tempList.AddExpression(tempMathArrayExpression)
  3740. END;
  3741. (* list of kept dimensions *)
  3742. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3743. FOR i := 0 TO indexListSize - 1 DO
  3744. expression := indexList.GetExpression(i);
  3745. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3746. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3747. ELSE
  3748. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3749. END
  3750. END;
  3751. tempList.AddExpression(tempMathArrayExpression);
  3752. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3753. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3754. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3755. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3756. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3757. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3758. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3759. expression.SetType(castReturnType);
  3760. ELSE
  3761. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3762. END;
  3763. actualParameters.AddExpression(expression)
  3764. ELSE
  3765. actualParameters.AddExpression(rhs)
  3766. END
  3767. END;
  3768. (* add dereference operator and create procedure call designator *)
  3769. ASSERT(left IS SyntaxTree.Designator);
  3770. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3771. ASSERT(expression IS SyntaxTree.Designator);
  3772. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3773. IF (rhs = NIL) & needsReshaping THEN
  3774. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3775. tempList := SyntaxTree.NewExpressionList();
  3776. FOR i := 0 TO indexList.Length() - 1 DO
  3777. expression := indexList.GetExpression(i);
  3778. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3779. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3780. ELSE
  3781. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3782. END
  3783. END;
  3784. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3785. END;
  3786. IF rhs = NIL THEN
  3787. (* special rule: index read operator calls are considered to be assignable *)
  3788. result.SetAssignable(TRUE)
  3789. END;
  3790. (* put information about this index operator call into the resulting designator *)
  3791. result.SetRelatedAsot(left);
  3792. result.SetRelatedIndexList(indexList)
  3793. END;
  3794. RETURN result
  3795. END NewIndexOperatorCall;
  3796. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3797. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3798. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3799. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3800. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3801. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3802. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3803. CONST trace = FALSE;
  3804. BEGIN
  3805. IF trace THEN
  3806. FOR i := 0 TO actualParameters.Length()-1 DO
  3807. Printout.Info("par", actualParameters.GetExpression(i));
  3808. END;
  3809. END;
  3810. operator := scope.firstOperator;
  3811. WHILE(operator # NIL) DO
  3812. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3813. procedureType := operator.type(SyntaxTree.ProcedureType);
  3814. distance := Distance(system, procedureType,actualParameters);
  3815. IF trace THEN Printout.Info("check op ",operator) END;
  3816. IF distance < bestDistance THEN
  3817. IF trace THEN Printout.Info("taken op",operator) END;
  3818. bestDistance := distance;
  3819. bestOperator := operator;
  3820. END;
  3821. END;
  3822. operator := operator.nextOperator;
  3823. END;
  3824. END FindInScope;
  3825. BEGIN
  3826. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3827. identifier := SyntaxTree.NewIdentifier("[]");
  3828. WHILE (recordType # NIL) DO
  3829. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3830. recordType := recordType.GetBaseRecord();
  3831. END;
  3832. RETURN bestOperator
  3833. END FindOperator;
  3834. BEGIN
  3835. type := left.type.resolved;
  3836. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3837. actualParameters := SyntaxTree.NewExpressionList();
  3838. FOR i := 0 TO parameters.Length()-1 DO
  3839. expression := ResolveExpression(parameters.GetExpression(i));
  3840. actualParameters.AddExpression(expression);
  3841. END;
  3842. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3843. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3844. IF op # NIL THEN
  3845. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3846. ASSERT(expression IS SyntaxTree.Designator);
  3847. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3848. result.SetRelatedAsot(left);
  3849. result.SetRelatedIndexList(parameters);
  3850. (* check if write operator exists, for var parameters *)
  3851. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3852. actualParameters := SyntaxTree.NewExpressionList();
  3853. FOR i := 0 TO parameters.Length()-1 DO
  3854. expression := ResolveExpression(parameters.GetExpression(i));
  3855. actualParameters.AddExpression(expression);
  3856. END;
  3857. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3858. actualParameters.AddExpression(rhs);
  3859. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3860. IF op = NIL THEN rhs := NIL END;
  3861. END;
  3862. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3863. ELSE
  3864. Error(position,Diagnostics.Invalid,"undefined operator");
  3865. result := SyntaxTree.invalidDesignator
  3866. END;
  3867. RETURN result;
  3868. END NewObjectOperatorCall;
  3869. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3870. 1. convert bracket designator chains into a single one that contains separators
  3871. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3872. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3873. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3874. - if an array or math array is indexed over, create index designator
  3875. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3876. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3877. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3878. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3879. - if an array-structured object type is indexed over, create procedure call designator
  3880. e.g.: a[x, y] -> a^."[]"(x, y)
  3881. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3882. - a[i, *] = a[i][*]
  3883. - a[*, i] # a[*][i]
  3884. Because:
  3885. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3886. - 'i-th column' = a[*, i]
  3887. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3888. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3889. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3890. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3891. **)
  3892. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3893. VAR
  3894. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3895. indexDesignator: SyntaxTree.IndexDesignator;
  3896. designator: SyntaxTree.Designator;
  3897. type: SyntaxTree.Type;
  3898. recordType: SyntaxTree.RecordType;
  3899. expression, rhs: SyntaxTree.Expression;
  3900. indexList: SyntaxTree.ExpressionList;
  3901. i: LONGINT;
  3902. hasError, done: BOOLEAN;
  3903. PROCEDURE FinalizeIndexDesignator;
  3904. BEGIN
  3905. IF indexDesignator # NIL THEN
  3906. (* the end of a tensor has been reached: *)
  3907. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3908. SetIndexBaseType(indexDesignator, type);
  3909. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3910. designator := indexDesignator;
  3911. type := designator.type.resolved;
  3912. indexDesignator := NIL;
  3913. ASSERT(SyntaxTree.Resolved IN type.state)
  3914. END
  3915. END FinalizeIndexDesignator;
  3916. BEGIN
  3917. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3918. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3919. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3920. (* copy all index list entries including a separator to the left bracket designator *)
  3921. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3922. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3923. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3924. END;
  3925. (* propagate the related RHS *)
  3926. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3927. (* only resolve left bracket designator and use as final result *)
  3928. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3929. ELSE
  3930. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3931. designator := ResolveDesignator(bracketDesignator.left);
  3932. type := designator.type.resolved;
  3933. indexDesignator := NIL;
  3934. (*!!! clean up *)
  3935. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3936. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3937. RETURN
  3938. END;
  3939. i := 0;
  3940. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3941. expression := bracketDesignator.parameters.GetExpression(i);
  3942. expression := ResolveExpression(expression);
  3943. bracketDesignator.parameters.SetExpression(i, expression);
  3944. IF expression = SyntaxTree.indexListSeparator THEN
  3945. (* finalize an existing index designator if needed *)
  3946. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3947. INC(i)
  3948. ELSE
  3949. (* do auto-dereferencing if needed *)
  3950. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3951. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3952. & (i=0)*)
  3953. THEN
  3954. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3955. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3956. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  3957. designator := SyntaxTree.invalidDesignator;
  3958. type := SyntaxTree.invalidType
  3959. ELSE
  3960. FinalizeIndexDesignator;
  3961. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3962. type := designator.type.resolved
  3963. END
  3964. END;
  3965. (* create a new index designator, if needed *)
  3966. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3967. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3968. indexDesignator.SetAssignable(designator.assignable);
  3969. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3970. (* designator := indexDesignator *)
  3971. END;
  3972. IF type = SyntaxTree.invalidType THEN
  3973. (* error already handled *)
  3974. INC(i)
  3975. ELSIF type IS SyntaxTree.ArrayType THEN
  3976. (* indexing over an array *)
  3977. ASSERT(indexDesignator # NIL);
  3978. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3979. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3980. INC(i)
  3981. ELSIF type IS SyntaxTree.StringType THEN
  3982. (* indexing over an array *)
  3983. ASSERT(indexDesignator # NIL);
  3984. AppendIndex(expression.position, indexDesignator, expression, type);
  3985. type := type(SyntaxTree.StringType).baseType.resolved;
  3986. INC(i)
  3987. ELSIF type IS SyntaxTree.MathArrayType THEN
  3988. (* indexing over a math array *)
  3989. ASSERT(indexDesignator # NIL);
  3990. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  3991. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3992. INC(i)
  3993. ELSIF IsArrayStructuredObjectType(type) THEN
  3994. (* indexing over ASOTs *)
  3995. FinalizeIndexDesignator;
  3996. ASSERT(type IS SyntaxTree.PointerType);
  3997. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3998. (*
  3999. - collect index list items from bracket designator that belong to ASOT
  4000. - check for errors
  4001. *)
  4002. indexList := SyntaxTree.NewExpressionList();
  4003. hasError := FALSE;
  4004. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4005. (* indexing over tensor ASOT:
  4006. - stop at index list end or separator
  4007. - dimensionality is given by number of index list items
  4008. *)
  4009. done := FALSE;
  4010. WHILE ~done DO
  4011. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4012. done := TRUE;
  4013. ELSE
  4014. expression := bracketDesignator.parameters.GetExpression(i);
  4015. IF expression = SyntaxTree.indexListSeparator THEN
  4016. done := TRUE;
  4017. ELSE
  4018. expression := ResolveExpression(expression);
  4019. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4020. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4021. hasError := TRUE
  4022. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4023. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4024. expression := SyntaxTree.invalidExpression;
  4025. hasError := TRUE
  4026. END;
  4027. indexList.AddExpression(expression)
  4028. END;
  4029. INC(i)
  4030. END
  4031. END
  4032. ELSE
  4033. (* indexing over non-tensor ASOT:
  4034. - ignore separators
  4035. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4036. *)
  4037. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4038. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4039. expression := bracketDesignator.parameters.GetExpression(i);
  4040. ELSE
  4041. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4042. END;
  4043. IF expression # SyntaxTree.indexListSeparator THEN
  4044. expression := ResolveExpression(expression);
  4045. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4046. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4047. expression := SyntaxTree.invalidExpression;
  4048. hasError := TRUE
  4049. END;
  4050. indexList.AddExpression(expression)
  4051. END;
  4052. INC(i)
  4053. END;
  4054. END;
  4055. IF hasError THEN
  4056. designator := SyntaxTree.invalidDesignator;
  4057. type := SyntaxTree.invalidType;
  4058. ELSE
  4059. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4060. and the last entry in the index list belongs to the array-structured object type in question.
  4061. E.g.: for a 2-dimensional array-structured object type:
  4062. - 'lhs := asot[1, 2]' -> read mode
  4063. - 'asot[1, 2] := rhs' -> write mode
  4064. - 'asot[1, 2, 3] := rhs' -> read mode
  4065. *)
  4066. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4067. rhs := bracketDesignator.relatedRhs
  4068. ELSE
  4069. rhs := NIL
  4070. END;
  4071. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4072. type := designator.type
  4073. END
  4074. ELSE
  4075. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4076. designator := SyntaxTree.invalidDesignator;
  4077. type := SyntaxTree.invalidType;
  4078. INC(i)
  4079. END
  4080. END
  4081. END;
  4082. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4083. resolvedExpression := designator
  4084. END
  4085. END VisitBracketDesignator;
  4086. (** check and resolve expression list
  4087. - resolve each expression in an expression list
  4088. - returns true if and only if all statements could have successfully been resolved
  4089. **)
  4090. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4091. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4092. BEGIN
  4093. result := TRUE;
  4094. FOR i := 0 TO expressionList.Length()-1 DO
  4095. expression := ResolveExpression(expressionList.GetExpression(i));
  4096. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4097. expressionList.SetExpression(i,expression);
  4098. END;
  4099. RETURN result
  4100. END ExpressionList;
  4101. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4102. BEGIN
  4103. type := type.resolved;
  4104. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4105. RETURN TRUE
  4106. ELSIF system.CanPassInRegister # NIL THEN
  4107. RETURN system.CanPassInRegister(type);
  4108. ELSE
  4109. RETURN FALSE
  4110. END;
  4111. END CanPassInRegister;
  4112. (** return procedure call designator left(actualParameters)
  4113. - check realtime procedure call in realtime procedure
  4114. - check number of parameters
  4115. - check parameter compatibility
  4116. return invalidDesignator if error
  4117. **)
  4118. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4119. VAR result: SyntaxTree.Designator;
  4120. numberFormalParameters, numberActualParameters: LONGINT;
  4121. formalType: SyntaxTree.ProcedureType;
  4122. formalParameter: SyntaxTree.Parameter;
  4123. actualParameter: SyntaxTree.Expression;
  4124. i: LONGINT;
  4125. BEGIN
  4126. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4127. result := SyntaxTree.invalidDesignator;
  4128. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4129. numberFormalParameters := formalType.numberParameters;
  4130. numberActualParameters := actualParameters.Length();
  4131. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4132. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4133. END;
  4134. IF ~ExpressionList(actualParameters) THEN
  4135. result := SyntaxTree.invalidDesignator
  4136. ELSE
  4137. IF numberActualParameters <= numberFormalParameters THEN
  4138. formalParameter := formalType.firstParameter;
  4139. FOR i := 0 TO numberActualParameters-1 DO
  4140. actualParameter := actualParameters.GetExpression(i);
  4141. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4142. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4143. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4144. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4145. ELSE
  4146. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4147. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4148. END;
  4149. actualParameters.SetExpression(i,actualParameter);
  4150. END;
  4151. formalParameter := formalParameter.nextParameter;
  4152. END;
  4153. WHILE (formalParameter # NIL) DO
  4154. IF formalParameter.defaultValue # NIL THEN
  4155. actualParameters.AddExpression(formalParameter.defaultValue);
  4156. formalParameter := formalParameter.nextParameter
  4157. ELSE
  4158. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4159. formalParameter := NIL;
  4160. END;
  4161. END;
  4162. ELSE
  4163. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4164. END;
  4165. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4166. result.SetAssignable(FALSE);
  4167. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4168. END;
  4169. RETURN result
  4170. END NewProcedureCallDesignator;
  4171. (**
  4172. builtin call designator generated in VisitParameterDesignator
  4173. -> nothing to be resolved
  4174. **)
  4175. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4176. BEGIN
  4177. resolvedExpression := x;
  4178. END VisitTypeGuardDesignator;
  4179. (**
  4180. builtin call designator generated in VisitParameterDesignator
  4181. -> nothing to be resolved
  4182. **)
  4183. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4184. BEGIN
  4185. IF ExpressionList(x.parameters) THEN
  4186. resolvedExpression := x;
  4187. END;
  4188. END VisitBuiltinCallDesignator;
  4189. (**
  4190. procedure call designator generated in VisitParameterDesignator
  4191. -> nothing to be resolved
  4192. **)
  4193. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4194. BEGIN
  4195. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4196. resolvedExpression := x;
  4197. END VisitProcedureCallDesignator;
  4198. (** return true if x is a variable else return false and report error **)
  4199. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4200. VAR result: BOOLEAN;
  4201. BEGIN
  4202. result := TRUE;
  4203. IF x = SyntaxTree.invalidExpression THEN
  4204. result := FALSE;
  4205. ELSIF ~IsVariable(x) THEN
  4206. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4207. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4208. result := FALSE;
  4209. END;
  4210. RETURN result
  4211. END CheckVariable;
  4212. (**
  4213. if expression x is of basic type then return true else report error and return false
  4214. **)
  4215. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4216. VAR result: BOOLEAN;
  4217. BEGIN
  4218. result := FALSE;
  4219. IF x = SyntaxTree.invalidExpression THEN
  4220. ELSIF ~IsBasicType(x.type) THEN
  4221. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4222. result := FALSE
  4223. ELSE result := TRUE
  4224. END;
  4225. RETURN result
  4226. END CheckBasicType;
  4227. (**
  4228. if expression x is of number type then return true else report error and return false
  4229. **)
  4230. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4231. VAR result: BOOLEAN;
  4232. BEGIN
  4233. result := FALSE;
  4234. IF x = SyntaxTree.invalidExpression THEN
  4235. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4236. Error(x.position,Diagnostics.Invalid,"is non number type");
  4237. ELSE result := TRUE
  4238. END;
  4239. RETURN result
  4240. END CheckNumberType;
  4241. (**
  4242. if expression x is of number or size type but not complex then return true else report error and return false
  4243. **)
  4244. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4245. VAR result: BOOLEAN;
  4246. BEGIN
  4247. result := FALSE;
  4248. IF x = SyntaxTree.invalidExpression THEN
  4249. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4250. Error(x.position,Diagnostics.Invalid,"is complex type");
  4251. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4252. Error(x.position,Diagnostics.Invalid,"is non number type");
  4253. ELSE result := TRUE
  4254. END;
  4255. RETURN result
  4256. END CheckNonComplexNumberSizeType;
  4257. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4258. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4259. BEGIN
  4260. result := FALSE; type := x.type.resolved;
  4261. IF x = SyntaxTree.invalidExpression THEN
  4262. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4263. TRACE(type.sizeInBits);
  4264. TRACE(system.addressType.sizeInBits);
  4265. Error(x.position,Diagnostics.Invalid,"is no address type");
  4266. ELSE result := TRUE
  4267. END;
  4268. RETURN result
  4269. END CheckAddressType;
  4270. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4271. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4272. BEGIN
  4273. result := FALSE; type := x.type.resolved;
  4274. IF x = SyntaxTree.invalidExpression THEN
  4275. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4276. Error(x.position,Diagnostics.Invalid,"is no size type");
  4277. ELSE result := TRUE
  4278. END;
  4279. RETURN result
  4280. END CheckSizeType;
  4281. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4282. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4283. BEGIN
  4284. result := FALSE; type := x.type.resolved;
  4285. IF x = SyntaxTree.invalidExpression THEN
  4286. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4287. Error(x.position,Diagnostics.Invalid,"is no object type");
  4288. ELSE result := TRUE
  4289. END;
  4290. RETURN result
  4291. END CheckObjectType;
  4292. (**
  4293. if expression x is of integer type then return true else report error and return false
  4294. **)
  4295. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4296. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4297. BEGIN
  4298. result := FALSE; type := x.type.resolved;
  4299. IF x = SyntaxTree.invalidExpression THEN
  4300. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4301. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4302. ELSE result := TRUE
  4303. END;
  4304. RETURN result
  4305. END CheckIntegerType;
  4306. (**
  4307. if expression x is of character type then return true else report error and return false
  4308. **)
  4309. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4310. VAR result: BOOLEAN;
  4311. BEGIN
  4312. result := FALSE;
  4313. IF x = SyntaxTree.invalidExpression THEN
  4314. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4315. Error(x.position,Diagnostics.Invalid,"is no character type");
  4316. ELSE result := TRUE
  4317. END;
  4318. RETURN result
  4319. END CheckCharacterType;
  4320. (**
  4321. if expression x is of real type then return true else report error and return false
  4322. **)
  4323. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4324. VAR result: BOOLEAN;
  4325. BEGIN
  4326. result := FALSE;
  4327. IF x = SyntaxTree.invalidExpression THEN
  4328. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4329. Error(x.position,Diagnostics.Invalid,"is no float type");
  4330. ELSE result := TRUE
  4331. END;
  4332. RETURN result
  4333. END CheckRealType;
  4334. (**
  4335. if expression x is of range type then return true else report error and return false
  4336. **)
  4337. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4338. VAR result: BOOLEAN;
  4339. BEGIN
  4340. result := FALSE;
  4341. IF x = SyntaxTree.invalidExpression THEN
  4342. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4343. Error(x.position,Diagnostics.Invalid,"is no range type");
  4344. ELSE result := TRUE
  4345. END;
  4346. RETURN result
  4347. END CheckRangeType;
  4348. (**
  4349. if expression x is of boolean type then return true else report error and return false
  4350. **)
  4351. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4352. VAR result: BOOLEAN;
  4353. BEGIN
  4354. result := FALSE;
  4355. IF x = SyntaxTree.invalidExpression THEN
  4356. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4357. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4358. ELSE result := TRUE
  4359. END;
  4360. RETURN result
  4361. END CheckBooleanType;
  4362. (**
  4363. if expression x is of set type then return true else report error and return false
  4364. **)
  4365. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4366. VAR result: BOOLEAN;
  4367. BEGIN
  4368. result := FALSE;
  4369. IF x = SyntaxTree.invalidExpression THEN
  4370. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4371. Error(x.position,Diagnostics.Invalid,"is no set type");
  4372. ELSE result := TRUE
  4373. END;
  4374. RETURN result
  4375. END CheckSetType;
  4376. (**
  4377. if expression x is of string or array of character type then return true else report error and return false
  4378. **)
  4379. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4380. VAR result: BOOLEAN;
  4381. BEGIN
  4382. result := FALSE;
  4383. IF x = SyntaxTree.invalidExpression THEN
  4384. ELSIF ~IsStringType(x.type.resolved) THEN
  4385. Error(x.position,Diagnostics.Invalid,"is no string type");
  4386. ELSE result := TRUE
  4387. END;
  4388. RETURN result
  4389. END CheckStringType;
  4390. (**
  4391. if expression x is a type declaration type return true else report error and return false
  4392. **)
  4393. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4394. VAR result: BOOLEAN;
  4395. BEGIN
  4396. result := FALSE;
  4397. IF x = SyntaxTree.invalidExpression THEN
  4398. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4399. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4400. ELSE result := TRUE
  4401. END;
  4402. RETURN result
  4403. END CheckTypeDeclarationType;
  4404. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4405. VAR result: BOOLEAN;
  4406. BEGIN
  4407. result := FALSE;
  4408. IF x = SyntaxTree.invalidExpression THEN
  4409. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4410. result := TRUE;
  4411. value := x.resolved(SyntaxTree.IntegerValue).value;
  4412. ELSE
  4413. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4414. END;
  4415. RETURN result;
  4416. END CheckIntegerValue;
  4417. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4418. BEGIN
  4419. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4420. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4421. ELSE
  4422. RETURN FALSE
  4423. END;
  4424. END IsUnsignedValue;
  4425. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4426. BEGIN
  4427. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4428. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4429. ELSE
  4430. RETURN FALSE
  4431. END
  4432. END IsAddressValue;
  4433. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4434. BEGIN
  4435. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4436. END IsAddressExpression;
  4437. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4438. BEGIN
  4439. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4440. END IsSizeExpression;
  4441. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4442. VAR result: BOOLEAN;
  4443. BEGIN
  4444. result := FALSE;
  4445. IF x = SyntaxTree.invalidExpression THEN
  4446. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4447. result := TRUE;
  4448. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4449. ELSE
  4450. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4451. END;
  4452. RETURN result;
  4453. END CheckEnumerationValue;
  4454. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4455. VAR result: BOOLEAN;
  4456. BEGIN
  4457. result := FALSE;
  4458. IF x = SyntaxTree.invalidExpression THEN
  4459. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4460. result := TRUE;
  4461. value := x.resolved(SyntaxTree.CharacterValue).value;
  4462. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4463. result := TRUE;
  4464. value := x.resolved(SyntaxTree.StringValue).value[0];
  4465. ELSE
  4466. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4467. END;
  4468. RETURN result;
  4469. END CheckCharacterValue;
  4470. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4471. VAR result: BOOLEAN;
  4472. BEGIN
  4473. result := FALSE;
  4474. IF x = SyntaxTree.invalidExpression THEN
  4475. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4476. value := x.resolved(SyntaxTree.IntegerValue).value;
  4477. IF (value > 0) OR includeZero & (value = 0) THEN
  4478. result := TRUE;
  4479. ELSE
  4480. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4481. END
  4482. ELSE
  4483. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4484. END;
  4485. RETURN result;
  4486. END CheckPositiveIntegerValue;
  4487. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4488. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4489. BEGIN
  4490. result := FALSE;
  4491. IF x = SyntaxTree.invalidExpression THEN
  4492. ELSE
  4493. type := x.type.resolved;
  4494. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4495. portType := type(SyntaxTree.PortType);
  4496. result := TRUE
  4497. ELSE
  4498. Error(x.position,Diagnostics.Invalid,"no port type");
  4499. END;
  4500. END;
  4501. RETURN result
  4502. END CheckPortType;
  4503. (* move to builtin procedure call statement ?
  4504. remove builtin procedure call designator ?
  4505. *)
  4506. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator): SyntaxTree.Expression;
  4507. VAR
  4508. numberActualParameters,numberFormalParameters: LONGINT;
  4509. formalParameter: SyntaxTree.Parameter;
  4510. actualParameter: SyntaxTree.Expression;
  4511. procedureType: SyntaxTree.ProcedureType;
  4512. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4513. inPort, outPort: SyntaxTree.PortType;
  4514. constructor: SyntaxTree.Procedure;
  4515. type0,type1,type2: SyntaxTree.Type;
  4516. type,base,parameterType: SyntaxTree.Type;
  4517. arrayType: SyntaxTree.ArrayType;
  4518. i,i0,i1: LONGINT;
  4519. r,r0,r1,im: LONGREAL;
  4520. c: CHAR;
  4521. id: LONGINT;
  4522. b: BOOLEAN;
  4523. mathArrayType: SyntaxTree.MathArrayType;
  4524. customBuiltin: SyntaxTree.CustomBuiltin;
  4525. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4526. VAR resultB: BOOLEAN;
  4527. BEGIN
  4528. IF numberActualParameters < from THEN
  4529. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4530. result := SyntaxTree.invalidExpression;
  4531. resultB := FALSE;
  4532. ELSIF numberActualParameters > to THEN
  4533. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4534. result := SyntaxTree.invalidExpression;
  4535. resultB := FALSE;
  4536. ELSE
  4537. resultB := TRUE;
  4538. END;
  4539. RETURN resultB
  4540. END CheckArity;
  4541. BEGIN
  4542. type := NIL; result := NIL;
  4543. type0 := NIL; type1 := NIL; type2 := NIL;
  4544. numberActualParameters := actualParameters.Length();
  4545. IF numberActualParameters>0 THEN
  4546. parameter0 := actualParameters.GetExpression(0);
  4547. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4548. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4549. result := SyntaxTree.invalidExpression
  4550. END
  4551. END;
  4552. IF numberActualParameters >1 THEN
  4553. parameter1 := actualParameters.GetExpression(1);
  4554. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4555. ELSE
  4556. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4557. result := SyntaxTree.invalidExpression
  4558. END
  4559. END;
  4560. IF numberActualParameters >2 THEN
  4561. parameter2 := actualParameters.GetExpression(2);
  4562. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4563. ELSE
  4564. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4565. result := SyntaxTree.invalidExpression
  4566. END
  4567. END;
  4568. id := builtin.id;
  4569. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4570. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4571. END;
  4572. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4573. ELSIF result # NIL THEN type := result.type (* operator *)
  4574. ELSE
  4575. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4576. result(SyntaxTree.Designator).SetLeft(left);
  4577. (* ---- ASSERT ----- *)
  4578. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4579. IF CheckBooleanType(parameter0) THEN
  4580. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4581. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4582. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4583. *)
  4584. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4585. Error(position,Diagnostics.Invalid,"assert failed");
  4586. END;
  4587. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4588. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4589. rules imposed by the architecture / current runtime
  4590. *)
  4591. END;
  4592. END;
  4593. (* ---- COPY ----- *)
  4594. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4595. IF~IsStringType(type0) THEN
  4596. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4597. END;
  4598. IF ~IsStringType(type1) THEN
  4599. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4600. ELSIF CheckVariable(parameter1) THEN
  4601. IF (type0 IS SyntaxTree.StringType) THEN
  4602. arrayType := type1(SyntaxTree.ArrayType);
  4603. IF arrayType.form = SyntaxTree.Static THEN
  4604. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4605. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4606. END;
  4607. END;
  4608. END;
  4609. END;
  4610. (* ---- INC, DEC----- *)
  4611. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4612. IF numberActualParameters = 1 THEN
  4613. parameter1 :=Global.NewIntegerValue(system,position,1);
  4614. actualParameters.AddExpression(parameter1);
  4615. END;
  4616. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4617. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4618. Error(position,Diagnostics.Invalid,"incompatible increment");
  4619. ELSE
  4620. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4621. actualParameters.SetExpression(1,parameter1);
  4622. END;
  4623. END;
  4624. (* ---- EXCL, INCL----- *)
  4625. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4626. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4627. IF IsIntegerValue(parameter1,i0) THEN
  4628. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4629. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4630. END;
  4631. END;
  4632. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4633. actualParameters.SetExpression(1,parameter1);
  4634. END;
  4635. (* ---- HALT, SYSTEM.HALT ----- *)
  4636. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4637. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4638. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4639. rules imposed by the architecture / current runtime
  4640. *)
  4641. END;
  4642. (* ---- WAIT ----- *)
  4643. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4644. IF CheckObjectType(parameter0) THEN
  4645. END;
  4646. (* ---- NEW ----- *)
  4647. ELSIF (id = Global.New) & CheckArity(1,Infinity) THEN
  4648. IF currentIsRealtime THEN
  4649. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4650. END;
  4651. (* check constructor *)
  4652. IF CheckVariable(parameter0) THEN
  4653. IF type0 IS SyntaxTree.PointerType THEN
  4654. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4655. END;
  4656. IF type0 IS SyntaxTree.ArrayType THEN
  4657. arrayType := type0(SyntaxTree.ArrayType);
  4658. IF arrayType.form = SyntaxTree.Static THEN
  4659. i := 1
  4660. ELSIF arrayType.form = SyntaxTree.Open THEN
  4661. i := Dimension(arrayType,{SyntaxTree.Open})+1;
  4662. ELSE HALT(100)
  4663. END;
  4664. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4665. i := 1;
  4666. REPEAT
  4667. actualParameter := actualParameters.GetExpression(i);
  4668. IF CheckSizeType(actualParameter) THEN
  4669. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4670. actualParameters.SetExpression(i,actualParameter);
  4671. END;
  4672. INC(i);
  4673. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4674. END;
  4675. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4676. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4677. IF constructor = NIL THEN
  4678. IF CheckArity(1,1) THEN END;
  4679. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4680. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4681. ELSE
  4682. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4683. numberFormalParameters := procedureType.numberParameters;
  4684. DEC(numberActualParameters);
  4685. IF numberActualParameters <= numberFormalParameters THEN
  4686. formalParameter := procedureType.firstParameter;
  4687. FOR i := 1 TO numberActualParameters DO
  4688. actualParameter := actualParameters.GetExpression(i);
  4689. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4690. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4691. ELSE
  4692. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4693. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4694. END;
  4695. actualParameters.SetExpression(i,actualParameter);
  4696. END;
  4697. formalParameter := formalParameter.nextParameter;
  4698. END;
  4699. WHILE (formalParameter # NIL) DO
  4700. IF formalParameter.defaultValue # NIL THEN
  4701. actualParameters.AddExpression(formalParameter.defaultValue);
  4702. formalParameter := formalParameter.nextParameter
  4703. ELSE
  4704. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4705. formalParameter := NIL;
  4706. END;
  4707. END;
  4708. ELSE
  4709. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4710. END;
  4711. END;
  4712. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4713. mathArrayType := type0(SyntaxTree.MathArrayType);
  4714. IF mathArrayType.form = SyntaxTree.Static THEN
  4715. Error(position,Diagnostics.Invalid,"new on static array");
  4716. ELSE
  4717. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4718. i0 := 2; i1 := Infinity;
  4719. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4720. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+1;
  4721. i1 := i0;
  4722. ELSE HALT(100);
  4723. END;
  4724. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4725. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4726. base := ArrayBase(type0,MAX(LONGINT));
  4727. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4728. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4729. IF ~CompatibleTo(system,type0,parameterType) THEN
  4730. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4731. result := SyntaxTree.invalidExpression;
  4732. ELSE
  4733. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4734. END;
  4735. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4736. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4737. IF ~CompatibleTo(system,type1,parameterType) THEN
  4738. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4739. result := SyntaxTree.invalidExpression;
  4740. ELSE
  4741. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4742. END;
  4743. ELSE
  4744. IF CheckArity(i0,i1) & (numberActualParameters >1) THEN
  4745. i := 1;
  4746. REPEAT
  4747. actualParameter := actualParameters.GetExpression(i);
  4748. IF CheckSizeType(actualParameter) THEN
  4749. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4750. actualParameters.SetExpression(i,actualParameter);
  4751. END;
  4752. INC(i);
  4753. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4754. END;
  4755. END;
  4756. END;
  4757. ELSIF type0 IS SyntaxTree.CellType THEN
  4758. IF ~(currentIsCellNet) THEN
  4759. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4760. ELSE
  4761. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4762. IF (constructor = NIL) & CheckArity(1,1) THEN
  4763. (* ok *)
  4764. ELSE
  4765. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4766. numberFormalParameters := procedureType.numberParameters;
  4767. DEC(numberActualParameters);
  4768. IF numberActualParameters <= numberFormalParameters THEN
  4769. formalParameter := procedureType.firstParameter;
  4770. FOR i := 1 TO numberActualParameters DO
  4771. actualParameter := actualParameters.GetExpression(i);
  4772. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4773. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4774. ELSE
  4775. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4776. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4777. END;
  4778. actualParameters.SetExpression(i,actualParameter);
  4779. END;
  4780. formalParameter := formalParameter.nextParameter;
  4781. END;
  4782. WHILE (formalParameter # NIL) DO
  4783. IF formalParameter.defaultValue # NIL THEN
  4784. actualParameters.AddExpression(formalParameter.defaultValue);
  4785. formalParameter := formalParameter.nextParameter
  4786. ELSE
  4787. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4788. formalParameter := NIL;
  4789. END;
  4790. END;
  4791. ELSE
  4792. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4793. END;
  4794. END;
  4795. END;
  4796. activeCellsStatement := TRUE;
  4797. ELSE
  4798. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4799. END;
  4800. END;
  4801. (* ---- DISPOSE ----- *)
  4802. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4803. IF ~IsPointerType(parameter0.type) THEN
  4804. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4805. ELSIF ~IsDisposable(parameter0.type) THEN
  4806. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4807. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4808. END
  4809. (* ---- GETPROCEDURE ----- *)
  4810. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4811. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4812. IF CheckVariable(parameter2) THEN
  4813. IF ~GetProcedureAllowed(parameter2.type) THEN
  4814. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4815. END;
  4816. END;
  4817. END;
  4818. (* ---- ABS ----- *)
  4819. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4820. (* note: ABS on complex numbers is done using overloading *)
  4821. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4822. type := type0;
  4823. IF IsIntegerValue(parameter0,i0) THEN
  4824. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4825. type := Global.GetIntegerType(system,ABS(i0));
  4826. ELSIF IsRealValue(parameter0,r) THEN
  4827. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4828. END;
  4829. ELSE
  4830. type := SyntaxTree.invalidType;
  4831. END;
  4832. (* ---- ASH, ASR ----- *)
  4833. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4834. type := type0;
  4835. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4836. (*
  4837. ConvertOperands(parameter0,parameter1); (* same type *)
  4838. *)
  4839. type := parameter0.type;
  4840. IF IsIntegerValue(parameter0,i0) THEN
  4841. IF IsIntegerValue(parameter1,i1) THEN
  4842. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4843. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4844. result := ResolveExpression(result);
  4845. type := Global.GetIntegerType(system,i0);
  4846. END;
  4847. END;
  4848. IF type.resolved.sizeInBits < 32 THEN
  4849. type := system.longintType;
  4850. END;
  4851. (*!compatibility with release, remove when resolved
  4852. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4853. *)
  4854. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4855. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4856. actualParameters.SetExpression(0,parameter0);
  4857. actualParameters.SetExpression(1,parameter1);
  4858. END;
  4859. (* ---- CAP ----- *)
  4860. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4861. type := system.characterType;
  4862. IF CheckCharacterType (parameter0) THEN
  4863. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4864. actualParameters.SetExpression(0,parameter0);
  4865. IF IsCharacterValue(parameter0,c) THEN
  4866. IF (c <= "z") & (c >= "a") THEN
  4867. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4868. ELSE
  4869. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4870. END;
  4871. END;
  4872. END;
  4873. (* ---- CHR ----- *)
  4874. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4875. IF id = Global.Chr THEN
  4876. type := system.characterType
  4877. ELSE
  4878. type := system.characterType32
  4879. END;
  4880. IF CheckIntegerType(parameter0) THEN
  4881. IF IsIntegerValue(parameter0,i0) THEN
  4882. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4883. result := ResolveExpression(result);
  4884. ELSE
  4885. (*
  4886. result := NewConversion(parameter0.position,parameter0,type);
  4887. *)
  4888. END;
  4889. END
  4890. (* ---- ENTIER ----- *)
  4891. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4892. type := system.longintType;
  4893. IF CheckRealType(parameter0) THEN
  4894. IF IsRealValue(parameter0,r) THEN
  4895. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4896. type := Global.GetIntegerType(system,ENTIER(r));
  4897. END
  4898. END;
  4899. (* ---- ENTIERH ----- *)
  4900. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4901. type := system.hugeintType;
  4902. IF CheckRealType(parameter0) THEN
  4903. IF IsRealValue(parameter0,r) THEN
  4904. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4905. END
  4906. END;
  4907. (* ---- LEN ----- *)
  4908. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4909. type := system.longintType;
  4910. base := type0;
  4911. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4912. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4913. actualParameters.SetExpression(0,parameter0);
  4914. type0 := parameter0.type.resolved;
  4915. base := type0;
  4916. END;
  4917. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4918. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4919. IF i1 < 0 THEN
  4920. Error(position,Diagnostics.Invalid,"invalid dimension");
  4921. base := SyntaxTree.invalidType;
  4922. ELSE
  4923. base := ArrayBase(base,i1);
  4924. IF (base # NIL) & Indexable(base) THEN
  4925. ELSE
  4926. Error(position,Diagnostics.Invalid,"len on no array");
  4927. IF VerboseErrorMessage THEN
  4928. Printout.Info("base",base);
  4929. END;
  4930. base := SyntaxTree.invalidType;
  4931. END;
  4932. END;
  4933. IF numberActualParameters=2 THEN
  4934. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4935. actualParameters.SetExpression(1,parameter1);
  4936. ELSIF base IS SyntaxTree.MathArrayType THEN
  4937. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4938. END;
  4939. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4940. IF base IS SyntaxTree.ArrayType THEN
  4941. arrayType := base(SyntaxTree.ArrayType);
  4942. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  4943. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4944. result := Global.NewIntegerValue(system,position,i);
  4945. type := result.type;(* arrayType.length.type;*)
  4946. ASSERT(type # NIL);
  4947. END;
  4948. ELSIF base IS SyntaxTree.MathArrayType THEN
  4949. mathArrayType := base(SyntaxTree.MathArrayType);
  4950. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  4951. result := Global.NewIntegerValue(system,position,i);
  4952. type := result.type;
  4953. (*
  4954. type := mathArrayType.length.type;
  4955. *)
  4956. ASSERT(type # NIL);
  4957. END;
  4958. END;
  4959. END;
  4960. ELSE
  4961. type := system.longintType;
  4962. END;
  4963. (* ---- FIRST ---- *)
  4964. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4965. type := system.longintType;
  4966. IF CheckRangeType(parameter0) THEN END;
  4967. result.SetAssignable(parameter0.assignable)
  4968. (* ---- LAST ---- *)
  4969. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4970. type := system.longintType;
  4971. IF CheckRangeType(parameter0) THEN END;
  4972. result.SetAssignable(parameter0.assignable)
  4973. (* ---- STEP ---- *)
  4974. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4975. type := system.longintType;
  4976. IF CheckRangeType(parameter0) THEN END;
  4977. result.SetAssignable(parameter0.assignable)
  4978. (* ---- RE ---- *)
  4979. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4980. IF CheckNumberType(parameter0) THEN
  4981. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4982. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4983. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  4984. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  4985. type := parameter0.type
  4986. ELSE
  4987. type := system.realType
  4988. END
  4989. END;
  4990. result.SetAssignable(parameter0.assignable)
  4991. (* ---- IM ---- *)
  4992. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  4993. IF CheckNumberType(parameter0) THEN
  4994. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4995. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4996. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  4997. ELSE
  4998. type := system.realType;
  4999. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5000. END
  5001. END;
  5002. result.SetAssignable(parameter0.assignable)
  5003. (* ---- MAX ----- *)
  5004. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5005. IF numberActualParameters = 1 THEN
  5006. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5007. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5008. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5009. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5010. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5011. *)
  5012. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5013. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5014. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5015. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5016. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5017. END;
  5018. ELSE
  5019. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5020. END
  5021. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5022. ConvertOperands(parameter0,parameter1);
  5023. actualParameters.SetExpression(0,parameter0);
  5024. actualParameters.SetExpression(1,parameter1);
  5025. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5026. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5027. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5028. END;
  5029. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5030. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5031. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5032. END;
  5033. END;
  5034. type := parameter0.type;
  5035. ELSE type := SyntaxTree.invalidType;
  5036. END;
  5037. (* ---- MIN ----- *)
  5038. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5039. IF numberActualParameters = 1 THEN
  5040. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5041. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5042. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5043. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5044. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5045. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5046. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5047. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5048. END;
  5049. ELSE
  5050. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5051. END
  5052. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5053. ConvertOperands(parameter0,parameter1);
  5054. actualParameters.SetExpression(0,parameter0);
  5055. actualParameters.SetExpression(1,parameter1);
  5056. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5057. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5058. ELSE result.SetResolved(parameter1.resolved)
  5059. END;
  5060. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5061. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5062. ELSE result.SetResolved(parameter1.resolved)
  5063. END;
  5064. END;
  5065. type := parameter0.type;
  5066. ELSE type := SyntaxTree.invalidType;
  5067. END;
  5068. (* ---- ODD ----- *)
  5069. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5070. type := system.booleanType;
  5071. IF CheckIntegerType(parameter0) THEN
  5072. IF IsIntegerValue(parameter0,i0) THEN
  5073. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5074. type := system.booleanType;
  5075. END;
  5076. END;
  5077. (* ---- ORD ----- *)
  5078. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5079. IF id = Global.Ord THEN
  5080. type := system.integerType;
  5081. ELSE
  5082. type := system.longintType;
  5083. END;
  5084. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5085. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5086. actualParameters.SetExpression(0,parameter0);
  5087. (* IF CheckCharacterType(parameter0) THEN*)
  5088. IF IsCharacterValue(parameter0,c)THEN
  5089. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5090. type := Global.GetSignedIntegerType(system,ORD(c));
  5091. END;
  5092. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5093. END;
  5094. (* ---- SHORT ----- *)
  5095. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5096. type := type0;
  5097. IF IsSignedIntegerType(type) THEN
  5098. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5099. ELSIF type = system.integerType THEN type := system.shortintType
  5100. ELSIF type = system.longintType THEN type := system.integerType
  5101. ELSIF type = system.hugeintType THEN type:= system.longintType
  5102. ELSE
  5103. CASE type.sizeInBits OF
  5104. 16: type := Global.Integer8
  5105. |32: type := Global.Integer16
  5106. |64: type := Global.Integer32
  5107. END;
  5108. END;
  5109. ELSIF type IS SyntaxTree.FloatType THEN
  5110. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5111. ELSIF type = system.longrealType THEN type := system.realType
  5112. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5113. END;
  5114. ELSIF type IS SyntaxTree.ComplexType THEN
  5115. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5116. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5117. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5118. END;
  5119. ELSE
  5120. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5121. END;
  5122. IF (parameter0.resolved # NIL) THEN
  5123. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5124. IF parameter0 IS SyntaxTree.Value THEN
  5125. result.SetResolved(parameter0(SyntaxTree.Value));
  5126. END;
  5127. END;
  5128. (* ---- LONG ----- *)
  5129. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5130. type := type0;
  5131. IF IsSignedIntegerType(type) THEN
  5132. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5133. ELSIF type = system.longintType THEN type := system.hugeintType
  5134. ELSIF type = system.integerType THEN type := system.longintType
  5135. ELSIF type = system.shortintType THEN type := system.integerType
  5136. ELSE
  5137. CASE type.sizeInBits OF
  5138. 8: type := Global.Integer16
  5139. |16: type := Global.Integer32
  5140. |32: type := Global.Integer64
  5141. END;
  5142. END;
  5143. ELSIF type IS SyntaxTree.FloatType THEN
  5144. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5145. ELSIF type= system.realType THEN type := system.longrealType
  5146. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5147. END;
  5148. ELSIF type IS SyntaxTree.ComplexType THEN
  5149. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5150. ELSIF type = system.complexType THEN type := system.longcomplexType
  5151. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5152. END;
  5153. ELSE
  5154. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5155. END;
  5156. IF (parameter0.resolved # NIL) THEN
  5157. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5158. IF parameter0 IS SyntaxTree.Value THEN
  5159. result.SetResolved(parameter0(SyntaxTree.Value));
  5160. END;
  5161. END;
  5162. (* ---- SIZE OF ----- *)
  5163. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5164. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5165. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5166. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5167. type := system.integerType;
  5168. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5169. ELSE
  5170. (* for variables, system sizeof could represent the physically occupied size
  5171. determined via the type descriptor, implement that ? *)
  5172. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5173. END
  5174. (* ---- SYSTEM.TRACE -----*)
  5175. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5176. FOR i := 0 TO numberActualParameters-1 DO
  5177. parameter0 := actualParameters.GetExpression(i);
  5178. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5179. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5180. END;
  5181. END;
  5182. (* remaining issues can only be tested in backend *)
  5183. (* ---- ADDRESSOF----- *)
  5184. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5185. IF HasAddress(parameter0) THEN
  5186. type := system.addressType;
  5187. ELSE
  5188. type := SyntaxTree.invalidType;
  5189. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5190. END;
  5191. (* ---- BIT ----- *)
  5192. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5193. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5194. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5195. actualParameters.SetExpression(0,parameter0);
  5196. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5197. actualParameters.SetExpression(1,parameter1);
  5198. END;
  5199. type := system.booleanType;
  5200. (* ----- MSK ---- *)
  5201. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5202. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5203. ConvertOperands(parameter0,parameter1);
  5204. actualParameters.SetExpression(0,parameter0);
  5205. actualParameters.SetExpression(1,parameter1);
  5206. END;
  5207. type := parameter0.type;
  5208. (* ---- SYSTEM.GET64 ----- *)
  5209. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5210. IF CheckAddressType(parameter0) THEN
  5211. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5212. actualParameters.SetExpression(0,parameter0);
  5213. END;
  5214. type := system.hugeintType;
  5215. (* ---- SYSTEM.GET32 ----- *)
  5216. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5217. IF CheckAddressType(parameter0) THEN
  5218. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5219. actualParameters.SetExpression(0,parameter0);
  5220. END;
  5221. type := system.longintType;
  5222. (* ---- SYSTEM.GET16 ----- *)
  5223. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5224. IF CheckAddressType(parameter0) THEN
  5225. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5226. actualParameters.SetExpression(0,parameter0);
  5227. END;
  5228. type := system.integerType;
  5229. (* ---- SYSTEM.GET8 ----- *)
  5230. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5231. IF CheckAddressType(parameter0) THEN
  5232. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5233. actualParameters.SetExpression(0,parameter0);
  5234. END;
  5235. type := system.shortintType;
  5236. (* ---- SYSTEM.GetStackPointer ----- *)
  5237. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5238. type := system.addressType;
  5239. (* ---- SYSTEM.GetFramePointer ----- *)
  5240. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5241. type := system.addressType;
  5242. (* ---- SYSTEM.GetActivity ----- *)
  5243. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5244. type := system.objectType;
  5245. (* ---- SYSTEM.SetStackPointer ----- *)
  5246. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5247. IF CheckAddressType(parameter0) THEN
  5248. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5249. actualParameters.SetExpression(0,parameter0);
  5250. END;
  5251. (* ---- SYSTEM.SetFramePointer ----- *)
  5252. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5253. IF CheckAddressType(parameter0) THEN
  5254. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5255. actualParameters.SetExpression(0,parameter0);
  5256. END;
  5257. (* ---- SYSTEM.SetActivity ----- *)
  5258. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5259. IF CheckObjectType(parameter0) THEN
  5260. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5261. actualParameters.SetExpression(0,parameter0);
  5262. END;
  5263. (* ---- LSH, LSL, ROT, ROR ----- *)
  5264. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5265. type := type0;
  5266. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5267. actualParameters.SetExpression(1, parameter1);
  5268. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5269. IF id = Global.Lsh THEN
  5270. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5271. ELSIF id = Global.Rot THEN
  5272. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5273. ELSIF id = Global.Ror THEN
  5274. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5275. END;
  5276. END;
  5277. (* ---- SYSTEM.VAL ----- *)
  5278. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5279. IF CheckTypeDeclarationType(parameter0) THEN
  5280. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5281. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5282. result := SyntaxTree.invalidExpression;
  5283. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5284. ELSE
  5285. IF (parameter1.resolved # NIL) THEN
  5286. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5287. IF parameter0 IS SyntaxTree.Value THEN
  5288. result.SetResolved(parameter0(SyntaxTree.Value));
  5289. END;
  5290. END;
  5291. result.SetAssignable(parameter1.assignable);
  5292. END;
  5293. END;
  5294. (* ---- SYSTEM.GET ----- *)
  5295. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5296. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5297. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5298. actualParameters.SetExpression(0,parameter0);
  5299. END;
  5300. (* ---- SYSTEM.PUT ----- *)
  5301. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5302. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5303. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5304. actualParameters.SetExpression(0,parameter0);
  5305. END;
  5306. (* ---- SYSTEM.PUT64 ----- *)
  5307. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5308. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5309. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5310. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5311. actualParameters.SetExpression(0,parameter0);
  5312. actualParameters.SetExpression(1,parameter1);
  5313. END;
  5314. (* ---- SYSTEM.PUT32 ----- *)
  5315. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5316. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5317. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5318. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5319. actualParameters.SetExpression(0,parameter0);
  5320. actualParameters.SetExpression(1,parameter1);
  5321. END;
  5322. (* ---- SYSTEM.PUT16 ----- *)
  5323. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5324. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5325. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5326. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5327. actualParameters.SetExpression(0,parameter0);
  5328. actualParameters.SetExpression(1,parameter1);
  5329. END;
  5330. (* ---- SYSTEM.PUT8 ----- *)
  5331. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5332. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5333. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5334. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5335. actualParameters.SetExpression(0,parameter0);
  5336. actualParameters.SetExpression(1,parameter1);
  5337. END;
  5338. (* ---- SYSTEM.MOVE ----- *)
  5339. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5340. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5341. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5342. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5343. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5344. actualParameters.SetExpression(0,parameter0);
  5345. actualParameters.SetExpression(1,parameter1);
  5346. actualParameters.SetExpression(2,parameter2);
  5347. END;
  5348. (* ---- SYSTEM.NEW ----- *)
  5349. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5350. IF ~IsPointerType(parameter0.type) THEN
  5351. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5352. ELSIF CheckSizeType(parameter1) THEN
  5353. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5354. actualParameters.SetExpression(1,parameter1);
  5355. END;
  5356. (* ----SYSTEM.REF ---- *)
  5357. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5358. type := system.addressType
  5359. (* ---- INCR ----- *)
  5360. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5361. type := system.sizeType;
  5362. base := type0;
  5363. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5364. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5365. IF i1 < 0 THEN
  5366. Error(position,Diagnostics.Invalid,"invalid dimension");
  5367. base := SyntaxTree.invalidType;
  5368. ELSE
  5369. base := ArrayBase(base,i1);
  5370. IF (base # NIL) & Indexable(base) THEN
  5371. ELSE
  5372. Error(position,Diagnostics.Invalid,"len on no array");
  5373. IF VerboseErrorMessage THEN
  5374. Printout.Info("base",base);
  5375. END;
  5376. base := SyntaxTree.invalidType;
  5377. END;
  5378. END;
  5379. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5380. actualParameters.SetExpression(1,parameter1);
  5381. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5382. mathArrayType := base(SyntaxTree.MathArrayType);
  5383. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5384. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5385. type := system.longintType;
  5386. END;
  5387. END;
  5388. ELSE
  5389. type := system.longintType;
  5390. END;
  5391. (* ---- SUM ----- *)
  5392. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5393. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5394. (* ---- ALL ----- *)
  5395. ELSIF (id = Global.All) & CheckArity(2,3) THEN (* can only be found by overloading *)
  5396. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5397. (* ---- DIM ----- *)
  5398. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5399. type := system.sizeType;
  5400. IF type0 IS SyntaxTree.MathArrayType THEN
  5401. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5402. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5403. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5404. END;
  5405. ELSE
  5406. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5407. END;
  5408. (* ---- CAS ----- *)
  5409. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5410. IF type0.IsComposite () THEN
  5411. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5412. result := SyntaxTree.invalidExpression;
  5413. ELSIF ~IsVariable (parameter0) THEN
  5414. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5415. result := SyntaxTree.invalidExpression;
  5416. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5417. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5418. result := SyntaxTree.invalidExpression;
  5419. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5420. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5421. result := SyntaxTree.invalidExpression;
  5422. ELSE
  5423. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5424. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5425. type := type0;
  5426. END;
  5427. (* ---- RESHAPE ----- *)
  5428. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5429. IF type0 IS SyntaxTree.MathArrayType THEN
  5430. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5431. base := ArrayBase(type0,MAX(LONGINT));
  5432. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5433. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5434. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5435. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5436. IF ~CompatibleTo(system,type0,parameterType) THEN
  5437. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5438. result := SyntaxTree.invalidExpression;
  5439. ELSE
  5440. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5441. END;
  5442. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5443. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5444. IF ~CompatibleTo(system,type1,parameterType) THEN
  5445. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5446. result := SyntaxTree.invalidExpression;
  5447. ELSE
  5448. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5449. END;
  5450. ELSE
  5451. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5452. result := SyntaxTree.invalidExpression;
  5453. END;
  5454. (* ---- SYSTEM.TYPECODE ----- *)
  5455. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5456. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5457. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5458. type := type.resolved;
  5459. IF type IS SyntaxTree.PointerType THEN
  5460. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5461. END;
  5462. IF ~(type IS SyntaxTree.RecordType) THEN
  5463. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5464. END;
  5465. ELSE
  5466. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5467. END;
  5468. type := system.addressType;
  5469. (* -------- FLT --------- *)
  5470. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5471. type := system.realType;
  5472. IF IsRealValue(parameter0, r) THEN
  5473. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5474. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5475. i0 := i; i := ABS(i);
  5476. IF i # 0 THEN
  5477. i1 := 23;
  5478. IF i >= 2*800000H THEN
  5479. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5480. ELSIF i < 800000H THEN
  5481. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5482. END;
  5483. i := (i1 + 127)*800000H - 800000H + i;
  5484. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5485. END;
  5486. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5487. END;
  5488. (* ------- CONNECT -------*)
  5489. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5490. IF ~(currentIsCellNet) THEN
  5491. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5492. END;
  5493. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5494. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5495. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5496. END;
  5497. IF numberActualParameters = 3 THEN
  5498. IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5499. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5500. END;
  5501. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5502. actualParameters.SetExpression(2,parameter2);
  5503. END;
  5504. activeCellsStatement := TRUE;
  5505. (* ---------- DELEGATE --------*)
  5506. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5507. IF ~(currentIsCellNet) THEN
  5508. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5509. END;
  5510. IF ~CheckPortType(parameter1, inPort) THEN
  5511. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5512. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5513. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5514. ELSIF (outPort.direction # inPort.direction) THEN
  5515. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5516. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5517. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5518. END;
  5519. activeCellsStatement := TRUE;
  5520. (* --------- RECEIVE ---------*)
  5521. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5522. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5523. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5524. IF inPort.direction # SyntaxTree.InPort THEN
  5525. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5526. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5527. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5528. END;
  5529. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5530. IF ~SameType(parameter2.type, system.integerType) THEN
  5531. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5532. END;
  5533. END;
  5534. END;
  5535. (* --------- SEND ---------*)
  5536. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5537. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5538. IF CheckPortType(parameter0,outPort) THEN
  5539. IF outPort.direction # SyntaxTree.OutPort THEN
  5540. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5541. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5542. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5543. ELSE
  5544. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5545. actualParameters.SetExpression(1,parameter1);
  5546. END;
  5547. END;
  5548. (* ------- custom builtins ----- *)
  5549. ELSIF id = Global.systemSpecial THEN
  5550. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5551. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5552. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5553. type := procedureType.returnType;
  5554. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5555. (* go through all formal parameters *)
  5556. formalParameter := procedureType.firstParameter;
  5557. FOR i := 0 TO actualParameters.Length() - 1 DO
  5558. actualParameter := actualParameters.GetExpression(i);
  5559. IF actualParameter = SyntaxTree.invalidExpression THEN
  5560. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5561. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5562. ELSE
  5563. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5564. END;
  5565. actualParameters.SetExpression(i, actualParameter);
  5566. formalParameter := formalParameter.nextParameter
  5567. END
  5568. END
  5569. ELSE
  5570. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5571. result := SyntaxTree.invalidExpression;
  5572. END;
  5573. END;
  5574. IF result # SyntaxTree.invalidExpression THEN
  5575. type := ResolveType(type);
  5576. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5577. result.SetType(type);
  5578. END;
  5579. RETURN result
  5580. END NewBuiltinCallDesignator;
  5581. (** return type guard designator left(type)
  5582. - check if type can be extended (i.e. is no static record)
  5583. - check if type is a type extension of left.type
  5584. - returns new type guard designator
  5585. returns invalidDesignator = invalidExpression if error
  5586. **)
  5587. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type): SyntaxTree.Designator;
  5588. VAR result: SyntaxTree.Designator;
  5589. BEGIN
  5590. result := SyntaxTree.invalidDesignator;
  5591. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5592. Error(position,Diagnostics.Invalid,"no type extension of type");
  5593. IF VerboseErrorMessage THEN
  5594. Printout.Info("left",left);
  5595. Printout.Info("type",type);
  5596. END;
  5597. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5598. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5599. ELSE
  5600. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5601. result.SetType(type);
  5602. result.SetAssignable(left.assignable);
  5603. END;
  5604. RETURN result
  5605. END NewTypeGuardDesignator;
  5606. (** check and resolve parameter designator left(expression list)
  5607. - check expression list
  5608. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5609. - elsif left is a procedure type then
  5610. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5611. - else return is a procedure call then return ProcedureCallDesignator
  5612. returns invalidDesignator = invalidExpression if error
  5613. **)
  5614. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5615. VAR
  5616. parameters: SyntaxTree.ExpressionList;
  5617. left: SyntaxTree.Designator;
  5618. result,expression: SyntaxTree.Expression;
  5619. typeDeclaration: SyntaxTree.TypeDeclaration;
  5620. type, expressionType: SyntaxTree.Type;
  5621. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5622. BEGIN
  5623. type := type.resolved;
  5624. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5625. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5626. END;
  5627. RETURN type
  5628. END BaseType;
  5629. BEGIN
  5630. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5631. result := SyntaxTree.invalidDesignator;
  5632. left := ResolveDesignator(designator.left);
  5633. IF left # SyntaxTree.invalidDesignator THEN
  5634. parameters := designator.parameters;
  5635. IF ExpressionList(parameters) THEN
  5636. IF (left.type = NIL) THEN
  5637. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5638. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5639. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5640. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5641. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5642. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5643. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5644. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left);
  5645. ELSE
  5646. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5647. END
  5648. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5649. expression := parameters.GetExpression(0);
  5650. type := typeDeclaration.declaredType.resolved;
  5651. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5652. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5653. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5654. OR (expressionType IS SyntaxTree.EnumerationType)
  5655. ) THEN
  5656. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5657. ELSE
  5658. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5659. END;
  5660. ELSE
  5661. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5662. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5663. result := SyntaxTree.invalidDesignator;
  5664. END;
  5665. ELSE
  5666. result := SyntaxTree.invalidDesignator
  5667. END;
  5668. END;
  5669. resolvedExpression := result;
  5670. END VisitParameterDesignator;
  5671. (** check dereference designator left^
  5672. - check if left is pointer type or left is object type
  5673. - return new dereference designator with type = left.baseType.type (if appropriate)
  5674. with error handling
  5675. returns invalidDesignator = invalidExpression if error
  5676. **)
  5677. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5678. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5679. BEGIN
  5680. result := SyntaxTree.invalidDesignator;
  5681. type := left.type;
  5682. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5683. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5684. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5685. result.SetAssignable(TRUE);
  5686. result.SetType(type);
  5687. result.SetHidden(left.isHidden);
  5688. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5689. type := type.resolved;
  5690. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5691. result.SetAssignable(TRUE);
  5692. result.SetType(type);
  5693. result.SetHidden(left.isHidden);
  5694. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5695. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5696. result.SetAssignable(TRUE);
  5697. result.SetType(type);
  5698. result.SetHidden(left.isHidden);
  5699. ELSE
  5700. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5701. IF VerboseErrorMessage THEN
  5702. Printout.Info("pointer", type);
  5703. Printout.Info("scope", currentScope);
  5704. END;
  5705. END;
  5706. RETURN result
  5707. END NewDereferenceDesignator;
  5708. (** check supercall designator left^
  5709. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5710. - return new supercall designator with type = left.type
  5711. with error handling
  5712. **)
  5713. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5714. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5715. objectScope: SyntaxTree.Scope;
  5716. BEGIN
  5717. result := SyntaxTree.invalidDesignator;
  5718. IF left = SyntaxTree.invalidDesignator THEN
  5719. (* error already handled *)
  5720. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5721. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5722. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5723. IF symbol IS SyntaxTree.Procedure THEN
  5724. procedure := symbol(SyntaxTree.Procedure);
  5725. objectScope := currentScope;
  5726. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5727. objectScope := objectScope.outerScope;
  5728. END;
  5729. IF (left.left = NIL) OR ~
  5730. (
  5731. (left.left IS SyntaxTree.SelfDesignator) OR
  5732. (left.left IS SyntaxTree.DereferenceDesignator)
  5733. & (left.left(SyntaxTree.Designator).left # NIL)
  5734. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5735. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5736. IF VerboseErrorMessage THEN
  5737. Printout.Info("left.left",left.left);
  5738. END;
  5739. ELSIF procedure.super # NIL THEN
  5740. result := SyntaxTree.NewSupercallDesignator(position,left);
  5741. result.SetType(left.type.resolved)
  5742. ELSE
  5743. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5744. END;
  5745. ELSE
  5746. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5747. END;
  5748. ELSE
  5749. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5750. END;
  5751. RETURN result
  5752. END NewSupercallDesignator;
  5753. (** check and semantically resolve arrow designator left^
  5754. - if left is procedure type -> result := SupercallDesignator
  5755. - else result := DereferenceDesignator
  5756. returns result via global variable resolvedExpression
  5757. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5758. **)
  5759. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5760. VAR left: SyntaxTree.Designator;
  5761. BEGIN
  5762. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5763. left := ResolveDesignator(arrowDesignator.left);
  5764. IF left # NIL THEN
  5765. IF (left.type = NIL) THEN
  5766. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5767. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5768. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5769. ELSE
  5770. IF IsPointerToObject(left.type) THEN
  5771. Warning(arrowDesignator.position, "forbidden dereference on object");
  5772. END;
  5773. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5774. END
  5775. END
  5776. END VisitArrowDesignator;
  5777. (** check and return expression
  5778. - if expression has no type then resolve expression
  5779. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5780. - return result
  5781. **)
  5782. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5783. VAR result,prev: SyntaxTree.Expression;
  5784. BEGIN
  5785. IF expression = NIL THEN result := NIL
  5786. ELSIF (expression.type = NIL) THEN
  5787. prev := resolvedExpression;
  5788. resolvedExpression := SyntaxTree.invalidExpression;
  5789. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5790. expression.SetType(SyntaxTree.invalidType);
  5791. END;
  5792. expression.Accept(SELF);
  5793. result := resolvedExpression;
  5794. IF currentIsRealtime THEN
  5795. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5796. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5797. END;
  5798. END;
  5799. (* designator modifiers for backends if they support it ...*)
  5800. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5801. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5802. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5803. END;
  5804. resolvedExpression := prev
  5805. ELSE
  5806. result := expression
  5807. END;
  5808. RETURN result
  5809. END ResolveExpression;
  5810. (**
  5811. check expression to be constant expression
  5812. - resolve expression
  5813. - if valid then check that of value type
  5814. report error and return invalidExpression if anything fails
  5815. **)
  5816. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5817. VAR position: LONGINT;
  5818. BEGIN
  5819. position := expression.position;
  5820. expression := ResolveExpression(expression);
  5821. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5822. ELSIF (expression.resolved = NIL) THEN
  5823. Error(position,Diagnostics.Invalid,"expression is not constant");
  5824. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5825. expression := SyntaxTree.invalidExpression;
  5826. END;
  5827. RETURN expression
  5828. END ConstantExpression;
  5829. (** check expression to be constant integer
  5830. - resolve expresssion
  5831. - if valid then check that of integer value type
  5832. report error and return invalidExpression if anything fails
  5833. **)
  5834. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5835. VAR position: LONGINT;
  5836. BEGIN
  5837. position := expression.position;
  5838. expression := ResolveExpression(expression);
  5839. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5840. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5841. expression := SyntaxTree.invalidExpression;
  5842. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5843. END;
  5844. RETURN expression
  5845. END ConstantInteger;
  5846. (** check expression as positive (>=0) constant integer
  5847. - resolve expression
  5848. - if valid then check that integer value
  5849. - if integer value then check that value >= 0
  5850. report error and return invalidExpression if anything fails
  5851. **)
  5852. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5853. VAR position: LONGINT;
  5854. BEGIN
  5855. position := expression.position;
  5856. expression := ConstantExpression(expression);
  5857. IF expression = SyntaxTree.invalidExpression THEN
  5858. (* error already reported *)
  5859. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5860. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5861. expression := SyntaxTree.invalidExpression
  5862. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5863. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5864. END;
  5865. RETURN expression
  5866. END ConstantIntegerGeq0;
  5867. (** check expression as condition
  5868. - resolve expression
  5869. - if valid expression then check that result type is boolean
  5870. report error and return invalidExpression if anything fails
  5871. **)
  5872. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5873. VAR position: LONGINT;
  5874. BEGIN
  5875. position := expression.position;
  5876. expression := ResolveExpression(expression);
  5877. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5878. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5879. expression := SyntaxTree.invalidExpression;
  5880. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5881. END;
  5882. RETURN expression
  5883. END ResolveCondition;
  5884. (*** symbols ***)
  5885. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5886. BEGIN
  5887. x.Accept(SELF);
  5888. END ResolveSymbol;
  5889. (** check a symbol
  5890. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5891. **)
  5892. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5893. VAR scope: SyntaxTree.Scope;
  5894. BEGIN
  5895. (* visibility *)
  5896. scope := symbol.scope;
  5897. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5898. scope := scope.outerScope;
  5899. END;
  5900. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5901. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5902. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5903. IF VerboseErrorMessage THEN
  5904. Printout.Info("symbol",symbol);
  5905. END;
  5906. END;
  5907. END;
  5908. END CheckSymbolVisibility;
  5909. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5910. If node is currently being resolved then emit a cyclic definition error.
  5911. Return TRUE only if node is fully resolved.
  5912. **)
  5913. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5914. VAR result: BOOLEAN;
  5915. BEGIN
  5916. IF SyntaxTree.Resolved IN x.state THEN
  5917. result := FALSE
  5918. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5919. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5920. result := FALSE;
  5921. ELSE
  5922. result := TRUE;
  5923. x.SetState(SyntaxTree.BeingResolved)
  5924. END;
  5925. RETURN result
  5926. END SymbolNeedsResolution;
  5927. (** check and resolve a type declaration symbol = Type
  5928. - set type to declaration type
  5929. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5930. This is so because the type declaration itself does not have a type but it only stands for a type.
  5931. In the implementation of the compiler this made a lot much easier.
  5932. - resolve and set declared type
  5933. - check symbol
  5934. **)
  5935. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5936. VAR prevScope: SyntaxTree.Scope;
  5937. BEGIN
  5938. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5939. IF SymbolNeedsResolution(typeDeclaration) THEN
  5940. prevScope := currentScope;
  5941. currentScope := typeDeclaration.scope;
  5942. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5943. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5944. CheckSymbolVisibility(typeDeclaration);
  5945. typeDeclaration.SetState(SyntaxTree.Resolved);
  5946. currentScope := prevScope;
  5947. END;
  5948. END VisitTypeDeclaration;
  5949. (** check and resolve a constant declaration symbol = (constant) expression
  5950. - check expression
  5951. - set type and value
  5952. - check symbol
  5953. **)
  5954. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  5955. VAR
  5956. expression: SyntaxTree.Expression;
  5957. type: SyntaxTree.Type;
  5958. name: Basic.SegmentedName;
  5959. replacement: Replacement;
  5960. stringReader: Streams.StringReader;
  5961. scanner: Scanner.Scanner; parser: Parser.Parser;
  5962. BEGIN
  5963. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  5964. IF SymbolNeedsResolution(constant) THEN
  5965. expression := constant.value;
  5966. IF replacements # NIL THEN
  5967. Global.GetSymbolSegmentedName(constant, name);
  5968. replacement := replacements;
  5969. WHILE (replacement # NIL) & (replacement.name # name) DO
  5970. replacement := replacement.next;
  5971. END;
  5972. IF replacement # NIL THEN
  5973. InfoSS(constant.position, "replacing constant", constant.name);
  5974. (*
  5975. NEW(stringReader, Strings.Length(replacement.string^));
  5976. stringReader.Set(replacement.string^);
  5977. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5978. NEW(parser, scanner, diagnostics);
  5979. expression := parser.Expression();
  5980. *)
  5981. expression := replacement.expression;
  5982. replacement.used := TRUE;
  5983. END;
  5984. END;
  5985. constant.SetType(SyntaxTree.invalidType);
  5986. expression := ConstantExpression(expression);
  5987. ASSERT(expression.type # NIL);
  5988. type := expression.type.resolved;
  5989. constant.SetType(type);
  5990. constant.SetValue(expression);
  5991. CheckSymbolVisibility(constant);
  5992. constant.SetState(SyntaxTree.Resolved);
  5993. END;
  5994. END VisitConstant;
  5995. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  5996. VAR procedureAlignment: LONGINT;
  5997. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  5998. (* least common multiple *)
  5999. VAR a,b: LONGINT;
  6000. BEGIN
  6001. a := a0; b := b0;
  6002. WHILE (a # b) DO
  6003. IF a < b THEN a := a+a0
  6004. ELSE b := b + b0
  6005. END;
  6006. END;
  6007. RETURN a
  6008. END LCM;
  6009. BEGIN
  6010. IF alignment > 1 THEN
  6011. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6012. IF (procedureAlignment > 1) THEN
  6013. alignment := LCM(alignment, procedureAlignment);
  6014. END;
  6015. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6016. END;
  6017. END AdaptStackAlignment;
  6018. (** check and resolve a variable / field
  6019. - check and set type
  6020. - negative check on open array type
  6021. - check symbol
  6022. **)
  6023. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6024. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6025. BEGIN
  6026. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6027. IF SymbolNeedsResolution(variable) THEN
  6028. modifiers := variable.modifiers;
  6029. (*
  6030. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6031. variable.AddFlags(flags);
  6032. *)
  6033. variable.SetType(ResolveType(variable.type));
  6034. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6035. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6036. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6037. END;
  6038. END;
  6039. CheckSymbolVisibility(variable);
  6040. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6041. variable.SetUntraced(TRUE);
  6042. IF ~ContainsPointer(variable.type) THEN
  6043. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6044. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6045. END;
  6046. END;
  6047. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6048. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6049. IF ~PowerOf2(value) THEN
  6050. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6051. ELSE
  6052. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6053. END;
  6054. END;
  6055. variable.SetAlignment(FALSE,value);
  6056. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6057. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6058. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6059. END;
  6060. variable.SetAlignment(TRUE, value);
  6061. END;
  6062. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6063. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6064. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6065. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6066. END;
  6067. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6068. IF variable.initializer # NIL THEN
  6069. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6070. END;
  6071. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6072. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6073. pointerType.SetPointerBase(variable.type);
  6074. pointerType.SetHidden(TRUE);
  6075. variable.SetType(ResolveType(pointerType));
  6076. END;
  6077. variable.SetState(SyntaxTree.Resolved);
  6078. END;
  6079. END VisitVariable;
  6080. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6081. BEGIN
  6082. VisitVariable(property);
  6083. END VisitProperty;
  6084. (** check and resolve a (procedure) parameter
  6085. - check and set type
  6086. - check symbol
  6087. - check parameter kind and set read-only flags if appropriate
  6088. **)
  6089. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6090. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6091. BEGIN
  6092. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6093. IF SymbolNeedsResolution(parameter) THEN
  6094. modifiers := parameter.modifiers;
  6095. parameter.SetType(ResolveType(parameter.type));
  6096. ASSERT(parameter.type.resolved # NIL);
  6097. CheckSymbolVisibility(parameter);
  6098. IF parameter.defaultValue # NIL THEN
  6099. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6100. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6101. ELSE
  6102. expression := ConstantExpression(parameter.defaultValue);
  6103. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6104. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6105. parameter.SetDefaultValue(expression);
  6106. END;
  6107. END;
  6108. END;
  6109. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6110. parameter.SetUntraced(TRUE);
  6111. IF ~ContainsPointer(parameter.type) THEN
  6112. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6113. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6114. END;
  6115. END;
  6116. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6117. parameter.SetMoveable(TRUE);
  6118. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6119. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6120. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6121. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6122. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6123. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6124. END;
  6125. END;
  6126. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6127. parameter.SetState(SyntaxTree.Resolved);
  6128. END;
  6129. END VisitParameter;
  6130. (** check and resolve a procedure (with declaration and implementation scope)
  6131. - check the procedure type
  6132. - check if method (i.e. in record scope), if so then
  6133. - check if (unique) constructor
  6134. - check if (unique) finalizer
  6135. - check if super method available, if so then check signature
  6136. - of not in record scope then negative check on constructor flag
  6137. - of not in record scope then negative check on finalizer flag
  6138. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6139. - check procedure symbol
  6140. **)
  6141. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6142. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6143. procedureType: SyntaxTree.ProcedureType;
  6144. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6145. qualifiedType: SyntaxTree.QualifiedType;
  6146. value: LONGINT;
  6147. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6148. position: LONGINT;
  6149. BEGIN
  6150. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6151. IF IsOberonInline(procedure) THEN
  6152. IF SyntaxTree.Public * procedure.access # {} THEN
  6153. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6154. END;
  6155. procedure.SetInline(FALSE);
  6156. procedure.SetOberonInline(TRUE);
  6157. END;
  6158. IF SymbolNeedsResolution(procedure) THEN
  6159. recentIsRealtime := currentIsRealtime;
  6160. recentIsBodyProcedure := currentIsBodyProcedure;
  6161. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6162. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6163. modifiers := procedureType.modifiers;
  6164. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6165. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6166. IF useDarwinCCalls THEN (*fld*)
  6167. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6168. ELSE
  6169. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6170. END
  6171. END;
  6172. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6173. procedureType.SetInterrupt(TRUE);
  6174. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6175. END;
  6176. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6177. procedureType.SetNoReturn(TRUE);
  6178. END;
  6179. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6180. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6181. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6182. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6183. END;
  6184. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6185. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6186. END;
  6187. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6188. IF ~PowerOf2(value) THEN
  6189. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6190. ELSE
  6191. procedureType.SetStackAlignment(value)
  6192. END;
  6193. END;
  6194. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6195. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6196. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6197. END;
  6198. CheckModifiers(modifiers, TRUE);
  6199. modifiers := procedureType.returnTypeModifiers;
  6200. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6201. CheckModifiers(modifiers, TRUE);
  6202. FixProcedureType(procedureType);
  6203. currentIsRealtime := procedureType.isRealtime;
  6204. currentIsBodyProcedure := procedure.isBodyProcedure;
  6205. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6206. THEN
  6207. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6208. END;
  6209. CheckSymbolVisibility(procedure);
  6210. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6211. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6212. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6213. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6214. END;
  6215. END;
  6216. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6217. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6218. procedureType.SetDelegate(TRUE);
  6219. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6220. IF record.pointerType.typeDeclaration = NIL THEN
  6221. selfParameter.SetType(record.pointerType);
  6222. ELSE
  6223. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6224. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6225. qualifiedType.SetResolved(record.pointerType);
  6226. selfParameter.SetType(qualifiedType);
  6227. END;
  6228. selfParameter.SetAccess(SyntaxTree.Hidden);
  6229. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6230. IF procedure.isConstructor THEN
  6231. (*! constructor is always visible, compatibility to paco
  6232. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6233. *)
  6234. procedure.MarkUsed;
  6235. IF procedureType.returnType # NIL THEN
  6236. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6237. END;
  6238. proc := procedure.scope.firstProcedure;
  6239. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6240. proc := proc.nextProcedure;
  6241. END;
  6242. IF proc # NIL THEN
  6243. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6244. ELSE
  6245. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6246. END;
  6247. END;
  6248. IF procedure.isFinalizer THEN
  6249. procedure.MarkUsed;
  6250. IF procedureType.returnType # NIL THEN
  6251. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6252. END;
  6253. IF procedureType.numberParameters # 0 THEN
  6254. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6255. END;
  6256. proc := procedure.scope.firstProcedure;
  6257. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6258. proc := proc.nextProcedure;
  6259. END;
  6260. IF proc # NIL THEN
  6261. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6262. ELSE
  6263. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6264. END;
  6265. END;
  6266. super := FindSuperProcedure(record.recordScope, procedure);
  6267. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6268. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6269. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6270. END;
  6271. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6272. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6273. END;
  6274. IF super.isFinal THEN
  6275. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6276. END;
  6277. procedure.SetSuper(super);
  6278. super.SetOverwritten(TRUE);
  6279. procedure.SetAccess(procedure.access+super.access);
  6280. procedure.MarkUsed;
  6281. END;
  6282. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6283. THEN
  6284. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6285. END;
  6286. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6287. IF cellsAreObjects THEN
  6288. procedureType.SetDelegate(TRUE);
  6289. END;
  6290. IF procedure.isConstructor THEN
  6291. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6292. END;
  6293. ELSIF procedure.isConstructor THEN
  6294. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6295. END;
  6296. Declarations(procedure.procedureScope);
  6297. (* body resolution part done as late fix of the procedure type *)
  6298. procedure.SetState(SyntaxTree.Resolved);
  6299. currentIsRealtime := recentIsRealtime;
  6300. currentIsBodyProcedure := recentIsBodyProcedure;
  6301. END;
  6302. END VisitProcedure;
  6303. (**
  6304. a builtin procedure is a global item that may not be modified locally
  6305. instead the resolving of builtin procedure calls are done in the NewBuiltinCallDesignator
  6306. **)
  6307. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6308. VAR type: SyntaxTree.Type;
  6309. BEGIN
  6310. type := ResolveType(builtinProcedure.type);
  6311. END VisitBuiltin;
  6312. (* nopov *)
  6313. (** check and resolve operator
  6314. - operators are first checked as procedures
  6315. - then additional operator-specific checks are done
  6316. - note that only module-scope operators are checked here
  6317. (operators in a record scope are only allowed in the context of
  6318. array-structured object types and checked in 'ResolveArrayStructure')
  6319. - also note that inter-operator conformity is not checked here
  6320. **)
  6321. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6322. VAR
  6323. procedureType: SyntaxTree.ProcedureType;
  6324. leftType, rightType: SyntaxTree.Type;
  6325. identifierNumber, position: LONGINT;
  6326. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6327. modifiers: SyntaxTree.Modifier;
  6328. (** whether a type is locally defined in the current module scope
  6329. for arrays, the base type must be locally defined **)
  6330. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6331. BEGIN
  6332. IF type = NIL THEN
  6333. RETURN FALSE
  6334. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6335. RETURN TRUE
  6336. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6337. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6338. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6339. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6340. ELSE
  6341. RETURN FALSE
  6342. END
  6343. END IsLocallyDefined;
  6344. BEGIN
  6345. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6346. procedureType := operator.type(SyntaxTree.ProcedureType);
  6347. modifiers := procedureType.modifiers;
  6348. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6349. CheckModifiers(modifiers, TRUE);
  6350. VisitProcedure(operator);
  6351. IF operator.scope IS SyntaxTree.RecordScope THEN
  6352. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6353. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6354. IF identifierNumber = -1 THEN
  6355. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6356. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6357. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6358. ELSE
  6359. IF procedureType.numberParameters < 1 THEN
  6360. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6361. ELSIF procedureType.numberParameters > 2 THEN
  6362. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6363. ELSE
  6364. (* determine operand types *)
  6365. leftType := procedureType.firstParameter.type;
  6366. IF procedureType.numberParameters > 1 THEN
  6367. rightType := procedureType.firstParameter.nextParameter.type
  6368. ELSE
  6369. rightType := NIL
  6370. END;
  6371. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6372. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6373. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6374. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6375. END
  6376. END;
  6377. (* TODO: refine the checks, think about how restrictive the checks should be
  6378. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6379. They might be used for intersection, union, complement of custom object types *)
  6380. (* defaults *)
  6381. hasReturnType := TRUE;
  6382. mustBeUnary := FALSE;
  6383. mustBeBinary := FALSE;
  6384. mustReturnBoolean := FALSE;
  6385. mustReturnInteger := FALSE;
  6386. mustHaveEquitypedOperands := FALSE;
  6387. (* operator-specific exceptions *)
  6388. CASE identifierNumber OF
  6389. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6390. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6391. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6392. mustBeBinary := TRUE
  6393. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6394. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6395. | Scanner.Times: mustBeBinary := TRUE
  6396. | Scanner.TimesTimes: mustBeBinary := TRUE
  6397. | Scanner.DotTimes: mustBeBinary := TRUE
  6398. | Scanner.PlusTimes: mustBeBinary := TRUE
  6399. | Scanner.Slash: mustBeBinary := TRUE
  6400. | Scanner.Backslash: mustBeBinary := TRUE
  6401. | Scanner.DotSlash: mustBeBinary := TRUE
  6402. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6403. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6404. | Scanner.Not: mustBeUnary := TRUE
  6405. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6406. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6407. | Scanner.Transpose: mustBeUnary := TRUE;
  6408. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6409. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6410. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6411. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6412. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6413. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6414. | Global.Abs: mustBeUnary := TRUE;
  6415. | Global.Ash: (* TODO: arity? *)
  6416. | Global.Cap: (* TODO: arity? *)
  6417. | Global.Chr: mustBeUnary := TRUE;
  6418. | Global.Entier: (* TODO: arity? *)
  6419. | Global.EntierH: (* TODO: arity? *)
  6420. | Global.Len: (* unary and binary *)
  6421. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6422. | Global.Max, Global.Min: (* unary and binary *)
  6423. | Global.Odd: (* TODO: arity? *)
  6424. | Global.Sum: (* TODO: arity? *)
  6425. | Global.All: (* TODO: arity? *)
  6426. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6427. | Scanner.Alias:
  6428. | Scanner.GreaterGreater, Scanner.LessLess:
  6429. mustBeBinary := TRUE;
  6430. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6431. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6432. END;
  6433. (* check parameter count *)
  6434. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6435. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6436. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6437. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6438. END;
  6439. (* check parameter types *)
  6440. (* TODO: is this used at all? *)
  6441. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6442. leftType := procedureType.firstParameter.type;
  6443. rightType := procedureType.firstParameter.nextParameter.type;
  6444. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6445. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6446. END
  6447. END;
  6448. (* check return type *)
  6449. IF hasReturnType THEN
  6450. IF procedureType.returnType = NIL THEN
  6451. Error(operator.position, Diagnostics.Invalid, "return type required")
  6452. ELSIF mustReturnBoolean THEN
  6453. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6454. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6455. END
  6456. ELSIF mustReturnInteger THEN
  6457. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6458. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6459. END
  6460. END
  6461. ELSIF procedureType.returnType # NIL THEN
  6462. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6463. END
  6464. END
  6465. END
  6466. END
  6467. END VisitOperator;
  6468. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6469. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6470. BEGIN
  6471. IF error THEN RETURN FALSE END;
  6472. prevScope := currentScope;
  6473. prevDiagnostics := diagnostics;
  6474. diagnostics := NIL; (* suppress error output *)
  6475. currentScope := module.moduleScope;
  6476. VisitImport(x);
  6477. IF ~error THEN
  6478. module.moduleScope.AddImport(x);
  6479. x.SetScope(module.moduleScope);
  6480. END;
  6481. currentScope := prevScope;
  6482. diagnostics := prevDiagnostics;
  6483. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6484. END AddImport;
  6485. (** check and resolve import
  6486. - check for name = SYSTEM
  6487. - check for forbidden self import
  6488. - search through global import cache: already imported?
  6489. - check if already imported indirectly
  6490. - import if necessary -> set module and enter into import cache
  6491. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6492. - after this import this direct import and all indirect imports are stored in the current module's import list
  6493. **)
  6494. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6495. VAR
  6496. module: SyntaxTree.Module;
  6497. moduleScope: SyntaxTree.ModuleScope;
  6498. import,reimport: SyntaxTree.Import;
  6499. filename: FileName;
  6500. prevScope: SyntaxTree.Scope;
  6501. BEGIN
  6502. IF SymbolNeedsResolution(x) THEN
  6503. prevScope := currentScope;
  6504. x.SetType(SyntaxTree.importType);
  6505. moduleScope := currentScope.ownerModule.moduleScope;
  6506. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6507. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6508. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6509. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6510. ELSE
  6511. (* search through global import list: already imported ? *)
  6512. IF (x.module = NIL) & (importCache # NIL) THEN
  6513. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6514. ELSE import := NIL
  6515. END;
  6516. IF x.module # NIL THEN (* already imported indirectly *)
  6517. module := x.module;
  6518. ELSIF import # NIL THEN (* already in module list *)
  6519. module := import.module;
  6520. ASSERT(module # NIL);
  6521. x.SetModule(module);
  6522. ELSE (* must be imported *)
  6523. Global.ModuleFileName(x.moduleName,x.context,filename);
  6524. IF symbolFileFormat # NIL THEN
  6525. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6526. IF module = NIL THEN
  6527. ErrorSS(x.position,"could not import",filename);
  6528. IF VerboseErrorMessage THEN
  6529. Printout.Info("import",x)
  6530. END
  6531. ELSE
  6532. (*
  6533. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6534. (*! should rather be done by importer *)
  6535. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6536. checker.importCache := importCache;
  6537. checker.arrayBaseImported := arrayBaseImported;
  6538. checker.global := global;
  6539. checker.Module(module); (* semantic check *)
  6540. error := error OR checker.error;
  6541. END;
  6542. *)
  6543. (*
  6544. ASSERT(SyntaxTree.Resolved IN module.state);
  6545. *)
  6546. x.SetModule(module);
  6547. IF importCache # NIL THEN
  6548. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6549. import.SetContext(x.context);
  6550. import.SetModule(module);
  6551. importCache.AddImport(import);
  6552. END;
  6553. END;
  6554. ELSE
  6555. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6556. END;
  6557. END;
  6558. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6559. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6560. END;
  6561. import := module.moduleScope.firstImport;
  6562. WHILE(import # NIL) DO
  6563. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6564. ASSERT(currentScope # NIL);
  6565. ASSERT(currentScope.ownerModule # NIL);
  6566. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6567. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6568. Error(x.position,Diagnostics.Invalid,"recursive import");
  6569. ELSE
  6570. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6571. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6572. IF reimport = NIL THEN (* indirect import *)
  6573. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6574. reimport.SetContext(import.context);
  6575. reimport.SetModule(import.module);
  6576. moduleScope.AddImport(reimport);
  6577. reimport.SetScope(moduleScope);
  6578. ELSE
  6579. ASSERT(import.module # NIL);
  6580. reimport.SetModule(import.module); (* direct or indirect import *)
  6581. END;
  6582. END;
  6583. import := import.nextImport;
  6584. END;
  6585. END;
  6586. END;
  6587. currentScope := prevScope;
  6588. (* ELSE nothing to be done *)
  6589. x.SetState(SyntaxTree.Resolved);
  6590. END;
  6591. END VisitImport;
  6592. (*** statements ***)
  6593. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6594. VAR prev,resolved: SyntaxTree.Statement;
  6595. BEGIN
  6596. prev := resolvedStatement;
  6597. resolvedStatement := x;
  6598. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6599. activeCellsStatement := FALSE;
  6600. x.Accept(SELF);
  6601. (* removed this, implementation restriction should be resolved by backend
  6602. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6603. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6604. END;
  6605. *)
  6606. resolved := resolvedStatement;
  6607. resolvedStatement := prev;
  6608. RETURN resolved
  6609. END ResolveStatement;
  6610. (** check and resolve statement sequence
  6611. - check all statements, replace if necessary
  6612. **)
  6613. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6614. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6615. BEGIN
  6616. IF statementSequence # NIL THEN (* else empty *)
  6617. FOR i := 0 TO statementSequence.Length()-1 DO
  6618. statement := statementSequence.GetStatement(i);
  6619. resolved := ResolveStatement(statement);
  6620. IF (resolved # statement) THEN
  6621. statementSequence.SetStatement(i,resolved);
  6622. END;
  6623. END;
  6624. END;
  6625. END StatementSequence;
  6626. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6627. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6628. - check if procedure is callable
  6629. - check return type = NIL (otherwise must be assignment statement)
  6630. **)
  6631. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6632. VAR call: SyntaxTree.Designator;
  6633. BEGIN
  6634. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6635. call := procedureCall.call;
  6636. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6637. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6638. END;
  6639. call := ResolveDesignator(call);
  6640. IF call = SyntaxTree.invalidDesignator THEN
  6641. (* error already handled *)
  6642. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6643. (* inline call in a statement *)
  6644. ELSIF ~IsCallable(call) THEN
  6645. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6646. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6647. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6648. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6649. END;
  6650. procedureCall.SetCall(call);
  6651. (*
  6652. IF call = SyntaxTree.invalidDesignator THEN
  6653. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6654. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6655. IF IsOberonInline(procedure) THEN
  6656. Warning(procedure.position,"call to inline proc");
  6657. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6658. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6659. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6660. resolvedStatement := block;
  6661. RETURN;
  6662. END;
  6663. END;
  6664. *)
  6665. END VisitProcedureCallStatement;
  6666. (** check and resolve assignment LHS := RHS
  6667. - resolve LHS and RHS
  6668. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6669. - check if assignment is compatible
  6670. - check if LHS is variable (i.e. assignable)
  6671. - convert RHS if necessary
  6672. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6673. - assignment between different ASOTs
  6674. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6675. - assignment to ASOT elements:
  6676. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6677. **)
  6678. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6679. VAR
  6680. left: SyntaxTree.Designator;
  6681. right, expression: SyntaxTree.Expression;
  6682. designator: SyntaxTree.Designator;
  6683. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6684. mathArrayType: SyntaxTree.MathArrayType;
  6685. BEGIN
  6686. right := ResolveExpression(assignment.right);
  6687. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6688. left := ResolveDesignator(assignment.left);
  6689. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6690. (* error already handled *)
  6691. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6692. (* LHS is index write operator call on ASOT *)
  6693. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6694. (* necessary ?
  6695. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6696. type := procedureType.firstParameter.type;
  6697. expression := procedureCallDesignator.parameters.GetExpression(0);
  6698. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6699. *)
  6700. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6701. ELSIF CheckVariable(left) THEN
  6702. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6703. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6704. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6705. (* conversion done by procedure call
  6706. (* try to convert to left argument *)
  6707. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6708. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6709. procedureCallDesignator.parameters.SetExpression(1, right);
  6710. END;
  6711. *)
  6712. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6713. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6714. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6715. ELSIF AssignmentCompatible(left, right) THEN
  6716. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6717. mathArrayType := MathArrayStructureOfType(left.type);
  6718. right := NewConversion(right.position, right, mathArrayType, NIL);
  6719. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6720. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6721. ELSE
  6722. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6723. assignment.SetLeft(left);
  6724. assignment.SetRight(right);
  6725. resolvedStatement := assignment
  6726. END
  6727. END
  6728. END
  6729. END VisitAssignment;
  6730. (** check and resolve assignment LHS := RHS
  6731. - resolve LHS and RHS
  6732. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6733. - check if assignment is compatible
  6734. - check if LHS is variable (i.e. assignable)
  6735. - convert RHS if necessary
  6736. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6737. - assignment between different ASOTs
  6738. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6739. - assignment to ASOT elements:
  6740. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6741. **)
  6742. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6743. VAR
  6744. left: SyntaxTree.Designator;
  6745. right: SyntaxTree.Expression;
  6746. inPort, outPort: SyntaxTree.PortType;
  6747. BEGIN
  6748. right := ResolveExpression(communication.right);
  6749. left := ResolveDesignator(communication.left);
  6750. communication.SetLeft(left);
  6751. communication.SetRight(right);
  6752. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6753. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6754. (* error already handled *)
  6755. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6756. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6757. IF outPort.direction # SyntaxTree.OutPort THEN
  6758. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6759. ELSIF outPort.sizeInBits # system.SizeOf(right.type) THEN
  6760. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6761. ELSE
  6762. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6763. communication.SetRight(right)
  6764. END;
  6765. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6766. IF CheckVariable(left) THEN
  6767. IF inPort.direction # SyntaxTree.InPort THEN
  6768. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6769. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6770. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6771. END;
  6772. END;
  6773. ELSE
  6774. Error(communication.position, -1, "unsupported stream operation");
  6775. END;
  6776. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6777. IF outPort.direction # SyntaxTree.OutPort THEN
  6778. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6779. ELSIF outPort.sizeInBits # system.SizeOf(right.type) THEN
  6780. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6781. ELSE
  6782. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6783. communication.SetRight(right)
  6784. END;
  6785. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6786. IF CheckVariable(right) THEN
  6787. IF inPort.direction # SyntaxTree.InPort THEN
  6788. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6789. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6790. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6791. END;
  6792. END;
  6793. ELSE
  6794. Error(communication.position, -1, "unsupported operation");
  6795. END
  6796. END VisitCommunicationStatement;
  6797. (** check and resolve if/eslif part
  6798. - check condition
  6799. - check statement sequence
  6800. **)
  6801. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6802. VAR prevUnreachable, b: BOOLEAN;
  6803. BEGIN
  6804. prevUnreachable := currentIsUnreachable;
  6805. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6806. IF IsBooleanValue(ifPart.condition,b) THEN
  6807. IF b=FALSE THEN
  6808. currentIsUnreachable := TRUE
  6809. ELSIF b=TRUE THEN
  6810. true := TRUE
  6811. END;
  6812. END;
  6813. StatementSequence(ifPart.statements);
  6814. currentIsUnreachable := prevUnreachable;
  6815. END IfPart;
  6816. (** check and resolve if statement
  6817. - check if parts and else part statement sequence
  6818. **)
  6819. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6820. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6821. BEGIN
  6822. prevUnreachable := currentIsUnreachable;
  6823. ifPartTrue := FALSE;
  6824. IfPart(ifStatement.ifPart,ifPartTrue);
  6825. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6826. elsif := ifStatement.GetElsifPart(i);
  6827. IfPart(elsif,ifPartTrue);
  6828. END;
  6829. IF ifStatement.elsePart # NIL THEN
  6830. IF ifPartTrue THEN
  6831. currentIsUnreachable := TRUE
  6832. END;
  6833. StatementSequence(ifStatement.elsePart)
  6834. END;
  6835. currentIsUnreachable := prevUnreachable;
  6836. END VisitIfStatement;
  6837. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6838. VAR variable: SyntaxTree.Designator;
  6839. type,variableType: SyntaxTree.Type;
  6840. withEntry: WithEntry;
  6841. BEGIN
  6842. variable := ResolveDesignator(withPart.variable);
  6843. variableType := variable.type.resolved;
  6844. withPart.SetVariable(variable);
  6845. type := ResolveType(withPart.type);
  6846. withPart.SetType(type);
  6847. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6848. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6849. END;
  6850. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6851. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6852. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6853. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6854. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6855. IF VerboseErrorMessage THEN
  6856. Printout.Info("variable",variable)
  6857. END;
  6858. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6859. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6860. IF VerboseErrorMessage THEN
  6861. Printout.Info("variable",variable);
  6862. Printout.Info("type",type);
  6863. END;
  6864. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6865. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6866. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6867. IF VerboseErrorMessage THEN
  6868. Printout.Info("variable",variable);
  6869. Printout.Info("type",type);
  6870. END;
  6871. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6872. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6873. ELSE
  6874. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6875. NEW(withEntry);
  6876. withEntry.previous := withEntries;
  6877. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6878. withEntry.type := type;
  6879. withEntries := withEntry;
  6880. StatementSequence(withPart.statements);
  6881. withEntries := withEntries.previous;
  6882. END;
  6883. END WithPart;
  6884. (** check and resolve with statement WITH variable: type DO ... END;
  6885. - check type and variable
  6886. - check that variable type is type extension of type
  6887. - check that variable is a variable
  6888. - enter new with scope and enter guardedVariable with same name and reference to variable
  6889. - create if statement:
  6890. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6891. **)
  6892. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6893. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6894. BEGIN
  6895. prevScope := currentScope; symbol := NIL;
  6896. FOR i := 0 TO withStatement.WithParts()-1 DO
  6897. WithPart(withStatement.GetWithPart(i),symbol);
  6898. END;
  6899. IF withStatement.elsePart # NIL THEN
  6900. StatementSequence(withStatement.elsePart)
  6901. END;
  6902. currentScope := prevScope;
  6903. END VisitWithStatement;
  6904. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6905. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6906. - check 'first' < 'last' and no overlaps between different case labels
  6907. - check statement sequence
  6908. **)
  6909. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6910. VAR
  6911. i: LONGINT;
  6912. position: LONGINT;
  6913. expression, left, right: SyntaxTree.Expression;
  6914. expressionType: SyntaxTree.Type;
  6915. l, r: LONGINT;
  6916. cl, cr: CHAR;
  6917. thiscases: SyntaxTree.CaseConstant;
  6918. BEGIN
  6919. thiscases := NIL;
  6920. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6921. expression := casePart.elements.GetExpression(i);
  6922. position := expression.position;
  6923. (* set context of range *)
  6924. IF expression IS SyntaxTree.RangeExpression THEN
  6925. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6926. END;
  6927. expression := ResolveExpression(expression);
  6928. IF expression = SyntaxTree.invalidExpression THEN
  6929. (* error already reported *)
  6930. expressionType := SyntaxTree.invalidType;
  6931. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6932. (* read out 'first' and 'last' *)
  6933. left := expression(SyntaxTree.RangeExpression).first;
  6934. right := expression(SyntaxTree.RangeExpression).last;
  6935. (* guaranteed by VisitRangeExpression: *)
  6936. ASSERT((left # NIL) & (right # NIL));
  6937. ASSERT(left.type.resolved = right.type.resolved);
  6938. left := CompatibleConversion(left.position, left, type);
  6939. right := CompatibleConversion(right.position, right, type);
  6940. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6941. expression(SyntaxTree.RangeExpression).SetLast(right);
  6942. expressionType := RegularType(position,left.type);
  6943. ELSE
  6944. expression := ConstantExpression(expression);
  6945. expression := CompatibleConversion(expression.position, expression, type);
  6946. (*
  6947. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6948. left := Global.NewCharacterValue(system,expression.position,cl);
  6949. expression := casePart.elements.GetExpression(i);
  6950. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6951. expression := left
  6952. END;
  6953. *)
  6954. casePart.elements.SetExpression(i,expression);
  6955. left := expression; right := expression;
  6956. expressionType := RegularType(position,expression.type)
  6957. END;
  6958. IF (expressionType = SyntaxTree.invalidType) THEN
  6959. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6960. Error(position,Diagnostics.Invalid,"inadmissible case label");
  6961. expression := SyntaxTree.invalidExpression;
  6962. ELSE
  6963. l := 0; r := 0;
  6964. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6965. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6966. l := ORD(cl); r := ORD(cr);
  6967. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6968. ELSE
  6969. expression := SyntaxTree.invalidExpression
  6970. END;
  6971. IF expression # SyntaxTree.invalidExpression THEN
  6972. IF l>r THEN
  6973. Error(position,Diagnostics.Invalid,"empty case label")
  6974. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6975. Error(position,Diagnostics.Invalid,"duplicate case label");
  6976. ELSE
  6977. IF l < min THEN min := l END;
  6978. IF r > max THEN max := r END;
  6979. END;
  6980. END;
  6981. END;
  6982. casePart.elements.SetExpression(i,expression);
  6983. END;
  6984. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  6985. casePart.SetConstants(thiscases);
  6986. StatementSequence(casePart.statements);
  6987. END CasePart;
  6988. (** check and resolve case statement CASE variable OF ... END;
  6989. - check variable
  6990. - check case parts
  6991. **)
  6992. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  6993. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  6994. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  6995. BEGIN
  6996. expression := ResolveExpression(caseStatement.variable);
  6997. type := RegularType(expression.position,expression.type);
  6998. IF type = SyntaxTree.invalidType THEN
  6999. expression := SyntaxTree.invalidExpression;
  7000. ELSIF IsIntegerType(type) THEN
  7001. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7002. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7003. (*
  7004. expression := Global.NewCharacterValue(system,expression.position,ch);
  7005. *)
  7006. type := expression.type;
  7007. ELSIF IsCharacterType(type) THEN
  7008. ELSIF IsEnumerationType(type) THEN
  7009. ELSE
  7010. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7011. expression := SyntaxTree.invalidExpression;
  7012. END;
  7013. caseStatement.SetVariable(expression);
  7014. caseList := NIL;
  7015. min := MAX(LONGINT); max := MIN(LONGINT);
  7016. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7017. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7018. END;
  7019. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7020. msg := "huge sparse case table ";
  7021. Strings.AppendInt(msg, max-min);
  7022. Strings.Append(msg,"/");
  7023. Strings.AppendInt(msg, caseStatement.CaseParts());
  7024. Warning(caseStatement.position,msg);
  7025. END;
  7026. caseStatement.SetMinMax(min,max);
  7027. StatementSequence(caseStatement.elsePart);
  7028. IF expression.resolved # NIL THEN
  7029. IF IsCharacterValue(expression,ch) THEN
  7030. l := ORD(ch)
  7031. ELSIF IsIntegerValue(expression,l) THEN
  7032. END;
  7033. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7034. END;
  7035. END VisitCaseStatement;
  7036. (** check and resolve while statement
  7037. - check condition
  7038. - check statement sequence
  7039. **)
  7040. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7041. VAR prevIsUnreachable,b: BOOLEAN;
  7042. BEGIN
  7043. prevIsUnreachable := currentIsUnreachable;
  7044. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7045. IF IsBooleanValue(whileStatement.condition,b) THEN
  7046. IF b=FALSE THEN
  7047. currentIsUnreachable := TRUE
  7048. END;
  7049. END;
  7050. StatementSequence(whileStatement.statements);
  7051. currentIsUnreachable := prevIsUnreachable
  7052. END VisitWhileStatement;
  7053. (** check and resolve repeat statement
  7054. - check condition
  7055. - check statement sequence
  7056. **)
  7057. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7058. BEGIN
  7059. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7060. StatementSequence(repeatStatement.statements);
  7061. END VisitRepeatStatement;
  7062. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7063. VAR withEntry: WithEntry;
  7064. BEGIN
  7065. withEntry := withEntries;
  7066. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7067. withEntry := withEntry.previous
  7068. END;
  7069. IF withEntry = NIL THEN RETURN FALSE
  7070. ELSE
  7071. type := withEntry.type;
  7072. RETURN TRUE
  7073. END;
  7074. END GetGuard;
  7075. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7076. - check that variable is an integer variable
  7077. - check that from is integer typed with compatible type
  7078. - check that to has compatible type
  7079. - check that by is constant integer with compatible type
  7080. **)
  7081. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7082. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7083. BEGIN
  7084. designator := ResolveDesignator(forStatement.variable);
  7085. type := SyntaxTree.invalidType;
  7086. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7087. designator := SyntaxTree.invalidDesignator;
  7088. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7089. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7090. designator := SyntaxTree.invalidDesignator;
  7091. ELSIF CheckVariable(designator) THEN
  7092. type := designator.type;
  7093. END;
  7094. forStatement.SetVariable(designator);
  7095. expression := ResolveExpression(forStatement.from);
  7096. IF expression = SyntaxTree.invalidExpression THEN
  7097. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7098. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7099. expression := SyntaxTree.invalidExpression;
  7100. ELSIF type # SyntaxTree.invalidType THEN
  7101. expression := NewConversion(expression.position,expression,type,NIL)
  7102. END;
  7103. forStatement.SetFrom(expression);
  7104. expression := ResolveExpression(forStatement.to);
  7105. IF expression = SyntaxTree.invalidExpression THEN
  7106. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7107. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7108. expression := SyntaxTree.invalidExpression;
  7109. ELSIF type # SyntaxTree.invalidType THEN
  7110. expression := NewConversion(expression.position,expression,type,NIL)
  7111. END;
  7112. forStatement.SetTo(expression);
  7113. IF forStatement.by # NIL THEN
  7114. expression := ConstantInteger(forStatement.by);
  7115. ELSE
  7116. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7117. END;
  7118. IF expression = SyntaxTree.invalidExpression THEN
  7119. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7120. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7121. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7122. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7123. ELSIF type # SyntaxTree.invalidType THEN
  7124. expression := NewConversion(expression.position,expression,type,NIL)
  7125. END;
  7126. forStatement.SetBy(expression);
  7127. StatementSequence(forStatement.statements);
  7128. END VisitForStatement;
  7129. (** check and resolve loop statement LOOP StatementSequence END
  7130. - check statement sequence
  7131. **)
  7132. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7133. BEGIN
  7134. StatementSequence(loopStatement.statements)
  7135. END VisitLoopStatement;
  7136. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7137. BEGIN
  7138. StatementSequence(exitableBlock.statements);
  7139. END VisitExitableBlock;
  7140. (** check and resolve exit statement EXIT
  7141. - check that exit is within LOOP statement block
  7142. **)
  7143. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7144. VAR outer: SyntaxTree.Statement;
  7145. BEGIN
  7146. outer := exitStatement.outer;
  7147. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7148. outer := outer.outer;
  7149. END;
  7150. IF outer = NIL THEN
  7151. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7152. END;
  7153. END VisitExitStatement;
  7154. (** check and resolve return statement RETURN [expression]
  7155. - check expression (if any)
  7156. - check if in procedure scope
  7157. - if in procedure scope then check expression compatibility
  7158. - if not in procecdure scope then check on return without expression
  7159. **)
  7160. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7161. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7162. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7163. BEGIN
  7164. position := returnStatement.position;
  7165. expression := returnStatement.returnValue;
  7166. IF expression # NIL THEN
  7167. expression := ResolveExpression(expression);
  7168. returnStatement.SetReturnValue(expression);
  7169. END;
  7170. outer := returnStatement.outer;
  7171. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7172. outer := outer.outer
  7173. END;
  7174. IF (outer # NIL) THEN
  7175. scope := outer(SyntaxTree.Body).inScope;
  7176. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7177. IF (expression # NIL) THEN
  7178. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7179. END;
  7180. ELSE
  7181. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7182. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7183. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7184. END;
  7185. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7186. IF returnType # NIL THEN
  7187. returnType := returnType.resolved;
  7188. IF expression = NIL THEN
  7189. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7190. ELSIF expression.type = NIL THEN
  7191. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7192. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7193. Error(position,Diagnostics.Invalid,"return type not compatible");
  7194. IF VerboseErrorMessage THEN
  7195. Printout.Info("returnType",returnType);
  7196. Printout.Info("expression",expression);
  7197. END;
  7198. ELSE
  7199. expression := NewConversion(expression.position,expression,returnType,NIL);
  7200. returnStatement.SetReturnValue(expression);
  7201. END;
  7202. ELSIF expression # NIL THEN
  7203. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7204. END;
  7205. END;
  7206. END;
  7207. END VisitReturnStatement;
  7208. (** check and resolve await statement AWAIT(condition: Expression)
  7209. - check await condition
  7210. **)
  7211. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7212. VAR condition: SyntaxTree.Expression;
  7213. BEGIN
  7214. condition := ResolveCondition(awaitStatement.condition);
  7215. IF currentIsRealtime THEN
  7216. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7217. END;
  7218. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7219. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7220. END;
  7221. awaitStatement.SetCondition(condition);
  7222. END VisitAwaitStatement;
  7223. PROCEDURE CheckSystemImport(position: LONGINT);
  7224. VAR import: SyntaxTree.Import;
  7225. BEGIN
  7226. import := currentScope.ownerModule.moduleScope.firstImport;
  7227. WHILE(import # NIL) DO
  7228. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7229. RETURN;
  7230. END;
  7231. import := import.nextImport;
  7232. END;
  7233. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7234. END CheckSystemImport;
  7235. (** check and resolve code statement: do nothing, must be done by assembler
  7236. **)
  7237. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7238. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7239. BEGIN
  7240. CheckSystemImport(code.position);
  7241. FOR i := 0 TO code.inRules.Length()-1 DO
  7242. statement := code.inRules.GetStatement(i);
  7243. IF statement IS SyntaxTree.Assignment THEN
  7244. WITH statement: SyntaxTree.Assignment DO
  7245. statement.SetRight(ResolveExpression(statement.right));
  7246. END;
  7247. ELSE
  7248. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7249. END;
  7250. END;
  7251. FOR i := 0 TO code.outRules.Length()-1 DO
  7252. statement := code.outRules.GetStatement(i);
  7253. IF statement IS SyntaxTree.Assignment THEN
  7254. WITH statement: SyntaxTree.Assignment DO
  7255. statement.SetLeft(ResolveDesignator(statement.left));
  7256. END;
  7257. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7258. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7259. ELSE
  7260. Printout.Info("out statement ", statement);
  7261. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7262. END;
  7263. END;
  7264. END VisitCode;
  7265. (** check and set flags of a statement block
  7266. - check for multiply occurence of a flag
  7267. - check and set priority only in bodies
  7268. - check for valid names
  7269. **)
  7270. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7271. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7272. flag: LONGINT; recordBody: SyntaxTree.Body;
  7273. PROCEDURE SetProtectedRecord;
  7274. VAR scope: SyntaxTree.Scope;
  7275. BEGIN
  7276. scope := currentScope;
  7277. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7278. scope := scope.outerScope
  7279. END;
  7280. IF scope # NIL THEN
  7281. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7282. END;
  7283. END SetProtectedRecord;
  7284. BEGIN
  7285. flags := {};
  7286. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7287. recordBody := block(SyntaxTree.Body)
  7288. ELSE
  7289. recordBody := NIL
  7290. END;
  7291. blockModifier := block.blockModifiers;
  7292. WHILE(blockModifier # NIL) DO
  7293. name := blockModifier.identifier;
  7294. expression := blockModifier.expression;
  7295. position := blockModifier.position;
  7296. flag := -1;
  7297. IF name=Global.NamePriority THEN
  7298. IF expression = NIL THEN
  7299. Error(position,Diagnostics.Invalid,"missing priority expression");
  7300. ELSIF recordBody = NIL THEN
  7301. Error(position,Diagnostics.Invalid,"priority not on record body");
  7302. ELSIF recordBody.priority # NIL THEN
  7303. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7304. ELSE
  7305. recordBody.SetPriority(expression);
  7306. END;
  7307. ELSIF expression # NIL THEN
  7308. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7309. ELSIF name=Global.NameExclusive THEN
  7310. IF block.isExclusive THEN
  7311. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7312. END;
  7313. block.SetExclusive(TRUE); SetProtectedRecord;
  7314. ELSIF name=Global.NameActive THEN
  7315. IF recordBody = NIL THEN
  7316. Error(position,Diagnostics.Invalid,"active not in record body");
  7317. ELSIF recordBody.isActive THEN
  7318. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7319. ELSE
  7320. recordBody.SetActive(TRUE); SetProtectedRecord;
  7321. END;
  7322. ELSIF name=Global.NameSafe THEN
  7323. IF recordBody = NIL THEN
  7324. Error(position,Diagnostics.Invalid,"safe not in record body");
  7325. ELSIF recordBody.isSafe THEN
  7326. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7327. ELSE
  7328. recordBody.SetSafe(TRUE);
  7329. SetProtectedRecord;
  7330. END;
  7331. ELSIF name=Global.NameRealtime THEN
  7332. IF recordBody = NIL THEN
  7333. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7334. ELSIF recordBody.isRealtime THEN
  7335. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7336. ELSE
  7337. recordBody.SetRealtime(TRUE);
  7338. block.SetRealtime(TRUE);
  7339. END;
  7340. ELSIF name=Global.NameUnchecked THEN
  7341. IF block.isUnchecked THEN
  7342. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7343. ELSE
  7344. block.SetUnchecked(TRUE);
  7345. END;
  7346. ELSIF (name=Global.NameUncooperative) THEN
  7347. IF block.isUncooperative THEN
  7348. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7349. ELSE
  7350. block.SetUncooperative(TRUE);
  7351. END;
  7352. ELSE
  7353. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7354. END;
  7355. blockModifier := blockModifier.nextModifier;
  7356. END;
  7357. END BlockFlags;
  7358. (** check and resolve statement block
  7359. - check flags (exclusive)
  7360. - check statement sequence
  7361. **)
  7362. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7363. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7364. BEGIN
  7365. BlockFlags(statementBlock);
  7366. IF statementBlock.isExclusive THEN
  7367. (* check that not in exclusive block *)
  7368. IF currentIsExclusive THEN
  7369. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7370. ELSIF currentIsRealtime THEN
  7371. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7372. END;
  7373. END;
  7374. recentExclusive := currentIsExclusive;
  7375. recentUnreachable := currentIsUnreachable;
  7376. recentRealtime := currentIsRealtime;
  7377. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7378. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7379. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7380. StatementSequence(statementBlock.statements);
  7381. currentIsRealtime := recentRealtime;
  7382. currentIsExclusive := recentExclusive;
  7383. currentIsUnreachable := recentUnreachable;
  7384. END VisitStatementBlock;
  7385. (** check and resolve body
  7386. - check flags (active, priority, safe)
  7387. - check body and finally part
  7388. **)
  7389. PROCEDURE Body(body: SyntaxTree.Body);
  7390. BEGIN
  7391. VisitStatementBlock(body);
  7392. IF body.isActive THEN
  7393. IF ~currentIsBodyProcedure THEN
  7394. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7395. ELSIF body.priority # NIL THEN
  7396. body.SetPriority(ConstantInteger(body.priority));
  7397. END;
  7398. ELSIF body.isSafe THEN
  7399. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7400. ELSIF body.priority # NIL THEN
  7401. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7402. END;
  7403. IF body.code # NIL THEN
  7404. CheckSystemImport(body.position);
  7405. END;
  7406. StatementSequence(body.finally)
  7407. END Body;
  7408. (*** scopes ***)
  7409. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7410. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7411. VAR duplicateSymbol: BOOLEAN;
  7412. BEGIN
  7413. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7414. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7415. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7416. END;
  7417. scope.EnterSymbol(symbol,duplicateSymbol);
  7418. IF ~allowDuplicate & duplicateSymbol THEN
  7419. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7420. IF VerboseErrorMessage THEN
  7421. Printout.Info("multiply defined identifier",symbol);
  7422. Printout.Info("in scope",scope);
  7423. END;
  7424. END;
  7425. END Register;
  7426. (**
  7427. implementation: check and resolve an implementation part
  7428. **)
  7429. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7430. move implementation checker to a separate object ? *)
  7431. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7432. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7433. BEGIN
  7434. prevIsRealtime := currentIsRealtime;
  7435. prevIsBodyProcedure := currentIsBodyProcedure;
  7436. prevIsCellNet := currentIsCellNet;
  7437. prevScope := currentScope;
  7438. currentScope := scope;
  7439. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7440. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7441. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7442. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7443. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7444. (*
  7445. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7446. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7447. END;
  7448. *)
  7449. END;
  7450. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7451. (* module body, record bodies are wrapped into an artifical procedure *)
  7452. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7453. Body(scope(SyntaxTree.ProcedureScope).body)
  7454. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7455. Body(scope(SyntaxTree.ProcedureScope).body)
  7456. END;
  7457. END;
  7458. currentScope := prevScope;
  7459. currentIsRealtime := prevIsRealtime;
  7460. currentIsBodyProcedure := prevIsBodyProcedure;
  7461. currentIsCellNet := prevIsCellNet;
  7462. END Implementation;
  7463. (** implementation phase:
  7464. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7465. **)
  7466. PROCEDURE Implementations(x: SyntaxTree.Module);
  7467. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7468. BEGIN
  7469. prevPhase := phase;
  7470. phase := InlinePhase;
  7471. scope := x.firstScope;
  7472. WHILE(scope # NIL) DO
  7473. Implementation(scope);
  7474. scope := scope.nextScope;
  7475. END;
  7476. phase := ImplementationPhase;
  7477. scope := x.firstScope;
  7478. WHILE(scope # NIL) DO
  7479. Implementation(scope);
  7480. scope := scope.nextScope;
  7481. END;
  7482. phase := prevPhase;
  7483. END Implementations;
  7484. (** declaration phase:
  7485. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7486. - import lists (for module scopes)
  7487. - parameter list (for procedure scopes)
  7488. - constant declarations
  7489. - type declarations
  7490. - variable declarations
  7491. - procedure declarations
  7492. preformed in two stages:
  7493. - first all symbols are entered into the symbol table (with uniqueness check),
  7494. - then all symbols are resolved
  7495. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7496. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7497. **)
  7498. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7499. VAR
  7500. constant: SyntaxTree.Constant;
  7501. typeDeclaration: SyntaxTree.TypeDeclaration;
  7502. variable: SyntaxTree.Variable;
  7503. procedure: SyntaxTree.Procedure;
  7504. prevScope: SyntaxTree.Scope;
  7505. parameter: SyntaxTree.Parameter;
  7506. import: SyntaxTree.Import;
  7507. symbol: SyntaxTree.Symbol;
  7508. prevPhase: LONGINT;
  7509. prevError : BOOLEAN;
  7510. property: SyntaxTree.Property;
  7511. type: SyntaxTree.Type;
  7512. atype : SyntaxTree.ArrayType;
  7513. BEGIN
  7514. prevError := error;
  7515. prevPhase := phase;
  7516. phase := DeclarationPhase;
  7517. prevScope := currentScope;
  7518. currentScope := scope;
  7519. error := FALSE;
  7520. (* first enter all symbols in scope *)
  7521. IF scope IS SyntaxTree.ModuleScope THEN
  7522. (* treat imports first for a module scope, , set default context if necessary *)
  7523. import := scope(SyntaxTree.ModuleScope).firstImport;
  7524. WHILE(import # NIL) DO
  7525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7526. Register(import, currentScope, FALSE);
  7527. import := import.nextImport;
  7528. END;
  7529. import := scope(SyntaxTree.ModuleScope).firstImport;
  7530. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7531. ResolveSymbol(import);
  7532. import := import.nextImport;
  7533. END;
  7534. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7535. (* enter parameters for a procedure scope *)
  7536. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7537. WHILE(parameter # NIL) DO
  7538. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7539. END;
  7540. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7541. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7542. ELSIF scope IS SyntaxTree.CellScope THEN
  7543. parameter := scope(SyntaxTree.CellScope).ownerCell.firstParameter;
  7544. WHILE(parameter # NIL) DO
  7545. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7546. variable.SetType(parameter.type);
  7547. variable.SetAccess(SyntaxTree.Hidden);
  7548. variable.SetModifiers(parameter.modifiers);
  7549. currentScope.PushVariable(variable);
  7550. (*
  7551. Register(parameter,scope, FALSE);
  7552. *)
  7553. parameter := parameter.nextParameter;
  7554. END;
  7555. property := scope(SyntaxTree.CellScope).ownerCell.firstProperty;
  7556. WHILE (property # NIL) DO
  7557. property.SetAccess(SyntaxTree.Hidden);
  7558. currentScope.PushVariable(property);
  7559. property := property.nextProperty;
  7560. END;
  7561. END;
  7562. IF error THEN RETURN END;
  7563. (* constants *)
  7564. constant := scope.firstConstant;
  7565. WHILE (constant # NIL) DO
  7566. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7567. END;
  7568. (* type declarations *)
  7569. typeDeclaration := scope.firstTypeDeclaration;
  7570. WHILE (typeDeclaration # NIL) DO
  7571. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7572. END;
  7573. (* variables *)
  7574. variable := scope.firstVariable;
  7575. WHILE (variable # NIL) DO
  7576. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7577. END;
  7578. (* procedures *)
  7579. procedure := scope.firstProcedure;
  7580. WHILE (procedure # NIL) DO
  7581. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7582. END;
  7583. (* now process all symbols without any presumption on the order *)
  7584. symbol := scope.firstSymbol;
  7585. WHILE(symbol # NIL) DO
  7586. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7587. ResolveSymbol(symbol);
  7588. END;
  7589. symbol := symbol.nextSymbol;
  7590. END;
  7591. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7592. symbol := scope.firstSymbol;
  7593. WHILE symbol # NIL DO
  7594. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7595. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7596. pointerFixes.Add(symbol, currentScope);
  7597. END;
  7598. IF ~symbol.type.resolved.isRealtime THEN
  7599. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7600. END;
  7601. END;
  7602. symbol := symbol.nextSymbol
  7603. END;
  7604. END;
  7605. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7606. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7607. END;
  7608. IF scope.ownerModule # NIL THEN
  7609. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7610. scope.ownerModule.AddScope(scope);
  7611. END;
  7612. phase := prevPhase;
  7613. currentScope := prevScope;
  7614. error := error OR prevError;
  7615. END Declarations;
  7616. (* nopov *)
  7617. (** check if all operators from one module are compatible to the ones in the other module
  7618. - check if there are not multiple operators with the same signature
  7619. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7620. - check for all operators whose signatures are compatible, whether the return types are compatible
  7621. note that:
  7622. - the return type is not considered to be part of the signature
  7623. - two signatures are considered compatible, if all of the operands are compatible
  7624. **)
  7625. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7626. VAR
  7627. thisOperator, thatOperator: SyntaxTree.Operator;
  7628. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7629. thisParameter, thatParameter: SyntaxTree.Parameter;
  7630. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7631. i: LONGINT;
  7632. BEGIN
  7633. currentScope := thisModuleScope;
  7634. hasError := FALSE;
  7635. (* go through all operators in the other module *)
  7636. thatOperator := thatModuleScope.firstOperator;
  7637. WHILE (thatOperator # NIL) & ~hasError DO
  7638. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7639. (* the other operator is accessible *)
  7640. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7641. (* the other operator is not the conversion operator *)
  7642. (* go through all operators in this module *)
  7643. thisOperator := thisModuleScope.firstOperator;
  7644. WHILE (thisOperator # NIL) & ~hasError DO
  7645. IF thisOperator # thatOperator THEN
  7646. (* the operators are not the same *)
  7647. IF thisOperator.name = thatOperator.name THEN
  7648. (* the operators share the same identifier *)
  7649. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7650. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7651. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7652. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7653. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7654. (* both operators have the same paramter count *)
  7655. thisParameter := thisProcedureType.firstParameter;
  7656. thatParameter := thatProcedureType.firstParameter;
  7657. operandsAreEqual := TRUE;
  7658. operandsAreCompatible := TRUE;
  7659. (* go through all parameters *)
  7660. FOR i := 1 TO thisProcedureType.numberParameters DO
  7661. ASSERT(thatParameter # NIL);
  7662. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7663. operandsAreEqual := FALSE;
  7664. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7665. operandsAreCompatible := FALSE
  7666. END
  7667. END;
  7668. thisParameter := thisParameter.nextParameter;
  7669. thatParameter := thatParameter.nextParameter
  7670. END;
  7671. IF operandsAreEqual THEN
  7672. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7673. hasError := TRUE
  7674. ELSIF operandsAreCompatible THEN
  7675. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7676. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7677. hasError := TRUE
  7678. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7679. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7680. hasError := TRUE
  7681. END
  7682. END
  7683. END
  7684. END
  7685. END;
  7686. thisOperator := thisOperator.nextOperator
  7687. END
  7688. END
  7689. END;
  7690. thatOperator := thatOperator.nextOperator
  7691. END
  7692. END CheckInterOperatorConformity;
  7693. (** check module:
  7694. - check module declaration
  7695. - add context, if necessary
  7696. - remove module from import cache, if necessary
  7697. - check declarations
  7698. - resolve all type fixes
  7699. - check implementation (bodies)
  7700. **)
  7701. PROCEDURE Module*(x: SyntaxTree.Module);
  7702. VAR (* nopov *)
  7703. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7704. BEGIN
  7705. prevScope := currentScope;
  7706. prevIsCellNet := currentIsCellNet;
  7707. module := x;
  7708. ASSERT(x # NIL);
  7709. global := system.globalScope[x.case];
  7710. x.moduleScope.SetGlobalScope(global);
  7711. currentScope := global;
  7712. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7713. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7714. RemoveModuleFromCache(importCache,x);
  7715. Declarations(x.moduleScope);
  7716. FixTypes();
  7717. IF module.isCellNet THEN
  7718. currentIsCellNet := TRUE;
  7719. modifier := x.modifiers;
  7720. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7721. CheckModifiers(modifier, FALSE);
  7722. END;
  7723. (* nopov *)
  7724. IF ~error THEN
  7725. (* check if operators conform to each other within this module *)
  7726. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7727. (* go through all imports *)
  7728. import := x.moduleScope.firstImport;
  7729. WHILE import # NIL DO
  7730. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7731. (* check if all operators in this module conform to the ones of the imported module *)
  7732. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7733. END;
  7734. import := import.nextImport
  7735. END;
  7736. END;
  7737. Implementations(x);
  7738. module := NIL;
  7739. currentIsCellNet := prevIsCellNet;
  7740. currentScope := prevScope;
  7741. END Module;
  7742. END Checker;
  7743. Warnings*=OBJECT (SyntaxTree.Visitor)
  7744. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7745. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7746. BEGIN
  7747. SELF.diagnostics := diagnostics
  7748. END InitWarnings;
  7749. (** types *)
  7750. PROCEDURE Type(x: SyntaxTree.Type);
  7751. BEGIN x.Accept(SELF)
  7752. END Type;
  7753. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7754. BEGIN END VisitType;
  7755. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7756. BEGIN END VisitBasicType;
  7757. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7758. BEGIN END VisitCharacterType;
  7759. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7760. BEGIN END VisitIntegerType;
  7761. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7762. BEGIN END VisitFloatType;
  7763. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7764. BEGIN END VisitQualifiedType;
  7765. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7766. BEGIN END VisitStringType;
  7767. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7768. BEGIN END VisitEnumerationType;
  7769. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7770. BEGIN END VisitRangeType;
  7771. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7772. BEGIN
  7773. IF ~(SyntaxTree.Warned IN x.state) THEN
  7774. x.SetState(SyntaxTree.Warned);
  7775. Type(x.arrayBase);
  7776. END;
  7777. END VisitArrayType;
  7778. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7779. BEGIN
  7780. IF ~(SyntaxTree.Warned IN x.state) THEN
  7781. x.SetState(SyntaxTree.Warned);
  7782. Type(x.arrayBase);
  7783. END;
  7784. END VisitMathArrayType;
  7785. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7786. BEGIN
  7787. IF ~(SyntaxTree.Warned IN x.state) THEN
  7788. x.SetState(SyntaxTree.Warned);
  7789. Type(x.pointerBase);
  7790. END;
  7791. END VisitPointerType;
  7792. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7793. BEGIN Scope(x.recordScope) END VisitRecordType;
  7794. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7795. BEGIN Scope(x.cellScope) END VisitCellType;
  7796. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7797. BEGIN END VisitProcedureType;
  7798. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7799. VAR msg: ARRAY 256 OF CHAR;
  7800. BEGIN
  7801. Global.GetSymbolName(x,msg);
  7802. Strings.Append(msg," ");
  7803. Strings.Append(msg,text);
  7804. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7805. END Warning;
  7806. (** symbols *)
  7807. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7808. BEGIN
  7809. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7810. IF ~(x IS SyntaxTree.Parameter) THEN
  7811. Warning(x,"never used");
  7812. END;
  7813. END;
  7814. x.Accept(SELF);
  7815. END Symbol;
  7816. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7817. BEGIN END VisitSymbol;
  7818. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7819. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7820. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7821. BEGIN END VisitConstant;
  7822. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7823. BEGIN END VisitVariable;
  7824. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7825. BEGIN END VisitProperty;
  7826. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7827. BEGIN END VisitParameter;
  7828. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7829. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7830. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7831. BEGIN END VisitOperator;
  7832. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7833. BEGIN END VisitImport;
  7834. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7835. VAR
  7836. symbol: SyntaxTree.Symbol;
  7837. BEGIN
  7838. symbol := scope.firstSymbol;
  7839. WHILE(symbol # NIL) DO
  7840. Symbol(symbol);
  7841. symbol := symbol.nextSymbol;
  7842. END;
  7843. END Scope;
  7844. PROCEDURE Module*(x: SyntaxTree.Module);
  7845. BEGIN
  7846. SELF.module := x;
  7847. Scope(x.moduleScope);
  7848. END Module;
  7849. END Warnings;
  7850. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7851. BEGIN
  7852. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7853. END IsOberonInline;
  7854. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7855. BEGIN
  7856. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7857. END Resolved;
  7858. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7859. VAR i: LONGINT;
  7860. BEGIN
  7861. i := 1;
  7862. WHILE i < x DO
  7863. i := i *2
  7864. END;
  7865. RETURN i=x
  7866. END PowerOf2;
  7867. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7868. BEGIN
  7869. RETURN
  7870. (scope # NIL) &
  7871. (scope IS SyntaxTree.ModuleScope)
  7872. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7873. OR
  7874. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7875. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7876. END IsCellNetScope;
  7877. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7878. BEGIN
  7879. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7880. END IsCellScope;
  7881. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7882. BEGIN
  7883. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7884. RETURN (scope # NIL) & IsCellNetScope(scope)
  7885. END InCellNetScope;
  7886. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7887. BEGIN
  7888. ASSERT(size MOD system.dataUnit = 0);
  7889. RETURN size DIV system.dataUnit
  7890. END ToMemoryUnits;
  7891. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7892. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7893. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7894. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7895. BEGIN
  7896. IF t = NIL THEN
  7897. RETURN TRUE
  7898. ELSE
  7899. t := t.resolved;
  7900. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7901. END;
  7902. END TypeAllowed;
  7903. BEGIN
  7904. type := type.resolved;
  7905. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7906. RETURN FALSE
  7907. ELSE
  7908. procedureType := type(SyntaxTree.ProcedureType);
  7909. numberParameters := procedureType.numberParameters;
  7910. RETURN
  7911. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7912. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7913. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7914. END;
  7915. END GetProcedureAllowed;
  7916. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7917. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7918. VAR import: SyntaxTree.Import;
  7919. BEGIN
  7920. import := importCache.ImportByModuleName(x.name,x.context);
  7921. IF import # NIL THEN
  7922. importCache.RemoveImporters(x.name,x.context);
  7923. END;
  7924. END RemoveModuleFromCache;
  7925. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7926. (* to <- this assignment compatibility *)
  7927. VAR result: BOOLEAN;
  7928. BEGIN
  7929. IF this= NIL THEN result := (to=NIL)
  7930. ELSIF to=NIL THEN result := FALSE
  7931. ELSE
  7932. (*! will be replaced by this:
  7933. ELSE result := this.CompatibleTo(to.resolved);
  7934. *)
  7935. this := this.resolved; to := to.resolved;
  7936. IF to=SyntaxTree.invalidType THEN result := FALSE
  7937. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7938. ELSIF to = this THEN
  7939. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7940. ELSIF to IS SyntaxTree.BasicType THEN
  7941. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7942. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7943. result := this.CompatibleTo(to.resolved)
  7944. ELSE
  7945. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7946. END
  7947. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7948. result := to.sizeInBits = this.sizeInBits;
  7949. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7950. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7951. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7952. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7953. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7954. result := TRUE;
  7955. ELSIF to IS SyntaxTree.AnyType THEN
  7956. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  7957. ELSIF to IS SyntaxTree.ObjectType THEN
  7958. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  7959. ELSIF to IS SyntaxTree.ByteType THEN
  7960. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7961. ELSIF to IS SyntaxTree.CharacterType THEN
  7962. result := IsCharacterType(this)
  7963. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  7964. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  7965. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  7966. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  7967. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  7968. result := TRUE;
  7969. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  7970. result := TRUE;
  7971. ELSE
  7972. result := FALSE
  7973. END;
  7974. ELSIF to IS SyntaxTree.PointerType THEN
  7975. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  7976. IsPointerType(this) & (IsTypeExtension(to,this) OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  7977. & (~to.isRealtime OR this.isRealtime);
  7978. ELSIF to IS SyntaxTree.ProcedureType THEN
  7979. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  7980. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  7981. & (~to.isRealtime OR this.isRealtime)
  7982. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  7983. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  7984. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  7985. ELSIF to IS SyntaxTree.RecordType THEN
  7986. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  7987. ELSIF to IS SyntaxTree.ArrayType THEN
  7988. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  7989. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  7990. ELSIF StaticArrayCompatible(to, this) THEN
  7991. result := TRUE
  7992. ELSE
  7993. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  7994. END;
  7995. ELSIF to IS SyntaxTree.MathArrayType THEN
  7996. IF this IS SyntaxTree.MathArrayType THEN
  7997. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  7998. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7999. result := TRUE;
  8000. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8001. result := TRUE;
  8002. ELSE
  8003. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8004. END;
  8005. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8006. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8007. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8008. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8009. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8010. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8011. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8012. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8013. ELSE
  8014. result := FALSE
  8015. END;
  8016. (* an array-structured object type is compatible to the type of its array structure *)
  8017. ELSIF IsArrayStructuredObjectType(this) THEN
  8018. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8019. ELSE
  8020. result := FALSE;
  8021. END;
  8022. ELSIF to IS SyntaxTree.StringType THEN
  8023. result := FALSE;
  8024. ELSIF to IS SyntaxTree.EnumerationType THEN
  8025. result := IsEnumerationExtension(this,to);
  8026. ELSIF to IS SyntaxTree.PortType THEN
  8027. result := SameType(to, this)
  8028. ELSE
  8029. Printout.Info("CompatibleTo",to);
  8030. HALT(100); (* implement missing type check *)
  8031. END;
  8032. END;
  8033. RETURN result
  8034. END CompatibleTo;
  8035. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8036. VAR actualBase, formalBase: SyntaxTree.Type;
  8037. BEGIN
  8038. IF SameType(formal,actual) THEN
  8039. RETURN TRUE
  8040. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8041. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8042. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8043. RETURN
  8044. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8045. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8046. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8047. & StaticArrayCompatible(formalBase,actualBase)
  8048. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8049. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8050. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8051. RETURN
  8052. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8053. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8054. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8055. & StaticArrayCompatible(formalBase,actualBase)
  8056. ELSE RETURN FALSE
  8057. END;
  8058. END StaticArrayCompatible;
  8059. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8060. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8061. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8062. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8063. BEGIN
  8064. result := SameType(formal,actual);
  8065. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8066. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8067. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8068. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8069. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8070. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8071. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8072. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8073. & TC(formalBase, actualBase);
  8074. END;
  8075. RETURN result
  8076. END TC;
  8077. BEGIN
  8078. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8079. ELSE
  8080. arrayBase := formalType.arrayBase.resolved;
  8081. IF (actualType IS SyntaxTree.StringType) THEN
  8082. result := arrayBase IS SyntaxTree.CharacterType
  8083. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8084. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8085. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8086. result := TC(formalType, actualType);
  8087. ELSE
  8088. result := (arrayBase IS SyntaxTree.ByteType)
  8089. END;
  8090. END;
  8091. RETURN result
  8092. END OpenArrayCompatible;
  8093. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8094. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8095. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8096. BEGIN
  8097. IF actualType IS SyntaxTree.MathArrayType THEN
  8098. actualArray := actualType(SyntaxTree.MathArrayType);
  8099. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8100. (*
  8101. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8102. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8103. *)
  8104. actualBase := ArrayBase(actualType,Infinity);
  8105. formalBase := ArrayBase(formalType,Infinity);
  8106. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8107. ELSE
  8108. (*
  8109. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8110. *)
  8111. formalBase := Resolved(formalType.arrayBase);
  8112. actualBase := Resolved(actualArray.arrayBase);
  8113. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8114. (*
  8115. ARRAY [k] -> ARRAY [n]
  8116. *)
  8117. result := (formalType.staticLength = actualArray.staticLength)
  8118. ELSE
  8119. result := TRUE
  8120. END;
  8121. IF ~result THEN
  8122. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8123. ELSIF actualBase = NIL THEN result := FALSE
  8124. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8125. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8126. ELSE
  8127. result := SameType(formalBase,actualBase)
  8128. END;
  8129. END;
  8130. ELSE
  8131. result := FALSE
  8132. END;
  8133. RETURN result
  8134. END MathArrayCompatible;
  8135. (**
  8136. Math Array Type distance for assignments / parameter passings of the form
  8137. from -> to
  8138. variants:
  8139. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8140. allowed:
  8141. static -> static (& size match)
  8142. static -> open
  8143. static -> tensor
  8144. open -> open
  8145. open -> tensor
  8146. open -> static
  8147. tensor -> tensor
  8148. tensor -> open
  8149. tensor -> static
  8150. **)
  8151. (*! think about the metric here: is form matching more important than element type matching? *)
  8152. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8153. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8154. BEGIN
  8155. fromBase := Resolved(from.arrayBase);
  8156. toBase := Resolved(to.arrayBase);
  8157. i := Infinity;
  8158. IF from = to THEN
  8159. i := 0;
  8160. ELSIF (from.form = to.form) THEN
  8161. (* static -> static, open -> open, tensor -> tensor *)
  8162. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8163. IF fromBase = toBase THEN i := 0
  8164. ELSIF toBase = NIL THEN i := 1
  8165. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8166. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8167. ELSE
  8168. i := TypeDistance(system,fromBase, toBase, varpar);
  8169. END;
  8170. END;
  8171. ELSIF (to.form = SyntaxTree.Static) THEN
  8172. (* forbidden *)
  8173. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8174. (* static -> tensor, open -> tensor, tensor -> open *)
  8175. IF toBase=fromBase THEN i := 0;
  8176. ELSIF toBase = NIL THEN i := 1;
  8177. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8178. toBase := ArrayBase(toBase,Infinity);
  8179. IF (fromBase=toBase) THEN i := 0
  8180. ELSIF (toBase = NIL) THEN i:= 1
  8181. ELSIF (fromBase = NIL) THEN i := Infinity;
  8182. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8183. END;
  8184. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8185. fromBase := ArrayBase(fromBase,Infinity);
  8186. IF (fromBase=toBase) THEN i := 0
  8187. ELSIF (toBase = NIL) THEN i := 1
  8188. ELSIF (fromBase = NIL) THEN i := Infinity;
  8189. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8190. END;
  8191. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8192. END;
  8193. IF i # Infinity THEN INC(i,2) END;
  8194. ELSIF (from.form = SyntaxTree.Static) THEN
  8195. (* static -> open *)
  8196. IF toBase=fromBase THEN i := 0
  8197. ELSIF toBase = NIL THEN i := 1
  8198. ELSIF fromBase = NIL THEN i := Infinity
  8199. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8200. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8201. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8202. END;
  8203. IF i # Infinity THEN INC(i,1) END;
  8204. ELSE HALT(100); (* unknown case *)
  8205. END;
  8206. RETURN i;
  8207. END MathArrayTypeDistance;
  8208. (** compute and return the distance of two array types
  8209. - return the distance of the base types
  8210. **)
  8211. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8212. VAR i: LONGINT;
  8213. BEGIN
  8214. i := Infinity;
  8215. IF from = to THEN
  8216. i := 0
  8217. ELSE
  8218. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8219. (*
  8220. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8221. i := TypeDistance(from.base, to.base);
  8222. IF i >= 0 THEN INC(i) END
  8223. ELSIF (from.mode = open) & (to.mode = open) THEN
  8224. i := TypeDistance(from.base, to.base);
  8225. *)
  8226. END;
  8227. RETURN i
  8228. END ArrayTypeDistance;
  8229. (** compute the signature distance of a procedure and an actual parameter list
  8230. - if any of the parameters are not compatible, the result is infinite
  8231. - add up and return the distance over all parameters
  8232. **)
  8233. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8234. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8235. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8236. BEGIN
  8237. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8238. result := Infinity
  8239. ELSE
  8240. formalParameter := procedureType.firstParameter;
  8241. i := 0;
  8242. result := 0;
  8243. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8244. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8245. actualParameter := actualParameters.GetExpression(i);
  8246. ASSERT(formalParameter.type # NIL);
  8247. IF (actualParameter.type = NIL) THEN distance := Infinity
  8248. ELSE
  8249. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8250. END;
  8251. IF distance = Infinity THEN
  8252. result := Infinity;
  8253. ELSE
  8254. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8255. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8256. (* already handled varpar *)
  8257. (*
  8258. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8259. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8260. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8261. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8262. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8263. END;
  8264. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8265. result := Infinity
  8266. END;
  8267. *)
  8268. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8269. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8270. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8271. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8272. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8273. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8274. END;
  8275. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8276. result := Infinity
  8277. END;
  8278. ELSE
  8279. result := Infinity
  8280. END;
  8281. ELSE
  8282. INC(result,distance);
  8283. END;
  8284. END;
  8285. formalParameter := formalParameter.nextParameter; INC(i);
  8286. END;
  8287. END;
  8288. ASSERT(result >= 0);
  8289. RETURN result
  8290. END Distance;
  8291. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8292. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8293. BEGIN
  8294. IF right.numberParameters # (procedureType.numberParameters) THEN
  8295. result := Infinity
  8296. ELSE
  8297. formalParameter := procedureType.firstParameter;
  8298. rightParameter := right.firstParameter;
  8299. i := 0;
  8300. result := 0;
  8301. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8302. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8303. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8304. IF distance = Infinity THEN
  8305. result := Infinity;
  8306. ELSE
  8307. INC(result,distance);
  8308. END;
  8309. formalParameter := formalParameter.nextParameter;
  8310. rightParameter := rightParameter.nextParameter;
  8311. END;
  8312. END;
  8313. ASSERT(result >= 0);
  8314. RETURN result
  8315. END ProcedureTypeDistance;
  8316. (** compute and return the distance between two types, used for computation of signature distance
  8317. from -> to
  8318. **)
  8319. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8320. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8321. BEGIN
  8322. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8323. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8324. END;
  8325. i := Infinity;
  8326. IF from = to THEN
  8327. i := 0
  8328. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8329. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8330. i := Infinity;
  8331. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8332. i := 1
  8333. ELSIF (from IS SyntaxTree.StringType) THEN
  8334. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8335. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8336. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8337. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8338. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8339. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8340. i := 1
  8341. ELSIF (from IS SyntaxTree.NilType) THEN
  8342. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8343. (*
  8344. ELSIF (from = NoType) THEN
  8345. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8346. *)
  8347. ELSIF (from IS SyntaxTree.BasicType) THEN
  8348. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8349. IF varpar & (i # 0) THEN i := Infinity END;
  8350. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8351. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8352. ELSIF (from IS SyntaxTree.RecordType) THEN
  8353. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8354. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8355. IF to IS SyntaxTree.MathArrayType THEN
  8356. (*
  8357. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8358. i := Infinity;
  8359. ELSE
  8360. *)
  8361. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8362. (*
  8363. END;
  8364. *)
  8365. END
  8366. ELSIF (from IS SyntaxTree.PointerType) THEN
  8367. ptr := from(SyntaxTree.PointerType);
  8368. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8369. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8370. (* ELSE i := TypeDistance(ptr.base, to); *)
  8371. END
  8372. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8373. IF (to IS SyntaxTree.ProcedureType) THEN
  8374. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8375. END;
  8376. (*no procedure test, procedure must be the same*)
  8377. END;
  8378. RETURN i
  8379. END TypeDistance;
  8380. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8381. BEGIN
  8382. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8383. END IsIntegerType;
  8384. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8385. BEGIN
  8386. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8387. END IsAddressType;
  8388. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8389. BEGIN
  8390. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8391. END IsSizeType;
  8392. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8393. BEGIN
  8394. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8395. END IsSignedIntegerType;
  8396. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8397. BEGIN
  8398. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8399. END IsUnsignedIntegerType;
  8400. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8401. VAR result: BOOLEAN;
  8402. BEGIN
  8403. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8404. value := x.resolved(SyntaxTree.IntegerValue).value;
  8405. result := TRUE
  8406. ELSE
  8407. result := FALSE
  8408. END;
  8409. RETURN result
  8410. END IsIntegerValue;
  8411. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8412. VAR result: BOOLEAN;
  8413. BEGIN
  8414. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8415. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8416. result := TRUE
  8417. ELSE
  8418. result := FALSE
  8419. END;
  8420. RETURN result
  8421. END IsEnumerationValue;
  8422. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8423. VAR result: BOOLEAN;
  8424. BEGIN
  8425. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8426. value := x.resolved(SyntaxTree.RealValue).value;
  8427. result := TRUE
  8428. ELSE
  8429. result := FALSE
  8430. END;
  8431. RETURN result
  8432. END IsRealValue;
  8433. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8434. VAR result: BOOLEAN;
  8435. BEGIN
  8436. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8437. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8438. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8439. result := TRUE
  8440. ELSE
  8441. result := FALSE
  8442. END;
  8443. RETURN result
  8444. END IsComplexValue;
  8445. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8446. VAR result: BOOLEAN;
  8447. BEGIN
  8448. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8449. value := x.resolved(SyntaxTree.CharacterValue).value;
  8450. result := TRUE
  8451. ELSE
  8452. result := FALSE
  8453. END;
  8454. RETURN result
  8455. END IsCharacterValue;
  8456. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8457. VAR result: BOOLEAN;
  8458. BEGIN
  8459. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8460. value := x.resolved(SyntaxTree.BooleanValue).value;
  8461. result := TRUE
  8462. ELSE
  8463. result := FALSE
  8464. END;
  8465. RETURN result
  8466. END IsBooleanValue;
  8467. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8468. VAR result: BOOLEAN;
  8469. BEGIN
  8470. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8471. value := x.resolved(SyntaxTree.SetValue).value;
  8472. result := TRUE
  8473. ELSE
  8474. result := FALSE
  8475. END;
  8476. RETURN result
  8477. END IsSetValue;
  8478. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8479. VAR result: BOOLEAN;
  8480. BEGIN
  8481. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8482. value := x.resolved(SyntaxTree.StringValue).value;
  8483. result := TRUE
  8484. ELSE
  8485. result := FALSE
  8486. END;
  8487. RETURN result
  8488. END IsStringValue;
  8489. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8490. BEGIN
  8491. x := x.resolved;
  8492. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8493. END Indexable;
  8494. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8495. BEGIN
  8496. RETURN t1.SameType(t2.resolved);
  8497. END SameType;
  8498. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8499. BEGIN
  8500. IF t IS SyntaxTree.MathArrayType THEN
  8501. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8502. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8503. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8504. DEC(max);
  8505. END;
  8506. ELSIF t IS SyntaxTree.ArrayType THEN
  8507. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8508. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8509. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8510. END;
  8511. END;
  8512. RETURN t;
  8513. END ArrayBase;
  8514. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8515. BEGIN
  8516. type := type.resolved;
  8517. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8518. base := type(SyntaxTree.ArrayType).arrayBase;
  8519. RETURN TRUE;
  8520. END;
  8521. RETURN FALSE;
  8522. END IsOpenArray;
  8523. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8524. BEGIN
  8525. type := type.resolved;
  8526. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8527. base := type(SyntaxTree.ArrayType).arrayBase;
  8528. dim := type(SyntaxTree.ArrayType).staticLength;
  8529. RETURN TRUE
  8530. ELSE
  8531. RETURN FALSE
  8532. END;
  8533. END IsStaticArray;
  8534. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8535. BEGIN
  8536. type := type.resolved;
  8537. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8538. base := type(SyntaxTree.ArrayType).arrayBase;
  8539. RETURN TRUE
  8540. ELSE
  8541. RETURN FALSE
  8542. END;
  8543. END IsDynamicArray;
  8544. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8545. VAR i: LONGINT;
  8546. BEGIN
  8547. i := 0;
  8548. t := t.resolved;
  8549. IF t IS SyntaxTree.MathArrayType THEN
  8550. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8551. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8552. END;
  8553. ELSIF t IS SyntaxTree.ArrayType THEN
  8554. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8555. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8556. END;
  8557. END;
  8558. RETURN i
  8559. END Dimension;
  8560. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8561. BEGIN
  8562. RETURN expression.assignable;
  8563. END IsVariable;
  8564. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8565. BEGIN
  8566. IF (symbol IS SyntaxTree.Parameter) THEN
  8567. WITH symbol: SyntaxTree.Parameter DO
  8568. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8569. END;
  8570. ELSE
  8571. RETURN FALSE
  8572. END;
  8573. END IsVariableParameter;
  8574. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8575. VAR result: BOOLEAN;
  8576. BEGIN
  8577. IF type = NIL THEN result := FALSE
  8578. ELSE
  8579. type := type.resolved;
  8580. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8581. END;
  8582. RETURN result
  8583. END IsPointerType;
  8584. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8585. VAR result: BOOLEAN;
  8586. BEGIN
  8587. IF type = NIL THEN result := FALSE
  8588. ELSE
  8589. type := type.resolved;
  8590. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8591. END;
  8592. RETURN result
  8593. END IsUnsafePointer;
  8594. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8595. BEGIN
  8596. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8597. END IsDisposable;
  8598. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8599. VAR result: BOOLEAN;
  8600. BEGIN
  8601. IF type = NIL THEN result := FALSE
  8602. ELSE
  8603. type := type.resolved;
  8604. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8605. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8606. result := result OR (type IS SyntaxTree.ObjectType);
  8607. END;
  8608. RETURN result
  8609. END IsPointerToRecord;
  8610. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8611. VAR result: BOOLEAN;
  8612. BEGIN
  8613. IF type = NIL THEN result := FALSE
  8614. ELSE
  8615. type := type.resolved;
  8616. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8617. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8618. ;
  8619. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8620. result := result OR (type IS SyntaxTree.ObjectType);
  8621. END;
  8622. RETURN result
  8623. END IsPointerToObject;
  8624. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8625. BEGIN
  8626. IF type # NIL THEN
  8627. RETURN type.resolved.hasPointers
  8628. ELSE
  8629. RETURN FALSE
  8630. END;
  8631. END ContainsPointer;
  8632. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8633. BEGIN
  8634. IF type = NIL THEN RETURN FALSE END;
  8635. type := type.resolved;
  8636. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8637. END IsStringType;
  8638. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8639. BEGIN
  8640. IF type = NIL THEN RETURN FALSE END;
  8641. type := type.resolved;
  8642. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8643. END IsCharacterType;
  8644. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8645. BEGIN
  8646. IF type = NIL THEN RETURN FALSE END;
  8647. type := type.resolved;
  8648. RETURN (type IS SyntaxTree.EnumerationType)
  8649. END IsEnumerationType;
  8650. (** cf. section "Type extension (base type)" in the language report **)
  8651. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8652. VAR result: BOOLEAN;
  8653. BEGIN
  8654. ASSERT(base # NIL); ASSERT(extension # NIL);
  8655. base := base.resolved; extension := extension.resolved;
  8656. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8657. result := TRUE;
  8658. ELSE
  8659. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8660. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8661. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8662. END;
  8663. WHILE (extension # NIL) & (extension # base) DO
  8664. IF extension IS SyntaxTree.RecordType THEN
  8665. extension := extension(SyntaxTree.RecordType).baseType;
  8666. IF (extension # NIL) THEN extension := extension.resolved END;
  8667. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8668. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8669. END;
  8670. ELSE extension := NIL;
  8671. END;
  8672. END;
  8673. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8674. END;
  8675. RETURN result
  8676. END IsTypeExtension;
  8677. (** check if base is the base enumeration type of extension **)
  8678. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8679. BEGIN
  8680. base := base.resolved; extension := extension.resolved;
  8681. WHILE (extension # NIL) & (extension # base) DO
  8682. IF extension IS SyntaxTree.EnumerationType THEN
  8683. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8684. IF extension # NIL THEN extension := extension.resolved END;
  8685. ELSE
  8686. extension := NIL
  8687. END;
  8688. END;
  8689. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8690. END IsEnumerationExtension;
  8691. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8692. BEGIN
  8693. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8694. RETURN TRUE
  8695. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8696. RETURN TRUE
  8697. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8698. RETURN TRUE
  8699. ELSE
  8700. RETURN FALSE
  8701. END
  8702. END IsCallable;
  8703. (** compute and return the distance of two record types
  8704. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8705. **)
  8706. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8707. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8708. BEGIN
  8709. i := 0;
  8710. WHILE (from # NIL) & (from # to) DO
  8711. baseType := from.baseType;
  8712. IF (baseType # NIL) THEN
  8713. baseType := baseType.resolved;
  8714. IF baseType IS SyntaxTree.PointerType THEN
  8715. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8716. END;
  8717. IF baseType IS SyntaxTree.RecordType THEN
  8718. from := baseType(SyntaxTree.RecordType);
  8719. ELSE
  8720. from := NIL;
  8721. END;
  8722. ELSE
  8723. from := NIL
  8724. END;
  8725. INC(i)
  8726. END;
  8727. IF from = NIL THEN i := Infinity END;
  8728. RETURN i
  8729. END RecordTypeDistance;
  8730. (** compute and return the distance of two pointer types **)
  8731. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8732. BEGIN
  8733. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8734. RETURN Infinity;
  8735. ELSE
  8736. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8737. END;
  8738. END PointerTypeDistance;
  8739. (** check if expression contains a symbol designator pointing to a type declaration.
  8740. - if so then enter type declaration into typeDeclaration and return true else return false
  8741. **)
  8742. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8743. VAR result: BOOLEAN;
  8744. BEGIN
  8745. result := FALSE;
  8746. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8747. result := TRUE;
  8748. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8749. END;
  8750. RETURN result
  8751. END IsTypeDesignator;
  8752. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8753. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8754. VAR result: BOOLEAN;
  8755. BEGIN
  8756. type := type.resolved;
  8757. IF type IS SyntaxTree.PointerType THEN
  8758. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8759. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8760. result := TRUE
  8761. ELSE
  8762. result := type IS SyntaxTree.RecordType
  8763. END;
  8764. RETURN result
  8765. END IsExtensibleType;
  8766. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8767. BEGIN
  8768. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8769. (d IS SyntaxTree.SymbolDesignator) &
  8770. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8771. OR
  8772. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8773. END IsUnextensibleRecord;
  8774. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8775. BEGIN
  8776. IF IsUnextensibleRecord(d) THEN
  8777. RETURN FALSE
  8778. ELSE RETURN IsExtensibleType(d.type.resolved)
  8779. END;
  8780. END IsExtensibleDesignator;
  8781. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8782. BEGIN
  8783. type := type.resolved;
  8784. IF (type IS SyntaxTree.PointerType) THEN
  8785. RETURN TRUE
  8786. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8787. RETURN TRUE
  8788. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8789. RETURN TRUE
  8790. ELSIF (type IS SyntaxTree.BasicType) THEN
  8791. RETURN TRUE
  8792. END;
  8793. RETURN FALSE
  8794. END IsBasicType;
  8795. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8796. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8797. BEGIN
  8798. baseType := record.baseType;
  8799. IF (baseType # NIL) THEN
  8800. baseType := baseType.resolved;
  8801. IF (baseType IS SyntaxTree.PointerType) THEN
  8802. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8803. END;
  8804. END;
  8805. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8806. recordType := baseType(SyntaxTree.RecordType);
  8807. ELSE
  8808. recordType := NIL;
  8809. END;
  8810. RETURN recordType
  8811. END RecordBase;
  8812. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8813. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8814. BEGIN
  8815. baseRecord := RecordBase(scope.ownerRecord);
  8816. IF baseRecord = NIL THEN RETURN NIL END;
  8817. scope := baseRecord.recordScope;
  8818. procedureType := procedure.type.resolved;
  8819. IF procedure IS SyntaxTree.Operator THEN
  8820. operator := scope.firstOperator;
  8821. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8822. (*
  8823. Printout.Info("not same ",procedureType);
  8824. Printout.Info("with ",operator.type);
  8825. *)
  8826. operator := operator.nextOperator;
  8827. END;
  8828. super := operator;
  8829. ELSE
  8830. super := scope.firstProcedure;
  8831. WHILE (super # NIL) & (super.name # procedure.name) DO
  8832. super := super.nextProcedure;
  8833. END;
  8834. END;
  8835. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8836. RETURN super
  8837. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8838. RETURN super
  8839. ELSE
  8840. RETURN FindSuperProcedure(scope,procedure);
  8841. END;
  8842. END FindSuperProcedure;
  8843. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8844. VAR procedure: SyntaxTree.Procedure;
  8845. BEGIN
  8846. procedure := record.recordScope.constructor;
  8847. IF procedure = NIL THEN
  8848. record := RecordBase(record);
  8849. IF record # NIL THEN
  8850. procedure := GetConstructor(record)
  8851. END;
  8852. END;
  8853. RETURN procedure;
  8854. END GetConstructor;
  8855. (* enter a case into a list of cases in a sorted way and check for collision *)
  8856. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8857. VAR prev,this,new: SyntaxTree.CaseConstant;
  8858. BEGIN
  8859. this := root;
  8860. prev := NIL;
  8861. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8862. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8863. RETURN FALSE
  8864. ELSE
  8865. IF (this # NIL) & (this.min = max+1) THEN
  8866. this.min := min
  8867. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8868. prev.max := min
  8869. ELSE
  8870. NEW(new); new.min := min; new.max := max;
  8871. new.next := this;
  8872. IF prev = NIL THEN
  8873. root := new;
  8874. ELSE
  8875. prev.next := new
  8876. END
  8877. END;
  8878. RETURN TRUE
  8879. END;
  8880. END EnterCase;
  8881. (** generate and return a new checker object, errors are entered into diagnostics **)
  8882. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8883. VAR checker: Checker;
  8884. BEGIN
  8885. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache);
  8886. RETURN checker
  8887. END NewChecker;
  8888. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8889. VAR warnings: Warnings;
  8890. BEGIN
  8891. NEW(warnings, diagnostics); RETURN warnings;
  8892. END NewWarnings;
  8893. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8894. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8895. END IsRangeType;
  8896. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8897. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8898. END IsMathArrayType;
  8899. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8900. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8901. END IsArrayType;
  8902. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8903. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8904. END IsComplexType;
  8905. (** if a type is an array-structured object type *)
  8906. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  8907. VAR recordType: SyntaxTree.RecordType;
  8908. BEGIN
  8909. IF type = NIL THEN
  8910. RETURN FALSE
  8911. ELSE
  8912. type := type.resolved;
  8913. IF type IS SyntaxTree.PointerType THEN
  8914. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8915. IF type IS SyntaxTree.RecordType THEN
  8916. recordType := type(SyntaxTree.RecordType);
  8917. RETURN recordType.isObject & recordType.HasArrayStructure()
  8918. ELSE
  8919. RETURN FALSE
  8920. END
  8921. ELSE
  8922. RETURN FALSE
  8923. END
  8924. END
  8925. END IsArrayStructuredObjectType;
  8926. (** the math array structure of a type
  8927. - for math arrays: the array itself
  8928. - for pointers: the math array structure of the pointer base
  8929. - for array-structured object types: the underlying structure
  8930. - for non-math arrays and all other types: NIL
  8931. **)
  8932. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  8933. VAR
  8934. result: SyntaxTree.MathArrayType;
  8935. BEGIN
  8936. IF type = NIL THEN
  8937. result := NIL
  8938. ELSE
  8939. type := type.resolved;
  8940. IF type IS SyntaxTree.PointerType THEN
  8941. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8942. END;
  8943. IF type IS SyntaxTree.MathArrayType THEN
  8944. result := type(SyntaxTree.MathArrayType)
  8945. ELSIF type IS SyntaxTree.RecordType THEN
  8946. result := type(SyntaxTree.RecordType).arrayStructure
  8947. ELSE
  8948. result := NIL
  8949. END
  8950. END;
  8951. RETURN result
  8952. END MathArrayStructureOfType;
  8953. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  8954. VAR
  8955. result: BOOLEAN;
  8956. rangeExpression: SyntaxTree.RangeExpression;
  8957. BEGIN
  8958. IF x IS SyntaxTree.RangeExpression THEN
  8959. rangeExpression := x(SyntaxTree.RangeExpression);
  8960. result := TRUE;
  8961. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8962. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  8963. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  8964. ELSE
  8965. result := FALSE
  8966. END;
  8967. RETURN result
  8968. END IsStaticRange;
  8969. (** whether a type is a math array of tensor form **)
  8970. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  8971. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8972. END IsTensor;
  8973. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  8974. BEGIN
  8975. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  8976. length := type(SyntaxTree.MathArrayType).staticLength;
  8977. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  8978. RETURN TRUE
  8979. ELSE
  8980. RETURN FALSE
  8981. END;
  8982. END IsStaticMathArray;
  8983. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  8984. BEGIN
  8985. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  8986. END SymbolHasAddress;
  8987. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  8988. BEGIN
  8989. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  8990. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  8991. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  8992. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  8993. END HasAddress;
  8994. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  8995. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  8996. BEGIN
  8997. IF (e IS SyntaxTree.Designator) THEN
  8998. d := e(SyntaxTree.Designator);
  8999. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9000. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9001. e := d.left;
  9002. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9003. END;
  9004. IF d # NIL THEN
  9005. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9006. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9007. END;
  9008. END;
  9009. RETURN FALSE;
  9010. END IsLocalVariable;
  9011. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9012. BEGIN
  9013. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9014. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9015. ELSE
  9016. RETURN TRUE
  9017. END;
  9018. END IsStaticProcedure;
  9019. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9020. CONST OptimizeMethodTable = FALSE;
  9021. BEGIN
  9022. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9023. END InMethodTable;
  9024. END FoxSemanticChecker.