FoxSyntaxTree.Mod 166 KB

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