FoxSemanticChecker.Mod 386 KB

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