FoxSemanticChecker.Mod 374 KB

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