FoxSemanticChecker.Mod 378 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426
  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,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. UndefinedPhase = 0; DeclarationPhase=1; ImplementationPhase=2;
  9. InliningSupport = TRUE;
  10. TYPE
  11. Position=SyntaxTree.Position;
  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
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. cooperative: BOOLEAN;
  66. error-: BOOLEAN;
  67. VerboseErrorMessage: BOOLEAN;
  68. typeFixes, pointerFixes: LateFixList;
  69. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  70. arrayBaseImported: BOOLEAN;
  71. complexNumbersImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. inConversion: LONGINT;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. currentScope-: SyntaxTree.Scope;
  83. currentIsRealtime: BOOLEAN;
  84. currentIsUnreachable: BOOLEAN;
  85. currentIsCellNet: BOOLEAN;
  86. currentIsBodyProcedure: BOOLEAN;
  87. currentIsExclusive: BOOLEAN;
  88. currentResultType: SyntaxTree.Type;
  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,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.cooperative := cooperative;
  99. SELF.system := system;
  100. SELF.symbolFileFormat := symbolFileFormat;
  101. error := FALSE;
  102. NEW(typeFixes);
  103. NEW(pointerFixes);
  104. currentScope := NIL;
  105. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  106. SELF.importCache := importCache;
  107. arrayBaseImported := FALSE;
  108. complexNumbersImported := FALSE;
  109. SELF.VerboseErrorMessage := verboseErrorMessage;
  110. global := NIL;
  111. phase := UndefinedPhase;
  112. currentIsRealtime := FALSE;
  113. currentIsUnreachable := FALSE;
  114. currentIsCellNet := FALSE;
  115. currentIsBodyProcedure := FALSE;
  116. currentIsExclusive := FALSE;
  117. withEntries := NIL;
  118. SELF.cellsAreObjects := system.cellsAreObjects;
  119. COPY(backend, backendName);
  120. inConversion := 0;
  121. END InitChecker;
  122. (*------------ service -------------*)
  123. (** report error **)
  124. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  125. VAR errModule: SyntaxTree.Module;
  126. BEGIN
  127. ASSERT(currentScope # NIL);
  128. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  129. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  130. error := TRUE;
  131. END Error;
  132. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  133. VAR errModule: SyntaxTree.Module;
  134. BEGIN
  135. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  136. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  137. END Warning;
  138. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  139. VAR errorMessage: ARRAY 256 OF CHAR;
  140. BEGIN
  141. Basic.Concat(errorMessage,msg," ", msg2);
  142. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  143. error := TRUE;
  144. END ErrorSS;
  145. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  146. VAR msg, msg2: ARRAY 256 OF CHAR;
  147. BEGIN
  148. COPY(msg1, msg);
  149. Strings.Append(msg, " = ");
  150. Basic.GetString(s, msg2);
  151. Strings.Append(msg, msg2);
  152. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  153. END InfoSS;
  154. (*** symbol lookup ***)
  155. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  156. **)
  157. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  158. VAR
  159. scope,baseScope: SyntaxTree.Scope;
  160. symbol, s: SyntaxTree.Symbol;
  161. ownerRecord,base: SyntaxTree.RecordType;
  162. BEGIN
  163. scope := inScope;
  164. symbol := NIL;
  165. WHILE (scope # NIL) & (symbol = NIL) DO
  166. symbol := scope.FindSymbol(name);
  167. s := NIL;
  168. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  169. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  170. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  171. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. END;
  173. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  174. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  175. ELSE
  176. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  177. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  178. symbol.MarkUsed;
  179. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  180. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  181. base := RecordBase(ownerRecord);
  182. IF (base # NIL) THEN
  183. baseScope := base.recordScope;
  184. symbol := Find(baseScope,name,FALSE);
  185. ELSE
  186. symbol := NIL;
  187. END;
  188. ELSE
  189. symbol := NIL;
  190. END;
  191. END;
  192. END;
  193. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  194. END;
  195. IF (symbol # NIL) THEN
  196. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  197. (*ASSERT(phase = DeclarationPhase);*)
  198. ResolveSymbol(symbol)
  199. END;
  200. symbol.MarkUsed;
  201. END;
  202. RETURN symbol
  203. END Find;
  204. (*------------ types -------------*)
  205. (** find type declaration with name qualifiedIdentifier and return resolved type
  206. - check qualified identifier prefix, set scope to module scope if appropriate
  207. - check suffix in scope
  208. **)
  209. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  210. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  211. BEGIN
  212. result := NIL;
  213. prevScope := currentScope;
  214. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  215. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  216. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  217. IF symbol(SyntaxTree.Import).module = NIL THEN
  218. Error(qualifiedIdentifier.position,"module not loaded");
  219. result := SyntaxTree.invalidType;
  220. symbol := NIL;
  221. ELSE
  222. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  223. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  224. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  225. IF VerboseErrorMessage THEN
  226. Printout.Info("scope", currentScope);
  227. Printout.Info("symbol", symbol);
  228. END;
  229. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  230. END;
  231. END;
  232. ELSE
  233. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  234. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  235. symbol := NIL;
  236. END;
  237. ELSE
  238. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  239. IF symbol = NIL THEN
  240. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  241. IF VerboseErrorMessage THEN
  242. Printout.Info("Qualident",qualifiedIdentifier);
  243. Printout.Info("in scope",currentScope) ;
  244. END;
  245. END;
  246. END;
  247. IF symbol = NIL THEN (* error already handled *)
  248. typeDeclaration := NIL;
  249. result := SyntaxTree.invalidType;
  250. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  251. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSE
  255. currentScope := symbol.scope;
  256. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  257. result := ResolveType(typeDeclaration.declaredType);
  258. symbol.MarkUsed;
  259. ASSERT(result # NIL);
  260. END;
  261. currentScope := prevScope;
  262. RETURN result
  263. END ResolveNamedType;
  264. (**
  265. check enumeration scope: enter symbols and check for duplicate names
  266. **)
  267. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  268. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  269. BEGIN
  270. prevScope := currentScope;
  271. currentScope := x;
  272. e := x.firstConstant;
  273. nextHighest := highest;
  274. WHILE (e # NIL) DO
  275. Register(e,x,FALSE);
  276. IF SymbolNeedsResolution(e) THEN
  277. IF e.value # NIL THEN
  278. value := ConstantExpression(e.value);
  279. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  280. ELSE
  281. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  282. value.SetType(x.ownerEnumeration);
  283. END;
  284. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  285. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  286. IF nextHighest > highest THEN highest := nextHighest END;
  287. END;
  288. e.SetValue(value);
  289. CheckSymbolVisibility(e);
  290. e.SetType(x.ownerEnumeration);
  291. e.SetState(SyntaxTree.Resolved);
  292. END;
  293. e := e.nextConstant;
  294. END;
  295. currentScope := prevScope;
  296. END CheckEnumerationScope;
  297. (**
  298. resolve enumeration type: check enumeration scope
  299. **)
  300. PROCEDURE ResolveEnumerationType(x: SyntaxTree.EnumerationType);
  301. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  302. lowest, highest: Basic.Integer;
  303. BEGIN
  304. IF x.enumerationBase # NIL THEN
  305. position := x.enumerationBase.position;
  306. baseType := ResolveType(x.enumerationBase);
  307. resolved := baseType.resolved;
  308. baseScope := NIL;
  309. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  310. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  311. Error(position, "base type is no enumeration type");
  312. ELSE
  313. enumerationBase := resolved(SyntaxTree.EnumerationType);
  314. lowest := enumerationBase.rangeHighest+1;
  315. END;
  316. x.SetEnumerationBase(baseType);
  317. ELSE lowest := 0;
  318. END;
  319. highest := lowest-1;
  320. CheckEnumerationScope(x.enumerationScope, highest);
  321. x.SetRange(lowest, highest);
  322. x.SetState(SyntaxTree.Resolved);
  323. END ResolveEnumerationType;
  324. (**
  325. resolve qualified type
  326. - find and resolve named type and set resolved type
  327. **)
  328. PROCEDURE ResolveQualifiedType(x: SyntaxTree.QualifiedType);
  329. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  330. BEGIN
  331. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  332. x.SetResolved(type.resolved);
  333. x.SetState(SyntaxTree.Resolved);
  334. x.SetTypeDeclaration (typeDeclaration);
  335. (*
  336. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  337. x.SetResolved(SyntaxTree.invalidType);
  338. END;
  339. *)
  340. END ResolveQualifiedType;
  341. (**
  342. resolve array type
  343. - check base type
  344. - array of math array forbidden
  345. - static array of open array forbidden
  346. **)
  347. PROCEDURE ResolveArrayType(x: SyntaxTree.ArrayType);
  348. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  349. BEGIN
  350. x.SetArrayBase(ResolveType(x.arrayBase));
  351. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  352. arrayBase := x.arrayBase.resolved;
  353. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  354. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  355. pointerType.SetPointerBase(arrayBase);
  356. pointerType.SetHidden(TRUE);
  357. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  358. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  359. ELSE
  360. x.SetArrayBase(pointerType);
  361. END;
  362. END;
  363. IF x.length # NIL THEN
  364. variableAccessed := FALSE;
  365. e := ResolveExpression(x.length);
  366. IF (e.resolved = NIL) THEN
  367. IF variableAccessed THEN
  368. Error(e.position, "forbidden variable access");
  369. END;
  370. IF CheckSizeType(e) THEN
  371. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  372. END;
  373. ELSE
  374. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  375. END;
  376. END;
  377. IF arrayBase IS SyntaxTree.ArrayType THEN
  378. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  379. Error(x.position,"forbidden static array of dynamic array");
  380. END;
  381. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  382. Error(x.position,"forbidden array mixed form");
  383. END;
  384. x.SetHasPointers(arrayBase.hasPointers);
  385. x.SetState(SyntaxTree.Resolved);
  386. END ResolveArrayType;
  387. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  388. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  389. BEGIN
  390. module := currentScope.ownerModule;
  391. IF module.name=name THEN
  392. (* do nothing *)
  393. ELSE
  394. moduleScope := module.moduleScope;
  395. import := moduleScope.FindImport(name);
  396. IF import = NIL THEN
  397. import := SyntaxTree.NewImport(position,name,name,TRUE);
  398. moduleScope.AddImport(import);
  399. Register(import,moduleScope,FALSE);
  400. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  401. ResolveImport(import);
  402. ELSIF import.direct=FALSE THEN
  403. import.SetScope(module.moduleScope);
  404. import.SetDirect(TRUE);
  405. IF moduleScope.FindSymbol(import.name) = NIL THEN
  406. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  407. duplicate.SetContext(import.context);
  408. duplicate.SetModule(import.module);
  409. Register(duplicate,moduleScope,TRUE);
  410. ResolveImport(duplicate);
  411. END;
  412. END;
  413. import.MarkUsed
  414. END;
  415. END ImportModule;
  416. (**
  417. resolve math array type
  418. - check base type
  419. - open math array of array forbidden
  420. - math array of tensor forbidden
  421. - static array of open array forbidden
  422. **)
  423. PROCEDURE ResolveMathArrayType(x: SyntaxTree.MathArrayType);
  424. VAR arrayBase: SyntaxTree.Type; modifiers: SyntaxTree.Modifier; position: SyntaxTree.Position;
  425. BEGIN
  426. x.SetArrayBase(ResolveType(x.arrayBase));
  427. IF x.length # NIL THEN
  428. x.SetLength(ConstantIntegerGeq0(x.length));
  429. END;
  430. arrayBase := x.arrayBase;
  431. IF arrayBase # NIL THEN
  432. arrayBase := arrayBase.resolved;
  433. IF arrayBase = SyntaxTree.invalidType THEN
  434. (* error already handled *)
  435. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  436. Error(x.position,"forbidden array mixed form");
  437. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  438. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  439. Error(x.position,"forbidden Tensor Array mix")
  440. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  441. Error(x.position,"forbidden static array of dynamic array")
  442. END;
  443. END;
  444. IF x.form = SyntaxTree.Static THEN
  445. x.SetIncrement(system.SizeOf(arrayBase));
  446. END;
  447. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  448. END;
  449. modifiers := x.modifiers;
  450. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  451. CheckModifiers(modifiers, TRUE);
  452. x.SetState(SyntaxTree.Resolved);
  453. END ResolveMathArrayType;
  454. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  455. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  456. (1) Rec = RECORD ... END; Ptr <---> Rec
  457. Ptr = POINTER TO Rec; ^ |
  458. | |
  459. TypeDesc TypeDesc
  460. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  461. ^ /
  462. | /
  463. TypeDesc <-- /
  464. *)
  465. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  466. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  467. BEGIN
  468. Strings.IntToStr(x.position.start,number);
  469. COPY(prefix,name);
  470. Strings.Append(name,"@");
  471. Strings.Append(name,number);
  472. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  473. typeDeclaration.SetDeclaredType(x);
  474. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  475. x.SetTypeDeclaration(typeDeclaration);
  476. currentScope.AddTypeDeclaration(typeDeclaration);
  477. typeDeclaration.SetScope(currentScope);
  478. END AnonymousTypeDeclaration;
  479. (**
  480. deferred pointer type resolving
  481. - resolve base type
  482. - check that base type is a record or array type
  483. - if error then set base type to invalid type
  484. **)
  485. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  486. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  487. BEGIN
  488. ASSERT(type.pointerBase # NIL);
  489. position := type.pointerBase.position;
  490. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  491. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  492. (* not for pointers, a type is needed for the records only
  493. IF type.typeDeclaration = NIL THEN
  494. AnonymousTypeDeclaration(type);
  495. END;
  496. *)
  497. END;
  498. resolved := ResolveType(type.pointerBase);
  499. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  500. type.SetPointerBase(resolved);
  501. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  502. recordType := resolved.resolved(SyntaxTree.RecordType);
  503. IF recordType.isObject & (recordType.baseType # NIL) THEN
  504. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  505. Error(position, "base type of object must be a realtime object");
  506. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  507. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  508. END;
  509. END;
  510. END;
  511. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  512. Error(position, "realtime object contains references to non-realtime objects");
  513. END;
  514. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  515. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  516. Error(position, "forbidden unsafe at static array");
  517. ELS
  518. *)
  519. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  520. Error(position, "forbidden unsafe at multidimensional array");
  521. END;
  522. END;
  523. ELSE
  524. Error(position, "forbidden pointer base type");
  525. type.SetPointerBase(SyntaxTree.invalidType)
  526. END
  527. END FixPointerType;
  528. (**
  529. resolve pointer type
  530. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  531. **)
  532. PROCEDURE ResolvePointerType(x: SyntaxTree.PointerType);
  533. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  534. modifiers: SyntaxTree.Modifier; position: Position;
  535. BEGIN
  536. modifiers := x.modifiers;
  537. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  538. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  539. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  540. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  541. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  542. (* inheritance cycle check
  543. example:
  544. A=POINTER TO RECORD(B) END;
  545. B=POINTER TO RECORD(A) END;
  546. *)
  547. IF x.pointerBase IS SyntaxTree.RecordType THEN
  548. recordType := x.pointerBase(SyntaxTree.RecordType);
  549. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  550. recordBaseType := ResolveType(recordType.baseType);
  551. recordType.SetBaseType(recordBaseType);
  552. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  553. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  554. END;
  555. CheckModifiers(modifiers, TRUE);
  556. typeFixes.Add(x,currentScope);
  557. x.SetState(SyntaxTree.Resolved);
  558. END ResolvePointerType;
  559. (**
  560. resolve port type
  561. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  562. **)
  563. PROCEDURE ResolvePortType(x: SyntaxTree.PortType);
  564. VAR value: Basic.Integer;
  565. BEGIN
  566. x.SetCellsAreObjects(cellsAreObjects);
  567. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  568. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  569. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  570. ELSE
  571. x.SetSize(system.SizeOf(system.longintType));
  572. END;
  573. x.SetState(SyntaxTree.Resolved);
  574. END ResolvePortType;
  575. (**
  576. deferred procedure type resolving
  577. - resolve return type
  578. - traverse and resolve parameters
  579. **)
  580. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  581. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  582. BEGIN
  583. resolved := ResolveType(procedureType.returnType);
  584. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  585. Error(procedureType.position,"forbidden open array return type");
  586. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  587. Error(procedureType.position,"procedure with return type does not return");
  588. END;
  589. procedureType.SetReturnType(resolved);
  590. IF (resolved # NIL) & StructuredReturnType (system,procedureType) THEN
  591. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  592. parameter.SetType(procedureType.returnType);
  593. parameter.SetAccess(SyntaxTree.Hidden);
  594. parameter.SetUntraced(procedureType.hasUntracedReturn);
  595. ResolveParameter(parameter);
  596. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  597. END;
  598. (* process parameters *)
  599. parameter :=procedureType.firstParameter;
  600. WHILE (parameter # NIL) DO
  601. ResolveParameter(parameter);
  602. parameter := parameter.nextParameter;
  603. END;
  604. parameter := procedureType.selfParameter;
  605. IF parameter # NIL THEN
  606. ResolveParameter(parameter)
  607. END;
  608. END FixProcedureType;
  609. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  610. VAR prev,this: SyntaxTree.Modifier;
  611. BEGIN
  612. this := modifiers;prev := NIL;
  613. WHILE (this # NIL) & (this.identifier # name) DO
  614. prev := this; this := this.nextModifier;
  615. END;
  616. IF this # NIL THEN
  617. IF this.expression # NIL THEN
  618. Error(this.position,"unexpected expression");
  619. END;
  620. this.Resolved;
  621. position := this.position;
  622. RETURN TRUE
  623. ELSE
  624. RETURN FALSE
  625. END;
  626. END HasFlag;
  627. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  628. VAR prev,this: SyntaxTree.Modifier;
  629. BEGIN
  630. this := modifiers;prev := NIL;
  631. WHILE (this # NIL) & (this.identifier # name) DO
  632. prev := this; this := this.nextModifier;
  633. END;
  634. IF this # NIL THEN
  635. IF this.expression = NIL THEN
  636. Error(this.position,"expected expression value");
  637. ELSE
  638. this.SetExpression(ConstantExpression(this.expression));
  639. IF CheckIntegerValue(this.expression,value) THEN END;
  640. END;
  641. this.Resolved;
  642. position := this.position;
  643. RETURN TRUE
  644. ELSE RETURN FALSE
  645. END;
  646. END HasValue;
  647. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  648. VAR prev,this: SyntaxTree.Modifier;
  649. BEGIN
  650. this := modifiers;prev := NIL;
  651. WHILE (this # NIL) & (this.identifier # name) DO
  652. prev := this; this := this.nextModifier;
  653. END;
  654. IF this # NIL THEN
  655. IF this.expression = NIL THEN
  656. Error(this.position,"expected expression value");
  657. ELSE
  658. this.SetExpression(ConstantExpression(this.expression));
  659. IF CheckStringValue(this.expression,value) THEN END;
  660. END;
  661. this.Resolved;
  662. position := this.position;
  663. RETURN TRUE
  664. ELSE RETURN FALSE
  665. END;
  666. END HasStringValue;
  667. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  668. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  669. BEGIN
  670. IF cellsAreObjects THEN RETURN FALSE END;
  671. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  672. IF svalue = "A2" THEN
  673. RETURN TRUE
  674. END;
  675. END;
  676. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  677. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  678. END;
  679. RETURN FALSE;
  680. (*
  681. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  682. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  683. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  684. IF svalue[0] = "~" THEN
  685. Strings.TrimLeft(svalue, "~");
  686. IF svalue = backendName THEN
  687. RETURN TRUE;
  688. END;
  689. ELSIF svalue # backendName THEN
  690. RETURN TRUE;
  691. END;
  692. END;
  693. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  694. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  695. END;
  696. RETURN FALSE;
  697. *)
  698. END SkipImplementation;
  699. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  700. VAR this: SyntaxTree.Modifier;
  701. BEGIN
  702. this := modifiers;
  703. WHILE this # NIL DO
  704. IF ~this.resolved THEN
  705. IF checkUse THEN
  706. Error(this.position,"unexpected modifier");
  707. ELSE
  708. this.SetExpression(ResolveExpression(this.expression));
  709. this.Resolved;
  710. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  711. END;
  712. END;
  713. this := this.nextModifier
  714. END;
  715. END CheckModifiers;
  716. (**
  717. resolve procedure type
  718. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  719. **)
  720. PROCEDURE ResolveProcedureType(procedureType: SyntaxTree.ProcedureType);
  721. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  722. BEGIN
  723. modifiers := procedureType.modifiers;
  724. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  725. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  726. procedureType.SetInterrupt(TRUE);
  727. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  728. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  729. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  730. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  731. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  732. Error(position, "undefined platform calling convention");
  733. ELSE
  734. procedureType.SetCallingConvention(system.platformCallingConvention);
  735. END;
  736. END;
  737. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  738. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  739. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  740. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  741. CheckModifiers(modifiers, TRUE);
  742. modifiers := procedureType.returnTypeModifiers;
  743. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  744. CheckModifiers(modifiers, TRUE);
  745. typeFixes.Add(procedureType,currentScope);
  746. procedureType.SetHasPointers(procedureType.isDelegate);
  747. procedureType.SetState(SyntaxTree.Resolved);
  748. END ResolveProcedureType;
  749. (** check and resolve record type
  750. - check base type: must be record, math array or array-structured object type
  751. - check declarations
  752. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  753. **)
  754. PROCEDURE ResolveRecordType(x: SyntaxTree.RecordType);
  755. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  756. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  757. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  758. hasPointers: BOOLEAN;
  759. modifiers: SyntaxTree.Modifier;
  760. value: Basic.Integer;
  761. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  762. BEGIN
  763. type := type.resolved;
  764. IF (type IS SyntaxTree.PointerType) &
  765. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  766. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  767. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  768. RETURN TRUE
  769. ELSE
  770. RETURN FALSE
  771. END;
  772. END IsPointerToRecord;
  773. BEGIN
  774. hasPointers := FALSE;
  775. modifiers := x.modifiers;
  776. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  777. END;
  778. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  779. CheckModifiers(modifiers, TRUE);
  780. IF x.baseType # NIL THEN
  781. position := x.baseType.position;
  782. baseType := ResolveType(x.baseType);
  783. resolved := baseType.resolved;
  784. hasPointers := hasPointers OR resolved.hasPointers;
  785. IF x.isObject THEN (* object *)
  786. ASSERT(x.pointerType # NIL);
  787. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  788. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  789. baseType := NIL
  790. ELSIF IsPointerToRecord(resolved,recordType) THEN
  791. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  792. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  793. ELSE
  794. Error(position,"object does not extend pointer to record, object or math array ")
  795. END;
  796. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  797. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  798. ELSIF IsPointerToRecord(resolved,recordType) THEN
  799. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  800. ELSIF resolved IS SyntaxTree.RecordType THEN
  801. ELSE
  802. Error(position,"pointer to record does not extend pointer to record or record")
  803. END;
  804. ELSE
  805. IF resolved IS SyntaxTree.RecordType THEN
  806. ELSE
  807. Error(position,"record does not extend record")
  808. END;
  809. END;
  810. x.SetBaseType(baseType);
  811. IF x.Level() > 15 THEN
  812. Error(position, "record/object inheritance level too high");
  813. (* note:
  814. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  815. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  816. inheritance history of a type.
  817. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  818. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  819. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  820. *)
  821. END;
  822. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  823. Error(position, "invalid inheritance of disposable types");
  824. END;
  825. END;
  826. Declarations(x.recordScope, FALSE, {0});
  827. x.SetState(SyntaxTree.Resolved);
  828. Declarations(x.recordScope, FALSE, {1});
  829. (* computation of sizes and offsets skipped -> done in backend / system *)
  830. recordBase := x.GetBaseRecord();
  831. IF recordBase = NIL THEN numberMethods := 0
  832. ELSE numberMethods := recordBase.recordScope.numberMethods
  833. END;
  834. isRealtime := TRUE;
  835. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  836. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  837. END;
  838. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  839. WHILE symbol # NIL DO
  840. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  841. IF symbol IS SyntaxTree.Variable THEN
  842. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  843. END;
  844. IF symbol IS SyntaxTree.Procedure THEN
  845. procedure := symbol(SyntaxTree.Procedure);
  846. IF procedure.super # NIL THEN
  847. procedure.SetMethodNumber(procedure.super.methodNumber);
  848. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  849. procedure.SetMethodNumber(numberMethods);
  850. INC(numberMethods);
  851. END;
  852. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  853. Error(procedure.position,"realtime procedure in non-realtime object")
  854. END;
  855. END;
  856. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  857. Error(symbol.position,"non-realtime symbol in realtime object")
  858. END;
  859. symbol := symbol.nextSymbol;
  860. END;
  861. IF isRealtime THEN x.SetRealtime(TRUE) END;
  862. x.recordScope.SetNumberMethods(numberMethods);
  863. (* TODO: is this needed anymore? *)
  864. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  865. Error(x.position,"object extends a record")
  866. END;
  867. IF ~x.isAbstract THEN
  868. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  869. Error(x.position, "non-abstract object contains abstract procedure");
  870. END;
  871. ELSE
  872. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  873. Error(x.position, "abstract object does not contain an abstract method");
  874. END;
  875. END;
  876. IF (x.typeDeclaration = NIL) THEN
  877. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  878. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  879. (*
  880. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  881. AnonymousTypeDeclaration(x,name);
  882. *)
  883. ELSE
  884. AnonymousTypeDeclaration(x,"Anonymous");
  885. END;
  886. END;
  887. x.SetHasPointers(hasPointers);
  888. x.SetState(SyntaxTree.Resolved);
  889. END ResolveRecordType;
  890. (** check and resolve cell type
  891. - check base type: must be cell
  892. - check declarations
  893. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  894. **)
  895. PROCEDURE ResolveCellType(x: SyntaxTree.CellType);
  896. VAR
  897. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  898. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  899. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  900. recordBase: SyntaxTree.RecordType;
  901. numberMethods: LONGINT;
  902. int: Basic.Integer;
  903. real: LONGREAL;
  904. bool: BOOLEAN;
  905. set: Basic.Set;
  906. v: SyntaxTree.Expression;
  907. str: Scanner.StringType;
  908. atype: SyntaxTree.ArrayType;
  909. prev: SyntaxTree.Scope;
  910. BEGIN
  911. recordBase := NIL;
  912. IF cellsAreObjects THEN
  913. IF x.baseType = NIL THEN
  914. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  915. ImportModule(qualifiedIdentifier.prefix, x.position);
  916. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  917. x.SetBaseType(ResolveType(x.baseType));
  918. recordBase := x.GetBaseRecord();
  919. IF recordBase = NIL THEN
  920. Error(x.position,"ActiveCellsRuntime.Cell not present");
  921. END;
  922. ELSE
  923. x.SetBaseType(ResolveType(x.baseType));
  924. END;
  925. ELSE
  926. x.SetBaseType(ResolveType(x.baseType));
  927. END;
  928. IF recordBase = NIL THEN numberMethods := 0
  929. ELSE numberMethods := recordBase.recordScope.numberMethods
  930. END;
  931. modifier := x.modifiers;
  932. (*IF ~x.isCellNet THEN*)
  933. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  934. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  935. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  936. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  937. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  938. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  939. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  940. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  941. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  942. symbol := system.activeCellsCapabilities;
  943. WHILE symbol # NIL DO
  944. IF HasFlag(modifier, symbol.name, position) THEN END;
  945. symbol := symbol.nextSymbol;
  946. END;
  947. modifier := x.modifiers;
  948. WHILE (modifier # NIL) DO
  949. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  950. IF modifier.expression # NIL THEN
  951. v := ConstantExpression(modifier.expression);
  952. property.SetValue(v);
  953. IF IsIntegerValue(modifier.expression, int) THEN
  954. (*property.SetValue(modifier.expression);*)
  955. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  956. property.SetType(system.longintType);
  957. ELSIF IsRealValue(modifier.expression, real) THEN
  958. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  959. property.SetType(system.longrealType);
  960. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  961. property.SetType(system.booleanType);
  962. ELSIF IsSetValue(modifier.expression, set) THEN
  963. property.SetType(system.setType);
  964. ELSIF IsStringValue(modifier.expression, str) THEN
  965. (*property.SetValue(modifier.expression);*)
  966. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  967. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  968. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  969. property.SetType(atype);
  970. ELSE
  971. Error(modifier.position, "unsupported property type");
  972. END;
  973. ELSE (* flag property *)
  974. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  975. property.SetType(system.booleanType);
  976. END;
  977. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  978. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  979. x.AddProperty(property);
  980. modifier := modifier.nextModifier;
  981. END;
  982. CheckModifiers(modifier, FALSE);
  983. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  984. (* process parameters *)
  985. prev := currentScope;
  986. currentScope := x.cellScope;
  987. parameter :=x.firstParameter;
  988. WHILE (parameter # NIL) DO
  989. ResolveParameter(parameter);
  990. type := parameter.type.resolved;
  991. IF ~(type IS SyntaxTree.PortType) THEN
  992. WHILE IsStaticArray(type, type, len) DO
  993. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  994. END;
  995. WHILE IsDynamicArray(type, type) DO
  996. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  997. END;
  998. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  999. Error(parameter.position, "invalid type, must be port or static array of port ");
  1000. END;
  1001. END;
  1002. parameter := parameter.nextParameter;
  1003. END;
  1004. currentScope := prev;
  1005. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1006. WHILE symbol # NIL DO
  1007. IF symbol IS SyntaxTree.Variable THEN
  1008. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1009. END;
  1010. symbol := symbol.nextSymbol;
  1011. END;
  1012. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1013. IF (x.typeDeclaration = NIL) THEN
  1014. AnonymousTypeDeclaration(x,"Anonymous");
  1015. END;
  1016. x.SetState(SyntaxTree.Resolved);
  1017. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1018. Warning(x.position, "Forbidden empty Body.");
  1019. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1020. Warning(x.position, "Non-empty body for an engine?");
  1021. END;
  1022. END ResolveCellType;
  1023. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  1024. If node is currently being resolved then emit a cyclic definition error.
  1025. Return TRUE only if node is fully resolved.
  1026. **)
  1027. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  1028. VAR result: BOOLEAN;
  1029. BEGIN
  1030. IF SyntaxTree.Resolved IN x.state THEN
  1031. result := FALSE
  1032. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  1033. Error(x.position,"cyclic definition");
  1034. result := FALSE;
  1035. ELSE
  1036. result := TRUE;
  1037. x.SetState(SyntaxTree.BeingResolved)
  1038. END;
  1039. RETURN result
  1040. END TypeNeedsResolution;
  1041. PROCEDURE ResolveType*(x: SyntaxTree.Type): SyntaxTree.Type;
  1042. BEGIN
  1043. IF (x = NIL) OR (x = SyntaxTree.invalidType) THEN RETURN x
  1044. ELSIF TypeNeedsResolution(x) THEN
  1045. WITH x: SyntaxTree.ProcedureType DO ResolveProcedureType(x)
  1046. |SyntaxTree.CellType DO ResolveCellType(x)
  1047. |SyntaxTree.RecordType DO ResolveRecordType(x)
  1048. |SyntaxTree.PortType DO ResolvePortType(x)
  1049. |SyntaxTree.PointerType DO ResolvePointerType(x)
  1050. |SyntaxTree.MathArrayType DO ResolveMathArrayType(x)
  1051. |SyntaxTree.ArrayType DO ResolveArrayType(x)
  1052. |SyntaxTree.RangeType DO x.SetState(SyntaxTree.Resolved)
  1053. |SyntaxTree.EnumerationType DO ResolveEnumerationType(x)
  1054. |SyntaxTree.StringType DO x.SetState(SyntaxTree.Resolved)
  1055. |SyntaxTree.QualifiedType DO ResolveQualifiedType(x)
  1056. |SyntaxTree.BasicType DO x.SetState(SyntaxTree.Resolved)
  1057. END; (* no other case may remain *)
  1058. END;
  1059. IF SyntaxTree.Resolved IN x.state THEN
  1060. RETURN x
  1061. ELSE
  1062. RETURN SyntaxTree.invalidType
  1063. END;
  1064. END ResolveType;
  1065. (** resolve all pending types (late resolving).
  1066. - type fixes are resolved at the end of the declaration phase
  1067. - type fixes may imply new type fixes that are also entered at the end of the list
  1068. **)
  1069. PROCEDURE FixTypes;
  1070. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1071. BEGIN
  1072. prevScope := currentScope;
  1073. p := typeFixes.Get(currentScope);
  1074. WHILE p # NIL DO
  1075. ASSERT(currentScope # NIL);
  1076. ASSERT(p IS SyntaxTree.Type);
  1077. IF p IS SyntaxTree.PointerType THEN
  1078. FixPointerType(p(SyntaxTree.PointerType))
  1079. ELSIF p IS SyntaxTree.ProcedureType THEN
  1080. FixProcedureType(p(SyntaxTree.ProcedureType))
  1081. ELSE
  1082. HALT(100);
  1083. END;
  1084. p := typeFixes.Get(currentScope);
  1085. END;
  1086. currentScope :=prevScope;
  1087. END FixTypes;
  1088. (*** compatibility rules ***)
  1089. (**
  1090. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1091. **)
  1092. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1093. VAR result: SyntaxTree.Type;
  1094. BEGIN
  1095. result := SyntaxTree.invalidType;
  1096. IF type = NIL THEN Error(position, "expression of type NIL");
  1097. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1098. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1099. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1100. ELSE result := type.resolved
  1101. END;
  1102. RETURN result
  1103. END RegularType;
  1104. (** returns signature compatibility of procedure types this and to
  1105. - if not compatible then error is reported
  1106. - compatibility means type equality
  1107. **)
  1108. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1109. VAR result: BOOLEAN;
  1110. BEGIN
  1111. result := SameType(to,this);
  1112. IF ~result THEN
  1113. Error(position, "signature incompatible");
  1114. IF VerboseErrorMessage THEN
  1115. Printout.Info("this",this);
  1116. Printout.Info("to",to);
  1117. END;
  1118. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1119. Error(position, "signature incompatible: realtime flag must be inherited");
  1120. END;
  1121. RETURN result
  1122. END SignatureCompatible;
  1123. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1124. - for var parameters compatibility means same type except for
  1125. - formal is of open array of system byte
  1126. - formal is of record type
  1127. - formal is of open array type
  1128. - formal is of open math array type
  1129. - for value parameters compatibllity means assignment compatibility except for
  1130. - formal is of open array type
  1131. if compatible the return true else report error and return false
  1132. **)
  1133. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1134. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1135. BEGIN
  1136. formalType := RegularType(formal.position,formal.type);
  1137. actualType := RegularType(actual.position,actual.type);
  1138. error := FALSE;
  1139. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1140. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1141. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1142. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1143. END;
  1144. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1145. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1146. ELSIF ~IsVariable(actual) THEN
  1147. result := FALSE; error := TRUE;
  1148. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1149. Error(actual.position,"not a variable: no operator for writing");
  1150. ELSE
  1151. Error(actual.position,"is not a variable");
  1152. END;
  1153. IF VerboseErrorMessage THEN
  1154. Printout.Info("actual",actual);
  1155. Printout.Info("formal",formal);
  1156. END;
  1157. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1158. result := CompatibleTo(system,actualType,formalType);
  1159. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1160. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1161. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1162. result := TRUE;
  1163. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1164. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1165. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1166. Error(actual.position,"incompatible non-static actual type");
  1167. END;
  1168. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1169. Error(actual.position,"incompatible tensor (use a range expression)");
  1170. END;
  1171. ELSE
  1172. result := SameType(actualType,formalType)
  1173. END
  1174. ELSE
  1175. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1176. actualType := system.characterType;
  1177. END;
  1178. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1179. result := TRUE; (* special rule for WINAPI parameters *)
  1180. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1181. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1182. ELSE
  1183. result := CompatibleTo(system,actualType,formalType);
  1184. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1185. Error(actual.position,"incompatible non-static actual type");
  1186. END;
  1187. END;
  1188. END;
  1189. IF ~result & ~error THEN
  1190. Error(actual.position,"incompatible parameter");
  1191. IF VerboseErrorMessage THEN
  1192. Printout.Info("actual",actual);
  1193. Printout.Info("formal",formal);
  1194. END;
  1195. END;
  1196. RETURN result
  1197. END ParameterCompatible;
  1198. (** check compatibility for expressions of the form left := right
  1199. - if compatible then return true else error report and return false
  1200. - check if left is variable
  1201. - check compatiblity
  1202. **)
  1203. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1204. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1205. BEGIN
  1206. result := FALSE;
  1207. leftType := RegularType(left.position,left.type);
  1208. rightType := RegularType(right.position,right.type);
  1209. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1210. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1211. END;
  1212. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1213. (* error already handled *)
  1214. result := TRUE;
  1215. ELSIF ~IsVariable(left) THEN
  1216. Error(left.position,"is not a variable");
  1217. IF VerboseErrorMessage THEN
  1218. Printout.Info("left",left);
  1219. Printout.Info("right",right);
  1220. END;
  1221. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1222. result := TRUE;
  1223. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1224. result := TRUE
  1225. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1226. Error(left.position,"incompatible assignment");
  1227. IF VerboseErrorMessage THEN
  1228. Printout.Info("left",left);
  1229. Printout.Info("right",right);
  1230. END;
  1231. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1232. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1233. Error(right.position,"forbidden assignment of a nested procedure");
  1234. ELSE
  1235. result := TRUE
  1236. END;
  1237. RETURN result
  1238. END AssignmentCompatible;
  1239. (*** expressions ***)
  1240. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1241. - check all elements on integer type
  1242. - if element range is constant, then check lower and upper bound
  1243. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1244. if an error occurs then report error and return invalidExpression
  1245. **)
  1246. PROCEDURE ResolveSet(set: SyntaxTree.Set): SyntaxTree.Expression;
  1247. VAR
  1248. i: LONGINT;
  1249. element: SyntaxTree.Expression;
  1250. constant: BOOLEAN;
  1251. elements: SyntaxTree.ExpressionList;
  1252. s: Basic.Set;
  1253. result: SyntaxTree.Expression;
  1254. value: SyntaxTree.Value;
  1255. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1256. VAR
  1257. left, right: SyntaxTree.Expression;
  1258. elementResult: SyntaxTree.Expression;
  1259. leftInteger, rightInteger: Basic.Integer;
  1260. BEGIN
  1261. (* set context of range *)
  1262. IF element IS SyntaxTree.RangeExpression THEN
  1263. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1264. END;
  1265. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1266. IF elementResult = SyntaxTree.invalidExpression THEN
  1267. (* error already reported *)
  1268. constant := FALSE
  1269. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1270. (* the element is a range expression *)
  1271. (* extract left and right hand side of range *)
  1272. left := elementResult(SyntaxTree.RangeExpression).first;
  1273. right := elementResult(SyntaxTree.RangeExpression).last;
  1274. (* guaranteed by VisitRangeExpression: *)
  1275. ASSERT((left # NIL) & (right # NIL));
  1276. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1277. ELSE
  1278. (* the element is not a range expression *)
  1279. (* check type and add conversion if needed *)
  1280. IF IsIntegerType(elementResult.type.resolved) THEN
  1281. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1282. ELSE
  1283. Error(elementResult.position, "non integer element in set");
  1284. elementResult := SyntaxTree.invalidExpression;
  1285. constant := FALSE
  1286. END;
  1287. left := elementResult;
  1288. right := elementResult
  1289. END;
  1290. IF elementResult # SyntaxTree.invalidExpression THEN
  1291. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1292. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1293. Error(left.position,"not allowed set integer value");
  1294. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1295. Error(right.position,"not allowed set integer value");
  1296. END
  1297. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1298. Error(right.position,"not allowed set integer value");
  1299. ELSE
  1300. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1301. s := {};
  1302. ELSE
  1303. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1304. IF leftInteger < 0 THEN leftInteger := 0 END;
  1305. (*!!!!!!!!! this is a hack !!!!!!! *)
  1306. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1307. extends the range x..31 to x..63 !!!!!! *)
  1308. s := s + {leftInteger..rightInteger};
  1309. END;
  1310. END;
  1311. ELSE
  1312. constant := FALSE;
  1313. END
  1314. END;
  1315. RETURN elementResult
  1316. END CheckElement;
  1317. BEGIN
  1318. result := set; constant := TRUE; s := {}; elements := set.elements;
  1319. IF elements # NIL THEN
  1320. FOR i := 0 TO elements.Length()-1 DO
  1321. element := elements.GetExpression(i);
  1322. element := CheckElement(element);
  1323. IF element = SyntaxTree.invalidExpression THEN
  1324. result := SyntaxTree.invalidExpression
  1325. END;
  1326. elements.SetExpression(i,element);
  1327. END;
  1328. END;
  1329. IF constant THEN
  1330. value := Global.NewSetValue(system,set.position,s);
  1331. result.SetResolved(value);
  1332. result.SetType(value.type);
  1333. ELSE
  1334. result.SetType(system.setType);
  1335. END;
  1336. (* optimization possible
  1337. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1338. left this to the programmer...
  1339. *)
  1340. RETURN result;
  1341. END ResolveSet;
  1342. (*
  1343. old variant: quite generic but needs better conversion handling, do this?
  1344. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1345. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1346. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1347. BEGIN
  1348. type := NIL;
  1349. numberElements := x.elements.Length();
  1350. FOR i := 0 TO numberElements-1 DO
  1351. expression := x.elements.GetExpression(i);
  1352. position := expression.position;
  1353. expression := ResolveExpression(x.elements.GetExpression(i));
  1354. x.elements.SetExpression(i,de);
  1355. IF type = NIL THEN
  1356. type := expression.type;
  1357. ELSIF CompatibleTo(system,expression.type,type) THEN
  1358. (* ok *)
  1359. ELSIF CompatibleTo(system,type,expression.type) THEN
  1360. type := expression.type
  1361. ELSE
  1362. Error(expression.position, "incompatible element types");
  1363. type := SyntaxTree.invalidType;
  1364. END;
  1365. END;
  1366. isValue := TRUE;
  1367. FOR i := 0 TO numberElements-1 DO
  1368. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1369. x.elements.SetExpression(i,expression);
  1370. isValue := isValue & (expression.resolved # NIL);
  1371. END;
  1372. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1373. arrayType.SetArrayBase(type);
  1374. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1375. IF isValue THEN
  1376. value := SyntaxTree.NewMathArrayValue(position);
  1377. value.SetElements(x.elements);
  1378. x.SetResolved(value);
  1379. END;
  1380. x.SetType(arrayType);
  1381. resolvedExpression := x;
  1382. END VisitMathArrayExpression;
  1383. *)
  1384. PROCEDURE ResolveMathArrayExpression(x: SyntaxTree.MathArrayExpression): SyntaxTree.Expression;
  1385. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1386. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1387. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1388. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1389. BEGIN
  1390. numberElements := x.elements.Length();
  1391. FOR i := 0 TO numberElements-1 DO
  1392. expression := x.elements.GetExpression(i);
  1393. IF expression IS SyntaxTree.MathArrayExpression THEN
  1394. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1395. ELSE
  1396. position := expression.position;
  1397. expression := ResolveExpression(x.elements.GetExpression(i));
  1398. x.elements.SetExpression(i,expression);
  1399. IF type = NIL THEN
  1400. type := expression.type;
  1401. ELSIF CompatibleTo(system,expression.type,type) THEN
  1402. (* ok *)
  1403. ELSIF CompatibleTo(system,type,expression.type) THEN
  1404. type := expression.type
  1405. ELSE
  1406. Error(expression.position, "incompatible element types");
  1407. type := SyntaxTree.invalidType;
  1408. END;
  1409. END;
  1410. END;
  1411. END RecursivelyFindType;
  1412. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1413. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1414. BEGIN
  1415. numberElements := x.elements.Length();
  1416. FOR i := 0 TO numberElements-1 DO
  1417. expression := x.elements.GetExpression(i);
  1418. IF expression IS SyntaxTree.MathArrayExpression THEN
  1419. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1420. ELSE
  1421. position := expression.position;
  1422. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1423. x.elements.SetExpression(i,expression);
  1424. isValue := isValue & (expression.resolved # NIL);
  1425. END;
  1426. END;
  1427. END RecursivelySetExpression;
  1428. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1429. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1430. arrayType: SyntaxTree.MathArrayType;
  1431. BEGIN
  1432. numberElements := x.elements.Length();
  1433. baseType := NIL;
  1434. gsize := 0;
  1435. FOR i := 0 TO numberElements-1 DO
  1436. expression := x.elements.GetExpression(i);
  1437. IF expression IS SyntaxTree.MathArrayExpression THEN
  1438. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1439. IF i=0 THEN
  1440. gsize := size;
  1441. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1442. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1443. ELSE expression.SetType(baseType)
  1444. END;
  1445. ELSIF baseType = NIL THEN baseType := type;
  1446. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1447. END;
  1448. END;
  1449. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1450. arrayType.SetArrayBase(baseType);
  1451. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1452. RETURN ResolveType(arrayType);
  1453. END RecursivelySetType;
  1454. BEGIN
  1455. type := NIL;
  1456. RecursivelyFindType(x);
  1457. isValue := TRUE;
  1458. RecursivelySetExpression(x);
  1459. arrayType := RecursivelySetType(x);
  1460. x.SetType(arrayType);
  1461. IF isValue THEN
  1462. value := SyntaxTree.NewMathArrayValue(x.position,x);
  1463. x.SetResolved(value);
  1464. value.SetType(arrayType);
  1465. END;
  1466. x.SetType(arrayType);
  1467. RETURN x;
  1468. END ResolveMathArrayExpression;
  1469. (** check and resolve unary expression **)
  1470. PROCEDURE ResolveUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression): SyntaxTree.Expression;
  1471. VAR
  1472. left: SyntaxTree.Expression;
  1473. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1474. bool: BOOLEAN;
  1475. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1476. value: SyntaxTree.Value;
  1477. BEGIN
  1478. type := SyntaxTree.invalidType;
  1479. left := ResolveExpression(unaryExpression.left);
  1480. unaryExpression.SetLeft(left);
  1481. operator := unaryExpression.operator;
  1482. result := unaryExpression;
  1483. IF ~system.operatorDefined[operator] THEN
  1484. Error(left.position,"Operator Not Defined");
  1485. RETURN SyntaxTree.invalidExpression;
  1486. ELSIF left.type = NIL THEN
  1487. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1488. RETURN SyntaxTree.invalidExpression;
  1489. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1490. RETURN SyntaxTree.invalidExpression;
  1491. END;
  1492. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1493. operatorCall := NewOperatorCall(unaryExpression.position, unaryExpression, operator,left,NIL,NIL);
  1494. END;
  1495. IF operatorCall # NIL THEN
  1496. result := operatorCall;
  1497. type := operatorCall.type;
  1498. (* admissible operators
  1499. Minus number, set
  1500. Not boolean
  1501. *)
  1502. ELSE
  1503. CASE unaryExpression.operator OF
  1504. |Scanner.Minus:
  1505. IF IsIntegerType(left.type.resolved) THEN
  1506. IF left.resolved # NIL THEN
  1507. int := -left.resolved(SyntaxTree.IntegerValue).value;
  1508. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1509. result.SetResolved(value);
  1510. type := Global.GetIntegerType(system,int);
  1511. value.SetType(type);
  1512. ELSE
  1513. type := left.type
  1514. END
  1515. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1516. IF IsRealValue(left,real) THEN
  1517. value := SyntaxTree.NewRealValue(unaryExpression.position,-real, Scanner.None);
  1518. result.SetResolved(value);
  1519. type := left.type;
  1520. value.SetType(type);
  1521. ELSE
  1522. type := left.type;
  1523. END;
  1524. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1525. IF IsSetValue(left,set) THEN
  1526. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1527. result.SetResolved(value);
  1528. type := Global.GetSetType(system,-set);
  1529. value.SetType(type);
  1530. ELSE
  1531. type := left.type;
  1532. END;
  1533. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1534. IF IsComplexValue(left, real, imaginary) THEN
  1535. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary, Scanner.None);
  1536. result.SetResolved(value);
  1537. type := left.type;
  1538. value.SetType(type);
  1539. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1540. ELSE
  1541. type := left.type;
  1542. END
  1543. ELSE
  1544. Error(left.position,"unary operator not applicable");
  1545. END;
  1546. |Scanner.Not:
  1547. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1548. IF IsBooleanValue(left,bool) THEN
  1549. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1550. result.SetResolved(value);
  1551. type := system.booleanType;
  1552. value.SetType(type);
  1553. ELSE
  1554. type := system.booleanType;
  1555. END;
  1556. ELSE
  1557. Error(left.position,"unary operator not applicable");
  1558. END;
  1559. |Scanner.Plus:
  1560. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  1561. result := left; type := left.type;
  1562. ELSE
  1563. Error(left.position,"unary operator not applicable");
  1564. END;
  1565. (* ADDRESS OF *)
  1566. |Scanner.Address:
  1567. IF HasAddress(left) THEN
  1568. type := system.addressType;
  1569. ELSE
  1570. type := SyntaxTree.invalidType;
  1571. Error(left.position,"has no address");
  1572. Printout.Info("par", left);
  1573. END;
  1574. (* SIZE OF *)
  1575. |Scanner.Size:
  1576. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  1577. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  1578. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  1579. value := SyntaxTree.NewIntegerValue(left.position, int);
  1580. result.SetResolved(value);
  1581. type := Global.GetIntegerType(system,int);
  1582. value.SetType(type)
  1583. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  1584. ELSE
  1585. (* for variables, system sizeof could represent the physically occupied size
  1586. determined via the type descriptor, implement that ? *)
  1587. Error(left.position,"is not a type symbol");
  1588. END
  1589. (* ALIAS OF *)
  1590. |Scanner.Alias:
  1591. type := left.type.resolved;
  1592. IF ~(type IS SyntaxTree.MathArrayType) THEN
  1593. type := SyntaxTree.invalidType;
  1594. Error(left.position,"alias on non math array type");
  1595. END;
  1596. ELSE
  1597. Error(left.position,"unary operator not defined");
  1598. END;
  1599. END;
  1600. result.SetType(type);
  1601. RETURN result
  1602. END ResolveUnaryExpression;
  1603. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  1604. VAR
  1605. result: SyntaxTree.Expression;
  1606. array: SyntaxTree.MathArrayExpression;
  1607. value: SyntaxTree.MathArrayValue;
  1608. isValue: BOOLEAN;
  1609. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  1610. BEGIN
  1611. type := type.resolved;
  1612. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  1613. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  1614. END;
  1615. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  1616. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  1617. END;
  1618. RETURN type
  1619. END BaseType;
  1620. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  1621. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  1622. BEGIN
  1623. numberElements := x.elements.Length();
  1624. FOR i := 0 TO numberElements-1 DO
  1625. expression := x.elements.GetExpression(i);
  1626. IF expression IS SyntaxTree.MathArrayExpression THEN
  1627. array := SyntaxTree.NewMathArrayExpression(position, SyntaxTree.NewExpressionList());
  1628. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  1629. expression := array;
  1630. ELSE
  1631. position := expression.position;
  1632. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1633. isValue := isValue & (expression.resolved # NIL);
  1634. END;
  1635. to.elements.AddExpression(expression);
  1636. END;
  1637. END RecursivelyConvert;
  1638. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1639. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1640. arrayType: SyntaxTree.MathArrayType;
  1641. BEGIN
  1642. numberElements := x.elements.Length();
  1643. baseType := NIL;
  1644. gsize := 0;
  1645. FOR i := 0 TO numberElements-1 DO
  1646. expression := x.elements.GetExpression(i);
  1647. IF expression IS SyntaxTree.MathArrayExpression THEN
  1648. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1649. IF i=0 THEN
  1650. gsize := size;
  1651. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1652. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1653. ELSE expression.SetType(baseType)
  1654. END;
  1655. ELSIF baseType = NIL THEN baseType := type;
  1656. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1657. END;
  1658. END;
  1659. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1660. arrayType.SetArrayBase(baseType);
  1661. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1662. RETURN ResolveType(arrayType);
  1663. END RecursivelySetType;
  1664. BEGIN
  1665. result := SyntaxTree.invalidExpression;
  1666. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  1667. result := expression (* do not convert *)
  1668. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  1669. isValue := TRUE;
  1670. type := BaseType(type);
  1671. array := SyntaxTree.NewMathArrayExpression(expression.position, SyntaxTree.NewExpressionList());
  1672. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  1673. value := SyntaxTree.NewMathArrayValue(array.position,array);
  1674. value.SetType(RecursivelySetType(array));
  1675. result := value;
  1676. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  1677. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  1678. result := NewOperatorCall(position,expression, Global.Conversion,expression,NIL,type);
  1679. IF result = NIL THEN
  1680. result := SyntaxTree.invalidExpression;
  1681. Error(position, "incompatible conversion");
  1682. IF VerboseErrorMessage THEN
  1683. Printout.Info("expression",expression);
  1684. Printout.Info("type",type);
  1685. END;
  1686. END;
  1687. END;
  1688. RETURN result
  1689. END MathArrayConversion;
  1690. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  1691. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  1692. BEGIN
  1693. result := expression; type := type.resolved;
  1694. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  1695. (* skip, no conversion *)
  1696. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  1697. int := expression(SyntaxTree.IntegerValue).value;
  1698. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  1699. int := Global.ConvertSigned(int,system.SizeOf(type));
  1700. result := SyntaxTree.NewIntegerValue(position,int);
  1701. result.SetType(type);
  1702. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  1703. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  1704. result := SyntaxTree.NewIntegerValue(position,int);
  1705. result.SetType(type);
  1706. ELSIF (type IS SyntaxTree.FloatType) THEN
  1707. result := SyntaxTree.NewRealValue(expression.position,int,Scanner.None);
  1708. result.SetType(type);
  1709. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1710. result := SyntaxTree.NewComplexValue(expression.position, int, 0, Scanner.None);
  1711. result.SetType(type);
  1712. ELSIF (type IS SyntaxTree.SetType) THEN
  1713. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  1714. result.SetType(type);
  1715. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  1716. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  1717. result.SetType(type);
  1718. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  1719. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  1720. result.SetType(type);
  1721. ELSIF (type IS SyntaxTree.PortType) THEN
  1722. result := ConvertValue(position, expression, system.integerType);
  1723. ELSE
  1724. Error(position, "integer value cannot be converted");
  1725. result := SyntaxTree.invalidExpression;
  1726. IF VerboseErrorMessage THEN
  1727. Printout.Info("expression",expression);
  1728. Printout.Info("type",type);
  1729. END;
  1730. END;
  1731. ELSIF IsRealValue(expression,real) THEN
  1732. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  1733. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  1734. result := SyntaxTree.NewIntegerValue(expression.position,int);
  1735. result.SetType(type);
  1736. ELSIF (type IS SyntaxTree.IntegerType) THEN
  1737. int := ENTIERH(real);
  1738. result := SyntaxTree.NewIntegerValue(expression.position,int);
  1739. result.SetType(type);
  1740. ELSIF (type IS SyntaxTree.FloatType) THEN
  1741. result := SyntaxTree.NewRealValue(position,real,Scanner.None);
  1742. result.SetType(type);
  1743. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1744. result := SyntaxTree.NewComplexValue(expression.position, real, 0, Scanner.None);
  1745. result.SetType(type);
  1746. result(SyntaxTree.ComplexValue).UpdateSubtype;
  1747. ELSIF (type IS SyntaxTree.PortType) THEN
  1748. result := ConvertValue(position, expression, system.integerType);
  1749. ELSE
  1750. Error(position, "real value cannot be converted");
  1751. result := SyntaxTree.invalidExpression;
  1752. END
  1753. ELSIF IsComplexValue(expression, real, imaginary) THEN
  1754. IF (type IS SyntaxTree.ComplexType) THEN
  1755. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary, 0);
  1756. result.SetType(type);
  1757. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1758. ELSE
  1759. Error(position, "complex value cannot be converted");
  1760. result := SyntaxTree.invalidExpression;
  1761. END
  1762. ELSIF IsSetValue(expression,set) THEN
  1763. IF (type IS SyntaxTree.IntegerType) THEN
  1764. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  1765. result.SetType(type);
  1766. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  1767. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  1768. result.SetType(type);
  1769. ELSIF (type IS SyntaxTree.PortType) THEN
  1770. result := ConvertValue(position, expression, system.integerType);
  1771. ELSIF (type IS SyntaxTree.SetType) THEN
  1772. set := Global.ConvertSet(set,system.SizeOf(type));
  1773. result := SyntaxTree.NewSetValue(expression.position,set);
  1774. result.SetType(type);
  1775. ELSE
  1776. Error(position, "set value cannot be converted");
  1777. result := SyntaxTree.invalidExpression;
  1778. END;
  1779. ELSIF IsStringValue(expression,string) THEN
  1780. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  1781. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  1782. result.SetType(type);
  1783. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  1784. ELSE
  1785. Error(position, "string value cannot be converted");
  1786. result := SyntaxTree.invalidExpression;
  1787. END;
  1788. ELSIF IsCharacterValue(expression,char) THEN
  1789. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  1790. string[0] := char; string[1] := 0X;
  1791. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  1792. result := SyntaxTree.NewStringValue(expression.position,string);
  1793. result.SetType(type);
  1794. ELSIF (type IS SyntaxTree.ByteType) THEN
  1795. (* do not simply set the new type as this could invalidate types of constants *)
  1796. result := SyntaxTree.NewCharacterValue(expression.position,char);
  1797. result.SetType(type)
  1798. ELSIF (type IS SyntaxTree.IntegerType) THEN
  1799. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  1800. result.SetType(type);
  1801. ELSIF (type IS SyntaxTree.SetType) THEN
  1802. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  1803. result.SetType(type);
  1804. ELSIF (type IS SyntaxTree.CharacterType) THEN
  1805. result := SyntaxTree.NewCharacterValue(expression.position,char);
  1806. result.SetType(type);
  1807. ELSIF (type IS SyntaxTree.PortType) THEN
  1808. result := ConvertValue(position, expression, system.integerType);
  1809. ELSE
  1810. Error(position, "character value cannot be converted");
  1811. result := SyntaxTree.invalidExpression;
  1812. END;
  1813. ELSIF expression IS SyntaxTree.NilValue THEN
  1814. IF type IS SyntaxTree.AddressType THEN
  1815. result := SyntaxTree.NewIntegerValue(position,0);
  1816. result.SetType(type);
  1817. ELSE
  1818. result := expression;
  1819. END;
  1820. (* nothing to be done *)
  1821. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  1822. result := MathArrayConversion(position, expression,type);
  1823. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  1824. int := expression(SyntaxTree.EnumerationValue).value;
  1825. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  1826. int := Global.ConvertSigned(int,system.SizeOf(type));
  1827. result := SyntaxTree.NewIntegerValue(position,int);
  1828. result.SetType(type);
  1829. ELSE
  1830. result := expression;
  1831. END;
  1832. (* nothing to be done *)
  1833. ELSE
  1834. Error(position, "expression cannot be converted");
  1835. IF VerboseErrorMessage THEN
  1836. Printout.Info("expression",expression);
  1837. Printout.Info("type",type);
  1838. END;
  1839. result := SyntaxTree.invalidExpression;
  1840. END;
  1841. RETURN result
  1842. END ConvertValue;
  1843. (**
  1844. return a conversion of an expression to a given type
  1845. - if expression is already of same type then return expression
  1846. - if incompatible conversion then report error and return invalidExpression
  1847. **)
  1848. PROCEDURE NewConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  1849. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  1850. BEGIN
  1851. type := type.resolved;
  1852. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  1853. result := expression;
  1854. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  1855. ELSIF expression = NIL THEN (* NIL expression *)
  1856. ELSIF expression.type = NIL THEN
  1857. Error(position, "expression of type NIL cannot be converted");
  1858. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  1859. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  1860. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  1861. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  1862. ELSIF expression.resolved # NIL THEN (* value *)
  1863. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  1864. IF value IS SyntaxTree.Value THEN
  1865. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  1866. result.SetResolved(value(SyntaxTree.Value));
  1867. result.SetType(value.type);
  1868. ELSE
  1869. result := value
  1870. END;
  1871. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  1872. expressionList := SyntaxTree.NewExpressionList();
  1873. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  1874. typeDeclaration.SetDeclaredType(type);
  1875. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,NIL,typeDeclaration);
  1876. typeSymbol.SetType(typeDeclaration.type);
  1877. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  1878. expressionList.AddExpression(expression);
  1879. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,NIL, Global.systemVal,NIL,expressionList);
  1880. result.SetType(type);
  1881. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  1882. IF inConversion>5 THEN
  1883. Error(expression.position,"recursive Conversion");
  1884. IF VerboseErrorMessage THEN
  1885. Printout.Info("expression",expression);
  1886. Printout.Info("type",type);
  1887. END;
  1888. END;
  1889. INC(inConversion);
  1890. IF inConversion < 10 THEN
  1891. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  1892. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  1893. result := expression
  1894. ELSE
  1895. result := MathArrayConversion(position, expression,type);
  1896. END;
  1897. ELSE
  1898. Error(expression.position,"cannot convert non array type to array type")
  1899. END;
  1900. END;
  1901. DEC(inConversion);
  1902. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  1903. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  1904. result := expression; result.SetType(type);
  1905. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  1906. OR ~(type IS SyntaxTree.ArrayType) THEN
  1907. Error(expression.position,"cannot convert array type to non-array type")
  1908. END;
  1909. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  1910. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  1911. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  1912. (*skip, no conversion*)
  1913. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  1914. (* skip, no conversion *)
  1915. ELSE
  1916. ASSERT(~(type IS SyntaxTree.RangeType));
  1917. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  1918. ASSERT(type # NIL);
  1919. END;
  1920. RETURN result
  1921. END NewConversion;
  1922. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  1923. BEGIN
  1924. IF CompatibleTo(system,expression.type, type) THEN
  1925. RETURN NewConversion(position, expression, type, NIL);
  1926. ELSE
  1927. Error(expression.position, "incompatible expression");
  1928. RETURN SyntaxTree.invalidExpression
  1929. END;
  1930. END CompatibleConversion;
  1931. (**
  1932. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  1933. **)
  1934. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  1935. VAR leftType,rightType: SyntaxTree.Type;
  1936. BEGIN
  1937. IF left.type = NIL THEN Error(left.position,"no type")
  1938. ELSIF right.type= NIL THEN Error(right.position,"no type")
  1939. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  1940. (* error already handled *)
  1941. ELSE
  1942. leftType := left.type.resolved; rightType := right.type.resolved;
  1943. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  1944. right := NewConversion(right.position, right, leftType, NIL);
  1945. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  1946. left := NewConversion(left.position,left,rightType,NIL);
  1947. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  1948. right := NewConversion(right.position, right, leftType, NIL);
  1949. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  1950. left := NewConversion(left.position,left,rightType,NIL);
  1951. ELSIF CompatibleTo(system,leftType,rightType) THEN
  1952. left := NewConversion(left.position,left,right.type.resolved,NIL);
  1953. ELSIF CompatibleTo(system,rightType,leftType) THEN
  1954. right := NewConversion(right.position,right,left.type.resolved,NIL);
  1955. ELSIF
  1956. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  1957. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  1958. (* must be the case LONGREAL / COMPLEX ) *)
  1959. left := NewConversion(left.position, left, Global.Complex128, NIL);
  1960. right := NewConversion(right.position, right, Global.Complex128, NIL);
  1961. ELSE
  1962. Error(left.position,"incompatible operands");
  1963. END;
  1964. END;
  1965. END ConvertOperands;
  1966. (** find and return best operator matching to parameter list (nil, if none)
  1967. - search current module scope and all (directly or indirectly) imported modules for matching operator
  1968. - take operator with smalles distance, where signature distance is computed in procedure Distance
  1969. **)
  1970. PROCEDURE FindOperator(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  1971. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  1972. identifier: SyntaxTree.Identifier;
  1973. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  1974. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  1975. BEGIN
  1976. operator := scope.firstOperator;
  1977. WHILE(operator # NIL) DO
  1978. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  1979. procedureType := operator.type(SyntaxTree.ProcedureType);
  1980. distance := Distance(system, procedureType,actualParameters);
  1981. IF (distance < Infinity) THEN
  1982. IF returnType # NIL THEN
  1983. IF procedureType.returnType = NIL THEN
  1984. distance := Infinity
  1985. ELSE
  1986. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  1987. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  1988. END;
  1989. END;
  1990. END;
  1991. (*
  1992. IF distance < Infinity THEN
  1993. TRACE(distance, operator);
  1994. Printout.Info("potential operator",operator);
  1995. ELSE
  1996. Printout.Info("operator not possible",operator);
  1997. END;
  1998. *)
  1999. IF distance < bestDistance THEN
  2000. bestDistance := distance;
  2001. bestOperator := operator;
  2002. END;
  2003. END;
  2004. operator := operator.nextOperator;
  2005. END;
  2006. (*
  2007. Printout.Info("taken operator",bestOperator);
  2008. *)
  2009. END FindInScope;
  2010. BEGIN
  2011. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2012. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2013. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2014. import := currentScope.ownerModule.moduleScope.firstImport;
  2015. WHILE (bestDistance > 0) & (import # NIL) DO
  2016. IF import.module # NIL THEN
  2017. identifier := Global.GetIdentifier(operator,import.module.case);
  2018. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2019. END;
  2020. import := import.nextImport;
  2021. END;
  2022. RETURN bestOperator
  2023. END FindOperator;
  2024. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2025. BEGIN
  2026. currentScope := scope;
  2027. END SetCurrentScope;
  2028. (**
  2029. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2030. - handle LEN and DIM operator for array-structured object types
  2031. - find operator, if found then
  2032. - if in other module then add import designator
  2033. - create symbol designator for operator
  2034. - if error then return invalidExpression, if no operator then return NIL
  2035. **)
  2036. PROCEDURE NewOperatorCall*(position: Position; original: SyntaxTree.Expression; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2037. VAR
  2038. operator: SyntaxTree.Operator;
  2039. import: SyntaxTree.Import;
  2040. expression, result: SyntaxTree.Expression;
  2041. designator: SyntaxTree.Designator;
  2042. actualParameters: SyntaxTree.ExpressionList;
  2043. castReturnType : SyntaxTree.MathArrayType;
  2044. BEGIN
  2045. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2046. result := SyntaxTree.invalidExpression
  2047. ELSIF leftExpression = NIL THEN
  2048. result := NIL
  2049. ELSE
  2050. IF ~complexNumbersImported THEN
  2051. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2052. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2053. THEN
  2054. (* operators on complex numbers *)
  2055. ImportModule(Global.ComplexNumbersName,position);
  2056. complexNumbersImported := TRUE;
  2057. END;
  2058. END;
  2059. (* import OCArrayBase if needed *)
  2060. IF ~arrayBaseImported THEN
  2061. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2062. IF op = Global.Dim THEN
  2063. (* not existing in OCArrayBase *)
  2064. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2065. (* not existing in OCArrayBase *)
  2066. ELSE
  2067. ImportModule(Global.ArrayBaseName,position);
  2068. arrayBaseImported := TRUE;
  2069. END
  2070. END;
  2071. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2072. (* LEN(RANGE) *)
  2073. ImportModule(Global.ArrayBaseName,position);
  2074. arrayBaseImported := TRUE;
  2075. END;
  2076. END;
  2077. actualParameters := SyntaxTree.NewExpressionList();
  2078. actualParameters.AddExpression(leftExpression);
  2079. IF rightExpression # NIL THEN
  2080. actualParameters.AddExpression(rightExpression)
  2081. END;
  2082. operator := FindOperator(system,op,actualParameters,resultType);
  2083. IF operator # NIL THEN
  2084. designator := NIL;
  2085. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2086. import := currentScope.ownerModule.moduleScope.firstImport;
  2087. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2088. import := import.nextImport;
  2089. END;
  2090. expression := NewSymbolDesignator(position,NIL,NIL,import);
  2091. designator := expression(SyntaxTree.Designator);
  2092. END;
  2093. expression := NewSymbolDesignator(position,NIL,designator,operator);
  2094. designator := expression(SyntaxTree.Designator);
  2095. result := NewProcedureCallDesignator(position, original, designator,actualParameters);
  2096. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2097. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2098. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2099. result.SetType(castReturnType);
  2100. END;
  2101. ELSE
  2102. result := NIL;
  2103. END;
  2104. END;
  2105. RETURN result
  2106. END NewOperatorCall;
  2107. (** check and resolve binary expression **)
  2108. PROCEDURE ResolveBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression): SyntaxTree.Expression;
  2109. VAR left,right,result: SyntaxTree.Expression;
  2110. leftType, rightType: SyntaxTree.Type;
  2111. il,ir: Basic.Integer; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: Basic.Integer;bl,br: BOOLEAN; sl,sr: Basic.Set; strl,strr: Scanner.StringType;
  2112. cl,cr: CHAR;
  2113. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2114. type: SyntaxTree.Type;
  2115. value: SyntaxTree.Value;
  2116. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2117. integerConstantFolding: BOOLEAN;
  2118. list: SyntaxTree.ExpressionList;
  2119. PROCEDURE NewBool(v: BOOLEAN);
  2120. BEGIN
  2121. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2122. value.SetType(system.booleanType);
  2123. result.SetResolved(value);
  2124. type := system.booleanType
  2125. END NewBool;
  2126. PROCEDURE NewSet(v: Basic.Set);
  2127. BEGIN
  2128. value := Global.NewSetValue(system,binaryExpression.position,v);
  2129. result.SetResolved(value);
  2130. type := value.type;
  2131. END NewSet;
  2132. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2133. BEGIN
  2134. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2135. (* type cast to "larger" type only if the value is still in the range *)
  2136. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2137. value.SetType(t);
  2138. END;
  2139. result.SetResolved(value);
  2140. type := value.type;
  2141. END NewInteger;
  2142. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2143. BEGIN
  2144. value := SyntaxTree.NewRealValue(binaryExpression.position,v, Scanner.None);
  2145. value.SetType(t);
  2146. result.SetResolved(value);
  2147. type := t;
  2148. END NewReal;
  2149. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2150. BEGIN
  2151. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue, Scanner.None);
  2152. value.SetType(t);
  2153. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2154. result.SetResolved(value);
  2155. type := t;
  2156. END NewComplex;
  2157. BEGIN
  2158. type := SyntaxTree.invalidType;
  2159. left := ResolveExpression(binaryExpression.left);
  2160. right := ResolveExpression(binaryExpression.right);
  2161. binaryExpression.SetLeft(left);
  2162. binaryExpression.SetRight(right);
  2163. result := binaryExpression;
  2164. operator := binaryExpression.operator;
  2165. IF ~system.operatorDefined[operator] THEN
  2166. Error(left.position,"Operator Not Defined");
  2167. RETURN SyntaxTree.invalidExpression;
  2168. END;
  2169. IF left.type = NIL THEN
  2170. Error(left.position,"Expression has no result type");
  2171. RETURN SyntaxTree.invalidExpression;
  2172. END;
  2173. IF right.type = NIL THEN
  2174. Error(right.position,"Expression has no result type");
  2175. RETURN SyntaxTree.invalidExpression;
  2176. END;
  2177. leftType := left.type.resolved; rightType := right.type.resolved;
  2178. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2179. operatorCall := NewOperatorCall(binaryExpression.position,binaryExpression, operator,left,right,NIL);
  2180. END;
  2181. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2182. list := SyntaxTree.NewExpressionList();
  2183. list.AddExpression(right);
  2184. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2185. END;
  2186. IF operatorCall # NIL THEN
  2187. result := operatorCall;
  2188. type := operatorCall.type;
  2189. (* admissible operators:
  2190. Times, Plus, Minus numeric numeric numeric
  2191. set set set
  2192. Slash numeric numeric real /complex
  2193. set set set
  2194. Div , Mod integer integer integer
  2195. And, Or bool bool bool
  2196. Equal, Unequal basic basic bool
  2197. pointer pointer bool
  2198. object object bool
  2199. record record bool
  2200. string string bool
  2201. enumerator enumerator bool
  2202. Less, LessEqual,
  2203. Greater, GreaterEqual integer/real integer/real bool
  2204. enumerator enumerator bool
  2205. In integer set bool
  2206. Is pointer type bool
  2207. object type bool
  2208. record type bool
  2209. Upto: special abbreviation for a..b
  2210. *)
  2211. ELSIF (left.type = NIL) THEN
  2212. Error(left.position,"type (left operand) = NIL in binary expression");
  2213. D.Str("nil type in "); D.Type(left); D.Ln;
  2214. result := SyntaxTree.invalidExpression;
  2215. ELSIF (right.type = NIL) THEN
  2216. Error(right.position,"type (right operand) = NIL in binary expression");
  2217. result := SyntaxTree.invalidExpression;
  2218. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2219. result := SyntaxTree.invalidExpression;
  2220. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2221. HALT(100);
  2222. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2223. type := system.booleanType;
  2224. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2225. Error(right.position,"is not a type ");
  2226. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2227. Error(binaryExpression.position,"is not a type extension of ");
  2228. IF VerboseErrorMessage THEN
  2229. Printout.Info("left",left);
  2230. Printout.Info("right",right);
  2231. END;
  2232. ELSIF IsUnsafePointer(left.type) THEN
  2233. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2234. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2235. NewBool(TRUE)
  2236. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2237. NewBool(TRUE);
  2238. ELSIF IsUnextensibleRecord(left) THEN
  2239. NewBool(FALSE)
  2240. END
  2241. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2242. Error(right.position,"must not be a type");
  2243. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2244. Error(left.position,"must not be a type");
  2245. ELSIF operator = Scanner.In THEN (* left IN right *)
  2246. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2247. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2248. NewBool(il IN sr);
  2249. ELSE
  2250. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2251. left := NewConversion(left.position, left, system.longintType,NIL);
  2252. binaryExpression.SetLeft(left)
  2253. END;
  2254. type := system.booleanType;
  2255. END
  2256. ELSE
  2257. Error(binaryExpression.position, "incompatible operands");
  2258. END
  2259. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2260. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2261. Error(binaryExpression.position,"incompatible operands");
  2262. END;
  2263. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2264. ELSE Error(binaryExpression.position,"operator not defined 1")
  2265. END
  2266. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2267. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2268. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2269. Error(binaryExpression.position,"incompatible operands");
  2270. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2271. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2272. left := NewConversion(left.position, left, system.addressType, NIL);
  2273. right := NewConversion(right.position, right, system.addressType, NIL);
  2274. binaryExpression.SetLeft(left);
  2275. binaryExpression.SetRight(right);
  2276. type := system.addressType;
  2277. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2278. ConvertOperands(left, right);
  2279. binaryExpression.SetLeft(left);
  2280. binaryExpression.SetRight(right);
  2281. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2282. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2283. END;
  2284. type := system.booleanType;
  2285. ELSE
  2286. Error(binaryExpression.position,"operator not defined 3");
  2287. END
  2288. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2289. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2290. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2291. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2292. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2293. CASE operator OF
  2294. |Scanner.Equal: NewBool(strl^=strr^);
  2295. |Scanner.Unequal:NewBool(strl^#strr^);
  2296. |Scanner.Less: NewBool(strl^<strr^);
  2297. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2298. |Scanner.Greater: NewBool(strl^>strr^);
  2299. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2300. ELSE
  2301. Error(binaryExpression.position,"operator not defined 4");
  2302. END;
  2303. END;
  2304. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2305. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2306. type := system.booleanType
  2307. ELSE
  2308. Error(binaryExpression.position,"operator not defined 5");
  2309. END;
  2310. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2311. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2312. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2313. type := system.booleanType;
  2314. ELSE
  2315. Error(binaryExpression.position,"operator not defined 6");
  2316. END
  2317. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2318. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2319. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2320. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2321. type := system.booleanType
  2322. ELSE
  2323. Error(binaryExpression.position,"operator not defined for enumerators");
  2324. END;
  2325. ELSE
  2326. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2327. END;
  2328. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2329. type := system.booleanType;
  2330. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2331. type := system.booleanType;
  2332. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2333. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2334. THEN
  2335. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2336. IF (leftType # rightType) THEN
  2337. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2338. ConvertOperands(left,right); (* operands must be of the same type here *)
  2339. END;
  2340. binaryExpression.SetLeft(left);
  2341. binaryExpression.SetRight(right);
  2342. leftType := left.type.resolved;
  2343. rightType := right.type.resolved;
  2344. END;
  2345. type := leftType;
  2346. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2347. Error(binaryExpression.position,"conversion failed ?");
  2348. IF VerboseErrorMessage THEN
  2349. Printout.Info("left",left);
  2350. Printout.Info("right",right);
  2351. END;
  2352. ELSIF IsIntegerType(leftType) THEN
  2353. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2354. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2355. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2356. Error(binaryExpression.position,"division by zero");
  2357. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2358. Error(binaryExpression.position,"integer division by negative number");
  2359. END;
  2360. END;
  2361. (* constant folding *)
  2362. (* bootstrap64
  2363. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2364. CASE operator OF
  2365. |Scanner.Plus: NewInteger(il+ir,left.type);
  2366. |Scanner.Minus: NewInteger(il-ir,left.type);
  2367. |Scanner.Times: NewInteger(il*ir,left.type);
  2368. |Scanner.Slash:
  2369. IF ir # 0 THEN
  2370. NewReal(il/ir, system.realType);
  2371. END;
  2372. |Scanner.Mod:
  2373. IF ir > 0 THEN
  2374. NewInteger(il MOD ir,left.type);
  2375. END;
  2376. |Scanner.Div:
  2377. IF ir > 0 THEN
  2378. NewInteger(il DIV ir,left.type);
  2379. END;
  2380. |Scanner.Equal: NewBool(il=ir);
  2381. |Scanner.Unequal:NewBool(il#ir);
  2382. |Scanner.Less: NewBool(il<ir);
  2383. |Scanner.LessEqual: NewBool(il<=ir);
  2384. |Scanner.Greater: NewBool(il>ir);
  2385. |Scanner.GreaterEqual: NewBool(il>=ir);
  2386. ELSE Error(binaryExpression.position,"operator not defined 7");
  2387. END;
  2388. ELS*)
  2389. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2390. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2391. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2392. CASE operator OF
  2393. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2394. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2395. |Scanner.Times: NewInteger(hl*hr,left.type);
  2396. |Scanner.Slash:
  2397. IF hr = 0 THEN
  2398. Error(binaryExpression.position,"division by zero");
  2399. ELSE
  2400. IF type.sizeInBits = 64 THEN
  2401. NewReal(hl/hr,system.longrealType);
  2402. ELSE
  2403. NewReal(hl/hr,system.realType)
  2404. END
  2405. END;
  2406. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2407. |Scanner.Mod:
  2408. IF hr = 0 THEN
  2409. Error(binaryExpression.position,"division by zero");
  2410. ELSE
  2411. NewInteger(hl MOD hr, left.type);
  2412. (* bootstrap64
  2413. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2414. *)
  2415. END;
  2416. |Scanner.Div:
  2417. IF hr = 0 THEN
  2418. Error(binaryExpression.position,"division by zero");
  2419. ELSE
  2420. NewInteger(hl DIV hr, left.type);
  2421. (* bootstrap64
  2422. NewInteger(Machine.DivH(hl,hr),left.type);
  2423. *)
  2424. END;
  2425. (* *)
  2426. |Scanner.Equal: NewBool(hl=hr);
  2427. |Scanner.Unequal: NewBool(hl#hr);
  2428. |Scanner.Less: NewBool(hl<hr);
  2429. |Scanner.LessEqual: NewBool(hl<=hr);
  2430. |Scanner.Greater: NewBool(hl>hr);
  2431. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2432. ELSE Error(binaryExpression.position,"operator not defined 8");
  2433. END;
  2434. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2435. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2436. type := left.type
  2437. ELSIF (operator = Scanner.Slash) THEN
  2438. left := NewConversion(left.position,left,system.realType,NIL);
  2439. right := NewConversion(right.position,right,system.realType,NIL);
  2440. binaryExpression.SetLeft(left);
  2441. binaryExpression.SetRight(right);
  2442. type := system.realType
  2443. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2444. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2445. type := system.booleanType
  2446. ELSE
  2447. Error(binaryExpression.position,"operator not defined 9");
  2448. END;
  2449. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2450. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2451. CASE operator OF
  2452. |Scanner.Plus: NewReal(rl+rr,leftType);
  2453. |Scanner.Minus: NewReal(rl-rr,leftType);
  2454. |Scanner.Times:NewReal(rl*rr,leftType);
  2455. |Scanner.Slash:
  2456. IF rr = 0 THEN
  2457. Error(binaryExpression.position,"division by zero");
  2458. ELSE
  2459. NewReal(rl/rr,leftType);
  2460. END
  2461. |Scanner.Equal: NewBool(rl=rr);
  2462. |Scanner.Unequal: NewBool(rl#rr);
  2463. |Scanner.Less: NewBool(rl<rr);
  2464. |Scanner.LessEqual: NewBool(rl<=rr);
  2465. |Scanner.Greater: NewBool(rl>rr);
  2466. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2467. ELSE Error(binaryExpression.position,"operator not defined 10");
  2468. END;
  2469. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2470. type := left.type
  2471. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2472. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2473. type := system.booleanType
  2474. ELSE
  2475. Error(binaryExpression.position,"operator not defined 11");
  2476. IF VerboseErrorMessage THEN
  2477. Printout.Info("left",left);
  2478. Printout.Info("right",right);
  2479. END;
  2480. END;
  2481. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2482. CASE operator OF
  2483. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2484. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2485. ELSE
  2486. Error(binaryExpression.position,"operator not defined");
  2487. IF VerboseErrorMessage THEN
  2488. Printout.Info("left", left);
  2489. Printout.Info("right", right)
  2490. END;
  2491. END;
  2492. IF ~error THEN
  2493. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2494. Error(binaryExpression.position,"division by zero")
  2495. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2496. (* do constant folding *)
  2497. CASE operator OF
  2498. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2499. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2500. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2501. |Scanner.Slash:
  2502. divisor := c * c + d * d;
  2503. ASSERT(divisor # 0);
  2504. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2505. |Scanner.Equal: NewBool((a = c) & (b = d))
  2506. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2507. END
  2508. END
  2509. END
  2510. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2511. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2512. CASE operator OF
  2513. |Scanner.And: NewBool(bl & br);
  2514. |Scanner.Or: NewBool(bl OR br);
  2515. |Scanner.Equal: NewBool(bl = br);
  2516. |Scanner.Unequal: NewBool(bl # br);
  2517. ELSE Error(binaryExpression.position,"operator not defined 12");
  2518. END;
  2519. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2520. type := system.booleanType
  2521. ELSE
  2522. Error(binaryExpression.position,"operator not defined 13");
  2523. END;
  2524. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2525. (* constant folding *)
  2526. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2527. IF operator = Scanner.Equal THEN
  2528. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2529. ELSIF operator = Scanner.Unequal THEN
  2530. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2531. END;
  2532. END;
  2533. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2534. type := system.booleanType;
  2535. ELSE
  2536. Error(binaryExpression.position, "operator not defined");
  2537. END;
  2538. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2539. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2540. CASE operator OF
  2541. |Scanner.Plus: NewSet(sl + sr);
  2542. |Scanner.Minus: NewSet(sl - sr);
  2543. |Scanner.Times: NewSet(sl * sr);
  2544. |Scanner.Slash: NewSet(sl / sr);
  2545. |Scanner.Equal: NewBool(sl=sr);
  2546. |Scanner.Unequal: NewBool(sl#sr);
  2547. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2548. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2549. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2550. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2551. ELSE Error(binaryExpression.position,"operator not defined 14");
  2552. END;
  2553. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2554. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2555. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2556. THEN
  2557. type := system.booleanType
  2558. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2559. type := left.type
  2560. ELSE
  2561. Error(binaryExpression.position,"operator not defined 15");
  2562. END;
  2563. ELSIF IsCharacterType(left.type) THEN
  2564. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  2565. CASE operator OF
  2566. |Scanner.Equal: NewBool(cl=cr);
  2567. |Scanner.Unequal: NewBool(cl#cr);
  2568. |Scanner.Less: NewBool(cl<cr);
  2569. |Scanner.LessEqual: NewBool(cl<=cr);
  2570. |Scanner.Greater: NewBool(cl>cr);
  2571. |Scanner.GreaterEqual: NewBool(cl>=cr);
  2572. ELSE Error(binaryExpression.position,"operator not defined 16");
  2573. END;
  2574. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2575. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2576. type := system.booleanType
  2577. ELSE
  2578. Error(binaryExpression.position,"operator not defined 17");
  2579. END;
  2580. ELSE
  2581. Error(binaryExpression.position,"operator not defined 18");
  2582. END;
  2583. ELSE
  2584. Error(binaryExpression.position,"operator not defined 19");
  2585. END;
  2586. IF type = SyntaxTree.invalidType THEN
  2587. result := SyntaxTree.invalidExpression
  2588. ELSE
  2589. result.SetType(type)
  2590. END;
  2591. RETURN result
  2592. END ResolveBinaryExpression;
  2593. (** resolve a range expression of the from <<first .. last BY step>>
  2594. - depending on the context different things are checked:
  2595. ArrayIndex:
  2596. - components must be integers
  2597. - replace missing lower bound with 0
  2598. - replace missing upper bound with MAX(LONGINT)
  2599. - replace missing step size with 1
  2600. SetElement:
  2601. - components must be integers
  2602. - replace missing lower bound with 0
  2603. - replace missing upper bound with MAX(SET)
  2604. - must not have step size
  2605. CaseGuard:
  2606. - components must be constant
  2607. - components must be integers or characters
  2608. - must have lower and upper bound present
  2609. - components are made compatible
  2610. - must not have step size
  2611. - if error: return invalidExpression
  2612. **)
  2613. PROCEDURE ResolveRangeExpression(x: SyntaxTree.RangeExpression): SyntaxTree.Expression;
  2614. VAR
  2615. hasError: BOOLEAN;
  2616. first, last, step: SyntaxTree.Expression;
  2617. BEGIN
  2618. hasError := FALSE;
  2619. first := x.first;
  2620. last := x.last;
  2621. step := x.step;
  2622. (* check lower bound *)
  2623. IF x.context = SyntaxTree.CaseGuard THEN
  2624. IF first = NIL THEN
  2625. Error(x.position, "missing lower bound");
  2626. hasError := TRUE
  2627. ELSE
  2628. first := ResolveExpression(first);
  2629. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  2630. Error(first.position, "lower bound not integer or character");
  2631. hasError := TRUE
  2632. ELSE
  2633. IF first IS SyntaxTree.StringValue THEN
  2634. (* add conversion from string to character *)
  2635. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  2636. END
  2637. END;
  2638. (* check if expression is constant *)
  2639. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  2640. (* error already reported *)
  2641. hasError := TRUE
  2642. END
  2643. END
  2644. ELSE (* ArrayIndex, SetElement *)
  2645. IF first = NIL THEN
  2646. first := SyntaxTree.NewIntegerValue(x.position, 0);
  2647. END;
  2648. first := ResolveExpression(first);
  2649. IF IsIntegerType(first.type.resolved) THEN
  2650. first := NewConversion(first.position, first, system.lenType, NIL)
  2651. ELSE
  2652. Error(first.position, "lower bound not integer");
  2653. hasError := TRUE
  2654. END
  2655. END;
  2656. (* check upper bound *)
  2657. IF x.context = SyntaxTree.CaseGuard THEN
  2658. IF last = NIL THEN
  2659. Error(x.position, "missing upper bound");
  2660. hasError := TRUE
  2661. ELSE
  2662. last := ResolveExpression(last);
  2663. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  2664. Error(last.position, "lower bound not integer or character");
  2665. hasError := TRUE
  2666. ELSE
  2667. IF last IS SyntaxTree.StringValue THEN
  2668. (* add conversion from string to character *)
  2669. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  2670. END
  2671. END;
  2672. (* check if expression is constant *)
  2673. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  2674. (* error already reported *)
  2675. hasError := TRUE
  2676. ELSE
  2677. (* try to make lower and upper bound compatible *)
  2678. ConvertOperands(first, last);
  2679. IF first.type.resolved # last.type.resolved THEN
  2680. Error(x.position, "lower and upper bounds incompatible");
  2681. hasError := TRUE
  2682. END
  2683. END
  2684. END
  2685. ELSE (* ArrayIndex, SetElement *)
  2686. IF last = NIL THEN
  2687. IF x.context = SyntaxTree.ArrayIndex THEN
  2688. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  2689. ELSE
  2690. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  2691. END
  2692. END;
  2693. last := ResolveExpression(last);
  2694. IF IsIntegerType(last.type.resolved) THEN
  2695. last := NewConversion(last.position, last, system.lenType, NIL)
  2696. ELSE
  2697. Error(last.position, "upper bound not integer");
  2698. hasError := TRUE
  2699. END
  2700. END;
  2701. (* check step size *)
  2702. IF x.context = SyntaxTree.ArrayIndex THEN
  2703. IF step = NIL THEN
  2704. step := SyntaxTree.NewIntegerValue(x.position, 1)
  2705. END;
  2706. step := ResolveExpression(step);
  2707. IF IsIntegerType(step.type.resolved) THEN
  2708. step := NewConversion(step.position, step, system.lenType, NIL)
  2709. ELSE
  2710. Error(step.position, "step size not integer");
  2711. hasError := TRUE
  2712. END
  2713. ELSE (* SetElement, CaseGuard *)
  2714. IF step # NIL THEN
  2715. Error(last.position, "step size not allowed in this context");
  2716. hasError := TRUE
  2717. END
  2718. END;
  2719. IF hasError THEN
  2720. RETURN SyntaxTree.invalidExpression
  2721. ELSE
  2722. x.SetFirst(first);
  2723. x.SetLast(last);
  2724. x.SetStep(step);
  2725. x.SetType(system.rangeType);
  2726. x.SetAssignable(FALSE); (* range expressions may never be assigned to *)
  2727. RETURN x;
  2728. END
  2729. END ResolveRangeExpression;
  2730. (** resolve the expression d and return result as designator
  2731. - resolve expression
  2732. - if expression is a designator then return designator else error message and return invalidDesignator
  2733. **)
  2734. PROCEDURE ResolveDesignator(d: SyntaxTree.Expression): SyntaxTree.Designator;
  2735. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  2736. BEGIN
  2737. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  2738. resolved := ResolveExpression(d);
  2739. IF resolved = SyntaxTree.invalidExpression THEN
  2740. (* error should already have been reported *)
  2741. result := SyntaxTree.invalidDesignator;
  2742. ELSIF resolved IS SyntaxTree.Designator THEN
  2743. result := resolved(SyntaxTree.Designator);
  2744. ELSE
  2745. Error(d.position,"is no designator ! ");
  2746. result := SyntaxTree.invalidDesignator;
  2747. END;
  2748. (* result.type might be nil. *)
  2749. RETURN result
  2750. END ResolveDesignator;
  2751. (**
  2752. self designator generated in this module
  2753. nothing to be resolved
  2754. **)
  2755. PROCEDURE ResolveSelfDesignator(x: SyntaxTree.SelfDesignator): SyntaxTree.Expression;
  2756. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  2757. BEGIN
  2758. (* check if in record scope *)
  2759. scope := currentScope;
  2760. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter # NIL) THEN
  2761. RETURN NewSymbolDesignator(x.position, x, NIL, scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter);
  2762. END;
  2763. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  2764. scope := scope.outerScope;
  2765. END;
  2766. IF scope = NIL THEN (* in module scope *)
  2767. x.SetType(system.anyType);
  2768. ELSIF scope IS SyntaxTree.CellScope THEN
  2769. cell := scope(SyntaxTree.CellScope).ownerCell;
  2770. x.SetType(cell);
  2771. ELSE (* in record scope *)
  2772. record := scope(SyntaxTree.RecordScope).ownerRecord;
  2773. IF (record # NIL) & (record.pointerType # NIL) THEN
  2774. type := ResolveType(record.pointerType);
  2775. x.SetType(type);
  2776. ELSE
  2777. x.SetType(record);
  2778. x.SetAssignable(TRUE); (* var parameter *)
  2779. END;
  2780. END;
  2781. RETURN x;
  2782. END ResolveSelfDesignator;
  2783. PROCEDURE ResolveResultDesignator(x: SyntaxTree.ResultDesignator): SyntaxTree.Expression;
  2784. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  2785. BEGIN
  2786. scope := currentScope;
  2787. IF (scope # NIL) & ((scope IS SyntaxTree.ProcedureScope) OR (scope IS SyntaxTree.BlockScope)) THEN
  2788. IF scope IS SyntaxTree.ProcedureScope THEN
  2789. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  2790. procedureType := procedure.type(SyntaxTree.ProcedureType);
  2791. returnType := procedureType.returnType;
  2792. ELSE
  2793. returnType := currentResultType;
  2794. END;
  2795. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType) & ReturnedAsParameter(system, returnType) OR ReturnedAsParameter(system,returnType)
  2796. THEN
  2797. x.SetType(returnType);
  2798. ELSE
  2799. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  2800. x.SetType(SyntaxTree.invalidType);
  2801. END;
  2802. ELSE
  2803. Error(x.position,"forbidden access to result designator");
  2804. x.SetType(SyntaxTree.invalidType);
  2805. END;
  2806. x.SetAssignable(TRUE);
  2807. RETURN x;
  2808. END ResolveResultDesignator;
  2809. (**
  2810. return symbol designator as an expression
  2811. - if symbol is a constant then return the constant value expression
  2812. - else
  2813. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  2814. - if symbol is a guarded variable then return a TypeGuardDesignator
  2815. - else return a symbol designator
  2816. **)
  2817. PROCEDURE NewSymbolDesignator*(position: Position; original: SyntaxTree.Designator; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  2818. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  2819. guardType: SyntaxTree.Type;
  2820. BEGIN
  2821. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  2822. result := SyntaxTree.invalidExpression;
  2823. ASSERT(symbol # NIL);
  2824. (*
  2825. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  2826. IF symbol IS SyntaxTree.TypeDeclaration THEN
  2827. Error(position, "type not allowed here");
  2828. ELS *)
  2829. (* not needed any more as values are stored in the expression
  2830. IF symbol IS SyntaxTree.Constant THEN
  2831. result := symbol(SyntaxTree.Constant).value
  2832. IF symbol(SyntaxTree.Constant).value # NIL THEN
  2833. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  2834. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  2835. ELSE
  2836. result := symbol(SyntaxTree.Constant).value
  2837. END;
  2838. ELSE
  2839. *)
  2840. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  2841. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  2842. THEN
  2843. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position,left)); (* auto self *)
  2844. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  2845. left := NewDereferenceDesignator(position,left,left);
  2846. left.SetHidden(TRUE);
  2847. END;
  2848. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  2849. scope := currentScope;
  2850. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  2851. scope := scope.outerScope;
  2852. END;
  2853. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  2854. Error(position, "forbidden access to symbol in parent procedure scope");
  2855. END;
  2856. END;
  2857. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator)
  2858. OR (left IS SyntaxTree.SelfDesignator)
  2859. OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  2860. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  2861. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  2862. ELSE
  2863. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  2864. END;
  2865. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  2866. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType)
  2867. )
  2868. OR (symbol IS SyntaxTree.Alias) & (symbol(SyntaxTree.Alias).expression.assignable)
  2869. ;
  2870. result := SyntaxTree.NewSymbolDesignator(position,original,left,symbol);
  2871. result.SetType(symbol.type);
  2872. result.SetAssignable(assignable);
  2873. symbol.MarkUsed;
  2874. IF symbol IS SyntaxTree.Constant THEN
  2875. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  2876. END;
  2877. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  2878. variableAccessed := TRUE
  2879. END;
  2880. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  2881. IF GetGuard(symbol,guardType) THEN
  2882. IF (symbol.type.resolved IS SyntaxTree.RecordType) OR ~assignable THEN (* type guard is tested and type cannot be changed *)
  2883. result.SetType(guardType);
  2884. ELSE
  2885. result := NewTypeGuardDesignator(position,original, result(SyntaxTree.SymbolDesignator),guardType, result);
  2886. END;
  2887. END;
  2888. END;
  2889. ASSERT(result.type # NIL);
  2890. RETURN result
  2891. END NewSymbolDesignator;
  2892. (** check and resolve an identifier designator "identifier"
  2893. - if identifier = self then return SelfDesignator
  2894. - else find symbol in current scope
  2895. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  2896. **)
  2897. PROCEDURE ResolveIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator): SyntaxTree.Expression;
  2898. VAR symbol: SyntaxTree.Symbol;
  2899. BEGIN
  2900. IF Trace THEN D.Str("ResolveIdentifierDesignator "); D.Ln; END;
  2901. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  2902. IF symbol # NIL THEN
  2903. ResolveSymbol(symbol);
  2904. ASSERT(symbol.type # NIL);
  2905. RETURN NewSymbolDesignator(identifierDesignator.position,identifierDesignator,NIL,symbol);
  2906. ELSE
  2907. Printout.Info("cannot find symbol in scope", currentScope);
  2908. Error(identifierDesignator.position,"Undeclared Identifier");
  2909. IF VerboseErrorMessage THEN
  2910. Printout.Info("undeclared identifier designator",identifierDesignator);
  2911. END;
  2912. RETURN SyntaxTree.invalidDesignator;
  2913. END;
  2914. END ResolveIdentifierDesignator;
  2915. (** check and resolve a selector designator of the form left.designator
  2916. - if left is a pointer type then do auto dereferenciation
  2917. - left denotes a search scope:
  2918. - if left type is import type then set search scope to respective module
  2919. - if left type is enumeration type then set search scope to respective enumeration scope
  2920. - elsif left type is record type then set search scope to record scope
  2921. - search symbol in computed scope
  2922. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  2923. **)
  2924. PROCEDURE ResolveSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator): SyntaxTree.Expression;
  2925. VAR
  2926. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  2927. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  2928. BEGIN
  2929. IF Trace THEN D.Str("ResolveSelectorDesignator"); D.Ln; END;
  2930. left := ResolveDesignator(selectorDesignator.left);
  2931. result := SyntaxTree.invalidDesignator;
  2932. IF left # NIL THEN
  2933. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  2934. left := NewDereferenceDesignator(selectorDesignator.position,left,left);
  2935. END;
  2936. scope := NIL;
  2937. IF left.type = NIL THEN
  2938. Error(selectorDesignator.position,"field on nil typed designator");
  2939. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  2940. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  2941. ELSIF left.type.resolved = SyntaxTree.importType THEN
  2942. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  2943. module := symbol(SyntaxTree.Import).module;
  2944. IF module # NIL THEN
  2945. scope := module.moduleScope
  2946. ELSE
  2947. Error(left.position,"module not loaded");
  2948. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  2949. END;
  2950. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  2951. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  2952. ASSERT(scope # NIL)
  2953. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  2954. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  2955. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  2956. IF type IS SyntaxTree.EnumerationType THEN
  2957. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  2958. ELSE
  2959. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  2960. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  2961. END;
  2962. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  2963. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  2964. ELSE
  2965. Error(selectorDesignator.position,"field on non-record type designator");
  2966. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  2967. END;
  2968. symbol := NIL;
  2969. IF scope # NIL THEN
  2970. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  2971. IF symbol # NIL THEN
  2972. ResolveSymbol(symbol);
  2973. result := NewSymbolDesignator(selectorDesignator.position,selectorDesignator,left,symbol);
  2974. symbol.MarkUsed
  2975. ELSE
  2976. Error(selectorDesignator.position,"undeclared identifier (selector)");
  2977. IF VerboseErrorMessage THEN
  2978. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  2979. Printout.Info("scope", scope);
  2980. Printout.Info("left", left);
  2981. Printout.Info("undeclared identifier",selectorDesignator);
  2982. Printout.Info("left resolved designator",left);
  2983. END
  2984. END;
  2985. END;
  2986. END;
  2987. RETURN result
  2988. END ResolveSelectorDesignator;
  2989. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  2990. VAR len,idx: Basic.Integer;
  2991. BEGIN
  2992. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  2993. IF idx < 0 THEN
  2994. Error(index.position,"index out of bounds (too small)")
  2995. ELSE
  2996. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  2997. Error(index.position,"index out of bounds (too large)");
  2998. END;
  2999. END;
  3000. END;
  3001. END IndexCheck;
  3002. (*
  3003. - if index designator has not type, use newBaseType as its type
  3004. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3005. - special rule: if static array of dynamic array occurs, make it all dynamic
  3006. index designator type: new base type: new index designator type:
  3007. NIL z z
  3008. ARRAY [x, y] z ARRAY [x, y] OF z
  3009. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3010. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3011. *)
  3012. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3013. VAR
  3014. mathArrayType: SyntaxTree.MathArrayType;
  3015. makeDynamic: BOOLEAN;
  3016. BEGIN
  3017. IF indexDesignator.type = NIL THEN
  3018. indexDesignator.SetType(newBaseType)
  3019. ELSE
  3020. (* index designator must be a of math array type *)
  3021. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3022. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3023. (* determine if all arrays have to be made dynamic *)
  3024. makeDynamic :=
  3025. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3026. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3027. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3028. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3029. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3030. END;
  3031. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3032. mathArrayType.SetArrayBase(newBaseType)
  3033. END
  3034. END SetIndexBaseType;
  3035. (** check and append index list element to index designator of math array
  3036. - check validity of single index or array range
  3037. - compute new type
  3038. - if range then create new array type (calculate length of resulting array)
  3039. - otherwise take sourceArray.arrayBase as new type
  3040. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3041. **)
  3042. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3043. VAR
  3044. targetArray: SyntaxTree.MathArrayType;
  3045. first, last, step: SyntaxTree.Expression;
  3046. firstValue, lastValue, stepValue, length: Basic.Integer;
  3047. rangeExpression: SyntaxTree.RangeExpression;
  3048. BEGIN
  3049. IF indexListItem.type = SyntaxTree.invalidType THEN
  3050. (* error already handled *)
  3051. indexDesignator.parameters.AddExpression(indexListItem)
  3052. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3053. indexDesignator.HasRange;
  3054. indexDesignator.HasTensorRange;
  3055. indexDesignator.parameters.AddExpression(indexListItem);
  3056. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3057. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3058. IndexCheck(indexListItem, sourceArray.length);
  3059. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3060. indexDesignator.parameters.AddExpression(indexListItem)
  3061. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3062. indexDesignator.HasRange;
  3063. (* if the range is given as an array range expression, check the validity of its components *)
  3064. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3065. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3066. first := rangeExpression.first;
  3067. last := rangeExpression.last;
  3068. step := rangeExpression.step;
  3069. (* perform static checks on range components *)
  3070. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3071. Error(indexListItem.position,"lower bound of array range too small")
  3072. END;
  3073. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3074. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3075. Error(indexListItem.position,"upper bound of array range too large")
  3076. END
  3077. END;
  3078. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3079. Error(indexListItem.position,"invalid step size")
  3080. END;
  3081. (* add conversions to size type *)
  3082. (* TODO: needed? *)
  3083. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3084. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3085. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3086. END;
  3087. IF indexDesignator.hasTensorRange THEN
  3088. (* the index designator's base type is a tensor: leave it as is *)
  3089. ELSE
  3090. (* append a new math array to the index designator's base type *)
  3091. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3092. IF ~error THEN
  3093. (*
  3094. (* optimization: calculate length of target array for static ranges *)
  3095. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3096. IF IsStaticallyOpenRange(rangeExpression) THEN
  3097. (* range is open ('*'): reuse source array length as target array length *)
  3098. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3099. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3100. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3101. IF lastValue = MAX(LONGINT) THEN
  3102. IF IsIntegerValue(sourceArray.length, length) THEN
  3103. lastValue := length - 1;
  3104. isStaticTargetArrayLength := TRUE
  3105. ELSE
  3106. isStaticTargetArrayLength := FALSE
  3107. END
  3108. ELSE
  3109. isStaticTargetArrayLength := TRUE
  3110. END;
  3111. IF isStaticTargetArrayLength THEN
  3112. (* calculate static target array length *)
  3113. IF firstValue > lastValue THEN
  3114. length := 0
  3115. ELSE
  3116. length := 1 + lastValue - firstValue;
  3117. IF length MOD stepValue = 0 THEN
  3118. length := length DIV stepValue
  3119. ELSE
  3120. length := length DIV stepValue + 1
  3121. END
  3122. END;
  3123. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3124. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3125. ASSERT(targetArray.form = SyntaxTree.Static)
  3126. END
  3127. END
  3128. END
  3129. *)
  3130. END;
  3131. SetIndexBaseType(indexDesignator, targetArray)
  3132. END;
  3133. indexDesignator.parameters.AddExpression(indexListItem)
  3134. ELSE
  3135. Error(position,"invalid index list item");
  3136. END;
  3137. END AppendMathIndex;
  3138. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3139. VAR parameters: SyntaxTree.ExpressionList;
  3140. BEGIN
  3141. parameters := index.parameters;
  3142. IF (expression.type = NIL) THEN
  3143. Error(position, "invalid index");
  3144. ELSIF IsIntegerType(expression.type.resolved) THEN
  3145. IF over IS SyntaxTree.ArrayType THEN
  3146. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3147. ELSIF over IS SyntaxTree.StringType THEN
  3148. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3149. END;
  3150. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3151. parameters.AddExpression(expression);
  3152. ELSE
  3153. Error(position, "invalid index");
  3154. END;
  3155. END AppendIndex;
  3156. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3157. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3158. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3159. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3160. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3161. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3162. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3163. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3164. CONST trace = FALSE;
  3165. BEGIN
  3166. IF trace THEN
  3167. FOR i := 0 TO actualParameters.Length()-1 DO
  3168. Printout.Info("par", actualParameters.GetExpression(i));
  3169. END;
  3170. END;
  3171. operator := scope.firstOperator;
  3172. WHILE(operator # NIL) DO
  3173. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3174. procedureType := operator.type(SyntaxTree.ProcedureType);
  3175. distance := Distance(system, procedureType,actualParameters);
  3176. IF trace THEN Printout.Info("check op ",operator) END;
  3177. IF distance < bestDistance THEN
  3178. IF trace THEN Printout.Info("taken op",operator) END;
  3179. bestDistance := distance;
  3180. bestOperator := operator;
  3181. END;
  3182. END;
  3183. operator := operator.nextOperator;
  3184. END;
  3185. END FindInScope;
  3186. BEGIN
  3187. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3188. IF oper = 0 THEN (* index *)
  3189. identifier := SyntaxTree.NewIdentifier("[]");
  3190. ELSE
  3191. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3192. END;
  3193. WHILE (recordType # NIL) DO
  3194. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3195. recordType := recordType.GetBaseRecord();
  3196. END;
  3197. RETURN bestOperator
  3198. END FindOperator;
  3199. BEGIN
  3200. type := left.type.resolved;
  3201. IF type IS SyntaxTree.RecordType THEN
  3202. pointer := FALSE;
  3203. recordType := type(SyntaxTree.RecordType);
  3204. ELSE
  3205. pointer := TRUE;
  3206. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3207. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3208. END;
  3209. actualParameters := SyntaxTree.NewExpressionList();
  3210. IF parameters # NIL THEN
  3211. FOR i := 0 TO parameters.Length()-1 DO
  3212. expression := ResolveExpression(parameters.GetExpression(i));
  3213. actualParameters.AddExpression(expression);
  3214. END;
  3215. END;
  3216. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3217. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3218. IF op # NIL THEN
  3219. designator := left(SyntaxTree.Designator);
  3220. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator,designator) END;
  3221. expression := NewSymbolDesignator(position, NIL, designator ,op);
  3222. ASSERT(expression IS SyntaxTree.Designator);
  3223. result := NewProcedureCallDesignator(position, NIL, expression(SyntaxTree.Designator), actualParameters);
  3224. (* check if write operator exists, for var parameters *)
  3225. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3226. actualParameters := SyntaxTree.NewExpressionList();
  3227. FOR i := 0 TO parameters.Length()-1 DO
  3228. expression := ResolveExpression(parameters.GetExpression(i));
  3229. actualParameters.AddExpression(expression);
  3230. END;
  3231. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3232. actualParameters.AddExpression(rhs);
  3233. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3234. IF op = NIL THEN rhs := NIL END;
  3235. END;
  3236. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3237. ELSE
  3238. result := NIL;
  3239. END;
  3240. RETURN result;
  3241. END NewObjectOperatorCall;
  3242. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3243. 1. convert bracket designator chains into a single one that contains separators
  3244. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3245. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3246. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3247. - if an array or math array is indexed over, create index designator
  3248. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3249. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3250. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3251. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3252. - if an array-structured object type is indexed over, create procedure call designator
  3253. e.g.: a[x, y] -> a^."[]"(x, y)
  3254. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3255. - a[i, *] = a[i][*]
  3256. - a[*, i] # a[*][i]
  3257. Because:
  3258. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3259. - 'i-th column' = a[*, i]
  3260. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3261. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3262. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3263. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3264. **)
  3265. PROCEDURE ResolveBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator): SyntaxTree.Expression;
  3266. VAR
  3267. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3268. indexDesignator: SyntaxTree.IndexDesignator;
  3269. designator: SyntaxTree.Designator;
  3270. type: SyntaxTree.Type;
  3271. expression: SyntaxTree.Expression;
  3272. i: LONGINT;
  3273. result: SyntaxTree.Expression;
  3274. PROCEDURE FinalizeIndexDesignator;
  3275. BEGIN
  3276. IF indexDesignator # NIL THEN
  3277. (* the end of a tensor has been reached: *)
  3278. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3279. SetIndexBaseType(indexDesignator, type);
  3280. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3281. designator := indexDesignator;
  3282. type := designator.type.resolved;
  3283. indexDesignator := NIL;
  3284. ASSERT(SyntaxTree.Resolved IN type.state)
  3285. END
  3286. END FinalizeIndexDesignator;
  3287. BEGIN
  3288. IF Trace THEN D.Str("ResolveBracketDesignator"); D.Ln; END;
  3289. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3290. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3291. (* copy all index list entries including a separator to the left bracket designator *)
  3292. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3293. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3294. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3295. END;
  3296. (* only resolve left bracket designator and use as final result *)
  3297. bracketDesignator.SetRelatedRhs(leftBracketDesignator.relatedRhs);
  3298. result := ResolveExpression(leftBracketDesignator)
  3299. ELSE
  3300. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3301. designator := ResolveDesignator(bracketDesignator.left);
  3302. type := designator.type.resolved;
  3303. indexDesignator := NIL;
  3304. (*!!! clean up *)
  3305. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  3306. OR (type IS SyntaxTree.RecordType)
  3307. THEN
  3308. result := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3309. IF result = NIL THEN
  3310. Error(bracketDesignator.position,"undefined operator");
  3311. result := SyntaxTree.invalidDesignator
  3312. END;
  3313. RETURN result;
  3314. END;
  3315. i := 0;
  3316. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3317. expression := bracketDesignator.parameters.GetExpression(i);
  3318. expression := ResolveExpression(expression);
  3319. bracketDesignator.parameters.SetExpression(i, expression);
  3320. IF expression = SyntaxTree.indexListSeparator THEN
  3321. (* finalize an existing index designator if needed *)
  3322. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3323. INC(i)
  3324. ELSE
  3325. (* do auto-dereferencing if needed *)
  3326. IF (type IS SyntaxTree.PointerType)
  3327. THEN
  3328. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3329. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3330. Error(expression.position, "forbidden range valued indexer over pointer to array");
  3331. designator := SyntaxTree.invalidDesignator;
  3332. type := SyntaxTree.invalidType
  3333. ELSE
  3334. FinalizeIndexDesignator;
  3335. designator := NewDereferenceDesignator(bracketDesignator.position, designator, designator);
  3336. type := designator.type.resolved
  3337. END
  3338. END;
  3339. (* create a new index designator, if needed *)
  3340. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3341. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, bracketDesignator, designator, SyntaxTree.NewExpressionList());
  3342. indexDesignator.SetAssignable(designator.assignable);
  3343. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3344. (* designator := indexDesignator *)
  3345. END;
  3346. IF type = SyntaxTree.invalidType THEN
  3347. (* error already handled *)
  3348. INC(i)
  3349. ELSIF type IS SyntaxTree.ArrayType THEN
  3350. (* indexing over an array *)
  3351. ASSERT(indexDesignator # NIL);
  3352. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3353. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3354. INC(i)
  3355. ELSIF type IS SyntaxTree.StringType THEN
  3356. (* indexing over an array *)
  3357. ASSERT(indexDesignator # NIL);
  3358. AppendIndex(expression.position, indexDesignator, expression, type);
  3359. type := type(SyntaxTree.StringType).baseType.resolved;
  3360. INC(i)
  3361. ELSIF type IS SyntaxTree.MathArrayType THEN
  3362. (* indexing over a math array *)
  3363. ASSERT(indexDesignator # NIL);
  3364. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  3365. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3366. INC(i)
  3367. ELSE
  3368. Error(expression.position,"indexing over non-array type");
  3369. designator := SyntaxTree.invalidDesignator;
  3370. type := SyntaxTree.invalidType;
  3371. INC(i)
  3372. END
  3373. END
  3374. END;
  3375. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  3376. result := designator
  3377. END;
  3378. RETURN result;
  3379. END ResolveBracketDesignator;
  3380. (** check and resolve expression list
  3381. - resolve each expression in an expression list
  3382. - returns true if and only if all statements could have successfully been resolved
  3383. **)
  3384. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  3385. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  3386. BEGIN
  3387. result := TRUE;
  3388. FOR i := 0 TO expressionList.Length()-1 DO
  3389. expression := ResolveExpression(expressionList.GetExpression(i));
  3390. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  3391. expressionList.SetExpression(i,expression);
  3392. END;
  3393. RETURN result
  3394. END ExpressionList;
  3395. (*
  3396. PROCEDURE ReplaceExpressions(x: SyntaxTree.ExpressionList; oScope, outer, rScope: SyntaxTree.Scope);
  3397. VAR i: LONGINT;
  3398. BEGIN
  3399. IF x # NIL THEN
  3400. FOR i := 0 TO x.Length()-1 DO
  3401. ReplaceExpression(x.GetExpression(i),oScope,outer,rScope);
  3402. END;
  3403. END;
  3404. END ReplaceExpressions;
  3405. PROCEDURE ReplaceExpression(x: SyntaxTree.Expression; oScope, outer, rScope: SyntaxTree.Scope);
  3406. VAR find: SyntaxTree.Symbol; name: ARRAY 32 OF CHAR;
  3407. BEGIN
  3408. IF x = NIL THEN RETURN END;
  3409. WITH
  3410. x: SyntaxTree.ResultDesignator DO ReplaceExpression(x.left,oScope,outer,rScope);
  3411. | SyntaxTree.SelfDesignator DO ReplaceExpression(x.left,oScope,outer,rScope);
  3412. | SyntaxTree.SupercallDesignator DO ReplaceExpression(x.left,oScope,outer,rScope);
  3413. | SyntaxTree.DereferenceDesignator DO ReplaceExpression(x.left,oScope,outer,rScope);
  3414. | SyntaxTree.TypeGuardDesignator DO ReplaceExpression(x.left,oScope,outer,rScope);
  3415. | SyntaxTree.BuiltinCallDesignator DO
  3416. ReplaceExpression(x.left,oScope,outer,rScope);
  3417. ReplaceExpressions(x.parameters,oScope,outer,rScope);
  3418. | SyntaxTree.StatementDesignator DO
  3419. ReplaceStatement(x.statement,oScope,outer,rScope);
  3420. ReplaceExpression(x.result,oScope,outer,rScope);
  3421. | SyntaxTree.ProcedureCallDesignator DO
  3422. ReplaceExpression(x.left,oScope,outer,rScope);
  3423. ReplaceExpressions(x.parameters,oScope,outer,rScope);
  3424. | SyntaxTree.InlineCallDesignator DO
  3425. (* IF x.initializers.Length()>0 THEN
  3426. Printout.Info("oScope", oScope);
  3427. Printout.Info("outer",outer);
  3428. Printout.Info("rScope",rScope);
  3429. END;
  3430. *)
  3431. ReplaceStatement(x.block,oScope,outer,rScope);
  3432. | SyntaxTree.IndexDesignator DO
  3433. ReplaceExpression(x.left,oScope,outer,rScope);
  3434. ReplaceExpressions(x.parameters,oScope,outer,rScope);
  3435. | SyntaxTree.SymbolDesignator DO
  3436. x.symbol.GetName(name);
  3437. TRACE("a symbol designator with ", name);
  3438. IF x.symbol.scope = oScope THEN
  3439. find := rScope.FindSymbol(x.symbol.name);
  3440. IF find # NIL THEN
  3441. x.symbol.GetName(name);
  3442. TRACE(name);
  3443. x.SetSymbol(find);
  3444. ASSERT(x.symbol.scope = rScope);
  3445. END;
  3446. END;
  3447. ReplaceExpression(x.left,oScope,outer,rScope);
  3448. | SyntaxTree.BracketDesignator DO HALT(100)
  3449. | SyntaxTree.ArrowDesignator DO HALT(100)
  3450. | SyntaxTree.ParameterDesignator DO HALT(100)
  3451. | SyntaxTree.SelectorDesignator DO HALT(100)
  3452. | SyntaxTree.IdentifierDesignator DO HALT(100)
  3453. | SyntaxTree.Conversion DO
  3454. ReplaceExpression(x.expression,oScope,outer,rScope);
  3455. | SyntaxTree.TensorRangeExpression DO
  3456. | SyntaxTree.RangeExpression DO
  3457. ReplaceExpression(x.first,oScope,outer,rScope);
  3458. ReplaceExpression(x.last,oScope,outer,rScope);
  3459. ReplaceExpression(x.step,oScope,outer,rScope);
  3460. | SyntaxTree.BinaryExpression DO
  3461. ReplaceExpression(x.left,oScope,outer,rScope);
  3462. ReplaceExpression(x.right,oScope,outer,rScope);
  3463. | SyntaxTree.UnaryExpression DO
  3464. ReplaceExpression(x.left,oScope,outer,rScope);
  3465. | SyntaxTree.MathArrayExpression DO
  3466. ReplaceExpressions(x.elements,oScope,outer,rScope);
  3467. | SyntaxTree.Set DO
  3468. ReplaceExpressions(x.elements,oScope,outer,rScope);
  3469. END;
  3470. END ReplaceExpression;
  3471. PROCEDURE ReplaceIfPart(x: SyntaxTree.IfPart; oScope, outer, rScope: SyntaxTree.Scope);
  3472. BEGIN
  3473. ReplaceExpression(x.condition,oScope,outer,rScope);
  3474. ReplaceStatements(x.statements,oScope,outer,rScope);
  3475. END ReplaceIfPart;
  3476. PROCEDURE ReplaceStatement(x: SyntaxTree.Statement; oScope, outer, rScope: SyntaxTree.Scope);
  3477. VAR i: LONGINT;
  3478. BEGIN
  3479. WITH x:
  3480. SyntaxTree.ProcedureCallStatement DO
  3481. ReplaceExpression(x.call,oScope,outer,rScope);
  3482. | SyntaxTree.Assignment DO
  3483. ReplaceExpression(x.left,oScope,outer,rScope);
  3484. ReplaceExpression(x.right,oScope,outer,rScope);
  3485. | SyntaxTree.CommunicationStatement DO
  3486. ReplaceExpression(x.left,oScope,outer,rScope);
  3487. ReplaceExpression(x.right,oScope,outer,rScope);
  3488. | SyntaxTree.IfStatement DO
  3489. ReplaceIfPart(x.ifPart,oScope,outer,rScope);
  3490. FOR i := 0 TO x.ElsifParts()-1 DO
  3491. ReplaceIfPart(x.GetElsifPart(i),oScope,outer,rScope);
  3492. END;
  3493. IF x.elsePart # NIL THEN
  3494. ReplaceStatements(x.elsePart,oScope,outer,rScope);
  3495. END;
  3496. | SyntaxTree.WithStatement DO
  3497. ReplaceExpression(x.variable,oScope,outer,rScope);
  3498. FOR i := 0 TO x.WithParts()-1 DO
  3499. ReplaceStatements(x.GetWithPart(i).statements,oScope,outer,rScope);
  3500. END;
  3501. IF x.elsePart # NIL THEN
  3502. ReplaceStatements(x.elsePart,oScope,outer,rScope);
  3503. END;
  3504. | SyntaxTree.CaseStatement DO
  3505. ReplaceExpression(x.variable,oScope,outer,rScope);
  3506. FOR i := 0 TO x.CaseParts()-1 DO
  3507. ReplaceStatements(x.GetCasePart(i).statements,oScope,outer,rScope);
  3508. END;
  3509. IF x.elsePart # NIL THEN
  3510. ReplaceStatements(x.elsePart,oScope,outer,rScope);
  3511. END;
  3512. | SyntaxTree.WhileStatement DO
  3513. ReplaceExpression(x.condition,oScope,outer,rScope);
  3514. ReplaceStatements(x.statements,oScope,outer,rScope);
  3515. | SyntaxTree.RepeatStatement DO
  3516. ReplaceExpression(x.condition,oScope,outer,rScope);
  3517. ReplaceStatements(x.statements,oScope,outer,rScope);
  3518. | SyntaxTree.ForStatement DO
  3519. ReplaceExpression(x.variable,oScope,outer,rScope);
  3520. ReplaceExpression(x.from,oScope,outer,rScope);
  3521. ReplaceExpression(x.to,oScope,outer,rScope);
  3522. ReplaceExpression(x.by,oScope,outer,rScope);
  3523. ReplaceStatements(x.statements,oScope,outer,rScope);
  3524. | SyntaxTree.LoopStatement DO
  3525. ReplaceStatements(x.statements,oScope,outer,rScope);
  3526. | SyntaxTree.ExitStatement DO
  3527. | SyntaxTree.ReturnStatement DO
  3528. ReplaceExpression(x.returnValue,oScope,outer,rScope);
  3529. | SyntaxTree.AwaitStatement DO
  3530. ReplaceExpression(x.condition,oScope,outer,rScope);
  3531. | SyntaxTree.StatementBlock DO
  3532. x.scope.SetOuter(outer);
  3533. Printout.Info("outer",outer);
  3534. Printout.Info("x.scope",x.scope);
  3535. ReplaceStatements(x.statements,oScope,x.scope,rScope);
  3536. END;
  3537. END ReplaceStatement;
  3538. PROCEDURE ReplaceStatements(statements: SyntaxTree.StatementSequence; oScope,outer,rScope: SyntaxTree.Scope);
  3539. VAR i: LONGINT;
  3540. BEGIN
  3541. FOR i := 0 TO statements.Length()-1 DO
  3542. ReplaceStatement(statements.GetStatement(i),oScope,outer,rScope);
  3543. END;
  3544. END ReplaceStatements;
  3545. *)
  3546. (** return procedure call designator left(actualParameters)
  3547. - check realtime procedure call in realtime procedure
  3548. - check number of parameters
  3549. - check parameter compatibility
  3550. return invalidDesignator if error
  3551. **)
  3552. PROCEDURE NewProcedureCallDesignator(position: Position; original: SyntaxTree.Expression; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  3553. VAR result: SyntaxTree.Designator;
  3554. numberFormalParameters, numberActualParameters: LONGINT;
  3555. formalType: SyntaxTree.ProcedureType;
  3556. formalParameter: SyntaxTree.Parameter;
  3557. actualParameter: SyntaxTree.Expression;
  3558. i: LONGINT;
  3559. self: SyntaxTree.Expression;
  3560. inlineScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType;
  3561. tooComplex : BOOLEAN;
  3562. duplicate: BOOLEAN;
  3563. procedure: SyntaxTree.Procedure;
  3564. block: SyntaxTree.StatementBlock;
  3565. returnType: SyntaxTree.Type;
  3566. const, cclone: SyntaxTree.Constant;
  3567. var, clone: SyntaxTree.Variable;
  3568. alias: SyntaxTree.Alias;
  3569. name: ARRAY 32 OF CHAR;
  3570. resolved: SyntaxTree.Statement;
  3571. previousResultType: SyntaxTree.Type;
  3572. symbols: SyntaxTree.Symbols;
  3573. BEGIN
  3574. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  3575. result := SyntaxTree.invalidDesignator;
  3576. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  3577. numberFormalParameters := formalType.numberParameters;
  3578. numberActualParameters := actualParameters.Length();
  3579. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  3580. Error(position, "forbidden call of non-realtime procedure in realtime block");
  3581. END;
  3582. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  3583. self := left.left;
  3584. IF (self # NIL) & ~IsVariable(self) THEN
  3585. Error(self.position, "Non-variable expression on variable receiver");
  3586. END;
  3587. END;
  3588. IF ~ExpressionList(actualParameters) THEN
  3589. result := SyntaxTree.invalidDesignator
  3590. ELSE
  3591. IF numberActualParameters <= numberFormalParameters THEN
  3592. formalParameter := formalType.firstParameter;
  3593. FOR i := 0 TO numberActualParameters-1 DO
  3594. actualParameter := actualParameters.GetExpression(i);
  3595. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  3596. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  3597. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  3598. Error(position, "non-realtime actual parameter in context of realtime procedure");
  3599. ELSE
  3600. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  3601. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  3602. END;
  3603. actualParameters.SetExpression(i,actualParameter);
  3604. END;
  3605. formalParameter := formalParameter.nextParameter;
  3606. END;
  3607. WHILE (formalParameter # NIL) DO
  3608. IF formalParameter.defaultValue # NIL THEN
  3609. actualParameters.AddExpression(formalParameter.defaultValue);
  3610. formalParameter := formalParameter.nextParameter
  3611. ELSE
  3612. Error(position, "less actual than formal parameters");
  3613. formalParameter := NIL;
  3614. END;
  3615. END;
  3616. ELSE
  3617. Error(position, "more actual than formal parameters")
  3618. END;
  3619. result := SyntaxTree.NewProcedureCallDesignator(position,original,left,actualParameters);
  3620. result.SetAssignable(FALSE);
  3621. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  3622. END;
  3623. IF result = SyntaxTree.invalidDesignator THEN
  3624. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  3625. procedure := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  3626. END;
  3627. IF InliningSupport & (procedure # NIL) & IsOberonInline(procedure) THEN
  3628. procedure.GetName(name);
  3629. inlineScope := SyntaxTree.NewBlockScope(currentScope);
  3630. currentScope := inlineScope;
  3631. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3632. formalParameter := procedureType.firstParameter;
  3633. (* we need a copy of the original parsed expression list here -- may contain inline calls *)
  3634. actualParameters := SyntaxTree.CloneExpressionList(result(SyntaxTree.ProcedureCallDesignator).parameters);
  3635. NEW(symbols);
  3636. (*ASSERT(ExpressionList(actualParameters));*)
  3637. tooComplex := FALSE;
  3638. i := 0;
  3639. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  3640. actualParameter := ResolveExpression(actualParameters.GetExpression(i));
  3641. formalParameter.GetName(name);
  3642. (*
  3643. Printout.Info("formal",formalParameter);
  3644. Printout.Info("actual",actualParameter);
  3645. *)
  3646. IF actualParameter.resolved # NIL THEN
  3647. actualParameter := actualParameter.resolved
  3648. END;
  3649. IF (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter})
  3650. (* & (actualParameter.resolved # NIL) & (actualParameter.resolved IS SyntaxTree.Value) *)
  3651. & SideEffectFree(actualParameter) THEN
  3652. IF (actualParameter.resolved # NIL) & (actualParameter.resolved IS SyntaxTree.Value) THEN
  3653. const := SyntaxTree.NewConstant(actualParameter.position,formalParameter.name);
  3654. const.SetValue(actualParameter);
  3655. const.SetType(actualParameter.type);
  3656. inlineScope.AddConstant(const);
  3657. symbols.Add(const);
  3658. ELSE
  3659. alias := SyntaxTree.NewAlias(actualParameter.position, formalParameter.name, actualParameter);
  3660. alias.SetType(actualParameter.type);
  3661. inlineScope.AddAlias(alias);
  3662. symbols.Add(alias);
  3663. END;
  3664. ELSE
  3665. var := SyntaxTree.NewVariable(actualParameter.position, formalParameter.name);
  3666. var.SetKind(formalParameter.kind);
  3667. var.PreferRegister(formalParameter.preferRegister);
  3668. (* copy expression to var *)
  3669. var.SetType(actualParameter.type);
  3670. var.SetInitializer(actualParameter);
  3671. (*
  3672. NEW(init, var, actualParameter);
  3673. inits.AddInitializer(init);
  3674. *)
  3675. inlineScope.AddVariable(var);
  3676. symbols.Add(var);
  3677. END;
  3678. formalParameter := formalParameter.nextParameter;
  3679. INC(i);
  3680. END;
  3681. (* in order to avoid name clashes with parameter names, we do this late *)
  3682. (* remark: setting currentScope := inlineScope here does not work because
  3683. parameters may themselves be inline expressions *)
  3684. FOR i := 0 TO symbols.Length()-1 DO
  3685. inlineScope.EnterSymbol(symbols.Get(i), duplicate);
  3686. END;
  3687. var := procedure.procedureScope.firstVariable;
  3688. WHILE (var # NIL) DO
  3689. clone := var.Clone();
  3690. inlineScope.AddVariable(clone);
  3691. inlineScope.EnterSymbol(clone, duplicate);
  3692. var := var.nextVariable;
  3693. END;
  3694. const := procedure.procedureScope.firstConstant;
  3695. WHILE (const # NIL) DO
  3696. cclone := const.Clone();
  3697. inlineScope.AddConstant(cclone);
  3698. inlineScope.EnterSymbol(cclone, duplicate);
  3699. const := const.nextConstant;
  3700. END;
  3701. (*IF ~tooComplex & (procedureType.returnType # NIL) THEN
  3702. IF resultDesignator # NIL THEN
  3703. returnDesignator := resultDesignator
  3704. ELSE
  3705. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  3706. END;
  3707. currentMapper.Add(NIL, returnDesignator, NIL, resultDesignator # NIL);
  3708. END;
  3709. *)
  3710. (*
  3711. rning(result.position,"call to inline proc","");
  3712. *)
  3713. (* do this after passing passing variables in order to prevent false positives for calls like f(f(f()));
  3714. *)
  3715. IF procedure.Resolving(TRUE) THEN
  3716. Warning(position,"inline recursion");
  3717. RETURN result; (* normal procedure call *)
  3718. END;
  3719. previousResultType := currentResultType;
  3720. currentResultType := procedureType.returnType;
  3721. ASSERT(system.GenerateVariableOffsets(inlineScope));
  3722. block := SyntaxTree.NewStatementBlock(result.position, NIL, inlineScope);
  3723. (* ResolveSymbol(procedure);*)
  3724. (* Printout.Info("before replacement", procedure.procedureScope.body.statements); *)
  3725. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  3726. (*Parameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);*)
  3727. resolved := ResolveStatement(block);
  3728. (*
  3729. ReplaceStatement(block, procedure.procedureScope, currentScope,block.scope);
  3730. *)
  3731. (* Printout.Info("after replacement", block.statements); *)
  3732. returnType := result.type;
  3733. result := SyntaxTree.NewInlineCallDesignator(result.position, original, block);
  3734. result.SetType(returnType);
  3735. currentScope := inlineScope.outerScope;
  3736. currentScope.AddScope(inlineScope);
  3737. currentResultType := previousResultType;
  3738. IF ~procedure.Resolving(FALSE) THEN (*HALT(100) *) END;
  3739. END;
  3740. RETURN result
  3741. END NewProcedureCallDesignator;
  3742. (**
  3743. builtin call designator generated in ResolveParameterDesignator
  3744. -> nothing to be resolved
  3745. **)
  3746. PROCEDURE ResolveBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator): SyntaxTree.Expression;
  3747. BEGIN
  3748. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  3749. RETURN NewBuiltinCallDesignator(x.position,NIL, NIL, x.parameters,NIL, ResolveType(x.returnType));
  3750. ELSIF ExpressionList(x.parameters) THEN
  3751. RETURN x;
  3752. END;
  3753. END ResolveBuiltinCallDesignator;
  3754. (** return true if x is a variable else return false and report error **)
  3755. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  3756. VAR result: BOOLEAN;
  3757. BEGIN
  3758. result := TRUE;
  3759. IF x = SyntaxTree.invalidExpression THEN
  3760. result := FALSE;
  3761. ELSIF ~IsVariable(x) THEN
  3762. Error(x.position,"non variable expression");
  3763. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  3764. result := FALSE;
  3765. END;
  3766. RETURN result
  3767. END CheckVariable;
  3768. (**
  3769. if expression x is of basic type then return true else report error and return false
  3770. **)
  3771. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  3772. VAR result: BOOLEAN;
  3773. BEGIN
  3774. result := FALSE;
  3775. IF x = SyntaxTree.invalidExpression THEN
  3776. ELSIF ~IsBasicType(x.type) THEN
  3777. Error(x.position,"is no basic type");
  3778. result := FALSE
  3779. ELSE result := TRUE
  3780. END;
  3781. RETURN result
  3782. END CheckBasicType;
  3783. (**
  3784. if expression x is of number type then return true else report error and return false
  3785. **)
  3786. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  3787. VAR result: BOOLEAN;
  3788. BEGIN
  3789. result := FALSE;
  3790. IF x = SyntaxTree.invalidExpression THEN
  3791. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  3792. Error(x.position,"is non number type");
  3793. ELSE result := TRUE
  3794. END;
  3795. RETURN result
  3796. END CheckNumberType;
  3797. (**
  3798. if expression x is of number or size type but not complex then return true else report error and return false
  3799. **)
  3800. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  3801. VAR result: BOOLEAN;
  3802. BEGIN
  3803. result := FALSE;
  3804. IF x = SyntaxTree.invalidExpression THEN
  3805. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  3806. Error(x.position,"is complex type");
  3807. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  3808. Error(x.position,"is non number type");
  3809. ELSE result := TRUE
  3810. END;
  3811. RETURN result
  3812. END CheckNonComplexNumberSizeType;
  3813. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  3814. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3815. BEGIN
  3816. result := FALSE; type := x.type.resolved;
  3817. IF x = SyntaxTree.invalidExpression THEN
  3818. 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
  3819. Error(x.position,"is no address type");
  3820. ELSE result := TRUE
  3821. END;
  3822. RETURN result
  3823. END CheckAddressType;
  3824. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  3825. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3826. BEGIN
  3827. result := FALSE; type := x.type.resolved;
  3828. IF x = SyntaxTree.invalidExpression THEN
  3829. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  3830. Error(x.position,"is no size type");
  3831. ELSE result := TRUE
  3832. END;
  3833. RETURN result
  3834. END CheckSizeType;
  3835. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  3836. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3837. BEGIN
  3838. result := FALSE; type := x.type.resolved;
  3839. IF x = SyntaxTree.invalidExpression THEN
  3840. 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
  3841. Error(x.position,"is no object type");
  3842. ELSE result := TRUE
  3843. END;
  3844. RETURN result
  3845. END CheckObjectType;
  3846. (**
  3847. if expression x is of integer type then return true else report error and return false
  3848. **)
  3849. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  3850. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3851. BEGIN
  3852. result := FALSE; type := x.type.resolved;
  3853. IF x = SyntaxTree.invalidExpression THEN
  3854. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  3855. Error(x.position,"is no integer type");
  3856. ELSE result := TRUE
  3857. END;
  3858. RETURN result
  3859. END CheckIntegerType;
  3860. (**
  3861. if expression x is of character type then return true else report error and return false
  3862. **)
  3863. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  3864. VAR result: BOOLEAN;
  3865. BEGIN
  3866. result := FALSE;
  3867. IF x = SyntaxTree.invalidExpression THEN
  3868. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  3869. Error(x.position,"is no character type");
  3870. ELSE result := TRUE
  3871. END;
  3872. RETURN result
  3873. END CheckCharacterType;
  3874. (**
  3875. if expression x is of real type then return true else report error and return false
  3876. **)
  3877. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  3878. VAR result: BOOLEAN;
  3879. BEGIN
  3880. result := FALSE;
  3881. IF x = SyntaxTree.invalidExpression THEN
  3882. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  3883. Error(x.position,"is no float type");
  3884. ELSE result := TRUE
  3885. END;
  3886. RETURN result
  3887. END CheckRealType;
  3888. (**
  3889. if expression x is of range type then return true else report error and return false
  3890. **)
  3891. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  3892. VAR result: BOOLEAN;
  3893. BEGIN
  3894. result := FALSE;
  3895. IF x = SyntaxTree.invalidExpression THEN
  3896. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  3897. Error(x.position,"is no range type");
  3898. ELSE result := TRUE
  3899. END;
  3900. RETURN result
  3901. END CheckRangeType;
  3902. (**
  3903. if expression x is of boolean type then return true else report error and return false
  3904. **)
  3905. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  3906. VAR result: BOOLEAN;
  3907. BEGIN
  3908. result := FALSE;
  3909. IF x = SyntaxTree.invalidExpression THEN
  3910. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  3911. Error(x.position,"is no boolean type");
  3912. ELSE result := TRUE
  3913. END;
  3914. RETURN result
  3915. END CheckBooleanType;
  3916. (**
  3917. if expression x is of set type then return true else report error and return false
  3918. **)
  3919. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  3920. VAR result: BOOLEAN;
  3921. BEGIN
  3922. result := FALSE;
  3923. IF x = SyntaxTree.invalidExpression THEN
  3924. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  3925. Error(x.position,"is no set type");
  3926. ELSE result := TRUE
  3927. END;
  3928. RETURN result
  3929. END CheckSetType;
  3930. (**
  3931. if expression x is of string or array of character type then return true else report error and return false
  3932. **)
  3933. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  3934. VAR result: BOOLEAN;
  3935. BEGIN
  3936. result := FALSE;
  3937. IF x = SyntaxTree.invalidExpression THEN
  3938. ELSIF ~IsStringType(x.type.resolved) THEN
  3939. Error(x.position,"is no string type");
  3940. ELSE result := TRUE
  3941. END;
  3942. RETURN result
  3943. END CheckStringType;
  3944. (**
  3945. if expression x is a type declaration type return true else report error and return false
  3946. **)
  3947. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  3948. VAR result: BOOLEAN;
  3949. BEGIN
  3950. result := FALSE;
  3951. IF x = SyntaxTree.invalidExpression THEN
  3952. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  3953. Error(x.position,"is not a type declaration");
  3954. ELSE result := TRUE
  3955. END;
  3956. RETURN result
  3957. END CheckTypeDeclarationType;
  3958. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  3959. VAR result: BOOLEAN;
  3960. BEGIN
  3961. result := FALSE;
  3962. IF x = SyntaxTree.invalidExpression THEN
  3963. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3964. result := TRUE;
  3965. value := x.resolved(SyntaxTree.IntegerValue).value;
  3966. ELSE
  3967. Error(x.position,"expression is not an integer constant");
  3968. END;
  3969. RETURN result;
  3970. END CheckIntegerValue;
  3971. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  3972. VAR result: BOOLEAN;
  3973. BEGIN
  3974. result := FALSE;
  3975. IF x = SyntaxTree.invalidExpression THEN
  3976. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  3977. result := TRUE;
  3978. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  3979. ELSE
  3980. Error(x.position,"expression is not an integer constant");
  3981. END;
  3982. RETURN result;
  3983. END CheckStringValue;
  3984. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  3985. BEGIN
  3986. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3987. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  3988. ELSE
  3989. RETURN FALSE
  3990. END;
  3991. END IsUnsignedValue;
  3992. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  3993. BEGIN
  3994. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3995. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  3996. ELSE
  3997. RETURN FALSE
  3998. END
  3999. END IsAddressValue;
  4000. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4001. BEGIN
  4002. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4003. END IsAddressExpression;
  4004. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4005. BEGIN
  4006. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4007. END IsSizeExpression;
  4008. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4009. VAR result: BOOLEAN;
  4010. BEGIN
  4011. result := FALSE;
  4012. IF x = SyntaxTree.invalidExpression THEN
  4013. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4014. result := TRUE;
  4015. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4016. ELSE
  4017. Error(x.position,"expression is not an integer constant");
  4018. END;
  4019. RETURN result;
  4020. END CheckEnumerationValue;
  4021. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4022. VAR result: BOOLEAN;
  4023. BEGIN
  4024. result := FALSE;
  4025. IF x = SyntaxTree.invalidExpression THEN
  4026. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4027. result := TRUE;
  4028. value := x.resolved(SyntaxTree.CharacterValue).value;
  4029. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4030. result := TRUE;
  4031. value := x.resolved(SyntaxTree.StringValue).value[0];
  4032. ELSE
  4033. Error(x.position,"expression is not a character constant");
  4034. END;
  4035. RETURN result;
  4036. END CheckCharacterValue;
  4037. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4038. VAR result: BOOLEAN;
  4039. BEGIN
  4040. result := FALSE;
  4041. IF x = SyntaxTree.invalidExpression THEN
  4042. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4043. value := x.resolved(SyntaxTree.IntegerValue).value;
  4044. IF (value > 0) OR includeZero & (value = 0) THEN
  4045. result := TRUE;
  4046. ELSE
  4047. Error(x.position,"integer is not positive");
  4048. END
  4049. ELSE
  4050. Error(x.position,"expression is not an integer constant");
  4051. END;
  4052. RETURN result;
  4053. END CheckPositiveIntegerValue;
  4054. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4055. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4056. BEGIN
  4057. result := FALSE;
  4058. IF x = SyntaxTree.invalidExpression THEN
  4059. ELSE
  4060. type := x.type.resolved;
  4061. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4062. portType := type(SyntaxTree.PortType);
  4063. result := TRUE
  4064. ELSE
  4065. Error(x.position,"no port type");
  4066. END;
  4067. END;
  4068. RETURN result
  4069. END CheckPortType;
  4070. (* move to builtin procedure call statement ?
  4071. remove builtin procedure call designator ?
  4072. *)
  4073. PROCEDURE NewBuiltinCallDesignator(position: Position; original: SyntaxTree.Designator; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4074. VAR
  4075. numberActualParameters,numberFormalParameters: LONGINT;
  4076. formalParameter: SyntaxTree.Parameter;
  4077. actualParameter: SyntaxTree.Expression;
  4078. procedureType: SyntaxTree.ProcedureType;
  4079. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4080. inPort, outPort: SyntaxTree.PortType;
  4081. constructor: SyntaxTree.Procedure;
  4082. type0,type1,type2: SyntaxTree.Type;
  4083. type,base,parameterType: SyntaxTree.Type;
  4084. arrayType: SyntaxTree.ArrayType;
  4085. i: LONGINT; i0, i1, value: Basic.Integer;
  4086. r,r0,r1,im: LONGREAL;
  4087. c: CHAR;
  4088. id: LONGINT;
  4089. b: BOOLEAN;
  4090. first: LONGINT;
  4091. mathArrayType: SyntaxTree.MathArrayType;
  4092. customBuiltin: SyntaxTree.CustomBuiltin;
  4093. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4094. VAR resultB: BOOLEAN;
  4095. BEGIN
  4096. IF numberActualParameters < from THEN
  4097. Error(position, "less actual than formal parameters");
  4098. result := SyntaxTree.invalidExpression;
  4099. resultB := FALSE;
  4100. ELSIF numberActualParameters > to THEN
  4101. Error(position, "more actual than formal parameters");
  4102. result := SyntaxTree.invalidExpression;
  4103. resultB := FALSE;
  4104. ELSE
  4105. resultB := TRUE;
  4106. END;
  4107. RETURN resultB
  4108. END CheckArity;
  4109. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4110. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4111. BEGIN
  4112. WHILE modifier # NIL DO
  4113. symbol := cellType.FindProperty(modifier.identifier);
  4114. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4115. propertyType := symbol.type.resolved;
  4116. modifierType := modifier.expression.type.resolved;
  4117. IF ~CompatibleTo(system, modifierType, propertyType) &
  4118. ~(
  4119. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4120. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4121. Error(modifier.position,"incompatible to cell property");
  4122. END;
  4123. ELSE
  4124. Error(modifier.position, "undefined property");
  4125. END;
  4126. modifier := modifier.nextModifier;
  4127. END;
  4128. END CheckModifiers;
  4129. BEGIN
  4130. type := NIL; result := NIL;
  4131. type0 := NIL; type1 := NIL; type2 := NIL;
  4132. numberActualParameters := actualParameters.Length();
  4133. IF numberActualParameters>0 THEN
  4134. parameter0 := actualParameters.GetExpression(0);
  4135. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4136. Error(parameter0.position,"forbidden type-less argument");
  4137. result := SyntaxTree.invalidExpression
  4138. END
  4139. END;
  4140. IF numberActualParameters >1 THEN
  4141. parameter1 := actualParameters.GetExpression(1);
  4142. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4143. ELSE
  4144. Error(parameter1.position,"forbidden type-less argument");
  4145. result := SyntaxTree.invalidExpression
  4146. END
  4147. END;
  4148. IF numberActualParameters >2 THEN
  4149. parameter2 := actualParameters.GetExpression(2);
  4150. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4151. ELSE
  4152. Error(parameter2.position,"forbidden type-less argument");
  4153. result := SyntaxTree.invalidExpression
  4154. END
  4155. END;
  4156. IF returnType # NIL THEN
  4157. id := Global.New;
  4158. result := NIL;
  4159. ELSE
  4160. id := builtin.id;
  4161. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4162. result := NewOperatorCall(position,original, builtin.id,parameter0,parameter1,NIL);
  4163. END;
  4164. END;
  4165. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4166. ELSIF result # NIL THEN type := result.type (* operator *)
  4167. ELSE
  4168. result := SyntaxTree.NewBuiltinCallDesignator(position,original, id,left,actualParameters);
  4169. result(SyntaxTree.Designator).SetLeft(left);
  4170. IF returnType # NIL THEN
  4171. type := returnType;
  4172. END;
  4173. (* ---- ASSERT ----- *)
  4174. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4175. IF CheckBooleanType(parameter0) THEN
  4176. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4177. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4178. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4179. *)
  4180. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4181. Error(position, "static assertion failed");
  4182. END;
  4183. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4184. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4185. rules imposed by the architecture / current runtime
  4186. *)
  4187. END;
  4188. END;
  4189. (* ---- COPY ----- *)
  4190. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4191. IF~IsStringType(type0) THEN
  4192. Error(parameter0.position,"no string type");
  4193. END;
  4194. IF ~IsStringType(type1) THEN
  4195. Error(parameter1.position,"no string type");
  4196. ELSIF CheckVariable(parameter1) THEN
  4197. IF (type0 IS SyntaxTree.StringType) THEN
  4198. arrayType := type1(SyntaxTree.ArrayType);
  4199. IF arrayType.form = SyntaxTree.Static THEN
  4200. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4201. Error(position, "destination length smaller than source length")
  4202. END;
  4203. END;
  4204. END;
  4205. END;
  4206. (* ---- INC, DEC----- *)
  4207. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4208. IF numberActualParameters = 1 THEN
  4209. parameter1 :=Global.NewIntegerValue(system,position,1);
  4210. actualParameters.AddExpression(parameter1);
  4211. END;
  4212. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4213. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4214. Error(position, "incompatible increment");
  4215. ELSE
  4216. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4217. actualParameters.SetExpression(1,parameter1);
  4218. END;
  4219. END;
  4220. (* ---- EXCL, INCL----- *)
  4221. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4222. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4223. IF IsIntegerValue(parameter1,i0) THEN
  4224. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4225. Error(position, "parameter out of SET range")
  4226. END;
  4227. END;
  4228. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4229. actualParameters.SetExpression(1,parameter1);
  4230. END;
  4231. (* ---- HALT, SYSTEM.HALT ----- *)
  4232. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4233. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4234. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4235. rules imposed by the architecture / current runtime
  4236. *)
  4237. END;
  4238. (* ---- WAIT ----- *)
  4239. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4240. IF CheckObjectType(parameter0) THEN
  4241. END;
  4242. (* ---- NEW ----- *)
  4243. ELSIF (id = Global.New) THEN
  4244. IF returnType # NIL THEN
  4245. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4246. ELSE
  4247. first := 1;
  4248. END;
  4249. IF CheckArity(first,Infinity) THEN
  4250. IF currentIsRealtime THEN
  4251. Error(position, "forbidden new in realtime block");
  4252. END;
  4253. IF IsUnsafePointer(type0) THEN
  4254. Error(position, "forbidden new on unsafe pointer");
  4255. END;
  4256. (* check constructor *)
  4257. IF (first =0) OR CheckVariable(parameter0) THEN
  4258. IF type0 IS SyntaxTree.PointerType THEN
  4259. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4260. ELSIF type0 IS SyntaxTree.CellType THEN
  4261. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4262. ELSE
  4263. Error(position, "forbidden new on value type");
  4264. END;
  4265. IF type0 IS SyntaxTree.ArrayType THEN
  4266. arrayType := type0(SyntaxTree.ArrayType);
  4267. IF arrayType.form = SyntaxTree.Static THEN
  4268. i := first
  4269. ELSIF arrayType.form = SyntaxTree.Open THEN
  4270. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4271. ELSE HALT(100)
  4272. END;
  4273. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4274. i := first;
  4275. REPEAT
  4276. actualParameter := actualParameters.GetExpression(i);
  4277. IF CheckSizeType(actualParameter) THEN
  4278. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4279. actualParameters.SetExpression(i,actualParameter);
  4280. END;
  4281. INC(i);
  4282. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4283. END;
  4284. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4285. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4286. Error(position, "forbidden new on abstract object");
  4287. END;
  4288. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4289. IF constructor = NIL THEN
  4290. IF CheckArity(first,first) THEN END;
  4291. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4292. Error(position, "new on object with hidden constructor");
  4293. ELSE
  4294. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4295. numberFormalParameters := procedureType.numberParameters;
  4296. IF numberActualParameters-first <= numberFormalParameters THEN
  4297. formalParameter := procedureType.firstParameter;
  4298. FOR i := first TO numberActualParameters-1 DO
  4299. actualParameter := actualParameters.GetExpression(i);
  4300. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4301. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4302. ELSE
  4303. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4304. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4305. END;
  4306. actualParameters.SetExpression(i,actualParameter);
  4307. END;
  4308. formalParameter := formalParameter.nextParameter;
  4309. END;
  4310. WHILE (formalParameter # NIL) DO
  4311. IF formalParameter.defaultValue # NIL THEN
  4312. actualParameters.AddExpression(formalParameter.defaultValue);
  4313. formalParameter := formalParameter.nextParameter
  4314. ELSE
  4315. Error(position, "less actual than formal parameters");
  4316. formalParameter := NIL;
  4317. END;
  4318. END;
  4319. ELSE
  4320. Error(position, "more actual than formal parameters")
  4321. END;
  4322. END;
  4323. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4324. mathArrayType := type0(SyntaxTree.MathArrayType);
  4325. IF mathArrayType.form = SyntaxTree.Static THEN
  4326. Error(position, "new on static array");
  4327. ELSE
  4328. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4329. i0 := first+1; i1 := Infinity;
  4330. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4331. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4332. i1 := i0;
  4333. ELSE HALT(100);
  4334. END;
  4335. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4336. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4337. base := ArrayBase(type0,MAX(LONGINT));
  4338. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4339. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4340. IF ~CompatibleTo(system,type0,parameterType) THEN
  4341. Error(parameter0.position,"incompatible parameter in new");
  4342. result := SyntaxTree.invalidExpression;
  4343. ELSE
  4344. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4345. END;
  4346. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4347. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4348. IF ~CompatibleTo(system,type1,parameterType) THEN
  4349. Error(parameter1.position,"parameter incompatible to math array len type");
  4350. result := SyntaxTree.invalidExpression;
  4351. ELSE
  4352. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4353. END;
  4354. ELSE
  4355. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4356. i := first;
  4357. REPEAT
  4358. actualParameter := actualParameters.GetExpression(i);
  4359. IF CheckSizeType(actualParameter) THEN
  4360. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4361. actualParameters.SetExpression(i,actualParameter);
  4362. END;
  4363. INC(i);
  4364. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4365. END;
  4366. END;
  4367. END;
  4368. ELSIF type0 IS SyntaxTree.CellType THEN
  4369. IF ~(currentIsCellNet) THEN
  4370. Error(position, "cell allocation outside activeCells ");
  4371. ELSE
  4372. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4373. IF (constructor = NIL) & CheckArity(1,1) THEN
  4374. (* ok *)
  4375. ELSE
  4376. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4377. numberFormalParameters := procedureType.numberParameters;
  4378. DEC(numberActualParameters);
  4379. IF numberActualParameters <= numberFormalParameters THEN
  4380. formalParameter := procedureType.firstParameter;
  4381. FOR i := first TO numberActualParameters DO
  4382. actualParameter := actualParameters.GetExpression(i);
  4383. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4384. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4385. ELSE
  4386. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4387. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4388. END;
  4389. actualParameters.SetExpression(i,actualParameter);
  4390. END;
  4391. formalParameter := formalParameter.nextParameter;
  4392. END;
  4393. WHILE (formalParameter # NIL) DO
  4394. IF formalParameter.defaultValue # NIL THEN
  4395. actualParameters.AddExpression(formalParameter.defaultValue);
  4396. formalParameter := formalParameter.nextParameter
  4397. ELSE
  4398. Error(position, "less actual than formal parameters");
  4399. formalParameter := NIL;
  4400. END;
  4401. END;
  4402. ELSE
  4403. Error(position, "more actual than formal parameters")
  4404. END;
  4405. END;
  4406. END;
  4407. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4408. activeCellsStatement := TRUE;
  4409. ELSE
  4410. Error(position, "cannot be allocated");
  4411. END;
  4412. END;
  4413. END;
  4414. (* ---- DISPOSE ----- *)
  4415. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4416. IF ~IsPointerType(parameter0.type) THEN
  4417. Error(parameter0.position,"is not a pointer")
  4418. ELSIF ~IsDisposable(parameter0.type) THEN
  4419. Error(parameter0.position,"is not disposable")
  4420. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4421. END
  4422. (* ---- GETPROCEDURE ----- *)
  4423. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4424. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4425. IF CheckVariable(parameter2) THEN
  4426. IF ~GetProcedureAllowed(parameter2.type) THEN
  4427. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4428. END;
  4429. END;
  4430. END;
  4431. (* ---- ABS ----- *)
  4432. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4433. (* note: ABS on complex numbers is done using overloading *)
  4434. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4435. type := type0;
  4436. IF IsIntegerValue(parameter0,i0) THEN
  4437. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4438. type := Global.GetIntegerType(system,ABS(i0));
  4439. ELSIF IsRealValue(parameter0,r) THEN
  4440. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r),Scanner.None));
  4441. END;
  4442. ELSE
  4443. type := SyntaxTree.invalidType;
  4444. END;
  4445. (* ---- ASH, ASR ----- *)
  4446. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4447. type := type0;
  4448. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4449. (*
  4450. ConvertOperands(parameter0,parameter1); (* same type *)
  4451. *)
  4452. type := parameter0.type;
  4453. IF IsIntegerValue(parameter0,i0) THEN
  4454. IF IsIntegerValue(parameter1,i1) THEN
  4455. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4456. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4457. result := ResolveExpression(result);
  4458. type := Global.GetIntegerType(system,i0);
  4459. END;
  4460. END;
  4461. IF type.resolved.sizeInBits < 32 THEN
  4462. type := system.longintType;
  4463. END;
  4464. (*!compatibility with release, remove when resolved
  4465. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4466. *)
  4467. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4468. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4469. actualParameters.SetExpression(0,parameter0);
  4470. actualParameters.SetExpression(1,parameter1);
  4471. END;
  4472. (* ---- CAP ----- *)
  4473. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4474. type := system.characterType;
  4475. IF CheckCharacterType (parameter0) THEN
  4476. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4477. actualParameters.SetExpression(0,parameter0);
  4478. IF IsCharacterValue(parameter0,c) THEN
  4479. IF (c <= "z") & (c >= "a") THEN
  4480. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4481. ELSE
  4482. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4483. END;
  4484. END;
  4485. END;
  4486. (* ---- CHR ----- *)
  4487. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4488. IF id = Global.Chr THEN
  4489. type := system.characterType
  4490. ELSE
  4491. type := system.characterType32
  4492. END;
  4493. IF CheckIntegerType(parameter0) THEN
  4494. IF IsIntegerValue(parameter0,i0) THEN
  4495. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4496. result := ResolveExpression(result);
  4497. ELSE
  4498. (*
  4499. result := NewConversion(parameter0.position,parameter0,type);
  4500. *)
  4501. END;
  4502. END
  4503. (* ---- ENTIER ----- *)
  4504. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4505. type := system.longintType;
  4506. IF CheckRealType(parameter0) THEN
  4507. IF IsRealValue(parameter0,r) THEN
  4508. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4509. type := Global.GetIntegerType(system,ENTIER(r));
  4510. END
  4511. END;
  4512. (* ---- ENTIERH ----- *)
  4513. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4514. type := system.hugeintType;
  4515. IF CheckRealType(parameter0) THEN
  4516. IF IsRealValue(parameter0,r) THEN
  4517. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4518. END
  4519. END;
  4520. (* ---- LEN ----- *)
  4521. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4522. type := system.lenType;
  4523. base := type0;
  4524. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4525. IF base(SyntaxTree.PointerType).isUnsafe THEN
  4526. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  4527. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  4528. Error(position, "forbidden len on unsafe pointer");
  4529. END;
  4530. type0 := base;
  4531. ELSE
  4532. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator),parameter0(SyntaxTree.Designator));
  4533. type0 := parameter0.type.resolved;
  4534. actualParameters.SetExpression(0,parameter0);
  4535. base := type0;
  4536. END;
  4537. END;
  4538. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4539. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4540. IF i1 < 0 THEN
  4541. Error(position, "invalid dimension");
  4542. base := SyntaxTree.invalidType;
  4543. ELSE
  4544. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  4545. IF (base # NIL) & Indexable(base) THEN
  4546. ELSE
  4547. Error(position, "len on no array");
  4548. IF VerboseErrorMessage THEN
  4549. Printout.Info("base",base);
  4550. END;
  4551. base := SyntaxTree.invalidType;
  4552. END;
  4553. END;
  4554. IF numberActualParameters=2 THEN
  4555. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  4556. actualParameters.SetExpression(1,parameter1);
  4557. ELSIF base IS SyntaxTree.MathArrayType THEN
  4558. Error(position, "missing dimension specification");
  4559. END;
  4560. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4561. IF base IS SyntaxTree.ArrayType THEN
  4562. arrayType := base(SyntaxTree.ArrayType);
  4563. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  4564. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4565. result := Global.NewIntegerValue(system,position,value);
  4566. type := result.type;(* arrayType.length.type;*)
  4567. ASSERT(type # NIL);
  4568. END;
  4569. ELSIF base IS SyntaxTree.MathArrayType THEN
  4570. mathArrayType := base(SyntaxTree.MathArrayType);
  4571. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  4572. result := Global.NewIntegerValue(system,position,value);
  4573. type := result.type;
  4574. (*
  4575. type := mathArrayType.length.type;
  4576. *)
  4577. ASSERT(type # NIL);
  4578. END;
  4579. END;
  4580. END;
  4581. ELSE
  4582. type := system.lenType;
  4583. END;
  4584. (* ---- FIRST ---- *)
  4585. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4586. type := system.lenType;
  4587. IF CheckRangeType(parameter0) THEN END;
  4588. result.SetAssignable(parameter0.assignable)
  4589. (* ---- LAST ---- *)
  4590. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4591. type := system.lenType;
  4592. IF CheckRangeType(parameter0) THEN END;
  4593. result.SetAssignable(parameter0.assignable)
  4594. (* ---- STEP ---- *)
  4595. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4596. type := system.lenType;
  4597. IF CheckRangeType(parameter0) THEN END;
  4598. result.SetAssignable(parameter0.assignable)
  4599. (* ---- RE ---- *)
  4600. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4601. IF CheckNumberType(parameter0) THEN
  4602. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4603. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4604. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r, Scanner.None)) END
  4605. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  4606. type := parameter0.type
  4607. ELSE
  4608. type := system.realType
  4609. END
  4610. END;
  4611. result.SetAssignable(parameter0.assignable)
  4612. (* ---- IM ---- *)
  4613. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  4614. IF CheckNumberType(parameter0) THEN
  4615. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4616. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4617. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im, Scanner.None)) END
  4618. ELSE
  4619. type := system.realType;
  4620. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0, Scanner.None))
  4621. END
  4622. END;
  4623. result.SetAssignable(parameter0.assignable)
  4624. (* ---- MAX ----- *)
  4625. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  4626. IF numberActualParameters = 1 THEN
  4627. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4628. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4629. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  4630. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  4631. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  4632. *)
  4633. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  4634. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType)), Scanner.None));
  4635. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  4636. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  4637. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  4638. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  4639. END;
  4640. ELSE
  4641. Error(parameter0.position,"is not a type symbol");
  4642. END
  4643. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  4644. ConvertOperands(parameter0,parameter1);
  4645. actualParameters.SetExpression(0,parameter0);
  4646. actualParameters.SetExpression(1,parameter1);
  4647. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  4648. IF r0 > r1 THEN result.SetResolved(parameter0.resolved)
  4649. ELSE result.SetResolved(parameter0.resolved)
  4650. END;
  4651. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4652. IF i0 > i1 THEN result.SetResolved(parameter0.resolved)
  4653. ELSE result.SetResolved(parameter1.resolved)
  4654. END;
  4655. END;
  4656. type := parameter0.type;
  4657. ELSE type := SyntaxTree.invalidType;
  4658. END;
  4659. (* ---- MIN ----- *)
  4660. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  4661. IF numberActualParameters = 1 THEN
  4662. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4663. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4664. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  4665. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  4666. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType)), Scanner.None));
  4667. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  4668. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  4669. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  4670. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  4671. END;
  4672. ELSE
  4673. Error(parameter0.position,"is not a type symbol");
  4674. END
  4675. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  4676. ConvertOperands(parameter0,parameter1);
  4677. actualParameters.SetExpression(0,parameter0);
  4678. actualParameters.SetExpression(1,parameter1);
  4679. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  4680. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  4681. ELSE result.SetResolved(parameter1.resolved)
  4682. END;
  4683. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4684. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  4685. ELSE result.SetResolved(parameter1.resolved)
  4686. END;
  4687. END;
  4688. type := parameter0.type;
  4689. ELSE type := SyntaxTree.invalidType;
  4690. END;
  4691. (* ---- ODD ----- *)
  4692. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  4693. type := system.booleanType;
  4694. IF CheckIntegerType(parameter0) THEN
  4695. IF IsIntegerValue(parameter0,i0) THEN
  4696. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  4697. type := system.booleanType;
  4698. END;
  4699. END;
  4700. (* ---- ORD ----- *)
  4701. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  4702. IF id = Global.Ord THEN
  4703. type := system.integerType;
  4704. ELSE
  4705. type := system.longintType;
  4706. END;
  4707. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  4708. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  4709. actualParameters.SetExpression(0,parameter0);
  4710. (* IF CheckCharacterType(parameter0) THEN*)
  4711. IF IsCharacterValue(parameter0,c)THEN
  4712. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  4713. type := Global.GetSignedIntegerType(system,ORD(c));
  4714. END;
  4715. ELSE Error(parameter0.position, "incompatible parameter");
  4716. END;
  4717. (* ---- SHORT ----- *)
  4718. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  4719. type := type0;
  4720. IF IsSignedIntegerType(type) THEN
  4721. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  4722. ELSIF type = system.integerType THEN type := system.shortintType
  4723. ELSIF type = system.longintType THEN type := system.integerType
  4724. ELSIF type = system.hugeintType THEN type:= system.longintType
  4725. ELSE
  4726. CASE type.sizeInBits OF
  4727. 16: type := Global.Integer8
  4728. |32: type := Global.Integer16
  4729. |64: type := Global.Integer32
  4730. END;
  4731. END;
  4732. ELSIF IsUnsignedIntegerType(type) THEN
  4733. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  4734. ELSE
  4735. CASE type.sizeInBits OF
  4736. 16: type := Global.Unsigned8
  4737. |32: type := Global.Unsigned16
  4738. |64: type := Global.Unsigned32
  4739. END;
  4740. END;
  4741. ELSIF type IS SyntaxTree.FloatType THEN
  4742. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  4743. ELSIF type = system.longrealType THEN type := system.realType
  4744. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  4745. END;
  4746. ELSIF type IS SyntaxTree.ComplexType THEN
  4747. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  4748. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  4749. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  4750. END;
  4751. ELSE
  4752. Error(parameter0.position,"short not applicable")
  4753. END;
  4754. IF (parameter0.resolved # NIL) THEN
  4755. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  4756. IF parameter0 IS SyntaxTree.Value THEN
  4757. result.SetResolved(parameter0(SyntaxTree.Value));
  4758. END;
  4759. END;
  4760. (* ---- LONG ----- *)
  4761. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  4762. type := type0;
  4763. IF IsSignedIntegerType(type) THEN
  4764. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  4765. ELSIF type = system.longintType THEN type := system.hugeintType
  4766. ELSIF type = system.integerType THEN type := system.longintType
  4767. ELSIF type = system.shortintType THEN type := system.integerType
  4768. ELSE
  4769. CASE type.sizeInBits OF
  4770. 8: type := Global.Integer16
  4771. |16: type := Global.Integer32
  4772. |32: type := Global.Integer64
  4773. END;
  4774. END;
  4775. ELSIF IsUnsignedIntegerType(type) THEN
  4776. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  4777. ELSE
  4778. CASE type.sizeInBits OF
  4779. 8: type := Global.Unsigned16
  4780. |16: type := Global.Unsigned32
  4781. |32: type := Global.Unsigned64
  4782. END;
  4783. END;
  4784. ELSIF type IS SyntaxTree.FloatType THEN
  4785. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  4786. ELSIF type= system.realType THEN type := system.longrealType
  4787. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  4788. END;
  4789. ELSIF type IS SyntaxTree.ComplexType THEN
  4790. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  4791. ELSIF type = system.complexType THEN type := system.longcomplexType
  4792. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  4793. END;
  4794. ELSE
  4795. Error(parameter0.position,"long not applicable")
  4796. END;
  4797. IF (parameter0.resolved # NIL) THEN
  4798. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  4799. IF parameter0 IS SyntaxTree.Value THEN
  4800. result.SetResolved(parameter0(SyntaxTree.Value));
  4801. END;
  4802. END;
  4803. (* ---- SIZE OF ----- *)
  4804. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  4805. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  4806. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  4807. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  4808. type := system.integerType;
  4809. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  4810. ELSE
  4811. (* for variables, system sizeof could represent the physically occupied size
  4812. determined via the type descriptor, implement that ? *)
  4813. Error(parameter0.position,"is not a type symbol");
  4814. END
  4815. (* ---- SYSTEM.TRACE -----*)
  4816. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  4817. FOR i := 0 TO numberActualParameters-1 DO
  4818. parameter0 := actualParameters.GetExpression(i);
  4819. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  4820. Error(parameter0.position,"incompatible parameter");
  4821. END;
  4822. END;
  4823. (* remaining issues can only be tested in backend *)
  4824. (* ---- ADDRESSOF----- *)
  4825. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  4826. IF HasAddress(parameter0) THEN
  4827. type := system.addressType;
  4828. ELSE
  4829. type := SyntaxTree.invalidType;
  4830. Error(parameter0.position,"has no address");
  4831. END;
  4832. (* ---- BIT ----- *)
  4833. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  4834. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  4835. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4836. actualParameters.SetExpression(0,parameter0);
  4837. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  4838. actualParameters.SetExpression(1,parameter1);
  4839. END;
  4840. type := system.booleanType;
  4841. (* ----- MSK ---- *)
  4842. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  4843. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4844. ConvertOperands(parameter0,parameter1);
  4845. actualParameters.SetExpression(0,parameter0);
  4846. actualParameters.SetExpression(1,parameter1);
  4847. END;
  4848. type := parameter0.type;
  4849. (* ---- SYSTEM.GET64 ----- *)
  4850. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  4851. IF CheckAddressType(parameter0) THEN
  4852. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4853. actualParameters.SetExpression(0,parameter0);
  4854. END;
  4855. type := system.hugeintType;
  4856. (* ---- SYSTEM.GET32 ----- *)
  4857. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  4858. IF CheckAddressType(parameter0) THEN
  4859. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4860. actualParameters.SetExpression(0,parameter0);
  4861. END;
  4862. type := system.longintType;
  4863. (* ---- SYSTEM.GET16 ----- *)
  4864. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  4865. IF CheckAddressType(parameter0) THEN
  4866. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4867. actualParameters.SetExpression(0,parameter0);
  4868. END;
  4869. type := system.integerType;
  4870. (* ---- SYSTEM.GET8 ----- *)
  4871. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  4872. IF CheckAddressType(parameter0) THEN
  4873. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4874. actualParameters.SetExpression(0,parameter0);
  4875. END;
  4876. type := system.shortintType;
  4877. (* ---- SYSTEM.GetStackPointer ----- *)
  4878. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  4879. type := system.addressType;
  4880. (* ---- SYSTEM.GetFramePointer ----- *)
  4881. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  4882. type := system.addressType;
  4883. (* ---- SYSTEM.GetActivity ----- *)
  4884. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  4885. type := system.objectType;
  4886. (* ---- SYSTEM.SetStackPointer ----- *)
  4887. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  4888. IF CheckAddressType(parameter0) THEN
  4889. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4890. actualParameters.SetExpression(0,parameter0);
  4891. END;
  4892. (* ---- SYSTEM.SetFramePointer ----- *)
  4893. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  4894. IF CheckAddressType(parameter0) THEN
  4895. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4896. actualParameters.SetExpression(0,parameter0);
  4897. END;
  4898. (* ---- SYSTEM.SetActivity ----- *)
  4899. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  4900. IF CheckObjectType(parameter0) THEN
  4901. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4902. actualParameters.SetExpression(0,parameter0);
  4903. END;
  4904. (* ---- LSH, LSL, ROT, ROR ----- *)
  4905. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  4906. type := type0;
  4907. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4908. actualParameters.SetExpression(1, parameter1);
  4909. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4910. IF id = Global.Lsh THEN
  4911. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  4912. ELSIF id = Global.Rot THEN
  4913. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  4914. ELSIF id = Global.Ror THEN
  4915. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  4916. END;
  4917. END;
  4918. (* ---- SYSTEM.VAL ----- *)
  4919. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  4920. IF CheckTypeDeclarationType(parameter0) THEN
  4921. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  4922. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  4923. result := SyntaxTree.invalidExpression;
  4924. Error(parameter0.position,"is no basic type");
  4925. ELSE
  4926. IF (parameter1.resolved # NIL) THEN
  4927. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  4928. IF parameter0 IS SyntaxTree.Value THEN
  4929. result.SetResolved(parameter0(SyntaxTree.Value));
  4930. END;
  4931. END;
  4932. result.SetAssignable(parameter1.assignable);
  4933. END;
  4934. END;
  4935. (* ---- SYSTEM.GET ----- *)
  4936. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  4937. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  4938. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4939. actualParameters.SetExpression(0,parameter0);
  4940. END;
  4941. (* ---- SYSTEM.PUT ----- *)
  4942. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  4943. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4944. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4945. actualParameters.SetExpression(0,parameter0);
  4946. END;
  4947. (* ---- SYSTEM.PUT64 ----- *)
  4948. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  4949. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4950. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4951. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  4952. actualParameters.SetExpression(0,parameter0);
  4953. actualParameters.SetExpression(1,parameter1);
  4954. END;
  4955. (* ---- SYSTEM.PUT32 ----- *)
  4956. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  4957. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4958. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4959. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4960. actualParameters.SetExpression(0,parameter0);
  4961. actualParameters.SetExpression(1,parameter1);
  4962. END;
  4963. (* ---- SYSTEM.PUT16 ----- *)
  4964. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  4965. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4966. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4967. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  4968. actualParameters.SetExpression(0,parameter0);
  4969. actualParameters.SetExpression(1,parameter1);
  4970. END;
  4971. (* ---- SYSTEM.PUT8 ----- *)
  4972. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  4973. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4974. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4975. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  4976. actualParameters.SetExpression(0,parameter0);
  4977. actualParameters.SetExpression(1,parameter1);
  4978. END;
  4979. (* ---- SYSTEM.MOVE ----- *)
  4980. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  4981. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  4982. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4983. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  4984. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  4985. actualParameters.SetExpression(0,parameter0);
  4986. actualParameters.SetExpression(1,parameter1);
  4987. actualParameters.SetExpression(2,parameter2);
  4988. END;
  4989. (* ---- SYSTEM.NEW ----- *)
  4990. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  4991. IF ~IsPointerType(parameter0.type) THEN
  4992. Error(parameter0.position,"is not a pointer")
  4993. ELSIF CheckSizeType(parameter1) THEN
  4994. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  4995. actualParameters.SetExpression(1,parameter1);
  4996. END;
  4997. (* ----SYSTEM.REF ---- *)
  4998. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  4999. type := system.addressType
  5000. (* ---- INCR ----- *)
  5001. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5002. type := system.lenType;
  5003. base := type0;
  5004. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5005. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5006. IF i1 < 0 THEN
  5007. Error(position, "invalid dimension");
  5008. base := SyntaxTree.invalidType;
  5009. ELSE
  5010. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5011. IF (base # NIL) & Indexable(base) THEN
  5012. ELSE
  5013. Error(position, "len on no array");
  5014. IF VerboseErrorMessage THEN
  5015. Printout.Info("base",base);
  5016. END;
  5017. base := SyntaxTree.invalidType;
  5018. END;
  5019. END;
  5020. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5021. actualParameters.SetExpression(1,parameter1);
  5022. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5023. mathArrayType := base(SyntaxTree.MathArrayType);
  5024. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5025. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5026. type := system.lenType;
  5027. END;
  5028. END;
  5029. ELSE
  5030. type := system.lenType;
  5031. END;
  5032. (* ---- SUM ----- *)
  5033. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5034. Error(position, "sum operator not applicable");
  5035. (* ---- ALL ----- *)
  5036. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5037. Error(position, "all operator not applicable");
  5038. (* ---- DIM ----- *)
  5039. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5040. type := system.lenType;
  5041. IF type0 IS SyntaxTree.MathArrayType THEN
  5042. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5043. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5044. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5045. END;
  5046. ELSE
  5047. Error(position, "dimension on non math array type");
  5048. END;
  5049. (* ---- CAS ----- *)
  5050. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5051. IF type0.IsComposite () THEN
  5052. Error(position, "first parameter of composite type");
  5053. result := SyntaxTree.invalidExpression;
  5054. ELSIF ~IsVariable (parameter0) THEN
  5055. Error(position, "first parameter not assignable");
  5056. result := SyntaxTree.invalidExpression;
  5057. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5058. Error(position, "second parameter incompatible");
  5059. result := SyntaxTree.invalidExpression;
  5060. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5061. Error(position, "third parameter incompatible");
  5062. result := SyntaxTree.invalidExpression;
  5063. ELSE
  5064. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5065. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5066. type := type0;
  5067. END;
  5068. (* ---- RESHAPE ----- *)
  5069. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5070. IF type0 IS SyntaxTree.MathArrayType THEN
  5071. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5072. base := ArrayBase(type0,MAX(LONGINT));
  5073. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5074. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5075. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5076. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5077. IF ~CompatibleTo(system,type0,parameterType) THEN
  5078. Error(parameter0.position,"incompatible parameter in reshape");
  5079. result := SyntaxTree.invalidExpression;
  5080. ELSE
  5081. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5082. END;
  5083. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5084. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5085. IF ~CompatibleTo(system,type1,parameterType) THEN
  5086. Error(parameter1.position,"parameter incompatible to math array of longint");
  5087. result := SyntaxTree.invalidExpression;
  5088. ELSE
  5089. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5090. END;
  5091. ELSE
  5092. Error(position,"reshape on non math array type");
  5093. result := SyntaxTree.invalidExpression;
  5094. END;
  5095. (* ---- SYSTEM.TYPECODE ----- *)
  5096. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5097. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5098. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5099. type := type.resolved;
  5100. IF type IS SyntaxTree.PointerType THEN
  5101. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5102. END;
  5103. IF ~(type IS SyntaxTree.RecordType) THEN
  5104. Error(parameter0.position,"must be type with type descriptor");
  5105. END;
  5106. ELSE
  5107. Error(parameter0.position,"is not a type symbol");
  5108. END;
  5109. type := system.addressType;
  5110. (* -------- FLT --------- *)
  5111. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5112. type := system.realType;
  5113. IF IsRealValue(parameter0, r) THEN
  5114. result.SetResolved(SyntaxTree.NewRealValue(position, r, Scanner.None));
  5115. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5116. value := ABS(i0);
  5117. IF value # 0 THEN
  5118. i1 := 23;
  5119. IF value >= 2*800000H THEN
  5120. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5121. ELSIF value < 800000H THEN
  5122. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5123. END;
  5124. value := (i1 + 127)*800000H - 800000H + value;
  5125. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5126. END;
  5127. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value), Scanner.None));
  5128. END;
  5129. (* ------- CONNECT -------*)
  5130. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5131. (*IF ~(currentIsCellNet) THEN
  5132. Error(position, "connection outside activeCells body block");
  5133. END;*)
  5134. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5135. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5136. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5137. END;
  5138. IF numberActualParameters = 3 THEN
  5139. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5140. Error(position, "incompatible channel size parameter");
  5141. END;
  5142. *)
  5143. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5144. actualParameters.SetExpression(2,parameter2);
  5145. END;
  5146. activeCellsStatement := TRUE;
  5147. (* ---------- DELEGATE --------*)
  5148. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5149. (*
  5150. IF ~(currentIsCellNet) THEN
  5151. Error(position, "connection delegation outside activeCells body block");
  5152. END;
  5153. *)
  5154. IF ~CheckPortType(parameter1, inPort) THEN
  5155. Error(parameter0.position,"not a port")
  5156. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5157. Error(parameter1.position,"not a port")
  5158. ELSIF (outPort.direction # inPort.direction) THEN
  5159. Error(parameter0.position,"invalid port direction");
  5160. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5161. Error(position, "incompatible port sizes");
  5162. END;
  5163. activeCellsStatement := TRUE;
  5164. (* --------- RECEIVE ---------*)
  5165. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5166. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5167. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5168. IF inPort.direction # SyntaxTree.InPort THEN
  5169. Error(parameter0.position,"not an in-port")
  5170. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5171. Error(parameter1.position,"incompatible to port type");
  5172. END;
  5173. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5174. IF ~SameType(parameter2.type, system.integerType) THEN
  5175. Error(parameter2.position,"incompatible to integer type");
  5176. END;
  5177. END;
  5178. END;
  5179. (* --------- SEND ---------*)
  5180. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5181. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5182. IF CheckPortType(parameter0,outPort) THEN
  5183. IF outPort.direction # SyntaxTree.OutPort THEN
  5184. Error(parameter1.position,"not an out-port")
  5185. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5186. Error(parameter1.position,"incompatible to port type");
  5187. ELSE
  5188. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5189. actualParameters.SetExpression(1,parameter1);
  5190. END;
  5191. END;
  5192. (* ------- custom builtins ----- *)
  5193. ELSIF id = Global.systemSpecial THEN
  5194. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5195. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5196. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5197. type := procedureType.returnType;
  5198. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5199. (* go through all formal parameters *)
  5200. formalParameter := procedureType.firstParameter;
  5201. FOR i := 0 TO actualParameters.Length() - 1 DO
  5202. actualParameter := actualParameters.GetExpression(i);
  5203. IF actualParameter = SyntaxTree.invalidExpression THEN
  5204. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5205. Error(position, "incompatible parameter")
  5206. ELSE
  5207. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5208. END;
  5209. actualParameters.SetExpression(i, actualParameter);
  5210. formalParameter := formalParameter.nextParameter
  5211. END
  5212. END
  5213. ELSE
  5214. Error(position, "builtin not implemented");
  5215. result := SyntaxTree.invalidExpression;
  5216. END;
  5217. END;
  5218. IF result # SyntaxTree.invalidExpression THEN
  5219. type := ResolveType(type);
  5220. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5221. result.SetType(type);
  5222. END;
  5223. RETURN result
  5224. END NewBuiltinCallDesignator;
  5225. (** return type guard designator left(type)
  5226. - check if type can be extended (i.e. is no static record)
  5227. - check if type is a type extension of left.type
  5228. - returns new type guard designator
  5229. returns invalidDesignator = invalidExpression if error
  5230. **)
  5231. PROCEDURE NewTypeGuardDesignator(position: Position; original: SyntaxTree.Designator; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5232. VAR result: SyntaxTree.Designator;
  5233. BEGIN
  5234. result := SyntaxTree.invalidDesignator;
  5235. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5236. Error(position, "no type extension of type");
  5237. IF VerboseErrorMessage THEN
  5238. Printout.Info("left",left);
  5239. Printout.Info("type",type);
  5240. END;
  5241. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5242. Error(position, "variable cannot be extended");
  5243. ELSIF IsUnsafePointer(left.type) THEN
  5244. Error(position, "forbidden type guard on unsafe pointer");
  5245. ELSE
  5246. result := SyntaxTree.NewTypeGuardDesignator(position,original,left,type);
  5247. result.SetType(type);
  5248. result.SetAssignable(left.assignable);
  5249. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5250. END;
  5251. RETURN result
  5252. END NewTypeGuardDesignator;
  5253. (** check and resolve parameter designator left(expression list)
  5254. - check expression list
  5255. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5256. - elsif left is a procedure type then
  5257. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5258. - else return is a procedure call then return ProcedureCallDesignator
  5259. returns invalidDesignator = invalidExpression if error
  5260. **)
  5261. PROCEDURE ResolveParameterDesignator(designator: SyntaxTree.ParameterDesignator): SyntaxTree.Expression;
  5262. VAR
  5263. parameters: SyntaxTree.ExpressionList;
  5264. left: SyntaxTree.Designator;
  5265. result,expression: SyntaxTree.Expression;
  5266. typeDeclaration: SyntaxTree.TypeDeclaration;
  5267. type, expressionType: SyntaxTree.Type;
  5268. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5269. BEGIN
  5270. type := type.resolved;
  5271. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5272. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5273. END;
  5274. RETURN type
  5275. END BaseType;
  5276. BEGIN
  5277. IF Trace THEN D.Str("ResolveParameterDesignator"); D.Ln; END;
  5278. result := SyntaxTree.invalidDesignator;
  5279. left := ResolveDesignator(designator.left);
  5280. IF left # SyntaxTree.invalidDesignator THEN
  5281. parameters := designator.parameters;
  5282. IF ExpressionList(parameters) THEN
  5283. IF (left.type = NIL) THEN
  5284. Error(left.position,"object is not a procedure or cannot be extended");
  5285. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5286. result := NewTypeGuardDesignator(designator.position,designator,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5287. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5288. result := NewTypeGuardDesignator(designator.position,designator,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5289. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5290. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5291. result := NewBuiltinCallDesignator(designator.position,designator,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5292. ELSE
  5293. result := NewProcedureCallDesignator(designator.position,designator,left,parameters)
  5294. END
  5295. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5296. expression := parameters.GetExpression(0);
  5297. type := typeDeclaration.declaredType.resolved;
  5298. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5299. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5300. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5301. OR (expressionType IS SyntaxTree.EnumerationType)
  5302. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5303. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5304. Error(left.position,"invalid unsigned type in explicit conversion");
  5305. ELSE
  5306. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5307. END;
  5308. ELSE
  5309. Error(left.position,"invalid type in explicit conversion");
  5310. END;
  5311. ELSE
  5312. Error(left.position,"called object is not a procedure or cannot be extended");
  5313. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5314. result := SyntaxTree.invalidDesignator;
  5315. END;
  5316. ELSE
  5317. result := SyntaxTree.invalidDesignator
  5318. END;
  5319. END;
  5320. RETURN result;
  5321. END ResolveParameterDesignator;
  5322. (** check dereference designator left^
  5323. - check if left is pointer type or left is object type
  5324. - return new dereference designator with type = left.baseType.type (if appropriate)
  5325. with error handling
  5326. returns invalidDesignator = invalidExpression if error
  5327. **)
  5328. PROCEDURE NewDereferenceDesignator(position: Position; original: SyntaxTree.Designator; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5329. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5330. BEGIN
  5331. result := SyntaxTree.invalidDesignator;
  5332. type := left.type;
  5333. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5334. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5335. result := SyntaxTree.NewDereferenceDesignator(position,original,left);
  5336. result.SetAssignable(TRUE);
  5337. result.SetType(type);
  5338. result.SetHidden(left.isHidden);
  5339. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5340. type := type.resolved;
  5341. result := SyntaxTree.NewDereferenceDesignator(position,original,left);
  5342. result.SetAssignable(TRUE);
  5343. result.SetType(type);
  5344. result.SetHidden(left.isHidden);
  5345. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5346. result := SyntaxTree.NewDereferenceDesignator(position,original,left);
  5347. result.SetAssignable(TRUE);
  5348. result.SetType(type);
  5349. result.SetHidden(left.isHidden);
  5350. ELSE
  5351. Error(position, "dereference on no pointer");
  5352. IF VerboseErrorMessage THEN
  5353. Printout.Info("pointer", type);
  5354. Printout.Info("scope", currentScope);
  5355. END;
  5356. END;
  5357. RETURN result
  5358. END NewDereferenceDesignator;
  5359. (** check supercall designator left^
  5360. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5361. - return new supercall designator with type = left.type
  5362. with error handling
  5363. **)
  5364. PROCEDURE NewSupercallDesignator(position: Position;original: SyntaxTree.Designator; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5365. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5366. objectScope: SyntaxTree.Scope;
  5367. BEGIN
  5368. result := SyntaxTree.invalidDesignator;
  5369. IF left = SyntaxTree.invalidDesignator THEN
  5370. (* error already handled *)
  5371. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5372. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5373. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5374. IF symbol IS SyntaxTree.Procedure THEN
  5375. procedure := symbol(SyntaxTree.Procedure);
  5376. objectScope := currentScope;
  5377. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5378. objectScope := objectScope.outerScope;
  5379. END;
  5380. IF (left.left = NIL) OR ~
  5381. (
  5382. (left.left IS SyntaxTree.SelfDesignator) OR
  5383. (left.left IS SyntaxTree.DereferenceDesignator)
  5384. & (left.left(SyntaxTree.Designator).left # NIL)
  5385. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5386. Error(position, "procedure not in immediate object scope");
  5387. IF VerboseErrorMessage THEN
  5388. Printout.Info("left.left",left.left);
  5389. END;
  5390. ELSIF procedure.super # NIL THEN
  5391. result := SyntaxTree.NewSupercallDesignator(position,original,left);
  5392. result.SetType(left.type.resolved)
  5393. ELSE
  5394. Error(position, "no supermethod for this procedure");
  5395. END;
  5396. ELSE
  5397. Error(position, "symbol is not a procedure");
  5398. END;
  5399. ELSE
  5400. Error(position, "is no symbol designator");
  5401. END;
  5402. RETURN result
  5403. END NewSupercallDesignator;
  5404. (** check and semantically resolve arrow designator left^
  5405. - if left is procedure type -> result := SupercallDesignator
  5406. - else result := DereferenceDesignator
  5407. returns result via global variable resolvedExpression
  5408. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5409. **)
  5410. PROCEDURE ResolveArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator): SyntaxTree.Expression;
  5411. VAR left: SyntaxTree.Designator;
  5412. BEGIN
  5413. IF Trace THEN D.Str("ResolveArrowDesignator"); D.Ln; END;
  5414. left := ResolveDesignator(arrowDesignator.left);
  5415. IF left # NIL THEN
  5416. IF (left.type = NIL) THEN
  5417. Error(arrowDesignator.position,"Invalid arrow designator");
  5418. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5419. RETURN NewSupercallDesignator(arrowDesignator.position,arrowDesignator,left);
  5420. ELSE
  5421. IF IsPointerToObject(left.type) THEN
  5422. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5423. END;
  5424. RETURN NewDereferenceDesignator(arrowDesignator.position,arrowDesignator,left)
  5425. END
  5426. END;
  5427. RETURN arrowDesignator;
  5428. END ResolveArrowDesignator;
  5429. (** check and return expression
  5430. - if expression has no type then resolve expression
  5431. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5432. - return result
  5433. **)
  5434. PROCEDURE ResolveExpression*(x: SyntaxTree.Expression): SyntaxTree.Expression;
  5435. VAR result: SyntaxTree.Expression; subtype: LONGINT; type: SyntaxTree.Type; pos: WORD;
  5436. BEGIN
  5437. IF (x = NIL) OR (x = SyntaxTree.invalidExpression) OR (x.type # NIL) THEN RETURN x END;
  5438. pos := x.position.start;
  5439. IF ~(x IS SyntaxTree.BuiltinCallDesignator) THEN
  5440. x.SetType(SyntaxTree.invalidType);
  5441. END;
  5442. result := x;
  5443. WITH x:
  5444. | SyntaxTree.ResultDesignator DO result := ResolveResultDesignator(x)
  5445. | SyntaxTree.SelfDesignator DO result := ResolveSelfDesignator(x)
  5446. | SyntaxTree.TypeGuardDesignator DO result := x;
  5447. | SyntaxTree.SymbolDesignator DO result := x;
  5448. | SyntaxTree.BuiltinCallDesignator DO result := ResolveBuiltinCallDesignator(x)
  5449. | SyntaxTree.ProcedureCallDesignator DO
  5450. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  5451. | SyntaxTree.BracketDesignator DO result := ResolveBracketDesignator(x)
  5452. | SyntaxTree.ArrowDesignator DO result := ResolveArrowDesignator(x)
  5453. | SyntaxTree.ParameterDesignator DO result := ResolveParameterDesignator(x)
  5454. | SyntaxTree.SelectorDesignator DO result := ResolveSelectorDesignator(x)
  5455. | SyntaxTree.IdentifierDesignator DO result := ResolveIdentifierDesignator(x)
  5456. | SyntaxTree.TensorRangeExpression DO x.SetType(NIL);
  5457. | SyntaxTree.RangeExpression DO result := ResolveRangeExpression(x)
  5458. | SyntaxTree.BinaryExpression DO result := ResolveBinaryExpression(x)
  5459. | SyntaxTree.UnaryExpression DO result := ResolveUnaryExpression(x)
  5460. | SyntaxTree.MathArrayExpression DO result := ResolveMathArrayExpression(x)
  5461. | SyntaxTree.Set DO result := ResolveSet(x)
  5462. | SyntaxTree.BooleanValue DO x.SetType(system.booleanType);
  5463. | SyntaxTree.IntegerValue DO x.SetType(Global.GetIntegerType(system,x.value));
  5464. | SyntaxTree.CharacterValue DO x.SetType(system.characterType);
  5465. | SyntaxTree.SetValue DO
  5466. x.SetType(system.setType);
  5467. | SyntaxTree.MathArrayValue DO
  5468. x.SetType(SyntaxTree.invalidType)
  5469. | SyntaxTree.RealValue DO
  5470. subtype := x.subtype;
  5471. IF subtype = Scanner.Real THEN
  5472. type := system.realType
  5473. ELSIF subtype = Scanner.Longreal THEN
  5474. type := system.longrealType
  5475. ELSE
  5476. HALT(100)
  5477. END;
  5478. x.SetType(type);
  5479. | SyntaxTree.ComplexValue DO
  5480. subtype := x.subtype;
  5481. IF subtype = Scanner.Real THEN
  5482. type := system.complexType
  5483. ELSIF subtype = Scanner.Longreal THEN
  5484. type := system.longcomplexType
  5485. ELSE
  5486. HALT(100)
  5487. END;
  5488. x.SetType(type);
  5489. | SyntaxTree.StringValue DO
  5490. x.SetType(ResolveType(SyntaxTree.NewStringType(x.position,system.characterType,x.length)));
  5491. | SyntaxTree.NilValue DO
  5492. x.SetType(system.nilType);
  5493. | SyntaxTree.EnumerationValue DO
  5494. x.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  5495. | SyntaxTree.InlineCallDesignator DO
  5496. ResolveStatementBlock(x.block);
  5497. END; (* no other case allowed *)
  5498. IF currentIsRealtime THEN
  5499. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5500. Error(x.position,"forbidden non-realtime expression in realtime procedure");
  5501. END;
  5502. END;
  5503. (* designator modifiers for backends if they support it ...*)
  5504. WITH x: SyntaxTree.Designator DO
  5505. IF (x.modifiers # NIL) THEN
  5506. WITH result: SyntaxTree.Designator DO
  5507. result.SetModifiers(x.modifiers);
  5508. CheckModifiers(result.modifiers, FALSE);
  5509. ELSE (* error case *)
  5510. END;
  5511. END;
  5512. ELSE
  5513. END;
  5514. RETURN result;
  5515. END ResolveExpression;
  5516. (**
  5517. check expression to be constant expression
  5518. - resolve expression
  5519. - if valid then check that of value type
  5520. report error and return invalidExpression if anything fails
  5521. **)
  5522. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5523. VAR position: Position;
  5524. BEGIN
  5525. position := expression.position;
  5526. expression := ResolveExpression(expression);
  5527. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5528. ELSIF (expression.resolved = NIL) THEN
  5529. Error(position, "expression is not constant");
  5530. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5531. expression := SyntaxTree.invalidExpression;
  5532. END;
  5533. RETURN expression
  5534. END ConstantExpression;
  5535. (** check expression to be constant integer
  5536. - resolve expresssion
  5537. - if valid then check that of integer value type
  5538. report error and return invalidExpression if anything fails
  5539. **)
  5540. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5541. VAR position: Position;
  5542. BEGIN
  5543. position := expression.position;
  5544. expression := ResolveExpression(expression);
  5545. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5546. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5547. expression := SyntaxTree.invalidExpression;
  5548. Error(position, "expression is not a constant integer");
  5549. END;
  5550. RETURN expression
  5551. END ConstantInteger;
  5552. (** check expression as positive (>=0) constant integer
  5553. - resolve expression
  5554. - if valid then check that integer value
  5555. - if integer value then check that value >= 0
  5556. report error and return invalidExpression if anything fails
  5557. **)
  5558. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5559. VAR position: Position;
  5560. BEGIN
  5561. position := expression.position;
  5562. expression := ConstantExpression(expression);
  5563. IF expression = SyntaxTree.invalidExpression THEN
  5564. (* error already reported *)
  5565. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5566. Error(position, "expression is not integer valued");
  5567. expression := SyntaxTree.invalidExpression
  5568. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  5569. Error(position, "integer is not greater or equal zero");
  5570. END;
  5571. RETURN expression
  5572. END ConstantIntegerGeq0;
  5573. (** check expression as condition
  5574. - resolve expression
  5575. - if valid expression then check that result type is boolean
  5576. report error and return invalidExpression if anything fails
  5577. **)
  5578. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5579. VAR position: Position;
  5580. BEGIN
  5581. position := expression.position;
  5582. expression := ResolveExpression(expression);
  5583. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5584. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5585. expression := SyntaxTree.invalidExpression;
  5586. Error(position, "expression is not boolean");
  5587. END;
  5588. RETURN expression
  5589. END ResolveCondition;
  5590. (*** symbols ***)
  5591. PROCEDURE ResolveSymbol*(x: SyntaxTree.Symbol);
  5592. BEGIN
  5593. WITH x:
  5594. | SyntaxTree.TypeDeclaration DO ResolveTypeDeclaration(x)
  5595. | SyntaxTree.Constant DO ResolveConstant(x)
  5596. | SyntaxTree.Parameter DO ResolveParameter(x)
  5597. | SyntaxTree.Variable DO ResolveVariable(x)
  5598. | SyntaxTree.Operator DO ResolveOperator(x)
  5599. | SyntaxTree.Procedure DO ResolveProcedure(x)
  5600. | SyntaxTree.Alias DO ResolveAlias(x)
  5601. | SyntaxTree.Builtin DO ResolveBuiltin(x)
  5602. | SyntaxTree.Import DO ResolveImport(x)
  5603. END;
  5604. END ResolveSymbol;
  5605. (** check a symbol
  5606. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5607. **)
  5608. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5609. VAR scope: SyntaxTree.Scope;
  5610. BEGIN
  5611. (* visibility *)
  5612. scope := symbol.scope;
  5613. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5614. scope := scope.outerScope;
  5615. END;
  5616. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5617. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5618. Error(symbol.position,"cannot be exported");
  5619. IF VerboseErrorMessage THEN
  5620. Printout.Info("symbol",symbol);
  5621. END;
  5622. END;
  5623. END;
  5624. END CheckSymbolVisibility;
  5625. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5626. If node is currently being resolved then emit a cyclic definition error.
  5627. Return TRUE only if node is fully resolved.
  5628. **)
  5629. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5630. VAR result: BOOLEAN;
  5631. BEGIN
  5632. IF SyntaxTree.Resolved IN x.state THEN
  5633. result := FALSE
  5634. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5635. Error(x.position,"cyclic definition");
  5636. result := FALSE;
  5637. ELSE
  5638. result := TRUE;
  5639. x.SetState(SyntaxTree.BeingResolved)
  5640. END;
  5641. RETURN result
  5642. END SymbolNeedsResolution;
  5643. (** check and resolve a type declaration symbol = Type
  5644. - set type to declaration type
  5645. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5646. This is so because the type declaration itself does not have a type but it only stands for a type.
  5647. In the implementation of the compiler this made a lot much easier.
  5648. - resolve and set declared type
  5649. - check symbol
  5650. **)
  5651. PROCEDURE ResolveTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5652. VAR prevScope: SyntaxTree.Scope;
  5653. BEGIN
  5654. IF Trace THEN D.Str("ResolveTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5655. IF SymbolNeedsResolution(typeDeclaration) THEN
  5656. typeDeclaration.SetState(SyntaxTree.Resolved);
  5657. prevScope := currentScope;
  5658. currentScope := typeDeclaration.scope;
  5659. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5660. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5661. CheckSymbolVisibility(typeDeclaration);
  5662. typeDeclaration.SetState(SyntaxTree.Resolved);
  5663. currentScope := prevScope;
  5664. END;
  5665. END ResolveTypeDeclaration;
  5666. (** check and resolve a constant declaration symbol = (constant) expression
  5667. - check expression
  5668. - set type and value
  5669. - check symbol
  5670. **)
  5671. PROCEDURE ResolveConstant(constant: SyntaxTree.Constant);
  5672. VAR
  5673. expression: SyntaxTree.Expression;
  5674. type: SyntaxTree.Type;
  5675. name: Basic.SegmentedName;
  5676. replacement: Replacement;
  5677. BEGIN
  5678. IF Trace THEN D.Str("ResolveConstant "); D.Str0(constant.name); D.Ln; END;
  5679. IF SymbolNeedsResolution(constant) THEN
  5680. expression := constant.value;
  5681. IF replacements # NIL THEN
  5682. Global.GetSymbolSegmentedName(constant, name);
  5683. replacement := replacements;
  5684. WHILE (replacement # NIL) & (replacement.name # name) DO
  5685. replacement := replacement.next;
  5686. END;
  5687. IF replacement # NIL THEN
  5688. InfoSS(constant.position, "replacing constant", constant.name);
  5689. (*
  5690. NEW(stringReader, Strings.Length(replacement.string^));
  5691. stringReader.Set(replacement.string^);
  5692. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5693. NEW(parser, scanner, diagnostics);
  5694. expression := parser.Expression();
  5695. *)
  5696. expression := replacement.expression;
  5697. replacement.used := TRUE;
  5698. END;
  5699. END;
  5700. constant.SetType(SyntaxTree.invalidType);
  5701. expression := ConstantExpression(expression);
  5702. ASSERT(expression.type # NIL);
  5703. type := expression.type.resolved;
  5704. constant.SetType(type);
  5705. constant.SetValue(expression);
  5706. CheckSymbolVisibility(constant);
  5707. constant.SetState(SyntaxTree.Resolved);
  5708. END;
  5709. END ResolveConstant;
  5710. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  5711. VAR procedureAlignment: LONGINT;
  5712. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  5713. (* least common multiple *)
  5714. VAR a,b: LONGINT;
  5715. BEGIN
  5716. a := a0; b := b0;
  5717. WHILE (a # b) DO
  5718. IF a < b THEN a := a+a0
  5719. ELSE b := b + b0
  5720. END;
  5721. END;
  5722. RETURN a
  5723. END LCM;
  5724. BEGIN
  5725. IF alignment > 1 THEN
  5726. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  5727. IF (procedureAlignment > 1) THEN
  5728. alignment := LCM(alignment, procedureAlignment);
  5729. END;
  5730. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  5731. END;
  5732. END AdaptStackAlignment;
  5733. (** check and resolve a variable / field
  5734. - check and set type
  5735. - negative check on open array type
  5736. - check symbol
  5737. **)
  5738. PROCEDURE ResolveVariable(variable: SyntaxTree.Variable);
  5739. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  5740. BEGIN
  5741. IF Trace THEN D.Str("ResolveVariable "); D.Str0(variable.name); D.Ln; END;
  5742. IF SymbolNeedsResolution(variable) THEN
  5743. modifiers := variable.modifiers;
  5744. (*
  5745. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  5746. variable.AddFlags(flags);
  5747. *)
  5748. variable.SetType(ResolveType(variable.type));
  5749. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  5750. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  5751. Error(variable.position,"forbidden open array variable");
  5752. END;
  5753. END;
  5754. CheckSymbolVisibility(variable);
  5755. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  5756. variable.SetUntraced(TRUE);
  5757. IF ~ContainsPointer(variable.type) THEN
  5758. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  5759. Error(position, "untraced flag on non-pointer variable");
  5760. END;
  5761. END;
  5762. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  5763. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5764. IF ~PowerOf2(value) THEN
  5765. Error(position, "forbidden alignment - must be power of two");
  5766. ELSE
  5767. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  5768. END;
  5769. END;
  5770. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  5771. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  5772. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5773. Error(position, "fixed position not possible in procedure");
  5774. END;
  5775. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  5776. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  5777. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5778. Error(position,"fictive offset not possible in procedure");
  5779. END;
  5780. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  5781. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  5782. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  5783. END;
  5784. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.PreferRegister(TRUE) END;
  5785. IF variable.type.resolved IS SyntaxTree.CellType THEN
  5786. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  5787. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  5788. END;
  5789. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  5790. IF variable.initializer # NIL THEN
  5791. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ResolveExpression(variable.initializer), variable.type));
  5792. END;
  5793. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  5794. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  5795. pointerType.SetPointerBase(variable.type);
  5796. pointerType.SetHidden(TRUE);
  5797. variable.SetType(ResolveType(pointerType));
  5798. END;
  5799. variable.SetState(SyntaxTree.Resolved);
  5800. END;
  5801. END ResolveVariable;
  5802. (** check and resolve a (procedure) parameter
  5803. - check and set type
  5804. - check symbol
  5805. - check parameter kind and set read-only flags if appropriate
  5806. **)
  5807. PROCEDURE ResolveParameter(parameter: SyntaxTree.Parameter);
  5808. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  5809. BEGIN
  5810. IF Trace THEN D.Str("ResolveParameter "); D.Str0(parameter.name); D.Ln; END;
  5811. IF SymbolNeedsResolution(parameter) THEN
  5812. modifiers := parameter.modifiers;
  5813. parameter.SetType(ResolveType(parameter.type));
  5814. ASSERT(parameter.type.resolved # NIL);
  5815. CheckSymbolVisibility(parameter);
  5816. IF parameter.defaultValue # NIL THEN
  5817. IF parameter.kind # SyntaxTree.ValueParameter THEN
  5818. Error(parameter.position,"forbidden default value on non-value parameter");
  5819. ELSE
  5820. expression := ConstantExpression(parameter.defaultValue);
  5821. IF CompatibleTo(system,expression.type, parameter.type) THEN
  5822. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  5823. parameter.SetDefaultValue(expression);
  5824. END;
  5825. END;
  5826. END;
  5827. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type) & (parameter.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  5828. Error(parameter.position, "forbidden value parameter of math array type ");
  5829. END;
  5830. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  5831. parameter.SetUntraced(TRUE);
  5832. IF ~ContainsPointer(parameter.type) THEN
  5833. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5834. Error(position, "untraced flag on non-pointer variable");
  5835. END;
  5836. END;
  5837. IF HasFlag(modifiers, Global.NameRegister,position) THEN
  5838. parameter.PreferRegister(TRUE);
  5839. END;
  5840. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  5841. parameter.SetMoveable(TRUE);
  5842. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  5843. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5844. Error(position, "illegal movable flag on non-address variable");
  5845. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  5846. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5847. Error(position, "unnecessary movable flag on variable variable");
  5848. END;
  5849. END;
  5850. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  5851. parameter.SetState(SyntaxTree.Resolved);
  5852. END;
  5853. END ResolveParameter;
  5854. (** check and resolve a procedure (with declaration and implementation scope)
  5855. - check the procedure type
  5856. - check if method (i.e. in record scope), if so then
  5857. - check if (unique) constructor
  5858. - check if (unique) finalizer
  5859. - check if super method available, if so then check signature
  5860. - of not in record scope then negative check on constructor flag
  5861. - of not in record scope then negative check on finalizer flag
  5862. - check declarations (including a delayed implementation check, cf procedure Declarations)
  5863. - check procedure symbol
  5864. **)
  5865. PROCEDURE ResolveProcedure(procedure: SyntaxTree.Procedure);
  5866. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  5867. procedureType: SyntaxTree.ProcedureType;
  5868. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  5869. qualifiedType: SyntaxTree.QualifiedType;
  5870. value: Basic.Integer;
  5871. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  5872. position: Position;
  5873. fp: SyntaxTree.Fingerprint;
  5874. BEGIN
  5875. IF SymbolNeedsResolution(procedure) THEN
  5876. IF Trace THEN D.Str("ResolveProcedure "); D.Str0(procedure.name); D.Ln; END;
  5877. IF IsOberonInline(procedure) THEN
  5878. procedure.SetInline(FALSE);
  5879. procedure.SetOberonInline(TRUE);
  5880. END;
  5881. recentIsRealtime := currentIsRealtime;
  5882. recentIsBodyProcedure := currentIsBodyProcedure;
  5883. CheckSymbolVisibility(procedure);
  5884. IF Trace THEN D.Str("undefined"); D.Ln; END;
  5885. procedureType := procedure.type(SyntaxTree.ProcedureType);
  5886. modifiers := procedureType.modifiers;
  5887. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN
  5888. procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  5889. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  5890. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  5891. END;
  5892. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  5893. procedureType.SetInterrupt(TRUE);
  5894. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  5895. END;
  5896. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  5897. procedureType.SetNoReturn(TRUE);
  5898. END;
  5899. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  5900. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  5901. IF (procedure.scope IS SyntaxTree.ModuleScope) THEN
  5902. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE);
  5903. ELSIF HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE);
  5904. END;
  5905. END;
  5906. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5907. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5908. END;
  5909. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  5910. IF ~PowerOf2(value) THEN
  5911. Error(position, "forbidden stack alignment - must be power of two");
  5912. ELSE
  5913. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5914. END;
  5915. END;
  5916. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  5917. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  5918. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  5919. END;
  5920. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  5921. fp.shallow := value;
  5922. fp.public := value;
  5923. fp.private := value;
  5924. fp.shallowAvailable := TRUE;
  5925. procedure.SetFingerprint(fp);
  5926. END;
  5927. CheckModifiers(modifiers, TRUE);
  5928. modifiers := procedureType.returnTypeModifiers;
  5929. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  5930. CheckModifiers(modifiers, TRUE);
  5931. procedure.SetState(SyntaxTree.Resolved);
  5932. FixProcedureType(procedureType);
  5933. currentIsRealtime := procedureType.isRealtime;
  5934. currentIsBodyProcedure := procedure.isBodyProcedure;
  5935. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  5936. THEN
  5937. Error(procedure.position,"problems during parameter offset computation");
  5938. END;
  5939. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  5940. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  5941. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  5942. Error(procedure.position,"problem during parameter offset generation");
  5943. END;
  5944. END;
  5945. IF procedure.scope IS SyntaxTree.RecordScope THEN
  5946. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  5947. procedureType.SetDelegate(TRUE);
  5948. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  5949. (* add auto-self *)
  5950. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  5951. IF (record.pointerType.typeDeclaration = NIL) THEN
  5952. selfParameter.SetType(record.pointerType);
  5953. ELSE
  5954. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  5955. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  5956. qualifiedType.SetResolved(record.pointerType);
  5957. selfParameter.SetType(qualifiedType);
  5958. END;
  5959. selfParameter.SetAccess(SyntaxTree.Hidden);
  5960. ELSIF (procedureType.selfParameter = NIL) THEN
  5961. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.VarParameter);
  5962. IF (record.typeDeclaration = NIL) THEN
  5963. selfParameter.SetType(record);
  5964. ELSE
  5965. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.typeDeclaration.name);
  5966. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  5967. qualifiedType.SetResolved(record);
  5968. selfParameter.SetType(qualifiedType);
  5969. END;
  5970. procedureType.SetSelfParameter(selfParameter);
  5971. END;
  5972. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  5973. IF procedure.isConstructor THEN
  5974. (*! constructor is always visible, compatibility to paco
  5975. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  5976. *)
  5977. procedure.MarkUsed;
  5978. IF procedureType.returnType # NIL THEN
  5979. Error(procedure.position,"constructor with forbidden return type");
  5980. END;
  5981. proc := procedure.scope.firstProcedure;
  5982. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  5983. proc := proc.nextProcedure;
  5984. END;
  5985. IF proc # NIL THEN
  5986. Error(procedure.position,"duplicate constructor")
  5987. ELSE
  5988. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  5989. END;
  5990. END;
  5991. IF procedure.isFinalizer THEN
  5992. procedure.MarkUsed;
  5993. IF procedureType.returnType # NIL THEN
  5994. Error(procedure.position,"finalizer with forbidden return type");
  5995. END;
  5996. IF procedureType.numberParameters # 0 THEN
  5997. Error(procedure.position,"finalizer with formal parameters");
  5998. END;
  5999. proc := procedure.scope.firstProcedure;
  6000. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6001. proc := proc.nextProcedure;
  6002. END;
  6003. IF proc # NIL THEN
  6004. Error(procedure.position,"duplicate finalizer")
  6005. ELSE
  6006. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6007. END;
  6008. END;
  6009. super := FindSuperProcedure(record.recordScope, procedure);
  6010. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6011. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6012. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6013. END;
  6014. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6015. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6016. END;
  6017. IF super.isFinal THEN
  6018. Error(procedure.position,"forbidden method extending final method");
  6019. END;
  6020. (*
  6021. IF super.access # procedure.access THEN
  6022. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6023. END;
  6024. *)
  6025. procedure.SetSuper(super);
  6026. super.SetOverwritten(TRUE);
  6027. procedure.SetAccess(procedure.access+super.access);
  6028. procedure.MarkUsed;
  6029. END;
  6030. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6031. THEN
  6032. Error(procedure.position,"problems during parameter offset computation");
  6033. END;
  6034. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6035. IF cellsAreObjects THEN
  6036. procedureType.SetDelegate(TRUE);
  6037. END;
  6038. IF procedure.isConstructor THEN
  6039. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6040. END;
  6041. ELSIF procedure.isConstructor THEN
  6042. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6043. END;
  6044. Declarations(procedure.procedureScope, FALSE, {0,1});
  6045. (* body resolution part done as late fix of the procedure type *)
  6046. procedure.SetState(SyntaxTree.Resolved);
  6047. currentIsRealtime := recentIsRealtime;
  6048. currentIsBodyProcedure := recentIsBodyProcedure;
  6049. END;
  6050. END ResolveProcedure;
  6051. PROCEDURE ResolveAlias(x: SyntaxTree.Alias);
  6052. BEGIN
  6053. x.SetExpression(ResolveExpression(x.expression));
  6054. END ResolveAlias;
  6055. (**
  6056. a builtin procedure is a global item that may not be modified locally
  6057. instead the resolving of builtin procedure calls are done in the esignator
  6058. **)
  6059. PROCEDURE ResolveBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6060. VAR type: SyntaxTree.Type;
  6061. BEGIN
  6062. type := ResolveType(builtinProcedure.type);
  6063. END ResolveBuiltin;
  6064. (* nopov *)
  6065. (** check and resolve operator
  6066. - operators are first checked as procedures
  6067. - then additional operator-specific checks are done
  6068. - note that only module-scope operators are checked here
  6069. (operators in a record scope are only allowed in the context of
  6070. array-structured object types and checked in 'ResolveArrayStructure')
  6071. - also note that inter-operator conformity is not checked here
  6072. **)
  6073. PROCEDURE ResolveOperator(operator: SyntaxTree.Operator);
  6074. VAR
  6075. procedureType: SyntaxTree.ProcedureType;
  6076. leftType, rightType: SyntaxTree.Type;
  6077. identifierNumber: LONGINT; position: Position;
  6078. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6079. modifiers: SyntaxTree.Modifier;
  6080. (** whether a type is locally defined in the current module scope
  6081. for arrays, the base type must be locally defined **)
  6082. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6083. BEGIN
  6084. IF type = NIL THEN
  6085. RETURN FALSE
  6086. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6087. RETURN TRUE
  6088. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6089. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6090. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6091. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6092. ELSE
  6093. RETURN FALSE
  6094. END
  6095. END IsLocallyDefined;
  6096. BEGIN
  6097. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6098. procedureType := operator.type(SyntaxTree.ProcedureType);
  6099. modifiers := procedureType.modifiers;
  6100. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6101. CheckModifiers(modifiers, TRUE);
  6102. ResolveProcedure(operator);
  6103. IF operator.scope IS SyntaxTree.RecordScope THEN
  6104. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6105. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6106. IF identifierNumber = -1 THEN
  6107. Error(operator.position, "operator with unknown identifier")
  6108. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6109. Error(operator.position, "identifier may not be used for operator")
  6110. ELSE
  6111. IF procedureType.numberParameters < 1 THEN
  6112. Error(operator.position, "operator without operand");
  6113. ELSIF procedureType.numberParameters > 2 THEN
  6114. Error(operator.position, "operator with more than two operands");
  6115. ELSE
  6116. (* determine operand types *)
  6117. leftType := procedureType.firstParameter.type;
  6118. IF procedureType.numberParameters > 1 THEN
  6119. rightType := procedureType.firstParameter.nextParameter.type
  6120. ELSE
  6121. rightType := NIL
  6122. END;
  6123. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6124. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6125. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6126. Error(operator.position, "none of the operands is declared in the same module")
  6127. END
  6128. END;
  6129. (* TODO: refine the checks, think about how restrictive the checks should be
  6130. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6131. They might be used for intersection, union, complement of custom object types *)
  6132. (* defaults *)
  6133. hasReturnType := TRUE;
  6134. mustBeUnary := FALSE;
  6135. mustBeBinary := FALSE;
  6136. mustReturnBoolean := FALSE;
  6137. mustReturnInteger := FALSE;
  6138. mustHaveEquitypedOperands := FALSE;
  6139. (* operator-specific exceptions *)
  6140. CASE identifierNumber OF
  6141. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6142. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6143. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6144. mustBeBinary := TRUE
  6145. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6146. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6147. | Scanner.Times: mustBeBinary := TRUE
  6148. | Scanner.TimesTimes: mustBeBinary := TRUE
  6149. | Scanner.DotTimes: mustBeBinary := TRUE
  6150. | Scanner.PlusTimes: mustBeBinary := TRUE
  6151. | Scanner.Slash: mustBeBinary := TRUE
  6152. | Scanner.Backslash: mustBeBinary := TRUE
  6153. | Scanner.DotSlash: mustBeBinary := TRUE
  6154. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6155. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6156. | Scanner.Not: mustBeUnary := TRUE
  6157. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6158. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6159. | Scanner.Transpose: mustBeUnary := TRUE;
  6160. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6161. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6162. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6163. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6164. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6165. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6166. | Global.Abs: mustBeUnary := TRUE;
  6167. | Global.Ash: (* TODO: arity? *)
  6168. | Global.Cap: (* TODO: arity? *)
  6169. | Global.Chr: mustBeUnary := TRUE;
  6170. | Global.Entier: (* TODO: arity? *)
  6171. | Global.EntierH: (* TODO: arity? *)
  6172. | Global.Len: (* unary and binary *)
  6173. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6174. | Global.Max, Global.Min: (* unary and binary *)
  6175. | Global.Odd: (* TODO: arity? *)
  6176. | Global.Sum: (* TODO: arity? *)
  6177. | Global.All: (* TODO: arity? *)
  6178. | Global.Re, Global.Im:
  6179. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6180. | Scanner.Alias:
  6181. | Global.Reshape:
  6182. | Scanner.GreaterGreater, Scanner.LessLess:
  6183. mustBeBinary := TRUE; hasReturnType := FALSE;
  6184. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6185. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6186. END;
  6187. (* check parameter count *)
  6188. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6189. Error(operator.position,"operator is not unary")
  6190. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6191. Error(operator.position,"operator is not binary")
  6192. END;
  6193. (* check parameter types *)
  6194. (* TODO: is this used at all? *)
  6195. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6196. leftType := procedureType.firstParameter.type;
  6197. rightType := procedureType.firstParameter.nextParameter.type;
  6198. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6199. Error(operator.position, "the two operands are not of the same type")
  6200. END
  6201. END;
  6202. (* check return type *)
  6203. IF hasReturnType THEN
  6204. IF procedureType.returnType = NIL THEN
  6205. Error(operator.position, "return type required")
  6206. ELSIF mustReturnBoolean THEN
  6207. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6208. Error(operator.position,"return type is not Boolean")
  6209. END
  6210. ELSIF mustReturnInteger THEN
  6211. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  6212. Error(operator.position,"return type is no size type")
  6213. END
  6214. END
  6215. ELSIF procedureType.returnType # NIL THEN
  6216. Error(operator.position, "return type not allowed")
  6217. END
  6218. END
  6219. END
  6220. END
  6221. END ResolveOperator;
  6222. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6223. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6224. BEGIN
  6225. IF error THEN RETURN FALSE END;
  6226. prevScope := currentScope;
  6227. prevDiagnostics := diagnostics;
  6228. diagnostics := NIL; (* suppress error output *)
  6229. currentScope := module.moduleScope;
  6230. ResolveImport(x);
  6231. IF ~error THEN
  6232. module.moduleScope.AddImport(x);
  6233. x.SetScope(module.moduleScope);
  6234. END;
  6235. currentScope := prevScope;
  6236. diagnostics := prevDiagnostics;
  6237. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6238. END AddImport;
  6239. (** check and resolve import
  6240. - check for name = SYSTEM
  6241. - check for forbidden self import
  6242. - search through global import cache: already imported?
  6243. - check if already imported indirectly
  6244. - import if necessary -> set module and enter into import cache
  6245. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6246. - after this import this direct import and all indirect imports are stored in the current module's import list
  6247. **)
  6248. PROCEDURE ResolveImport(x: SyntaxTree.Import);
  6249. VAR
  6250. module: SyntaxTree.Module;
  6251. moduleScope: SyntaxTree.ModuleScope;
  6252. import,reimport: SyntaxTree.Import;
  6253. filename: FileName;
  6254. prevScope: SyntaxTree.Scope;
  6255. BEGIN
  6256. IF SymbolNeedsResolution(x) THEN
  6257. prevScope := currentScope;
  6258. x.SetType(SyntaxTree.importType);
  6259. moduleScope := currentScope.ownerModule.moduleScope;
  6260. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6261. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6262. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6263. Error(x.position,"forbidden self import");
  6264. ELSE
  6265. (* search through global import list: already imported ? *)
  6266. IF (x.module = NIL) & (importCache # NIL) THEN
  6267. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6268. ELSE import := NIL
  6269. END;
  6270. IF x.module # NIL THEN (* already imported indirectly *)
  6271. module := x.module;
  6272. ELSIF import # NIL THEN (* already in module list *)
  6273. module := import.module;
  6274. ASSERT(module # NIL);
  6275. x.SetModule(module);
  6276. ELSE (* must be imported *)
  6277. Global.ModuleFileName(x.moduleName,x.context,filename);
  6278. IF symbolFileFormat # NIL THEN
  6279. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6280. IF module = NIL THEN
  6281. ErrorSS(x.position,"could not import",filename);
  6282. IF VerboseErrorMessage THEN
  6283. Printout.Info("import",x)
  6284. END
  6285. ELSE
  6286. (*
  6287. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6288. (*! should rather be done by importer *)
  6289. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6290. checker.importCache := importCache;
  6291. checker.arrayBaseImported := arrayBaseImported;
  6292. checker.global := global;
  6293. checker.Module(module); (* semantic check *)
  6294. error := error OR checker.error;
  6295. END;
  6296. *)
  6297. (*
  6298. ASSERT(SyntaxTree.Resolved IN module.state);
  6299. *)
  6300. x.SetModule(module);
  6301. IF importCache # NIL THEN
  6302. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6303. import.SetContext(x.context);
  6304. import.SetModule(module);
  6305. importCache.AddImport(import);
  6306. END;
  6307. END;
  6308. ELSE
  6309. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6310. END;
  6311. END;
  6312. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6313. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6314. END;
  6315. import := module.moduleScope.firstImport;
  6316. WHILE(import # NIL) DO
  6317. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6318. ASSERT(currentScope # NIL);
  6319. ASSERT(currentScope.ownerModule # NIL);
  6320. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6321. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6322. Error(x.position,"recursive import");
  6323. ELSE
  6324. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6325. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6326. IF reimport = NIL THEN (* indirect import *)
  6327. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6328. reimport.SetContext(import.context);
  6329. reimport.SetModule(import.module);
  6330. moduleScope.AddImport(reimport);
  6331. reimport.SetScope(moduleScope);
  6332. ELSE
  6333. ASSERT(import.module # NIL);
  6334. reimport.SetModule(import.module); (* direct or indirect import *)
  6335. END;
  6336. END;
  6337. import := import.nextImport;
  6338. END;
  6339. END;
  6340. END;
  6341. currentScope := prevScope;
  6342. (* ELSE nothing to be done *)
  6343. x.SetState(SyntaxTree.Resolved);
  6344. END;
  6345. END ResolveImport;
  6346. (*** statements ***)
  6347. PROCEDURE ResolveStatement*(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6348. VAR result: SyntaxTree.Statement; pos: WORD;
  6349. BEGIN
  6350. pos := x.position.start;
  6351. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6352. activeCellsStatement := FALSE;
  6353. result := x;
  6354. WITH x:
  6355. SyntaxTree.ProcedureCallStatement DO result := ResolveProcedureCallStatement(x)
  6356. | SyntaxTree.Assignment DO result := ResolveAssignment(x)
  6357. | SyntaxTree.CommunicationStatement DO result := ResolveCommunicationStatement(x)
  6358. | SyntaxTree.IfStatement DO result := ResolveIfStatement(x)
  6359. | SyntaxTree.WithStatement DO result := ResolveWithStatement(x)
  6360. | SyntaxTree.CaseStatement DO result := ResolveCaseStatement(x)
  6361. | SyntaxTree.WhileStatement DO result := ResolveWhileStatement(x)
  6362. | SyntaxTree.RepeatStatement DO result := ResolveRepeatStatement(x)
  6363. | SyntaxTree.ForStatement DO result := ResolveForStatement(x)
  6364. | SyntaxTree.LoopStatement DO result := ResolveLoopStatement(x)
  6365. | SyntaxTree.ExitableBlock DO result := ResolveExitableBlock(x)
  6366. | SyntaxTree.ExitStatement DO result := ResolveExitStatement(x)
  6367. | SyntaxTree.ReturnStatement DO result := ResolveReturnStatement(x)
  6368. | SyntaxTree.AwaitStatement DO result := ResolveAwaitStatement(x)
  6369. | SyntaxTree.StatementBlock DO ResolveStatementBlock(x)
  6370. | SyntaxTree.Code DO ResolveCode(x)
  6371. END;
  6372. RETURN result;
  6373. END ResolveStatement;
  6374. (** check and resolve statement sequence
  6375. - check all statements, replace if necessary
  6376. **)
  6377. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6378. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6379. BEGIN
  6380. IF statementSequence # NIL THEN (* else empty *)
  6381. FOR i := 0 TO statementSequence.Length()-1 DO
  6382. statement := statementSequence.GetStatement(i);
  6383. resolved := ResolveStatement(statement);
  6384. IF (resolved # statement) THEN
  6385. statementSequence.SetStatement(i,resolved);
  6386. END;
  6387. END;
  6388. END;
  6389. END StatementSequence;
  6390. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6391. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6392. - check if procedure is callable
  6393. - check return type = NIL (otherwise must be assignment statement)
  6394. **)
  6395. PROCEDURE ResolveProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement): SyntaxTree.Statement;
  6396. VAR call: SyntaxTree.Designator;
  6397. BEGIN
  6398. IF Trace THEN D.Str("ResolveProcedureCallStatement"); D.Ln; END;
  6399. call := procedureCall.call;
  6400. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6401. IF procedureCall.ignore THEN Error(procedureCall.position, "ignoring non-procedure call") END;
  6402. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6403. END;
  6404. call := ResolveDesignator(call);
  6405. IF call = SyntaxTree.invalidDesignator THEN
  6406. (* error already handled *)
  6407. ELSIF ~IsCallable(call) THEN
  6408. Error(procedureCall.position,"called object is not a procedure");
  6409. ELSIF call.type # NIL THEN
  6410. IF ~procedureCall.ignore THEN
  6411. Error(procedureCall.position,"calling procedure with non-void return type");
  6412. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6413. END;
  6414. ELSIF procedureCall.ignore THEN
  6415. Error(procedureCall.position,"ignoring procedure call without return value");
  6416. END;
  6417. procedureCall.SetCall(call);
  6418. RETURN procedureCall;
  6419. END ResolveProcedureCallStatement;
  6420. (** check and resolve assignment LHS := RHS
  6421. - resolve LHS and RHS
  6422. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6423. - check if assignment is compatible
  6424. - check if LHS is variable (i.e. assignable)
  6425. - convert RHS if necessary
  6426. **)
  6427. PROCEDURE ResolveAssignment(assignment: SyntaxTree.Assignment): SyntaxTree.Statement;
  6428. VAR
  6429. left: SyntaxTree.Designator;
  6430. right, expression: SyntaxTree.Expression;
  6431. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6432. BEGIN
  6433. right := ResolveExpression(assignment.right);
  6434. assignment.left.SetRelatedRhs(right);
  6435. left := ResolveDesignator(assignment.left);
  6436. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6437. (* error already handled *)
  6438. ELSIF IsIndexOperator(left) & left.assignable THEN
  6439. (* LHS is index write operator call *)
  6440. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6441. RETURN SyntaxTree.NewProcedureCallStatement(assignment.position, FALSE, procedureCallDesignator, assignment.outer);
  6442. ELSIF CheckVariable(left) THEN
  6443. expression := NewOperatorCall(assignment.position, NIL, Scanner.Becomes, left, right, NIL);
  6444. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6445. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6446. RETURN SyntaxTree.NewProcedureCallStatement(assignment.position, FALSE, procedureCallDesignator, assignment.outer);
  6447. ELSIF AssignmentCompatible(left, right) THEN
  6448. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6449. assignment.SetLeft(left);
  6450. assignment.SetRight(right);
  6451. END
  6452. END;
  6453. RETURN assignment;
  6454. END ResolveAssignment;
  6455. PROCEDURE ResolveCommunicationStatement(communication: SyntaxTree.CommunicationStatement): SyntaxTree.Statement;
  6456. VAR
  6457. left: SyntaxTree.Designator;
  6458. right: SyntaxTree.Expression;
  6459. inPort, outPort: SyntaxTree.PortType;
  6460. expression: SyntaxTree.Expression;
  6461. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6462. BEGIN
  6463. right := ResolveExpression(communication.right);
  6464. left := ResolveDesignator(communication.left);
  6465. communication.SetLeft(left);
  6466. communication.SetRight(right);
  6467. expression := NewOperatorCall(communication.position, NIL, communication.op, left, right, NIL);
  6468. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6469. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6470. RETURN SyntaxTree.NewProcedureCallStatement(communication.position, FALSE, procedureCallDesignator, communication.outer);
  6471. ELSE
  6472. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6473. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6474. (* error already handled *)
  6475. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6476. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6477. IF outPort.direction # SyntaxTree.OutPort THEN
  6478. Error(left.position,"not an out-port")
  6479. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6480. Error(left.position,"incompatible to port type");
  6481. ELSE
  6482. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6483. communication.SetRight(right)
  6484. END;
  6485. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6486. IF CheckVariable(left) THEN
  6487. IF inPort.direction # SyntaxTree.InPort THEN
  6488. Error(left.position,"not an in-port")
  6489. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6490. Error(right.position,"incompatible to port type");
  6491. END;
  6492. END;
  6493. ELSE
  6494. Error(communication.position,"unsupported stream operation");
  6495. END;
  6496. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6497. IF outPort.direction # SyntaxTree.OutPort THEN
  6498. Error(left.position,"not an out-port")
  6499. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6500. Error(left.position,"incompatible to port type");
  6501. ELSE
  6502. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6503. communication.SetRight(right)
  6504. END;
  6505. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6506. IF CheckVariable(right) THEN
  6507. IF inPort.direction # SyntaxTree.InPort THEN
  6508. Error(left.position,"not an in-port")
  6509. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6510. Error(right.position,"incompatible to port type");
  6511. END;
  6512. END;
  6513. ELSE
  6514. Error(communication.position, "unsupported operation");
  6515. END;
  6516. END;
  6517. RETURN communication;
  6518. END ResolveCommunicationStatement;
  6519. (** check and resolve if/eslif part
  6520. - check condition
  6521. - check statement sequence
  6522. **)
  6523. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6524. VAR prevUnreachable, b: BOOLEAN;
  6525. BEGIN
  6526. prevUnreachable := currentIsUnreachable;
  6527. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6528. IF IsBooleanValue(ifPart.condition,b) THEN
  6529. IF b=FALSE THEN
  6530. currentIsUnreachable := TRUE
  6531. ELSIF b=TRUE THEN
  6532. true := TRUE
  6533. END;
  6534. END;
  6535. StatementSequence(ifPart.statements);
  6536. currentIsUnreachable := prevUnreachable;
  6537. END IfPart;
  6538. (** check and resolve if statement
  6539. - check if parts and else part statement sequence
  6540. **)
  6541. PROCEDURE ResolveIfStatement(ifStatement: SyntaxTree.IfStatement): SyntaxTree.Statement;
  6542. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6543. BEGIN
  6544. prevUnreachable := currentIsUnreachable;
  6545. ifPartTrue := FALSE;
  6546. IfPart(ifStatement.ifPart,ifPartTrue);
  6547. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6548. elsif := ifStatement.GetElsifPart(i);
  6549. IfPart(elsif,ifPartTrue);
  6550. END;
  6551. IF ifStatement.elsePart # NIL THEN
  6552. IF ifPartTrue THEN
  6553. currentIsUnreachable := TRUE
  6554. END;
  6555. StatementSequence(ifStatement.elsePart)
  6556. END;
  6557. currentIsUnreachable := prevUnreachable;
  6558. RETURN ifStatement;
  6559. END ResolveIfStatement;
  6560. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; variable: SyntaxTree.Designator);
  6561. VAR
  6562. type,variableType: SyntaxTree.Type;
  6563. withEntry: WithEntry;
  6564. symbol: SyntaxTree.Symbol
  6565. BEGIN
  6566. type := ResolveType(withPart.type);
  6567. withPart.SetType(type);
  6568. variableType := variable.type.resolved;
  6569. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6570. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6571. END;
  6572. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6573. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6574. Error(variable.position,"is not extensible designator");
  6575. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6576. Error(variable.position,"is no local variable ");
  6577. IF VerboseErrorMessage THEN
  6578. Printout.Info("variable",variable)
  6579. END;
  6580. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6581. Error(variable.position,"withguarded symbol is no type extension of ");
  6582. IF VerboseErrorMessage THEN
  6583. Printout.Info("variable",variable);
  6584. Printout.Info("type",type);
  6585. END;
  6586. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6587. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6588. Error(variable.position,"withguarded symbol is no variable ");
  6589. IF VerboseErrorMessage THEN
  6590. Printout.Info("variable",variable);
  6591. Printout.Info("type",type);
  6592. END;
  6593. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6594. Error(variable.position,"invalid change of withguarded symbol");
  6595. ELSE
  6596. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6597. NEW(withEntry);
  6598. withEntry.previous := withEntries;
  6599. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6600. withEntry.type := type;
  6601. withEntries := withEntry;
  6602. StatementSequence(withPart.statements);
  6603. withEntries := withEntries.previous;
  6604. END;
  6605. END WithPart;
  6606. (** check and resolve with statement WITH variable: type DO ... END;
  6607. - check type and variable
  6608. - check that variable type is type extension of type
  6609. - check that variable is a variable
  6610. - enter new with scope and enter guardedVariable with same name and reference to variable
  6611. - create if statement:
  6612. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6613. **)
  6614. PROCEDURE ResolveWithStatement(withStatement: SyntaxTree.WithStatement): SyntaxTree.Statement;
  6615. VAR i,j: LONGINT; prevScope: SyntaxTree.Scope; variable: SyntaxTree.Designator;
  6616. BEGIN
  6617. prevScope := currentScope;
  6618. variable := ResolveDesignator(withStatement.variable);
  6619. withStatement.SetVariable(variable);
  6620. FOR i := 0 TO withStatement.WithParts()-1 DO
  6621. WithPart(withStatement.GetWithPart(i),variable);
  6622. END;
  6623. FOR i := 0 TO withStatement.WithParts()-1 DO
  6624. FOR j := i+1 TO withStatement.WithParts()-1 DO
  6625. IF IsTypeExtension(withStatement.GetWithPart(i).type, withStatement.GetWithPart(j).type) THEN
  6626. Error(withStatement.GetWithPart(j).position, "unreachable extended type");
  6627. END;
  6628. END;
  6629. END;
  6630. IF withStatement.elsePart # NIL THEN
  6631. StatementSequence(withStatement.elsePart)
  6632. END;
  6633. currentScope := prevScope;
  6634. RETURN withStatement;
  6635. END ResolveWithStatement;
  6636. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6637. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6638. - check 'first' < 'last' and no overlaps between different case labels
  6639. - check statement sequence
  6640. **)
  6641. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  6642. VAR
  6643. i: LONGINT;
  6644. position: Position;
  6645. expression, left, right: SyntaxTree.Expression;
  6646. expressionType: SyntaxTree.Type;
  6647. l, r: Basic.Integer;
  6648. cl, cr: CHAR;
  6649. thiscases: SyntaxTree.CaseConstant;
  6650. BEGIN
  6651. thiscases := NIL;
  6652. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6653. expression := casePart.elements.GetExpression(i);
  6654. position := expression.position;
  6655. (* set context of range *)
  6656. IF expression IS SyntaxTree.RangeExpression THEN
  6657. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6658. END;
  6659. expression := ResolveExpression(expression);
  6660. IF expression = SyntaxTree.invalidExpression THEN
  6661. (* error already reported *)
  6662. expressionType := SyntaxTree.invalidType;
  6663. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6664. (* read out 'first' and 'last' *)
  6665. left := expression(SyntaxTree.RangeExpression).first;
  6666. right := expression(SyntaxTree.RangeExpression).last;
  6667. (* guaranteed by ResolveRangeExpression: *)
  6668. ASSERT((left # NIL) & (right # NIL));
  6669. ASSERT(left.type.resolved = right.type.resolved);
  6670. left := CompatibleConversion(left.position, left, type);
  6671. right := CompatibleConversion(right.position, right, type);
  6672. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6673. expression(SyntaxTree.RangeExpression).SetLast(right);
  6674. expressionType := RegularType(position,left.type);
  6675. ELSE
  6676. expression := ConstantExpression(expression);
  6677. expression := CompatibleConversion(expression.position, expression, type);
  6678. (*
  6679. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6680. left := Global.NewCharacterValue(system,expression.position,cl);
  6681. expression := casePart.elements.GetExpression(i);
  6682. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6683. expression := left
  6684. END;
  6685. *)
  6686. casePart.elements.SetExpression(i,expression);
  6687. left := expression; right := expression;
  6688. expressionType := RegularType(position,expression.type)
  6689. END;
  6690. IF (expressionType = SyntaxTree.invalidType) THEN
  6691. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6692. Error(position, "inadmissible case label");
  6693. expression := SyntaxTree.invalidExpression;
  6694. ELSE
  6695. l := 0; r := 0;
  6696. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6697. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6698. l := ORD(cl); r := ORD(cr);
  6699. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6700. ELSE
  6701. expression := SyntaxTree.invalidExpression
  6702. END;
  6703. IF expression # SyntaxTree.invalidExpression THEN
  6704. IF l>r THEN
  6705. Error(position, "empty case label")
  6706. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6707. Error(position, "duplicate case label");
  6708. ELSE
  6709. IF l < min THEN min := l END;
  6710. IF r > max THEN max := r END;
  6711. END;
  6712. END;
  6713. END;
  6714. casePart.elements.SetExpression(i,expression);
  6715. END;
  6716. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  6717. casePart.SetConstants(thiscases);
  6718. StatementSequence(casePart.statements);
  6719. END CasePart;
  6720. (** check and resolve case statement CASE variable OF ... END;
  6721. - check variable
  6722. - check case parts
  6723. **)
  6724. PROCEDURE ResolveCaseStatement(caseStatement: SyntaxTree.CaseStatement): SyntaxTree.Statement;
  6725. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  6726. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer;
  6727. BEGIN
  6728. expression := ResolveExpression(caseStatement.variable);
  6729. type := RegularType(expression.position,expression.type);
  6730. IF type = SyntaxTree.invalidType THEN
  6731. expression := SyntaxTree.invalidExpression;
  6732. ELSIF IsIntegerType(type) THEN
  6733. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  6734. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  6735. (*
  6736. expression := Global.NewCharacterValue(system,expression.position,ch);
  6737. *)
  6738. type := expression.type;
  6739. ELSIF IsCharacterType(type) THEN
  6740. ELSIF IsEnumerationType(type) THEN
  6741. ELSE
  6742. Error(caseStatement.variable.position,"variable must be integer or character type");
  6743. expression := SyntaxTree.invalidExpression;
  6744. END;
  6745. caseStatement.SetVariable(expression);
  6746. caseList := NIL;
  6747. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  6748. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  6749. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  6750. END;
  6751. caseStatement.SetMinMax(min,max);
  6752. StatementSequence(caseStatement.elsePart);
  6753. IF expression.resolved # NIL THEN
  6754. IF IsCharacterValue(expression,ch) THEN
  6755. l := ORD(ch)
  6756. ELSIF IsIntegerValue(expression,l) THEN
  6757. END;
  6758. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  6759. END;
  6760. RETURN caseStatement;
  6761. END ResolveCaseStatement;
  6762. (** check and resolve while statement
  6763. - check condition
  6764. - check statement sequence
  6765. **)
  6766. PROCEDURE ResolveWhileStatement(whileStatement: SyntaxTree.WhileStatement): SyntaxTree.Statement;
  6767. VAR prevIsUnreachable,b: BOOLEAN;
  6768. BEGIN
  6769. prevIsUnreachable := currentIsUnreachable;
  6770. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  6771. IF IsBooleanValue(whileStatement.condition,b) THEN
  6772. IF b=FALSE THEN
  6773. currentIsUnreachable := TRUE
  6774. END;
  6775. END;
  6776. StatementSequence(whileStatement.statements);
  6777. currentIsUnreachable := prevIsUnreachable;
  6778. RETURN whileStatement;
  6779. END ResolveWhileStatement;
  6780. (** check and resolve repeat statement
  6781. - check condition
  6782. - check statement sequence
  6783. **)
  6784. PROCEDURE ResolveRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement): SyntaxTree.Statement;
  6785. BEGIN
  6786. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  6787. StatementSequence(repeatStatement.statements);
  6788. RETURN repeatStatement;
  6789. END ResolveRepeatStatement;
  6790. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  6791. VAR withEntry: WithEntry;
  6792. BEGIN
  6793. withEntry := withEntries;
  6794. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  6795. withEntry := withEntry.previous
  6796. END;
  6797. IF withEntry = NIL THEN RETURN FALSE
  6798. ELSE
  6799. type := withEntry.type;
  6800. RETURN TRUE
  6801. END;
  6802. END GetGuard;
  6803. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  6804. - check that variable is an integer variable
  6805. - check that from is integer typed with compatible type
  6806. - check that to has compatible type
  6807. - check that by is constant integer with compatible type
  6808. **)
  6809. PROCEDURE ResolveForStatement(forStatement: SyntaxTree.ForStatement): SyntaxTree.Statement;
  6810. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  6811. BEGIN
  6812. designator := ResolveDesignator(forStatement.variable);
  6813. type := SyntaxTree.invalidType;
  6814. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  6815. designator := SyntaxTree.invalidDesignator;
  6816. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  6817. Error(designator.position,"control variable of non-integer type");
  6818. designator := SyntaxTree.invalidDesignator;
  6819. ELSIF CheckVariable(designator) THEN
  6820. type := designator.type;
  6821. END;
  6822. forStatement.SetVariable(designator);
  6823. expression := ResolveExpression(forStatement.from);
  6824. IF expression = SyntaxTree.invalidExpression THEN
  6825. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6826. Error(expression.position,"start value of incompatible type");
  6827. expression := SyntaxTree.invalidExpression;
  6828. ELSIF type # SyntaxTree.invalidType THEN
  6829. expression := NewConversion(expression.position,expression,type,NIL)
  6830. END;
  6831. forStatement.SetFrom(expression);
  6832. expression := ResolveExpression(forStatement.to);
  6833. IF expression = SyntaxTree.invalidExpression THEN
  6834. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6835. Error(expression.position,"end value of incompatible type");
  6836. expression := SyntaxTree.invalidExpression;
  6837. ELSIF type # SyntaxTree.invalidType THEN
  6838. expression := NewConversion(expression.position,expression,type,NIL)
  6839. END;
  6840. forStatement.SetTo(expression);
  6841. IF forStatement.by # NIL THEN
  6842. expression := ConstantInteger(forStatement.by);
  6843. ELSE
  6844. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  6845. END;
  6846. IF expression = SyntaxTree.invalidExpression THEN
  6847. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6848. Error(expression.position,"step value of incompatible type");
  6849. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  6850. Error(expression.position,"invalid step value");
  6851. ELSIF type # SyntaxTree.invalidType THEN
  6852. expression := NewConversion(expression.position,expression,type,NIL)
  6853. END;
  6854. forStatement.SetBy(expression);
  6855. StatementSequence(forStatement.statements);
  6856. RETURN forStatement;
  6857. END ResolveForStatement;
  6858. (** check and resolve loop statement LOOP StatementSequence END
  6859. - check statement sequence
  6860. **)
  6861. PROCEDURE ResolveLoopStatement(loopStatement: SyntaxTree.LoopStatement): SyntaxTree.Statement;
  6862. BEGIN
  6863. StatementSequence(loopStatement.statements);
  6864. RETURN loopStatement;
  6865. END ResolveLoopStatement;
  6866. PROCEDURE ResolveExitableBlock(exitableBlock: SyntaxTree.ExitableBlock): SyntaxTree.Statement;
  6867. BEGIN
  6868. StatementSequence(exitableBlock.statements);
  6869. RETURN exitableBlock;
  6870. END ResolveExitableBlock;
  6871. (** check and resolve exit statement EXIT
  6872. - check that exit is within LOOP statement block
  6873. **)
  6874. PROCEDURE ResolveExitStatement(exitStatement: SyntaxTree.ExitStatement): SyntaxTree.Statement;
  6875. VAR outer: SyntaxTree.Statement;
  6876. BEGIN
  6877. outer := exitStatement.outer;
  6878. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  6879. outer := outer.outer;
  6880. END;
  6881. IF outer = NIL THEN
  6882. Error(exitStatement.position,"exit statement not within loop statement");
  6883. END;
  6884. RETURN exitStatement;
  6885. END ResolveExitStatement;
  6886. (** check and resolve return statement RETURN [expression]
  6887. - check expression (if any)
  6888. - check if in procedure scope
  6889. - if in procedure scope then check expression compatibility
  6890. - if not in procecdure scope then check on return without expression
  6891. **)
  6892. PROCEDURE ResolveReturnStatement(returnStatement: SyntaxTree.ReturnStatement): SyntaxTree.Statement;
  6893. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  6894. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  6895. BEGIN
  6896. position := returnStatement.position;
  6897. expression := returnStatement.returnValue;
  6898. IF expression # NIL THEN
  6899. expression := ResolveExpression(expression);
  6900. returnStatement.SetReturnValue(expression);
  6901. END;
  6902. outer := returnStatement.outer;
  6903. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  6904. outer := outer.outer
  6905. END;
  6906. IF (outer # NIL) THEN
  6907. scope := outer(SyntaxTree.Body).inScope;
  6908. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  6909. IF (expression # NIL) THEN
  6910. Error(position, "return statement with parameter not in procedure scope");
  6911. END;
  6912. ELSE
  6913. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  6914. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  6915. Error(position, "return statement in procedure that does not return");
  6916. END;
  6917. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  6918. IF returnType # NIL THEN
  6919. returnType := returnType.resolved;
  6920. IF expression = NIL THEN
  6921. Error(position, "empty return type in procedure providing a return type")
  6922. ELSIF expression.type = NIL THEN
  6923. Error(position,"returned type incompatible: expression has no type");
  6924. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  6925. Error(position, "return type not compatible");
  6926. IF VerboseErrorMessage THEN
  6927. Printout.Info("returnType",returnType);
  6928. Printout.Info("expression",expression);
  6929. END;
  6930. ELSE
  6931. expression := NewConversion(expression.position,expression,returnType,NIL);
  6932. returnStatement.SetReturnValue(expression);
  6933. END;
  6934. ELSIF expression # NIL THEN
  6935. Error(position, "non-empty return type in procedure providing no return type");
  6936. END;
  6937. END;
  6938. END;
  6939. RETURN returnStatement;
  6940. END ResolveReturnStatement;
  6941. (** check and resolve await statement AWAIT(condition: Expression)
  6942. - check await condition
  6943. **)
  6944. PROCEDURE ResolveAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement): SyntaxTree.Statement;
  6945. VAR condition: SyntaxTree.Expression;
  6946. BEGIN
  6947. condition := ResolveCondition(awaitStatement.condition);
  6948. IF currentIsRealtime THEN
  6949. Error(awaitStatement.position,"forbidden await statement in realtime block");
  6950. END;
  6951. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  6952. Error(awaitStatement.position,"senseless await statement with constant condition");
  6953. END;
  6954. awaitStatement.SetCondition(condition);
  6955. RETURN awaitStatement;
  6956. END ResolveAwaitStatement;
  6957. PROCEDURE CheckSystemImport(position: Position);
  6958. VAR import: SyntaxTree.Import;
  6959. BEGIN
  6960. import := currentScope.ownerModule.moduleScope.firstImport;
  6961. WHILE(import # NIL) DO
  6962. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  6963. RETURN;
  6964. END;
  6965. import := import.nextImport;
  6966. END;
  6967. Error(position, "forbidden code without system import");
  6968. END CheckSystemImport;
  6969. (** check and resolve code statement: do nothing, must be done by assembler
  6970. **)
  6971. PROCEDURE ResolveCode(code: SyntaxTree.Code);
  6972. BEGIN
  6973. CheckSystemImport(code.position);
  6974. END ResolveCode;
  6975. (** check and set flags of a statement block
  6976. - check for multiply occurence of a flag
  6977. - check and set priority only in bodies
  6978. - check for valid names
  6979. **)
  6980. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  6981. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  6982. flag: LONGINT; recordBody: SyntaxTree.Body;
  6983. PROCEDURE SetProtectedRecord;
  6984. VAR scope: SyntaxTree.Scope;
  6985. BEGIN
  6986. scope := currentScope;
  6987. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  6988. scope := scope.outerScope
  6989. END;
  6990. IF scope # NIL THEN
  6991. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  6992. END;
  6993. END SetProtectedRecord;
  6994. BEGIN
  6995. flags := {};
  6996. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  6997. recordBody := block(SyntaxTree.Body)
  6998. ELSE
  6999. recordBody := NIL
  7000. END;
  7001. blockModifier := block.blockModifiers;
  7002. WHILE(blockModifier # NIL) DO
  7003. name := blockModifier.identifier;
  7004. expression := blockModifier.expression;
  7005. position := blockModifier.position;
  7006. flag := -1;
  7007. IF name=Global.NamePriority THEN
  7008. IF expression = NIL THEN
  7009. Error(position, "missing priority expression");
  7010. ELSIF recordBody = NIL THEN
  7011. Error(position, "priority not on record body");
  7012. ELSIF recordBody.priority # NIL THEN
  7013. Error(position, "duplicate priority expression");
  7014. ELSE
  7015. recordBody.SetPriority(expression);
  7016. END;
  7017. ELSIF expression # NIL THEN
  7018. Error(expression.position,"expression not in connection with priority")
  7019. ELSIF name=Global.NameExclusive THEN
  7020. IF block.isExclusive THEN
  7021. Error(position, "duplicate exclusive flag")
  7022. END;
  7023. block.SetExclusive(TRUE); SetProtectedRecord;
  7024. ELSIF name=Global.NameActive THEN
  7025. IF recordBody = NIL THEN
  7026. Error(position, "active not in record body");
  7027. ELSIF recordBody.isActive THEN
  7028. Error(position, "duplicate active flag")
  7029. ELSE
  7030. recordBody.SetActive(TRUE); SetProtectedRecord;
  7031. END;
  7032. ELSIF name=Global.NameSafe THEN
  7033. IF recordBody = NIL THEN
  7034. Error(position, "safe not in record body");
  7035. ELSIF recordBody.isSafe THEN
  7036. Error(position, "duplicate safe flag")
  7037. ELSE
  7038. recordBody.SetSafe(TRUE);
  7039. SetProtectedRecord;
  7040. END;
  7041. ELSIF name=Global.NameRealtime THEN
  7042. IF recordBody = NIL THEN
  7043. Error(position, "realtime not in record body");
  7044. ELSIF recordBody.isRealtime THEN
  7045. Error(position, "duplicate realtime flag")
  7046. ELSE
  7047. recordBody.SetRealtime(TRUE);
  7048. block.SetRealtime(TRUE);
  7049. END;
  7050. ELSIF name=Global.NameUnchecked THEN
  7051. IF block.isUnchecked THEN
  7052. Error(position, "duplicate unchecked flag")
  7053. ELSE
  7054. block.SetUnchecked(TRUE);
  7055. END;
  7056. ELSIF (name=Global.NameUncooperative) THEN
  7057. IF block.isUncooperative THEN
  7058. Error(position, "duplicate uncooperative flag")
  7059. ELSE
  7060. block.SetUncooperative(TRUE);
  7061. END;
  7062. ELSE
  7063. Error(position, "unknown block modifier");
  7064. END;
  7065. blockModifier := blockModifier.nextModifier;
  7066. END;
  7067. END BlockFlags;
  7068. (** check and resolve statement block
  7069. - check flags (exclusive)
  7070. - check statement sequence
  7071. **)
  7072. PROCEDURE ResolveStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7073. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN; recentScope: SyntaxTree.Scope;
  7074. BEGIN
  7075. recentScope := currentScope;
  7076. (* IF statementBlock.scope # NIL THEN currentScope := statementBlock.scope END;*)
  7077. BlockFlags(statementBlock);
  7078. IF statementBlock.isExclusive THEN
  7079. (* check that not in exclusive block *)
  7080. IF currentIsExclusive THEN
  7081. Error (statementBlock.position,"forbidden recursive exclusive")
  7082. ELSIF currentIsRealtime THEN
  7083. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7084. END;
  7085. END;
  7086. recentExclusive := currentIsExclusive;
  7087. recentUnreachable := currentIsUnreachable;
  7088. recentRealtime := currentIsRealtime;
  7089. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7090. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7091. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7092. StatementSequence(statementBlock.statements);
  7093. currentIsRealtime := recentRealtime;
  7094. currentIsExclusive := recentExclusive;
  7095. currentIsUnreachable := recentUnreachable;
  7096. currentScope := recentScope;
  7097. END ResolveStatementBlock;
  7098. (** check and resolve body
  7099. - check flags (active, priority, safe)
  7100. - check body and finally part
  7101. **)
  7102. PROCEDURE Body(body: SyntaxTree.Body);
  7103. BEGIN
  7104. ResolveStatementBlock(body);
  7105. IF body.isActive THEN
  7106. IF ~currentIsBodyProcedure THEN
  7107. Error(body.position,"active flag not in object body");
  7108. ELSIF body.priority # NIL THEN
  7109. body.SetPriority(ConstantInteger(body.priority));
  7110. END;
  7111. ELSIF body.isSafe THEN
  7112. Error(body.position,"safe flag not in active body");
  7113. ELSIF body.priority # NIL THEN
  7114. Error(body.position,"priority flag not in active body");
  7115. END;
  7116. IF body.code # NIL THEN
  7117. CheckSystemImport(body.position);
  7118. END;
  7119. StatementSequence(body.finally)
  7120. END Body;
  7121. (*** scopes ***)
  7122. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7123. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7124. VAR duplicateSymbol: BOOLEAN;
  7125. BEGIN
  7126. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7127. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7128. Error(symbol.position,"globally defined keyword")
  7129. END;
  7130. scope.EnterSymbol(symbol,duplicateSymbol);
  7131. IF ~allowDuplicate & duplicateSymbol THEN
  7132. Error(symbol.position,"Multiply defined identifier.");
  7133. IF VerboseErrorMessage THEN
  7134. Printout.Info("multiply defined identifier",symbol);
  7135. Printout.Info("in scope",scope);
  7136. END;
  7137. END;
  7138. END Register;
  7139. (**
  7140. implementation: check and resolve an implementation part
  7141. **)
  7142. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7143. move implementation checker to a separate object ? *)
  7144. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7145. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7146. procedureType: SyntaxTree.ProcedureType;
  7147. BEGIN
  7148. prevIsRealtime := currentIsRealtime;
  7149. prevIsBodyProcedure := currentIsBodyProcedure;
  7150. prevIsCellNet := currentIsCellNet;
  7151. prevScope := currentScope;
  7152. currentScope := scope;
  7153. WITH scope: SyntaxTree.ProcedureScope DO
  7154. procedure := scope.ownerProcedure;
  7155. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7156. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7157. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7158. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7159. IF scope.body # NIL THEN
  7160. IF ~procedure.Resolving(TRUE) THEN (* inline cycle detection *)
  7161. Body(scope(SyntaxTree.ProcedureScope).body)
  7162. END;
  7163. IF ~procedure.Resolving(FALSE) THEN (*HALT(100)*) END;
  7164. END;
  7165. IF procedureType.noPAF THEN
  7166. IF scope.firstVariable # NIL THEN
  7167. Error(procedure.position, "forbidden variable in procedure without activation frame");
  7168. ELSIF procedureType.firstParameter # NIL THEN
  7169. Error(procedure.position, "forbidden parameter in procedure without activation frame");
  7170. END;
  7171. END;
  7172. ELSE
  7173. END;
  7174. currentScope := prevScope;
  7175. currentIsRealtime := prevIsRealtime;
  7176. currentIsBodyProcedure := prevIsBodyProcedure;
  7177. currentIsCellNet := prevIsCellNet;
  7178. END Implementation;
  7179. (** implementation phase:
  7180. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7181. **)
  7182. PROCEDURE Implementations(x: SyntaxTree.Module);
  7183. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7184. BEGIN
  7185. prevPhase := phase;
  7186. phase := ImplementationPhase;
  7187. Implementation(x.moduleScope);
  7188. scope := x.moduleScope.firstScope;
  7189. WHILE(scope # NIL) DO
  7190. Implementation(scope);
  7191. scope := scope.nextScope;
  7192. END;
  7193. phase := prevPhase;
  7194. END Implementations;
  7195. (** declaration phase:
  7196. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7197. - import lists (for module scopes)
  7198. - parameter list (for procedure scopes)
  7199. - constant declarations
  7200. - type declarations
  7201. - variable declarations
  7202. - procedure declarations
  7203. preformed in two stages:
  7204. - first all symbols are entered into the symbol table (with uniqueness check),
  7205. - then all symbols are resolved
  7206. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7207. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7208. phases :
  7209. 0 = before procedures
  7210. 1 = procedures and later
  7211. **)
  7212. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7213. VAR
  7214. constant: SyntaxTree.Constant;
  7215. typeDeclaration: SyntaxTree.TypeDeclaration;
  7216. declaredType: SyntaxTree.Type;
  7217. variable: SyntaxTree.Variable;
  7218. procedure: SyntaxTree.Procedure;
  7219. procedureType : SyntaxTree.ProcedureType;
  7220. prevScope: SyntaxTree.Scope;
  7221. parameter: SyntaxTree.Parameter;
  7222. import: SyntaxTree.Import;
  7223. symbol: SyntaxTree.Symbol;
  7224. prevPhase: LONGINT;
  7225. prevError : BOOLEAN;
  7226. i: LONGINT;
  7227. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7228. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7229. BEGIN
  7230. IF type.baseType # NIL THEN
  7231. baseType := type.baseType.resolved;
  7232. IF baseType IS SyntaxTree.PointerType THEN
  7233. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7234. END;
  7235. (*
  7236. IF baseType IS SyntaxTree.CellType THEN
  7237. DeclareCell(baseType(SyntaxTree.CellType));
  7238. END;
  7239. *)
  7240. END;
  7241. parameter := type.firstParameter;
  7242. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7243. (*
  7244. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7245. variable.SetType(parameter.type);
  7246. variable.SetAccess(SyntaxTree.Hidden);
  7247. variable.SetModifiers(parameter.modifiers);
  7248. currentScope.PushVariable(variable);
  7249. *)
  7250. Register(parameter,scope, FALSE);
  7251. parameter := parameter.nextParameter;
  7252. END;
  7253. property := type.firstProperty;
  7254. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7255. (*
  7256. variable := currentScope.FindVariable(property.name);
  7257. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7258. prop := variable(SyntaxTree.Property);
  7259. ELSE (* add, duplicate symbols detection later *)
  7260. prop := SyntaxTree.NewProperty(property.position, property.name);
  7261. currentScope.PushVariable(prop);
  7262. END;
  7263. prop.SetType(property.type);
  7264. prop.SetValue(property.value);
  7265. prop.SetAccess(SyntaxTree.Hidden);
  7266. *)
  7267. Register(property, scope, FALSE);
  7268. property := property.nextProperty;
  7269. END;
  7270. END DeclareCell;
  7271. BEGIN
  7272. prevError := error;
  7273. prevPhase := phase;
  7274. phase := DeclarationPhase;
  7275. prevScope := currentScope;
  7276. currentScope := scope;
  7277. error := FALSE;
  7278. IF 0 IN phases THEN
  7279. (* first enter all symbols in scope *)
  7280. IF scope IS SyntaxTree.ModuleScope THEN
  7281. (* treat imports first for a module scope, , set default context if necessary *)
  7282. import := scope(SyntaxTree.ModuleScope).firstImport;
  7283. WHILE(import # NIL) DO
  7284. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7285. Register(import, currentScope, FALSE);
  7286. import := import.nextImport;
  7287. END;
  7288. import := scope(SyntaxTree.ModuleScope).firstImport;
  7289. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7290. ResolveSymbol(import);
  7291. import := import.nextImport;
  7292. END;
  7293. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7294. (* enter parameters for a procedure scope *)
  7295. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7296. parameter := procedureType.firstParameter;
  7297. WHILE(parameter # NIL) DO
  7298. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7299. END;
  7300. parameter := procedureType.returnParameter;
  7301. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7302. parameter := procedureType.selfParameter;
  7303. IF parameter # NIL THEN
  7304. Register(parameter, currentScope, FALSE);
  7305. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7306. END;
  7307. ELSIF scope IS SyntaxTree.CellScope THEN
  7308. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7309. IF~skipImplementation THEN
  7310. import := scope(SyntaxTree.CellScope).firstImport;
  7311. WHILE(import # NIL) DO
  7312. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7313. Register(import, currentScope, FALSE);
  7314. import := import.nextImport;
  7315. END;
  7316. import := scope(SyntaxTree.CellScope).firstImport;
  7317. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7318. ResolveSymbol(import);
  7319. import := import.nextImport;
  7320. END;
  7321. END;
  7322. END;
  7323. IF error THEN RETURN END;
  7324. IF skipImplementation THEN
  7325. scope.Clear;
  7326. END;
  7327. (* constants *)
  7328. constant := scope.firstConstant;
  7329. WHILE (constant # NIL) DO
  7330. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7331. END;
  7332. (* type declarations *)
  7333. typeDeclaration := scope.firstTypeDeclaration;
  7334. WHILE (typeDeclaration # NIL) DO
  7335. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7336. END;
  7337. (* variables *)
  7338. variable := scope.firstVariable;
  7339. WHILE (variable # NIL) DO
  7340. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7341. END;
  7342. (* procedures *)
  7343. IF scope.procedures # NIL THEN
  7344. FOR i := 0 TO scope.procedures.Length()-1 DO
  7345. procedure := scope.procedures.GetProcedure(i);
  7346. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7347. IF procedureType.selfParameter = NIL THEN
  7348. scope.AddProcedure(procedure);
  7349. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7350. ELSE
  7351. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7352. IF typeDeclaration = NIL THEN
  7353. Error(procedureType.selfParameter.position, "No such type declaration");
  7354. ELSE
  7355. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7356. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7357. declaredType := typeDeclaration.declaredType;
  7358. IF declaredType IS SyntaxTree.PointerType THEN
  7359. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7360. END;
  7361. IF declaredType IS SyntaxTree.RecordType THEN
  7362. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7363. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7364. ELSE
  7365. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7366. END;
  7367. END;
  7368. END;
  7369. END;
  7370. END;
  7371. END;
  7372. (* now process all symbols without any presumption on the order *)
  7373. symbol := scope.firstSymbol;
  7374. WHILE(symbol # NIL) DO
  7375. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7376. IF (symbol IS SyntaxTree.Procedure) THEN
  7377. IF 1 IN phases THEN
  7378. ResolveSymbol(symbol);
  7379. END;
  7380. ELSE
  7381. IF 0 IN phases THEN
  7382. ResolveSymbol(symbol);
  7383. END;
  7384. END;
  7385. END;
  7386. symbol := symbol.nextSymbol;
  7387. END;
  7388. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7389. symbol := scope.firstSymbol;
  7390. WHILE symbol # NIL DO
  7391. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7392. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7393. pointerFixes.Add(symbol, currentScope);
  7394. END;
  7395. IF ~symbol.type.resolved.isRealtime THEN
  7396. Error(symbol.position,"symbol has no realtime type");
  7397. END;
  7398. END;
  7399. symbol := symbol.nextSymbol
  7400. END;
  7401. END;
  7402. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7403. Error(Basic.invalidPosition,"problems during offset computation in module");
  7404. END;
  7405. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7406. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7407. scope.ownerModule.moduleScope.AddScope(scope);
  7408. END;
  7409. phase := prevPhase;
  7410. currentScope := prevScope;
  7411. error := error OR prevError;
  7412. END Declarations;
  7413. (* nopov *)
  7414. (** check if all operators from one module are compatible to the ones in the other module
  7415. - check if there are not multiple operators with the same signature
  7416. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7417. - check for all operators whose signatures are compatible, whether the return types are compatible
  7418. note that:
  7419. - the return type is not considered to be part of the signature
  7420. - two signatures are considered compatible, if all of the operands are compatible
  7421. **)
  7422. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7423. VAR
  7424. thisOperator, thatOperator: SyntaxTree.Operator;
  7425. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7426. thisParameter, thatParameter: SyntaxTree.Parameter;
  7427. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7428. i: LONGINT;
  7429. BEGIN
  7430. currentScope := thisModuleScope;
  7431. hasError := FALSE;
  7432. (* go through all operators in the other module *)
  7433. thatOperator := thatModuleScope.firstOperator;
  7434. WHILE (thatOperator # NIL) & ~hasError DO
  7435. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7436. (* the other operator is accessible *)
  7437. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7438. (* the other operator is not the conversion operator *)
  7439. (* go through all operators in this module *)
  7440. thisOperator := thisModuleScope.firstOperator;
  7441. WHILE (thisOperator # NIL) & ~hasError DO
  7442. IF thisOperator # thatOperator THEN
  7443. (* the operators are not the same *)
  7444. IF thisOperator.name = thatOperator.name THEN
  7445. (* the operators share the same identifier *)
  7446. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7447. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7448. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7449. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7450. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7451. (* both operators have the same paramter count *)
  7452. thisParameter := thisProcedureType.firstParameter;
  7453. thatParameter := thatProcedureType.firstParameter;
  7454. operandsAreEqual := TRUE;
  7455. operandsAreCompatible := TRUE;
  7456. (* go through all parameters *)
  7457. FOR i := 1 TO thisProcedureType.numberParameters DO
  7458. ASSERT(thatParameter # NIL);
  7459. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7460. operandsAreEqual := FALSE;
  7461. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  7462. operandsAreCompatible := FALSE
  7463. END
  7464. END;
  7465. thisParameter := thisParameter.nextParameter;
  7466. thatParameter := thatParameter.nextParameter
  7467. END;
  7468. IF operandsAreEqual THEN
  7469. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7470. hasError := TRUE
  7471. ELSIF operandsAreCompatible THEN
  7472. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7473. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7474. hasError := TRUE
  7475. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7476. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7477. hasError := TRUE
  7478. END
  7479. END
  7480. END
  7481. END
  7482. END;
  7483. thisOperator := thisOperator.nextOperator
  7484. END
  7485. END
  7486. END;
  7487. thatOperator := thatOperator.nextOperator
  7488. END
  7489. END CheckInterOperatorConformity;
  7490. (** check module:
  7491. - check module declaration
  7492. - add context, if necessary
  7493. - remove module from import cache, if necessary
  7494. - check declarations
  7495. - resolve all type fixes
  7496. - check implementation (bodies)
  7497. **)
  7498. PROCEDURE Module*(x: SyntaxTree.Module);
  7499. VAR (* nopov *)
  7500. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7501. BEGIN
  7502. prevScope := currentScope;
  7503. prevIsCellNet := currentIsCellNet;
  7504. module := x;
  7505. ASSERT(x # NIL);
  7506. global := system.globalScope[x.case];
  7507. x.moduleScope.SetGlobalScope(global);
  7508. currentScope := global;
  7509. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  7510. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7511. RemoveModuleFromCache(importCache,x);
  7512. Declarations(x.moduleScope, FALSE, {0,1});
  7513. FixTypes();
  7514. IF module.isCellNet THEN
  7515. currentIsCellNet := TRUE;
  7516. modifier := x.modifiers;
  7517. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7518. CheckModifiers(modifier, FALSE);
  7519. END;
  7520. (* nopov *)
  7521. IF ~error THEN
  7522. (* check if operators conform to each other within this module *)
  7523. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7524. (* go through all imports *)
  7525. import := x.moduleScope.firstImport;
  7526. WHILE import # NIL DO
  7527. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7528. (* check if all operators in this module conform to the ones of the imported module *)
  7529. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7530. END;
  7531. import := import.nextImport
  7532. END;
  7533. END;
  7534. Implementations(x);
  7535. module := NIL;
  7536. currentIsCellNet := prevIsCellNet;
  7537. currentScope := prevScope;
  7538. END Module;
  7539. END Checker;
  7540. Warnings*=OBJECT
  7541. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7542. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7543. BEGIN
  7544. SELF.diagnostics := diagnostics
  7545. END InitWarnings;
  7546. (** types *)
  7547. PROCEDURE Type(CONST x: SyntaxTree.Type);
  7548. BEGIN
  7549. IF SyntaxTree.Warned IN x.state THEN RETURN END;
  7550. x.SetState(SyntaxTree.Warned);
  7551. WITH x:
  7552. SyntaxTree.ArrayType DO Type(x.arrayBase);
  7553. |SyntaxTree.MathArrayType DO Type(x.arrayBase);
  7554. |SyntaxTree.PointerType DO Type(x.pointerBase);
  7555. |SyntaxTree.RecordType DO Scope(x.recordScope);
  7556. |SyntaxTree.CellType DO Scope(x.cellScope)
  7557. ELSE
  7558. END;
  7559. END Type;
  7560. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7561. VAR msg: ARRAY 256 OF CHAR;
  7562. BEGIN
  7563. Global.GetSymbolName(x,msg);
  7564. Strings.Append(msg," ");
  7565. Strings.Append(msg,text);
  7566. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  7567. END Warning;
  7568. (** symbols *)
  7569. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7570. BEGIN
  7571. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7572. IF ~(x IS SyntaxTree.Parameter) THEN
  7573. Warning(x,"never used");
  7574. END;
  7575. END;
  7576. WITH x:
  7577. SyntaxTree.Procedure DO
  7578. Scope(x.procedureScope)
  7579. | SyntaxTree.TypeDeclaration DO
  7580. Type(x.declaredType);
  7581. ELSE
  7582. END;
  7583. END Symbol;
  7584. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7585. VAR
  7586. symbol: SyntaxTree.Symbol;
  7587. BEGIN
  7588. symbol := scope.firstSymbol;
  7589. WHILE(symbol # NIL) DO
  7590. Symbol(symbol);
  7591. symbol := symbol.nextSymbol;
  7592. END;
  7593. END Scope;
  7594. PROCEDURE Module*(x: SyntaxTree.Module);
  7595. BEGIN
  7596. SELF.module := x;
  7597. Scope(x.moduleScope);
  7598. END Module;
  7599. END Warnings;
  7600. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7601. BEGIN
  7602. RETURN procedure.isOberonInline OR procedure.isInline &
  7603. ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7604. END IsOberonInline;
  7605. PROCEDURE SideEffectFree(e: SyntaxTree.Expression): BOOLEAN;
  7606. BEGIN
  7607. IF e = NIL THEN RETURN TRUE
  7608. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SideEffectFree(e(SyntaxTree.SymbolDesignator).left)
  7609. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  7610. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  7611. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  7612. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SideEffectFree(e(SyntaxTree.DereferenceDesignator).left)
  7613. ELSE RETURN FALSE
  7614. END;
  7615. END SideEffectFree;
  7616. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7617. BEGIN
  7618. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7619. END Resolved;
  7620. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  7621. VAR i: LONGINT;
  7622. BEGIN
  7623. i := 1;
  7624. WHILE i < x DO
  7625. i := i *2
  7626. END;
  7627. RETURN i=x
  7628. END PowerOf2;
  7629. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7630. BEGIN
  7631. RETURN
  7632. (scope # NIL) &
  7633. (scope IS SyntaxTree.ModuleScope)
  7634. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7635. OR
  7636. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7637. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7638. END IsCellNetScope;
  7639. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7640. BEGIN
  7641. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7642. END IsCellScope;
  7643. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7644. BEGIN
  7645. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7646. RETURN (scope # NIL) & IsCellNetScope(scope)
  7647. END InCellNetScope;
  7648. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7649. BEGIN
  7650. ASSERT(size MOD system.dataUnit = 0);
  7651. RETURN size DIV system.dataUnit
  7652. END ToMemoryUnits;
  7653. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7654. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7655. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7656. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7657. BEGIN
  7658. IF t = NIL THEN
  7659. RETURN TRUE
  7660. ELSE
  7661. t := t.resolved;
  7662. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7663. END;
  7664. END TypeAllowed;
  7665. BEGIN
  7666. type := type.resolved;
  7667. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7668. RETURN FALSE
  7669. ELSE
  7670. procedureType := type(SyntaxTree.ProcedureType);
  7671. numberParameters := procedureType.numberParameters;
  7672. RETURN
  7673. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7674. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7675. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7676. END;
  7677. END GetProcedureAllowed;
  7678. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7679. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7680. VAR import: SyntaxTree.Import;
  7681. BEGIN
  7682. import := importCache.ImportByModuleName(x.name,x.context);
  7683. IF import # NIL THEN
  7684. importCache.RemoveImporters(x.name,x.context);
  7685. END;
  7686. END RemoveModuleFromCache;
  7687. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7688. (* to <- this assignment compatibility *)
  7689. VAR result: BOOLEAN;
  7690. BEGIN
  7691. IF this= NIL THEN result := (to=NIL)
  7692. ELSIF to=NIL THEN result := FALSE
  7693. ELSE
  7694. (*! will be replaced by this:
  7695. ELSE result := this.CompatibleTo(to.resolved);
  7696. *)
  7697. this := this.resolved; to := to.resolved;
  7698. IF to=SyntaxTree.invalidType THEN result := FALSE
  7699. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7700. ELSIF (to = this) OR (to.SameType(this)) THEN
  7701. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7702. ELSIF to IS SyntaxTree.BasicType THEN
  7703. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7704. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7705. result := this.CompatibleTo(to.resolved)
  7706. ELSE
  7707. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7708. END
  7709. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7710. result := to.sizeInBits >= this.sizeInBits;
  7711. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7712. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7713. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7714. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7715. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7716. result := TRUE;
  7717. ELSIF to IS SyntaxTree.AnyType THEN
  7718. 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);
  7719. ELSIF to IS SyntaxTree.ObjectType THEN
  7720. 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 *) ;
  7721. ELSIF to IS SyntaxTree.ByteType THEN
  7722. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7723. ELSIF to IS SyntaxTree.CharacterType THEN
  7724. result := IsCharacterType(this)
  7725. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  7726. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  7727. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)
  7728. OR IsTensor(this)
  7729. ) THEN
  7730. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  7731. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  7732. result := TRUE;
  7733. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  7734. result := TRUE;
  7735. ELSE
  7736. result := FALSE
  7737. END;
  7738. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  7739. result := TRUE;
  7740. ELSIF to IS SyntaxTree.PointerType THEN
  7741. result := (this IS SyntaxTree.NilType) OR
  7742. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  7743. (IsPointerType(this) & IsTypeExtension(to,this) OR
  7744. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  7745. ELSIF to IS SyntaxTree.ProcedureType THEN
  7746. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  7747. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  7748. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  7749. ELSIF to IS SyntaxTree.RecordType THEN
  7750. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  7751. ELSIF to IS SyntaxTree.ArrayType THEN
  7752. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  7753. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  7754. ELSIF StaticArrayCompatible(to, this) THEN
  7755. result := TRUE
  7756. ELSE
  7757. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  7758. END;
  7759. ELSIF to IS SyntaxTree.MathArrayType THEN
  7760. IF this IS SyntaxTree.MathArrayType THEN
  7761. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  7762. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7763. result := TRUE;
  7764. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  7765. result := TRUE;
  7766. ELSE
  7767. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  7768. END;
  7769. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  7770. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  7771. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  7772. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  7773. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  7774. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  7775. (* ARRAY [x] OF <- ARRAY [x] OF *)
  7776. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  7777. ELSE
  7778. result := FALSE
  7779. END;
  7780. ELSE
  7781. result := FALSE;
  7782. END;
  7783. ELSIF to IS SyntaxTree.StringType THEN
  7784. result := FALSE;
  7785. ELSIF to IS SyntaxTree.EnumerationType THEN
  7786. result := IsEnumerationExtension(this,to);
  7787. ELSIF to IS SyntaxTree.PortType THEN
  7788. result := SameType(to, this)
  7789. ELSE
  7790. Printout.Info("CompatibleTo",to);
  7791. HALT(100); (* implement missing type check *)
  7792. END;
  7793. END;
  7794. RETURN result
  7795. END CompatibleTo;
  7796. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  7797. VAR actualBase, formalBase: SyntaxTree.Type;
  7798. BEGIN
  7799. IF SameType(formal,actual) THEN
  7800. RETURN TRUE
  7801. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7802. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7803. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  7804. RETURN
  7805. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7806. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7807. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  7808. & StaticArrayCompatible(formalBase,actualBase)
  7809. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7810. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7811. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7812. RETURN
  7813. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7814. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7815. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  7816. & StaticArrayCompatible(formalBase,actualBase)
  7817. ELSE RETURN FALSE
  7818. END;
  7819. END StaticArrayCompatible;
  7820. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7821. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  7822. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  7823. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  7824. BEGIN
  7825. result := SameType(formal,actual);
  7826. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7827. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7828. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7829. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  7830. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7831. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7832. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7833. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7834. & TC(formalBase, actualBase);
  7835. END;
  7836. RETURN result
  7837. END TC;
  7838. BEGIN
  7839. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  7840. ELSE
  7841. arrayBase := formalType.arrayBase.resolved;
  7842. IF (actualType IS SyntaxTree.StringType) THEN
  7843. result := arrayBase IS SyntaxTree.CharacterType
  7844. ELSIF actualType IS SyntaxTree.ArrayType THEN
  7845. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  7846. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  7847. result := TC(formalType, actualType);
  7848. ELSE
  7849. result := (arrayBase IS SyntaxTree.ByteType)
  7850. END;
  7851. END;
  7852. RETURN result
  7853. END OpenArrayCompatible;
  7854. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7855. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  7856. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  7857. BEGIN
  7858. IF actualType IS SyntaxTree.MathArrayType THEN
  7859. actualArray := actualType(SyntaxTree.MathArrayType);
  7860. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  7861. (*
  7862. ARRAY [?] OF -> ARRAY [?|*|k] OF
  7863. ARRAY [?|*|k] OF -> ARRAY [?] OF
  7864. *)
  7865. actualBase := ArrayBase(actualType,Infinity);
  7866. formalBase := ArrayBase(formalType,Infinity);
  7867. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  7868. ELSE
  7869. (*
  7870. ARRAY [*|k] OF -> ARRAY [*|n] OF
  7871. *)
  7872. formalBase := Resolved(formalType.arrayBase);
  7873. actualBase := Resolved(actualArray.arrayBase);
  7874. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  7875. (*
  7876. ARRAY [k] -> ARRAY [n]
  7877. *)
  7878. result := (formalType.staticLength = actualArray.staticLength)
  7879. ELSE
  7880. result := TRUE
  7881. END;
  7882. IF ~result THEN
  7883. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  7884. ELSIF actualBase = NIL THEN result := FALSE
  7885. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  7886. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  7887. ELSE
  7888. result := SameType(formalBase,actualBase)
  7889. END;
  7890. END;
  7891. ELSE
  7892. result := FALSE
  7893. END;
  7894. RETURN result
  7895. END MathArrayCompatible;
  7896. (**
  7897. Math Array Type distance for assignments / parameter passings of the form
  7898. from -> to
  7899. variants:
  7900. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  7901. allowed:
  7902. static -> static (& size match)
  7903. static -> open
  7904. static -> tensor
  7905. open -> open
  7906. open -> tensor
  7907. open -> static
  7908. tensor -> tensor
  7909. tensor -> open
  7910. tensor -> static
  7911. **)
  7912. (*! think about the metric here: is form matching more important than element type matching? *)
  7913. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  7914. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  7915. BEGIN
  7916. fromBase := Resolved(from.arrayBase);
  7917. toBase := Resolved(to.arrayBase);
  7918. i := Infinity;
  7919. IF (from = to) OR (from.SameType(to)) THEN
  7920. i := 0;
  7921. ELSIF (from.form = to.form) THEN
  7922. (* static -> static, open -> open, tensor -> tensor *)
  7923. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  7924. IF fromBase = toBase THEN i := 0
  7925. ELSIF toBase = NIL THEN i := 1
  7926. ELSIF fromBase = NIL THEN i := Infinity;
  7927. ELSIF toBase.SameType(fromBase) THEN i := 0
  7928. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  7929. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  7930. ELSE
  7931. i := TypeDistance(system,fromBase, toBase, varpar);
  7932. IF i < Infinity THEN i := i * 5 END;
  7933. END;
  7934. END;
  7935. ELSIF (to.form = SyntaxTree.Static) THEN
  7936. (* forbidden *)
  7937. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  7938. (* static -> tensor, open -> tensor, tensor -> open *)
  7939. IF (toBase=fromBase) THEN i := 0;
  7940. ELSIF toBase = NIL THEN i := 1;
  7941. ELSIF toBase.SameType(fromBase) THEN i := 0
  7942. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  7943. toBase := ArrayBase(toBase,Infinity);
  7944. IF (fromBase=toBase) THEN i := 0
  7945. ELSIF (toBase = NIL) THEN i:= 1
  7946. ELSIF (fromBase = NIL) THEN i := Infinity;
  7947. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  7948. IF i < Infinity THEN i := i * 5 END;
  7949. END;
  7950. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  7951. fromBase := ArrayBase(fromBase,Infinity);
  7952. IF (fromBase=toBase) THEN i := 0
  7953. ELSIF (toBase = NIL) THEN i := 1
  7954. ELSIF (fromBase = NIL) THEN i := Infinity;
  7955. ELSIF toBase.SameType(fromBase) THEN i := 0
  7956. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  7957. IF i < Infinity THEN i := i * 5 END;
  7958. END;
  7959. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  7960. IF i < Infinity THEN i := i * 5 END;
  7961. END;
  7962. IF i # Infinity THEN INC(i,2) END;
  7963. ELSIF (from.form = SyntaxTree.Static) THEN
  7964. (* static -> open *)
  7965. IF (toBase=fromBase) THEN i := 0;
  7966. ELSIF toBase = NIL THEN i := 1
  7967. ELSIF fromBase = NIL THEN i := Infinity
  7968. ELSIF toBase.SameType(fromBase) THEN i := 0
  7969. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  7970. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  7971. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  7972. IF i < Infinity THEN i := i * 5 END;
  7973. END;
  7974. IF i # Infinity THEN INC(i,1) END;
  7975. ELSE HALT(100); (* unknown case *)
  7976. END;
  7977. RETURN i;
  7978. END MathArrayTypeDistance;
  7979. (** compute and return the distance of two array types
  7980. - return the distance of the base types
  7981. **)
  7982. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  7983. VAR i: LONGINT;
  7984. BEGIN
  7985. i := Infinity;
  7986. IF from = to THEN
  7987. i := 0
  7988. ELSE
  7989. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  7990. (*
  7991. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  7992. i := TypeDistance(from.base, to.base);
  7993. IF i >= 0 THEN INC(i) END
  7994. ELSIF (from.mode = open) & (to.mode = open) THEN
  7995. i := TypeDistance(from.base, to.base);
  7996. *)
  7997. END;
  7998. RETURN i
  7999. END ArrayTypeDistance;
  8000. (** compute the signature distance of a procedure and an actual parameter list
  8001. - if any of the parameters are not compatible, the result is infinite
  8002. - add up and return the distance over all parameters
  8003. **)
  8004. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8005. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8006. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8007. BEGIN
  8008. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8009. result := Infinity
  8010. ELSE
  8011. formalParameter := procedureType.firstParameter;
  8012. i := 0;
  8013. result := 0;
  8014. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8015. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8016. actualParameter := actualParameters.GetExpression(i);
  8017. ASSERT(formalParameter.type # NIL);
  8018. IF (actualParameter.type = NIL) THEN distance := Infinity
  8019. ELSE
  8020. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8021. END;
  8022. IF distance = Infinity THEN
  8023. result := Infinity;
  8024. ELSE
  8025. to := formalParameter.type.resolved;
  8026. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8027. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8028. (* already handled varpar *)
  8029. (*
  8030. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8031. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8032. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8033. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8034. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8035. END;
  8036. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8037. result := Infinity
  8038. END;
  8039. *)
  8040. INC(result, distance);
  8041. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8042. INC(result, distance);
  8043. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8044. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8045. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8046. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8047. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8048. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8049. END;
  8050. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8051. result := Infinity
  8052. END;
  8053. ELSE
  8054. result := Infinity
  8055. END;
  8056. ELSE
  8057. INC(result,distance);
  8058. END;
  8059. END;
  8060. (*
  8061. Printout.Info("actual=", actualParameter);
  8062. Printout.Info("formal=", formalParameter);
  8063. TRACE(result);
  8064. *)
  8065. formalParameter := formalParameter.nextParameter; INC(i);
  8066. END;
  8067. END;
  8068. ASSERT(result >= 0);
  8069. RETURN result
  8070. END Distance;
  8071. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8072. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8073. BEGIN
  8074. IF right.numberParameters # (procedureType.numberParameters) THEN
  8075. result := Infinity
  8076. ELSE
  8077. formalParameter := procedureType.firstParameter;
  8078. rightParameter := right.firstParameter;
  8079. i := 0;
  8080. result := 0;
  8081. IF right.isDelegate & ~procedureType.isDelegate THEN
  8082. INC(result);
  8083. ELSIF procedureType.isDelegate & ~right.isDelegate THEN
  8084. result := Infinity;
  8085. END;
  8086. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8087. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8088. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8089. IF distance = Infinity THEN
  8090. result := Infinity;
  8091. ELSE
  8092. INC(result,distance);
  8093. END;
  8094. formalParameter := formalParameter.nextParameter;
  8095. rightParameter := rightParameter.nextParameter;
  8096. END;
  8097. END;
  8098. ASSERT(result >= 0);
  8099. RETURN result
  8100. END ProcedureTypeDistance;
  8101. (** compute and return the distance between two types, used for computation of signature distance
  8102. from -> to
  8103. **)
  8104. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8105. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8106. BEGIN
  8107. i := Infinity;
  8108. IF from = to THEN
  8109. i := 0
  8110. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8111. ELSIF to.SameType(from) THEN
  8112. i := 0;
  8113. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8114. i := Infinity;
  8115. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8116. i := 10;
  8117. ELSIF (from IS SyntaxTree.StringType) THEN
  8118. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8119. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8120. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8121. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8122. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8123. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8124. i := 1
  8125. ELSIF (from IS SyntaxTree.NilType) THEN
  8126. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8127. (*
  8128. ELSIF (from = NoType) THEN
  8129. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8130. *)
  8131. ELSIF (from IS SyntaxTree.BasicType) THEN
  8132. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8133. IF varpar & (i # 0) THEN i := Infinity END;
  8134. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8135. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8136. ELSIF (from IS SyntaxTree.RecordType) THEN
  8137. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8138. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8139. IF to IS SyntaxTree.MathArrayType THEN
  8140. (*
  8141. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8142. i := Infinity;
  8143. ELSE
  8144. *)
  8145. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8146. (*
  8147. END;
  8148. *)
  8149. END
  8150. ELSIF (from IS SyntaxTree.PointerType) THEN
  8151. ptr := from(SyntaxTree.PointerType);
  8152. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8153. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8154. (* ELSE i := TypeDistance(ptr.base, to); *)
  8155. END
  8156. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8157. IF (to IS SyntaxTree.ProcedureType) THEN
  8158. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8159. END;
  8160. ELSIF (from IS SyntaxTree.PortType) THEN
  8161. IF (to IS SyntaxTree.PortType) THEN
  8162. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8163. i := 0;
  8164. END;
  8165. END;
  8166. (*no procedure test, procedure must be the same*)
  8167. END;
  8168. RETURN i
  8169. END TypeDistance;
  8170. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8171. BEGIN
  8172. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8173. END IsIntegerType;
  8174. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8175. BEGIN
  8176. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8177. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8178. OR IsPointerType(type)
  8179. )
  8180. END IsAddressType;
  8181. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8182. BEGIN
  8183. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  8184. END IsSizeType;
  8185. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8186. BEGIN
  8187. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8188. END IsSignedIntegerType;
  8189. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8190. BEGIN
  8191. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8192. END IsUnsignedIntegerType;
  8193. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8194. VAR result: BOOLEAN;
  8195. BEGIN
  8196. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8197. value := x.resolved(SyntaxTree.IntegerValue).value;
  8198. result := TRUE
  8199. ELSE
  8200. result := FALSE
  8201. END;
  8202. RETURN result
  8203. END IsIntegerValue;
  8204. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8205. VAR result: BOOLEAN;
  8206. BEGIN
  8207. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8208. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8209. result := TRUE
  8210. ELSE
  8211. result := FALSE
  8212. END;
  8213. RETURN result
  8214. END IsEnumerationValue;
  8215. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8216. VAR result: BOOLEAN;
  8217. BEGIN
  8218. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8219. value := x.resolved(SyntaxTree.RealValue).value;
  8220. result := TRUE
  8221. ELSE
  8222. result := FALSE
  8223. END;
  8224. RETURN result
  8225. END IsRealValue;
  8226. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8227. VAR result: BOOLEAN;
  8228. BEGIN
  8229. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8230. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8231. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8232. result := TRUE
  8233. ELSE
  8234. result := FALSE
  8235. END;
  8236. RETURN result
  8237. END IsComplexValue;
  8238. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8239. VAR result: BOOLEAN;
  8240. BEGIN
  8241. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8242. value := x.resolved(SyntaxTree.CharacterValue).value;
  8243. result := TRUE
  8244. ELSE
  8245. result := FALSE
  8246. END;
  8247. RETURN result
  8248. END IsCharacterValue;
  8249. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8250. VAR result: BOOLEAN;
  8251. BEGIN
  8252. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8253. value := x.resolved(SyntaxTree.BooleanValue).value;
  8254. result := TRUE
  8255. ELSE
  8256. result := FALSE
  8257. END;
  8258. RETURN result
  8259. END IsBooleanValue;
  8260. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8261. VAR result: BOOLEAN;
  8262. BEGIN
  8263. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8264. value := x.resolved(SyntaxTree.SetValue).value;
  8265. result := TRUE
  8266. ELSE
  8267. result := FALSE
  8268. END;
  8269. RETURN result
  8270. END IsSetValue;
  8271. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8272. VAR result: BOOLEAN;
  8273. BEGIN
  8274. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8275. value := x.resolved(SyntaxTree.StringValue).value;
  8276. result := TRUE
  8277. ELSE
  8278. result := FALSE
  8279. END;
  8280. RETURN result
  8281. END IsStringValue;
  8282. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8283. BEGIN
  8284. x := x.resolved;
  8285. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8286. END Indexable;
  8287. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8288. BEGIN
  8289. RETURN t1.SameType(t2.resolved);
  8290. END SameType;
  8291. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8292. BEGIN
  8293. IF t IS SyntaxTree.MathArrayType THEN
  8294. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8295. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8296. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8297. DEC(max);
  8298. END;
  8299. ELSIF t IS SyntaxTree.ArrayType THEN
  8300. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8301. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8302. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8303. END;
  8304. END;
  8305. RETURN t;
  8306. END ArrayBase;
  8307. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8308. BEGIN
  8309. type := type.resolved;
  8310. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8311. base := type(SyntaxTree.ArrayType).arrayBase;
  8312. RETURN TRUE;
  8313. END;
  8314. RETURN FALSE;
  8315. END IsOpenArray;
  8316. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8317. BEGIN
  8318. type := type.resolved;
  8319. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8320. base := type(SyntaxTree.ArrayType).arrayBase;
  8321. dim := type(SyntaxTree.ArrayType).staticLength;
  8322. RETURN TRUE
  8323. ELSE
  8324. RETURN FALSE
  8325. END;
  8326. END IsStaticArray;
  8327. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8328. BEGIN
  8329. type := type.resolved;
  8330. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8331. base := type(SyntaxTree.ArrayType).arrayBase;
  8332. RETURN TRUE
  8333. ELSE
  8334. RETURN FALSE
  8335. END;
  8336. END IsDynamicArray;
  8337. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8338. VAR i: LONGINT;
  8339. BEGIN
  8340. i := 0;
  8341. t := t.resolved;
  8342. IF t IS SyntaxTree.MathArrayType THEN
  8343. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8344. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8345. END;
  8346. ELSIF t IS SyntaxTree.ArrayType THEN
  8347. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8348. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8349. END;
  8350. END;
  8351. RETURN i
  8352. END Dimension;
  8353. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8354. BEGIN
  8355. RETURN expression.assignable;
  8356. END IsVariable;
  8357. PROCEDURE IsReferenceType*(kind: LONGINT; type: SyntaxTree.Type): BOOLEAN;
  8358. BEGIN
  8359. type := type.resolved;
  8360. RETURN (kind = SyntaxTree.VarParameter) OR (kind = SyntaxTree.ConstParameter) & ((type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType));
  8361. END IsReferenceType;
  8362. PROCEDURE IsReference*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8363. BEGIN
  8364. WITH symbol:
  8365. SyntaxTree.Parameter DO
  8366. RETURN IsReferenceType(symbol.kind, symbol.type);
  8367. | SyntaxTree.Variable DO
  8368. RETURN IsReferenceType(symbol.kind, symbol.type);
  8369. ELSE
  8370. RETURN FALSE
  8371. END;
  8372. END IsReference;
  8373. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8374. VAR result: BOOLEAN;
  8375. BEGIN
  8376. IF type = NIL THEN result := FALSE
  8377. ELSE
  8378. type := type.resolved;
  8379. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8380. END;
  8381. RETURN result
  8382. END IsPointerType;
  8383. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8384. VAR result: BOOLEAN;
  8385. BEGIN
  8386. IF type = NIL THEN result := FALSE
  8387. ELSE
  8388. type := type.resolved;
  8389. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  8390. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8391. & type(SyntaxTree.MathArrayType).isUnsafe;
  8392. END;
  8393. RETURN result
  8394. END IsUnsafePointer;
  8395. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8396. BEGIN
  8397. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8398. END IsDisposable;
  8399. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8400. VAR result: BOOLEAN;
  8401. BEGIN
  8402. IF type = NIL THEN result := FALSE
  8403. ELSE
  8404. type := type.resolved;
  8405. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8406. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8407. result := result OR (type IS SyntaxTree.ObjectType);
  8408. END;
  8409. RETURN result
  8410. END IsPointerToRecord;
  8411. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8412. VAR result: BOOLEAN;
  8413. BEGIN
  8414. IF type = NIL THEN result := FALSE
  8415. ELSE
  8416. type := type.resolved;
  8417. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8418. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8419. ;
  8420. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8421. result := result OR (type IS SyntaxTree.ObjectType);
  8422. END;
  8423. RETURN result
  8424. END IsPointerToObject;
  8425. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8426. BEGIN
  8427. IF type # NIL THEN
  8428. RETURN type.resolved.hasPointers
  8429. ELSE
  8430. RETURN FALSE
  8431. END;
  8432. END ContainsPointer;
  8433. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8434. BEGIN
  8435. IF type = NIL THEN RETURN FALSE END;
  8436. type := type.resolved;
  8437. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8438. END IsStringType;
  8439. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8440. BEGIN
  8441. IF type = NIL THEN RETURN FALSE END;
  8442. type := type.resolved;
  8443. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8444. END IsCharacterType;
  8445. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8446. BEGIN
  8447. IF type = NIL THEN RETURN FALSE END;
  8448. type := type.resolved;
  8449. RETURN (type IS SyntaxTree.EnumerationType)
  8450. END IsEnumerationType;
  8451. (** cf. section "Type extension (base type)" in the language report **)
  8452. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8453. VAR result: BOOLEAN;
  8454. BEGIN
  8455. ASSERT(base # NIL); ASSERT(extension # NIL);
  8456. base := base.resolved; extension := extension.resolved;
  8457. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8458. result := TRUE;
  8459. ELSE
  8460. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8461. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8462. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8463. END;
  8464. WHILE (extension # NIL) & (extension # base) DO
  8465. IF extension IS SyntaxTree.RecordType THEN
  8466. extension := extension(SyntaxTree.RecordType).baseType;
  8467. IF (extension # NIL) THEN extension := extension.resolved END;
  8468. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8469. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8470. END;
  8471. ELSE extension := NIL;
  8472. END;
  8473. END;
  8474. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8475. END;
  8476. RETURN result
  8477. END IsTypeExtension;
  8478. (** check if base is the base enumeration type of extension **)
  8479. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8480. BEGIN
  8481. base := base.resolved; extension := extension.resolved;
  8482. WHILE (extension # NIL) & (extension # base) DO
  8483. IF extension IS SyntaxTree.EnumerationType THEN
  8484. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8485. IF extension # NIL THEN extension := extension.resolved END;
  8486. ELSE
  8487. extension := NIL
  8488. END;
  8489. END;
  8490. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8491. END IsEnumerationExtension;
  8492. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8493. BEGIN
  8494. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8495. RETURN TRUE
  8496. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8497. RETURN TRUE
  8498. ELSIF expression IS SyntaxTree.InlineCallDesignator THEN
  8499. RETURN TRUE
  8500. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8501. RETURN TRUE
  8502. ELSE
  8503. RETURN FALSE
  8504. END
  8505. END IsCallable;
  8506. (** compute and return the distance of two record types
  8507. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8508. **)
  8509. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8510. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8511. BEGIN
  8512. i := 0;
  8513. WHILE (from # NIL) & (from # to) DO
  8514. baseType := from.baseType;
  8515. IF (baseType # NIL) THEN
  8516. baseType := baseType.resolved;
  8517. IF baseType IS SyntaxTree.PointerType THEN
  8518. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8519. END;
  8520. IF baseType IS SyntaxTree.RecordType THEN
  8521. from := baseType(SyntaxTree.RecordType);
  8522. ELSE
  8523. from := NIL;
  8524. END;
  8525. ELSE
  8526. from := NIL
  8527. END;
  8528. INC(i)
  8529. END;
  8530. IF from = NIL THEN i := Infinity END;
  8531. RETURN i
  8532. END RecordTypeDistance;
  8533. (** compute and return the distance of two pointer types **)
  8534. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8535. BEGIN
  8536. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8537. RETURN Infinity;
  8538. ELSE
  8539. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8540. END;
  8541. END PointerTypeDistance;
  8542. (** check if expression contains a symbol designator pointing to a type declaration.
  8543. - if so then enter type declaration into typeDeclaration and return true else return false
  8544. **)
  8545. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8546. VAR result: BOOLEAN;
  8547. BEGIN
  8548. result := FALSE;
  8549. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8550. result := TRUE;
  8551. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8552. END;
  8553. RETURN result
  8554. END IsTypeDesignator;
  8555. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8556. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8557. VAR result: BOOLEAN;
  8558. BEGIN
  8559. type := type.resolved;
  8560. IF type IS SyntaxTree.PointerType THEN
  8561. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8562. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8563. result := TRUE
  8564. ELSE
  8565. result := type IS SyntaxTree.RecordType
  8566. END;
  8567. RETURN result
  8568. END IsExtensibleType;
  8569. PROCEDURE IsIndexOperator*(expression: SyntaxTree.Expression): BOOLEAN;
  8570. VAR left: SyntaxTree.Expression; symbol: SyntaxTree.Symbol;
  8571. BEGIN
  8572. WITH expression: SyntaxTree.ProcedureCallDesignator DO
  8573. left := expression.left;
  8574. WITH left: SyntaxTree.SymbolDesignator DO
  8575. symbol := left.symbol;
  8576. WITH symbol: SyntaxTree.Operator DO
  8577. RETURN symbol.name = "[]";
  8578. ELSE
  8579. RETURN FALSE
  8580. END;
  8581. ELSE
  8582. RETURN FALSE
  8583. END;
  8584. ELSE
  8585. RETURN FALSE
  8586. END;
  8587. END IsIndexOperator;
  8588. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8589. BEGIN
  8590. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8591. (d IS SyntaxTree.SymbolDesignator) &
  8592. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8593. OR
  8594. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8595. END IsUnextensibleRecord;
  8596. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8597. BEGIN
  8598. IF IsUnextensibleRecord(d) THEN
  8599. RETURN FALSE
  8600. ELSE RETURN IsExtensibleType(d.type.resolved)
  8601. END;
  8602. END IsExtensibleDesignator;
  8603. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8604. BEGIN
  8605. type := type.resolved;
  8606. IF (type IS SyntaxTree.PointerType) THEN
  8607. RETURN TRUE
  8608. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8609. RETURN TRUE
  8610. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8611. RETURN TRUE
  8612. ELSIF (type IS SyntaxTree.BasicType) THEN
  8613. RETURN TRUE
  8614. END;
  8615. RETURN FALSE
  8616. END IsBasicType;
  8617. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8618. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8619. BEGIN
  8620. baseType := record.baseType;
  8621. IF (baseType # NIL) THEN
  8622. baseType := baseType.resolved;
  8623. IF (baseType IS SyntaxTree.PointerType) THEN
  8624. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8625. END;
  8626. END;
  8627. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8628. recordType := baseType(SyntaxTree.RecordType);
  8629. ELSE
  8630. recordType := NIL;
  8631. END;
  8632. RETURN recordType
  8633. END RecordBase;
  8634. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8635. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8636. BEGIN
  8637. baseRecord := RecordBase(scope.ownerRecord);
  8638. IF baseRecord = NIL THEN RETURN NIL END;
  8639. scope := baseRecord.recordScope;
  8640. procedureType := procedure.type.resolved;
  8641. IF procedure IS SyntaxTree.Operator THEN
  8642. operator := scope.firstOperator;
  8643. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8644. (*
  8645. Printout.Info("not same ",procedureType);
  8646. Printout.Info("with ",operator.type);
  8647. *)
  8648. operator := operator.nextOperator;
  8649. END;
  8650. super := operator;
  8651. ELSE
  8652. super := scope.firstProcedure;
  8653. WHILE (super # NIL) & (super.name # procedure.name) DO
  8654. super := super.nextProcedure;
  8655. END;
  8656. END;
  8657. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8658. RETURN super
  8659. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8660. RETURN super
  8661. ELSE
  8662. RETURN FindSuperProcedure(scope,procedure);
  8663. END;
  8664. END FindSuperProcedure;
  8665. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8666. VAR procedure: SyntaxTree.Procedure;
  8667. BEGIN
  8668. procedure := record.recordScope.constructor;
  8669. IF procedure = NIL THEN
  8670. record := RecordBase(record);
  8671. IF record # NIL THEN
  8672. procedure := GetConstructor(record)
  8673. END;
  8674. END;
  8675. RETURN procedure;
  8676. END GetConstructor;
  8677. (* enter a case into a list of cases in a sorted way and check for collision *)
  8678. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  8679. VAR prev,this,new: SyntaxTree.CaseConstant;
  8680. BEGIN
  8681. this := root;
  8682. prev := NIL;
  8683. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8684. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8685. RETURN FALSE
  8686. ELSE
  8687. IF (this # NIL) & (this.min = max+1) THEN
  8688. this.min := min
  8689. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8690. prev.max := min
  8691. ELSE
  8692. NEW(new); new.min := min; new.max := max;
  8693. new.next := this;
  8694. IF prev = NIL THEN
  8695. root := new;
  8696. ELSE
  8697. prev.next := new
  8698. END
  8699. END;
  8700. RETURN TRUE
  8701. END;
  8702. END EnterCase;
  8703. (** generate and return a new checker object, errors are entered into diagnostics **)
  8704. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  8705. VAR checker: Checker;
  8706. BEGIN
  8707. NEW(checker, diagnostics,verboseErrorMessage,cooperative,system,symbolFileFormat,importCache,backend);
  8708. RETURN checker
  8709. END NewChecker;
  8710. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8711. VAR warnings: Warnings;
  8712. BEGIN
  8713. NEW(warnings, diagnostics); RETURN warnings;
  8714. END NewWarnings;
  8715. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8716. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8717. END IsRangeType;
  8718. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8720. END IsMathArrayType;
  8721. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8722. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8723. END IsArrayType;
  8724. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8725. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8726. END IsComplexType;
  8727. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  8728. VAR
  8729. result: BOOLEAN;
  8730. rangeExpression: SyntaxTree.RangeExpression;
  8731. BEGIN
  8732. IF x IS SyntaxTree.RangeExpression THEN
  8733. rangeExpression := x(SyntaxTree.RangeExpression);
  8734. result := TRUE;
  8735. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8736. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  8737. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  8738. ELSE
  8739. result := FALSE
  8740. END;
  8741. RETURN result
  8742. END IsStaticRange;
  8743. (** whether a type is a math array of tensor form **)
  8744. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  8745. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8746. END IsTensor;
  8747. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  8748. BEGIN
  8749. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  8750. length := type(SyntaxTree.MathArrayType).staticLength;
  8751. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  8752. RETURN TRUE
  8753. ELSE
  8754. RETURN FALSE
  8755. END;
  8756. END IsStaticMathArray;
  8757. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8758. BEGIN
  8759. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  8760. END SymbolHasAddress;
  8761. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  8762. BEGIN
  8763. RETURN
  8764. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  8765. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  8766. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  8767. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  8768. ;
  8769. END HasAddress;
  8770. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  8771. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  8772. BEGIN
  8773. IF (e IS SyntaxTree.Designator) THEN
  8774. d := e(SyntaxTree.Designator);
  8775. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  8776. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  8777. e := d.left;
  8778. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  8779. END;
  8780. IF d # NIL THEN
  8781. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  8782. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  8783. END;
  8784. END;
  8785. RETURN FALSE;
  8786. END IsLocalVariable;
  8787. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8788. BEGIN
  8789. IF procedure.scope IS SyntaxTree.RecordScope THEN
  8790. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  8791. ELSE
  8792. RETURN TRUE
  8793. END;
  8794. END IsStaticProcedure;
  8795. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8796. CONST OptimizeMethodTable = FALSE;
  8797. BEGIN
  8798. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  8799. END InMethodTable;
  8800. PROCEDURE CanPassInRegister*(system: Global.System; type: SyntaxTree.Type): BOOLEAN;
  8801. BEGIN
  8802. type := type.resolved;
  8803. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  8804. RETURN TRUE
  8805. ELSIF system.CanPassInRegister # NIL THEN
  8806. RETURN system.CanPassInRegister(type);
  8807. ELSE
  8808. RETURN FALSE
  8809. END;
  8810. END CanPassInRegister;
  8811. PROCEDURE ReturnedAsParameter*(system: Global.System; type: SyntaxTree.Type): BOOLEAN;
  8812. BEGIN
  8813. IF type = NIL THEN RETURN FALSE
  8814. ELSE
  8815. type := type.resolved;
  8816. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  8817. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) & ~CanPassInRegister(system,type);
  8818. END
  8819. END ReturnedAsParameter;
  8820. PROCEDURE StructuredReturnType*(system: Global.System; procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  8821. BEGIN
  8822. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(system,procedureType.returnType);
  8823. END StructuredReturnType;
  8824. END FoxSemanticChecker.
  8825. System.FreeDownTo FoxSemanticChecker ~
  8826. System.FreeDownTo FoxIntermediateBackend ~