FoxSemanticChecker.Mod 391 KB

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