FoxSyntaxTree.Mod 168 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. UndefinedCallingConvention*=6;
  24. (** Access Flags *)
  25. InternalRead* = 0; (** can read symbol in same module *)
  26. InternalWrite* = 1; (** can write symbol in same module *)
  27. ProtectedRead* = 2; (** can read symbol in type extentions *)
  28. ProtectedWrite* = 3; (** can write symbol in type extentions *)
  29. PublicRead* = 4; (** can read everywhere *)
  30. PublicWrite* = 5; (** can write everywhere *)
  31. Hidden* = {};
  32. Internal* = {InternalRead, InternalWrite};
  33. Protected* = {ProtectedRead, ProtectedWrite} ;
  34. Public* = {PublicRead, PublicWrite} ;
  35. ReadOnly* = {InternalRead, ProtectedRead,PublicRead};
  36. (** parameter forms *)
  37. ValueParameter* = 0; VarParameter* = 1; ConstParameter* = 2;
  38. InPort*=3; OutPort*=4;
  39. (** array forms *)
  40. Static*=1; (* ARRAY x OF .. / ARRAY [x] OF ... *)
  41. Open*=2; (* ARRAY OF ... / ARRAY [*] OF ... *)
  42. Tensor*=3; (* ARRAY [?] OF ... *)
  43. SemiDynamic*=4;
  44. (** node states, important for checker to avoid cycles *)
  45. Undefined*={}; BeingResolved*=1; Resolved*=2; FingerPrinted*=3; Warned*=4;
  46. (* context in which a range expression is used *)
  47. ArrayIndex* = 0;
  48. SetElement* = 1;
  49. CaseGuard* = 2;
  50. (* reflection flags *)
  51. FlagProcedureDelegate*=0;
  52. FlagProcedureConstructor*=1;
  53. FlagParameterVar*=1;
  54. FlagParameterConst*=2;
  55. TYPE
  56. Position*= Scanner.Position;
  57. SourceCode*= Scanner.StringType;
  58. BinaryCode*= BitSets.BitSet;
  59. String*= Scanner.StringType;
  60. IdentifierString*= Scanner.IdentifierString;
  61. CallingConvention*= LONGINT;
  62. (** visitor pattern implementation *)
  63. (* to use this object in your implementation, copy and paste and replace "x: " by "x: SyntaxTree." *)
  64. Visitor* = OBJECT
  65. (** types *)
  66. PROCEDURE VisitType*(x: Type);
  67. BEGIN HALT(100) (* abstract *) END VisitType;
  68. PROCEDURE VisitBasicType*(x: BasicType);
  69. BEGIN HALT(100) (* abstract *) END VisitBasicType;
  70. PROCEDURE VisitByteType*(x: ByteType);
  71. BEGIN HALT(100) (* abstract *) END VisitByteType;
  72. PROCEDURE VisitAnyType*(x: AnyType);
  73. BEGIN HALT(100) (* abstract *) END VisitAnyType;
  74. PROCEDURE VisitObjectType*(x: ObjectType);
  75. BEGIN HALT(100) (* abstract *) END VisitObjectType;
  76. PROCEDURE VisitNilType*(x: NilType);
  77. BEGIN HALT(100) (* abstract *) END VisitNilType;
  78. PROCEDURE VisitAddressType*(x: AddressType);
  79. BEGIN HALT(100) (* abstract *) END VisitAddressType;
  80. PROCEDURE VisitSizeType*(x: SizeType);
  81. BEGIN HALT(100) (* abstract *) END VisitSizeType;
  82. PROCEDURE VisitBooleanType*(x: BooleanType);
  83. BEGIN HALT(100) (* abstract *) END VisitBooleanType;
  84. PROCEDURE VisitSetType*(x: SetType);
  85. BEGIN HALT(100) (* abstract *) END VisitSetType;
  86. PROCEDURE VisitCharacterType*(x: CharacterType);
  87. BEGIN HALT(100) END VisitCharacterType;
  88. PROCEDURE VisitIntegerType*(x: IntegerType);
  89. BEGIN HALT(100) END VisitIntegerType;
  90. PROCEDURE VisitFloatType*(x: FloatType);
  91. BEGIN HALT(100) END VisitFloatType;
  92. PROCEDURE VisitComplexType*(x: ComplexType);
  93. BEGIN HALT(100) END VisitComplexType;
  94. PROCEDURE VisitQualifiedType*(x: QualifiedType);
  95. BEGIN HALT(100) (* abstract *) END VisitQualifiedType;
  96. PROCEDURE VisitStringType*(x: StringType);
  97. BEGIN HALT(100) (* abstract *) END VisitStringType;
  98. PROCEDURE VisitEnumerationType*(x: EnumerationType);
  99. BEGIN HALT(100) (* abstract *) END VisitEnumerationType;
  100. PROCEDURE VisitRangeType*(x: RangeType);
  101. BEGIN HALT(100) (* abstract *) END VisitRangeType;
  102. PROCEDURE VisitArrayType*(x: ArrayType);
  103. BEGIN HALT(100) (* abstract *) END VisitArrayType;
  104. PROCEDURE VisitMathArrayType*(x: MathArrayType);
  105. BEGIN HALT(100) (* abstract *) END VisitMathArrayType;
  106. PROCEDURE VisitPointerType*(x: PointerType);
  107. BEGIN HALT(100) (* abstract *) END VisitPointerType;
  108. PROCEDURE VisitPortType*(x: PortType);
  109. BEGIN HALT(100) (* abstract *) END VisitPortType;
  110. PROCEDURE VisitRecordType*(x: RecordType);
  111. BEGIN HALT(100) (* abstract *) END VisitRecordType;
  112. PROCEDURE VisitCellType*(x: CellType);
  113. BEGIN HALT(100) (* abstract *) END VisitCellType;
  114. PROCEDURE VisitProcedureType*(x: ProcedureType);
  115. BEGIN HALT(100) (* abstract *) END VisitProcedureType;
  116. (** expressions *)
  117. PROCEDURE VisitExpression*(x: Expression);
  118. BEGIN HALT(100) (* abstract *) END VisitExpression;
  119. PROCEDURE VisitSet*(x: Set);
  120. BEGIN HALT(100) (* abstract *) END VisitSet;
  121. PROCEDURE VisitMathArrayExpression*(x: MathArrayExpression);
  122. BEGIN HALT(100) (* abstract *) END VisitMathArrayExpression;
  123. PROCEDURE VisitUnaryExpression*(x: UnaryExpression);
  124. BEGIN HALT(100) (* abstract *) END VisitUnaryExpression;
  125. PROCEDURE VisitBinaryExpression*(x: BinaryExpression);
  126. BEGIN HALT(100) (* abstract *) END VisitBinaryExpression;
  127. PROCEDURE VisitRangeExpression*(x: RangeExpression);
  128. BEGIN HALT(100) (* abstract *) END VisitRangeExpression;
  129. PROCEDURE VisitTensorRangeExpression*(x: TensorRangeExpression);
  130. BEGIN HALT(100) (* abstract *) END VisitTensorRangeExpression;
  131. PROCEDURE VisitConversion*(x: Conversion);
  132. BEGIN HALT(100) (* abstract *) END VisitConversion;
  133. (** designators (expressions) *)
  134. PROCEDURE VisitDesignator*(x: Designator);
  135. BEGIN HALT(100) (* abstract *) END VisitDesignator;
  136. PROCEDURE VisitIdentifierDesignator*(x: IdentifierDesignator);
  137. BEGIN HALT(100) (* abstract *) END VisitIdentifierDesignator;
  138. PROCEDURE VisitSelectorDesignator*(x: SelectorDesignator);
  139. BEGIN HALT(100) (* abstract *) END VisitSelectorDesignator;
  140. PROCEDURE VisitParameterDesignator*(x: ParameterDesignator);
  141. BEGIN HALT(100) (* abstract *) END VisitParameterDesignator;
  142. PROCEDURE VisitArrowDesignator*(x: ArrowDesignator);
  143. BEGIN HALT(100) (* abstract *) END VisitArrowDesignator;
  144. PROCEDURE VisitBracketDesignator*(x: BracketDesignator);
  145. BEGIN HALT(100) (* abstract *) END VisitBracketDesignator;
  146. PROCEDURE VisitSymbolDesignator*(x: SymbolDesignator);
  147. BEGIN HALT(100) (* abstract *) END VisitSymbolDesignator;
  148. PROCEDURE VisitIndexDesignator*(x: IndexDesignator);
  149. BEGIN HALT(100) (* abstract *) END VisitIndexDesignator;
  150. PROCEDURE VisitProcedureCallDesignator*(x: ProcedureCallDesignator);
  151. BEGIN HALT(100) (* abstract *) END VisitProcedureCallDesignator;
  152. PROCEDURE VisitStatementDesignator*(x: StatementDesignator);
  153. BEGIN HALT(100) (* abstract *) END VisitStatementDesignator;
  154. PROCEDURE VisitBuiltinCallDesignator*(x: BuiltinCallDesignator);
  155. BEGIN HALT(100) (* abstract *) END VisitBuiltinCallDesignator;
  156. PROCEDURE VisitTypeGuardDesignator*(x: TypeGuardDesignator);
  157. BEGIN HALT(100) (* abstract *) END VisitTypeGuardDesignator;
  158. PROCEDURE VisitDereferenceDesignator*(x: DereferenceDesignator);
  159. BEGIN HALT(100) (* abstract *) END VisitDereferenceDesignator;
  160. PROCEDURE VisitSupercallDesignator*(x: SupercallDesignator);
  161. BEGIN HALT(100) (* abstract *) END VisitSupercallDesignator;
  162. PROCEDURE VisitSelfDesignator*(x: SelfDesignator);
  163. BEGIN HALT(100) (* abstract *) END VisitSelfDesignator;
  164. PROCEDURE VisitResultDesignator*(x: ResultDesignator);
  165. BEGIN HALT(100) (* abstract *) END VisitResultDesignator;
  166. (** values *)
  167. PROCEDURE VisitValue*(x: Value);
  168. BEGIN HALT(100) (* abstract *) END VisitValue;
  169. PROCEDURE VisitBooleanValue*(x: BooleanValue);
  170. BEGIN HALT(100) (* abstract *) END VisitBooleanValue;
  171. PROCEDURE VisitIntegerValue*(x: IntegerValue);
  172. BEGIN HALT(100) (* abstract *) END VisitIntegerValue;
  173. PROCEDURE VisitCharacterValue*(x: CharacterValue);
  174. BEGIN HALT(100) (* abstract *) END VisitCharacterValue;
  175. PROCEDURE VisitSetValue*(x: SetValue);
  176. BEGIN HALT(100) (* abstract *) END VisitSetValue;
  177. PROCEDURE VisitMathArrayValue*(x: MathArrayValue);
  178. BEGIN HALT(100) (* abstract *) END VisitMathArrayValue;
  179. PROCEDURE VisitRealValue*(x: RealValue);
  180. BEGIN HALT(100) (* abstract *) END VisitRealValue;
  181. PROCEDURE VisitComplexValue*(x: ComplexValue);
  182. BEGIN HALT(100) (* abstract *) END VisitComplexValue;
  183. PROCEDURE VisitStringValue*(x: StringValue);
  184. BEGIN HALT(100) (* abstract *) END VisitStringValue;
  185. PROCEDURE VisitNilValue*(x: NilValue);
  186. BEGIN HALT(100) (* abstract *) END VisitNilValue;
  187. PROCEDURE VisitEnumerationValue*(x: EnumerationValue);
  188. BEGIN HALT(100) (* abstract *) END VisitEnumerationValue;
  189. (** symbols *)
  190. PROCEDURE VisitSymbol*(x: Symbol);
  191. BEGIN HALT(100) (* abstract *) END VisitSymbol;
  192. PROCEDURE VisitModule*(x: Module);
  193. BEGIN HALT(100) (* abstract *) END VisitModule;
  194. PROCEDURE VisitTypeDeclaration*(x: TypeDeclaration);
  195. BEGIN HALT(100) (* abstract *) END VisitTypeDeclaration;
  196. PROCEDURE VisitConstant*(x: Constant);
  197. BEGIN HALT(100) (* abstract *) END VisitConstant;
  198. PROCEDURE VisitVariable*(x: Variable);
  199. BEGIN HALT(100) (* abstract *) END VisitVariable;
  200. PROCEDURE VisitParameter*(x: Parameter);
  201. BEGIN HALT(100) (* abstract *) END VisitParameter;
  202. PROCEDURE VisitProperty*(x: Property);
  203. BEGIN HALT(100) (* abstract *) END VisitProperty;
  204. PROCEDURE VisitProcedure*(x: Procedure);
  205. BEGIN HALT(100) (* abstract *) END VisitProcedure;
  206. PROCEDURE VisitBuiltin*(x: Builtin);
  207. BEGIN HALT(100) (* abstract *) END VisitBuiltin;
  208. PROCEDURE VisitOperator*(x: Operator);
  209. BEGIN HALT(100) (* abstract *) END VisitOperator;
  210. PROCEDURE VisitImport*(x: Import);
  211. BEGIN HALT(100) (* abstract *) END VisitImport;
  212. (** statements *)
  213. PROCEDURE VisitStatement*(x: Statement);
  214. BEGIN HALT(100) (* abstract *) END VisitStatement;
  215. PROCEDURE VisitProcedureCallStatement*(x: ProcedureCallStatement);
  216. BEGIN HALT(100) (* abstract *) END VisitProcedureCallStatement;
  217. PROCEDURE VisitAssignment*(x: Assignment);
  218. BEGIN HALT(100) (* abstract *) END VisitAssignment;
  219. PROCEDURE VisitCommunicationStatement*(x: CommunicationStatement);
  220. BEGIN HALT(100) (* abstract *) END VisitCommunicationStatement;
  221. PROCEDURE VisitIfStatement*(x: IfStatement);
  222. BEGIN HALT(100) (* abstract *) END VisitIfStatement;
  223. PROCEDURE VisitWithStatement*(x: WithStatement);
  224. BEGIN HALT(100) (* abstract *) END VisitWithStatement;
  225. PROCEDURE VisitCaseStatement*(x: CaseStatement);
  226. BEGIN HALT(100) (* abstract *) END VisitCaseStatement;
  227. PROCEDURE VisitWhileStatement*(x: WhileStatement);
  228. BEGIN HALT(100) (* abstract *) END VisitWhileStatement;
  229. PROCEDURE VisitRepeatStatement*(x: RepeatStatement);
  230. BEGIN HALT(100) (* abstract *) END VisitRepeatStatement;
  231. PROCEDURE VisitForStatement*(x: ForStatement);
  232. BEGIN HALT(100) (* abstract *) END VisitForStatement;
  233. PROCEDURE VisitLoopStatement*(x: LoopStatement);
  234. BEGIN HALT(100) (* abstract *) END VisitLoopStatement;
  235. PROCEDURE VisitExitableBlock*(x: ExitableBlock);
  236. BEGIN HALT(100) (* abstract *) END VisitExitableBlock;
  237. PROCEDURE VisitExitStatement*(x: ExitStatement);
  238. BEGIN HALT(100) (* abstract *) END VisitExitStatement;
  239. PROCEDURE VisitReturnStatement*(x: ReturnStatement);
  240. BEGIN HALT(100) (* abstract *) END VisitReturnStatement;
  241. PROCEDURE VisitAwaitStatement*(x: AwaitStatement);
  242. BEGIN HALT(100) (* abstract *) END VisitAwaitStatement;
  243. PROCEDURE VisitStatementBlock*(x: StatementBlock);
  244. BEGIN HALT(100) (* abstract *) END VisitStatementBlock;
  245. PROCEDURE VisitCode*(x: Code);
  246. BEGIN HALT(100) (* abstract *) END VisitCode;
  247. END Visitor;
  248. ArrayAccessOperators* = RECORD
  249. len*: Operator; (* length operator *)
  250. generalRead*, generalWrite*: Operator; (* operators on ARRAY [*] RANGE, for tensors *)
  251. read*, write*: POINTER TO ARRAY OF Operator; (* fixed-dim. operators *)
  252. END;
  253. FingerPrint*= RECORD
  254. shallow*,public*, private*: LONGINT;
  255. shallowAvailable*, deepAvailable*: BOOLEAN;
  256. END;
  257. (** identifiers in a program text **)
  258. Identifier* = Basic.String;
  259. (** qualified identifiers << Identifier.Identifier >> **)
  260. QualifiedIdentifier* = OBJECT
  261. VAR
  262. prefix-, suffix-: Identifier; (* use string index instead ? *)
  263. position-: Position;
  264. PROCEDURE & InitQualifiedIdentifier( position: Position; prefix, suffix: Identifier);
  265. BEGIN
  266. (* ASSERT(suffix # invalidIdentifier); can happen but should be catched by the parser with error report and not here with trap *)
  267. SELF.position := position;
  268. SELF.prefix := prefix; SELF.suffix := suffix;
  269. END InitQualifiedIdentifier;
  270. PROCEDURE GetName*(VAR name: Basic.SegmentedName);
  271. BEGIN
  272. Basic.InitSegmentedName(name);
  273. IF prefix # invalidIdentifier THEN Basic.SuffixSegmentedName(name, prefix) END;
  274. Basic.SuffixSegmentedName(name, suffix)
  275. END GetName;
  276. END QualifiedIdentifier;
  277. (**** types ****)
  278. (**
  279. Type
  280. BasicType
  281. ObjectType
  282. NilType
  283. AnyType
  284. ByteType
  285. AddressType
  286. SizeType
  287. BooleanType
  288. SetType
  289. CharacterType
  290. RangeType
  291. NumberType
  292. IntegerType
  293. FloatType
  294. ComplexType
  295. QualifiedType
  296. StringType
  297. EnumerationType
  298. ArrayType
  299. MathArrayType
  300. PointerType
  301. PortType
  302. RecordType
  303. CellType
  304. ProcedureType
  305. *)
  306. Type* = OBJECT
  307. VAR
  308. typeDeclaration-: TypeDeclaration; (* link to declaration (if any), needed for printing, debugging and symbol lookup *)
  309. scope-: Scope; (* scope where the type has been declared *)
  310. resolved-: Type; (* indirection to resolved type to preserve qualified types *)
  311. position-,end-: Position;
  312. state-: SET;
  313. hasPointers-: BOOLEAN;
  314. fingerprint-: FingerPrint;
  315. isRealtime-: BOOLEAN;
  316. recursion: BOOLEAN;
  317. sizeInBits-: LONGINT; (* allocation size of this type in bits *)
  318. alignmentInBits-: LONGINT;
  319. PROCEDURE & InitType*( position: Position);
  320. BEGIN
  321. SELF.position := position; state := Undefined;
  322. end := invalidPosition;
  323. typeDeclaration := NIL;
  324. scope := NIL;
  325. resolved := SELF;
  326. sizeInBits := MIN(LONGINT);
  327. alignmentInBits := 0;
  328. isRealtime := FALSE;
  329. recursion := FALSE;
  330. hasPointers := FALSE;
  331. InitFingerPrint(fingerprint);
  332. END InitType;
  333. PROCEDURE SetSize*(sizeInBits: LONGINT);
  334. BEGIN SELF.sizeInBits := sizeInBits
  335. END SetSize;
  336. PROCEDURE SetAlignmentInBits*(alignmentInBits: LONGINT);
  337. BEGIN SELF.alignmentInBits := alignmentInBits
  338. END SetAlignmentInBits;
  339. PROCEDURE End*( position: LONGINT );
  340. BEGIN SELF.position.end := position;
  341. END End;
  342. PROCEDURE SetFingerPrint*(CONST fp: FingerPrint);
  343. BEGIN
  344. SELF.fingerprint := fp
  345. END SetFingerPrint;
  346. PROCEDURE SetState*(state: LONGINT);
  347. BEGIN INCL(SELF.state,state);
  348. END SetState;
  349. PROCEDURE SetHasPointers*(has: BOOLEAN);
  350. BEGIN
  351. hasPointers := has
  352. END SetHasPointers;
  353. PROCEDURE RemoveState*(state: LONGINT);
  354. BEGIN EXCL(SELF.state,state)
  355. END RemoveState;
  356. PROCEDURE SetTypeDeclaration*(typeDeclaration: TypeDeclaration);
  357. BEGIN SELF.typeDeclaration := typeDeclaration
  358. END SetTypeDeclaration;
  359. PROCEDURE SetScope*(scope: Scope);
  360. BEGIN SELF.scope := scope
  361. END SetScope;
  362. PROCEDURE SetRealtime*(isRealtime: BOOLEAN);
  363. BEGIN SELF.isRealtime := isRealtime
  364. END SetRealtime;
  365. PROCEDURE SameType*(this: Type): BOOLEAN;
  366. BEGIN RETURN FALSE
  367. END SameType;
  368. (** assignment compatibility of this := SELF *)
  369. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  370. BEGIN RETURN FALSE
  371. END CompatibleTo;
  372. (** Returns if the type is a pointer *)
  373. PROCEDURE IsPointer*(): BOOLEAN;
  374. BEGIN RETURN FALSE
  375. END IsPointer;
  376. (** Returns if the type consists of more than one parts. Implies that an instance of this type cannot be (easily) represented in one register. *)
  377. PROCEDURE IsComposite*(): BOOLEAN;
  378. BEGIN RETURN FALSE
  379. END IsComposite;
  380. (** Returns if the type needs to be traced for garbage collection *)
  381. PROCEDURE NeedsTrace*(): BOOLEAN;
  382. BEGIN RETURN IsPointer ();
  383. END NeedsTrace;
  384. PROCEDURE Accept*(v: Visitor);
  385. VAR position: LONGINT;
  386. BEGIN position := SELF.position.start; v.VisitType(SELF)
  387. END Accept;
  388. PROCEDURE IsRecordType*(): BOOLEAN;
  389. BEGIN
  390. RETURN FALSE;
  391. END IsRecordType;
  392. END Type;
  393. (* basic types, defined in global name space *)
  394. BasicType*= OBJECT(Type)
  395. VAR name-: Identifier;
  396. PROCEDURE & InitBasicType(CONST id: ARRAY OF CHAR; sizeInBits: LONGINT);
  397. VAR str: IdentifierString;
  398. BEGIN
  399. COPY(id, str);Basic.AppendNumber(str,sizeInBits); name := NewIdentifier(str);
  400. InitType(invalidPosition);
  401. SetSize(sizeInBits);
  402. SELF.name := name
  403. END InitBasicType;
  404. PROCEDURE SetName*(CONST id: ARRAY OF CHAR);
  405. BEGIN
  406. name := NewIdentifier(id);
  407. END SetName;
  408. PROCEDURE SetTypeDeclaration*(typeDeclaration: TypeDeclaration);
  409. BEGIN HALT(100);
  410. END SetTypeDeclaration;
  411. PROCEDURE Accept*(v: Visitor);
  412. VAR position: LONGINT;
  413. BEGIN position := SELF.position.start; v.VisitBasicType(SELF)
  414. END Accept;
  415. END BasicType;
  416. (** <<OBJECT>>
  417. object type (base type of all objects)
  418. **)
  419. ObjectType*=OBJECT(BasicType)
  420. PROCEDURE & InitObjectType(sizeInBits: LONGINT);
  421. BEGIN
  422. InitBasicType("@Object",sizeInBits);
  423. hasPointers := TRUE;
  424. END InitObjectType;
  425. PROCEDURE SameType*(this: Type): BOOLEAN;
  426. BEGIN RETURN (this IS ObjectType)
  427. END SameType;
  428. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  429. BEGIN RETURN ((to IS AnyType) OR (to IS ObjectType))
  430. END CompatibleTo;
  431. PROCEDURE Accept*(v: Visitor);
  432. VAR position: LONGINT;
  433. BEGIN position := SELF.position.start; v.VisitObjectType(SELF)
  434. END Accept;
  435. PROCEDURE IsPointer*(): BOOLEAN;
  436. BEGIN RETURN TRUE
  437. END IsPointer;
  438. END ObjectType;
  439. (** <<NIL>>
  440. nil type (type of NIL pointers), may be replaced by any type
  441. **)
  442. NilType*=OBJECT(BasicType)
  443. PROCEDURE & InitNilType(sizeInBits: LONGINT);
  444. BEGIN
  445. InitBasicType("@Nil",sizeInBits);
  446. SetRealtime(TRUE);
  447. hasPointers := TRUE;
  448. END InitNilType;
  449. PROCEDURE SameType*(this: Type): BOOLEAN;
  450. BEGIN RETURN (this IS NilType)
  451. END SameType;
  452. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  453. 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)
  454. END CompatibleTo;
  455. PROCEDURE IsPointer*(): BOOLEAN;
  456. BEGIN RETURN TRUE
  457. END IsPointer;
  458. PROCEDURE Accept*(v: Visitor);
  459. VAR position: LONGINT;
  460. BEGIN position := SELF.position.start; v.VisitNilType(SELF)
  461. END Accept;
  462. END NilType;
  463. (** <<SYSTEM.BYTE>>
  464. any pointer type (pointer to record and pointer to array)
  465. **)
  466. AnyType*=OBJECT(BasicType)
  467. PROCEDURE & InitAnyType(sizeInBits: LONGINT);
  468. BEGIN
  469. InitBasicType("@Any",sizeInBits);
  470. hasPointers := TRUE;
  471. END InitAnyType;
  472. PROCEDURE SameType*(this: Type): BOOLEAN;
  473. BEGIN RETURN this IS AnyType
  474. END SameType;
  475. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  476. BEGIN RETURN (to IS AnyType)
  477. END CompatibleTo;
  478. PROCEDURE IsPointer*(): BOOLEAN;
  479. BEGIN RETURN TRUE
  480. END IsPointer;
  481. PROCEDURE Accept*(v: Visitor);
  482. VAR position: LONGINT;
  483. BEGIN position := SELF.position.start; v.VisitAnyType(SELF)
  484. END Accept;
  485. END AnyType;
  486. (** <<SYSTEM.BYTE>>
  487. byte type
  488. **)
  489. ByteType*=OBJECT(BasicType)
  490. PROCEDURE & InitByteType(sizeInBits: LONGINT);
  491. BEGIN
  492. InitBasicType("@Byte",sizeInBits);
  493. SetRealtime(TRUE);
  494. END InitByteType;
  495. PROCEDURE Accept*(v: Visitor);
  496. VAR position: LONGINT;
  497. BEGIN position := SELF.position.start; v.VisitByteType(SELF)
  498. END Accept;
  499. PROCEDURE SameType*(this: Type): BOOLEAN;
  500. BEGIN RETURN this IS ByteType
  501. END SameType;
  502. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  503. BEGIN RETURN (to IS ByteType)
  504. END CompatibleTo;
  505. END ByteType;
  506. (** <<ADDRESS>>
  507. address type
  508. **)
  509. AddressType*=OBJECT(BasicType)
  510. PROCEDURE & InitAddressType(sizeInBits: LONGINT);
  511. BEGIN
  512. InitBasicType("@Address",sizeInBits);
  513. SetRealtime(TRUE);
  514. END InitAddressType;
  515. PROCEDURE SameType*(this: Type): BOOLEAN;
  516. BEGIN RETURN (this IS AddressType)
  517. END SameType;
  518. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  519. BEGIN RETURN (to IS AddressType) OR (to IS SizeType) OR (to IS IntegerType) & (to.sizeInBits >= sizeInBits) OR (to IS PointerType) & to(PointerType).isUnsafe
  520. END CompatibleTo;
  521. PROCEDURE Accept*(v: Visitor);
  522. VAR position: LONGINT;
  523. BEGIN position := SELF.position.start; v.VisitAddressType(SELF)
  524. END Accept;
  525. END AddressType;
  526. (** <<SIZE>>
  527. size type (signed address type)
  528. **)
  529. SizeType*=OBJECT(BasicType)
  530. PROCEDURE & InitSizeType(sizeInBits: LONGINT);
  531. BEGIN
  532. InitBasicType("@Size",sizeInBits);
  533. SetRealtime(TRUE);
  534. END InitSizeType;
  535. PROCEDURE SameType*(this: Type): BOOLEAN;
  536. BEGIN RETURN (this IS SizeType)
  537. END SameType;
  538. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  539. BEGIN RETURN (to IS SizeType) OR (to IS AddressType) OR (to IS IntegerType) & (to.sizeInBits >= sizeInBits)
  540. END CompatibleTo;
  541. PROCEDURE Accept*(v: Visitor);
  542. VAR position: LONGINT;
  543. BEGIN position := SELF.position.start; v.VisitSizeType(SELF)
  544. END Accept;
  545. END SizeType;
  546. (** <<BOOLEAN>>
  547. boolean type
  548. **)
  549. BooleanType*=OBJECT(BasicType)
  550. PROCEDURE & InitBooleanType(sizeInBits: LONGINT);
  551. BEGIN
  552. InitBasicType("@Boolean",sizeInBits);
  553. SetRealtime(TRUE);
  554. END InitBooleanType;
  555. PROCEDURE SameType*(this: Type): BOOLEAN;
  556. BEGIN RETURN this IS BooleanType
  557. END SameType;
  558. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  559. BEGIN RETURN (to IS BooleanType)
  560. END CompatibleTo;
  561. PROCEDURE Accept*(v: Visitor);
  562. VAR position: LONGINT;
  563. BEGIN position := SELF.position.start; v.VisitBooleanType(SELF)
  564. END Accept;
  565. END BooleanType;
  566. (** <<SET>>
  567. set type
  568. **)
  569. SetType*=OBJECT(BasicType)
  570. PROCEDURE & InitSetType(sizeInBits: LONGINT);
  571. BEGIN
  572. InitBasicType("@Set",sizeInBits);
  573. SetRealtime(TRUE);
  574. END InitSetType;
  575. PROCEDURE SameType*(this: Type): BOOLEAN;
  576. BEGIN RETURN (this = SELF) OR (this IS SetType) & (this.sizeInBits = sizeInBits);
  577. END SameType;
  578. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  579. BEGIN RETURN (to IS SetType) & (to.sizeInBits >= sizeInBits)
  580. END CompatibleTo;
  581. PROCEDURE Accept*(v: Visitor);
  582. VAR position: LONGINT;
  583. BEGIN position := SELF.position.start; v.VisitSetType(SELF)
  584. END Accept;
  585. END SetType;
  586. (** <<CHAR, CHAR8, CHAR16, CHAR32>>
  587. character types
  588. **)
  589. CharacterType*=OBJECT(BasicType)
  590. PROCEDURE & InitCharacterType(sizeInBits: LONGINT);
  591. BEGIN
  592. InitBasicType("@Character", sizeInBits);
  593. SetRealtime(TRUE);
  594. END InitCharacterType;
  595. PROCEDURE SameType*(this: Type): BOOLEAN;
  596. BEGIN RETURN (this = SELF) OR (this IS CharacterType) & (this.sizeInBits = sizeInBits)
  597. END SameType;
  598. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  599. BEGIN RETURN ((to IS CharacterType) OR (to IS ByteType)) & (to.sizeInBits >= sizeInBits)
  600. END CompatibleTo;
  601. PROCEDURE Accept*(v: Visitor);
  602. VAR position: LONGINT;
  603. BEGIN position := SELF.position.start; v.VisitCharacterType(SELF)
  604. END Accept;
  605. END CharacterType;
  606. (** type of ranges (case constants, set elements, array indices)
  607. represented by basic type <<RANGE>>
  608. **)
  609. RangeType* = OBJECT(BasicType)
  610. PROCEDURE & InitRangeType(sizeInBits: LONGINT);
  611. BEGIN
  612. InitBasicType("@RangeType",sizeInBits);
  613. SetRealtime(TRUE);
  614. END InitRangeType;
  615. PROCEDURE SameType*(this: Type): BOOLEAN;
  616. BEGIN RETURN (this = SELF) OR (this IS RangeType)
  617. END SameType;
  618. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  619. BEGIN RETURN SameType(to)
  620. END CompatibleTo;
  621. PROCEDURE IsComposite*(): BOOLEAN;
  622. BEGIN RETURN TRUE
  623. END IsComposite;
  624. PROCEDURE Accept*(v: Visitor);
  625. VAR position: LONGINT;
  626. BEGIN position := SELF.position.start; v.VisitRangeType(SELF)
  627. END Accept;
  628. END RangeType;
  629. (* number types: IntegerType or FloatType *)
  630. NumberType*=OBJECT(BasicType)
  631. PROCEDURE & InitNumberType( CONST name: ARRAY OF CHAR; sizeInBits: LONGINT);
  632. BEGIN
  633. InitBasicType(name, sizeInBits);
  634. SetRealtime(TRUE);
  635. END InitNumberType;
  636. END NumberType;
  637. (** <<SHORTINT, INTEGER, LONGINT, HUGEINT>>
  638. integer types
  639. **)
  640. IntegerType*= OBJECT (NumberType)
  641. VAR signed-: BOOLEAN;
  642. PROCEDURE & InitIntegerType(sizeInBits: LONGINT; signed: BOOLEAN);
  643. BEGIN
  644. IF signed THEN
  645. InitNumberType("@Integer",sizeInBits);
  646. ELSE
  647. InitNumberType("@Unsigned",sizeInBits);
  648. END;
  649. SELF.signed := signed;
  650. END InitIntegerType;
  651. PROCEDURE SameType*(this: Type): BOOLEAN;
  652. BEGIN RETURN (this = SELF) OR (this IS IntegerType) & (this.sizeInBits = sizeInBits) & (this(IntegerType).signed = signed)
  653. END SameType;
  654. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  655. BEGIN RETURN ((to IS IntegerType) OR (to IS AddressType) OR (to IS SizeType) OR (to IS ByteType)) & (to.sizeInBits >= sizeInBits) OR (to IS FloatType)
  656. OR (to IS ComplexType) & CompatibleTo((to(ComplexType).componentType))
  657. END CompatibleTo;
  658. PROCEDURE Accept*(v: Visitor);
  659. VAR position: LONGINT;
  660. BEGIN position := SELF.position.start; v.VisitIntegerType(SELF)
  661. END Accept;
  662. END IntegerType;
  663. (** <<REAL,LONGREAL>>
  664. real types: REAL, LONGREAL
  665. **)
  666. FloatType*= OBJECT (NumberType)
  667. PROCEDURE & InitFloatType(sizeInBits: LONGINT);
  668. BEGIN
  669. InitNumberType("@Float",sizeInBits);
  670. END InitFloatType;
  671. PROCEDURE SameType*(this: Type): BOOLEAN;
  672. BEGIN RETURN (this = SELF) OR (this IS FloatType) & (this.sizeInBits = sizeInBits)
  673. END SameType;
  674. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  675. BEGIN
  676. RETURN (to IS FloatType) & (to.sizeInBits >= sizeInBits)
  677. OR (to IS ComplexType) & CompatibleTo((to(ComplexType).componentType))
  678. END CompatibleTo;
  679. PROCEDURE Accept*(v: Visitor);
  680. VAR position: LONGINT;
  681. BEGIN position := SELF.position.start; v.VisitFloatType(SELF)
  682. END Accept;
  683. END FloatType;
  684. (** <<COMPLEX,LONGCOMPLEX>>
  685. complex types: COMPLEX, LONGCOMPLEX
  686. **)
  687. ComplexType*= OBJECT (NumberType)
  688. VAR componentType-: Type; (* REAL or LONGREAL*)
  689. PROCEDURE & InitComplexType(componentType: Type);
  690. BEGIN
  691. ASSERT(componentType # NIL);
  692. SELF.componentType := componentType;
  693. sizeInBits := 2 * componentType.sizeInBits;
  694. InitNumberType("@Complex",sizeInBits);
  695. END InitComplexType;
  696. PROCEDURE SameType*(this: Type): BOOLEAN;
  697. BEGIN RETURN (this = SELF) OR (this IS ComplexType) & (componentType.SameType(this(ComplexType).componentType))
  698. END SameType;
  699. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  700. BEGIN RETURN (to IS ComplexType) & (componentType.CompatibleTo(to(ComplexType).componentType))
  701. END CompatibleTo;
  702. PROCEDURE IsComposite*(): BOOLEAN;
  703. BEGIN RETURN TRUE
  704. END IsComposite;
  705. PROCEDURE Accept*(v: Visitor);
  706. VAR position: LONGINT;
  707. BEGIN position := SELF.position.start; v.VisitComplexType(SELF)
  708. END Accept;
  709. END ComplexType;
  710. (** <<qualifiedIdentifier = resolved>>
  711. named reference to a type
  712. **)
  713. QualifiedType* = OBJECT (Type)
  714. VAR
  715. qualifiedIdentifier-: QualifiedIdentifier;
  716. PROCEDURE & InitQualifiedType( position: Position; scope: Scope; qualifiedIdentifier: QualifiedIdentifier);
  717. BEGIN
  718. ASSERT(qualifiedIdentifier # NIL);
  719. InitType( position);
  720. SELF.scope := scope;
  721. SELF.qualifiedIdentifier := qualifiedIdentifier;
  722. resolved := NIL;
  723. END InitQualifiedType;
  724. PROCEDURE SetResolved*(resolved: Type);
  725. BEGIN SELF.resolved := resolved; IF resolved # NIL THEN hasPointers := resolved.hasPointers END;
  726. END SetResolved;
  727. PROCEDURE SameType*(this: Type): BOOLEAN;
  728. BEGIN RETURN (this = SELF) OR (resolved # NIL) & (this.resolved # NIL) & resolved.SameType(this.resolved)
  729. END SameType;
  730. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  731. BEGIN RETURN (resolved # NIL) & resolved.CompatibleTo(to)
  732. END CompatibleTo;
  733. PROCEDURE IsPointer*(): BOOLEAN;
  734. BEGIN RETURN (resolved # NIL) & resolved.IsPointer()
  735. END IsPointer;
  736. PROCEDURE IsComposite*(): BOOLEAN;
  737. BEGIN RETURN (resolved # NIL) & resolved.IsComposite()
  738. END IsComposite;
  739. PROCEDURE Accept*(v: Visitor);
  740. VAR position: LONGINT;
  741. BEGIN position := SELF.position.start; v.VisitQualifiedType(SELF)
  742. END Accept;
  743. PROCEDURE NeedsTrace* (): BOOLEAN;
  744. BEGIN RETURN (resolved # NIL) & (resolved.NeedsTrace());
  745. END NeedsTrace;
  746. PROCEDURE IsRecordType*(): BOOLEAN;
  747. BEGIN
  748. RETURN (resolved # NIL) & (resolved.IsRecordType());
  749. END IsRecordType;
  750. END QualifiedType;
  751. (** string literal type **)
  752. StringType*= OBJECT(Type)
  753. VAR
  754. length-: LONGINT;
  755. baseType-: Type;
  756. PROCEDURE & InitStringType(position: Position; baseType: Type; length: LONGINT);
  757. BEGIN
  758. InitType(position);
  759. SetRealtime(TRUE);
  760. SELF.length := length;
  761. SELF.baseType := baseType;
  762. END InitStringType;
  763. PROCEDURE SetLength*(length: LONGINT);
  764. BEGIN SELF.length := length
  765. END SetLength;
  766. PROCEDURE SameType*(this: Type): BOOLEAN;
  767. BEGIN RETURN (this IS StringType) & (this(StringType).length = length)
  768. END SameType;
  769. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  770. BEGIN
  771. IF to IS ArrayType THEN
  772. WITH to: ArrayType DO
  773. RETURN to.arrayBase.SameType(baseType.resolved) & ((to.form = Open) OR (to.staticLength >= length))
  774. END;
  775. ELSIF to IS CharacterType THEN
  776. RETURN (length=2) & baseType.CompatibleTo(to)
  777. ELSE RETURN FALSE
  778. END;
  779. END CompatibleTo;
  780. PROCEDURE IsComposite*(): BOOLEAN;
  781. BEGIN RETURN TRUE
  782. END IsComposite;
  783. PROCEDURE Accept*(v: Visitor);
  784. VAR position: LONGINT;
  785. BEGIN position := SELF.position.start; v.VisitStringType(SELF)
  786. END Accept;
  787. END StringType;
  788. (** enumeration type of the form <<enum (base) red,green,blue end>> **)
  789. EnumerationType*=OBJECT(Type)
  790. VAR
  791. enumerationScope-: EnumerationScope;
  792. enumerationBase-: Type;
  793. rangeLowest-,rangeHighest-: Basic.Integer;
  794. PROCEDURE &InitEnumerationType(position: Position; scope: Scope; enumerationScope: EnumerationScope);
  795. BEGIN
  796. InitType(position);
  797. SetRealtime(TRUE);
  798. SELF.scope := scope;
  799. enumerationBase := NIL;
  800. rangeLowest := 0; rangeHighest := 0;
  801. SELF.enumerationScope := enumerationScope;
  802. enumerationScope.ownerEnumeration := SELF;
  803. END InitEnumerationType;
  804. PROCEDURE SetEnumerationBase*(base: Type);
  805. BEGIN enumerationBase := base
  806. END SetEnumerationBase;
  807. PROCEDURE SetRange*(lowest,highest: Basic.Integer);
  808. BEGIN rangeLowest := lowest; rangeHighest := highest;
  809. END SetRange;
  810. PROCEDURE Extends*(this: EnumerationType): BOOLEAN;
  811. BEGIN RETURN (SELF = this) OR (enumerationBase # NIL) & (enumerationBase.resolved(EnumerationType).Extends(this));
  812. END Extends;
  813. PROCEDURE SameType*(this: Type): BOOLEAN;
  814. BEGIN RETURN this = SELF
  815. END SameType;
  816. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  817. BEGIN RETURN (to IS EnumerationType) & (to(EnumerationType).Extends(SELF))
  818. END CompatibleTo;
  819. PROCEDURE Accept*(v: Visitor);
  820. VAR position: LONGINT;
  821. BEGIN position := SELF.position.start; v.VisitEnumerationType(SELF)
  822. END Accept;
  823. END EnumerationType;
  824. (** <<ARRAY [length] OF baseType>> *)
  825. ArrayType* = OBJECT (Type)
  826. VAR
  827. arrayBase-: Type;
  828. length-: Expression;
  829. staticLength-: LONGINT;
  830. form-: LONGINT; (* redundant: (form = Open) = (staticLength = 0) else (form = Static) *)
  831. PROCEDURE & InitArrayType(position: Position; scope: Scope; form: LONGINT);
  832. BEGIN
  833. length := NIL; arrayBase := NIL; InitType(position); staticLength := 0; SELF.form := form; SELF.scope := scope;
  834. END InitArrayType;
  835. PROCEDURE SetArrayBase*( type: Type );
  836. BEGIN
  837. arrayBase := type; IF (arrayBase # NIL) & (arrayBase.hasPointers) THEN SetHasPointers(TRUE) END;
  838. END SetArrayBase;
  839. PROCEDURE SetForm*(f: LONGINT);
  840. BEGIN
  841. form := f;
  842. END SetForm;
  843. PROCEDURE SetLength*(length: Expression);
  844. BEGIN
  845. SELF.length := length;
  846. IF (length.resolved # NIL) & (length.resolved IS IntegerValue) THEN
  847. staticLength := LONGINT (length.resolved(IntegerValue).value) (* TODO: staticLength should be of type Basic.Integer too *)
  848. END;
  849. END SetLength;
  850. PROCEDURE Child*(nr: LONGINT):Type;
  851. BEGIN
  852. IF nr = 0 THEN RETURN SELF;
  853. ELSIF nr = 1 THEN RETURN arrayBase.resolved;
  854. ELSE RETURN arrayBase.resolved(ArrayType).Child(nr-1);
  855. END;
  856. END Child;
  857. (* recursion safety for cases such as
  858. A= POINTER TO ARRAY OF B;
  859. B= POINTER TO ARRAY OF A;
  860. *)
  861. PROCEDURE SameType*(this: Type): BOOLEAN;
  862. VAR result : BOOLEAN;
  863. BEGIN
  864. result := FALSE;
  865. IF this = SELF THEN
  866. result := TRUE
  867. ELSIF recursion THEN
  868. result := TRUE;
  869. ELSIF this IS ArrayType THEN
  870. recursion := TRUE;
  871. WITH this: ArrayType DO
  872. result := (this.form = form) & (this.staticLength = staticLength) & arrayBase.SameType(this.arrayBase.resolved);
  873. END;
  874. END;
  875. recursion := FALSE;
  876. RETURN result
  877. END SameType;
  878. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  879. BEGIN
  880. RETURN (form = Static) & SameType(to)
  881. END CompatibleTo;
  882. PROCEDURE IsComposite*(): BOOLEAN;
  883. BEGIN RETURN TRUE
  884. END IsComposite;
  885. PROCEDURE NeedsTrace*(): BOOLEAN;
  886. BEGIN RETURN arrayBase.resolved.NeedsTrace ();
  887. END NeedsTrace;
  888. PROCEDURE Accept*(v: Visitor);
  889. VAR position: LONGINT;
  890. BEGIN position := SELF.position.start; v.VisitArrayType(SELF)
  891. END Accept;
  892. END ArrayType;
  893. (** <<ARRAY '[' length | '*' | '?' ']' OF baseType>> **)
  894. MathArrayType* = OBJECT (Type)
  895. VAR
  896. arrayBase-: Type;
  897. length-: Expression;
  898. staticLength-: LONGINT;
  899. staticIncrementInBits-: LONGINT;
  900. form-: LONGINT;
  901. PROCEDURE & InitMathArrayType(position: Position;scope: Scope; form: LONGINT);
  902. BEGIN
  903. length := NIL; arrayBase := NIL; InitType(position); staticLength := 0; staticIncrementInBits := 0; SetForm(form); SELF.scope := scope;
  904. END InitMathArrayType;
  905. PROCEDURE SetForm*(form: LONGINT);
  906. BEGIN
  907. SELF.form := form; IF form # Static THEN SetHasPointers(TRUE) END;
  908. END SetForm;
  909. PROCEDURE SetArrayBase*( type: Type );
  910. BEGIN
  911. arrayBase := type; IF (arrayBase # NIL) & (arrayBase.hasPointers) THEN SetHasPointers(TRUE) END;
  912. END SetArrayBase;
  913. PROCEDURE SetLength*(length: Expression);
  914. BEGIN
  915. SELF.length := length;
  916. IF (length # NIL) & (length.resolved # NIL) & (length.resolved IS IntegerValue) THEN
  917. staticLength := LONGINT (length.resolved(IntegerValue).value); (* TODO: staticLength should be of type Basic.Integer too *)
  918. (* optimization: unless the base type is a dynamic array, make this array static *)
  919. IF ~((arrayBase # NIL) & (arrayBase IS MathArrayType) & (arrayBase(MathArrayType).form # Static)) THEN
  920. form := Static;
  921. END
  922. ELSIF length = NIL THEN
  923. form := Open;
  924. END;
  925. END SetLength;
  926. PROCEDURE SetIncrement*(increment: LONGINT);
  927. BEGIN staticIncrementInBits := increment
  928. END SetIncrement;
  929. (* recursion safety for cases such as
  930. A= POINTER TO ARRAY OF B;
  931. B= POINTER TO ARRAY OF A;
  932. *)
  933. PROCEDURE SameType*(this: Type): BOOLEAN;
  934. VAR result: BOOLEAN;
  935. BEGIN
  936. result := FALSE;
  937. IF this = SELF THEN
  938. result := TRUE
  939. ELSIF recursion THEN
  940. result := TRUE;
  941. ELSIF this IS MathArrayType THEN
  942. recursion := TRUE;
  943. WITH this: MathArrayType DO
  944. result := (this.form = form) & (this.staticLength = staticLength) &
  945. ((arrayBase = NIL) & (this.arrayBase = NIL) OR (arrayBase # NIL) & (this.arrayBase # NIL) &
  946. arrayBase.SameType(this.arrayBase.resolved));
  947. END;
  948. END;
  949. recursion := FALSE;
  950. RETURN result
  951. END SameType;
  952. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  953. BEGIN
  954. HALT(200); (*! implement *)
  955. RETURN (form = Static) & SameType(to)
  956. END CompatibleTo;
  957. (** get the element type of a math array, i.e. the first type in the math array chain that is not a math array **)
  958. PROCEDURE ElementType*(): Type;
  959. VAR
  960. type: Type;
  961. BEGIN
  962. type := SELF;
  963. WHILE type IS MathArrayType DO
  964. type := type(MathArrayType).arrayBase.resolved
  965. END;
  966. RETURN type
  967. END ElementType;
  968. (** get the number of dimensions of a math array; 0 in case of tensors **)
  969. PROCEDURE Dimensionality*(): LONGINT;
  970. VAR
  971. type: Type;
  972. dim: LONGINT;
  973. BEGIN
  974. IF form = Tensor THEN
  975. dim := 0
  976. ELSE
  977. type := SELF;
  978. dim := 0;
  979. WHILE type IS MathArrayType DO
  980. ASSERT(type(MathArrayType).form # Tensor);
  981. INC(dim);
  982. type := type(MathArrayType).arrayBase.resolved
  983. END
  984. END;
  985. RETURN dim
  986. END Dimensionality;
  987. (** if the math array is of the form ARRAY [*, *, ..., *], i.e. contains no static length and is not a tensor either **)
  988. PROCEDURE IsFullyDynamic*(): BOOLEAN;
  989. VAR
  990. type: Type;
  991. result: BOOLEAN;
  992. BEGIN
  993. IF form = Tensor THEN
  994. result := FALSE;
  995. ELSE
  996. result := TRUE;
  997. type := SELF;
  998. WHILE type IS MathArrayType DO
  999. IF type(MathArrayType).form # Open THEN result := FALSE END;
  1000. type := type(MathArrayType).arrayBase.resolved
  1001. END
  1002. END;
  1003. RETURN result
  1004. END IsFullyDynamic;
  1005. PROCEDURE NeedsTrace*(): BOOLEAN;
  1006. BEGIN RETURN hasPointers OR (arrayBase # NIL) & (arrayBase.resolved.NeedsTrace());
  1007. END NeedsTrace;
  1008. PROCEDURE IsComposite*(): BOOLEAN;
  1009. BEGIN RETURN TRUE
  1010. END IsComposite;
  1011. PROCEDURE Accept*(v: Visitor);
  1012. VAR position: LONGINT;
  1013. BEGIN position := SELF.position.start; v.VisitMathArrayType(SELF)
  1014. END Accept;
  1015. END MathArrayType;
  1016. (** <<POINTER TO pointerBase>> **)
  1017. PointerType* = OBJECT (Type)
  1018. VAR
  1019. modifiers-: Modifier; (* set by the parser *)
  1020. pointerBase-: Type;
  1021. isPlain-: BOOLEAN;
  1022. isUnsafe-: BOOLEAN;
  1023. isUntraced-: BOOLEAN;
  1024. isDisposable-: BOOLEAN;
  1025. isHidden-: BOOLEAN;
  1026. PROCEDURE & InitPointerType(position: Position; scope: Scope);
  1027. BEGIN
  1028. modifiers := NIL;
  1029. pointerBase := NIL;
  1030. isPlain := FALSE;
  1031. isUnsafe := FALSE;
  1032. isDisposable := FALSE;
  1033. InitType(position);
  1034. SELF.scope := scope;
  1035. hasPointers := TRUE;
  1036. isHidden := FALSE;
  1037. isUntraced := FALSE;
  1038. END InitPointerType;
  1039. PROCEDURE SetHidden*(hidden: BOOLEAN);
  1040. BEGIN
  1041. isHidden := hidden;
  1042. END SetHidden; (** <<POINTER TO pointerBase>> **)
  1043. PROCEDURE SetModifiers*(flags: Modifier);
  1044. BEGIN modifiers := flags
  1045. END SetModifiers;
  1046. PROCEDURE SetPointerBase*( type: Type );
  1047. BEGIN
  1048. pointerBase := type;
  1049. END SetPointerBase;
  1050. PROCEDURE SetPlain*(plain: BOOLEAN);
  1051. BEGIN
  1052. isPlain := plain;
  1053. END SetPlain;
  1054. PROCEDURE SetUnsafe*(unsafe: BOOLEAN);
  1055. BEGIN
  1056. isUnsafe := unsafe;
  1057. END SetUnsafe;
  1058. PROCEDURE SetUntraced*(untraced: BOOLEAN);
  1059. BEGIN
  1060. isUntraced := untraced;
  1061. END SetUntraced;
  1062. PROCEDURE SetDisposable*(disposable: BOOLEAN);
  1063. BEGIN
  1064. isDisposable := disposable;
  1065. END SetDisposable;
  1066. PROCEDURE Extends*(this: Type): BOOLEAN;
  1067. VAR result: BOOLEAN; extension, base: Type;
  1068. BEGIN
  1069. result := FALSE;
  1070. IF ((this IS ObjectType) OR (this IS AnyType)) & (pointerBase.resolved IS RecordType) THEN result := TRUE
  1071. ELSE
  1072. extension := pointerBase.resolved;
  1073. IF this IS PointerType THEN
  1074. base := this(PointerType).pointerBase.resolved;
  1075. ELSIF this IS RecordType THEN
  1076. base := this
  1077. ELSE base := NIL
  1078. END;
  1079. IF (extension IS RecordType) & (base # NIL) THEN
  1080. result := extension(RecordType).Extends(base)
  1081. END;
  1082. END;
  1083. RETURN result
  1084. END Extends;
  1085. PROCEDURE SameType*(this: Type): BOOLEAN;
  1086. BEGIN RETURN (SELF = this) OR (this IS PointerType) & (this(PointerType).pointerBase.SameType(pointerBase.resolved) & (this(PointerType).isUnsafe = isUnsafe))
  1087. END SameType;
  1088. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1089. BEGIN RETURN SameType(to) OR ~(to IS RecordType) & SELF.Extends(to)
  1090. END CompatibleTo;
  1091. PROCEDURE IsPointer*(): BOOLEAN;
  1092. BEGIN RETURN TRUE
  1093. END IsPointer;
  1094. PROCEDURE NeedsTrace*(): BOOLEAN;
  1095. BEGIN RETURN ~isUntraced;
  1096. END NeedsTrace;
  1097. PROCEDURE Accept*(v: Visitor);
  1098. VAR position: LONGINT;
  1099. BEGIN position := SELF.position.start; v.VisitPointerType(SELF)
  1100. END Accept;
  1101. END PointerType;
  1102. (** << PORT (IN | OUT) [(size)] >>**)
  1103. PortType* = OBJECT (Type)
  1104. VAR
  1105. direction-: LONGINT;
  1106. sizeExpression-: Expression; (* generated by parser *)
  1107. sizeInBits-: LONGINT; (* computed by checker *)
  1108. cellsAreObjects-: BOOLEAN;
  1109. PROCEDURE & InitPortType(position: Position; direction: LONGINT; sizeExpression: Expression; scope: Scope);
  1110. BEGIN
  1111. InitType(position);
  1112. SELF.sizeExpression := sizeExpression;
  1113. SELF.direction := direction;
  1114. SELF.scope := scope;
  1115. cellsAreObjects := FALSE;
  1116. END InitPortType;
  1117. PROCEDURE SetSize*(size: LONGINT);
  1118. BEGIN sizeInBits := size
  1119. END SetSize;
  1120. PROCEDURE SetSizeExpression*(sizeExpression: Expression);
  1121. BEGIN SELF.sizeExpression := sizeExpression
  1122. END SetSizeExpression;
  1123. PROCEDURE SetCellsAreObjects*(b: BOOLEAN);
  1124. BEGIN
  1125. cellsAreObjects := b;
  1126. hasPointers := b;
  1127. END SetCellsAreObjects;
  1128. PROCEDURE SameType*(this: Type): BOOLEAN;
  1129. BEGIN RETURN (this IS PortType) & (this(PortType).direction = direction) & (this(PortType).sizeInBits = sizeInBits)
  1130. END SameType;
  1131. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1132. BEGIN RETURN SameType(to)
  1133. END CompatibleTo;
  1134. PROCEDURE IsPointer*(): BOOLEAN;
  1135. BEGIN RETURN cellsAreObjects;
  1136. END IsPointer;
  1137. PROCEDURE Accept*(v: Visitor);
  1138. VAR position: LONGINT;
  1139. BEGIN position := SELF.position.start; v.VisitPortType(SELF)
  1140. END Accept;
  1141. END PortType;
  1142. (** << recordType =
  1143. [POINTER TO] RECORD (baseType) .. END |
  1144. OBJECT (baseType) .. END
  1145. >> **)
  1146. RecordType* = OBJECT (Type)
  1147. VAR
  1148. recordScope-:RecordScope;
  1149. baseType-: Type;
  1150. pointerType-: PointerType; (* for support of A = POINTER TO RECORD ... END and B = POINTER TO RECORD (A) END; *)
  1151. modifiers-: Modifier;
  1152. isObject-,isProtected: BOOLEAN;
  1153. isAbstract-: BOOLEAN;
  1154. (* a math array type describing an object's array structure; NIL if the type does not exhibit an array structure *)
  1155. arrayStructure-: MathArrayType;
  1156. (* list of all operators needed to access an array-structured object type *)
  1157. arrayAccessOperators-: ArrayAccessOperators;
  1158. PROCEDURE & InitRecordType( position: Position; scope: Scope; recordScope: RecordScope);
  1159. BEGIN
  1160. InitType( position);
  1161. SELF.scope := scope;
  1162. baseType := NIL;
  1163. pointerType := NIL;
  1164. SELF.recordScope := recordScope;
  1165. ASSERT(recordScope # NIL);
  1166. ASSERT(recordScope.ownerRecord = NIL); (* cannot register twice ! *)
  1167. recordScope.ownerRecord := SELF;
  1168. isObject := FALSE; isProtected := FALSE;
  1169. arrayStructure := NIL;
  1170. modifiers := NIL;
  1171. isAbstract := FALSE;
  1172. END InitRecordType;
  1173. PROCEDURE SetAbstract*(abstract: BOOLEAN);
  1174. BEGIN
  1175. isAbstract := abstract;
  1176. END SetAbstract;
  1177. PROCEDURE SetModifiers*(flag: Modifier);
  1178. BEGIN SELF.modifiers := flag;
  1179. END SetModifiers;
  1180. PROCEDURE SetBaseType*( type: Type );
  1181. BEGIN
  1182. baseType := type; IF (baseType # NIL) & (baseType.hasPointers) THEN hasPointers := TRUE END;
  1183. END SetBaseType;
  1184. PROCEDURE SetPointerType*(pointerType: PointerType);
  1185. BEGIN SELF.pointerType := pointerType
  1186. END SetPointerType;
  1187. PROCEDURE IsObject*(isObject: BOOLEAN);
  1188. BEGIN SELF.isObject := isObject
  1189. END IsObject;
  1190. PROCEDURE IsActive*(): BOOLEAN;
  1191. VAR base: RecordType;
  1192. BEGIN
  1193. IF (recordScope.bodyProcedure # NIL) & (recordScope.bodyProcedure.procedureScope.body # NIL) & (recordScope.bodyProcedure.procedureScope.body.isActive) THEN RETURN TRUE END;
  1194. base := GetBaseRecord();
  1195. IF base # NIL THEN RETURN base.IsActive() END;
  1196. RETURN FALSE
  1197. END IsActive;
  1198. PROCEDURE IsProtected*(): BOOLEAN;
  1199. VAR base: RecordType;
  1200. BEGIN
  1201. IF isProtected THEN RETURN TRUE END;
  1202. base := GetBaseRecord();
  1203. IF base # NIL THEN RETURN base.IsProtected() END;
  1204. RETURN FALSE
  1205. END IsProtected;
  1206. PROCEDURE SetProtected*(protected: BOOLEAN);
  1207. BEGIN SELF.isProtected := protected
  1208. END SetProtected;
  1209. PROCEDURE Level*():LONGINT;
  1210. VAR type: RecordType; res: LONGINT;
  1211. BEGIN
  1212. type := SELF;
  1213. res := 0;
  1214. WHILE (type # NIL) & (type.baseType # NIL) DO
  1215. INC(res);
  1216. type := type.GetBaseRecord();
  1217. END;
  1218. RETURN res;
  1219. END Level;
  1220. PROCEDURE GetBaseRecord*():RecordType;
  1221. BEGIN
  1222. IF baseType = NIL THEN RETURN NIL; END;
  1223. IF baseType.resolved IS RecordType THEN
  1224. RETURN baseType.resolved(RecordType);
  1225. ELSIF baseType.resolved IS PointerType THEN
  1226. IF baseType.resolved(PointerType).pointerBase.resolved # NIL THEN
  1227. RETURN baseType.resolved(PointerType).pointerBase.resolved(RecordType);
  1228. END;
  1229. END;
  1230. RETURN NIL;
  1231. END GetBaseRecord;
  1232. PROCEDURE Extends*(this: Type): BOOLEAN;
  1233. VAR result: BOOLEAN; extension: Type;
  1234. BEGIN
  1235. result := FALSE;
  1236. IF this = SELF THEN result := TRUE
  1237. ELSIF this IS RecordType THEN
  1238. IF (baseType # NIL) THEN
  1239. extension := baseType.resolved;
  1240. IF extension IS PointerType THEN
  1241. result := extension(PointerType).Extends(this)
  1242. ELSIF extension IS RecordType THEN
  1243. result := extension(RecordType).Extends(this)
  1244. END;
  1245. END;
  1246. END;
  1247. RETURN result
  1248. END Extends;
  1249. PROCEDURE SameType*(this: Type): BOOLEAN;
  1250. BEGIN RETURN (this = SELF)
  1251. END SameType;
  1252. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1253. BEGIN RETURN Extends(to)
  1254. END CompatibleTo;
  1255. PROCEDURE SetArrayStructure*(arrayStructure: MathArrayType);
  1256. BEGIN SELF.arrayStructure := arrayStructure
  1257. END SetArrayStructure;
  1258. PROCEDURE SetArrayAccessOperators*(arrayAccessOperators: ArrayAccessOperators);
  1259. BEGIN SELF.arrayAccessOperators := arrayAccessOperators
  1260. END SetArrayAccessOperators;
  1261. PROCEDURE HasArrayStructure*(): BOOLEAN;
  1262. BEGIN RETURN (arrayStructure # NIL)
  1263. END HasArrayStructure
  1264. ;
  1265. PROCEDURE IsComposite*(): BOOLEAN;
  1266. BEGIN RETURN TRUE
  1267. END IsComposite;
  1268. PROCEDURE NeedsTrace*(): BOOLEAN;
  1269. BEGIN RETURN recordScope.NeedsTrace();
  1270. END NeedsTrace;
  1271. PROCEDURE Accept*(v: Visitor);
  1272. VAR position: LONGINT;
  1273. BEGIN position := SELF.position.start; v.VisitRecordType(SELF)
  1274. END Accept;
  1275. PROCEDURE IsRecordType*(): BOOLEAN;
  1276. BEGIN
  1277. RETURN TRUE;
  1278. END IsRecordType;
  1279. END RecordType;
  1280. CellType*=OBJECT (Type)
  1281. VAR
  1282. firstParameter-,lastParameter-: Parameter; numberParameters-: LONGINT; (* parameters *)
  1283. firstProperty-, lastProperty-: Property; numberProperties: LONGINT; (* capabilities *)
  1284. cellScope-: CellScope;
  1285. isCellNet-: BOOLEAN;
  1286. modifiers-: Modifier;
  1287. baseType-: Type;
  1288. PROCEDURE &InitCellType(position: Position; scope: Scope; cellScope: CellScope);
  1289. BEGIN
  1290. InitType(position);
  1291. SELF.scope := scope;
  1292. numberParameters := 0; firstParameter := NIL; lastParameter := NIL;
  1293. numberProperties := 0; firstProperty := NIL; lastProperty := NIL;
  1294. SELF.cellScope := cellScope;
  1295. isCellNet := FALSE;
  1296. baseType := NIL;
  1297. END InitCellType;
  1298. PROCEDURE SetBaseType*(base: Type);
  1299. BEGIN
  1300. baseType := base;
  1301. END SetBaseType;
  1302. PROCEDURE GetBaseValueType*(): Type;
  1303. BEGIN
  1304. IF baseType = NIL THEN
  1305. RETURN NIL
  1306. ELSIF baseType.resolved IS PointerType THEN
  1307. RETURN baseType.resolved(PointerType).pointerBase.resolved
  1308. ELSE
  1309. RETURN baseType.resolved;
  1310. END;
  1311. END GetBaseValueType;
  1312. PROCEDURE GetBaseRecord*():RecordType;
  1313. BEGIN
  1314. IF baseType = NIL THEN RETURN NIL; END;
  1315. IF baseType.resolved IS CellType THEN
  1316. RETURN baseType.resolved(CellType).GetBaseRecord();
  1317. ELSIF baseType.resolved IS RecordType THEN
  1318. RETURN baseType.resolved(RecordType);
  1319. ELSIF baseType.resolved IS PointerType THEN
  1320. IF baseType.resolved(PointerType).pointerBase.resolved # NIL THEN
  1321. RETURN baseType.resolved(PointerType).pointerBase.resolved(RecordType);
  1322. END;
  1323. END;
  1324. RETURN NIL;
  1325. END GetBaseRecord;
  1326. PROCEDURE AddParameter*(p: Parameter);
  1327. BEGIN
  1328. ASSERT(p # NIL);
  1329. IF lastParameter= NIL THEN firstParameter := p ELSE lastParameter.nextParameter := p; p.prevParameter := lastParameter; END;
  1330. lastParameter := p;
  1331. INC(numberParameters);
  1332. END AddParameter;
  1333. PROCEDURE AddProperty*(p: Property);
  1334. BEGIN
  1335. ASSERT(p # NIL);
  1336. IF lastProperty= NIL THEN firstProperty := p ELSE lastProperty.nextProperty := p; p.prevProperty := lastProperty; END;
  1337. lastProperty := p;
  1338. INC(numberProperties);
  1339. END AddProperty;
  1340. PROCEDURE FindParameter*(identifier: Identifier): Parameter;
  1341. VAR p: Parameter;
  1342. BEGIN
  1343. p := NIL;
  1344. IF (baseType # NIL) & (baseType.resolved IS CellType) THEN
  1345. p := baseType.resolved(CellType).FindParameter(identifier);
  1346. END;
  1347. IF p = NIL THEN
  1348. p := firstParameter;
  1349. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextParameter END;
  1350. END;
  1351. RETURN p;
  1352. END FindParameter;
  1353. PROCEDURE FindProperty*(identifier: Identifier): Property;
  1354. VAR p: Property;
  1355. BEGIN
  1356. p := firstProperty;
  1357. WHILE(p#NIL) & (p.name # identifier) DO p := p.nextProperty END;
  1358. IF p = NIL THEN
  1359. IF (baseType # NIL) & (baseType.resolved IS CellType) THEN
  1360. p := baseType.resolved(CellType).FindProperty(identifier);
  1361. END;
  1362. END;
  1363. RETURN p;
  1364. END FindProperty;
  1365. PROCEDURE SetModifiers*(flag: Modifier);
  1366. BEGIN SELF.modifiers := flag;
  1367. END SetModifiers;
  1368. PROCEDURE IsCellNet*(t: BOOLEAN);
  1369. BEGIN isCellNet := t
  1370. END IsCellNet;
  1371. PROCEDURE SameType*(this: Type): BOOLEAN;
  1372. BEGIN RETURN this = SELF
  1373. END SameType;
  1374. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1375. BEGIN RETURN SameType(to)
  1376. END CompatibleTo;
  1377. PROCEDURE IsComposite*(): BOOLEAN;
  1378. BEGIN RETURN TRUE
  1379. END IsComposite;
  1380. PROCEDURE Accept*(v: Visitor);
  1381. VAR position: LONGINT;
  1382. BEGIN position := SELF.position.start; v.VisitCellType(SELF)
  1383. END Accept;
  1384. END CellType;
  1385. (** <<procedureType = PROCEDURE [{DELEGATE}] (firstParameter .. lastParameter): returnType>>
  1386. also used as type for procedures
  1387. **)
  1388. ProcedureType* = OBJECT (Type)
  1389. VAR
  1390. modifiers-: Modifier; (* set by the parser *)
  1391. returnType-: Type;
  1392. returnTypeModifiers-: Modifier;
  1393. hasUntracedReturn-: BOOLEAN;
  1394. firstParameter-,lastParameter-: Parameter; numberParameters-: LONGINT; (* parameters *)
  1395. returnParameter-: Parameter; (* not really necessary in syntax tree but very handy for backends *)
  1396. selfParameter-: Parameter;
  1397. isDelegate-,isInterrupt-,noPAF-,noReturn-: BOOLEAN;
  1398. pcOffset-: LONGINT; (* PC offset: used for ARM interrupt procedures *)
  1399. callingConvention-: CallingConvention;
  1400. stackAlignment-: LONGINT;
  1401. parametersOffset-: LONGINT; (* stack parameter offset -- in units of addresses: one pointer = 1 *)
  1402. PROCEDURE & InitProcedureType( position: Position; scope: Scope);
  1403. BEGIN
  1404. InitType( position);
  1405. SELF.scope := scope;
  1406. modifiers := NIL;
  1407. firstParameter := NIL; lastParameter := NIL; numberParameters := 0; returnParameter := NIL;
  1408. returnType := NIL;
  1409. stackAlignment := 1;
  1410. isDelegate := FALSE; isInterrupt := FALSE; noPAF := FALSE;
  1411. callingConvention := OberonCallingConvention;
  1412. parametersOffset := 0;
  1413. pcOffset := 0;
  1414. hasUntracedReturn := FALSE;
  1415. returnTypeModifiers := NIL;
  1416. selfParameter := NIL;
  1417. END InitProcedureType;
  1418. PROCEDURE SetNoPAF*(noPAF: BOOLEAN);
  1419. BEGIN SELF.noPAF := noPAF
  1420. END SetNoPAF;
  1421. PROCEDURE SetNoReturn*(noReturn: BOOLEAN);
  1422. BEGIN SELF.noReturn := noReturn
  1423. END SetNoReturn;
  1424. PROCEDURE SetPcOffset*(pcOffset: LONGINT);
  1425. BEGIN SELF.pcOffset := pcOffset
  1426. END SetPcOffset;
  1427. PROCEDURE SetInterrupt*(isInterrupt: BOOLEAN);
  1428. BEGIN SELF.isInterrupt := isInterrupt
  1429. END SetInterrupt;
  1430. PROCEDURE SetModifiers*(flags: Modifier);
  1431. BEGIN modifiers := flags
  1432. END SetModifiers;
  1433. PROCEDURE SetReturnTypeModifiers*(flags: Modifier);
  1434. BEGIN returnTypeModifiers := flags
  1435. END SetReturnTypeModifiers;
  1436. PROCEDURE SetDelegate*(delegate: BOOLEAN);
  1437. BEGIN SELF.isDelegate := delegate; SELF.hasPointers := delegate;
  1438. END SetDelegate;
  1439. PROCEDURE SetUntracedReturn*(untraced: BOOLEAN);
  1440. BEGIN
  1441. hasUntracedReturn := untraced;
  1442. END SetUntracedReturn;
  1443. PROCEDURE SetStackAlignment*(alignment: LONGINT);
  1444. BEGIN
  1445. stackAlignment := alignment;
  1446. END SetStackAlignment;
  1447. PROCEDURE SetParametersOffset*(ofs: LONGINT);
  1448. BEGIN parametersOffset := ofs
  1449. END SetParametersOffset;
  1450. PROCEDURE SetReturnParameter*(parameter: Parameter);
  1451. BEGIN returnParameter := parameter
  1452. END SetReturnParameter;
  1453. PROCEDURE SetSelfParameter*(parameter: Parameter);
  1454. BEGIN selfParameter := parameter
  1455. END SetSelfParameter;
  1456. PROCEDURE SetCallingConvention*(cc: CallingConvention);
  1457. BEGIN callingConvention := cc
  1458. END SetCallingConvention;
  1459. PROCEDURE AddParameter*(p: Parameter);
  1460. BEGIN
  1461. ASSERT(p # NIL);
  1462. IF lastParameter= NIL THEN firstParameter := p ELSE lastParameter.nextParameter := p; p.prevParameter := lastParameter; END;
  1463. lastParameter := p;
  1464. INC(numberParameters);
  1465. ASSERT(p.access # {}); (* no hidden parameters ! *)
  1466. END AddParameter;
  1467. PROCEDURE RevertParameters*;
  1468. VAR this,next: Parameter; pnum: LONGINT;
  1469. BEGIN
  1470. pnum := numberParameters;
  1471. IF lastParameter # NIL THEN
  1472. this := lastParameter;
  1473. lastParameter := NIL;
  1474. firstParameter := NIL;
  1475. numberParameters := 0;
  1476. WHILE this # NIL DO
  1477. next := this.prevParameter;
  1478. this.prevParameter := NIL; this.nextParameter := NIL;
  1479. AddParameter(this);
  1480. this := next;
  1481. END;
  1482. END;
  1483. ASSERT(pnum = numberParameters);
  1484. END RevertParameters;
  1485. PROCEDURE SetReturnType*( type: Type );
  1486. BEGIN
  1487. returnType := type;
  1488. END SetReturnType;
  1489. PROCEDURE SameSignature*(this: Type): BOOLEAN;
  1490. VAR result: BOOLEAN; p1,p2: Parameter;
  1491. BEGIN
  1492. result := FALSE;
  1493. IF recursion THEN
  1494. result := TRUE
  1495. ELSIF this = SELF THEN
  1496. result := TRUE
  1497. ELSIF this IS ProcedureType THEN
  1498. recursion := TRUE;
  1499. WITH this: ProcedureType DO
  1500. result := (returnType = NIL) & (this.returnType = NIL) OR (returnType # NIL) & (this.returnType # NIL) & returnType.SameType(this.returnType.resolved);
  1501. result := result & (callingConvention = this.callingConvention);
  1502. result := result & (noReturn = this.noReturn);
  1503. result := result & (isInterrupt = this.isInterrupt);
  1504. IF result THEN
  1505. p1 := selfParameter; p2 := this.selfParameter;
  1506. IF (p1 = NIL) # (p2=NIL) OR (p1 # NIL) & ((p1.kind # p2.kind)) THEN
  1507. RETURN FALSE
  1508. END;
  1509. p1 := firstParameter; p2 := this.firstParameter;
  1510. 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
  1511. p1 := p1.nextParameter; p2 := p2.nextParameter
  1512. END;
  1513. result := ((p1=NIL) OR (p1.access = Hidden)) & ((p2=NIL) OR (p2.access= Hidden));
  1514. END;
  1515. END;
  1516. END;
  1517. recursion := FALSE;
  1518. RETURN result
  1519. END SameSignature;
  1520. PROCEDURE SameType*(this: Type): BOOLEAN;
  1521. BEGIN
  1522. RETURN SameSignature(this)
  1523. & (this(ProcedureType).isDelegate = isDelegate)
  1524. & (this(ProcedureType).isRealtime = isRealtime);
  1525. END SameType;
  1526. PROCEDURE CompatibleTo*(to: Type): BOOLEAN;
  1527. BEGIN
  1528. RETURN SameSignature(to) & (~isDelegate OR to(ProcedureType).isDelegate) & (~to.isRealtime OR isRealtime)
  1529. & ((stackAlignment <=1) OR (stackAlignment <= to(ProcedureType).stackAlignment));
  1530. END CompatibleTo;
  1531. PROCEDURE IsComposite*(): BOOLEAN;
  1532. BEGIN RETURN isDelegate
  1533. END IsComposite;
  1534. PROCEDURE Accept*(v: Visitor);
  1535. VAR position: LONGINT;
  1536. BEGIN position := SELF.position.start; v.VisitProcedureType(SELF)
  1537. END Accept;
  1538. (** Returns if the type needs to be traced for garbage collection *)
  1539. PROCEDURE NeedsTrace*(): BOOLEAN;
  1540. BEGIN RETURN isDelegate;
  1541. END NeedsTrace;
  1542. END ProcedureType;
  1543. (**** expressions ****)
  1544. Expression* = OBJECT
  1545. VAR
  1546. type-: Type; (* the expression's type. Resolved by checker *)
  1547. assignable-: BOOLEAN; (* expression can be assigned to (or used as var-parameter): expression := ... *)
  1548. position-, end-: Position;
  1549. state-: SET;
  1550. resolved-: Value;
  1551. isHidden-: BOOLEAN;
  1552. PROCEDURE End*( position: Position);
  1553. BEGIN SELF.end := position;
  1554. END End;
  1555. PROCEDURE SetState*(state: LONGINT);
  1556. BEGIN INCL(SELF.state,state);
  1557. END SetState;
  1558. PROCEDURE &InitExpression(position: Position);
  1559. BEGIN SELF.position := position; end := invalidPosition; state := Undefined; type := NIL; assignable := FALSE; resolved := NIL; isHidden := FALSE;
  1560. END InitExpression;
  1561. PROCEDURE SetHidden*(hidden: BOOLEAN);
  1562. BEGIN isHidden := hidden
  1563. END SetHidden;
  1564. PROCEDURE SetType*(type: Type);
  1565. BEGIN
  1566. SELF.type := type;
  1567. END SetType;
  1568. PROCEDURE SetResolved*(value: Value);
  1569. BEGIN SELF.resolved := value
  1570. END SetResolved;
  1571. PROCEDURE SetAssignable*(assignable: BOOLEAN);
  1572. BEGIN SELF.assignable := assignable
  1573. END SetAssignable;
  1574. PROCEDURE Clone(): Expression;
  1575. VAR clone: Expression;
  1576. BEGIN
  1577. (* support cloning here for more robust error reporting -- should not happen normally *)
  1578. NEW(clone, position); RETURN clone
  1579. END Clone;
  1580. PROCEDURE Accept*(v: Visitor);
  1581. VAR position: LONGINT;
  1582. BEGIN position := SELF.position.start; v.VisitExpression(SELF)
  1583. END Accept;
  1584. PROCEDURE NeedsTrace* (): BOOLEAN;
  1585. BEGIN RETURN FALSE;
  1586. END NeedsTrace;
  1587. END Expression;
  1588. (** <<expression, expression, ...>> **)
  1589. ExpressionList* = OBJECT
  1590. VAR list: Basic.List;
  1591. PROCEDURE & InitList;
  1592. BEGIN NEW( list,8 );
  1593. END InitList;
  1594. PROCEDURE Length*( ): LONGINT;
  1595. BEGIN RETURN list.Length();
  1596. END Length;
  1597. PROCEDURE AddExpression*( d: Expression );
  1598. BEGIN list.Add(d)
  1599. END AddExpression;
  1600. PROCEDURE GetExpression*( index: LONGINT ): Expression;
  1601. VAR p: ANY;
  1602. BEGIN
  1603. p := list.Get(index); RETURN p(Expression);
  1604. END GetExpression;
  1605. PROCEDURE SetExpression*(index: LONGINT; expression: Expression);
  1606. BEGIN list.Set(index,expression)
  1607. END SetExpression;
  1608. PROCEDURE RemoveExpression*(i: LONGINT);
  1609. BEGIN list.RemoveByIndex(i);
  1610. END RemoveExpression;
  1611. PROCEDURE Revert*;
  1612. VAR i,j,last: LONGINT; ei,ej: ANY;
  1613. BEGIN
  1614. last := Length()-1;
  1615. FOR i := 0 TO last DO
  1616. j := last-i;
  1617. ei := list.Get(i);
  1618. ej := list.Get(j);
  1619. list.Set(i,ej);
  1620. list.Set(j,ei);
  1621. END;
  1622. END Revert;
  1623. PROCEDURE Clone*(VAR list: ExpressionList);
  1624. VAR i: LONGINT;
  1625. BEGIN
  1626. IF list = NIL THEN NEW(list) END;
  1627. FOR i := 0 TO Length()-1 DO
  1628. list.AddExpression(CloneExpression(GetExpression(i)));
  1629. END;
  1630. END Clone;
  1631. END ExpressionList;
  1632. (** << {elements} >> **)
  1633. Set* = OBJECT (Expression)
  1634. VAR elements-: ExpressionList; (* an element of the form from .. to is represented as a RangeExpression *)
  1635. PROCEDURE & InitSet( position: Position );
  1636. BEGIN
  1637. InitExpression( position );
  1638. elements := NewExpressionList();
  1639. END InitSet;
  1640. PROCEDURE Clone(): Expression;
  1641. VAR copy: Set;
  1642. BEGIN
  1643. NEW(copy, position); elements.Clone(copy.elements); RETURN copy
  1644. END Clone;
  1645. PROCEDURE Accept*(v: Visitor);
  1646. VAR position: LONGINT;
  1647. BEGIN position := SELF.position.start; v.VisitSet(SELF)
  1648. END Accept;
  1649. END Set;
  1650. (** << [elements] >> **)
  1651. MathArrayExpression* = OBJECT (Expression)
  1652. VAR elements-: ExpressionList; (* an element of the form from .. to is represented as a RangeExpression *)
  1653. PROCEDURE & InitMathArrayExpression( position: Position );
  1654. BEGIN
  1655. InitExpression( position );
  1656. elements := NewExpressionList();
  1657. END InitMathArrayExpression;
  1658. PROCEDURE Clone(): Expression;
  1659. VAR copy: MathArrayExpression;
  1660. BEGIN
  1661. NEW(copy, position); elements.Clone(copy.elements); RETURN copy
  1662. END Clone;
  1663. PROCEDURE Accept*(v: Visitor);
  1664. VAR position: LONGINT;
  1665. BEGIN position := SELF.position.start; v.VisitMathArrayExpression(SELF)
  1666. END Accept;
  1667. END MathArrayExpression;
  1668. (** <<operator left>> **)
  1669. UnaryExpression* = OBJECT (Expression)
  1670. VAR
  1671. left-: Expression;
  1672. operator-: LONGINT; (* one of Scanner.Minus ... Scanner.Not *)
  1673. PROCEDURE & InitUnaryExpression( position: Position; operand: Expression; operator: LONGINT );
  1674. BEGIN
  1675. InitExpression( position ); SELF.left := operand; SELF.operator := operator;
  1676. END InitUnaryExpression;
  1677. PROCEDURE SetLeft*(left: Expression);
  1678. BEGIN SELF.left := left
  1679. END SetLeft;
  1680. PROCEDURE Clone(): Expression;
  1681. VAR copy: UnaryExpression;
  1682. BEGIN
  1683. NEW(copy, position, CloneExpression(left), operator); RETURN copy
  1684. END Clone;
  1685. PROCEDURE Accept*(v: Visitor);
  1686. VAR position: LONGINT;
  1687. BEGIN position := SELF.position.start; v.VisitUnaryExpression(SELF)
  1688. END Accept;
  1689. END UnaryExpression;
  1690. (** <<left operator right>> **)
  1691. BinaryExpression* = OBJECT (Expression)
  1692. VAR
  1693. left-, right-: Expression;
  1694. operator-: LONGINT; (* one of Scanner.Equal ... Scanner.Minus *)
  1695. PROCEDURE & InitBinaryExpression( position: Position; left, right: Expression; operator: LONGINT );
  1696. BEGIN
  1697. InitExpression( position ); SELF.left := left; SELF.right := right; SELF.operator := operator;
  1698. END InitBinaryExpression;
  1699. PROCEDURE SetLeft*(left: Expression);
  1700. BEGIN SELF.left := left
  1701. END SetLeft;
  1702. PROCEDURE SetRight*(right: Expression);
  1703. BEGIN SELF.right := right
  1704. END SetRight;
  1705. PROCEDURE Clone(): Expression;
  1706. VAR copy: BinaryExpression;
  1707. BEGIN
  1708. NEW(copy, position, CloneExpression(left), CloneExpression(right), operator); RETURN copy
  1709. END Clone;
  1710. PROCEDURE Accept*(v: Visitor);
  1711. VAR position: LONGINT;
  1712. BEGIN position := SELF.position.start; v.VisitBinaryExpression(SELF)
  1713. END Accept;
  1714. END BinaryExpression;
  1715. (** expression that denotes a range
  1716. <<[first] '..' [last] ['by' step] | '*' >>
  1717. **)
  1718. RangeExpression* = OBJECT (Expression)
  1719. VAR
  1720. first-, last-, step-: Expression;
  1721. missingFirst-, missingLast-, missingStep-: BOOLEAN; (* only for printout*)
  1722. context-: SHORTINT; (* one of ArrayIndex, SetElement or CaseGuard *)
  1723. PROCEDURE &InitRangeExpression(position: Position; first, last, step: Expression);
  1724. BEGIN
  1725. context := ArrayIndex; (* by default, a range represents array indices *)
  1726. InitExpression(position);
  1727. missingFirst := (first = NIL);
  1728. missingLast := (last = NIL);
  1729. missingStep := (step = NIL);
  1730. SELF.first := first;
  1731. SELF.last := last;
  1732. SELF.step := step;
  1733. END InitRangeExpression;
  1734. PROCEDURE SetFirst*(first: Expression);
  1735. BEGIN
  1736. SELF.first := first
  1737. END SetFirst;
  1738. PROCEDURE SetLast*(last: Expression);
  1739. BEGIN SELF.last := last
  1740. END SetLast;
  1741. PROCEDURE SetStep*(step: Expression);
  1742. BEGIN SELF.step := step
  1743. END SetStep;
  1744. PROCEDURE SetContext*(context: SHORTINT);
  1745. BEGIN
  1746. SELF.context := context
  1747. END SetContext;
  1748. PROCEDURE Clone(): Expression;
  1749. VAR copy: RangeExpression;
  1750. BEGIN
  1751. NEW(copy, position, CloneExpression(first), CloneExpression(last), CloneExpression(step)); RETURN copy
  1752. END Clone;
  1753. PROCEDURE Accept*(v: Visitor);
  1754. VAR position: LONGINT;
  1755. BEGIN position := SELF.position.start; v.VisitRangeExpression(SELF)
  1756. END Accept;
  1757. END RangeExpression;
  1758. (** << ? >> **)
  1759. TensorRangeExpression*=OBJECT (Expression);
  1760. PROCEDURE &InitTensorRangeExpression(position: Position);
  1761. BEGIN
  1762. InitExpression(position);
  1763. END InitTensorRangeExpression;
  1764. PROCEDURE Clone(): Expression;
  1765. VAR copy: TensorRangeExpression;
  1766. BEGIN
  1767. NEW(copy, position); RETURN copy
  1768. END Clone;
  1769. PROCEDURE Accept*(v: Visitor);
  1770. VAR position: LONGINT;
  1771. BEGIN position := SELF.position.start; v.VisitTensorRangeExpression(SELF)
  1772. END Accept;
  1773. END TensorRangeExpression;
  1774. (** convert expression from expression.type to Conversion.type **)
  1775. Conversion* = OBJECT (Expression)
  1776. VAR
  1777. expression-: Expression;
  1778. typeExpression-: Expression; (* for printout *)
  1779. PROCEDURE & InitConversion( position: Position; expression: Expression; type: Type; typeExpression: Expression);
  1780. BEGIN
  1781. InitExpression( position ); SELF.expression := expression; SELF.typeExpression := typeExpression; SELF.type := type;
  1782. END InitConversion;
  1783. PROCEDURE SetExpression*(expression: Expression);
  1784. BEGIN SELF.expression := expression
  1785. END SetExpression;
  1786. PROCEDURE Clone(): Expression;
  1787. VAR copy: Conversion;
  1788. BEGIN
  1789. NEW(copy, position, CloneExpression(expression), type, CloneExpression(typeExpression)); RETURN copy
  1790. END Clone;
  1791. PROCEDURE Accept*(v: Visitor);
  1792. VAR position: LONGINT;
  1793. BEGIN position := SELF.position.start; v.VisitConversion(SELF)
  1794. END Accept;
  1795. END Conversion;
  1796. (**** designators ****)
  1797. (** abstract **)
  1798. Designator* = OBJECT(Expression)
  1799. VAR
  1800. left-: Expression; (* currently only designators are allowed but for later purposes ... (as for example (a+b).c) *)
  1801. relatedRhs-: Expression; (* the RHS of an assignment if this designator is used on the LHS: *)
  1802. (* used if this designator contains an index operator call, e.g. 'asot.^"[]"(indexList)': *)
  1803. relatedAsot-: Expression; (* the ASOT on which the index operator is called *)
  1804. relatedIndexList-: ExpressionList; (* the index list *)
  1805. modifiers-: Modifier;
  1806. PROCEDURE &InitDesignator*(position: Position);
  1807. BEGIN
  1808. InitExpression(position);
  1809. left := NIL;
  1810. relatedRhs := NIL;
  1811. relatedAsot := NIL;
  1812. relatedIndexList := NIL;
  1813. modifiers := NIL;
  1814. END InitDesignator;
  1815. PROCEDURE SetLeft*(expression: Expression);
  1816. BEGIN left := expression
  1817. END SetLeft;
  1818. PROCEDURE SetRelatedRhs*(relatedRhs: Expression);
  1819. BEGIN SELF.relatedRhs := relatedRhs
  1820. END SetRelatedRhs;
  1821. PROCEDURE SetRelatedAsot*(relatedAsot: Expression);
  1822. BEGIN SELF.relatedAsot := relatedAsot
  1823. END SetRelatedAsot;
  1824. PROCEDURE SetRelatedIndexList*(relatedIndexList: ExpressionList);
  1825. BEGIN SELF.relatedIndexList := relatedIndexList
  1826. END SetRelatedIndexList;
  1827. PROCEDURE SetModifiers*(flags: Modifier);
  1828. BEGIN modifiers := flags
  1829. END SetModifiers;
  1830. PROCEDURE Clone(): Expression;
  1831. VAR clone: Designator;
  1832. BEGIN
  1833. (* support cloning here for more robust error reporting -- should not happen normally *)
  1834. NEW(clone, position); RETURN clone
  1835. END Clone;
  1836. PROCEDURE Accept*(v: Visitor);
  1837. VAR position: LONGINT;
  1838. BEGIN position := SELF.position.start; v.VisitDesignator(SELF)
  1839. END Accept;
  1840. END Designator;
  1841. (*** first phase (parse time) designators ***)
  1842. (** <<identifier>>
  1843. may designate any symbol such as Variable, TypeDeclaration, Procedure
  1844. **)
  1845. IdentifierDesignator* = OBJECT(Designator)
  1846. VAR identifier-: Identifier;
  1847. PROCEDURE &InitIdentifierDesignator(position: Position; id: Identifier);
  1848. BEGIN InitDesignator(position); identifier := id
  1849. END InitIdentifierDesignator;
  1850. PROCEDURE Clone(): Expression;
  1851. VAR copy: IdentifierDesignator;
  1852. BEGIN
  1853. NEW(copy, position, identifier); RETURN copy
  1854. END Clone;
  1855. PROCEDURE Accept*(v: Visitor);
  1856. VAR position: LONGINT;
  1857. BEGIN position := SELF.position.start; v.VisitIdentifierDesignator(SELF)
  1858. END Accept;
  1859. END IdentifierDesignator;
  1860. (** <<left.identifier>>
  1861. may designate a record / module element (constant, type, variable, procedure)
  1862. **)
  1863. SelectorDesignator* = OBJECT (Designator)
  1864. VAR identifier-: Identifier;
  1865. PROCEDURE & InitSelector(position: Position; left: Designator; identifier: Identifier);
  1866. BEGIN InitDesignator(position); SELF.left := left; SELF.identifier := identifier;
  1867. END InitSelector;
  1868. PROCEDURE Clone(): Expression;
  1869. VAR copy: SelectorDesignator;
  1870. BEGIN
  1871. NEW(copy, position, CloneDesignator(left), identifier); RETURN copy
  1872. END Clone;
  1873. PROCEDURE Accept*(v: Visitor);
  1874. VAR position: LONGINT;
  1875. BEGIN position := SELF.position.start; v.VisitSelectorDesignator(SELF)
  1876. END Accept;
  1877. END SelectorDesignator;
  1878. (** <<left(arg1, arg2, ...)>>
  1879. may designate a function call or a type guard
  1880. **)
  1881. ParameterDesignator* = OBJECT(Designator)
  1882. VAR
  1883. parameters-: ExpressionList;
  1884. PROCEDURE &InitParameterDesignator(position: Position; left: Designator; parameters: ExpressionList);
  1885. BEGIN InitDesignator(position); SELF.left := left; SELF.parameters := parameters
  1886. END InitParameterDesignator;
  1887. PROCEDURE Clone(): Expression;
  1888. VAR copy: ParameterDesignator;
  1889. BEGIN
  1890. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  1891. END Clone;
  1892. PROCEDURE Accept*(v: Visitor);
  1893. VAR position: LONGINT;
  1894. BEGIN position := SELF.position.start; v.VisitParameterDesignator(SELF)
  1895. END Accept;
  1896. END ParameterDesignator;
  1897. (** <<left^>>
  1898. may designate a pointer dereference or a method supercall
  1899. **)
  1900. ArrowDesignator* = OBJECT (Designator)
  1901. PROCEDURE &InitArrowDesignator(position: Position; left: Designator);
  1902. BEGIN InitDesignator(position); SELF.left := left;
  1903. END InitArrowDesignator;
  1904. PROCEDURE Clone(): Expression;
  1905. VAR copy: ArrowDesignator;
  1906. BEGIN
  1907. NEW(copy, position, CloneDesignator(left(Designator))); RETURN copy
  1908. END Clone;
  1909. PROCEDURE Accept*(v: Visitor);
  1910. VAR position: LONGINT;
  1911. BEGIN position := SELF.position.start; v.VisitArrowDesignator(SELF)
  1912. END Accept;
  1913. END ArrowDesignator;
  1914. (** <<left[parameters]>>
  1915. designates an index designator, before checker
  1916. **)
  1917. BracketDesignator* = OBJECT(Designator)
  1918. VAR parameters-: ExpressionList;
  1919. PROCEDURE &InitBracketDesignator(position: Position; left: Designator; parameters: ExpressionList);
  1920. BEGIN InitDesignator(position); SELF.left := left; SELF.parameters := parameters;
  1921. END InitBracketDesignator;
  1922. PROCEDURE Clone(): Expression;
  1923. VAR copy: BracketDesignator;
  1924. BEGIN
  1925. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  1926. END Clone;
  1927. PROCEDURE Accept*(v: Visitor);
  1928. VAR position: LONGINT;
  1929. BEGIN position := SELF.position.start; v.VisitBracketDesignator(SELF)
  1930. END Accept;
  1931. END BracketDesignator;
  1932. (*** second phase (after checker) designators ***)
  1933. (** symbol designator emerged from IdentifierDesignator or from Selector **)
  1934. SymbolDesignator* = OBJECT(Designator)
  1935. VAR
  1936. symbol-: Symbol;
  1937. PROCEDURE &InitSymbolDesignator(position: Position; left: Designator; symbol: Symbol);
  1938. BEGIN
  1939. InitDesignator(position);
  1940. SELF.left := left;
  1941. SELF.symbol := symbol;
  1942. END InitSymbolDesignator;
  1943. PROCEDURE Accept*(v: Visitor);
  1944. VAR position: LONGINT;
  1945. BEGIN
  1946. position := SELF.position.start; v.VisitSymbolDesignator(SELF);
  1947. END Accept;
  1948. PROCEDURE Clone(): Expression;
  1949. VAR copy: SymbolDesignator;
  1950. BEGIN
  1951. NEW(copy, position, CloneDesignator(left), symbol); RETURN copy
  1952. END Clone;
  1953. PROCEDURE SetSymbol*(s: Symbol);
  1954. BEGIN SELF.symbol := s;
  1955. END SetSymbol;
  1956. PROCEDURE NeedsTrace* (): BOOLEAN;
  1957. BEGIN
  1958. RETURN symbol.NeedsTrace() & ((left = NIL) OR (left.NeedsTrace()));
  1959. END NeedsTrace;
  1960. END SymbolDesignator;
  1961. (** <<left[parameters]>>
  1962. (ranged) indexer
  1963. **)
  1964. IndexDesignator* = OBJECT(Designator)
  1965. VAR
  1966. parameters-: ExpressionList;
  1967. hasRange-: BOOLEAN;
  1968. hasTensorRange-: BOOLEAN;
  1969. PROCEDURE &InitIndexDesignator(position: Position; left: Designator);
  1970. BEGIN
  1971. InitDesignator(position);
  1972. SELF.left := left;
  1973. parameters := NewExpressionList();
  1974. hasRange := FALSE;
  1975. hasTensorRange := FALSE;
  1976. END InitIndexDesignator;
  1977. PROCEDURE HasRange*;
  1978. BEGIN hasRange := TRUE;
  1979. END HasRange;
  1980. PROCEDURE HasTensorRange*;
  1981. BEGIN hasTensorRange := TRUE;
  1982. END HasTensorRange;
  1983. PROCEDURE Clone(): Expression;
  1984. VAR copy: IndexDesignator;
  1985. BEGIN
  1986. NEW(copy, position, CloneDesignator(left));
  1987. parameters.Clone(copy.parameters);
  1988. copy.hasRange := hasRange; copy.hasTensorRange := hasTensorRange ; RETURN copy
  1989. END Clone;
  1990. PROCEDURE Accept*(v: Visitor);
  1991. VAR position: LONGINT;
  1992. BEGIN position := SELF.position.start; v.VisitIndexDesignator(SELF)
  1993. END Accept;
  1994. PROCEDURE NeedsTrace* (): BOOLEAN;
  1995. BEGIN RETURN type.NeedsTrace() & left.NeedsTrace(); (* for x[y]: if x is untraced, then also x[y] should be treated untraced *)
  1996. END NeedsTrace;
  1997. END IndexDesignator;
  1998. StatementDesignator* = OBJECT (Designator)
  1999. VAR
  2000. statement-: Statement;
  2001. result-: Expression;
  2002. PROCEDURE & InitStatementDesignator(position: Position; s: Statement);
  2003. BEGIN
  2004. InitDesignator(position); statement := s; result := NIL;
  2005. END InitStatementDesignator;
  2006. PROCEDURE Clone(): Expression;
  2007. VAR copy: StatementDesignator;
  2008. BEGIN
  2009. NEW(copy, position, CloneStatement(statement)) ;
  2010. copy.result := CloneExpression(result);
  2011. RETURN copy
  2012. END Clone;
  2013. PROCEDURE SetResult*(r: Expression);
  2014. BEGIN result := r
  2015. END SetResult;
  2016. PROCEDURE Accept*(v: Visitor);
  2017. VAR position: LONGINT;
  2018. BEGIN position := SELF.position.start; v.VisitStatementDesignator(SELF)
  2019. END Accept;
  2020. END StatementDesignator;
  2021. (** <<left(parameters)>>
  2022. procedure call
  2023. **)
  2024. ProcedureCallDesignator*= OBJECT (Designator)
  2025. VAR parameters-: ExpressionList;
  2026. PROCEDURE & InitProcedureCallDesignator(position: Position; left: Designator; parameters: ExpressionList);
  2027. BEGIN
  2028. InitDesignator(position); SELF.left := left; SELF.parameters := parameters;
  2029. END InitProcedureCallDesignator;
  2030. PROCEDURE Clone(): Expression;
  2031. VAR copy: ProcedureCallDesignator;
  2032. BEGIN
  2033. NEW(copy, position, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  2034. END Clone;
  2035. PROCEDURE Accept*(v: Visitor);
  2036. VAR position: LONGINT;
  2037. BEGIN position := SELF.position.start; v.VisitProcedureCallDesignator(SELF)
  2038. END Accept;
  2039. END ProcedureCallDesignator;
  2040. (** <<procedure(parameters)>>
  2041. builtin procedure call **)
  2042. BuiltinCallDesignator*= OBJECT (Designator) (*! should this be an extension of a procedure call designator ? *)
  2043. VAR
  2044. id-: LONGINT;
  2045. parameters-: ExpressionList;
  2046. builtin-: Builtin;
  2047. returnType-: Type;
  2048. PROCEDURE & InitBuiltinCallDesignator(position: Position; id: LONGINT; left: Designator; parameters: ExpressionList);
  2049. BEGIN
  2050. InitDesignator(position); SELF.parameters := parameters; SELF.id := id; SELF.left := left; returnType := NIL;
  2051. END InitBuiltinCallDesignator;
  2052. PROCEDURE SetReturnType*(type: Type);
  2053. BEGIN
  2054. returnType := type (* used for NEW Type() expression *)
  2055. END SetReturnType;
  2056. PROCEDURE Clone(): Expression;
  2057. VAR copy: BuiltinCallDesignator;
  2058. BEGIN
  2059. NEW(copy, position, id, CloneDesignator(left), CloneExpressionList(parameters)); RETURN copy
  2060. END Clone;
  2061. PROCEDURE Accept*(v: Visitor);
  2062. VAR position: LONGINT;
  2063. BEGIN position := SELF.position.start; v.VisitBuiltinCallDesignator(SELF)
  2064. END Accept;
  2065. END BuiltinCallDesignator;
  2066. (** <<left(type)>>
  2067. resolved parameter designator, designates a type guard
  2068. **)
  2069. TypeGuardDesignator* = OBJECT(Designator)
  2070. VAR
  2071. typeExpression-: Expression; (* for printing only *)
  2072. PROCEDURE &InitTypeGuardDesignator(position: Position; left: Designator; type: Type);
  2073. BEGIN InitDesignator(position); SELF.left := left; SELF.type := type; typeExpression := NIL;
  2074. END InitTypeGuardDesignator;
  2075. PROCEDURE SetTypeExpression*(typeExpression: Expression);
  2076. BEGIN SELF.typeExpression := typeExpression
  2077. END SetTypeExpression;
  2078. PROCEDURE Clone(): Expression;
  2079. VAR copy: TypeGuardDesignator;
  2080. BEGIN
  2081. NEW(copy, position, CloneDesignator(left), type); RETURN copy
  2082. END Clone;
  2083. PROCEDURE Accept*(v: Visitor);
  2084. VAR position: LONGINT;
  2085. BEGIN position := SELF.position.start; v.VisitTypeGuardDesignator(SELF)
  2086. END Accept;
  2087. PROCEDURE NeedsTrace* (): BOOLEAN;
  2088. BEGIN RETURN left.NeedsTrace() & type.NeedsTrace(); (* for x(Y): if x is untraced, then x as Y should also be treated untraced *)
  2089. END NeedsTrace;
  2090. END TypeGuardDesignator;
  2091. (** <<left^>> resolved as dereference operation on pointer variable left **)
  2092. DereferenceDesignator*= OBJECT (Designator)
  2093. PROCEDURE &InitDereferenceDesignator(position: Position; left: Designator);
  2094. BEGIN InitDesignator(position); SELF.left := left;
  2095. END InitDereferenceDesignator;
  2096. PROCEDURE Clone(): Expression;
  2097. VAR copy: DereferenceDesignator;
  2098. BEGIN
  2099. NEW(copy, position, CloneDesignator(left)); RETURN copy
  2100. END Clone;
  2101. PROCEDURE Accept*(v: Visitor);
  2102. VAR position: LONGINT;
  2103. BEGIN position := SELF.position.start; v.VisitDereferenceDesignator(SELF)
  2104. END Accept;
  2105. PROCEDURE NeedsTrace* (): BOOLEAN;
  2106. BEGIN
  2107. (*! semantic of x.y.z := new : if x is untraced then the effect of y.z := new remains untraced!
  2108. In other words: difference between y := x.y; y.z := new and x.y.z := new.
  2109. *)
  2110. RETURN left.NeedsTrace() & type.NeedsTrace(); (* for x^: if x is an untraced pointer, the content of x^ is also treated untraced *)
  2111. END NeedsTrace;
  2112. END DereferenceDesignator;
  2113. (** <<left^>> resolved as supercall operation on method left **)
  2114. SupercallDesignator*= OBJECT (Designator)
  2115. PROCEDURE &InitSupercallDesignator(position: Position; left: Designator);
  2116. BEGIN InitDesignator(position); SELF.left := left;
  2117. END InitSupercallDesignator;
  2118. PROCEDURE Clone(): Expression;
  2119. VAR copy: SupercallDesignator;
  2120. BEGIN
  2121. NEW(copy, position, CloneDesignator(left)); RETURN copy
  2122. END Clone;
  2123. PROCEDURE Accept*(v: Visitor);
  2124. VAR position: LONGINT;
  2125. BEGIN position := SELF.position.start; v.VisitSupercallDesignator(SELF)
  2126. END Accept;
  2127. END SupercallDesignator;
  2128. (** <<SELF.x>> **)
  2129. SelfDesignator*= OBJECT (Designator)
  2130. PROCEDURE &InitSelfDesignator(position: Position);
  2131. BEGIN InitDesignator(position);
  2132. END InitSelfDesignator;
  2133. PROCEDURE Clone(): Expression;
  2134. VAR copy: SelfDesignator;
  2135. BEGIN
  2136. NEW(copy, position); RETURN copy
  2137. END Clone;
  2138. PROCEDURE Accept*(v: Visitor);
  2139. VAR position: LONGINT;
  2140. BEGIN position := SELF.position.start; v.VisitSelfDesignator(SELF)
  2141. END Accept;
  2142. PROCEDURE NeedsTrace* (): BOOLEAN;
  2143. BEGIN RETURN type.NeedsTrace();
  2144. END NeedsTrace;
  2145. END SelfDesignator;
  2146. (** <<RESULT>> **)
  2147. ResultDesignator*= OBJECT (Designator)
  2148. PROCEDURE &InitResultDesignator(position: Position);
  2149. BEGIN InitDesignator(position);
  2150. END InitResultDesignator;
  2151. PROCEDURE Clone(): Expression;
  2152. VAR copy: ResultDesignator;
  2153. BEGIN
  2154. NEW(copy, position); RETURN copy
  2155. END Clone;
  2156. PROCEDURE Accept*(v: Visitor);
  2157. VAR position: LONGINT;
  2158. BEGIN position := SELF.position.start; v.VisitResultDesignator(SELF)
  2159. END Accept;
  2160. END ResultDesignator;
  2161. (**** values ****)
  2162. Value* = OBJECT (Expression)
  2163. VAR fingerprint-: FingerPrint;
  2164. PROCEDURE &InitValue(position: Position);
  2165. BEGIN SELF.position := position; resolved := SELF; InitFingerPrint(fingerprint);
  2166. END InitValue;
  2167. PROCEDURE SetFingerPrint*(CONST fp: FingerPrint);
  2168. BEGIN
  2169. SELF.fingerprint := fp
  2170. END SetFingerPrint;
  2171. PROCEDURE Accept*(v: Visitor);
  2172. VAR position: LONGINT;
  2173. BEGIN position := SELF.position.start; v.VisitValue(SELF)
  2174. END Accept;
  2175. PROCEDURE Equals*(v: Value):BOOLEAN;
  2176. BEGIN HALT(100); (* abstract *) RETURN FALSE; END Equals;
  2177. END Value;
  2178. (** <<value = TRUE , FALSE>> **)
  2179. BooleanValue* = OBJECT (Value)
  2180. VAR
  2181. value-: BOOLEAN;
  2182. PROCEDURE & InitBooleanValue(position: Position; value: BOOLEAN);
  2183. BEGIN
  2184. InitValue(position); SELF.value := value;
  2185. END InitBooleanValue;
  2186. PROCEDURE SetValue*(value: BOOLEAN);
  2187. BEGIN SELF.value := value
  2188. END SetValue;
  2189. PROCEDURE Clone(): Expression;
  2190. VAR copy: BooleanValue;
  2191. BEGIN
  2192. NEW(copy, position, value); RETURN copy
  2193. END Clone;
  2194. PROCEDURE Accept*(v: Visitor);
  2195. VAR position: LONGINT;
  2196. BEGIN position := SELF.position.start; v.VisitBooleanValue(SELF)
  2197. END Accept;
  2198. PROCEDURE Equals*(v: Value):BOOLEAN;
  2199. BEGIN RETURN (v IS BooleanValue) & (v(BooleanValue).value = value); END Equals;
  2200. END BooleanValue;
  2201. (** <<value = 123456>> **)
  2202. IntegerValue* = OBJECT (Value)
  2203. VAR
  2204. value-: Basic.Integer;
  2205. PROCEDURE & InitIntegerValue(position: Position; value: Basic.Integer);
  2206. BEGIN
  2207. InitValue(position); SELF.value := value;
  2208. END InitIntegerValue;
  2209. PROCEDURE SetValue*(value: Basic.Integer);
  2210. BEGIN SELF.value := value;
  2211. END SetValue;
  2212. PROCEDURE Clone(): Expression;
  2213. VAR copy: IntegerValue;
  2214. BEGIN
  2215. NEW(copy, position, value); 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-: Basic.Set;
  2252. PROCEDURE & InitSetValue(position: Position; value: Basic.Set);
  2253. BEGIN
  2254. InitValue(position); SELF.value := value;
  2255. END InitSetValue;
  2256. PROCEDURE SetValue*(value: Basic.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-: Basic.Integer;
  2422. PROCEDURE & InitEnumerationValue(position: Position; value: Basic.Integer);
  2423. BEGIN
  2424. InitValue(position); SELF.value := value;
  2425. END InitEnumerationValue;
  2426. PROCEDURE SetValue*(value: Basic.Integer);
  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*: Basic.Integer; 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-: Basic.Integer;
  3242. PROCEDURE & InitCaseStatement( position: Position ; outer: Statement);
  3243. BEGIN
  3244. InitStatement(position,outer ); variable := NIL; elsePart := NIL; caseParts := NIL;
  3245. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  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*(): Basic.Integer;
  3283. VAR val: Basic.Integer; 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: Basic.Integer);
  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;
  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: Basic.Integer): 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: Basic.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: Basic.Integer ): 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.