FoxSemanticChecker.Mod 386 KB

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