FoxSemanticChecker.Mod 388 KB

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