FoxSemanticChecker.Mod 395 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. replacements*: Replacement;
  93. cellsAreObjects: BOOLEAN;
  94. variableAccessed: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. error := FALSE;
  103. NEW(typeFixes);
  104. NEW(pointerFixes);
  105. resolvedType := NIL;
  106. resolvedExpression := NIL;
  107. resolvedStatement := NIL;
  108. currentScope := NIL;
  109. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  110. SELF.importCache := importCache;
  111. arrayBaseImported := FALSE;
  112. SELF.VerboseErrorMessage := verboseErrorMessage;
  113. global := NIL;
  114. phase := UndefinedPhase;
  115. currentIsRealtime := FALSE;
  116. currentIsUnreachable := FALSE;
  117. currentIsCellNet := FALSE;
  118. currentIsBodyProcedure := FALSE;
  119. currentIsExclusive := FALSE;
  120. withEntries := NIL;
  121. SELF.cellsAreObjects := system.cellsAreObjects;
  122. END InitChecker;
  123. (** report error **)
  124. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  125. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  126. BEGIN
  127. IF diagnostics # NIL THEN
  128. Basic.GetErrorMessage(code,message,errorMessage);
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  132. END;
  133. error := TRUE;
  134. END Error;
  135. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  136. VAR errModule: SyntaxTree.Module;
  137. BEGIN
  138. IF diagnostics # NIL THEN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  141. END;
  142. END Warning;
  143. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  144. VAR errorMessage: ARRAY 256 OF CHAR;
  145. BEGIN
  146. IF diagnostics # NIL THEN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  149. END;
  150. error := TRUE;
  151. END ErrorSS;
  152. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  153. VAR msg, msg2: ARRAY 256 OF CHAR;
  154. BEGIN
  155. IF diagnostics # NIL THEN
  156. COPY(msg1, msg);
  157. Strings.Append(msg, " = ");
  158. Basic.GetString(s, msg2);
  159. Strings.Append(msg, msg2);
  160. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  161. END;
  162. END InfoSS;
  163. (*** symbol lookup ***)
  164. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  165. **)
  166. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  167. VAR
  168. scope,baseScope: SyntaxTree.Scope;
  169. symbol, s: SyntaxTree.Symbol;
  170. ownerRecord,base: SyntaxTree.RecordType;
  171. BEGIN
  172. scope := inScope;
  173. symbol := NIL;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. s := NIL;
  177. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  178. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  179. END;
  180. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  181. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  182. ELSE
  183. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  184. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  185. symbol.MarkUsed;
  186. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  187. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  188. base := RecordBase(ownerRecord);
  189. IF (base # NIL) THEN
  190. baseScope := base.recordScope;
  191. symbol := Find(baseScope,name,FALSE);
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. ELSE
  196. symbol := NIL;
  197. END;
  198. END;
  199. END;
  200. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  201. END;
  202. IF (symbol # NIL) THEN
  203. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  204. ASSERT(phase = DeclarationPhase);
  205. ResolveSymbol(symbol)
  206. END;
  207. symbol.MarkUsed;
  208. END;
  209. RETURN symbol
  210. END Find;
  211. (*** types ***)
  212. (** find type declaration with name qualifiedIdentifier and return resolved type
  213. - check qualified identifier prefix, set scope to module scope if appropriate
  214. - check suffix in scope
  215. **)
  216. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  217. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  218. BEGIN
  219. result := NIL;
  220. prevScope := currentScope;
  221. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  222. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  223. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  224. IF symbol(SyntaxTree.Import).module = NIL THEN
  225. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  226. result := SyntaxTree.invalidType;
  227. symbol := NIL;
  228. ELSE
  229. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  230. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  231. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  232. IF VerboseErrorMessage THEN
  233. Printout.Info("scope", currentScope);
  234. Printout.Info("symbol", symbol);
  235. END;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  237. END;
  238. END;
  239. ELSE
  240. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  241. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  242. symbol := NIL;
  243. END;
  244. ELSE
  245. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  246. IF symbol = NIL THEN
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  248. IF VerboseErrorMessage THEN
  249. Printout.Info("Qualident",qualifiedIdentifier);
  250. Printout.Info("in scope",currentScope) ;
  251. END;
  252. END;
  253. END;
  254. IF symbol = NIL THEN (* error already handled *)
  255. typeDeclaration := NIL;
  256. result := SyntaxTree.invalidType;
  257. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  258. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSE
  262. currentScope := symbol.scope;
  263. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  264. result := ResolveType(typeDeclaration.declaredType);
  265. symbol.MarkUsed;
  266. ASSERT(result # NIL);
  267. END;
  268. currentScope := prevScope;
  269. RETURN result
  270. END ResolveNamedType;
  271. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  272. If node is currently being resolved then emit a cyclic definition error.
  273. Return TRUE only if node is fully resolved.
  274. **)
  275. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  276. VAR result: BOOLEAN;
  277. BEGIN
  278. IF SyntaxTree.Resolved IN x.state THEN
  279. result := FALSE
  280. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  281. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  282. result := FALSE;
  283. ELSE
  284. result := TRUE;
  285. x.SetState(SyntaxTree.BeingResolved)
  286. END;
  287. RETURN result
  288. END TypeNeedsResolution;
  289. (** Return invalid type if x is currently being resolved, return x otherwise**)
  290. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  291. BEGIN
  292. IF SyntaxTree.Resolved IN x.state THEN
  293. RETURN x
  294. ELSE
  295. RETURN SyntaxTree.invalidType
  296. END;
  297. END ResolvedType;
  298. PROCEDURE VisitType(x: SyntaxTree.Type);
  299. BEGIN
  300. ASSERT(x = SyntaxTree.invalidType);
  301. END VisitType;
  302. (** resolve basic type **)
  303. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  304. BEGIN
  305. IF TypeNeedsResolution(x) THEN
  306. x.SetState(SyntaxTree.Resolved);
  307. END;
  308. resolvedType := ResolvedType(x)
  309. END VisitBasicType;
  310. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitByteType;
  314. (** resolve character type **)
  315. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitCharacterType;
  319. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitBooleanType;
  323. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitSetType;
  327. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitAddressType;
  331. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitSizeType;
  335. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitAnyType;
  339. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitObjectType;
  343. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitNilType;
  347. (** resolve integer type **)
  348. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitIntegerType;
  352. (** resolve real type **)
  353. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitFloatType;
  357. (** resolve complex type **)
  358. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  359. BEGIN
  360. VisitBasicType(x);
  361. END VisitComplexType;
  362. (**
  363. resolve string type: nothing to be done
  364. **)
  365. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  366. BEGIN
  367. IF TypeNeedsResolution(x) THEN
  368. x.SetState(SyntaxTree.Resolved);
  369. END;
  370. resolvedType := ResolvedType(x)
  371. END VisitStringType;
  372. (**
  373. check enumeration scope: enter symbols and check for duplicate names
  374. **)
  375. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  376. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  377. BEGIN
  378. prevScope := currentScope;
  379. currentScope := x;
  380. e := x.firstConstant;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  408. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: LONGINT;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. x.SetArrayBase(pointerType);
  479. END;
  480. IF x.length # NIL THEN
  481. variableAccessed := FALSE;
  482. e := ResolveExpression(x.length);
  483. IF (e.resolved = NIL) THEN
  484. IF variableAccessed THEN
  485. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  486. END;
  487. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  488. ELSE
  489. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  490. END;
  491. END;
  492. IF arrayBase IS SyntaxTree.ArrayType THEN
  493. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  494. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  495. END;
  496. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  497. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  498. END;
  499. x.SetHasPointers(arrayBase.hasPointers);
  500. x.SetState(SyntaxTree.Resolved);
  501. END;
  502. resolvedType := ResolvedType(x);
  503. END VisitArrayType;
  504. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  505. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  506. BEGIN
  507. module := currentScope.ownerModule;
  508. IF module.name=name THEN
  509. (* do nothing *)
  510. ELSE
  511. moduleScope := module.moduleScope;
  512. import := moduleScope.FindImport(name);
  513. IF import = NIL THEN
  514. import := SyntaxTree.NewImport(position,name,name,TRUE);
  515. moduleScope.AddImport(import);
  516. Register(import,moduleScope,FALSE);
  517. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  518. VisitImport(import);
  519. ELSIF import.direct=FALSE THEN
  520. import.SetScope(module.moduleScope);
  521. import.SetDirect(TRUE);
  522. IF moduleScope.FindSymbol(import.name) = NIL THEN
  523. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  524. duplicate.SetContext(import.context);
  525. duplicate.SetModule(import.module);
  526. Register(duplicate,moduleScope,TRUE);
  527. VisitImport(duplicate);
  528. END;
  529. END;
  530. import.MarkUsed
  531. END;
  532. END ImportModule;
  533. (**
  534. resolve math array type
  535. - check base type
  536. - open math array of array forbidden
  537. - math array of tensor forbidden
  538. - static array of open array forbidden
  539. **)
  540. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  541. VAR arrayBase: SyntaxTree.Type;
  542. BEGIN
  543. IF TypeNeedsResolution(x) THEN
  544. x.SetArrayBase(ResolveType(x.arrayBase));
  545. IF x.length # NIL THEN
  546. x.SetLength(ConstantIntegerGeq0(x.length));
  547. END;
  548. arrayBase := x.arrayBase;
  549. IF arrayBase # NIL THEN
  550. arrayBase := arrayBase.resolved;
  551. IF arrayBase = SyntaxTree.invalidType THEN
  552. (* error already handled *)
  553. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  554. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  555. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  556. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  557. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  558. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  559. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  560. END;
  561. END;
  562. IF x.form = SyntaxTree.Static THEN
  563. x.SetIncrement(system.SizeOf(arrayBase));
  564. END;
  565. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  566. END;
  567. x.SetState(SyntaxTree.Resolved);
  568. END;
  569. resolvedType := ResolvedType(x);
  570. END VisitMathArrayType;
  571. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  572. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  573. (1) Rec = RECORD ... END; Ptr <---> Rec
  574. Ptr = POINTER TO Rec; ^ |
  575. | |
  576. TypeDesc TypeDesc
  577. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  578. ^ /
  579. | /
  580. TypeDesc <-- /
  581. *)
  582. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  583. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  584. BEGIN
  585. Strings.IntToStr(x.position,number);
  586. COPY(prefix,name);
  587. Strings.Append(name,"@");
  588. Strings.Append(name,number);
  589. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  590. typeDeclaration.SetDeclaredType(x);
  591. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  592. x.SetTypeDeclaration(typeDeclaration);
  593. currentScope.AddTypeDeclaration(typeDeclaration);
  594. typeDeclaration.SetScope(currentScope);
  595. END AnonymousTypeDeclaration;
  596. (**
  597. deferred pointer type resolving
  598. - resolve base type
  599. - check that base type is a record or array type
  600. - if error then set base type to invalid type
  601. **)
  602. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  603. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  604. BEGIN
  605. ASSERT(type.pointerBase # NIL);
  606. position := type.pointerBase.position;
  607. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  608. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  609. (* not for pointers, a type is needed for the records only
  610. IF type.typeDeclaration = NIL THEN
  611. AnonymousTypeDeclaration(type);
  612. END;
  613. *)
  614. END;
  615. resolved := ResolveType(type.pointerBase);
  616. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  617. type.SetPointerBase(resolved);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  619. recordType := resolved.resolved(SyntaxTree.RecordType);
  620. IF recordType.isObject & (recordType.baseType # NIL) THEN
  621. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  622. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  623. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  624. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  625. END;
  626. END;
  627. END;
  628. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  629. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  630. END;
  631. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  632. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  633. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  634. ELS
  635. *)
  636. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  638. END;
  639. END;
  640. ELSE
  641. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  642. type.SetPointerBase(SyntaxTree.invalidType)
  643. END
  644. END FixPointerType;
  645. (**
  646. resolve pointer type
  647. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  648. **)
  649. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  650. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  651. modifiers: SyntaxTree.Modifier; position: LONGINT;
  652. BEGIN
  653. IF TypeNeedsResolution(x) THEN
  654. modifiers := x.modifiers;
  655. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  656. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  657. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  658. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  659. (* inheritance cycle check
  660. example:
  661. A=POINTER TO RECORD(B) END;
  662. B=POINTER TO RECORD(A) END;
  663. *)
  664. IF x.pointerBase IS SyntaxTree.RecordType THEN
  665. recordType := x.pointerBase(SyntaxTree.RecordType);
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  685. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  686. x.SetSize(value)
  687. ELSE
  688. x.SetSize(system.SizeOf(system.longintType));
  689. END;
  690. x.SetState(SyntaxTree.Resolved);
  691. END;
  692. resolvedType := ResolvedType(x)
  693. END VisitPortType;
  694. (**
  695. deferred procedure type resolving
  696. - resolve return type
  697. - traverse and resolve parameters
  698. **)
  699. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  700. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  701. BEGIN
  702. resolved := ResolveType(procedureType.returnType);
  703. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  704. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  705. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  706. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  707. END;
  708. procedureType.SetReturnType(resolved);
  709. IF (resolved # NIL) THEN
  710. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  711. parameter.SetType(procedureType.returnType);
  712. parameter.SetAccess(SyntaxTree.Hidden);
  713. parameter.SetUntraced(procedureType.hasUntracedReturn);
  714. VisitParameter(parameter);
  715. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  716. END;
  717. (* process parameters *)
  718. parameter :=procedureType.firstParameter;
  719. WHILE (parameter # NIL) DO
  720. VisitParameter(parameter);
  721. parameter := parameter.nextParameter;
  722. END;
  723. END FixProcedureType;
  724. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  725. VAR prev,this: SyntaxTree.Modifier;
  726. BEGIN
  727. this := modifiers;prev := NIL;
  728. WHILE (this # NIL) & (this.identifier # name) DO
  729. prev := this; this := this.nextModifier;
  730. END;
  731. IF this # NIL THEN
  732. IF this.expression # NIL THEN
  733. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  734. END;
  735. this.Resolved;
  736. position := this.position;
  737. RETURN TRUE
  738. ELSE
  739. RETURN FALSE
  740. END;
  741. END HasFlag;
  742. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  743. VAR prev,this: SyntaxTree.Modifier;
  744. BEGIN
  745. this := modifiers;prev := NIL;
  746. WHILE (this # NIL) & (this.identifier # name) DO
  747. prev := this; this := this.nextModifier;
  748. END;
  749. IF this # NIL THEN
  750. IF this.expression = NIL THEN
  751. Error(this.position,Diagnostics.Invalid,"expected expression value");
  752. ELSE
  753. this.SetExpression(ConstantExpression(this.expression));
  754. IF CheckIntegerValue(this.expression,value) THEN END;
  755. END;
  756. this.Resolved;
  757. position := this.position;
  758. RETURN TRUE
  759. ELSE RETURN FALSE
  760. END;
  761. END HasValue;
  762. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  763. VAR this: SyntaxTree.Modifier;
  764. BEGIN
  765. this := modifiers;
  766. WHILE this # NIL DO
  767. IF ~this.resolved THEN
  768. IF checkUse THEN
  769. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  770. ELSE
  771. this.SetExpression(ResolveExpression(this.expression));
  772. this.Resolved;
  773. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  774. END;
  775. END;
  776. this := this.nextModifier
  777. END;
  778. END CheckModifiers;
  779. (**
  780. resolve procedure type
  781. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  782. **)
  783. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  784. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  785. BEGIN
  786. IF TypeNeedsResolution(procedureType) THEN
  787. modifiers := procedureType.modifiers;
  788. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  789. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  790. procedureType.SetInterrupt(TRUE);
  791. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  792. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  793. IF useDarwinCCalls THEN (*fld*)
  794. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  795. ELSE
  796. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  797. END
  798. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  799. procedureType.SetNoReturn(TRUE);
  800. END;
  801. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  802. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  803. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  804. CheckModifiers(modifiers, TRUE);
  805. modifiers := procedureType.returnTypeModifiers;
  806. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  807. CheckModifiers(modifiers, TRUE);
  808. typeFixes.Add(procedureType,currentScope);
  809. procedureType.SetHasPointers(procedureType.isDelegate);
  810. procedureType.SetState(SyntaxTree.Resolved);
  811. END;
  812. resolvedType := ResolvedType(procedureType)
  813. END VisitProcedureType;
  814. (** check and resolve record type
  815. - check base type: must be record, math array or array-structured object type
  816. - check declarations
  817. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  818. **)
  819. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  820. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  821. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  822. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  823. hasPointers: BOOLEAN;
  824. modifiers: SyntaxTree.Modifier;
  825. value: LONGINT;
  826. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  827. BEGIN
  828. type := type.resolved;
  829. IF (type IS SyntaxTree.PointerType) &
  830. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  831. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  832. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  833. RETURN TRUE
  834. ELSE
  835. RETURN FALSE
  836. END;
  837. END IsPointerToRecord;
  838. BEGIN
  839. IF TypeNeedsResolution(x) THEN
  840. hasPointers := FALSE;
  841. modifiers := x.modifiers;
  842. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  843. CheckModifiers(modifiers, TRUE);
  844. IF x.baseType # NIL THEN
  845. position := x.baseType.position;
  846. baseType := ResolveType(x.baseType);
  847. resolved := baseType.resolved;
  848. hasPointers := hasPointers OR resolved.hasPointers;
  849. IF x.isObject THEN (* object *)
  850. ASSERT(x.pointerType # NIL);
  851. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  852. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  853. baseType := NIL
  854. ELSIF IsPointerToRecord(resolved,recordType) THEN
  855. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  856. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  857. ELSE
  858. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  859. END;
  860. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  861. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  862. ELSIF IsPointerToRecord(resolved,recordType) THEN
  863. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  864. ELSIF resolved IS SyntaxTree.RecordType THEN
  865. ELSE
  866. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  867. END;
  868. ELSE
  869. IF resolved IS SyntaxTree.RecordType THEN
  870. ELSE
  871. Error(position, Diagnostics.Invalid,"record does not extend record")
  872. END;
  873. END;
  874. x.SetBaseType(baseType);
  875. IF x.Level() > 15 THEN
  876. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  877. (* note:
  878. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  879. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  880. inheritance history of a type.
  881. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  882. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  883. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  884. *)
  885. END;
  886. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  887. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  888. END;
  889. END;
  890. Declarations(x.recordScope);
  891. ResolveArrayStructure(x);
  892. (* computation of sizes and offsets skipped -> done in backend / system *)
  893. recordBase := x.GetBaseRecord();
  894. IF recordBase = NIL THEN numberMethods := 0
  895. ELSE numberMethods := recordBase.recordScope.numberMethods
  896. END;
  897. isRealtime := TRUE;
  898. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  899. WHILE symbol # NIL DO
  900. IF symbol IS SyntaxTree.Variable THEN
  901. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  902. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  903. END;
  904. IF symbol IS SyntaxTree.Procedure THEN
  905. procedure := symbol(SyntaxTree.Procedure);
  906. IF procedure.super # NIL THEN
  907. procedure.SetMethodNumber(procedure.super.methodNumber)
  908. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  909. procedure.SetMethodNumber(numberMethods);
  910. INC(numberMethods);
  911. END;
  912. END;
  913. symbol := symbol.nextSymbol;
  914. END;
  915. IF isRealtime THEN x.SetRealtime(TRUE) END;
  916. x.recordScope.SetNumberMethods(numberMethods);
  917. (* TODO: is this needed anymore? *)
  918. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  919. Error(x.position,Diagnostics.Invalid,"object extends a record")
  920. END;
  921. IF (x.typeDeclaration = NIL) THEN
  922. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  923. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  924. (*
  925. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  926. AnonymousTypeDeclaration(x,name);
  927. *)
  928. ELSE
  929. AnonymousTypeDeclaration(x,"Anonymous");
  930. END;
  931. END;
  932. x.SetHasPointers(hasPointers);
  933. x.SetState(SyntaxTree.Resolved);
  934. END;
  935. resolvedType := ResolvedType(x);
  936. END VisitRecordType;
  937. (** check and resolve cell type
  938. - check base type: must be cell
  939. - check declarations
  940. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  941. **)
  942. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  943. VAR
  944. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  945. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  946. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  947. recordBase: SyntaxTree.RecordType;
  948. numberMethods, int: LONGINT;
  949. real: LONGREAL;
  950. bool: BOOLEAN;
  951. set: SET;
  952. variable: SyntaxTree.Variable;
  953. v: SyntaxTree.Expression;
  954. str: Scanner.StringType;
  955. atype: SyntaxTree.ArrayType;
  956. prev: SyntaxTree.Scope;
  957. BEGIN
  958. IF TypeNeedsResolution(x) THEN
  959. recordBase := NIL;
  960. IF cellsAreObjects THEN
  961. IF x.baseType = NIL THEN
  962. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  963. ImportModule(qualifiedIdentifier.prefix, x.position);
  964. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  965. x.SetBaseType(ResolveType(x.baseType));
  966. recordBase := x.GetBaseRecord();
  967. IF recordBase = NIL THEN
  968. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  969. END;
  970. ELSE
  971. x.SetBaseType(ResolveType(x.baseType));
  972. END;
  973. ELSE
  974. x.SetBaseType(ResolveType(x.baseType));
  975. END;
  976. IF recordBase = NIL THEN numberMethods := 0
  977. ELSE numberMethods := recordBase.recordScope.numberMethods
  978. END;
  979. modifier := x.modifiers;
  980. (*IF ~x.isCellNet THEN*)
  981. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  982. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  983. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  984. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  985. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  986. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  987. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  988. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  989. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  990. symbol := system.activeCellsCapabilities;
  991. WHILE symbol # NIL DO
  992. IF HasFlag(modifier, symbol.name, position) THEN END;
  993. symbol := symbol.nextSymbol;
  994. END;
  995. modifier := x.modifiers;
  996. WHILE (modifier # NIL) DO
  997. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  998. IF modifier.expression # NIL THEN
  999. v := ConstantExpression(modifier.expression);
  1000. property.SetValue(v);
  1001. IF IsIntegerValue(modifier.expression, int) THEN
  1002. (*property.SetValue(modifier.expression);*)
  1003. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1004. property.SetType(system.longintType);
  1005. ELSIF IsRealValue(modifier.expression, real) THEN
  1006. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1007. property.SetType(system.longrealType);
  1008. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1009. property.SetType(system.booleanType);
  1010. ELSIF IsSetValue(modifier.expression, set) THEN
  1011. property.SetType(system.setType);
  1012. ELSIF IsStringValue(modifier.expression, str) THEN
  1013. (*property.SetValue(modifier.expression);*)
  1014. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1015. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1016. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1017. property.SetType(atype);
  1018. ELSE
  1019. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1020. END;
  1021. ELSE (* flag property *)
  1022. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1023. property.SetType(system.booleanType);
  1024. END;
  1025. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1026. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1027. x.AddProperty(property);
  1028. modifier := modifier.nextModifier;
  1029. END;
  1030. (*ELSE
  1031. (* no: this should not be allowed on cell net types or check against global scope ...
  1032. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1033. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1034. END;
  1035. END
  1036. *)
  1037. END;*)
  1038. CheckModifiers(modifier, FALSE);
  1039. Declarations(x.cellScope);
  1040. (* process parameters *)
  1041. prev := currentScope;
  1042. currentScope := x.cellScope;
  1043. parameter :=x.firstParameter;
  1044. WHILE (parameter # NIL) DO
  1045. VisitParameter(parameter);
  1046. type := parameter.type.resolved;
  1047. IF ~(type IS SyntaxTree.PortType) THEN
  1048. WHILE IsStaticArray(type, type, len) DO
  1049. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1050. END;
  1051. WHILE IsDynamicArray(type, type) DO
  1052. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1053. END;
  1054. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1055. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1056. END;
  1057. END;
  1058. parameter := parameter.nextParameter;
  1059. END;
  1060. currentScope := prev;
  1061. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1062. WHILE symbol # NIL DO
  1063. IF symbol IS SyntaxTree.Variable THEN
  1064. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1065. END;
  1066. symbol := symbol.nextSymbol;
  1067. END;
  1068. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1069. IF (x.typeDeclaration = NIL) THEN
  1070. AnonymousTypeDeclaration(x,"Anonymous");
  1071. END;
  1072. x.SetState(SyntaxTree.Resolved);
  1073. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1074. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1075. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1076. Warning(x.position, "Non-empty body for an engine?");
  1077. END;
  1078. END;
  1079. resolvedType := ResolvedType(x);
  1080. END VisitCellType;
  1081. (* check if an object is an array-structured object type
  1082. - determine the array structure
  1083. - collect operators from top to bottom in the inheritance hierarchy
  1084. - check if LEN operator is declared
  1085. - determine number of possible index operators
  1086. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1087. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1088. *)
  1089. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1090. VAR
  1091. indexOperatorCount, i: LONGINT;
  1092. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1093. isTensor: BOOLEAN;
  1094. BEGIN
  1095. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1096. (* determine array structure *)
  1097. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1098. END;
  1099. IF recordType.HasArrayStructure() THEN
  1100. (* the object is an ASOT *)
  1101. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1102. (* reset array access operators *)
  1103. arrayAccessOperators.len := NIL;
  1104. arrayAccessOperators.generalRead := NIL;
  1105. arrayAccessOperators.generalWrite := NIL;
  1106. IF isTensor THEN
  1107. (* all operators of dimensionalities 1 to max *)
  1108. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1109. ELSE
  1110. (* all operators of certain dimensionality *)
  1111. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1112. END;
  1113. NEW(arrayAccessOperators.read, indexOperatorCount);
  1114. NEW(arrayAccessOperators.write, indexOperatorCount);
  1115. FOR i := 0 TO indexOperatorCount - 1 DO
  1116. arrayAccessOperators.read[i] := NIL;
  1117. arrayAccessOperators.write[i] := NIL
  1118. END;
  1119. (* collect access operators in the record scope *)
  1120. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1121. IF arrayAccessOperators.len = NIL THEN
  1122. (* TODO: think about making this operator optional for static array structures *)
  1123. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1124. END;
  1125. (* show error messages *)
  1126. IF isTensor THEN
  1127. (* require ARRAY [*] OF RANGE *)
  1128. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1129. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1130. ELSE
  1131. (* forbid ARRAY [*] OF RANGE *)
  1132. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1133. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1134. (* require RANGE, RANGE, ... RANGE *)
  1135. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1136. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1137. END;
  1138. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1139. ELSE
  1140. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1141. IF recordType.recordScope.firstOperator # NIL THEN
  1142. RETURN;
  1143. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1144. END
  1145. END
  1146. END ResolveArrayStructure;
  1147. (** collect array access operators in a record scope **)
  1148. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1149. VAR
  1150. baseType: SyntaxTree.Type;
  1151. operator: SyntaxTree.Operator;
  1152. isReadOperator, isGeneralOperator: BOOLEAN;
  1153. indexListSize, indexListKind, hashValue: LONGINT;
  1154. BEGIN
  1155. (* if a parent record scope exists, collect the operators there first *)
  1156. baseType := recordScope.ownerRecord.baseType;
  1157. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1158. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1159. END;
  1160. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1161. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1162. END;
  1163. (* go through all operators in the current record scope *)
  1164. operator := recordScope.firstOperator;
  1165. WHILE operator # NIL DO
  1166. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1167. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1168. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1169. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1170. IF isGeneralOperator THEN
  1171. IF isReadOperator THEN
  1172. arrayAccessOperators.generalRead := operator
  1173. ELSE
  1174. arrayAccessOperators.generalWrite := operator
  1175. END
  1176. ELSE
  1177. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1178. IF isReadOperator THEN
  1179. arrayAccessOperators.read[hashValue] := operator
  1180. ELSE
  1181. arrayAccessOperators.write[hashValue] := operator
  1182. END
  1183. END
  1184. END
  1185. ELSE
  1186. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1187. END;
  1188. operator := operator.nextOperator
  1189. END
  1190. END CollectArrayAccessOperators;
  1191. (** the hash value of an index operator **)
  1192. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1193. VAR result: LONGINT;
  1194. BEGIN
  1195. IF isTensor THEN
  1196. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1197. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1198. ELSE
  1199. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1200. END
  1201. ELSE
  1202. result := indexListKind
  1203. END;
  1204. RETURN result
  1205. END IndexOperatorHash;
  1206. (** 2 to the power of exponent **)
  1207. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1208. VAR result, i: LONGINT;
  1209. BEGIN
  1210. result := 1;
  1211. FOR i := 1 TO exponent DO
  1212. result := result * 2;
  1213. END;
  1214. RETURN result
  1215. END TwoToThePowerOf;
  1216. (** check if a LEN operator has a correct signature. i.e.
  1217. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1218. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1219. **)
  1220. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1221. VAR
  1222. procedureType: SyntaxTree.ProcedureType;
  1223. returnedArrayType: SyntaxTree.MathArrayType;
  1224. result: BOOLEAN;
  1225. BEGIN
  1226. result := FALSE;
  1227. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1228. IF (procedureType.numberParameters = 0) THEN
  1229. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1230. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1231. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1232. IF returnedArrayType.form = SyntaxTree.Open THEN
  1233. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1234. result := TRUE
  1235. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1236. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1237. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1238. result := TRUE
  1239. END
  1240. END
  1241. END
  1242. END
  1243. END;
  1244. IF result THEN
  1245. (* export symbol automatically *)
  1246. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1247. ELSE
  1248. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1249. END;
  1250. RETURN result
  1251. END CheckLenOperator;
  1252. (** check if an index operator has a correct signature. i.e.
  1253. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1254. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1255. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1256. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1257. - determine if it is a read or write operator (existance of return type)
  1258. - check index parameters
  1259. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1260. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1261. [LONGINT] -> binary 0 -> 0
  1262. [RANGE] -> binary 1 -> 1
  1263. [LONGINT, LONGINT] -> binary 00 -> 0
  1264. [LONGINT, RANGE] -> binary 01 -> 1
  1265. [RANGE, LONGINT] -> binary 10 -> 2
  1266. [RANGE, RANGE] -> binary 11 -> 3
  1267. etc.
  1268. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1269. - for read operators, check if return type matches the type of data that is read
  1270. - for write operators, check if last parameter type matches the type of data that is written
  1271. **)
  1272. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1273. VAR
  1274. elementType, otherElementType, dataType: SyntaxTree.Type;
  1275. procedureType: SyntaxTree.ProcedureType;
  1276. mathArrayType: SyntaxTree.MathArrayType;
  1277. parameter: SyntaxTree.Parameter;
  1278. parameterCount, rangeCount, i: LONGINT;
  1279. hasTypeError: BOOLEAN;
  1280. BEGIN
  1281. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1282. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1283. (* determine if it is a read or write operator *)
  1284. isReadOperator := (procedureType.returnType # NIL);
  1285. IF isReadOperator THEN
  1286. indexListSize := parameterCount;
  1287. ELSE
  1288. indexListSize := parameterCount - 1;
  1289. END;
  1290. IF indexListSize < 1 THEN
  1291. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1292. RETURN FALSE
  1293. END;
  1294. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1295. (* general operator *)
  1296. isGeneralOperator := TRUE;
  1297. IF indexListSize > 1 THEN
  1298. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1299. RETURN FALSE
  1300. END;
  1301. (* ARRAY [*] OF RANGE*)
  1302. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1303. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1304. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1305. RETURN FALSE
  1306. END;
  1307. parameter := procedureType.firstParameter.nextParameter
  1308. ELSE
  1309. (* fixed-dim. operator *)
  1310. isGeneralOperator := FALSE;
  1311. (* check number of index parameters *)
  1312. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1313. (* for tensors, limited to a certain size *)
  1314. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1315. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1316. RETURN FALSE
  1317. END
  1318. ELSE
  1319. (* for non-tensors, depends on dimensionality *)
  1320. IF indexListSize # arrayStructure.Dimensionality() THEN
  1321. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1322. RETURN FALSE
  1323. END
  1324. END;
  1325. (* go through all index parameters
  1326. - count the number of ranges
  1327. - determine the index list kind number
  1328. *)
  1329. indexListKind := 0;
  1330. rangeCount := 0;
  1331. parameter := procedureType.firstParameter;
  1332. FOR i := 1 TO indexListSize DO
  1333. indexListKind := indexListKind * 2;
  1334. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1335. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1336. INC(indexListKind);
  1337. INC(rangeCount)
  1338. ELSE
  1339. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1340. RETURN FALSE
  1341. END;
  1342. parameter := parameter.nextParameter
  1343. END;
  1344. END;
  1345. (*
  1346. - for read operators: check type of last parameter
  1347. - for write operators: check return type
  1348. *)
  1349. IF isReadOperator THEN
  1350. dataType := procedureType.returnType (* the return type *)
  1351. ELSE
  1352. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1353. END;
  1354. elementType := arrayStructure.ElementType();
  1355. hasTypeError := FALSE;
  1356. IF isGeneralOperator THEN
  1357. (* ARRAY [?] OF <Element> *)
  1358. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1359. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1360. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1361. hasTypeError := TRUE
  1362. END
  1363. ELSE
  1364. hasTypeError := TRUE
  1365. END
  1366. ELSE
  1367. IF rangeCount = 0 THEN
  1368. (* <Element> *)
  1369. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1370. ELSE
  1371. (* ARRAY [*, *, ..., *] OF <Element> *)
  1372. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1373. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1374. IF mathArrayType.IsFullyDynamic() THEN
  1375. IF mathArrayType.Dimensionality() = rangeCount THEN
  1376. otherElementType := mathArrayType.ElementType();
  1377. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1378. ELSE
  1379. hasTypeError := TRUE
  1380. END
  1381. ELSE
  1382. hasTypeError := TRUE
  1383. END
  1384. ELSE
  1385. hasTypeError := TRUE
  1386. END
  1387. END
  1388. END;
  1389. IF hasTypeError THEN
  1390. IF isReadOperator THEN
  1391. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1392. ELSE
  1393. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1394. END;
  1395. RETURN FALSE
  1396. END;
  1397. (* export symbol automatically *)
  1398. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1399. RETURN TRUE
  1400. END CheckIndexOperator;
  1401. (** resolve all pending types (late resolving).
  1402. - type fixes are resolved at the end of the declaration phase
  1403. - type fixes may imply new type fixes that are also entered at the end of the list
  1404. **)
  1405. PROCEDURE FixTypes;
  1406. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1407. BEGIN
  1408. prevScope := currentScope;
  1409. p := typeFixes.Get(currentScope);
  1410. WHILE p # NIL DO
  1411. ASSERT(currentScope # NIL);
  1412. ASSERT(p IS SyntaxTree.Type);
  1413. IF p IS SyntaxTree.PointerType THEN
  1414. FixPointerType(p(SyntaxTree.PointerType))
  1415. ELSIF p IS SyntaxTree.ProcedureType THEN
  1416. FixProcedureType(p(SyntaxTree.ProcedureType))
  1417. ELSE
  1418. HALT(100);
  1419. END;
  1420. p := typeFixes.Get(currentScope);
  1421. END;
  1422. currentScope :=prevScope;
  1423. END FixTypes;
  1424. (**
  1425. resolve type x
  1426. - if x is nil then return nil
  1427. - if x cannot be resolved then the result is invalidType else the result is x
  1428. - the resolved type is entered into x.resolved
  1429. **)
  1430. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1431. VAR prev,resolved: SyntaxTree.Type;
  1432. BEGIN
  1433. prev := resolvedType;
  1434. resolvedType := SyntaxTree.invalidType;
  1435. IF x = NIL THEN resolvedType := NIL
  1436. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1437. END;
  1438. resolved := resolvedType;
  1439. resolvedType := prev;
  1440. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1441. RETURN resolved
  1442. END ResolveType;
  1443. (*** compatibility rules ***)
  1444. (**
  1445. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1446. **)
  1447. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1448. VAR result: SyntaxTree.Type;
  1449. BEGIN
  1450. result := SyntaxTree.invalidType;
  1451. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1452. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1453. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1454. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1455. ELSE result := type.resolved
  1456. END;
  1457. RETURN result
  1458. END RegularType;
  1459. (** returns signature compatibility of procedure types this and to
  1460. - if not compatible then error is reported
  1461. - compatibility means type equality
  1462. **)
  1463. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1464. VAR result: BOOLEAN;
  1465. BEGIN
  1466. result := SameType(to,this);
  1467. IF ~result THEN
  1468. Error(position,Diagnostics.Invalid,"signature incompatible");
  1469. IF VerboseErrorMessage THEN
  1470. Printout.Info("this",this);
  1471. Printout.Info("to",to);
  1472. END;
  1473. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1474. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1475. END;
  1476. RETURN result
  1477. END SignatureCompatible;
  1478. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1479. - for var parameters compatibility means same type except for
  1480. - formal is of open array of system byte
  1481. - formal is of record type
  1482. - formal is of open array type
  1483. - formal is of open math array type
  1484. - for value parameters compatibllity means assignment compatibility except for
  1485. - formal is of open array type
  1486. if compatible the return true else report error and return false
  1487. **)
  1488. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1489. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1490. BEGIN
  1491. formalType := RegularType(formal.position,formal.type);
  1492. actualType := RegularType(actual.position,actual.type);
  1493. error := FALSE;
  1494. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1496. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1497. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1498. END;
  1499. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1500. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1501. ELSIF ~IsVariable(actual) THEN
  1502. result := FALSE; error := TRUE;
  1503. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1504. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1505. ELSE
  1506. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1507. END;
  1508. IF VerboseErrorMessage THEN
  1509. Printout.Info("actual",actual);
  1510. Printout.Info("formal",formal);
  1511. END;
  1512. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1513. result := CompatibleTo(system,actualType,formalType);
  1514. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1515. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1516. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1517. IF IsArrayStructuredObjectType(actualType) THEN
  1518. actualType := MathArrayStructureOfType(actualType)
  1519. END;
  1520. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1521. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1522. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1523. END;
  1524. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1525. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1526. END;
  1527. ELSE
  1528. result := SameType(actualType,formalType)
  1529. END
  1530. ELSE
  1531. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1532. actualType := system.characterType;
  1533. END;
  1534. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1535. result := TRUE; (* special rule for WINAPI parameters *)
  1536. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1537. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1538. ELSE
  1539. result := CompatibleTo(system,actualType,formalType);
  1540. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1541. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1542. END;
  1543. END;
  1544. END;
  1545. IF ~result & ~error THEN
  1546. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1547. IF VerboseErrorMessage THEN
  1548. Printout.Info("actual",actual);
  1549. Printout.Info("formal",formal);
  1550. END;
  1551. END;
  1552. RETURN result
  1553. END ParameterCompatible;
  1554. (** check compatibility for expressions of the form left := right
  1555. - if compatible then return true else error report and return false
  1556. - check if left is variable
  1557. - check compatiblity
  1558. **)
  1559. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1560. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1561. BEGIN
  1562. result := FALSE;
  1563. leftType := RegularType(left.position,left.type);
  1564. rightType := RegularType(right.position,right.type);
  1565. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1566. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1567. END;
  1568. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1569. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1570. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1571. (* error already handled *)
  1572. result := TRUE;
  1573. ELSIF ~IsVariable(left) THEN
  1574. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1575. IF VerboseErrorMessage THEN
  1576. Printout.Info("left",left);
  1577. Printout.Info("right",right);
  1578. END;
  1579. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1580. result := TRUE;
  1581. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1582. result := TRUE
  1583. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1584. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1585. IF VerboseErrorMessage THEN
  1586. Printout.Info("left",left);
  1587. Printout.Info("right",right);
  1588. END;
  1589. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1590. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1591. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1592. ELSE
  1593. result := TRUE
  1594. END;
  1595. RETURN result
  1596. END AssignmentCompatible;
  1597. (*** values ***)
  1598. (** check and resolve integer value **)
  1599. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1600. VAR hugeint: HUGEINT;
  1601. BEGIN
  1602. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1603. value.SetType(Global.GetIntegerType(system,hugeint));
  1604. resolvedExpression := value
  1605. END VisitIntegerValue;
  1606. (** check and resolve real value **)
  1607. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1608. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1609. BEGIN
  1610. subtype := value(SyntaxTree.RealValue).subtype;
  1611. IF subtype = Scanner.Real THEN
  1612. type := system.realType
  1613. ELSIF subtype = Scanner.Longreal THEN
  1614. type := system.longrealType
  1615. ELSE
  1616. HALT(100)
  1617. END;
  1618. value.SetType(type);
  1619. resolvedExpression := value
  1620. END VisitRealValue;
  1621. (** check and resolve complex value **)
  1622. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1623. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1624. BEGIN
  1625. subtype := value(SyntaxTree.ComplexValue).subtype;
  1626. IF subtype = Scanner.Real THEN
  1627. type := system.complexType
  1628. ELSIF subtype = Scanner.Longreal THEN
  1629. type := system.longcomplexType
  1630. ELSE
  1631. HALT(100)
  1632. END;
  1633. value.SetType(type);
  1634. resolvedExpression := value
  1635. END VisitComplexValue;
  1636. (** check and resolve set value **)
  1637. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1638. BEGIN
  1639. value.SetType(system.setType);
  1640. resolvedExpression := value
  1641. END VisitSetValue;
  1642. (** check and resolve set value **)
  1643. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1644. BEGIN
  1645. value.SetType(SyntaxTree.invalidType);
  1646. resolvedExpression := value
  1647. END VisitMathArrayValue;
  1648. (** check and resolve boolean value **)
  1649. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1650. BEGIN
  1651. value.SetType(system.booleanType);
  1652. resolvedExpression := value
  1653. END VisitBooleanValue;
  1654. (** check and resolve string value **)
  1655. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1656. BEGIN
  1657. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1658. resolvedExpression := value
  1659. END VisitStringValue;
  1660. (** check and resolve character value **)
  1661. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1662. BEGIN
  1663. value.SetType(system.characterType);
  1664. resolvedExpression := value
  1665. END VisitCharacterValue;
  1666. (** check and resolve nil value **)
  1667. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1668. BEGIN
  1669. value.SetType(system.nilType);
  1670. resolvedExpression := value
  1671. END VisitNilValue;
  1672. (** check and resolve enumerator value **)
  1673. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1674. BEGIN
  1675. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1676. ASSERT(value.type # NIL);
  1677. resolvedExpression := value
  1678. END VisitEnumerationValue;
  1679. (*** expressions ***)
  1680. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1681. - check all elements on integer type
  1682. - if element range is constant, then check lower and upper bound
  1683. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1684. if an error occurs then report error and return invalidExpression
  1685. **)
  1686. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1687. VAR
  1688. i: LONGINT;
  1689. element: SyntaxTree.Expression;
  1690. constant: BOOLEAN;
  1691. elements: SyntaxTree.ExpressionList;
  1692. s: SET;
  1693. result: SyntaxTree.Expression;
  1694. value: SyntaxTree.Value;
  1695. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1696. VAR
  1697. left, right: SyntaxTree.Expression;
  1698. elementResult: SyntaxTree.Expression;
  1699. leftInteger, rightInteger, temp: LONGINT;
  1700. BEGIN
  1701. (* set context of range *)
  1702. IF element IS SyntaxTree.RangeExpression THEN
  1703. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1704. END;
  1705. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1706. IF elementResult = SyntaxTree.invalidExpression THEN
  1707. (* error already reported *)
  1708. constant := FALSE
  1709. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1710. (* the element is a range expression *)
  1711. (* extract left and right hand side of range *)
  1712. left := elementResult(SyntaxTree.RangeExpression).first;
  1713. right := elementResult(SyntaxTree.RangeExpression).last;
  1714. (* guaranteed by VisitRangeExpression: *)
  1715. ASSERT((left # NIL) & (right # NIL));
  1716. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1717. ELSE
  1718. (* the element is not a range expression *)
  1719. (* check type and add conversion if needed *)
  1720. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1721. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1722. ELSE
  1723. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1724. elementResult := SyntaxTree.invalidExpression;
  1725. constant := FALSE
  1726. END;
  1727. left := elementResult;
  1728. right := elementResult
  1729. END;
  1730. IF elementResult # SyntaxTree.invalidExpression THEN
  1731. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1732. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1733. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1734. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1735. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1736. END
  1737. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1738. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1739. ELSE
  1740. IF leftInteger > rightInteger THEN
  1741. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1742. END;
  1743. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1744. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1745. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1746. extends the range x..31 to x..63 !!!!!! *)
  1747. s := s + {leftInteger..rightInteger};
  1748. (* same but more explicit:
  1749. WHILE (leftInteger <= rightInteger) DO
  1750. INCL(s,leftInteger);
  1751. INC(leftInteger);
  1752. END;
  1753. *)
  1754. END;
  1755. ELSE
  1756. constant := FALSE;
  1757. END
  1758. END;
  1759. RETURN elementResult
  1760. END CheckElement;
  1761. BEGIN
  1762. result := set; constant := TRUE; s := {}; elements := set.elements;
  1763. IF elements # NIL THEN
  1764. FOR i := 0 TO elements.Length()-1 DO
  1765. element := elements.GetExpression(i);
  1766. element := CheckElement(element);
  1767. IF element = SyntaxTree.invalidExpression THEN
  1768. result := SyntaxTree.invalidExpression
  1769. END;
  1770. elements.SetExpression(i,element);
  1771. END;
  1772. END;
  1773. IF constant THEN
  1774. value := SyntaxTree.NewSetValue(set.position,s);
  1775. value.SetType(system.setType);
  1776. result.SetResolved(value);
  1777. END;
  1778. (* optimization possible
  1779. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1780. left this to the programmer...
  1781. *)
  1782. result.SetType(system.setType);
  1783. resolvedExpression := result;
  1784. END VisitSet;
  1785. (*
  1786. old variant: quite generic but needs better conversion handling, do this?
  1787. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1788. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1789. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1790. BEGIN
  1791. type := NIL;
  1792. numberElements := x.elements.Length();
  1793. FOR i := 0 TO numberElements-1 DO
  1794. expression := x.elements.GetExpression(i);
  1795. position := expression.position;
  1796. expression := ResolveExpression(x.elements.GetExpression(i));
  1797. x.elements.SetExpression(i,de);
  1798. IF type = NIL THEN
  1799. type := expression.type;
  1800. ELSIF CompatibleTo(system,expression.type,type) THEN
  1801. (* ok *)
  1802. ELSIF CompatibleTo(system,type,expression.type) THEN
  1803. type := expression.type
  1804. ELSE
  1805. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1806. type := SyntaxTree.invalidType;
  1807. END;
  1808. END;
  1809. isValue := TRUE;
  1810. FOR i := 0 TO numberElements-1 DO
  1811. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1812. x.elements.SetExpression(i,expression);
  1813. isValue := isValue & (expression.resolved # NIL);
  1814. END;
  1815. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1816. arrayType.SetArrayBase(type);
  1817. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1818. IF isValue THEN
  1819. value := SyntaxTree.NewMathArrayValue(position);
  1820. value.SetElements(x.elements);
  1821. x.SetResolved(value);
  1822. END;
  1823. x.SetType(arrayType);
  1824. resolvedExpression := x;
  1825. END VisitMathArrayExpression;
  1826. *)
  1827. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1828. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1829. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1830. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1831. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1832. BEGIN
  1833. numberElements := x.elements.Length();
  1834. FOR i := 0 TO numberElements-1 DO
  1835. expression := x.elements.GetExpression(i);
  1836. IF expression IS SyntaxTree.MathArrayExpression THEN
  1837. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1838. ELSE
  1839. position := expression.position;
  1840. expression := ResolveExpression(x.elements.GetExpression(i));
  1841. x.elements.SetExpression(i,expression);
  1842. IF type = NIL THEN
  1843. type := expression.type;
  1844. ELSIF CompatibleTo(system,expression.type,type) THEN
  1845. (* ok *)
  1846. ELSIF CompatibleTo(system,type,expression.type) THEN
  1847. type := expression.type
  1848. ELSE
  1849. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1850. type := SyntaxTree.invalidType;
  1851. END;
  1852. END;
  1853. END;
  1854. END RecursivelyFindType;
  1855. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1856. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1857. BEGIN
  1858. numberElements := x.elements.Length();
  1859. FOR i := 0 TO numberElements-1 DO
  1860. expression := x.elements.GetExpression(i);
  1861. IF expression IS SyntaxTree.MathArrayExpression THEN
  1862. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1863. ELSE
  1864. position := expression.position;
  1865. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1866. x.elements.SetExpression(i,expression);
  1867. isValue := isValue & (expression.resolved # NIL);
  1868. END;
  1869. END;
  1870. END RecursivelySetExpression;
  1871. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1872. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1873. arrayType: SyntaxTree.MathArrayType;
  1874. BEGIN
  1875. numberElements := x.elements.Length();
  1876. baseType := NIL;
  1877. gsize := 0;
  1878. FOR i := 0 TO numberElements-1 DO
  1879. expression := x.elements.GetExpression(i);
  1880. IF expression IS SyntaxTree.MathArrayExpression THEN
  1881. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1882. IF i=0 THEN
  1883. gsize := size;
  1884. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1885. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1886. ELSE expression.SetType(baseType)
  1887. END;
  1888. ELSIF baseType = NIL THEN baseType := type;
  1889. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1890. END;
  1891. END;
  1892. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1893. arrayType.SetArrayBase(baseType);
  1894. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1895. RETURN ResolveType(arrayType);
  1896. END RecursivelySetType;
  1897. BEGIN
  1898. type := NIL;
  1899. RecursivelyFindType(x);
  1900. isValue := TRUE;
  1901. RecursivelySetExpression(x);
  1902. arrayType := RecursivelySetType(x);
  1903. x.SetType(arrayType);
  1904. IF isValue THEN
  1905. value := SyntaxTree.NewMathArrayValue(x.position);
  1906. value.SetArray(x);
  1907. x.SetResolved(value);
  1908. value.SetType(arrayType);
  1909. END;
  1910. x.SetType(arrayType);
  1911. resolvedExpression := x;
  1912. END VisitMathArrayExpression;
  1913. (** check and resolve unary expression **)
  1914. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1915. VAR
  1916. left: SyntaxTree.Expression;
  1917. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1918. bool: BOOLEAN;
  1919. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1920. value: SyntaxTree.Value;
  1921. BEGIN
  1922. type := SyntaxTree.invalidType;
  1923. left := ResolveExpression(unaryExpression.left);
  1924. unaryExpression.SetLeft(left);
  1925. operator := unaryExpression.operator;
  1926. result := unaryExpression;
  1927. IF ~system.operatorDefined[operator] THEN
  1928. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1929. RETURN
  1930. ELSIF left.type = NIL THEN
  1931. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1932. resolvedExpression := SyntaxTree.invalidExpression;
  1933. RETURN
  1934. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1935. RETURN
  1936. END;
  1937. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1938. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1939. END;
  1940. IF operatorCall # NIL THEN
  1941. result := operatorCall;
  1942. type := operatorCall.type;
  1943. (* admissible operators
  1944. Minus number, set
  1945. Not boolean
  1946. *)
  1947. ELSE
  1948. CASE unaryExpression.operator OF
  1949. |Scanner.Minus:
  1950. IF IsIntegerType(left.type.resolved) THEN
  1951. IF left.resolved # NIL THEN
  1952. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1953. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1954. result.SetResolved(value);
  1955. type := Global.GetIntegerType(system,int);
  1956. value.SetType(type);
  1957. ELSE
  1958. type := left.type
  1959. END
  1960. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1961. IF IsRealValue(left,real) THEN
  1962. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1963. result.SetResolved(value);
  1964. type := left.type;
  1965. value.SetType(type);
  1966. ELSE
  1967. type := left.type;
  1968. END;
  1969. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1970. IF IsSetValue(left,set) THEN
  1971. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1972. result.SetResolved(value);
  1973. type := left.type;
  1974. value.SetType(type);
  1975. ELSE
  1976. type := left.type;
  1977. END;
  1978. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1979. IF IsComplexValue(left, real, imaginary) THEN
  1980. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1981. result.SetResolved(value);
  1982. type := left.type;
  1983. value.SetType(type);
  1984. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1985. ELSE
  1986. type := left.type;
  1987. END
  1988. ELSE
  1989. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1990. END;
  1991. |Scanner.Not:
  1992. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1993. IF IsBooleanValue(left,bool) THEN
  1994. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1995. result.SetResolved(value);
  1996. type := system.booleanType;
  1997. value.SetType(type);
  1998. ELSE
  1999. type := system.booleanType;
  2000. END;
  2001. ELSE
  2002. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2003. END;
  2004. |Scanner.Plus:
  2005. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2006. result := left; type := left.type;
  2007. ELSE
  2008. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2009. END;
  2010. (* ADDRESS OF *)
  2011. |Scanner.Address:
  2012. IF HasAddress(left) THEN
  2013. type := system.addressType;
  2014. ELSE
  2015. type := SyntaxTree.invalidType;
  2016. Error(left.position,Diagnostics.Invalid,"has no address");
  2017. Printout.Info("par", left);
  2018. END;
  2019. (* SIZE OF *)
  2020. |Scanner.Size:
  2021. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2022. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2023. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2024. value := SyntaxTree.NewIntegerValue(left.position, int);
  2025. result.SetResolved(value);
  2026. type := Global.GetIntegerType(system,int);
  2027. value.SetType(type)
  2028. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2029. ELSE
  2030. (* for variables, system sizeof could represent the physically occupied size
  2031. determined via the type descriptor, implement that ? *)
  2032. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2033. END
  2034. (* ALIAS OF *)
  2035. |Scanner.Alias:
  2036. type := left.type.resolved;
  2037. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2038. type := SyntaxTree.invalidType;
  2039. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2040. END;
  2041. ELSE
  2042. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2043. END;
  2044. END;
  2045. result.SetType(type);
  2046. resolvedExpression := result
  2047. END VisitUnaryExpression;
  2048. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2049. VAR
  2050. result: SyntaxTree.Expression;
  2051. array: SyntaxTree.MathArrayExpression;
  2052. value: SyntaxTree.MathArrayValue;
  2053. isValue: BOOLEAN;
  2054. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2055. BEGIN
  2056. type := type.resolved;
  2057. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2058. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2059. END;
  2060. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2061. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2062. END;
  2063. RETURN type
  2064. END BaseType;
  2065. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2066. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2067. BEGIN
  2068. numberElements := x.elements.Length();
  2069. FOR i := 0 TO numberElements-1 DO
  2070. expression := x.elements.GetExpression(i);
  2071. IF expression IS SyntaxTree.MathArrayExpression THEN
  2072. array := SyntaxTree.NewMathArrayExpression(position);
  2073. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2074. expression := array;
  2075. ELSE
  2076. position := expression.position;
  2077. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2078. isValue := isValue & (expression.resolved # NIL);
  2079. END;
  2080. to.elements.AddExpression(expression);
  2081. END;
  2082. END RecursivelyConvert;
  2083. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2084. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2085. arrayType: SyntaxTree.MathArrayType;
  2086. BEGIN
  2087. numberElements := x.elements.Length();
  2088. baseType := NIL;
  2089. gsize := 0;
  2090. FOR i := 0 TO numberElements-1 DO
  2091. expression := x.elements.GetExpression(i);
  2092. IF expression IS SyntaxTree.MathArrayExpression THEN
  2093. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2094. IF i=0 THEN
  2095. gsize := size;
  2096. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2097. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2098. ELSE expression.SetType(baseType)
  2099. END;
  2100. ELSIF baseType = NIL THEN baseType := type;
  2101. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2102. END;
  2103. END;
  2104. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2105. arrayType.SetArrayBase(baseType);
  2106. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2107. RETURN ResolveType(arrayType);
  2108. END RecursivelySetType;
  2109. BEGIN
  2110. result := SyntaxTree.invalidExpression;
  2111. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2112. result := expression (* do not convert *)
  2113. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2114. isValue := TRUE;
  2115. type := BaseType(type);
  2116. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2117. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2118. value := SyntaxTree.NewMathArrayValue(array.position);
  2119. value.SetArray(array);
  2120. value.SetType(RecursivelySetType(array));
  2121. result := value;
  2122. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2123. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2124. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2125. IF result = NIL THEN
  2126. result := SyntaxTree.invalidExpression;
  2127. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2128. IF VerboseErrorMessage THEN
  2129. Printout.Info("expression",expression);
  2130. Printout.Info("type",type);
  2131. END;
  2132. END;
  2133. END;
  2134. RETURN result
  2135. END MathArrayConversion;
  2136. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2137. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2138. BEGIN
  2139. result := expression; type := type.resolved;
  2140. IF (expression IS SyntaxTree.IntegerValue) THEN
  2141. int := expression(SyntaxTree.IntegerValue).hvalue;
  2142. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2143. int := Global.ConvertSigned(int,system.SizeOf(type));
  2144. result := SyntaxTree.NewIntegerValue(position,int);
  2145. result.SetType(type);
  2146. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2147. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2148. result := SyntaxTree.NewIntegerValue(position,int);
  2149. result.SetType(type);
  2150. ELSIF (type IS SyntaxTree.FloatType) THEN
  2151. result := SyntaxTree.NewRealValue(expression.position,int);
  2152. result.SetType(type);
  2153. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2154. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2155. result.SetType(type);
  2156. ELSIF (type IS SyntaxTree.SetType) THEN
  2157. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2158. result.SetType(type);
  2159. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2160. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2161. result.SetType(type);
  2162. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2163. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2164. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2165. END;
  2166. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2167. result.SetType(type);
  2168. ELSIF (type IS SyntaxTree.PortType) THEN
  2169. result := ConvertValue(position, expression, system.integerType);
  2170. ELSE
  2171. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2172. result := SyntaxTree.invalidExpression;
  2173. IF VerboseErrorMessage THEN
  2174. Printout.Info("expression",expression);
  2175. Printout.Info("type",type);
  2176. END;
  2177. END;
  2178. ELSIF IsRealValue(expression,real) THEN
  2179. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2180. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2181. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2182. result.SetType(type);
  2183. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2184. int := ENTIERH(real);
  2185. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2186. result.SetType(type);
  2187. ELSIF (type IS SyntaxTree.FloatType) THEN
  2188. result := SyntaxTree.NewRealValue(position,real);
  2189. result.SetType(type);
  2190. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2191. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2192. result.SetType(type);
  2193. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2194. ELSIF (type IS SyntaxTree.PortType) THEN
  2195. result := ConvertValue(position, expression, system.integerType);
  2196. ELSE
  2197. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2198. result := SyntaxTree.invalidExpression;
  2199. END
  2200. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2201. IF (type IS SyntaxTree.ComplexType) THEN
  2202. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2203. result.SetType(type);
  2204. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2205. ELSE
  2206. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2207. result := SyntaxTree.invalidExpression;
  2208. END
  2209. ELSIF IsSetValue(expression,set) THEN
  2210. IF (type IS SyntaxTree.IntegerType) THEN
  2211. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2214. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.PortType) THEN
  2217. result := ConvertValue(position, expression, system.integerType);
  2218. ELSE
  2219. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2220. result := SyntaxTree.invalidExpression;
  2221. END;
  2222. ELSIF IsStringValue(expression,string) THEN
  2223. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2224. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2225. result.SetType(type);
  2226. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2227. ELSE
  2228. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2229. result := SyntaxTree.invalidExpression;
  2230. END;
  2231. ELSIF IsCharacterValue(expression,char) THEN
  2232. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2233. string[0] := char; string[1] := 0X;
  2234. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2235. result := SyntaxTree.NewStringValue(expression.position,string);
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.ByteType) THEN
  2238. (* do not simply set the new type as this could invalidate types of constants *)
  2239. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2240. result.SetType(type)
  2241. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2242. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.SetType) THEN
  2245. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2248. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2249. result.SetType(type);
  2250. ELSIF (type IS SyntaxTree.PortType) THEN
  2251. result := ConvertValue(position, expression, system.integerType);
  2252. ELSE
  2253. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2254. result := SyntaxTree.invalidExpression;
  2255. END;
  2256. ELSIF expression IS SyntaxTree.NilValue THEN
  2257. IF type IS SyntaxTree.AddressType THEN
  2258. result := SyntaxTree.NewIntegerValue(position,0);
  2259. result.SetType(type);
  2260. ELSE
  2261. result := expression;
  2262. END;
  2263. (* nothing to be done *)
  2264. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2265. result := MathArrayConversion(position, expression,type);
  2266. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2267. int := expression(SyntaxTree.EnumerationValue).value;
  2268. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2269. int := Global.ConvertSigned(int,system.SizeOf(type));
  2270. result := SyntaxTree.NewIntegerValue(position,int);
  2271. result.SetType(type);
  2272. ELSE
  2273. result := expression;
  2274. END;
  2275. (* nothing to be done *)
  2276. ELSE
  2277. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2278. IF VerboseErrorMessage THEN
  2279. Printout.Info("expression",expression);
  2280. Printout.Info("type",type);
  2281. END;
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. RETURN result
  2285. END ConvertValue;
  2286. (**
  2287. return a conversion of an expression to a given type
  2288. - if expression is already of same type then return expression
  2289. - if incompatible conversion then report error and return invalidExpression
  2290. **)
  2291. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2292. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2293. BEGIN
  2294. type := type.resolved;
  2295. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2296. result := expression;
  2297. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2298. ELSIF expression = NIL THEN (* NIL expression *)
  2299. ELSIF expression.type = NIL THEN
  2300. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2301. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2302. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2303. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2304. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2305. ELSIF expression.resolved # NIL THEN (* value *)
  2306. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2307. IF value IS SyntaxTree.Value THEN
  2308. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2309. result.SetResolved(value(SyntaxTree.Value));
  2310. result.SetType(value.type);
  2311. ELSE
  2312. result := value
  2313. END;
  2314. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2315. expressionList := SyntaxTree.NewExpressionList();
  2316. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2317. typeDeclaration.SetDeclaredType(type);
  2318. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2319. typeSymbol.SetType(typeDeclaration.type);
  2320. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2321. expressionList.AddExpression(expression);
  2322. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2323. result.SetType(type);
  2324. ELSIF IsArrayStructuredObjectType(type) THEN
  2325. (* no type can be converted to an array-structured object type *)
  2326. HALT(100)
  2327. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2328. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2329. result := MathArrayConversion(position, expression,type);
  2330. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2331. expression := ConvertToMathArray(expression);
  2332. type := MathArrayStructureOfType(type);
  2333. result := MathArrayConversion(position, expression, type)
  2334. ELSE
  2335. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2336. END;
  2337. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2338. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2339. OR ~(type IS SyntaxTree.ArrayType) THEN
  2340. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2341. END;
  2342. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2343. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2344. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2345. (*skip, no conversion*)
  2346. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2347. (* skip, no conversion *)
  2348. ELSE
  2349. ASSERT(~(type IS SyntaxTree.RangeType));
  2350. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2351. ASSERT(type # NIL);
  2352. END;
  2353. RETURN result
  2354. END NewConversion;
  2355. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2356. BEGIN
  2357. IF CompatibleTo(system,expression.type, type) THEN
  2358. RETURN NewConversion(position, expression, type, NIL);
  2359. ELSE
  2360. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2361. RETURN SyntaxTree.invalidExpression
  2362. END;
  2363. END CompatibleConversion;
  2364. (**
  2365. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2366. **)
  2367. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2368. VAR leftType,rightType: SyntaxTree.Type;
  2369. BEGIN
  2370. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2371. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2372. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2373. (* error already handled *)
  2374. ELSE
  2375. leftType := left.type.resolved; rightType := right.type.resolved;
  2376. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2377. right := NewConversion(right.position, right, leftType, NIL);
  2378. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2379. left := NewConversion(left.position,left,rightType,NIL);
  2380. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2381. right := NewConversion(right.position, right, leftType, NIL);
  2382. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2383. left := NewConversion(left.position,left,rightType,NIL);
  2384. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2385. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2386. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2387. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2388. ELSIF
  2389. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2390. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2391. (* must be the case LONGREAL / COMPLEX ) *)
  2392. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2393. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2394. ELSE
  2395. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2396. END;
  2397. END;
  2398. END ConvertOperands;
  2399. (** find and return best operator matching to parameter list (nil, if none)
  2400. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2401. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2402. **)
  2403. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2404. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2405. identifier: SyntaxTree.Identifier;
  2406. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2407. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2408. BEGIN
  2409. operator := scope.firstOperator;
  2410. WHILE(operator # NIL) DO
  2411. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2412. procedureType := operator.type(SyntaxTree.ProcedureType);
  2413. distance := Distance(system, procedureType,actualParameters);
  2414. IF (distance < Infinity) THEN
  2415. IF returnType # NIL THEN
  2416. IF procedureType.returnType = NIL THEN
  2417. distance := Infinity
  2418. ELSE
  2419. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2420. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2421. END;
  2422. END;
  2423. END;
  2424. (*
  2425. IF distance < Infinity THEN
  2426. TRACE(distance, operator);
  2427. Printout.Info("potential operator",operator);
  2428. ELSE
  2429. Printout.Info("operator not possible",operator);
  2430. END;
  2431. *)
  2432. IF distance < bestDistance THEN
  2433. bestDistance := distance;
  2434. bestOperator := operator;
  2435. END;
  2436. END;
  2437. operator := operator.nextOperator;
  2438. END;
  2439. (*
  2440. Printout.Info("taken operator",bestOperator);
  2441. *)
  2442. END FindInScope;
  2443. BEGIN
  2444. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2445. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2446. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2447. import := currentScope.ownerModule.moduleScope.firstImport;
  2448. WHILE (bestDistance > 0) & (import # NIL) DO
  2449. IF import.module # NIL THEN
  2450. identifier := Global.GetIdentifier(operator,import.module.case);
  2451. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2452. END;
  2453. import := import.nextImport;
  2454. END;
  2455. RETURN bestOperator
  2456. END FindOperator;
  2457. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2458. BEGIN
  2459. currentScope := scope;
  2460. END SetCurrentScope;
  2461. (**
  2462. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2463. - handle LEN and DIM operator for array-structured object types
  2464. - find operator, if found then
  2465. - if in other module then add import designator
  2466. - create symbol designator for operator
  2467. - if error then return invalidExpression, if no operator then return NIL
  2468. **)
  2469. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2470. VAR
  2471. operator: SyntaxTree.Operator;
  2472. import: SyntaxTree.Import;
  2473. expression, result: SyntaxTree.Expression;
  2474. designator: SyntaxTree.Designator;
  2475. actualParameters, tempList: SyntaxTree.ExpressionList;
  2476. recordType: SyntaxTree.RecordType;
  2477. castReturnType : SyntaxTree.MathArrayType;
  2478. BEGIN
  2479. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2480. result := SyntaxTree.invalidExpression
  2481. ELSIF leftExpression = NIL THEN
  2482. result := NIL
  2483. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2484. (* LEN or DIM operator on array-structured object type *)
  2485. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2486. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2487. IF recordType.arrayAccessOperators.len = NIL THEN
  2488. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2489. result := SyntaxTree.invalidExpression
  2490. ELSE
  2491. ASSERT(leftExpression IS SyntaxTree.Designator);
  2492. designator := leftExpression(SyntaxTree.Designator);
  2493. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2494. ASSERT(expression IS SyntaxTree.Designator);
  2495. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2496. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2497. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2498. result := designator
  2499. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2500. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2501. tempList := SyntaxTree.NewExpressionList();
  2502. tempList.AddExpression(rightExpression);
  2503. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2504. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2505. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2506. tempList := SyntaxTree.NewExpressionList();
  2507. tempList.AddExpression(designator);
  2508. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2509. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2510. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2511. END
  2512. END;
  2513. ELSE
  2514. (* import OCArrayBase if needed *)
  2515. IF ~arrayBaseImported THEN
  2516. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2517. (* operators on complex numbers *)
  2518. ImportModule(Global.ArrayBaseName,position);
  2519. arrayBaseImported := TRUE;
  2520. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2521. IF op = Global.Dim THEN
  2522. (* not existing in OCArrayBase *)
  2523. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2524. (* not existing in OCArrayBase *)
  2525. ELSE
  2526. ImportModule(Global.ArrayBaseName,position);
  2527. arrayBaseImported := TRUE;
  2528. END
  2529. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2530. ImportModule(Global.ArrayBaseName,position);
  2531. arrayBaseImported := TRUE
  2532. END;
  2533. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2534. (* LEN(RANGE) *)
  2535. ImportModule(Global.ArrayBaseName,position);
  2536. arrayBaseImported := TRUE;
  2537. END;
  2538. END;
  2539. actualParameters := SyntaxTree.NewExpressionList();
  2540. actualParameters.AddExpression(leftExpression);
  2541. IF rightExpression # NIL THEN
  2542. actualParameters.AddExpression(rightExpression)
  2543. END;
  2544. operator := FindOperator(system,op,actualParameters,resultType);
  2545. IF operator # NIL THEN
  2546. designator := NIL;
  2547. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2548. import := currentScope.ownerModule.moduleScope.firstImport;
  2549. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2550. import := import.nextImport;
  2551. END;
  2552. expression := NewSymbolDesignator(position,NIL,import);
  2553. designator := expression(SyntaxTree.Designator);
  2554. END;
  2555. expression := NewSymbolDesignator(position,designator,operator);
  2556. designator := expression(SyntaxTree.Designator);
  2557. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2558. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2559. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2560. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2561. result.SetType(castReturnType);
  2562. END;
  2563. ELSE
  2564. result := NIL;
  2565. END;
  2566. END;
  2567. RETURN result
  2568. END NewOperatorCall;
  2569. (** check and resolve binary expression **)
  2570. (*! clean up *)
  2571. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2572. VAR left,right,result: SyntaxTree.Expression;
  2573. leftType, rightType: SyntaxTree.Type;
  2574. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2575. cl,cr: CHAR;
  2576. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2577. type: SyntaxTree.Type;
  2578. value: SyntaxTree.Value;
  2579. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2580. integerConstantFolding: BOOLEAN;
  2581. PROCEDURE NewBool(v: BOOLEAN);
  2582. BEGIN
  2583. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2584. value.SetType(system.booleanType);
  2585. result.SetResolved(value);
  2586. type := system.booleanType
  2587. END NewBool;
  2588. PROCEDURE NewSet(v: SET);
  2589. BEGIN
  2590. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2591. value.SetType(system.setType);
  2592. result.SetResolved(value);
  2593. type := system.setType;
  2594. END NewSet;
  2595. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2596. BEGIN
  2597. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2598. (* type cast to "larger" type only if the value is still in the range *)
  2599. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2600. value.SetType(t);
  2601. END;
  2602. result.SetResolved(value);
  2603. type := value.type;
  2604. END NewInteger;
  2605. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2606. BEGIN
  2607. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2608. value.SetType(t);
  2609. result.SetResolved(value);
  2610. type := t;
  2611. END NewReal;
  2612. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2613. BEGIN
  2614. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2615. value.SetType(t);
  2616. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2617. result.SetResolved(value);
  2618. type := t;
  2619. END NewComplex;
  2620. BEGIN
  2621. type := SyntaxTree.invalidType;
  2622. left := ResolveExpression(binaryExpression.left);
  2623. right := ResolveExpression(binaryExpression.right);
  2624. binaryExpression.SetLeft(left);
  2625. binaryExpression.SetRight(right);
  2626. result := binaryExpression;
  2627. operator := binaryExpression.operator;
  2628. IF ~system.operatorDefined[operator] THEN
  2629. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2630. result := SyntaxTree.invalidExpression;
  2631. RETURN
  2632. END;
  2633. IF left.type = NIL THEN
  2634. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2635. result := SyntaxTree.invalidExpression;
  2636. RETURN;
  2637. END;
  2638. IF right.type = NIL THEN
  2639. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2640. result := SyntaxTree.invalidExpression;
  2641. RETURN;
  2642. END;
  2643. leftType := left.type.resolved; rightType := right.type.resolved;
  2644. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2645. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2646. END;
  2647. IF operatorCall # NIL THEN
  2648. result := operatorCall;
  2649. type := operatorCall.type;
  2650. (* admissible operators:
  2651. Times, Plus, Minus numeric numeric numeric
  2652. set set set
  2653. Slash numeric numeric real /complex
  2654. set set set
  2655. Div , Mod integer integer integer
  2656. And, Or bool bool bool
  2657. Equal, Unequal basic basic bool
  2658. pointer pointer bool
  2659. object object bool
  2660. record record bool
  2661. string string bool
  2662. enumerator enumerator bool
  2663. Less, LessEqual,
  2664. Greater, GreaterEqual integer/real integer/real bool
  2665. enumerator enumerator bool
  2666. In integer set bool
  2667. Is pointer type bool
  2668. object type bool
  2669. record type bool
  2670. Upto: special abbreviation for a..b
  2671. *)
  2672. ELSIF (left.type = NIL) THEN
  2673. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2674. D.Str("nil type in "); D.Type(left); D.Ln;
  2675. result := SyntaxTree.invalidExpression;
  2676. ELSIF (right.type = NIL) THEN
  2677. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2678. result := SyntaxTree.invalidExpression;
  2679. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2680. result := SyntaxTree.invalidExpression;
  2681. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2682. HALT(100);
  2683. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2684. type := system.booleanType;
  2685. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2686. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2687. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2688. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2689. IF VerboseErrorMessage THEN
  2690. Printout.Info("left",left);
  2691. Printout.Info("right",right);
  2692. END;
  2693. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2694. NewBool(TRUE)
  2695. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2696. NewBool(TRUE);
  2697. ELSIF IsUnextensibleRecord(left) THEN
  2698. NewBool(FALSE)
  2699. END
  2700. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2701. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2702. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2703. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2704. ELSIF operator = Scanner.In THEN (* left IN right *)
  2705. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2706. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2707. NewBool(il IN sr);
  2708. ELSE
  2709. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2710. left := NewConversion(left.position, left, system.longintType,NIL);
  2711. binaryExpression.SetLeft(left)
  2712. END;
  2713. type := system.booleanType;
  2714. END
  2715. ELSE
  2716. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2717. END
  2718. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2719. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2720. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2721. END;
  2722. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2723. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2724. END
  2725. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2726. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2727. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2728. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2729. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2730. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2731. ConvertOperands(left, right);
  2732. binaryExpression.SetLeft(left);
  2733. binaryExpression.SetRight(right);
  2734. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2735. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2736. END;
  2737. type := system.booleanType;
  2738. ELSE
  2739. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2740. END
  2741. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2742. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2743. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2744. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2745. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2746. CASE operator OF
  2747. |Scanner.Equal: NewBool(strl^=strr^);
  2748. |Scanner.Unequal:NewBool(strl^#strr^);
  2749. |Scanner.Less: NewBool(strl^<strr^);
  2750. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2751. |Scanner.Greater: NewBool(strl^>strr^);
  2752. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2753. ELSE
  2754. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2755. END;
  2756. END;
  2757. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2758. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2759. type := system.booleanType
  2760. ELSE
  2761. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2762. END;
  2763. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2764. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2765. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2766. type := system.booleanType;
  2767. ELSE
  2768. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2769. END
  2770. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2771. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2772. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2773. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2774. type := system.booleanType
  2775. ELSE
  2776. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2777. END;
  2778. ELSE
  2779. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2780. END;
  2781. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2782. type := system.booleanType;
  2783. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2784. type := system.booleanType;
  2785. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2786. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2787. THEN
  2788. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2789. IF (leftType # rightType) THEN
  2790. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2791. ConvertOperands(left,right); (* operands must be of the same type here *)
  2792. END;
  2793. binaryExpression.SetLeft(left);
  2794. binaryExpression.SetRight(right);
  2795. leftType := left.type.resolved;
  2796. rightType := right.type.resolved;
  2797. END;
  2798. type := leftType;
  2799. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2801. IF VerboseErrorMessage THEN
  2802. Printout.Info("left",left);
  2803. Printout.Info("right",right);
  2804. END;
  2805. ELSIF IsIntegerType(leftType) THEN
  2806. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2807. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2808. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2809. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2810. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2811. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2812. END;
  2813. END;
  2814. (* constant folding *)
  2815. (* bootstrap64
  2816. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2817. CASE operator OF
  2818. |Scanner.Plus: NewInteger(il+ir,left.type);
  2819. |Scanner.Minus: NewInteger(il-ir,left.type);
  2820. |Scanner.Times: NewInteger(il*ir,left.type);
  2821. |Scanner.Slash:
  2822. IF ir # 0 THEN
  2823. NewReal(il/ir, system.realType);
  2824. END;
  2825. |Scanner.Mod:
  2826. IF ir > 0 THEN
  2827. NewInteger(il MOD ir,left.type);
  2828. END;
  2829. |Scanner.Div:
  2830. IF ir > 0 THEN
  2831. NewInteger(il DIV ir,left.type);
  2832. END;
  2833. |Scanner.Equal: NewBool(il=ir);
  2834. |Scanner.Unequal:NewBool(il#ir);
  2835. |Scanner.Less: NewBool(il<ir);
  2836. |Scanner.LessEqual: NewBool(il<=ir);
  2837. |Scanner.Greater: NewBool(il>ir);
  2838. |Scanner.GreaterEqual: NewBool(il>=ir);
  2839. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2840. END;
  2841. ELS*)
  2842. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2843. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2844. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2845. CASE operator OF
  2846. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2847. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2848. |Scanner.Times: NewInteger(hl*hr,left.type);
  2849. |Scanner.Slash:
  2850. IF hr = 0 THEN
  2851. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2852. ELSE
  2853. IF type.sizeInBits = 64 THEN
  2854. NewReal(hl/hr,system.longrealType);
  2855. ELSE
  2856. NewReal(hl/hr,system.realType)
  2857. END
  2858. END;
  2859. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2860. |Scanner.Mod:
  2861. IF hr = 0 THEN
  2862. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2863. ELSE
  2864. NewInteger(hl MOD hr, left.type);
  2865. (* bootstrap64
  2866. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2867. *)
  2868. END;
  2869. |Scanner.Div:
  2870. IF hr = 0 THEN
  2871. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2872. ELSE
  2873. NewInteger(hl DIV hr, left.type);
  2874. (* bootstrap64
  2875. NewInteger(Machine.DivH(hl,hr),left.type);
  2876. *)
  2877. END;
  2878. (* *)
  2879. |Scanner.Equal: NewBool(hl=hr);
  2880. |Scanner.Unequal: NewBool(hl#hr);
  2881. |Scanner.Less: NewBool(hl<hr);
  2882. |Scanner.LessEqual: NewBool(hl<=hr);
  2883. |Scanner.Greater: NewBool(hl>hr);
  2884. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2885. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2886. END;
  2887. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2888. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2889. type := left.type
  2890. ELSIF (operator = Scanner.Slash) THEN
  2891. left := NewConversion(left.position,left,system.realType,NIL);
  2892. right := NewConversion(right.position,right,system.realType,NIL);
  2893. binaryExpression.SetLeft(left);
  2894. binaryExpression.SetRight(right);
  2895. type := system.realType
  2896. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2897. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2898. type := system.booleanType
  2899. ELSE
  2900. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2901. END;
  2902. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2903. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2904. CASE operator OF
  2905. |Scanner.Plus: NewReal(rl+rr,leftType);
  2906. |Scanner.Minus: NewReal(rl-rr,leftType);
  2907. |Scanner.Times:NewReal(rl*rr,leftType);
  2908. |Scanner.Slash:
  2909. IF rr = 0 THEN
  2910. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2911. ELSE
  2912. NewReal(rl/rr,leftType);
  2913. END
  2914. |Scanner.Equal: NewBool(rl=rr);
  2915. |Scanner.Unequal: NewBool(rl#rr);
  2916. |Scanner.Less: NewBool(rl<rr);
  2917. |Scanner.LessEqual: NewBool(rl<=rr);
  2918. |Scanner.Greater: NewBool(rl>rr);
  2919. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2920. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2921. END;
  2922. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2923. type := left.type
  2924. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2925. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2926. type := system.booleanType
  2927. ELSE
  2928. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2929. IF VerboseErrorMessage THEN
  2930. Printout.Info("left",left);
  2931. Printout.Info("right",right);
  2932. END;
  2933. END;
  2934. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2935. CASE operator OF
  2936. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2937. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2938. ELSE
  2939. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2940. IF VerboseErrorMessage THEN
  2941. Printout.Info("left", left);
  2942. Printout.Info("right", right)
  2943. END;
  2944. END;
  2945. IF ~error THEN
  2946. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2947. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2948. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2949. (* do constant folding *)
  2950. CASE operator OF
  2951. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2952. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2953. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2954. |Scanner.Slash:
  2955. divisor := c * c + d * d;
  2956. ASSERT(divisor # 0);
  2957. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2958. |Scanner.Equal: NewBool((a = c) & (b = d))
  2959. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2960. END
  2961. END
  2962. END
  2963. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2964. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2965. CASE operator OF
  2966. |Scanner.And: NewBool(bl & br);
  2967. |Scanner.Or: NewBool(bl OR br);
  2968. |Scanner.Equal: NewBool(bl = br);
  2969. |Scanner.Unequal: NewBool(bl # br);
  2970. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2971. END;
  2972. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2973. type := system.booleanType
  2974. ELSE
  2975. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2976. END;
  2977. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2978. (* constant folding *)
  2979. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2980. IF operator = Scanner.Equal THEN
  2981. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2982. ELSIF operator = Scanner.Unequal THEN
  2983. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2984. END;
  2985. END;
  2986. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2987. type := system.booleanType;
  2988. ELSE
  2989. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2990. END;
  2991. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2992. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2993. CASE operator OF
  2994. |Scanner.Plus: NewSet(sl + sr);
  2995. |Scanner.Minus: NewSet(sl - sr);
  2996. |Scanner.Times: NewSet(sl * sr);
  2997. |Scanner.Slash: NewSet(sl / sr);
  2998. |Scanner.Equal: NewBool(sl=sr);
  2999. |Scanner.Unequal: NewBool(sl#sr);
  3000. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3001. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3002. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3003. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3004. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3005. END;
  3006. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3007. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3008. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3009. THEN
  3010. type := system.booleanType
  3011. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3012. type := left.type
  3013. ELSE
  3014. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3015. END;
  3016. ELSIF IsCharacterType(left.type) THEN
  3017. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3018. CASE operator OF
  3019. |Scanner.Equal: NewBool(cl=cr);
  3020. |Scanner.Unequal: NewBool(cl#cr);
  3021. |Scanner.Less: NewBool(cl<cr);
  3022. |Scanner.LessEqual: NewBool(cl<=cr);
  3023. |Scanner.Greater: NewBool(cl>cr);
  3024. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3025. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3026. END;
  3027. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3028. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3029. type := system.booleanType
  3030. ELSE
  3031. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3032. END;
  3033. ELSE
  3034. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3035. END;
  3036. ELSE
  3037. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3038. END;
  3039. IF type = SyntaxTree.invalidType THEN
  3040. result := SyntaxTree.invalidExpression
  3041. ELSE
  3042. result.SetType(type)
  3043. END;
  3044. resolvedExpression := result
  3045. END VisitBinaryExpression;
  3046. (** resolve a range expression of the from <<first .. last BY step>>
  3047. - depending on the context different things are checked:
  3048. ArrayIndex:
  3049. - components must be integers
  3050. - replace missing lower bound with 0
  3051. - replace missing upper bound with MAX(LONGINT)
  3052. - replace missing step size with 1
  3053. SetElement:
  3054. - components must be integers
  3055. - replace missing lower bound with 0
  3056. - replace missing upper bound with MAX(SET)
  3057. - must not have step size
  3058. CaseGuard:
  3059. - components must be constant
  3060. - components must be integers or characters
  3061. - must have lower and upper bound present
  3062. - components are made compatible
  3063. - must not have step size
  3064. - if error: return invalidExpression
  3065. **)
  3066. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3067. VAR
  3068. hasError: BOOLEAN;
  3069. first, last, step: SyntaxTree.Expression;
  3070. BEGIN
  3071. hasError := FALSE;
  3072. first := x.first;
  3073. last := x.last;
  3074. step := x.step;
  3075. (* check lower bound *)
  3076. IF x.context = SyntaxTree.CaseGuard THEN
  3077. IF first = NIL THEN
  3078. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3079. hasError := TRUE
  3080. ELSE
  3081. first := ResolveExpression(first);
  3082. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3083. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3084. hasError := TRUE
  3085. ELSE
  3086. IF first IS SyntaxTree.StringValue THEN
  3087. (* add conversion from string to character *)
  3088. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3089. END
  3090. END;
  3091. (* check if expression is constant *)
  3092. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3093. (* error already reported *)
  3094. hasError := TRUE
  3095. END
  3096. END
  3097. ELSE (* ArrayIndex, SetElement *)
  3098. IF first = NIL THEN
  3099. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3100. END;
  3101. first := ResolveExpression(first);
  3102. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3103. first := NewConversion(first.position, first, system.longintType, NIL)
  3104. ELSE
  3105. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3106. hasError := TRUE
  3107. END
  3108. END;
  3109. (* check upper bound *)
  3110. IF x.context = SyntaxTree.CaseGuard THEN
  3111. IF last = NIL THEN
  3112. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3113. hasError := TRUE
  3114. ELSE
  3115. last := ResolveExpression(last);
  3116. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3117. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3118. hasError := TRUE
  3119. ELSE
  3120. IF last IS SyntaxTree.StringValue THEN
  3121. (* add conversion from string to character *)
  3122. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3123. END
  3124. END;
  3125. (* check if expression is constant *)
  3126. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3127. (* error already reported *)
  3128. hasError := TRUE
  3129. ELSE
  3130. (* try to make lower and upper bound compatible *)
  3131. ConvertOperands(first, last);
  3132. IF first.type.resolved # last.type.resolved THEN
  3133. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3134. hasError := TRUE
  3135. END
  3136. END
  3137. END
  3138. ELSE (* ArrayIndex, SetElement *)
  3139. IF last = NIL THEN
  3140. IF x.context = SyntaxTree.ArrayIndex THEN
  3141. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3142. ELSE
  3143. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3144. END
  3145. END;
  3146. last := ResolveExpression(last);
  3147. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3148. last := NewConversion(last.position, last, system.longintType, NIL)
  3149. ELSE
  3150. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3151. hasError := TRUE
  3152. END
  3153. END;
  3154. (* check step size *)
  3155. IF x.context = SyntaxTree.ArrayIndex THEN
  3156. IF step = NIL THEN
  3157. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3158. END;
  3159. step := ResolveExpression(step);
  3160. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3161. step := NewConversion(step.position, step, system.longintType, NIL)
  3162. ELSE
  3163. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3164. hasError := TRUE
  3165. END
  3166. ELSE (* SetElement, CaseGuard *)
  3167. IF step # NIL THEN
  3168. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3169. hasError := TRUE
  3170. END
  3171. END;
  3172. IF hasError THEN
  3173. resolvedExpression := SyntaxTree.invalidExpression
  3174. ELSE
  3175. x.SetFirst(first);
  3176. x.SetLast(last);
  3177. x.SetStep(step);
  3178. x.SetType(system.rangeType);
  3179. resolvedExpression := x;
  3180. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3181. END
  3182. END VisitRangeExpression;
  3183. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3184. BEGIN
  3185. x.SetType(NIL);
  3186. resolvedExpression := x;
  3187. END VisitTensorRangeExpression;
  3188. (** resolve the expression d and return result as designator
  3189. - resolve expression
  3190. - if expression is a designator then return designator else error message and return invalidDesignator
  3191. **)
  3192. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3193. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3194. BEGIN
  3195. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3196. resolved := ResolveExpression(d);
  3197. IF resolved = SyntaxTree.invalidExpression THEN
  3198. (* error should already have been reported *)
  3199. result := SyntaxTree.invalidDesignator;
  3200. ELSIF resolved IS SyntaxTree.Designator THEN
  3201. result := resolved(SyntaxTree.Designator);
  3202. ELSE
  3203. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3204. result := SyntaxTree.invalidDesignator;
  3205. END;
  3206. (* result.type might be nil. *)
  3207. RETURN result
  3208. END ResolveDesignator;
  3209. (**
  3210. symbol designator generated in this module
  3211. nothing to be resolved
  3212. **)
  3213. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3214. BEGIN
  3215. resolvedExpression := x;
  3216. END VisitSymbolDesignator;
  3217. (**
  3218. self designator generated in this module
  3219. nothing to be resolved
  3220. **)
  3221. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3222. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3223. BEGIN
  3224. (* check if in record scope *)
  3225. scope := currentScope;
  3226. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3227. scope := scope.outerScope;
  3228. END;
  3229. IF scope = NIL THEN (* in module scope *)
  3230. x.SetType(system.anyType);
  3231. ELSIF scope IS SyntaxTree.CellScope THEN
  3232. cell := scope(SyntaxTree.CellScope).ownerCell;
  3233. x.SetType(cell);
  3234. ELSE (* in record scope *)
  3235. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3236. IF (record # NIL) & (record.pointerType # NIL) THEN
  3237. type := ResolveType(record.pointerType);
  3238. x.SetType(type);
  3239. ELSE
  3240. x.SetType(record);
  3241. END;
  3242. END;
  3243. resolvedExpression := x;
  3244. END VisitSelfDesignator;
  3245. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3246. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3247. BEGIN
  3248. scope := currentScope;
  3249. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3250. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3251. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3252. returnType := procedureType.returnType;
  3253. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3254. THEN
  3255. x.SetType(returnType);
  3256. ELSE
  3257. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3258. x.SetType(SyntaxTree.invalidType);
  3259. END;
  3260. ELSE
  3261. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3262. x.SetType(SyntaxTree.invalidType);
  3263. END;
  3264. x.SetAssignable(TRUE);
  3265. resolvedExpression := x;
  3266. END VisitResultDesignator;
  3267. (**
  3268. return symbol designator as an expression
  3269. - if symbol is a constant then return the constant value expression
  3270. - else
  3271. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3272. - if symbol is a guarded variable then return a TypeGuardDesignator
  3273. - else return a symbol designator
  3274. **)
  3275. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3276. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3277. guardType: SyntaxTree.Type;
  3278. BEGIN
  3279. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3280. result := SyntaxTree.invalidExpression;
  3281. ASSERT(symbol # NIL);
  3282. (*
  3283. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3284. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3285. Error(position,Diagnostics.Invalid,"type not allowed here");
  3286. ELS *)
  3287. (* not needed any more as values are stored in the expression
  3288. IF symbol IS SyntaxTree.Constant THEN
  3289. result := symbol(SyntaxTree.Constant).value
  3290. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3291. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3292. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3293. ELSE
  3294. result := symbol(SyntaxTree.Constant).value
  3295. END;
  3296. ELSE
  3297. *)
  3298. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3299. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3300. THEN
  3301. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3302. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3303. left := NewDereferenceDesignator(position,left);
  3304. left.SetHidden(TRUE);
  3305. END;
  3306. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3307. scope := currentScope;
  3308. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3309. scope := scope.outerScope;
  3310. END;
  3311. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3312. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3313. END;
  3314. END;
  3315. 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);
  3316. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3317. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3318. ELSE
  3319. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3320. END;
  3321. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3322. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3323. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3324. result.SetType(symbol.type);
  3325. result.SetAssignable(assignable);
  3326. symbol.MarkUsed;
  3327. IF symbol IS SyntaxTree.Constant THEN
  3328. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3329. END;
  3330. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3331. variableAccessed := TRUE
  3332. END;
  3333. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3334. IF GetGuard(symbol,guardType) THEN
  3335. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3336. END;
  3337. END;
  3338. ASSERT(result.type # NIL);
  3339. RETURN result
  3340. END NewSymbolDesignator;
  3341. (** check and resolve an identifier designator "identifier"
  3342. - if identifier = self then return SelfDesignator
  3343. - else find symbol in current scope
  3344. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3345. **)
  3346. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3347. VAR symbol: SyntaxTree.Symbol;
  3348. BEGIN
  3349. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3350. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3351. IF symbol # NIL THEN
  3352. ResolveSymbol(symbol);
  3353. ASSERT(symbol.type # NIL);
  3354. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3355. ELSE
  3356. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3357. IF VerboseErrorMessage THEN
  3358. Printout.Info("undeclared identifier designator",identifierDesignator);
  3359. END;
  3360. resolvedExpression := SyntaxTree.invalidDesignator;
  3361. END;
  3362. END VisitIdentifierDesignator;
  3363. (** check and resolve a selector designator of the form left.designator
  3364. - if left is a pointer type then do auto dereferenciation
  3365. - left denotes a search scope:
  3366. - if left type is import type then set search scope to respective module
  3367. - if left type is enumeration type then set search scope to respective enumeration scope
  3368. - elsif left type is record type then set search scope to record scope
  3369. - search symbol in computed scope
  3370. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3371. **)
  3372. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3373. VAR
  3374. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3375. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3376. BEGIN
  3377. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3378. left := ResolveDesignator(selectorDesignator.left);
  3379. result := SyntaxTree.invalidDesignator;
  3380. IF left # NIL THEN
  3381. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3382. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3383. END;
  3384. scope := NIL;
  3385. IF left.type = NIL THEN
  3386. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3387. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3388. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3389. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3390. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3391. module := symbol(SyntaxTree.Import).module;
  3392. IF module # NIL THEN
  3393. scope := module.moduleScope
  3394. ELSE
  3395. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3396. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3397. END;
  3398. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3399. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3400. ASSERT(scope # NIL)
  3401. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3402. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3403. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3404. IF type IS SyntaxTree.EnumerationType THEN
  3405. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3406. ELSE
  3407. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3408. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3409. END;
  3410. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3411. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3412. ELSE
  3413. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3414. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3415. END;
  3416. symbol := NIL;
  3417. IF scope # NIL THEN
  3418. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3419. IF symbol # NIL THEN
  3420. ResolveSymbol(symbol);
  3421. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3422. symbol.MarkUsed
  3423. ELSE
  3424. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3425. IF VerboseErrorMessage THEN
  3426. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3427. Printout.Info("scope", scope);
  3428. Printout.Info("left", left);
  3429. Printout.Info("undeclared identifier",selectorDesignator);
  3430. Printout.Info("left resolved designator",left);
  3431. END
  3432. END;
  3433. END;
  3434. END;
  3435. resolvedExpression := result;
  3436. END VisitSelectorDesignator;
  3437. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3438. VAR len,idx: LONGINT;
  3439. BEGIN
  3440. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3441. IF idx < 0 THEN
  3442. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3443. ELSE
  3444. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3445. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3446. END;
  3447. END;
  3448. END;
  3449. END IndexCheck;
  3450. (*
  3451. - if index designator has not type, use newBaseType as its type
  3452. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3453. - special rule: if static array of dynamic array occurs, make it all dynamic
  3454. index designator type: new base type: new index designator type:
  3455. NIL z z
  3456. ARRAY [x, y] z ARRAY [x, y] OF z
  3457. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3458. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3459. *)
  3460. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3461. VAR
  3462. mathArrayType: SyntaxTree.MathArrayType;
  3463. makeDynamic: BOOLEAN;
  3464. BEGIN
  3465. IF indexDesignator.type = NIL THEN
  3466. indexDesignator.SetType(newBaseType)
  3467. ELSE
  3468. (* index designator must be a of math array type *)
  3469. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3470. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3471. (* determine if all arrays have to be made dynamic *)
  3472. makeDynamic :=
  3473. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3474. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3475. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3476. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3477. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3478. END;
  3479. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3480. mathArrayType.SetArrayBase(newBaseType)
  3481. END
  3482. END SetIndexBaseType;
  3483. (** check and append index list element to index designator of math array
  3484. - check validity of single index or array range
  3485. - compute new type
  3486. - if range then create new array type (calculate length of resulting array)
  3487. - otherwise take sourceArray.arrayBase as new type
  3488. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3489. **)
  3490. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3491. VAR
  3492. targetArray: SyntaxTree.MathArrayType;
  3493. first, last, step: SyntaxTree.Expression;
  3494. firstValue, lastValue, stepValue, length: LONGINT;
  3495. rangeExpression: SyntaxTree.RangeExpression;
  3496. BEGIN
  3497. IF indexListItem.type = SyntaxTree.invalidType THEN
  3498. (* error already handled *)
  3499. indexDesignator.parameters.AddExpression(indexListItem)
  3500. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3501. indexDesignator.HasRange;
  3502. indexDesignator.HasTensorRange;
  3503. indexDesignator.parameters.AddExpression(indexListItem);
  3504. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3505. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3506. IndexCheck(indexListItem, sourceArray.length);
  3507. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3508. indexDesignator.parameters.AddExpression(indexListItem)
  3509. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3510. indexDesignator.HasRange;
  3511. (* if the range is given as an array range expression, check the validity of its components *)
  3512. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3513. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3514. first := rangeExpression.first;
  3515. last := rangeExpression.last;
  3516. step := rangeExpression.step;
  3517. (* perform static checks on range components *)
  3518. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3519. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3520. END;
  3521. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3522. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3523. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3524. END
  3525. END;
  3526. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3527. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3528. END;
  3529. (* add conversions to size type *)
  3530. (* TODO: needed? *)
  3531. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3532. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3533. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3534. END;
  3535. IF indexDesignator.hasTensorRange THEN
  3536. (* the index designator's base type is a tensor: leave it as is *)
  3537. ELSE
  3538. (* append a new math array to the index designator's base type *)
  3539. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3540. IF ~error THEN
  3541. (*
  3542. (* optimization: calculate length of target array for static ranges *)
  3543. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3544. IF IsStaticallyOpenRange(rangeExpression) THEN
  3545. (* range is open ('*'): reuse source array length as target array length *)
  3546. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3547. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3548. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3549. IF lastValue = MAX(LONGINT) THEN
  3550. IF IsIntegerValue(sourceArray.length, length) THEN
  3551. lastValue := length - 1;
  3552. isStaticTargetArrayLength := TRUE
  3553. ELSE
  3554. isStaticTargetArrayLength := FALSE
  3555. END
  3556. ELSE
  3557. isStaticTargetArrayLength := TRUE
  3558. END;
  3559. IF isStaticTargetArrayLength THEN
  3560. (* calculate static target array length *)
  3561. IF firstValue > lastValue THEN
  3562. length := 0
  3563. ELSE
  3564. length := 1 + lastValue - firstValue;
  3565. IF length MOD stepValue = 0 THEN
  3566. length := length DIV stepValue
  3567. ELSE
  3568. length := length DIV stepValue + 1
  3569. END
  3570. END;
  3571. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3572. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3573. ASSERT(targetArray.form = SyntaxTree.Static)
  3574. END
  3575. END
  3576. END
  3577. *)
  3578. END;
  3579. SetIndexBaseType(indexDesignator, targetArray)
  3580. END;
  3581. indexDesignator.parameters.AddExpression(indexListItem)
  3582. ELSE
  3583. Error(position, Diagnostics.Invalid,"invalid index list item");
  3584. END;
  3585. END AppendMathIndex;
  3586. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3587. VAR parameters: SyntaxTree.ExpressionList;
  3588. BEGIN
  3589. parameters := index.parameters;
  3590. IF (expression.type = NIL) THEN
  3591. Error(position,Diagnostics.Invalid,"invalid index");
  3592. ELSIF IsIntegerType(expression.type.resolved) THEN
  3593. IF over IS SyntaxTree.ArrayType THEN
  3594. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3595. ELSIF over IS SyntaxTree.StringType THEN
  3596. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3597. END;
  3598. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3599. parameters.AddExpression(expression);
  3600. ELSE
  3601. Error(position,Diagnostics.Invalid,"invalid index");
  3602. END;
  3603. END AppendIndex;
  3604. (** convert an expression to math array type
  3605. if expression is of math array type: return expression itself
  3606. if expression is of array-structured object type: return an index operator call on it
  3607. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3608. otherwise: return invalid expression
  3609. **)
  3610. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3611. VAR
  3612. result: SyntaxTree.Expression;
  3613. mathArrayType: SyntaxTree.MathArrayType;
  3614. BEGIN
  3615. IF expression.type = NIL THEN
  3616. result := SyntaxTree.invalidExpression
  3617. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3618. (* expression of math array type *)
  3619. result := expression
  3620. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3621. (* expression of array-structured object type *)
  3622. mathArrayType := MathArrayStructureOfType(expression.type);
  3623. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3624. ELSE
  3625. result := SyntaxTree.invalidExpression
  3626. END;
  3627. RETURN result
  3628. END ConvertToMathArray;
  3629. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3630. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3631. VAR
  3632. result: SyntaxTree.ExpressionList;
  3633. i: LONGINT;
  3634. BEGIN
  3635. result := SyntaxTree.NewExpressionList();
  3636. FOR i := 1 TO itemCount DO
  3637. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3638. END;
  3639. RETURN result
  3640. END ListOfOpenRanges;
  3641. (** create a procedure call designator for an index operator call on an array-structured object type
  3642. - use given index list as actual parameters
  3643. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3644. **)
  3645. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3646. VAR
  3647. operator: SyntaxTree.Operator;
  3648. expression: SyntaxTree.Expression;
  3649. actualParameters, tempList: SyntaxTree.ExpressionList;
  3650. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3651. result, tempDesignator: SyntaxTree.Designator;
  3652. recordType: SyntaxTree.RecordType;
  3653. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3654. i, hashValue, indexListSize, indexListKind: LONGINT;
  3655. castReturnType: SyntaxTree.MathArrayType;
  3656. BEGIN
  3657. ASSERT(IsArrayStructuredObjectType(left.type));
  3658. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3659. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3660. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3661. indexListSize := indexList.Length();
  3662. indexListKind := 0;
  3663. containsNonRange := FALSE;
  3664. FOR i := 0 TO indexList.Length() - 1 DO
  3665. indexListKind := indexListKind * 2;
  3666. expression := indexList.GetExpression(i);
  3667. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3668. INC(indexListKind)
  3669. ELSE
  3670. containsNonRange := TRUE
  3671. END
  3672. END;
  3673. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3674. (* select applicable index operator
  3675. - try to look up optimal index operator
  3676. - if not present, use operator on ranges
  3677. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3678. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3679. *)
  3680. usesGeneralOperator := FALSE;
  3681. IF rhs # NIL THEN
  3682. (* write operator *)
  3683. IF hashValue = -1 THEN
  3684. operator := NIL
  3685. ELSE
  3686. operator := recordType.arrayAccessOperators.write[hashValue];
  3687. END;
  3688. IF operator = NIL THEN
  3689. usesPureRangeOperator := TRUE;
  3690. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3691. operator := recordType.arrayAccessOperators.generalWrite;
  3692. usesGeneralOperator := TRUE
  3693. ELSE
  3694. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3695. operator := recordType.arrayAccessOperators.write[hashValue];
  3696. END
  3697. END
  3698. ELSE
  3699. (* read operator *)
  3700. IF hashValue = -1 THEN
  3701. operator := NIL
  3702. ELSE
  3703. operator := recordType.arrayAccessOperators.read[hashValue];
  3704. END;
  3705. IF operator = NIL THEN
  3706. usesPureRangeOperator := TRUE;
  3707. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3708. operator := recordType.arrayAccessOperators.generalRead;
  3709. usesGeneralOperator := TRUE
  3710. ELSE
  3711. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3712. operator := recordType.arrayAccessOperators.read[hashValue];
  3713. END
  3714. END
  3715. END;
  3716. IF operator = NIL THEN
  3717. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3718. result := SyntaxTree.invalidDesignator;
  3719. ELSE
  3720. (* determine if reshaping is needed *)
  3721. needsReshaping := containsNonRange & usesPureRangeOperator;
  3722. (* import OCArrayBase if reshaping is needed *)
  3723. IF needsReshaping & ~arrayBaseImported THEN
  3724. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3725. arrayBaseImported := TRUE
  3726. END;
  3727. (* add the index list item to the list of actual parameters
  3728. - for general operators: add a single inline array containing the index list items as parameter
  3729. - otherwise: add all index list items as individual parameters
  3730. *)
  3731. actualParameters := SyntaxTree.NewExpressionList();
  3732. IF usesGeneralOperator THEN
  3733. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3734. END;
  3735. FOR i := 0 TO indexListSize - 1 DO
  3736. expression := indexList.GetExpression(i);
  3737. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3738. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3739. tempList := SyntaxTree.NewExpressionList();
  3740. tempList.AddExpression(expression);
  3741. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3742. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3743. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3744. END;
  3745. IF usesGeneralOperator THEN
  3746. tempMathArrayExpression.elements.AddExpression(expression);
  3747. ELSE
  3748. actualParameters.AddExpression(expression)
  3749. END
  3750. END;
  3751. IF usesGeneralOperator THEN
  3752. actualParameters.AddExpression(tempMathArrayExpression)
  3753. END;
  3754. IF rhs # NIL THEN
  3755. (* add actual parameter for RHS *)
  3756. IF needsReshaping THEN
  3757. (* reshape using OCArrayBase.ExpandDimensions *)
  3758. tempList := SyntaxTree.NewExpressionList();
  3759. (* source array *)
  3760. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3761. tempList.AddExpression(rhs);
  3762. ELSE
  3763. (* convert scalar to one-dimensional array *)
  3764. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3765. tempMathArrayExpression.elements.AddExpression(rhs);
  3766. tempList.AddExpression(tempMathArrayExpression)
  3767. END;
  3768. (* list of kept dimensions *)
  3769. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3770. FOR i := 0 TO indexListSize - 1 DO
  3771. expression := indexList.GetExpression(i);
  3772. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3773. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3774. ELSE
  3775. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3776. END
  3777. END;
  3778. tempList.AddExpression(tempMathArrayExpression);
  3779. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3780. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3781. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3782. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3783. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3784. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3785. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3786. expression.SetType(castReturnType);
  3787. ELSE
  3788. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3789. END;
  3790. actualParameters.AddExpression(expression)
  3791. ELSE
  3792. actualParameters.AddExpression(rhs)
  3793. END
  3794. END;
  3795. (* add dereference operator and create procedure call designator *)
  3796. ASSERT(left IS SyntaxTree.Designator);
  3797. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3798. ASSERT(expression IS SyntaxTree.Designator);
  3799. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3800. IF (rhs = NIL) & needsReshaping THEN
  3801. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3802. tempList := SyntaxTree.NewExpressionList();
  3803. FOR i := 0 TO indexList.Length() - 1 DO
  3804. expression := indexList.GetExpression(i);
  3805. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3806. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3807. ELSE
  3808. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3809. END
  3810. END;
  3811. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3812. END;
  3813. IF rhs = NIL THEN
  3814. (* special rule: index read operator calls are considered to be assignable *)
  3815. result.SetAssignable(TRUE)
  3816. END;
  3817. (* put information about this index operator call into the resulting designator *)
  3818. result.SetRelatedAsot(left);
  3819. result.SetRelatedIndexList(indexList)
  3820. END;
  3821. RETURN result
  3822. END NewIndexOperatorCall;
  3823. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3824. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3825. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3826. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3827. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3828. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3829. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3830. CONST trace = FALSE;
  3831. BEGIN
  3832. IF trace THEN
  3833. FOR i := 0 TO actualParameters.Length()-1 DO
  3834. Printout.Info("par", actualParameters.GetExpression(i));
  3835. END;
  3836. END;
  3837. operator := scope.firstOperator;
  3838. WHILE(operator # NIL) DO
  3839. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3840. procedureType := operator.type(SyntaxTree.ProcedureType);
  3841. distance := Distance(system, procedureType,actualParameters);
  3842. IF trace THEN Printout.Info("check op ",operator) END;
  3843. IF distance < bestDistance THEN
  3844. IF trace THEN Printout.Info("taken op",operator) END;
  3845. bestDistance := distance;
  3846. bestOperator := operator;
  3847. END;
  3848. END;
  3849. operator := operator.nextOperator;
  3850. END;
  3851. END FindInScope;
  3852. BEGIN
  3853. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3854. identifier := SyntaxTree.NewIdentifier("[]");
  3855. WHILE (recordType # NIL) DO
  3856. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3857. recordType := recordType.GetBaseRecord();
  3858. END;
  3859. RETURN bestOperator
  3860. END FindOperator;
  3861. BEGIN
  3862. type := left.type.resolved;
  3863. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3864. actualParameters := SyntaxTree.NewExpressionList();
  3865. FOR i := 0 TO parameters.Length()-1 DO
  3866. expression := ResolveExpression(parameters.GetExpression(i));
  3867. actualParameters.AddExpression(expression);
  3868. END;
  3869. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3870. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3871. IF op # NIL THEN
  3872. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3873. ASSERT(expression IS SyntaxTree.Designator);
  3874. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3875. result.SetRelatedAsot(left);
  3876. result.SetRelatedIndexList(parameters);
  3877. (* check if write operator exists, for var parameters *)
  3878. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3879. actualParameters := SyntaxTree.NewExpressionList();
  3880. FOR i := 0 TO parameters.Length()-1 DO
  3881. expression := ResolveExpression(parameters.GetExpression(i));
  3882. actualParameters.AddExpression(expression);
  3883. END;
  3884. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3885. actualParameters.AddExpression(rhs);
  3886. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3887. IF op = NIL THEN rhs := NIL END;
  3888. END;
  3889. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3890. ELSE
  3891. Error(position,Diagnostics.Invalid,"undefined operator");
  3892. result := SyntaxTree.invalidDesignator
  3893. END;
  3894. RETURN result;
  3895. END NewObjectOperatorCall;
  3896. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3897. 1. convert bracket designator chains into a single one that contains separators
  3898. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3899. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3900. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3901. - if an array or math array is indexed over, create index designator
  3902. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3903. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3904. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3905. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3906. - if an array-structured object type is indexed over, create procedure call designator
  3907. e.g.: a[x, y] -> a^."[]"(x, y)
  3908. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3909. - a[i, *] = a[i][*]
  3910. - a[*, i] # a[*][i]
  3911. Because:
  3912. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3913. - 'i-th column' = a[*, i]
  3914. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3915. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3916. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3917. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3918. **)
  3919. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3920. VAR
  3921. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3922. indexDesignator: SyntaxTree.IndexDesignator;
  3923. designator: SyntaxTree.Designator;
  3924. type: SyntaxTree.Type;
  3925. recordType: SyntaxTree.RecordType;
  3926. expression, rhs: SyntaxTree.Expression;
  3927. indexList: SyntaxTree.ExpressionList;
  3928. i: LONGINT;
  3929. hasError, done: BOOLEAN;
  3930. PROCEDURE FinalizeIndexDesignator;
  3931. BEGIN
  3932. IF indexDesignator # NIL THEN
  3933. (* the end of a tensor has been reached: *)
  3934. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3935. SetIndexBaseType(indexDesignator, type);
  3936. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3937. designator := indexDesignator;
  3938. type := designator.type.resolved;
  3939. indexDesignator := NIL;
  3940. ASSERT(SyntaxTree.Resolved IN type.state)
  3941. END
  3942. END FinalizeIndexDesignator;
  3943. BEGIN
  3944. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3945. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3946. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3947. (* copy all index list entries including a separator to the left bracket designator *)
  3948. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3949. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3950. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3951. END;
  3952. (* propagate the related RHS *)
  3953. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3954. (* only resolve left bracket designator and use as final result *)
  3955. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3956. ELSE
  3957. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3958. designator := ResolveDesignator(bracketDesignator.left);
  3959. type := designator.type.resolved;
  3960. indexDesignator := NIL;
  3961. (*!!! clean up *)
  3962. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3963. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3964. RETURN
  3965. END;
  3966. i := 0;
  3967. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3968. expression := bracketDesignator.parameters.GetExpression(i);
  3969. expression := ResolveExpression(expression);
  3970. bracketDesignator.parameters.SetExpression(i, expression);
  3971. IF expression = SyntaxTree.indexListSeparator THEN
  3972. (* finalize an existing index designator if needed *)
  3973. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3974. INC(i)
  3975. ELSE
  3976. (* do auto-dereferencing if needed *)
  3977. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3978. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3979. & (i=0)*)
  3980. THEN
  3981. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3982. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3983. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  3984. designator := SyntaxTree.invalidDesignator;
  3985. type := SyntaxTree.invalidType
  3986. ELSE
  3987. FinalizeIndexDesignator;
  3988. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3989. type := designator.type.resolved
  3990. END
  3991. END;
  3992. (* create a new index designator, if needed *)
  3993. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3994. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3995. indexDesignator.SetAssignable(designator.assignable);
  3996. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3997. (* designator := indexDesignator *)
  3998. END;
  3999. IF type = SyntaxTree.invalidType THEN
  4000. (* error already handled *)
  4001. INC(i)
  4002. ELSIF type IS SyntaxTree.ArrayType THEN
  4003. (* indexing over an array *)
  4004. ASSERT(indexDesignator # NIL);
  4005. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4006. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4007. INC(i)
  4008. ELSIF type IS SyntaxTree.StringType THEN
  4009. (* indexing over an array *)
  4010. ASSERT(indexDesignator # NIL);
  4011. AppendIndex(expression.position, indexDesignator, expression, type);
  4012. type := type(SyntaxTree.StringType).baseType.resolved;
  4013. INC(i)
  4014. ELSIF type IS SyntaxTree.MathArrayType THEN
  4015. (* indexing over a math array *)
  4016. ASSERT(indexDesignator # NIL);
  4017. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4018. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4019. INC(i)
  4020. ELSIF IsArrayStructuredObjectType(type) THEN
  4021. (* indexing over ASOTs *)
  4022. FinalizeIndexDesignator;
  4023. ASSERT(type IS SyntaxTree.PointerType);
  4024. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4025. (*
  4026. - collect index list items from bracket designator that belong to ASOT
  4027. - check for errors
  4028. *)
  4029. indexList := SyntaxTree.NewExpressionList();
  4030. hasError := FALSE;
  4031. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4032. (* indexing over tensor ASOT:
  4033. - stop at index list end or separator
  4034. - dimensionality is given by number of index list items
  4035. *)
  4036. done := FALSE;
  4037. WHILE ~done DO
  4038. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4039. done := TRUE;
  4040. ELSE
  4041. expression := bracketDesignator.parameters.GetExpression(i);
  4042. IF expression = SyntaxTree.indexListSeparator THEN
  4043. done := TRUE;
  4044. ELSE
  4045. expression := ResolveExpression(expression);
  4046. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4047. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4048. hasError := TRUE
  4049. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4050. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4051. expression := SyntaxTree.invalidExpression;
  4052. hasError := TRUE
  4053. END;
  4054. indexList.AddExpression(expression)
  4055. END;
  4056. INC(i)
  4057. END
  4058. END
  4059. ELSE
  4060. (* indexing over non-tensor ASOT:
  4061. - ignore separators
  4062. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4063. *)
  4064. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4065. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4066. expression := bracketDesignator.parameters.GetExpression(i);
  4067. ELSE
  4068. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4069. END;
  4070. IF expression # SyntaxTree.indexListSeparator THEN
  4071. expression := ResolveExpression(expression);
  4072. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4073. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4074. expression := SyntaxTree.invalidExpression;
  4075. hasError := TRUE
  4076. END;
  4077. indexList.AddExpression(expression)
  4078. END;
  4079. INC(i)
  4080. END;
  4081. END;
  4082. IF hasError THEN
  4083. designator := SyntaxTree.invalidDesignator;
  4084. type := SyntaxTree.invalidType;
  4085. ELSE
  4086. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4087. and the last entry in the index list belongs to the array-structured object type in question.
  4088. E.g.: for a 2-dimensional array-structured object type:
  4089. - 'lhs := asot[1, 2]' -> read mode
  4090. - 'asot[1, 2] := rhs' -> write mode
  4091. - 'asot[1, 2, 3] := rhs' -> read mode
  4092. *)
  4093. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4094. rhs := bracketDesignator.relatedRhs
  4095. ELSE
  4096. rhs := NIL
  4097. END;
  4098. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4099. type := designator.type
  4100. END
  4101. ELSE
  4102. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4103. designator := SyntaxTree.invalidDesignator;
  4104. type := SyntaxTree.invalidType;
  4105. INC(i)
  4106. END
  4107. END
  4108. END;
  4109. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4110. resolvedExpression := designator
  4111. END
  4112. END VisitBracketDesignator;
  4113. (** check and resolve expression list
  4114. - resolve each expression in an expression list
  4115. - returns true if and only if all statements could have successfully been resolved
  4116. **)
  4117. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4118. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4119. BEGIN
  4120. result := TRUE;
  4121. FOR i := 0 TO expressionList.Length()-1 DO
  4122. expression := ResolveExpression(expressionList.GetExpression(i));
  4123. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4124. expressionList.SetExpression(i,expression);
  4125. END;
  4126. RETURN result
  4127. END ExpressionList;
  4128. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4129. BEGIN
  4130. type := type.resolved;
  4131. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4132. RETURN TRUE
  4133. ELSIF system.CanPassInRegister # NIL THEN
  4134. RETURN system.CanPassInRegister(type);
  4135. ELSE
  4136. RETURN FALSE
  4137. END;
  4138. END CanPassInRegister;
  4139. (** return procedure call designator left(actualParameters)
  4140. - check realtime procedure call in realtime procedure
  4141. - check number of parameters
  4142. - check parameter compatibility
  4143. return invalidDesignator if error
  4144. **)
  4145. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4146. VAR result: SyntaxTree.Designator;
  4147. numberFormalParameters, numberActualParameters: LONGINT;
  4148. formalType: SyntaxTree.ProcedureType;
  4149. formalParameter: SyntaxTree.Parameter;
  4150. actualParameter: SyntaxTree.Expression;
  4151. i: LONGINT;
  4152. BEGIN
  4153. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4154. result := SyntaxTree.invalidDesignator;
  4155. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4156. numberFormalParameters := formalType.numberParameters;
  4157. numberActualParameters := actualParameters.Length();
  4158. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4159. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4160. END;
  4161. IF ~ExpressionList(actualParameters) THEN
  4162. result := SyntaxTree.invalidDesignator
  4163. ELSE
  4164. IF numberActualParameters <= numberFormalParameters THEN
  4165. formalParameter := formalType.firstParameter;
  4166. FOR i := 0 TO numberActualParameters-1 DO
  4167. actualParameter := actualParameters.GetExpression(i);
  4168. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4169. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4170. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4171. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4172. ELSE
  4173. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4174. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4175. END;
  4176. actualParameters.SetExpression(i,actualParameter);
  4177. END;
  4178. formalParameter := formalParameter.nextParameter;
  4179. END;
  4180. WHILE (formalParameter # NIL) DO
  4181. IF formalParameter.defaultValue # NIL THEN
  4182. actualParameters.AddExpression(formalParameter.defaultValue);
  4183. formalParameter := formalParameter.nextParameter
  4184. ELSE
  4185. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4186. formalParameter := NIL;
  4187. END;
  4188. END;
  4189. ELSE
  4190. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4191. END;
  4192. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4193. result.SetAssignable(FALSE);
  4194. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4195. END;
  4196. RETURN result
  4197. END NewProcedureCallDesignator;
  4198. (**
  4199. builtin call designator generated in VisitParameterDesignator
  4200. -> nothing to be resolved
  4201. **)
  4202. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4203. BEGIN
  4204. resolvedExpression := x;
  4205. END VisitTypeGuardDesignator;
  4206. (**
  4207. builtin call designator generated in VisitParameterDesignator
  4208. -> nothing to be resolved
  4209. **)
  4210. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4211. BEGIN
  4212. IF ExpressionList(x.parameters) THEN
  4213. resolvedExpression := x;
  4214. END;
  4215. END VisitBuiltinCallDesignator;
  4216. (**
  4217. procedure call designator generated in VisitParameterDesignator
  4218. -> nothing to be resolved
  4219. **)
  4220. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4221. BEGIN
  4222. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4223. resolvedExpression := x;
  4224. END VisitProcedureCallDesignator;
  4225. (** return true if x is a variable else return false and report error **)
  4226. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4227. VAR result: BOOLEAN;
  4228. BEGIN
  4229. result := TRUE;
  4230. IF x = SyntaxTree.invalidExpression THEN
  4231. result := FALSE;
  4232. ELSIF ~IsVariable(x) THEN
  4233. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4234. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4235. result := FALSE;
  4236. END;
  4237. RETURN result
  4238. END CheckVariable;
  4239. (**
  4240. if expression x is of basic type then return true else report error and return false
  4241. **)
  4242. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4243. VAR result: BOOLEAN;
  4244. BEGIN
  4245. result := FALSE;
  4246. IF x = SyntaxTree.invalidExpression THEN
  4247. ELSIF ~IsBasicType(x.type) THEN
  4248. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4249. result := FALSE
  4250. ELSE result := TRUE
  4251. END;
  4252. RETURN result
  4253. END CheckBasicType;
  4254. (**
  4255. if expression x is of number type then return true else report error and return false
  4256. **)
  4257. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4258. VAR result: BOOLEAN;
  4259. BEGIN
  4260. result := FALSE;
  4261. IF x = SyntaxTree.invalidExpression THEN
  4262. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4263. Error(x.position,Diagnostics.Invalid,"is non number type");
  4264. ELSE result := TRUE
  4265. END;
  4266. RETURN result
  4267. END CheckNumberType;
  4268. (**
  4269. if expression x is of number or size type but not complex then return true else report error and return false
  4270. **)
  4271. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4272. VAR result: BOOLEAN;
  4273. BEGIN
  4274. result := FALSE;
  4275. IF x = SyntaxTree.invalidExpression THEN
  4276. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4277. Error(x.position,Diagnostics.Invalid,"is complex type");
  4278. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4279. Error(x.position,Diagnostics.Invalid,"is non number type");
  4280. ELSE result := TRUE
  4281. END;
  4282. RETURN result
  4283. END CheckNonComplexNumberSizeType;
  4284. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4285. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4286. BEGIN
  4287. result := FALSE; type := x.type.resolved;
  4288. IF x = SyntaxTree.invalidExpression THEN
  4289. 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
  4290. TRACE(type.sizeInBits);
  4291. TRACE(system.addressType.sizeInBits);
  4292. Error(x.position,Diagnostics.Invalid,"is no address type");
  4293. ELSE result := TRUE
  4294. END;
  4295. RETURN result
  4296. END CheckAddressType;
  4297. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4298. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4299. BEGIN
  4300. result := FALSE; type := x.type.resolved;
  4301. IF x = SyntaxTree.invalidExpression THEN
  4302. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4303. Error(x.position,Diagnostics.Invalid,"is no size type");
  4304. ELSE result := TRUE
  4305. END;
  4306. RETURN result
  4307. END CheckSizeType;
  4308. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4309. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4310. BEGIN
  4311. result := FALSE; type := x.type.resolved;
  4312. IF x = SyntaxTree.invalidExpression THEN
  4313. 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
  4314. Error(x.position,Diagnostics.Invalid,"is no object type");
  4315. ELSE result := TRUE
  4316. END;
  4317. RETURN result
  4318. END CheckObjectType;
  4319. (**
  4320. if expression x is of integer type then return true else report error and return false
  4321. **)
  4322. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4323. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4324. BEGIN
  4325. result := FALSE; type := x.type.resolved;
  4326. IF x = SyntaxTree.invalidExpression THEN
  4327. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4328. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4329. ELSE result := TRUE
  4330. END;
  4331. RETURN result
  4332. END CheckIntegerType;
  4333. (**
  4334. if expression x is of character type then return true else report error and return false
  4335. **)
  4336. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4337. VAR result: BOOLEAN;
  4338. BEGIN
  4339. result := FALSE;
  4340. IF x = SyntaxTree.invalidExpression THEN
  4341. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4342. Error(x.position,Diagnostics.Invalid,"is no character type");
  4343. ELSE result := TRUE
  4344. END;
  4345. RETURN result
  4346. END CheckCharacterType;
  4347. (**
  4348. if expression x is of real type then return true else report error and return false
  4349. **)
  4350. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4351. VAR result: BOOLEAN;
  4352. BEGIN
  4353. result := FALSE;
  4354. IF x = SyntaxTree.invalidExpression THEN
  4355. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4356. Error(x.position,Diagnostics.Invalid,"is no float type");
  4357. ELSE result := TRUE
  4358. END;
  4359. RETURN result
  4360. END CheckRealType;
  4361. (**
  4362. if expression x is of range type then return true else report error and return false
  4363. **)
  4364. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4365. VAR result: BOOLEAN;
  4366. BEGIN
  4367. result := FALSE;
  4368. IF x = SyntaxTree.invalidExpression THEN
  4369. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4370. Error(x.position,Diagnostics.Invalid,"is no range type");
  4371. ELSE result := TRUE
  4372. END;
  4373. RETURN result
  4374. END CheckRangeType;
  4375. (**
  4376. if expression x is of boolean type then return true else report error and return false
  4377. **)
  4378. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4379. VAR result: BOOLEAN;
  4380. BEGIN
  4381. result := FALSE;
  4382. IF x = SyntaxTree.invalidExpression THEN
  4383. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4384. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4385. ELSE result := TRUE
  4386. END;
  4387. RETURN result
  4388. END CheckBooleanType;
  4389. (**
  4390. if expression x is of set type then return true else report error and return false
  4391. **)
  4392. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4393. VAR result: BOOLEAN;
  4394. BEGIN
  4395. result := FALSE;
  4396. IF x = SyntaxTree.invalidExpression THEN
  4397. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4398. Error(x.position,Diagnostics.Invalid,"is no set type");
  4399. ELSE result := TRUE
  4400. END;
  4401. RETURN result
  4402. END CheckSetType;
  4403. (**
  4404. if expression x is of string or array of character type then return true else report error and return false
  4405. **)
  4406. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4407. VAR result: BOOLEAN;
  4408. BEGIN
  4409. result := FALSE;
  4410. IF x = SyntaxTree.invalidExpression THEN
  4411. ELSIF ~IsStringType(x.type.resolved) THEN
  4412. Error(x.position,Diagnostics.Invalid,"is no string type");
  4413. ELSE result := TRUE
  4414. END;
  4415. RETURN result
  4416. END CheckStringType;
  4417. (**
  4418. if expression x is a type declaration type return true else report error and return false
  4419. **)
  4420. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4421. VAR result: BOOLEAN;
  4422. BEGIN
  4423. result := FALSE;
  4424. IF x = SyntaxTree.invalidExpression THEN
  4425. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4426. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4427. ELSE result := TRUE
  4428. END;
  4429. RETURN result
  4430. END CheckTypeDeclarationType;
  4431. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4432. VAR result: BOOLEAN;
  4433. BEGIN
  4434. result := FALSE;
  4435. IF x = SyntaxTree.invalidExpression THEN
  4436. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4437. result := TRUE;
  4438. value := x.resolved(SyntaxTree.IntegerValue).value;
  4439. ELSE
  4440. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4441. END;
  4442. RETURN result;
  4443. END CheckIntegerValue;
  4444. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4445. BEGIN
  4446. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4447. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4448. ELSE
  4449. RETURN FALSE
  4450. END;
  4451. END IsUnsignedValue;
  4452. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4453. BEGIN
  4454. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4455. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4456. ELSE
  4457. RETURN FALSE
  4458. END
  4459. END IsAddressValue;
  4460. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4461. BEGIN
  4462. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4463. END IsAddressExpression;
  4464. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4465. BEGIN
  4466. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4467. END IsSizeExpression;
  4468. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4469. VAR result: BOOLEAN;
  4470. BEGIN
  4471. result := FALSE;
  4472. IF x = SyntaxTree.invalidExpression THEN
  4473. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4474. result := TRUE;
  4475. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4476. ELSE
  4477. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4478. END;
  4479. RETURN result;
  4480. END CheckEnumerationValue;
  4481. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4482. VAR result: BOOLEAN;
  4483. BEGIN
  4484. result := FALSE;
  4485. IF x = SyntaxTree.invalidExpression THEN
  4486. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4487. result := TRUE;
  4488. value := x.resolved(SyntaxTree.CharacterValue).value;
  4489. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4490. result := TRUE;
  4491. value := x.resolved(SyntaxTree.StringValue).value[0];
  4492. ELSE
  4493. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4494. END;
  4495. RETURN result;
  4496. END CheckCharacterValue;
  4497. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4498. VAR result: BOOLEAN;
  4499. BEGIN
  4500. result := FALSE;
  4501. IF x = SyntaxTree.invalidExpression THEN
  4502. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4503. value := x.resolved(SyntaxTree.IntegerValue).value;
  4504. IF (value > 0) OR includeZero & (value = 0) THEN
  4505. result := TRUE;
  4506. ELSE
  4507. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4508. END
  4509. ELSE
  4510. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4511. END;
  4512. RETURN result;
  4513. END CheckPositiveIntegerValue;
  4514. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4515. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4516. BEGIN
  4517. result := FALSE;
  4518. IF x = SyntaxTree.invalidExpression THEN
  4519. ELSE
  4520. type := x.type.resolved;
  4521. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4522. portType := type(SyntaxTree.PortType);
  4523. result := TRUE
  4524. ELSE
  4525. Error(x.position,Diagnostics.Invalid,"no port type");
  4526. END;
  4527. END;
  4528. RETURN result
  4529. END CheckPortType;
  4530. (* move to builtin procedure call statement ?
  4531. remove builtin procedure call designator ?
  4532. *)
  4533. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator): SyntaxTree.Expression;
  4534. VAR
  4535. numberActualParameters,numberFormalParameters: LONGINT;
  4536. formalParameter: SyntaxTree.Parameter;
  4537. actualParameter: SyntaxTree.Expression;
  4538. procedureType: SyntaxTree.ProcedureType;
  4539. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4540. inPort, outPort: SyntaxTree.PortType;
  4541. constructor: SyntaxTree.Procedure;
  4542. type0,type1,type2: SyntaxTree.Type;
  4543. type,base,parameterType: SyntaxTree.Type;
  4544. arrayType: SyntaxTree.ArrayType;
  4545. i,i0,i1: LONGINT;
  4546. r,r0,r1,im: LONGREAL;
  4547. c: CHAR;
  4548. id: LONGINT;
  4549. b: BOOLEAN;
  4550. mathArrayType: SyntaxTree.MathArrayType;
  4551. customBuiltin: SyntaxTree.CustomBuiltin;
  4552. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4553. VAR resultB: BOOLEAN;
  4554. BEGIN
  4555. IF numberActualParameters < from THEN
  4556. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4557. result := SyntaxTree.invalidExpression;
  4558. resultB := FALSE;
  4559. ELSIF numberActualParameters > to THEN
  4560. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4561. result := SyntaxTree.invalidExpression;
  4562. resultB := FALSE;
  4563. ELSE
  4564. resultB := TRUE;
  4565. END;
  4566. RETURN resultB
  4567. END CheckArity;
  4568. BEGIN
  4569. type := NIL; result := NIL;
  4570. type0 := NIL; type1 := NIL; type2 := NIL;
  4571. numberActualParameters := actualParameters.Length();
  4572. IF numberActualParameters>0 THEN
  4573. parameter0 := actualParameters.GetExpression(0);
  4574. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4575. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4576. result := SyntaxTree.invalidExpression
  4577. END
  4578. END;
  4579. IF numberActualParameters >1 THEN
  4580. parameter1 := actualParameters.GetExpression(1);
  4581. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4582. ELSE
  4583. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4584. result := SyntaxTree.invalidExpression
  4585. END
  4586. END;
  4587. IF numberActualParameters >2 THEN
  4588. parameter2 := actualParameters.GetExpression(2);
  4589. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4590. ELSE
  4591. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4592. result := SyntaxTree.invalidExpression
  4593. END
  4594. END;
  4595. id := builtin.id;
  4596. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4597. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4598. END;
  4599. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4600. ELSIF result # NIL THEN type := result.type (* operator *)
  4601. ELSE
  4602. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4603. result(SyntaxTree.Designator).SetLeft(left);
  4604. (* ---- ASSERT ----- *)
  4605. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4606. IF CheckBooleanType(parameter0) THEN
  4607. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4608. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4609. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4610. *)
  4611. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4612. Error(position,Diagnostics.Invalid,"assert failed");
  4613. END;
  4614. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4615. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4616. rules imposed by the architecture / current runtime
  4617. *)
  4618. END;
  4619. END;
  4620. (* ---- COPY ----- *)
  4621. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4622. IF~IsStringType(type0) THEN
  4623. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4624. END;
  4625. IF ~IsStringType(type1) THEN
  4626. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4627. ELSIF CheckVariable(parameter1) THEN
  4628. IF (type0 IS SyntaxTree.StringType) THEN
  4629. arrayType := type1(SyntaxTree.ArrayType);
  4630. IF arrayType.form = SyntaxTree.Static THEN
  4631. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4632. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4633. END;
  4634. END;
  4635. END;
  4636. END;
  4637. (* ---- INC, DEC----- *)
  4638. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4639. IF numberActualParameters = 1 THEN
  4640. parameter1 :=Global.NewIntegerValue(system,position,1);
  4641. actualParameters.AddExpression(parameter1);
  4642. END;
  4643. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4644. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4645. Error(position,Diagnostics.Invalid,"incompatible increment");
  4646. ELSE
  4647. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4648. actualParameters.SetExpression(1,parameter1);
  4649. END;
  4650. END;
  4651. (* ---- EXCL, INCL----- *)
  4652. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4653. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4654. IF IsIntegerValue(parameter1,i0) THEN
  4655. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4656. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4657. END;
  4658. END;
  4659. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4660. actualParameters.SetExpression(1,parameter1);
  4661. END;
  4662. (* ---- HALT, SYSTEM.HALT ----- *)
  4663. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4664. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4665. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4666. rules imposed by the architecture / current runtime
  4667. *)
  4668. END;
  4669. (* ---- WAIT ----- *)
  4670. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4671. IF CheckObjectType(parameter0) THEN
  4672. END;
  4673. (* ---- NEW ----- *)
  4674. ELSIF (id = Global.New) & CheckArity(1,Infinity) THEN
  4675. IF currentIsRealtime THEN
  4676. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4677. END;
  4678. (* check constructor *)
  4679. IF CheckVariable(parameter0) THEN
  4680. IF type0 IS SyntaxTree.PointerType THEN
  4681. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4682. END;
  4683. IF type0 IS SyntaxTree.ArrayType THEN
  4684. arrayType := type0(SyntaxTree.ArrayType);
  4685. IF arrayType.form = SyntaxTree.Static THEN
  4686. i := 1
  4687. ELSIF arrayType.form = SyntaxTree.Open THEN
  4688. i := Dimension(arrayType,{SyntaxTree.Open})+1;
  4689. ELSE HALT(100)
  4690. END;
  4691. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4692. i := 1;
  4693. REPEAT
  4694. actualParameter := actualParameters.GetExpression(i);
  4695. IF CheckSizeType(actualParameter) THEN
  4696. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4697. actualParameters.SetExpression(i,actualParameter);
  4698. END;
  4699. INC(i);
  4700. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4701. END;
  4702. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4703. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4704. IF constructor = NIL THEN
  4705. IF CheckArity(1,1) THEN END;
  4706. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4707. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4708. ELSE
  4709. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4710. numberFormalParameters := procedureType.numberParameters;
  4711. DEC(numberActualParameters);
  4712. IF numberActualParameters <= numberFormalParameters THEN
  4713. formalParameter := procedureType.firstParameter;
  4714. FOR i := 1 TO numberActualParameters DO
  4715. actualParameter := actualParameters.GetExpression(i);
  4716. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4717. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4718. ELSE
  4719. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4720. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4721. END;
  4722. actualParameters.SetExpression(i,actualParameter);
  4723. END;
  4724. formalParameter := formalParameter.nextParameter;
  4725. END;
  4726. WHILE (formalParameter # NIL) DO
  4727. IF formalParameter.defaultValue # NIL THEN
  4728. actualParameters.AddExpression(formalParameter.defaultValue);
  4729. formalParameter := formalParameter.nextParameter
  4730. ELSE
  4731. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4732. formalParameter := NIL;
  4733. END;
  4734. END;
  4735. ELSE
  4736. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4737. END;
  4738. END;
  4739. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4740. mathArrayType := type0(SyntaxTree.MathArrayType);
  4741. IF mathArrayType.form = SyntaxTree.Static THEN
  4742. Error(position,Diagnostics.Invalid,"new on static array");
  4743. ELSE
  4744. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4745. i0 := 2; i1 := Infinity;
  4746. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4747. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+1;
  4748. i1 := i0;
  4749. ELSE HALT(100);
  4750. END;
  4751. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4752. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4753. base := ArrayBase(type0,MAX(LONGINT));
  4754. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4755. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4756. IF ~CompatibleTo(system,type0,parameterType) THEN
  4757. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4758. result := SyntaxTree.invalidExpression;
  4759. ELSE
  4760. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4761. END;
  4762. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4763. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4764. IF ~CompatibleTo(system,type1,parameterType) THEN
  4765. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4766. result := SyntaxTree.invalidExpression;
  4767. ELSE
  4768. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4769. END;
  4770. ELSE
  4771. IF CheckArity(i0,i1) & (numberActualParameters >1) THEN
  4772. i := 1;
  4773. REPEAT
  4774. actualParameter := actualParameters.GetExpression(i);
  4775. IF CheckSizeType(actualParameter) THEN
  4776. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4777. actualParameters.SetExpression(i,actualParameter);
  4778. END;
  4779. INC(i);
  4780. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4781. END;
  4782. END;
  4783. END;
  4784. ELSIF type0 IS SyntaxTree.CellType THEN
  4785. IF ~(currentIsCellNet) THEN
  4786. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4787. ELSE
  4788. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4789. IF (constructor = NIL) & CheckArity(1,1) THEN
  4790. (* ok *)
  4791. ELSE
  4792. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4793. numberFormalParameters := procedureType.numberParameters;
  4794. DEC(numberActualParameters);
  4795. IF numberActualParameters <= numberFormalParameters THEN
  4796. formalParameter := procedureType.firstParameter;
  4797. FOR i := 1 TO numberActualParameters DO
  4798. actualParameter := actualParameters.GetExpression(i);
  4799. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4800. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4801. ELSE
  4802. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4803. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4804. END;
  4805. actualParameters.SetExpression(i,actualParameter);
  4806. END;
  4807. formalParameter := formalParameter.nextParameter;
  4808. END;
  4809. WHILE (formalParameter # NIL) DO
  4810. IF formalParameter.defaultValue # NIL THEN
  4811. actualParameters.AddExpression(formalParameter.defaultValue);
  4812. formalParameter := formalParameter.nextParameter
  4813. ELSE
  4814. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4815. formalParameter := NIL;
  4816. END;
  4817. END;
  4818. ELSE
  4819. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4820. END;
  4821. END;
  4822. END;
  4823. activeCellsStatement := TRUE;
  4824. ELSE
  4825. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4826. END;
  4827. END;
  4828. (* ---- DISPOSE ----- *)
  4829. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4830. IF ~IsPointerType(parameter0.type) THEN
  4831. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4832. ELSIF ~IsDisposable(parameter0.type) THEN
  4833. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4834. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4835. END
  4836. (* ---- GETPROCEDURE ----- *)
  4837. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4838. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4839. IF CheckVariable(parameter2) THEN
  4840. IF ~GetProcedureAllowed(parameter2.type) THEN
  4841. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4842. END;
  4843. END;
  4844. END;
  4845. (* ---- ABS ----- *)
  4846. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4847. (* note: ABS on complex numbers is done using overloading *)
  4848. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4849. type := type0;
  4850. IF IsIntegerValue(parameter0,i0) THEN
  4851. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4852. type := Global.GetIntegerType(system,ABS(i0));
  4853. ELSIF IsRealValue(parameter0,r) THEN
  4854. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4855. END;
  4856. ELSE
  4857. type := SyntaxTree.invalidType;
  4858. END;
  4859. (* ---- ASH, ASR ----- *)
  4860. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4861. type := type0;
  4862. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4863. (*
  4864. ConvertOperands(parameter0,parameter1); (* same type *)
  4865. *)
  4866. type := parameter0.type;
  4867. IF IsIntegerValue(parameter0,i0) THEN
  4868. IF IsIntegerValue(parameter1,i1) THEN
  4869. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4870. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4871. result := ResolveExpression(result);
  4872. type := Global.GetIntegerType(system,i0);
  4873. END;
  4874. END;
  4875. IF type.resolved.sizeInBits < 32 THEN
  4876. type := system.longintType;
  4877. END;
  4878. (*!compatibility with release, remove when resolved
  4879. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4880. *)
  4881. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4882. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4883. actualParameters.SetExpression(0,parameter0);
  4884. actualParameters.SetExpression(1,parameter1);
  4885. END;
  4886. (* ---- CAP ----- *)
  4887. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4888. type := system.characterType;
  4889. IF CheckCharacterType (parameter0) THEN
  4890. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4891. actualParameters.SetExpression(0,parameter0);
  4892. IF IsCharacterValue(parameter0,c) THEN
  4893. IF (c <= "z") & (c >= "a") THEN
  4894. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4895. ELSE
  4896. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4897. END;
  4898. END;
  4899. END;
  4900. (* ---- CHR ----- *)
  4901. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4902. IF id = Global.Chr THEN
  4903. type := system.characterType
  4904. ELSE
  4905. type := system.characterType32
  4906. END;
  4907. IF CheckIntegerType(parameter0) THEN
  4908. IF IsIntegerValue(parameter0,i0) THEN
  4909. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4910. result := ResolveExpression(result);
  4911. ELSE
  4912. (*
  4913. result := NewConversion(parameter0.position,parameter0,type);
  4914. *)
  4915. END;
  4916. END
  4917. (* ---- ENTIER ----- *)
  4918. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4919. type := system.longintType;
  4920. IF CheckRealType(parameter0) THEN
  4921. IF IsRealValue(parameter0,r) THEN
  4922. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4923. type := Global.GetIntegerType(system,ENTIER(r));
  4924. END
  4925. END;
  4926. (* ---- ENTIERH ----- *)
  4927. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4928. type := system.hugeintType;
  4929. IF CheckRealType(parameter0) THEN
  4930. IF IsRealValue(parameter0,r) THEN
  4931. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4932. END
  4933. END;
  4934. (* ---- LEN ----- *)
  4935. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4936. type := system.longintType;
  4937. base := type0;
  4938. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4939. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4940. actualParameters.SetExpression(0,parameter0);
  4941. type0 := parameter0.type.resolved;
  4942. base := type0;
  4943. END;
  4944. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4945. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4946. IF i1 < 0 THEN
  4947. Error(position,Diagnostics.Invalid,"invalid dimension");
  4948. base := SyntaxTree.invalidType;
  4949. ELSE
  4950. base := ArrayBase(base,i1);
  4951. IF (base # NIL) & Indexable(base) THEN
  4952. ELSE
  4953. Error(position,Diagnostics.Invalid,"len on no array");
  4954. IF VerboseErrorMessage THEN
  4955. Printout.Info("base",base);
  4956. END;
  4957. base := SyntaxTree.invalidType;
  4958. END;
  4959. END;
  4960. IF numberActualParameters=2 THEN
  4961. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4962. actualParameters.SetExpression(1,parameter1);
  4963. ELSIF base IS SyntaxTree.MathArrayType THEN
  4964. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4965. END;
  4966. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4967. IF base IS SyntaxTree.ArrayType THEN
  4968. arrayType := base(SyntaxTree.ArrayType);
  4969. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  4970. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4971. result := Global.NewIntegerValue(system,position,i);
  4972. type := result.type;(* arrayType.length.type;*)
  4973. ASSERT(type # NIL);
  4974. END;
  4975. ELSIF base IS SyntaxTree.MathArrayType THEN
  4976. mathArrayType := base(SyntaxTree.MathArrayType);
  4977. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  4978. result := Global.NewIntegerValue(system,position,i);
  4979. type := result.type;
  4980. (*
  4981. type := mathArrayType.length.type;
  4982. *)
  4983. ASSERT(type # NIL);
  4984. END;
  4985. END;
  4986. END;
  4987. ELSE
  4988. type := system.longintType;
  4989. END;
  4990. (* ---- FIRST ---- *)
  4991. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4992. type := system.longintType;
  4993. IF CheckRangeType(parameter0) THEN END;
  4994. result.SetAssignable(parameter0.assignable)
  4995. (* ---- LAST ---- *)
  4996. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4997. type := system.longintType;
  4998. IF CheckRangeType(parameter0) THEN END;
  4999. result.SetAssignable(parameter0.assignable)
  5000. (* ---- STEP ---- *)
  5001. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5002. type := system.longintType;
  5003. IF CheckRangeType(parameter0) THEN END;
  5004. result.SetAssignable(parameter0.assignable)
  5005. (* ---- RE ---- *)
  5006. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5007. IF CheckNumberType(parameter0) THEN
  5008. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5009. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5010. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5011. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5012. type := parameter0.type
  5013. ELSE
  5014. type := system.realType
  5015. END
  5016. END;
  5017. result.SetAssignable(parameter0.assignable)
  5018. (* ---- IM ---- *)
  5019. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5020. IF CheckNumberType(parameter0) THEN
  5021. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5022. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5023. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5024. ELSE
  5025. type := system.realType;
  5026. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5027. END
  5028. END;
  5029. result.SetAssignable(parameter0.assignable)
  5030. (* ---- MAX ----- *)
  5031. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5032. IF numberActualParameters = 1 THEN
  5033. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5034. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5035. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5036. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5037. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5038. *)
  5039. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5040. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5041. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5042. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5043. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5044. END;
  5045. ELSE
  5046. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5047. END
  5048. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5049. ConvertOperands(parameter0,parameter1);
  5050. actualParameters.SetExpression(0,parameter0);
  5051. actualParameters.SetExpression(1,parameter1);
  5052. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5053. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5054. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5055. END;
  5056. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5057. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5058. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5059. END;
  5060. END;
  5061. type := parameter0.type;
  5062. ELSE type := SyntaxTree.invalidType;
  5063. END;
  5064. (* ---- MIN ----- *)
  5065. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5066. IF numberActualParameters = 1 THEN
  5067. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5068. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5069. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5070. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5071. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5072. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5073. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5074. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5075. END;
  5076. ELSE
  5077. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5078. END
  5079. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5080. ConvertOperands(parameter0,parameter1);
  5081. actualParameters.SetExpression(0,parameter0);
  5082. actualParameters.SetExpression(1,parameter1);
  5083. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5084. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5085. ELSE result.SetResolved(parameter1.resolved)
  5086. END;
  5087. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5088. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5089. ELSE result.SetResolved(parameter1.resolved)
  5090. END;
  5091. END;
  5092. type := parameter0.type;
  5093. ELSE type := SyntaxTree.invalidType;
  5094. END;
  5095. (* ---- ODD ----- *)
  5096. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5097. type := system.booleanType;
  5098. IF CheckIntegerType(parameter0) THEN
  5099. IF IsIntegerValue(parameter0,i0) THEN
  5100. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5101. type := system.booleanType;
  5102. END;
  5103. END;
  5104. (* ---- ORD ----- *)
  5105. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5106. IF id = Global.Ord THEN
  5107. type := system.integerType;
  5108. ELSE
  5109. type := system.longintType;
  5110. END;
  5111. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5112. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5113. actualParameters.SetExpression(0,parameter0);
  5114. (* IF CheckCharacterType(parameter0) THEN*)
  5115. IF IsCharacterValue(parameter0,c)THEN
  5116. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5117. type := Global.GetSignedIntegerType(system,ORD(c));
  5118. END;
  5119. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5120. END;
  5121. (* ---- SHORT ----- *)
  5122. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5123. type := type0;
  5124. IF IsSignedIntegerType(type) THEN
  5125. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5126. ELSIF type = system.integerType THEN type := system.shortintType
  5127. ELSIF type = system.longintType THEN type := system.integerType
  5128. ELSIF type = system.hugeintType THEN type:= system.longintType
  5129. ELSE
  5130. CASE type.sizeInBits OF
  5131. 16: type := Global.Integer8
  5132. |32: type := Global.Integer16
  5133. |64: type := Global.Integer32
  5134. END;
  5135. END;
  5136. ELSIF type IS SyntaxTree.FloatType THEN
  5137. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5138. ELSIF type = system.longrealType THEN type := system.realType
  5139. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5140. END;
  5141. ELSIF type IS SyntaxTree.ComplexType THEN
  5142. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5143. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5144. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5145. END;
  5146. ELSE
  5147. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5148. END;
  5149. IF (parameter0.resolved # NIL) THEN
  5150. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5151. IF parameter0 IS SyntaxTree.Value THEN
  5152. result.SetResolved(parameter0(SyntaxTree.Value));
  5153. END;
  5154. END;
  5155. (* ---- LONG ----- *)
  5156. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5157. type := type0;
  5158. IF IsSignedIntegerType(type) THEN
  5159. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5160. ELSIF type = system.longintType THEN type := system.hugeintType
  5161. ELSIF type = system.integerType THEN type := system.longintType
  5162. ELSIF type = system.shortintType THEN type := system.integerType
  5163. ELSE
  5164. CASE type.sizeInBits OF
  5165. 8: type := Global.Integer16
  5166. |16: type := Global.Integer32
  5167. |32: type := Global.Integer64
  5168. END;
  5169. END;
  5170. ELSIF type IS SyntaxTree.FloatType THEN
  5171. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5172. ELSIF type= system.realType THEN type := system.longrealType
  5173. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5174. END;
  5175. ELSIF type IS SyntaxTree.ComplexType THEN
  5176. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5177. ELSIF type = system.complexType THEN type := system.longcomplexType
  5178. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5179. END;
  5180. ELSE
  5181. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5182. END;
  5183. IF (parameter0.resolved # NIL) THEN
  5184. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5185. IF parameter0 IS SyntaxTree.Value THEN
  5186. result.SetResolved(parameter0(SyntaxTree.Value));
  5187. END;
  5188. END;
  5189. (* ---- SIZE OF ----- *)
  5190. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5191. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5192. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5193. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5194. type := system.integerType;
  5195. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5196. ELSE
  5197. (* for variables, system sizeof could represent the physically occupied size
  5198. determined via the type descriptor, implement that ? *)
  5199. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5200. END
  5201. (* ---- SYSTEM.TRACE -----*)
  5202. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5203. FOR i := 0 TO numberActualParameters-1 DO
  5204. parameter0 := actualParameters.GetExpression(i);
  5205. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5206. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5207. END;
  5208. END;
  5209. (* remaining issues can only be tested in backend *)
  5210. (* ---- ADDRESSOF----- *)
  5211. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5212. IF HasAddress(parameter0) THEN
  5213. type := system.addressType;
  5214. ELSE
  5215. type := SyntaxTree.invalidType;
  5216. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5217. END;
  5218. (* ---- BIT ----- *)
  5219. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5220. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5221. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5222. actualParameters.SetExpression(0,parameter0);
  5223. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5224. actualParameters.SetExpression(1,parameter1);
  5225. END;
  5226. type := system.booleanType;
  5227. (* ----- MSK ---- *)
  5228. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5229. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5230. ConvertOperands(parameter0,parameter1);
  5231. actualParameters.SetExpression(0,parameter0);
  5232. actualParameters.SetExpression(1,parameter1);
  5233. END;
  5234. type := parameter0.type;
  5235. (* ---- SYSTEM.GET64 ----- *)
  5236. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5237. IF CheckAddressType(parameter0) THEN
  5238. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5239. actualParameters.SetExpression(0,parameter0);
  5240. END;
  5241. type := system.hugeintType;
  5242. (* ---- SYSTEM.GET32 ----- *)
  5243. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5244. IF CheckAddressType(parameter0) THEN
  5245. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5246. actualParameters.SetExpression(0,parameter0);
  5247. END;
  5248. type := system.longintType;
  5249. (* ---- SYSTEM.GET16 ----- *)
  5250. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5251. IF CheckAddressType(parameter0) THEN
  5252. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5253. actualParameters.SetExpression(0,parameter0);
  5254. END;
  5255. type := system.integerType;
  5256. (* ---- SYSTEM.GET8 ----- *)
  5257. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5258. IF CheckAddressType(parameter0) THEN
  5259. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5260. actualParameters.SetExpression(0,parameter0);
  5261. END;
  5262. type := system.shortintType;
  5263. (* ---- SYSTEM.GetStackPointer ----- *)
  5264. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5265. type := system.addressType;
  5266. (* ---- SYSTEM.GetFramePointer ----- *)
  5267. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5268. type := system.addressType;
  5269. (* ---- SYSTEM.GetActivity ----- *)
  5270. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5271. type := system.objectType;
  5272. (* ---- SYSTEM.SetStackPointer ----- *)
  5273. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5274. IF CheckAddressType(parameter0) THEN
  5275. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5276. actualParameters.SetExpression(0,parameter0);
  5277. END;
  5278. (* ---- SYSTEM.SetFramePointer ----- *)
  5279. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5280. IF CheckAddressType(parameter0) THEN
  5281. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5282. actualParameters.SetExpression(0,parameter0);
  5283. END;
  5284. (* ---- SYSTEM.SetActivity ----- *)
  5285. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5286. IF CheckObjectType(parameter0) THEN
  5287. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5288. actualParameters.SetExpression(0,parameter0);
  5289. END;
  5290. (* ---- LSH, LSL, ROT, ROR ----- *)
  5291. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5292. type := type0;
  5293. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5294. actualParameters.SetExpression(1, parameter1);
  5295. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5296. IF id = Global.Lsh THEN
  5297. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5298. ELSIF id = Global.Rot THEN
  5299. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5300. ELSIF id = Global.Ror THEN
  5301. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5302. END;
  5303. END;
  5304. (* ---- SYSTEM.VAL ----- *)
  5305. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5306. IF CheckTypeDeclarationType(parameter0) THEN
  5307. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5308. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5309. result := SyntaxTree.invalidExpression;
  5310. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5311. ELSE
  5312. IF (parameter1.resolved # NIL) THEN
  5313. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5314. IF parameter0 IS SyntaxTree.Value THEN
  5315. result.SetResolved(parameter0(SyntaxTree.Value));
  5316. END;
  5317. END;
  5318. result.SetAssignable(parameter1.assignable);
  5319. END;
  5320. END;
  5321. (* ---- SYSTEM.GET ----- *)
  5322. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5323. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5324. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5325. actualParameters.SetExpression(0,parameter0);
  5326. END;
  5327. (* ---- SYSTEM.PUT ----- *)
  5328. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5329. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5330. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5331. actualParameters.SetExpression(0,parameter0);
  5332. END;
  5333. (* ---- SYSTEM.PUT64 ----- *)
  5334. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5335. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5336. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5337. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5338. actualParameters.SetExpression(0,parameter0);
  5339. actualParameters.SetExpression(1,parameter1);
  5340. END;
  5341. (* ---- SYSTEM.PUT32 ----- *)
  5342. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5343. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5344. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5345. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5346. actualParameters.SetExpression(0,parameter0);
  5347. actualParameters.SetExpression(1,parameter1);
  5348. END;
  5349. (* ---- SYSTEM.PUT16 ----- *)
  5350. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5351. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5352. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5353. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5354. actualParameters.SetExpression(0,parameter0);
  5355. actualParameters.SetExpression(1,parameter1);
  5356. END;
  5357. (* ---- SYSTEM.PUT8 ----- *)
  5358. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5359. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5360. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5361. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5362. actualParameters.SetExpression(0,parameter0);
  5363. actualParameters.SetExpression(1,parameter1);
  5364. END;
  5365. (* ---- SYSTEM.MOVE ----- *)
  5366. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5367. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5368. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5369. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5370. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5371. actualParameters.SetExpression(0,parameter0);
  5372. actualParameters.SetExpression(1,parameter1);
  5373. actualParameters.SetExpression(2,parameter2);
  5374. END;
  5375. (* ---- SYSTEM.NEW ----- *)
  5376. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5377. IF ~IsPointerType(parameter0.type) THEN
  5378. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5379. ELSIF CheckSizeType(parameter1) THEN
  5380. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5381. actualParameters.SetExpression(1,parameter1);
  5382. END;
  5383. (* ----SYSTEM.REF ---- *)
  5384. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5385. type := system.addressType
  5386. (* ---- INCR ----- *)
  5387. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5388. type := system.sizeType;
  5389. base := type0;
  5390. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5391. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5392. IF i1 < 0 THEN
  5393. Error(position,Diagnostics.Invalid,"invalid dimension");
  5394. base := SyntaxTree.invalidType;
  5395. ELSE
  5396. base := ArrayBase(base,i1);
  5397. IF (base # NIL) & Indexable(base) THEN
  5398. ELSE
  5399. Error(position,Diagnostics.Invalid,"len on no array");
  5400. IF VerboseErrorMessage THEN
  5401. Printout.Info("base",base);
  5402. END;
  5403. base := SyntaxTree.invalidType;
  5404. END;
  5405. END;
  5406. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5407. actualParameters.SetExpression(1,parameter1);
  5408. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5409. mathArrayType := base(SyntaxTree.MathArrayType);
  5410. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5411. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5412. type := system.longintType;
  5413. END;
  5414. END;
  5415. ELSE
  5416. type := system.longintType;
  5417. END;
  5418. (* ---- SUM ----- *)
  5419. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5420. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5421. (* ---- ALL ----- *)
  5422. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5423. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5424. (* ---- DIM ----- *)
  5425. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5426. type := system.sizeType;
  5427. IF type0 IS SyntaxTree.MathArrayType THEN
  5428. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5429. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5430. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5431. END;
  5432. ELSE
  5433. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5434. END;
  5435. (* ---- CAS ----- *)
  5436. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5437. IF type0.IsComposite () THEN
  5438. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5439. result := SyntaxTree.invalidExpression;
  5440. ELSIF ~IsVariable (parameter0) THEN
  5441. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5442. result := SyntaxTree.invalidExpression;
  5443. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5444. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5445. result := SyntaxTree.invalidExpression;
  5446. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5447. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5448. result := SyntaxTree.invalidExpression;
  5449. ELSE
  5450. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5451. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5452. type := type0;
  5453. END;
  5454. (* ---- RESHAPE ----- *)
  5455. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5456. IF type0 IS SyntaxTree.MathArrayType THEN
  5457. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5458. base := ArrayBase(type0,MAX(LONGINT));
  5459. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5460. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5461. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5462. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5463. IF ~CompatibleTo(system,type0,parameterType) THEN
  5464. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5465. result := SyntaxTree.invalidExpression;
  5466. ELSE
  5467. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5468. END;
  5469. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5470. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5471. IF ~CompatibleTo(system,type1,parameterType) THEN
  5472. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5473. result := SyntaxTree.invalidExpression;
  5474. ELSE
  5475. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5476. END;
  5477. ELSE
  5478. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5479. result := SyntaxTree.invalidExpression;
  5480. END;
  5481. (* ---- SYSTEM.TYPECODE ----- *)
  5482. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5483. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5484. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5485. type := type.resolved;
  5486. IF type IS SyntaxTree.PointerType THEN
  5487. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5488. END;
  5489. IF ~(type IS SyntaxTree.RecordType) THEN
  5490. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5491. END;
  5492. ELSE
  5493. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5494. END;
  5495. type := system.addressType;
  5496. (* -------- FLT --------- *)
  5497. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5498. type := system.realType;
  5499. IF IsRealValue(parameter0, r) THEN
  5500. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5501. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5502. i0 := i; i := ABS(i);
  5503. IF i # 0 THEN
  5504. i1 := 23;
  5505. IF i >= 2*800000H THEN
  5506. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5507. ELSIF i < 800000H THEN
  5508. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5509. END;
  5510. i := (i1 + 127)*800000H - 800000H + i;
  5511. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5512. END;
  5513. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5514. END;
  5515. (* ------- CONNECT -------*)
  5516. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5517. IF ~(currentIsCellNet) THEN
  5518. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5519. END;
  5520. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5521. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5522. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5523. END;
  5524. IF numberActualParameters = 3 THEN
  5525. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5526. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5527. END;
  5528. *)
  5529. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5530. actualParameters.SetExpression(2,parameter2);
  5531. END;
  5532. activeCellsStatement := TRUE;
  5533. (* ---------- DELEGATE --------*)
  5534. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5535. IF ~(currentIsCellNet) THEN
  5536. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5537. END;
  5538. IF ~CheckPortType(parameter1, inPort) THEN
  5539. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5540. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5541. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5542. ELSIF (outPort.direction # inPort.direction) THEN
  5543. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5544. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5545. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5546. END;
  5547. activeCellsStatement := TRUE;
  5548. (* --------- RECEIVE ---------*)
  5549. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5550. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5551. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5552. IF inPort.direction # SyntaxTree.InPort THEN
  5553. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5554. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5555. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5556. END;
  5557. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5558. IF ~SameType(parameter2.type, system.integerType) THEN
  5559. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5560. END;
  5561. END;
  5562. END;
  5563. (* --------- SEND ---------*)
  5564. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5565. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5566. IF CheckPortType(parameter0,outPort) THEN
  5567. IF outPort.direction # SyntaxTree.OutPort THEN
  5568. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5569. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5570. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5571. ELSE
  5572. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5573. actualParameters.SetExpression(1,parameter1);
  5574. END;
  5575. END;
  5576. (* ------- custom builtins ----- *)
  5577. ELSIF id = Global.systemSpecial THEN
  5578. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5579. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5580. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5581. type := procedureType.returnType;
  5582. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5583. (* go through all formal parameters *)
  5584. formalParameter := procedureType.firstParameter;
  5585. FOR i := 0 TO actualParameters.Length() - 1 DO
  5586. actualParameter := actualParameters.GetExpression(i);
  5587. IF actualParameter = SyntaxTree.invalidExpression THEN
  5588. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5589. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5590. ELSE
  5591. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5592. END;
  5593. actualParameters.SetExpression(i, actualParameter);
  5594. formalParameter := formalParameter.nextParameter
  5595. END
  5596. END
  5597. ELSE
  5598. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5599. result := SyntaxTree.invalidExpression;
  5600. END;
  5601. END;
  5602. IF result # SyntaxTree.invalidExpression THEN
  5603. type := ResolveType(type);
  5604. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5605. result.SetType(type);
  5606. END;
  5607. RETURN result
  5608. END NewBuiltinCallDesignator;
  5609. (** return type guard designator left(type)
  5610. - check if type can be extended (i.e. is no static record)
  5611. - check if type is a type extension of left.type
  5612. - returns new type guard designator
  5613. returns invalidDesignator = invalidExpression if error
  5614. **)
  5615. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5616. VAR result: SyntaxTree.Designator;
  5617. BEGIN
  5618. result := SyntaxTree.invalidDesignator;
  5619. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5620. Error(position,Diagnostics.Invalid,"no type extension of type");
  5621. IF VerboseErrorMessage THEN
  5622. Printout.Info("left",left);
  5623. Printout.Info("type",type);
  5624. END;
  5625. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5626. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5627. ELSE
  5628. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5629. result.SetType(type);
  5630. result.SetAssignable(left.assignable);
  5631. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5632. END;
  5633. RETURN result
  5634. END NewTypeGuardDesignator;
  5635. (** check and resolve parameter designator left(expression list)
  5636. - check expression list
  5637. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5638. - elsif left is a procedure type then
  5639. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5640. - else return is a procedure call then return ProcedureCallDesignator
  5641. returns invalidDesignator = invalidExpression if error
  5642. **)
  5643. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5644. VAR
  5645. parameters: SyntaxTree.ExpressionList;
  5646. left: SyntaxTree.Designator;
  5647. result,expression: SyntaxTree.Expression;
  5648. typeDeclaration: SyntaxTree.TypeDeclaration;
  5649. type, expressionType: SyntaxTree.Type;
  5650. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5651. BEGIN
  5652. type := type.resolved;
  5653. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5654. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5655. END;
  5656. RETURN type
  5657. END BaseType;
  5658. BEGIN
  5659. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5660. result := SyntaxTree.invalidDesignator;
  5661. left := ResolveDesignator(designator.left);
  5662. IF left # SyntaxTree.invalidDesignator THEN
  5663. parameters := designator.parameters;
  5664. IF ExpressionList(parameters) THEN
  5665. IF (left.type = NIL) THEN
  5666. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5667. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5668. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5669. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5670. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5671. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5672. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5673. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left);
  5674. ELSE
  5675. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5676. END
  5677. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5678. expression := parameters.GetExpression(0);
  5679. type := typeDeclaration.declaredType.resolved;
  5680. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5681. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5682. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5683. OR (expressionType IS SyntaxTree.EnumerationType)
  5684. ) THEN
  5685. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5686. ELSE
  5687. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5688. END;
  5689. ELSE
  5690. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5691. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5692. result := SyntaxTree.invalidDesignator;
  5693. END;
  5694. ELSE
  5695. result := SyntaxTree.invalidDesignator
  5696. END;
  5697. END;
  5698. resolvedExpression := result;
  5699. END VisitParameterDesignator;
  5700. (** check dereference designator left^
  5701. - check if left is pointer type or left is object type
  5702. - return new dereference designator with type = left.baseType.type (if appropriate)
  5703. with error handling
  5704. returns invalidDesignator = invalidExpression if error
  5705. **)
  5706. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5707. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5708. BEGIN
  5709. result := SyntaxTree.invalidDesignator;
  5710. type := left.type;
  5711. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5712. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5713. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5714. result.SetAssignable(TRUE);
  5715. result.SetType(type);
  5716. result.SetHidden(left.isHidden);
  5717. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5718. type := type.resolved;
  5719. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5720. result.SetAssignable(TRUE);
  5721. result.SetType(type);
  5722. result.SetHidden(left.isHidden);
  5723. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5724. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5725. result.SetAssignable(TRUE);
  5726. result.SetType(type);
  5727. result.SetHidden(left.isHidden);
  5728. ELSE
  5729. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5730. IF VerboseErrorMessage THEN
  5731. Printout.Info("pointer", type);
  5732. Printout.Info("scope", currentScope);
  5733. END;
  5734. END;
  5735. RETURN result
  5736. END NewDereferenceDesignator;
  5737. (** check supercall designator left^
  5738. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5739. - return new supercall designator with type = left.type
  5740. with error handling
  5741. **)
  5742. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5743. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5744. objectScope: SyntaxTree.Scope;
  5745. BEGIN
  5746. result := SyntaxTree.invalidDesignator;
  5747. IF left = SyntaxTree.invalidDesignator THEN
  5748. (* error already handled *)
  5749. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5750. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5751. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5752. IF symbol IS SyntaxTree.Procedure THEN
  5753. procedure := symbol(SyntaxTree.Procedure);
  5754. objectScope := currentScope;
  5755. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5756. objectScope := objectScope.outerScope;
  5757. END;
  5758. IF (left.left = NIL) OR ~
  5759. (
  5760. (left.left IS SyntaxTree.SelfDesignator) OR
  5761. (left.left IS SyntaxTree.DereferenceDesignator)
  5762. & (left.left(SyntaxTree.Designator).left # NIL)
  5763. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5764. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5765. IF VerboseErrorMessage THEN
  5766. Printout.Info("left.left",left.left);
  5767. END;
  5768. ELSIF procedure.super # NIL THEN
  5769. result := SyntaxTree.NewSupercallDesignator(position,left);
  5770. result.SetType(left.type.resolved)
  5771. ELSE
  5772. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5773. END;
  5774. ELSE
  5775. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5776. END;
  5777. ELSE
  5778. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5779. END;
  5780. RETURN result
  5781. END NewSupercallDesignator;
  5782. (** check and semantically resolve arrow designator left^
  5783. - if left is procedure type -> result := SupercallDesignator
  5784. - else result := DereferenceDesignator
  5785. returns result via global variable resolvedExpression
  5786. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5787. **)
  5788. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5789. VAR left: SyntaxTree.Designator;
  5790. BEGIN
  5791. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5792. left := ResolveDesignator(arrowDesignator.left);
  5793. IF left # NIL THEN
  5794. IF (left.type = NIL) THEN
  5795. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5796. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5797. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5798. ELSE
  5799. IF IsPointerToObject(left.type) THEN
  5800. Warning(arrowDesignator.position, "forbidden dereference on object");
  5801. END;
  5802. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5803. END
  5804. END
  5805. END VisitArrowDesignator;
  5806. (** check and return expression
  5807. - if expression has no type then resolve expression
  5808. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5809. - return result
  5810. **)
  5811. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5812. VAR result,prev: SyntaxTree.Expression;
  5813. BEGIN
  5814. IF expression = NIL THEN result := NIL
  5815. ELSIF (expression.type = NIL) THEN
  5816. prev := resolvedExpression;
  5817. resolvedExpression := SyntaxTree.invalidExpression;
  5818. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5819. expression.SetType(SyntaxTree.invalidType);
  5820. END;
  5821. expression.Accept(SELF);
  5822. result := resolvedExpression;
  5823. IF currentIsRealtime THEN
  5824. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5825. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5826. END;
  5827. END;
  5828. (* designator modifiers for backends if they support it ...*)
  5829. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5830. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5831. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5832. END;
  5833. resolvedExpression := prev
  5834. ELSE
  5835. result := expression
  5836. END;
  5837. RETURN result
  5838. END ResolveExpression;
  5839. (**
  5840. check expression to be constant expression
  5841. - resolve expression
  5842. - if valid then check that of value type
  5843. report error and return invalidExpression if anything fails
  5844. **)
  5845. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5846. VAR position: LONGINT;
  5847. BEGIN
  5848. position := expression.position;
  5849. expression := ResolveExpression(expression);
  5850. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5851. ELSIF (expression.resolved = NIL) THEN
  5852. Error(position,Diagnostics.Invalid,"expression is not constant");
  5853. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5854. expression := SyntaxTree.invalidExpression;
  5855. END;
  5856. RETURN expression
  5857. END ConstantExpression;
  5858. (** check expression to be constant integer
  5859. - resolve expresssion
  5860. - if valid then check that of integer value type
  5861. report error and return invalidExpression if anything fails
  5862. **)
  5863. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5864. VAR position: LONGINT;
  5865. BEGIN
  5866. position := expression.position;
  5867. expression := ResolveExpression(expression);
  5868. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5869. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5870. expression := SyntaxTree.invalidExpression;
  5871. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5872. END;
  5873. RETURN expression
  5874. END ConstantInteger;
  5875. (** check expression as positive (>=0) constant integer
  5876. - resolve expression
  5877. - if valid then check that integer value
  5878. - if integer value then check that value >= 0
  5879. report error and return invalidExpression if anything fails
  5880. **)
  5881. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5882. VAR position: LONGINT;
  5883. BEGIN
  5884. position := expression.position;
  5885. expression := ConstantExpression(expression);
  5886. IF expression = SyntaxTree.invalidExpression THEN
  5887. (* error already reported *)
  5888. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5889. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5890. expression := SyntaxTree.invalidExpression
  5891. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5892. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5893. END;
  5894. RETURN expression
  5895. END ConstantIntegerGeq0;
  5896. (** check expression as condition
  5897. - resolve expression
  5898. - if valid expression then check that result type is boolean
  5899. report error and return invalidExpression if anything fails
  5900. **)
  5901. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5902. VAR position: LONGINT;
  5903. BEGIN
  5904. position := expression.position;
  5905. expression := ResolveExpression(expression);
  5906. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5907. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5908. expression := SyntaxTree.invalidExpression;
  5909. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5910. END;
  5911. RETURN expression
  5912. END ResolveCondition;
  5913. (*** symbols ***)
  5914. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5915. BEGIN
  5916. x.Accept(SELF);
  5917. END ResolveSymbol;
  5918. (** check a symbol
  5919. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5920. **)
  5921. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5922. VAR scope: SyntaxTree.Scope;
  5923. BEGIN
  5924. (* visibility *)
  5925. scope := symbol.scope;
  5926. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5927. scope := scope.outerScope;
  5928. END;
  5929. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5930. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5931. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5932. IF VerboseErrorMessage THEN
  5933. Printout.Info("symbol",symbol);
  5934. END;
  5935. END;
  5936. END;
  5937. END CheckSymbolVisibility;
  5938. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5939. If node is currently being resolved then emit a cyclic definition error.
  5940. Return TRUE only if node is fully resolved.
  5941. **)
  5942. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5943. VAR result: BOOLEAN;
  5944. BEGIN
  5945. IF SyntaxTree.Resolved IN x.state THEN
  5946. result := FALSE
  5947. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5948. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5949. result := FALSE;
  5950. ELSE
  5951. result := TRUE;
  5952. x.SetState(SyntaxTree.BeingResolved)
  5953. END;
  5954. RETURN result
  5955. END SymbolNeedsResolution;
  5956. (** check and resolve a type declaration symbol = Type
  5957. - set type to declaration type
  5958. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5959. This is so because the type declaration itself does not have a type but it only stands for a type.
  5960. In the implementation of the compiler this made a lot much easier.
  5961. - resolve and set declared type
  5962. - check symbol
  5963. **)
  5964. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5965. VAR prevScope: SyntaxTree.Scope;
  5966. BEGIN
  5967. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5968. IF SymbolNeedsResolution(typeDeclaration) THEN
  5969. prevScope := currentScope;
  5970. currentScope := typeDeclaration.scope;
  5971. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5972. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5973. CheckSymbolVisibility(typeDeclaration);
  5974. typeDeclaration.SetState(SyntaxTree.Resolved);
  5975. currentScope := prevScope;
  5976. END;
  5977. END VisitTypeDeclaration;
  5978. (** check and resolve a constant declaration symbol = (constant) expression
  5979. - check expression
  5980. - set type and value
  5981. - check symbol
  5982. **)
  5983. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  5984. VAR
  5985. expression: SyntaxTree.Expression;
  5986. type: SyntaxTree.Type;
  5987. name: Basic.SegmentedName;
  5988. replacement: Replacement;
  5989. stringReader: Streams.StringReader;
  5990. scanner: Scanner.Scanner; parser: Parser.Parser;
  5991. BEGIN
  5992. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  5993. IF SymbolNeedsResolution(constant) THEN
  5994. expression := constant.value;
  5995. IF replacements # NIL THEN
  5996. Global.GetSymbolSegmentedName(constant, name);
  5997. replacement := replacements;
  5998. WHILE (replacement # NIL) & (replacement.name # name) DO
  5999. replacement := replacement.next;
  6000. END;
  6001. IF replacement # NIL THEN
  6002. InfoSS(constant.position, "replacing constant", constant.name);
  6003. (*
  6004. NEW(stringReader, Strings.Length(replacement.string^));
  6005. stringReader.Set(replacement.string^);
  6006. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6007. NEW(parser, scanner, diagnostics);
  6008. expression := parser.Expression();
  6009. *)
  6010. expression := replacement.expression;
  6011. replacement.used := TRUE;
  6012. END;
  6013. END;
  6014. constant.SetType(SyntaxTree.invalidType);
  6015. expression := ConstantExpression(expression);
  6016. ASSERT(expression.type # NIL);
  6017. type := expression.type.resolved;
  6018. constant.SetType(type);
  6019. constant.SetValue(expression);
  6020. CheckSymbolVisibility(constant);
  6021. constant.SetState(SyntaxTree.Resolved);
  6022. END;
  6023. END VisitConstant;
  6024. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6025. VAR procedureAlignment: LONGINT;
  6026. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6027. (* least common multiple *)
  6028. VAR a,b: LONGINT;
  6029. BEGIN
  6030. a := a0; b := b0;
  6031. WHILE (a # b) DO
  6032. IF a < b THEN a := a+a0
  6033. ELSE b := b + b0
  6034. END;
  6035. END;
  6036. RETURN a
  6037. END LCM;
  6038. BEGIN
  6039. IF alignment > 1 THEN
  6040. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6041. IF (procedureAlignment > 1) THEN
  6042. alignment := LCM(alignment, procedureAlignment);
  6043. END;
  6044. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6045. END;
  6046. END AdaptStackAlignment;
  6047. (** check and resolve a variable / field
  6048. - check and set type
  6049. - negative check on open array type
  6050. - check symbol
  6051. **)
  6052. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6053. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6054. BEGIN
  6055. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6056. IF SymbolNeedsResolution(variable) THEN
  6057. modifiers := variable.modifiers;
  6058. (*
  6059. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6060. variable.AddFlags(flags);
  6061. *)
  6062. variable.SetType(ResolveType(variable.type));
  6063. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6064. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6065. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6066. END;
  6067. END;
  6068. CheckSymbolVisibility(variable);
  6069. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6070. variable.SetUntraced(TRUE);
  6071. IF ~ContainsPointer(variable.type) THEN
  6072. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6073. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6074. END;
  6075. END;
  6076. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6077. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6078. IF ~PowerOf2(value) THEN
  6079. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6080. ELSE
  6081. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6082. END;
  6083. END;
  6084. variable.SetAlignment(FALSE,value);
  6085. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6086. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6087. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6088. END;
  6089. variable.SetAlignment(TRUE, value);
  6090. END;
  6091. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6092. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6093. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6094. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6095. END;
  6096. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6097. IF variable.initializer # NIL THEN
  6098. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6099. END;
  6100. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6101. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6102. pointerType.SetPointerBase(variable.type);
  6103. pointerType.SetHidden(TRUE);
  6104. variable.SetType(ResolveType(pointerType));
  6105. END;
  6106. variable.SetState(SyntaxTree.Resolved);
  6107. END;
  6108. END VisitVariable;
  6109. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6110. BEGIN
  6111. VisitVariable(property)
  6112. END VisitProperty;
  6113. (** check and resolve a (procedure) parameter
  6114. - check and set type
  6115. - check symbol
  6116. - check parameter kind and set read-only flags if appropriate
  6117. **)
  6118. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6119. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6120. BEGIN
  6121. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6122. IF SymbolNeedsResolution(parameter) THEN
  6123. modifiers := parameter.modifiers;
  6124. parameter.SetType(ResolveType(parameter.type));
  6125. ASSERT(parameter.type.resolved # NIL);
  6126. CheckSymbolVisibility(parameter);
  6127. IF parameter.defaultValue # NIL THEN
  6128. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6129. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6130. ELSE
  6131. expression := ConstantExpression(parameter.defaultValue);
  6132. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6133. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6134. parameter.SetDefaultValue(expression);
  6135. END;
  6136. END;
  6137. END;
  6138. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6139. parameter.SetUntraced(TRUE);
  6140. IF ~ContainsPointer(parameter.type) THEN
  6141. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6142. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6143. END;
  6144. END;
  6145. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6146. parameter.SetMoveable(TRUE);
  6147. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6148. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6149. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6150. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6151. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6152. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6153. END;
  6154. END;
  6155. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6156. parameter.SetState(SyntaxTree.Resolved);
  6157. END;
  6158. END VisitParameter;
  6159. (** check and resolve a procedure (with declaration and implementation scope)
  6160. - check the procedure type
  6161. - check if method (i.e. in record scope), if so then
  6162. - check if (unique) constructor
  6163. - check if (unique) finalizer
  6164. - check if super method available, if so then check signature
  6165. - of not in record scope then negative check on constructor flag
  6166. - of not in record scope then negative check on finalizer flag
  6167. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6168. - check procedure symbol
  6169. **)
  6170. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6171. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6172. procedureType: SyntaxTree.ProcedureType;
  6173. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6174. qualifiedType: SyntaxTree.QualifiedType;
  6175. value: LONGINT;
  6176. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6177. position: LONGINT;
  6178. BEGIN
  6179. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6180. IF IsOberonInline(procedure) THEN
  6181. IF SyntaxTree.Public * procedure.access # {} THEN
  6182. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6183. END;
  6184. procedure.SetInline(FALSE);
  6185. procedure.SetOberonInline(TRUE);
  6186. END;
  6187. IF SymbolNeedsResolution(procedure) THEN
  6188. recentIsRealtime := currentIsRealtime;
  6189. recentIsBodyProcedure := currentIsBodyProcedure;
  6190. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6191. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6192. modifiers := procedureType.modifiers;
  6193. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6194. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6195. IF useDarwinCCalls THEN (*fld*)
  6196. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6197. ELSE
  6198. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6199. END
  6200. END;
  6201. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6202. procedureType.SetInterrupt(TRUE);
  6203. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6204. END;
  6205. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6206. procedureType.SetNoReturn(TRUE);
  6207. END;
  6208. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6209. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6210. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6211. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6212. END;
  6213. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6214. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6215. END;
  6216. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6217. IF ~PowerOf2(value) THEN
  6218. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6219. ELSE
  6220. procedureType.SetStackAlignment(value)
  6221. END;
  6222. END;
  6223. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6224. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6225. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6226. END;
  6227. CheckModifiers(modifiers, TRUE);
  6228. modifiers := procedureType.returnTypeModifiers;
  6229. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6230. CheckModifiers(modifiers, TRUE);
  6231. FixProcedureType(procedureType);
  6232. currentIsRealtime := procedureType.isRealtime;
  6233. currentIsBodyProcedure := procedure.isBodyProcedure;
  6234. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6235. THEN
  6236. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6237. END;
  6238. CheckSymbolVisibility(procedure);
  6239. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6240. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6241. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6242. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6243. END;
  6244. END;
  6245. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6246. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6247. procedureType.SetDelegate(TRUE);
  6248. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6249. IF record.pointerType.typeDeclaration = NIL THEN
  6250. selfParameter.SetType(record.pointerType);
  6251. ELSE
  6252. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6253. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6254. qualifiedType.SetResolved(record.pointerType);
  6255. selfParameter.SetType(qualifiedType);
  6256. END;
  6257. selfParameter.SetAccess(SyntaxTree.Hidden);
  6258. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6259. IF procedure.isConstructor THEN
  6260. (*! constructor is always visible, compatibility to paco
  6261. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6262. *)
  6263. procedure.MarkUsed;
  6264. IF procedureType.returnType # NIL THEN
  6265. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6266. END;
  6267. proc := procedure.scope.firstProcedure;
  6268. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6269. proc := proc.nextProcedure;
  6270. END;
  6271. IF proc # NIL THEN
  6272. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6273. ELSE
  6274. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6275. END;
  6276. END;
  6277. IF procedure.isFinalizer THEN
  6278. procedure.MarkUsed;
  6279. IF procedureType.returnType # NIL THEN
  6280. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6281. END;
  6282. IF procedureType.numberParameters # 0 THEN
  6283. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6284. END;
  6285. proc := procedure.scope.firstProcedure;
  6286. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6287. proc := proc.nextProcedure;
  6288. END;
  6289. IF proc # NIL THEN
  6290. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6291. ELSE
  6292. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6293. END;
  6294. END;
  6295. super := FindSuperProcedure(record.recordScope, procedure);
  6296. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6297. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6298. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6299. END;
  6300. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6301. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6302. END;
  6303. IF super.isFinal THEN
  6304. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6305. END;
  6306. procedure.SetSuper(super);
  6307. super.SetOverwritten(TRUE);
  6308. procedure.SetAccess(procedure.access+super.access);
  6309. procedure.MarkUsed;
  6310. END;
  6311. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6312. THEN
  6313. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6314. END;
  6315. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6316. IF cellsAreObjects THEN
  6317. procedureType.SetDelegate(TRUE);
  6318. END;
  6319. IF procedure.isConstructor THEN
  6320. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6321. END;
  6322. ELSIF procedure.isConstructor THEN
  6323. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6324. END;
  6325. Declarations(procedure.procedureScope);
  6326. (* body resolution part done as late fix of the procedure type *)
  6327. procedure.SetState(SyntaxTree.Resolved);
  6328. currentIsRealtime := recentIsRealtime;
  6329. currentIsBodyProcedure := recentIsBodyProcedure;
  6330. END;
  6331. END VisitProcedure;
  6332. (**
  6333. a builtin procedure is a global item that may not be modified locally
  6334. instead the resolving of builtin procedure calls are done in the NewBuiltinCallDesignator
  6335. **)
  6336. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6337. VAR type: SyntaxTree.Type;
  6338. BEGIN
  6339. type := ResolveType(builtinProcedure.type);
  6340. END VisitBuiltin;
  6341. (* nopov *)
  6342. (** check and resolve operator
  6343. - operators are first checked as procedures
  6344. - then additional operator-specific checks are done
  6345. - note that only module-scope operators are checked here
  6346. (operators in a record scope are only allowed in the context of
  6347. array-structured object types and checked in 'ResolveArrayStructure')
  6348. - also note that inter-operator conformity is not checked here
  6349. **)
  6350. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6351. VAR
  6352. procedureType: SyntaxTree.ProcedureType;
  6353. leftType, rightType: SyntaxTree.Type;
  6354. identifierNumber, position: LONGINT;
  6355. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6356. modifiers: SyntaxTree.Modifier;
  6357. (** whether a type is locally defined in the current module scope
  6358. for arrays, the base type must be locally defined **)
  6359. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6360. BEGIN
  6361. IF type = NIL THEN
  6362. RETURN FALSE
  6363. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6364. RETURN TRUE
  6365. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6366. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6367. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6368. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6369. ELSE
  6370. RETURN FALSE
  6371. END
  6372. END IsLocallyDefined;
  6373. BEGIN
  6374. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6375. procedureType := operator.type(SyntaxTree.ProcedureType);
  6376. modifiers := procedureType.modifiers;
  6377. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6378. CheckModifiers(modifiers, TRUE);
  6379. VisitProcedure(operator);
  6380. IF operator.scope IS SyntaxTree.RecordScope THEN
  6381. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6382. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6383. IF identifierNumber = -1 THEN
  6384. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6385. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6386. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6387. ELSE
  6388. IF procedureType.numberParameters < 1 THEN
  6389. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6390. ELSIF procedureType.numberParameters > 2 THEN
  6391. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6392. ELSE
  6393. (* determine operand types *)
  6394. leftType := procedureType.firstParameter.type;
  6395. IF procedureType.numberParameters > 1 THEN
  6396. rightType := procedureType.firstParameter.nextParameter.type
  6397. ELSE
  6398. rightType := NIL
  6399. END;
  6400. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6401. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6402. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6403. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6404. END
  6405. END;
  6406. (* TODO: refine the checks, think about how restrictive the checks should be
  6407. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6408. They might be used for intersection, union, complement of custom object types *)
  6409. (* defaults *)
  6410. hasReturnType := TRUE;
  6411. mustBeUnary := FALSE;
  6412. mustBeBinary := FALSE;
  6413. mustReturnBoolean := FALSE;
  6414. mustReturnInteger := FALSE;
  6415. mustHaveEquitypedOperands := FALSE;
  6416. (* operator-specific exceptions *)
  6417. CASE identifierNumber OF
  6418. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6419. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6420. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6421. mustBeBinary := TRUE
  6422. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6423. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6424. | Scanner.Times: mustBeBinary := TRUE
  6425. | Scanner.TimesTimes: mustBeBinary := TRUE
  6426. | Scanner.DotTimes: mustBeBinary := TRUE
  6427. | Scanner.PlusTimes: mustBeBinary := TRUE
  6428. | Scanner.Slash: mustBeBinary := TRUE
  6429. | Scanner.Backslash: mustBeBinary := TRUE
  6430. | Scanner.DotSlash: mustBeBinary := TRUE
  6431. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6432. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6433. | Scanner.Not: mustBeUnary := TRUE
  6434. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6435. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6436. | Scanner.Transpose: mustBeUnary := TRUE;
  6437. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6438. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6439. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6440. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6441. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6442. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6443. | Global.Abs: mustBeUnary := TRUE;
  6444. | Global.Ash: (* TODO: arity? *)
  6445. | Global.Cap: (* TODO: arity? *)
  6446. | Global.Chr: mustBeUnary := TRUE;
  6447. | Global.Entier: (* TODO: arity? *)
  6448. | Global.EntierH: (* TODO: arity? *)
  6449. | Global.Len: (* unary and binary *)
  6450. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6451. | Global.Max, Global.Min: (* unary and binary *)
  6452. | Global.Odd: (* TODO: arity? *)
  6453. | Global.Sum: (* TODO: arity? *)
  6454. | Global.All: (* TODO: arity? *)
  6455. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6456. | Scanner.Alias:
  6457. | Scanner.GreaterGreater, Scanner.LessLess:
  6458. mustBeBinary := TRUE; hasReturnType := FALSE;
  6459. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6460. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6461. END;
  6462. (* check parameter count *)
  6463. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6464. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6465. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6466. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6467. END;
  6468. (* check parameter types *)
  6469. (* TODO: is this used at all? *)
  6470. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6471. leftType := procedureType.firstParameter.type;
  6472. rightType := procedureType.firstParameter.nextParameter.type;
  6473. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6474. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6475. END
  6476. END;
  6477. (* check return type *)
  6478. IF hasReturnType THEN
  6479. IF procedureType.returnType = NIL THEN
  6480. Error(operator.position, Diagnostics.Invalid, "return type required")
  6481. ELSIF mustReturnBoolean THEN
  6482. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6483. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6484. END
  6485. ELSIF mustReturnInteger THEN
  6486. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6487. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6488. END
  6489. END
  6490. ELSIF procedureType.returnType # NIL THEN
  6491. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6492. END
  6493. END
  6494. END
  6495. END
  6496. END VisitOperator;
  6497. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6498. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6499. BEGIN
  6500. IF error THEN RETURN FALSE END;
  6501. prevScope := currentScope;
  6502. prevDiagnostics := diagnostics;
  6503. diagnostics := NIL; (* suppress error output *)
  6504. currentScope := module.moduleScope;
  6505. VisitImport(x);
  6506. IF ~error THEN
  6507. module.moduleScope.AddImport(x);
  6508. x.SetScope(module.moduleScope);
  6509. END;
  6510. currentScope := prevScope;
  6511. diagnostics := prevDiagnostics;
  6512. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6513. END AddImport;
  6514. (** check and resolve import
  6515. - check for name = SYSTEM
  6516. - check for forbidden self import
  6517. - search through global import cache: already imported?
  6518. - check if already imported indirectly
  6519. - import if necessary -> set module and enter into import cache
  6520. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6521. - after this import this direct import and all indirect imports are stored in the current module's import list
  6522. **)
  6523. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6524. VAR
  6525. module: SyntaxTree.Module;
  6526. moduleScope: SyntaxTree.ModuleScope;
  6527. import,reimport: SyntaxTree.Import;
  6528. filename: FileName;
  6529. prevScope: SyntaxTree.Scope;
  6530. BEGIN
  6531. IF SymbolNeedsResolution(x) THEN
  6532. prevScope := currentScope;
  6533. x.SetType(SyntaxTree.importType);
  6534. moduleScope := currentScope.ownerModule.moduleScope;
  6535. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6536. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6537. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6538. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6539. ELSE
  6540. (* search through global import list: already imported ? *)
  6541. IF (x.module = NIL) & (importCache # NIL) THEN
  6542. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6543. ELSE import := NIL
  6544. END;
  6545. IF x.module # NIL THEN (* already imported indirectly *)
  6546. module := x.module;
  6547. ELSIF import # NIL THEN (* already in module list *)
  6548. module := import.module;
  6549. ASSERT(module # NIL);
  6550. x.SetModule(module);
  6551. ELSE (* must be imported *)
  6552. Global.ModuleFileName(x.moduleName,x.context,filename);
  6553. IF symbolFileFormat # NIL THEN
  6554. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6555. IF module = NIL THEN
  6556. ErrorSS(x.position,"could not import",filename);
  6557. IF VerboseErrorMessage THEN
  6558. Printout.Info("import",x)
  6559. END
  6560. ELSE
  6561. (*
  6562. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6563. (*! should rather be done by importer *)
  6564. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6565. checker.importCache := importCache;
  6566. checker.arrayBaseImported := arrayBaseImported;
  6567. checker.global := global;
  6568. checker.Module(module); (* semantic check *)
  6569. error := error OR checker.error;
  6570. END;
  6571. *)
  6572. (*
  6573. ASSERT(SyntaxTree.Resolved IN module.state);
  6574. *)
  6575. x.SetModule(module);
  6576. IF importCache # NIL THEN
  6577. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6578. import.SetContext(x.context);
  6579. import.SetModule(module);
  6580. importCache.AddImport(import);
  6581. END;
  6582. END;
  6583. ELSE
  6584. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6585. END;
  6586. END;
  6587. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6588. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6589. END;
  6590. import := module.moduleScope.firstImport;
  6591. WHILE(import # NIL) DO
  6592. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6593. ASSERT(currentScope # NIL);
  6594. ASSERT(currentScope.ownerModule # NIL);
  6595. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6596. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6597. Error(x.position,Diagnostics.Invalid,"recursive import");
  6598. ELSE
  6599. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6600. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6601. IF reimport = NIL THEN (* indirect import *)
  6602. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6603. reimport.SetContext(import.context);
  6604. reimport.SetModule(import.module);
  6605. moduleScope.AddImport(reimport);
  6606. reimport.SetScope(moduleScope);
  6607. ELSE
  6608. ASSERT(import.module # NIL);
  6609. reimport.SetModule(import.module); (* direct or indirect import *)
  6610. END;
  6611. END;
  6612. import := import.nextImport;
  6613. END;
  6614. END;
  6615. END;
  6616. currentScope := prevScope;
  6617. (* ELSE nothing to be done *)
  6618. x.SetState(SyntaxTree.Resolved);
  6619. END;
  6620. END VisitImport;
  6621. (*** statements ***)
  6622. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6623. VAR prev,resolved: SyntaxTree.Statement;
  6624. BEGIN
  6625. prev := resolvedStatement;
  6626. resolvedStatement := x;
  6627. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6628. activeCellsStatement := FALSE;
  6629. x.Accept(SELF);
  6630. (* removed this, implementation restriction should be resolved by backend
  6631. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6632. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6633. END;
  6634. *)
  6635. resolved := resolvedStatement;
  6636. resolvedStatement := prev;
  6637. RETURN resolved
  6638. END ResolveStatement;
  6639. (** check and resolve statement sequence
  6640. - check all statements, replace if necessary
  6641. **)
  6642. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6643. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6644. BEGIN
  6645. IF statementSequence # NIL THEN (* else empty *)
  6646. FOR i := 0 TO statementSequence.Length()-1 DO
  6647. statement := statementSequence.GetStatement(i);
  6648. resolved := ResolveStatement(statement);
  6649. IF (resolved # statement) THEN
  6650. statementSequence.SetStatement(i,resolved);
  6651. END;
  6652. END;
  6653. END;
  6654. END StatementSequence;
  6655. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6656. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6657. - check if procedure is callable
  6658. - check return type = NIL (otherwise must be assignment statement)
  6659. **)
  6660. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6661. VAR call: SyntaxTree.Designator;
  6662. BEGIN
  6663. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6664. call := procedureCall.call;
  6665. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6666. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6667. END;
  6668. call := ResolveDesignator(call);
  6669. IF call = SyntaxTree.invalidDesignator THEN
  6670. (* error already handled *)
  6671. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6672. (* inline call in a statement *)
  6673. ELSIF ~IsCallable(call) THEN
  6674. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6675. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6676. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6677. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6678. END;
  6679. procedureCall.SetCall(call);
  6680. (*
  6681. IF call = SyntaxTree.invalidDesignator THEN
  6682. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6683. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6684. IF IsOberonInline(procedure) THEN
  6685. Warning(procedure.position,"call to inline proc");
  6686. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6687. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6688. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6689. resolvedStatement := block;
  6690. RETURN;
  6691. END;
  6692. END;
  6693. *)
  6694. END VisitProcedureCallStatement;
  6695. (** check and resolve assignment LHS := RHS
  6696. - resolve LHS and RHS
  6697. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6698. - check if assignment is compatible
  6699. - check if LHS is variable (i.e. assignable)
  6700. - convert RHS if necessary
  6701. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6702. - assignment between different ASOTs
  6703. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6704. - assignment to ASOT elements:
  6705. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6706. **)
  6707. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6708. VAR
  6709. left: SyntaxTree.Designator;
  6710. right, expression: SyntaxTree.Expression;
  6711. designator: SyntaxTree.Designator;
  6712. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6713. mathArrayType: SyntaxTree.MathArrayType;
  6714. BEGIN
  6715. right := ResolveExpression(assignment.right);
  6716. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6717. left := ResolveDesignator(assignment.left);
  6718. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6719. (* error already handled *)
  6720. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6721. (* LHS is index write operator call on ASOT *)
  6722. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6723. (* necessary ?
  6724. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6725. type := procedureType.firstParameter.type;
  6726. expression := procedureCallDesignator.parameters.GetExpression(0);
  6727. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6728. *)
  6729. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6730. ELSIF CheckVariable(left) THEN
  6731. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6732. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6733. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6734. (* conversion done by procedure call
  6735. (* try to convert to left argument *)
  6736. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6737. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6738. procedureCallDesignator.parameters.SetExpression(1, right);
  6739. END;
  6740. *)
  6741. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6742. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6743. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6744. ELSIF AssignmentCompatible(left, right) THEN
  6745. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6746. mathArrayType := MathArrayStructureOfType(left.type);
  6747. right := NewConversion(right.position, right, mathArrayType, NIL);
  6748. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6749. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6750. ELSE
  6751. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6752. assignment.SetLeft(left);
  6753. assignment.SetRight(right);
  6754. resolvedStatement := assignment
  6755. END
  6756. END
  6757. END
  6758. END VisitAssignment;
  6759. (** check and resolve assignment LHS := RHS
  6760. - resolve LHS and RHS
  6761. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6762. - check if assignment is compatible
  6763. - check if LHS is variable (i.e. assignable)
  6764. - convert RHS if necessary
  6765. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6766. - assignment between different ASOTs
  6767. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6768. - assignment to ASOT elements:
  6769. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6770. **)
  6771. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6772. VAR
  6773. left: SyntaxTree.Designator;
  6774. right: SyntaxTree.Expression;
  6775. inPort, outPort: SyntaxTree.PortType;
  6776. expression: SyntaxTree.Expression;
  6777. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6778. BEGIN
  6779. right := ResolveExpression(communication.right);
  6780. left := ResolveDesignator(communication.left);
  6781. communication.SetLeft(left);
  6782. communication.SetRight(right);
  6783. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6784. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6785. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6786. (* conversion done by procedure call
  6787. (* try to convert to left argument *)
  6788. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6789. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6790. procedureCallDesignator.parameters.SetExpression(1, right);
  6791. END;
  6792. *)
  6793. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6794. ELSE
  6795. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6796. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6797. (* error already handled *)
  6798. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6799. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6800. IF outPort.direction # SyntaxTree.OutPort THEN
  6801. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6802. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6803. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6804. ELSE
  6805. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6806. communication.SetRight(right)
  6807. END;
  6808. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6809. IF CheckVariable(left) THEN
  6810. IF inPort.direction # SyntaxTree.InPort THEN
  6811. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6812. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6813. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6814. END;
  6815. END;
  6816. ELSE
  6817. Error(communication.position, -1, "unsupported stream operation");
  6818. END;
  6819. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6820. IF outPort.direction # SyntaxTree.OutPort THEN
  6821. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6822. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6823. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6824. ELSE
  6825. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6826. communication.SetRight(right)
  6827. END;
  6828. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6829. IF CheckVariable(right) THEN
  6830. IF inPort.direction # SyntaxTree.InPort THEN
  6831. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6832. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6833. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6834. END;
  6835. END;
  6836. ELSE
  6837. Error(communication.position, -1, "unsupported operation");
  6838. END;
  6839. END;
  6840. END VisitCommunicationStatement;
  6841. (** check and resolve if/eslif part
  6842. - check condition
  6843. - check statement sequence
  6844. **)
  6845. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6846. VAR prevUnreachable, b: BOOLEAN;
  6847. BEGIN
  6848. prevUnreachable := currentIsUnreachable;
  6849. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6850. IF IsBooleanValue(ifPart.condition,b) THEN
  6851. IF b=FALSE THEN
  6852. currentIsUnreachable := TRUE
  6853. ELSIF b=TRUE THEN
  6854. true := TRUE
  6855. END;
  6856. END;
  6857. StatementSequence(ifPart.statements);
  6858. currentIsUnreachable := prevUnreachable;
  6859. END IfPart;
  6860. (** check and resolve if statement
  6861. - check if parts and else part statement sequence
  6862. **)
  6863. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6864. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6865. BEGIN
  6866. prevUnreachable := currentIsUnreachable;
  6867. ifPartTrue := FALSE;
  6868. IfPart(ifStatement.ifPart,ifPartTrue);
  6869. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6870. elsif := ifStatement.GetElsifPart(i);
  6871. IfPart(elsif,ifPartTrue);
  6872. END;
  6873. IF ifStatement.elsePart # NIL THEN
  6874. IF ifPartTrue THEN
  6875. currentIsUnreachable := TRUE
  6876. END;
  6877. StatementSequence(ifStatement.elsePart)
  6878. END;
  6879. currentIsUnreachable := prevUnreachable;
  6880. END VisitIfStatement;
  6881. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6882. VAR variable: SyntaxTree.Designator;
  6883. type,variableType: SyntaxTree.Type;
  6884. withEntry: WithEntry;
  6885. BEGIN
  6886. variable := ResolveDesignator(withPart.variable);
  6887. variableType := variable.type.resolved;
  6888. withPart.SetVariable(variable);
  6889. type := ResolveType(withPart.type);
  6890. withPart.SetType(type);
  6891. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6892. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6893. END;
  6894. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6895. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6896. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6897. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6898. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6899. IF VerboseErrorMessage THEN
  6900. Printout.Info("variable",variable)
  6901. END;
  6902. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6903. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6904. IF VerboseErrorMessage THEN
  6905. Printout.Info("variable",variable);
  6906. Printout.Info("type",type);
  6907. END;
  6908. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6909. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6910. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6911. IF VerboseErrorMessage THEN
  6912. Printout.Info("variable",variable);
  6913. Printout.Info("type",type);
  6914. END;
  6915. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6916. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6917. ELSE
  6918. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6919. NEW(withEntry);
  6920. withEntry.previous := withEntries;
  6921. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6922. withEntry.type := type;
  6923. withEntries := withEntry;
  6924. StatementSequence(withPart.statements);
  6925. withEntries := withEntries.previous;
  6926. END;
  6927. END WithPart;
  6928. (** check and resolve with statement WITH variable: type DO ... END;
  6929. - check type and variable
  6930. - check that variable type is type extension of type
  6931. - check that variable is a variable
  6932. - enter new with scope and enter guardedVariable with same name and reference to variable
  6933. - create if statement:
  6934. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6935. **)
  6936. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6937. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6938. BEGIN
  6939. prevScope := currentScope; symbol := NIL;
  6940. FOR i := 0 TO withStatement.WithParts()-1 DO
  6941. WithPart(withStatement.GetWithPart(i),symbol);
  6942. END;
  6943. IF withStatement.elsePart # NIL THEN
  6944. StatementSequence(withStatement.elsePart)
  6945. END;
  6946. currentScope := prevScope;
  6947. END VisitWithStatement;
  6948. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6949. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6950. - check 'first' < 'last' and no overlaps between different case labels
  6951. - check statement sequence
  6952. **)
  6953. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6954. VAR
  6955. i: LONGINT;
  6956. position: LONGINT;
  6957. expression, left, right: SyntaxTree.Expression;
  6958. expressionType: SyntaxTree.Type;
  6959. l, r: LONGINT;
  6960. cl, cr: CHAR;
  6961. thiscases: SyntaxTree.CaseConstant;
  6962. BEGIN
  6963. thiscases := NIL;
  6964. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6965. expression := casePart.elements.GetExpression(i);
  6966. position := expression.position;
  6967. (* set context of range *)
  6968. IF expression IS SyntaxTree.RangeExpression THEN
  6969. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6970. END;
  6971. expression := ResolveExpression(expression);
  6972. IF expression = SyntaxTree.invalidExpression THEN
  6973. (* error already reported *)
  6974. expressionType := SyntaxTree.invalidType;
  6975. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6976. (* read out 'first' and 'last' *)
  6977. left := expression(SyntaxTree.RangeExpression).first;
  6978. right := expression(SyntaxTree.RangeExpression).last;
  6979. (* guaranteed by VisitRangeExpression: *)
  6980. ASSERT((left # NIL) & (right # NIL));
  6981. ASSERT(left.type.resolved = right.type.resolved);
  6982. left := CompatibleConversion(left.position, left, type);
  6983. right := CompatibleConversion(right.position, right, type);
  6984. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6985. expression(SyntaxTree.RangeExpression).SetLast(right);
  6986. expressionType := RegularType(position,left.type);
  6987. ELSE
  6988. expression := ConstantExpression(expression);
  6989. expression := CompatibleConversion(expression.position, expression, type);
  6990. (*
  6991. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6992. left := Global.NewCharacterValue(system,expression.position,cl);
  6993. expression := casePart.elements.GetExpression(i);
  6994. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6995. expression := left
  6996. END;
  6997. *)
  6998. casePart.elements.SetExpression(i,expression);
  6999. left := expression; right := expression;
  7000. expressionType := RegularType(position,expression.type)
  7001. END;
  7002. IF (expressionType = SyntaxTree.invalidType) THEN
  7003. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7004. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7005. expression := SyntaxTree.invalidExpression;
  7006. ELSE
  7007. l := 0; r := 0;
  7008. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7009. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7010. l := ORD(cl); r := ORD(cr);
  7011. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7012. ELSE
  7013. expression := SyntaxTree.invalidExpression
  7014. END;
  7015. IF expression # SyntaxTree.invalidExpression THEN
  7016. IF l>r THEN
  7017. Error(position,Diagnostics.Invalid,"empty case label")
  7018. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7019. Error(position,Diagnostics.Invalid,"duplicate case label");
  7020. ELSE
  7021. IF l < min THEN min := l END;
  7022. IF r > max THEN max := r END;
  7023. END;
  7024. END;
  7025. END;
  7026. casePart.elements.SetExpression(i,expression);
  7027. END;
  7028. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7029. casePart.SetConstants(thiscases);
  7030. StatementSequence(casePart.statements);
  7031. END CasePart;
  7032. (** check and resolve case statement CASE variable OF ... END;
  7033. - check variable
  7034. - check case parts
  7035. **)
  7036. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7037. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7038. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7039. BEGIN
  7040. expression := ResolveExpression(caseStatement.variable);
  7041. type := RegularType(expression.position,expression.type);
  7042. IF type = SyntaxTree.invalidType THEN
  7043. expression := SyntaxTree.invalidExpression;
  7044. ELSIF IsIntegerType(type) THEN
  7045. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7046. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7047. (*
  7048. expression := Global.NewCharacterValue(system,expression.position,ch);
  7049. *)
  7050. type := expression.type;
  7051. ELSIF IsCharacterType(type) THEN
  7052. ELSIF IsEnumerationType(type) THEN
  7053. ELSE
  7054. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7055. expression := SyntaxTree.invalidExpression;
  7056. END;
  7057. caseStatement.SetVariable(expression);
  7058. caseList := NIL;
  7059. min := MAX(LONGINT); max := MIN(LONGINT);
  7060. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7061. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7062. END;
  7063. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7064. msg := "huge sparse case table ";
  7065. Strings.AppendInt(msg, max-min);
  7066. Strings.Append(msg,"/");
  7067. Strings.AppendInt(msg, caseStatement.CaseParts());
  7068. Warning(caseStatement.position,msg);
  7069. END;
  7070. caseStatement.SetMinMax(min,max);
  7071. StatementSequence(caseStatement.elsePart);
  7072. IF expression.resolved # NIL THEN
  7073. IF IsCharacterValue(expression,ch) THEN
  7074. l := ORD(ch)
  7075. ELSIF IsIntegerValue(expression,l) THEN
  7076. END;
  7077. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7078. END;
  7079. END VisitCaseStatement;
  7080. (** check and resolve while statement
  7081. - check condition
  7082. - check statement sequence
  7083. **)
  7084. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7085. VAR prevIsUnreachable,b: BOOLEAN;
  7086. BEGIN
  7087. prevIsUnreachable := currentIsUnreachable;
  7088. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7089. IF IsBooleanValue(whileStatement.condition,b) THEN
  7090. IF b=FALSE THEN
  7091. currentIsUnreachable := TRUE
  7092. END;
  7093. END;
  7094. StatementSequence(whileStatement.statements);
  7095. currentIsUnreachable := prevIsUnreachable
  7096. END VisitWhileStatement;
  7097. (** check and resolve repeat statement
  7098. - check condition
  7099. - check statement sequence
  7100. **)
  7101. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7102. BEGIN
  7103. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7104. StatementSequence(repeatStatement.statements);
  7105. END VisitRepeatStatement;
  7106. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7107. VAR withEntry: WithEntry;
  7108. BEGIN
  7109. withEntry := withEntries;
  7110. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7111. withEntry := withEntry.previous
  7112. END;
  7113. IF withEntry = NIL THEN RETURN FALSE
  7114. ELSE
  7115. type := withEntry.type;
  7116. RETURN TRUE
  7117. END;
  7118. END GetGuard;
  7119. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7120. - check that variable is an integer variable
  7121. - check that from is integer typed with compatible type
  7122. - check that to has compatible type
  7123. - check that by is constant integer with compatible type
  7124. **)
  7125. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7126. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7127. BEGIN
  7128. designator := ResolveDesignator(forStatement.variable);
  7129. type := SyntaxTree.invalidType;
  7130. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7131. designator := SyntaxTree.invalidDesignator;
  7132. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7133. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7134. designator := SyntaxTree.invalidDesignator;
  7135. ELSIF CheckVariable(designator) THEN
  7136. type := designator.type;
  7137. END;
  7138. forStatement.SetVariable(designator);
  7139. expression := ResolveExpression(forStatement.from);
  7140. IF expression = SyntaxTree.invalidExpression THEN
  7141. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7142. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7143. expression := SyntaxTree.invalidExpression;
  7144. ELSIF type # SyntaxTree.invalidType THEN
  7145. expression := NewConversion(expression.position,expression,type,NIL)
  7146. END;
  7147. forStatement.SetFrom(expression);
  7148. expression := ResolveExpression(forStatement.to);
  7149. IF expression = SyntaxTree.invalidExpression THEN
  7150. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7151. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7152. expression := SyntaxTree.invalidExpression;
  7153. ELSIF type # SyntaxTree.invalidType THEN
  7154. expression := NewConversion(expression.position,expression,type,NIL)
  7155. END;
  7156. forStatement.SetTo(expression);
  7157. IF forStatement.by # NIL THEN
  7158. expression := ConstantInteger(forStatement.by);
  7159. ELSE
  7160. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7161. END;
  7162. IF expression = SyntaxTree.invalidExpression THEN
  7163. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7164. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7165. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7166. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7167. ELSIF type # SyntaxTree.invalidType THEN
  7168. expression := NewConversion(expression.position,expression,type,NIL)
  7169. END;
  7170. forStatement.SetBy(expression);
  7171. StatementSequence(forStatement.statements);
  7172. END VisitForStatement;
  7173. (** check and resolve loop statement LOOP StatementSequence END
  7174. - check statement sequence
  7175. **)
  7176. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7177. BEGIN
  7178. StatementSequence(loopStatement.statements)
  7179. END VisitLoopStatement;
  7180. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7181. BEGIN
  7182. StatementSequence(exitableBlock.statements);
  7183. END VisitExitableBlock;
  7184. (** check and resolve exit statement EXIT
  7185. - check that exit is within LOOP statement block
  7186. **)
  7187. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7188. VAR outer: SyntaxTree.Statement;
  7189. BEGIN
  7190. outer := exitStatement.outer;
  7191. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7192. outer := outer.outer;
  7193. END;
  7194. IF outer = NIL THEN
  7195. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7196. END;
  7197. END VisitExitStatement;
  7198. (** check and resolve return statement RETURN [expression]
  7199. - check expression (if any)
  7200. - check if in procedure scope
  7201. - if in procedure scope then check expression compatibility
  7202. - if not in procecdure scope then check on return without expression
  7203. **)
  7204. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7205. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7206. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7207. BEGIN
  7208. position := returnStatement.position;
  7209. expression := returnStatement.returnValue;
  7210. IF expression # NIL THEN
  7211. expression := ResolveExpression(expression);
  7212. returnStatement.SetReturnValue(expression);
  7213. END;
  7214. outer := returnStatement.outer;
  7215. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7216. outer := outer.outer
  7217. END;
  7218. IF (outer # NIL) THEN
  7219. scope := outer(SyntaxTree.Body).inScope;
  7220. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7221. IF (expression # NIL) THEN
  7222. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7223. END;
  7224. ELSE
  7225. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7226. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7227. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7228. END;
  7229. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7230. IF returnType # NIL THEN
  7231. returnType := returnType.resolved;
  7232. IF expression = NIL THEN
  7233. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7234. ELSIF expression.type = NIL THEN
  7235. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7236. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7237. Error(position,Diagnostics.Invalid,"return type not compatible");
  7238. IF VerboseErrorMessage THEN
  7239. Printout.Info("returnType",returnType);
  7240. Printout.Info("expression",expression);
  7241. END;
  7242. ELSE
  7243. expression := NewConversion(expression.position,expression,returnType,NIL);
  7244. returnStatement.SetReturnValue(expression);
  7245. END;
  7246. ELSIF expression # NIL THEN
  7247. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7248. END;
  7249. END;
  7250. END;
  7251. END VisitReturnStatement;
  7252. (** check and resolve await statement AWAIT(condition: Expression)
  7253. - check await condition
  7254. **)
  7255. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7256. VAR condition: SyntaxTree.Expression;
  7257. BEGIN
  7258. condition := ResolveCondition(awaitStatement.condition);
  7259. IF currentIsRealtime THEN
  7260. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7261. END;
  7262. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7263. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7264. END;
  7265. awaitStatement.SetCondition(condition);
  7266. END VisitAwaitStatement;
  7267. PROCEDURE CheckSystemImport(position: LONGINT);
  7268. VAR import: SyntaxTree.Import;
  7269. BEGIN
  7270. import := currentScope.ownerModule.moduleScope.firstImport;
  7271. WHILE(import # NIL) DO
  7272. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7273. RETURN;
  7274. END;
  7275. import := import.nextImport;
  7276. END;
  7277. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7278. END CheckSystemImport;
  7279. (** check and resolve code statement: do nothing, must be done by assembler
  7280. **)
  7281. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7282. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7283. BEGIN
  7284. CheckSystemImport(code.position);
  7285. FOR i := 0 TO code.inRules.Length()-1 DO
  7286. statement := code.inRules.GetStatement(i);
  7287. IF statement IS SyntaxTree.Assignment THEN
  7288. WITH statement: SyntaxTree.Assignment DO
  7289. statement.SetRight(ResolveExpression(statement.right));
  7290. END;
  7291. ELSE
  7292. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7293. END;
  7294. END;
  7295. FOR i := 0 TO code.outRules.Length()-1 DO
  7296. statement := code.outRules.GetStatement(i);
  7297. IF statement IS SyntaxTree.Assignment THEN
  7298. WITH statement: SyntaxTree.Assignment DO
  7299. statement.SetLeft(ResolveDesignator(statement.left));
  7300. END;
  7301. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7302. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7303. ELSE
  7304. Printout.Info("out statement ", statement);
  7305. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7306. END;
  7307. END;
  7308. END VisitCode;
  7309. (** check and set flags of a statement block
  7310. - check for multiply occurence of a flag
  7311. - check and set priority only in bodies
  7312. - check for valid names
  7313. **)
  7314. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7315. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7316. flag: LONGINT; recordBody: SyntaxTree.Body;
  7317. PROCEDURE SetProtectedRecord;
  7318. VAR scope: SyntaxTree.Scope;
  7319. BEGIN
  7320. scope := currentScope;
  7321. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7322. scope := scope.outerScope
  7323. END;
  7324. IF scope # NIL THEN
  7325. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7326. END;
  7327. END SetProtectedRecord;
  7328. BEGIN
  7329. flags := {};
  7330. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7331. recordBody := block(SyntaxTree.Body)
  7332. ELSE
  7333. recordBody := NIL
  7334. END;
  7335. blockModifier := block.blockModifiers;
  7336. WHILE(blockModifier # NIL) DO
  7337. name := blockModifier.identifier;
  7338. expression := blockModifier.expression;
  7339. position := blockModifier.position;
  7340. flag := -1;
  7341. IF name=Global.NamePriority THEN
  7342. IF expression = NIL THEN
  7343. Error(position,Diagnostics.Invalid,"missing priority expression");
  7344. ELSIF recordBody = NIL THEN
  7345. Error(position,Diagnostics.Invalid,"priority not on record body");
  7346. ELSIF recordBody.priority # NIL THEN
  7347. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7348. ELSE
  7349. recordBody.SetPriority(expression);
  7350. END;
  7351. ELSIF expression # NIL THEN
  7352. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7353. ELSIF name=Global.NameExclusive THEN
  7354. IF block.isExclusive THEN
  7355. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7356. END;
  7357. block.SetExclusive(TRUE); SetProtectedRecord;
  7358. ELSIF name=Global.NameActive THEN
  7359. IF recordBody = NIL THEN
  7360. Error(position,Diagnostics.Invalid,"active not in record body");
  7361. ELSIF recordBody.isActive THEN
  7362. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7363. ELSE
  7364. recordBody.SetActive(TRUE); SetProtectedRecord;
  7365. END;
  7366. ELSIF name=Global.NameSafe THEN
  7367. IF recordBody = NIL THEN
  7368. Error(position,Diagnostics.Invalid,"safe not in record body");
  7369. ELSIF recordBody.isSafe THEN
  7370. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7371. ELSE
  7372. recordBody.SetSafe(TRUE);
  7373. SetProtectedRecord;
  7374. END;
  7375. ELSIF name=Global.NameRealtime THEN
  7376. IF recordBody = NIL THEN
  7377. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7378. ELSIF recordBody.isRealtime THEN
  7379. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7380. ELSE
  7381. recordBody.SetRealtime(TRUE);
  7382. block.SetRealtime(TRUE);
  7383. END;
  7384. ELSIF name=Global.NameUnchecked THEN
  7385. IF block.isUnchecked THEN
  7386. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7387. ELSE
  7388. block.SetUnchecked(TRUE);
  7389. END;
  7390. ELSIF (name=Global.NameUncooperative) THEN
  7391. IF block.isUncooperative THEN
  7392. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7393. ELSE
  7394. block.SetUncooperative(TRUE);
  7395. END;
  7396. ELSE
  7397. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7398. END;
  7399. blockModifier := blockModifier.nextModifier;
  7400. END;
  7401. END BlockFlags;
  7402. (** check and resolve statement block
  7403. - check flags (exclusive)
  7404. - check statement sequence
  7405. **)
  7406. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7407. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7408. BEGIN
  7409. BlockFlags(statementBlock);
  7410. IF statementBlock.isExclusive THEN
  7411. (* check that not in exclusive block *)
  7412. IF currentIsExclusive THEN
  7413. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7414. ELSIF currentIsRealtime THEN
  7415. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7416. END;
  7417. END;
  7418. recentExclusive := currentIsExclusive;
  7419. recentUnreachable := currentIsUnreachable;
  7420. recentRealtime := currentIsRealtime;
  7421. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7422. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7423. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7424. StatementSequence(statementBlock.statements);
  7425. currentIsRealtime := recentRealtime;
  7426. currentIsExclusive := recentExclusive;
  7427. currentIsUnreachable := recentUnreachable;
  7428. END VisitStatementBlock;
  7429. (** check and resolve body
  7430. - check flags (active, priority, safe)
  7431. - check body and finally part
  7432. **)
  7433. PROCEDURE Body(body: SyntaxTree.Body);
  7434. BEGIN
  7435. VisitStatementBlock(body);
  7436. IF body.isActive THEN
  7437. IF ~currentIsBodyProcedure THEN
  7438. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7439. ELSIF body.priority # NIL THEN
  7440. body.SetPriority(ConstantInteger(body.priority));
  7441. END;
  7442. ELSIF body.isSafe THEN
  7443. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7444. ELSIF body.priority # NIL THEN
  7445. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7446. END;
  7447. IF body.code # NIL THEN
  7448. CheckSystemImport(body.position);
  7449. END;
  7450. StatementSequence(body.finally)
  7451. END Body;
  7452. (*** scopes ***)
  7453. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7454. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7455. VAR duplicateSymbol: BOOLEAN;
  7456. BEGIN
  7457. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7458. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7459. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7460. END;
  7461. scope.EnterSymbol(symbol,duplicateSymbol);
  7462. IF ~allowDuplicate & duplicateSymbol THEN
  7463. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7464. IF VerboseErrorMessage THEN
  7465. Printout.Info("multiply defined identifier",symbol);
  7466. Printout.Info("in scope",scope);
  7467. END;
  7468. END;
  7469. END Register;
  7470. (**
  7471. implementation: check and resolve an implementation part
  7472. **)
  7473. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7474. move implementation checker to a separate object ? *)
  7475. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7476. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7477. BEGIN
  7478. prevIsRealtime := currentIsRealtime;
  7479. prevIsBodyProcedure := currentIsBodyProcedure;
  7480. prevIsCellNet := currentIsCellNet;
  7481. prevScope := currentScope;
  7482. currentScope := scope;
  7483. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7484. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7485. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7486. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7487. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7488. (*
  7489. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7490. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7491. END;
  7492. *)
  7493. END;
  7494. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7495. (* module body, record bodies are wrapped into an artifical procedure *)
  7496. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7497. Body(scope(SyntaxTree.ProcedureScope).body)
  7498. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7499. Body(scope(SyntaxTree.ProcedureScope).body)
  7500. END;
  7501. END;
  7502. currentScope := prevScope;
  7503. currentIsRealtime := prevIsRealtime;
  7504. currentIsBodyProcedure := prevIsBodyProcedure;
  7505. currentIsCellNet := prevIsCellNet;
  7506. END Implementation;
  7507. (** implementation phase:
  7508. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7509. **)
  7510. PROCEDURE Implementations(x: SyntaxTree.Module);
  7511. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7512. BEGIN
  7513. prevPhase := phase;
  7514. phase := InlinePhase;
  7515. scope := x.firstScope;
  7516. WHILE(scope # NIL) DO
  7517. Implementation(scope);
  7518. scope := scope.nextScope;
  7519. END;
  7520. phase := ImplementationPhase;
  7521. scope := x.firstScope;
  7522. WHILE(scope # NIL) DO
  7523. Implementation(scope);
  7524. scope := scope.nextScope;
  7525. END;
  7526. phase := prevPhase;
  7527. END Implementations;
  7528. (** declaration phase:
  7529. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7530. - import lists (for module scopes)
  7531. - parameter list (for procedure scopes)
  7532. - constant declarations
  7533. - type declarations
  7534. - variable declarations
  7535. - procedure declarations
  7536. preformed in two stages:
  7537. - first all symbols are entered into the symbol table (with uniqueness check),
  7538. - then all symbols are resolved
  7539. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7540. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7541. **)
  7542. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7543. VAR
  7544. constant: SyntaxTree.Constant;
  7545. typeDeclaration: SyntaxTree.TypeDeclaration;
  7546. variable: SyntaxTree.Variable;
  7547. procedure: SyntaxTree.Procedure;
  7548. prevScope: SyntaxTree.Scope;
  7549. parameter: SyntaxTree.Parameter;
  7550. import: SyntaxTree.Import;
  7551. symbol: SyntaxTree.Symbol;
  7552. prevPhase: LONGINT;
  7553. prevError : BOOLEAN;
  7554. type: SyntaxTree.Type;
  7555. atype : SyntaxTree.ArrayType;
  7556. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7557. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7558. BEGIN
  7559. IF type.baseType # NIL THEN
  7560. baseType := type.baseType.resolved;
  7561. IF baseType IS SyntaxTree.PointerType THEN
  7562. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7563. END;
  7564. IF baseType IS SyntaxTree.CellType THEN
  7565. DeclareCell(baseType(SyntaxTree.CellType));
  7566. END;
  7567. END;
  7568. parameter := type.firstParameter;
  7569. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7570. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7571. variable.SetType(parameter.type);
  7572. variable.SetAccess(SyntaxTree.Hidden);
  7573. variable.SetModifiers(parameter.modifiers);
  7574. currentScope.PushVariable(variable);
  7575. (*
  7576. Register(parameter,scope, FALSE);
  7577. *)
  7578. parameter := parameter.nextParameter;
  7579. END;
  7580. property := type.firstProperty;
  7581. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7582. variable := currentScope.FindVariable(property.name);
  7583. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7584. prop := variable(SyntaxTree.Property);
  7585. ELSE (* add, duplicate symbols detection later *)
  7586. prop := SyntaxTree.NewProperty(property.position, property.name);
  7587. currentScope.PushVariable(prop);
  7588. END;
  7589. prop.SetType(property.type);
  7590. prop.SetValue(property.value);
  7591. prop.SetAccess(SyntaxTree.Hidden);
  7592. property := property.nextProperty;
  7593. END;
  7594. END DeclareCell;
  7595. BEGIN
  7596. prevError := error;
  7597. prevPhase := phase;
  7598. phase := DeclarationPhase;
  7599. prevScope := currentScope;
  7600. currentScope := scope;
  7601. error := FALSE;
  7602. (* first enter all symbols in scope *)
  7603. IF scope IS SyntaxTree.ModuleScope THEN
  7604. (* treat imports first for a module scope, , set default context if necessary *)
  7605. import := scope(SyntaxTree.ModuleScope).firstImport;
  7606. WHILE(import # NIL) DO
  7607. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7608. Register(import, currentScope, FALSE);
  7609. import := import.nextImport;
  7610. END;
  7611. import := scope(SyntaxTree.ModuleScope).firstImport;
  7612. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7613. ResolveSymbol(import);
  7614. import := import.nextImport;
  7615. END;
  7616. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7617. (* enter parameters for a procedure scope *)
  7618. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7619. WHILE(parameter # NIL) DO
  7620. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7621. END;
  7622. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7623. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7624. ELSIF scope IS SyntaxTree.CellScope THEN
  7625. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7626. END;
  7627. IF error THEN RETURN END;
  7628. (* constants *)
  7629. constant := scope.firstConstant;
  7630. WHILE (constant # NIL) DO
  7631. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7632. END;
  7633. (* type declarations *)
  7634. typeDeclaration := scope.firstTypeDeclaration;
  7635. WHILE (typeDeclaration # NIL) DO
  7636. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7637. END;
  7638. (* variables *)
  7639. variable := scope.firstVariable;
  7640. WHILE (variable # NIL) DO
  7641. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7642. END;
  7643. (* procedures *)
  7644. procedure := scope.firstProcedure;
  7645. WHILE (procedure # NIL) DO
  7646. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7647. END;
  7648. (* now process all symbols without any presumption on the order *)
  7649. symbol := scope.firstSymbol;
  7650. WHILE(symbol # NIL) DO
  7651. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7652. ResolveSymbol(symbol);
  7653. END;
  7654. symbol := symbol.nextSymbol;
  7655. END;
  7656. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7657. symbol := scope.firstSymbol;
  7658. WHILE symbol # NIL DO
  7659. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7660. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7661. pointerFixes.Add(symbol, currentScope);
  7662. END;
  7663. IF ~symbol.type.resolved.isRealtime THEN
  7664. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7665. END;
  7666. END;
  7667. symbol := symbol.nextSymbol
  7668. END;
  7669. END;
  7670. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7671. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7672. END;
  7673. IF scope.ownerModule # NIL THEN
  7674. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7675. scope.ownerModule.AddScope(scope);
  7676. END;
  7677. phase := prevPhase;
  7678. currentScope := prevScope;
  7679. error := error OR prevError;
  7680. END Declarations;
  7681. (* nopov *)
  7682. (** check if all operators from one module are compatible to the ones in the other module
  7683. - check if there are not multiple operators with the same signature
  7684. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7685. - check for all operators whose signatures are compatible, whether the return types are compatible
  7686. note that:
  7687. - the return type is not considered to be part of the signature
  7688. - two signatures are considered compatible, if all of the operands are compatible
  7689. **)
  7690. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7691. VAR
  7692. thisOperator, thatOperator: SyntaxTree.Operator;
  7693. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7694. thisParameter, thatParameter: SyntaxTree.Parameter;
  7695. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7696. i: LONGINT;
  7697. BEGIN
  7698. currentScope := thisModuleScope;
  7699. hasError := FALSE;
  7700. (* go through all operators in the other module *)
  7701. thatOperator := thatModuleScope.firstOperator;
  7702. WHILE (thatOperator # NIL) & ~hasError DO
  7703. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7704. (* the other operator is accessible *)
  7705. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7706. (* the other operator is not the conversion operator *)
  7707. (* go through all operators in this module *)
  7708. thisOperator := thisModuleScope.firstOperator;
  7709. WHILE (thisOperator # NIL) & ~hasError DO
  7710. IF thisOperator # thatOperator THEN
  7711. (* the operators are not the same *)
  7712. IF thisOperator.name = thatOperator.name THEN
  7713. (* the operators share the same identifier *)
  7714. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7715. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7716. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7717. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7718. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7719. (* both operators have the same paramter count *)
  7720. thisParameter := thisProcedureType.firstParameter;
  7721. thatParameter := thatProcedureType.firstParameter;
  7722. operandsAreEqual := TRUE;
  7723. operandsAreCompatible := TRUE;
  7724. (* go through all parameters *)
  7725. FOR i := 1 TO thisProcedureType.numberParameters DO
  7726. ASSERT(thatParameter # NIL);
  7727. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7728. operandsAreEqual := FALSE;
  7729. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7730. operandsAreCompatible := FALSE
  7731. END
  7732. END;
  7733. thisParameter := thisParameter.nextParameter;
  7734. thatParameter := thatParameter.nextParameter
  7735. END;
  7736. IF operandsAreEqual THEN
  7737. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7738. hasError := TRUE
  7739. ELSIF operandsAreCompatible THEN
  7740. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7741. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7742. hasError := TRUE
  7743. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7744. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7745. hasError := TRUE
  7746. END
  7747. END
  7748. END
  7749. END
  7750. END;
  7751. thisOperator := thisOperator.nextOperator
  7752. END
  7753. END
  7754. END;
  7755. thatOperator := thatOperator.nextOperator
  7756. END
  7757. END CheckInterOperatorConformity;
  7758. (** check module:
  7759. - check module declaration
  7760. - add context, if necessary
  7761. - remove module from import cache, if necessary
  7762. - check declarations
  7763. - resolve all type fixes
  7764. - check implementation (bodies)
  7765. **)
  7766. PROCEDURE Module*(x: SyntaxTree.Module);
  7767. VAR (* nopov *)
  7768. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7769. BEGIN
  7770. prevScope := currentScope;
  7771. prevIsCellNet := currentIsCellNet;
  7772. module := x;
  7773. ASSERT(x # NIL);
  7774. global := system.globalScope[x.case];
  7775. x.moduleScope.SetGlobalScope(global);
  7776. currentScope := global;
  7777. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7778. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7779. RemoveModuleFromCache(importCache,x);
  7780. Declarations(x.moduleScope);
  7781. FixTypes();
  7782. IF module.isCellNet THEN
  7783. currentIsCellNet := TRUE;
  7784. modifier := x.modifiers;
  7785. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7786. CheckModifiers(modifier, FALSE);
  7787. END;
  7788. (* nopov *)
  7789. IF ~error THEN
  7790. (* check if operators conform to each other within this module *)
  7791. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7792. (* go through all imports *)
  7793. import := x.moduleScope.firstImport;
  7794. WHILE import # NIL DO
  7795. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7796. (* check if all operators in this module conform to the ones of the imported module *)
  7797. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7798. END;
  7799. import := import.nextImport
  7800. END;
  7801. END;
  7802. Implementations(x);
  7803. module := NIL;
  7804. currentIsCellNet := prevIsCellNet;
  7805. currentScope := prevScope;
  7806. END Module;
  7807. END Checker;
  7808. Warnings*=OBJECT (SyntaxTree.Visitor)
  7809. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7810. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7811. BEGIN
  7812. SELF.diagnostics := diagnostics
  7813. END InitWarnings;
  7814. (** types *)
  7815. PROCEDURE Type(x: SyntaxTree.Type);
  7816. BEGIN x.Accept(SELF)
  7817. END Type;
  7818. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7819. BEGIN END VisitType;
  7820. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7821. BEGIN END VisitBasicType;
  7822. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7823. BEGIN END VisitCharacterType;
  7824. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7825. BEGIN END VisitIntegerType;
  7826. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7827. BEGIN END VisitFloatType;
  7828. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7829. BEGIN END VisitQualifiedType;
  7830. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7831. BEGIN END VisitStringType;
  7832. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7833. BEGIN END VisitEnumerationType;
  7834. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7835. BEGIN END VisitRangeType;
  7836. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7837. BEGIN
  7838. IF ~(SyntaxTree.Warned IN x.state) THEN
  7839. x.SetState(SyntaxTree.Warned);
  7840. Type(x.arrayBase);
  7841. END;
  7842. END VisitArrayType;
  7843. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7844. BEGIN
  7845. IF ~(SyntaxTree.Warned IN x.state) THEN
  7846. x.SetState(SyntaxTree.Warned);
  7847. Type(x.arrayBase);
  7848. END;
  7849. END VisitMathArrayType;
  7850. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7851. BEGIN
  7852. IF ~(SyntaxTree.Warned IN x.state) THEN
  7853. x.SetState(SyntaxTree.Warned);
  7854. Type(x.pointerBase);
  7855. END;
  7856. END VisitPointerType;
  7857. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7858. BEGIN Scope(x.recordScope) END VisitRecordType;
  7859. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7860. BEGIN Scope(x.cellScope) END VisitCellType;
  7861. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7862. BEGIN END VisitProcedureType;
  7863. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7864. VAR msg: ARRAY 256 OF CHAR;
  7865. BEGIN
  7866. Global.GetSymbolName(x,msg);
  7867. Strings.Append(msg," ");
  7868. Strings.Append(msg,text);
  7869. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7870. END Warning;
  7871. (** symbols *)
  7872. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7873. BEGIN
  7874. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7875. IF ~(x IS SyntaxTree.Parameter) THEN
  7876. Warning(x,"never used");
  7877. END;
  7878. END;
  7879. x.Accept(SELF);
  7880. END Symbol;
  7881. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7882. BEGIN END VisitSymbol;
  7883. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7884. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7885. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7886. BEGIN END VisitConstant;
  7887. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7888. BEGIN END VisitVariable;
  7889. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7890. BEGIN END VisitProperty;
  7891. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7892. BEGIN END VisitParameter;
  7893. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7894. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7895. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7896. BEGIN END VisitOperator;
  7897. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7898. BEGIN END VisitImport;
  7899. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7900. VAR
  7901. symbol: SyntaxTree.Symbol;
  7902. BEGIN
  7903. symbol := scope.firstSymbol;
  7904. WHILE(symbol # NIL) DO
  7905. Symbol(symbol);
  7906. symbol := symbol.nextSymbol;
  7907. END;
  7908. END Scope;
  7909. PROCEDURE Module*(x: SyntaxTree.Module);
  7910. BEGIN
  7911. SELF.module := x;
  7912. Scope(x.moduleScope);
  7913. END Module;
  7914. END Warnings;
  7915. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7916. BEGIN
  7917. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7918. END IsOberonInline;
  7919. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7920. BEGIN
  7921. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7922. END Resolved;
  7923. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7924. VAR i: LONGINT;
  7925. BEGIN
  7926. i := 1;
  7927. WHILE i < x DO
  7928. i := i *2
  7929. END;
  7930. RETURN i=x
  7931. END PowerOf2;
  7932. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7933. BEGIN
  7934. RETURN
  7935. (scope # NIL) &
  7936. (scope IS SyntaxTree.ModuleScope)
  7937. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7938. OR
  7939. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7940. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7941. END IsCellNetScope;
  7942. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7943. BEGIN
  7944. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7945. END IsCellScope;
  7946. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7947. BEGIN
  7948. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7949. RETURN (scope # NIL) & IsCellNetScope(scope)
  7950. END InCellNetScope;
  7951. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7952. BEGIN
  7953. ASSERT(size MOD system.dataUnit = 0);
  7954. RETURN size DIV system.dataUnit
  7955. END ToMemoryUnits;
  7956. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7957. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7958. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7959. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7960. BEGIN
  7961. IF t = NIL THEN
  7962. RETURN TRUE
  7963. ELSE
  7964. t := t.resolved;
  7965. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7966. END;
  7967. END TypeAllowed;
  7968. BEGIN
  7969. type := type.resolved;
  7970. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7971. RETURN FALSE
  7972. ELSE
  7973. procedureType := type(SyntaxTree.ProcedureType);
  7974. numberParameters := procedureType.numberParameters;
  7975. RETURN
  7976. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7977. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7978. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7979. END;
  7980. END GetProcedureAllowed;
  7981. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7982. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7983. VAR import: SyntaxTree.Import;
  7984. BEGIN
  7985. import := importCache.ImportByModuleName(x.name,x.context);
  7986. IF import # NIL THEN
  7987. importCache.RemoveImporters(x.name,x.context);
  7988. END;
  7989. END RemoveModuleFromCache;
  7990. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7991. (* to <- this assignment compatibility *)
  7992. VAR result: BOOLEAN;
  7993. BEGIN
  7994. IF this= NIL THEN result := (to=NIL)
  7995. ELSIF to=NIL THEN result := FALSE
  7996. ELSE
  7997. (*! will be replaced by this:
  7998. ELSE result := this.CompatibleTo(to.resolved);
  7999. *)
  8000. this := this.resolved; to := to.resolved;
  8001. IF to=SyntaxTree.invalidType THEN result := FALSE
  8002. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8003. ELSIF to = this THEN
  8004. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8005. ELSIF to IS SyntaxTree.BasicType THEN
  8006. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8007. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8008. result := this.CompatibleTo(to.resolved)
  8009. ELSE
  8010. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8011. END
  8012. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8013. result := to.sizeInBits = this.sizeInBits;
  8014. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8015. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8016. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8017. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8018. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8019. result := TRUE;
  8020. ELSIF to IS SyntaxTree.AnyType THEN
  8021. 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);
  8022. ELSIF to IS SyntaxTree.ObjectType THEN
  8023. 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 *) ;
  8024. ELSIF to IS SyntaxTree.ByteType THEN
  8025. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8026. ELSIF to IS SyntaxTree.CharacterType THEN
  8027. result := IsCharacterType(this)
  8028. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8029. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8030. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  8031. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8032. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8033. result := TRUE;
  8034. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8035. result := TRUE;
  8036. ELSE
  8037. result := FALSE
  8038. END;
  8039. ELSIF to IS SyntaxTree.PointerType THEN
  8040. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8041. IsPointerType(this) & (IsTypeExtension(to,this) OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8042. & (~to.isRealtime OR this.isRealtime);
  8043. ELSIF to IS SyntaxTree.ProcedureType THEN
  8044. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8045. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8046. & (~to.isRealtime OR this.isRealtime)
  8047. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8048. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8049. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8050. ELSIF to IS SyntaxTree.RecordType THEN
  8051. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8052. ELSIF to IS SyntaxTree.ArrayType THEN
  8053. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8054. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8055. ELSIF StaticArrayCompatible(to, this) THEN
  8056. result := TRUE
  8057. ELSE
  8058. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8059. END;
  8060. ELSIF to IS SyntaxTree.MathArrayType THEN
  8061. IF this IS SyntaxTree.MathArrayType THEN
  8062. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8063. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8064. result := TRUE;
  8065. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8066. result := TRUE;
  8067. ELSE
  8068. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8069. END;
  8070. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8071. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8072. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8073. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8074. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8075. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8076. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8077. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8078. ELSE
  8079. result := FALSE
  8080. END;
  8081. (* an array-structured object type is compatible to the type of its array structure *)
  8082. ELSIF IsArrayStructuredObjectType(this) THEN
  8083. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8084. ELSE
  8085. result := FALSE;
  8086. END;
  8087. ELSIF to IS SyntaxTree.StringType THEN
  8088. result := FALSE;
  8089. ELSIF to IS SyntaxTree.EnumerationType THEN
  8090. result := IsEnumerationExtension(this,to);
  8091. ELSIF to IS SyntaxTree.PortType THEN
  8092. result := SameType(to, this)
  8093. ELSE
  8094. Printout.Info("CompatibleTo",to);
  8095. HALT(100); (* implement missing type check *)
  8096. END;
  8097. END;
  8098. RETURN result
  8099. END CompatibleTo;
  8100. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8101. VAR actualBase, formalBase: SyntaxTree.Type;
  8102. BEGIN
  8103. IF SameType(formal,actual) THEN
  8104. RETURN TRUE
  8105. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8106. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8107. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8108. RETURN
  8109. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8110. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8111. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8112. & StaticArrayCompatible(formalBase,actualBase)
  8113. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8114. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8115. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8116. RETURN
  8117. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8118. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8119. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8120. & StaticArrayCompatible(formalBase,actualBase)
  8121. ELSE RETURN FALSE
  8122. END;
  8123. END StaticArrayCompatible;
  8124. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8125. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8126. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8127. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8128. BEGIN
  8129. result := SameType(formal,actual);
  8130. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8131. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8132. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8133. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8134. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8135. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8136. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8137. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8138. & TC(formalBase, actualBase);
  8139. END;
  8140. RETURN result
  8141. END TC;
  8142. BEGIN
  8143. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8144. ELSE
  8145. arrayBase := formalType.arrayBase.resolved;
  8146. IF (actualType IS SyntaxTree.StringType) THEN
  8147. result := arrayBase IS SyntaxTree.CharacterType
  8148. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8149. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8150. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8151. result := TC(formalType, actualType);
  8152. ELSE
  8153. result := (arrayBase IS SyntaxTree.ByteType)
  8154. END;
  8155. END;
  8156. RETURN result
  8157. END OpenArrayCompatible;
  8158. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8159. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8160. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8161. BEGIN
  8162. IF actualType IS SyntaxTree.MathArrayType THEN
  8163. actualArray := actualType(SyntaxTree.MathArrayType);
  8164. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8165. (*
  8166. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8167. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8168. *)
  8169. actualBase := ArrayBase(actualType,Infinity);
  8170. formalBase := ArrayBase(formalType,Infinity);
  8171. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8172. ELSE
  8173. (*
  8174. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8175. *)
  8176. formalBase := Resolved(formalType.arrayBase);
  8177. actualBase := Resolved(actualArray.arrayBase);
  8178. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8179. (*
  8180. ARRAY [k] -> ARRAY [n]
  8181. *)
  8182. result := (formalType.staticLength = actualArray.staticLength)
  8183. ELSE
  8184. result := TRUE
  8185. END;
  8186. IF ~result THEN
  8187. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8188. ELSIF actualBase = NIL THEN result := FALSE
  8189. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8190. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8191. ELSE
  8192. result := SameType(formalBase,actualBase)
  8193. END;
  8194. END;
  8195. ELSE
  8196. result := FALSE
  8197. END;
  8198. RETURN result
  8199. END MathArrayCompatible;
  8200. (**
  8201. Math Array Type distance for assignments / parameter passings of the form
  8202. from -> to
  8203. variants:
  8204. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8205. allowed:
  8206. static -> static (& size match)
  8207. static -> open
  8208. static -> tensor
  8209. open -> open
  8210. open -> tensor
  8211. open -> static
  8212. tensor -> tensor
  8213. tensor -> open
  8214. tensor -> static
  8215. **)
  8216. (*! think about the metric here: is form matching more important than element type matching? *)
  8217. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8218. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8219. BEGIN
  8220. fromBase := Resolved(from.arrayBase);
  8221. toBase := Resolved(to.arrayBase);
  8222. i := Infinity;
  8223. IF from = to THEN
  8224. i := 0;
  8225. ELSIF (from.form = to.form) THEN
  8226. (* static -> static, open -> open, tensor -> tensor *)
  8227. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8228. IF fromBase = toBase THEN i := 0
  8229. ELSIF toBase = NIL THEN i := 1
  8230. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8231. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8232. ELSE
  8233. i := TypeDistance(system,fromBase, toBase, varpar);
  8234. END;
  8235. END;
  8236. ELSIF (to.form = SyntaxTree.Static) THEN
  8237. (* forbidden *)
  8238. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8239. (* static -> tensor, open -> tensor, tensor -> open *)
  8240. IF toBase=fromBase THEN i := 0;
  8241. ELSIF toBase = NIL THEN i := 1;
  8242. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8243. toBase := ArrayBase(toBase,Infinity);
  8244. IF (fromBase=toBase) THEN i := 0
  8245. ELSIF (toBase = NIL) THEN i:= 1
  8246. ELSIF (fromBase = NIL) THEN i := Infinity;
  8247. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8248. END;
  8249. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8250. fromBase := ArrayBase(fromBase,Infinity);
  8251. IF (fromBase=toBase) THEN i := 0
  8252. ELSIF (toBase = NIL) THEN i := 1
  8253. ELSIF (fromBase = NIL) THEN i := Infinity;
  8254. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8255. END;
  8256. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8257. END;
  8258. IF i # Infinity THEN INC(i,2) END;
  8259. ELSIF (from.form = SyntaxTree.Static) THEN
  8260. (* static -> open *)
  8261. IF toBase=fromBase THEN i := 0
  8262. ELSIF toBase = NIL THEN i := 1
  8263. ELSIF fromBase = NIL THEN i := Infinity
  8264. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8265. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8266. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8267. END;
  8268. IF i # Infinity THEN INC(i,1) END;
  8269. ELSE HALT(100); (* unknown case *)
  8270. END;
  8271. RETURN i;
  8272. END MathArrayTypeDistance;
  8273. (** compute and return the distance of two array types
  8274. - return the distance of the base types
  8275. **)
  8276. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8277. VAR i: LONGINT;
  8278. BEGIN
  8279. i := Infinity;
  8280. IF from = to THEN
  8281. i := 0
  8282. ELSE
  8283. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8284. (*
  8285. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8286. i := TypeDistance(from.base, to.base);
  8287. IF i >= 0 THEN INC(i) END
  8288. ELSIF (from.mode = open) & (to.mode = open) THEN
  8289. i := TypeDistance(from.base, to.base);
  8290. *)
  8291. END;
  8292. RETURN i
  8293. END ArrayTypeDistance;
  8294. (** compute the signature distance of a procedure and an actual parameter list
  8295. - if any of the parameters are not compatible, the result is infinite
  8296. - add up and return the distance over all parameters
  8297. **)
  8298. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8299. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8300. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8301. BEGIN
  8302. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8303. result := Infinity
  8304. ELSE
  8305. formalParameter := procedureType.firstParameter;
  8306. i := 0;
  8307. result := 0;
  8308. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8309. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8310. actualParameter := actualParameters.GetExpression(i);
  8311. ASSERT(formalParameter.type # NIL);
  8312. IF (actualParameter.type = NIL) THEN distance := Infinity
  8313. ELSE
  8314. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8315. END;
  8316. IF distance = Infinity THEN
  8317. result := Infinity;
  8318. ELSE
  8319. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8320. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8321. (* already handled varpar *)
  8322. (*
  8323. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8324. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8325. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8326. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8327. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8328. END;
  8329. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8330. result := Infinity
  8331. END;
  8332. *)
  8333. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8334. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8335. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8336. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8337. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8338. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8339. END;
  8340. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8341. result := Infinity
  8342. END;
  8343. ELSE
  8344. result := Infinity
  8345. END;
  8346. ELSE
  8347. INC(result,distance);
  8348. END;
  8349. END;
  8350. formalParameter := formalParameter.nextParameter; INC(i);
  8351. END;
  8352. END;
  8353. ASSERT(result >= 0);
  8354. RETURN result
  8355. END Distance;
  8356. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8357. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8358. BEGIN
  8359. IF right.numberParameters # (procedureType.numberParameters) THEN
  8360. result := Infinity
  8361. ELSE
  8362. formalParameter := procedureType.firstParameter;
  8363. rightParameter := right.firstParameter;
  8364. i := 0;
  8365. result := 0;
  8366. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8367. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8368. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8369. IF distance = Infinity THEN
  8370. result := Infinity;
  8371. ELSE
  8372. INC(result,distance);
  8373. END;
  8374. formalParameter := formalParameter.nextParameter;
  8375. rightParameter := rightParameter.nextParameter;
  8376. END;
  8377. END;
  8378. ASSERT(result >= 0);
  8379. RETURN result
  8380. END ProcedureTypeDistance;
  8381. (** compute and return the distance between two types, used for computation of signature distance
  8382. from -> to
  8383. **)
  8384. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8385. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8386. BEGIN
  8387. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8388. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8389. END;
  8390. i := Infinity;
  8391. IF from = to THEN
  8392. i := 0
  8393. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8394. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8395. i := Infinity;
  8396. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8397. i := 1
  8398. ELSIF (from IS SyntaxTree.StringType) THEN
  8399. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8400. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8401. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8402. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8403. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8404. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8405. i := 1
  8406. ELSIF (from IS SyntaxTree.NilType) THEN
  8407. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8408. (*
  8409. ELSIF (from = NoType) THEN
  8410. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8411. *)
  8412. ELSIF (from IS SyntaxTree.BasicType) THEN
  8413. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8414. IF varpar & (i # 0) THEN i := Infinity END;
  8415. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8416. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8417. ELSIF (from IS SyntaxTree.RecordType) THEN
  8418. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8419. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8420. IF to IS SyntaxTree.MathArrayType THEN
  8421. (*
  8422. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8423. i := Infinity;
  8424. ELSE
  8425. *)
  8426. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8427. (*
  8428. END;
  8429. *)
  8430. END
  8431. ELSIF (from IS SyntaxTree.PointerType) THEN
  8432. ptr := from(SyntaxTree.PointerType);
  8433. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8434. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8435. (* ELSE i := TypeDistance(ptr.base, to); *)
  8436. END
  8437. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8438. IF (to IS SyntaxTree.ProcedureType) THEN
  8439. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8440. END;
  8441. ELSIF (from IS SyntaxTree.PortType) THEN
  8442. IF (to IS SyntaxTree.PortType) THEN
  8443. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8444. i := 0;
  8445. END;
  8446. END;
  8447. (*no procedure test, procedure must be the same*)
  8448. END;
  8449. RETURN i
  8450. END TypeDistance;
  8451. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8452. BEGIN
  8453. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8454. END IsIntegerType;
  8455. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8456. BEGIN
  8457. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8458. END IsAddressType;
  8459. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8460. BEGIN
  8461. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8462. END IsSizeType;
  8463. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8464. BEGIN
  8465. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8466. END IsSignedIntegerType;
  8467. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8468. BEGIN
  8469. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8470. END IsUnsignedIntegerType;
  8471. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8472. VAR result: BOOLEAN;
  8473. BEGIN
  8474. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8475. value := x.resolved(SyntaxTree.IntegerValue).value;
  8476. result := TRUE
  8477. ELSE
  8478. result := FALSE
  8479. END;
  8480. RETURN result
  8481. END IsIntegerValue;
  8482. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8483. VAR result: BOOLEAN;
  8484. BEGIN
  8485. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8486. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8487. result := TRUE
  8488. ELSE
  8489. result := FALSE
  8490. END;
  8491. RETURN result
  8492. END IsEnumerationValue;
  8493. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8494. VAR result: BOOLEAN;
  8495. BEGIN
  8496. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8497. value := x.resolved(SyntaxTree.RealValue).value;
  8498. result := TRUE
  8499. ELSE
  8500. result := FALSE
  8501. END;
  8502. RETURN result
  8503. END IsRealValue;
  8504. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8505. VAR result: BOOLEAN;
  8506. BEGIN
  8507. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8508. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8509. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8510. result := TRUE
  8511. ELSE
  8512. result := FALSE
  8513. END;
  8514. RETURN result
  8515. END IsComplexValue;
  8516. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8517. VAR result: BOOLEAN;
  8518. BEGIN
  8519. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8520. value := x.resolved(SyntaxTree.CharacterValue).value;
  8521. result := TRUE
  8522. ELSE
  8523. result := FALSE
  8524. END;
  8525. RETURN result
  8526. END IsCharacterValue;
  8527. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8528. VAR result: BOOLEAN;
  8529. BEGIN
  8530. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8531. value := x.resolved(SyntaxTree.BooleanValue).value;
  8532. result := TRUE
  8533. ELSE
  8534. result := FALSE
  8535. END;
  8536. RETURN result
  8537. END IsBooleanValue;
  8538. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8539. VAR result: BOOLEAN;
  8540. BEGIN
  8541. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8542. value := x.resolved(SyntaxTree.SetValue).value;
  8543. result := TRUE
  8544. ELSE
  8545. result := FALSE
  8546. END;
  8547. RETURN result
  8548. END IsSetValue;
  8549. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8550. VAR result: BOOLEAN;
  8551. BEGIN
  8552. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8553. value := x.resolved(SyntaxTree.StringValue).value;
  8554. result := TRUE
  8555. ELSE
  8556. result := FALSE
  8557. END;
  8558. RETURN result
  8559. END IsStringValue;
  8560. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8561. BEGIN
  8562. x := x.resolved;
  8563. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8564. END Indexable;
  8565. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8566. BEGIN
  8567. RETURN t1.SameType(t2.resolved);
  8568. END SameType;
  8569. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8570. BEGIN
  8571. IF t IS SyntaxTree.MathArrayType THEN
  8572. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8573. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8574. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8575. DEC(max);
  8576. END;
  8577. ELSIF t IS SyntaxTree.ArrayType THEN
  8578. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8579. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8580. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8581. END;
  8582. END;
  8583. RETURN t;
  8584. END ArrayBase;
  8585. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8586. BEGIN
  8587. type := type.resolved;
  8588. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8589. base := type(SyntaxTree.ArrayType).arrayBase;
  8590. RETURN TRUE;
  8591. END;
  8592. RETURN FALSE;
  8593. END IsOpenArray;
  8594. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8595. BEGIN
  8596. type := type.resolved;
  8597. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8598. base := type(SyntaxTree.ArrayType).arrayBase;
  8599. dim := type(SyntaxTree.ArrayType).staticLength;
  8600. RETURN TRUE
  8601. ELSE
  8602. RETURN FALSE
  8603. END;
  8604. END IsStaticArray;
  8605. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8606. BEGIN
  8607. type := type.resolved;
  8608. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8609. base := type(SyntaxTree.ArrayType).arrayBase;
  8610. RETURN TRUE
  8611. ELSE
  8612. RETURN FALSE
  8613. END;
  8614. END IsDynamicArray;
  8615. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8616. VAR i: LONGINT;
  8617. BEGIN
  8618. i := 0;
  8619. t := t.resolved;
  8620. IF t IS SyntaxTree.MathArrayType THEN
  8621. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8622. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8623. END;
  8624. ELSIF t IS SyntaxTree.ArrayType THEN
  8625. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8626. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8627. END;
  8628. END;
  8629. RETURN i
  8630. END Dimension;
  8631. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8632. BEGIN
  8633. RETURN expression.assignable;
  8634. END IsVariable;
  8635. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8636. BEGIN
  8637. IF (symbol IS SyntaxTree.Parameter) THEN
  8638. WITH symbol: SyntaxTree.Parameter DO
  8639. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8640. END;
  8641. ELSE
  8642. RETURN FALSE
  8643. END;
  8644. END IsVariableParameter;
  8645. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8646. VAR result: BOOLEAN;
  8647. BEGIN
  8648. IF type = NIL THEN result := FALSE
  8649. ELSE
  8650. type := type.resolved;
  8651. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8652. END;
  8653. RETURN result
  8654. END IsPointerType;
  8655. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8656. VAR result: BOOLEAN;
  8657. BEGIN
  8658. IF type = NIL THEN result := FALSE
  8659. ELSE
  8660. type := type.resolved;
  8661. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8662. END;
  8663. RETURN result
  8664. END IsUnsafePointer;
  8665. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8666. BEGIN
  8667. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8668. END IsDisposable;
  8669. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8670. VAR result: BOOLEAN;
  8671. BEGIN
  8672. IF type = NIL THEN result := FALSE
  8673. ELSE
  8674. type := type.resolved;
  8675. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8676. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8677. result := result OR (type IS SyntaxTree.ObjectType);
  8678. END;
  8679. RETURN result
  8680. END IsPointerToRecord;
  8681. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8682. VAR result: BOOLEAN;
  8683. BEGIN
  8684. IF type = NIL THEN result := FALSE
  8685. ELSE
  8686. type := type.resolved;
  8687. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8688. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8689. ;
  8690. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8691. result := result OR (type IS SyntaxTree.ObjectType);
  8692. END;
  8693. RETURN result
  8694. END IsPointerToObject;
  8695. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8696. BEGIN
  8697. IF type # NIL THEN
  8698. RETURN type.resolved.hasPointers
  8699. ELSE
  8700. RETURN FALSE
  8701. END;
  8702. END ContainsPointer;
  8703. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8704. BEGIN
  8705. IF type = NIL THEN RETURN FALSE END;
  8706. type := type.resolved;
  8707. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8708. END IsStringType;
  8709. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8710. BEGIN
  8711. IF type = NIL THEN RETURN FALSE END;
  8712. type := type.resolved;
  8713. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8714. END IsCharacterType;
  8715. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8716. BEGIN
  8717. IF type = NIL THEN RETURN FALSE END;
  8718. type := type.resolved;
  8719. RETURN (type IS SyntaxTree.EnumerationType)
  8720. END IsEnumerationType;
  8721. (** cf. section "Type extension (base type)" in the language report **)
  8722. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8723. VAR result: BOOLEAN;
  8724. BEGIN
  8725. ASSERT(base # NIL); ASSERT(extension # NIL);
  8726. base := base.resolved; extension := extension.resolved;
  8727. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8728. result := TRUE;
  8729. ELSE
  8730. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8731. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8732. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8733. END;
  8734. WHILE (extension # NIL) & (extension # base) DO
  8735. IF extension IS SyntaxTree.RecordType THEN
  8736. extension := extension(SyntaxTree.RecordType).baseType;
  8737. IF (extension # NIL) THEN extension := extension.resolved END;
  8738. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8739. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8740. END;
  8741. ELSE extension := NIL;
  8742. END;
  8743. END;
  8744. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8745. END;
  8746. RETURN result
  8747. END IsTypeExtension;
  8748. (** check if base is the base enumeration type of extension **)
  8749. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8750. BEGIN
  8751. base := base.resolved; extension := extension.resolved;
  8752. WHILE (extension # NIL) & (extension # base) DO
  8753. IF extension IS SyntaxTree.EnumerationType THEN
  8754. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8755. IF extension # NIL THEN extension := extension.resolved END;
  8756. ELSE
  8757. extension := NIL
  8758. END;
  8759. END;
  8760. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8761. END IsEnumerationExtension;
  8762. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8763. BEGIN
  8764. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8765. RETURN TRUE
  8766. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8767. RETURN TRUE
  8768. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8769. RETURN TRUE
  8770. ELSE
  8771. RETURN FALSE
  8772. END
  8773. END IsCallable;
  8774. (** compute and return the distance of two record types
  8775. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8776. **)
  8777. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8778. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8779. BEGIN
  8780. i := 0;
  8781. WHILE (from # NIL) & (from # to) DO
  8782. baseType := from.baseType;
  8783. IF (baseType # NIL) THEN
  8784. baseType := baseType.resolved;
  8785. IF baseType IS SyntaxTree.PointerType THEN
  8786. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8787. END;
  8788. IF baseType IS SyntaxTree.RecordType THEN
  8789. from := baseType(SyntaxTree.RecordType);
  8790. ELSE
  8791. from := NIL;
  8792. END;
  8793. ELSE
  8794. from := NIL
  8795. END;
  8796. INC(i)
  8797. END;
  8798. IF from = NIL THEN i := Infinity END;
  8799. RETURN i
  8800. END RecordTypeDistance;
  8801. (** compute and return the distance of two pointer types **)
  8802. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8803. BEGIN
  8804. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8805. RETURN Infinity;
  8806. ELSE
  8807. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8808. END;
  8809. END PointerTypeDistance;
  8810. (** check if expression contains a symbol designator pointing to a type declaration.
  8811. - if so then enter type declaration into typeDeclaration and return true else return false
  8812. **)
  8813. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8814. VAR result: BOOLEAN;
  8815. BEGIN
  8816. result := FALSE;
  8817. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8818. result := TRUE;
  8819. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8820. END;
  8821. RETURN result
  8822. END IsTypeDesignator;
  8823. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8824. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8825. VAR result: BOOLEAN;
  8826. BEGIN
  8827. type := type.resolved;
  8828. IF type IS SyntaxTree.PointerType THEN
  8829. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8830. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8831. result := TRUE
  8832. ELSE
  8833. result := type IS SyntaxTree.RecordType
  8834. END;
  8835. RETURN result
  8836. END IsExtensibleType;
  8837. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8838. BEGIN
  8839. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8840. (d IS SyntaxTree.SymbolDesignator) &
  8841. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8842. OR
  8843. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8844. END IsUnextensibleRecord;
  8845. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8846. BEGIN
  8847. IF IsUnextensibleRecord(d) THEN
  8848. RETURN FALSE
  8849. ELSE RETURN IsExtensibleType(d.type.resolved)
  8850. END;
  8851. END IsExtensibleDesignator;
  8852. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8853. BEGIN
  8854. type := type.resolved;
  8855. IF (type IS SyntaxTree.PointerType) THEN
  8856. RETURN TRUE
  8857. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8858. RETURN TRUE
  8859. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8860. RETURN TRUE
  8861. ELSIF (type IS SyntaxTree.BasicType) THEN
  8862. RETURN TRUE
  8863. END;
  8864. RETURN FALSE
  8865. END IsBasicType;
  8866. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8867. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8868. BEGIN
  8869. baseType := record.baseType;
  8870. IF (baseType # NIL) THEN
  8871. baseType := baseType.resolved;
  8872. IF (baseType IS SyntaxTree.PointerType) THEN
  8873. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8874. END;
  8875. END;
  8876. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8877. recordType := baseType(SyntaxTree.RecordType);
  8878. ELSE
  8879. recordType := NIL;
  8880. END;
  8881. RETURN recordType
  8882. END RecordBase;
  8883. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8884. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8885. BEGIN
  8886. baseRecord := RecordBase(scope.ownerRecord);
  8887. IF baseRecord = NIL THEN RETURN NIL END;
  8888. scope := baseRecord.recordScope;
  8889. procedureType := procedure.type.resolved;
  8890. IF procedure IS SyntaxTree.Operator THEN
  8891. operator := scope.firstOperator;
  8892. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8893. (*
  8894. Printout.Info("not same ",procedureType);
  8895. Printout.Info("with ",operator.type);
  8896. *)
  8897. operator := operator.nextOperator;
  8898. END;
  8899. super := operator;
  8900. ELSE
  8901. super := scope.firstProcedure;
  8902. WHILE (super # NIL) & (super.name # procedure.name) DO
  8903. super := super.nextProcedure;
  8904. END;
  8905. END;
  8906. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8907. RETURN super
  8908. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8909. RETURN super
  8910. ELSE
  8911. RETURN FindSuperProcedure(scope,procedure);
  8912. END;
  8913. END FindSuperProcedure;
  8914. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8915. VAR procedure: SyntaxTree.Procedure;
  8916. BEGIN
  8917. procedure := record.recordScope.constructor;
  8918. IF procedure = NIL THEN
  8919. record := RecordBase(record);
  8920. IF record # NIL THEN
  8921. procedure := GetConstructor(record)
  8922. END;
  8923. END;
  8924. RETURN procedure;
  8925. END GetConstructor;
  8926. (* enter a case into a list of cases in a sorted way and check for collision *)
  8927. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8928. VAR prev,this,new: SyntaxTree.CaseConstant;
  8929. BEGIN
  8930. this := root;
  8931. prev := NIL;
  8932. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8933. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8934. RETURN FALSE
  8935. ELSE
  8936. IF (this # NIL) & (this.min = max+1) THEN
  8937. this.min := min
  8938. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8939. prev.max := min
  8940. ELSE
  8941. NEW(new); new.min := min; new.max := max;
  8942. new.next := this;
  8943. IF prev = NIL THEN
  8944. root := new;
  8945. ELSE
  8946. prev.next := new
  8947. END
  8948. END;
  8949. RETURN TRUE
  8950. END;
  8951. END EnterCase;
  8952. (** generate and return a new checker object, errors are entered into diagnostics **)
  8953. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8954. VAR checker: Checker;
  8955. BEGIN
  8956. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache);
  8957. RETURN checker
  8958. END NewChecker;
  8959. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8960. VAR warnings: Warnings;
  8961. BEGIN
  8962. NEW(warnings, diagnostics); RETURN warnings;
  8963. END NewWarnings;
  8964. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8965. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8966. END IsRangeType;
  8967. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8968. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8969. END IsMathArrayType;
  8970. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8971. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8972. END IsArrayType;
  8973. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8974. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8975. END IsComplexType;
  8976. (** if a type is an array-structured object type *)
  8977. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  8978. VAR recordType: SyntaxTree.RecordType;
  8979. BEGIN
  8980. IF type = NIL THEN
  8981. RETURN FALSE
  8982. ELSE
  8983. type := type.resolved;
  8984. IF type IS SyntaxTree.PointerType THEN
  8985. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8986. IF type IS SyntaxTree.RecordType THEN
  8987. recordType := type(SyntaxTree.RecordType);
  8988. RETURN recordType.isObject & recordType.HasArrayStructure()
  8989. ELSE
  8990. RETURN FALSE
  8991. END
  8992. ELSE
  8993. RETURN FALSE
  8994. END
  8995. END
  8996. END IsArrayStructuredObjectType;
  8997. (** the math array structure of a type
  8998. - for math arrays: the array itself
  8999. - for pointers: the math array structure of the pointer base
  9000. - for array-structured object types: the underlying structure
  9001. - for non-math arrays and all other types: NIL
  9002. **)
  9003. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9004. VAR
  9005. result: SyntaxTree.MathArrayType;
  9006. BEGIN
  9007. IF type = NIL THEN
  9008. result := NIL
  9009. ELSE
  9010. type := type.resolved;
  9011. IF type IS SyntaxTree.PointerType THEN
  9012. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9013. END;
  9014. IF type IS SyntaxTree.MathArrayType THEN
  9015. result := type(SyntaxTree.MathArrayType)
  9016. ELSIF type IS SyntaxTree.RecordType THEN
  9017. result := type(SyntaxTree.RecordType).arrayStructure
  9018. ELSE
  9019. result := NIL
  9020. END
  9021. END;
  9022. RETURN result
  9023. END MathArrayStructureOfType;
  9024. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9025. VAR
  9026. result: BOOLEAN;
  9027. rangeExpression: SyntaxTree.RangeExpression;
  9028. BEGIN
  9029. IF x IS SyntaxTree.RangeExpression THEN
  9030. rangeExpression := x(SyntaxTree.RangeExpression);
  9031. result := TRUE;
  9032. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9033. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9034. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9035. ELSE
  9036. result := FALSE
  9037. END;
  9038. RETURN result
  9039. END IsStaticRange;
  9040. (** whether a type is a math array of tensor form **)
  9041. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9042. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9043. END IsTensor;
  9044. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9045. BEGIN
  9046. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9047. length := type(SyntaxTree.MathArrayType).staticLength;
  9048. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9049. RETURN TRUE
  9050. ELSE
  9051. RETURN FALSE
  9052. END;
  9053. END IsStaticMathArray;
  9054. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9055. BEGIN
  9056. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9057. END SymbolHasAddress;
  9058. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9059. BEGIN
  9060. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9061. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9062. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9063. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9064. END HasAddress;
  9065. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9066. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9067. BEGIN
  9068. IF (e IS SyntaxTree.Designator) THEN
  9069. d := e(SyntaxTree.Designator);
  9070. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9071. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9072. e := d.left;
  9073. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9074. END;
  9075. IF d # NIL THEN
  9076. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9077. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9078. END;
  9079. END;
  9080. RETURN FALSE;
  9081. END IsLocalVariable;
  9082. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9083. BEGIN
  9084. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9085. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9086. ELSE
  9087. RETURN TRUE
  9088. END;
  9089. END IsStaticProcedure;
  9090. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9091. CONST OptimizeMethodTable = FALSE;
  9092. BEGIN
  9093. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9094. END InMethodTable;
  9095. END FoxSemanticChecker.