FoxSemanticChecker.Mod 393 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. replacements*: Replacement;
  93. cellsAreObjects: BOOLEAN;
  94. variableAccessed: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. error := FALSE;
  103. NEW(typeFixes);
  104. NEW(pointerFixes);
  105. resolvedType := NIL;
  106. resolvedExpression := NIL;
  107. resolvedStatement := NIL;
  108. currentScope := NIL;
  109. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  110. SELF.importCache := importCache;
  111. arrayBaseImported := FALSE;
  112. SELF.VerboseErrorMessage := verboseErrorMessage;
  113. global := NIL;
  114. phase := UndefinedPhase;
  115. currentIsRealtime := FALSE;
  116. currentIsUnreachable := FALSE;
  117. currentIsCellNet := FALSE;
  118. currentIsBodyProcedure := FALSE;
  119. currentIsExclusive := FALSE;
  120. withEntries := NIL;
  121. SELF.cellsAreObjects := system.cellsAreObjects;
  122. END InitChecker;
  123. (** report error **)
  124. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  125. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  126. BEGIN
  127. IF diagnostics # NIL THEN
  128. Basic.GetErrorMessage(code,message,errorMessage);
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  132. END;
  133. error := TRUE;
  134. END Error;
  135. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  136. VAR errModule: SyntaxTree.Module;
  137. BEGIN
  138. IF diagnostics # NIL THEN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  141. END;
  142. END Warning;
  143. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  144. VAR errorMessage: ARRAY 256 OF CHAR;
  145. BEGIN
  146. IF diagnostics # NIL THEN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  149. END;
  150. error := TRUE;
  151. END ErrorSS;
  152. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  153. VAR msg, msg2: ARRAY 256 OF CHAR;
  154. BEGIN
  155. IF diagnostics # NIL THEN
  156. COPY(msg1, msg);
  157. Strings.Append(msg, " = ");
  158. Basic.GetString(s, msg2);
  159. Strings.Append(msg, msg2);
  160. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  161. END;
  162. END InfoSS;
  163. (*** symbol lookup ***)
  164. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  165. **)
  166. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  167. VAR
  168. scope,baseScope: SyntaxTree.Scope;
  169. symbol, s: SyntaxTree.Symbol;
  170. ownerRecord,base: SyntaxTree.RecordType;
  171. BEGIN
  172. scope := inScope;
  173. symbol := NIL;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. s := NIL;
  177. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  178. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  179. END;
  180. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  181. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  182. ELSE
  183. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  184. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  185. symbol.MarkUsed;
  186. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  187. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  188. base := RecordBase(ownerRecord);
  189. IF (base # NIL) THEN
  190. baseScope := base.recordScope;
  191. symbol := Find(baseScope,name,FALSE);
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. ELSE
  196. symbol := NIL;
  197. END;
  198. END;
  199. END;
  200. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  201. END;
  202. IF (symbol # NIL) THEN
  203. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  204. ASSERT(phase = DeclarationPhase);
  205. ResolveSymbol(symbol)
  206. END;
  207. symbol.MarkUsed;
  208. END;
  209. RETURN symbol
  210. END Find;
  211. (*** types ***)
  212. (** find type declaration with name qualifiedIdentifier and return resolved type
  213. - check qualified identifier prefix, set scope to module scope if appropriate
  214. - check suffix in scope
  215. **)
  216. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  217. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  218. BEGIN
  219. result := NIL;
  220. prevScope := currentScope;
  221. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  222. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  223. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  224. IF symbol(SyntaxTree.Import).module = NIL THEN
  225. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  226. result := SyntaxTree.invalidType;
  227. symbol := NIL;
  228. ELSE
  229. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  230. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  231. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  232. IF VerboseErrorMessage THEN
  233. Printout.Info("scope", currentScope);
  234. Printout.Info("symbol", symbol);
  235. END;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  237. END;
  238. END;
  239. ELSE
  240. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  241. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  242. symbol := NIL;
  243. END;
  244. ELSE
  245. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  246. IF symbol = NIL THEN
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  248. IF VerboseErrorMessage THEN
  249. Printout.Info("Qualident",qualifiedIdentifier);
  250. Printout.Info("in scope",currentScope) ;
  251. END;
  252. END;
  253. END;
  254. IF symbol = NIL THEN (* error already handled *)
  255. typeDeclaration := NIL;
  256. result := SyntaxTree.invalidType;
  257. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  258. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSE
  262. currentScope := symbol.scope;
  263. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  264. result := ResolveType(typeDeclaration.declaredType);
  265. symbol.MarkUsed;
  266. ASSERT(result # NIL);
  267. END;
  268. currentScope := prevScope;
  269. RETURN result
  270. END ResolveNamedType;
  271. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  272. If node is currently being resolved then emit a cyclic definition error.
  273. Return TRUE only if node is fully resolved.
  274. **)
  275. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  276. VAR result: BOOLEAN;
  277. BEGIN
  278. IF SyntaxTree.Resolved IN x.state THEN
  279. result := FALSE
  280. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  281. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  282. result := FALSE;
  283. ELSE
  284. result := TRUE;
  285. x.SetState(SyntaxTree.BeingResolved)
  286. END;
  287. RETURN result
  288. END TypeNeedsResolution;
  289. (** Return invalid type if x is currently being resolved, return x otherwise**)
  290. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  291. BEGIN
  292. IF SyntaxTree.Resolved IN x.state THEN
  293. RETURN x
  294. ELSE
  295. RETURN SyntaxTree.invalidType
  296. END;
  297. END ResolvedType;
  298. PROCEDURE VisitType(x: SyntaxTree.Type);
  299. BEGIN
  300. ASSERT(x = SyntaxTree.invalidType);
  301. END VisitType;
  302. (** resolve basic type **)
  303. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  304. BEGIN
  305. IF TypeNeedsResolution(x) THEN
  306. x.SetState(SyntaxTree.Resolved);
  307. END;
  308. resolvedType := ResolvedType(x)
  309. END VisitBasicType;
  310. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitByteType;
  314. (** resolve character type **)
  315. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitCharacterType;
  319. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitBooleanType;
  323. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitSetType;
  327. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitAddressType;
  331. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitSizeType;
  335. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitAnyType;
  339. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitObjectType;
  343. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitNilType;
  347. (** resolve integer type **)
  348. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitIntegerType;
  352. (** resolve real type **)
  353. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitFloatType;
  357. (** resolve complex type **)
  358. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  359. BEGIN
  360. VisitBasicType(x);
  361. END VisitComplexType;
  362. (**
  363. resolve string type: nothing to be done
  364. **)
  365. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  366. BEGIN
  367. IF TypeNeedsResolution(x) THEN
  368. x.SetState(SyntaxTree.Resolved);
  369. END;
  370. resolvedType := ResolvedType(x)
  371. END VisitStringType;
  372. (**
  373. check enumeration scope: enter symbols and check for duplicate names
  374. **)
  375. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  376. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  377. BEGIN
  378. prevScope := currentScope;
  379. currentScope := x;
  380. e := x.firstConstant;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  408. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: LONGINT;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. x.SetArrayBase(pointerType);
  479. END;
  480. IF x.length # NIL THEN
  481. variableAccessed := FALSE;
  482. e := ResolveExpression(x.length);
  483. IF (e.resolved = NIL) THEN
  484. IF variableAccessed THEN
  485. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  486. END;
  487. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  488. ELSE
  489. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  490. END;
  491. END;
  492. IF arrayBase IS SyntaxTree.ArrayType THEN
  493. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  494. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  495. END;
  496. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  497. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  498. END;
  499. x.SetHasPointers(arrayBase.hasPointers);
  500. x.SetState(SyntaxTree.Resolved);
  501. END;
  502. resolvedType := ResolvedType(x);
  503. END VisitArrayType;
  504. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  505. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  506. BEGIN
  507. module := currentScope.ownerModule;
  508. IF module.name=name THEN
  509. (* do nothing *)
  510. ELSE
  511. moduleScope := module.moduleScope;
  512. import := moduleScope.FindImport(name);
  513. IF import = NIL THEN
  514. import := SyntaxTree.NewImport(position,name,name,TRUE);
  515. moduleScope.AddImport(import);
  516. Register(import,moduleScope,FALSE);
  517. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  518. VisitImport(import);
  519. ELSIF import.direct=FALSE THEN
  520. import.SetScope(module.moduleScope);
  521. import.SetDirect(TRUE);
  522. IF moduleScope.FindSymbol(import.name) = NIL THEN
  523. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  524. duplicate.SetContext(import.context);
  525. duplicate.SetModule(import.module);
  526. Register(duplicate,moduleScope,TRUE);
  527. VisitImport(duplicate);
  528. END;
  529. END;
  530. import.MarkUsed
  531. END;
  532. END ImportModule;
  533. (**
  534. resolve math array type
  535. - check base type
  536. - open math array of array forbidden
  537. - math array of tensor forbidden
  538. - static array of open array forbidden
  539. **)
  540. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  541. VAR arrayBase: SyntaxTree.Type;
  542. BEGIN
  543. IF TypeNeedsResolution(x) THEN
  544. x.SetArrayBase(ResolveType(x.arrayBase));
  545. IF x.length # NIL THEN
  546. x.SetLength(ConstantIntegerGeq0(x.length));
  547. END;
  548. arrayBase := x.arrayBase;
  549. IF arrayBase # NIL THEN
  550. arrayBase := arrayBase.resolved;
  551. IF arrayBase = SyntaxTree.invalidType THEN
  552. (* error already handled *)
  553. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  554. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  555. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  556. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  557. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  558. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  559. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  560. END;
  561. END;
  562. IF x.form = SyntaxTree.Static THEN
  563. x.SetIncrement(system.SizeOf(arrayBase));
  564. END;
  565. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  566. END;
  567. x.SetState(SyntaxTree.Resolved);
  568. END;
  569. resolvedType := ResolvedType(x);
  570. END VisitMathArrayType;
  571. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  572. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  573. (1) Rec = RECORD ... END; Ptr <---> Rec
  574. Ptr = POINTER TO Rec; ^ |
  575. | |
  576. TypeDesc TypeDesc
  577. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  578. ^ /
  579. | /
  580. TypeDesc <-- /
  581. *)
  582. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  583. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  584. BEGIN
  585. Strings.IntToStr(x.position,number);
  586. COPY(prefix,name);
  587. Strings.Append(name,"@");
  588. Strings.Append(name,number);
  589. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  590. typeDeclaration.SetDeclaredType(x);
  591. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  592. x.SetTypeDeclaration(typeDeclaration);
  593. currentScope.AddTypeDeclaration(typeDeclaration);
  594. typeDeclaration.SetScope(currentScope);
  595. END AnonymousTypeDeclaration;
  596. (**
  597. deferred pointer type resolving
  598. - resolve base type
  599. - check that base type is a record or array type
  600. - if error then set base type to invalid type
  601. **)
  602. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  603. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  604. BEGIN
  605. ASSERT(type.pointerBase # NIL);
  606. position := type.pointerBase.position;
  607. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  608. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  609. (* not for pointers, a type is needed for the records only
  610. IF type.typeDeclaration = NIL THEN
  611. AnonymousTypeDeclaration(type);
  612. END;
  613. *)
  614. END;
  615. resolved := ResolveType(type.pointerBase);
  616. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  617. type.SetPointerBase(resolved);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  619. recordType := resolved.resolved(SyntaxTree.RecordType);
  620. IF recordType.isObject & (recordType.baseType # NIL) THEN
  621. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  622. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  623. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  624. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  625. END;
  626. END;
  627. END;
  628. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  629. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  630. END;
  631. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  632. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  633. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  634. ELS
  635. *)
  636. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  638. END;
  639. END;
  640. ELSE
  641. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  642. type.SetPointerBase(SyntaxTree.invalidType)
  643. END
  644. END FixPointerType;
  645. (**
  646. resolve pointer type
  647. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  648. **)
  649. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  650. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  651. modifiers: SyntaxTree.Modifier; position: LONGINT;
  652. BEGIN
  653. IF TypeNeedsResolution(x) THEN
  654. modifiers := x.modifiers;
  655. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  656. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  657. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  658. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  659. (* inheritance cycle check
  660. example:
  661. A=POINTER TO RECORD(B) END;
  662. B=POINTER TO RECORD(A) END;
  663. *)
  664. IF x.pointerBase IS SyntaxTree.RecordType THEN
  665. recordType := x.pointerBase(SyntaxTree.RecordType);
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  685. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  686. x.SetSize(value)
  687. ELSE
  688. x.SetSize(system.SizeOf(system.longintType));
  689. END;
  690. x.SetState(SyntaxTree.Resolved);
  691. END;
  692. resolvedType := ResolvedType(x)
  693. END VisitPortType;
  694. (**
  695. deferred procedure type resolving
  696. - resolve return type
  697. - traverse and resolve parameters
  698. **)
  699. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  700. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  701. BEGIN
  702. resolved := ResolveType(procedureType.returnType);
  703. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  704. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  705. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  706. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  707. END;
  708. procedureType.SetReturnType(resolved);
  709. IF (resolved # NIL) THEN
  710. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  711. parameter.SetType(procedureType.returnType);
  712. parameter.SetAccess(SyntaxTree.Hidden);
  713. parameter.SetUntraced(procedureType.hasUntracedReturn);
  714. VisitParameter(parameter);
  715. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  716. END;
  717. (* process parameters *)
  718. parameter :=procedureType.firstParameter;
  719. WHILE (parameter # NIL) DO
  720. VisitParameter(parameter);
  721. parameter := parameter.nextParameter;
  722. END;
  723. END FixProcedureType;
  724. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  725. VAR prev,this: SyntaxTree.Modifier;
  726. BEGIN
  727. this := modifiers;prev := NIL;
  728. WHILE (this # NIL) & (this.identifier # name) DO
  729. prev := this; this := this.nextModifier;
  730. END;
  731. IF this # NIL THEN
  732. IF this.expression # NIL THEN
  733. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  734. END;
  735. this.Resolved;
  736. position := this.position;
  737. RETURN TRUE
  738. ELSE
  739. RETURN FALSE
  740. END;
  741. END HasFlag;
  742. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  743. VAR prev,this: SyntaxTree.Modifier;
  744. BEGIN
  745. this := modifiers;prev := NIL;
  746. WHILE (this # NIL) & (this.identifier # name) DO
  747. prev := this; this := this.nextModifier;
  748. END;
  749. IF this # NIL THEN
  750. IF this.expression = NIL THEN
  751. Error(this.position,Diagnostics.Invalid,"expected expression value");
  752. ELSE
  753. this.SetExpression(ConstantExpression(this.expression));
  754. IF CheckIntegerValue(this.expression,value) THEN END;
  755. END;
  756. this.Resolved;
  757. position := this.position;
  758. RETURN TRUE
  759. ELSE RETURN FALSE
  760. END;
  761. END HasValue;
  762. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  763. VAR this: SyntaxTree.Modifier;
  764. BEGIN
  765. this := modifiers;
  766. WHILE this # NIL DO
  767. IF ~this.resolved THEN
  768. IF checkUse THEN
  769. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  770. ELSE
  771. this.SetExpression(ResolveExpression(this.expression));
  772. this.Resolved;
  773. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  774. END;
  775. END;
  776. this := this.nextModifier
  777. END;
  778. END CheckModifiers;
  779. (**
  780. resolve procedure type
  781. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  782. **)
  783. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  784. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  785. BEGIN
  786. IF TypeNeedsResolution(procedureType) THEN
  787. modifiers := procedureType.modifiers;
  788. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  789. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  790. procedureType.SetInterrupt(TRUE);
  791. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  792. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  793. IF useDarwinCCalls THEN (*fld*)
  794. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  795. ELSE
  796. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  797. END
  798. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  799. procedureType.SetNoReturn(TRUE);
  800. END;
  801. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  802. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  803. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  804. CheckModifiers(modifiers, TRUE);
  805. modifiers := procedureType.returnTypeModifiers;
  806. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  807. CheckModifiers(modifiers, TRUE);
  808. typeFixes.Add(procedureType,currentScope);
  809. procedureType.SetHasPointers(procedureType.isDelegate);
  810. procedureType.SetState(SyntaxTree.Resolved);
  811. END;
  812. resolvedType := ResolvedType(procedureType)
  813. END VisitProcedureType;
  814. (** check and resolve record type
  815. - check base type: must be record, math array or array-structured object type
  816. - check declarations
  817. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  818. **)
  819. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  820. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  821. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  822. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  823. hasPointers: BOOLEAN;
  824. modifiers: SyntaxTree.Modifier;
  825. value: LONGINT;
  826. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  827. BEGIN
  828. type := type.resolved;
  829. IF (type IS SyntaxTree.PointerType) &
  830. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  831. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  832. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  833. RETURN TRUE
  834. ELSE
  835. RETURN FALSE
  836. END;
  837. END IsPointerToRecord;
  838. BEGIN
  839. IF TypeNeedsResolution(x) THEN
  840. hasPointers := FALSE;
  841. modifiers := x.modifiers;
  842. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  843. CheckModifiers(modifiers, TRUE);
  844. IF x.baseType # NIL THEN
  845. position := x.baseType.position;
  846. baseType := ResolveType(x.baseType);
  847. resolved := baseType.resolved;
  848. hasPointers := hasPointers OR resolved.hasPointers;
  849. IF x.isObject THEN (* object *)
  850. ASSERT(x.pointerType # NIL);
  851. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  852. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  853. baseType := NIL
  854. ELSIF IsPointerToRecord(resolved,recordType) THEN
  855. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  856. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  857. ELSE
  858. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  859. END;
  860. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  861. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  862. ELSIF IsPointerToRecord(resolved,recordType) THEN
  863. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  864. ELSIF resolved IS SyntaxTree.RecordType THEN
  865. ELSE
  866. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  867. END;
  868. ELSE
  869. IF resolved IS SyntaxTree.RecordType THEN
  870. ELSE
  871. Error(position, Diagnostics.Invalid,"record does not extend record")
  872. END;
  873. END;
  874. x.SetBaseType(baseType);
  875. IF x.Level() > 15 THEN
  876. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  877. (* note:
  878. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  879. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  880. inheritance history of a type.
  881. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  882. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  883. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  884. *)
  885. END;
  886. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  887. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  888. END;
  889. END;
  890. Declarations(x.recordScope);
  891. ResolveArrayStructure(x);
  892. (* computation of sizes and offsets skipped -> done in backend / system *)
  893. recordBase := x.GetBaseRecord();
  894. IF recordBase = NIL THEN numberMethods := 0
  895. ELSE numberMethods := recordBase.recordScope.numberMethods
  896. END;
  897. isRealtime := TRUE;
  898. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  899. WHILE symbol # NIL DO
  900. IF symbol IS SyntaxTree.Variable THEN
  901. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  902. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  903. END;
  904. IF symbol IS SyntaxTree.Procedure THEN
  905. procedure := symbol(SyntaxTree.Procedure);
  906. IF procedure.super # NIL THEN
  907. procedure.SetMethodNumber(procedure.super.methodNumber)
  908. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  909. procedure.SetMethodNumber(numberMethods);
  910. INC(numberMethods);
  911. END;
  912. END;
  913. symbol := symbol.nextSymbol;
  914. END;
  915. IF isRealtime THEN x.SetRealtime(TRUE) END;
  916. x.recordScope.SetNumberMethods(numberMethods);
  917. (* TODO: is this needed anymore? *)
  918. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  919. Error(x.position,Diagnostics.Invalid,"object extends a record")
  920. END;
  921. IF (x.typeDeclaration = NIL) THEN
  922. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  923. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  924. (*
  925. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  926. AnonymousTypeDeclaration(x,name);
  927. *)
  928. ELSE
  929. AnonymousTypeDeclaration(x,"Anonymous");
  930. END;
  931. END;
  932. x.SetHasPointers(hasPointers);
  933. x.SetState(SyntaxTree.Resolved);
  934. END;
  935. resolvedType := ResolvedType(x);
  936. END VisitRecordType;
  937. (** check and resolve cell type
  938. - check base type: must be cell
  939. - check declarations
  940. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  941. **)
  942. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  943. VAR
  944. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  945. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  946. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  947. recordBase: SyntaxTree.RecordType;
  948. numberMethods, int: LONGINT;
  949. real: LONGREAL;
  950. bool: BOOLEAN;
  951. set: SET;
  952. variable: SyntaxTree.Variable;
  953. v: SyntaxTree.Expression;
  954. str: Scanner.StringType;
  955. atype: SyntaxTree.ArrayType;
  956. prev: SyntaxTree.Scope;
  957. BEGIN
  958. IF TypeNeedsResolution(x) THEN
  959. recordBase := NIL;
  960. IF cellsAreObjects THEN
  961. IF x.baseType = NIL THEN
  962. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  963. ImportModule(qualifiedIdentifier.prefix, x.position);
  964. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  965. x.SetBaseType(ResolveType(x.baseType));
  966. recordBase := x.GetBaseRecord();
  967. IF recordBase = NIL THEN
  968. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  969. END;
  970. ELSE
  971. x.SetBaseType(ResolveType(x.baseType));
  972. END;
  973. ELSE
  974. x.SetBaseType(ResolveType(x.baseType));
  975. END;
  976. IF recordBase = NIL THEN numberMethods := 0
  977. ELSE numberMethods := recordBase.recordScope.numberMethods
  978. END;
  979. modifier := x.modifiers;
  980. (*IF ~x.isCellNet THEN*)
  981. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  982. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  983. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  984. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  985. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  986. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  987. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  988. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  989. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  990. symbol := system.activeCellsCapabilities;
  991. WHILE symbol # NIL DO
  992. IF HasFlag(modifier, symbol.name, position) THEN END;
  993. symbol := symbol.nextSymbol;
  994. END;
  995. modifier := x.modifiers;
  996. WHILE (modifier # NIL) DO
  997. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  998. IF modifier.expression # NIL THEN
  999. v := ConstantExpression(modifier.expression);
  1000. property.SetValue(v);
  1001. IF IsIntegerValue(modifier.expression, int) THEN
  1002. (*property.SetValue(modifier.expression);*)
  1003. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1004. property.SetType(system.longintType);
  1005. ELSIF IsRealValue(modifier.expression, real) THEN
  1006. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1007. property.SetType(system.longrealType);
  1008. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1009. property.SetType(system.booleanType);
  1010. ELSIF IsSetValue(modifier.expression, set) THEN
  1011. property.SetType(system.setType);
  1012. ELSIF IsStringValue(modifier.expression, str) THEN
  1013. (*property.SetValue(modifier.expression);*)
  1014. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1015. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1016. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1017. property.SetType(atype);
  1018. ELSE
  1019. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1020. END;
  1021. ELSE (* flag property *)
  1022. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1023. property.SetType(system.booleanType);
  1024. END;
  1025. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1026. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1027. x.AddProperty(property);
  1028. modifier := modifier.nextModifier;
  1029. END;
  1030. (*ELSE
  1031. (* no: this should not be allowed on cell net types or check against global scope ...
  1032. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1033. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1034. END;
  1035. END
  1036. *)
  1037. END;*)
  1038. CheckModifiers(modifier, FALSE);
  1039. Declarations(x.cellScope);
  1040. (* process parameters *)
  1041. prev := currentScope;
  1042. currentScope := x.cellScope;
  1043. parameter :=x.firstParameter;
  1044. WHILE (parameter # NIL) DO
  1045. VisitParameter(parameter);
  1046. type := parameter.type.resolved;
  1047. IF ~(type IS SyntaxTree.PortType) THEN
  1048. WHILE IsStaticArray(type, type, len) DO
  1049. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1050. END;
  1051. WHILE IsDynamicArray(type, type) DO
  1052. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1053. END;
  1054. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1055. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1056. END;
  1057. END;
  1058. parameter := parameter.nextParameter;
  1059. END;
  1060. currentScope := prev;
  1061. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1062. WHILE symbol # NIL DO
  1063. IF symbol IS SyntaxTree.Variable THEN
  1064. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1065. END;
  1066. symbol := symbol.nextSymbol;
  1067. END;
  1068. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1069. IF (x.typeDeclaration = NIL) THEN
  1070. AnonymousTypeDeclaration(x,"Anonymous");
  1071. END;
  1072. x.SetState(SyntaxTree.Resolved);
  1073. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1074. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1075. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1076. Warning(x.position, "Non-empty body for an engine?");
  1077. END;
  1078. END;
  1079. resolvedType := ResolvedType(x);
  1080. END VisitCellType;
  1081. (* check if an object is an array-structured object type
  1082. - determine the array structure
  1083. - collect operators from top to bottom in the inheritance hierarchy
  1084. - check if LEN operator is declared
  1085. - determine number of possible index operators
  1086. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1087. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1088. *)
  1089. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1090. VAR
  1091. indexOperatorCount, i: LONGINT;
  1092. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1093. isTensor: BOOLEAN;
  1094. BEGIN
  1095. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1096. (* determine array structure *)
  1097. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1098. END;
  1099. IF recordType.HasArrayStructure() THEN
  1100. (* the object is an ASOT *)
  1101. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1102. (* reset array access operators *)
  1103. arrayAccessOperators.len := NIL;
  1104. arrayAccessOperators.generalRead := NIL;
  1105. arrayAccessOperators.generalWrite := NIL;
  1106. IF isTensor THEN
  1107. (* all operators of dimensionalities 1 to max *)
  1108. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1109. ELSE
  1110. (* all operators of certain dimensionality *)
  1111. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1112. END;
  1113. NEW(arrayAccessOperators.read, indexOperatorCount);
  1114. NEW(arrayAccessOperators.write, indexOperatorCount);
  1115. FOR i := 0 TO indexOperatorCount - 1 DO
  1116. arrayAccessOperators.read[i] := NIL;
  1117. arrayAccessOperators.write[i] := NIL
  1118. END;
  1119. (* collect access operators in the record scope *)
  1120. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1121. IF arrayAccessOperators.len = NIL THEN
  1122. (* TODO: think about making this operator optional for static array structures *)
  1123. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1124. END;
  1125. (* show error messages *)
  1126. IF isTensor THEN
  1127. (* require ARRAY [*] OF RANGE *)
  1128. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1129. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1130. ELSE
  1131. (* forbid ARRAY [*] OF RANGE *)
  1132. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1133. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1134. (* require RANGE, RANGE, ... RANGE *)
  1135. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1136. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1137. END;
  1138. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1139. ELSE
  1140. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1141. IF recordType.recordScope.firstOperator # NIL THEN
  1142. RETURN;
  1143. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1144. END
  1145. END
  1146. END ResolveArrayStructure;
  1147. (** collect array access operators in a record scope **)
  1148. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1149. VAR
  1150. baseType: SyntaxTree.Type;
  1151. operator: SyntaxTree.Operator;
  1152. isReadOperator, isGeneralOperator: BOOLEAN;
  1153. indexListSize, indexListKind, hashValue: LONGINT;
  1154. BEGIN
  1155. (* if a parent record scope exists, collect the operators there first *)
  1156. baseType := recordScope.ownerRecord.baseType;
  1157. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1158. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1159. END;
  1160. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1161. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1162. END;
  1163. (* go through all operators in the current record scope *)
  1164. operator := recordScope.firstOperator;
  1165. WHILE operator # NIL DO
  1166. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1167. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1168. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1169. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1170. IF isGeneralOperator THEN
  1171. IF isReadOperator THEN
  1172. arrayAccessOperators.generalRead := operator
  1173. ELSE
  1174. arrayAccessOperators.generalWrite := operator
  1175. END
  1176. ELSE
  1177. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1178. IF isReadOperator THEN
  1179. arrayAccessOperators.read[hashValue] := operator
  1180. ELSE
  1181. arrayAccessOperators.write[hashValue] := operator
  1182. END
  1183. END
  1184. END
  1185. ELSE
  1186. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1187. END;
  1188. operator := operator.nextOperator
  1189. END
  1190. END CollectArrayAccessOperators;
  1191. (** the hash value of an index operator **)
  1192. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1193. VAR result: LONGINT;
  1194. BEGIN
  1195. IF isTensor THEN
  1196. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1197. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1198. ELSE
  1199. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1200. END
  1201. ELSE
  1202. result := indexListKind
  1203. END;
  1204. RETURN result
  1205. END IndexOperatorHash;
  1206. (** 2 to the power of exponent **)
  1207. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1208. VAR result, i: LONGINT;
  1209. BEGIN
  1210. result := 1;
  1211. FOR i := 1 TO exponent DO
  1212. result := result * 2;
  1213. END;
  1214. RETURN result
  1215. END TwoToThePowerOf;
  1216. (** check if a LEN operator has a correct signature. i.e.
  1217. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1218. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1219. **)
  1220. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1221. VAR
  1222. procedureType: SyntaxTree.ProcedureType;
  1223. returnedArrayType: SyntaxTree.MathArrayType;
  1224. result: BOOLEAN;
  1225. BEGIN
  1226. result := FALSE;
  1227. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1228. IF (procedureType.numberParameters = 0) THEN
  1229. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1230. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1231. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1232. IF returnedArrayType.form = SyntaxTree.Open THEN
  1233. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1234. result := TRUE
  1235. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1236. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1237. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1238. result := TRUE
  1239. END
  1240. END
  1241. END
  1242. END
  1243. END;
  1244. IF result THEN
  1245. (* export symbol automatically *)
  1246. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1247. ELSE
  1248. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1249. END;
  1250. RETURN result
  1251. END CheckLenOperator;
  1252. (** check if an index operator has a correct signature. i.e.
  1253. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1254. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1255. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1256. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1257. - determine if it is a read or write operator (existance of return type)
  1258. - check index parameters
  1259. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1260. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1261. [LONGINT] -> binary 0 -> 0
  1262. [RANGE] -> binary 1 -> 1
  1263. [LONGINT, LONGINT] -> binary 00 -> 0
  1264. [LONGINT, RANGE] -> binary 01 -> 1
  1265. [RANGE, LONGINT] -> binary 10 -> 2
  1266. [RANGE, RANGE] -> binary 11 -> 3
  1267. etc.
  1268. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1269. - for read operators, check if return type matches the type of data that is read
  1270. - for write operators, check if last parameter type matches the type of data that is written
  1271. **)
  1272. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1273. VAR
  1274. elementType, otherElementType, dataType: SyntaxTree.Type;
  1275. procedureType: SyntaxTree.ProcedureType;
  1276. mathArrayType: SyntaxTree.MathArrayType;
  1277. parameter: SyntaxTree.Parameter;
  1278. parameterCount, rangeCount, i: LONGINT;
  1279. hasTypeError: BOOLEAN;
  1280. BEGIN
  1281. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1282. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1283. (* determine if it is a read or write operator *)
  1284. isReadOperator := (procedureType.returnType # NIL);
  1285. IF isReadOperator THEN
  1286. indexListSize := parameterCount;
  1287. ELSE
  1288. indexListSize := parameterCount - 1;
  1289. END;
  1290. IF indexListSize < 1 THEN
  1291. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1292. RETURN FALSE
  1293. END;
  1294. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1295. (* general operator *)
  1296. isGeneralOperator := TRUE;
  1297. IF indexListSize > 1 THEN
  1298. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1299. RETURN FALSE
  1300. END;
  1301. (* ARRAY [*] OF RANGE*)
  1302. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1303. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1304. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1305. RETURN FALSE
  1306. END;
  1307. parameter := procedureType.firstParameter.nextParameter
  1308. ELSE
  1309. (* fixed-dim. operator *)
  1310. isGeneralOperator := FALSE;
  1311. (* check number of index parameters *)
  1312. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1313. (* for tensors, limited to a certain size *)
  1314. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1315. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1316. RETURN FALSE
  1317. END
  1318. ELSE
  1319. (* for non-tensors, depends on dimensionality *)
  1320. IF indexListSize # arrayStructure.Dimensionality() THEN
  1321. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1322. RETURN FALSE
  1323. END
  1324. END;
  1325. (* go through all index parameters
  1326. - count the number of ranges
  1327. - determine the index list kind number
  1328. *)
  1329. indexListKind := 0;
  1330. rangeCount := 0;
  1331. parameter := procedureType.firstParameter;
  1332. FOR i := 1 TO indexListSize DO
  1333. indexListKind := indexListKind * 2;
  1334. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1335. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1336. INC(indexListKind);
  1337. INC(rangeCount)
  1338. ELSE
  1339. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1340. RETURN FALSE
  1341. END;
  1342. parameter := parameter.nextParameter
  1343. END;
  1344. END;
  1345. (*
  1346. - for read operators: check type of last parameter
  1347. - for write operators: check return type
  1348. *)
  1349. IF isReadOperator THEN
  1350. dataType := procedureType.returnType (* the return type *)
  1351. ELSE
  1352. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1353. END;
  1354. elementType := arrayStructure.ElementType();
  1355. hasTypeError := FALSE;
  1356. IF isGeneralOperator THEN
  1357. (* ARRAY [?] OF <Element> *)
  1358. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1359. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1360. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1361. hasTypeError := TRUE
  1362. END
  1363. ELSE
  1364. hasTypeError := TRUE
  1365. END
  1366. ELSE
  1367. IF rangeCount = 0 THEN
  1368. (* <Element> *)
  1369. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1370. ELSE
  1371. (* ARRAY [*, *, ..., *] OF <Element> *)
  1372. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1373. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1374. IF mathArrayType.IsFullyDynamic() THEN
  1375. IF mathArrayType.Dimensionality() = rangeCount THEN
  1376. otherElementType := mathArrayType.ElementType();
  1377. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1378. ELSE
  1379. hasTypeError := TRUE
  1380. END
  1381. ELSE
  1382. hasTypeError := TRUE
  1383. END
  1384. ELSE
  1385. hasTypeError := TRUE
  1386. END
  1387. END
  1388. END;
  1389. IF hasTypeError THEN
  1390. IF isReadOperator THEN
  1391. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1392. ELSE
  1393. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1394. END;
  1395. RETURN FALSE
  1396. END;
  1397. (* export symbol automatically *)
  1398. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1399. RETURN TRUE
  1400. END CheckIndexOperator;
  1401. (** resolve all pending types (late resolving).
  1402. - type fixes are resolved at the end of the declaration phase
  1403. - type fixes may imply new type fixes that are also entered at the end of the list
  1404. **)
  1405. PROCEDURE FixTypes;
  1406. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1407. BEGIN
  1408. prevScope := currentScope;
  1409. p := typeFixes.Get(currentScope);
  1410. WHILE p # NIL DO
  1411. ASSERT(currentScope # NIL);
  1412. ASSERT(p IS SyntaxTree.Type);
  1413. IF p IS SyntaxTree.PointerType THEN
  1414. FixPointerType(p(SyntaxTree.PointerType))
  1415. ELSIF p IS SyntaxTree.ProcedureType THEN
  1416. FixProcedureType(p(SyntaxTree.ProcedureType))
  1417. ELSE
  1418. HALT(100);
  1419. END;
  1420. p := typeFixes.Get(currentScope);
  1421. END;
  1422. currentScope :=prevScope;
  1423. END FixTypes;
  1424. (**
  1425. resolve type x
  1426. - if x is nil then return nil
  1427. - if x cannot be resolved then the result is invalidType else the result is x
  1428. - the resolved type is entered into x.resolved
  1429. **)
  1430. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1431. VAR prev,resolved: SyntaxTree.Type;
  1432. BEGIN
  1433. prev := resolvedType;
  1434. resolvedType := SyntaxTree.invalidType;
  1435. IF x = NIL THEN resolvedType := NIL
  1436. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1437. END;
  1438. resolved := resolvedType;
  1439. resolvedType := prev;
  1440. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1441. RETURN resolved
  1442. END ResolveType;
  1443. (*** compatibility rules ***)
  1444. (**
  1445. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1446. **)
  1447. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1448. VAR result: SyntaxTree.Type;
  1449. BEGIN
  1450. result := SyntaxTree.invalidType;
  1451. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1452. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1453. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1454. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1455. ELSE result := type.resolved
  1456. END;
  1457. RETURN result
  1458. END RegularType;
  1459. (** returns signature compatibility of procedure types this and to
  1460. - if not compatible then error is reported
  1461. - compatibility means type equality
  1462. **)
  1463. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1464. VAR result: BOOLEAN;
  1465. BEGIN
  1466. result := SameType(to,this);
  1467. IF ~result THEN
  1468. Error(position,Diagnostics.Invalid,"signature incompatible");
  1469. IF VerboseErrorMessage THEN
  1470. Printout.Info("this",this);
  1471. Printout.Info("to",to);
  1472. END;
  1473. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1474. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1475. END;
  1476. RETURN result
  1477. END SignatureCompatible;
  1478. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1479. - for var parameters compatibility means same type except for
  1480. - formal is of open array of system byte
  1481. - formal is of record type
  1482. - formal is of open array type
  1483. - formal is of open math array type
  1484. - for value parameters compatibllity means assignment compatibility except for
  1485. - formal is of open array type
  1486. if compatible the return true else report error and return false
  1487. **)
  1488. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1489. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1490. BEGIN
  1491. formalType := RegularType(formal.position,formal.type);
  1492. actualType := RegularType(actual.position,actual.type);
  1493. error := FALSE;
  1494. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1496. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1497. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1498. END;
  1499. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1500. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1501. ELSIF ~IsVariable(actual) THEN
  1502. result := FALSE; error := TRUE;
  1503. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1504. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1505. ELSE
  1506. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1507. END;
  1508. IF VerboseErrorMessage THEN
  1509. Printout.Info("actual",actual);
  1510. Printout.Info("formal",formal);
  1511. END;
  1512. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1513. result := CompatibleTo(system,actualType,formalType);
  1514. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1515. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1516. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1517. IF IsArrayStructuredObjectType(actualType) THEN
  1518. actualType := MathArrayStructureOfType(actualType)
  1519. END;
  1520. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1521. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1522. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1523. END;
  1524. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1525. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1526. END;
  1527. ELSE
  1528. result := SameType(actualType,formalType)
  1529. END
  1530. ELSE
  1531. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1532. actualType := system.characterType;
  1533. END;
  1534. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1535. result := TRUE; (* special rule for WINAPI parameters *)
  1536. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1537. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1538. ELSE
  1539. result := CompatibleTo(system,actualType,formalType);
  1540. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1541. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1542. END;
  1543. END;
  1544. END;
  1545. IF ~result & ~error THEN
  1546. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1547. IF VerboseErrorMessage THEN
  1548. Printout.Info("actual",actual);
  1549. Printout.Info("formal",formal);
  1550. END;
  1551. END;
  1552. RETURN result
  1553. END ParameterCompatible;
  1554. (** check compatibility for expressions of the form left := right
  1555. - if compatible then return true else error report and return false
  1556. - check if left is variable
  1557. - check compatiblity
  1558. **)
  1559. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1560. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1561. BEGIN
  1562. result := FALSE;
  1563. leftType := RegularType(left.position,left.type);
  1564. rightType := RegularType(right.position,right.type);
  1565. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1566. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1567. END;
  1568. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1569. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1570. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1571. (* error already handled *)
  1572. result := TRUE;
  1573. ELSIF ~IsVariable(left) THEN
  1574. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1575. IF VerboseErrorMessage THEN
  1576. Printout.Info("left",left);
  1577. Printout.Info("right",right);
  1578. END;
  1579. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1580. result := TRUE;
  1581. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1582. result := TRUE
  1583. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1584. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1585. IF VerboseErrorMessage THEN
  1586. Printout.Info("left",left);
  1587. Printout.Info("right",right);
  1588. END;
  1589. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1590. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1591. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1592. ELSE
  1593. result := TRUE
  1594. END;
  1595. RETURN result
  1596. END AssignmentCompatible;
  1597. (*** values ***)
  1598. (** check and resolve integer value **)
  1599. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1600. VAR hugeint: HUGEINT;
  1601. BEGIN
  1602. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1603. value.SetType(Global.GetIntegerType(system,hugeint));
  1604. resolvedExpression := value
  1605. END VisitIntegerValue;
  1606. (** check and resolve real value **)
  1607. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1608. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1609. BEGIN
  1610. subtype := value(SyntaxTree.RealValue).subtype;
  1611. IF subtype = Scanner.Real THEN
  1612. type := system.realType
  1613. ELSIF subtype = Scanner.Longreal THEN
  1614. type := system.longrealType
  1615. ELSE
  1616. HALT(100)
  1617. END;
  1618. value.SetType(type);
  1619. resolvedExpression := value
  1620. END VisitRealValue;
  1621. (** check and resolve complex value **)
  1622. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1623. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1624. BEGIN
  1625. subtype := value(SyntaxTree.ComplexValue).subtype;
  1626. IF subtype = Scanner.Real THEN
  1627. type := system.complexType
  1628. ELSIF subtype = Scanner.Longreal THEN
  1629. type := system.longcomplexType
  1630. ELSE
  1631. HALT(100)
  1632. END;
  1633. value.SetType(type);
  1634. resolvedExpression := value
  1635. END VisitComplexValue;
  1636. (** check and resolve set value **)
  1637. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1638. BEGIN
  1639. value.SetType(system.setType);
  1640. resolvedExpression := value
  1641. END VisitSetValue;
  1642. (** check and resolve set value **)
  1643. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1644. BEGIN
  1645. value.SetType(SyntaxTree.invalidType);
  1646. resolvedExpression := value
  1647. END VisitMathArrayValue;
  1648. (** check and resolve boolean value **)
  1649. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1650. BEGIN
  1651. value.SetType(system.booleanType);
  1652. resolvedExpression := value
  1653. END VisitBooleanValue;
  1654. (** check and resolve string value **)
  1655. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1656. BEGIN
  1657. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1658. resolvedExpression := value
  1659. END VisitStringValue;
  1660. (** check and resolve character value **)
  1661. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1662. BEGIN
  1663. value.SetType(system.characterType);
  1664. resolvedExpression := value
  1665. END VisitCharacterValue;
  1666. (** check and resolve nil value **)
  1667. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1668. BEGIN
  1669. value.SetType(system.nilType);
  1670. resolvedExpression := value
  1671. END VisitNilValue;
  1672. (** check and resolve enumerator value **)
  1673. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1674. BEGIN
  1675. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1676. ASSERT(value.type # NIL);
  1677. resolvedExpression := value
  1678. END VisitEnumerationValue;
  1679. (*** expressions ***)
  1680. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1681. - check all elements on integer type
  1682. - if element range is constant, then check lower and upper bound
  1683. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1684. if an error occurs then report error and return invalidExpression
  1685. **)
  1686. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1687. VAR
  1688. i: LONGINT;
  1689. element: SyntaxTree.Expression;
  1690. constant: BOOLEAN;
  1691. elements: SyntaxTree.ExpressionList;
  1692. s: SET;
  1693. result: SyntaxTree.Expression;
  1694. value: SyntaxTree.Value;
  1695. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1696. VAR
  1697. left, right: SyntaxTree.Expression;
  1698. elementResult: SyntaxTree.Expression;
  1699. leftInteger, rightInteger: LONGINT;
  1700. BEGIN
  1701. (* set context of range *)
  1702. IF element IS SyntaxTree.RangeExpression THEN
  1703. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1704. END;
  1705. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1706. IF elementResult = SyntaxTree.invalidExpression THEN
  1707. (* error already reported *)
  1708. constant := FALSE
  1709. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1710. (* the element is a range expression *)
  1711. (* extract left and right hand side of range *)
  1712. left := elementResult(SyntaxTree.RangeExpression).first;
  1713. right := elementResult(SyntaxTree.RangeExpression).last;
  1714. (* guaranteed by VisitRangeExpression: *)
  1715. ASSERT((left # NIL) & (right # NIL));
  1716. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1717. ELSE
  1718. (* the element is not a range expression *)
  1719. (* check type and add conversion if needed *)
  1720. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1721. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1722. ELSE
  1723. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1724. elementResult := SyntaxTree.invalidExpression;
  1725. constant := FALSE
  1726. END;
  1727. left := elementResult;
  1728. right := elementResult
  1729. END;
  1730. IF elementResult # SyntaxTree.invalidExpression THEN
  1731. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1732. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1733. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1734. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1735. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1736. END
  1737. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1738. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1739. ELSE
  1740. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1741. s := s + {leftInteger..rightInteger};
  1742. (* same but more explicit:
  1743. WHILE (leftInteger <= rightInteger) DO
  1744. INCL(s,leftInteger);
  1745. INC(leftInteger);
  1746. END;
  1747. *)
  1748. END;
  1749. ELSE
  1750. constant := FALSE;
  1751. END
  1752. END;
  1753. RETURN elementResult
  1754. END CheckElement;
  1755. BEGIN
  1756. result := set; constant := TRUE; s := {}; elements := set.elements;
  1757. IF elements # NIL THEN
  1758. FOR i := 0 TO elements.Length()-1 DO
  1759. element := elements.GetExpression(i);
  1760. element := CheckElement(element);
  1761. IF element = SyntaxTree.invalidExpression THEN
  1762. result := SyntaxTree.invalidExpression
  1763. END;
  1764. elements.SetExpression(i,element);
  1765. END;
  1766. END;
  1767. IF constant THEN
  1768. value := SyntaxTree.NewSetValue(set.position,s);
  1769. value.SetType(system.setType);
  1770. result.SetResolved(value);
  1771. END;
  1772. (* optimization possible
  1773. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1774. left this to the programmer...
  1775. *)
  1776. result.SetType(system.setType);
  1777. resolvedExpression := result;
  1778. END VisitSet;
  1779. (*
  1780. old variant: quite generic but needs better conversion handling, do this?
  1781. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1782. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1783. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1784. BEGIN
  1785. type := NIL;
  1786. numberElements := x.elements.Length();
  1787. FOR i := 0 TO numberElements-1 DO
  1788. expression := x.elements.GetExpression(i);
  1789. position := expression.position;
  1790. expression := ResolveExpression(x.elements.GetExpression(i));
  1791. x.elements.SetExpression(i,de);
  1792. IF type = NIL THEN
  1793. type := expression.type;
  1794. ELSIF CompatibleTo(system,expression.type,type) THEN
  1795. (* ok *)
  1796. ELSIF CompatibleTo(system,type,expression.type) THEN
  1797. type := expression.type
  1798. ELSE
  1799. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1800. type := SyntaxTree.invalidType;
  1801. END;
  1802. END;
  1803. isValue := TRUE;
  1804. FOR i := 0 TO numberElements-1 DO
  1805. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1806. x.elements.SetExpression(i,expression);
  1807. isValue := isValue & (expression.resolved # NIL);
  1808. END;
  1809. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1810. arrayType.SetArrayBase(type);
  1811. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1812. IF isValue THEN
  1813. value := SyntaxTree.NewMathArrayValue(position);
  1814. value.SetElements(x.elements);
  1815. x.SetResolved(value);
  1816. END;
  1817. x.SetType(arrayType);
  1818. resolvedExpression := x;
  1819. END VisitMathArrayExpression;
  1820. *)
  1821. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1822. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1823. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1824. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1825. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1826. BEGIN
  1827. numberElements := x.elements.Length();
  1828. FOR i := 0 TO numberElements-1 DO
  1829. expression := x.elements.GetExpression(i);
  1830. IF expression IS SyntaxTree.MathArrayExpression THEN
  1831. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1832. ELSE
  1833. position := expression.position;
  1834. expression := ResolveExpression(x.elements.GetExpression(i));
  1835. x.elements.SetExpression(i,expression);
  1836. IF type = NIL THEN
  1837. type := expression.type;
  1838. ELSIF CompatibleTo(system,expression.type,type) THEN
  1839. (* ok *)
  1840. ELSIF CompatibleTo(system,type,expression.type) THEN
  1841. type := expression.type
  1842. ELSE
  1843. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1844. type := SyntaxTree.invalidType;
  1845. END;
  1846. END;
  1847. END;
  1848. END RecursivelyFindType;
  1849. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1850. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1851. BEGIN
  1852. numberElements := x.elements.Length();
  1853. FOR i := 0 TO numberElements-1 DO
  1854. expression := x.elements.GetExpression(i);
  1855. IF expression IS SyntaxTree.MathArrayExpression THEN
  1856. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1857. ELSE
  1858. position := expression.position;
  1859. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1860. x.elements.SetExpression(i,expression);
  1861. isValue := isValue & (expression.resolved # NIL);
  1862. END;
  1863. END;
  1864. END RecursivelySetExpression;
  1865. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1866. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1867. arrayType: SyntaxTree.MathArrayType;
  1868. BEGIN
  1869. numberElements := x.elements.Length();
  1870. baseType := NIL;
  1871. gsize := 0;
  1872. FOR i := 0 TO numberElements-1 DO
  1873. expression := x.elements.GetExpression(i);
  1874. IF expression IS SyntaxTree.MathArrayExpression THEN
  1875. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1876. IF i=0 THEN
  1877. gsize := size;
  1878. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1879. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1880. ELSE expression.SetType(baseType)
  1881. END;
  1882. ELSIF baseType = NIL THEN baseType := type;
  1883. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1884. END;
  1885. END;
  1886. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1887. arrayType.SetArrayBase(baseType);
  1888. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1889. RETURN ResolveType(arrayType);
  1890. END RecursivelySetType;
  1891. BEGIN
  1892. type := NIL;
  1893. RecursivelyFindType(x);
  1894. isValue := TRUE;
  1895. RecursivelySetExpression(x);
  1896. arrayType := RecursivelySetType(x);
  1897. x.SetType(arrayType);
  1898. IF isValue THEN
  1899. value := SyntaxTree.NewMathArrayValue(x.position);
  1900. value.SetArray(x);
  1901. x.SetResolved(value);
  1902. value.SetType(arrayType);
  1903. END;
  1904. x.SetType(arrayType);
  1905. resolvedExpression := x;
  1906. END VisitMathArrayExpression;
  1907. (** check and resolve unary expression **)
  1908. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1909. VAR
  1910. left: SyntaxTree.Expression;
  1911. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1912. bool: BOOLEAN;
  1913. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1914. value: SyntaxTree.Value;
  1915. BEGIN
  1916. type := SyntaxTree.invalidType;
  1917. left := ResolveExpression(unaryExpression.left);
  1918. unaryExpression.SetLeft(left);
  1919. operator := unaryExpression.operator;
  1920. result := unaryExpression;
  1921. IF ~system.operatorDefined[operator] THEN
  1922. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1923. RETURN
  1924. ELSIF left.type = NIL THEN
  1925. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1926. resolvedExpression := SyntaxTree.invalidExpression;
  1927. RETURN
  1928. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1929. RETURN
  1930. END;
  1931. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1932. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1933. END;
  1934. IF operatorCall # NIL THEN
  1935. result := operatorCall;
  1936. type := operatorCall.type;
  1937. (* admissible operators
  1938. Minus number, set
  1939. Not boolean
  1940. *)
  1941. ELSE
  1942. CASE unaryExpression.operator OF
  1943. |Scanner.Minus:
  1944. IF IsIntegerType(left.type.resolved) THEN
  1945. IF left.resolved # NIL THEN
  1946. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1947. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1948. result.SetResolved(value);
  1949. type := Global.GetIntegerType(system,int);
  1950. value.SetType(type);
  1951. ELSE
  1952. type := left.type
  1953. END
  1954. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1955. IF IsRealValue(left,real) THEN
  1956. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1957. result.SetResolved(value);
  1958. type := left.type;
  1959. value.SetType(type);
  1960. ELSE
  1961. type := left.type;
  1962. END;
  1963. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1964. IF IsSetValue(left,set) THEN
  1965. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1966. result.SetResolved(value);
  1967. type := left.type;
  1968. value.SetType(type);
  1969. ELSE
  1970. type := left.type;
  1971. END;
  1972. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1973. IF IsComplexValue(left, real, imaginary) THEN
  1974. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1975. result.SetResolved(value);
  1976. type := left.type;
  1977. value.SetType(type);
  1978. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1979. ELSE
  1980. type := left.type;
  1981. END
  1982. ELSE
  1983. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1984. END;
  1985. |Scanner.Not:
  1986. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1987. IF IsBooleanValue(left,bool) THEN
  1988. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1989. result.SetResolved(value);
  1990. type := system.booleanType;
  1991. value.SetType(type);
  1992. ELSE
  1993. type := system.booleanType;
  1994. END;
  1995. ELSE
  1996. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1997. END;
  1998. |Scanner.Plus:
  1999. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2000. result := left; type := left.type;
  2001. ELSE
  2002. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2003. END;
  2004. (* ADDRESS OF *)
  2005. |Scanner.Address:
  2006. IF HasAddress(left) THEN
  2007. type := system.addressType;
  2008. ELSE
  2009. type := SyntaxTree.invalidType;
  2010. Error(left.position,Diagnostics.Invalid,"has no address");
  2011. Printout.Info("par", left);
  2012. END;
  2013. (* SIZE OF *)
  2014. |Scanner.Size:
  2015. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2016. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2017. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2018. value := SyntaxTree.NewIntegerValue(left.position, int);
  2019. result.SetResolved(value);
  2020. type := Global.GetIntegerType(system,int);
  2021. value.SetType(type)
  2022. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2023. ELSE
  2024. (* for variables, system sizeof could represent the physically occupied size
  2025. determined via the type descriptor, implement that ? *)
  2026. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2027. END
  2028. (* ALIAS OF *)
  2029. |Scanner.Alias:
  2030. type := left.type.resolved;
  2031. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2032. type := SyntaxTree.invalidType;
  2033. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2034. END;
  2035. ELSE
  2036. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2037. END;
  2038. END;
  2039. result.SetType(type);
  2040. resolvedExpression := result
  2041. END VisitUnaryExpression;
  2042. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2043. VAR
  2044. result: SyntaxTree.Expression;
  2045. array: SyntaxTree.MathArrayExpression;
  2046. value: SyntaxTree.MathArrayValue;
  2047. isValue: BOOLEAN;
  2048. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2049. BEGIN
  2050. type := type.resolved;
  2051. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2052. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2053. END;
  2054. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2055. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2056. END;
  2057. RETURN type
  2058. END BaseType;
  2059. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2060. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2061. BEGIN
  2062. numberElements := x.elements.Length();
  2063. FOR i := 0 TO numberElements-1 DO
  2064. expression := x.elements.GetExpression(i);
  2065. IF expression IS SyntaxTree.MathArrayExpression THEN
  2066. array := SyntaxTree.NewMathArrayExpression(position);
  2067. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2068. expression := array;
  2069. ELSE
  2070. position := expression.position;
  2071. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2072. isValue := isValue & (expression.resolved # NIL);
  2073. END;
  2074. to.elements.AddExpression(expression);
  2075. END;
  2076. END RecursivelyConvert;
  2077. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2078. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2079. arrayType: SyntaxTree.MathArrayType;
  2080. BEGIN
  2081. numberElements := x.elements.Length();
  2082. baseType := NIL;
  2083. gsize := 0;
  2084. FOR i := 0 TO numberElements-1 DO
  2085. expression := x.elements.GetExpression(i);
  2086. IF expression IS SyntaxTree.MathArrayExpression THEN
  2087. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2088. IF i=0 THEN
  2089. gsize := size;
  2090. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2091. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2092. ELSE expression.SetType(baseType)
  2093. END;
  2094. ELSIF baseType = NIL THEN baseType := type;
  2095. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2096. END;
  2097. END;
  2098. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2099. arrayType.SetArrayBase(baseType);
  2100. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2101. RETURN ResolveType(arrayType);
  2102. END RecursivelySetType;
  2103. BEGIN
  2104. result := SyntaxTree.invalidExpression;
  2105. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2106. result := expression (* do not convert *)
  2107. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2108. isValue := TRUE;
  2109. type := BaseType(type);
  2110. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2111. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2112. value := SyntaxTree.NewMathArrayValue(array.position);
  2113. value.SetArray(array);
  2114. value.SetType(RecursivelySetType(array));
  2115. result := value;
  2116. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2117. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2118. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2119. IF result = NIL THEN
  2120. result := SyntaxTree.invalidExpression;
  2121. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2122. IF VerboseErrorMessage THEN
  2123. Printout.Info("expression",expression);
  2124. Printout.Info("type",type);
  2125. END;
  2126. END;
  2127. END;
  2128. RETURN result
  2129. END MathArrayConversion;
  2130. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2131. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2132. BEGIN
  2133. result := expression; type := type.resolved;
  2134. IF (expression IS SyntaxTree.IntegerValue) THEN
  2135. int := expression(SyntaxTree.IntegerValue).hvalue;
  2136. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2137. int := Global.ConvertSigned(int,system.SizeOf(type));
  2138. result := SyntaxTree.NewIntegerValue(position,int);
  2139. result.SetType(type);
  2140. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2141. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2142. result := SyntaxTree.NewIntegerValue(position,int);
  2143. result.SetType(type);
  2144. ELSIF (type IS SyntaxTree.FloatType) THEN
  2145. result := SyntaxTree.NewRealValue(expression.position,int);
  2146. result.SetType(type);
  2147. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2148. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2149. result.SetType(type);
  2150. ELSIF (type IS SyntaxTree.SetType) THEN
  2151. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2152. result.SetType(type);
  2153. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2154. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2155. result.SetType(type);
  2156. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2157. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2158. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2159. END;
  2160. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2161. result.SetType(type);
  2162. ELSIF (type IS SyntaxTree.PortType) THEN
  2163. result := ConvertValue(position, expression, system.integerType);
  2164. ELSE
  2165. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2166. result := SyntaxTree.invalidExpression;
  2167. IF VerboseErrorMessage THEN
  2168. Printout.Info("expression",expression);
  2169. Printout.Info("type",type);
  2170. END;
  2171. END;
  2172. ELSIF IsRealValue(expression,real) THEN
  2173. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2174. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2175. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2176. result.SetType(type);
  2177. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2178. int := ENTIERH(real);
  2179. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2180. result.SetType(type);
  2181. ELSIF (type IS SyntaxTree.FloatType) THEN
  2182. result := SyntaxTree.NewRealValue(position,real);
  2183. result.SetType(type);
  2184. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2185. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2186. result.SetType(type);
  2187. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2188. ELSIF (type IS SyntaxTree.PortType) THEN
  2189. result := ConvertValue(position, expression, system.integerType);
  2190. ELSE
  2191. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2192. result := SyntaxTree.invalidExpression;
  2193. END
  2194. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2195. IF (type IS SyntaxTree.ComplexType) THEN
  2196. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2197. result.SetType(type);
  2198. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2199. ELSE
  2200. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2201. result := SyntaxTree.invalidExpression;
  2202. END
  2203. ELSIF IsSetValue(expression,set) THEN
  2204. IF (type IS SyntaxTree.IntegerType) THEN
  2205. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2208. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.PortType) THEN
  2211. result := ConvertValue(position, expression, system.integerType);
  2212. ELSE
  2213. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2214. result := SyntaxTree.invalidExpression;
  2215. END;
  2216. ELSIF IsStringValue(expression,string) THEN
  2217. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2218. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2219. result.SetType(type);
  2220. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2221. ELSE
  2222. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2223. result := SyntaxTree.invalidExpression;
  2224. END;
  2225. ELSIF IsCharacterValue(expression,char) THEN
  2226. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2227. string[0] := char; string[1] := 0X;
  2228. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2229. result := SyntaxTree.NewStringValue(expression.position,string);
  2230. result.SetType(type);
  2231. ELSIF (type IS SyntaxTree.ByteType) THEN
  2232. (* do not simply set the new type as this could invalidate types of constants *)
  2233. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2234. result.SetType(type)
  2235. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2236. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2237. result.SetType(type);
  2238. ELSIF (type IS SyntaxTree.SetType) THEN
  2239. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2240. result.SetType(type);
  2241. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2242. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.PortType) THEN
  2245. result := ConvertValue(position, expression, system.integerType);
  2246. ELSE
  2247. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2248. result := SyntaxTree.invalidExpression;
  2249. END;
  2250. ELSIF expression IS SyntaxTree.NilValue THEN
  2251. IF type IS SyntaxTree.AddressType THEN
  2252. result := SyntaxTree.NewIntegerValue(position,0);
  2253. result.SetType(type);
  2254. ELSE
  2255. result := expression;
  2256. END;
  2257. (* nothing to be done *)
  2258. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2259. result := MathArrayConversion(position, expression,type);
  2260. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2261. int := expression(SyntaxTree.EnumerationValue).value;
  2262. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2263. int := Global.ConvertSigned(int,system.SizeOf(type));
  2264. result := SyntaxTree.NewIntegerValue(position,int);
  2265. result.SetType(type);
  2266. ELSE
  2267. result := expression;
  2268. END;
  2269. (* nothing to be done *)
  2270. ELSE
  2271. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2272. IF VerboseErrorMessage THEN
  2273. Printout.Info("expression",expression);
  2274. Printout.Info("type",type);
  2275. END;
  2276. result := SyntaxTree.invalidExpression;
  2277. END;
  2278. RETURN result
  2279. END ConvertValue;
  2280. (**
  2281. return a conversion of an expression to a given type
  2282. - if expression is already of same type then return expression
  2283. - if incompatible conversion then report error and return invalidExpression
  2284. **)
  2285. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2286. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2287. BEGIN
  2288. type := type.resolved;
  2289. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2290. result := expression;
  2291. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2292. ELSIF expression = NIL THEN (* NIL expression *)
  2293. ELSIF expression.type = NIL THEN
  2294. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2295. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2296. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2297. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2298. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2299. ELSIF expression.resolved # NIL THEN (* value *)
  2300. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2301. IF value IS SyntaxTree.Value THEN
  2302. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2303. result.SetResolved(value(SyntaxTree.Value));
  2304. result.SetType(value.type);
  2305. ELSE
  2306. result := value
  2307. END;
  2308. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2309. expressionList := SyntaxTree.NewExpressionList();
  2310. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2311. typeDeclaration.SetDeclaredType(type);
  2312. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2313. typeSymbol.SetType(typeDeclaration.type);
  2314. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2315. expressionList.AddExpression(expression);
  2316. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2317. result.SetType(type);
  2318. ELSIF IsArrayStructuredObjectType(type) THEN
  2319. (* no type can be converted to an array-structured object type *)
  2320. HALT(100)
  2321. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2322. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2323. result := MathArrayConversion(position, expression,type);
  2324. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2325. expression := ConvertToMathArray(expression);
  2326. type := MathArrayStructureOfType(type);
  2327. result := MathArrayConversion(position, expression, type)
  2328. ELSE
  2329. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2330. END;
  2331. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2332. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2333. OR ~(type IS SyntaxTree.ArrayType) THEN
  2334. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2335. END;
  2336. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2337. (*skip, no conversion*)
  2338. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2339. (* skip, no conversion *)
  2340. ELSE
  2341. ASSERT(~(type IS SyntaxTree.RangeType));
  2342. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2343. ASSERT(type # NIL);
  2344. END;
  2345. RETURN result
  2346. END NewConversion;
  2347. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2348. BEGIN
  2349. IF CompatibleTo(system,expression.type, type) THEN
  2350. RETURN NewConversion(position, expression, type, NIL);
  2351. ELSE
  2352. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2353. RETURN SyntaxTree.invalidExpression
  2354. END;
  2355. END CompatibleConversion;
  2356. (**
  2357. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2358. **)
  2359. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2360. VAR leftType,rightType: SyntaxTree.Type;
  2361. BEGIN
  2362. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2363. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2364. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2365. (* error already handled *)
  2366. ELSE
  2367. leftType := left.type.resolved; rightType := right.type.resolved;
  2368. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2369. right := NewConversion(right.position, right, leftType, NIL);
  2370. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2371. left := NewConversion(left.position,left,rightType,NIL);
  2372. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2373. right := NewConversion(right.position, right, leftType, NIL);
  2374. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2375. left := NewConversion(left.position,left,rightType,NIL);
  2376. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2377. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2378. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2379. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2380. ELSIF
  2381. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2382. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2383. (* must be the case LONGREAL / COMPLEX ) *)
  2384. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2385. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2386. ELSE
  2387. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2388. END;
  2389. END;
  2390. END ConvertOperands;
  2391. (** find and return best operator matching to parameter list (nil, if none)
  2392. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2393. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2394. **)
  2395. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2396. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2397. identifier: SyntaxTree.Identifier;
  2398. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2399. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2400. BEGIN
  2401. operator := scope.firstOperator;
  2402. WHILE(operator # NIL) DO
  2403. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2404. procedureType := operator.type(SyntaxTree.ProcedureType);
  2405. distance := Distance(system, procedureType,actualParameters);
  2406. IF (distance < Infinity) THEN
  2407. IF returnType # NIL THEN
  2408. IF procedureType.returnType = NIL THEN
  2409. distance := Infinity
  2410. ELSE
  2411. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2412. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2413. END;
  2414. END;
  2415. END;
  2416. (*
  2417. IF distance < Infinity THEN
  2418. TRACE(distance, operator);
  2419. Printout.Info("potential operator",operator);
  2420. ELSE
  2421. Printout.Info("operator not possible",operator);
  2422. END;
  2423. *)
  2424. IF distance < bestDistance THEN
  2425. bestDistance := distance;
  2426. bestOperator := operator;
  2427. END;
  2428. END;
  2429. operator := operator.nextOperator;
  2430. END;
  2431. (*
  2432. Printout.Info("taken operator",bestOperator);
  2433. *)
  2434. END FindInScope;
  2435. BEGIN
  2436. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2437. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2438. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2439. import := currentScope.ownerModule.moduleScope.firstImport;
  2440. WHILE (bestDistance > 0) & (import # NIL) DO
  2441. IF import.module # NIL THEN
  2442. identifier := Global.GetIdentifier(operator,import.module.case);
  2443. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2444. END;
  2445. import := import.nextImport;
  2446. END;
  2447. RETURN bestOperator
  2448. END FindOperator;
  2449. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2450. BEGIN
  2451. currentScope := scope;
  2452. END SetCurrentScope;
  2453. (**
  2454. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2455. - handle LEN and DIM operator for array-structured object types
  2456. - find operator, if found then
  2457. - if in other module then add import designator
  2458. - create symbol designator for operator
  2459. - if error then return invalidExpression, if no operator then return NIL
  2460. **)
  2461. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2462. VAR
  2463. operator: SyntaxTree.Operator;
  2464. import: SyntaxTree.Import;
  2465. expression, result: SyntaxTree.Expression;
  2466. designator: SyntaxTree.Designator;
  2467. actualParameters, tempList: SyntaxTree.ExpressionList;
  2468. recordType: SyntaxTree.RecordType;
  2469. castReturnType : SyntaxTree.MathArrayType;
  2470. BEGIN
  2471. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2472. result := SyntaxTree.invalidExpression
  2473. ELSIF leftExpression = NIL THEN
  2474. result := NIL
  2475. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2476. (* LEN or DIM operator on array-structured object type *)
  2477. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2478. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2479. IF recordType.arrayAccessOperators.len = NIL THEN
  2480. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2481. result := SyntaxTree.invalidExpression
  2482. ELSE
  2483. ASSERT(leftExpression IS SyntaxTree.Designator);
  2484. designator := leftExpression(SyntaxTree.Designator);
  2485. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2486. ASSERT(expression IS SyntaxTree.Designator);
  2487. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2488. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2489. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2490. result := designator
  2491. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2492. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2493. tempList := SyntaxTree.NewExpressionList();
  2494. tempList.AddExpression(rightExpression);
  2495. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2496. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2497. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2498. tempList := SyntaxTree.NewExpressionList();
  2499. tempList.AddExpression(designator);
  2500. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2501. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2502. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2503. END
  2504. END;
  2505. ELSE
  2506. (* import OCArrayBase if needed *)
  2507. IF ~arrayBaseImported THEN
  2508. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2509. (* operators on complex numbers *)
  2510. ImportModule(Global.ArrayBaseName,position);
  2511. arrayBaseImported := TRUE;
  2512. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2513. IF op = Global.Dim THEN
  2514. (* not existing in OCArrayBase *)
  2515. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2516. (* not existing in OCArrayBase *)
  2517. ELSE
  2518. ImportModule(Global.ArrayBaseName,position);
  2519. arrayBaseImported := TRUE;
  2520. END
  2521. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2522. ImportModule(Global.ArrayBaseName,position);
  2523. arrayBaseImported := TRUE
  2524. END;
  2525. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2526. (* LEN(RANGE) *)
  2527. ImportModule(Global.ArrayBaseName,position);
  2528. arrayBaseImported := TRUE;
  2529. END;
  2530. END;
  2531. actualParameters := SyntaxTree.NewExpressionList();
  2532. actualParameters.AddExpression(leftExpression);
  2533. IF rightExpression # NIL THEN
  2534. actualParameters.AddExpression(rightExpression)
  2535. END;
  2536. operator := FindOperator(system,op,actualParameters,resultType);
  2537. IF operator # NIL THEN
  2538. designator := NIL;
  2539. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2540. import := currentScope.ownerModule.moduleScope.firstImport;
  2541. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2542. import := import.nextImport;
  2543. END;
  2544. expression := NewSymbolDesignator(position,NIL,import);
  2545. designator := expression(SyntaxTree.Designator);
  2546. END;
  2547. expression := NewSymbolDesignator(position,designator,operator);
  2548. designator := expression(SyntaxTree.Designator);
  2549. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2550. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2551. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2552. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2553. result.SetType(castReturnType);
  2554. END;
  2555. ELSE
  2556. result := NIL;
  2557. END;
  2558. END;
  2559. RETURN result
  2560. END NewOperatorCall;
  2561. (** check and resolve binary expression **)
  2562. (*! clean up *)
  2563. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2564. VAR left,right,result: SyntaxTree.Expression;
  2565. leftType, rightType: SyntaxTree.Type;
  2566. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2567. cl,cr: CHAR;
  2568. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2569. type: SyntaxTree.Type;
  2570. value: SyntaxTree.Value;
  2571. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2572. integerConstantFolding: BOOLEAN;
  2573. PROCEDURE NewBool(v: BOOLEAN);
  2574. BEGIN
  2575. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2576. value.SetType(system.booleanType);
  2577. result.SetResolved(value);
  2578. type := system.booleanType
  2579. END NewBool;
  2580. PROCEDURE NewSet(v: SET);
  2581. BEGIN
  2582. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2583. value.SetType(system.setType);
  2584. result.SetResolved(value);
  2585. type := system.setType;
  2586. END NewSet;
  2587. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2588. BEGIN
  2589. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2590. (* type cast to "larger" type only if the value is still in the range *)
  2591. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2592. value.SetType(t);
  2593. END;
  2594. result.SetResolved(value);
  2595. type := value.type;
  2596. END NewInteger;
  2597. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2598. BEGIN
  2599. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2600. value.SetType(t);
  2601. result.SetResolved(value);
  2602. type := t;
  2603. END NewReal;
  2604. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2605. BEGIN
  2606. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2607. value.SetType(t);
  2608. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2609. result.SetResolved(value);
  2610. type := t;
  2611. END NewComplex;
  2612. BEGIN
  2613. type := SyntaxTree.invalidType;
  2614. left := ResolveExpression(binaryExpression.left);
  2615. right := ResolveExpression(binaryExpression.right);
  2616. binaryExpression.SetLeft(left);
  2617. binaryExpression.SetRight(right);
  2618. result := binaryExpression;
  2619. operator := binaryExpression.operator;
  2620. IF ~system.operatorDefined[operator] THEN
  2621. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2622. result := SyntaxTree.invalidExpression;
  2623. RETURN
  2624. END;
  2625. IF left.type = NIL THEN
  2626. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2627. result := SyntaxTree.invalidExpression;
  2628. RETURN;
  2629. END;
  2630. IF right.type = NIL THEN
  2631. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2632. result := SyntaxTree.invalidExpression;
  2633. RETURN;
  2634. END;
  2635. leftType := left.type.resolved; rightType := right.type.resolved;
  2636. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2637. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2638. END;
  2639. IF operatorCall # NIL THEN
  2640. result := operatorCall;
  2641. type := operatorCall.type;
  2642. (* admissible operators:
  2643. Times, Plus, Minus numeric numeric numeric
  2644. set set set
  2645. Slash numeric numeric real /complex
  2646. set set set
  2647. Div , Mod integer integer integer
  2648. And, Or bool bool bool
  2649. Equal, Unequal basic basic bool
  2650. pointer pointer bool
  2651. object object bool
  2652. record record bool
  2653. string string bool
  2654. enumerator enumerator bool
  2655. Less, LessEqual,
  2656. Greater, GreaterEqual integer/real integer/real bool
  2657. enumerator enumerator bool
  2658. In integer set bool
  2659. Is pointer type bool
  2660. object type bool
  2661. record type bool
  2662. Upto: special abbreviation for a..b
  2663. *)
  2664. ELSIF (left.type = NIL) THEN
  2665. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2666. D.Str("nil type in "); D.Type(left); D.Ln;
  2667. result := SyntaxTree.invalidExpression;
  2668. ELSIF (right.type = NIL) THEN
  2669. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2670. result := SyntaxTree.invalidExpression;
  2671. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2672. result := SyntaxTree.invalidExpression;
  2673. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2674. HALT(100);
  2675. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2676. type := system.booleanType;
  2677. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2678. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2679. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2680. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2681. IF VerboseErrorMessage THEN
  2682. Printout.Info("left",left);
  2683. Printout.Info("right",right);
  2684. END;
  2685. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2686. NewBool(TRUE)
  2687. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2688. NewBool(TRUE);
  2689. ELSIF IsUnextensibleRecord(left) THEN
  2690. NewBool(FALSE)
  2691. END
  2692. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2693. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2694. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2695. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2696. ELSIF operator = Scanner.In THEN (* left IN right *)
  2697. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2698. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2699. NewBool(il IN sr);
  2700. ELSE
  2701. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2702. left := NewConversion(left.position, left, system.longintType,NIL);
  2703. binaryExpression.SetLeft(left)
  2704. END;
  2705. type := system.booleanType;
  2706. END
  2707. ELSE
  2708. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2709. END
  2710. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2711. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2712. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2713. END;
  2714. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2715. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2716. END
  2717. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2718. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2719. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2720. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2721. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2722. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2723. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2724. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2725. END;
  2726. type := system.booleanType;
  2727. ELSE
  2728. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2729. END
  2730. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2731. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2732. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2733. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2734. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2735. CASE operator OF
  2736. |Scanner.Equal: NewBool(strl^=strr^);
  2737. |Scanner.Unequal:NewBool(strl^#strr^);
  2738. |Scanner.Less: NewBool(strl^<strr^);
  2739. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2740. |Scanner.Greater: NewBool(strl^>strr^);
  2741. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2742. ELSE
  2743. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2744. END;
  2745. END;
  2746. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2747. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2748. type := system.booleanType
  2749. ELSE
  2750. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2751. END;
  2752. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2753. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2754. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2755. type := system.booleanType;
  2756. ELSE
  2757. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2758. END
  2759. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2760. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2761. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2762. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2763. type := system.booleanType
  2764. ELSE
  2765. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2766. END;
  2767. ELSE
  2768. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2769. END;
  2770. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2771. type := system.booleanType;
  2772. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2773. type := system.booleanType;
  2774. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2775. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2776. THEN
  2777. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2778. IF (leftType # rightType) THEN
  2779. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2780. ConvertOperands(left,right); (* operands must be of the same type here *)
  2781. END;
  2782. binaryExpression.SetLeft(left);
  2783. binaryExpression.SetRight(right);
  2784. leftType := left.type.resolved;
  2785. rightType := right.type.resolved;
  2786. END;
  2787. type := leftType;
  2788. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2789. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2790. IF VerboseErrorMessage THEN
  2791. Printout.Info("left",left);
  2792. Printout.Info("right",right);
  2793. END;
  2794. ELSIF IsIntegerType(leftType) THEN
  2795. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2796. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2797. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2798. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2799. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2801. END;
  2802. END;
  2803. (* constant folding *)
  2804. (* bootstrap64
  2805. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2806. CASE operator OF
  2807. |Scanner.Plus: NewInteger(il+ir,left.type);
  2808. |Scanner.Minus: NewInteger(il-ir,left.type);
  2809. |Scanner.Times: NewInteger(il*ir,left.type);
  2810. |Scanner.Slash:
  2811. IF ir # 0 THEN
  2812. NewReal(il/ir, system.realType);
  2813. END;
  2814. |Scanner.Mod:
  2815. IF ir > 0 THEN
  2816. NewInteger(il MOD ir,left.type);
  2817. END;
  2818. |Scanner.Div:
  2819. IF ir > 0 THEN
  2820. NewInteger(il DIV ir,left.type);
  2821. END;
  2822. |Scanner.Equal: NewBool(il=ir);
  2823. |Scanner.Unequal:NewBool(il#ir);
  2824. |Scanner.Less: NewBool(il<ir);
  2825. |Scanner.LessEqual: NewBool(il<=ir);
  2826. |Scanner.Greater: NewBool(il>ir);
  2827. |Scanner.GreaterEqual: NewBool(il>=ir);
  2828. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2829. END;
  2830. ELS*)
  2831. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2832. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2833. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2834. CASE operator OF
  2835. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2836. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2837. |Scanner.Times: NewInteger(hl*hr,left.type);
  2838. |Scanner.Slash:
  2839. IF hr = 0 THEN
  2840. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2841. ELSE
  2842. IF type.sizeInBits = 64 THEN
  2843. NewReal(hl/hr,system.longrealType);
  2844. ELSE
  2845. NewReal(hl/hr,system.realType)
  2846. END
  2847. END;
  2848. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2849. |Scanner.Mod:
  2850. IF hr = 0 THEN
  2851. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2852. ELSE
  2853. NewInteger(hl MOD hr, left.type);
  2854. (* bootstrap64
  2855. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2856. *)
  2857. END;
  2858. |Scanner.Div:
  2859. IF hr = 0 THEN
  2860. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2861. ELSE
  2862. NewInteger(hl DIV hr, left.type);
  2863. (* bootstrap64
  2864. NewInteger(Machine.DivH(hl,hr),left.type);
  2865. *)
  2866. END;
  2867. (* *)
  2868. |Scanner.Equal: NewBool(hl=hr);
  2869. |Scanner.Unequal: NewBool(hl#hr);
  2870. |Scanner.Less: NewBool(hl<hr);
  2871. |Scanner.LessEqual: NewBool(hl<=hr);
  2872. |Scanner.Greater: NewBool(hl>hr);
  2873. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2874. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2875. END;
  2876. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2877. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2878. type := left.type
  2879. ELSIF (operator = Scanner.Slash) THEN
  2880. left := NewConversion(left.position,left,system.realType,NIL);
  2881. right := NewConversion(right.position,right,system.realType,NIL);
  2882. binaryExpression.SetLeft(left);
  2883. binaryExpression.SetRight(right);
  2884. type := system.realType
  2885. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2886. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2887. type := system.booleanType
  2888. ELSE
  2889. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2890. END;
  2891. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2892. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2893. CASE operator OF
  2894. |Scanner.Plus: NewReal(rl+rr,leftType);
  2895. |Scanner.Minus: NewReal(rl-rr,leftType);
  2896. |Scanner.Times:NewReal(rl*rr,leftType);
  2897. |Scanner.Slash:
  2898. IF rr = 0 THEN
  2899. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2900. ELSE
  2901. NewReal(rl/rr,leftType);
  2902. END
  2903. |Scanner.Equal: NewBool(rl=rr);
  2904. |Scanner.Unequal: NewBool(rl#rr);
  2905. |Scanner.Less: NewBool(rl<rr);
  2906. |Scanner.LessEqual: NewBool(rl<=rr);
  2907. |Scanner.Greater: NewBool(rl>rr);
  2908. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2909. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2910. END;
  2911. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2912. type := left.type
  2913. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2914. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2915. type := system.booleanType
  2916. ELSE
  2917. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2918. IF VerboseErrorMessage THEN
  2919. Printout.Info("left",left);
  2920. Printout.Info("right",right);
  2921. END;
  2922. END;
  2923. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2924. CASE operator OF
  2925. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2926. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2927. ELSE
  2928. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2929. IF VerboseErrorMessage THEN
  2930. Printout.Info("left", left);
  2931. Printout.Info("right", right)
  2932. END;
  2933. END;
  2934. IF ~error THEN
  2935. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2936. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2937. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2938. (* do constant folding *)
  2939. CASE operator OF
  2940. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2941. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2942. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2943. |Scanner.Slash:
  2944. divisor := c * c + d * d;
  2945. ASSERT(divisor # 0);
  2946. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2947. |Scanner.Equal: NewBool((a = c) & (b = d))
  2948. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2949. END
  2950. END
  2951. END
  2952. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2953. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2954. CASE operator OF
  2955. |Scanner.And: NewBool(bl & br);
  2956. |Scanner.Or: NewBool(bl OR br);
  2957. |Scanner.Equal: NewBool(bl = br);
  2958. |Scanner.Unequal: NewBool(bl # br);
  2959. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2960. END;
  2961. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2962. type := system.booleanType
  2963. ELSE
  2964. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2965. END;
  2966. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2967. (* constant folding *)
  2968. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2969. IF operator = Scanner.Equal THEN
  2970. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2971. ELSIF operator = Scanner.Unequal THEN
  2972. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2973. END;
  2974. END;
  2975. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2976. type := system.booleanType;
  2977. ELSE
  2978. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2979. END;
  2980. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2981. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2982. CASE operator OF
  2983. |Scanner.Plus: NewSet(sl + sr);
  2984. |Scanner.Minus: NewSet(sl - sr);
  2985. |Scanner.Times: NewSet(sl * sr);
  2986. |Scanner.Slash: NewSet(sl / sr);
  2987. |Scanner.Equal: NewBool(sl=sr);
  2988. |Scanner.Unequal: NewBool(sl#sr);
  2989. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2990. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2991. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2992. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2993. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  2994. END;
  2995. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2996. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2997. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2998. THEN
  2999. type := system.booleanType
  3000. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3001. type := left.type
  3002. ELSE
  3003. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3004. END;
  3005. ELSIF IsCharacterType(left.type) THEN
  3006. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3007. CASE operator OF
  3008. |Scanner.Equal: NewBool(cl=cr);
  3009. |Scanner.Unequal: NewBool(cl#cr);
  3010. |Scanner.Less: NewBool(cl<cr);
  3011. |Scanner.LessEqual: NewBool(cl<=cr);
  3012. |Scanner.Greater: NewBool(cl>cr);
  3013. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3014. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3015. END;
  3016. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3017. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3018. type := system.booleanType
  3019. ELSE
  3020. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3021. END;
  3022. ELSE
  3023. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3024. END;
  3025. ELSE
  3026. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3027. END;
  3028. IF type = SyntaxTree.invalidType THEN
  3029. result := SyntaxTree.invalidExpression
  3030. ELSE
  3031. result.SetType(type)
  3032. END;
  3033. resolvedExpression := result
  3034. END VisitBinaryExpression;
  3035. (** resolve a range expression of the from <<first .. last BY step>>
  3036. - depending on the context different things are checked:
  3037. ArrayIndex:
  3038. - components must be integers
  3039. - replace missing lower bound with 0
  3040. - replace missing upper bound with MAX(LONGINT)
  3041. - replace missing step size with 1
  3042. SetElement:
  3043. - components must be integers
  3044. - replace missing lower bound with 0
  3045. - replace missing upper bound with MAX(SET)
  3046. - must not have step size
  3047. CaseGuard:
  3048. - components must be constant
  3049. - components must be integers or characters
  3050. - must have lower and upper bound present
  3051. - components are made compatible
  3052. - must not have step size
  3053. - if error: return invalidExpression
  3054. **)
  3055. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3056. VAR
  3057. hasError: BOOLEAN;
  3058. first, last, step: SyntaxTree.Expression;
  3059. BEGIN
  3060. hasError := FALSE;
  3061. first := x.first;
  3062. last := x.last;
  3063. step := x.step;
  3064. (* check lower bound *)
  3065. IF x.context = SyntaxTree.CaseGuard THEN
  3066. IF first = NIL THEN
  3067. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3068. hasError := TRUE
  3069. ELSE
  3070. first := ResolveExpression(first);
  3071. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3072. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3073. hasError := TRUE
  3074. ELSE
  3075. IF first IS SyntaxTree.StringValue THEN
  3076. (* add conversion from string to character *)
  3077. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3078. END
  3079. END;
  3080. (* check if expression is constant *)
  3081. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3082. (* error already reported *)
  3083. hasError := TRUE
  3084. END
  3085. END
  3086. ELSE (* ArrayIndex, SetElement *)
  3087. IF first = NIL THEN
  3088. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3089. END;
  3090. first := ResolveExpression(first);
  3091. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3092. first := NewConversion(first.position, first, system.longintType, NIL)
  3093. ELSE
  3094. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3095. hasError := TRUE
  3096. END
  3097. END;
  3098. (* check upper bound *)
  3099. IF x.context = SyntaxTree.CaseGuard THEN
  3100. IF last = NIL THEN
  3101. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3102. hasError := TRUE
  3103. ELSE
  3104. last := ResolveExpression(last);
  3105. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3106. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3107. hasError := TRUE
  3108. ELSE
  3109. IF last IS SyntaxTree.StringValue THEN
  3110. (* add conversion from string to character *)
  3111. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3112. END
  3113. END;
  3114. (* check if expression is constant *)
  3115. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3116. (* error already reported *)
  3117. hasError := TRUE
  3118. ELSE
  3119. (* try to make lower and upper bound compatible *)
  3120. ConvertOperands(first, last);
  3121. IF first.type.resolved # last.type.resolved THEN
  3122. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3123. hasError := TRUE
  3124. END
  3125. END
  3126. END
  3127. ELSE (* ArrayIndex, SetElement *)
  3128. IF last = NIL THEN
  3129. IF x.context = SyntaxTree.ArrayIndex THEN
  3130. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3131. ELSE
  3132. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3133. END
  3134. END;
  3135. last := ResolveExpression(last);
  3136. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3137. last := NewConversion(last.position, last, system.longintType, NIL)
  3138. ELSE
  3139. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3140. hasError := TRUE
  3141. END
  3142. END;
  3143. (* check step size *)
  3144. IF x.context = SyntaxTree.ArrayIndex THEN
  3145. IF step = NIL THEN
  3146. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3147. END;
  3148. step := ResolveExpression(step);
  3149. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3150. step := NewConversion(step.position, step, system.longintType, NIL)
  3151. ELSE
  3152. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3153. hasError := TRUE
  3154. END
  3155. ELSE (* SetElement, CaseGuard *)
  3156. IF step # NIL THEN
  3157. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3158. hasError := TRUE
  3159. END
  3160. END;
  3161. IF hasError THEN
  3162. resolvedExpression := SyntaxTree.invalidExpression
  3163. ELSE
  3164. x.SetFirst(first);
  3165. x.SetLast(last);
  3166. x.SetStep(step);
  3167. x.SetType(system.rangeType);
  3168. resolvedExpression := x;
  3169. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3170. END
  3171. END VisitRangeExpression;
  3172. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3173. BEGIN
  3174. x.SetType(NIL);
  3175. resolvedExpression := x;
  3176. END VisitTensorRangeExpression;
  3177. (** resolve the expression d and return result as designator
  3178. - resolve expression
  3179. - if expression is a designator then return designator else error message and return invalidDesignator
  3180. **)
  3181. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3182. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3183. BEGIN
  3184. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3185. resolved := ResolveExpression(d);
  3186. IF resolved = SyntaxTree.invalidExpression THEN
  3187. (* error should already have been reported *)
  3188. result := SyntaxTree.invalidDesignator;
  3189. ELSIF resolved IS SyntaxTree.Designator THEN
  3190. result := resolved(SyntaxTree.Designator);
  3191. ELSE
  3192. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3193. result := SyntaxTree.invalidDesignator;
  3194. END;
  3195. (* result.type might be nil. *)
  3196. RETURN result
  3197. END ResolveDesignator;
  3198. (**
  3199. symbol designator generated in this module
  3200. nothing to be resolved
  3201. **)
  3202. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3203. BEGIN
  3204. resolvedExpression := x;
  3205. END VisitSymbolDesignator;
  3206. (**
  3207. self designator generated in this module
  3208. nothing to be resolved
  3209. **)
  3210. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3211. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3212. BEGIN
  3213. (* check if in record scope *)
  3214. scope := currentScope;
  3215. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3216. scope := scope.outerScope;
  3217. END;
  3218. IF scope = NIL THEN (* in module scope *)
  3219. x.SetType(system.anyType);
  3220. ELSIF scope IS SyntaxTree.CellScope THEN
  3221. cell := scope(SyntaxTree.CellScope).ownerCell;
  3222. x.SetType(cell);
  3223. ELSE (* in record scope *)
  3224. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3225. IF (record # NIL) & (record.pointerType # NIL) THEN
  3226. type := ResolveType(record.pointerType);
  3227. x.SetType(type);
  3228. ELSE
  3229. x.SetType(record);
  3230. END;
  3231. END;
  3232. resolvedExpression := x;
  3233. END VisitSelfDesignator;
  3234. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3235. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3236. BEGIN
  3237. scope := currentScope;
  3238. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3239. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3240. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3241. returnType := procedureType.returnType;
  3242. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3243. THEN
  3244. x.SetType(returnType);
  3245. ELSE
  3246. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3247. x.SetType(SyntaxTree.invalidType);
  3248. END;
  3249. ELSE
  3250. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3251. x.SetType(SyntaxTree.invalidType);
  3252. END;
  3253. x.SetAssignable(TRUE);
  3254. resolvedExpression := x;
  3255. END VisitResultDesignator;
  3256. (**
  3257. return symbol designator as an expression
  3258. - if symbol is a constant then return the constant value expression
  3259. - else
  3260. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3261. - if symbol is a guarded variable then return a TypeGuardDesignator
  3262. - else return a symbol designator
  3263. **)
  3264. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3265. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3266. guardType: SyntaxTree.Type;
  3267. BEGIN
  3268. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3269. result := SyntaxTree.invalidExpression;
  3270. ASSERT(symbol # NIL);
  3271. (*
  3272. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3273. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3274. Error(position,Diagnostics.Invalid,"type not allowed here");
  3275. ELS *)
  3276. (* not needed any more as values are stored in the expression
  3277. IF symbol IS SyntaxTree.Constant THEN
  3278. result := symbol(SyntaxTree.Constant).value
  3279. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3280. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3281. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3282. ELSE
  3283. result := symbol(SyntaxTree.Constant).value
  3284. END;
  3285. ELSE
  3286. *)
  3287. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3288. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3289. THEN
  3290. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3291. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3292. left := NewDereferenceDesignator(position,left);
  3293. left.SetHidden(TRUE);
  3294. END;
  3295. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3296. scope := currentScope;
  3297. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3298. scope := scope.outerScope;
  3299. END;
  3300. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3301. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3302. END;
  3303. END;
  3304. 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);
  3305. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3306. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3307. ELSE
  3308. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3309. END;
  3310. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3311. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3312. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3313. result.SetType(symbol.type);
  3314. result.SetAssignable(assignable);
  3315. symbol.MarkUsed;
  3316. IF symbol IS SyntaxTree.Constant THEN
  3317. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3318. END;
  3319. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3320. variableAccessed := TRUE
  3321. END;
  3322. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3323. IF GetGuard(symbol,guardType) THEN
  3324. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType);
  3325. END;
  3326. END;
  3327. ASSERT(result.type # NIL);
  3328. RETURN result
  3329. END NewSymbolDesignator;
  3330. (** check and resolve an identifier designator "identifier"
  3331. - if identifier = self then return SelfDesignator
  3332. - else find symbol in current scope
  3333. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3334. **)
  3335. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3336. VAR symbol: SyntaxTree.Symbol;
  3337. BEGIN
  3338. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3339. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3340. IF symbol # NIL THEN
  3341. ResolveSymbol(symbol);
  3342. ASSERT(symbol.type # NIL);
  3343. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3344. ELSE
  3345. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3346. IF VerboseErrorMessage THEN
  3347. Printout.Info("undeclared identifier designator",identifierDesignator);
  3348. END;
  3349. resolvedExpression := SyntaxTree.invalidDesignator;
  3350. END;
  3351. END VisitIdentifierDesignator;
  3352. (** check and resolve a selector designator of the form left.designator
  3353. - if left is a pointer type then do auto dereferenciation
  3354. - left denotes a search scope:
  3355. - if left type is import type then set search scope to respective module
  3356. - if left type is enumeration type then set search scope to respective enumeration scope
  3357. - elsif left type is record type then set search scope to record scope
  3358. - search symbol in computed scope
  3359. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3360. **)
  3361. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3362. VAR
  3363. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3364. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3365. BEGIN
  3366. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3367. left := ResolveDesignator(selectorDesignator.left);
  3368. result := SyntaxTree.invalidDesignator;
  3369. IF left # NIL THEN
  3370. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3371. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3372. END;
  3373. scope := NIL;
  3374. IF left.type = NIL THEN
  3375. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3376. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3377. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3378. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3379. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3380. module := symbol(SyntaxTree.Import).module;
  3381. IF module # NIL THEN
  3382. scope := module.moduleScope
  3383. ELSE
  3384. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3385. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3386. END;
  3387. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3388. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3389. ASSERT(scope # NIL)
  3390. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3391. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3392. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3393. IF type IS SyntaxTree.EnumerationType THEN
  3394. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3395. ELSE
  3396. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3397. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3398. END;
  3399. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3400. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3401. ELSE
  3402. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3403. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3404. END;
  3405. symbol := NIL;
  3406. IF scope # NIL THEN
  3407. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3408. IF symbol # NIL THEN
  3409. ResolveSymbol(symbol);
  3410. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3411. symbol.MarkUsed
  3412. ELSE
  3413. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3414. IF VerboseErrorMessage THEN
  3415. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3416. Printout.Info("scope", scope);
  3417. Printout.Info("left", left);
  3418. Printout.Info("undeclared identifier",selectorDesignator);
  3419. Printout.Info("left resolved designator",left);
  3420. END
  3421. END;
  3422. END;
  3423. END;
  3424. resolvedExpression := result;
  3425. END VisitSelectorDesignator;
  3426. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3427. VAR len,idx: LONGINT;
  3428. BEGIN
  3429. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3430. IF idx < 0 THEN
  3431. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3432. ELSE
  3433. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3434. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3435. END;
  3436. END;
  3437. END;
  3438. END IndexCheck;
  3439. (*
  3440. - if index designator has not type, use newBaseType as its type
  3441. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3442. - special rule: if static array of dynamic array occurs, make it all dynamic
  3443. index designator type: new base type: new index designator type:
  3444. NIL z z
  3445. ARRAY [x, y] z ARRAY [x, y] OF z
  3446. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3447. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3448. *)
  3449. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3450. VAR
  3451. mathArrayType: SyntaxTree.MathArrayType;
  3452. makeDynamic: BOOLEAN;
  3453. BEGIN
  3454. IF indexDesignator.type = NIL THEN
  3455. indexDesignator.SetType(newBaseType)
  3456. ELSE
  3457. (* index designator must be a of math array type *)
  3458. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3459. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3460. (* determine if all arrays have to be made dynamic *)
  3461. makeDynamic :=
  3462. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3463. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3464. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3465. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3466. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3467. END;
  3468. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3469. mathArrayType.SetArrayBase(newBaseType)
  3470. END
  3471. END SetIndexBaseType;
  3472. (** check and append index list element to index designator of math array
  3473. - check validity of single index or array range
  3474. - compute new type
  3475. - if range then create new array type (calculate length of resulting array)
  3476. - otherwise take sourceArray.arrayBase as new type
  3477. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3478. **)
  3479. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3480. VAR
  3481. targetArray: SyntaxTree.MathArrayType;
  3482. first, last, step: SyntaxTree.Expression;
  3483. firstValue, lastValue, stepValue, length: LONGINT;
  3484. rangeExpression: SyntaxTree.RangeExpression;
  3485. BEGIN
  3486. IF indexListItem.type = SyntaxTree.invalidType THEN
  3487. (* error already handled *)
  3488. indexDesignator.parameters.AddExpression(indexListItem)
  3489. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3490. indexDesignator.HasRange;
  3491. indexDesignator.HasTensorRange;
  3492. indexDesignator.parameters.AddExpression(indexListItem);
  3493. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3494. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3495. IndexCheck(indexListItem, sourceArray.length);
  3496. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3497. indexDesignator.parameters.AddExpression(indexListItem)
  3498. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3499. indexDesignator.HasRange;
  3500. (* if the range is given as an array range expression, check the validity of its components *)
  3501. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3502. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3503. first := rangeExpression.first;
  3504. last := rangeExpression.last;
  3505. step := rangeExpression.step;
  3506. (* perform static checks on range components *)
  3507. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3508. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3509. END;
  3510. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3511. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3512. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3513. END
  3514. END;
  3515. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3516. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3517. END;
  3518. (* add conversions to size type *)
  3519. (* TODO: needed? *)
  3520. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3521. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3522. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3523. END;
  3524. IF indexDesignator.hasTensorRange THEN
  3525. (* the index designator's base type is a tensor: leave it as is *)
  3526. ELSE
  3527. (* append a new math array to the index designator's base type *)
  3528. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3529. IF ~error THEN
  3530. (*
  3531. (* optimization: calculate length of target array for static ranges *)
  3532. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3533. IF IsStaticallyOpenRange(rangeExpression) THEN
  3534. (* range is open ('*'): reuse source array length as target array length *)
  3535. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3536. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3537. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3538. IF lastValue = MAX(LONGINT) THEN
  3539. IF IsIntegerValue(sourceArray.length, length) THEN
  3540. lastValue := length - 1;
  3541. isStaticTargetArrayLength := TRUE
  3542. ELSE
  3543. isStaticTargetArrayLength := FALSE
  3544. END
  3545. ELSE
  3546. isStaticTargetArrayLength := TRUE
  3547. END;
  3548. IF isStaticTargetArrayLength THEN
  3549. (* calculate static target array length *)
  3550. IF firstValue > lastValue THEN
  3551. length := 0
  3552. ELSE
  3553. length := 1 + lastValue - firstValue;
  3554. IF length MOD stepValue = 0 THEN
  3555. length := length DIV stepValue
  3556. ELSE
  3557. length := length DIV stepValue + 1
  3558. END
  3559. END;
  3560. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3561. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3562. ASSERT(targetArray.form = SyntaxTree.Static)
  3563. END
  3564. END
  3565. END
  3566. *)
  3567. END;
  3568. SetIndexBaseType(indexDesignator, targetArray)
  3569. END;
  3570. indexDesignator.parameters.AddExpression(indexListItem)
  3571. ELSE
  3572. Error(position, Diagnostics.Invalid,"invalid index list item");
  3573. END;
  3574. END AppendMathIndex;
  3575. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3576. VAR parameters: SyntaxTree.ExpressionList;
  3577. BEGIN
  3578. parameters := index.parameters;
  3579. IF (expression.type = NIL) THEN
  3580. Error(position,Diagnostics.Invalid,"invalid index");
  3581. ELSIF IsIntegerType(expression.type.resolved) THEN
  3582. IF over IS SyntaxTree.ArrayType THEN
  3583. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3584. ELSIF over IS SyntaxTree.StringType THEN
  3585. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3586. END;
  3587. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3588. parameters.AddExpression(expression);
  3589. ELSE
  3590. Error(position,Diagnostics.Invalid,"invalid index");
  3591. END;
  3592. END AppendIndex;
  3593. (** convert an expression to math array type
  3594. if expression is of math array type: return expression itself
  3595. if expression is of array-structured object type: return an index operator call on it
  3596. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3597. otherwise: return invalid expression
  3598. **)
  3599. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3600. VAR
  3601. result: SyntaxTree.Expression;
  3602. mathArrayType: SyntaxTree.MathArrayType;
  3603. BEGIN
  3604. IF expression.type = NIL THEN
  3605. result := SyntaxTree.invalidExpression
  3606. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3607. (* expression of math array type *)
  3608. result := expression
  3609. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3610. (* expression of array-structured object type *)
  3611. mathArrayType := MathArrayStructureOfType(expression.type);
  3612. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3613. ELSE
  3614. result := SyntaxTree.invalidExpression
  3615. END;
  3616. RETURN result
  3617. END ConvertToMathArray;
  3618. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3619. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3620. VAR
  3621. result: SyntaxTree.ExpressionList;
  3622. i: LONGINT;
  3623. BEGIN
  3624. result := SyntaxTree.NewExpressionList();
  3625. FOR i := 1 TO itemCount DO
  3626. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3627. END;
  3628. RETURN result
  3629. END ListOfOpenRanges;
  3630. (** create a procedure call designator for an index operator call on an array-structured object type
  3631. - use given index list as actual parameters
  3632. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3633. **)
  3634. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3635. VAR
  3636. operator: SyntaxTree.Operator;
  3637. expression: SyntaxTree.Expression;
  3638. actualParameters, tempList: SyntaxTree.ExpressionList;
  3639. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3640. result, tempDesignator: SyntaxTree.Designator;
  3641. recordType: SyntaxTree.RecordType;
  3642. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3643. i, hashValue, indexListSize, indexListKind: LONGINT;
  3644. castReturnType: SyntaxTree.MathArrayType;
  3645. BEGIN
  3646. ASSERT(IsArrayStructuredObjectType(left.type));
  3647. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3648. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3649. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3650. indexListSize := indexList.Length();
  3651. indexListKind := 0;
  3652. containsNonRange := FALSE;
  3653. FOR i := 0 TO indexList.Length() - 1 DO
  3654. indexListKind := indexListKind * 2;
  3655. expression := indexList.GetExpression(i);
  3656. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3657. INC(indexListKind)
  3658. ELSE
  3659. containsNonRange := TRUE
  3660. END
  3661. END;
  3662. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3663. (* select applicable index operator
  3664. - try to look up optimal index operator
  3665. - if not present, use operator on ranges
  3666. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3667. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3668. *)
  3669. usesGeneralOperator := FALSE;
  3670. IF rhs # NIL THEN
  3671. (* write operator *)
  3672. IF hashValue = -1 THEN
  3673. operator := NIL
  3674. ELSE
  3675. operator := recordType.arrayAccessOperators.write[hashValue];
  3676. END;
  3677. IF operator = NIL THEN
  3678. usesPureRangeOperator := TRUE;
  3679. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3680. operator := recordType.arrayAccessOperators.generalWrite;
  3681. usesGeneralOperator := TRUE
  3682. ELSE
  3683. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3684. operator := recordType.arrayAccessOperators.write[hashValue];
  3685. END
  3686. END
  3687. ELSE
  3688. (* read operator *)
  3689. IF hashValue = -1 THEN
  3690. operator := NIL
  3691. ELSE
  3692. operator := recordType.arrayAccessOperators.read[hashValue];
  3693. END;
  3694. IF operator = NIL THEN
  3695. usesPureRangeOperator := TRUE;
  3696. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3697. operator := recordType.arrayAccessOperators.generalRead;
  3698. usesGeneralOperator := TRUE
  3699. ELSE
  3700. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3701. operator := recordType.arrayAccessOperators.read[hashValue];
  3702. END
  3703. END
  3704. END;
  3705. IF operator = NIL THEN
  3706. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3707. result := SyntaxTree.invalidDesignator;
  3708. ELSE
  3709. (* determine if reshaping is needed *)
  3710. needsReshaping := containsNonRange & usesPureRangeOperator;
  3711. (* import OCArrayBase if reshaping is needed *)
  3712. IF needsReshaping & ~arrayBaseImported THEN
  3713. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3714. arrayBaseImported := TRUE
  3715. END;
  3716. (* add the index list item to the list of actual parameters
  3717. - for general operators: add a single inline array containing the index list items as parameter
  3718. - otherwise: add all index list items as individual parameters
  3719. *)
  3720. actualParameters := SyntaxTree.NewExpressionList();
  3721. IF usesGeneralOperator THEN
  3722. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3723. END;
  3724. FOR i := 0 TO indexListSize - 1 DO
  3725. expression := indexList.GetExpression(i);
  3726. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3727. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3728. tempList := SyntaxTree.NewExpressionList();
  3729. tempList.AddExpression(expression);
  3730. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3731. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3732. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3733. END;
  3734. IF usesGeneralOperator THEN
  3735. tempMathArrayExpression.elements.AddExpression(expression);
  3736. ELSE
  3737. actualParameters.AddExpression(expression)
  3738. END
  3739. END;
  3740. IF usesGeneralOperator THEN
  3741. actualParameters.AddExpression(tempMathArrayExpression)
  3742. END;
  3743. IF rhs # NIL THEN
  3744. (* add actual parameter for RHS *)
  3745. IF needsReshaping THEN
  3746. (* reshape using OCArrayBase.ExpandDimensions *)
  3747. tempList := SyntaxTree.NewExpressionList();
  3748. (* source array *)
  3749. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3750. tempList.AddExpression(rhs);
  3751. ELSE
  3752. (* convert scalar to one-dimensional array *)
  3753. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3754. tempMathArrayExpression.elements.AddExpression(rhs);
  3755. tempList.AddExpression(tempMathArrayExpression)
  3756. END;
  3757. (* list of kept dimensions *)
  3758. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3759. FOR i := 0 TO indexListSize - 1 DO
  3760. expression := indexList.GetExpression(i);
  3761. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3762. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3763. ELSE
  3764. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3765. END
  3766. END;
  3767. tempList.AddExpression(tempMathArrayExpression);
  3768. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3769. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3770. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3771. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3772. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3773. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3774. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3775. expression.SetType(castReturnType);
  3776. ELSE
  3777. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3778. END;
  3779. actualParameters.AddExpression(expression)
  3780. ELSE
  3781. actualParameters.AddExpression(rhs)
  3782. END
  3783. END;
  3784. (* add dereference operator and create procedure call designator *)
  3785. ASSERT(left IS SyntaxTree.Designator);
  3786. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3787. ASSERT(expression IS SyntaxTree.Designator);
  3788. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3789. IF (rhs = NIL) & needsReshaping THEN
  3790. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3791. tempList := SyntaxTree.NewExpressionList();
  3792. FOR i := 0 TO indexList.Length() - 1 DO
  3793. expression := indexList.GetExpression(i);
  3794. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3795. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3796. ELSE
  3797. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3798. END
  3799. END;
  3800. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3801. END;
  3802. IF rhs = NIL THEN
  3803. (* special rule: index read operator calls are considered to be assignable *)
  3804. result.SetAssignable(TRUE)
  3805. END;
  3806. (* put information about this index operator call into the resulting designator *)
  3807. result.SetRelatedAsot(left);
  3808. result.SetRelatedIndexList(indexList)
  3809. END;
  3810. RETURN result
  3811. END NewIndexOperatorCall;
  3812. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3813. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3814. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3815. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3816. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3817. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3818. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3819. CONST trace = FALSE;
  3820. BEGIN
  3821. IF trace THEN
  3822. FOR i := 0 TO actualParameters.Length()-1 DO
  3823. Printout.Info("par", actualParameters.GetExpression(i));
  3824. END;
  3825. END;
  3826. operator := scope.firstOperator;
  3827. WHILE(operator # NIL) DO
  3828. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3829. procedureType := operator.type(SyntaxTree.ProcedureType);
  3830. distance := Distance(system, procedureType,actualParameters);
  3831. IF trace THEN Printout.Info("check op ",operator) END;
  3832. IF distance < bestDistance THEN
  3833. IF trace THEN Printout.Info("taken op",operator) END;
  3834. bestDistance := distance;
  3835. bestOperator := operator;
  3836. END;
  3837. END;
  3838. operator := operator.nextOperator;
  3839. END;
  3840. END FindInScope;
  3841. BEGIN
  3842. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3843. identifier := SyntaxTree.NewIdentifier("[]");
  3844. WHILE (recordType # NIL) DO
  3845. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3846. recordType := recordType.GetBaseRecord();
  3847. END;
  3848. RETURN bestOperator
  3849. END FindOperator;
  3850. BEGIN
  3851. type := left.type.resolved;
  3852. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3853. actualParameters := SyntaxTree.NewExpressionList();
  3854. FOR i := 0 TO parameters.Length()-1 DO
  3855. expression := ResolveExpression(parameters.GetExpression(i));
  3856. actualParameters.AddExpression(expression);
  3857. END;
  3858. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3859. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3860. IF op # NIL THEN
  3861. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3862. ASSERT(expression IS SyntaxTree.Designator);
  3863. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3864. result.SetRelatedAsot(left);
  3865. result.SetRelatedIndexList(parameters);
  3866. (* check if write operator exists, for var parameters *)
  3867. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3868. actualParameters := SyntaxTree.NewExpressionList();
  3869. FOR i := 0 TO parameters.Length()-1 DO
  3870. expression := ResolveExpression(parameters.GetExpression(i));
  3871. actualParameters.AddExpression(expression);
  3872. END;
  3873. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3874. actualParameters.AddExpression(rhs);
  3875. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3876. IF op = NIL THEN rhs := NIL END;
  3877. END;
  3878. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3879. ELSE
  3880. Error(position,Diagnostics.Invalid,"undefined operator");
  3881. result := SyntaxTree.invalidDesignator
  3882. END;
  3883. RETURN result;
  3884. END NewObjectOperatorCall;
  3885. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3886. 1. convert bracket designator chains into a single one that contains separators
  3887. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3888. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3889. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3890. - if an array or math array is indexed over, create index designator
  3891. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3892. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3893. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3894. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3895. - if an array-structured object type is indexed over, create procedure call designator
  3896. e.g.: a[x, y] -> a^."[]"(x, y)
  3897. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3898. - a[i, *] = a[i][*]
  3899. - a[*, i] # a[*][i]
  3900. Because:
  3901. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3902. - 'i-th column' = a[*, i]
  3903. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3904. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3905. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3906. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3907. **)
  3908. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3909. VAR
  3910. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3911. indexDesignator: SyntaxTree.IndexDesignator;
  3912. designator: SyntaxTree.Designator;
  3913. type: SyntaxTree.Type;
  3914. recordType: SyntaxTree.RecordType;
  3915. expression, rhs: SyntaxTree.Expression;
  3916. indexList: SyntaxTree.ExpressionList;
  3917. i: LONGINT;
  3918. hasError, done: BOOLEAN;
  3919. PROCEDURE FinalizeIndexDesignator;
  3920. BEGIN
  3921. IF indexDesignator # NIL THEN
  3922. (* the end of a tensor has been reached: *)
  3923. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3924. SetIndexBaseType(indexDesignator, type);
  3925. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3926. designator := indexDesignator;
  3927. type := designator.type.resolved;
  3928. indexDesignator := NIL;
  3929. ASSERT(SyntaxTree.Resolved IN type.state)
  3930. END
  3931. END FinalizeIndexDesignator;
  3932. BEGIN
  3933. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3934. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3935. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3936. (* copy all index list entries including a separator to the left bracket designator *)
  3937. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3938. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3939. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3940. END;
  3941. (* propagate the related RHS *)
  3942. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3943. (* only resolve left bracket designator and use as final result *)
  3944. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3945. ELSE
  3946. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3947. designator := ResolveDesignator(bracketDesignator.left);
  3948. type := designator.type.resolved;
  3949. indexDesignator := NIL;
  3950. (*!!! clean up *)
  3951. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3952. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3953. RETURN
  3954. END;
  3955. i := 0;
  3956. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3957. expression := bracketDesignator.parameters.GetExpression(i);
  3958. expression := ResolveExpression(expression);
  3959. bracketDesignator.parameters.SetExpression(i, expression);
  3960. IF expression = SyntaxTree.indexListSeparator THEN
  3961. (* finalize an existing index designator if needed *)
  3962. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3963. INC(i)
  3964. ELSE
  3965. (* do auto-dereferencing if needed *)
  3966. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3967. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3968. & (i=0)*)
  3969. THEN
  3970. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3971. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3972. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  3973. designator := SyntaxTree.invalidDesignator;
  3974. type := SyntaxTree.invalidType
  3975. ELSE
  3976. FinalizeIndexDesignator;
  3977. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3978. type := designator.type.resolved
  3979. END
  3980. END;
  3981. (* create a new index designator, if needed *)
  3982. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3983. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3984. indexDesignator.SetAssignable(designator.assignable);
  3985. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3986. (* designator := indexDesignator *)
  3987. END;
  3988. IF type = SyntaxTree.invalidType THEN
  3989. (* error already handled *)
  3990. INC(i)
  3991. ELSIF type IS SyntaxTree.ArrayType THEN
  3992. (* indexing over an array *)
  3993. ASSERT(indexDesignator # NIL);
  3994. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3995. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3996. INC(i)
  3997. ELSIF type IS SyntaxTree.StringType THEN
  3998. (* indexing over an array *)
  3999. ASSERT(indexDesignator # NIL);
  4000. AppendIndex(expression.position, indexDesignator, expression, type);
  4001. type := type(SyntaxTree.StringType).baseType.resolved;
  4002. INC(i)
  4003. ELSIF type IS SyntaxTree.MathArrayType THEN
  4004. (* indexing over a math array *)
  4005. ASSERT(indexDesignator # NIL);
  4006. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4007. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4008. INC(i)
  4009. ELSIF IsArrayStructuredObjectType(type) THEN
  4010. (* indexing over ASOTs *)
  4011. FinalizeIndexDesignator;
  4012. ASSERT(type IS SyntaxTree.PointerType);
  4013. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4014. (*
  4015. - collect index list items from bracket designator that belong to ASOT
  4016. - check for errors
  4017. *)
  4018. indexList := SyntaxTree.NewExpressionList();
  4019. hasError := FALSE;
  4020. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4021. (* indexing over tensor ASOT:
  4022. - stop at index list end or separator
  4023. - dimensionality is given by number of index list items
  4024. *)
  4025. done := FALSE;
  4026. WHILE ~done DO
  4027. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4028. done := TRUE;
  4029. ELSE
  4030. expression := bracketDesignator.parameters.GetExpression(i);
  4031. IF expression = SyntaxTree.indexListSeparator THEN
  4032. done := TRUE;
  4033. ELSE
  4034. expression := ResolveExpression(expression);
  4035. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4036. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4037. hasError := TRUE
  4038. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4039. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4040. expression := SyntaxTree.invalidExpression;
  4041. hasError := TRUE
  4042. END;
  4043. indexList.AddExpression(expression)
  4044. END;
  4045. INC(i)
  4046. END
  4047. END
  4048. ELSE
  4049. (* indexing over non-tensor ASOT:
  4050. - ignore separators
  4051. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4052. *)
  4053. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4054. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4055. expression := bracketDesignator.parameters.GetExpression(i);
  4056. ELSE
  4057. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4058. END;
  4059. IF expression # SyntaxTree.indexListSeparator THEN
  4060. expression := ResolveExpression(expression);
  4061. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4062. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4063. expression := SyntaxTree.invalidExpression;
  4064. hasError := TRUE
  4065. END;
  4066. indexList.AddExpression(expression)
  4067. END;
  4068. INC(i)
  4069. END;
  4070. END;
  4071. IF hasError THEN
  4072. designator := SyntaxTree.invalidDesignator;
  4073. type := SyntaxTree.invalidType;
  4074. ELSE
  4075. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4076. and the last entry in the index list belongs to the array-structured object type in question.
  4077. E.g.: for a 2-dimensional array-structured object type:
  4078. - 'lhs := asot[1, 2]' -> read mode
  4079. - 'asot[1, 2] := rhs' -> write mode
  4080. - 'asot[1, 2, 3] := rhs' -> read mode
  4081. *)
  4082. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4083. rhs := bracketDesignator.relatedRhs
  4084. ELSE
  4085. rhs := NIL
  4086. END;
  4087. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4088. type := designator.type
  4089. END
  4090. ELSE
  4091. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4092. designator := SyntaxTree.invalidDesignator;
  4093. type := SyntaxTree.invalidType;
  4094. INC(i)
  4095. END
  4096. END
  4097. END;
  4098. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4099. resolvedExpression := designator
  4100. END
  4101. END VisitBracketDesignator;
  4102. (** check and resolve expression list
  4103. - resolve each expression in an expression list
  4104. - returns true if and only if all statements could have successfully been resolved
  4105. **)
  4106. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4107. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4108. BEGIN
  4109. result := TRUE;
  4110. FOR i := 0 TO expressionList.Length()-1 DO
  4111. expression := ResolveExpression(expressionList.GetExpression(i));
  4112. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4113. expressionList.SetExpression(i,expression);
  4114. END;
  4115. RETURN result
  4116. END ExpressionList;
  4117. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4118. BEGIN
  4119. type := type.resolved;
  4120. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4121. RETURN TRUE
  4122. ELSIF system.CanPassInRegister # NIL THEN
  4123. RETURN system.CanPassInRegister(type);
  4124. ELSE
  4125. RETURN FALSE
  4126. END;
  4127. END CanPassInRegister;
  4128. (** return procedure call designator left(actualParameters)
  4129. - check realtime procedure call in realtime procedure
  4130. - check number of parameters
  4131. - check parameter compatibility
  4132. return invalidDesignator if error
  4133. **)
  4134. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4135. VAR result: SyntaxTree.Designator;
  4136. numberFormalParameters, numberActualParameters: LONGINT;
  4137. formalType: SyntaxTree.ProcedureType;
  4138. formalParameter: SyntaxTree.Parameter;
  4139. actualParameter: SyntaxTree.Expression;
  4140. i: LONGINT;
  4141. BEGIN
  4142. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4143. result := SyntaxTree.invalidDesignator;
  4144. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4145. numberFormalParameters := formalType.numberParameters;
  4146. numberActualParameters := actualParameters.Length();
  4147. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4148. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4149. END;
  4150. IF ~ExpressionList(actualParameters) THEN
  4151. result := SyntaxTree.invalidDesignator
  4152. ELSE
  4153. IF numberActualParameters <= numberFormalParameters THEN
  4154. formalParameter := formalType.firstParameter;
  4155. FOR i := 0 TO numberActualParameters-1 DO
  4156. actualParameter := actualParameters.GetExpression(i);
  4157. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4158. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4159. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4160. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4161. ELSE
  4162. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4163. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4164. END;
  4165. actualParameters.SetExpression(i,actualParameter);
  4166. END;
  4167. formalParameter := formalParameter.nextParameter;
  4168. END;
  4169. WHILE (formalParameter # NIL) DO
  4170. IF formalParameter.defaultValue # NIL THEN
  4171. actualParameters.AddExpression(formalParameter.defaultValue);
  4172. formalParameter := formalParameter.nextParameter
  4173. ELSE
  4174. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4175. formalParameter := NIL;
  4176. END;
  4177. END;
  4178. ELSE
  4179. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4180. END;
  4181. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4182. result.SetAssignable(FALSE);
  4183. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4184. END;
  4185. RETURN result
  4186. END NewProcedureCallDesignator;
  4187. (**
  4188. builtin call designator generated in VisitParameterDesignator
  4189. -> nothing to be resolved
  4190. **)
  4191. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4192. BEGIN
  4193. resolvedExpression := x;
  4194. END VisitTypeGuardDesignator;
  4195. (**
  4196. builtin call designator generated in VisitParameterDesignator
  4197. -> nothing to be resolved
  4198. **)
  4199. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4200. BEGIN
  4201. IF ExpressionList(x.parameters) THEN
  4202. resolvedExpression := x;
  4203. END;
  4204. END VisitBuiltinCallDesignator;
  4205. (**
  4206. procedure call designator generated in VisitParameterDesignator
  4207. -> nothing to be resolved
  4208. **)
  4209. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4210. BEGIN
  4211. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4212. resolvedExpression := x;
  4213. END VisitProcedureCallDesignator;
  4214. (** return true if x is a variable else return false and report error **)
  4215. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4216. VAR result: BOOLEAN;
  4217. BEGIN
  4218. result := TRUE;
  4219. IF x = SyntaxTree.invalidExpression THEN
  4220. result := FALSE;
  4221. ELSIF ~IsVariable(x) THEN
  4222. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4223. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4224. result := FALSE;
  4225. END;
  4226. RETURN result
  4227. END CheckVariable;
  4228. (**
  4229. if expression x is of basic type then return true else report error and return false
  4230. **)
  4231. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4232. VAR result: BOOLEAN;
  4233. BEGIN
  4234. result := FALSE;
  4235. IF x = SyntaxTree.invalidExpression THEN
  4236. ELSIF ~IsBasicType(x.type) THEN
  4237. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4238. result := FALSE
  4239. ELSE result := TRUE
  4240. END;
  4241. RETURN result
  4242. END CheckBasicType;
  4243. (**
  4244. if expression x is of number type then return true else report error and return false
  4245. **)
  4246. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4247. VAR result: BOOLEAN;
  4248. BEGIN
  4249. result := FALSE;
  4250. IF x = SyntaxTree.invalidExpression THEN
  4251. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4252. Error(x.position,Diagnostics.Invalid,"is non number type");
  4253. ELSE result := TRUE
  4254. END;
  4255. RETURN result
  4256. END CheckNumberType;
  4257. (**
  4258. if expression x is of number or size type but not complex then return true else report error and return false
  4259. **)
  4260. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4261. VAR result: BOOLEAN;
  4262. BEGIN
  4263. result := FALSE;
  4264. IF x = SyntaxTree.invalidExpression THEN
  4265. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4266. Error(x.position,Diagnostics.Invalid,"is complex type");
  4267. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4268. Error(x.position,Diagnostics.Invalid,"is non number type");
  4269. ELSE result := TRUE
  4270. END;
  4271. RETURN result
  4272. END CheckNonComplexNumberSizeType;
  4273. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4274. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4275. BEGIN
  4276. result := FALSE; type := x.type.resolved;
  4277. IF x = SyntaxTree.invalidExpression THEN
  4278. 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
  4279. TRACE(type.sizeInBits);
  4280. TRACE(system.addressType.sizeInBits);
  4281. Error(x.position,Diagnostics.Invalid,"is no address type");
  4282. ELSE result := TRUE
  4283. END;
  4284. RETURN result
  4285. END CheckAddressType;
  4286. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4287. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4288. BEGIN
  4289. result := FALSE; type := x.type.resolved;
  4290. IF x = SyntaxTree.invalidExpression THEN
  4291. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4292. Error(x.position,Diagnostics.Invalid,"is no size type");
  4293. ELSE result := TRUE
  4294. END;
  4295. RETURN result
  4296. END CheckSizeType;
  4297. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4298. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4299. BEGIN
  4300. result := FALSE; type := x.type.resolved;
  4301. IF x = SyntaxTree.invalidExpression THEN
  4302. ELSIF ~(type IS SyntaxTree.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
  4303. Error(x.position,Diagnostics.Invalid,"is no object type");
  4304. ELSE result := TRUE
  4305. END;
  4306. RETURN result
  4307. END CheckObjectType;
  4308. (**
  4309. if expression x is of integer type then return true else report error and return false
  4310. **)
  4311. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4312. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4313. BEGIN
  4314. result := FALSE; type := x.type.resolved;
  4315. IF x = SyntaxTree.invalidExpression THEN
  4316. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4317. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4318. ELSE result := TRUE
  4319. END;
  4320. RETURN result
  4321. END CheckIntegerType;
  4322. (**
  4323. if expression x is of character type then return true else report error and return false
  4324. **)
  4325. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4326. VAR result: BOOLEAN;
  4327. BEGIN
  4328. result := FALSE;
  4329. IF x = SyntaxTree.invalidExpression THEN
  4330. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4331. Error(x.position,Diagnostics.Invalid,"is no character type");
  4332. ELSE result := TRUE
  4333. END;
  4334. RETURN result
  4335. END CheckCharacterType;
  4336. (**
  4337. if expression x is of real type then return true else report error and return false
  4338. **)
  4339. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4340. VAR result: BOOLEAN;
  4341. BEGIN
  4342. result := FALSE;
  4343. IF x = SyntaxTree.invalidExpression THEN
  4344. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4345. Error(x.position,Diagnostics.Invalid,"is no float type");
  4346. ELSE result := TRUE
  4347. END;
  4348. RETURN result
  4349. END CheckRealType;
  4350. (**
  4351. if expression x is of range type then return true else report error and return false
  4352. **)
  4353. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4354. VAR result: BOOLEAN;
  4355. BEGIN
  4356. result := FALSE;
  4357. IF x = SyntaxTree.invalidExpression THEN
  4358. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4359. Error(x.position,Diagnostics.Invalid,"is no range type");
  4360. ELSE result := TRUE
  4361. END;
  4362. RETURN result
  4363. END CheckRangeType;
  4364. (**
  4365. if expression x is of boolean type then return true else report error and return false
  4366. **)
  4367. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN;
  4369. BEGIN
  4370. result := FALSE;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4373. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4374. ELSE result := TRUE
  4375. END;
  4376. RETURN result
  4377. END CheckBooleanType;
  4378. (**
  4379. if expression x is of set type then return true else report error and return false
  4380. **)
  4381. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4382. VAR result: BOOLEAN;
  4383. BEGIN
  4384. result := FALSE;
  4385. IF x = SyntaxTree.invalidExpression THEN
  4386. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4387. Error(x.position,Diagnostics.Invalid,"is no set type");
  4388. ELSE result := TRUE
  4389. END;
  4390. RETURN result
  4391. END CheckSetType;
  4392. (**
  4393. if expression x is of string or array of character type then return true else report error and return false
  4394. **)
  4395. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN;
  4397. BEGIN
  4398. result := FALSE;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. ELSIF ~IsStringType(x.type.resolved) THEN
  4401. Error(x.position,Diagnostics.Invalid,"is no string type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckStringType;
  4406. (**
  4407. if expression x is a type declaration type return true else report error and return false
  4408. **)
  4409. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN;
  4411. BEGIN
  4412. result := FALSE;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4415. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckTypeDeclarationType;
  4420. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4421. VAR result: BOOLEAN;
  4422. BEGIN
  4423. result := FALSE;
  4424. IF x = SyntaxTree.invalidExpression THEN
  4425. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4426. result := TRUE;
  4427. value := x.resolved(SyntaxTree.IntegerValue).value;
  4428. ELSE
  4429. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4430. END;
  4431. RETURN result;
  4432. END CheckIntegerValue;
  4433. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4434. BEGIN
  4435. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4436. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4437. ELSE
  4438. RETURN FALSE
  4439. END;
  4440. END IsUnsignedValue;
  4441. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4442. BEGIN
  4443. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4444. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4445. ELSE
  4446. RETURN FALSE
  4447. END
  4448. END IsAddressValue;
  4449. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4450. BEGIN
  4451. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4452. END IsAddressExpression;
  4453. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4454. BEGIN
  4455. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4456. END IsSizeExpression;
  4457. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4458. VAR result: BOOLEAN;
  4459. BEGIN
  4460. result := FALSE;
  4461. IF x = SyntaxTree.invalidExpression THEN
  4462. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4463. result := TRUE;
  4464. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4465. ELSE
  4466. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4467. END;
  4468. RETURN result;
  4469. END CheckEnumerationValue;
  4470. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4471. VAR result: BOOLEAN;
  4472. BEGIN
  4473. result := FALSE;
  4474. IF x = SyntaxTree.invalidExpression THEN
  4475. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4476. result := TRUE;
  4477. value := x.resolved(SyntaxTree.CharacterValue).value;
  4478. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4479. result := TRUE;
  4480. value := x.resolved(SyntaxTree.StringValue).value[0];
  4481. ELSE
  4482. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4483. END;
  4484. RETURN result;
  4485. END CheckCharacterValue;
  4486. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4487. VAR result: BOOLEAN;
  4488. BEGIN
  4489. result := FALSE;
  4490. IF x = SyntaxTree.invalidExpression THEN
  4491. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4492. value := x.resolved(SyntaxTree.IntegerValue).value;
  4493. IF (value > 0) OR includeZero & (value = 0) THEN
  4494. result := TRUE;
  4495. ELSE
  4496. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4497. END
  4498. ELSE
  4499. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4500. END;
  4501. RETURN result;
  4502. END CheckPositiveIntegerValue;
  4503. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4504. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSE
  4509. type := x.type.resolved;
  4510. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4511. portType := type(SyntaxTree.PortType);
  4512. result := TRUE
  4513. ELSE
  4514. Error(x.position,Diagnostics.Invalid,"no port type");
  4515. END;
  4516. END;
  4517. RETURN result
  4518. END CheckPortType;
  4519. (* move to builtin procedure call statement ?
  4520. remove builtin procedure call designator ?
  4521. *)
  4522. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator): SyntaxTree.Expression;
  4523. VAR
  4524. numberActualParameters,numberFormalParameters: LONGINT;
  4525. formalParameter: SyntaxTree.Parameter;
  4526. actualParameter: SyntaxTree.Expression;
  4527. procedureType: SyntaxTree.ProcedureType;
  4528. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4529. inPort, outPort: SyntaxTree.PortType;
  4530. constructor: SyntaxTree.Procedure;
  4531. type0,type1,type2: SyntaxTree.Type;
  4532. type,base,parameterType: SyntaxTree.Type;
  4533. arrayType: SyntaxTree.ArrayType;
  4534. i,i0,i1: LONGINT;
  4535. r,r0,r1,im: LONGREAL;
  4536. c: CHAR;
  4537. id: LONGINT;
  4538. b: BOOLEAN;
  4539. mathArrayType: SyntaxTree.MathArrayType;
  4540. customBuiltin: SyntaxTree.CustomBuiltin;
  4541. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4542. VAR resultB: BOOLEAN;
  4543. BEGIN
  4544. IF numberActualParameters < from THEN
  4545. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4546. result := SyntaxTree.invalidExpression;
  4547. resultB := FALSE;
  4548. ELSIF numberActualParameters > to THEN
  4549. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4550. result := SyntaxTree.invalidExpression;
  4551. resultB := FALSE;
  4552. ELSE
  4553. resultB := TRUE;
  4554. END;
  4555. RETURN resultB
  4556. END CheckArity;
  4557. BEGIN
  4558. type := NIL; result := NIL;
  4559. type0 := NIL; type1 := NIL; type2 := NIL;
  4560. numberActualParameters := actualParameters.Length();
  4561. IF numberActualParameters>0 THEN
  4562. parameter0 := actualParameters.GetExpression(0);
  4563. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4564. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4565. result := SyntaxTree.invalidExpression
  4566. END
  4567. END;
  4568. IF numberActualParameters >1 THEN
  4569. parameter1 := actualParameters.GetExpression(1);
  4570. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4571. ELSE
  4572. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4573. result := SyntaxTree.invalidExpression
  4574. END
  4575. END;
  4576. IF numberActualParameters >2 THEN
  4577. parameter2 := actualParameters.GetExpression(2);
  4578. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4579. ELSE
  4580. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4581. result := SyntaxTree.invalidExpression
  4582. END
  4583. END;
  4584. id := builtin.id;
  4585. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4586. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4587. END;
  4588. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4589. ELSIF result # NIL THEN type := result.type (* operator *)
  4590. ELSE
  4591. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4592. result(SyntaxTree.Designator).SetLeft(left);
  4593. (* ---- ASSERT ----- *)
  4594. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4595. IF CheckBooleanType(parameter0) THEN
  4596. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4597. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4598. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4599. *)
  4600. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4601. Error(position,Diagnostics.Invalid,"assert failed");
  4602. END;
  4603. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4604. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4605. rules imposed by the architecture / current runtime
  4606. *)
  4607. END;
  4608. END;
  4609. (* ---- COPY ----- *)
  4610. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4611. IF~IsStringType(type0) THEN
  4612. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4613. END;
  4614. IF ~IsStringType(type1) THEN
  4615. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4616. ELSIF CheckVariable(parameter1) THEN
  4617. IF (type0 IS SyntaxTree.StringType) THEN
  4618. arrayType := type1(SyntaxTree.ArrayType);
  4619. IF arrayType.form = SyntaxTree.Static THEN
  4620. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4621. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4622. END;
  4623. END;
  4624. END;
  4625. END;
  4626. (* ---- INC, DEC----- *)
  4627. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4628. IF numberActualParameters = 1 THEN
  4629. parameter1 :=Global.NewIntegerValue(system,position,1);
  4630. actualParameters.AddExpression(parameter1);
  4631. END;
  4632. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4633. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4634. Error(position,Diagnostics.Invalid,"incompatible increment");
  4635. ELSE
  4636. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4637. actualParameters.SetExpression(1,parameter1);
  4638. END;
  4639. END;
  4640. (* ---- EXCL, INCL----- *)
  4641. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4642. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4643. IF IsIntegerValue(parameter1,i0) THEN
  4644. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4645. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4646. END;
  4647. END;
  4648. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4649. actualParameters.SetExpression(1,parameter1);
  4650. END;
  4651. (* ---- HALT, SYSTEM.HALT ----- *)
  4652. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4653. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4654. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4655. rules imposed by the architecture / current runtime
  4656. *)
  4657. END;
  4658. (* ---- WAIT ----- *)
  4659. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4660. IF CheckObjectType(parameter0) THEN
  4661. END;
  4662. (* ---- NEW ----- *)
  4663. ELSIF (id = Global.New) & CheckArity(1,Infinity) THEN
  4664. IF currentIsRealtime THEN
  4665. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4666. END;
  4667. (* check constructor *)
  4668. IF CheckVariable(parameter0) THEN
  4669. IF type0 IS SyntaxTree.PointerType THEN
  4670. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4671. END;
  4672. IF type0 IS SyntaxTree.ArrayType THEN
  4673. arrayType := type0(SyntaxTree.ArrayType);
  4674. IF arrayType.form = SyntaxTree.Static THEN
  4675. i := 1
  4676. ELSIF arrayType.form = SyntaxTree.Open THEN
  4677. i := Dimension(arrayType,{SyntaxTree.Open})+1;
  4678. ELSE HALT(100)
  4679. END;
  4680. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4681. i := 1;
  4682. REPEAT
  4683. actualParameter := actualParameters.GetExpression(i);
  4684. IF CheckSizeType(actualParameter) THEN
  4685. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4686. actualParameters.SetExpression(i,actualParameter);
  4687. END;
  4688. INC(i);
  4689. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4690. END;
  4691. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4692. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4693. IF constructor = NIL THEN
  4694. IF CheckArity(1,1) THEN END;
  4695. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4696. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4697. ELSE
  4698. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4699. numberFormalParameters := procedureType.numberParameters;
  4700. DEC(numberActualParameters);
  4701. IF numberActualParameters <= numberFormalParameters THEN
  4702. formalParameter := procedureType.firstParameter;
  4703. FOR i := 1 TO numberActualParameters DO
  4704. actualParameter := actualParameters.GetExpression(i);
  4705. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4706. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4707. ELSE
  4708. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4709. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4710. END;
  4711. actualParameters.SetExpression(i,actualParameter);
  4712. END;
  4713. formalParameter := formalParameter.nextParameter;
  4714. END;
  4715. WHILE (formalParameter # NIL) DO
  4716. IF formalParameter.defaultValue # NIL THEN
  4717. actualParameters.AddExpression(formalParameter.defaultValue);
  4718. formalParameter := formalParameter.nextParameter
  4719. ELSE
  4720. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4721. formalParameter := NIL;
  4722. END;
  4723. END;
  4724. ELSE
  4725. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4726. END;
  4727. END;
  4728. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4729. mathArrayType := type0(SyntaxTree.MathArrayType);
  4730. IF mathArrayType.form = SyntaxTree.Static THEN
  4731. Error(position,Diagnostics.Invalid,"new on static array");
  4732. ELSE
  4733. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4734. i0 := 2; i1 := Infinity;
  4735. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4736. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+1;
  4737. i1 := i0;
  4738. ELSE HALT(100);
  4739. END;
  4740. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4741. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4742. base := ArrayBase(type0,MAX(LONGINT));
  4743. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4744. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4745. IF ~CompatibleTo(system,type0,parameterType) THEN
  4746. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4747. result := SyntaxTree.invalidExpression;
  4748. ELSE
  4749. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4750. END;
  4751. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4752. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4753. IF ~CompatibleTo(system,type1,parameterType) THEN
  4754. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4755. result := SyntaxTree.invalidExpression;
  4756. ELSE
  4757. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4758. END;
  4759. ELSE
  4760. IF CheckArity(i0,i1) & (numberActualParameters >1) THEN
  4761. i := 1;
  4762. REPEAT
  4763. actualParameter := actualParameters.GetExpression(i);
  4764. IF CheckSizeType(actualParameter) THEN
  4765. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4766. actualParameters.SetExpression(i,actualParameter);
  4767. END;
  4768. INC(i);
  4769. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4770. END;
  4771. END;
  4772. END;
  4773. ELSIF type0 IS SyntaxTree.CellType THEN
  4774. IF ~(currentIsCellNet) THEN
  4775. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4776. ELSE
  4777. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4778. IF (constructor = NIL) & CheckArity(1,1) THEN
  4779. (* ok *)
  4780. ELSE
  4781. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4782. numberFormalParameters := procedureType.numberParameters;
  4783. DEC(numberActualParameters);
  4784. IF numberActualParameters <= numberFormalParameters THEN
  4785. formalParameter := procedureType.firstParameter;
  4786. FOR i := 1 TO numberActualParameters DO
  4787. actualParameter := actualParameters.GetExpression(i);
  4788. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4789. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4790. ELSE
  4791. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4792. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4793. END;
  4794. actualParameters.SetExpression(i,actualParameter);
  4795. END;
  4796. formalParameter := formalParameter.nextParameter;
  4797. END;
  4798. WHILE (formalParameter # NIL) DO
  4799. IF formalParameter.defaultValue # NIL THEN
  4800. actualParameters.AddExpression(formalParameter.defaultValue);
  4801. formalParameter := formalParameter.nextParameter
  4802. ELSE
  4803. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4804. formalParameter := NIL;
  4805. END;
  4806. END;
  4807. ELSE
  4808. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4809. END;
  4810. END;
  4811. END;
  4812. activeCellsStatement := TRUE;
  4813. ELSE
  4814. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4815. END;
  4816. END;
  4817. (* ---- DISPOSE ----- *)
  4818. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4819. IF ~IsPointerType(parameter0.type) THEN
  4820. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4821. ELSIF ~IsDisposable(parameter0.type) THEN
  4822. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4823. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4824. END
  4825. (* ---- GETPROCEDURE ----- *)
  4826. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4827. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4828. IF CheckVariable(parameter2) THEN
  4829. IF ~GetProcedureAllowed(parameter2.type) THEN
  4830. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4831. END;
  4832. END;
  4833. END;
  4834. (* ---- ABS ----- *)
  4835. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4836. (* note: ABS on complex numbers is done using overloading *)
  4837. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4838. type := type0;
  4839. IF IsIntegerValue(parameter0,i0) THEN
  4840. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4841. type := Global.GetIntegerType(system,ABS(i0));
  4842. ELSIF IsRealValue(parameter0,r) THEN
  4843. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4844. END;
  4845. ELSE
  4846. type := SyntaxTree.invalidType;
  4847. END;
  4848. (* ---- ASH, ASR ----- *)
  4849. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4850. type := type0;
  4851. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4852. (*
  4853. ConvertOperands(parameter0,parameter1); (* same type *)
  4854. *)
  4855. type := parameter0.type;
  4856. IF IsIntegerValue(parameter0,i0) THEN
  4857. IF IsIntegerValue(parameter1,i1) THEN
  4858. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4859. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4860. result := ResolveExpression(result);
  4861. type := Global.GetIntegerType(system,i0);
  4862. END;
  4863. END;
  4864. IF type.resolved.sizeInBits < 32 THEN
  4865. type := system.longintType;
  4866. END;
  4867. (*!compatibility with release, remove when resolved
  4868. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4869. *)
  4870. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4871. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4872. actualParameters.SetExpression(0,parameter0);
  4873. actualParameters.SetExpression(1,parameter1);
  4874. END;
  4875. (* ---- CAP ----- *)
  4876. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4877. type := system.characterType;
  4878. IF CheckCharacterType (parameter0) THEN
  4879. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4880. actualParameters.SetExpression(0,parameter0);
  4881. IF IsCharacterValue(parameter0,c) THEN
  4882. IF (c <= "z") & (c >= "a") THEN
  4883. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4884. ELSE
  4885. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4886. END;
  4887. END;
  4888. END;
  4889. (* ---- CHR ----- *)
  4890. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4891. IF id = Global.Chr THEN
  4892. type := system.characterType
  4893. ELSE
  4894. type := system.characterType32
  4895. END;
  4896. IF CheckIntegerType(parameter0) THEN
  4897. IF IsIntegerValue(parameter0,i0) THEN
  4898. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4899. result := ResolveExpression(result);
  4900. ELSE
  4901. (*
  4902. result := NewConversion(parameter0.position,parameter0,type);
  4903. *)
  4904. END;
  4905. END
  4906. (* ---- ENTIER ----- *)
  4907. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4908. type := system.longintType;
  4909. IF CheckRealType(parameter0) THEN
  4910. IF IsRealValue(parameter0,r) THEN
  4911. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4912. type := Global.GetIntegerType(system,ENTIER(r));
  4913. END
  4914. END;
  4915. (* ---- ENTIERH ----- *)
  4916. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4917. type := system.hugeintType;
  4918. IF CheckRealType(parameter0) THEN
  4919. IF IsRealValue(parameter0,r) THEN
  4920. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4921. END
  4922. END;
  4923. (* ---- LEN ----- *)
  4924. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4925. type := system.longintType;
  4926. base := type0;
  4927. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4928. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4929. actualParameters.SetExpression(0,parameter0);
  4930. type0 := parameter0.type.resolved;
  4931. base := type0;
  4932. END;
  4933. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4934. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4935. IF i1 < 0 THEN
  4936. Error(position,Diagnostics.Invalid,"invalid dimension");
  4937. base := SyntaxTree.invalidType;
  4938. ELSE
  4939. base := ArrayBase(base,i1);
  4940. IF (base # NIL) & Indexable(base) THEN
  4941. ELSE
  4942. Error(position,Diagnostics.Invalid,"len on no array");
  4943. IF VerboseErrorMessage THEN
  4944. Printout.Info("base",base);
  4945. END;
  4946. base := SyntaxTree.invalidType;
  4947. END;
  4948. END;
  4949. IF numberActualParameters=2 THEN
  4950. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4951. actualParameters.SetExpression(1,parameter1);
  4952. ELSIF base IS SyntaxTree.MathArrayType THEN
  4953. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4954. END;
  4955. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4956. IF base IS SyntaxTree.ArrayType THEN
  4957. arrayType := base(SyntaxTree.ArrayType);
  4958. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  4959. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4960. result := Global.NewIntegerValue(system,position,i);
  4961. type := result.type;(* arrayType.length.type;*)
  4962. ASSERT(type # NIL);
  4963. END;
  4964. ELSIF base IS SyntaxTree.MathArrayType THEN
  4965. mathArrayType := base(SyntaxTree.MathArrayType);
  4966. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  4967. result := Global.NewIntegerValue(system,position,i);
  4968. type := result.type;
  4969. (*
  4970. type := mathArrayType.length.type;
  4971. *)
  4972. ASSERT(type # NIL);
  4973. END;
  4974. END;
  4975. END;
  4976. ELSE
  4977. type := system.longintType;
  4978. END;
  4979. (* ---- FIRST ---- *)
  4980. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4981. type := system.longintType;
  4982. IF CheckRangeType(parameter0) THEN END;
  4983. result.SetAssignable(parameter0.assignable)
  4984. (* ---- LAST ---- *)
  4985. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4986. type := system.longintType;
  4987. IF CheckRangeType(parameter0) THEN END;
  4988. result.SetAssignable(parameter0.assignable)
  4989. (* ---- STEP ---- *)
  4990. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4991. type := system.longintType;
  4992. IF CheckRangeType(parameter0) THEN END;
  4993. result.SetAssignable(parameter0.assignable)
  4994. (* ---- RE ---- *)
  4995. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4996. IF CheckNumberType(parameter0) THEN
  4997. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4998. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4999. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5000. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5001. type := parameter0.type
  5002. ELSE
  5003. type := system.realType
  5004. END
  5005. END;
  5006. result.SetAssignable(parameter0.assignable)
  5007. (* ---- IM ---- *)
  5008. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5009. IF CheckNumberType(parameter0) THEN
  5010. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5011. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5012. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5013. ELSE
  5014. type := system.realType;
  5015. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5016. END
  5017. END;
  5018. result.SetAssignable(parameter0.assignable)
  5019. (* ---- MAX ----- *)
  5020. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5021. IF numberActualParameters = 1 THEN
  5022. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5023. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5024. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5025. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5026. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5027. *)
  5028. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5029. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5030. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5031. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5032. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5033. END;
  5034. ELSE
  5035. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5036. END
  5037. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5038. ConvertOperands(parameter0,parameter1);
  5039. actualParameters.SetExpression(0,parameter0);
  5040. actualParameters.SetExpression(1,parameter1);
  5041. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5042. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5043. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5044. END;
  5045. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5046. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5047. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5048. END;
  5049. END;
  5050. type := parameter0.type;
  5051. ELSE type := SyntaxTree.invalidType;
  5052. END;
  5053. (* ---- MIN ----- *)
  5054. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5055. IF numberActualParameters = 1 THEN
  5056. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5057. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5058. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5059. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5060. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5061. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5062. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5063. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5064. END;
  5065. ELSE
  5066. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5067. END
  5068. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5069. ConvertOperands(parameter0,parameter1);
  5070. actualParameters.SetExpression(0,parameter0);
  5071. actualParameters.SetExpression(1,parameter1);
  5072. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5073. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5074. ELSE result.SetResolved(parameter1.resolved)
  5075. END;
  5076. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5077. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5078. ELSE result.SetResolved(parameter1.resolved)
  5079. END;
  5080. END;
  5081. type := parameter0.type;
  5082. ELSE type := SyntaxTree.invalidType;
  5083. END;
  5084. (* ---- ODD ----- *)
  5085. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5086. type := system.booleanType;
  5087. IF CheckIntegerType(parameter0) THEN
  5088. IF IsIntegerValue(parameter0,i0) THEN
  5089. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5090. type := system.booleanType;
  5091. END;
  5092. END;
  5093. (* ---- ORD ----- *)
  5094. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5095. IF id = Global.Ord THEN
  5096. type := system.integerType;
  5097. ELSE
  5098. type := system.longintType;
  5099. END;
  5100. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5101. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5102. actualParameters.SetExpression(0,parameter0);
  5103. (* IF CheckCharacterType(parameter0) THEN*)
  5104. IF IsCharacterValue(parameter0,c)THEN
  5105. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5106. type := Global.GetSignedIntegerType(system,ORD(c));
  5107. END;
  5108. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5109. END;
  5110. (* ---- SHORT ----- *)
  5111. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5112. type := type0;
  5113. IF IsSignedIntegerType(type) THEN
  5114. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5115. ELSIF type = system.integerType THEN type := system.shortintType
  5116. ELSIF type = system.longintType THEN type := system.integerType
  5117. ELSIF type = system.hugeintType THEN type:= system.longintType
  5118. ELSE
  5119. CASE type.sizeInBits OF
  5120. 16: type := Global.Integer8
  5121. |32: type := Global.Integer16
  5122. |64: type := Global.Integer32
  5123. END;
  5124. END;
  5125. ELSIF type IS SyntaxTree.FloatType THEN
  5126. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5127. ELSIF type = system.longrealType THEN type := system.realType
  5128. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5129. END;
  5130. ELSIF type IS SyntaxTree.ComplexType THEN
  5131. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5132. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5133. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5134. END;
  5135. ELSE
  5136. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5137. END;
  5138. IF (parameter0.resolved # NIL) THEN
  5139. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5140. IF parameter0 IS SyntaxTree.Value THEN
  5141. result.SetResolved(parameter0(SyntaxTree.Value));
  5142. END;
  5143. END;
  5144. (* ---- LONG ----- *)
  5145. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5146. type := type0;
  5147. IF IsSignedIntegerType(type) THEN
  5148. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5149. ELSIF type = system.longintType THEN type := system.hugeintType
  5150. ELSIF type = system.integerType THEN type := system.longintType
  5151. ELSIF type = system.shortintType THEN type := system.integerType
  5152. ELSE
  5153. CASE type.sizeInBits OF
  5154. 8: type := Global.Integer16
  5155. |16: type := Global.Integer32
  5156. |32: type := Global.Integer64
  5157. END;
  5158. END;
  5159. ELSIF type IS SyntaxTree.FloatType THEN
  5160. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5161. ELSIF type= system.realType THEN type := system.longrealType
  5162. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5163. END;
  5164. ELSIF type IS SyntaxTree.ComplexType THEN
  5165. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5166. ELSIF type = system.complexType THEN type := system.longcomplexType
  5167. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5168. END;
  5169. ELSE
  5170. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5171. END;
  5172. IF (parameter0.resolved # NIL) THEN
  5173. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5174. IF parameter0 IS SyntaxTree.Value THEN
  5175. result.SetResolved(parameter0(SyntaxTree.Value));
  5176. END;
  5177. END;
  5178. (* ---- SIZE OF ----- *)
  5179. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5180. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5181. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5182. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5183. type := system.integerType;
  5184. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5185. ELSE
  5186. (* for variables, system sizeof could represent the physically occupied size
  5187. determined via the type descriptor, implement that ? *)
  5188. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5189. END
  5190. (* ---- SYSTEM.TRACE -----*)
  5191. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5192. FOR i := 0 TO numberActualParameters-1 DO
  5193. parameter0 := actualParameters.GetExpression(i);
  5194. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5195. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5196. END;
  5197. END;
  5198. (* remaining issues can only be tested in backend *)
  5199. (* ---- ADDRESSOF----- *)
  5200. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5201. IF HasAddress(parameter0) THEN
  5202. type := system.addressType;
  5203. ELSE
  5204. type := SyntaxTree.invalidType;
  5205. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5206. END;
  5207. (* ---- BIT ----- *)
  5208. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5209. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5210. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5211. actualParameters.SetExpression(0,parameter0);
  5212. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5213. actualParameters.SetExpression(1,parameter1);
  5214. END;
  5215. type := system.booleanType;
  5216. (* ----- MSK ---- *)
  5217. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5218. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5219. ConvertOperands(parameter0,parameter1);
  5220. actualParameters.SetExpression(0,parameter0);
  5221. actualParameters.SetExpression(1,parameter1);
  5222. END;
  5223. type := parameter0.type;
  5224. (* ---- SYSTEM.GET64 ----- *)
  5225. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5226. IF CheckAddressType(parameter0) THEN
  5227. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5228. actualParameters.SetExpression(0,parameter0);
  5229. END;
  5230. type := system.hugeintType;
  5231. (* ---- SYSTEM.GET32 ----- *)
  5232. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5233. IF CheckAddressType(parameter0) THEN
  5234. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5235. actualParameters.SetExpression(0,parameter0);
  5236. END;
  5237. type := system.longintType;
  5238. (* ---- SYSTEM.GET16 ----- *)
  5239. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5240. IF CheckAddressType(parameter0) THEN
  5241. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5242. actualParameters.SetExpression(0,parameter0);
  5243. END;
  5244. type := system.integerType;
  5245. (* ---- SYSTEM.GET8 ----- *)
  5246. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5247. IF CheckAddressType(parameter0) THEN
  5248. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5249. actualParameters.SetExpression(0,parameter0);
  5250. END;
  5251. type := system.shortintType;
  5252. (* ---- SYSTEM.GetStackPointer ----- *)
  5253. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5254. type := system.addressType;
  5255. (* ---- SYSTEM.GetFramePointer ----- *)
  5256. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5257. type := system.addressType;
  5258. (* ---- SYSTEM.GetActivity ----- *)
  5259. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5260. type := system.objectType;
  5261. (* ---- SYSTEM.SetStackPointer ----- *)
  5262. ELSIF (id = Global.systemSetStackPointer) & 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.SetFramePointer ----- *)
  5268. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5269. IF CheckAddressType(parameter0) THEN
  5270. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5271. actualParameters.SetExpression(0,parameter0);
  5272. END;
  5273. (* ---- SYSTEM.SetActivity ----- *)
  5274. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5275. IF CheckObjectType(parameter0) THEN
  5276. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5277. actualParameters.SetExpression(0,parameter0);
  5278. END;
  5279. (* ---- LSH, LSL, ROT, ROR ----- *)
  5280. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5281. type := type0;
  5282. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5283. actualParameters.SetExpression(1, parameter1);
  5284. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5285. IF id = Global.Lsh THEN
  5286. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5287. ELSIF id = Global.Rot THEN
  5288. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5289. ELSIF id = Global.Ror THEN
  5290. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5291. END;
  5292. END;
  5293. (* ---- SYSTEM.VAL ----- *)
  5294. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5295. IF CheckTypeDeclarationType(parameter0) THEN
  5296. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5297. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5298. result := SyntaxTree.invalidExpression;
  5299. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5300. ELSE
  5301. IF (parameter1.resolved # NIL) THEN
  5302. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5303. IF parameter0 IS SyntaxTree.Value THEN
  5304. result.SetResolved(parameter0(SyntaxTree.Value));
  5305. END;
  5306. END;
  5307. result.SetAssignable(parameter1.assignable);
  5308. END;
  5309. END;
  5310. (* ---- SYSTEM.GET ----- *)
  5311. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5312. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5313. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5314. actualParameters.SetExpression(0,parameter0);
  5315. END;
  5316. (* ---- SYSTEM.PUT ----- *)
  5317. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5318. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5319. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5320. actualParameters.SetExpression(0,parameter0);
  5321. END;
  5322. (* ---- SYSTEM.PUT64 ----- *)
  5323. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5324. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5325. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5326. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5327. actualParameters.SetExpression(0,parameter0);
  5328. actualParameters.SetExpression(1,parameter1);
  5329. END;
  5330. (* ---- SYSTEM.PUT32 ----- *)
  5331. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5332. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5333. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5334. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5335. actualParameters.SetExpression(0,parameter0);
  5336. actualParameters.SetExpression(1,parameter1);
  5337. END;
  5338. (* ---- SYSTEM.PUT16 ----- *)
  5339. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5340. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5341. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5342. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5343. actualParameters.SetExpression(0,parameter0);
  5344. actualParameters.SetExpression(1,parameter1);
  5345. END;
  5346. (* ---- SYSTEM.PUT8 ----- *)
  5347. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5348. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5349. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5350. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5351. actualParameters.SetExpression(0,parameter0);
  5352. actualParameters.SetExpression(1,parameter1);
  5353. END;
  5354. (* ---- SYSTEM.MOVE ----- *)
  5355. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5356. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5357. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5358. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5359. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5360. actualParameters.SetExpression(0,parameter0);
  5361. actualParameters.SetExpression(1,parameter1);
  5362. actualParameters.SetExpression(2,parameter2);
  5363. END;
  5364. (* ---- SYSTEM.NEW ----- *)
  5365. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5366. IF ~IsPointerType(parameter0.type) THEN
  5367. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5368. ELSIF CheckSizeType(parameter1) THEN
  5369. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5370. actualParameters.SetExpression(1,parameter1);
  5371. END;
  5372. (* ----SYSTEM.REF ---- *)
  5373. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5374. type := system.addressType
  5375. (* ---- INCR ----- *)
  5376. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5377. type := system.sizeType;
  5378. base := type0;
  5379. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5380. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5381. IF i1 < 0 THEN
  5382. Error(position,Diagnostics.Invalid,"invalid dimension");
  5383. base := SyntaxTree.invalidType;
  5384. ELSE
  5385. base := ArrayBase(base,i1);
  5386. IF (base # NIL) & Indexable(base) THEN
  5387. ELSE
  5388. Error(position,Diagnostics.Invalid,"len on no array");
  5389. IF VerboseErrorMessage THEN
  5390. Printout.Info("base",base);
  5391. END;
  5392. base := SyntaxTree.invalidType;
  5393. END;
  5394. END;
  5395. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5396. actualParameters.SetExpression(1,parameter1);
  5397. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5398. mathArrayType := base(SyntaxTree.MathArrayType);
  5399. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5400. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5401. type := system.longintType;
  5402. END;
  5403. END;
  5404. ELSE
  5405. type := system.longintType;
  5406. END;
  5407. (* ---- SUM ----- *)
  5408. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5409. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5410. (* ---- ALL ----- *)
  5411. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5412. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5413. (* ---- DIM ----- *)
  5414. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5415. type := system.sizeType;
  5416. IF type0 IS SyntaxTree.MathArrayType THEN
  5417. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5418. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5419. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5420. END;
  5421. ELSE
  5422. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5423. END;
  5424. (* ---- CAS ----- *)
  5425. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5426. IF type0.IsComposite () THEN
  5427. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5428. result := SyntaxTree.invalidExpression;
  5429. ELSIF ~IsVariable (parameter0) THEN
  5430. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5431. result := SyntaxTree.invalidExpression;
  5432. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5433. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5434. result := SyntaxTree.invalidExpression;
  5435. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5436. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5437. result := SyntaxTree.invalidExpression;
  5438. ELSE
  5439. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5440. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5441. type := type0;
  5442. END;
  5443. (* ---- RESHAPE ----- *)
  5444. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5445. IF type0 IS SyntaxTree.MathArrayType THEN
  5446. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5447. base := ArrayBase(type0,MAX(LONGINT));
  5448. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5449. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5450. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5451. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5452. IF ~CompatibleTo(system,type0,parameterType) THEN
  5453. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5454. result := SyntaxTree.invalidExpression;
  5455. ELSE
  5456. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5457. END;
  5458. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5459. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5460. IF ~CompatibleTo(system,type1,parameterType) THEN
  5461. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5462. result := SyntaxTree.invalidExpression;
  5463. ELSE
  5464. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5465. END;
  5466. ELSE
  5467. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5468. result := SyntaxTree.invalidExpression;
  5469. END;
  5470. (* ---- SYSTEM.TYPECODE ----- *)
  5471. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5472. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5473. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5474. type := type.resolved;
  5475. IF type IS SyntaxTree.PointerType THEN
  5476. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5477. END;
  5478. IF ~(type IS SyntaxTree.RecordType) THEN
  5479. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5480. END;
  5481. ELSE
  5482. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5483. END;
  5484. type := system.addressType;
  5485. (* -------- FLT --------- *)
  5486. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5487. type := system.realType;
  5488. IF IsRealValue(parameter0, r) THEN
  5489. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5490. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5491. i0 := i; i := ABS(i);
  5492. IF i # 0 THEN
  5493. i1 := 23;
  5494. IF i >= 2*800000H THEN
  5495. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5496. ELSIF i < 800000H THEN
  5497. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5498. END;
  5499. i := (i1 + 127)*800000H - 800000H + i;
  5500. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5501. END;
  5502. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5503. END;
  5504. (* ------- CONNECT -------*)
  5505. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5506. IF ~(currentIsCellNet) THEN
  5507. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5508. END;
  5509. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5510. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5511. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5512. END;
  5513. IF numberActualParameters = 3 THEN
  5514. IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5515. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5516. END;
  5517. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5518. actualParameters.SetExpression(2,parameter2);
  5519. END;
  5520. activeCellsStatement := TRUE;
  5521. (* ---------- DELEGATE --------*)
  5522. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5523. IF ~(currentIsCellNet) THEN
  5524. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5525. END;
  5526. IF ~CheckPortType(parameter1, inPort) THEN
  5527. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5528. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5529. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5530. ELSIF (outPort.direction # inPort.direction) THEN
  5531. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5532. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5533. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5534. END;
  5535. activeCellsStatement := TRUE;
  5536. (* --------- RECEIVE ---------*)
  5537. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5538. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5539. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5540. IF inPort.direction # SyntaxTree.InPort THEN
  5541. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5542. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5543. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5544. END;
  5545. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5546. IF ~SameType(parameter2.type, system.integerType) THEN
  5547. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5548. END;
  5549. END;
  5550. END;
  5551. (* --------- SEND ---------*)
  5552. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5553. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5554. IF CheckPortType(parameter0,outPort) THEN
  5555. IF outPort.direction # SyntaxTree.OutPort THEN
  5556. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5557. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5558. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5559. ELSE
  5560. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5561. actualParameters.SetExpression(1,parameter1);
  5562. END;
  5563. END;
  5564. (* ------- custom builtins ----- *)
  5565. ELSIF id = Global.systemSpecial THEN
  5566. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5567. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5568. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5569. type := procedureType.returnType;
  5570. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5571. (* go through all formal parameters *)
  5572. formalParameter := procedureType.firstParameter;
  5573. FOR i := 0 TO actualParameters.Length() - 1 DO
  5574. actualParameter := actualParameters.GetExpression(i);
  5575. IF actualParameter = SyntaxTree.invalidExpression THEN
  5576. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5577. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5578. ELSE
  5579. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5580. END;
  5581. actualParameters.SetExpression(i, actualParameter);
  5582. formalParameter := formalParameter.nextParameter
  5583. END
  5584. END
  5585. ELSE
  5586. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5587. result := SyntaxTree.invalidExpression;
  5588. END;
  5589. END;
  5590. IF result # SyntaxTree.invalidExpression THEN
  5591. type := ResolveType(type);
  5592. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5593. result.SetType(type);
  5594. END;
  5595. RETURN result
  5596. END NewBuiltinCallDesignator;
  5597. (** return type guard designator left(type)
  5598. - check if type can be extended (i.e. is no static record)
  5599. - check if type is a type extension of left.type
  5600. - returns new type guard designator
  5601. returns invalidDesignator = invalidExpression if error
  5602. **)
  5603. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type): SyntaxTree.Designator;
  5604. VAR result: SyntaxTree.Designator;
  5605. BEGIN
  5606. result := SyntaxTree.invalidDesignator;
  5607. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5608. Error(position,Diagnostics.Invalid,"no type extension of type");
  5609. IF VerboseErrorMessage THEN
  5610. Printout.Info("left",left);
  5611. Printout.Info("type",type);
  5612. END;
  5613. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5614. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5615. ELSE
  5616. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5617. result.SetType(type);
  5618. result.SetAssignable(left.assignable);
  5619. END;
  5620. RETURN result
  5621. END NewTypeGuardDesignator;
  5622. (** check and resolve parameter designator left(expression list)
  5623. - check expression list
  5624. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5625. - elsif left is a procedure type then
  5626. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5627. - else return is a procedure call then return ProcedureCallDesignator
  5628. returns invalidDesignator = invalidExpression if error
  5629. **)
  5630. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5631. VAR
  5632. parameters: SyntaxTree.ExpressionList;
  5633. left: SyntaxTree.Designator;
  5634. result,expression: SyntaxTree.Expression;
  5635. typeDeclaration: SyntaxTree.TypeDeclaration;
  5636. type, expressionType: SyntaxTree.Type;
  5637. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5638. BEGIN
  5639. type := type.resolved;
  5640. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5641. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5642. END;
  5643. RETURN type
  5644. END BaseType;
  5645. BEGIN
  5646. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5647. result := SyntaxTree.invalidDesignator;
  5648. left := ResolveDesignator(designator.left);
  5649. IF left # SyntaxTree.invalidDesignator THEN
  5650. parameters := designator.parameters;
  5651. IF ExpressionList(parameters) THEN
  5652. IF (left.type = NIL) THEN
  5653. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5654. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5655. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5656. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5657. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5658. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5659. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5660. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left);
  5661. ELSE
  5662. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5663. END
  5664. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5665. expression := parameters.GetExpression(0);
  5666. type := typeDeclaration.declaredType.resolved;
  5667. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5668. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5669. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5670. OR (expressionType IS SyntaxTree.EnumerationType)
  5671. ) THEN
  5672. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5673. ELSE
  5674. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5675. END;
  5676. ELSE
  5677. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5678. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5679. result := SyntaxTree.invalidDesignator;
  5680. END;
  5681. ELSE
  5682. result := SyntaxTree.invalidDesignator
  5683. END;
  5684. END;
  5685. resolvedExpression := result;
  5686. END VisitParameterDesignator;
  5687. (** check dereference designator left^
  5688. - check if left is pointer type or left is object type
  5689. - return new dereference designator with type = left.baseType.type (if appropriate)
  5690. with error handling
  5691. returns invalidDesignator = invalidExpression if error
  5692. **)
  5693. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5694. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5695. BEGIN
  5696. result := SyntaxTree.invalidDesignator;
  5697. type := left.type;
  5698. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5699. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  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.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5705. type := type.resolved;
  5706. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5707. result.SetAssignable(TRUE);
  5708. result.SetType(type);
  5709. result.SetHidden(left.isHidden);
  5710. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5711. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5712. result.SetAssignable(TRUE);
  5713. result.SetType(type);
  5714. result.SetHidden(left.isHidden);
  5715. ELSE
  5716. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5717. IF VerboseErrorMessage THEN
  5718. Printout.Info("pointer", type);
  5719. Printout.Info("scope", currentScope);
  5720. END;
  5721. END;
  5722. RETURN result
  5723. END NewDereferenceDesignator;
  5724. (** check supercall designator left^
  5725. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5726. - return new supercall designator with type = left.type
  5727. with error handling
  5728. **)
  5729. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5730. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5731. objectScope: SyntaxTree.Scope;
  5732. BEGIN
  5733. result := SyntaxTree.invalidDesignator;
  5734. IF left = SyntaxTree.invalidDesignator THEN
  5735. (* error already handled *)
  5736. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5737. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5738. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5739. IF symbol IS SyntaxTree.Procedure THEN
  5740. procedure := symbol(SyntaxTree.Procedure);
  5741. objectScope := currentScope;
  5742. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5743. objectScope := objectScope.outerScope;
  5744. END;
  5745. IF (left.left = NIL) OR ~
  5746. (
  5747. (left.left IS SyntaxTree.SelfDesignator) OR
  5748. (left.left IS SyntaxTree.DereferenceDesignator)
  5749. & (left.left(SyntaxTree.Designator).left # NIL)
  5750. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5751. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5752. IF VerboseErrorMessage THEN
  5753. Printout.Info("left.left",left.left);
  5754. END;
  5755. ELSIF procedure.super # NIL THEN
  5756. result := SyntaxTree.NewSupercallDesignator(position,left);
  5757. result.SetType(left.type.resolved)
  5758. ELSE
  5759. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5760. END;
  5761. ELSE
  5762. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5763. END;
  5764. ELSE
  5765. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5766. END;
  5767. RETURN result
  5768. END NewSupercallDesignator;
  5769. (** check and semantically resolve arrow designator left^
  5770. - if left is procedure type -> result := SupercallDesignator
  5771. - else result := DereferenceDesignator
  5772. returns result via global variable resolvedExpression
  5773. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5774. **)
  5775. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5776. VAR left: SyntaxTree.Designator;
  5777. BEGIN
  5778. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5779. left := ResolveDesignator(arrowDesignator.left);
  5780. IF left # NIL THEN
  5781. IF (left.type = NIL) THEN
  5782. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5783. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5784. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5785. ELSE
  5786. IF IsPointerToObject(left.type) THEN
  5787. Warning(arrowDesignator.position, "forbidden dereference on object");
  5788. END;
  5789. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5790. END
  5791. END
  5792. END VisitArrowDesignator;
  5793. (** check and return expression
  5794. - if expression has no type then resolve expression
  5795. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5796. - return result
  5797. **)
  5798. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5799. VAR result,prev: SyntaxTree.Expression;
  5800. BEGIN
  5801. IF expression = NIL THEN result := NIL
  5802. ELSIF (expression.type = NIL) THEN
  5803. prev := resolvedExpression;
  5804. resolvedExpression := SyntaxTree.invalidExpression;
  5805. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5806. expression.SetType(SyntaxTree.invalidType);
  5807. END;
  5808. expression.Accept(SELF);
  5809. result := resolvedExpression;
  5810. IF currentIsRealtime THEN
  5811. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5812. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5813. END;
  5814. END;
  5815. (* designator modifiers for backends if they support it ...*)
  5816. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5817. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5818. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5819. END;
  5820. resolvedExpression := prev
  5821. ELSE
  5822. result := expression
  5823. END;
  5824. RETURN result
  5825. END ResolveExpression;
  5826. (**
  5827. check expression to be constant expression
  5828. - resolve expression
  5829. - if valid then check that of value type
  5830. report error and return invalidExpression if anything fails
  5831. **)
  5832. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5833. VAR position: LONGINT;
  5834. BEGIN
  5835. position := expression.position;
  5836. expression := ResolveExpression(expression);
  5837. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5838. ELSIF (expression.resolved = NIL) THEN
  5839. Error(position,Diagnostics.Invalid,"expression is not constant");
  5840. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5841. expression := SyntaxTree.invalidExpression;
  5842. END;
  5843. RETURN expression
  5844. END ConstantExpression;
  5845. (** check expression to be constant integer
  5846. - resolve expresssion
  5847. - if valid then check that of integer value type
  5848. report error and return invalidExpression if anything fails
  5849. **)
  5850. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5851. VAR position: LONGINT;
  5852. BEGIN
  5853. position := expression.position;
  5854. expression := ResolveExpression(expression);
  5855. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5856. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5857. expression := SyntaxTree.invalidExpression;
  5858. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5859. END;
  5860. RETURN expression
  5861. END ConstantInteger;
  5862. (** check expression as positive (>=0) constant integer
  5863. - resolve expression
  5864. - if valid then check that integer value
  5865. - if integer value then check that value >= 0
  5866. report error and return invalidExpression if anything fails
  5867. **)
  5868. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5869. VAR position: LONGINT;
  5870. BEGIN
  5871. position := expression.position;
  5872. expression := ConstantExpression(expression);
  5873. IF expression = SyntaxTree.invalidExpression THEN
  5874. (* error already reported *)
  5875. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5876. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5877. expression := SyntaxTree.invalidExpression
  5878. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5879. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5880. END;
  5881. RETURN expression
  5882. END ConstantIntegerGeq0;
  5883. (** check expression as condition
  5884. - resolve expression
  5885. - if valid expression then check that result type is boolean
  5886. report error and return invalidExpression if anything fails
  5887. **)
  5888. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5889. VAR position: LONGINT;
  5890. BEGIN
  5891. position := expression.position;
  5892. expression := ResolveExpression(expression);
  5893. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5894. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5895. expression := SyntaxTree.invalidExpression;
  5896. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5897. END;
  5898. RETURN expression
  5899. END ResolveCondition;
  5900. (*** symbols ***)
  5901. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5902. BEGIN
  5903. x.Accept(SELF);
  5904. END ResolveSymbol;
  5905. (** check a symbol
  5906. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5907. **)
  5908. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5909. VAR scope: SyntaxTree.Scope;
  5910. BEGIN
  5911. (* visibility *)
  5912. scope := symbol.scope;
  5913. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5914. scope := scope.outerScope;
  5915. END;
  5916. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5917. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5918. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5919. IF VerboseErrorMessage THEN
  5920. Printout.Info("symbol",symbol);
  5921. END;
  5922. END;
  5923. END;
  5924. END CheckSymbolVisibility;
  5925. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5926. If node is currently being resolved then emit a cyclic definition error.
  5927. Return TRUE only if node is fully resolved.
  5928. **)
  5929. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5930. VAR result: BOOLEAN;
  5931. BEGIN
  5932. IF SyntaxTree.Resolved IN x.state THEN
  5933. result := FALSE
  5934. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5935. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5936. result := FALSE;
  5937. ELSE
  5938. result := TRUE;
  5939. x.SetState(SyntaxTree.BeingResolved)
  5940. END;
  5941. RETURN result
  5942. END SymbolNeedsResolution;
  5943. (** check and resolve a type declaration symbol = Type
  5944. - set type to declaration type
  5945. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5946. This is so because the type declaration itself does not have a type but it only stands for a type.
  5947. In the implementation of the compiler this made a lot much easier.
  5948. - resolve and set declared type
  5949. - check symbol
  5950. **)
  5951. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5952. VAR prevScope: SyntaxTree.Scope;
  5953. BEGIN
  5954. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5955. IF SymbolNeedsResolution(typeDeclaration) THEN
  5956. prevScope := currentScope;
  5957. currentScope := typeDeclaration.scope;
  5958. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5959. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5960. CheckSymbolVisibility(typeDeclaration);
  5961. typeDeclaration.SetState(SyntaxTree.Resolved);
  5962. currentScope := prevScope;
  5963. END;
  5964. END VisitTypeDeclaration;
  5965. (** check and resolve a constant declaration symbol = (constant) expression
  5966. - check expression
  5967. - set type and value
  5968. - check symbol
  5969. **)
  5970. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  5971. VAR
  5972. expression: SyntaxTree.Expression;
  5973. type: SyntaxTree.Type;
  5974. name: Basic.SegmentedName;
  5975. replacement: Replacement;
  5976. stringReader: Streams.StringReader;
  5977. scanner: Scanner.Scanner; parser: Parser.Parser;
  5978. BEGIN
  5979. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  5980. IF SymbolNeedsResolution(constant) THEN
  5981. expression := constant.value;
  5982. IF replacements # NIL THEN
  5983. Global.GetSymbolSegmentedName(constant, name);
  5984. replacement := replacements;
  5985. WHILE (replacement # NIL) & (replacement.name # name) DO
  5986. replacement := replacement.next;
  5987. END;
  5988. IF replacement # NIL THEN
  5989. InfoSS(constant.position, "replacing constant", constant.name);
  5990. (*
  5991. NEW(stringReader, Strings.Length(replacement.string^));
  5992. stringReader.Set(replacement.string^);
  5993. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5994. NEW(parser, scanner, diagnostics);
  5995. expression := parser.Expression();
  5996. *)
  5997. expression := replacement.expression;
  5998. replacement.used := TRUE;
  5999. END;
  6000. END;
  6001. constant.SetType(SyntaxTree.invalidType);
  6002. expression := ConstantExpression(expression);
  6003. ASSERT(expression.type # NIL);
  6004. type := expression.type.resolved;
  6005. constant.SetType(type);
  6006. constant.SetValue(expression);
  6007. CheckSymbolVisibility(constant);
  6008. constant.SetState(SyntaxTree.Resolved);
  6009. END;
  6010. END VisitConstant;
  6011. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6012. VAR procedureAlignment: LONGINT;
  6013. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6014. (* least common multiple *)
  6015. VAR a,b: LONGINT;
  6016. BEGIN
  6017. a := a0; b := b0;
  6018. WHILE (a # b) DO
  6019. IF a < b THEN a := a+a0
  6020. ELSE b := b + b0
  6021. END;
  6022. END;
  6023. RETURN a
  6024. END LCM;
  6025. BEGIN
  6026. IF alignment > 1 THEN
  6027. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6028. IF (procedureAlignment > 1) THEN
  6029. alignment := LCM(alignment, procedureAlignment);
  6030. END;
  6031. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6032. END;
  6033. END AdaptStackAlignment;
  6034. (** check and resolve a variable / field
  6035. - check and set type
  6036. - negative check on open array type
  6037. - check symbol
  6038. **)
  6039. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6040. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6041. BEGIN
  6042. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6043. IF SymbolNeedsResolution(variable) THEN
  6044. modifiers := variable.modifiers;
  6045. (*
  6046. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6047. variable.AddFlags(flags);
  6048. *)
  6049. variable.SetType(ResolveType(variable.type));
  6050. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6051. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6052. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6053. END;
  6054. END;
  6055. CheckSymbolVisibility(variable);
  6056. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6057. variable.SetUntraced(TRUE);
  6058. IF ~ContainsPointer(variable.type) THEN
  6059. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6060. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6061. END;
  6062. END;
  6063. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6064. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6065. IF ~PowerOf2(value) THEN
  6066. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6067. ELSE
  6068. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6069. END;
  6070. END;
  6071. variable.SetAlignment(FALSE,value);
  6072. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6073. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6074. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6075. END;
  6076. variable.SetAlignment(TRUE, value);
  6077. END;
  6078. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6079. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6080. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6081. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6082. END;
  6083. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6084. IF variable.initializer # NIL THEN
  6085. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6086. END;
  6087. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6088. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6089. pointerType.SetPointerBase(variable.type);
  6090. pointerType.SetHidden(TRUE);
  6091. variable.SetType(ResolveType(pointerType));
  6092. END;
  6093. variable.SetState(SyntaxTree.Resolved);
  6094. END;
  6095. END VisitVariable;
  6096. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6097. BEGIN
  6098. VisitVariable(property)
  6099. END VisitProperty;
  6100. (** check and resolve a (procedure) parameter
  6101. - check and set type
  6102. - check symbol
  6103. - check parameter kind and set read-only flags if appropriate
  6104. **)
  6105. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6106. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6107. BEGIN
  6108. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6109. IF SymbolNeedsResolution(parameter) THEN
  6110. modifiers := parameter.modifiers;
  6111. parameter.SetType(ResolveType(parameter.type));
  6112. ASSERT(parameter.type.resolved # NIL);
  6113. CheckSymbolVisibility(parameter);
  6114. IF parameter.defaultValue # NIL THEN
  6115. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6116. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6117. ELSE
  6118. expression := ConstantExpression(parameter.defaultValue);
  6119. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6120. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6121. parameter.SetDefaultValue(expression);
  6122. END;
  6123. END;
  6124. END;
  6125. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6126. parameter.SetUntraced(TRUE);
  6127. IF ~ContainsPointer(parameter.type) THEN
  6128. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6129. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6130. END;
  6131. END;
  6132. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6133. parameter.SetMoveable(TRUE);
  6134. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6135. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6136. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6137. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6138. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6139. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6140. END;
  6141. END;
  6142. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6143. parameter.SetState(SyntaxTree.Resolved);
  6144. END;
  6145. END VisitParameter;
  6146. (** check and resolve a procedure (with declaration and implementation scope)
  6147. - check the procedure type
  6148. - check if method (i.e. in record scope), if so then
  6149. - check if (unique) constructor
  6150. - check if (unique) finalizer
  6151. - check if super method available, if so then check signature
  6152. - of not in record scope then negative check on constructor flag
  6153. - of not in record scope then negative check on finalizer flag
  6154. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6155. - check procedure symbol
  6156. **)
  6157. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6158. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6159. procedureType: SyntaxTree.ProcedureType;
  6160. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6161. qualifiedType: SyntaxTree.QualifiedType;
  6162. value: LONGINT;
  6163. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6164. position: LONGINT;
  6165. BEGIN
  6166. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6167. IF IsOberonInline(procedure) THEN
  6168. IF SyntaxTree.Public * procedure.access # {} THEN
  6169. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6170. END;
  6171. procedure.SetInline(FALSE);
  6172. procedure.SetOberonInline(TRUE);
  6173. END;
  6174. IF SymbolNeedsResolution(procedure) THEN
  6175. recentIsRealtime := currentIsRealtime;
  6176. recentIsBodyProcedure := currentIsBodyProcedure;
  6177. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6178. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6179. modifiers := procedureType.modifiers;
  6180. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6181. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6182. IF useDarwinCCalls THEN (*fld*)
  6183. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6184. ELSE
  6185. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6186. END
  6187. END;
  6188. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6189. procedureType.SetInterrupt(TRUE);
  6190. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6191. END;
  6192. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6193. procedureType.SetNoReturn(TRUE);
  6194. END;
  6195. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6196. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6197. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6198. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6199. END;
  6200. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6201. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6202. END;
  6203. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6204. IF ~PowerOf2(value) THEN
  6205. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6206. ELSE
  6207. procedureType.SetStackAlignment(value)
  6208. END;
  6209. END;
  6210. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6211. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6212. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6213. END;
  6214. CheckModifiers(modifiers, TRUE);
  6215. modifiers := procedureType.returnTypeModifiers;
  6216. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6217. CheckModifiers(modifiers, TRUE);
  6218. FixProcedureType(procedureType);
  6219. currentIsRealtime := procedureType.isRealtime;
  6220. currentIsBodyProcedure := procedure.isBodyProcedure;
  6221. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6222. THEN
  6223. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6224. END;
  6225. CheckSymbolVisibility(procedure);
  6226. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6227. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6228. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6229. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6230. END;
  6231. END;
  6232. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6233. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6234. procedureType.SetDelegate(TRUE);
  6235. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6236. IF record.pointerType.typeDeclaration = NIL THEN
  6237. selfParameter.SetType(record.pointerType);
  6238. ELSE
  6239. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6240. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6241. qualifiedType.SetResolved(record.pointerType);
  6242. selfParameter.SetType(qualifiedType);
  6243. END;
  6244. selfParameter.SetAccess(SyntaxTree.Hidden);
  6245. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6246. IF procedure.isConstructor THEN
  6247. (*! constructor is always visible, compatibility to paco
  6248. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6249. *)
  6250. procedure.MarkUsed;
  6251. IF procedureType.returnType # NIL THEN
  6252. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6253. END;
  6254. proc := procedure.scope.firstProcedure;
  6255. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6256. proc := proc.nextProcedure;
  6257. END;
  6258. IF proc # NIL THEN
  6259. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6260. ELSE
  6261. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6262. END;
  6263. END;
  6264. IF procedure.isFinalizer THEN
  6265. procedure.MarkUsed;
  6266. IF procedureType.returnType # NIL THEN
  6267. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6268. END;
  6269. IF procedureType.numberParameters # 0 THEN
  6270. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6271. END;
  6272. proc := procedure.scope.firstProcedure;
  6273. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6274. proc := proc.nextProcedure;
  6275. END;
  6276. IF proc # NIL THEN
  6277. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6278. ELSE
  6279. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6280. END;
  6281. END;
  6282. super := FindSuperProcedure(record.recordScope, procedure);
  6283. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6284. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6285. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6286. END;
  6287. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6288. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6289. END;
  6290. IF super.isFinal THEN
  6291. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6292. END;
  6293. procedure.SetSuper(super);
  6294. super.SetOverwritten(TRUE);
  6295. procedure.SetAccess(procedure.access+super.access);
  6296. procedure.MarkUsed;
  6297. END;
  6298. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6299. THEN
  6300. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6301. END;
  6302. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6303. IF cellsAreObjects THEN
  6304. procedureType.SetDelegate(TRUE);
  6305. END;
  6306. IF procedure.isConstructor THEN
  6307. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6308. END;
  6309. ELSIF procedure.isConstructor THEN
  6310. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6311. END;
  6312. Declarations(procedure.procedureScope);
  6313. (* body resolution part done as late fix of the procedure type *)
  6314. procedure.SetState(SyntaxTree.Resolved);
  6315. currentIsRealtime := recentIsRealtime;
  6316. currentIsBodyProcedure := recentIsBodyProcedure;
  6317. END;
  6318. END VisitProcedure;
  6319. (**
  6320. a builtin procedure is a global item that may not be modified locally
  6321. instead the resolving of builtin procedure calls are done in the NewBuiltinCallDesignator
  6322. **)
  6323. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6324. VAR type: SyntaxTree.Type;
  6325. BEGIN
  6326. type := ResolveType(builtinProcedure.type);
  6327. END VisitBuiltin;
  6328. (* nopov *)
  6329. (** check and resolve operator
  6330. - operators are first checked as procedures
  6331. - then additional operator-specific checks are done
  6332. - note that only module-scope operators are checked here
  6333. (operators in a record scope are only allowed in the context of
  6334. array-structured object types and checked in 'ResolveArrayStructure')
  6335. - also note that inter-operator conformity is not checked here
  6336. **)
  6337. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6338. VAR
  6339. procedureType: SyntaxTree.ProcedureType;
  6340. leftType, rightType: SyntaxTree.Type;
  6341. identifierNumber, position: LONGINT;
  6342. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6343. modifiers: SyntaxTree.Modifier;
  6344. (** whether a type is locally defined in the current module scope
  6345. for arrays, the base type must be locally defined **)
  6346. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6347. BEGIN
  6348. IF type = NIL THEN
  6349. RETURN FALSE
  6350. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6351. RETURN TRUE
  6352. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6353. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6354. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6355. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6356. ELSE
  6357. RETURN FALSE
  6358. END
  6359. END IsLocallyDefined;
  6360. BEGIN
  6361. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6362. procedureType := operator.type(SyntaxTree.ProcedureType);
  6363. modifiers := procedureType.modifiers;
  6364. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6365. CheckModifiers(modifiers, TRUE);
  6366. VisitProcedure(operator);
  6367. IF operator.scope IS SyntaxTree.RecordScope THEN
  6368. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6369. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6370. IF identifierNumber = -1 THEN
  6371. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6372. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6373. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6374. ELSE
  6375. IF procedureType.numberParameters < 1 THEN
  6376. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6377. ELSIF procedureType.numberParameters > 2 THEN
  6378. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6379. ELSE
  6380. (* determine operand types *)
  6381. leftType := procedureType.firstParameter.type;
  6382. IF procedureType.numberParameters > 1 THEN
  6383. rightType := procedureType.firstParameter.nextParameter.type
  6384. ELSE
  6385. rightType := NIL
  6386. END;
  6387. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6388. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6389. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6390. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6391. END
  6392. END;
  6393. (* TODO: refine the checks, think about how restrictive the checks should be
  6394. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6395. They might be used for intersection, union, complement of custom object types *)
  6396. (* defaults *)
  6397. hasReturnType := TRUE;
  6398. mustBeUnary := FALSE;
  6399. mustBeBinary := FALSE;
  6400. mustReturnBoolean := FALSE;
  6401. mustReturnInteger := FALSE;
  6402. mustHaveEquitypedOperands := FALSE;
  6403. (* operator-specific exceptions *)
  6404. CASE identifierNumber OF
  6405. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6406. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6407. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6408. mustBeBinary := TRUE
  6409. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6410. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6411. | Scanner.Times: mustBeBinary := TRUE
  6412. | Scanner.TimesTimes: mustBeBinary := TRUE
  6413. | Scanner.DotTimes: mustBeBinary := TRUE
  6414. | Scanner.PlusTimes: mustBeBinary := TRUE
  6415. | Scanner.Slash: mustBeBinary := TRUE
  6416. | Scanner.Backslash: mustBeBinary := TRUE
  6417. | Scanner.DotSlash: mustBeBinary := TRUE
  6418. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6419. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6420. | Scanner.Not: mustBeUnary := TRUE
  6421. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6422. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6423. | Scanner.Transpose: mustBeUnary := TRUE;
  6424. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6425. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6426. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6427. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6428. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6429. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6430. | Global.Abs: mustBeUnary := TRUE;
  6431. | Global.Ash: (* TODO: arity? *)
  6432. | Global.Cap: (* TODO: arity? *)
  6433. | Global.Chr: mustBeUnary := TRUE;
  6434. | Global.Entier: (* TODO: arity? *)
  6435. | Global.EntierH: (* TODO: arity? *)
  6436. | Global.Len: (* unary and binary *)
  6437. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6438. | Global.Max, Global.Min: (* unary and binary *)
  6439. | Global.Odd: (* TODO: arity? *)
  6440. | Global.Sum: (* TODO: arity? *)
  6441. | Global.All: (* TODO: arity? *)
  6442. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6443. | Scanner.Alias:
  6444. | Scanner.GreaterGreater, Scanner.LessLess:
  6445. mustBeBinary := TRUE;
  6446. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6447. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6448. END;
  6449. (* check parameter count *)
  6450. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6451. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6452. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6453. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6454. END;
  6455. (* check parameter types *)
  6456. (* TODO: is this used at all? *)
  6457. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6458. leftType := procedureType.firstParameter.type;
  6459. rightType := procedureType.firstParameter.nextParameter.type;
  6460. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6461. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6462. END
  6463. END;
  6464. (* check return type *)
  6465. IF hasReturnType THEN
  6466. IF procedureType.returnType = NIL THEN
  6467. Error(operator.position, Diagnostics.Invalid, "return type required")
  6468. ELSIF mustReturnBoolean THEN
  6469. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6470. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6471. END
  6472. ELSIF mustReturnInteger THEN
  6473. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6474. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6475. END
  6476. END
  6477. ELSIF procedureType.returnType # NIL THEN
  6478. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6479. END
  6480. END
  6481. END
  6482. END
  6483. END VisitOperator;
  6484. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6485. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6486. BEGIN
  6487. IF error THEN RETURN FALSE END;
  6488. prevScope := currentScope;
  6489. prevDiagnostics := diagnostics;
  6490. diagnostics := NIL; (* suppress error output *)
  6491. currentScope := module.moduleScope;
  6492. VisitImport(x);
  6493. IF ~error THEN
  6494. module.moduleScope.AddImport(x);
  6495. x.SetScope(module.moduleScope);
  6496. END;
  6497. currentScope := prevScope;
  6498. diagnostics := prevDiagnostics;
  6499. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6500. END AddImport;
  6501. (** check and resolve import
  6502. - check for name = SYSTEM
  6503. - check for forbidden self import
  6504. - search through global import cache: already imported?
  6505. - check if already imported indirectly
  6506. - import if necessary -> set module and enter into import cache
  6507. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6508. - after this import this direct import and all indirect imports are stored in the current module's import list
  6509. **)
  6510. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6511. VAR
  6512. module: SyntaxTree.Module;
  6513. moduleScope: SyntaxTree.ModuleScope;
  6514. import,reimport: SyntaxTree.Import;
  6515. filename: FileName;
  6516. prevScope: SyntaxTree.Scope;
  6517. BEGIN
  6518. IF SymbolNeedsResolution(x) THEN
  6519. prevScope := currentScope;
  6520. x.SetType(SyntaxTree.importType);
  6521. moduleScope := currentScope.ownerModule.moduleScope;
  6522. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6523. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6524. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6525. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6526. ELSE
  6527. (* search through global import list: already imported ? *)
  6528. IF (x.module = NIL) & (importCache # NIL) THEN
  6529. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6530. ELSE import := NIL
  6531. END;
  6532. IF x.module # NIL THEN (* already imported indirectly *)
  6533. module := x.module;
  6534. ELSIF import # NIL THEN (* already in module list *)
  6535. module := import.module;
  6536. ASSERT(module # NIL);
  6537. x.SetModule(module);
  6538. ELSE (* must be imported *)
  6539. Global.ModuleFileName(x.moduleName,x.context,filename);
  6540. IF symbolFileFormat # NIL THEN
  6541. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6542. IF module = NIL THEN
  6543. ErrorSS(x.position,"could not import",filename);
  6544. IF VerboseErrorMessage THEN
  6545. Printout.Info("import",x)
  6546. END
  6547. ELSE
  6548. (*
  6549. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6550. (*! should rather be done by importer *)
  6551. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6552. checker.importCache := importCache;
  6553. checker.arrayBaseImported := arrayBaseImported;
  6554. checker.global := global;
  6555. checker.Module(module); (* semantic check *)
  6556. error := error OR checker.error;
  6557. END;
  6558. *)
  6559. (*
  6560. ASSERT(SyntaxTree.Resolved IN module.state);
  6561. *)
  6562. x.SetModule(module);
  6563. IF importCache # NIL THEN
  6564. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6565. import.SetContext(x.context);
  6566. import.SetModule(module);
  6567. importCache.AddImport(import);
  6568. END;
  6569. END;
  6570. ELSE
  6571. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6572. END;
  6573. END;
  6574. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6575. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6576. END;
  6577. import := module.moduleScope.firstImport;
  6578. WHILE(import # NIL) DO
  6579. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6580. ASSERT(currentScope # NIL);
  6581. ASSERT(currentScope.ownerModule # NIL);
  6582. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6583. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6584. Error(x.position,Diagnostics.Invalid,"recursive import");
  6585. ELSE
  6586. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6587. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6588. IF reimport = NIL THEN (* indirect import *)
  6589. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6590. reimport.SetContext(import.context);
  6591. reimport.SetModule(import.module);
  6592. moduleScope.AddImport(reimport);
  6593. reimport.SetScope(moduleScope);
  6594. ELSE
  6595. ASSERT(import.module # NIL);
  6596. reimport.SetModule(import.module); (* direct or indirect import *)
  6597. END;
  6598. END;
  6599. import := import.nextImport;
  6600. END;
  6601. END;
  6602. END;
  6603. currentScope := prevScope;
  6604. (* ELSE nothing to be done *)
  6605. x.SetState(SyntaxTree.Resolved);
  6606. END;
  6607. END VisitImport;
  6608. (*** statements ***)
  6609. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6610. VAR prev,resolved: SyntaxTree.Statement;
  6611. BEGIN
  6612. prev := resolvedStatement;
  6613. resolvedStatement := x;
  6614. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6615. activeCellsStatement := FALSE;
  6616. x.Accept(SELF);
  6617. (* removed this, implementation restriction should be resolved by backend
  6618. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6619. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6620. END;
  6621. *)
  6622. resolved := resolvedStatement;
  6623. resolvedStatement := prev;
  6624. RETURN resolved
  6625. END ResolveStatement;
  6626. (** check and resolve statement sequence
  6627. - check all statements, replace if necessary
  6628. **)
  6629. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6630. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6631. BEGIN
  6632. IF statementSequence # NIL THEN (* else empty *)
  6633. FOR i := 0 TO statementSequence.Length()-1 DO
  6634. statement := statementSequence.GetStatement(i);
  6635. resolved := ResolveStatement(statement);
  6636. IF (resolved # statement) THEN
  6637. statementSequence.SetStatement(i,resolved);
  6638. END;
  6639. END;
  6640. END;
  6641. END StatementSequence;
  6642. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6643. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6644. - check if procedure is callable
  6645. - check return type = NIL (otherwise must be assignment statement)
  6646. **)
  6647. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6648. VAR call: SyntaxTree.Designator;
  6649. BEGIN
  6650. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6651. call := procedureCall.call;
  6652. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6653. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6654. END;
  6655. call := ResolveDesignator(call);
  6656. IF call = SyntaxTree.invalidDesignator THEN
  6657. (* error already handled *)
  6658. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6659. (* inline call in a statement *)
  6660. ELSIF ~IsCallable(call) THEN
  6661. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6662. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6663. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6664. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6665. END;
  6666. procedureCall.SetCall(call);
  6667. (*
  6668. IF call = SyntaxTree.invalidDesignator THEN
  6669. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6670. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6671. IF IsOberonInline(procedure) THEN
  6672. Warning(procedure.position,"call to inline proc");
  6673. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6674. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6675. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6676. resolvedStatement := block;
  6677. RETURN;
  6678. END;
  6679. END;
  6680. *)
  6681. END VisitProcedureCallStatement;
  6682. (** check and resolve assignment LHS := RHS
  6683. - resolve LHS and RHS
  6684. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6685. - check if assignment is compatible
  6686. - check if LHS is variable (i.e. assignable)
  6687. - convert RHS if necessary
  6688. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6689. - assignment between different ASOTs
  6690. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6691. - assignment to ASOT elements:
  6692. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6693. **)
  6694. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6695. VAR
  6696. left: SyntaxTree.Designator;
  6697. right, expression: SyntaxTree.Expression;
  6698. designator: SyntaxTree.Designator;
  6699. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6700. mathArrayType: SyntaxTree.MathArrayType;
  6701. BEGIN
  6702. right := ResolveExpression(assignment.right);
  6703. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6704. left := ResolveDesignator(assignment.left);
  6705. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6706. (* error already handled *)
  6707. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6708. (* LHS is index write operator call on ASOT *)
  6709. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6710. (* necessary ?
  6711. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6712. type := procedureType.firstParameter.type;
  6713. expression := procedureCallDesignator.parameters.GetExpression(0);
  6714. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6715. *)
  6716. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6717. ELSIF CheckVariable(left) THEN
  6718. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6719. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6720. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6721. (* conversion done by procedure call
  6722. (* try to convert to left argument *)
  6723. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6724. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6725. procedureCallDesignator.parameters.SetExpression(1, right);
  6726. END;
  6727. *)
  6728. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6729. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6730. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6731. ELSIF AssignmentCompatible(left, right) THEN
  6732. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6733. mathArrayType := MathArrayStructureOfType(left.type);
  6734. right := NewConversion(right.position, right, mathArrayType, NIL);
  6735. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6736. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6737. ELSE
  6738. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6739. assignment.SetLeft(left);
  6740. assignment.SetRight(right);
  6741. resolvedStatement := assignment
  6742. END
  6743. END
  6744. END
  6745. END VisitAssignment;
  6746. (** check and resolve assignment LHS := RHS
  6747. - resolve LHS and RHS
  6748. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6749. - check if assignment is compatible
  6750. - check if LHS is variable (i.e. assignable)
  6751. - convert RHS if necessary
  6752. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6753. - assignment between different ASOTs
  6754. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6755. - assignment to ASOT elements:
  6756. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6757. **)
  6758. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6759. VAR
  6760. left: SyntaxTree.Designator;
  6761. right: SyntaxTree.Expression;
  6762. inPort, outPort: SyntaxTree.PortType;
  6763. BEGIN
  6764. right := ResolveExpression(communication.right);
  6765. left := ResolveDesignator(communication.left);
  6766. communication.SetLeft(left);
  6767. communication.SetRight(right);
  6768. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6769. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6770. (* error already handled *)
  6771. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6772. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6773. IF outPort.direction # SyntaxTree.OutPort THEN
  6774. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6775. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6776. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6777. ELSE
  6778. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6779. communication.SetRight(right)
  6780. END;
  6781. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6782. IF CheckVariable(left) THEN
  6783. IF inPort.direction # SyntaxTree.InPort THEN
  6784. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6785. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6786. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6787. END;
  6788. END;
  6789. ELSE
  6790. Error(communication.position, -1, "unsupported stream operation");
  6791. END;
  6792. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6793. IF outPort.direction # SyntaxTree.OutPort THEN
  6794. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6795. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6796. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6797. ELSE
  6798. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6799. communication.SetRight(right)
  6800. END;
  6801. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6802. IF CheckVariable(right) THEN
  6803. IF inPort.direction # SyntaxTree.InPort THEN
  6804. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6805. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6806. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6807. END;
  6808. END;
  6809. ELSE
  6810. Error(communication.position, -1, "unsupported operation");
  6811. END
  6812. END VisitCommunicationStatement;
  6813. (** check and resolve if/eslif part
  6814. - check condition
  6815. - check statement sequence
  6816. **)
  6817. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6818. VAR prevUnreachable, b: BOOLEAN;
  6819. BEGIN
  6820. prevUnreachable := currentIsUnreachable;
  6821. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6822. IF IsBooleanValue(ifPart.condition,b) THEN
  6823. IF b=FALSE THEN
  6824. currentIsUnreachable := TRUE
  6825. ELSIF b=TRUE THEN
  6826. true := TRUE
  6827. END;
  6828. END;
  6829. StatementSequence(ifPart.statements);
  6830. currentIsUnreachable := prevUnreachable;
  6831. END IfPart;
  6832. (** check and resolve if statement
  6833. - check if parts and else part statement sequence
  6834. **)
  6835. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6836. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6837. BEGIN
  6838. prevUnreachable := currentIsUnreachable;
  6839. ifPartTrue := FALSE;
  6840. IfPart(ifStatement.ifPart,ifPartTrue);
  6841. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6842. elsif := ifStatement.GetElsifPart(i);
  6843. IfPart(elsif,ifPartTrue);
  6844. END;
  6845. IF ifStatement.elsePart # NIL THEN
  6846. IF ifPartTrue THEN
  6847. currentIsUnreachable := TRUE
  6848. END;
  6849. StatementSequence(ifStatement.elsePart)
  6850. END;
  6851. currentIsUnreachable := prevUnreachable;
  6852. END VisitIfStatement;
  6853. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6854. VAR variable: SyntaxTree.Designator;
  6855. type,variableType: SyntaxTree.Type;
  6856. withEntry: WithEntry;
  6857. BEGIN
  6858. variable := ResolveDesignator(withPart.variable);
  6859. variableType := variable.type.resolved;
  6860. withPart.SetVariable(variable);
  6861. type := ResolveType(withPart.type);
  6862. withPart.SetType(type);
  6863. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6864. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6865. END;
  6866. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6867. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6868. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6869. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6870. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6871. IF VerboseErrorMessage THEN
  6872. Printout.Info("variable",variable)
  6873. END;
  6874. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6875. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6876. IF VerboseErrorMessage THEN
  6877. Printout.Info("variable",variable);
  6878. Printout.Info("type",type);
  6879. END;
  6880. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6881. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6882. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6883. IF VerboseErrorMessage THEN
  6884. Printout.Info("variable",variable);
  6885. Printout.Info("type",type);
  6886. END;
  6887. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6888. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6889. ELSE
  6890. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6891. NEW(withEntry);
  6892. withEntry.previous := withEntries;
  6893. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6894. withEntry.type := type;
  6895. withEntries := withEntry;
  6896. StatementSequence(withPart.statements);
  6897. withEntries := withEntries.previous;
  6898. END;
  6899. END WithPart;
  6900. (** check and resolve with statement WITH variable: type DO ... END;
  6901. - check type and variable
  6902. - check that variable type is type extension of type
  6903. - check that variable is a variable
  6904. - enter new with scope and enter guardedVariable with same name and reference to variable
  6905. - create if statement:
  6906. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6907. **)
  6908. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6909. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6910. BEGIN
  6911. prevScope := currentScope; symbol := NIL;
  6912. FOR i := 0 TO withStatement.WithParts()-1 DO
  6913. WithPart(withStatement.GetWithPart(i),symbol);
  6914. END;
  6915. IF withStatement.elsePart # NIL THEN
  6916. StatementSequence(withStatement.elsePart)
  6917. END;
  6918. currentScope := prevScope;
  6919. END VisitWithStatement;
  6920. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6921. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6922. - check 'first' < 'last' and no overlaps between different case labels
  6923. - check statement sequence
  6924. **)
  6925. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6926. VAR
  6927. i: LONGINT;
  6928. position: LONGINT;
  6929. expression, left, right: SyntaxTree.Expression;
  6930. expressionType: SyntaxTree.Type;
  6931. l, r: LONGINT;
  6932. cl, cr: CHAR;
  6933. thiscases: SyntaxTree.CaseConstant;
  6934. BEGIN
  6935. thiscases := NIL;
  6936. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6937. expression := casePart.elements.GetExpression(i);
  6938. position := expression.position;
  6939. (* set context of range *)
  6940. IF expression IS SyntaxTree.RangeExpression THEN
  6941. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6942. END;
  6943. expression := ResolveExpression(expression);
  6944. IF expression = SyntaxTree.invalidExpression THEN
  6945. (* error already reported *)
  6946. expressionType := SyntaxTree.invalidType;
  6947. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6948. (* read out 'first' and 'last' *)
  6949. left := expression(SyntaxTree.RangeExpression).first;
  6950. right := expression(SyntaxTree.RangeExpression).last;
  6951. (* guaranteed by VisitRangeExpression: *)
  6952. ASSERT((left # NIL) & (right # NIL));
  6953. ASSERT(left.type.resolved = right.type.resolved);
  6954. left := CompatibleConversion(left.position, left, type);
  6955. right := CompatibleConversion(right.position, right, type);
  6956. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6957. expression(SyntaxTree.RangeExpression).SetLast(right);
  6958. expressionType := RegularType(position,left.type);
  6959. ELSE
  6960. expression := ConstantExpression(expression);
  6961. expression := CompatibleConversion(expression.position, expression, type);
  6962. (*
  6963. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6964. left := Global.NewCharacterValue(system,expression.position,cl);
  6965. expression := casePart.elements.GetExpression(i);
  6966. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6967. expression := left
  6968. END;
  6969. *)
  6970. casePart.elements.SetExpression(i,expression);
  6971. left := expression; right := expression;
  6972. expressionType := RegularType(position,expression.type)
  6973. END;
  6974. IF (expressionType = SyntaxTree.invalidType) THEN
  6975. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6976. Error(position,Diagnostics.Invalid,"inadmissible case label");
  6977. expression := SyntaxTree.invalidExpression;
  6978. ELSE
  6979. l := 0; r := 0;
  6980. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6981. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6982. l := ORD(cl); r := ORD(cr);
  6983. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6984. ELSE
  6985. expression := SyntaxTree.invalidExpression
  6986. END;
  6987. IF expression # SyntaxTree.invalidExpression THEN
  6988. IF l>r THEN
  6989. Error(position,Diagnostics.Invalid,"empty case label")
  6990. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6991. Error(position,Diagnostics.Invalid,"duplicate case label");
  6992. ELSE
  6993. IF l < min THEN min := l END;
  6994. IF r > max THEN max := r END;
  6995. END;
  6996. END;
  6997. END;
  6998. casePart.elements.SetExpression(i,expression);
  6999. END;
  7000. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7001. casePart.SetConstants(thiscases);
  7002. StatementSequence(casePart.statements);
  7003. END CasePart;
  7004. (** check and resolve case statement CASE variable OF ... END;
  7005. - check variable
  7006. - check case parts
  7007. **)
  7008. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7009. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7010. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7011. BEGIN
  7012. expression := ResolveExpression(caseStatement.variable);
  7013. type := RegularType(expression.position,expression.type);
  7014. IF type = SyntaxTree.invalidType THEN
  7015. expression := SyntaxTree.invalidExpression;
  7016. ELSIF IsIntegerType(type) THEN
  7017. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7018. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7019. (*
  7020. expression := Global.NewCharacterValue(system,expression.position,ch);
  7021. *)
  7022. type := expression.type;
  7023. ELSIF IsCharacterType(type) THEN
  7024. ELSIF IsEnumerationType(type) THEN
  7025. ELSE
  7026. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7027. expression := SyntaxTree.invalidExpression;
  7028. END;
  7029. caseStatement.SetVariable(expression);
  7030. caseList := NIL;
  7031. min := MAX(LONGINT); max := MIN(LONGINT);
  7032. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7033. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7034. END;
  7035. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7036. msg := "huge sparse case table ";
  7037. Strings.AppendInt(msg, max-min);
  7038. Strings.Append(msg,"/");
  7039. Strings.AppendInt(msg, caseStatement.CaseParts());
  7040. Warning(caseStatement.position,msg);
  7041. END;
  7042. caseStatement.SetMinMax(min,max);
  7043. StatementSequence(caseStatement.elsePart);
  7044. IF expression.resolved # NIL THEN
  7045. IF IsCharacterValue(expression,ch) THEN
  7046. l := ORD(ch)
  7047. ELSIF IsIntegerValue(expression,l) THEN
  7048. END;
  7049. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7050. END;
  7051. END VisitCaseStatement;
  7052. (** check and resolve while statement
  7053. - check condition
  7054. - check statement sequence
  7055. **)
  7056. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7057. VAR prevIsUnreachable,b: BOOLEAN;
  7058. BEGIN
  7059. prevIsUnreachable := currentIsUnreachable;
  7060. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7061. IF IsBooleanValue(whileStatement.condition,b) THEN
  7062. IF b=FALSE THEN
  7063. currentIsUnreachable := TRUE
  7064. END;
  7065. END;
  7066. StatementSequence(whileStatement.statements);
  7067. currentIsUnreachable := prevIsUnreachable
  7068. END VisitWhileStatement;
  7069. (** check and resolve repeat statement
  7070. - check condition
  7071. - check statement sequence
  7072. **)
  7073. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7074. BEGIN
  7075. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7076. StatementSequence(repeatStatement.statements);
  7077. END VisitRepeatStatement;
  7078. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7079. VAR withEntry: WithEntry;
  7080. BEGIN
  7081. withEntry := withEntries;
  7082. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7083. withEntry := withEntry.previous
  7084. END;
  7085. IF withEntry = NIL THEN RETURN FALSE
  7086. ELSE
  7087. type := withEntry.type;
  7088. RETURN TRUE
  7089. END;
  7090. END GetGuard;
  7091. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7092. - check that variable is an integer variable
  7093. - check that from is integer typed with compatible type
  7094. - check that to has compatible type
  7095. - check that by is constant integer with compatible type
  7096. **)
  7097. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7098. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7099. BEGIN
  7100. designator := ResolveDesignator(forStatement.variable);
  7101. type := SyntaxTree.invalidType;
  7102. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7103. designator := SyntaxTree.invalidDesignator;
  7104. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7105. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7106. designator := SyntaxTree.invalidDesignator;
  7107. ELSIF CheckVariable(designator) THEN
  7108. type := designator.type;
  7109. END;
  7110. forStatement.SetVariable(designator);
  7111. expression := ResolveExpression(forStatement.from);
  7112. IF expression = SyntaxTree.invalidExpression THEN
  7113. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7114. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7115. expression := SyntaxTree.invalidExpression;
  7116. ELSIF type # SyntaxTree.invalidType THEN
  7117. expression := NewConversion(expression.position,expression,type,NIL)
  7118. END;
  7119. forStatement.SetFrom(expression);
  7120. expression := ResolveExpression(forStatement.to);
  7121. IF expression = SyntaxTree.invalidExpression THEN
  7122. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7123. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7124. expression := SyntaxTree.invalidExpression;
  7125. ELSIF type # SyntaxTree.invalidType THEN
  7126. expression := NewConversion(expression.position,expression,type,NIL)
  7127. END;
  7128. forStatement.SetTo(expression);
  7129. IF forStatement.by # NIL THEN
  7130. expression := ConstantInteger(forStatement.by);
  7131. ELSE
  7132. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7133. END;
  7134. IF expression = SyntaxTree.invalidExpression THEN
  7135. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7136. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7137. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7138. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7139. ELSIF type # SyntaxTree.invalidType THEN
  7140. expression := NewConversion(expression.position,expression,type,NIL)
  7141. END;
  7142. forStatement.SetBy(expression);
  7143. StatementSequence(forStatement.statements);
  7144. END VisitForStatement;
  7145. (** check and resolve loop statement LOOP StatementSequence END
  7146. - check statement sequence
  7147. **)
  7148. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7149. BEGIN
  7150. StatementSequence(loopStatement.statements)
  7151. END VisitLoopStatement;
  7152. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7153. BEGIN
  7154. StatementSequence(exitableBlock.statements);
  7155. END VisitExitableBlock;
  7156. (** check and resolve exit statement EXIT
  7157. - check that exit is within LOOP statement block
  7158. **)
  7159. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7160. VAR outer: SyntaxTree.Statement;
  7161. BEGIN
  7162. outer := exitStatement.outer;
  7163. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7164. outer := outer.outer;
  7165. END;
  7166. IF outer = NIL THEN
  7167. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7168. END;
  7169. END VisitExitStatement;
  7170. (** check and resolve return statement RETURN [expression]
  7171. - check expression (if any)
  7172. - check if in procedure scope
  7173. - if in procedure scope then check expression compatibility
  7174. - if not in procecdure scope then check on return without expression
  7175. **)
  7176. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7177. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7178. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7179. BEGIN
  7180. position := returnStatement.position;
  7181. expression := returnStatement.returnValue;
  7182. IF expression # NIL THEN
  7183. expression := ResolveExpression(expression);
  7184. returnStatement.SetReturnValue(expression);
  7185. END;
  7186. outer := returnStatement.outer;
  7187. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7188. outer := outer.outer
  7189. END;
  7190. IF (outer # NIL) THEN
  7191. scope := outer(SyntaxTree.Body).inScope;
  7192. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7193. IF (expression # NIL) THEN
  7194. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7195. END;
  7196. ELSE
  7197. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7198. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7199. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7200. END;
  7201. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7202. IF returnType # NIL THEN
  7203. returnType := returnType.resolved;
  7204. IF expression = NIL THEN
  7205. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7206. ELSIF expression.type = NIL THEN
  7207. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7208. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7209. Error(position,Diagnostics.Invalid,"return type not compatible");
  7210. IF VerboseErrorMessage THEN
  7211. Printout.Info("returnType",returnType);
  7212. Printout.Info("expression",expression);
  7213. END;
  7214. ELSE
  7215. expression := NewConversion(expression.position,expression,returnType,NIL);
  7216. returnStatement.SetReturnValue(expression);
  7217. END;
  7218. ELSIF expression # NIL THEN
  7219. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7220. END;
  7221. END;
  7222. END;
  7223. END VisitReturnStatement;
  7224. (** check and resolve await statement AWAIT(condition: Expression)
  7225. - check await condition
  7226. **)
  7227. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7228. VAR condition: SyntaxTree.Expression;
  7229. BEGIN
  7230. condition := ResolveCondition(awaitStatement.condition);
  7231. IF currentIsRealtime THEN
  7232. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7233. END;
  7234. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7235. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7236. END;
  7237. awaitStatement.SetCondition(condition);
  7238. END VisitAwaitStatement;
  7239. PROCEDURE CheckSystemImport(position: LONGINT);
  7240. VAR import: SyntaxTree.Import;
  7241. BEGIN
  7242. import := currentScope.ownerModule.moduleScope.firstImport;
  7243. WHILE(import # NIL) DO
  7244. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7245. RETURN;
  7246. END;
  7247. import := import.nextImport;
  7248. END;
  7249. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7250. END CheckSystemImport;
  7251. (** check and resolve code statement: do nothing, must be done by assembler
  7252. **)
  7253. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7254. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7255. BEGIN
  7256. CheckSystemImport(code.position);
  7257. FOR i := 0 TO code.inRules.Length()-1 DO
  7258. statement := code.inRules.GetStatement(i);
  7259. IF statement IS SyntaxTree.Assignment THEN
  7260. WITH statement: SyntaxTree.Assignment DO
  7261. statement.SetRight(ResolveExpression(statement.right));
  7262. END;
  7263. ELSE
  7264. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7265. END;
  7266. END;
  7267. FOR i := 0 TO code.outRules.Length()-1 DO
  7268. statement := code.outRules.GetStatement(i);
  7269. IF statement IS SyntaxTree.Assignment THEN
  7270. WITH statement: SyntaxTree.Assignment DO
  7271. statement.SetLeft(ResolveDesignator(statement.left));
  7272. END;
  7273. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7274. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7275. ELSE
  7276. Printout.Info("out statement ", statement);
  7277. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7278. END;
  7279. END;
  7280. END VisitCode;
  7281. (** check and set flags of a statement block
  7282. - check for multiply occurence of a flag
  7283. - check and set priority only in bodies
  7284. - check for valid names
  7285. **)
  7286. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7287. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7288. flag: LONGINT; recordBody: SyntaxTree.Body;
  7289. PROCEDURE SetProtectedRecord;
  7290. VAR scope: SyntaxTree.Scope;
  7291. BEGIN
  7292. scope := currentScope;
  7293. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7294. scope := scope.outerScope
  7295. END;
  7296. IF scope # NIL THEN
  7297. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7298. END;
  7299. END SetProtectedRecord;
  7300. BEGIN
  7301. flags := {};
  7302. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7303. recordBody := block(SyntaxTree.Body)
  7304. ELSE
  7305. recordBody := NIL
  7306. END;
  7307. blockModifier := block.blockModifiers;
  7308. WHILE(blockModifier # NIL) DO
  7309. name := blockModifier.identifier;
  7310. expression := blockModifier.expression;
  7311. position := blockModifier.position;
  7312. flag := -1;
  7313. IF name=Global.NamePriority THEN
  7314. IF expression = NIL THEN
  7315. Error(position,Diagnostics.Invalid,"missing priority expression");
  7316. ELSIF recordBody = NIL THEN
  7317. Error(position,Diagnostics.Invalid,"priority not on record body");
  7318. ELSIF recordBody.priority # NIL THEN
  7319. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7320. ELSE
  7321. recordBody.SetPriority(expression);
  7322. END;
  7323. ELSIF expression # NIL THEN
  7324. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7325. ELSIF name=Global.NameExclusive THEN
  7326. IF block.isExclusive THEN
  7327. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7328. END;
  7329. block.SetExclusive(TRUE); SetProtectedRecord;
  7330. ELSIF name=Global.NameActive THEN
  7331. IF recordBody = NIL THEN
  7332. Error(position,Diagnostics.Invalid,"active not in record body");
  7333. ELSIF recordBody.isActive THEN
  7334. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7335. ELSE
  7336. recordBody.SetActive(TRUE); SetProtectedRecord;
  7337. END;
  7338. ELSIF name=Global.NameSafe THEN
  7339. IF recordBody = NIL THEN
  7340. Error(position,Diagnostics.Invalid,"safe not in record body");
  7341. ELSIF recordBody.isSafe THEN
  7342. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7343. ELSE
  7344. recordBody.SetSafe(TRUE);
  7345. SetProtectedRecord;
  7346. END;
  7347. ELSIF name=Global.NameRealtime THEN
  7348. IF recordBody = NIL THEN
  7349. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7350. ELSIF recordBody.isRealtime THEN
  7351. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7352. ELSE
  7353. recordBody.SetRealtime(TRUE);
  7354. block.SetRealtime(TRUE);
  7355. END;
  7356. ELSIF name=Global.NameUnchecked THEN
  7357. IF block.isUnchecked THEN
  7358. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7359. ELSE
  7360. block.SetUnchecked(TRUE);
  7361. END;
  7362. ELSIF (name=Global.NameUncooperative) THEN
  7363. IF block.isUncooperative THEN
  7364. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7365. ELSE
  7366. block.SetUncooperative(TRUE);
  7367. END;
  7368. ELSE
  7369. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7370. END;
  7371. blockModifier := blockModifier.nextModifier;
  7372. END;
  7373. END BlockFlags;
  7374. (** check and resolve statement block
  7375. - check flags (exclusive)
  7376. - check statement sequence
  7377. **)
  7378. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7379. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7380. BEGIN
  7381. BlockFlags(statementBlock);
  7382. IF statementBlock.isExclusive THEN
  7383. (* check that not in exclusive block *)
  7384. IF currentIsExclusive THEN
  7385. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7386. ELSIF currentIsRealtime THEN
  7387. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7388. END;
  7389. END;
  7390. recentExclusive := currentIsExclusive;
  7391. recentUnreachable := currentIsUnreachable;
  7392. recentRealtime := currentIsRealtime;
  7393. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7394. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7395. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7396. StatementSequence(statementBlock.statements);
  7397. currentIsRealtime := recentRealtime;
  7398. currentIsExclusive := recentExclusive;
  7399. currentIsUnreachable := recentUnreachable;
  7400. END VisitStatementBlock;
  7401. (** check and resolve body
  7402. - check flags (active, priority, safe)
  7403. - check body and finally part
  7404. **)
  7405. PROCEDURE Body(body: SyntaxTree.Body);
  7406. BEGIN
  7407. VisitStatementBlock(body);
  7408. IF body.isActive THEN
  7409. IF ~currentIsBodyProcedure THEN
  7410. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7411. ELSIF body.priority # NIL THEN
  7412. body.SetPriority(ConstantInteger(body.priority));
  7413. END;
  7414. ELSIF body.isSafe THEN
  7415. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7416. ELSIF body.priority # NIL THEN
  7417. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7418. END;
  7419. IF body.code # NIL THEN
  7420. CheckSystemImport(body.position);
  7421. END;
  7422. StatementSequence(body.finally)
  7423. END Body;
  7424. (*** scopes ***)
  7425. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7426. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7427. VAR duplicateSymbol: BOOLEAN;
  7428. BEGIN
  7429. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7430. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7431. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7432. END;
  7433. scope.EnterSymbol(symbol,duplicateSymbol);
  7434. IF ~allowDuplicate & duplicateSymbol THEN
  7435. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7436. IF VerboseErrorMessage THEN
  7437. Printout.Info("multiply defined identifier",symbol);
  7438. Printout.Info("in scope",scope);
  7439. END;
  7440. END;
  7441. END Register;
  7442. (**
  7443. implementation: check and resolve an implementation part
  7444. **)
  7445. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7446. move implementation checker to a separate object ? *)
  7447. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7448. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7449. BEGIN
  7450. prevIsRealtime := currentIsRealtime;
  7451. prevIsBodyProcedure := currentIsBodyProcedure;
  7452. prevIsCellNet := currentIsCellNet;
  7453. prevScope := currentScope;
  7454. currentScope := scope;
  7455. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7456. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7457. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7458. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7459. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7460. (*
  7461. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7462. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7463. END;
  7464. *)
  7465. END;
  7466. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7467. (* module body, record bodies are wrapped into an artifical procedure *)
  7468. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7469. Body(scope(SyntaxTree.ProcedureScope).body)
  7470. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7471. Body(scope(SyntaxTree.ProcedureScope).body)
  7472. END;
  7473. END;
  7474. currentScope := prevScope;
  7475. currentIsRealtime := prevIsRealtime;
  7476. currentIsBodyProcedure := prevIsBodyProcedure;
  7477. currentIsCellNet := prevIsCellNet;
  7478. END Implementation;
  7479. (** implementation phase:
  7480. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7481. **)
  7482. PROCEDURE Implementations(x: SyntaxTree.Module);
  7483. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7484. BEGIN
  7485. prevPhase := phase;
  7486. phase := InlinePhase;
  7487. scope := x.firstScope;
  7488. WHILE(scope # NIL) DO
  7489. Implementation(scope);
  7490. scope := scope.nextScope;
  7491. END;
  7492. phase := ImplementationPhase;
  7493. scope := x.firstScope;
  7494. WHILE(scope # NIL) DO
  7495. Implementation(scope);
  7496. scope := scope.nextScope;
  7497. END;
  7498. phase := prevPhase;
  7499. END Implementations;
  7500. (** declaration phase:
  7501. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7502. - import lists (for module scopes)
  7503. - parameter list (for procedure scopes)
  7504. - constant declarations
  7505. - type declarations
  7506. - variable declarations
  7507. - procedure declarations
  7508. preformed in two stages:
  7509. - first all symbols are entered into the symbol table (with uniqueness check),
  7510. - then all symbols are resolved
  7511. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7512. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7513. **)
  7514. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7515. VAR
  7516. constant: SyntaxTree.Constant;
  7517. typeDeclaration: SyntaxTree.TypeDeclaration;
  7518. variable: SyntaxTree.Variable;
  7519. procedure: SyntaxTree.Procedure;
  7520. prevScope: SyntaxTree.Scope;
  7521. parameter: SyntaxTree.Parameter;
  7522. import: SyntaxTree.Import;
  7523. symbol: SyntaxTree.Symbol;
  7524. prevPhase: LONGINT;
  7525. prevError : BOOLEAN;
  7526. type: SyntaxTree.Type;
  7527. atype : SyntaxTree.ArrayType;
  7528. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7529. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7530. BEGIN
  7531. IF type.baseType # NIL THEN
  7532. baseType := type.baseType.resolved;
  7533. IF baseType IS SyntaxTree.PointerType THEN
  7534. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7535. END;
  7536. IF baseType IS SyntaxTree.CellType THEN
  7537. DeclareCell(baseType(SyntaxTree.CellType));
  7538. END;
  7539. END;
  7540. parameter := type.firstParameter;
  7541. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7542. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7543. variable.SetType(parameter.type);
  7544. variable.SetAccess(SyntaxTree.Hidden);
  7545. variable.SetModifiers(parameter.modifiers);
  7546. currentScope.PushVariable(variable);
  7547. (*
  7548. Register(parameter,scope, FALSE);
  7549. *)
  7550. parameter := parameter.nextParameter;
  7551. END;
  7552. property := type.firstProperty;
  7553. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7554. variable := currentScope.FindVariable(property.name);
  7555. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7556. prop := variable(SyntaxTree.Property);
  7557. ELSE (* add, duplicate symbols detection later *)
  7558. prop := SyntaxTree.NewProperty(property.position, property.name);
  7559. currentScope.PushVariable(prop);
  7560. END;
  7561. prop.SetType(property.type);
  7562. prop.SetValue(property.value);
  7563. prop.SetAccess(SyntaxTree.Hidden);
  7564. property := property.nextProperty;
  7565. END;
  7566. END DeclareCell;
  7567. BEGIN
  7568. prevError := error;
  7569. prevPhase := phase;
  7570. phase := DeclarationPhase;
  7571. prevScope := currentScope;
  7572. currentScope := scope;
  7573. error := FALSE;
  7574. (* first enter all symbols in scope *)
  7575. IF scope IS SyntaxTree.ModuleScope THEN
  7576. (* treat imports first for a module scope, , set default context if necessary *)
  7577. import := scope(SyntaxTree.ModuleScope).firstImport;
  7578. WHILE(import # NIL) DO
  7579. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7580. Register(import, currentScope, FALSE);
  7581. import := import.nextImport;
  7582. END;
  7583. import := scope(SyntaxTree.ModuleScope).firstImport;
  7584. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7585. ResolveSymbol(import);
  7586. import := import.nextImport;
  7587. END;
  7588. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7589. (* enter parameters for a procedure scope *)
  7590. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7591. WHILE(parameter # NIL) DO
  7592. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7593. END;
  7594. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7595. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7596. ELSIF scope IS SyntaxTree.CellScope THEN
  7597. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7598. END;
  7599. IF error THEN RETURN END;
  7600. (* constants *)
  7601. constant := scope.firstConstant;
  7602. WHILE (constant # NIL) DO
  7603. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7604. END;
  7605. (* type declarations *)
  7606. typeDeclaration := scope.firstTypeDeclaration;
  7607. WHILE (typeDeclaration # NIL) DO
  7608. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7609. END;
  7610. (* variables *)
  7611. variable := scope.firstVariable;
  7612. WHILE (variable # NIL) DO
  7613. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7614. END;
  7615. (* procedures *)
  7616. procedure := scope.firstProcedure;
  7617. WHILE (procedure # NIL) DO
  7618. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7619. END;
  7620. (* now process all symbols without any presumption on the order *)
  7621. symbol := scope.firstSymbol;
  7622. WHILE(symbol # NIL) DO
  7623. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7624. ResolveSymbol(symbol);
  7625. END;
  7626. symbol := symbol.nextSymbol;
  7627. END;
  7628. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7629. symbol := scope.firstSymbol;
  7630. WHILE symbol # NIL DO
  7631. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7632. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7633. pointerFixes.Add(symbol, currentScope);
  7634. END;
  7635. IF ~symbol.type.resolved.isRealtime THEN
  7636. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7637. END;
  7638. END;
  7639. symbol := symbol.nextSymbol
  7640. END;
  7641. END;
  7642. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7643. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7644. END;
  7645. IF scope.ownerModule # NIL THEN
  7646. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7647. scope.ownerModule.AddScope(scope);
  7648. END;
  7649. phase := prevPhase;
  7650. currentScope := prevScope;
  7651. error := error OR prevError;
  7652. END Declarations;
  7653. (* nopov *)
  7654. (** check if all operators from one module are compatible to the ones in the other module
  7655. - check if there are not multiple operators with the same signature
  7656. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7657. - check for all operators whose signatures are compatible, whether the return types are compatible
  7658. note that:
  7659. - the return type is not considered to be part of the signature
  7660. - two signatures are considered compatible, if all of the operands are compatible
  7661. **)
  7662. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7663. VAR
  7664. thisOperator, thatOperator: SyntaxTree.Operator;
  7665. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7666. thisParameter, thatParameter: SyntaxTree.Parameter;
  7667. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7668. i: LONGINT;
  7669. BEGIN
  7670. currentScope := thisModuleScope;
  7671. hasError := FALSE;
  7672. (* go through all operators in the other module *)
  7673. thatOperator := thatModuleScope.firstOperator;
  7674. WHILE (thatOperator # NIL) & ~hasError DO
  7675. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7676. (* the other operator is accessible *)
  7677. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7678. (* the other operator is not the conversion operator *)
  7679. (* go through all operators in this module *)
  7680. thisOperator := thisModuleScope.firstOperator;
  7681. WHILE (thisOperator # NIL) & ~hasError DO
  7682. IF thisOperator # thatOperator THEN
  7683. (* the operators are not the same *)
  7684. IF thisOperator.name = thatOperator.name THEN
  7685. (* the operators share the same identifier *)
  7686. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7687. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7688. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7689. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7690. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7691. (* both operators have the same paramter count *)
  7692. thisParameter := thisProcedureType.firstParameter;
  7693. thatParameter := thatProcedureType.firstParameter;
  7694. operandsAreEqual := TRUE;
  7695. operandsAreCompatible := TRUE;
  7696. (* go through all parameters *)
  7697. FOR i := 1 TO thisProcedureType.numberParameters DO
  7698. ASSERT(thatParameter # NIL);
  7699. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7700. operandsAreEqual := FALSE;
  7701. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7702. operandsAreCompatible := FALSE
  7703. END
  7704. END;
  7705. thisParameter := thisParameter.nextParameter;
  7706. thatParameter := thatParameter.nextParameter
  7707. END;
  7708. IF operandsAreEqual THEN
  7709. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7710. hasError := TRUE
  7711. ELSIF operandsAreCompatible THEN
  7712. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7713. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7714. hasError := TRUE
  7715. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7716. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7717. hasError := TRUE
  7718. END
  7719. END
  7720. END
  7721. END
  7722. END;
  7723. thisOperator := thisOperator.nextOperator
  7724. END
  7725. END
  7726. END;
  7727. thatOperator := thatOperator.nextOperator
  7728. END
  7729. END CheckInterOperatorConformity;
  7730. (** check module:
  7731. - check module declaration
  7732. - add context, if necessary
  7733. - remove module from import cache, if necessary
  7734. - check declarations
  7735. - resolve all type fixes
  7736. - check implementation (bodies)
  7737. **)
  7738. PROCEDURE Module*(x: SyntaxTree.Module);
  7739. VAR (* nopov *)
  7740. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7741. BEGIN
  7742. prevScope := currentScope;
  7743. prevIsCellNet := currentIsCellNet;
  7744. module := x;
  7745. ASSERT(x # NIL);
  7746. global := system.globalScope[x.case];
  7747. x.moduleScope.SetGlobalScope(global);
  7748. currentScope := global;
  7749. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7750. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7751. RemoveModuleFromCache(importCache,x);
  7752. Declarations(x.moduleScope);
  7753. FixTypes();
  7754. IF module.isCellNet THEN
  7755. currentIsCellNet := TRUE;
  7756. modifier := x.modifiers;
  7757. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7758. CheckModifiers(modifier, FALSE);
  7759. END;
  7760. (* nopov *)
  7761. IF ~error THEN
  7762. (* check if operators conform to each other within this module *)
  7763. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7764. (* go through all imports *)
  7765. import := x.moduleScope.firstImport;
  7766. WHILE import # NIL DO
  7767. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7768. (* check if all operators in this module conform to the ones of the imported module *)
  7769. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7770. END;
  7771. import := import.nextImport
  7772. END;
  7773. END;
  7774. Implementations(x);
  7775. module := NIL;
  7776. currentIsCellNet := prevIsCellNet;
  7777. currentScope := prevScope;
  7778. END Module;
  7779. END Checker;
  7780. Warnings*=OBJECT (SyntaxTree.Visitor)
  7781. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7782. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7783. BEGIN
  7784. SELF.diagnostics := diagnostics
  7785. END InitWarnings;
  7786. (** types *)
  7787. PROCEDURE Type(x: SyntaxTree.Type);
  7788. BEGIN x.Accept(SELF)
  7789. END Type;
  7790. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7791. BEGIN END VisitType;
  7792. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7793. BEGIN END VisitBasicType;
  7794. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7795. BEGIN END VisitCharacterType;
  7796. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7797. BEGIN END VisitIntegerType;
  7798. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7799. BEGIN END VisitFloatType;
  7800. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7801. BEGIN END VisitQualifiedType;
  7802. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7803. BEGIN END VisitStringType;
  7804. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7805. BEGIN END VisitEnumerationType;
  7806. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7807. BEGIN END VisitRangeType;
  7808. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7809. BEGIN
  7810. IF ~(SyntaxTree.Warned IN x.state) THEN
  7811. x.SetState(SyntaxTree.Warned);
  7812. Type(x.arrayBase);
  7813. END;
  7814. END VisitArrayType;
  7815. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7816. BEGIN
  7817. IF ~(SyntaxTree.Warned IN x.state) THEN
  7818. x.SetState(SyntaxTree.Warned);
  7819. Type(x.arrayBase);
  7820. END;
  7821. END VisitMathArrayType;
  7822. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7823. BEGIN
  7824. IF ~(SyntaxTree.Warned IN x.state) THEN
  7825. x.SetState(SyntaxTree.Warned);
  7826. Type(x.pointerBase);
  7827. END;
  7828. END VisitPointerType;
  7829. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7830. BEGIN Scope(x.recordScope) END VisitRecordType;
  7831. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7832. BEGIN Scope(x.cellScope) END VisitCellType;
  7833. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7834. BEGIN END VisitProcedureType;
  7835. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7836. VAR msg: ARRAY 256 OF CHAR;
  7837. BEGIN
  7838. Global.GetSymbolName(x,msg);
  7839. Strings.Append(msg," ");
  7840. Strings.Append(msg,text);
  7841. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7842. END Warning;
  7843. (** symbols *)
  7844. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7845. BEGIN
  7846. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7847. IF ~(x IS SyntaxTree.Parameter) THEN
  7848. Warning(x,"never used");
  7849. END;
  7850. END;
  7851. x.Accept(SELF);
  7852. END Symbol;
  7853. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7854. BEGIN END VisitSymbol;
  7855. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7856. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7857. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7858. BEGIN END VisitConstant;
  7859. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7860. BEGIN END VisitVariable;
  7861. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7862. BEGIN END VisitProperty;
  7863. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7864. BEGIN END VisitParameter;
  7865. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7866. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7867. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7868. BEGIN END VisitOperator;
  7869. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7870. BEGIN END VisitImport;
  7871. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7872. VAR
  7873. symbol: SyntaxTree.Symbol;
  7874. BEGIN
  7875. symbol := scope.firstSymbol;
  7876. WHILE(symbol # NIL) DO
  7877. Symbol(symbol);
  7878. symbol := symbol.nextSymbol;
  7879. END;
  7880. END Scope;
  7881. PROCEDURE Module*(x: SyntaxTree.Module);
  7882. BEGIN
  7883. SELF.module := x;
  7884. Scope(x.moduleScope);
  7885. END Module;
  7886. END Warnings;
  7887. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7888. BEGIN
  7889. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7890. END IsOberonInline;
  7891. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7892. BEGIN
  7893. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7894. END Resolved;
  7895. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7896. VAR i: LONGINT;
  7897. BEGIN
  7898. i := 1;
  7899. WHILE i < x DO
  7900. i := i *2
  7901. END;
  7902. RETURN i=x
  7903. END PowerOf2;
  7904. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7905. BEGIN
  7906. RETURN
  7907. (scope # NIL) &
  7908. (scope IS SyntaxTree.ModuleScope)
  7909. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7910. OR
  7911. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7912. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7913. END IsCellNetScope;
  7914. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7915. BEGIN
  7916. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7917. END IsCellScope;
  7918. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7919. BEGIN
  7920. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7921. RETURN (scope # NIL) & IsCellNetScope(scope)
  7922. END InCellNetScope;
  7923. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7924. BEGIN
  7925. ASSERT(size MOD system.dataUnit = 0);
  7926. RETURN size DIV system.dataUnit
  7927. END ToMemoryUnits;
  7928. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7929. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7930. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7931. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7932. BEGIN
  7933. IF t = NIL THEN
  7934. RETURN TRUE
  7935. ELSE
  7936. t := t.resolved;
  7937. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7938. END;
  7939. END TypeAllowed;
  7940. BEGIN
  7941. type := type.resolved;
  7942. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7943. RETURN FALSE
  7944. ELSE
  7945. procedureType := type(SyntaxTree.ProcedureType);
  7946. numberParameters := procedureType.numberParameters;
  7947. RETURN
  7948. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7949. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7950. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7951. END;
  7952. END GetProcedureAllowed;
  7953. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7954. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7955. VAR import: SyntaxTree.Import;
  7956. BEGIN
  7957. import := importCache.ImportByModuleName(x.name,x.context);
  7958. IF import # NIL THEN
  7959. importCache.RemoveImporters(x.name,x.context);
  7960. END;
  7961. END RemoveModuleFromCache;
  7962. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7963. (* to <- this assignment compatibility *)
  7964. VAR result: BOOLEAN;
  7965. BEGIN
  7966. IF this= NIL THEN result := (to=NIL)
  7967. ELSIF to=NIL THEN result := FALSE
  7968. ELSE
  7969. (*! will be replaced by this:
  7970. ELSE result := this.CompatibleTo(to.resolved);
  7971. *)
  7972. this := this.resolved; to := to.resolved;
  7973. IF to=SyntaxTree.invalidType THEN result := FALSE
  7974. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7975. ELSIF to = this THEN
  7976. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7977. ELSIF to IS SyntaxTree.BasicType THEN
  7978. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7979. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7980. result := this.CompatibleTo(to.resolved)
  7981. ELSE
  7982. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7983. END
  7984. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7985. result := to.sizeInBits = this.sizeInBits;
  7986. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7987. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7988. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7989. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7990. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7991. result := TRUE;
  7992. ELSIF to IS SyntaxTree.AnyType THEN
  7993. 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);
  7994. ELSIF to IS SyntaxTree.ObjectType THEN
  7995. 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 *) ;
  7996. ELSIF to IS SyntaxTree.ByteType THEN
  7997. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7998. ELSIF to IS SyntaxTree.CharacterType THEN
  7999. result := IsCharacterType(this)
  8000. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8001. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8002. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  8003. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8004. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8005. result := TRUE;
  8006. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8007. result := TRUE;
  8008. ELSE
  8009. result := FALSE
  8010. END;
  8011. ELSIF to IS SyntaxTree.PointerType THEN
  8012. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8013. IsPointerType(this) & (IsTypeExtension(to,this) OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8014. & (~to.isRealtime OR this.isRealtime);
  8015. ELSIF to IS SyntaxTree.ProcedureType THEN
  8016. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8017. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8018. & (~to.isRealtime OR this.isRealtime)
  8019. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8020. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8021. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8022. ELSIF to IS SyntaxTree.RecordType THEN
  8023. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8024. ELSIF to IS SyntaxTree.ArrayType THEN
  8025. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8026. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8027. ELSIF StaticArrayCompatible(to, this) THEN
  8028. result := TRUE
  8029. ELSE
  8030. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8031. END;
  8032. ELSIF to IS SyntaxTree.MathArrayType THEN
  8033. IF this IS SyntaxTree.MathArrayType THEN
  8034. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8035. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8036. result := TRUE;
  8037. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8038. result := TRUE;
  8039. ELSE
  8040. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8041. END;
  8042. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8043. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8044. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8045. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8046. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8047. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8048. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8049. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8050. ELSE
  8051. result := FALSE
  8052. END;
  8053. (* an array-structured object type is compatible to the type of its array structure *)
  8054. ELSIF IsArrayStructuredObjectType(this) THEN
  8055. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8056. ELSE
  8057. result := FALSE;
  8058. END;
  8059. ELSIF to IS SyntaxTree.StringType THEN
  8060. result := FALSE;
  8061. ELSIF to IS SyntaxTree.EnumerationType THEN
  8062. result := IsEnumerationExtension(this,to);
  8063. ELSIF to IS SyntaxTree.PortType THEN
  8064. result := SameType(to, this)
  8065. ELSE
  8066. Printout.Info("CompatibleTo",to);
  8067. HALT(100); (* implement missing type check *)
  8068. END;
  8069. END;
  8070. RETURN result
  8071. END CompatibleTo;
  8072. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8073. VAR actualBase, formalBase: SyntaxTree.Type;
  8074. BEGIN
  8075. IF SameType(formal,actual) THEN
  8076. RETURN TRUE
  8077. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8078. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8079. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8080. RETURN
  8081. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8082. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8083. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8084. & StaticArrayCompatible(formalBase,actualBase)
  8085. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8086. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8087. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8088. RETURN
  8089. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8090. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8091. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8092. & StaticArrayCompatible(formalBase,actualBase)
  8093. ELSE RETURN FALSE
  8094. END;
  8095. END StaticArrayCompatible;
  8096. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8097. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8098. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8099. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8100. BEGIN
  8101. result := SameType(formal,actual);
  8102. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8103. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8104. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8105. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8106. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8107. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8108. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8109. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8110. & TC(formalBase, actualBase);
  8111. END;
  8112. RETURN result
  8113. END TC;
  8114. BEGIN
  8115. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8116. ELSE
  8117. arrayBase := formalType.arrayBase.resolved;
  8118. IF (actualType IS SyntaxTree.StringType) THEN
  8119. result := arrayBase IS SyntaxTree.CharacterType
  8120. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8121. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8122. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8123. result := TC(formalType, actualType);
  8124. ELSE
  8125. result := (arrayBase IS SyntaxTree.ByteType)
  8126. END;
  8127. END;
  8128. RETURN result
  8129. END OpenArrayCompatible;
  8130. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8131. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8132. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8133. BEGIN
  8134. IF actualType IS SyntaxTree.MathArrayType THEN
  8135. actualArray := actualType(SyntaxTree.MathArrayType);
  8136. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8137. (*
  8138. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8139. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8140. *)
  8141. actualBase := ArrayBase(actualType,Infinity);
  8142. formalBase := ArrayBase(formalType,Infinity);
  8143. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8144. ELSE
  8145. (*
  8146. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8147. *)
  8148. formalBase := Resolved(formalType.arrayBase);
  8149. actualBase := Resolved(actualArray.arrayBase);
  8150. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8151. (*
  8152. ARRAY [k] -> ARRAY [n]
  8153. *)
  8154. result := (formalType.staticLength = actualArray.staticLength)
  8155. ELSE
  8156. result := TRUE
  8157. END;
  8158. IF ~result THEN
  8159. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8160. ELSIF actualBase = NIL THEN result := FALSE
  8161. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8162. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8163. ELSE
  8164. result := SameType(formalBase,actualBase)
  8165. END;
  8166. END;
  8167. ELSE
  8168. result := FALSE
  8169. END;
  8170. RETURN result
  8171. END MathArrayCompatible;
  8172. (**
  8173. Math Array Type distance for assignments / parameter passings of the form
  8174. from -> to
  8175. variants:
  8176. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8177. allowed:
  8178. static -> static (& size match)
  8179. static -> open
  8180. static -> tensor
  8181. open -> open
  8182. open -> tensor
  8183. open -> static
  8184. tensor -> tensor
  8185. tensor -> open
  8186. tensor -> static
  8187. **)
  8188. (*! think about the metric here: is form matching more important than element type matching? *)
  8189. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8190. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8191. BEGIN
  8192. fromBase := Resolved(from.arrayBase);
  8193. toBase := Resolved(to.arrayBase);
  8194. i := Infinity;
  8195. IF from = to THEN
  8196. i := 0;
  8197. ELSIF (from.form = to.form) THEN
  8198. (* static -> static, open -> open, tensor -> tensor *)
  8199. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8200. IF fromBase = toBase THEN i := 0
  8201. ELSIF toBase = NIL THEN i := 1
  8202. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8203. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8204. ELSE
  8205. i := TypeDistance(system,fromBase, toBase, varpar);
  8206. END;
  8207. END;
  8208. ELSIF (to.form = SyntaxTree.Static) THEN
  8209. (* forbidden *)
  8210. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8211. (* static -> tensor, open -> tensor, tensor -> open *)
  8212. IF toBase=fromBase THEN i := 0;
  8213. ELSIF toBase = NIL THEN i := 1;
  8214. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8215. toBase := ArrayBase(toBase,Infinity);
  8216. IF (fromBase=toBase) THEN i := 0
  8217. ELSIF (toBase = NIL) THEN i:= 1
  8218. ELSIF (fromBase = NIL) THEN i := Infinity;
  8219. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8220. END;
  8221. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8222. fromBase := ArrayBase(fromBase,Infinity);
  8223. IF (fromBase=toBase) THEN i := 0
  8224. ELSIF (toBase = NIL) THEN i := 1
  8225. ELSIF (fromBase = NIL) THEN i := Infinity;
  8226. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8227. END;
  8228. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8229. END;
  8230. IF i # Infinity THEN INC(i,2) END;
  8231. ELSIF (from.form = SyntaxTree.Static) THEN
  8232. (* static -> open *)
  8233. IF toBase=fromBase THEN i := 0
  8234. ELSIF toBase = NIL THEN i := 1
  8235. ELSIF fromBase = NIL THEN i := Infinity
  8236. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8237. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8238. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8239. END;
  8240. IF i # Infinity THEN INC(i,1) END;
  8241. ELSE HALT(100); (* unknown case *)
  8242. END;
  8243. RETURN i;
  8244. END MathArrayTypeDistance;
  8245. (** compute and return the distance of two array types
  8246. - return the distance of the base types
  8247. **)
  8248. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8249. VAR i: LONGINT;
  8250. BEGIN
  8251. i := Infinity;
  8252. IF from = to THEN
  8253. i := 0
  8254. ELSE
  8255. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8256. (*
  8257. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8258. i := TypeDistance(from.base, to.base);
  8259. IF i >= 0 THEN INC(i) END
  8260. ELSIF (from.mode = open) & (to.mode = open) THEN
  8261. i := TypeDistance(from.base, to.base);
  8262. *)
  8263. END;
  8264. RETURN i
  8265. END ArrayTypeDistance;
  8266. (** compute the signature distance of a procedure and an actual parameter list
  8267. - if any of the parameters are not compatible, the result is infinite
  8268. - add up and return the distance over all parameters
  8269. **)
  8270. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8271. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8272. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8273. BEGIN
  8274. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8275. result := Infinity
  8276. ELSE
  8277. formalParameter := procedureType.firstParameter;
  8278. i := 0;
  8279. result := 0;
  8280. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8281. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8282. actualParameter := actualParameters.GetExpression(i);
  8283. ASSERT(formalParameter.type # NIL);
  8284. IF (actualParameter.type = NIL) THEN distance := Infinity
  8285. ELSE
  8286. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8287. END;
  8288. IF distance = Infinity THEN
  8289. result := Infinity;
  8290. ELSE
  8291. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8292. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8293. (* already handled varpar *)
  8294. (*
  8295. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8296. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8297. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8298. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8299. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8300. END;
  8301. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8302. result := Infinity
  8303. END;
  8304. *)
  8305. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8306. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8307. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8308. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8309. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8310. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8311. END;
  8312. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8313. result := Infinity
  8314. END;
  8315. ELSE
  8316. result := Infinity
  8317. END;
  8318. ELSE
  8319. INC(result,distance);
  8320. END;
  8321. END;
  8322. formalParameter := formalParameter.nextParameter; INC(i);
  8323. END;
  8324. END;
  8325. ASSERT(result >= 0);
  8326. RETURN result
  8327. END Distance;
  8328. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8329. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8330. BEGIN
  8331. IF right.numberParameters # (procedureType.numberParameters) THEN
  8332. result := Infinity
  8333. ELSE
  8334. formalParameter := procedureType.firstParameter;
  8335. rightParameter := right.firstParameter;
  8336. i := 0;
  8337. result := 0;
  8338. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8339. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8340. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8341. IF distance = Infinity THEN
  8342. result := Infinity;
  8343. ELSE
  8344. INC(result,distance);
  8345. END;
  8346. formalParameter := formalParameter.nextParameter;
  8347. rightParameter := rightParameter.nextParameter;
  8348. END;
  8349. END;
  8350. ASSERT(result >= 0);
  8351. RETURN result
  8352. END ProcedureTypeDistance;
  8353. (** compute and return the distance between two types, used for computation of signature distance
  8354. from -> to
  8355. **)
  8356. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8357. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8358. BEGIN
  8359. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8360. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8361. END;
  8362. i := Infinity;
  8363. IF from = to THEN
  8364. i := 0
  8365. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8366. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8367. i := Infinity;
  8368. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8369. i := 1
  8370. ELSIF (from IS SyntaxTree.StringType) THEN
  8371. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8372. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8373. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8374. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8375. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8376. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8377. i := 1
  8378. ELSIF (from IS SyntaxTree.NilType) THEN
  8379. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8380. (*
  8381. ELSIF (from = NoType) THEN
  8382. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8383. *)
  8384. ELSIF (from IS SyntaxTree.BasicType) THEN
  8385. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8386. IF varpar & (i # 0) THEN i := Infinity END;
  8387. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8388. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8389. ELSIF (from IS SyntaxTree.RecordType) THEN
  8390. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8391. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8392. IF to IS SyntaxTree.MathArrayType THEN
  8393. (*
  8394. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8395. i := Infinity;
  8396. ELSE
  8397. *)
  8398. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8399. (*
  8400. END;
  8401. *)
  8402. END
  8403. ELSIF (from IS SyntaxTree.PointerType) THEN
  8404. ptr := from(SyntaxTree.PointerType);
  8405. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8406. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8407. (* ELSE i := TypeDistance(ptr.base, to); *)
  8408. END
  8409. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8410. IF (to IS SyntaxTree.ProcedureType) THEN
  8411. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8412. END;
  8413. (*no procedure test, procedure must be the same*)
  8414. END;
  8415. RETURN i
  8416. END TypeDistance;
  8417. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8418. BEGIN
  8419. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8420. END IsIntegerType;
  8421. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8422. BEGIN
  8423. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8424. END IsAddressType;
  8425. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8426. BEGIN
  8427. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8428. END IsSizeType;
  8429. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8430. BEGIN
  8431. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8432. END IsSignedIntegerType;
  8433. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8434. BEGIN
  8435. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8436. END IsUnsignedIntegerType;
  8437. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8438. VAR result: BOOLEAN;
  8439. BEGIN
  8440. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8441. value := x.resolved(SyntaxTree.IntegerValue).value;
  8442. result := TRUE
  8443. ELSE
  8444. result := FALSE
  8445. END;
  8446. RETURN result
  8447. END IsIntegerValue;
  8448. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8449. VAR result: BOOLEAN;
  8450. BEGIN
  8451. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8452. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8453. result := TRUE
  8454. ELSE
  8455. result := FALSE
  8456. END;
  8457. RETURN result
  8458. END IsEnumerationValue;
  8459. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8460. VAR result: BOOLEAN;
  8461. BEGIN
  8462. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8463. value := x.resolved(SyntaxTree.RealValue).value;
  8464. result := TRUE
  8465. ELSE
  8466. result := FALSE
  8467. END;
  8468. RETURN result
  8469. END IsRealValue;
  8470. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8471. VAR result: BOOLEAN;
  8472. BEGIN
  8473. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8474. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8475. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8476. result := TRUE
  8477. ELSE
  8478. result := FALSE
  8479. END;
  8480. RETURN result
  8481. END IsComplexValue;
  8482. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8483. VAR result: BOOLEAN;
  8484. BEGIN
  8485. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8486. value := x.resolved(SyntaxTree.CharacterValue).value;
  8487. result := TRUE
  8488. ELSE
  8489. result := FALSE
  8490. END;
  8491. RETURN result
  8492. END IsCharacterValue;
  8493. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8494. VAR result: BOOLEAN;
  8495. BEGIN
  8496. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8497. value := x.resolved(SyntaxTree.BooleanValue).value;
  8498. result := TRUE
  8499. ELSE
  8500. result := FALSE
  8501. END;
  8502. RETURN result
  8503. END IsBooleanValue;
  8504. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8505. VAR result: BOOLEAN;
  8506. BEGIN
  8507. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8508. value := x.resolved(SyntaxTree.SetValue).value;
  8509. result := TRUE
  8510. ELSE
  8511. result := FALSE
  8512. END;
  8513. RETURN result
  8514. END IsSetValue;
  8515. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8516. VAR result: BOOLEAN;
  8517. BEGIN
  8518. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8519. value := x.resolved(SyntaxTree.StringValue).value;
  8520. result := TRUE
  8521. ELSE
  8522. result := FALSE
  8523. END;
  8524. RETURN result
  8525. END IsStringValue;
  8526. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8527. BEGIN
  8528. x := x.resolved;
  8529. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8530. END Indexable;
  8531. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8532. BEGIN
  8533. RETURN t1.SameType(t2.resolved);
  8534. END SameType;
  8535. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8536. BEGIN
  8537. IF t IS SyntaxTree.MathArrayType THEN
  8538. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8539. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8540. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8541. DEC(max);
  8542. END;
  8543. ELSIF t IS SyntaxTree.ArrayType THEN
  8544. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8545. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8546. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8547. END;
  8548. END;
  8549. RETURN t;
  8550. END ArrayBase;
  8551. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8552. BEGIN
  8553. type := type.resolved;
  8554. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8555. base := type(SyntaxTree.ArrayType).arrayBase;
  8556. RETURN TRUE;
  8557. END;
  8558. RETURN FALSE;
  8559. END IsOpenArray;
  8560. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8561. BEGIN
  8562. type := type.resolved;
  8563. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8564. base := type(SyntaxTree.ArrayType).arrayBase;
  8565. dim := type(SyntaxTree.ArrayType).staticLength;
  8566. RETURN TRUE
  8567. ELSE
  8568. RETURN FALSE
  8569. END;
  8570. END IsStaticArray;
  8571. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8572. BEGIN
  8573. type := type.resolved;
  8574. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8575. base := type(SyntaxTree.ArrayType).arrayBase;
  8576. RETURN TRUE
  8577. ELSE
  8578. RETURN FALSE
  8579. END;
  8580. END IsDynamicArray;
  8581. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8582. VAR i: LONGINT;
  8583. BEGIN
  8584. i := 0;
  8585. t := t.resolved;
  8586. IF t IS SyntaxTree.MathArrayType THEN
  8587. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8588. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8589. END;
  8590. ELSIF t IS SyntaxTree.ArrayType THEN
  8591. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8592. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8593. END;
  8594. END;
  8595. RETURN i
  8596. END Dimension;
  8597. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8598. BEGIN
  8599. RETURN expression.assignable;
  8600. END IsVariable;
  8601. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8602. BEGIN
  8603. IF (symbol IS SyntaxTree.Parameter) THEN
  8604. WITH symbol: SyntaxTree.Parameter DO
  8605. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8606. END;
  8607. ELSE
  8608. RETURN FALSE
  8609. END;
  8610. END IsVariableParameter;
  8611. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8612. VAR result: BOOLEAN;
  8613. BEGIN
  8614. IF type = NIL THEN result := FALSE
  8615. ELSE
  8616. type := type.resolved;
  8617. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8618. END;
  8619. RETURN result
  8620. END IsPointerType;
  8621. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8622. VAR result: BOOLEAN;
  8623. BEGIN
  8624. IF type = NIL THEN result := FALSE
  8625. ELSE
  8626. type := type.resolved;
  8627. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8628. END;
  8629. RETURN result
  8630. END IsUnsafePointer;
  8631. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8632. BEGIN
  8633. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8634. END IsDisposable;
  8635. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8636. VAR result: BOOLEAN;
  8637. BEGIN
  8638. IF type = NIL THEN result := FALSE
  8639. ELSE
  8640. type := type.resolved;
  8641. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8642. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8643. result := result OR (type IS SyntaxTree.ObjectType);
  8644. END;
  8645. RETURN result
  8646. END IsPointerToRecord;
  8647. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8648. VAR result: BOOLEAN;
  8649. BEGIN
  8650. IF type = NIL THEN result := FALSE
  8651. ELSE
  8652. type := type.resolved;
  8653. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8654. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8655. ;
  8656. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8657. result := result OR (type IS SyntaxTree.ObjectType);
  8658. END;
  8659. RETURN result
  8660. END IsPointerToObject;
  8661. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8662. BEGIN
  8663. IF type # NIL THEN
  8664. RETURN type.resolved.hasPointers
  8665. ELSE
  8666. RETURN FALSE
  8667. END;
  8668. END ContainsPointer;
  8669. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8670. BEGIN
  8671. IF type = NIL THEN RETURN FALSE END;
  8672. type := type.resolved;
  8673. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8674. END IsStringType;
  8675. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8676. BEGIN
  8677. IF type = NIL THEN RETURN FALSE END;
  8678. type := type.resolved;
  8679. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8680. END IsCharacterType;
  8681. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8682. BEGIN
  8683. IF type = NIL THEN RETURN FALSE END;
  8684. type := type.resolved;
  8685. RETURN (type IS SyntaxTree.EnumerationType)
  8686. END IsEnumerationType;
  8687. (** cf. section "Type extension (base type)" in the language report **)
  8688. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8689. VAR result: BOOLEAN;
  8690. BEGIN
  8691. ASSERT(base # NIL); ASSERT(extension # NIL);
  8692. base := base.resolved; extension := extension.resolved;
  8693. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8694. result := TRUE;
  8695. ELSE
  8696. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8697. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8698. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8699. END;
  8700. WHILE (extension # NIL) & (extension # base) DO
  8701. IF extension IS SyntaxTree.RecordType THEN
  8702. extension := extension(SyntaxTree.RecordType).baseType;
  8703. IF (extension # NIL) THEN extension := extension.resolved END;
  8704. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8705. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8706. END;
  8707. ELSE extension := NIL;
  8708. END;
  8709. END;
  8710. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8711. END;
  8712. RETURN result
  8713. END IsTypeExtension;
  8714. (** check if base is the base enumeration type of extension **)
  8715. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8716. BEGIN
  8717. base := base.resolved; extension := extension.resolved;
  8718. WHILE (extension # NIL) & (extension # base) DO
  8719. IF extension IS SyntaxTree.EnumerationType THEN
  8720. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8721. IF extension # NIL THEN extension := extension.resolved END;
  8722. ELSE
  8723. extension := NIL
  8724. END;
  8725. END;
  8726. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8727. END IsEnumerationExtension;
  8728. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8729. BEGIN
  8730. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8731. RETURN TRUE
  8732. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8733. RETURN TRUE
  8734. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8735. RETURN TRUE
  8736. ELSE
  8737. RETURN FALSE
  8738. END
  8739. END IsCallable;
  8740. (** compute and return the distance of two record types
  8741. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8742. **)
  8743. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8744. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8745. BEGIN
  8746. i := 0;
  8747. WHILE (from # NIL) & (from # to) DO
  8748. baseType := from.baseType;
  8749. IF (baseType # NIL) THEN
  8750. baseType := baseType.resolved;
  8751. IF baseType IS SyntaxTree.PointerType THEN
  8752. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8753. END;
  8754. IF baseType IS SyntaxTree.RecordType THEN
  8755. from := baseType(SyntaxTree.RecordType);
  8756. ELSE
  8757. from := NIL;
  8758. END;
  8759. ELSE
  8760. from := NIL
  8761. END;
  8762. INC(i)
  8763. END;
  8764. IF from = NIL THEN i := Infinity END;
  8765. RETURN i
  8766. END RecordTypeDistance;
  8767. (** compute and return the distance of two pointer types **)
  8768. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8769. BEGIN
  8770. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8771. RETURN Infinity;
  8772. ELSE
  8773. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8774. END;
  8775. END PointerTypeDistance;
  8776. (** check if expression contains a symbol designator pointing to a type declaration.
  8777. - if so then enter type declaration into typeDeclaration and return true else return false
  8778. **)
  8779. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8780. VAR result: BOOLEAN;
  8781. BEGIN
  8782. result := FALSE;
  8783. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8784. result := TRUE;
  8785. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8786. END;
  8787. RETURN result
  8788. END IsTypeDesignator;
  8789. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8790. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8791. VAR result: BOOLEAN;
  8792. BEGIN
  8793. type := type.resolved;
  8794. IF type IS SyntaxTree.PointerType THEN
  8795. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8796. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8797. result := TRUE
  8798. ELSE
  8799. result := type IS SyntaxTree.RecordType
  8800. END;
  8801. RETURN result
  8802. END IsExtensibleType;
  8803. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8804. BEGIN
  8805. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8806. (d IS SyntaxTree.SymbolDesignator) &
  8807. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8808. OR
  8809. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8810. END IsUnextensibleRecord;
  8811. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8812. BEGIN
  8813. IF IsUnextensibleRecord(d) THEN
  8814. RETURN FALSE
  8815. ELSE RETURN IsExtensibleType(d.type.resolved)
  8816. END;
  8817. END IsExtensibleDesignator;
  8818. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8819. BEGIN
  8820. type := type.resolved;
  8821. IF (type IS SyntaxTree.PointerType) THEN
  8822. RETURN TRUE
  8823. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8824. RETURN TRUE
  8825. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8826. RETURN TRUE
  8827. ELSIF (type IS SyntaxTree.BasicType) THEN
  8828. RETURN TRUE
  8829. END;
  8830. RETURN FALSE
  8831. END IsBasicType;
  8832. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8833. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8834. BEGIN
  8835. baseType := record.baseType;
  8836. IF (baseType # NIL) THEN
  8837. baseType := baseType.resolved;
  8838. IF (baseType IS SyntaxTree.PointerType) THEN
  8839. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8840. END;
  8841. END;
  8842. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8843. recordType := baseType(SyntaxTree.RecordType);
  8844. ELSE
  8845. recordType := NIL;
  8846. END;
  8847. RETURN recordType
  8848. END RecordBase;
  8849. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8850. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8851. BEGIN
  8852. baseRecord := RecordBase(scope.ownerRecord);
  8853. IF baseRecord = NIL THEN RETURN NIL END;
  8854. scope := baseRecord.recordScope;
  8855. procedureType := procedure.type.resolved;
  8856. IF procedure IS SyntaxTree.Operator THEN
  8857. operator := scope.firstOperator;
  8858. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8859. (*
  8860. Printout.Info("not same ",procedureType);
  8861. Printout.Info("with ",operator.type);
  8862. *)
  8863. operator := operator.nextOperator;
  8864. END;
  8865. super := operator;
  8866. ELSE
  8867. super := scope.firstProcedure;
  8868. WHILE (super # NIL) & (super.name # procedure.name) DO
  8869. super := super.nextProcedure;
  8870. END;
  8871. END;
  8872. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8873. RETURN super
  8874. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8875. RETURN super
  8876. ELSE
  8877. RETURN FindSuperProcedure(scope,procedure);
  8878. END;
  8879. END FindSuperProcedure;
  8880. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8881. VAR procedure: SyntaxTree.Procedure;
  8882. BEGIN
  8883. procedure := record.recordScope.constructor;
  8884. IF procedure = NIL THEN
  8885. record := RecordBase(record);
  8886. IF record # NIL THEN
  8887. procedure := GetConstructor(record)
  8888. END;
  8889. END;
  8890. RETURN procedure;
  8891. END GetConstructor;
  8892. (* enter a case into a list of cases in a sorted way and check for collision *)
  8893. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8894. VAR prev,this,new: SyntaxTree.CaseConstant;
  8895. BEGIN
  8896. this := root;
  8897. prev := NIL;
  8898. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8899. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8900. RETURN FALSE
  8901. ELSE
  8902. IF (this # NIL) & (this.min = max+1) THEN
  8903. this.min := min
  8904. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8905. prev.max := min
  8906. ELSE
  8907. NEW(new); new.min := min; new.max := max;
  8908. new.next := this;
  8909. IF prev = NIL THEN
  8910. root := new;
  8911. ELSE
  8912. prev.next := new
  8913. END
  8914. END;
  8915. RETURN TRUE
  8916. END;
  8917. END EnterCase;
  8918. (** generate and return a new checker object, errors are entered into diagnostics **)
  8919. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8920. VAR checker: Checker;
  8921. BEGIN
  8922. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache);
  8923. RETURN checker
  8924. END NewChecker;
  8925. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8926. VAR warnings: Warnings;
  8927. BEGIN
  8928. NEW(warnings, diagnostics); RETURN warnings;
  8929. END NewWarnings;
  8930. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8931. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8932. END IsRangeType;
  8933. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8934. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8935. END IsMathArrayType;
  8936. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8937. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8938. END IsArrayType;
  8939. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8940. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8941. END IsComplexType;
  8942. (** if a type is an array-structured object type *)
  8943. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  8944. VAR recordType: SyntaxTree.RecordType;
  8945. BEGIN
  8946. IF type = NIL THEN
  8947. RETURN FALSE
  8948. ELSE
  8949. type := type.resolved;
  8950. IF type IS SyntaxTree.PointerType THEN
  8951. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8952. IF type IS SyntaxTree.RecordType THEN
  8953. recordType := type(SyntaxTree.RecordType);
  8954. RETURN recordType.isObject & recordType.HasArrayStructure()
  8955. ELSE
  8956. RETURN FALSE
  8957. END
  8958. ELSE
  8959. RETURN FALSE
  8960. END
  8961. END
  8962. END IsArrayStructuredObjectType;
  8963. (** the math array structure of a type
  8964. - for math arrays: the array itself
  8965. - for pointers: the math array structure of the pointer base
  8966. - for array-structured object types: the underlying structure
  8967. - for non-math arrays and all other types: NIL
  8968. **)
  8969. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  8970. VAR
  8971. result: SyntaxTree.MathArrayType;
  8972. BEGIN
  8973. IF type = NIL THEN
  8974. result := NIL
  8975. ELSE
  8976. type := type.resolved;
  8977. IF type IS SyntaxTree.PointerType THEN
  8978. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8979. END;
  8980. IF type IS SyntaxTree.MathArrayType THEN
  8981. result := type(SyntaxTree.MathArrayType)
  8982. ELSIF type IS SyntaxTree.RecordType THEN
  8983. result := type(SyntaxTree.RecordType).arrayStructure
  8984. ELSE
  8985. result := NIL
  8986. END
  8987. END;
  8988. RETURN result
  8989. END MathArrayStructureOfType;
  8990. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  8991. VAR
  8992. result: BOOLEAN;
  8993. rangeExpression: SyntaxTree.RangeExpression;
  8994. BEGIN
  8995. IF x IS SyntaxTree.RangeExpression THEN
  8996. rangeExpression := x(SyntaxTree.RangeExpression);
  8997. result := TRUE;
  8998. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8999. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9000. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9001. ELSE
  9002. result := FALSE
  9003. END;
  9004. RETURN result
  9005. END IsStaticRange;
  9006. (** whether a type is a math array of tensor form **)
  9007. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9008. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9009. END IsTensor;
  9010. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9011. BEGIN
  9012. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9013. length := type(SyntaxTree.MathArrayType).staticLength;
  9014. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9015. RETURN TRUE
  9016. ELSE
  9017. RETURN FALSE
  9018. END;
  9019. END IsStaticMathArray;
  9020. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9021. BEGIN
  9022. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9023. END SymbolHasAddress;
  9024. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9025. BEGIN
  9026. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9027. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9028. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9029. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9030. END HasAddress;
  9031. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9032. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9033. BEGIN
  9034. IF (e IS SyntaxTree.Designator) THEN
  9035. d := e(SyntaxTree.Designator);
  9036. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9037. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9038. e := d.left;
  9039. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9040. END;
  9041. IF d # NIL THEN
  9042. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9043. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9044. END;
  9045. END;
  9046. RETURN FALSE;
  9047. END IsLocalVariable;
  9048. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9049. BEGIN
  9050. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9051. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9052. ELSE
  9053. RETURN TRUE
  9054. END;
  9055. END IsStaticProcedure;
  9056. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9057. CONST OptimizeMethodTable = FALSE;
  9058. BEGIN
  9059. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9060. END InMethodTable;
  9061. END FoxSemanticChecker.