FoxSyntaxTree.Mod 167 KB

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