FoxSyntaxTree.Mod 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120
  1. MODULE FoxSyntaxTree; (** AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Abstract Syntax Tree"; **)
  2. (* (c) fof ETHZ 2009 *)
  3. (**
  4. note on documentation:
  5. Most objects in this module are commented with an informal Oberon syntax example indicating which variables of the respective object stand for what symbol /expression etc.
  6. This syntax example should not be confused with a profound description of the syntax in an EBNF form, which can rather be found in the parser module.
  7. The informal Oberon syntax is marked with << ... >>
  8. **)
  9. IMPORT
  10. Basic := FoxBasic, Scanner := FoxScanner, BitSets, StringPool, Strings(* , D := Debugging (* only for debuggging / counting *) *) ;
  11. CONST
  12. (** general flags: used in statements, procedure types and symbols
  13. general flags are unique and may overlap with access flags only
  14. flag numbers have no meaning and are not used for object files etc., i.e. flag renumbering is possible without effect
  15. *)
  16. (** calling conventions *)
  17. OberonCallingConvention* =0;
  18. CCallingConvention* =1;
  19. WinAPICallingConvention* =2;
  20. DarwinCCallingConvention* =3;
  21. InterruptCallingConvention* = 4;
  22. (** Access Flags *)
  23. InternalRead* = 0; (** can read symbol in same module *)
  24. InternalWrite* = 1; (** can write symbol in same module *)
  25. ProtectedRead* = 2; (** can read symbol in type extentions *)
  26. ProtectedWrite* = 3; (** can write symbol in type extentions *)
  27. PublicRead* = 4; (** can read everywhere *)
  28. PublicWrite* = 5; (** can write everywhere *)
  29. Hidden* = {};
  30. Internal* = {InternalRead, InternalWrite};
  31. Protected* = {ProtectedRead, ProtectedWrite} ;
  32. Public* = {PublicRead, PublicWrite} ;
  33. ReadOnly* = {InternalRead, ProtectedRead,PublicRead};
  34. (** parameter forms *)
  35. ValueParameter* = 0; VarParameter* = 1; ConstParameter* = 2;
  36. InPort*=3; OutPort*=4;
  37. (** array forms *)
  38. Static*=1; (* ARRAY x OF .. / ARRAY [x] OF ... *)
  39. Open*=2; (* ARRAY OF ... / ARRAY [*] OF ... *)
  40. Tensor*=3; (* ARRAY [?] OF ... *)
  41. SemiDynamic*=4;
  42. (** node states, important for checker to avoid cycles *)
  43. Undefined*={}; BeingResolved*=1; Resolved*=2; FingerPrinted*=3; Warned*=4;
  44. (* context in which a range expression is used *)
  45. ArrayIndex* = 0;
  46. SetElement* = 1;
  47. CaseGuard* = 2;
  48. (* reflection flags *)
  49. FlagProcedureDelegate*=0;
  50. FlagProcedureConstructor*=1;
  51. FlagParameterVar*=1;
  52. FlagParameterConst*=2;
  53. TYPE
  54. Position*= Scanner.Position;
  55. SourceCode*= Scanner.StringType;
  56. BinaryCode*= BitSets.BitSet;
  57. String*= Scanner.StringType;
  58. IdentifierString*= Scanner.IdentifierString;
  59. CallingConvention*= LONGINT;
  60. (** visitor pattern implementation *)
  61. (* to use this object in your implementation, copy and paste and replace "x: " by "x: SyntaxTree." *)
  62. Visitor* = OBJECT
  63. (** types *)
  64. PROCEDURE VisitType*(x: Type);
  65. BEGIN HALT(100) (* abstract *) END VisitType;
  66. PROCEDURE VisitBasicType*(x: BasicType);
  67. BEGIN HALT(100) (* abstract *) END VisitBasicType;
  68. PROCEDURE VisitByteType*(x: ByteType);
  69. BEGIN HALT(100) (* abstract *) END VisitByteType;
  70. PROCEDURE VisitAnyType*(x: AnyType);
  71. BEGIN HALT(100) (* abstract *) END VisitAnyType;
  72. PROCEDURE VisitObjectType*(x: ObjectType);
  73. BEGIN HALT(100) (* abstract *) END VisitObjectType;
  74. PROCEDURE VisitNilType*(x: NilType);
  75. BEGIN HALT(100) (* abstract *) END VisitNilType;
  76. PROCEDURE VisitAddressType*(x: AddressType);
  77. BEGIN HALT(100) (* abstract *) END VisitAddressType;
  78. PROCEDURE VisitSizeType*(x: SizeType);
  79. BEGIN HALT(100) (* abstract *) END VisitSizeType;
  80. PROCEDURE VisitBooleanType*(x: BooleanType);
  81. BEGIN HALT(100) (* abstract *) END VisitBooleanType;
  82. PROCEDURE VisitSetType*(x: SetType);
  83. BEGIN HALT(100) (* abstract *) END VisitSetType;
  84. PROCEDURE VisitCharacterType*(x: CharacterType);
  85. BEGIN HALT(100) END VisitCharacterType;
  86. PROCEDURE VisitIntegerType*(x: IntegerType);
  87. BEGIN HALT(100) END VisitIntegerType;
  88. PROCEDURE VisitFloatType*(x: FloatType);
  89. BEGIN HALT(100) END VisitFloatType;
  90. PROCEDURE VisitComplexType*(x: ComplexType);
  91. BEGIN HALT(100) END VisitComplexType;
  92. PROCEDURE VisitQualifiedType*(x: QualifiedType);
  93. BEGIN HALT(100) (* abstract *) END VisitQualifiedType;
  94. PROCEDURE VisitStringType*(x: StringType);
  95. BEGIN HALT(100) (* abstract *) END VisitStringType;
  96. PROCEDURE VisitEnumerationType*(x: EnumerationType);
  97. BEGIN HALT(100) (* abstract *) END VisitEnumerationType;
  98. PROCEDURE VisitRangeType*(x: RangeType);
  99. BEGIN HALT(100) (* abstract *) END VisitRangeType;
  100. PROCEDURE VisitArrayType*(x: ArrayType);
  101. BEGIN HALT(100) (* abstract *) END VisitArrayType;
  102. PROCEDURE VisitMathArrayType*(x: MathArrayType);
  103. BEGIN HALT(100) (* abstract *) END VisitMathArrayType;
  104. PROCEDURE VisitPointerType*(x: PointerType);
  105. BEGIN HALT(100) (* abstract *) END VisitPointerType;
  106. PROCEDURE VisitPortType*(x: PortType);
  107. BEGIN HALT(100) (* abstract *) END VisitPortType;
  108. PROCEDURE VisitRecordType*(x: RecordType);
  109. BEGIN HALT(100) (* abstract *) END VisitRecordType;
  110. PROCEDURE VisitCellType*(x: CellType);
  111. BEGIN HALT(100) (* abstract *) END VisitCellType;
  112. PROCEDURE VisitProcedureType*(x: ProcedureType);
  113. BEGIN HALT(100) (* abstract *) END VisitProcedureType;
  114. (** expressions *)
  115. PROCEDURE VisitExpression*(x: Expression);
  116. BEGIN HALT(100) (* abstract *) END VisitExpression;
  117. PROCEDURE VisitSet*(x: Set);
  118. BEGIN HALT(100) (* abstract *) END VisitSet;
  119. PROCEDURE VisitMathArrayExpression*(x: MathArrayExpression);
  120. BEGIN HALT(100) (* abstract *) END VisitMathArrayExpression;
  121. PROCEDURE VisitUnaryExpression*(x: UnaryExpression);
  122. BEGIN HALT(100) (* abstract *) END VisitUnaryExpression;
  123. PROCEDURE VisitBinaryExpression*(x: BinaryExpression);
  124. BEGIN HALT(100) (* abstract *) END VisitBinaryExpression;
  125. PROCEDURE VisitRangeExpression*(x: RangeExpression);
  126. BEGIN HALT(100) (* abstract *) END VisitRangeExpression;
  127. PROCEDURE VisitTensorRangeExpression*(x: TensorRangeExpression);
  128. BEGIN HALT(100) (* abstract *) END VisitTensorRangeExpression;
  129. PROCEDURE VisitConversion*(x: Conversion);
  130. BEGIN HALT(100) (* abstract *) END VisitConversion;
  131. (** designators (expressions) *)
  132. PROCEDURE VisitDesignator*(x: Designator);
  133. BEGIN HALT(100) (* abstract *) END VisitDesignator;
  134. PROCEDURE VisitIdentifierDesignator*(x: IdentifierDesignator);
  135. BEGIN HALT(100) (* abstract *) END VisitIdentifierDesignator;
  136. PROCEDURE VisitSelectorDesignator*(x: SelectorDesignator);
  137. BEGIN HALT(100) (* abstract *) END VisitSelectorDesignator;
  138. PROCEDURE VisitParameterDesignator*(x: ParameterDesignator);
  139. BEGIN HALT(100) (* abstract *) END VisitParameterDesignator;
  140. PROCEDURE VisitArrowDesignator*(x: ArrowDesignator);
  141. BEGIN HALT(100) (* abstract *) END VisitArrowDesignator;
  142. PROCEDURE VisitBracketDesignator*(x: BracketDesignator);
  143. BEGIN HALT(100) (* abstract *) END VisitBracketDesignator;
  144. PROCEDURE VisitSymbolDesignator*(x: SymbolDesignator);
  145. BEGIN HALT(100) (* abstract *) END VisitSymbolDesignator;
  146. PROCEDURE VisitIndexDesignator*(x: IndexDesignator);
  147. BEGIN HALT(100) (* abstract *) END VisitIndexDesignator;
  148. PROCEDURE VisitProcedureCallDesignator*(x: ProcedureCallDesignator);
  149. BEGIN HALT(100) (* abstract *) END VisitProcedureCallDesignator;
  150. PROCEDURE VisitStatementDesignator*(x: StatementDesignator);
  151. BEGIN HALT(100) (* abstract *) END VisitStatementDesignator;
  152. PROCEDURE VisitBuiltinCallDesignator*(x: BuiltinCallDesignator);
  153. BEGIN HALT(100) (* abstract *) END VisitBuiltinCallDesignator;
  154. PROCEDURE VisitTypeGuardDesignator*(x: TypeGuardDesignator);
  155. BEGIN HALT(100) (* abstract *) END VisitTypeGuardDesignator;
  156. PROCEDURE VisitDereferenceDesignator*(x: DereferenceDesignator);
  157. BEGIN HALT(100) (* abstract *) END VisitDereferenceDesignator;
  158. PROCEDURE VisitSupercallDesignator*(x: SupercallDesignator);
  159. BEGIN HALT(100) (* abstract *) END VisitSupercallDesignator;
  160. PROCEDURE VisitSelfDesignator*(x: SelfDesignator);
  161. BEGIN HALT(100) (* abstract *) END VisitSelfDesignator;
  162. PROCEDURE VisitResultDesignator*(x: ResultDesignator);
  163. BEGIN HALT(100) (* abstract *) END VisitResultDesignator;
  164. (** values *)
  165. PROCEDURE VisitValue*(x: Value);
  166. BEGIN HALT(100) (* abstract *) END VisitValue;
  167. PROCEDURE VisitBooleanValue*(x: BooleanValue);
  168. BEGIN HALT(100) (* abstract *) END VisitBooleanValue;
  169. PROCEDURE VisitIntegerValue*(x: IntegerValue);
  170. BEGIN HALT(100) (* abstract *) END VisitIntegerValue;
  171. PROCEDURE VisitCharacterValue*(x: CharacterValue);
  172. BEGIN HALT(100) (* abstract *) END VisitCharacterValue;
  173. PROCEDURE VisitSetValue*(x: SetValue);
  174. BEGIN HALT(100) (* abstract *) END VisitSetValue;
  175. PROCEDURE VisitMathArrayValue*(x: MathArrayValue);
  176. BEGIN HALT(100) (* abstract *) END VisitMathArrayValue;
  177. PROCEDURE VisitRealValue*(x: RealValue);
  178. BEGIN HALT(100) (* abstract *) END VisitRealValue;
  179. PROCEDURE VisitComplexValue*(x: ComplexValue);
  180. BEGIN HALT(100) (* abstract *) END VisitComplexValue;
  181. PROCEDURE VisitStringValue*(x: StringValue);
  182. BEGIN HALT(100) (* abstract *) END VisitStringValue;
  183. PROCEDURE VisitNilValue*(x: NilValue);
  184. BEGIN HALT(100) (* abstract *) END VisitNilValue;
  185. PROCEDURE VisitEnumerationValue*(x: EnumerationValue);
  186. BEGIN HALT(100) (* abstract *) END VisitEnumerationValue;
  187. (** symbols *)
  188. PROCEDURE VisitSymbol*(x: Symbol);
  189. BEGIN HALT(100) (* abstract *) END VisitSymbol;
  190. PROCEDURE VisitModule*(x: Module);
  191. BEGIN HALT(100) (* abstract *) END VisitModule;
  192. PROCEDURE VisitTypeDeclaration*(x: TypeDeclaration);
  193. BEGIN HALT(100) (* abstract *) END VisitTypeDeclaration;
  194. PROCEDURE VisitConstant*(x: Constant);
  195. BEGIN HALT(100) (* abstract *) END VisitConstant;
  196. PROCEDURE VisitVariable*(x: Variable);
  197. BEGIN HALT(100) (* abstract *) END VisitVariable;
  198. PROCEDURE VisitParameter*(x: Parameter);
  199. BEGIN HALT(100) (* abstract *) END VisitParameter;
  200. PROCEDURE VisitProperty*(x: Property);
  201. BEGIN HALT(100) (* abstract *) END VisitProperty;
  202. PROCEDURE VisitProcedure*(x: Procedure);
  203. BEGIN HALT(100) (* abstract *) END VisitProcedure;
  204. PROCEDURE VisitBuiltin*(x: Builtin);
  205. BEGIN HALT(100) (* abstract *) END VisitBuiltin;
  206. PROCEDURE VisitOperator*(x: Operator);
  207. BEGIN HALT(100) (* abstract *) END VisitOperator;
  208. PROCEDURE VisitImport*(x: Import);
  209. BEGIN HALT(100) (* abstract *) END VisitImport;
  210. (** statements *)
  211. PROCEDURE VisitStatement*(x: Statement);
  212. BEGIN HALT(100) (* abstract *) END VisitStatement;
  213. PROCEDURE VisitProcedureCallStatement*(x: ProcedureCallStatement);
  214. BEGIN HALT(100) (* abstract *) END VisitProcedureCallStatement;
  215. PROCEDURE VisitAssignment*(x: Assignment);
  216. BEGIN HALT(100) (* abstract *) END VisitAssignment;
  217. PROCEDURE VisitCommunicationStatement*(x: CommunicationStatement);
  218. BEGIN HALT(100) (* abstract *) END VisitCommunicationStatement;
  219. PROCEDURE VisitIfStatement*(x: IfStatement);
  220. BEGIN HALT(100) (* abstract *) END VisitIfStatement;
  221. PROCEDURE VisitWithStatement*(x: WithStatement);
  222. BEGIN HALT(100) (* abstract *) END VisitWithStatement;
  223. PROCEDURE VisitCaseStatement*(x: CaseStatement);
  224. BEGIN HALT(100) (* abstract *) END VisitCaseStatement;
  225. PROCEDURE VisitWhileStatement*(x: WhileStatement);
  226. BEGIN HALT(100) (* abstract *) END VisitWhileStatement;
  227. PROCEDURE VisitRepeatStatement*(x: RepeatStatement);
  228. BEGIN HALT(100) (* abstract *) END VisitRepeatStatement;
  229. PROCEDURE VisitForStatement*(x: ForStatement);
  230. BEGIN HALT(100) (* abstract *) END VisitForStatement;
  231. PROCEDURE VisitLoopStatement*(x: LoopStatement);
  232. BEGIN HALT(100) (* abstract *) END VisitLoopStatement;
  233. PROCEDURE VisitExitableBlock*(x: ExitableBlock);
  234. BEGIN HALT(100) (* abstract *) END VisitExitableBlock;
  235. PROCEDURE VisitExitStatement*(x: ExitStatement);
  236. BEGIN HALT(100) (* abstract *) END VisitExitStatement;
  237. PROCEDURE VisitReturnStatement*(x: ReturnStatement);
  238. BEGIN HALT(100) (* abstract *) END VisitReturnStatement;
  239. PROCEDURE VisitAwaitStatement*(x: AwaitStatement);
  240. BEGIN HALT(100) (* abstract *) END VisitAwaitStatement;
  241. PROCEDURE VisitStatementBlock*(x: StatementBlock);
  242. BEGIN HALT(100) (* abstract *) END VisitStatementBlock;
  243. PROCEDURE VisitCode*(x: Code);
  244. BEGIN HALT(100) (* abstract *) END VisitCode;
  245. END Visitor;
  246. ArrayAccessOperators* = RECORD
  247. len*: Operator; (* length operator *)
  248. generalRead*, generalWrite*: Operator; (* operators on ARRAY [*] RANGE, for tensors *)
  249. read*, write*: POINTER TO ARRAY OF Operator; (* fixed-dim. operators *)
  250. END;
  251. FingerPrint*= RECORD
  252. shallow*,public*, private*: LONGINT;
  253. shallowAvailable*, deepAvailable*: BOOLEAN;
  254. END;
  255. (** identifiers in a program text **)
  256. Identifier* = Basic.String;
  257. (** qualified identifiers << Identifier.Identifier >> **)
  258. QualifiedIdentifier* = OBJECT
  259. VAR
  260. prefix-, suffix-: Identifier; (* use string index instead ? *)
  261. position-: Position;
  262. PROCEDURE & InitQualifiedIdentifier( position: Position; prefix, suffix: Identifier);
  263. BEGIN
  264. (* ASSERT(suffix # invalidIdentifier); can happen but should be catched by the parser with error report and not here with trap *)
  265. SELF.position := position;
  266. SELF.prefix := prefix; SELF.suffix := suffix;
  267. END InitQualifiedIdentifier;
  268. PROCEDURE GetName*(VAR name: Basic.SegmentedName);
  269. BEGIN
  270. Basic.InitSegmentedName(name);
  271. IF prefix # invalidIdentifier THEN Basic.SuffixSegmentedName(name, prefix) END;
  272. Basic.SuffixSegmentedName(name, suffix)
  273. END GetName;
  274. END QualifiedIdentifier;
  275. (**** types ****)
  276. (**
  277. Type
  278. BasicType
  279. ObjectType
  280. NilType
  281. AnyType
  282. ByteType
  283. AddressType
  284. SizeType
  285. BooleanType
  286. SetType
  287. CharacterType
  288. RangeType
  289. NumberType
  290. IntegerType
  291. FloatType
  292. ComplexType
  293. QualifiedType
  294. StringType
  295. EnumerationType
  296. ArrayType
  297. MathArrayType
  298. PointerType
  299. PortType
  300. RecordType
  301. CellType
  302. ProcedureType
  303. *)
  304. Type* = OBJECT
  305. VAR
  306. typeDeclaration-: TypeDeclaration; (* link to declaration (if any), needed for printing, debugging and symbol lookup *)
  307. scope-: Scope; (* scope where the type has been declared *)
  308. resolved-: Type; (* indirection to resolved type to preserve qualified types *)
  309. position-,end-: Position;
  310. state-: SET;
  311. hasPointers-: BOOLEAN;
  312. fingerprint-: FingerPrint;
  313. isRealtime-: BOOLEAN;
  314. recursion: BOOLEAN;
  315. sizeInBits-: LONGINT; (* allocation size of this type in bits *)
  316. alignmentInBits-: LONGINT;
  317. PROCEDURE & InitType*( position: Position);
  318. BEGIN
  319. SELF.position := position; state := Undefined;
  320. end := invalidPosition;
  321. typeDeclaration := NIL;
  322. scope := NIL;
  323. resolved := SELF;
  324. sizeInBits := MIN(LONGINT);
  325. alignmentInBits := 0;
  326. isRealtime := FALSE;
  327. recursion := FALSE;
  328. hasPointers := FALSE;
  329. InitFingerPrint(fingerprint);
  330. END InitType;
  331. PROCEDURE SetSize*(sizeInBits: LONGINT);
  332. BEGIN SELF.sizeInBits := sizeInBits
  333. END SetSize;
  334. PROCEDURE SetAlignmentInBits*(alignmentInBits: LONGINT);
  335. BEGIN SELF.alignmentInBits := alignmentInBits
  336. END SetAlignmentInBits;
  337. PROCEDURE End*( position: LONGINT );
  338. BEGIN SELF.position.end := position;
  339. END End;
  340. PROCEDURE SetFingerPrint*(CONST fp: FingerPrint);
  341. BEGIN
  342. SELF.fingerprint := fp
  343. END SetFingerPrint;
  344. PROCEDURE SetState*(state: LONGINT);
  345. BEGIN INCL(SELF.state,state);
  346. END SetState;
  347. PROCEDURE SetHasPointers*(has: BOOLEAN);
  348. BEGIN
  349. hasPointers := has
  350. END SetHasPointers;
  351. PROCEDURE RemoveState*(state: LONGINT);
  352. BEGIN EXCL(SELF.state,state)
  353. END RemoveState;
  354. PROCEDURE SetTypeDeclaration*(typeDeclaration: TypeDeclaration);
  355. BEGIN SELF.typeDeclaration := typeDeclaration
  356. END SetTypeDeclaration;
  357. PROCEDURE SetScope*(scope: Scope);
  358. BEGIN SELF.scope := scope
  359. END SetScope;
  360. PROCEDURE SetRealtime*(isRealtime: BOOLEAN);
  361. BEGIN SELF.isRealtime := isRealtime
  362. END SetRealtime;
  363. PROCEDURE SameType*(this: Type): BOOLEAN;
  364. BEGIN RETURN FALSE
  365. END SameType;
  366. (** assignment compatibility of this := SELF *)
  367. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  368. BEGIN RETURN FALSE
  369. END CompatibleTo;
  370. (** Returns if the type is a pointer *)
  371. PROCEDURE IsPointer*(): BOOLEAN;
  372. BEGIN RETURN FALSE
  373. END IsPointer;
  374. (** Returns if the type consists of more than one parts. Implies that an instance of this type cannot be (easily) represented in one register. *)
  375. PROCEDURE IsComposite*(): BOOLEAN;
  376. BEGIN RETURN FALSE
  377. END IsComposite;
  378. (** Returns if the type needs to be traced for garbage collection *)
  379. PROCEDURE NeedsTrace*(): BOOLEAN;
  380. BEGIN RETURN IsPointer ();
  381. END NeedsTrace;
  382. PROCEDURE Accept*(v: Visitor);
  383. VAR position: LONGINT;
  384. BEGIN position := SELF.position.start; v.VisitType(SELF)
  385. END Accept;
  386. PROCEDURE IsRecordType*(): BOOLEAN;
  387. BEGIN
  388. RETURN FALSE;
  389. END IsRecordType;
  390. END Type;
  391. (* basic types, defined in global name space *)
  392. BasicType*= OBJECT(Type)
  393. VAR name-: Identifier;
  394. PROCEDURE & InitBasicType(CONST id: ARRAY OF CHAR; sizeInBits: LONGINT);
  395. VAR str: IdentifierString;
  396. BEGIN
  397. COPY(id, str);Basic.AppendNumber(str,sizeInBits); name := NewIdentifier(str);
  398. InitType(invalidPosition);
  399. SetSize(sizeInBits);
  400. SELF.name := name
  401. END InitBasicType;
  402. PROCEDURE SetName*(CONST id: ARRAY OF CHAR);
  403. BEGIN
  404. name := NewIdentifier(id);
  405. END SetName;
  406. PROCEDURE SetTypeDeclaration*(typeDeclaration: TypeDeclaration);
  407. BEGIN HALT(100);
  408. END SetTypeDeclaration;
  409. PROCEDURE Accept*(v: Visitor);
  410. VAR position: LONGINT;
  411. BEGIN position := SELF.position.start; v.VisitBasicType(SELF)
  412. END Accept;
  413. END BasicType;
  414. (** <<OBJECT>>
  415. object type (base type of all objects)
  416. **)
  417. ObjectType*=OBJECT(BasicType)
  418. PROCEDURE & InitObjectType(sizeInBits: LONGINT);
  419. BEGIN
  420. InitBasicType("@Object",sizeInBits);
  421. hasPointers := TRUE;
  422. END InitObjectType;
  423. PROCEDURE SameType*(this: Type): BOOLEAN;
  424. BEGIN RETURN (this IS ObjectType)
  425. END SameType;
  426. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  427. BEGIN RETURN ((to IS AnyType) OR (to IS ObjectType))
  428. END CompatibleTo;
  429. PROCEDURE Accept*(v: Visitor);
  430. VAR position: LONGINT;
  431. BEGIN position := SELF.position.start; v.VisitObjectType(SELF)
  432. END Accept;
  433. PROCEDURE IsPointer*(): BOOLEAN;
  434. BEGIN RETURN TRUE
  435. END IsPointer;
  436. END ObjectType;
  437. (** <<NIL>>
  438. nil type (type of NIL pointers), may be replaced by any type
  439. **)
  440. NilType*=OBJECT(BasicType)
  441. PROCEDURE & InitNilType(sizeInBits: LONGINT);
  442. BEGIN
  443. InitBasicType("@Nil",sizeInBits);
  444. SetRealtime(TRUE);
  445. hasPointers := TRUE;
  446. END InitNilType;
  447. PROCEDURE SameType*(this: Type): BOOLEAN;
  448. BEGIN RETURN (this IS NilType)
  449. END SameType;
  450. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  451. BEGIN RETURN (to IS NilType) OR (to IS ObjectType) OR (to IS AnyType) OR (to IS PointerType) OR (to IS ProcedureType) OR (to IS AddressType)
  452. END CompatibleTo;
  453. PROCEDURE IsPointer*(): BOOLEAN;
  454. BEGIN RETURN TRUE
  455. END IsPointer;
  456. PROCEDURE Accept*(v: Visitor);
  457. VAR position: LONGINT;
  458. BEGIN position := SELF.position.start; v.VisitNilType(SELF)
  459. END Accept;
  460. END NilType;
  461. (** <<SYSTEM.BYTE>>
  462. any pointer type (pointer to record and pointer to array)
  463. **)
  464. AnyType*=OBJECT(BasicType)
  465. PROCEDURE & InitAnyType(sizeInBits: LONGINT);
  466. BEGIN
  467. InitBasicType("@Any",sizeInBits);
  468. hasPointers := TRUE;
  469. END InitAnyType;
  470. PROCEDURE SameType*(this: Type): BOOLEAN;
  471. BEGIN RETURN this IS AnyType
  472. END SameType;
  473. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  474. BEGIN RETURN (to IS AnyType)
  475. END CompatibleTo;
  476. PROCEDURE IsPointer*(): BOOLEAN;
  477. BEGIN RETURN TRUE
  478. END IsPointer;
  479. PROCEDURE Accept*(v: Visitor);
  480. VAR position: LONGINT;
  481. BEGIN position := SELF.position.start; v.VisitAnyType(SELF)
  482. END Accept;
  483. END AnyType;
  484. (** <<SYSTEM.BYTE>>
  485. byte type
  486. **)
  487. ByteType*=OBJECT(BasicType)
  488. PROCEDURE & InitByteType(sizeInBits: LONGINT);
  489. BEGIN
  490. InitBasicType("@Byte",sizeInBits);
  491. SetRealtime(TRUE);
  492. END InitByteType;
  493. PROCEDURE Accept*(v: Visitor);
  494. VAR position: LONGINT;
  495. BEGIN position := SELF.position.start; v.VisitByteType(SELF)
  496. END Accept;
  497. PROCEDURE SameType*(this: Type): BOOLEAN;
  498. BEGIN RETURN this IS ByteType
  499. END SameType;
  500. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  501. BEGIN RETURN (to IS ByteType)
  502. END CompatibleTo;
  503. END ByteType;
  504. (** <<ADDRESS>>
  505. address type
  506. **)
  507. AddressType*=OBJECT(BasicType)
  508. PROCEDURE & InitAddressType(sizeInBits: LONGINT);
  509. BEGIN
  510. InitBasicType("@Address",sizeInBits);
  511. SetRealtime(TRUE);
  512. END InitAddressType;
  513. PROCEDURE SameType*(this: Type): BOOLEAN;
  514. BEGIN RETURN (this IS AddressType)
  515. END SameType;
  516. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  517. BEGIN RETURN (to IS AddressType) OR (to IS SizeType) OR (to IS IntegerType) & (to.sizeInBits >= sizeInBits) OR (to IS PointerType) & to(PointerType).isUnsafe
  518. END CompatibleTo;
  519. PROCEDURE Accept*(v: Visitor);
  520. VAR position: LONGINT;
  521. BEGIN position := SELF.position.start; v.VisitAddressType(SELF)
  522. END Accept;
  523. END AddressType;
  524. (** <<SIZE>>
  525. size type (signed address type)
  526. **)
  527. SizeType*=OBJECT(BasicType)
  528. PROCEDURE & InitSizeType(sizeInBits: LONGINT);
  529. BEGIN
  530. InitBasicType("@Size",sizeInBits);
  531. SetRealtime(TRUE);
  532. END InitSizeType;
  533. PROCEDURE SameType*(this: Type): BOOLEAN;
  534. BEGIN RETURN (this IS SizeType)
  535. END SameType;
  536. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  537. BEGIN RETURN (to IS SizeType) OR (to IS AddressType) OR (to IS IntegerType) & (to.sizeInBits >= sizeInBits)
  538. END CompatibleTo;
  539. PROCEDURE Accept*(v: Visitor);
  540. VAR position: LONGINT;
  541. BEGIN position := SELF.position.start; v.VisitSizeType(SELF)
  542. END Accept;
  543. END SizeType;
  544. (** <<BOOLEAN>>
  545. boolean type
  546. **)
  547. BooleanType*=OBJECT(BasicType)
  548. PROCEDURE & InitBooleanType(sizeInBits: LONGINT);
  549. BEGIN
  550. InitBasicType("@Boolean",sizeInBits);
  551. SetRealtime(TRUE);
  552. END InitBooleanType;
  553. PROCEDURE SameType*(this: Type): BOOLEAN;
  554. BEGIN RETURN this IS BooleanType
  555. END SameType;
  556. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  557. BEGIN RETURN (to IS BooleanType)
  558. END CompatibleTo;
  559. PROCEDURE Accept*(v: Visitor);
  560. VAR position: LONGINT;
  561. BEGIN position := SELF.position.start; v.VisitBooleanType(SELF)
  562. END Accept;
  563. END BooleanType;
  564. (** <<SET>>
  565. set type
  566. **)
  567. SetType*=OBJECT(BasicType)
  568. PROCEDURE & InitSetType(sizeInBits: LONGINT);
  569. BEGIN
  570. InitBasicType("@Set",sizeInBits);
  571. SetRealtime(TRUE);
  572. END InitSetType;
  573. PROCEDURE SameType*(this: Type): BOOLEAN;
  574. BEGIN RETURN (this = SELF) OR (this IS SetType) & (this.sizeInBits = sizeInBits);
  575. END SameType;
  576. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  577. BEGIN RETURN (to IS SetType) & (to.sizeInBits >= sizeInBits)
  578. END CompatibleTo;
  579. PROCEDURE Accept*(v: Visitor);
  580. VAR position: LONGINT;
  581. BEGIN position := SELF.position.start; v.VisitSetType(SELF)
  582. END Accept;
  583. END SetType;
  584. (** <<CHAR, CHAR8, CHAR16, CHAR32>>
  585. character types
  586. **)
  587. CharacterType*=OBJECT(BasicType)
  588. PROCEDURE & InitCharacterType(sizeInBits: LONGINT);
  589. BEGIN
  590. InitBasicType("@Character", sizeInBits);
  591. SetRealtime(TRUE);
  592. END InitCharacterType;
  593. PROCEDURE SameType*(this: Type): BOOLEAN;
  594. BEGIN RETURN (this = SELF) OR (this IS CharacterType) & (this.sizeInBits = sizeInBits)
  595. END SameType;
  596. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  597. BEGIN RETURN ((to IS CharacterType) OR (to IS ByteType)) & (to.sizeInBits >= sizeInBits)
  598. END CompatibleTo;
  599. PROCEDURE Accept*(v: Visitor);
  600. VAR position: LONGINT;
  601. BEGIN position := SELF.position.start; v.VisitCharacterType(SELF)
  602. END Accept;
  603. END CharacterType;
  604. (** type of ranges (case constants, set elements, array indices)
  605. represented by basic type <<RANGE>>
  606. **)
  607. RangeType* = OBJECT(BasicType)
  608. PROCEDURE & InitRangeType(sizeInBits: LONGINT);
  609. BEGIN
  610. InitBasicType("@RangeType",sizeInBits);
  611. SetRealtime(TRUE);
  612. END InitRangeType;
  613. PROCEDURE SameType*(this: Type): BOOLEAN;
  614. BEGIN RETURN (this = SELF) OR (this IS RangeType)
  615. END SameType;
  616. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  617. BEGIN RETURN SameType(to)
  618. END CompatibleTo;
  619. PROCEDURE IsComposite*(): BOOLEAN;
  620. BEGIN RETURN TRUE
  621. END IsComposite;
  622. PROCEDURE Accept*(v: Visitor);
  623. VAR position: LONGINT;
  624. BEGIN position := SELF.position.start; v.VisitRangeType(SELF)
  625. END Accept;
  626. END RangeType;
  627. (* number types: IntegerType or FloatType *)
  628. NumberType*=OBJECT(BasicType)
  629. PROCEDURE & InitNumberType( CONST name: ARRAY OF CHAR; sizeInBits: LONGINT);
  630. BEGIN
  631. InitBasicType(name, sizeInBits);
  632. SetRealtime(TRUE);
  633. END InitNumberType;
  634. END NumberType;
  635. (** <<SHORTINT, INTEGER, LONGINT, HUGEINT>>
  636. integer types
  637. **)
  638. IntegerType*= OBJECT (NumberType)
  639. VAR signed-: BOOLEAN;
  640. PROCEDURE & InitIntegerType(sizeInBits: LONGINT; signed: BOOLEAN);
  641. BEGIN
  642. IF signed THEN
  643. InitNumberType("@Integer",sizeInBits);
  644. ELSE
  645. InitNumberType("@Unsigned",sizeInBits);
  646. END;
  647. SELF.signed := signed;
  648. END InitIntegerType;
  649. PROCEDURE SameType*(this: Type): BOOLEAN;
  650. BEGIN RETURN (this = SELF) OR (this IS IntegerType) & (this.sizeInBits = sizeInBits) & (this(IntegerType).signed = signed)
  651. END SameType;
  652. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  653. BEGIN RETURN ((to IS IntegerType) OR (to IS AddressType) OR (to IS SizeType) OR (to IS ByteType)) & (to.sizeInBits >= sizeInBits) OR (to IS FloatType)
  654. OR (to IS ComplexType) & CompatibleTo((to(ComplexType).componentType))
  655. END CompatibleTo;
  656. PROCEDURE Accept*(v: Visitor);
  657. VAR position: LONGINT;
  658. BEGIN position := SELF.position.start; v.VisitIntegerType(SELF)
  659. END Accept;
  660. END IntegerType;
  661. (** <<REAL,LONGREAL>>
  662. real types: REAL, LONGREAL
  663. **)
  664. FloatType*= OBJECT (NumberType)
  665. PROCEDURE & InitFloatType(sizeInBits: LONGINT);
  666. BEGIN
  667. InitNumberType("@Float",sizeInBits);
  668. END InitFloatType;
  669. PROCEDURE SameType*(this: Type): BOOLEAN;
  670. BEGIN RETURN (this = SELF) OR (this IS FloatType) & (this.sizeInBits = sizeInBits)
  671. END SameType;
  672. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  673. BEGIN
  674. RETURN (to IS FloatType) & (to.sizeInBits >= sizeInBits)
  675. OR (to IS ComplexType) & CompatibleTo((to(ComplexType).componentType))
  676. END CompatibleTo;
  677. PROCEDURE Accept*(v: Visitor);
  678. VAR position: LONGINT;
  679. BEGIN position := SELF.position.start; v.VisitFloatType(SELF)
  680. END Accept;
  681. END FloatType;
  682. (** <<COMPLEX,LONGCOMPLEX>>
  683. complex types: COMPLEX, LONGCOMPLEX
  684. **)
  685. ComplexType*= OBJECT (NumberType)
  686. VAR componentType-: Type; (* REAL or LONGREAL*)
  687. PROCEDURE & InitComplexType(componentType: Type);
  688. BEGIN
  689. ASSERT(componentType # NIL);
  690. SELF.componentType := componentType;
  691. sizeInBits := 2 * componentType.sizeInBits;
  692. InitNumberType("@Complex",sizeInBits);
  693. END InitComplexType;
  694. PROCEDURE SameType*(this: Type): BOOLEAN;
  695. BEGIN RETURN (this = SELF) OR (this IS ComplexType) & (componentType.SameType(this(ComplexType).componentType))
  696. END SameType;
  697. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  698. BEGIN RETURN (to IS ComplexType) & (componentType.CompatibleTo(to(ComplexType).componentType))
  699. END CompatibleTo;
  700. PROCEDURE IsComposite*(): BOOLEAN;
  701. BEGIN RETURN TRUE
  702. END IsComposite;
  703. PROCEDURE Accept*(v: Visitor);
  704. VAR position: LONGINT;
  705. BEGIN position := SELF.position.start; v.VisitComplexType(SELF)
  706. END Accept;
  707. END ComplexType;
  708. (** <<qualifiedIdentifier = resolved>>
  709. named reference to a type
  710. **)
  711. QualifiedType* = OBJECT (Type)
  712. VAR
  713. qualifiedIdentifier-: QualifiedIdentifier;
  714. PROCEDURE & InitQualifiedType( position: Position; scope: Scope; qualifiedIdentifier: QualifiedIdentifier);
  715. BEGIN
  716. ASSERT(qualifiedIdentifier # NIL);
  717. InitType( position);
  718. SELF.scope := scope;
  719. SELF.qualifiedIdentifier := qualifiedIdentifier;
  720. resolved := NIL;
  721. END InitQualifiedType;
  722. PROCEDURE SetResolved*(resolved: Type);
  723. BEGIN SELF.resolved := resolved; IF resolved # NIL THEN hasPointers := resolved.hasPointers END;
  724. END SetResolved;
  725. PROCEDURE SameType*(this: Type): BOOLEAN;
  726. BEGIN RETURN (this = SELF) OR (resolved # NIL) & (this.resolved # NIL) & resolved.SameType(this.resolved)
  727. END SameType;
  728. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  729. BEGIN RETURN (resolved # NIL) & resolved.CompatibleTo(to)
  730. END CompatibleTo;
  731. PROCEDURE IsPointer*(): BOOLEAN;
  732. BEGIN RETURN (resolved # NIL) & resolved.IsPointer()
  733. END IsPointer;
  734. PROCEDURE IsComposite*(): BOOLEAN;
  735. BEGIN RETURN (resolved # NIL) & resolved.IsComposite()
  736. END IsComposite;
  737. PROCEDURE Accept*(v: Visitor);
  738. VAR position: LONGINT;
  739. BEGIN position := SELF.position.start; v.VisitQualifiedType(SELF)
  740. END Accept;
  741. PROCEDURE NeedsTrace* (): BOOLEAN;
  742. BEGIN RETURN (resolved # NIL) & (resolved.NeedsTrace());
  743. END NeedsTrace;
  744. PROCEDURE IsRecordType*(): BOOLEAN;
  745. BEGIN
  746. RETURN (resolved # NIL) & (resolved.IsRecordType());
  747. END IsRecordType;
  748. END QualifiedType;
  749. (** string literal type **)
  750. StringType*= OBJECT(Type)
  751. VAR
  752. length-: LONGINT;
  753. baseType-: Type;
  754. PROCEDURE & InitStringType(position: Position; baseType: Type; length: LONGINT);
  755. BEGIN
  756. InitType(position);
  757. SetRealtime(TRUE);
  758. SELF.length := length;
  759. SELF.baseType := baseType;
  760. END InitStringType;
  761. PROCEDURE SetLength*(length: LONGINT);
  762. BEGIN SELF.length := length
  763. END SetLength;
  764. PROCEDURE SameType*(this: Type): BOOLEAN;
  765. BEGIN RETURN (this IS StringType) & (this(StringType).length = length)
  766. END SameType;
  767. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  768. BEGIN
  769. IF to IS ArrayType THEN
  770. WITH to: ArrayType DO
  771. RETURN to.arrayBase.SameType(baseType.resolved) & ((to.form = Open) OR (to.staticLength >= length))
  772. END;
  773. ELSIF to IS CharacterType THEN
  774. RETURN (length=2) & baseType.CompatibleTo(to)
  775. ELSE RETURN FALSE
  776. END;
  777. END CompatibleTo;
  778. PROCEDURE IsComposite*(): BOOLEAN;
  779. BEGIN RETURN TRUE
  780. END IsComposite;
  781. PROCEDURE Accept*(v: Visitor);
  782. VAR position: LONGINT;
  783. BEGIN position := SELF.position.start; v.VisitStringType(SELF)
  784. END Accept;
  785. END StringType;
  786. (** enumeration type of the form <<enum (base) red,green,blue end>> **)
  787. EnumerationType*=OBJECT(Type)
  788. VAR
  789. enumerationScope-: EnumerationScope;
  790. enumerationBase-: Type;
  791. rangeLowest-,rangeHighest-: LONGINT;
  792. PROCEDURE &InitEnumerationType(position: Position; scope: Scope; enumerationScope: EnumerationScope);
  793. BEGIN
  794. InitType(position);
  795. SetRealtime(TRUE);
  796. SELF.scope := scope;
  797. enumerationBase := NIL;
  798. rangeLowest := 0; rangeHighest := 0;
  799. SELF.enumerationScope := enumerationScope;
  800. enumerationScope.ownerEnumeration := SELF;
  801. END InitEnumerationType;
  802. PROCEDURE SetEnumerationBase*(base: Type);
  803. BEGIN enumerationBase := base
  804. END SetEnumerationBase;
  805. PROCEDURE SetRange*(lowest,highest: LONGINT);
  806. BEGIN rangeLowest := lowest; rangeHighest := highest;
  807. END SetRange;
  808. PROCEDURE Extends*(this: EnumerationType): BOOLEAN;
  809. BEGIN RETURN (SELF = this) OR (enumerationBase # NIL) & (enumerationBase.resolved(EnumerationType).Extends(this));
  810. END Extends;
  811. PROCEDURE SameType*(this: Type): BOOLEAN;
  812. BEGIN RETURN this = SELF
  813. END SameType;
  814. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  815. BEGIN RETURN (to IS EnumerationType) & (to(EnumerationType).Extends(SELF))
  816. END CompatibleTo;
  817. PROCEDURE Accept*(v: Visitor);
  818. VAR position: LONGINT;
  819. BEGIN position := SELF.position.start; v.VisitEnumerationType(SELF)
  820. END Accept;
  821. END EnumerationType;
  822. (** <<ARRAY [length] OF baseType>> *)
  823. ArrayType* = OBJECT (Type)
  824. VAR
  825. arrayBase-: Type;
  826. length-: Expression;
  827. staticLength-: LONGINT;
  828. form-: LONGINT; (* redundant: (form = Open) = (staticLength = 0) else (form = Static) *)
  829. PROCEDURE & InitArrayType(position: Position; scope: Scope; form: LONGINT);
  830. BEGIN
  831. length := NIL; arrayBase := NIL; InitType(position); staticLength := 0; SELF.form := form; SELF.scope := scope;
  832. END InitArrayType;
  833. PROCEDURE SetArrayBase*( type: Type );
  834. BEGIN
  835. arrayBase := type; IF (arrayBase # NIL) & (arrayBase.hasPointers) THEN SetHasPointers(TRUE) END;
  836. END SetArrayBase;
  837. PROCEDURE SetForm*(f: LONGINT);
  838. BEGIN
  839. form := f;
  840. END SetForm;
  841. PROCEDURE SetLength*(length: Expression);
  842. BEGIN
  843. SELF.length := length;
  844. IF (length.resolved # NIL) & (length.resolved IS IntegerValue) THEN
  845. staticLength := length.resolved(IntegerValue).value
  846. END;
  847. END SetLength;
  848. PROCEDURE Child*(nr: LONGINT):Type;
  849. BEGIN
  850. IF nr = 0 THEN RETURN SELF;
  851. ELSIF nr = 1 THEN RETURN arrayBase.resolved;
  852. ELSE RETURN arrayBase.resolved(ArrayType).Child(nr-1);
  853. END;
  854. END Child;
  855. (* recursion safety for cases such as
  856. A= POINTER TO ARRAY OF B;
  857. B= POINTER TO ARRAY OF A;
  858. *)
  859. PROCEDURE SameType*(this: Type): BOOLEAN;
  860. VAR result : BOOLEAN;
  861. BEGIN
  862. result := FALSE;
  863. IF this = SELF THEN
  864. result := TRUE
  865. ELSIF recursion THEN
  866. result := TRUE;
  867. ELSIF this IS ArrayType THEN
  868. recursion := TRUE;
  869. WITH this: ArrayType DO
  870. result := (this.form = form) & (this.staticLength = staticLength) & arrayBase.SameType(this.arrayBase.resolved);
  871. END;
  872. END;
  873. recursion := FALSE;
  874. RETURN result
  875. END SameType;
  876. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  877. BEGIN
  878. RETURN (form = Static) & SameType(to)
  879. END CompatibleTo;
  880. PROCEDURE IsComposite*(): BOOLEAN;
  881. BEGIN RETURN TRUE
  882. END IsComposite;
  883. PROCEDURE NeedsTrace*(): BOOLEAN;
  884. BEGIN RETURN arrayBase.resolved.NeedsTrace ();
  885. END NeedsTrace;
  886. PROCEDURE Accept*(v: Visitor);
  887. VAR position: LONGINT;
  888. BEGIN position := SELF.position.start; v.VisitArrayType(SELF)
  889. END Accept;
  890. END ArrayType;
  891. (** <<ARRAY '[' length | '*' | '?' ']' OF baseType>> **)
  892. MathArrayType* = OBJECT (Type)
  893. VAR
  894. arrayBase-: Type;
  895. length-: Expression;
  896. staticLength-: LONGINT;
  897. staticIncrementInBits-: LONGINT;
  898. form-: LONGINT;
  899. PROCEDURE & InitMathArrayType(position: Position;scope: Scope; form: LONGINT);
  900. BEGIN
  901. length := NIL; arrayBase := NIL; InitType(position); staticLength := 0; staticIncrementInBits := 0; SetForm(form); SELF.scope := scope;
  902. END InitMathArrayType;
  903. PROCEDURE SetForm*(form: LONGINT);
  904. BEGIN
  905. SELF.form := form; IF form # Static THEN SetHasPointers(TRUE) END;
  906. END SetForm;
  907. PROCEDURE SetArrayBase*( type: Type );
  908. BEGIN
  909. arrayBase := type; IF (arrayBase # NIL) & (arrayBase.hasPointers) THEN SetHasPointers(TRUE) END;
  910. END SetArrayBase;
  911. PROCEDURE SetLength*(length: Expression);
  912. BEGIN
  913. SELF.length := length;
  914. IF (length # NIL) & (length.resolved # NIL) & (length.resolved IS IntegerValue) THEN
  915. staticLength := length.resolved(IntegerValue).value;
  916. (* optimization: unless the base type is a dynamic array, make this array static *)
  917. IF ~((arrayBase # NIL) & (arrayBase IS MathArrayType) & (arrayBase(MathArrayType).form # Static)) THEN
  918. form := Static;
  919. END
  920. ELSIF length = NIL THEN
  921. form := Open;
  922. END;
  923. END SetLength;
  924. PROCEDURE SetIncrement*(increment: LONGINT);
  925. BEGIN staticIncrementInBits := increment
  926. END SetIncrement;
  927. (* recursion safety for cases such as
  928. A= POINTER TO ARRAY OF B;
  929. B= POINTER TO ARRAY OF A;
  930. *)
  931. PROCEDURE SameType*(this: Type): BOOLEAN;
  932. VAR result: BOOLEAN;
  933. BEGIN
  934. result := FALSE;
  935. IF this = SELF THEN
  936. result := TRUE
  937. ELSIF recursion THEN
  938. result := TRUE;
  939. ELSIF this IS MathArrayType THEN
  940. recursion := TRUE;
  941. WITH this: MathArrayType DO
  942. result := (this.form = form) & (this.staticLength = staticLength) &
  943. ((arrayBase = NIL) & (this.arrayBase = NIL) OR (arrayBase # NIL) & (this.arrayBase # NIL) &
  944. arrayBase.SameType(this.arrayBase.resolved));
  945. END;
  946. END;
  947. recursion := FALSE;
  948. RETURN result
  949. END SameType;
  950. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  951. BEGIN
  952. HALT(200); (*! implement *)
  953. RETURN (form = Static) & SameType(to)
  954. END CompatibleTo;
  955. (** get the element type of a math array, i.e. the first type in the math array chain that is not a math array **)
  956. PROCEDURE ElementType*(): Type;
  957. VAR
  958. type: Type;
  959. BEGIN
  960. type := SELF;
  961. WHILE type IS MathArrayType DO
  962. type := type(MathArrayType).arrayBase.resolved
  963. END;
  964. RETURN type
  965. END ElementType;
  966. (** get the number of dimensions of a math array; 0 in case of tensors **)
  967. PROCEDURE Dimensionality*(): LONGINT;
  968. VAR
  969. type: Type;
  970. dim: LONGINT;
  971. BEGIN
  972. IF form = Tensor THEN
  973. dim := 0
  974. ELSE
  975. type := SELF;
  976. dim := 0;
  977. WHILE type IS MathArrayType DO
  978. ASSERT(type(MathArrayType).form # Tensor);
  979. INC(dim);
  980. type := type(MathArrayType).arrayBase.resolved
  981. END
  982. END;
  983. RETURN dim
  984. END Dimensionality;
  985. (** if the math array is of the form ARRAY [*, *, ..., *], i.e. contains no static length and is not a tensor either **)
  986. PROCEDURE IsFullyDynamic*(): BOOLEAN;
  987. VAR
  988. type: Type;
  989. result: BOOLEAN;
  990. BEGIN
  991. IF form = Tensor THEN
  992. result := FALSE;
  993. ELSE
  994. result := TRUE;
  995. type := SELF;
  996. WHILE type IS MathArrayType DO
  997. IF type(MathArrayType).form # Open THEN result := FALSE END;
  998. type := type(MathArrayType).arrayBase.resolved
  999. END
  1000. END;
  1001. RETURN result
  1002. END IsFullyDynamic;
  1003. PROCEDURE NeedsTrace*(): BOOLEAN;
  1004. BEGIN RETURN hasPointers OR (arrayBase # NIL) & (arrayBase.resolved.NeedsTrace());
  1005. END NeedsTrace;
  1006. PROCEDURE IsComposite*(): BOOLEAN;
  1007. BEGIN RETURN TRUE
  1008. END IsComposite;
  1009. PROCEDURE Accept*(v: Visitor);
  1010. VAR position: LONGINT;
  1011. BEGIN position := SELF.position.start; v.VisitMathArrayType(SELF)
  1012. END Accept;
  1013. END MathArrayType;
  1014. (** <<POINTER TO pointerBase>> **)
  1015. PointerType* = OBJECT (Type)
  1016. VAR
  1017. modifiers-: Modifier; (* set by the parser *)
  1018. pointerBase-: Type;
  1019. isPlain-: BOOLEAN;
  1020. isUnsafe-: BOOLEAN;
  1021. isUntraced-: BOOLEAN;
  1022. isDisposable-: BOOLEAN;
  1023. isHidden-: BOOLEAN;
  1024. PROCEDURE & InitPointerType(position: Position; scope: Scope);
  1025. BEGIN
  1026. modifiers := NIL;
  1027. pointerBase := NIL;
  1028. isPlain := FALSE;
  1029. isUnsafe := FALSE;
  1030. isDisposable := FALSE;
  1031. InitType(position);
  1032. SELF.scope := scope;
  1033. hasPointers := TRUE;
  1034. isHidden := FALSE;
  1035. isUntraced := FALSE;
  1036. END InitPointerType;
  1037. PROCEDURE SetHidden*(hidden: BOOLEAN);
  1038. BEGIN
  1039. isHidden := hidden;
  1040. END SetHidden; (** <<POINTER TO pointerBase>> **)
  1041. PROCEDURE SetModifiers*(flags: Modifier);
  1042. BEGIN modifiers := flags
  1043. END SetModifiers;
  1044. PROCEDURE SetPointerBase*( type: Type );
  1045. BEGIN
  1046. pointerBase := type;
  1047. END SetPointerBase;
  1048. PROCEDURE SetPlain*(plain: BOOLEAN);
  1049. BEGIN
  1050. isPlain := plain;
  1051. END SetPlain;
  1052. PROCEDURE SetUnsafe*(unsafe: BOOLEAN);
  1053. BEGIN
  1054. isUnsafe := unsafe;
  1055. END SetUnsafe;
  1056. PROCEDURE SetUntraced*(untraced: BOOLEAN);
  1057. BEGIN
  1058. isUntraced := untraced;
  1059. END SetUntraced;
  1060. PROCEDURE SetDisposable*(disposable: BOOLEAN);
  1061. BEGIN
  1062. isDisposable := disposable;
  1063. END SetDisposable;
  1064. PROCEDURE Extends*(this: Type): BOOLEAN;
  1065. VAR result: BOOLEAN; extension, base: Type;
  1066. BEGIN
  1067. result := FALSE;
  1068. IF ((this IS ObjectType) OR (this IS AnyType)) & (pointerBase.resolved IS RecordType) THEN result := TRUE
  1069. ELSE
  1070. extension := pointerBase.resolved;
  1071. IF this IS PointerType THEN
  1072. base := this(PointerType).pointerBase.resolved;
  1073. ELSIF this IS RecordType THEN
  1074. base := this
  1075. ELSE base := NIL
  1076. END;
  1077. IF (extension IS RecordType) & (base # NIL) THEN
  1078. result := extension(RecordType).Extends(base)
  1079. END;
  1080. END;
  1081. RETURN result
  1082. END Extends;
  1083. PROCEDURE SameType*(this: Type): BOOLEAN;
  1084. BEGIN RETURN (SELF = this) OR (this IS PointerType) & (this(PointerType).pointerBase.SameType(pointerBase.resolved) & (this(PointerType).isUnsafe = isUnsafe))
  1085. END SameType;
  1086. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1087. BEGIN RETURN SameType(to) OR ~(to IS RecordType) & SELF.Extends(to)
  1088. END CompatibleTo;
  1089. PROCEDURE IsPointer*(): BOOLEAN;
  1090. BEGIN RETURN TRUE
  1091. END IsPointer;
  1092. PROCEDURE NeedsTrace*(): BOOLEAN;
  1093. BEGIN RETURN ~isUntraced;
  1094. END NeedsTrace;
  1095. PROCEDURE Accept*(v: Visitor);
  1096. VAR position: LONGINT;
  1097. BEGIN position := SELF.position.start; v.VisitPointerType(SELF)
  1098. END Accept;
  1099. END PointerType;
  1100. (** << PORT (IN | OUT) [(size)] >>**)
  1101. PortType* = OBJECT (Type)
  1102. VAR
  1103. direction-: LONGINT;
  1104. sizeExpression-: Expression; (* generated by parser *)
  1105. sizeInBits-: LONGINT; (* computed by checker *)
  1106. cellsAreObjects-: BOOLEAN;
  1107. PROCEDURE & InitPortType(position: Position; direction: LONGINT; sizeExpression: Expression; scope: Scope);
  1108. BEGIN
  1109. InitType(position);
  1110. SELF.sizeExpression := sizeExpression;
  1111. SELF.direction := direction;
  1112. SELF.scope := scope;
  1113. cellsAreObjects := FALSE;
  1114. END InitPortType;
  1115. PROCEDURE SetSize*(size: LONGINT);
  1116. BEGIN sizeInBits := size
  1117. END SetSize;
  1118. PROCEDURE SetSizeExpression*(sizeExpression: Expression);
  1119. BEGIN SELF.sizeExpression := sizeExpression
  1120. END SetSizeExpression;
  1121. PROCEDURE SetCellsAreObjects*(b: BOOLEAN);
  1122. BEGIN
  1123. cellsAreObjects := b;
  1124. hasPointers := b;
  1125. END SetCellsAreObjects;
  1126. PROCEDURE SameType*(this: Type): BOOLEAN;
  1127. BEGIN RETURN (this IS PortType) & (this(PortType).direction = direction) & (this(PortType).sizeInBits = sizeInBits)
  1128. END SameType;
  1129. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1130. BEGIN RETURN SameType(to)
  1131. END CompatibleTo;
  1132. PROCEDURE IsPointer*(): BOOLEAN;
  1133. BEGIN RETURN cellsAreObjects;
  1134. END IsPointer;
  1135. PROCEDURE Accept*(v: Visitor);
  1136. VAR position: LONGINT;
  1137. BEGIN position := SELF.position.start; v.VisitPortType(SELF)
  1138. END Accept;
  1139. END PortType;
  1140. (** << recordType =
  1141. [POINTER TO] RECORD (baseType) .. END |
  1142. OBJECT (baseType) .. END
  1143. >> **)
  1144. RecordType* = OBJECT (Type)
  1145. VAR
  1146. recordScope-:RecordScope;
  1147. baseType-: Type;
  1148. pointerType-: PointerType; (* for support of A = POINTER TO RECORD ... END and B = POINTER TO RECORD (A) END; *)
  1149. modifiers-: Modifier;
  1150. isObject-,isProtected: BOOLEAN;
  1151. isAbstract-: BOOLEAN;
  1152. (* a math array type describing an object's array structure; NIL if the type does not exhibit an array structure *)
  1153. arrayStructure-: MathArrayType;
  1154. (* list of all operators needed to access an array-structured object type *)
  1155. arrayAccessOperators-: ArrayAccessOperators;
  1156. PROCEDURE & InitRecordType( position: Position; scope: Scope; recordScope: RecordScope);
  1157. BEGIN
  1158. InitType( position);
  1159. SELF.scope := scope;
  1160. baseType := NIL;
  1161. pointerType := NIL;
  1162. SELF.recordScope := recordScope;
  1163. ASSERT(recordScope # NIL);
  1164. ASSERT(recordScope.ownerRecord = NIL); (* cannot register twice ! *)
  1165. recordScope.ownerRecord := SELF;
  1166. isObject := FALSE; isProtected := FALSE;
  1167. arrayStructure := NIL;
  1168. modifiers := NIL;
  1169. isAbstract := FALSE;
  1170. END InitRecordType;
  1171. PROCEDURE SetAbstract*(abstract: BOOLEAN);
  1172. BEGIN
  1173. isAbstract := abstract;
  1174. END SetAbstract;
  1175. PROCEDURE SetModifiers*(flag: Modifier);
  1176. BEGIN SELF.modifiers := flag;
  1177. END SetModifiers;
  1178. PROCEDURE SetBaseType*( type: Type );
  1179. BEGIN
  1180. baseType := type; IF (baseType # NIL) & (baseType.hasPointers) THEN hasPointers := TRUE END;
  1181. END SetBaseType;
  1182. PROCEDURE SetPointerType*(pointerType: PointerType);
  1183. BEGIN SELF.pointerType := pointerType
  1184. END SetPointerType;
  1185. PROCEDURE IsObject*(isObject: BOOLEAN);
  1186. BEGIN SELF.isObject := isObject
  1187. END IsObject;
  1188. PROCEDURE IsActive*(): BOOLEAN;
  1189. VAR base: RecordType;
  1190. BEGIN
  1191. IF (recordScope.bodyProcedure # NIL) & (recordScope.bodyProcedure.procedureScope.body # NIL) & (recordScope.bodyProcedure.procedureScope.body.isActive) THEN RETURN TRUE END;
  1192. base := GetBaseRecord();
  1193. IF base # NIL THEN RETURN base.IsActive() END;
  1194. RETURN FALSE
  1195. END IsActive;
  1196. PROCEDURE IsProtected*(): BOOLEAN;
  1197. VAR base: RecordType;
  1198. BEGIN
  1199. IF isProtected THEN RETURN TRUE END;
  1200. base := GetBaseRecord();
  1201. IF base # NIL THEN RETURN base.IsProtected() END;
  1202. RETURN FALSE
  1203. END IsProtected;
  1204. PROCEDURE SetProtected*(protected: BOOLEAN);
  1205. BEGIN SELF.isProtected := protected
  1206. END SetProtected;
  1207. PROCEDURE Level*():LONGINT;
  1208. VAR type: RecordType; res: LONGINT;
  1209. BEGIN
  1210. type := SELF;
  1211. res := 0;
  1212. WHILE (type # NIL) & (type.baseType # NIL) DO
  1213. INC(res);
  1214. type := type.GetBaseRecord();
  1215. END;
  1216. RETURN res;
  1217. END Level;
  1218. PROCEDURE GetBaseRecord*():RecordType;
  1219. BEGIN
  1220. IF baseType = NIL THEN RETURN NIL; END;
  1221. IF baseType.resolved IS RecordType THEN
  1222. RETURN baseType.resolved(RecordType);
  1223. ELSIF baseType.resolved IS PointerType THEN
  1224. IF baseType.resolved(PointerType).pointerBase.resolved # NIL THEN
  1225. RETURN baseType.resolved(PointerType).pointerBase.resolved(RecordType);
  1226. END;
  1227. END;
  1228. RETURN NIL;
  1229. END GetBaseRecord;
  1230. PROCEDURE Extends*(this: Type): BOOLEAN;
  1231. VAR result: BOOLEAN; extension: Type;
  1232. BEGIN
  1233. result := FALSE;
  1234. IF this = SELF THEN result := TRUE
  1235. ELSIF this IS RecordType THEN
  1236. IF (baseType # NIL) THEN
  1237. extension := baseType.resolved;
  1238. IF extension IS PointerType THEN
  1239. result := extension(PointerType).Extends(this)
  1240. ELSIF extension IS RecordType THEN
  1241. result := extension(RecordType).Extends(this)
  1242. END;
  1243. END;
  1244. END;
  1245. RETURN result
  1246. END Extends;
  1247. PROCEDURE SameType*(this: Type): BOOLEAN;
  1248. BEGIN RETURN (this = SELF)
  1249. END SameType;
  1250. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1251. BEGIN RETURN Extends(to)
  1252. END CompatibleTo;
  1253. PROCEDURE SetArrayStructure*(arrayStructure: MathArrayType);
  1254. BEGIN SELF.arrayStructure := arrayStructure
  1255. END SetArrayStructure;
  1256. PROCEDURE SetArrayAccessOperators*(arrayAccessOperators: ArrayAccessOperators);
  1257. BEGIN SELF.arrayAccessOperators := arrayAccessOperators
  1258. END SetArrayAccessOperators;
  1259. PROCEDURE HasArrayStructure*(): BOOLEAN;
  1260. BEGIN RETURN (arrayStructure # NIL)
  1261. END HasArrayStructure
  1262. ;
  1263. PROCEDURE IsComposite*(): BOOLEAN;
  1264. BEGIN RETURN TRUE
  1265. END IsComposite;
  1266. PROCEDURE NeedsTrace*(): BOOLEAN;
  1267. BEGIN RETURN recordScope.NeedsTrace();
  1268. END NeedsTrace;
  1269. PROCEDURE Accept*(v: Visitor);
  1270. VAR position: LONGINT;
  1271. BEGIN position := SELF.position.start; v.VisitRecordType(SELF)
  1272. END Accept;
  1273. PROCEDURE IsRecordType*(): BOOLEAN;
  1274. BEGIN
  1275. RETURN TRUE;
  1276. END IsRecordType;
  1277. END RecordType;
  1278. CellType*=OBJECT (Type)
  1279. VAR
  1280. firstParameter-,lastParameter-: Parameter; numberParameters-: LONGINT; (* parameters *)
  1281. firstProperty-, lastProperty-: Property; numberProperties: LONGINT; (* capabilities *)
  1282. cellScope-: CellScope;
  1283. isCellNet-: BOOLEAN;
  1284. modifiers-: Modifier;
  1285. baseType-: Type;
  1286. PROCEDURE &InitCellType(position: Position; scope: Scope; cellScope: CellScope);
  1287. BEGIN
  1288. InitType(position);
  1289. SELF.scope := scope;
  1290. numberParameters := 0; firstParameter := NIL; lastParameter := NIL;
  1291. numberProperties := 0; firstProperty := NIL; lastProperty := NIL;
  1292. SELF.cellScope := cellScope;
  1293. isCellNet := FALSE;
  1294. baseType := NIL;
  1295. END InitCellType;
  1296. PROCEDURE SetBaseType*(base: Type);
  1297. BEGIN
  1298. baseType := base;
  1299. END SetBaseType;
  1300. PROCEDURE GetBaseValueType*(): Type;
  1301. BEGIN
  1302. IF baseType = NIL THEN
  1303. RETURN NIL
  1304. ELSIF baseType.resolved IS PointerType THEN
  1305. RETURN baseType.resolved(PointerType).pointerBase.resolved
  1306. ELSE
  1307. RETURN baseType.resolved;
  1308. END;
  1309. END GetBaseValueType;
  1310. PROCEDURE GetBaseRecord*():RecordType;
  1311. BEGIN
  1312. IF baseType = NIL THEN RETURN NIL; END;
  1313. IF baseType.resolved IS CellType THEN
  1314. RETURN baseType.resolved(CellType).GetBaseRecord();
  1315. ELSIF baseType.resolved IS RecordType THEN
  1316. RETURN baseType.resolved(RecordType);
  1317. ELSIF baseType.resolved IS PointerType THEN
  1318. IF baseType.resolved(PointerType).pointerBase.resolved # NIL THEN
  1319. RETURN baseType.resolved(PointerType).pointerBase.resolved(RecordType);
  1320. END;
  1321. END;
  1322. RETURN NIL;
  1323. END GetBaseRecord;
  1324. PROCEDURE AddParameter*(p: Parameter);
  1325. BEGIN
  1326. ASSERT(p # NIL);
  1327. IF lastParameter= NIL THEN firstParameter := p ELSE lastParameter.nextParameter := p; p.prevParameter := lastParameter; END;
  1328. lastParameter := p;
  1329. INC(numberParameters);
  1330. END AddParameter;
  1331. PROCEDURE AddProperty*(p: Property);
  1332. BEGIN
  1333. ASSERT(p # NIL);
  1334. IF lastProperty= NIL THEN firstProperty := p ELSE lastProperty.nextProperty := p; p.prevProperty := lastProperty; END;
  1335. lastProperty := p;
  1336. INC(numberProperties);
  1337. END AddProperty;
  1338. PROCEDURE FindParameter*(identifier: Identifier): Parameter;
  1339. VAR p: Parameter;
  1340. BEGIN
  1341. p := NIL;
  1342. IF (baseType # NIL) & (baseType.resolved IS CellType) THEN
  1343. p := baseType.resolved(CellType).FindParameter(identifier);
  1344. END;
  1345. IF p = NIL THEN
  1346. p := firstParameter;
  1347. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextParameter END;
  1348. END;
  1349. RETURN p;
  1350. END FindParameter;
  1351. PROCEDURE FindProperty*(identifier: Identifier): Property;
  1352. VAR p: Property;
  1353. BEGIN
  1354. p := firstProperty;
  1355. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextProperty END;
  1356. IF p = NIL THEN
  1357. IF (baseType # NIL) & (baseType.resolved IS CellType) THEN
  1358. p := baseType.resolved(CellType).FindProperty(identifier);
  1359. END;
  1360. END;
  1361. RETURN p;
  1362. END FindProperty;
  1363. PROCEDURE SetModifiers*(flag: Modifier);
  1364. BEGIN SELF.modifiers := flag;
  1365. END SetModifiers;
  1366. PROCEDURE IsCellNet*(t: BOOLEAN);
  1367. BEGIN isCellNet := t
  1368. END IsCellNet;
  1369. PROCEDURE SameType*(this: Type): BOOLEAN;
  1370. BEGIN RETURN this = SELF
  1371. END SameType;
  1372. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1373. BEGIN RETURN SameType(to)
  1374. END CompatibleTo;
  1375. PROCEDURE IsComposite*(): BOOLEAN;
  1376. BEGIN RETURN TRUE
  1377. END IsComposite;
  1378. PROCEDURE Accept*(v: Visitor);
  1379. VAR position: LONGINT;
  1380. BEGIN position := SELF.position.start; v.VisitCellType(SELF)
  1381. END Accept;
  1382. END CellType;
  1383. (** <<procedureType = PROCEDURE [{DELEGATE}] (firstParameter .. lastParameter): returnType>>
  1384. also used as type for procedures
  1385. **)
  1386. ProcedureType* = OBJECT (Type)
  1387. VAR
  1388. modifiers-: Modifier; (* set by the parser *)
  1389. returnType-: Type;
  1390. returnTypeModifiers-: Modifier;
  1391. hasUntracedReturn-: BOOLEAN;
  1392. firstParameter-,lastParameter-: Parameter; numberParameters-: LONGINT; (* parameters *)
  1393. returnParameter-: Parameter; (* not really necessary in syntax tree but very handy for backends *)
  1394. selfParameter-: Parameter;
  1395. isDelegate-,isInterrupt-,noPAF-,noReturn-: BOOLEAN;
  1396. pcOffset-: LONGINT; (* PC offset: used for ARM interrupt procedures *)
  1397. callingConvention-: CallingConvention;
  1398. stackAlignment-: LONGINT;
  1399. parametersOffset-: LONGINT; (* stack parameter offset -- in units of addresses: one pointer = 1 *)
  1400. PROCEDURE & InitProcedureType( position: Position; scope: Scope);
  1401. BEGIN
  1402. InitType( position);
  1403. SELF.scope := scope;
  1404. modifiers := NIL;
  1405. firstParameter := NIL; lastParameter := NIL; numberParameters := 0; returnParameter := NIL;
  1406. returnType := NIL;
  1407. stackAlignment := 1;
  1408. isDelegate := FALSE; isInterrupt := FALSE; noPAF := FALSE;
  1409. callingConvention := OberonCallingConvention;
  1410. parametersOffset := 0;
  1411. pcOffset := 0;
  1412. hasUntracedReturn := FALSE;
  1413. returnTypeModifiers := NIL;
  1414. selfParameter := NIL;
  1415. END InitProcedureType;
  1416. PROCEDURE SetNoPAF*(noPAF: BOOLEAN);
  1417. BEGIN SELF.noPAF := noPAF
  1418. END SetNoPAF;
  1419. PROCEDURE SetNoReturn*(noReturn: BOOLEAN);
  1420. BEGIN SELF.noReturn := noReturn
  1421. END SetNoReturn;
  1422. PROCEDURE SetPcOffset*(pcOffset: LONGINT);
  1423. BEGIN SELF.pcOffset := pcOffset
  1424. END SetPcOffset;
  1425. PROCEDURE SetInterrupt*(isInterrupt: BOOLEAN);
  1426. BEGIN SELF.isInterrupt := isInterrupt
  1427. END SetInterrupt;
  1428. PROCEDURE SetModifiers*(flags: Modifier);
  1429. BEGIN modifiers := flags
  1430. END SetModifiers;
  1431. PROCEDURE SetReturnTypeModifiers*(flags: Modifier);
  1432. BEGIN returnTypeModifiers := flags
  1433. END SetReturnTypeModifiers;
  1434. PROCEDURE SetDelegate*(delegate: BOOLEAN);
  1435. BEGIN SELF.isDelegate := delegate; SELF.hasPointers := delegate;
  1436. END SetDelegate;
  1437. PROCEDURE SetUntracedReturn*(untraced: BOOLEAN);
  1438. BEGIN
  1439. hasUntracedReturn := untraced;
  1440. END SetUntracedReturn;
  1441. PROCEDURE SetStackAlignment*(alignment: LONGINT);
  1442. BEGIN
  1443. stackAlignment := alignment;
  1444. END SetStackAlignment;
  1445. PROCEDURE SetParametersOffset*(ofs: LONGINT);
  1446. BEGIN parametersOffset := ofs
  1447. END SetParametersOffset;
  1448. PROCEDURE SetReturnParameter*(parameter: Parameter);
  1449. BEGIN returnParameter := parameter
  1450. END SetReturnParameter;
  1451. PROCEDURE SetSelfParameter*(parameter: Parameter);
  1452. BEGIN selfParameter := parameter
  1453. END SetSelfParameter;
  1454. PROCEDURE SetCallingConvention*(cc: CallingConvention);
  1455. BEGIN callingConvention := cc
  1456. END SetCallingConvention;
  1457. PROCEDURE AddParameter*(p: Parameter);
  1458. BEGIN
  1459. ASSERT(p # NIL);
  1460. IF lastParameter= NIL THEN firstParameter := p ELSE lastParameter.nextParameter := p; p.prevParameter := lastParameter; END;
  1461. lastParameter := p;
  1462. INC(numberParameters);
  1463. ASSERT(p.access # {}); (* no hidden parameters ! *)
  1464. END AddParameter;
  1465. PROCEDURE RevertParameters*;
  1466. VAR this,next: Parameter; pnum: LONGINT;
  1467. BEGIN
  1468. pnum := numberParameters;
  1469. IF lastParameter # NIL THEN
  1470. this := lastParameter;
  1471. lastParameter := NIL;
  1472. firstParameter := NIL;
  1473. numberParameters := 0;
  1474. WHILE this # NIL DO
  1475. next := this.prevParameter;
  1476. this.prevParameter := NIL; this.nextParameter := NIL;
  1477. AddParameter(this);
  1478. this := next;
  1479. END;
  1480. END;
  1481. ASSERT(pnum = numberParameters);
  1482. END RevertParameters;
  1483. PROCEDURE SetReturnType*( type: Type );
  1484. BEGIN
  1485. returnType := type;
  1486. END SetReturnType;
  1487. PROCEDURE SameType*(this: Type): BOOLEAN;
  1488. VAR result: BOOLEAN; p1,p2: Parameter;
  1489. BEGIN
  1490. result := FALSE;
  1491. IF recursion THEN
  1492. result := TRUE
  1493. ELSIF this = SELF THEN
  1494. result := TRUE
  1495. ELSIF this IS ProcedureType THEN
  1496. recursion := TRUE;
  1497. WITH this: ProcedureType DO
  1498. result := (returnType = NIL) & (this.returnType = NIL) OR (returnType # NIL) & (this.returnType # NIL) & returnType.SameType(this.returnType.resolved);
  1499. result := result & (callingConvention = this.callingConvention);
  1500. result := result & (noReturn = this.noReturn);
  1501. result := result & (isDelegate = this.isDelegate);
  1502. IF result THEN
  1503. p1 := selfParameter; p2 := this.selfParameter;
  1504. IF (p1 = NIL) # (p2=NIL) OR (p1 # NIL) & ((p1.kind # p2.kind)) THEN
  1505. RETURN FALSE
  1506. END;
  1507. p1 := firstParameter; p2 := this.firstParameter;
  1508. WHILE (p1 # NIL) & (p2 # NIL) & (p1.access # Hidden) & (p2.access # Hidden) & (p1.kind = p2.kind) & (p1.type.SameType(p2.type) OR (p2.type.resolved # NIL) & p1.type.SameType(p2.type.resolved) OR (p1.type.resolved IS AddressType) & (p2.type.resolved IS PointerType) & p2.type.resolved(PointerType).isUnsafe) DO
  1509. p1 := p1.nextParameter; p2 := p2.nextParameter
  1510. END;
  1511. result := ((p1=NIL) OR (p1.access = Hidden)) & ((p2=NIL) OR (p2.access= Hidden));
  1512. END;
  1513. END;
  1514. END;
  1515. recursion := FALSE;
  1516. RETURN result
  1517. END SameType;
  1518. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1519. BEGIN
  1520. RETURN SameType(to) & (~isDelegate OR to(ProcedureType).isDelegate) & (~to.isRealtime OR isRealtime);
  1521. END CompatibleTo;
  1522. PROCEDURE IsComposite*(): BOOLEAN;
  1523. BEGIN RETURN isDelegate
  1524. END IsComposite;
  1525. PROCEDURE Accept*(v: Visitor);
  1526. VAR position: LONGINT;
  1527. BEGIN position := SELF.position.start; v.VisitProcedureType(SELF)
  1528. END Accept;
  1529. (** Returns if the type needs to be traced for garbage collection *)
  1530. PROCEDURE NeedsTrace*(): BOOLEAN;
  1531. BEGIN RETURN isDelegate;
  1532. END NeedsTrace;
  1533. END ProcedureType;
  1534. (**** expressions ****)
  1535. Expression* = OBJECT
  1536. VAR
  1537. type-: Type; (* the expression's type. Resolved by checker *)
  1538. assignable-: BOOLEAN; (* expression can be assigned to (or used as var-parameter): expression := ... *)
  1539. position-, end-: Position;
  1540. state-: SET;
  1541. resolved-: Value;
  1542. isHidden-: BOOLEAN;
  1543. PROCEDURE End*( position: Position);
  1544. BEGIN SELF.end := position;
  1545. END End;
  1546. PROCEDURE SetState*(state: LONGINT);
  1547. BEGIN INCL(SELF.state,state);
  1548. END SetState;
  1549. PROCEDURE &InitExpression(position: Position);
  1550. BEGIN SELF.position := position; end := invalidPosition; state := Undefined; type := NIL; assignable := FALSE; resolved := NIL; isHidden := FALSE;
  1551. END InitExpression;
  1552. PROCEDURE SetHidden*(hidden: BOOLEAN);
  1553. BEGIN isHidden := hidden
  1554. END SetHidden;
  1555. PROCEDURE SetType*(type: Type);
  1556. BEGIN
  1557. SELF.type := type;
  1558. END SetType;
  1559. PROCEDURE SetResolved*(value: Value);
  1560. BEGIN SELF.resolved := value
  1561. END SetResolved;
  1562. PROCEDURE SetAssignable*(assignable: BOOLEAN);
  1563. BEGIN SELF.assignable := assignable
  1564. END SetAssignable;
  1565. PROCEDURE Clone(): Expression;
  1566. VAR clone: Expression;
  1567. BEGIN
  1568. (* support cloning here for more robust error reporting -- should not happen normally *)
  1569. NEW(clone, position); RETURN clone
  1570. END Clone;
  1571. PROCEDURE Accept*(v: Visitor);
  1572. VAR position: LONGINT;
  1573. BEGIN position := SELF.position.start; v.VisitExpression(SELF)
  1574. END Accept;
  1575. PROCEDURE NeedsTrace* (): BOOLEAN;
  1576. BEGIN RETURN FALSE;
  1577. END NeedsTrace;
  1578. END Expression;
  1579. (** <<expression, expression, ...>> **)
  1580. ExpressionList* = OBJECT
  1581. VAR list: Basic.List;
  1582. PROCEDURE & InitList;
  1583. BEGIN NEW( list,8 );
  1584. END InitList;
  1585. PROCEDURE Length*( ): LONGINT;
  1586. BEGIN RETURN list.Length();
  1587. END Length;
  1588. PROCEDURE AddExpression*( d: Expression );
  1589. BEGIN list.Add(d)
  1590. END AddExpression;
  1591. PROCEDURE GetExpression*( index: LONGINT ): Expression;
  1592. VAR p: ANY;
  1593. BEGIN
  1594. p := list.Get(index); RETURN p(Expression);
  1595. END GetExpression;
  1596. PROCEDURE SetExpression*(index: LONGINT; expression: Expression);
  1597. BEGIN list.Set(index,expression)
  1598. END SetExpression;
  1599. PROCEDURE RemoveExpression*(i: LONGINT);
  1600. BEGIN list.RemoveByIndex(i);
  1601. END RemoveExpression;
  1602. PROCEDURE Revert*;
  1603. VAR i,j,last: LONGINT; ei,ej: ANY;
  1604. BEGIN
  1605. last := Length()-1;
  1606. FOR i := 0 TO last DO
  1607. j := last-i;
  1608. ei := list.Get(i);
  1609. ej := list.Get(j);
  1610. list.Set(i,ej);
  1611. list.Set(j,ei);
  1612. END;
  1613. END Revert;
  1614. PROCEDURE Clone*(VAR list: ExpressionList);
  1615. VAR i: LONGINT;
  1616. BEGIN
  1617. IF list = NIL THEN NEW(list) END;
  1618. FOR i := 0 TO Length()-1 DO
  1619. list.AddExpression(CloneExpression(GetExpression(i)));
  1620. END;
  1621. END Clone;
  1622. END ExpressionList;
  1623. (** << {elements} >> **)
  1624. Set* = OBJECT (Expression)
  1625. VAR elements-: ExpressionList; (* an element of the form from .. to is represented as a RangeExpression *)
  1626. PROCEDURE & InitSet( position: Position );
  1627. BEGIN
  1628. InitExpression( position );
  1629. elements := NewExpressionList();
  1630. END InitSet;
  1631. PROCEDURE Clone(): Expression;
  1632. VAR copy: Set;
  1633. BEGIN
  1634. NEW(copy, position); elements.Clone(copy.elements); RETURN copy
  1635. END Clone;
  1636. PROCEDURE Accept*(v: Visitor);
  1637. VAR position: LONGINT;
  1638. BEGIN position := SELF.position.start; v.VisitSet(SELF)
  1639. END Accept;
  1640. END Set;
  1641. (** << [elements] >> **)
  1642. MathArrayExpression* = OBJECT (Expression)
  1643. VAR elements-: ExpressionList; (* an element of the form from .. to is represented as a RangeExpression *)
  1644. PROCEDURE & InitMathArrayExpression( position: Position );
  1645. BEGIN
  1646. InitExpression( position );
  1647. elements := NewExpressionList();
  1648. END InitMathArrayExpression;
  1649. PROCEDURE Clone(): Expression;
  1650. VAR copy: MathArrayExpression;
  1651. BEGIN
  1652. NEW(copy, position); elements.Clone(copy.elements); RETURN copy
  1653. END Clone;
  1654. PROCEDURE Accept*(v: Visitor);
  1655. VAR position: LONGINT;
  1656. BEGIN position := SELF.position.start; v.VisitMathArrayExpression(SELF)
  1657. END Accept;
  1658. END MathArrayExpression;
  1659. (** <<operator left>> **)
  1660. UnaryExpression* = OBJECT (Expression)
  1661. VAR
  1662. left-: Expression;
  1663. operator-: LONGINT; (* one of Scanner.Minus ... Scanner.Not *)
  1664. PROCEDURE & InitUnaryExpression( position: Position; operand: Expression; operator: LONGINT );
  1665. BEGIN
  1666. InitExpression( position ); SELF.left := operand; SELF.operator := operator;
  1667. END InitUnaryExpression;
  1668. PROCEDURE SetLeft*(left: Expression);
  1669. BEGIN SELF.left := left
  1670. END SetLeft;
  1671. PROCEDURE Clone(): Expression;
  1672. VAR copy: UnaryExpression;
  1673. BEGIN
  1674. NEW(copy, position, CloneExpression(left), operator); RETURN copy
  1675. END Clone;
  1676. PROCEDURE Accept*(v: Visitor);
  1677. VAR position: LONGINT;
  1678. BEGIN position := SELF.position.start; v.VisitUnaryExpression(SELF)
  1679. END Accept;
  1680. END UnaryExpression;
  1681. (** <<left operator right>> **)
  1682. BinaryExpression* = OBJECT (Expression)
  1683. VAR
  1684. left-, right-: Expression;
  1685. operator-: LONGINT; (* one of Scanner.Equal ... Scanner.Minus *)
  1686. PROCEDURE & InitBinaryExpression( position: Position; left, right: Expression; operator: LONGINT );
  1687. BEGIN
  1688. InitExpression( position ); SELF.left := left; SELF.right := right; SELF.operator := operator;
  1689. END InitBinaryExpression;
  1690. PROCEDURE SetLeft*(left: Expression);
  1691. BEGIN SELF.left := left
  1692. END SetLeft;
  1693. PROCEDURE SetRight*(right: Expression);
  1694. BEGIN SELF.right := right
  1695. END SetRight;
  1696. PROCEDURE Clone(): Expression;
  1697. VAR copy: BinaryExpression;
  1698. BEGIN
  1699. NEW(copy, position, CloneExpression(left), CloneExpression(right), operator); RETURN copy
  1700. END Clone;
  1701. PROCEDURE Accept*(v: Visitor);
  1702. VAR position: LONGINT;
  1703. BEGIN position := SELF.position.start; v.VisitBinaryExpression(SELF)
  1704. END Accept;
  1705. END BinaryExpression;
  1706. (** expression that denotes a range
  1707. <<[first] '..' [last] ['by' step] | '*' >>
  1708. **)
  1709. RangeExpression* = OBJECT (Expression)
  1710. VAR
  1711. first-, last-, step-: Expression;
  1712. missingFirst-, missingLast-, missingStep-: BOOLEAN; (* only for printout*)
  1713. context-: SHORTINT; (* one of ArrayIndex, SetElement or CaseGuard *)
  1714. PROCEDURE &InitRangeExpression(position: Position; first, last, step: Expression);
  1715. BEGIN
  1716. context := ArrayIndex; (* by default, a range represents array indices *)
  1717. InitExpression(position);
  1718. missingFirst := (first = NIL);
  1719. missingLast := (last = NIL);
  1720. missingStep := (step = NIL);
  1721. SELF.first := first;
  1722. SELF.last := last;
  1723. SELF.step := step;
  1724. END InitRangeExpression;
  1725. PROCEDURE SetFirst*(first: Expression);
  1726. BEGIN
  1727. SELF.first := first
  1728. END SetFirst;
  1729. PROCEDURE SetLast*(last: Expression);
  1730. BEGIN SELF.last := last
  1731. END SetLast;
  1732. PROCEDURE SetStep*(step: Expression);
  1733. BEGIN SELF.step := step
  1734. END SetStep;
  1735. PROCEDURE SetContext*(context: SHORTINT);
  1736. BEGIN
  1737. SELF.context := context
  1738. END SetContext;
  1739. PROCEDURE Clone(): Expression;
  1740. VAR copy: RangeExpression;
  1741. BEGIN
  1742. NEW(copy, position, CloneExpression(first), CloneExpression(last), CloneExpression(step)); RETURN copy
  1743. END Clone;
  1744. PROCEDURE Accept*(v: Visitor);
  1745. VAR position: LONGINT;
  1746. BEGIN position := SELF.position.start; v.VisitRangeExpression(SELF)
  1747. END Accept;
  1748. END RangeExpression;
  1749. (** << ? >> **)
  1750. TensorRangeExpression*=OBJECT (Expression);
  1751. PROCEDURE &InitTensorRangeExpression(position: Position);
  1752. BEGIN
  1753. InitExpression(position);
  1754. END InitTensorRangeExpression;
  1755. PROCEDURE Clone(): Expression;
  1756. VAR copy: TensorRangeExpression;
  1757. BEGIN
  1758. NEW(copy, position); RETURN copy
  1759. END Clone;
  1760. PROCEDURE Accept*(v: Visitor);
  1761. VAR position: LONGINT;
  1762. BEGIN position := SELF.position.start; v.VisitTensorRangeExpression(SELF)
  1763. END Accept;
  1764. END TensorRangeExpression;
  1765. (** convert expression from expression.type to Conversion.type **)
  1766. Conversion* = OBJECT (Expression)
  1767. VAR
  1768. expression-: Expression;
  1769. typeExpression-: Expression; (* for printout *)
  1770. PROCEDURE & InitConversion( position: Position; expression: Expression; type: Type; typeExpression: Expression);
  1771. BEGIN
  1772. InitExpression( position ); SELF.expression := expression; SELF.typeExpression := typeExpression; SELF.type := type;
  1773. END InitConversion;
  1774. PROCEDURE SetExpression*(expression: Expression);
  1775. BEGIN SELF.expression := expression
  1776. END SetExpression;
  1777. PROCEDURE Clone(): Expression;
  1778. VAR copy: Conversion;
  1779. BEGIN
  1780. NEW(copy, position, CloneExpression(expression), type, CloneExpression(typeExpression)); RETURN copy
  1781. END Clone;
  1782. PROCEDURE Accept*(v: Visitor);
  1783. VAR position: LONGINT;
  1784. BEGIN position := SELF.position.start; v.VisitConversion(SELF)
  1785. END Accept;
  1786. END Conversion;
  1787. (**** designators ****)
  1788. (** abstract **)
  1789. Designator* = OBJECT(Expression)
  1790. VAR
  1791. left-: Expression; (* currently only designators are allowed but for later purposes ... (as for example (a+b).c) *)
  1792. relatedRhs-: Expression; (* the RHS of an assignment if this designator is used on the LHS: *)
  1793. (* used if this designator contains an index operator call, e.g. 'asot.^"[]"(indexList)': *)
  1794. relatedAsot-: Expression; (* the ASOT on which the index operator is called *)
  1795. relatedIndexList-: ExpressionList; (* the index list *)
  1796. modifiers-: Modifier;
  1797. PROCEDURE &InitDesignator*(position: Position);
  1798. BEGIN
  1799. InitExpression(position);
  1800. left := NIL;
  1801. relatedRhs := NIL;
  1802. relatedAsot := NIL;
  1803. relatedIndexList := NIL;
  1804. modifiers := NIL;
  1805. END InitDesignator;
  1806. PROCEDURE SetLeft*(expression: Expression);
  1807. BEGIN left := expression
  1808. END SetLeft;
  1809. PROCEDURE SetRelatedRhs*(relatedRhs: Expression);
  1810. BEGIN SELF.relatedRhs := relatedRhs
  1811. END SetRelatedRhs;
  1812. PROCEDURE SetRelatedAsot*(relatedAsot: Expression);
  1813. BEGIN SELF.relatedAsot := relatedAsot
  1814. END SetRelatedAsot;
  1815. PROCEDURE SetRelatedIndexList*(relatedIndexList: ExpressionList);
  1816. BEGIN SELF.relatedIndexList := relatedIndexList
  1817. END SetRelatedIndexList;
  1818. PROCEDURE SetModifiers*(flags: Modifier);
  1819. BEGIN modifiers := flags
  1820. END SetModifiers;
  1821. PROCEDURE Clone(): Expression;
  1822. VAR clone: Designator;
  1823. BEGIN
  1824. (* support cloning here for more robust error reporting -- should not happen normally *)
  1825. NEW(clone, position); RETURN clone
  1826. END Clone;
  1827. PROCEDURE Accept*(v: Visitor);
  1828. VAR position: LONGINT;
  1829. BEGIN position := SELF.position.start; v.VisitDesignator(SELF)
  1830. END Accept;
  1831. END Designator;
  1832. (*** first phase (parse time) designators ***)
  1833. (** <<identifier>>
  1834. may designate any symbol such as Variable, TypeDeclaration, Procedure
  1835. **)
  1836. IdentifierDesignator* = OBJECT(Designator)
  1837. VAR identifier-: Identifier;
  1838. PROCEDURE &InitIdentifierDesignator(position: Position; id: Identifier);
  1839. BEGIN InitDesignator(position); identifier := id
  1840. END InitIdentifierDesignator;
  1841. PROCEDURE Clone(): Expression;
  1842. VAR copy: IdentifierDesignator;
  1843. BEGIN
  1844. NEW(copy, position, identifier); RETURN copy
  1845. END Clone;
  1846. PROCEDURE Accept*(v: Visitor);
  1847. VAR position: LONGINT;
  1848. BEGIN position := SELF.position.start; v.VisitIdentifierDesignator(SELF)
  1849. END Accept;
  1850. END IdentifierDesignator;
  1851. (** <<left.identifier>>
  1852. may designate a record / module element (constant, type, variable, procedure)
  1853. **)
  1854. SelectorDesignator* = OBJECT (Designator)
  1855. VAR identifier-: Identifier;
  1856. PROCEDURE & InitSelector(position: Position; left: Designator; identifier: Identifier);
  1857. BEGIN InitDesignator(position); SELF.left := left; SELF.identifier := identifier;
  1858. END InitSelector;
  1859. PROCEDURE Clone(): Expression;
  1860. VAR copy: SelectorDesignator;
  1861. BEGIN
  1862. NEW(copy, position, CloneDesignator(left), identifier); RETURN copy
  1863. END Clone;
  1864. PROCEDURE Accept*(v: Visitor);
  1865. VAR position: LONGINT;
  1866. BEGIN position := SELF.position.start; v.VisitSelectorDesignator(SELF)
  1867. END Accept;
  1868. END SelectorDesignator;
  1869. (** <<left(arg1, arg2, ...)>>
  1870. may designate a function call or a type guard
  1871. **)
  1872. ParameterDesignator* = OBJECT(Designator)
  1873. VAR
  1874. parameters-: ExpressionList;
  1875. PROCEDURE &InitParameterDesignator(position: Position; left: Designator; parameters: ExpressionList);
  1876. BEGIN InitDesignator(position); SELF.left := left; SELF.parameters := parameters
  1877. END InitParameterDesignator;
  1878. PROCEDURE Clone(): Expression;
  1879. VAR copy: ParameterDesignator;
  1880. BEGIN
  1881. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  1882. END Clone;
  1883. PROCEDURE Accept*(v: Visitor);
  1884. VAR position: LONGINT;
  1885. BEGIN position := SELF.position.start; v.VisitParameterDesignator(SELF)
  1886. END Accept;
  1887. END ParameterDesignator;
  1888. (** <<left^>>
  1889. may designate a pointer dereference or a method supercall
  1890. **)
  1891. ArrowDesignator* = OBJECT (Designator)
  1892. PROCEDURE &InitArrowDesignator(position: Position; left: Designator);
  1893. BEGIN InitDesignator(position); SELF.left := left;
  1894. END InitArrowDesignator;
  1895. PROCEDURE Clone(): Expression;
  1896. VAR copy: ArrowDesignator;
  1897. BEGIN
  1898. NEW(copy, position, CloneDesignator(left(Designator))); RETURN copy
  1899. END Clone;
  1900. PROCEDURE Accept*(v: Visitor);
  1901. VAR position: LONGINT;
  1902. BEGIN position := SELF.position.start; v.VisitArrowDesignator(SELF)
  1903. END Accept;
  1904. END ArrowDesignator;
  1905. (** <<left[parameters]>>
  1906. designates an index designator, before checker
  1907. **)
  1908. BracketDesignator* = OBJECT(Designator)
  1909. VAR parameters-: ExpressionList;
  1910. PROCEDURE &InitBracketDesignator(position: Position; left: Designator; parameters: ExpressionList);
  1911. BEGIN InitDesignator(position); SELF.left := left; SELF.parameters := parameters;
  1912. END InitBracketDesignator;
  1913. PROCEDURE Clone(): Expression;
  1914. VAR copy: BracketDesignator;
  1915. BEGIN
  1916. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  1917. END Clone;
  1918. PROCEDURE Accept*(v: Visitor);
  1919. VAR position: LONGINT;
  1920. BEGIN position := SELF.position.start; v.VisitBracketDesignator(SELF)
  1921. END Accept;
  1922. END BracketDesignator;
  1923. (*** second phase (after checker) designators ***)
  1924. (** symbol designator emerged from IdentifierDesignator or from Selector **)
  1925. SymbolDesignator* = OBJECT(Designator)
  1926. VAR
  1927. symbol-: Symbol;
  1928. PROCEDURE &InitSymbolDesignator(position: Position; left: Designator; symbol: Symbol);
  1929. BEGIN
  1930. InitDesignator(position);
  1931. SELF.left := left;
  1932. SELF.symbol := symbol;
  1933. END InitSymbolDesignator;
  1934. PROCEDURE Accept*(v: Visitor);
  1935. VAR position: LONGINT;
  1936. BEGIN
  1937. position := SELF.position.start; v.VisitSymbolDesignator(SELF);
  1938. END Accept;
  1939. PROCEDURE Clone(): Expression;
  1940. VAR copy: SymbolDesignator;
  1941. BEGIN
  1942. NEW(copy, position, CloneDesignator(left), symbol); RETURN copy
  1943. END Clone;
  1944. PROCEDURE SetSymbol*(s: Symbol);
  1945. BEGIN SELF.symbol := s;
  1946. END SetSymbol;
  1947. PROCEDURE NeedsTrace* (): BOOLEAN;
  1948. BEGIN
  1949. RETURN symbol.NeedsTrace() & ((left = NIL) OR (left.NeedsTrace()));
  1950. END NeedsTrace;
  1951. END SymbolDesignator;
  1952. (** <<left[parameters]>>
  1953. (ranged) indexer
  1954. **)
  1955. IndexDesignator* = OBJECT(Designator)
  1956. VAR
  1957. parameters-: ExpressionList;
  1958. hasRange-: BOOLEAN;
  1959. hasTensorRange-: BOOLEAN;
  1960. PROCEDURE &InitIndexDesignator(position: Position; left: Designator);
  1961. BEGIN
  1962. InitDesignator(position);
  1963. SELF.left := left;
  1964. parameters := NewExpressionList();
  1965. hasRange := FALSE;
  1966. hasTensorRange := FALSE;
  1967. END InitIndexDesignator;
  1968. PROCEDURE HasRange*;
  1969. BEGIN hasRange := TRUE;
  1970. END HasRange;
  1971. PROCEDURE HasTensorRange*;
  1972. BEGIN hasTensorRange := TRUE;
  1973. END HasTensorRange;
  1974. PROCEDURE Clone(): Expression;
  1975. VAR copy: IndexDesignator;
  1976. BEGIN
  1977. NEW(copy, position, CloneDesignator(left));
  1978. parameters.Clone(copy.parameters);
  1979. copy.hasRange := hasRange; copy.hasTensorRange := hasTensorRange ; RETURN copy
  1980. END Clone;
  1981. PROCEDURE Accept*(v: Visitor);
  1982. VAR position: LONGINT;
  1983. BEGIN position := SELF.position.start; v.VisitIndexDesignator(SELF)
  1984. END Accept;
  1985. PROCEDURE NeedsTrace* (): BOOLEAN;
  1986. BEGIN RETURN type.NeedsTrace() & left.NeedsTrace(); (* for x[y]: if x is untraced, then also x[y] should be treated untraced *)
  1987. END NeedsTrace;
  1988. END IndexDesignator;
  1989. StatementDesignator* = OBJECT (Designator)
  1990. VAR
  1991. statement-: Statement;
  1992. result-: Expression;
  1993. PROCEDURE & InitStatementDesignator(position: Position; s: Statement);
  1994. BEGIN
  1995. InitDesignator(position); statement := s; result := NIL;
  1996. END InitStatementDesignator;
  1997. PROCEDURE Clone(): Expression;
  1998. VAR copy: StatementDesignator;
  1999. BEGIN
  2000. NEW(copy, position, CloneStatement(statement)) ;
  2001. copy.result := CloneExpression(result);
  2002. RETURN copy
  2003. END Clone;
  2004. PROCEDURE SetResult*(r: Expression);
  2005. BEGIN result := r
  2006. END SetResult;
  2007. PROCEDURE Accept*(v: Visitor);
  2008. VAR position: LONGINT;
  2009. BEGIN position := SELF.position.start; v.VisitStatementDesignator(SELF)
  2010. END Accept;
  2011. END StatementDesignator;
  2012. (** <<left(parameters)>>
  2013. procedure call
  2014. **)
  2015. ProcedureCallDesignator*= OBJECT (Designator)
  2016. VAR parameters-: ExpressionList;
  2017. PROCEDURE & InitProcedureCallDesignator(position: Position; left: Designator; parameters: ExpressionList);
  2018. BEGIN
  2019. InitDesignator(position); SELF.left := left; SELF.parameters := parameters;
  2020. END InitProcedureCallDesignator;
  2021. PROCEDURE Clone(): Expression;
  2022. VAR copy: ProcedureCallDesignator;
  2023. BEGIN
  2024. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  2025. END Clone;
  2026. PROCEDURE Accept*(v: Visitor);
  2027. VAR position: LONGINT;
  2028. BEGIN position := SELF.position.start; v.VisitProcedureCallDesignator(SELF)
  2029. END Accept;
  2030. END ProcedureCallDesignator;
  2031. (** <<procedure(parameters)>>
  2032. builtin procedure call **)
  2033. BuiltinCallDesignator*= OBJECT (Designator) (*! should this be an extension of a procedure call designator ? *)
  2034. VAR
  2035. id-: LONGINT;
  2036. parameters-: ExpressionList;
  2037. builtin-: Builtin;
  2038. returnType-: Type;
  2039. PROCEDURE & InitBuiltinCallDesignator(position: Position; id: LONGINT; left: Designator; parameters: ExpressionList);
  2040. BEGIN
  2041. InitDesignator(position); SELF.parameters := parameters; SELF.id := id; SELF.left := left; returnType := NIL;
  2042. END InitBuiltinCallDesignator;
  2043. PROCEDURE SetReturnType*(type: Type);
  2044. BEGIN
  2045. returnType := type (* used for NEW Type() expression *)
  2046. END SetReturnType;
  2047. PROCEDURE Clone(): Expression;
  2048. VAR copy: BuiltinCallDesignator;
  2049. BEGIN
  2050. NEW(copy, position, id, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  2051. END Clone;
  2052. PROCEDURE Accept*(v: Visitor);
  2053. VAR position: LONGINT;
  2054. BEGIN position := SELF.position.start; v.VisitBuiltinCallDesignator(SELF)
  2055. END Accept;
  2056. END BuiltinCallDesignator;
  2057. (** <<left(type)>>
  2058. resolved parameter designator, designates a type guard
  2059. **)
  2060. TypeGuardDesignator* = OBJECT(Designator)
  2061. VAR
  2062. typeExpression-: Expression; (* for printing only *)
  2063. PROCEDURE &InitTypeGuardDesignator(position: Position; left: Designator; type: Type);
  2064. BEGIN InitDesignator(position); SELF.left := left; SELF.type := type; typeExpression := NIL;
  2065. END InitTypeGuardDesignator;
  2066. PROCEDURE SetTypeExpression*(typeExpression: Expression);
  2067. BEGIN SELF.typeExpression := typeExpression
  2068. END SetTypeExpression;
  2069. PROCEDURE Clone(): Expression;
  2070. VAR copy: TypeGuardDesignator;
  2071. BEGIN
  2072. NEW(copy, position, CloneDesignator(left), type); RETURN copy
  2073. END Clone;
  2074. PROCEDURE Accept*(v: Visitor);
  2075. VAR position: LONGINT;
  2076. BEGIN position := SELF.position.start; v.VisitTypeGuardDesignator(SELF)
  2077. END Accept;
  2078. PROCEDURE NeedsTrace* (): BOOLEAN;
  2079. BEGIN RETURN left.NeedsTrace() & type.NeedsTrace(); (* for x(Y): if x is untraced, then x as Y should also be treated untraced *)
  2080. END NeedsTrace;
  2081. END TypeGuardDesignator;
  2082. (** <<left^>> resolved as dereference operation on pointer variable left **)
  2083. DereferenceDesignator*= OBJECT (Designator)
  2084. PROCEDURE &InitDereferenceDesignator(position: Position; left: Designator);
  2085. BEGIN InitDesignator(position); SELF.left := left;
  2086. END InitDereferenceDesignator;
  2087. PROCEDURE Clone(): Expression;
  2088. VAR copy: DereferenceDesignator;
  2089. BEGIN
  2090. NEW(copy, position, CloneDesignator(left)); RETURN copy
  2091. END Clone;
  2092. PROCEDURE Accept*(v: Visitor);
  2093. VAR position: LONGINT;
  2094. BEGIN position := SELF.position.start; v.VisitDereferenceDesignator(SELF)
  2095. END Accept;
  2096. PROCEDURE NeedsTrace* (): BOOLEAN;
  2097. BEGIN
  2098. (*! semantic of x.y.z := new : if x is untraced then the effect of y.z := new remains untraced!
  2099. In other words: difference between y := x.y; y.z := new and x.y.z := new.
  2100. *)
  2101. RETURN left.NeedsTrace() & type.NeedsTrace(); (* for x^: if x is an untraced pointer, the content of x^ is also treated untraced *)
  2102. END NeedsTrace;
  2103. END DereferenceDesignator;
  2104. (** <<left^>> resolved as supercall operation on method left **)
  2105. SupercallDesignator*= OBJECT (Designator)
  2106. PROCEDURE &InitSupercallDesignator(position: Position; left: Designator);
  2107. BEGIN InitDesignator(position); SELF.left := left;
  2108. END InitSupercallDesignator;
  2109. PROCEDURE Clone(): Expression;
  2110. VAR copy: SupercallDesignator;
  2111. BEGIN
  2112. NEW(copy, position, CloneDesignator(left)); RETURN copy
  2113. END Clone;
  2114. PROCEDURE Accept*(v: Visitor);
  2115. VAR position: LONGINT;
  2116. BEGIN position := SELF.position.start; v.VisitSupercallDesignator(SELF)
  2117. END Accept;
  2118. END SupercallDesignator;
  2119. (** <<SELF.x>> **)
  2120. SelfDesignator*= OBJECT (Designator)
  2121. PROCEDURE &InitSelfDesignator(position: Position);
  2122. BEGIN InitDesignator(position);
  2123. END InitSelfDesignator;
  2124. PROCEDURE Clone(): Expression;
  2125. VAR copy: SelfDesignator;
  2126. BEGIN
  2127. NEW(copy, position); RETURN copy
  2128. END Clone;
  2129. PROCEDURE Accept*(v: Visitor);
  2130. VAR position: LONGINT;
  2131. BEGIN position := SELF.position.start; v.VisitSelfDesignator(SELF)
  2132. END Accept;
  2133. PROCEDURE NeedsTrace* (): BOOLEAN;
  2134. BEGIN RETURN type.NeedsTrace();
  2135. END NeedsTrace;
  2136. END SelfDesignator;
  2137. (** <<RESULT>> **)
  2138. ResultDesignator*= OBJECT (Designator)
  2139. PROCEDURE &InitResultDesignator(position: Position);
  2140. BEGIN InitDesignator(position);
  2141. END InitResultDesignator;
  2142. PROCEDURE Clone(): Expression;
  2143. VAR copy: ResultDesignator;
  2144. BEGIN
  2145. NEW(copy, position); RETURN copy
  2146. END Clone;
  2147. PROCEDURE Accept*(v: Visitor);
  2148. VAR position: LONGINT;
  2149. BEGIN position := SELF.position.start; v.VisitResultDesignator(SELF)
  2150. END Accept;
  2151. END ResultDesignator;
  2152. (**** values ****)
  2153. Value* = OBJECT (Expression)
  2154. VAR fingerprint-: FingerPrint;
  2155. PROCEDURE &InitValue(position: Position);
  2156. BEGIN SELF.position := position; resolved := SELF; InitFingerPrint(fingerprint);
  2157. END InitValue;
  2158. PROCEDURE SetFingerPrint*(CONST fp: FingerPrint);
  2159. BEGIN
  2160. SELF.fingerprint := fp
  2161. END SetFingerPrint;
  2162. PROCEDURE Accept*(v: Visitor);
  2163. VAR position: LONGINT;
  2164. BEGIN position := SELF.position.start; v.VisitValue(SELF)
  2165. END Accept;
  2166. PROCEDURE Equals*(v: Value):BOOLEAN;
  2167. BEGIN HALT(100); (* abstract *) RETURN FALSE; END Equals;
  2168. END Value;
  2169. (** <<value = TRUE , FALSE>> **)
  2170. BooleanValue* = OBJECT (Value)
  2171. VAR
  2172. value-: BOOLEAN;
  2173. PROCEDURE & InitBooleanValue(position: Position; value: BOOLEAN);
  2174. BEGIN
  2175. InitValue(position); SELF.value := value;
  2176. END InitBooleanValue;
  2177. PROCEDURE SetValue*(value: BOOLEAN);
  2178. BEGIN SELF.value := value
  2179. END SetValue;
  2180. PROCEDURE Clone(): Expression;
  2181. VAR copy: BooleanValue;
  2182. BEGIN
  2183. NEW(copy, position, value); RETURN copy
  2184. END Clone;
  2185. PROCEDURE Accept*(v: Visitor);
  2186. VAR position: LONGINT;
  2187. BEGIN position := SELF.position.start; v.VisitBooleanValue(SELF)
  2188. END Accept;
  2189. PROCEDURE Equals*(v: Value):BOOLEAN;
  2190. BEGIN RETURN (v IS BooleanValue) & (v(BooleanValue).value = value); END Equals;
  2191. END BooleanValue;
  2192. (** <<value = 123456>> **)
  2193. IntegerValue* = OBJECT (Value)
  2194. VAR
  2195. hvalue-: HUGEINT;
  2196. value-: LONGINT;
  2197. PROCEDURE & InitIntegerValue(position: Position; hvalue: HUGEINT);
  2198. BEGIN
  2199. InitValue(position); SELF.hvalue := hvalue; SELF.value := SHORT(hvalue);
  2200. END InitIntegerValue;
  2201. PROCEDURE SetValue*(hvalue: HUGEINT);
  2202. BEGIN SELF.hvalue := hvalue; SELF.value := SHORT(hvalue);
  2203. END SetValue;
  2204. PROCEDURE Clone(): Expression;
  2205. VAR copy: IntegerValue;
  2206. BEGIN
  2207. NEW(copy, position, hvalue); RETURN copy
  2208. END Clone;
  2209. PROCEDURE Accept*(v: Visitor);
  2210. VAR position: LONGINT;
  2211. BEGIN position := SELF.position.start; v.VisitIntegerValue(SELF)
  2212. END Accept;
  2213. PROCEDURE Equals*(v: Value):BOOLEAN;
  2214. BEGIN RETURN (v IS IntegerValue) & (v(IntegerValue).value = value); END Equals;
  2215. END IntegerValue;
  2216. (** <<value = 'c', 65X>> **)
  2217. CharacterValue*= OBJECT(Value)
  2218. VAR
  2219. value-: CHAR; (* potential for extension to support CHAR16 and CHAR32 *)
  2220. PROCEDURE & InitCharacterValue(position: Position; value: CHAR);
  2221. BEGIN
  2222. InitValue(position); SELF.value := value;
  2223. END InitCharacterValue;
  2224. PROCEDURE SetValue*(value: CHAR);
  2225. BEGIN SELF.value := value
  2226. END SetValue;
  2227. PROCEDURE Clone(): Expression;
  2228. VAR copy: CharacterValue;
  2229. BEGIN
  2230. NEW(copy, position, value); RETURN copy
  2231. END Clone;
  2232. PROCEDURE Accept*(v: Visitor);
  2233. VAR position: LONGINT;
  2234. BEGIN position := SELF.position.start; v.VisitCharacterValue(SELF)
  2235. END Accept;
  2236. PROCEDURE Equals*(v: Value):BOOLEAN;
  2237. BEGIN RETURN (v IS CharacterValue) & (v(CharacterValue).value = value); END Equals;
  2238. END CharacterValue;
  2239. SetValueType = SetValue;
  2240. (** <<value = {1,2,3..5}>> **)
  2241. SetValue* = OBJECT (Value)
  2242. VAR
  2243. value-: SET;
  2244. PROCEDURE & InitSetValue(position: Position; value: SET);
  2245. BEGIN
  2246. InitValue(position); SELF.value := value;
  2247. END InitSetValue;
  2248. PROCEDURE SetValue*(value: SET);
  2249. BEGIN SELF.value := value
  2250. END SetValue;
  2251. PROCEDURE Clone(): Expression;
  2252. VAR copy: SetValueType;
  2253. BEGIN
  2254. NEW(copy, position, value); RETURN copy
  2255. END Clone;
  2256. PROCEDURE Accept*(v: Visitor);
  2257. VAR position: LONGINT;
  2258. BEGIN position := SELF.position.start; v.VisitSetValue(SELF)
  2259. END Accept;
  2260. END SetValue;
  2261. (** << [elements] >> **)
  2262. MathArrayValue* = OBJECT (Value)
  2263. VAR array-: MathArrayExpression; (* an element of the form from .. to is represented as a RangeExpression *)
  2264. PROCEDURE & InitMathArrayValue(position: Position);
  2265. BEGIN
  2266. InitValue(position);
  2267. array := NIL;
  2268. END InitMathArrayValue;
  2269. PROCEDURE SetArray*(array: MathArrayExpression);
  2270. BEGIN SELF.array := array
  2271. END SetArray;
  2272. PROCEDURE Clone(): Expression;
  2273. VAR copy: MathArrayValue;
  2274. BEGIN
  2275. NEW(copy, position);
  2276. IF array # NIL THEN copy.array := array.Clone()(MathArrayExpression) END;
  2277. RETURN copy
  2278. END Clone;
  2279. PROCEDURE Accept*(v: Visitor);
  2280. VAR position: LONGINT;
  2281. BEGIN position := SELF.position.start; v.VisitMathArrayValue(SELF)
  2282. END Accept;
  2283. END MathArrayValue;
  2284. (** <<value = 1.2345E01>> **)
  2285. RealValue* = OBJECT (Value)
  2286. VAR
  2287. value-: LONGREAL;
  2288. subtype-: LONGINT; (* accuracy information: REAL vs. LONGREAL *)
  2289. PROCEDURE & InitRealValue(position: Position; value: LONGREAL);
  2290. BEGIN
  2291. InitValue(position); SELF.value := value; SELF.subtype := 0;
  2292. END InitRealValue;
  2293. PROCEDURE SetValue*(value: LONGREAL);
  2294. BEGIN SELF.value := value
  2295. END SetValue;
  2296. PROCEDURE SetSubtype*(subtype: LONGINT);
  2297. BEGIN SELF.subtype := subtype;
  2298. END SetSubtype;
  2299. PROCEDURE Clone(): Expression;
  2300. VAR copy: RealValue;
  2301. BEGIN
  2302. NEW(copy, position, value); RETURN copy
  2303. END Clone;
  2304. PROCEDURE Accept*(v: Visitor);
  2305. VAR position: LONGINT;
  2306. BEGIN position := SELF.position.start; v.VisitRealValue(SELF)
  2307. END Accept;
  2308. PROCEDURE Equals*(v: Value):BOOLEAN;
  2309. BEGIN RETURN (v IS RealValue) & (v(RealValue).value = value); END Equals;
  2310. END RealValue;
  2311. ComplexValue* = OBJECT (Value)
  2312. VAR
  2313. realValue-, imagValue-: LONGREAL;
  2314. subtype-: LONGINT; (* accuracy information of components: REAL vs. LONGREAL *)
  2315. PROCEDURE & InitComplexValue(position: Position; realValue, imagValue: LONGREAL);
  2316. BEGIN
  2317. InitValue(position); SELF.realValue := realValue; SELF.imagValue := imagValue; SELF.subtype := 0;
  2318. END InitComplexValue;
  2319. PROCEDURE SetValue*(realValue, imagValue: LONGREAL);
  2320. BEGIN SELF.realValue := realValue; SELF.imagValue := imagValue;
  2321. END SetValue;
  2322. PROCEDURE UpdateSubtype*;
  2323. BEGIN
  2324. ASSERT((type # NIL) & (type.resolved # NIL) & (type.resolved IS ComplexType) & (type.resolved(ComplexType).componentType IS FloatType));
  2325. CASE type.resolved(ComplexType).componentType(FloatType).sizeInBits OF
  2326. | 32: subtype := Scanner.Real
  2327. | 64: subtype := Scanner.Longreal
  2328. END
  2329. END UpdateSubtype;
  2330. PROCEDURE SetSubtype*(subtype: LONGINT);
  2331. BEGIN SELF.subtype := subtype;
  2332. END SetSubtype;
  2333. PROCEDURE Clone(): Expression;
  2334. VAR copy: ComplexValue;
  2335. BEGIN
  2336. NEW(copy, position, realValue, imagValue); copy.subtype := subtype; RETURN copy
  2337. END Clone;
  2338. PROCEDURE Accept*(v: Visitor);
  2339. VAR position: LONGINT;
  2340. BEGIN position := SELF.position.start; v.VisitComplexValue(SELF)
  2341. END Accept;
  2342. PROCEDURE Equals*(v: Value):BOOLEAN;
  2343. BEGIN RETURN (v IS ComplexValue) & (v(ComplexValue).realValue = realValue) & (v(ComplexValue).imagValue = imagValue);
  2344. (* TODO: append this?
  2345. OR (v IS RealValue) & (v(RealValue).value = realValue) & (imagValue := 0)
  2346. *)
  2347. END Equals;
  2348. END ComplexValue;
  2349. (** <<value = "string">> **)
  2350. StringValue* = OBJECT (Value)
  2351. VAR
  2352. value-: String;
  2353. length-: LONGINT;
  2354. PROCEDURE & InitStringValue(position: Position; value: String);
  2355. BEGIN
  2356. InitValue(position); SELF.value := value;
  2357. length := 0;
  2358. WHILE (length<LEN(value)) & (value[length] # 0X) DO
  2359. INC(length);
  2360. END;
  2361. IF length < LEN(value) THEN INC(length) END
  2362. END InitStringValue;
  2363. PROCEDURE SetValue*(CONST value: String);
  2364. BEGIN SELF.value := value
  2365. END SetValue;
  2366. PROCEDURE Append*(CONST value: String);
  2367. VAR new: String; len: LONGINT;
  2368. BEGIN
  2369. len := Strings.Length(SELF.value^) + Strings.Length(value^) + 1;
  2370. IF LEN(SELF.value) < len THEN
  2371. NEW(new, len);
  2372. COPY(SELF.value^, new^);
  2373. SELF.value := new
  2374. END;
  2375. Strings.Append(SELF.value^, value^);
  2376. length := len
  2377. END Append;
  2378. PROCEDURE AppendChar*(CONST ch: CHAR);
  2379. VAR v: String;
  2380. BEGIN
  2381. NEW(v,2); v[0] := ch;
  2382. Append(v);
  2383. END AppendChar;
  2384. PROCEDURE Clone(): Expression;
  2385. VAR copy: StringValue;
  2386. BEGIN
  2387. NEW(copy, position, value); RETURN copy
  2388. END Clone;
  2389. PROCEDURE Accept*(v: Visitor);
  2390. VAR position: LONGINT;
  2391. BEGIN position := SELF.position.start; v.VisitStringValue(SELF)
  2392. END Accept;
  2393. PROCEDURE Equals*(v: Value):BOOLEAN;
  2394. BEGIN RETURN (v IS StringValue) & (v(StringValue).value = value); END Equals;
  2395. END StringValue;
  2396. (** <<value = NIL>> **)
  2397. NilValue* = OBJECT (Value)
  2398. PROCEDURE Accept*(v: Visitor);
  2399. VAR position: LONGINT;
  2400. BEGIN position := SELF.position.start; v.VisitNilValue(SELF)
  2401. END Accept;
  2402. PROCEDURE Clone(): Expression;
  2403. VAR copy: NilValue;
  2404. BEGIN
  2405. NEW(copy, position); RETURN copy
  2406. END Clone;
  2407. PROCEDURE Equals*(v: Value):BOOLEAN;
  2408. BEGIN RETURN (v IS NilValue); END Equals;
  2409. END NilValue;
  2410. (** <<value = enum.x >> **)
  2411. EnumerationValue* = OBJECT (Value)
  2412. VAR
  2413. value-: LONGINT;
  2414. PROCEDURE & InitEnumerationValue(position: Position; value: LONGINT);
  2415. BEGIN
  2416. InitValue(position); SELF.value := value;
  2417. END InitEnumerationValue;
  2418. PROCEDURE SetValue*(value: LONGINT);
  2419. BEGIN SELF.value := value
  2420. END SetValue;
  2421. PROCEDURE Clone(): Expression;
  2422. VAR copy: EnumerationValue;
  2423. BEGIN
  2424. NEW(copy, position, value); RETURN copy
  2425. END Clone;
  2426. PROCEDURE Accept*(v: Visitor);
  2427. VAR position: LONGINT;
  2428. BEGIN position := SELF.position.start; v.VisitEnumerationValue(SELF)
  2429. END Accept;
  2430. PROCEDURE Equals*(v: Value):BOOLEAN;
  2431. BEGIN RETURN (v IS EnumerationValue) & (v(EnumerationValue).value = value); END Equals;
  2432. END EnumerationValue;
  2433. (**** symbols ****)
  2434. Symbol*= OBJECT
  2435. VAR
  2436. nextSymbol-: Symbol;
  2437. name-: Identifier; (* constant / variable / parameter / type name / module name *)
  2438. externalName-: Scanner.StringType; (* variable / procedure *)
  2439. access-: SET; (* access flags (exported, readonly etc.) *)
  2440. type-: Type; (* type of constant / variable / parameter / procedure return type *)
  2441. scope-:Scope; (* container of symbol *)
  2442. offsetInBits-: LONGINT; (* offset in stack or heap, in bits *)
  2443. used-, written-: BOOLEAN;
  2444. fixed-: BOOLEAN;
  2445. alignment-: LONGINT;
  2446. position-, end-: Position; state-: SET;
  2447. fingerprint-: FingerPrint;
  2448. comment-: Comment;
  2449. PROCEDURE & InitSymbol(position: Position; name:Identifier);
  2450. BEGIN
  2451. SELF.position := position; state := Undefined;
  2452. SELF.end := invalidPosition;
  2453. nextSymbol := NIL;
  2454. SELF.name := name;
  2455. externalName := NIL;
  2456. scope:= NIL;
  2457. type := NIL;
  2458. access := Internal;
  2459. state := Undefined;
  2460. offsetInBits := MIN(LONGINT);
  2461. alignment := 0; (* take default *)
  2462. fixed := FALSE;
  2463. used := FALSE; written := FALSE;
  2464. InitFingerPrint(fingerprint);
  2465. comment := NIL;
  2466. END InitSymbol;
  2467. PROCEDURE SetAlignment*(fix: BOOLEAN; align: LONGINT);
  2468. BEGIN SELF.alignment := align; fixed := fix;
  2469. END SetAlignment;
  2470. PROCEDURE SetFingerPrint*(CONST fp: FingerPrint);
  2471. BEGIN
  2472. SELF.fingerprint := fp
  2473. END SetFingerPrint;
  2474. PROCEDURE SetState*(state: LONGINT);
  2475. BEGIN INCL(SELF.state,state);
  2476. END SetState;
  2477. PROCEDURE SetScope*(scope: Scope);
  2478. BEGIN SELF.scope := scope
  2479. END SetScope;
  2480. PROCEDURE SetType*(type: Type);
  2481. BEGIN
  2482. SELF.type := type;
  2483. END SetType;
  2484. PROCEDURE SetNext*(symbol: Symbol);
  2485. BEGIN SELF.nextSymbol := symbol; END SetNext;
  2486. PROCEDURE SetAccess*(access: SET);
  2487. BEGIN
  2488. (* consistency guarantee *)
  2489. IF PublicWrite IN access THEN ASSERT(ProtectedWrite IN access) END;
  2490. IF ProtectedWrite IN access THEN ASSERT(InternalWrite IN access) END;
  2491. IF PublicRead IN access THEN ASSERT(ProtectedRead IN access) END;
  2492. IF ProtectedRead IN access THEN ASSERT(InternalRead IN access)END;
  2493. SELF.access := access;
  2494. END SetAccess;
  2495. PROCEDURE SetOffset*(ofs: LONGINT);
  2496. BEGIN offsetInBits := ofs
  2497. END SetOffset;
  2498. PROCEDURE MarkUsed*;
  2499. BEGIN used := TRUE
  2500. END MarkUsed;
  2501. PROCEDURE MarkWritten*;
  2502. BEGIN written := TRUE
  2503. END MarkWritten;
  2504. PROCEDURE GetName*(VAR str: ARRAY OF CHAR);
  2505. BEGIN Basic.GetString(name, str);
  2506. END GetName;
  2507. PROCEDURE SetComment*(comment: Comment);
  2508. BEGIN SELF.comment := comment
  2509. END SetComment;
  2510. PROCEDURE Accept*(v: Visitor);
  2511. VAR position: LONGINT;
  2512. BEGIN position := SELF.position.start; v.VisitSymbol(SELF)
  2513. END Accept;
  2514. PROCEDURE SetExternalName*(name: Scanner.StringType);
  2515. BEGIN externalName := name;
  2516. END SetExternalName;
  2517. PROCEDURE NeedsTrace* (): BOOLEAN;
  2518. BEGIN RETURN FALSE;
  2519. END NeedsTrace;
  2520. (* If a symbol needs to be vieible in the object file
  2521. A symbol needs to be visible in an object file when it is require during linking
  2522. This is the case for exported symbols but also for methods in a method table, for instance.
  2523. *)
  2524. PROCEDURE NeedsSection*(): BOOLEAN;
  2525. BEGIN
  2526. RETURN access * Public # {};
  2527. END NeedsSection;
  2528. END Symbol;
  2529. (**
  2530. <<TYPE name = declaredType>>
  2531. TypeDeclaration symbol represents a type declaration of the form TYPE name = declaredType.
  2532. Note that the declared type is not stored in the symbol's type field but rather in the declaredType field.
  2533. The type of a type declaration is set to "typeDeclarationType" in the semantic checker
  2534. **)
  2535. TypeDeclaration*= OBJECT(Symbol)
  2536. VAR
  2537. nextTypeDeclaration-: TypeDeclaration;
  2538. declaredType-: Type;
  2539. PROCEDURE &InitTypeDeclaration(position: Position; name: Identifier);
  2540. BEGIN
  2541. InitSymbol(position,name);
  2542. nextTypeDeclaration := NIL;
  2543. declaredType := NIL;
  2544. type := typeDeclarationType;
  2545. END InitTypeDeclaration;
  2546. PROCEDURE SetDeclaredType*(type: Type);
  2547. BEGIN
  2548. declaredType := type;
  2549. IF ~(type IS BasicType) THEN
  2550. type.typeDeclaration := SELF;
  2551. END;
  2552. END SetDeclaredType;
  2553. PROCEDURE SetType*(type: Type);
  2554. BEGIN
  2555. ASSERT(type = typeDeclarationType);
  2556. END SetType;
  2557. PROCEDURE Accept*(v: Visitor);
  2558. VAR position: LONGINT;
  2559. BEGIN position := SELF.position.start; v.VisitTypeDeclaration(SELF)
  2560. END Accept;
  2561. (* type declarations should be generally included in object files *)
  2562. PROCEDURE NeedsSection*(): BOOLEAN;
  2563. BEGIN
  2564. RETURN TRUE;
  2565. END NeedsSection;
  2566. END TypeDeclaration;
  2567. (** <<CONST name = value >>
  2568. Constant declaration symbol. Represents a constant being defined in the form CONST name = value
  2569. The type of the constant is stored in the type field and is resolved by the semantic checker.
  2570. **)
  2571. Constant* = OBJECT (Symbol)
  2572. VAR
  2573. value-: Expression;
  2574. nextConstant-: Constant;
  2575. PROCEDURE & InitConstant( position: Position; name: Identifier );
  2576. BEGIN
  2577. InitSymbol(position,name);
  2578. value := NIL;
  2579. nextConstant := NIL;
  2580. END InitConstant;
  2581. PROCEDURE SetValue*( value: Expression );
  2582. BEGIN
  2583. SELF.value := value;
  2584. END SetValue;
  2585. PROCEDURE Accept*(v: Visitor);
  2586. VAR position: LONGINT;
  2587. BEGIN position := SELF.position.start; v.VisitConstant(SELF)
  2588. END Accept;
  2589. END Constant;
  2590. (** <<VAR name: type >>
  2591. Variable declaration symbol. Represents a variable defined in the form VAR name: Type.
  2592. The type of the variable is stored in the symbol's type field and is resolved by the semantic checker.
  2593. **)
  2594. Variable* = OBJECT (Symbol)
  2595. VAR
  2596. nextVariable-: Variable;
  2597. untraced-: BOOLEAN;
  2598. fictive-: BOOLEAN; (* variable is not allocated but has a fixed offset *)
  2599. fictiveOffset-: LONGINT; (* offset of fictive as provided by the source code *)
  2600. useRegister-: BOOLEAN; registerNumber-: LONGINT;
  2601. modifiers-: Modifier;
  2602. initializer-: Expression;
  2603. usedAsReference-: BOOLEAN;
  2604. PROCEDURE & InitVariable*( position: Position; name: Identifier);
  2605. BEGIN
  2606. InitSymbol(position,name);
  2607. nextVariable := NIL;
  2608. modifiers := NIL;
  2609. untraced := FALSE;
  2610. modifiers := NIL;
  2611. useRegister := FALSE;
  2612. registerNumber := -1;
  2613. usedAsReference := FALSE;
  2614. initializer := NIL;
  2615. fictive := FALSE;
  2616. END InitVariable;
  2617. PROCEDURE UsedAsReference*;
  2618. BEGIN
  2619. usedAsReference := TRUE
  2620. END UsedAsReference;
  2621. PROCEDURE SetUntraced*(u: BOOLEAN);
  2622. BEGIN untraced := u
  2623. END SetUntraced;
  2624. PROCEDURE SetUseRegister*(u: BOOLEAN);
  2625. BEGIN
  2626. useRegister := u
  2627. END SetUseRegister;
  2628. PROCEDURE SetRegisterNumber*(reg: LONGINT);
  2629. BEGIN
  2630. registerNumber := reg
  2631. END SetRegisterNumber;
  2632. PROCEDURE SetFictive*(offset: LONGINT);
  2633. BEGIN
  2634. fictive := TRUE;
  2635. fictiveOffset := offset;
  2636. END SetFictive;
  2637. PROCEDURE SetModifiers*(flag: Modifier);
  2638. BEGIN SELF.modifiers := flag;
  2639. END SetModifiers;
  2640. PROCEDURE SetInitializer*(initializer: Expression);
  2641. BEGIN SELF.initializer := initializer;
  2642. END SetInitializer;
  2643. PROCEDURE Accept*(v: Visitor);
  2644. VAR position: LONGINT;
  2645. BEGIN position := SELF.position.start; v.VisitVariable(SELF)
  2646. END Accept;
  2647. PROCEDURE NeedsTrace* (): BOOLEAN;
  2648. BEGIN RETURN ~untraced & (externalName = NIL) & type.NeedsTrace ();
  2649. END NeedsTrace;
  2650. END Variable;
  2651. (** << [VAR | CONST] name: type >>
  2652. Parameter declaration symbol. Represents a parameter in the form [VAR | CONST] name: Type.
  2653. The parameter's type is stored in the symbol's type field and is resolved by the semantic checker.
  2654. **)
  2655. Parameter* = OBJECT (Symbol)
  2656. VAR
  2657. nextParameter-, prevParameter-: Parameter;
  2658. modifiers-: Modifier;
  2659. defaultValue-: Expression;
  2660. kind-: LONGINT; (* ValueParameter, ConstParameter, VarParameter *)
  2661. ownerType-: Type;
  2662. untraced-: BOOLEAN;
  2663. movable-: BOOLEAN;
  2664. selfParameter-: BOOLEAN;
  2665. PROCEDURE & InitParameter( position: Position; ownerType: Type ; name: Identifier; kind: LONGINT);
  2666. BEGIN
  2667. InitSymbol( position, name );
  2668. SELF.kind := kind;
  2669. IF kind = ConstParameter THEN access := access END;
  2670. nextParameter := NIL;
  2671. SELF.ownerType := ownerType;
  2672. modifiers := NIL;
  2673. untraced := FALSE;
  2674. defaultValue := NIL;
  2675. movable := FALSE;
  2676. selfParameter := FALSE;
  2677. END InitParameter;
  2678. PROCEDURE SetModifiers*(flag: Modifier);
  2679. BEGIN SELF.modifiers := flag;
  2680. END SetModifiers;
  2681. PROCEDURE SetUntraced*(untraced: BOOLEAN);
  2682. BEGIN SELF.untraced := untraced
  2683. END SetUntraced;
  2684. PROCEDURE SetMoveable*(movable: BOOLEAN);
  2685. BEGIN SELF.movable := movable
  2686. END SetMoveable;
  2687. PROCEDURE SetSelfParameter*(b: BOOLEAN);
  2688. BEGIN
  2689. selfParameter := b;
  2690. END SetSelfParameter;
  2691. PROCEDURE SetDefaultValue*(e: Expression);
  2692. BEGIN defaultValue := e
  2693. END SetDefaultValue;
  2694. PROCEDURE Accept*(v: Visitor);
  2695. VAR position: LONGINT;
  2696. BEGIN position := SELF.position.start; v.VisitParameter(SELF)
  2697. END Accept;
  2698. PROCEDURE SetKind*(kind: LONGINT);
  2699. BEGIN SELF.kind := kind; END SetKind;
  2700. PROCEDURE NeedsTrace* (): BOOLEAN;
  2701. BEGIN RETURN ~untraced & type.NeedsTrace ();
  2702. END NeedsTrace;
  2703. END Parameter;
  2704. Property* = OBJECT (Variable)
  2705. VAR
  2706. nextProperty-, prevProperty-: Property;
  2707. value-: Expression;
  2708. PROCEDURE & InitProperty(position: Position; name: Identifier);
  2709. BEGIN
  2710. InitSymbol( position, name );
  2711. END InitProperty;
  2712. PROCEDURE SetValue*(e: Expression);
  2713. BEGIN value := e
  2714. END SetValue;
  2715. PROCEDURE Accept*(v: Visitor);
  2716. VAR position: LONGINT;
  2717. BEGIN position := SELF.position.start; v.VisitProperty(SELF)
  2718. END Accept;
  2719. END Property;
  2720. (** Procedure declaration symbol. Represents a procedure being defined in the form PROCEDURE name(parameters): returnType;
  2721. Note that the type of a procedure is a ProcedureType (and not the return type of the procedure).
  2722. Parameters, local variables, constants and type declarations are stored in the procedureScope field.
  2723. **)
  2724. Procedure* = OBJECT (Symbol)
  2725. VAR
  2726. nextProcedure-: Procedure;
  2727. procedureScope- : ProcedureScope;
  2728. super-: Procedure;
  2729. level-, methodNumber-: LONGINT;
  2730. isBodyProcedure-, isConstructor-,isFinalizer-,isInline-,isOberonInline-, isEntry-, isExit-,isFinal-,isAbstract-,isOverwritten-: BOOLEAN;
  2731. PROCEDURE & InitProcedure( position: Position; name: Identifier; scope: ProcedureScope);
  2732. BEGIN
  2733. InitSymbol(position,name);
  2734. nextProcedure := NIL;
  2735. procedureScope := scope;
  2736. ASSERT(scope.ownerProcedure = NIL); (* cannot register twice ! *)
  2737. scope.ownerProcedure := SELF;
  2738. super := NIL;
  2739. level := 0;
  2740. methodNumber := -1;
  2741. isBodyProcedure := FALSE;
  2742. isConstructor := FALSE;
  2743. isFinalizer := FALSE;
  2744. isInline := FALSE;
  2745. isOberonInline := FALSE;
  2746. isEntry := FALSE;
  2747. isExit := FALSE;
  2748. isFinal := FALSE;
  2749. isAbstract := FALSE;
  2750. isOverwritten := FALSE;
  2751. END InitProcedure;
  2752. PROCEDURE SetSuper*(super: Procedure);
  2753. BEGIN
  2754. SELF.super := super
  2755. END SetSuper;
  2756. PROCEDURE SetBodyProcedure*(isBodyProcedure: BOOLEAN);
  2757. BEGIN SELF.isBodyProcedure := isBodyProcedure;
  2758. END SetBodyProcedure;
  2759. PROCEDURE SetConstructor*(isConstructor: BOOLEAN);
  2760. BEGIN SELF.isConstructor := isConstructor
  2761. END SetConstructor;
  2762. PROCEDURE SetFinalizer*(isFinalizer: BOOLEAN);
  2763. BEGIN SELF.isFinalizer := isFinalizer
  2764. END SetFinalizer;
  2765. PROCEDURE SetInline*(isInline: BOOLEAN);
  2766. BEGIN SELF.isInline := isInline
  2767. END SetInline;
  2768. PROCEDURE SetOberonInline*(isInline: BOOLEAN);
  2769. BEGIN SELF.isOberonInline := isInline
  2770. END SetOberonInline;
  2771. PROCEDURE SetEntry*(entry: BOOLEAN);
  2772. BEGIN SELF.isEntry := entry
  2773. END SetEntry;
  2774. PROCEDURE SetExit*(exit: BOOLEAN);
  2775. BEGIN SELF.isExit := exit
  2776. END SetExit;
  2777. PROCEDURE SetFinal*(final: BOOLEAN);
  2778. BEGIN SELF.isFinal := final
  2779. END SetFinal;
  2780. PROCEDURE SetOverwritten*(locallyOverwritten: BOOLEAN);
  2781. BEGIN SELF.isOverwritten := locallyOverwritten
  2782. END SetOverwritten;
  2783. PROCEDURE SetAbstract*(abstract: BOOLEAN);
  2784. BEGIN SELF.isAbstract := abstract
  2785. END SetAbstract;
  2786. PROCEDURE SetLevel*(level: LONGINT);
  2787. BEGIN SELF.level := level
  2788. END SetLevel;
  2789. PROCEDURE SetMethodNumber*(methodNumber: LONGINT);
  2790. BEGIN SELF.methodNumber := methodNumber
  2791. END SetMethodNumber;
  2792. PROCEDURE NeedsSection*(): BOOLEAN;
  2793. BEGIN
  2794. RETURN (access * Public # {}) OR (methodNumber >= 0);
  2795. END NeedsSection;
  2796. PROCEDURE Accept*(v: Visitor);
  2797. VAR position: LONGINT;
  2798. BEGIN position := SELF.position.start; v.VisitProcedure(SELF)
  2799. END Accept;
  2800. END Procedure;
  2801. (** Builtin symbol stands for a builtin procedure. Is resolved by the semantic checker. **)
  2802. Builtin* = OBJECT (Symbol)
  2803. VAR
  2804. nextBuiltin-: Builtin;
  2805. id-: LONGINT;
  2806. PROCEDURE & InitBuiltin(position: Position; name:Identifier; id: LONGINT);
  2807. BEGIN
  2808. InitSymbol(position,name); SELF.id := id;
  2809. END InitBuiltin;
  2810. PROCEDURE Accept*(v: Visitor);
  2811. VAR position: LONGINT;
  2812. BEGIN position := SELF.position.start; v.VisitBuiltin(SELF)
  2813. END Accept;
  2814. END Builtin;
  2815. CustomBuiltin*=OBJECT (Builtin)
  2816. VAR
  2817. subType-: SHORTINT;
  2818. PROCEDURE & InitCustomBuiltin(position: Position; name: Identifier; id: LONGINT; subType: SHORTINT);
  2819. BEGIN
  2820. InitBuiltin(position,name,id);
  2821. SELF.subType := subType;
  2822. END InitCustomBuiltin;
  2823. (* TODO: check if this is correct *)
  2824. PROCEDURE CompatibleTo*(otherType: Type): BOOLEAN;
  2825. BEGIN RETURN FALSE
  2826. END CompatibleTo;
  2827. END CustomBuiltin;
  2828. Operator* = OBJECT (Procedure)
  2829. VAR
  2830. nextOperator-: Operator;
  2831. isDynamic-: BOOLEAN; (* nopov *)
  2832. PROCEDURE & InitOperator(position: Position; name: Identifier; scope: ProcedureScope);
  2833. BEGIN
  2834. InitProcedure(position,name,scope);
  2835. nextOperator := NIL;
  2836. isDynamic := FALSE
  2837. END InitOperator;
  2838. (* nopov *)
  2839. PROCEDURE SetDynamic*(isDynamic: BOOLEAN);
  2840. BEGIN SELF.isDynamic := isDynamic
  2841. END SetDynamic;
  2842. PROCEDURE Accept*(v: Visitor);
  2843. VAR position: LONGINT;
  2844. BEGIN position := SELF.position.start; v.VisitOperator(SELF)
  2845. END Accept;
  2846. END Operator;
  2847. Import* = OBJECT (Symbol)
  2848. VAR
  2849. nextImport-: Import;
  2850. module-: Module;
  2851. moduleName-: Identifier;
  2852. context-: Identifier;
  2853. direct-: BOOLEAN; (* direct import *)
  2854. PROCEDURE & InitImport( position: Position; name, moduleName: Identifier; direct: BOOLEAN );
  2855. BEGIN
  2856. InitSymbol(position,name);
  2857. SELF.direct := direct;
  2858. module := NIL;
  2859. context := invalidIdentifier;
  2860. SELF.moduleName := moduleName;
  2861. type := importType;
  2862. END InitImport;
  2863. PROCEDURE SetType*(type: Type);
  2864. BEGIN
  2865. ASSERT(type = importType);
  2866. END SetType;
  2867. PROCEDURE SetModule*(module: Module);
  2868. BEGIN
  2869. SELF.module := module;
  2870. END SetModule;
  2871. PROCEDURE SetDirect*(d: BOOLEAN);
  2872. BEGIN
  2873. direct := d
  2874. END SetDirect;
  2875. PROCEDURE SetModuleName*(moduleName: Identifier);
  2876. BEGIN SELF.moduleName := moduleName
  2877. END SetModuleName;
  2878. PROCEDURE SetContext*(context: Identifier);
  2879. BEGIN
  2880. SELF.context := context
  2881. END SetContext;
  2882. PROCEDURE Accept*(v: Visitor);
  2883. VAR position: LONGINT;
  2884. BEGIN position := SELF.position.start; v.VisitImport(SELF)
  2885. END Accept;
  2886. END Import;
  2887. StatementSequence* = OBJECT
  2888. VAR
  2889. list: Basic.List;
  2890. PROCEDURE & InitList;
  2891. BEGIN NEW( list,32 );
  2892. END InitList;
  2893. PROCEDURE Length*( ): LONGINT;
  2894. BEGIN RETURN list.Length();
  2895. END Length;
  2896. PROCEDURE AddStatement*( statement: Statement);
  2897. BEGIN list.Add( statement );
  2898. END AddStatement;
  2899. PROCEDURE PrependStatement*( statement: Statement);
  2900. BEGIN list.Prepend( statement );
  2901. END PrependStatement;
  2902. PROCEDURE HasStatement*( statement: Statement):BOOLEAN;
  2903. BEGIN RETURN list.Contains(statement);
  2904. END HasStatement;
  2905. PROCEDURE GetStatement*( index: LONGINT ): Statement;
  2906. VAR p: ANY;
  2907. BEGIN p := list.Get( index ); RETURN p( Statement );
  2908. END GetStatement;
  2909. PROCEDURE SetStatement*(index: LONGINT; statement: Statement);
  2910. BEGIN
  2911. list.Set(index,statement);
  2912. END SetStatement;
  2913. PROCEDURE RemoveStatement*(statement: Statement);
  2914. BEGIN
  2915. list.Remove(statement);
  2916. END RemoveStatement;
  2917. PROCEDURE InsertBefore*(search, new: Statement);
  2918. BEGIN
  2919. list.Insert(list.IndexOf(search), new);
  2920. END InsertBefore;
  2921. PROCEDURE Clone(VAR copy: StatementSequence);
  2922. VAR i: LONGINT;
  2923. BEGIN
  2924. IF copy = NIL THEN NEW(copy) END;
  2925. FOR i := 0 TO Length()-1 DO
  2926. copy.AddStatement(CloneStatement(GetStatement(i)))
  2927. END;
  2928. END Clone;
  2929. END StatementSequence;
  2930. (**** statements ****)
  2931. Statement*= OBJECT
  2932. VAR outer-: Statement;
  2933. position-,end-: Position;
  2934. isUnreachable-: BOOLEAN;
  2935. comment-: Comment;
  2936. PROCEDURE & InitStatement*(position: Position; outer: Statement);
  2937. BEGIN
  2938. SELF.position := position;
  2939. end := invalidPosition;
  2940. SELF.outer := outer;
  2941. isUnreachable := FALSE;
  2942. comment := NIL;
  2943. END InitStatement;
  2944. PROCEDURE SetOuter*(o: Statement);
  2945. BEGIN outer := o
  2946. END SetOuter;
  2947. PROCEDURE SetUnreachable*(unreachable: BOOLEAN);
  2948. BEGIN isUnreachable := unreachable
  2949. END SetUnreachable;
  2950. PROCEDURE Accept*(v: Visitor);
  2951. VAR position: LONGINT;
  2952. BEGIN position := SELF.position.start; v.VisitStatement(SELF)
  2953. END Accept;
  2954. PROCEDURE SetComment*(comment: Comment);
  2955. BEGIN SELF.comment := comment
  2956. END SetComment;
  2957. PROCEDURE Clone(): Statement;
  2958. BEGIN
  2959. HALT(200) (* abstract *)
  2960. END Clone;
  2961. PROCEDURE End*(pos: Position);
  2962. BEGIN
  2963. end := pos;
  2964. END End;
  2965. END Statement;
  2966. (** << call(...) >> **)
  2967. ProcedureCallStatement*= OBJECT(Statement)
  2968. VAR call-: Designator;
  2969. PROCEDURE & InitProcedureCallStatement(position: Position; call: Designator; outer: Statement);
  2970. BEGIN InitStatement(position,outer); SELF.call := call;
  2971. END InitProcedureCallStatement;
  2972. PROCEDURE SetCall*(call: Designator);
  2973. BEGIN SELF.call := call;
  2974. END SetCall;
  2975. PROCEDURE Clone(): Statement;
  2976. VAR copy: ProcedureCallStatement;
  2977. BEGIN
  2978. NEW(copy, position, CloneDesignator(call), outer);
  2979. RETURN copy
  2980. END Clone;
  2981. PROCEDURE Accept*(v: Visitor);
  2982. VAR position: LONGINT;
  2983. BEGIN position := SELF.position.start; v.VisitProcedureCallStatement(SELF)
  2984. END Accept;
  2985. END ProcedureCallStatement;
  2986. (** << left := right >> **)
  2987. Assignment* = OBJECT (Statement)
  2988. VAR left-: Designator; right-: Expression;
  2989. PROCEDURE & InitAssignment*( position: Position; left: Designator; right: Expression; outer: Statement );
  2990. BEGIN
  2991. InitStatement( position,outer ); SELF.left := left; SELF.right := right;
  2992. END InitAssignment;
  2993. PROCEDURE SetLeft*(left: Designator);
  2994. BEGIN SELF.left := left
  2995. END SetLeft;
  2996. PROCEDURE SetRight*(right: Expression);
  2997. BEGIN SELF.right := right
  2998. END SetRight;
  2999. PROCEDURE Clone(): Statement;
  3000. VAR copy: Assignment;
  3001. BEGIN
  3002. NEW(copy, position, CloneDesignator(left), CloneExpression(right), outer);
  3003. RETURN copy
  3004. END Clone;
  3005. PROCEDURE Accept*(v: Visitor);
  3006. VAR position: LONGINT;
  3007. BEGIN position := SELF.position.start; v.VisitAssignment(SELF)
  3008. END Accept;
  3009. END Assignment;
  3010. (** << left ('!' | '?' | '<<' | '>>') right >> **)
  3011. CommunicationStatement* = OBJECT (Statement)
  3012. VAR
  3013. left-: Designator; right-: Expression; op-: LONGINT;
  3014. PROCEDURE & InitAssignment*( position: Position; op: LONGINT; left: Designator; right: Expression; outer: Statement );
  3015. BEGIN
  3016. InitStatement( position,outer ); SELF.op := op; SELF.left := left; SELF.right := right;
  3017. END InitAssignment;
  3018. PROCEDURE SetLeft*(left: Designator);
  3019. BEGIN SELF.left := left
  3020. END SetLeft;
  3021. PROCEDURE SetRight*(right: Expression);
  3022. BEGIN SELF.right := right
  3023. END SetRight;
  3024. PROCEDURE Accept*(v: Visitor);
  3025. VAR position: LONGINT;
  3026. BEGIN position := SELF.position.start; v.VisitCommunicationStatement(SELF)
  3027. END Accept;
  3028. END CommunicationStatement;
  3029. Part*= OBJECT
  3030. VAR
  3031. position-, end-: Position;
  3032. PROCEDURE InitPart;
  3033. BEGIN
  3034. position := invalidPosition; end := invalidPosition;
  3035. END InitPart;
  3036. PROCEDURE SetPosition*(pos: Position);
  3037. BEGIN
  3038. position := pos;
  3039. END SetPosition;
  3040. PROCEDURE SetEnd*(pos: Position);
  3041. BEGIN
  3042. end := pos;
  3043. END SetEnd;
  3044. END Part;
  3045. (** << ... condition THEN statements ... >> **)
  3046. IfPart*= OBJECT (Part)
  3047. VAR
  3048. condition-: Expression;
  3049. statements-: StatementSequence;
  3050. comment-: Comment;
  3051. PROCEDURE & InitIfPart;
  3052. BEGIN
  3053. InitPart;
  3054. statements := NIL; condition := NIL; comment := NIL;
  3055. END InitIfPart;
  3056. PROCEDURE SetCondition*(condition: Expression);
  3057. BEGIN SELF.condition := condition
  3058. END SetCondition;
  3059. PROCEDURE SetStatements*(statements: StatementSequence);
  3060. BEGIN SELF.statements := statements
  3061. END SetStatements;
  3062. PROCEDURE SetComment*(comment: Comment);
  3063. BEGIN SELF.comment := comment
  3064. END SetComment;
  3065. PROCEDURE Clone(): IfPart;
  3066. VAR copy: IfPart;
  3067. BEGIN
  3068. NEW(copy); copy.condition := CloneExpression(condition);
  3069. copy.statements := CloneStatementSequence(statements);
  3070. RETURN copy
  3071. END Clone;
  3072. END IfPart;
  3073. (** << IF ifPart {ELSIF elsifParts} ELSE elseParts >> **)
  3074. IfStatement* = OBJECT (Statement)
  3075. VAR
  3076. ifPart-: IfPart;
  3077. elsifParts: Basic.List;
  3078. elsePart-: StatementSequence;
  3079. PROCEDURE & InitIfStatement( position: Position ; outer: Statement);
  3080. BEGIN
  3081. InitStatement( position,outer ); ifPart := NewIfPart();
  3082. ifPart.SetPosition(position);
  3083. elsePart := NIL; elsifParts := NIL;
  3084. END InitIfStatement;
  3085. PROCEDURE SetElsePart*( elsePart: StatementSequence );
  3086. BEGIN
  3087. SELF.elsePart := elsePart;
  3088. END SetElsePart;
  3089. PROCEDURE AddElsifPart*( elsifPart: IfPart );
  3090. BEGIN
  3091. IF elsifParts = NIL THEN NEW(elsifParts,4); END;
  3092. elsifParts.Add( elsifPart );
  3093. END AddElsifPart;
  3094. PROCEDURE GetElsifPart*( i: LONGINT ): IfPart;
  3095. VAR a: ANY;
  3096. BEGIN a := elsifParts.Get( i ); RETURN a( IfPart )
  3097. END GetElsifPart;
  3098. PROCEDURE ElsifParts*( ): LONGINT;
  3099. BEGIN
  3100. IF elsifParts = NIL THEN RETURN 0 ELSE RETURN elsifParts.Length(); END;
  3101. END ElsifParts;
  3102. PROCEDURE Clone(): Statement;
  3103. VAR copy: IfStatement; i: LONGINT;
  3104. BEGIN
  3105. NEW(copy, position, outer);
  3106. copy.ifPart := ifPart.Clone();
  3107. FOR i := 0 TO ElsifParts()-1 DO
  3108. copy.AddElsifPart(GetElsifPart(i).Clone());
  3109. END;
  3110. copy.SetElsePart(CloneStatementSequence(elsePart));
  3111. RETURN copy
  3112. END Clone;
  3113. PROCEDURE Accept*(v: Visitor);
  3114. VAR position: LONGINT;
  3115. BEGIN position := SELF.position.start; v.VisitIfStatement(SELF)
  3116. END Accept;
  3117. END IfStatement;
  3118. WithPart*= OBJECT (Part)
  3119. VAR
  3120. variable-: Designator;
  3121. type-: Type; (* initially is qualified type *)
  3122. statements-: StatementSequence;
  3123. comment-: Comment;
  3124. PROCEDURE &InitWithPart();
  3125. BEGIN
  3126. InitPart();
  3127. type := NIL; variable := NIL; statements := NIL; comment := NIL;
  3128. END InitWithPart;
  3129. PROCEDURE SetVariable*( variable: Designator);
  3130. BEGIN
  3131. SELF.variable := variable
  3132. END SetVariable;
  3133. PROCEDURE SetType*( type: Type );
  3134. BEGIN
  3135. SELF.type := type
  3136. END SetType;
  3137. PROCEDURE SetStatements*( statements: StatementSequence );
  3138. BEGIN
  3139. SELF.statements := statements;
  3140. END SetStatements;
  3141. PROCEDURE SetComment*(comment: Comment);
  3142. BEGIN SELF.comment := comment
  3143. END SetComment;
  3144. PROCEDURE Clone(): WithPart;
  3145. VAR copy: WithPart;
  3146. BEGIN
  3147. NEW(copy);
  3148. copy.SetVariable(CloneDesignator(variable));
  3149. copy.SetType(type);
  3150. copy.SetStatements(CloneStatementSequence(statements));
  3151. RETURN copy
  3152. END Clone;
  3153. END WithPart;
  3154. (** << WITH variable : type DO statements END >> **)
  3155. WithStatement* = OBJECT (Statement)
  3156. VAR
  3157. withParts-: Basic.List;
  3158. elsePart-: StatementSequence;
  3159. PROCEDURE & InitWithStatement( position: Position; outer: Statement );
  3160. BEGIN
  3161. InitStatement( position,outer );
  3162. NEW(withParts,4); elsePart := NIL;
  3163. END InitWithStatement;
  3164. PROCEDURE AddWithPart*( withPart: WithPart );
  3165. BEGIN withParts.Add( withPart );
  3166. END AddWithPart;
  3167. PROCEDURE GetWithPart*( i: LONGINT ): WithPart;
  3168. VAR a: ANY;
  3169. BEGIN a := withParts.Get( i ); RETURN a( WithPart )
  3170. END GetWithPart;
  3171. PROCEDURE WithParts*( ): LONGINT;
  3172. BEGIN
  3173. IF withParts = NIL THEN RETURN 0 ELSE RETURN withParts.Length(); END;
  3174. END WithParts;
  3175. PROCEDURE SetElsePart*( elsePart: StatementSequence );
  3176. BEGIN
  3177. SELF.elsePart := elsePart;
  3178. END SetElsePart;
  3179. PROCEDURE Clone(): Statement;
  3180. VAR copy: WithStatement; i: LONGINT;
  3181. BEGIN
  3182. NEW(copy, position, outer);
  3183. FOR i := 0 TO WithParts()-1 DO
  3184. copy.AddWithPart(GetWithPart(i).Clone());
  3185. END;
  3186. copy.SetElsePart(CloneStatementSequence(elsePart));
  3187. RETURN copy
  3188. END Clone;
  3189. PROCEDURE Accept*(v: Visitor);
  3190. VAR position: LONGINT;
  3191. BEGIN position := SELF.position.start; v.VisitWithStatement(SELF)
  3192. END Accept;
  3193. END WithStatement;
  3194. CaseConstant*= POINTER TO RECORD min*,max*: LONGINT; next*: CaseConstant END;
  3195. (** << elements : statements >> **)
  3196. CasePart* = OBJECT (Part)
  3197. VAR
  3198. elements-: ExpressionList; (* expression list inserted by the parser *)
  3199. firstConstant-: CaseConstant; (* expression list resolved to int32s, inserted by checker *)
  3200. statements-: StatementSequence;
  3201. comment-: Comment;
  3202. PROCEDURE & InitCasePart;
  3203. BEGIN
  3204. InitPart;
  3205. elements := NewExpressionList(); firstConstant := NIL;
  3206. END InitCasePart;
  3207. PROCEDURE SetStatements*( statements: StatementSequence );
  3208. BEGIN
  3209. SELF.statements := statements;
  3210. END SetStatements;
  3211. PROCEDURE SetConstants*(firstConstant: CaseConstant);
  3212. BEGIN SELF.firstConstant := firstConstant
  3213. END SetConstants;
  3214. PROCEDURE SetComment*(comment: Comment);
  3215. BEGIN SELF.comment := comment
  3216. END SetComment;
  3217. PROCEDURE Clone(): CasePart;
  3218. VAR copy: CasePart;
  3219. BEGIN
  3220. NEW(copy);
  3221. copy.SetStatements(CloneStatementSequence(statements));
  3222. copy.firstConstant := firstConstant;
  3223. elements.Clone(copy.elements);
  3224. RETURN copy
  3225. END Clone;
  3226. END CasePart;
  3227. (** << CASE varaible OF caseParts ELSE elsePart >> **)
  3228. CaseStatement* = OBJECT (Statement)
  3229. VAR
  3230. variable-: Expression;
  3231. elsePart-: StatementSequence;
  3232. caseParts-: Basic.List;
  3233. min-,max-: LONGINT;
  3234. PROCEDURE & InitCaseStatement( position: Position ; outer: Statement);
  3235. BEGIN
  3236. InitStatement(position,outer ); variable := NIL; elsePart := NIL; caseParts := NIL;
  3237. min := MAX(LONGINT); max := MIN(LONGINT);
  3238. END InitCaseStatement;
  3239. PROCEDURE SetVariable*( expression: Expression );
  3240. BEGIN SELF.variable := expression;
  3241. END SetVariable;
  3242. PROCEDURE SetElsePart*( elsePart: StatementSequence );
  3243. BEGIN SELF.elsePart := elsePart;
  3244. END SetElsePart;
  3245. PROCEDURE AddCasePart*( casePart: CasePart );
  3246. BEGIN
  3247. IF caseParts = NIL THEN NEW(caseParts,4); END;
  3248. caseParts.Add( casePart );
  3249. END AddCasePart;
  3250. PROCEDURE GetCasePart*( i: LONGINT ): CasePart;
  3251. VAR a: ANY;
  3252. BEGIN a := caseParts.Get( i ); RETURN a( CasePart )
  3253. END GetCasePart;
  3254. PROCEDURE CaseParts*( ): LONGINT;
  3255. BEGIN
  3256. IF caseParts = NIL THEN RETURN 0 ELSE RETURN caseParts.Length(); END;
  3257. END CaseParts;
  3258. PROCEDURE Clone(): Statement;
  3259. VAR copy: CaseStatement; i: LONGINT;
  3260. BEGIN
  3261. NEW(copy, position, outer);
  3262. copy.SetVariable(CloneExpression(variable));
  3263. copy.SetElsePart(CloneStatementSequence(elsePart));
  3264. FOR i := 0 TO CaseParts()-1 DO
  3265. copy.AddCasePart(GetCasePart(i).Clone());
  3266. END;
  3267. copy.min := min; copy.max := max;
  3268. RETURN copy
  3269. END Clone;
  3270. PROCEDURE Accept*(v: Visitor);
  3271. VAR position: LONGINT;
  3272. BEGIN position := SELF.position.start; v.VisitCaseStatement(SELF)
  3273. END Accept;
  3274. PROCEDURE MaxConstant*(): LONGINT;
  3275. VAR val,i: LONGINT; part: CasePart; const: CaseConstant;
  3276. BEGIN
  3277. val := -1;
  3278. FOR i := 0 TO CaseParts() - 1 DO
  3279. part := GetCasePart(i);
  3280. const := part.firstConstant;
  3281. WHILE(const # NIL) DO
  3282. IF const.max > val THEN val := const.max; END;
  3283. const := const.next;
  3284. END;
  3285. END;
  3286. RETURN val;
  3287. END MaxConstant;
  3288. PROCEDURE SetMinMax*(min,max: LONGINT);
  3289. BEGIN
  3290. SELF.min := min; SELF.max := max;
  3291. END SetMinMax;
  3292. END CaseStatement;
  3293. (** << WHILE condition DO statements END >> **)
  3294. WhileStatement* = OBJECT (Statement)
  3295. VAR
  3296. condition-: Expression;
  3297. statements-: StatementSequence;
  3298. PROCEDURE & InitWhileStatement( position: Position ; outer: Statement);
  3299. BEGIN
  3300. InitStatement( position,outer ); condition := NIL; statements := NIL;
  3301. END InitWhileStatement;
  3302. PROCEDURE SetCondition*( condition: Expression );
  3303. BEGIN
  3304. SELF.condition := condition
  3305. END SetCondition;
  3306. PROCEDURE SetStatements*( statements: StatementSequence );
  3307. BEGIN
  3308. SELF.statements := statements;
  3309. END SetStatements;
  3310. PROCEDURE Clone(): Statement;
  3311. VAR copy: WhileStatement;
  3312. BEGIN
  3313. NEW(copy, position, outer);
  3314. copy.SetCondition(CloneExpression(condition));
  3315. copy.SetStatements(CloneStatementSequence(statements));
  3316. RETURN copy
  3317. END Clone;
  3318. PROCEDURE Accept*(v: Visitor);
  3319. VAR position: LONGINT;
  3320. BEGIN position := SELF.position.start; v.VisitWhileStatement(SELF)
  3321. END Accept;
  3322. END WhileStatement;
  3323. (** << REPEAT statements UNTIL condition >> **)
  3324. RepeatStatement* = OBJECT (Statement)
  3325. VAR
  3326. condition-: Expression;
  3327. statements-: StatementSequence;
  3328. PROCEDURE & InitRepeatStatement( position: Position; outer: Statement );
  3329. BEGIN
  3330. InitStatement( position,outer ); condition := NIL; statements := NIL;
  3331. END InitRepeatStatement;
  3332. PROCEDURE SetCondition*( condition: Expression );
  3333. BEGIN
  3334. SELF.condition := condition
  3335. END SetCondition;
  3336. PROCEDURE SetStatements*( statements: StatementSequence );
  3337. BEGIN
  3338. SELF.statements := statements;
  3339. END SetStatements;
  3340. PROCEDURE Clone(): Statement;
  3341. VAR copy: RepeatStatement;
  3342. BEGIN
  3343. NEW(copy, position, outer);
  3344. copy.SetCondition(CloneExpression(condition));
  3345. copy.SetStatements(CloneStatementSequence(statements));
  3346. RETURN copy
  3347. END Clone;
  3348. PROCEDURE Accept*(v: Visitor);
  3349. VAR position: LONGINT;
  3350. BEGIN position := SELF.position.start; v.VisitRepeatStatement(SELF)
  3351. END Accept;
  3352. END RepeatStatement;
  3353. (** << FOR variable := from TO to BY by DO statements END >> **)
  3354. ForStatement* = OBJECT (Statement)
  3355. VAR
  3356. variable-: Designator;
  3357. from-, to-, by-: Expression;
  3358. statements-: StatementSequence;
  3359. PROCEDURE & InitForStatement( position: Position; outer: Statement );
  3360. BEGIN
  3361. InitStatement( position,outer ); variable := NIL;from := NIL; to := NIL; by := NIL; statements := NIL;
  3362. END InitForStatement;
  3363. PROCEDURE SetVariable*( variable: Designator);
  3364. BEGIN
  3365. SELF.variable := variable
  3366. END SetVariable;
  3367. PROCEDURE SetFrom*( from: Expression );
  3368. BEGIN
  3369. SELF.from := from
  3370. END SetFrom;
  3371. PROCEDURE SetTo*( to: Expression );
  3372. BEGIN
  3373. SELF.to := to
  3374. END SetTo;
  3375. PROCEDURE SetBy*( by: Expression );
  3376. BEGIN SELF.by := by
  3377. END SetBy;
  3378. PROCEDURE SetStatements*( statements: StatementSequence );
  3379. BEGIN SELF.statements := statements;
  3380. END SetStatements;
  3381. PROCEDURE Clone(): Statement;
  3382. VAR copy: ForStatement;
  3383. BEGIN
  3384. NEW(copy, position, outer);
  3385. copy.SetVariable(CloneDesignator(variable));
  3386. copy.SetFrom(CloneExpression(from));
  3387. copy.SetTo(CloneExpression(to));
  3388. copy.SetBy(CloneExpression(by));
  3389. copy.SetStatements(CloneStatementSequence(statements));
  3390. RETURN copy
  3391. END Clone;
  3392. PROCEDURE Accept*(v: Visitor);
  3393. VAR position: LONGINT;
  3394. BEGIN position := SELF.position.start; v.VisitForStatement(SELF)
  3395. END Accept;
  3396. END ForStatement;
  3397. ExitableBlock*= OBJECT (Statement)
  3398. VAR statements-: StatementSequence;
  3399. PROCEDURE & InitExitableBlock( position: Position ; outer: Statement);
  3400. BEGIN
  3401. InitStatement( position ,outer); statements := NIL;
  3402. END InitExitableBlock;
  3403. PROCEDURE SetStatements*( statements: StatementSequence );
  3404. BEGIN SELF.statements := statements;
  3405. END SetStatements;
  3406. PROCEDURE Clone(): Statement;
  3407. VAR copy: ExitableBlock;
  3408. BEGIN
  3409. NEW(copy, position, outer);
  3410. copy.SetStatements(CloneStatementSequence(statements));
  3411. RETURN copy
  3412. END Clone;
  3413. PROCEDURE Accept*(v: Visitor);
  3414. VAR position: LONGINT;
  3415. BEGIN position := SELF.position.start; v.VisitExitableBlock(SELF)
  3416. END Accept;
  3417. END ExitableBlock;
  3418. (** << LOOP statements END >> **)
  3419. LoopStatement* = OBJECT (ExitableBlock)
  3420. PROCEDURE Clone(): Statement;
  3421. VAR copy: LoopStatement;
  3422. BEGIN
  3423. NEW(copy, position, outer);
  3424. copy.SetStatements(CloneStatementSequence(statements));
  3425. RETURN copy
  3426. END Clone;
  3427. PROCEDURE Accept*(v: Visitor);
  3428. VAR position: LONGINT;
  3429. BEGIN position := SELF.position.start; v.VisitLoopStatement(SELF)
  3430. END Accept;
  3431. END LoopStatement;
  3432. (** << EXIT >> **)
  3433. ExitStatement* = OBJECT (Statement)
  3434. PROCEDURE Accept*(v: Visitor);
  3435. VAR position: LONGINT;
  3436. BEGIN position := SELF.position.start; v.VisitExitStatement(SELF)
  3437. END Accept;
  3438. PROCEDURE Clone(): Statement;
  3439. VAR copy: ExitStatement;
  3440. BEGIN
  3441. NEW(copy, position, outer);
  3442. RETURN copy
  3443. END Clone;
  3444. END ExitStatement;
  3445. (** << RETURN returnValue >> **)
  3446. ReturnStatement* = OBJECT (Statement)
  3447. VAR returnValue-: Expression; (* strictly speaking this is not a value but this term is in common use here *)
  3448. PROCEDURE & InitReturnStatement( position: Position ; outer: Statement);
  3449. BEGIN
  3450. InitStatement( position,outer ); returnValue := NIL
  3451. END InitReturnStatement;
  3452. PROCEDURE SetReturnValue*( returnValue: Expression );
  3453. BEGIN SELF.returnValue := returnValue
  3454. END SetReturnValue;
  3455. PROCEDURE Clone(): Statement;
  3456. VAR copy: ReturnStatement;
  3457. BEGIN
  3458. NEW(copy, position, outer);
  3459. copy.SetReturnValue(CloneExpression(returnValue));
  3460. RETURN copy
  3461. END Clone;
  3462. PROCEDURE Accept*(v: Visitor);
  3463. VAR position: LONGINT;
  3464. BEGIN position := SELF.position.start; v.VisitReturnStatement(SELF)
  3465. END Accept;
  3466. END ReturnStatement;
  3467. (** << AWAIT condition >> **)
  3468. AwaitStatement* = OBJECT (Statement)
  3469. VAR condition-: Expression;
  3470. PROCEDURE & InitAwaitStatement( position: Position; outer: Statement );
  3471. BEGIN
  3472. InitStatement( position,outer ); condition := NIL
  3473. END InitAwaitStatement;
  3474. PROCEDURE SetCondition*( condition: Expression );
  3475. BEGIN SELF.condition := condition
  3476. END SetCondition;
  3477. PROCEDURE Clone(): Statement;
  3478. VAR copy: AwaitStatement;
  3479. BEGIN
  3480. NEW(copy, position, outer);
  3481. copy.SetCondition(CloneExpression(condition));
  3482. RETURN copy
  3483. END Clone;
  3484. PROCEDURE Accept*(v: Visitor);
  3485. VAR position: LONGINT;
  3486. BEGIN position := SELF.position.start; v.VisitAwaitStatement(SELF)
  3487. END Accept;
  3488. END AwaitStatement;
  3489. (* << Identifier ( Expression) >> *)
  3490. Modifier*= OBJECT
  3491. VAR
  3492. identifier-: Identifier; expression-: Expression;
  3493. resolved-: BOOLEAN;
  3494. nextModifier-: Modifier;
  3495. position-: Position;
  3496. PROCEDURE & InitModifier(position: Position; identifier: Identifier; expression: Expression);
  3497. BEGIN
  3498. SELF.position := position;
  3499. SELF.identifier := identifier; SELF.expression := expression; nextModifier := NIL; resolved := FALSE;
  3500. END InitModifier;
  3501. PROCEDURE Resolved*;
  3502. BEGIN resolved := TRUE
  3503. END Resolved;
  3504. PROCEDURE SetExpression*(e: Expression);
  3505. BEGIN SELF.expression := e
  3506. END SetExpression;
  3507. PROCEDURE SetNext*(modifier: Modifier);
  3508. BEGIN nextModifier := modifier
  3509. END SetNext;
  3510. END Modifier;
  3511. (** << BEGIN {Modifier, Modifier ... } statements END >> **)
  3512. StatementBlock* = OBJECT (Statement)
  3513. VAR
  3514. statements-: StatementSequence;
  3515. blockModifiers-: Modifier;
  3516. isExclusive-: BOOLEAN;
  3517. isRealtime-: BOOLEAN;
  3518. isUnchecked-: BOOLEAN;
  3519. isUncooperative-: BOOLEAN;
  3520. PROCEDURE & InitStatementBlock( position: Position ; outer: Statement);
  3521. BEGIN
  3522. InitStatement( position ,outer); statements := NIL; blockModifiers := NIL;
  3523. isExclusive := FALSE;
  3524. isRealtime := FALSE;
  3525. isUnchecked := FALSE;
  3526. isUncooperative := FALSE;
  3527. END InitStatementBlock;
  3528. PROCEDURE SetRealtime*(b: BOOLEAN);
  3529. BEGIN
  3530. isRealtime := b
  3531. END SetRealtime;
  3532. PROCEDURE SetUnchecked*(unchecked: BOOLEAN);
  3533. BEGIN
  3534. isUnchecked := unchecked
  3535. END SetUnchecked;
  3536. PROCEDURE SetUncooperative*(uncooperative: BOOLEAN);
  3537. BEGIN
  3538. isUncooperative := uncooperative
  3539. END SetUncooperative;
  3540. PROCEDURE SetModifier*(modifier: Modifier);
  3541. BEGIN
  3542. blockModifiers := modifier;
  3543. END SetModifier;
  3544. PROCEDURE SetExclusive*(excl: BOOLEAN);
  3545. BEGIN isExclusive := excl
  3546. END SetExclusive;
  3547. PROCEDURE SetStatementSequence*( statements: StatementSequence );
  3548. BEGIN SELF.statements := statements;
  3549. END SetStatementSequence;
  3550. PROCEDURE Accept*(v: Visitor);
  3551. VAR position: LONGINT;
  3552. BEGIN position := SELF.position.start; v.VisitStatementBlock(SELF)
  3553. END Accept;
  3554. END StatementBlock;
  3555. (** << CODE {flags} {character} END >> **)
  3556. Code*= OBJECT(Statement)
  3557. VAR
  3558. sourceCode-: SourceCode; sourceCodeLength-: LONGINT;
  3559. inlineCode-: BinaryCode;
  3560. inRules-, outRules-: StatementSequence;
  3561. PROCEDURE & InitCode(position: Position; outer: Statement);
  3562. BEGIN
  3563. InitStatement(position,outer);
  3564. inlineCode := NIL;
  3565. sourceCode := NIL; sourceCodeLength := 0;
  3566. NEW(inRules); NEW(outRules);
  3567. END InitCode;
  3568. PROCEDURE SetSourceCode*(source: SourceCode; length: LONGINT);
  3569. BEGIN sourceCode := source; sourceCodeLength := length;
  3570. ASSERT(sourceCodeLength <= LEN(source));
  3571. END SetSourceCode;
  3572. PROCEDURE SetBinaryCode*(code: BinaryCode);
  3573. BEGIN
  3574. inlineCode := code;
  3575. END SetBinaryCode;
  3576. PROCEDURE Clone(): Statement;
  3577. VAR copy: Code; s: Scanner.StringType;
  3578. BEGIN
  3579. NEW(copy, position, outer);
  3580. NEW(s, sourceCodeLength);
  3581. Strings.Copy(sourceCode^,0,sourceCodeLength,s^);
  3582. copy.SetSourceCode(s, sourceCodeLength);
  3583. copy.inRules := CloneStatementSequence(inRules);
  3584. copy.outRules := CloneStatementSequence(outRules);
  3585. RETURN copy
  3586. END Clone;
  3587. PROCEDURE Accept*(v: Visitor);
  3588. VAR position: LONGINT;
  3589. BEGIN position := SELF.position.start; v.VisitCode(SELF)
  3590. END Accept;
  3591. END Code;
  3592. (** << BEGIN {flags} statements FINALLY statements END >> **)
  3593. Body*= OBJECT(StatementBlock)
  3594. VAR
  3595. finally-: StatementSequence;
  3596. priority-: Expression; (* set by checker *)
  3597. inScope-: ProcedureScope;
  3598. code-: Code;
  3599. isActive-, isSafe-: BOOLEAN;
  3600. PROCEDURE & InitBody(position: Position; scope: ProcedureScope);
  3601. BEGIN
  3602. InitStatementBlock(position,NIL); finally := NIL; priority := NIL; inScope := scope; code := NIL;
  3603. isActive := FALSE; isSafe := FALSE; isRealtime := FALSE;
  3604. END InitBody;
  3605. PROCEDURE SetActive*(active: BOOLEAN);
  3606. BEGIN SELF.isActive := active
  3607. END SetActive;
  3608. PROCEDURE SetSafe*(safe: BOOLEAN);
  3609. BEGIN SELF.isSafe := safe
  3610. END SetSafe;
  3611. PROCEDURE SetFinally*( finally: StatementSequence );
  3612. BEGIN SELF.finally := finally
  3613. END SetFinally;
  3614. PROCEDURE SetPriority*(expression: Expression);
  3615. BEGIN priority := expression
  3616. END SetPriority;
  3617. PROCEDURE SetCode*(code: Code);
  3618. BEGIN SELF.code := code;
  3619. END SetCode;
  3620. END Body;
  3621. (** (* comment *) *)
  3622. Comment*=OBJECT
  3623. VAR position-: Position;
  3624. source-: String; (* currently: POINTER TO ARRAY OF CHAR *)
  3625. scope-: Scope;
  3626. item-: ANY; sameLine-: BOOLEAN;
  3627. nextComment-: Comment;
  3628. PROCEDURE & InitComment(pos: Position; scope: Scope; CONST s: ARRAY OF CHAR; length: LONGINT);
  3629. VAR i: LONGINT;
  3630. BEGIN
  3631. SELF.scope := scope;
  3632. NEW(source,length);
  3633. FOR i := 0 TO length-1 DO
  3634. source[i] := s[i];
  3635. END;
  3636. SELF.position := pos;
  3637. nextComment := NIL;
  3638. item := NIL; sameLine := FALSE;
  3639. END InitComment;
  3640. PROCEDURE SetItem*(p: ANY; sameLine: BOOLEAN);
  3641. BEGIN
  3642. item := p; SELF.sameLine := sameLine
  3643. END SetItem;
  3644. END Comment;
  3645. (**** building blocks ****)
  3646. Scope*=OBJECT
  3647. VAR
  3648. firstSymbol-: Symbol; numberSymbols-: LONGINT; (* all symbols in scope (sorted) *)
  3649. symbolTable: Basic.HashTableInt;
  3650. firstConstant-,lastConstant-: Constant; numberConstants-: LONGINT; (* constants *)
  3651. firstTypeDeclaration-,lastTypeDeclaration-: TypeDeclaration; numberTypeDeclarations-: LONGINT; (* type declarations *)
  3652. firstVariable-,lastVariable-: Variable; numberVariables-: LONGINT; (* variables *)
  3653. firstProcedure-,lastProcedure-: Procedure; numberProcedures-: LONGINT; (* procedures *)
  3654. procedures-: ProcedureList;
  3655. outerScope-: Scope; nextScope-: Scope;
  3656. ownerModule-: Module;
  3657. PROCEDURE & InitScope(outer: Scope);
  3658. BEGIN
  3659. firstSymbol := NIL; numberSymbols := 0;
  3660. firstConstant := NIL; lastConstant := NIL; numberConstants := 0;
  3661. firstTypeDeclaration := NIL; lastTypeDeclaration := NIL; numberTypeDeclarations := 0;
  3662. firstVariable := NIL; lastVariable := NIL; numberVariables := 0;
  3663. firstProcedure := NIL; lastProcedure := NIL; numberProcedures := 0;
  3664. outerScope := outer;
  3665. IF outer # NIL THEN
  3666. ownerModule := outer.ownerModule
  3667. ELSE
  3668. ownerModule := NIL;
  3669. END;
  3670. nextScope := NIL;
  3671. NEW(symbolTable,11);
  3672. END InitScope;
  3673. PROCEDURE Clear*;
  3674. BEGIN
  3675. firstConstant := NIL; lastConstant := NIL; numberConstants := 0;
  3676. firstTypeDeclaration := NIL; lastTypeDeclaration := NIL; numberTypeDeclarations := 0;
  3677. firstVariable := NIL; lastVariable := NIL; numberVariables := 0;
  3678. firstProcedure := NIL; lastProcedure := NIL; numberProcedures := 0;
  3679. END Clear;
  3680. (** Enter a symbol in the scope, aplhabetically sorted, duplicate = TRUE if multiply identifier *)
  3681. PROCEDURE EnterSymbol*(symbol: Symbol; VAR duplicate: BOOLEAN);
  3682. VAR p,q: Symbol;
  3683. BEGIN
  3684. ASSERT(symbol.nextSymbol = NIL,101); (* symbol may only be present in one scope at a time ! *)
  3685. ASSERT(symbol.scope = NIL,102);
  3686. ASSERT(symbol.name # invalidIdentifier,103);
  3687. p := firstSymbol; q := NIL;
  3688. WHILE (p # NIL) & (StringPool.CompareString(p.name,symbol.name)<0) DO q := p; p := p.nextSymbol END;
  3689. IF (p#NIL) & (symbol.name = p.name) THEN
  3690. duplicate := TRUE;
  3691. ELSE
  3692. duplicate := FALSE
  3693. END;
  3694. symbol.nextSymbol := p;
  3695. IF q = NIL THEN firstSymbol := symbol ELSE q.nextSymbol := symbol END;
  3696. symbol.SetScope(SELF);
  3697. symbolTable.Put(symbol.name,symbol);
  3698. INC(numberSymbols);
  3699. END EnterSymbol;
  3700. (** Find symbol by name *)
  3701. PROCEDURE FindSymbol*(identifier: Identifier): Symbol;
  3702. VAR p: Symbol; a: ANY;
  3703. BEGIN
  3704. IF identifier # invalidIdentifier THEN
  3705. a := symbolTable.Get(identifier);
  3706. IF (a # NIL) & ~(a IS Operator) THEN
  3707. p := a(Symbol);
  3708. END;
  3709. (*
  3710. p := firstSymbol;
  3711. WHILE(p#NIL) & ((p.name # identifier) OR (p IS Operator)) DO p := p.nextSymbol END;
  3712. *)
  3713. END;
  3714. RETURN p;
  3715. END FindSymbol;
  3716. PROCEDURE AddConstant*(c: Constant);
  3717. BEGIN
  3718. ASSERT(c # NIL);
  3719. IF lastConstant= NIL THEN firstConstant := c ELSE lastConstant.nextConstant := c END;
  3720. lastConstant := c;
  3721. INC(numberConstants);
  3722. END AddConstant;
  3723. PROCEDURE FindConstant*(identifier: Identifier): Constant;
  3724. VAR p: Constant;
  3725. BEGIN
  3726. p := firstConstant;
  3727. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextConstant END;
  3728. RETURN p;
  3729. END FindConstant;
  3730. PROCEDURE AddTypeDeclaration*(t: TypeDeclaration);
  3731. BEGIN
  3732. ASSERT(t # NIL);
  3733. IF lastTypeDeclaration= NIL THEN firstTypeDeclaration := t ELSE lastTypeDeclaration.nextTypeDeclaration := t END;
  3734. INC(numberTypeDeclarations);
  3735. lastTypeDeclaration := t;
  3736. END AddTypeDeclaration;
  3737. PROCEDURE FindTypeDeclaration*(identifier: Identifier): TypeDeclaration;
  3738. VAR p: TypeDeclaration;
  3739. BEGIN
  3740. p := firstTypeDeclaration;
  3741. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextTypeDeclaration END;
  3742. RETURN p;
  3743. END FindTypeDeclaration;
  3744. PROCEDURE AddVariable*(v: Variable);
  3745. BEGIN
  3746. ASSERT(v # NIL);
  3747. IF lastVariable= NIL THEN firstVariable := v ELSE lastVariable.nextVariable := v END;
  3748. INC(numberVariables);
  3749. lastVariable := v;
  3750. END AddVariable;
  3751. PROCEDURE PushVariable*(v: Variable);
  3752. BEGIN
  3753. ASSERT(v # NIL);
  3754. IF lastVariable= NIL THEN lastVariable := v ELSE v.nextVariable := firstVariable END;
  3755. INC(numberVariables);
  3756. firstVariable := v;
  3757. END PushVariable;
  3758. (* insert variable after variable in list -- can be important to keep variable offsets in order *)
  3759. (* pre: v # NIL, after # NIL *)
  3760. PROCEDURE InsertVariable*(v: Variable; after: Variable);
  3761. BEGIN
  3762. ASSERT(v # NIL);
  3763. ASSERT(after # NIL);
  3764. v.nextVariable := after.nextVariable;
  3765. after.nextVariable := v.nextVariable;
  3766. IF after = lastVariable THEN lastVariable := v END;
  3767. END InsertVariable;
  3768. PROCEDURE FindVariable*(identifier: Identifier): Variable;
  3769. VAR p: Variable;
  3770. BEGIN
  3771. p := firstVariable;
  3772. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextVariable END;
  3773. RETURN p;
  3774. END FindVariable;
  3775. PROCEDURE AddProcedure*(p: Procedure);
  3776. BEGIN
  3777. ASSERT(p # NIL);
  3778. IF lastProcedure= NIL THEN firstProcedure := p ELSE lastProcedure.nextProcedure := p END;
  3779. INC(numberProcedures);
  3780. lastProcedure := p;
  3781. END AddProcedure;
  3782. PROCEDURE AddProcedureDeclaration*(p: Procedure);
  3783. BEGIN
  3784. IF procedures = NIL THEN NEW(procedures) END;
  3785. procedures.AddProcedure(p);
  3786. END AddProcedureDeclaration;
  3787. PROCEDURE FindProcedure*(identifier: Identifier): Procedure;
  3788. VAR p: Procedure;
  3789. BEGIN
  3790. p := firstProcedure;
  3791. WHILE (p#NIL) & ((p.name # identifier) OR (p IS Operator)) DO p := p.nextProcedure END;
  3792. RETURN p;
  3793. END FindProcedure;
  3794. PROCEDURE FindMethod*(number: LONGINT): Procedure;
  3795. VAR p: Procedure;
  3796. BEGIN
  3797. p := firstProcedure;
  3798. WHILE (p# NIL) & (p.methodNumber # number) DO
  3799. p := p.nextProcedure
  3800. END;
  3801. RETURN p;
  3802. END FindMethod;
  3803. PROCEDURE Level*(): LONGINT;
  3804. VAR scope: Scope; level: LONGINT;
  3805. BEGIN
  3806. level := 0;
  3807. scope := SELF;
  3808. WHILE(scope.outerScope # NIL) DO
  3809. scope := scope.outerScope;
  3810. INC(level);
  3811. END;
  3812. RETURN level;
  3813. END Level;
  3814. PROCEDURE NeedsTrace* (): BOOLEAN;
  3815. VAR variable: Variable;
  3816. BEGIN
  3817. variable := firstVariable;
  3818. WHILE variable # NIL DO
  3819. IF variable.NeedsTrace () THEN RETURN TRUE END;
  3820. variable := variable.nextVariable;
  3821. END;
  3822. RETURN FALSE;
  3823. END NeedsTrace;
  3824. END Scope;
  3825. ProcedureScope*=OBJECT (Scope)
  3826. VAR
  3827. ownerProcedure-: Procedure;
  3828. body-: Body;
  3829. PROCEDURE & InitProcedureScope(outer: Scope);
  3830. BEGIN
  3831. InitScope(outer);
  3832. ownerProcedure := NIL;
  3833. body := NIL;
  3834. END InitProcedureScope;
  3835. PROCEDURE SetBody*(body: Body);
  3836. BEGIN
  3837. SELF.body := body;
  3838. END SetBody;
  3839. PROCEDURE NeedsTrace* (): BOOLEAN;
  3840. VAR parameter: Parameter;
  3841. BEGIN
  3842. parameter := ownerProcedure.type.resolved(ProcedureType).firstParameter;
  3843. WHILE parameter # NIL DO
  3844. IF parameter.NeedsTrace () THEN RETURN TRUE END;
  3845. parameter := parameter.nextParameter;
  3846. END;
  3847. RETURN NeedsTrace^();
  3848. END NeedsTrace;
  3849. END ProcedureScope;
  3850. EnumerationScope*= OBJECT(Scope)
  3851. VAR
  3852. ownerEnumeration-: EnumerationType;
  3853. (** Find symbol by name *)
  3854. PROCEDURE FindSymbol*(identifier: Identifier): Symbol;
  3855. VAR p: Symbol; base: Type;
  3856. BEGIN
  3857. p := FindSymbol^(identifier);
  3858. IF p = NIL THEN
  3859. base := ownerEnumeration.enumerationBase;
  3860. IF (base # NIL) & (base.resolved IS EnumerationType) THEN
  3861. p := base.resolved(EnumerationType).enumerationScope.FindSymbol(identifier)
  3862. END;
  3863. END;
  3864. RETURN p;
  3865. END FindSymbol;
  3866. PROCEDURE &InitEnumerationScope(outer: Scope);
  3867. BEGIN
  3868. InitScope(outer);
  3869. ownerEnumeration := NIL; (* must be set by EnumerationType *)
  3870. END InitEnumerationScope;
  3871. END EnumerationScope;
  3872. RecordScope*= OBJECT(Scope)
  3873. VAR
  3874. ownerRecord-: RecordType;
  3875. bodyProcedure-: Procedure;
  3876. constructor-: Procedure;
  3877. finalizer-: Procedure;
  3878. numberMethods-: LONGINT;
  3879. firstParameter-,lastParameter-: Parameter; numberParameters-: LONGINT; (* parameters for Active Cells programming*)
  3880. firstOperator-, lastOperator-: Operator; numberOperators: LONGINT; (* defined operators *)
  3881. PROCEDURE & InitRecordScope(outer: Scope);
  3882. BEGIN
  3883. InitScope(outer);
  3884. ownerRecord := NIL;
  3885. numberMethods := 0;
  3886. bodyProcedure := NIL;
  3887. constructor := NIL;
  3888. finalizer := NIL;
  3889. firstOperator := NIL; lastOperator := NIL; numberOperators := 0;
  3890. END InitRecordScope;
  3891. PROCEDURE SetBodyProcedure*(body: Procedure);
  3892. BEGIN SELF.bodyProcedure := body;
  3893. END SetBodyProcedure;
  3894. PROCEDURE SetConstructor*(body: Procedure);
  3895. BEGIN SELF.constructor := body
  3896. END SetConstructor;
  3897. PROCEDURE SetFinalizer*(body: Procedure);
  3898. BEGIN SELF.finalizer := body
  3899. END SetFinalizer;
  3900. PROCEDURE SetNumberMethods*(numberMethods: LONGINT);
  3901. BEGIN SELF.numberMethods := numberMethods;
  3902. END SetNumberMethods;
  3903. PROCEDURE AddOperator*(p: Operator);
  3904. BEGIN
  3905. ASSERT(p # NIL);
  3906. IF lastOperator= NIL THEN firstOperator := p ELSE lastOperator.nextOperator := p END;
  3907. INC(numberOperators);
  3908. lastOperator := p;
  3909. END AddOperator;
  3910. (** Find symbol by name *)
  3911. PROCEDURE FindSymbol*(identifier: Identifier): Symbol;
  3912. VAR p: Symbol; base: RecordType;
  3913. BEGIN
  3914. p := FindSymbol^(identifier);
  3915. IF p = NIL THEN
  3916. base := ownerRecord.GetBaseRecord();
  3917. IF (base # NIL) THEN
  3918. p := base.recordScope.FindSymbol(identifier)
  3919. END;
  3920. END;
  3921. RETURN p;
  3922. END FindSymbol;
  3923. (* if there is an abstract procedure in the scope, return it. Otherwise return nil *)
  3924. PROCEDURE AbstractProcedure*(inScope: Scope): Procedure;
  3925. VAR p: Procedure; s: Symbol; base: RecordType;
  3926. BEGIN
  3927. p := firstProcedure;
  3928. WHILE p # NIL DO
  3929. IF p.isAbstract THEN
  3930. IF inScope # SELF THEN (* elevate to mother scope, if necesary *)
  3931. s := inScope.FindSymbol(p.name);
  3932. IF s = p THEN (* procedure is not overwritten *)
  3933. RETURN p
  3934. ELSE
  3935. ASSERT(s # NIL);
  3936. ASSERT(s IS Procedure);
  3937. END;
  3938. ELSE
  3939. RETURN p
  3940. END;
  3941. END;
  3942. p := p.nextProcedure;
  3943. END;
  3944. base := ownerRecord.GetBaseRecord();
  3945. IF (base # NIL) THEN
  3946. RETURN base.recordScope.AbstractProcedure(inScope);
  3947. END;
  3948. RETURN NIL;
  3949. END AbstractProcedure;
  3950. PROCEDURE FindConstant*(identifier: Identifier): Constant;
  3951. VAR p: Constant; base: RecordType;
  3952. BEGIN
  3953. p := FindConstant^(identifier);
  3954. IF p = NIL THEN
  3955. base := ownerRecord.GetBaseRecord();
  3956. IF (base # NIL) THEN
  3957. p := base.recordScope.FindConstant(identifier)
  3958. END;
  3959. END;
  3960. RETURN p;
  3961. END FindConstant;
  3962. PROCEDURE FindTypeDeclaration*(identifier: Identifier): TypeDeclaration;
  3963. VAR p: TypeDeclaration; base: RecordType;
  3964. BEGIN
  3965. p := FindTypeDeclaration^(identifier);
  3966. IF p = NIL THEN
  3967. base := ownerRecord.GetBaseRecord();
  3968. IF (base # NIL) THEN
  3969. p := base.recordScope.FindTypeDeclaration(identifier)
  3970. END;
  3971. END;
  3972. RETURN p;
  3973. END FindTypeDeclaration;
  3974. PROCEDURE FindVariable*(identifier: Identifier): Variable;
  3975. VAR p: Variable; base: RecordType;
  3976. BEGIN
  3977. p := FindVariable^(identifier);
  3978. IF p = NIL THEN
  3979. base := ownerRecord.GetBaseRecord();
  3980. IF (base # NIL) THEN
  3981. p := base.recordScope.FindVariable(identifier)
  3982. END;
  3983. END;
  3984. RETURN p;
  3985. END FindVariable;
  3986. PROCEDURE FindProcedure*(identifier: Identifier): Procedure;
  3987. VAR p: Procedure; base: RecordType;
  3988. BEGIN
  3989. p := FindProcedure^(identifier);
  3990. IF p = NIL THEN
  3991. base := ownerRecord.GetBaseRecord();
  3992. IF (base # NIL) THEN
  3993. p := base.recordScope.FindProcedure(identifier)
  3994. END;
  3995. END;
  3996. RETURN p;
  3997. END FindProcedure;
  3998. PROCEDURE FindMethod*(number: LONGINT): Procedure;
  3999. VAR p: Procedure; base: RecordType;
  4000. BEGIN
  4001. p := FindMethod^(number);
  4002. IF p = NIL THEN
  4003. base := ownerRecord.GetBaseRecord();
  4004. IF (base # NIL) THEN
  4005. p := base.recordScope.FindMethod(number)
  4006. END;
  4007. END;
  4008. RETURN p;
  4009. END FindMethod;
  4010. PROCEDURE NeedsTrace* (): BOOLEAN;
  4011. VAR base: RecordType;
  4012. BEGIN
  4013. base := ownerRecord.GetBaseRecord();
  4014. IF (base # NIL) & (base.NeedsTrace ()) THEN RETURN TRUE END;
  4015. RETURN NeedsTrace^();
  4016. END NeedsTrace;
  4017. END RecordScope;
  4018. CellScope*=OBJECT (Scope)
  4019. VAR
  4020. ownerCell-: CellType;
  4021. bodyProcedure-: Procedure;
  4022. constructor-: Procedure;
  4023. firstImport-,lastImport-: Import; numberImports: LONGINT; (* imported modules *)
  4024. PROCEDURE & InitCellScope(outer: Scope);
  4025. BEGIN
  4026. InitScope(outer);
  4027. ownerCell := NIL;
  4028. bodyProcedure := NIL;
  4029. constructor := NIL;
  4030. firstImport := NIL; lastImport := NIL; numberImports := 0;
  4031. END InitCellScope;
  4032. PROCEDURE Clear*;
  4033. BEGIN
  4034. Clear^;
  4035. firstImport := NIL; lastImport := NIL; numberImports := 0;
  4036. constructor := NIL;
  4037. bodyProcedure := NIL;
  4038. END Clear;
  4039. PROCEDURE SetOwnerCell*(owner: CellType);
  4040. BEGIN
  4041. ownerCell := owner
  4042. END SetOwnerCell;
  4043. PROCEDURE SetBodyProcedure*(bodyProcedure: Procedure);
  4044. BEGIN
  4045. SELF.bodyProcedure := bodyProcedure;
  4046. END SetBodyProcedure;
  4047. PROCEDURE SetConstructor*(p: Procedure);
  4048. BEGIN constructor := p
  4049. END SetConstructor;
  4050. PROCEDURE AddImport*(i: Import);
  4051. BEGIN
  4052. ASSERT(i # NIL);
  4053. ASSERT(i.nextImport = NIL);
  4054. IF lastImport= NIL THEN firstImport:= i ELSE lastImport.nextImport := i END;
  4055. lastImport := i;
  4056. INC(numberImports);
  4057. END AddImport;
  4058. PROCEDURE FindImport*(identifier: Identifier): Import;
  4059. VAR p: Import;
  4060. BEGIN
  4061. p := firstImport;
  4062. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextImport END; (* finds imports and re-imports! *)
  4063. RETURN p;
  4064. END FindImport;
  4065. PROCEDURE GetImport*( index: LONGINT ): Import;
  4066. VAR import: Import;
  4067. BEGIN
  4068. import := firstImport;
  4069. WHILE(import # NIL) & (index > 0) DO
  4070. import := import.nextImport;
  4071. DEC(index);
  4072. END;
  4073. RETURN import;
  4074. END GetImport;
  4075. PROCEDURE FindSymbol*(identifier: Identifier): Symbol;
  4076. VAR p: Symbol; base: Type;
  4077. BEGIN
  4078. p := FindSymbol^(identifier);
  4079. IF p = NIL THEN
  4080. IF ownerCell.isCellNet THEN
  4081. RETURN ownerCell.FindProperty(identifier);
  4082. END;
  4083. END;
  4084. IF p = NIL THEN
  4085. base := ownerCell.baseType;
  4086. IF (base # NIL) THEN
  4087. base := base.resolved;
  4088. IF base IS PointerType THEN
  4089. base := base(PointerType).pointerBase.resolved;
  4090. END;
  4091. WITH base: CellType DO
  4092. p := base.cellScope.FindSymbol(identifier)
  4093. |base: RecordType DO
  4094. p := base.recordScope.FindSymbol(identifier)
  4095. END;
  4096. END;
  4097. END;
  4098. RETURN p;
  4099. END FindSymbol;
  4100. END CellScope;
  4101. (**
  4102. <<
  4103. IMPORT firstImport .. lastImport;
  4104. ...
  4105. firstOperator ... lastOperator
  4106. ....
  4107. >>
  4108. **)
  4109. ModuleScope*= OBJECT(Scope)
  4110. VAR
  4111. firstImport-,lastImport-: Import; numberImports: LONGINT; (* imported modules *)
  4112. firstOperator-,lastOperator-: Operator; numberOperators: LONGINT; (* defined operators *)
  4113. firstBuiltin-,lastBuiltin-: Builtin; numberBuiltins: LONGINT; (* defined builtins, only for global and system module *)
  4114. firstComment-,lastComment-: Comment; numberComments-: LONGINT; (* comments *)
  4115. bodyProcedure-: Procedure;
  4116. PROCEDURE & InitModuleScope;
  4117. BEGIN
  4118. InitScope(NIL);
  4119. firstComment := NIL; lastComment := NIL; numberComments := 0;
  4120. firstImport:= NIL; lastImport := NIL; numberImports := 0;
  4121. firstOperator := NIL; lastOperator := NIL; numberOperators := 0;
  4122. END InitModuleScope;
  4123. PROCEDURE SetBodyProcedure*(body: Procedure);
  4124. BEGIN SELF.bodyProcedure := body;
  4125. END SetBodyProcedure;
  4126. PROCEDURE SetGlobalScope*(outer: Scope);
  4127. BEGIN
  4128. SELF.outerScope := outer;
  4129. END SetGlobalScope;
  4130. PROCEDURE AddBuiltin*(p: Builtin);
  4131. BEGIN
  4132. ASSERT(p # NIL);
  4133. IF lastBuiltin= NIL THEN firstBuiltin := p ELSE lastBuiltin.nextBuiltin := p END;
  4134. INC(numberBuiltins);
  4135. lastBuiltin := p;
  4136. END AddBuiltin;
  4137. PROCEDURE AddOperator*(p: Operator);
  4138. BEGIN
  4139. ASSERT(p # NIL);
  4140. IF lastOperator= NIL THEN firstOperator := p ELSE lastOperator.nextOperator := p END;
  4141. INC(numberOperators);
  4142. lastOperator := p;
  4143. END AddOperator;
  4144. PROCEDURE FindOperator*(identifier: Identifier): Operator;
  4145. VAR p: Operator;
  4146. BEGIN
  4147. p := firstOperator;
  4148. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextOperator END;
  4149. RETURN p;
  4150. END FindOperator;
  4151. PROCEDURE AddImport*(i: Import);
  4152. BEGIN
  4153. ASSERT(i # NIL);
  4154. ASSERT(i.nextImport = NIL);
  4155. IF lastImport= NIL THEN firstImport:= i ELSE lastImport.nextImport := i END;
  4156. lastImport := i;
  4157. INC(numberImports);
  4158. END AddImport;
  4159. PROCEDURE FindImport*(identifier: Identifier): Import;
  4160. VAR p: Import;
  4161. BEGIN
  4162. p := firstImport;
  4163. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextImport END; (* finds imports and re-imports! *)
  4164. RETURN p;
  4165. END FindImport;
  4166. PROCEDURE GetImport*( index: LONGINT ): Import;
  4167. VAR import: Import;
  4168. BEGIN
  4169. import := firstImport;
  4170. WHILE(import # NIL) & (index > 0) DO
  4171. import := import.nextImport;
  4172. DEC(index);
  4173. END;
  4174. RETURN import;
  4175. END GetImport;
  4176. PROCEDURE AddComment*(comment: Comment);
  4177. BEGIN
  4178. ASSERT(comment # NIL);
  4179. IF lastComment= NIL THEN firstComment := comment ELSE lastComment.nextComment := comment END;
  4180. INC(numberComments);
  4181. lastComment := comment;
  4182. END AddComment;
  4183. PROCEDURE ImportByModuleName*(moduleName,context: Identifier): Import;
  4184. VAR p: Import;
  4185. BEGIN
  4186. p := firstImport;
  4187. WHILE(p#NIL) & ~((moduleName = p.moduleName) & (context = p.context)) DO p := p.nextImport END;
  4188. RETURN p;
  4189. END ImportByModuleName;
  4190. PROCEDURE RemoveImporters*(moduleName,context: Identifier);
  4191. VAR this: Import;
  4192. PROCEDURE Check(p: Import): BOOLEAN;
  4193. VAR result: BOOLEAN;
  4194. BEGIN
  4195. IF (moduleName = p.moduleName) & (context = p.context) THEN
  4196. result := TRUE
  4197. ELSE
  4198. result := p.module.moduleScope.ImportByModuleName(moduleName,context) # NIL;
  4199. END;
  4200. RETURN result
  4201. END Check;
  4202. BEGIN
  4203. WHILE(firstImport # NIL) & Check(firstImport) DO
  4204. firstImport := firstImport.nextImport;
  4205. DEC(numberImports);
  4206. END;
  4207. IF firstImport = NIL THEN lastImport := NIL
  4208. ELSE
  4209. this :=firstImport;
  4210. WHILE(this.nextImport # NIL) DO
  4211. IF Check(this.nextImport) THEN
  4212. this.nextImport := this.nextImport.nextImport;
  4213. DEC(numberImports);
  4214. ELSE
  4215. this := this.nextImport
  4216. END;
  4217. END;
  4218. lastImport := this;
  4219. END;
  4220. END RemoveImporters;
  4221. END ModuleScope;
  4222. (* << MODULE name ['in' context] moduleScope name '.' >> *)
  4223. Module* = OBJECT (Symbol)
  4224. VAR
  4225. sourceName-: Basic.FileName;
  4226. moduleScope-: ModuleScope;
  4227. context-:Identifier; (* modules context *)
  4228. case-: LONGINT; (* module notation in lower or upper case, important for printout and operators *)
  4229. isCellNet-: BOOLEAN;
  4230. firstScope-,lastScope-: Scope; numberScopes-: LONGINT; (* list of all scopes for checker / backend traversal etc. *)
  4231. closingComment-: Comment;
  4232. modifiers-: Modifier;
  4233. PROCEDURE & InitModule( CONST sourceName: ARRAY OF CHAR; position: Position; name: Identifier; scope: ModuleScope; case: LONGINT);
  4234. BEGIN
  4235. InitSymbol(position,name);
  4236. COPY (sourceName, SELF.sourceName);
  4237. moduleScope := scope;
  4238. ASSERT(scope.ownerModule = NIL); (* cannot register twice ! *)
  4239. scope.ownerModule := SELF;
  4240. context := invalidIdentifier;
  4241. SELF.case := case;
  4242. firstScope := NIL; lastScope := NIL; numberScopes := 0;
  4243. SetType(moduleType);
  4244. closingComment := NIL;
  4245. isCellNet := FALSE;
  4246. modifiers := NIL;
  4247. END InitModule;
  4248. PROCEDURE SetCase*(case: LONGINT);
  4249. BEGIN
  4250. SELF.case := case
  4251. END SetCase;
  4252. PROCEDURE SetCellNet*(isCellNet: BOOLEAN);
  4253. BEGIN SELF.isCellNet := isCellNet
  4254. END SetCellNet;
  4255. PROCEDURE SetContext*(context: Identifier);
  4256. BEGIN SELF.context := context;
  4257. END SetContext;
  4258. PROCEDURE SetName*(name: Identifier);
  4259. BEGIN SELF.name := name
  4260. END SetName;
  4261. PROCEDURE SetClosingComment*(comment: Comment);
  4262. BEGIN SELF.closingComment := comment
  4263. END SetClosingComment;
  4264. PROCEDURE SetModifiers*(modifiers: Modifier);
  4265. BEGIN SELF.modifiers := modifiers
  4266. END SetModifiers;
  4267. PROCEDURE AddScope*(c: Scope);
  4268. BEGIN
  4269. IF lastScope= NIL THEN firstScope := c ELSE lastScope.nextScope := c END;
  4270. lastScope := c;
  4271. INC(numberScopes);
  4272. END AddScope;
  4273. PROCEDURE Accept*(v: Visitor);
  4274. VAR position: LONGINT;
  4275. BEGIN position := SELF.position.start; v.VisitModule(SELF)
  4276. END Accept;
  4277. END Module;
  4278. (** <<expression, expression, ...>> **)
  4279. SymbolList* = OBJECT
  4280. VAR list: Basic.List;
  4281. PROCEDURE & InitList*;
  4282. BEGIN NEW( list,8 );
  4283. END InitList;
  4284. PROCEDURE Length*( ): LONGINT;
  4285. BEGIN RETURN list.Length();
  4286. END Length;
  4287. PROCEDURE AddSymbol*( d: Symbol );
  4288. BEGIN list.Add(d)
  4289. END AddSymbol;
  4290. PROCEDURE GetSymbol*( index: LONGINT ): Symbol;
  4291. VAR p: ANY;
  4292. BEGIN
  4293. p := list.Get(index); RETURN p(Symbol);
  4294. END GetSymbol;
  4295. PROCEDURE SetSymbol*(index: LONGINT; expression: Symbol);
  4296. BEGIN list.Set(index,expression)
  4297. END SetSymbol;
  4298. PROCEDURE RemoveSymbol*(i: LONGINT);
  4299. BEGIN list.RemoveByIndex(i);
  4300. END RemoveSymbol;
  4301. (*
  4302. PROCEDURE Clone*(VAR list: SymbolList);
  4303. VAR i: LONGINT;
  4304. BEGIN
  4305. IF list = NIL THEN NEW(list) END;
  4306. FOR i := 0 TO Length()-1 DO
  4307. list.AddSymbol(CloneSymbol(GetSymbol(i)));
  4308. END;
  4309. END Clone;
  4310. *)
  4311. END SymbolList;
  4312. ProcedureList* = OBJECT
  4313. VAR list: Basic.List;
  4314. PROCEDURE & InitList*;
  4315. BEGIN NEW( list,8 );
  4316. END InitList;
  4317. PROCEDURE Length*( ): LONGINT;
  4318. BEGIN RETURN list.Length();
  4319. END Length;
  4320. PROCEDURE AddProcedure*( d: Procedure );
  4321. BEGIN list.Add(d)
  4322. END AddProcedure;
  4323. PROCEDURE GetProcedure*( index: LONGINT ): Procedure;
  4324. VAR p: ANY;
  4325. BEGIN
  4326. IF index >= list.Length() THEN RETURN NIL END;
  4327. p := list.Get(index);
  4328. IF p = NIL THEN
  4329. RETURN NIL
  4330. ELSE
  4331. RETURN p(Procedure);
  4332. END;
  4333. END GetProcedure;
  4334. PROCEDURE SetProcedure*(index: LONGINT; expression: Procedure);
  4335. BEGIN list.GrowAndSet(index,expression)
  4336. END SetProcedure;
  4337. PROCEDURE RemoveProcedure*(i: LONGINT);
  4338. BEGIN list.RemoveByIndex(i);
  4339. END RemoveProcedure;
  4340. (*
  4341. PROCEDURE Clone*(VAR list: ProcedureList);
  4342. VAR i: LONGINT;
  4343. BEGIN
  4344. IF list = NIL THEN NEW(list) END;
  4345. FOR i := 0 TO Length()-1 DO
  4346. list.AddProcedure(CloneProcedure(GetProcedure(i)));
  4347. END;
  4348. END Clone;
  4349. *)
  4350. END ProcedureList;
  4351. VAR
  4352. (* invalid items used, for example, by parser and checker *)
  4353. invalidIdentifier-: Identifier;
  4354. invalidQualifiedIdentifier-: QualifiedIdentifier;
  4355. invalidType-: Type;
  4356. invalidExpression-: Expression; (* mapped to invalidDesignator for better error handling in checker *)
  4357. invalidDesignator-: Designator;
  4358. invalidValue-: Value;
  4359. invalidSymbol-: Symbol;
  4360. invalidPosition-: Position;
  4361. anonymousIdentifier-: Identifier;
  4362. importType-: Type;
  4363. typeDeclarationType-: Type;
  4364. moduleType-: Type;
  4365. indexListSeparator-: Expression;
  4366. PROCEDURE InitFingerPrint*(VAR fingerprint: FingerPrint);
  4367. BEGIN
  4368. fingerprint.shallowAvailable := FALSE;
  4369. fingerprint.deepAvailable := FALSE;
  4370. fingerprint.shallow := 0;
  4371. fingerprint.private := 0;
  4372. fingerprint.public := 0;
  4373. END InitFingerPrint;
  4374. PROCEDURE NewModule*( CONST sourceName: ARRAY OF CHAR; position: Position; name: Identifier;scope: ModuleScope; case: LONGINT ): Module;
  4375. VAR module: Module;
  4376. BEGIN
  4377. NEW( module, sourceName, position, name, scope, case); RETURN module;
  4378. END NewModule;
  4379. PROCEDURE NewComment*(position: Position; scope: Scope; CONST source: ARRAY OF CHAR; length: LONGINT): Comment;
  4380. VAR comment: Comment;
  4381. BEGIN
  4382. NEW(comment,position,scope,source,length); RETURN comment;
  4383. END NewComment;
  4384. PROCEDURE NewImport*( position: Position; alias, name: Identifier; direct: BOOLEAN): Import;
  4385. VAR import: Import;
  4386. BEGIN
  4387. NEW( import, position, alias, name, direct ); RETURN import
  4388. END NewImport;
  4389. PROCEDURE NewConstant*( position: Position; name: Identifier ): Constant;
  4390. VAR constant: Constant;
  4391. BEGIN
  4392. NEW( constant, position, name ); RETURN constant
  4393. END NewConstant;
  4394. PROCEDURE NewProcedure*( position: Position; name: Identifier; scope: ProcedureScope ): Procedure;
  4395. VAR procedure: Procedure;
  4396. BEGIN
  4397. NEW( procedure, position, name, scope); RETURN procedure
  4398. END NewProcedure;
  4399. PROCEDURE NewBuiltin*(position: Position; name: Identifier; id: LONGINT): Builtin;
  4400. VAR builtin: Builtin;
  4401. BEGIN
  4402. NEW(builtin,position,name,id); RETURN builtin
  4403. END NewBuiltin;
  4404. PROCEDURE NewCustomBuiltin*(position: Position; name: Identifier; id: LONGINT; subType: SHORTINT): CustomBuiltin;
  4405. VAR builtin:CustomBuiltin;
  4406. BEGIN
  4407. NEW(builtin,position,name,id,subType); RETURN builtin
  4408. END NewCustomBuiltin;
  4409. PROCEDURE NewOperator*( position: Position; name: Identifier; scope: ProcedureScope): Operator;
  4410. VAR operator: Operator;
  4411. BEGIN
  4412. NEW( operator, position, name, scope); RETURN operator
  4413. END NewOperator;
  4414. PROCEDURE NewType*(): Type; (* for error handling: invalid Type, is realtime type *)
  4415. VAR type: Type;
  4416. BEGIN
  4417. NEW( type, invalidPosition);
  4418. type.SetRealtime(TRUE);
  4419. RETURN type
  4420. END NewType;
  4421. PROCEDURE NewByteType*(sizeInBits: LONGINT): ByteType;
  4422. VAR basicType: ByteType;
  4423. BEGIN
  4424. NEW(basicType, sizeInBits); RETURN basicType;
  4425. END NewByteType;
  4426. PROCEDURE NewAnyType*(sizeInBits: LONGINT): AnyType;
  4427. VAR basicType: AnyType;
  4428. BEGIN
  4429. NEW(basicType, sizeInBits); RETURN basicType;
  4430. END NewAnyType;
  4431. PROCEDURE NewObjectType*(sizeInBits: LONGINT): ObjectType;
  4432. VAR basicType: ObjectType;
  4433. BEGIN
  4434. NEW(basicType, sizeInBits); RETURN basicType;
  4435. END NewObjectType;
  4436. PROCEDURE NewNilType*(sizeInBits: LONGINT): NilType;
  4437. VAR basicType: NilType;
  4438. BEGIN
  4439. NEW(basicType, sizeInBits); RETURN basicType;
  4440. END NewNilType;
  4441. PROCEDURE NewAddressType*(sizeInBits: LONGINT): AddressType;
  4442. VAR basicType: AddressType;
  4443. BEGIN
  4444. NEW(basicType, sizeInBits); RETURN basicType;
  4445. END NewAddressType;
  4446. PROCEDURE NewSizeType*(sizeInBits: LONGINT): SizeType;
  4447. VAR basicType: SizeType;
  4448. BEGIN
  4449. NEW(basicType, sizeInBits); RETURN basicType;
  4450. END NewSizeType;
  4451. PROCEDURE NewBooleanType*(sizeInBits: LONGINT): BooleanType;
  4452. VAR basicType: BooleanType;
  4453. BEGIN
  4454. NEW(basicType, sizeInBits); RETURN basicType;
  4455. END NewBooleanType;
  4456. PROCEDURE NewSetType*(sizeInBits: LONGINT): SetType;
  4457. VAR basicType: SetType;
  4458. BEGIN
  4459. NEW(basicType, sizeInBits); RETURN basicType;
  4460. END NewSetType;
  4461. PROCEDURE NewCharacterType*(sizeInBits: LONGINT): CharacterType;
  4462. VAR basicType: CharacterType;
  4463. BEGIN
  4464. NEW(basicType, sizeInBits); RETURN basicType;
  4465. END NewCharacterType;
  4466. PROCEDURE NewRangeType*(sizeInBits: LONGINT): RangeType;
  4467. VAR basicType: RangeType;
  4468. BEGIN
  4469. NEW(basicType, sizeInBits); RETURN basicType;
  4470. END NewRangeType;
  4471. PROCEDURE NewComplexType*(base: Type): ComplexType;
  4472. VAR basicType: ComplexType;
  4473. BEGIN
  4474. NEW(basicType, base); RETURN basicType;
  4475. END NewComplexType;
  4476. PROCEDURE NewIntegerType*(size: LONGINT; signed: BOOLEAN): IntegerType;
  4477. VAR basicType: IntegerType;
  4478. BEGIN
  4479. NEW(basicType, size, signed); RETURN basicType;
  4480. END NewIntegerType;
  4481. PROCEDURE NewFloatType*(sizeInBits: LONGINT): FloatType;
  4482. VAR basicType: FloatType;
  4483. BEGIN
  4484. NEW(basicType, sizeInBits); RETURN basicType;
  4485. END NewFloatType;
  4486. PROCEDURE NewTypeDeclaration*(position: Position; name: Identifier): TypeDeclaration;
  4487. VAR typeDeclaration: TypeDeclaration;
  4488. BEGIN
  4489. ASSERT(name # invalidIdentifier);
  4490. NEW(typeDeclaration,position,name); RETURN typeDeclaration
  4491. END NewTypeDeclaration;
  4492. PROCEDURE NewStringType*( position: Position; baseType: Type; length: LONGINT): StringType;
  4493. VAR stringType: StringType;
  4494. BEGIN
  4495. NEW( stringType, position, baseType, length); RETURN stringType;
  4496. END NewStringType;
  4497. PROCEDURE NewEnumerationType*( position: Position; scope: Scope; enumerationScope: EnumerationScope): EnumerationType;
  4498. VAR enumerationType: EnumerationType;
  4499. BEGIN
  4500. NEW( enumerationType, position, scope, enumerationScope); RETURN enumerationType;
  4501. END NewEnumerationType;
  4502. PROCEDURE NewArrayType*( position: Position; scope: Scope; form: LONGINT): ArrayType;
  4503. VAR arrayType: ArrayType;
  4504. BEGIN
  4505. NEW( arrayType, position,scope, form); RETURN arrayType;
  4506. END NewArrayType;
  4507. PROCEDURE NewMathArrayType*( position: Position; scope: Scope; form: LONGINT): MathArrayType;
  4508. VAR mathArrayType: MathArrayType;
  4509. BEGIN
  4510. NEW( mathArrayType, position,scope,form); RETURN mathArrayType;
  4511. END NewMathArrayType;
  4512. PROCEDURE NewPointerType*( position: Position; scope: Scope): PointerType;
  4513. VAR pointerType: PointerType;
  4514. BEGIN
  4515. NEW( pointerType, position,scope); RETURN pointerType;
  4516. END NewPointerType;
  4517. PROCEDURE NewPortType*( position: Position; direction: LONGINT; sizeExpression: Expression; scope: Scope): PortType;
  4518. VAR portType: PortType;
  4519. BEGIN
  4520. NEW( portType, position, direction, sizeExpression, scope); RETURN portType;
  4521. END NewPortType;
  4522. PROCEDURE NewRecordType*( position: Position; scope: Scope; recordScope: RecordScope): RecordType;
  4523. VAR recordType: RecordType;
  4524. BEGIN
  4525. NEW( recordType, position, scope, recordScope); RETURN recordType
  4526. END NewRecordType;
  4527. PROCEDURE NewCellType*(position: Position; scope:Scope; cellScope: CellScope): CellType;
  4528. VAR actorType: CellType;
  4529. BEGIN
  4530. NEW(actorType, position, scope, cellScope); RETURN actorType;
  4531. END NewCellType;
  4532. PROCEDURE NewProcedureType*( position: Position; scope: Scope): ProcedureType;
  4533. VAR procedureType: ProcedureType;
  4534. BEGIN
  4535. NEW( procedureType, position,scope); RETURN procedureType;
  4536. END NewProcedureType;
  4537. PROCEDURE NewQualifiedType*( position: Position; scope: Scope; qualifiedIdentifier: QualifiedIdentifier): QualifiedType;
  4538. VAR qualifiedType: QualifiedType;
  4539. BEGIN
  4540. NEW( qualifiedType, position,scope,qualifiedIdentifier ); RETURN qualifiedType
  4541. END NewQualifiedType;
  4542. PROCEDURE NewSymbol*(name: Identifier): Symbol; (* for error handling: invalid Symbol *)
  4543. VAR symbol: Symbol;
  4544. BEGIN
  4545. NEW(symbol,invalidPosition,name); RETURN symbol
  4546. END NewSymbol;
  4547. PROCEDURE NewVariable*( position: Position; name: Identifier): Variable;
  4548. VAR variable: Variable;
  4549. BEGIN
  4550. NEW( variable, position, name ); RETURN variable
  4551. END NewVariable;
  4552. PROCEDURE NewQualifiedIdentifier*( position: Position; prefix, suffix: Identifier ): QualifiedIdentifier;
  4553. VAR qualifiedIdentifier: QualifiedIdentifier;
  4554. BEGIN
  4555. NEW( qualifiedIdentifier, position, prefix, suffix ); RETURN qualifiedIdentifier
  4556. END NewQualifiedIdentifier;
  4557. PROCEDURE NewIdentifier*(CONST name: ARRAY OF CHAR): Identifier;
  4558. BEGIN
  4559. RETURN Basic.MakeString(name);
  4560. END NewIdentifier;
  4561. PROCEDURE NewParameter*( position: Position; ownerType:Type ; name: Identifier; passAs: LONGINT): Parameter;
  4562. VAR parameter: Parameter;
  4563. BEGIN
  4564. NEW( parameter, position, ownerType, name, passAs); RETURN parameter;
  4565. END NewParameter;
  4566. PROCEDURE NewProperty*( position: Position; name: Identifier): Property;
  4567. VAR property: Property;
  4568. BEGIN
  4569. NEW( property, position, name); RETURN property;
  4570. END NewProperty;
  4571. PROCEDURE NewExpressionList*(): ExpressionList;
  4572. VAR expressionList: ExpressionList;
  4573. BEGIN
  4574. NEW(expressionList); RETURN expressionList
  4575. END NewExpressionList;
  4576. PROCEDURE CloneExpressionList*(l: ExpressionList): ExpressionList;
  4577. VAR copy: ExpressionList;
  4578. BEGIN
  4579. IF l = NIL THEN RETURN NIL ELSE l.Clone(copy); RETURN copy END;
  4580. END CloneExpressionList;
  4581. PROCEDURE NewDesignator*(): Designator; (* for error handling: invalid Designator *)
  4582. VAR designator: Designator;
  4583. BEGIN
  4584. NEW(designator,invalidPosition); RETURN designator;
  4585. END NewDesignator;
  4586. PROCEDURE NewIdentifierDesignator*( position: Position; identifier: Identifier): IdentifierDesignator;
  4587. VAR identifierDesignator: IdentifierDesignator;
  4588. BEGIN
  4589. NEW( identifierDesignator, position, identifier ); RETURN identifierDesignator
  4590. END NewIdentifierDesignator;
  4591. PROCEDURE NewSelectorDesignator*( position: Position; left: Designator; name: Identifier ): SelectorDesignator;
  4592. VAR selectorDesignator: SelectorDesignator;
  4593. BEGIN
  4594. NEW( selectorDesignator, position, left, name ); RETURN selectorDesignator
  4595. END NewSelectorDesignator;
  4596. PROCEDURE NewParameterDesignator*( position: Position; left: Designator; expressionList: ExpressionList ): ParameterDesignator;
  4597. VAR parameterDesignator: ParameterDesignator;
  4598. BEGIN
  4599. NEW( parameterDesignator,position, left, expressionList ); RETURN parameterDesignator
  4600. END NewParameterDesignator;
  4601. PROCEDURE NewArrowDesignator*( position: Position; left: Designator ): ArrowDesignator;
  4602. VAR dereferenceDesignator: ArrowDesignator;
  4603. BEGIN
  4604. NEW( dereferenceDesignator, position, left ); RETURN dereferenceDesignator;
  4605. END NewArrowDesignator;
  4606. PROCEDURE NewBracketDesignator*( position: Position; left: Designator; expressionList: ExpressionList ): BracketDesignator;
  4607. VAR bracketDesignator: BracketDesignator;
  4608. BEGIN
  4609. NEW( bracketDesignator, position, left, expressionList ); RETURN bracketDesignator
  4610. END NewBracketDesignator;
  4611. PROCEDURE NewSymbolDesignator*( position: Position; left: Designator; symbol: Symbol ): SymbolDesignator;
  4612. VAR symbolDesignator: SymbolDesignator;
  4613. BEGIN
  4614. NEW( symbolDesignator, position, left, symbol); RETURN symbolDesignator
  4615. END NewSymbolDesignator;
  4616. PROCEDURE NewIndexDesignator*( position: Position; left: Designator): IndexDesignator;
  4617. VAR indexDesignator: IndexDesignator;
  4618. BEGIN
  4619. NEW( indexDesignator, position, left); RETURN indexDesignator
  4620. END NewIndexDesignator;
  4621. PROCEDURE NewProcedureCallDesignator*(position: Position; left: Designator; parameters: ExpressionList): ProcedureCallDesignator;
  4622. VAR procedureCallDesignator: ProcedureCallDesignator;
  4623. BEGIN
  4624. NEW(procedureCallDesignator, position, left, parameters); RETURN procedureCallDesignator
  4625. END NewProcedureCallDesignator;
  4626. PROCEDURE NewBuiltinCallDesignator*(position: Position; id: LONGINT; left: Designator; parameters: ExpressionList): BuiltinCallDesignator;
  4627. VAR builtinCallDesignator: BuiltinCallDesignator;
  4628. BEGIN
  4629. NEW(builtinCallDesignator, position, id, left,parameters); RETURN builtinCallDesignator
  4630. END NewBuiltinCallDesignator;
  4631. PROCEDURE NewTypeGuardDesignator*(position: Position; left: Designator; type: Type): TypeGuardDesignator;
  4632. VAR guardDesignator: TypeGuardDesignator;
  4633. BEGIN
  4634. NEW(guardDesignator,position,left,type); RETURN guardDesignator;
  4635. END NewTypeGuardDesignator;
  4636. PROCEDURE NewDereferenceDesignator*( position: Position; left: Designator): DereferenceDesignator;
  4637. VAR dereferenceDesignator: DereferenceDesignator;
  4638. BEGIN
  4639. NEW( dereferenceDesignator, position, left); RETURN dereferenceDesignator
  4640. END NewDereferenceDesignator;
  4641. PROCEDURE NewSupercallDesignator*( position: Position; left: Designator): SupercallDesignator;
  4642. VAR supercallDesignator: SupercallDesignator;
  4643. BEGIN
  4644. NEW( supercallDesignator, position, left); RETURN supercallDesignator
  4645. END NewSupercallDesignator;
  4646. PROCEDURE NewSelfDesignator*( position: Position): SelfDesignator;
  4647. VAR selfDesignator: SelfDesignator;
  4648. BEGIN
  4649. NEW( selfDesignator, position); RETURN selfDesignator
  4650. END NewSelfDesignator;
  4651. PROCEDURE NewResultDesignator*( position: Position): ResultDesignator;
  4652. VAR resultDesignator: ResultDesignator;
  4653. BEGIN
  4654. NEW( resultDesignator, position); RETURN resultDesignator
  4655. END NewResultDesignator;
  4656. PROCEDURE NewExpression*(): Expression; (* for error handling: invalid Expression *)
  4657. VAR expression: Expression;
  4658. BEGIN
  4659. NEW(expression,invalidPosition); RETURN expression;
  4660. END NewExpression;
  4661. PROCEDURE CloneExpression*(e: Expression): Expression;
  4662. VAR copy: Expression;
  4663. BEGIN
  4664. IF e = NIL THEN
  4665. RETURN NIL
  4666. ELSE
  4667. copy := e.Clone();
  4668. copy.type := e.type;
  4669. copy.assignable := e.assignable;
  4670. copy.position := e.position;
  4671. copy.state := e.state;
  4672. IF e.resolved = e THEN copy.resolved := copy(Value);
  4673. ELSIF e.resolved # NIL THEN copy.resolved := CloneExpression(e.resolved)(Value);
  4674. END;
  4675. RETURN copy
  4676. END;
  4677. END CloneExpression;
  4678. PROCEDURE CloneDesignator*(e: Expression): Designator;
  4679. BEGIN
  4680. IF e = NIL THEN RETURN NIL ELSE RETURN CloneExpression(e)(Designator) END;
  4681. END CloneDesignator;
  4682. PROCEDURE NewElement*( position: Position; from,to: Expression ): Expression;
  4683. BEGIN
  4684. IF from = to THEN RETURN from
  4685. ELSE RETURN NewRangeExpression(position,from,to,NIL)
  4686. END;
  4687. END NewElement;
  4688. PROCEDURE NewSet*( position: Position ): Set;
  4689. VAR set: Set;
  4690. BEGIN NEW( set, position ); RETURN set
  4691. END NewSet;
  4692. PROCEDURE NewMathArrayExpression*( position: Position ): MathArrayExpression;
  4693. VAR mathArrayExpression: MathArrayExpression;
  4694. BEGIN NEW( mathArrayExpression, position ); RETURN mathArrayExpression
  4695. END NewMathArrayExpression;
  4696. PROCEDURE NewBinaryExpression*( position: Position; left, right: Expression; operator: LONGINT ): BinaryExpression;
  4697. VAR binaryExpression: BinaryExpression;
  4698. BEGIN
  4699. NEW( binaryExpression, position, left, right, operator ); RETURN binaryExpression;
  4700. END NewBinaryExpression;
  4701. PROCEDURE NewRangeExpression*(position: Position; first, last, step: Expression): RangeExpression;
  4702. VAR rangeExpression: RangeExpression;
  4703. BEGIN
  4704. NEW(rangeExpression, position, first, last, step); RETURN rangeExpression
  4705. END NewRangeExpression;
  4706. PROCEDURE NewTensorRangeExpression*(position: Position): TensorRangeExpression;
  4707. VAR tensorRangeExpression: TensorRangeExpression;
  4708. BEGIN
  4709. NEW(tensorRangeExpression,position); RETURN tensorRangeExpression
  4710. END NewTensorRangeExpression;
  4711. PROCEDURE NewUnaryExpression*( position: Position; operand: Expression; operator: LONGINT ): UnaryExpression;
  4712. VAR unaryExpression: UnaryExpression;
  4713. BEGIN
  4714. NEW( unaryExpression, position, operand, operator ); RETURN unaryExpression;
  4715. END NewUnaryExpression;
  4716. PROCEDURE NewConversion*( position: Position; expression: Expression; type: Type; typeExpression: Expression): Conversion;
  4717. VAR conversion: Conversion;
  4718. BEGIN
  4719. ASSERT(type # NIL);
  4720. NEW( conversion, position, expression,type, typeExpression ); RETURN conversion;
  4721. END NewConversion;
  4722. PROCEDURE NewValue*(): Value;(* for error handling: invalid Value *)
  4723. VAR value: Value;
  4724. BEGIN
  4725. NEW(value,invalidPosition); RETURN value;
  4726. END NewValue;
  4727. PROCEDURE NewIntegerValue*( position: Position; value: HUGEINT): IntegerValue;
  4728. VAR integerValue: IntegerValue;
  4729. BEGIN
  4730. NEW( integerValue, position, value); RETURN integerValue;
  4731. END NewIntegerValue;
  4732. PROCEDURE NewCharacterValue*( position: Position; value: CHAR): CharacterValue;
  4733. VAR characterValue: CharacterValue;
  4734. BEGIN
  4735. NEW( characterValue, position, value); RETURN characterValue;
  4736. END NewCharacterValue;
  4737. PROCEDURE NewSetValue*(position: Position; value: SET): SetValue;
  4738. VAR setValue: SetValue;
  4739. BEGIN
  4740. NEW(setValue, position, value); RETURN setValue
  4741. END NewSetValue;
  4742. PROCEDURE NewMathArrayValue*( position: Position ): MathArrayValue;
  4743. VAR mathArrayValue: MathArrayValue;
  4744. BEGIN NEW( mathArrayValue, position ); RETURN mathArrayValue
  4745. END NewMathArrayValue;
  4746. PROCEDURE NewRealValue*( position: Position; value: LONGREAL): RealValue;
  4747. VAR realValue: RealValue;
  4748. BEGIN
  4749. NEW( realValue, position, value); RETURN realValue
  4750. END NewRealValue;
  4751. PROCEDURE NewComplexValue*( position: Position; realValue, imagValue: LONGREAL): ComplexValue;
  4752. VAR complexValue: ComplexValue;
  4753. BEGIN
  4754. NEW( complexValue, position, realValue, imagValue); RETURN complexValue
  4755. END NewComplexValue;
  4756. PROCEDURE NewStringValue*( position: Position; value: String): StringValue;
  4757. VAR stringValue: StringValue;
  4758. BEGIN
  4759. NEW( stringValue, position, value ); RETURN stringValue
  4760. END NewStringValue;
  4761. PROCEDURE NewBooleanValue*( position: Position; value: BOOLEAN): BooleanValue;
  4762. VAR booleanValue: BooleanValue;
  4763. BEGIN
  4764. NEW( booleanValue, position, value ); RETURN booleanValue;
  4765. END NewBooleanValue;
  4766. PROCEDURE NewNilValue*( position: Position ): NilValue;
  4767. VAR nilValue: NilValue;
  4768. BEGIN
  4769. NEW( nilValue, position ); RETURN nilValue
  4770. END NewNilValue;
  4771. PROCEDURE NewEnumerationValue*( position: Position; value: LONGINT ): EnumerationValue;
  4772. VAR enumeratorValue: EnumerationValue;
  4773. BEGIN
  4774. NEW( enumeratorValue, position, value ); RETURN enumeratorValue
  4775. END NewEnumerationValue;
  4776. PROCEDURE NewStatement*(outer: Statement): Statement; (* for error handling: invalid Statement *)
  4777. VAR statement: Statement;
  4778. BEGIN NEW(statement,invalidPosition,outer); RETURN statement;
  4779. END NewStatement;
  4780. PROCEDURE CloneStatement*(statement: Statement): Statement;
  4781. BEGIN IF statement = NIL THEN RETURN NIL ELSE RETURN statement.Clone() END
  4782. END CloneStatement;
  4783. PROCEDURE NewStatementSequence*(): StatementSequence;
  4784. VAR statementSequence: StatementSequence;
  4785. BEGIN
  4786. NEW( statementSequence); RETURN statementSequence
  4787. END NewStatementSequence;
  4788. PROCEDURE CloneStatementSequence*(statementSequence: StatementSequence): StatementSequence;
  4789. VAR copy: StatementSequence;
  4790. BEGIN IF statementSequence = NIL THEN RETURN NIL ELSE statementSequence.Clone(copy); RETURN copy END
  4791. END CloneStatementSequence;
  4792. PROCEDURE NewModifier*(position: Position; identifier: Identifier; expression: Expression): Modifier;
  4793. VAR blockModifier: Modifier;
  4794. BEGIN
  4795. NEW(blockModifier,position,identifier,expression); RETURN blockModifier
  4796. END NewModifier;
  4797. PROCEDURE NewStatementBlock*( position: Position ; outer: Statement): StatementBlock;
  4798. VAR statementBlock: StatementBlock;
  4799. BEGIN
  4800. NEW( statementBlock, position, outer ); RETURN statementBlock
  4801. END NewStatementBlock;
  4802. PROCEDURE NewStatementDesignator*(position: Position; s: Statement): StatementDesignator;
  4803. VAR statementDesignator: StatementDesignator;
  4804. BEGIN
  4805. NEW( statementDesignator, position, s); RETURN statementDesignator
  4806. END NewStatementDesignator;
  4807. PROCEDURE NewBody*( position: Position ; scope: ProcedureScope): Body;
  4808. VAR body: Body;
  4809. BEGIN
  4810. NEW( body, position,scope ); RETURN body
  4811. END NewBody;
  4812. PROCEDURE NewIfPart*(): IfPart;
  4813. VAR ifPart: IfPart;
  4814. BEGIN
  4815. NEW( ifPart); RETURN ifPart
  4816. END NewIfPart;
  4817. PROCEDURE NewIfStatement*( position: Position ; outer: Statement): IfStatement;
  4818. VAR ifStatement: IfStatement;
  4819. BEGIN
  4820. NEW( ifStatement, position,outer ); RETURN ifStatement
  4821. END NewIfStatement;
  4822. PROCEDURE NewAssignment*( position: Position; left: Designator; right: Expression; outer: Statement): Assignment;
  4823. VAR assignment: Assignment;
  4824. BEGIN
  4825. NEW( assignment, position, left, right,outer ); RETURN assignment
  4826. END NewAssignment;
  4827. PROCEDURE NewCommunicationStatement*( position: Position; op: LONGINT; left: Designator; right: Expression; outer: Statement): CommunicationStatement;
  4828. VAR communication: CommunicationStatement;
  4829. BEGIN
  4830. NEW( communication, position, op, left, right,outer ); RETURN communication
  4831. END NewCommunicationStatement;
  4832. PROCEDURE NewProcedureCallStatement*(position: Position; call: Designator; outer: Statement): ProcedureCallStatement;
  4833. VAR caller: ProcedureCallStatement;
  4834. BEGIN
  4835. NEW(caller,position,call,outer); RETURN caller
  4836. END NewProcedureCallStatement;
  4837. PROCEDURE NewCaseStatement*( position: Position ; outer: Statement): CaseStatement;
  4838. VAR caseStatement: CaseStatement;
  4839. BEGIN
  4840. NEW( caseStatement, position,outer ); RETURN caseStatement
  4841. END NewCaseStatement;
  4842. PROCEDURE NewCasePart*(): CasePart;
  4843. VAR casePart: CasePart;
  4844. BEGIN
  4845. NEW( casePart); RETURN casePart
  4846. END NewCasePart;
  4847. PROCEDURE NewWithPart*(): WithPart;
  4848. VAR withPart: WithPart;
  4849. BEGIN
  4850. NEW( withPart); RETURN withPart
  4851. END NewWithPart;
  4852. PROCEDURE NewWithStatement*( position: Position; outer: Statement): WithStatement;
  4853. VAR withStatement: WithStatement;
  4854. BEGIN
  4855. NEW( withStatement, position, outer ); RETURN withStatement
  4856. END NewWithStatement;
  4857. PROCEDURE NewWhileStatement*( position: Position ; outer: Statement): WhileStatement;
  4858. VAR whileStatement: WhileStatement;
  4859. BEGIN
  4860. NEW( whileStatement, position,outer ); RETURN whileStatement
  4861. END NewWhileStatement;
  4862. PROCEDURE NewRepeatStatement*( position: Position ; outer: Statement): RepeatStatement;
  4863. VAR repeatStatement: RepeatStatement;
  4864. BEGIN
  4865. NEW( repeatStatement, position ,outer); RETURN repeatStatement
  4866. END NewRepeatStatement;
  4867. PROCEDURE NewForStatement*( position: Position; outer: Statement ): ForStatement;
  4868. VAR forStatement: ForStatement;
  4869. BEGIN
  4870. NEW( forStatement, position,outer ); RETURN forStatement
  4871. END NewForStatement;
  4872. PROCEDURE NewLoopStatement*( position: Position ; outer: Statement): LoopStatement;
  4873. VAR loopStatement: LoopStatement;
  4874. BEGIN
  4875. NEW( loopStatement, position ,outer); RETURN loopStatement
  4876. END NewLoopStatement;
  4877. PROCEDURE NewExitableBlock*( position: Position ; outer: Statement): ExitableBlock;
  4878. VAR loopStatement: ExitableBlock;
  4879. BEGIN
  4880. NEW( loopStatement, position ,outer); RETURN loopStatement
  4881. END NewExitableBlock;
  4882. PROCEDURE NewExitStatement*( position: Position ; outer: Statement): ExitStatement;
  4883. VAR exitStatement: ExitStatement;
  4884. BEGIN
  4885. NEW( exitStatement, position, outer); RETURN exitStatement
  4886. END NewExitStatement;
  4887. PROCEDURE NewReturnStatement*( position: Position; outer: Statement ): ReturnStatement;
  4888. VAR returnStatement: ReturnStatement;
  4889. BEGIN
  4890. NEW( returnStatement, position,outer ); RETURN returnStatement
  4891. END NewReturnStatement;
  4892. PROCEDURE NewAwaitStatement*( position: Position; outer: Statement ): AwaitStatement;
  4893. VAR awaitStatement: AwaitStatement;
  4894. BEGIN
  4895. NEW( awaitStatement, position, outer ); RETURN awaitStatement
  4896. END NewAwaitStatement;
  4897. PROCEDURE NewCode*(position: Position; outer: Statement): Code;
  4898. VAR code: Code;
  4899. BEGIN
  4900. NEW(code,position,outer); RETURN code
  4901. END NewCode;
  4902. PROCEDURE NewProcedureScope*(outer: Scope): ProcedureScope;
  4903. VAR scope: ProcedureScope;
  4904. BEGIN NEW(scope,outer); RETURN scope
  4905. END NewProcedureScope;
  4906. PROCEDURE NewModuleScope*(): ModuleScope;
  4907. VAR scope: ModuleScope;
  4908. BEGIN NEW(scope); RETURN scope
  4909. END NewModuleScope;
  4910. PROCEDURE NewRecordScope*(outer: Scope): RecordScope;
  4911. VAR scope: RecordScope;
  4912. BEGIN NEW(scope,outer); RETURN scope
  4913. END NewRecordScope;
  4914. PROCEDURE NewCellScope*(outer: Scope): CellScope;
  4915. VAR scope: CellScope;
  4916. BEGIN NEW(scope,outer); RETURN scope
  4917. END NewCellScope;
  4918. PROCEDURE NewEnumerationScope*(outer: Scope): EnumerationScope;
  4919. VAR scope: EnumerationScope;
  4920. BEGIN NEW(scope,outer); RETURN scope
  4921. END NewEnumerationScope;
  4922. PROCEDURE Init;
  4923. BEGIN;
  4924. invalidPosition.start := -1;
  4925. invalidIdentifier := Basic.invalidString;
  4926. invalidQualifiedIdentifier := NewQualifiedIdentifier(invalidPosition,invalidIdentifier,Basic.emptyString);
  4927. invalidType := NewType();
  4928. invalidDesignator := NewDesignator();
  4929. invalidDesignator.SetType(invalidType);
  4930. invalidExpression := invalidDesignator;
  4931. invalidValue := NewValue();
  4932. invalidSymbol := NewSymbol(NewIdentifier(""));
  4933. invalidSymbol.SetType(invalidType);
  4934. importType := NewType();
  4935. importType.SetState(Resolved);
  4936. typeDeclarationType := NewType();
  4937. typeDeclarationType.SetState(Resolved);
  4938. moduleType := NewType();
  4939. moduleType.SetState(Resolved);
  4940. anonymousIdentifier := NewIdentifier("");
  4941. indexListSeparator := NewDesignator();
  4942. indexListSeparator.SetType(invalidType);
  4943. END Init;
  4944. BEGIN
  4945. Init;
  4946. END FoxSyntaxTree.