FoxSyntaxTree.Mod 168 KB

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