FoxSyntaxTree.Mod 167 KB

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