FoxSyntaxTree.Mod 168 KB

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