FoxSemanticChecker.Mod 392 KB

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