FoxSemanticChecker.Mod 400 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  83. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  84. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  85. currentScope-: SyntaxTree.Scope;
  86. currentIsRealtime: BOOLEAN;
  87. currentIsUnreachable: BOOLEAN;
  88. currentIsCellNet: BOOLEAN;
  89. currentIsBodyProcedure: BOOLEAN;
  90. currentIsExclusive: BOOLEAN;
  91. global: SyntaxTree.ModuleScope;
  92. withEntries: WithEntry;
  93. activeCellsStatement: BOOLEAN;
  94. replacements*: Replacement;
  95. cellsAreObjects: BOOLEAN;
  96. variableAccessed: BOOLEAN;
  97. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  98. BEGIN
  99. SELF.diagnostics := diagnostics;
  100. SELF.useDarwinCCalls := useDarwinCCalls;
  101. SELF.cooperative := cooperative;
  102. SELF.system := system;
  103. SELF.symbolFileFormat := symbolFileFormat;
  104. error := FALSE;
  105. NEW(typeFixes);
  106. NEW(pointerFixes);
  107. resolvedType := NIL;
  108. resolvedExpression := NIL;
  109. resolvedStatement := NIL;
  110. currentScope := NIL;
  111. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  112. SELF.importCache := importCache;
  113. arrayBaseImported := FALSE;
  114. complexNumbersImported := FALSE;
  115. SELF.VerboseErrorMessage := verboseErrorMessage;
  116. global := NIL;
  117. phase := UndefinedPhase;
  118. currentIsRealtime := FALSE;
  119. currentIsUnreachable := FALSE;
  120. currentIsCellNet := FALSE;
  121. currentIsBodyProcedure := FALSE;
  122. currentIsExclusive := FALSE;
  123. withEntries := NIL;
  124. SELF.cellsAreObjects := system.cellsAreObjects;
  125. COPY(backend, backendName);
  126. END InitChecker;
  127. (** report error **)
  128. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  129. VAR errModule: SyntaxTree.Module;
  130. BEGIN
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  141. END Warning;
  142. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  143. VAR errorMessage: ARRAY 256 OF CHAR;
  144. BEGIN
  145. Basic.Concat(errorMessage,msg," ", msg2);
  146. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  147. error := TRUE;
  148. END ErrorSS;
  149. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  150. VAR msg, msg2: ARRAY 256 OF CHAR;
  151. BEGIN
  152. COPY(msg1, msg);
  153. Strings.Append(msg, " = ");
  154. Basic.GetString(s, msg2);
  155. Strings.Append(msg, msg2);
  156. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  157. END InfoSS;
  158. (*** symbol lookup ***)
  159. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  160. **)
  161. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  162. VAR
  163. scope,baseScope: SyntaxTree.Scope;
  164. symbol, s: SyntaxTree.Symbol;
  165. ownerRecord,base: SyntaxTree.RecordType;
  166. BEGIN
  167. scope := inScope;
  168. symbol := NIL;
  169. WHILE (scope # NIL) & (symbol = NIL) DO
  170. symbol := scope.FindSymbol(name);
  171. s := NIL;
  172. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  173. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  175. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. END;
  177. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  178. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  179. ELSE
  180. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  181. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  182. symbol.MarkUsed;
  183. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  184. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  185. base := RecordBase(ownerRecord);
  186. IF (base # NIL) THEN
  187. baseScope := base.recordScope;
  188. symbol := Find(baseScope,name,FALSE);
  189. ELSE
  190. symbol := NIL;
  191. END;
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. END;
  196. END;
  197. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  198. END;
  199. IF (symbol # NIL) THEN
  200. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  201. ASSERT(phase = DeclarationPhase);
  202. ResolveSymbol(symbol)
  203. END;
  204. symbol.MarkUsed;
  205. END;
  206. RETURN symbol
  207. END Find;
  208. (*** types ***)
  209. (** find type declaration with name qualifiedIdentifier and return resolved type
  210. - check qualified identifier prefix, set scope to module scope if appropriate
  211. - check suffix in scope
  212. **)
  213. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  214. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  215. BEGIN
  216. result := NIL;
  217. prevScope := currentScope;
  218. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  219. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  220. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  221. IF symbol(SyntaxTree.Import).module = NIL THEN
  222. Error(qualifiedIdentifier.position,"module not loaded");
  223. result := SyntaxTree.invalidType;
  224. symbol := NIL;
  225. ELSE
  226. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  227. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  228. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  229. IF VerboseErrorMessage THEN
  230. Printout.Info("scope", currentScope);
  231. Printout.Info("symbol", symbol);
  232. END;
  233. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  234. END;
  235. END;
  236. ELSE
  237. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  238. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  239. symbol := NIL;
  240. END;
  241. ELSE
  242. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  243. IF symbol = NIL THEN
  244. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  245. IF VerboseErrorMessage THEN
  246. Printout.Info("Qualident",qualifiedIdentifier);
  247. Printout.Info("in scope",currentScope) ;
  248. END;
  249. END;
  250. END;
  251. IF symbol = NIL THEN (* error already handled *)
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  255. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  256. typeDeclaration := NIL;
  257. result := SyntaxTree.invalidType;
  258. ELSE
  259. currentScope := symbol.scope;
  260. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  261. result := ResolveType(typeDeclaration.declaredType);
  262. symbol.MarkUsed;
  263. ASSERT(result # NIL);
  264. END;
  265. currentScope := prevScope;
  266. RETURN result
  267. END ResolveNamedType;
  268. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  269. If node is currently being resolved then emit a cyclic definition error.
  270. Return TRUE only if node is fully resolved.
  271. **)
  272. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  273. VAR result: BOOLEAN;
  274. BEGIN
  275. IF SyntaxTree.Resolved IN x.state THEN
  276. result := FALSE
  277. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  278. Error(x.position,"cyclic definition");
  279. result := FALSE;
  280. ELSE
  281. result := TRUE;
  282. x.SetState(SyntaxTree.BeingResolved)
  283. END;
  284. RETURN result
  285. END TypeNeedsResolution;
  286. (** Return invalid type if x is currently being resolved, return x otherwise**)
  287. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  288. BEGIN
  289. IF SyntaxTree.Resolved IN x.state THEN
  290. RETURN x
  291. ELSE
  292. RETURN SyntaxTree.invalidType
  293. END;
  294. END ResolvedType;
  295. PROCEDURE VisitType(x: SyntaxTree.Type);
  296. BEGIN
  297. ASSERT(x = SyntaxTree.invalidType);
  298. END VisitType;
  299. (** resolve basic type **)
  300. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  301. BEGIN
  302. IF TypeNeedsResolution(x) THEN
  303. x.SetState(SyntaxTree.Resolved);
  304. END;
  305. resolvedType := ResolvedType(x)
  306. END VisitBasicType;
  307. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitByteType;
  311. (** resolve character type **)
  312. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  313. BEGIN
  314. VisitBasicType(x);
  315. END VisitCharacterType;
  316. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitBooleanType;
  320. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  321. BEGIN
  322. VisitBasicType(x);
  323. END VisitSetType;
  324. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  325. BEGIN
  326. VisitBasicType(x);
  327. END VisitAddressType;
  328. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  329. BEGIN
  330. VisitBasicType(x);
  331. END VisitSizeType;
  332. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  333. BEGIN
  334. VisitBasicType(x);
  335. END VisitAnyType;
  336. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitObjectType;
  340. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  341. BEGIN
  342. VisitBasicType(x);
  343. END VisitNilType;
  344. (** resolve integer type **)
  345. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitIntegerType;
  349. (** resolve real type **)
  350. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  351. BEGIN
  352. VisitBasicType(x);
  353. END VisitFloatType;
  354. (** resolve complex type **)
  355. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  356. BEGIN
  357. VisitBasicType(x);
  358. END VisitComplexType;
  359. (**
  360. resolve string type: nothing to be done
  361. **)
  362. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  363. BEGIN
  364. IF TypeNeedsResolution(x) THEN
  365. x.SetState(SyntaxTree.Resolved);
  366. END;
  367. resolvedType := ResolvedType(x)
  368. END VisitStringType;
  369. (**
  370. check enumeration scope: enter symbols and check for duplicate names
  371. **)
  372. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  373. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  374. BEGIN
  375. prevScope := currentScope;
  376. currentScope := x;
  377. e := x.firstConstant;
  378. WHILE (e # NIL) DO
  379. Register(e,x,FALSE);
  380. IF SymbolNeedsResolution(e) THEN
  381. IF e.value # NIL THEN
  382. value := ConstantExpression(e.value);
  383. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  384. ELSE
  385. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  386. value.SetType(x.ownerEnumeration);
  387. END;
  388. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  389. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  390. IF nextHighest > highest THEN highest := nextHighest END;
  391. END;
  392. e.SetValue(value);
  393. CheckSymbolVisibility(e);
  394. e.SetType(x.ownerEnumeration);
  395. e.SetState(SyntaxTree.Resolved);
  396. END;
  397. e := e.nextConstant;
  398. END;
  399. currentScope := prevScope;
  400. END CheckEnumerationScope;
  401. (**
  402. resolve enumeration type: check enumeration scope
  403. **)
  404. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  405. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  406. lowest, highest: LONGINT;
  407. BEGIN
  408. IF TypeNeedsResolution(x) THEN
  409. IF x.enumerationBase # NIL THEN
  410. position := x.enumerationBase.position;
  411. baseType := ResolveType(x.enumerationBase);
  412. resolved := baseType.resolved;
  413. baseScope := NIL;
  414. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  415. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  416. Error(position, "base type is no enumeration type");
  417. ELSE
  418. enumerationBase := resolved(SyntaxTree.EnumerationType);
  419. lowest := enumerationBase.rangeHighest+1;
  420. END;
  421. x.SetEnumerationBase(baseType);
  422. ELSE lowest := 0;
  423. END;
  424. highest := lowest-1;
  425. CheckEnumerationScope(x.enumerationScope, highest);
  426. x.SetRange(lowest, highest);
  427. x.SetState(SyntaxTree.Resolved);
  428. END;
  429. resolvedType := ResolvedType(x);
  430. END VisitEnumerationType;
  431. (**
  432. resolve range type: nothing to be done
  433. **)
  434. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  435. BEGIN
  436. IF TypeNeedsResolution(x) THEN
  437. x.SetState(SyntaxTree.Resolved);
  438. END;
  439. resolvedType := ResolvedType(x)
  440. END VisitRangeType;
  441. (**
  442. resolve qualified type
  443. - find and resolve named type and set resolved type
  444. **)
  445. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  446. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  447. BEGIN
  448. IF TypeNeedsResolution(x) THEN
  449. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  450. x.SetResolved(type.resolved);
  451. x.SetState(SyntaxTree.Resolved);
  452. x.SetTypeDeclaration (typeDeclaration);
  453. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  454. x.SetResolved(SyntaxTree.invalidType);
  455. END;
  456. resolvedType := x;
  457. END VisitQualifiedType;
  458. (**
  459. resolve array type
  460. - check base type
  461. - array of math array forbidden
  462. - static array of open array forbidden
  463. **)
  464. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  465. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  466. BEGIN
  467. IF TypeNeedsResolution(x) THEN
  468. x.SetArrayBase(ResolveType(x.arrayBase));
  469. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  470. arrayBase := x.arrayBase.resolved;
  471. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  472. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  473. pointerType.SetPointerBase(arrayBase);
  474. pointerType.SetHidden(TRUE);
  475. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  476. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  477. ELSE
  478. x.SetArrayBase(pointerType);
  479. END;
  480. END;
  481. IF x.length # NIL THEN
  482. variableAccessed := FALSE;
  483. e := ResolveExpression(x.length);
  484. IF (e.resolved = NIL) THEN
  485. IF variableAccessed THEN
  486. Error(e.position, "forbidden variable access");
  487. END;
  488. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  489. ELSE
  490. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  491. END;
  492. END;
  493. IF arrayBase IS SyntaxTree.ArrayType THEN
  494. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  495. Error(x.position,"forbidden static array of dynamic array");
  496. END;
  497. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  498. Error(x.position,"forbidden array mixed form");
  499. END;
  500. x.SetHasPointers(arrayBase.hasPointers);
  501. x.SetState(SyntaxTree.Resolved);
  502. END;
  503. resolvedType := ResolvedType(x);
  504. END VisitArrayType;
  505. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  506. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  507. BEGIN
  508. module := currentScope.ownerModule;
  509. IF module.name=name THEN
  510. (* do nothing *)
  511. ELSE
  512. moduleScope := module.moduleScope;
  513. import := moduleScope.FindImport(name);
  514. IF import = NIL THEN
  515. import := SyntaxTree.NewImport(position,name,name,TRUE);
  516. moduleScope.AddImport(import);
  517. Register(import,moduleScope,FALSE);
  518. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  519. VisitImport(import);
  520. ELSIF import.direct=FALSE THEN
  521. import.SetScope(module.moduleScope);
  522. import.SetDirect(TRUE);
  523. IF moduleScope.FindSymbol(import.name) = NIL THEN
  524. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  525. duplicate.SetContext(import.context);
  526. duplicate.SetModule(import.module);
  527. Register(duplicate,moduleScope,TRUE);
  528. VisitImport(duplicate);
  529. END;
  530. END;
  531. import.MarkUsed
  532. END;
  533. END ImportModule;
  534. (**
  535. resolve math array type
  536. - check base type
  537. - open math array of array forbidden
  538. - math array of tensor forbidden
  539. - static array of open array forbidden
  540. **)
  541. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  542. VAR arrayBase: SyntaxTree.Type;
  543. BEGIN
  544. IF TypeNeedsResolution(x) THEN
  545. x.SetArrayBase(ResolveType(x.arrayBase));
  546. IF x.length # NIL THEN
  547. x.SetLength(ConstantIntegerGeq0(x.length));
  548. END;
  549. arrayBase := x.arrayBase;
  550. IF arrayBase # NIL THEN
  551. arrayBase := arrayBase.resolved;
  552. IF arrayBase = SyntaxTree.invalidType THEN
  553. (* error already handled *)
  554. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  555. Error(x.position,"forbidden array mixed form");
  556. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  557. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  558. Error(x.position,"forbidden Tensor Array mix")
  559. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  560. Error(x.position,"forbidden static array of dynamic array")
  561. END;
  562. END;
  563. IF x.form = SyntaxTree.Static THEN
  564. x.SetIncrement(system.SizeOf(arrayBase));
  565. END;
  566. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  567. END;
  568. x.SetState(SyntaxTree.Resolved);
  569. END;
  570. resolvedType := ResolvedType(x);
  571. END VisitMathArrayType;
  572. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  573. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  574. (1) Rec = RECORD ... END; Ptr <---> Rec
  575. Ptr = POINTER TO Rec; ^ |
  576. | |
  577. TypeDesc TypeDesc
  578. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  579. ^ /
  580. | /
  581. TypeDesc <-- /
  582. *)
  583. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  584. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  585. BEGIN
  586. Strings.IntToStr(x.position.start,number);
  587. COPY(prefix,name);
  588. Strings.Append(name,"@");
  589. Strings.Append(name,number);
  590. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  591. typeDeclaration.SetDeclaredType(x);
  592. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  593. x.SetTypeDeclaration(typeDeclaration);
  594. currentScope.AddTypeDeclaration(typeDeclaration);
  595. typeDeclaration.SetScope(currentScope);
  596. END AnonymousTypeDeclaration;
  597. (**
  598. deferred pointer type resolving
  599. - resolve base type
  600. - check that base type is a record or array type
  601. - if error then set base type to invalid type
  602. **)
  603. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  604. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  605. BEGIN
  606. ASSERT(type.pointerBase # NIL);
  607. position := type.pointerBase.position;
  608. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  609. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  610. (* not for pointers, a type is needed for the records only
  611. IF type.typeDeclaration = NIL THEN
  612. AnonymousTypeDeclaration(type);
  613. END;
  614. *)
  615. END;
  616. resolved := ResolveType(type.pointerBase);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  618. type.SetPointerBase(resolved);
  619. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  620. recordType := resolved.resolved(SyntaxTree.RecordType);
  621. IF recordType.isObject & (recordType.baseType # NIL) THEN
  622. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "base type of object must be a realtime object");
  624. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  625. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  626. END;
  627. END;
  628. END;
  629. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  630. Error(position, "realtime object contains references to non-realtime objects");
  631. END;
  632. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  633. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  634. Error(position, "forbidden unsafe at static array");
  635. ELS
  636. *)
  637. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  638. Error(position, "forbidden unsafe at multidimensional array");
  639. END;
  640. END;
  641. ELSE
  642. Error(position, "forbidden pointer base type");
  643. type.SetPointerBase(SyntaxTree.invalidType)
  644. END
  645. END FixPointerType;
  646. (**
  647. resolve pointer type
  648. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  649. **)
  650. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  651. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  652. modifiers: SyntaxTree.Modifier; position: Position;
  653. BEGIN
  654. IF TypeNeedsResolution(x) THEN
  655. modifiers := x.modifiers;
  656. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  657. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  658. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  659. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  660. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  661. (* inheritance cycle check
  662. example:
  663. A=POINTER TO RECORD(B) END;
  664. B=POINTER TO RECORD(A) END;
  665. *)
  666. IF x.pointerBase IS SyntaxTree.RecordType THEN
  667. recordType := x.pointerBase(SyntaxTree.RecordType);
  668. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  669. recordBaseType := ResolveType(recordType.baseType);
  670. recordType.SetBaseType(recordBaseType);
  671. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  672. END;
  673. CheckModifiers(modifiers, TRUE);
  674. typeFixes.Add(x,currentScope);
  675. x.SetState(SyntaxTree.Resolved);
  676. END;
  677. resolvedType := ResolvedType(x)
  678. END VisitPointerType;
  679. (**
  680. resolve port type
  681. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  682. **)
  683. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  684. VAR value: LONGINT;
  685. BEGIN
  686. IF TypeNeedsResolution(x) THEN
  687. x.SetCellsAreObjects(cellsAreObjects);
  688. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  689. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  690. x.SetSize(value)
  691. ELSE
  692. x.SetSize(system.SizeOf(system.longintType));
  693. END;
  694. x.SetState(SyntaxTree.Resolved);
  695. END;
  696. resolvedType := ResolvedType(x)
  697. END VisitPortType;
  698. (**
  699. deferred procedure type resolving
  700. - resolve return type
  701. - traverse and resolve parameters
  702. **)
  703. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  704. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  705. BEGIN
  706. resolved := ResolveType(procedureType.returnType);
  707. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  708. Error(procedureType.position,"forbidden open array return type");
  709. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  710. Error(procedureType.position,"procedure with return type does not return");
  711. END;
  712. procedureType.SetReturnType(resolved);
  713. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  714. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  715. parameter.SetType(procedureType.returnType);
  716. parameter.SetAccess(SyntaxTree.Hidden);
  717. parameter.SetUntraced(procedureType.hasUntracedReturn);
  718. VisitParameter(parameter);
  719. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  720. END;
  721. (* process parameters *)
  722. parameter :=procedureType.firstParameter;
  723. WHILE (parameter # NIL) DO
  724. VisitParameter(parameter);
  725. parameter := parameter.nextParameter;
  726. END;
  727. parameter := procedureType.selfParameter;
  728. IF parameter # NIL THEN
  729. VisitParameter(parameter)
  730. END;
  731. END FixProcedureType;
  732. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  733. VAR prev,this: SyntaxTree.Modifier;
  734. BEGIN
  735. this := modifiers;prev := NIL;
  736. WHILE (this # NIL) & (this.identifier # name) DO
  737. prev := this; this := this.nextModifier;
  738. END;
  739. IF this # NIL THEN
  740. IF this.expression # NIL THEN
  741. Error(this.position,"unexpected expression");
  742. END;
  743. this.Resolved;
  744. position := this.position;
  745. RETURN TRUE
  746. ELSE
  747. RETURN FALSE
  748. END;
  749. END HasFlag;
  750. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  751. VAR prev,this: SyntaxTree.Modifier;
  752. BEGIN
  753. this := modifiers;prev := NIL;
  754. WHILE (this # NIL) & (this.identifier # name) DO
  755. prev := this; this := this.nextModifier;
  756. END;
  757. IF this # NIL THEN
  758. IF this.expression = NIL THEN
  759. Error(this.position,"expected expression value");
  760. ELSE
  761. this.SetExpression(ConstantExpression(this.expression));
  762. IF CheckIntegerValue(this.expression,value) THEN END;
  763. END;
  764. this.Resolved;
  765. position := this.position;
  766. RETURN TRUE
  767. ELSE RETURN FALSE
  768. END;
  769. END HasValue;
  770. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  771. VAR prev,this: SyntaxTree.Modifier;
  772. BEGIN
  773. this := modifiers;prev := NIL;
  774. WHILE (this # NIL) & (this.identifier # name) DO
  775. prev := this; this := this.nextModifier;
  776. END;
  777. IF this # NIL THEN
  778. IF this.expression = NIL THEN
  779. Error(this.position,"expected expression value");
  780. ELSE
  781. this.SetExpression(ConstantExpression(this.expression));
  782. IF CheckStringValue(this.expression,value) THEN END;
  783. END;
  784. this.Resolved;
  785. position := this.position;
  786. RETURN TRUE
  787. ELSE RETURN FALSE
  788. END;
  789. END HasStringValue;
  790. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  791. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  792. BEGIN
  793. IF cellsAreObjects THEN RETURN FALSE END;
  794. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  795. IF svalue = "A2" THEN
  796. RETURN TRUE
  797. END;
  798. END;
  799. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  800. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  801. END;
  802. RETURN FALSE;
  803. (*
  804. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  805. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  806. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  807. IF svalue[0] = "~" THEN
  808. Strings.TrimLeft(svalue, "~");
  809. IF svalue = backendName THEN
  810. RETURN TRUE;
  811. END;
  812. ELSIF svalue # backendName THEN
  813. RETURN TRUE;
  814. END;
  815. END;
  816. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  817. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  818. END;
  819. RETURN FALSE;
  820. *)
  821. END SkipImplementation;
  822. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  823. VAR this: SyntaxTree.Modifier;
  824. BEGIN
  825. this := modifiers;
  826. WHILE this # NIL DO
  827. IF ~this.resolved THEN
  828. IF checkUse THEN
  829. Error(this.position,"unexpected modifier");
  830. ELSE
  831. this.SetExpression(ResolveExpression(this.expression));
  832. this.Resolved;
  833. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  834. END;
  835. END;
  836. this := this.nextModifier
  837. END;
  838. END CheckModifiers;
  839. (**
  840. resolve procedure type
  841. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  842. **)
  843. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  844. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  845. BEGIN
  846. IF TypeNeedsResolution(procedureType) THEN
  847. modifiers := procedureType.modifiers;
  848. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  849. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  850. procedureType.SetInterrupt(TRUE);
  851. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  852. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  853. IF useDarwinCCalls THEN (*fld*)
  854. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  855. ELSE
  856. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  857. END
  858. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  859. procedureType.SetNoReturn(TRUE);
  860. END;
  861. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  862. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  863. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  864. CheckModifiers(modifiers, TRUE);
  865. modifiers := procedureType.returnTypeModifiers;
  866. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  867. CheckModifiers(modifiers, TRUE);
  868. typeFixes.Add(procedureType,currentScope);
  869. procedureType.SetHasPointers(procedureType.isDelegate);
  870. procedureType.SetState(SyntaxTree.Resolved);
  871. END;
  872. resolvedType := ResolvedType(procedureType)
  873. END VisitProcedureType;
  874. (** check and resolve record type
  875. - check base type: must be record, math array or array-structured object type
  876. - check declarations
  877. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  878. **)
  879. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  880. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  881. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  882. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  883. hasPointers: BOOLEAN;
  884. modifiers: SyntaxTree.Modifier;
  885. value: LONGINT;
  886. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  887. BEGIN
  888. type := type.resolved;
  889. IF (type IS SyntaxTree.PointerType) &
  890. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  891. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  892. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  893. RETURN TRUE
  894. ELSE
  895. RETURN FALSE
  896. END;
  897. END IsPointerToRecord;
  898. BEGIN
  899. IF TypeNeedsResolution(x) THEN
  900. hasPointers := FALSE;
  901. modifiers := x.modifiers;
  902. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  903. CheckModifiers(modifiers, TRUE);
  904. IF x.baseType # NIL THEN
  905. position := x.baseType.position;
  906. baseType := ResolveType(x.baseType);
  907. resolved := baseType.resolved;
  908. hasPointers := hasPointers OR resolved.hasPointers;
  909. IF x.isObject THEN (* object *)
  910. ASSERT(x.pointerType # NIL);
  911. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  912. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  913. baseType := NIL
  914. ELSIF IsPointerToRecord(resolved,recordType) THEN
  915. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  916. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  917. ELSE
  918. Error(position,"object does not extend pointer to record, object or math array ")
  919. END;
  920. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  921. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  922. ELSIF IsPointerToRecord(resolved,recordType) THEN
  923. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  924. ELSIF resolved IS SyntaxTree.RecordType THEN
  925. ELSE
  926. Error(position,"pointer to record does not extend pointer to record or record")
  927. END;
  928. ELSE
  929. IF resolved IS SyntaxTree.RecordType THEN
  930. ELSE
  931. Error(position,"record does not extend record")
  932. END;
  933. END;
  934. x.SetBaseType(baseType);
  935. IF x.Level() > 15 THEN
  936. Error(position, "record/object inheritance level too high");
  937. (* note:
  938. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  939. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  940. inheritance history of a type.
  941. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  942. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  943. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  944. *)
  945. END;
  946. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  947. Error(position, "invalid inheritance of disposable types");
  948. END;
  949. END;
  950. Declarations(x.recordScope, FALSE, {0});
  951. x.SetState(SyntaxTree.Resolved);
  952. Declarations(x.recordScope, FALSE, {1});
  953. ResolveArrayStructure(x);
  954. (* computation of sizes and offsets skipped -> done in backend / system *)
  955. recordBase := x.GetBaseRecord();
  956. IF recordBase = NIL THEN numberMethods := 0
  957. ELSE numberMethods := recordBase.recordScope.numberMethods
  958. END;
  959. isRealtime := TRUE;
  960. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  961. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  962. END;
  963. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  964. WHILE symbol # NIL DO
  965. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  966. IF symbol IS SyntaxTree.Variable THEN
  967. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  968. END;
  969. IF symbol IS SyntaxTree.Procedure THEN
  970. procedure := symbol(SyntaxTree.Procedure);
  971. IF procedure.super # NIL THEN
  972. procedure.SetMethodNumber(procedure.super.methodNumber)
  973. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  974. procedure.SetMethodNumber(numberMethods);
  975. INC(numberMethods);
  976. END;
  977. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  978. Error(procedure.position,"realtime procedure in non-realtime object")
  979. END;
  980. END;
  981. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  982. Error(symbol.position,"non-realtime symbol in realtime object")
  983. END;
  984. symbol := symbol.nextSymbol;
  985. END;
  986. IF isRealtime THEN x.SetRealtime(TRUE) END;
  987. x.recordScope.SetNumberMethods(numberMethods);
  988. (* TODO: is this needed anymore? *)
  989. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  990. Error(x.position,"object extends a record")
  991. END;
  992. IF (x.typeDeclaration = NIL) THEN
  993. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  994. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  995. (*
  996. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  997. AnonymousTypeDeclaration(x,name);
  998. *)
  999. ELSE
  1000. AnonymousTypeDeclaration(x,"Anonymous");
  1001. END;
  1002. END;
  1003. x.SetHasPointers(hasPointers);
  1004. x.SetState(SyntaxTree.Resolved);
  1005. END;
  1006. resolvedType := ResolvedType(x);
  1007. END VisitRecordType;
  1008. (** check and resolve cell type
  1009. - check base type: must be cell
  1010. - check declarations
  1011. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1012. **)
  1013. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1014. VAR
  1015. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1016. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1017. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1018. recordBase: SyntaxTree.RecordType;
  1019. numberMethods, int: LONGINT;
  1020. real: LONGREAL;
  1021. bool: BOOLEAN;
  1022. set: SET;
  1023. v: SyntaxTree.Expression;
  1024. str: Scanner.StringType;
  1025. atype: SyntaxTree.ArrayType;
  1026. prev: SyntaxTree.Scope;
  1027. skip: BOOLEAN;
  1028. svalue: ARRAY 32 OF CHAR;
  1029. BEGIN
  1030. IF TypeNeedsResolution(x) THEN
  1031. recordBase := NIL;
  1032. IF cellsAreObjects THEN
  1033. IF x.baseType = NIL THEN
  1034. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1035. ImportModule(qualifiedIdentifier.prefix, x.position);
  1036. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1037. x.SetBaseType(ResolveType(x.baseType));
  1038. recordBase := x.GetBaseRecord();
  1039. IF recordBase = NIL THEN
  1040. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1041. END;
  1042. ELSE
  1043. x.SetBaseType(ResolveType(x.baseType));
  1044. END;
  1045. ELSE
  1046. x.SetBaseType(ResolveType(x.baseType));
  1047. END;
  1048. IF recordBase = NIL THEN numberMethods := 0
  1049. ELSE numberMethods := recordBase.recordScope.numberMethods
  1050. END;
  1051. modifier := x.modifiers;
  1052. (*IF ~x.isCellNet THEN*)
  1053. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1054. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1055. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1056. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1057. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1058. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1059. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1060. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1061. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1062. symbol := system.activeCellsCapabilities;
  1063. WHILE symbol # NIL DO
  1064. IF HasFlag(modifier, symbol.name, position) THEN END;
  1065. symbol := symbol.nextSymbol;
  1066. END;
  1067. modifier := x.modifiers;
  1068. WHILE (modifier # NIL) DO
  1069. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1070. IF modifier.expression # NIL THEN
  1071. v := ConstantExpression(modifier.expression);
  1072. property.SetValue(v);
  1073. IF IsIntegerValue(modifier.expression, int) THEN
  1074. (*property.SetValue(modifier.expression);*)
  1075. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1076. property.SetType(system.longintType);
  1077. ELSIF IsRealValue(modifier.expression, real) THEN
  1078. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1079. property.SetType(system.longrealType);
  1080. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1081. property.SetType(system.booleanType);
  1082. ELSIF IsSetValue(modifier.expression, set) THEN
  1083. property.SetType(system.setType);
  1084. ELSIF IsStringValue(modifier.expression, str) THEN
  1085. (*property.SetValue(modifier.expression);*)
  1086. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1087. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1088. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1089. property.SetType(atype);
  1090. ELSE
  1091. Error(modifier.position, "unsupported property type");
  1092. END;
  1093. ELSE (* flag property *)
  1094. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1095. property.SetType(system.booleanType);
  1096. END;
  1097. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1098. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1099. x.AddProperty(property);
  1100. modifier := modifier.nextModifier;
  1101. END;
  1102. CheckModifiers(modifier, FALSE);
  1103. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1104. (* process parameters *)
  1105. prev := currentScope;
  1106. currentScope := x.cellScope;
  1107. parameter :=x.firstParameter;
  1108. WHILE (parameter # NIL) DO
  1109. VisitParameter(parameter);
  1110. type := parameter.type.resolved;
  1111. IF ~(type IS SyntaxTree.PortType) THEN
  1112. WHILE IsStaticArray(type, type, len) DO
  1113. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1114. END;
  1115. WHILE IsDynamicArray(type, type) DO
  1116. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1117. END;
  1118. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1119. Error(parameter.position, "invalid type, must be port or static array of port ");
  1120. END;
  1121. END;
  1122. parameter := parameter.nextParameter;
  1123. END;
  1124. currentScope := prev;
  1125. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1126. WHILE symbol # NIL DO
  1127. IF symbol IS SyntaxTree.Variable THEN
  1128. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1129. END;
  1130. symbol := symbol.nextSymbol;
  1131. END;
  1132. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1133. IF (x.typeDeclaration = NIL) THEN
  1134. AnonymousTypeDeclaration(x,"Anonymous");
  1135. END;
  1136. x.SetState(SyntaxTree.Resolved);
  1137. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1138. Warning(x.position, "Forbidden empty Body.");
  1139. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1140. Warning(x.position, "Non-empty body for an engine?");
  1141. END;
  1142. END;
  1143. resolvedType := ResolvedType(x);
  1144. END VisitCellType;
  1145. (* check if an object is an array-structured object type
  1146. - determine the array structure
  1147. - collect operators from top to bottom in the inheritance hierarchy
  1148. - check if LEN operator is declared
  1149. - determine number of possible index operators
  1150. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1151. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1152. *)
  1153. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1154. VAR
  1155. indexOperatorCount, i: LONGINT;
  1156. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1157. isTensor: BOOLEAN;
  1158. BEGIN
  1159. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1160. (* determine array structure *)
  1161. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1162. END;
  1163. IF recordType.HasArrayStructure() THEN
  1164. (* the object is an ASOT *)
  1165. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1166. (* reset array access operators *)
  1167. arrayAccessOperators.len := NIL;
  1168. arrayAccessOperators.generalRead := NIL;
  1169. arrayAccessOperators.generalWrite := NIL;
  1170. IF isTensor THEN
  1171. (* all operators of dimensionalities 1 to max *)
  1172. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1173. ELSE
  1174. (* all operators of certain dimensionality *)
  1175. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1176. END;
  1177. NEW(arrayAccessOperators.read, indexOperatorCount);
  1178. NEW(arrayAccessOperators.write, indexOperatorCount);
  1179. FOR i := 0 TO indexOperatorCount - 1 DO
  1180. arrayAccessOperators.read[i] := NIL;
  1181. arrayAccessOperators.write[i] := NIL
  1182. END;
  1183. (* collect access operators in the record scope *)
  1184. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1185. IF arrayAccessOperators.len = NIL THEN
  1186. (* TODO: think about making this operator optional for static array structures *)
  1187. Error(recordType.position, "LEN operator missing")
  1188. END;
  1189. (* show error messages *)
  1190. IF isTensor THEN
  1191. (* require ARRAY [*] OF RANGE *)
  1192. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1193. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1194. ELSE
  1195. (* forbid ARRAY [*] OF RANGE *)
  1196. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1197. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1198. (* require RANGE, RANGE, ... RANGE *)
  1199. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1200. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1201. END;
  1202. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1203. ELSE
  1204. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1205. IF recordType.recordScope.firstOperator # NIL THEN
  1206. RETURN;
  1207. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1208. END
  1209. END
  1210. END ResolveArrayStructure;
  1211. (** collect array access operators in a record scope **)
  1212. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1213. VAR
  1214. baseType: SyntaxTree.Type;
  1215. operator: SyntaxTree.Operator;
  1216. isReadOperator, isGeneralOperator: BOOLEAN;
  1217. indexListSize, indexListKind, hashValue: LONGINT;
  1218. BEGIN
  1219. (* if a parent record scope exists, collect the operators there first *)
  1220. baseType := recordScope.ownerRecord.baseType;
  1221. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1222. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1223. END;
  1224. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1225. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1226. END;
  1227. (* go through all operators in the current record scope *)
  1228. operator := recordScope.firstOperator;
  1229. WHILE operator # NIL DO
  1230. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1231. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1232. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1233. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1234. IF isGeneralOperator THEN
  1235. IF isReadOperator THEN
  1236. arrayAccessOperators.generalRead := operator
  1237. ELSE
  1238. arrayAccessOperators.generalWrite := operator
  1239. END
  1240. ELSE
  1241. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1242. IF isReadOperator THEN
  1243. arrayAccessOperators.read[hashValue] := operator
  1244. ELSE
  1245. arrayAccessOperators.write[hashValue] := operator
  1246. END
  1247. END
  1248. END
  1249. ELSE
  1250. Error(operator.position, 'invalid operator')
  1251. END;
  1252. operator := operator.nextOperator
  1253. END
  1254. END CollectArrayAccessOperators;
  1255. (** the hash value of an index operator **)
  1256. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1257. VAR result: LONGINT;
  1258. BEGIN
  1259. IF isTensor THEN
  1260. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1261. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1262. ELSE
  1263. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1264. END
  1265. ELSE
  1266. result := indexListKind
  1267. END;
  1268. RETURN result
  1269. END IndexOperatorHash;
  1270. (** 2 to the power of exponent **)
  1271. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1272. VAR result, i: LONGINT;
  1273. BEGIN
  1274. result := 1;
  1275. FOR i := 1 TO exponent DO
  1276. result := result * 2;
  1277. END;
  1278. RETURN result
  1279. END TwoToThePowerOf;
  1280. (** check if a LEN operator has a correct signature. i.e.
  1281. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1282. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1283. **)
  1284. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1285. VAR
  1286. procedureType: SyntaxTree.ProcedureType;
  1287. returnedArrayType: SyntaxTree.MathArrayType;
  1288. result: BOOLEAN;
  1289. BEGIN
  1290. result := FALSE;
  1291. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1292. IF (procedureType.numberParameters = 0) THEN
  1293. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1294. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1295. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1296. IF returnedArrayType.form = SyntaxTree.Open THEN
  1297. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1298. result := TRUE
  1299. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1300. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1301. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1302. result := TRUE
  1303. END
  1304. END
  1305. END
  1306. END
  1307. END;
  1308. IF result THEN
  1309. (* export symbol automatically *)
  1310. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1311. ELSE
  1312. Error(operator.position, "LEN operator with invalid signature");
  1313. END;
  1314. RETURN result
  1315. END CheckLenOperator;
  1316. (** check if an index operator has a correct signature. i.e.
  1317. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1318. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1319. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1320. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1321. - determine if it is a read or write operator (existance of return type)
  1322. - check index parameters
  1323. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1324. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1325. [LONGINT] -> binary 0 -> 0
  1326. [RANGE] -> binary 1 -> 1
  1327. [LONGINT, LONGINT] -> binary 00 -> 0
  1328. [LONGINT, RANGE] -> binary 01 -> 1
  1329. [RANGE, LONGINT] -> binary 10 -> 2
  1330. [RANGE, RANGE] -> binary 11 -> 3
  1331. etc.
  1332. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1333. - for read operators, check if return type matches the type of data that is read
  1334. - for write operators, check if last parameter type matches the type of data that is written
  1335. **)
  1336. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1337. VAR
  1338. elementType, otherElementType, dataType: SyntaxTree.Type;
  1339. procedureType: SyntaxTree.ProcedureType;
  1340. mathArrayType: SyntaxTree.MathArrayType;
  1341. parameter: SyntaxTree.Parameter;
  1342. parameterCount, rangeCount, i: LONGINT;
  1343. hasTypeError: BOOLEAN;
  1344. BEGIN
  1345. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1346. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1347. (* determine if it is a read or write operator *)
  1348. isReadOperator := (procedureType.returnType # NIL);
  1349. IF isReadOperator THEN
  1350. indexListSize := parameterCount;
  1351. ELSE
  1352. indexListSize := parameterCount - 1;
  1353. END;
  1354. IF indexListSize < 1 THEN
  1355. Error(operator.position, "index operator with too few parameters");
  1356. RETURN FALSE
  1357. END;
  1358. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1359. (* general operator *)
  1360. isGeneralOperator := TRUE;
  1361. IF indexListSize > 1 THEN
  1362. Error(operator.position, "index operator with too many parameters");
  1363. RETURN FALSE
  1364. END;
  1365. (* ARRAY [*] OF RANGE*)
  1366. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1367. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1368. Error(operator.position, "index parameter not dynamic math array of range");
  1369. RETURN FALSE
  1370. END;
  1371. parameter := procedureType.firstParameter.nextParameter
  1372. ELSE
  1373. (* fixed-dim. operator *)
  1374. isGeneralOperator := FALSE;
  1375. (* check number of index parameters *)
  1376. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1377. (* for tensors, limited to a certain size *)
  1378. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1379. Error(operator.position, "too many index parameters for tensor");
  1380. RETURN FALSE
  1381. END
  1382. ELSE
  1383. (* for non-tensors, depends on dimensionality *)
  1384. IF indexListSize # arrayStructure.Dimensionality() THEN
  1385. Error(operator.position, "index parameter count does not match dimensionality");
  1386. RETURN FALSE
  1387. END
  1388. END;
  1389. (* go through all index parameters
  1390. - count the number of ranges
  1391. - determine the index list kind number
  1392. *)
  1393. indexListKind := 0;
  1394. rangeCount := 0;
  1395. parameter := procedureType.firstParameter;
  1396. FOR i := 1 TO indexListSize DO
  1397. indexListKind := indexListKind * 2;
  1398. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1399. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1400. INC(indexListKind);
  1401. INC(rangeCount)
  1402. ELSE
  1403. Error(parameter.position, "integer or range expected");
  1404. RETURN FALSE
  1405. END;
  1406. parameter := parameter.nextParameter
  1407. END;
  1408. END;
  1409. (*
  1410. - for read operators: check type of last parameter
  1411. - for write operators: check return type
  1412. *)
  1413. IF isReadOperator THEN
  1414. dataType := procedureType.returnType (* the return type *)
  1415. ELSE
  1416. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1417. END;
  1418. elementType := arrayStructure.ElementType();
  1419. hasTypeError := FALSE;
  1420. IF isGeneralOperator THEN
  1421. (* ARRAY [?] OF <Element> *)
  1422. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1423. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1424. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1425. hasTypeError := TRUE
  1426. END
  1427. ELSE
  1428. hasTypeError := TRUE
  1429. END
  1430. ELSE
  1431. IF rangeCount = 0 THEN
  1432. (* <Element> *)
  1433. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1434. ELSE
  1435. (* ARRAY [*, *, ..., *] OF <Element> *)
  1436. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1437. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1438. IF mathArrayType.IsFullyDynamic() THEN
  1439. IF mathArrayType.Dimensionality() = rangeCount THEN
  1440. otherElementType := mathArrayType.ElementType();
  1441. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1442. ELSE
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. ELSE
  1449. hasTypeError := TRUE
  1450. END
  1451. END
  1452. END;
  1453. IF hasTypeError THEN
  1454. IF isReadOperator THEN
  1455. Error(operator.position, "return type does not match")
  1456. ELSE
  1457. Error(parameter.position, "type of last parameter does not match")
  1458. END;
  1459. RETURN FALSE
  1460. END;
  1461. (* export symbol automatically *)
  1462. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1463. RETURN TRUE
  1464. END CheckIndexOperator;
  1465. (** resolve all pending types (late resolving).
  1466. - type fixes are resolved at the end of the declaration phase
  1467. - type fixes may imply new type fixes that are also entered at the end of the list
  1468. **)
  1469. PROCEDURE FixTypes;
  1470. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1471. BEGIN
  1472. prevScope := currentScope;
  1473. p := typeFixes.Get(currentScope);
  1474. WHILE p # NIL DO
  1475. ASSERT(currentScope # NIL);
  1476. ASSERT(p IS SyntaxTree.Type);
  1477. IF p IS SyntaxTree.PointerType THEN
  1478. FixPointerType(p(SyntaxTree.PointerType))
  1479. ELSIF p IS SyntaxTree.ProcedureType THEN
  1480. FixProcedureType(p(SyntaxTree.ProcedureType))
  1481. ELSE
  1482. HALT(100);
  1483. END;
  1484. p := typeFixes.Get(currentScope);
  1485. END;
  1486. currentScope :=prevScope;
  1487. END FixTypes;
  1488. (**
  1489. resolve type x
  1490. - if x is nil then return nil
  1491. - if x cannot be resolved then the result is invalidType else the result is x
  1492. - the resolved type is entered into x.resolved
  1493. **)
  1494. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1495. VAR prev,resolved: SyntaxTree.Type;
  1496. BEGIN
  1497. prev := resolvedType;
  1498. resolvedType := SyntaxTree.invalidType;
  1499. IF x = NIL THEN resolvedType := NIL
  1500. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1501. END;
  1502. resolved := resolvedType;
  1503. resolvedType := prev;
  1504. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1505. RETURN resolved
  1506. END ResolveType;
  1507. (*** compatibility rules ***)
  1508. (**
  1509. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1510. **)
  1511. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1512. VAR result: SyntaxTree.Type;
  1513. BEGIN
  1514. result := SyntaxTree.invalidType;
  1515. IF type = NIL THEN Error(position, "expression of type NIL");
  1516. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1517. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1518. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1519. ELSE result := type.resolved
  1520. END;
  1521. RETURN result
  1522. END RegularType;
  1523. (** returns signature compatibility of procedure types this and to
  1524. - if not compatible then error is reported
  1525. - compatibility means type equality
  1526. **)
  1527. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1528. VAR result: BOOLEAN;
  1529. BEGIN
  1530. result := SameType(to,this);
  1531. IF ~result THEN
  1532. Error(position, "signature incompatible");
  1533. IF VerboseErrorMessage THEN
  1534. Printout.Info("this",this);
  1535. Printout.Info("to",to);
  1536. END;
  1537. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1538. Error(position, "signature incompatible: realtime flag must be inherited");
  1539. END;
  1540. RETURN result
  1541. END SignatureCompatible;
  1542. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1543. - for var parameters compatibility means same type except for
  1544. - formal is of open array of system byte
  1545. - formal is of record type
  1546. - formal is of open array type
  1547. - formal is of open math array type
  1548. - for value parameters compatibllity means assignment compatibility except for
  1549. - formal is of open array type
  1550. if compatible the return true else report error and return false
  1551. **)
  1552. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1553. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1554. BEGIN
  1555. formalType := RegularType(formal.position,formal.type);
  1556. actualType := RegularType(actual.position,actual.type);
  1557. error := FALSE;
  1558. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1559. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1560. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1561. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1562. END;
  1563. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1564. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1565. ELSIF ~IsVariable(actual) THEN
  1566. result := FALSE; error := TRUE;
  1567. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1568. Error(actual.position,"not a variable: no operator for writing");
  1569. ELSE
  1570. Error(actual.position,"is not a variable");
  1571. END;
  1572. IF VerboseErrorMessage THEN
  1573. Printout.Info("actual",actual);
  1574. Printout.Info("formal",formal);
  1575. END;
  1576. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1577. result := CompatibleTo(system,actualType,formalType);
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1581. IF IsArrayStructuredObjectType(actualType) THEN
  1582. actualType := MathArrayStructureOfType(actualType)
  1583. END;
  1584. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1585. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1586. Error(actual.position,"incompatible non-static actual type");
  1587. END;
  1588. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1589. Error(actual.position,"incompatible tensor (use a range expression)");
  1590. END;
  1591. ELSE
  1592. result := SameType(actualType,formalType)
  1593. END
  1594. ELSE
  1595. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1596. actualType := system.characterType;
  1597. END;
  1598. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1599. result := TRUE; (* special rule for WINAPI parameters *)
  1600. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1601. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1602. ELSE
  1603. result := CompatibleTo(system,actualType,formalType);
  1604. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1605. Error(actual.position,"incompatible non-static actual type");
  1606. END;
  1607. END;
  1608. END;
  1609. IF ~result & ~error THEN
  1610. Error(actual.position,"incompatible parameter");
  1611. IF VerboseErrorMessage THEN
  1612. Printout.Info("actual",actual);
  1613. Printout.Info("formal",formal);
  1614. END;
  1615. END;
  1616. RETURN result
  1617. END ParameterCompatible;
  1618. (** check compatibility for expressions of the form left := right
  1619. - if compatible then return true else error report and return false
  1620. - check if left is variable
  1621. - check compatiblity
  1622. **)
  1623. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1624. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1625. BEGIN
  1626. result := FALSE;
  1627. leftType := RegularType(left.position,left.type);
  1628. rightType := RegularType(right.position,right.type);
  1629. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1630. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1631. END;
  1632. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1633. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1634. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1635. (* error already handled *)
  1636. result := TRUE;
  1637. ELSIF ~IsVariable(left) THEN
  1638. Error(left.position,"is not a variable");
  1639. IF VerboseErrorMessage THEN
  1640. Printout.Info("left",left);
  1641. Printout.Info("right",right);
  1642. END;
  1643. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1644. result := TRUE;
  1645. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1646. result := TRUE
  1647. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1648. Error(left.position,"incompatible assignment");
  1649. IF VerboseErrorMessage THEN
  1650. Printout.Info("left",left);
  1651. Printout.Info("right",right);
  1652. END;
  1653. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1654. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1655. Error(right.position,"forbidden assignment of a nested procedure");
  1656. ELSE
  1657. result := TRUE
  1658. END;
  1659. RETURN result
  1660. END AssignmentCompatible;
  1661. (*** values ***)
  1662. (** check and resolve integer value **)
  1663. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1664. VAR hugeint: HUGEINT;
  1665. BEGIN
  1666. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1667. value.SetType(Global.GetIntegerType(system,hugeint));
  1668. resolvedExpression := value
  1669. END VisitIntegerValue;
  1670. (** check and resolve real value **)
  1671. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1672. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1673. BEGIN
  1674. subtype := value(SyntaxTree.RealValue).subtype;
  1675. IF subtype = Scanner.Real THEN
  1676. type := system.realType
  1677. ELSIF subtype = Scanner.Longreal THEN
  1678. type := system.longrealType
  1679. ELSE
  1680. HALT(100)
  1681. END;
  1682. value.SetType(type);
  1683. resolvedExpression := value
  1684. END VisitRealValue;
  1685. (** check and resolve complex value **)
  1686. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1687. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1688. BEGIN
  1689. subtype := value(SyntaxTree.ComplexValue).subtype;
  1690. IF subtype = Scanner.Real THEN
  1691. type := system.complexType
  1692. ELSIF subtype = Scanner.Longreal THEN
  1693. type := system.longcomplexType
  1694. ELSE
  1695. HALT(100)
  1696. END;
  1697. value.SetType(type);
  1698. resolvedExpression := value
  1699. END VisitComplexValue;
  1700. (** check and resolve set value **)
  1701. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1702. BEGIN
  1703. value.SetType(system.setType);
  1704. resolvedExpression := value
  1705. END VisitSetValue;
  1706. (** check and resolve set value **)
  1707. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1708. BEGIN
  1709. value.SetType(SyntaxTree.invalidType);
  1710. resolvedExpression := value
  1711. END VisitMathArrayValue;
  1712. (** check and resolve boolean value **)
  1713. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1714. BEGIN
  1715. value.SetType(system.booleanType);
  1716. resolvedExpression := value
  1717. END VisitBooleanValue;
  1718. (** check and resolve string value **)
  1719. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1720. BEGIN
  1721. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1722. resolvedExpression := value
  1723. END VisitStringValue;
  1724. (** check and resolve character value **)
  1725. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1726. BEGIN
  1727. value.SetType(system.characterType);
  1728. resolvedExpression := value
  1729. END VisitCharacterValue;
  1730. (** check and resolve nil value **)
  1731. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1732. BEGIN
  1733. value.SetType(system.nilType);
  1734. resolvedExpression := value
  1735. END VisitNilValue;
  1736. (** check and resolve enumerator value **)
  1737. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1738. BEGIN
  1739. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1740. ASSERT(value.type # NIL);
  1741. resolvedExpression := value
  1742. END VisitEnumerationValue;
  1743. (*** expressions ***)
  1744. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1745. - check all elements on integer type
  1746. - if element range is constant, then check lower and upper bound
  1747. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1748. if an error occurs then report error and return invalidExpression
  1749. **)
  1750. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1751. VAR
  1752. i: LONGINT;
  1753. element: SyntaxTree.Expression;
  1754. constant: BOOLEAN;
  1755. elements: SyntaxTree.ExpressionList;
  1756. s: SET;
  1757. result: SyntaxTree.Expression;
  1758. value: SyntaxTree.Value;
  1759. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1760. VAR
  1761. left, right: SyntaxTree.Expression;
  1762. elementResult: SyntaxTree.Expression;
  1763. leftInteger, rightInteger, temp: LONGINT;
  1764. BEGIN
  1765. (* set context of range *)
  1766. IF element IS SyntaxTree.RangeExpression THEN
  1767. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1768. END;
  1769. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1770. IF elementResult = SyntaxTree.invalidExpression THEN
  1771. (* error already reported *)
  1772. constant := FALSE
  1773. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1774. (* the element is a range expression *)
  1775. (* extract left and right hand side of range *)
  1776. left := elementResult(SyntaxTree.RangeExpression).first;
  1777. right := elementResult(SyntaxTree.RangeExpression).last;
  1778. (* guaranteed by VisitRangeExpression: *)
  1779. ASSERT((left # NIL) & (right # NIL));
  1780. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1781. ELSE
  1782. (* the element is not a range expression *)
  1783. (* check type and add conversion if needed *)
  1784. IF IsIntegerType(elementResult.type.resolved) THEN
  1785. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1786. ELSE
  1787. Error(elementResult.position, "non integer element in set");
  1788. elementResult := SyntaxTree.invalidExpression;
  1789. constant := FALSE
  1790. END;
  1791. left := elementResult;
  1792. right := elementResult
  1793. END;
  1794. IF elementResult # SyntaxTree.invalidExpression THEN
  1795. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1796. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1797. Error(left.position,"not allowed set integer value");
  1798. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1799. Error(right.position,"not allowed set integer value");
  1800. END
  1801. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1802. Error(right.position,"not allowed set integer value");
  1803. ELSE
  1804. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1805. s := {};
  1806. ELSE
  1807. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1808. IF leftInteger < 0 THEN leftInteger := 0 END;
  1809. (*!!!!!!!!! this is a hack !!!!!!! *)
  1810. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1811. extends the range x..31 to x..63 !!!!!! *)
  1812. s := s + {leftInteger..rightInteger};
  1813. END;
  1814. END;
  1815. ELSE
  1816. constant := FALSE;
  1817. END
  1818. END;
  1819. RETURN elementResult
  1820. END CheckElement;
  1821. BEGIN
  1822. result := set; constant := TRUE; s := {}; elements := set.elements;
  1823. IF elements # NIL THEN
  1824. FOR i := 0 TO elements.Length()-1 DO
  1825. element := elements.GetExpression(i);
  1826. element := CheckElement(element);
  1827. IF element = SyntaxTree.invalidExpression THEN
  1828. result := SyntaxTree.invalidExpression
  1829. END;
  1830. elements.SetExpression(i,element);
  1831. END;
  1832. END;
  1833. IF constant THEN
  1834. value := SyntaxTree.NewSetValue(set.position,s);
  1835. value.SetType(system.setType);
  1836. result.SetResolved(value);
  1837. END;
  1838. (* optimization possible
  1839. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1840. left this to the programmer...
  1841. *)
  1842. result.SetType(system.setType);
  1843. resolvedExpression := result;
  1844. END VisitSet;
  1845. (*
  1846. old variant: quite generic but needs better conversion handling, do this?
  1847. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1848. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1849. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1850. BEGIN
  1851. type := NIL;
  1852. numberElements := x.elements.Length();
  1853. FOR i := 0 TO numberElements-1 DO
  1854. expression := x.elements.GetExpression(i);
  1855. position := expression.position;
  1856. expression := ResolveExpression(x.elements.GetExpression(i));
  1857. x.elements.SetExpression(i,de);
  1858. IF type = NIL THEN
  1859. type := expression.type;
  1860. ELSIF CompatibleTo(system,expression.type,type) THEN
  1861. (* ok *)
  1862. ELSIF CompatibleTo(system,type,expression.type) THEN
  1863. type := expression.type
  1864. ELSE
  1865. Error(expression.position, "incompatible element types");
  1866. type := SyntaxTree.invalidType;
  1867. END;
  1868. END;
  1869. isValue := TRUE;
  1870. FOR i := 0 TO numberElements-1 DO
  1871. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1872. x.elements.SetExpression(i,expression);
  1873. isValue := isValue & (expression.resolved # NIL);
  1874. END;
  1875. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1876. arrayType.SetArrayBase(type);
  1877. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1878. IF isValue THEN
  1879. value := SyntaxTree.NewMathArrayValue(position);
  1880. value.SetElements(x.elements);
  1881. x.SetResolved(value);
  1882. END;
  1883. x.SetType(arrayType);
  1884. resolvedExpression := x;
  1885. END VisitMathArrayExpression;
  1886. *)
  1887. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1888. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1889. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1890. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1891. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1892. BEGIN
  1893. numberElements := x.elements.Length();
  1894. FOR i := 0 TO numberElements-1 DO
  1895. expression := x.elements.GetExpression(i);
  1896. IF expression IS SyntaxTree.MathArrayExpression THEN
  1897. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1898. ELSE
  1899. position := expression.position;
  1900. expression := ResolveExpression(x.elements.GetExpression(i));
  1901. x.elements.SetExpression(i,expression);
  1902. IF type = NIL THEN
  1903. type := expression.type;
  1904. ELSIF CompatibleTo(system,expression.type,type) THEN
  1905. (* ok *)
  1906. ELSIF CompatibleTo(system,type,expression.type) THEN
  1907. type := expression.type
  1908. ELSE
  1909. Error(expression.position, "incompatible element types");
  1910. type := SyntaxTree.invalidType;
  1911. END;
  1912. END;
  1913. END;
  1914. END RecursivelyFindType;
  1915. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1916. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1917. BEGIN
  1918. numberElements := x.elements.Length();
  1919. FOR i := 0 TO numberElements-1 DO
  1920. expression := x.elements.GetExpression(i);
  1921. IF expression IS SyntaxTree.MathArrayExpression THEN
  1922. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1923. ELSE
  1924. position := expression.position;
  1925. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1926. x.elements.SetExpression(i,expression);
  1927. isValue := isValue & (expression.resolved # NIL);
  1928. END;
  1929. END;
  1930. END RecursivelySetExpression;
  1931. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1932. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1933. arrayType: SyntaxTree.MathArrayType;
  1934. BEGIN
  1935. numberElements := x.elements.Length();
  1936. baseType := NIL;
  1937. gsize := 0;
  1938. FOR i := 0 TO numberElements-1 DO
  1939. expression := x.elements.GetExpression(i);
  1940. IF expression IS SyntaxTree.MathArrayExpression THEN
  1941. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1942. IF i=0 THEN
  1943. gsize := size;
  1944. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1945. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1946. ELSE expression.SetType(baseType)
  1947. END;
  1948. ELSIF baseType = NIL THEN baseType := type;
  1949. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1950. END;
  1951. END;
  1952. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1953. arrayType.SetArrayBase(baseType);
  1954. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1955. RETURN ResolveType(arrayType);
  1956. END RecursivelySetType;
  1957. BEGIN
  1958. type := NIL;
  1959. RecursivelyFindType(x);
  1960. isValue := TRUE;
  1961. RecursivelySetExpression(x);
  1962. arrayType := RecursivelySetType(x);
  1963. x.SetType(arrayType);
  1964. IF isValue THEN
  1965. value := SyntaxTree.NewMathArrayValue(x.position);
  1966. value.SetArray(x);
  1967. x.SetResolved(value);
  1968. value.SetType(arrayType);
  1969. END;
  1970. x.SetType(arrayType);
  1971. resolvedExpression := x;
  1972. END VisitMathArrayExpression;
  1973. (** check and resolve unary expression **)
  1974. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1975. VAR
  1976. left: SyntaxTree.Expression;
  1977. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1978. bool: BOOLEAN;
  1979. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1980. value: SyntaxTree.Value;
  1981. BEGIN
  1982. type := SyntaxTree.invalidType;
  1983. left := ResolveExpression(unaryExpression.left);
  1984. unaryExpression.SetLeft(left);
  1985. operator := unaryExpression.operator;
  1986. result := unaryExpression;
  1987. IF ~system.operatorDefined[operator] THEN
  1988. Error(left.position,"Operator Not Defined");
  1989. RETURN
  1990. ELSIF left.type = NIL THEN
  1991. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1992. resolvedExpression := SyntaxTree.invalidExpression;
  1993. RETURN
  1994. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1995. RETURN
  1996. END;
  1997. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1998. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1999. END;
  2000. IF operatorCall # NIL THEN
  2001. result := operatorCall;
  2002. type := operatorCall.type;
  2003. (* admissible operators
  2004. Minus number, set
  2005. Not boolean
  2006. *)
  2007. ELSE
  2008. CASE unaryExpression.operator OF
  2009. |Scanner.Minus:
  2010. IF IsIntegerType(left.type.resolved) THEN
  2011. IF left.resolved # NIL THEN
  2012. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2013. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2014. result.SetResolved(value);
  2015. type := Global.GetIntegerType(system,int);
  2016. value.SetType(type);
  2017. ELSE
  2018. type := left.type
  2019. END
  2020. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2021. IF IsRealValue(left,real) THEN
  2022. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2023. result.SetResolved(value);
  2024. type := left.type;
  2025. value.SetType(type);
  2026. ELSE
  2027. type := left.type;
  2028. END;
  2029. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2030. IF IsSetValue(left,set) THEN
  2031. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2032. result.SetResolved(value);
  2033. type := left.type;
  2034. value.SetType(type);
  2035. ELSE
  2036. type := left.type;
  2037. END;
  2038. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2039. IF IsComplexValue(left, real, imaginary) THEN
  2040. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2041. result.SetResolved(value);
  2042. type := left.type;
  2043. value.SetType(type);
  2044. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2045. ELSE
  2046. type := left.type;
  2047. END
  2048. ELSE
  2049. Error(left.position,"unary operator not applicable");
  2050. END;
  2051. |Scanner.Not:
  2052. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2053. IF IsBooleanValue(left,bool) THEN
  2054. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2055. result.SetResolved(value);
  2056. type := system.booleanType;
  2057. value.SetType(type);
  2058. ELSE
  2059. type := system.booleanType;
  2060. END;
  2061. ELSE
  2062. Error(left.position,"unary operator not applicable");
  2063. END;
  2064. |Scanner.Plus:
  2065. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2066. result := left; type := left.type;
  2067. ELSE
  2068. Error(left.position,"unary operator not applicable");
  2069. END;
  2070. (* ADDRESS OF *)
  2071. |Scanner.Address:
  2072. IF HasAddress(left) THEN
  2073. type := system.addressType;
  2074. ELSE
  2075. type := SyntaxTree.invalidType;
  2076. Error(left.position,"has no address");
  2077. Printout.Info("par", left);
  2078. END;
  2079. (* SIZE OF *)
  2080. |Scanner.Size:
  2081. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2082. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2083. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2084. value := SyntaxTree.NewIntegerValue(left.position, int);
  2085. result.SetResolved(value);
  2086. type := Global.GetIntegerType(system,int);
  2087. value.SetType(type)
  2088. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2089. ELSE
  2090. (* for variables, system sizeof could represent the physically occupied size
  2091. determined via the type descriptor, implement that ? *)
  2092. Error(left.position,"is not a type symbol");
  2093. END
  2094. (* ALIAS OF *)
  2095. |Scanner.Alias:
  2096. type := left.type.resolved;
  2097. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2098. type := SyntaxTree.invalidType;
  2099. Error(left.position,"alias on non math array type");
  2100. END;
  2101. ELSE
  2102. Error(left.position,"unary operator not defined");
  2103. END;
  2104. END;
  2105. result.SetType(type);
  2106. resolvedExpression := result
  2107. END VisitUnaryExpression;
  2108. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2109. VAR
  2110. result: SyntaxTree.Expression;
  2111. array: SyntaxTree.MathArrayExpression;
  2112. value: SyntaxTree.MathArrayValue;
  2113. isValue: BOOLEAN;
  2114. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2115. BEGIN
  2116. type := type.resolved;
  2117. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2118. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2119. END;
  2120. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2121. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2122. END;
  2123. RETURN type
  2124. END BaseType;
  2125. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2126. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2127. BEGIN
  2128. numberElements := x.elements.Length();
  2129. FOR i := 0 TO numberElements-1 DO
  2130. expression := x.elements.GetExpression(i);
  2131. IF expression IS SyntaxTree.MathArrayExpression THEN
  2132. array := SyntaxTree.NewMathArrayExpression(position);
  2133. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2134. expression := array;
  2135. ELSE
  2136. position := expression.position;
  2137. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2138. isValue := isValue & (expression.resolved # NIL);
  2139. END;
  2140. to.elements.AddExpression(expression);
  2141. END;
  2142. END RecursivelyConvert;
  2143. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2144. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2145. arrayType: SyntaxTree.MathArrayType;
  2146. BEGIN
  2147. numberElements := x.elements.Length();
  2148. baseType := NIL;
  2149. gsize := 0;
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2154. IF i=0 THEN
  2155. gsize := size;
  2156. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2157. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2158. ELSE expression.SetType(baseType)
  2159. END;
  2160. ELSIF baseType = NIL THEN baseType := type;
  2161. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2162. END;
  2163. END;
  2164. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2165. arrayType.SetArrayBase(baseType);
  2166. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2167. RETURN ResolveType(arrayType);
  2168. END RecursivelySetType;
  2169. BEGIN
  2170. result := SyntaxTree.invalidExpression;
  2171. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2172. result := expression (* do not convert *)
  2173. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2174. isValue := TRUE;
  2175. type := BaseType(type);
  2176. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2177. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2178. value := SyntaxTree.NewMathArrayValue(array.position);
  2179. value.SetArray(array);
  2180. value.SetType(RecursivelySetType(array));
  2181. result := value;
  2182. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2183. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2184. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2185. IF result = NIL THEN
  2186. result := SyntaxTree.invalidExpression;
  2187. Error(position, "incompatible conversion");
  2188. IF VerboseErrorMessage THEN
  2189. Printout.Info("expression",expression);
  2190. Printout.Info("type",type);
  2191. END;
  2192. END;
  2193. END;
  2194. RETURN result
  2195. END MathArrayConversion;
  2196. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2197. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2198. BEGIN
  2199. result := expression; type := type.resolved;
  2200. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2201. (* skip, no conversion *)
  2202. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2203. int := expression(SyntaxTree.IntegerValue).hvalue;
  2204. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2205. int := Global.ConvertSigned(int,system.SizeOf(type));
  2206. result := SyntaxTree.NewIntegerValue(position,int);
  2207. result.SetType(type);
  2208. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2209. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2210. result := SyntaxTree.NewIntegerValue(position,int);
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.FloatType) THEN
  2213. result := SyntaxTree.NewRealValue(expression.position,int);
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2216. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2217. result.SetType(type);
  2218. ELSIF (type IS SyntaxTree.SetType) THEN
  2219. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2222. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2223. result.SetType(type);
  2224. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2225. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2226. Error(position, "huge integer value incompatible to enumeration");
  2227. END;
  2228. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2229. result.SetType(type);
  2230. ELSIF (type IS SyntaxTree.PortType) THEN
  2231. result := ConvertValue(position, expression, system.integerType);
  2232. ELSE
  2233. Error(position, "integer value cannot be converted");
  2234. result := SyntaxTree.invalidExpression;
  2235. IF VerboseErrorMessage THEN
  2236. Printout.Info("expression",expression);
  2237. Printout.Info("type",type);
  2238. END;
  2239. END;
  2240. ELSIF IsRealValue(expression,real) THEN
  2241. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2242. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2243. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2246. int := ENTIERH(real);
  2247. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2248. result.SetType(type);
  2249. ELSIF (type IS SyntaxTree.FloatType) THEN
  2250. result := SyntaxTree.NewRealValue(position,real);
  2251. result.SetType(type);
  2252. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2253. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2254. result.SetType(type);
  2255. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2256. ELSIF (type IS SyntaxTree.PortType) THEN
  2257. result := ConvertValue(position, expression, system.integerType);
  2258. ELSE
  2259. Error(position, "real value cannot be converted");
  2260. result := SyntaxTree.invalidExpression;
  2261. END
  2262. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2263. IF (type IS SyntaxTree.ComplexType) THEN
  2264. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2265. result.SetType(type);
  2266. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2267. ELSE
  2268. Error(position, "complex value cannot be converted");
  2269. result := SyntaxTree.invalidExpression;
  2270. END
  2271. ELSIF IsSetValue(expression,set) THEN
  2272. IF (type IS SyntaxTree.IntegerType) THEN
  2273. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2276. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2277. result.SetType(type);
  2278. ELSIF (type IS SyntaxTree.PortType) THEN
  2279. result := ConvertValue(position, expression, system.integerType);
  2280. ELSE
  2281. Error(position, "set value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. ELSIF IsStringValue(expression,string) THEN
  2285. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2286. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2289. ELSE
  2290. Error(position, "string value cannot be converted");
  2291. result := SyntaxTree.invalidExpression;
  2292. END;
  2293. ELSIF IsCharacterValue(expression,char) THEN
  2294. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2295. string[0] := char; string[1] := 0X;
  2296. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2297. result := SyntaxTree.NewStringValue(expression.position,string);
  2298. result.SetType(type);
  2299. ELSIF (type IS SyntaxTree.ByteType) THEN
  2300. (* do not simply set the new type as this could invalidate types of constants *)
  2301. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2302. result.SetType(type)
  2303. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2304. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2305. result.SetType(type);
  2306. ELSIF (type IS SyntaxTree.SetType) THEN
  2307. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2308. result.SetType(type);
  2309. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2310. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2311. result.SetType(type);
  2312. ELSIF (type IS SyntaxTree.PortType) THEN
  2313. result := ConvertValue(position, expression, system.integerType);
  2314. ELSE
  2315. Error(position, "character value cannot be converted");
  2316. result := SyntaxTree.invalidExpression;
  2317. END;
  2318. ELSIF expression IS SyntaxTree.NilValue THEN
  2319. IF type IS SyntaxTree.AddressType THEN
  2320. result := SyntaxTree.NewIntegerValue(position,0);
  2321. result.SetType(type);
  2322. ELSE
  2323. result := expression;
  2324. END;
  2325. (* nothing to be done *)
  2326. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2327. result := MathArrayConversion(position, expression,type);
  2328. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2329. int := expression(SyntaxTree.EnumerationValue).value;
  2330. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2331. int := Global.ConvertSigned(int,system.SizeOf(type));
  2332. result := SyntaxTree.NewIntegerValue(position,int);
  2333. result.SetType(type);
  2334. ELSE
  2335. result := expression;
  2336. END;
  2337. (* nothing to be done *)
  2338. ELSE
  2339. Error(position, "expression cannot be converted");
  2340. IF VerboseErrorMessage THEN
  2341. Printout.Info("expression",expression);
  2342. Printout.Info("type",type);
  2343. END;
  2344. result := SyntaxTree.invalidExpression;
  2345. END;
  2346. RETURN result
  2347. END ConvertValue;
  2348. (**
  2349. return a conversion of an expression to a given type
  2350. - if expression is already of same type then return expression
  2351. - if incompatible conversion then report error and return invalidExpression
  2352. **)
  2353. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2354. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2355. BEGIN
  2356. type := type.resolved;
  2357. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2358. result := expression;
  2359. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2360. ELSIF expression = NIL THEN (* NIL expression *)
  2361. ELSIF expression.type = NIL THEN
  2362. Error(position, "expression of type NIL cannot be converted");
  2363. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2364. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2365. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2366. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2367. ELSIF expression.resolved # NIL THEN (* value *)
  2368. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2369. IF value IS SyntaxTree.Value THEN
  2370. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2371. result.SetResolved(value(SyntaxTree.Value));
  2372. result.SetType(value.type);
  2373. ELSE
  2374. result := value
  2375. END;
  2376. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2377. expressionList := SyntaxTree.NewExpressionList();
  2378. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2379. typeDeclaration.SetDeclaredType(type);
  2380. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2381. typeSymbol.SetType(typeDeclaration.type);
  2382. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2383. expressionList.AddExpression(expression);
  2384. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2385. result.SetType(type);
  2386. ELSIF IsArrayStructuredObjectType(type) THEN
  2387. (* no type can be converted to an array-structured object type *)
  2388. HALT(100)
  2389. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2390. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2391. result := MathArrayConversion(position, expression,type);
  2392. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2393. expression := ConvertToMathArray(expression);
  2394. type := MathArrayStructureOfType(type);
  2395. result := MathArrayConversion(position, expression, type)
  2396. ELSE
  2397. Error(expression.position,"cannot convert non array type to array type")
  2398. END;
  2399. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2400. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2401. OR ~(type IS SyntaxTree.ArrayType) THEN
  2402. Error(expression.position,"cannot convert array type to non-array type")
  2403. END;
  2404. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2405. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2406. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2407. (*skip, no conversion*)
  2408. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2409. (* skip, no conversion *)
  2410. ELSE
  2411. ASSERT(~(type IS SyntaxTree.RangeType));
  2412. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2413. ASSERT(type # NIL);
  2414. END;
  2415. RETURN result
  2416. END NewConversion;
  2417. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2418. BEGIN
  2419. IF CompatibleTo(system,expression.type, type) THEN
  2420. RETURN NewConversion(position, expression, type, NIL);
  2421. ELSE
  2422. Error(expression.position, "incompatible expression");
  2423. RETURN SyntaxTree.invalidExpression
  2424. END;
  2425. END CompatibleConversion;
  2426. (**
  2427. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2428. **)
  2429. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2430. VAR leftType,rightType: SyntaxTree.Type;
  2431. BEGIN
  2432. IF left.type = NIL THEN Error(left.position,"no type")
  2433. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2434. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2435. (* error already handled *)
  2436. ELSE
  2437. leftType := left.type.resolved; rightType := right.type.resolved;
  2438. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2439. right := NewConversion(right.position, right, leftType, NIL);
  2440. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2441. left := NewConversion(left.position,left,rightType,NIL);
  2442. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2443. right := NewConversion(right.position, right, leftType, NIL);
  2444. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2445. left := NewConversion(left.position,left,rightType,NIL);
  2446. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2447. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2448. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2449. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2450. ELSIF
  2451. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2452. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2453. (* must be the case LONGREAL / COMPLEX ) *)
  2454. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2455. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2456. ELSE
  2457. Error(left.position,"incompatible operands");
  2458. END;
  2459. END;
  2460. END ConvertOperands;
  2461. (** find and return best operator matching to parameter list (nil, if none)
  2462. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2463. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2464. **)
  2465. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2466. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2467. identifier: SyntaxTree.Identifier;
  2468. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2469. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2470. BEGIN
  2471. operator := scope.firstOperator;
  2472. WHILE(operator # NIL) DO
  2473. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2474. procedureType := operator.type(SyntaxTree.ProcedureType);
  2475. distance := Distance(system, procedureType,actualParameters);
  2476. IF (distance < Infinity) THEN
  2477. IF returnType # NIL THEN
  2478. IF procedureType.returnType = NIL THEN
  2479. distance := Infinity
  2480. ELSE
  2481. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2482. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2483. END;
  2484. END;
  2485. END;
  2486. (*
  2487. IF distance < Infinity THEN
  2488. TRACE(distance, operator);
  2489. Printout.Info("potential operator",operator);
  2490. ELSE
  2491. Printout.Info("operator not possible",operator);
  2492. END;
  2493. *)
  2494. IF distance < bestDistance THEN
  2495. bestDistance := distance;
  2496. bestOperator := operator;
  2497. END;
  2498. END;
  2499. operator := operator.nextOperator;
  2500. END;
  2501. (*
  2502. Printout.Info("taken operator",bestOperator);
  2503. *)
  2504. END FindInScope;
  2505. BEGIN
  2506. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2507. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2508. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2509. import := currentScope.ownerModule.moduleScope.firstImport;
  2510. WHILE (bestDistance > 0) & (import # NIL) DO
  2511. IF import.module # NIL THEN
  2512. identifier := Global.GetIdentifier(operator,import.module.case);
  2513. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2514. END;
  2515. import := import.nextImport;
  2516. END;
  2517. RETURN bestOperator
  2518. END FindOperator;
  2519. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2520. BEGIN
  2521. currentScope := scope;
  2522. END SetCurrentScope;
  2523. (**
  2524. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2525. - handle LEN and DIM operator for array-structured object types
  2526. - find operator, if found then
  2527. - if in other module then add import designator
  2528. - create symbol designator for operator
  2529. - if error then return invalidExpression, if no operator then return NIL
  2530. **)
  2531. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2532. VAR
  2533. operator: SyntaxTree.Operator;
  2534. import: SyntaxTree.Import;
  2535. expression, result: SyntaxTree.Expression;
  2536. designator: SyntaxTree.Designator;
  2537. actualParameters, tempList: SyntaxTree.ExpressionList;
  2538. recordType: SyntaxTree.RecordType;
  2539. castReturnType : SyntaxTree.MathArrayType;
  2540. BEGIN
  2541. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2542. result := SyntaxTree.invalidExpression
  2543. ELSIF leftExpression = NIL THEN
  2544. result := NIL
  2545. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2546. (* LEN or DIM operator on array-structured object type *)
  2547. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2548. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2549. IF recordType.arrayAccessOperators.len = NIL THEN
  2550. Error(position, "call of undeclared LEN operator");
  2551. result := SyntaxTree.invalidExpression
  2552. ELSE
  2553. ASSERT(leftExpression IS SyntaxTree.Designator);
  2554. designator := leftExpression(SyntaxTree.Designator);
  2555. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2556. ASSERT(expression IS SyntaxTree.Designator);
  2557. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2558. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2559. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2560. result := designator
  2561. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2562. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2563. tempList := SyntaxTree.NewExpressionList();
  2564. tempList.AddExpression(rightExpression);
  2565. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2566. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2567. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2568. tempList := SyntaxTree.NewExpressionList();
  2569. tempList.AddExpression(designator);
  2570. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2571. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2572. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2573. END
  2574. END;
  2575. ELSE
  2576. IF ~complexNumbersImported THEN
  2577. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2578. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2579. THEN
  2580. (* operators on complex numbers *)
  2581. ImportModule(Global.ComplexNumbersName,position);
  2582. complexNumbersImported := TRUE;
  2583. END;
  2584. END;
  2585. (* import OCArrayBase if needed *)
  2586. IF ~arrayBaseImported THEN
  2587. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2588. IF op = Global.Dim THEN
  2589. (* not existing in OCArrayBase *)
  2590. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2591. (* not existing in OCArrayBase *)
  2592. ELSE
  2593. ImportModule(Global.ArrayBaseName,position);
  2594. arrayBaseImported := TRUE;
  2595. END
  2596. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2597. ImportModule(Global.ArrayBaseName,position);
  2598. arrayBaseImported := TRUE
  2599. END;
  2600. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2601. (* LEN(RANGE) *)
  2602. ImportModule(Global.ArrayBaseName,position);
  2603. arrayBaseImported := TRUE;
  2604. END;
  2605. END;
  2606. actualParameters := SyntaxTree.NewExpressionList();
  2607. actualParameters.AddExpression(leftExpression);
  2608. IF rightExpression # NIL THEN
  2609. actualParameters.AddExpression(rightExpression)
  2610. END;
  2611. operator := FindOperator(system,op,actualParameters,resultType);
  2612. IF operator # NIL THEN
  2613. designator := NIL;
  2614. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2615. import := currentScope.ownerModule.moduleScope.firstImport;
  2616. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2617. import := import.nextImport;
  2618. END;
  2619. expression := NewSymbolDesignator(position,NIL,import);
  2620. designator := expression(SyntaxTree.Designator);
  2621. END;
  2622. expression := NewSymbolDesignator(position,designator,operator);
  2623. designator := expression(SyntaxTree.Designator);
  2624. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2625. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2626. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2627. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2628. result.SetType(castReturnType);
  2629. END;
  2630. ELSE
  2631. result := NIL;
  2632. END;
  2633. END;
  2634. RETURN result
  2635. END NewOperatorCall;
  2636. (** check and resolve binary expression **)
  2637. (*! clean up *)
  2638. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2639. VAR left,right,result: SyntaxTree.Expression;
  2640. leftType, rightType: SyntaxTree.Type;
  2641. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2642. cl,cr: CHAR;
  2643. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2644. type: SyntaxTree.Type;
  2645. value: SyntaxTree.Value;
  2646. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2647. integerConstantFolding: BOOLEAN;
  2648. list: SyntaxTree.ExpressionList;
  2649. PROCEDURE NewBool(v: BOOLEAN);
  2650. BEGIN
  2651. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2652. value.SetType(system.booleanType);
  2653. result.SetResolved(value);
  2654. type := system.booleanType
  2655. END NewBool;
  2656. PROCEDURE NewSet(v: SET);
  2657. BEGIN
  2658. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2659. value.SetType(system.setType);
  2660. result.SetResolved(value);
  2661. type := system.setType;
  2662. END NewSet;
  2663. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2664. BEGIN
  2665. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2666. (* type cast to "larger" type only if the value is still in the range *)
  2667. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2668. value.SetType(t);
  2669. END;
  2670. result.SetResolved(value);
  2671. type := value.type;
  2672. END NewInteger;
  2673. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2674. BEGIN
  2675. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2676. value.SetType(t);
  2677. result.SetResolved(value);
  2678. type := t;
  2679. END NewReal;
  2680. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2681. BEGIN
  2682. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2683. value.SetType(t);
  2684. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2685. result.SetResolved(value);
  2686. type := t;
  2687. END NewComplex;
  2688. BEGIN
  2689. type := SyntaxTree.invalidType;
  2690. left := ResolveExpression(binaryExpression.left);
  2691. right := ResolveExpression(binaryExpression.right);
  2692. binaryExpression.SetLeft(left);
  2693. binaryExpression.SetRight(right);
  2694. result := binaryExpression;
  2695. operator := binaryExpression.operator;
  2696. IF ~system.operatorDefined[operator] THEN
  2697. Error(left.position,"Operator Not Defined");
  2698. result := SyntaxTree.invalidExpression;
  2699. RETURN
  2700. END;
  2701. IF left.type = NIL THEN
  2702. Error(left.position,"Expression has no result type");
  2703. result := SyntaxTree.invalidExpression;
  2704. RETURN;
  2705. END;
  2706. IF right.type = NIL THEN
  2707. Error(right.position,"Expression has no result type");
  2708. result := SyntaxTree.invalidExpression;
  2709. RETURN;
  2710. END;
  2711. leftType := left.type.resolved; rightType := right.type.resolved;
  2712. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2713. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2714. END;
  2715. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2716. list := SyntaxTree.NewExpressionList();
  2717. list.AddExpression(right);
  2718. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2719. END;
  2720. IF operatorCall # NIL THEN
  2721. result := operatorCall;
  2722. type := operatorCall.type;
  2723. (* admissible operators:
  2724. Times, Plus, Minus numeric numeric numeric
  2725. set set set
  2726. Slash numeric numeric real /complex
  2727. set set set
  2728. Div , Mod integer integer integer
  2729. And, Or bool bool bool
  2730. Equal, Unequal basic basic bool
  2731. pointer pointer bool
  2732. object object bool
  2733. record record bool
  2734. string string bool
  2735. enumerator enumerator bool
  2736. Less, LessEqual,
  2737. Greater, GreaterEqual integer/real integer/real bool
  2738. enumerator enumerator bool
  2739. In integer set bool
  2740. Is pointer type bool
  2741. object type bool
  2742. record type bool
  2743. Upto: special abbreviation for a..b
  2744. *)
  2745. ELSIF (left.type = NIL) THEN
  2746. Error(left.position,"type (left operand) = NIL in binary expression");
  2747. D.Str("nil type in "); D.Type(left); D.Ln;
  2748. result := SyntaxTree.invalidExpression;
  2749. ELSIF (right.type = NIL) THEN
  2750. Error(right.position,"type (right operand) = NIL in binary expression");
  2751. result := SyntaxTree.invalidExpression;
  2752. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2753. result := SyntaxTree.invalidExpression;
  2754. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2755. HALT(100);
  2756. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2757. type := system.booleanType;
  2758. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2759. Error(right.position,"is not a type ");
  2760. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2761. Error(binaryExpression.position,"is not a type extension of ");
  2762. IF VerboseErrorMessage THEN
  2763. Printout.Info("left",left);
  2764. Printout.Info("right",right);
  2765. END;
  2766. ELSIF IsUnsafePointer(left.type) THEN
  2767. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2768. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2769. NewBool(TRUE)
  2770. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2771. NewBool(TRUE);
  2772. ELSIF IsUnextensibleRecord(left) THEN
  2773. NewBool(FALSE)
  2774. END
  2775. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2776. Error(right.position,"must not be a type");
  2777. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2778. Error(left.position,"must not be a type");
  2779. ELSIF operator = Scanner.In THEN (* left IN right *)
  2780. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2781. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2782. NewBool(il IN sr);
  2783. ELSE
  2784. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2785. left := NewConversion(left.position, left, system.longintType,NIL);
  2786. binaryExpression.SetLeft(left)
  2787. END;
  2788. type := system.booleanType;
  2789. END
  2790. ELSE
  2791. Error(binaryExpression.position, "incompatible operands");
  2792. END
  2793. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2794. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2795. Error(binaryExpression.position,"incompatible operands");
  2796. END;
  2797. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2798. ELSE Error(binaryExpression.position,"operator not defined 1")
  2799. END
  2800. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2801. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2802. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2803. Error(binaryExpression.position,"incompatible operands");
  2804. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2805. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2806. left := NewConversion(left.position, left, system.addressType, NIL);
  2807. right := NewConversion(right.position, right, system.addressType, NIL);
  2808. binaryExpression.SetLeft(left);
  2809. binaryExpression.SetRight(right);
  2810. type := system.addressType;
  2811. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2812. ConvertOperands(left, right);
  2813. binaryExpression.SetLeft(left);
  2814. binaryExpression.SetRight(right);
  2815. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2816. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2817. END;
  2818. type := system.booleanType;
  2819. ELSE
  2820. Error(binaryExpression.position,"operator not defined 3");
  2821. END
  2822. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2823. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2824. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2825. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2826. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2827. CASE operator OF
  2828. |Scanner.Equal: NewBool(strl^=strr^);
  2829. |Scanner.Unequal:NewBool(strl^#strr^);
  2830. |Scanner.Less: NewBool(strl^<strr^);
  2831. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2832. |Scanner.Greater: NewBool(strl^>strr^);
  2833. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2834. ELSE
  2835. Error(binaryExpression.position,"operator not defined 4");
  2836. END;
  2837. END;
  2838. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2839. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2840. type := system.booleanType
  2841. ELSE
  2842. Error(binaryExpression.position,"operator not defined 5");
  2843. END;
  2844. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2845. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2846. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2847. type := system.booleanType;
  2848. ELSE
  2849. Error(binaryExpression.position,"operator not defined 6");
  2850. END
  2851. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2852. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2853. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2854. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2855. type := system.booleanType
  2856. ELSE
  2857. Error(binaryExpression.position,"operator not defined for enumerators");
  2858. END;
  2859. ELSE
  2860. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2861. END;
  2862. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2863. type := system.booleanType;
  2864. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2865. type := system.booleanType;
  2866. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2867. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2868. THEN
  2869. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2870. IF (leftType # rightType) THEN
  2871. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2872. ConvertOperands(left,right); (* operands must be of the same type here *)
  2873. END;
  2874. binaryExpression.SetLeft(left);
  2875. binaryExpression.SetRight(right);
  2876. leftType := left.type.resolved;
  2877. rightType := right.type.resolved;
  2878. END;
  2879. type := leftType;
  2880. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2881. Error(binaryExpression.position,"conversion failed ?");
  2882. IF VerboseErrorMessage THEN
  2883. Printout.Info("left",left);
  2884. Printout.Info("right",right);
  2885. END;
  2886. ELSIF IsIntegerType(leftType) THEN
  2887. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2888. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2889. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2890. Error(binaryExpression.position,"division by zero");
  2891. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2892. Error(binaryExpression.position,"integer division by negative number");
  2893. END;
  2894. END;
  2895. (* constant folding *)
  2896. (* bootstrap64
  2897. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2898. CASE operator OF
  2899. |Scanner.Plus: NewInteger(il+ir,left.type);
  2900. |Scanner.Minus: NewInteger(il-ir,left.type);
  2901. |Scanner.Times: NewInteger(il*ir,left.type);
  2902. |Scanner.Slash:
  2903. IF ir # 0 THEN
  2904. NewReal(il/ir, system.realType);
  2905. END;
  2906. |Scanner.Mod:
  2907. IF ir > 0 THEN
  2908. NewInteger(il MOD ir,left.type);
  2909. END;
  2910. |Scanner.Div:
  2911. IF ir > 0 THEN
  2912. NewInteger(il DIV ir,left.type);
  2913. END;
  2914. |Scanner.Equal: NewBool(il=ir);
  2915. |Scanner.Unequal:NewBool(il#ir);
  2916. |Scanner.Less: NewBool(il<ir);
  2917. |Scanner.LessEqual: NewBool(il<=ir);
  2918. |Scanner.Greater: NewBool(il>ir);
  2919. |Scanner.GreaterEqual: NewBool(il>=ir);
  2920. ELSE Error(binaryExpression.position,"operator not defined 7");
  2921. END;
  2922. ELS*)
  2923. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2924. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2925. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2926. CASE operator OF
  2927. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2928. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2929. |Scanner.Times: NewInteger(hl*hr,left.type);
  2930. |Scanner.Slash:
  2931. IF hr = 0 THEN
  2932. Error(binaryExpression.position,"division by zero");
  2933. ELSE
  2934. IF type.sizeInBits = 64 THEN
  2935. NewReal(hl/hr,system.longrealType);
  2936. ELSE
  2937. NewReal(hl/hr,system.realType)
  2938. END
  2939. END;
  2940. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2941. |Scanner.Mod:
  2942. IF hr = 0 THEN
  2943. Error(binaryExpression.position,"division by zero");
  2944. ELSE
  2945. NewInteger(hl MOD hr, left.type);
  2946. (* bootstrap64
  2947. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2948. *)
  2949. END;
  2950. |Scanner.Div:
  2951. IF hr = 0 THEN
  2952. Error(binaryExpression.position,"division by zero");
  2953. ELSE
  2954. NewInteger(hl DIV hr, left.type);
  2955. (* bootstrap64
  2956. NewInteger(Machine.DivH(hl,hr),left.type);
  2957. *)
  2958. END;
  2959. (* *)
  2960. |Scanner.Equal: NewBool(hl=hr);
  2961. |Scanner.Unequal: NewBool(hl#hr);
  2962. |Scanner.Less: NewBool(hl<hr);
  2963. |Scanner.LessEqual: NewBool(hl<=hr);
  2964. |Scanner.Greater: NewBool(hl>hr);
  2965. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2966. ELSE Error(binaryExpression.position,"operator not defined 8");
  2967. END;
  2968. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2969. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2970. type := left.type
  2971. ELSIF (operator = Scanner.Slash) THEN
  2972. left := NewConversion(left.position,left,system.realType,NIL);
  2973. right := NewConversion(right.position,right,system.realType,NIL);
  2974. binaryExpression.SetLeft(left);
  2975. binaryExpression.SetRight(right);
  2976. type := system.realType
  2977. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2978. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2979. type := system.booleanType
  2980. ELSE
  2981. Error(binaryExpression.position,"operator not defined 9");
  2982. END;
  2983. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2984. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2985. CASE operator OF
  2986. |Scanner.Plus: NewReal(rl+rr,leftType);
  2987. |Scanner.Minus: NewReal(rl-rr,leftType);
  2988. |Scanner.Times:NewReal(rl*rr,leftType);
  2989. |Scanner.Slash:
  2990. IF rr = 0 THEN
  2991. Error(binaryExpression.position,"division by zero");
  2992. ELSE
  2993. NewReal(rl/rr,leftType);
  2994. END
  2995. |Scanner.Equal: NewBool(rl=rr);
  2996. |Scanner.Unequal: NewBool(rl#rr);
  2997. |Scanner.Less: NewBool(rl<rr);
  2998. |Scanner.LessEqual: NewBool(rl<=rr);
  2999. |Scanner.Greater: NewBool(rl>rr);
  3000. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3001. ELSE Error(binaryExpression.position,"operator not defined 10");
  3002. END;
  3003. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3004. type := left.type
  3005. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3006. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3007. type := system.booleanType
  3008. ELSE
  3009. Error(binaryExpression.position,"operator not defined 11");
  3010. IF VerboseErrorMessage THEN
  3011. Printout.Info("left",left);
  3012. Printout.Info("right",right);
  3013. END;
  3014. END;
  3015. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3016. CASE operator OF
  3017. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3018. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3019. ELSE
  3020. Error(binaryExpression.position,"operator not defined");
  3021. IF VerboseErrorMessage THEN
  3022. Printout.Info("left", left);
  3023. Printout.Info("right", right)
  3024. END;
  3025. END;
  3026. IF ~error THEN
  3027. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3028. Error(binaryExpression.position,"division by zero")
  3029. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3030. (* do constant folding *)
  3031. CASE operator OF
  3032. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3033. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3034. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3035. |Scanner.Slash:
  3036. divisor := c * c + d * d;
  3037. ASSERT(divisor # 0);
  3038. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3039. |Scanner.Equal: NewBool((a = c) & (b = d))
  3040. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3041. END
  3042. END
  3043. END
  3044. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3045. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3046. CASE operator OF
  3047. |Scanner.And: NewBool(bl & br);
  3048. |Scanner.Or: NewBool(bl OR br);
  3049. |Scanner.Equal: NewBool(bl = br);
  3050. |Scanner.Unequal: NewBool(bl # br);
  3051. ELSE Error(binaryExpression.position,"operator not defined 12");
  3052. END;
  3053. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3054. type := system.booleanType
  3055. ELSE
  3056. Error(binaryExpression.position,"operator not defined 13");
  3057. END;
  3058. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3059. (* constant folding *)
  3060. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3061. IF operator = Scanner.Equal THEN
  3062. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3063. ELSIF operator = Scanner.Unequal THEN
  3064. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3065. END;
  3066. END;
  3067. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3068. type := system.booleanType;
  3069. ELSE
  3070. Error(binaryExpression.position, "operator not defined");
  3071. END;
  3072. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3073. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3074. CASE operator OF
  3075. |Scanner.Plus: NewSet(sl + sr);
  3076. |Scanner.Minus: NewSet(sl - sr);
  3077. |Scanner.Times: NewSet(sl * sr);
  3078. |Scanner.Slash: NewSet(sl / sr);
  3079. |Scanner.Equal: NewBool(sl=sr);
  3080. |Scanner.Unequal: NewBool(sl#sr);
  3081. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3082. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3083. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3084. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3085. ELSE Error(binaryExpression.position,"operator not defined 14");
  3086. END;
  3087. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3088. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3089. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3090. THEN
  3091. type := system.booleanType
  3092. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3093. type := left.type
  3094. ELSE
  3095. Error(binaryExpression.position,"operator not defined 15");
  3096. END;
  3097. ELSIF IsCharacterType(left.type) THEN
  3098. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3099. CASE operator OF
  3100. |Scanner.Equal: NewBool(cl=cr);
  3101. |Scanner.Unequal: NewBool(cl#cr);
  3102. |Scanner.Less: NewBool(cl<cr);
  3103. |Scanner.LessEqual: NewBool(cl<=cr);
  3104. |Scanner.Greater: NewBool(cl>cr);
  3105. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3106. ELSE Error(binaryExpression.position,"operator not defined 16");
  3107. END;
  3108. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3109. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3110. type := system.booleanType
  3111. ELSE
  3112. Error(binaryExpression.position,"operator not defined 17");
  3113. END;
  3114. ELSE
  3115. Error(binaryExpression.position,"operator not defined 18");
  3116. END;
  3117. ELSE
  3118. Error(binaryExpression.position,"operator not defined 19");
  3119. END;
  3120. IF type = SyntaxTree.invalidType THEN
  3121. result := SyntaxTree.invalidExpression
  3122. ELSE
  3123. result.SetType(type)
  3124. END;
  3125. resolvedExpression := result
  3126. END VisitBinaryExpression;
  3127. (** resolve a range expression of the from <<first .. last BY step>>
  3128. - depending on the context different things are checked:
  3129. ArrayIndex:
  3130. - components must be integers
  3131. - replace missing lower bound with 0
  3132. - replace missing upper bound with MAX(LONGINT)
  3133. - replace missing step size with 1
  3134. SetElement:
  3135. - components must be integers
  3136. - replace missing lower bound with 0
  3137. - replace missing upper bound with MAX(SET)
  3138. - must not have step size
  3139. CaseGuard:
  3140. - components must be constant
  3141. - components must be integers or characters
  3142. - must have lower and upper bound present
  3143. - components are made compatible
  3144. - must not have step size
  3145. - if error: return invalidExpression
  3146. **)
  3147. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3148. VAR
  3149. hasError: BOOLEAN;
  3150. first, last, step: SyntaxTree.Expression;
  3151. BEGIN
  3152. hasError := FALSE;
  3153. first := x.first;
  3154. last := x.last;
  3155. step := x.step;
  3156. (* check lower bound *)
  3157. IF x.context = SyntaxTree.CaseGuard THEN
  3158. IF first = NIL THEN
  3159. Error(x.position, "missing lower bound");
  3160. hasError := TRUE
  3161. ELSE
  3162. first := ResolveExpression(first);
  3163. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3164. Error(first.position, "lower bound not integer or character");
  3165. hasError := TRUE
  3166. ELSE
  3167. IF first IS SyntaxTree.StringValue THEN
  3168. (* add conversion from string to character *)
  3169. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3170. END
  3171. END;
  3172. (* check if expression is constant *)
  3173. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3174. (* error already reported *)
  3175. hasError := TRUE
  3176. END
  3177. END
  3178. ELSE (* ArrayIndex, SetElement *)
  3179. IF first = NIL THEN
  3180. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3181. END;
  3182. first := ResolveExpression(first);
  3183. IF IsIntegerType(first.type.resolved) THEN
  3184. first := NewConversion(first.position, first, system.longintType, NIL)
  3185. ELSE
  3186. Error(first.position, "lower bound not integer");
  3187. hasError := TRUE
  3188. END
  3189. END;
  3190. (* check upper bound *)
  3191. IF x.context = SyntaxTree.CaseGuard THEN
  3192. IF last = NIL THEN
  3193. Error(x.position, "missing upper bound");
  3194. hasError := TRUE
  3195. ELSE
  3196. last := ResolveExpression(last);
  3197. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3198. Error(last.position, "lower bound not integer or character");
  3199. hasError := TRUE
  3200. ELSE
  3201. IF last IS SyntaxTree.StringValue THEN
  3202. (* add conversion from string to character *)
  3203. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3204. END
  3205. END;
  3206. (* check if expression is constant *)
  3207. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3208. (* error already reported *)
  3209. hasError := TRUE
  3210. ELSE
  3211. (* try to make lower and upper bound compatible *)
  3212. ConvertOperands(first, last);
  3213. IF first.type.resolved # last.type.resolved THEN
  3214. Error(x.position, "lower and upper bounds incompatible");
  3215. hasError := TRUE
  3216. END
  3217. END
  3218. END
  3219. ELSE (* ArrayIndex, SetElement *)
  3220. IF last = NIL THEN
  3221. IF x.context = SyntaxTree.ArrayIndex THEN
  3222. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3223. ELSE
  3224. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3225. END
  3226. END;
  3227. last := ResolveExpression(last);
  3228. IF IsIntegerType(last.type.resolved) THEN
  3229. last := NewConversion(last.position, last, system.longintType, NIL)
  3230. ELSE
  3231. Error(last.position, "upper bound not integer");
  3232. hasError := TRUE
  3233. END
  3234. END;
  3235. (* check step size *)
  3236. IF x.context = SyntaxTree.ArrayIndex THEN
  3237. IF step = NIL THEN
  3238. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3239. END;
  3240. step := ResolveExpression(step);
  3241. IF IsIntegerType(step.type.resolved) THEN
  3242. step := NewConversion(step.position, step, system.longintType, NIL)
  3243. ELSE
  3244. Error(step.position, "step size not integer");
  3245. hasError := TRUE
  3246. END
  3247. ELSE (* SetElement, CaseGuard *)
  3248. IF step # NIL THEN
  3249. Error(last.position, "step size not allowed in this context");
  3250. hasError := TRUE
  3251. END
  3252. END;
  3253. IF hasError THEN
  3254. resolvedExpression := SyntaxTree.invalidExpression
  3255. ELSE
  3256. x.SetFirst(first);
  3257. x.SetLast(last);
  3258. x.SetStep(step);
  3259. x.SetType(system.rangeType);
  3260. resolvedExpression := x;
  3261. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3262. END
  3263. END VisitRangeExpression;
  3264. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3265. BEGIN
  3266. x.SetType(NIL);
  3267. resolvedExpression := x;
  3268. END VisitTensorRangeExpression;
  3269. (** resolve the expression d and return result as designator
  3270. - resolve expression
  3271. - if expression is a designator then return designator else error message and return invalidDesignator
  3272. **)
  3273. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3274. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3275. BEGIN
  3276. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3277. resolved := ResolveExpression(d);
  3278. IF resolved = SyntaxTree.invalidExpression THEN
  3279. (* error should already have been reported *)
  3280. result := SyntaxTree.invalidDesignator;
  3281. ELSIF resolved IS SyntaxTree.Designator THEN
  3282. result := resolved(SyntaxTree.Designator);
  3283. ELSE
  3284. Error(d.position,"is no designator ! ");
  3285. result := SyntaxTree.invalidDesignator;
  3286. END;
  3287. (* result.type might be nil. *)
  3288. RETURN result
  3289. END ResolveDesignator;
  3290. (**
  3291. symbol designator generated in this module
  3292. nothing to be resolved
  3293. **)
  3294. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3295. BEGIN
  3296. resolvedExpression := x;
  3297. END VisitSymbolDesignator;
  3298. (**
  3299. self designator generated in this module
  3300. nothing to be resolved
  3301. **)
  3302. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3303. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3304. BEGIN
  3305. (* check if in record scope *)
  3306. scope := currentScope;
  3307. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3308. scope := scope.outerScope;
  3309. END;
  3310. IF scope = NIL THEN (* in module scope *)
  3311. x.SetType(system.anyType);
  3312. ELSIF scope IS SyntaxTree.CellScope THEN
  3313. cell := scope(SyntaxTree.CellScope).ownerCell;
  3314. x.SetType(cell);
  3315. ELSE (* in record scope *)
  3316. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3317. IF (record # NIL) & (record.pointerType # NIL) THEN
  3318. type := ResolveType(record.pointerType);
  3319. x.SetType(type);
  3320. ELSE
  3321. x.SetType(record);
  3322. END;
  3323. END;
  3324. resolvedExpression := x;
  3325. END VisitSelfDesignator;
  3326. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3327. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3328. BEGIN
  3329. scope := currentScope;
  3330. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3331. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3332. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3333. returnType := procedureType.returnType;
  3334. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3335. THEN
  3336. x.SetType(returnType);
  3337. ELSE
  3338. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3339. x.SetType(SyntaxTree.invalidType);
  3340. END;
  3341. ELSE
  3342. Error(x.position,"forbidden access to result designator");
  3343. x.SetType(SyntaxTree.invalidType);
  3344. END;
  3345. x.SetAssignable(TRUE);
  3346. resolvedExpression := x;
  3347. END VisitResultDesignator;
  3348. (**
  3349. return symbol designator as an expression
  3350. - if symbol is a constant then return the constant value expression
  3351. - else
  3352. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3353. - if symbol is a guarded variable then return a TypeGuardDesignator
  3354. - else return a symbol designator
  3355. **)
  3356. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3357. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3358. guardType: SyntaxTree.Type;
  3359. BEGIN
  3360. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3361. result := SyntaxTree.invalidExpression;
  3362. ASSERT(symbol # NIL);
  3363. (*
  3364. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3365. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3366. Error(position, "type not allowed here");
  3367. ELS *)
  3368. (* not needed any more as values are stored in the expression
  3369. IF symbol IS SyntaxTree.Constant THEN
  3370. result := symbol(SyntaxTree.Constant).value
  3371. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3372. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3373. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3374. ELSE
  3375. result := symbol(SyntaxTree.Constant).value
  3376. END;
  3377. ELSE
  3378. *)
  3379. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3380. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3381. THEN
  3382. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3383. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3384. left := NewDereferenceDesignator(position,left);
  3385. left.SetHidden(TRUE);
  3386. END;
  3387. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3388. scope := currentScope;
  3389. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3390. scope := scope.outerScope;
  3391. END;
  3392. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3393. Error(position, "forbidden access to symbol in parent procedure scope");
  3394. END;
  3395. END;
  3396. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3397. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3398. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3399. ELSE
  3400. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3401. END;
  3402. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3403. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3404. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3405. result.SetType(symbol.type);
  3406. result.SetAssignable(assignable);
  3407. symbol.MarkUsed;
  3408. IF symbol IS SyntaxTree.Constant THEN
  3409. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3410. END;
  3411. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3412. variableAccessed := TRUE
  3413. END;
  3414. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3415. IF GetGuard(symbol,guardType) THEN
  3416. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3417. END;
  3418. END;
  3419. ASSERT(result.type # NIL);
  3420. RETURN result
  3421. END NewSymbolDesignator;
  3422. (** check and resolve an identifier designator "identifier"
  3423. - if identifier = self then return SelfDesignator
  3424. - else find symbol in current scope
  3425. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3426. **)
  3427. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3428. VAR symbol: SyntaxTree.Symbol;
  3429. BEGIN
  3430. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3431. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3432. IF symbol # NIL THEN
  3433. ResolveSymbol(symbol);
  3434. ASSERT(symbol.type # NIL);
  3435. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3436. ELSE
  3437. Error(identifierDesignator.position,"Undeclared Identifier");
  3438. IF VerboseErrorMessage THEN
  3439. Printout.Info("undeclared identifier designator",identifierDesignator);
  3440. END;
  3441. resolvedExpression := SyntaxTree.invalidDesignator;
  3442. END;
  3443. END VisitIdentifierDesignator;
  3444. (** check and resolve a selector designator of the form left.designator
  3445. - if left is a pointer type then do auto dereferenciation
  3446. - left denotes a search scope:
  3447. - if left type is import type then set search scope to respective module
  3448. - if left type is enumeration type then set search scope to respective enumeration scope
  3449. - elsif left type is record type then set search scope to record scope
  3450. - search symbol in computed scope
  3451. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3452. **)
  3453. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3454. VAR
  3455. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3456. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3457. BEGIN
  3458. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3459. left := ResolveDesignator(selectorDesignator.left);
  3460. result := SyntaxTree.invalidDesignator;
  3461. IF left # NIL THEN
  3462. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3463. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3464. END;
  3465. scope := NIL;
  3466. IF left.type = NIL THEN
  3467. Error(selectorDesignator.position,"field on nil typed designator");
  3468. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3469. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3470. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3471. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3472. module := symbol(SyntaxTree.Import).module;
  3473. IF module # NIL THEN
  3474. scope := module.moduleScope
  3475. ELSE
  3476. Error(left.position,"module not loaded");
  3477. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3478. END;
  3479. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3480. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3481. ASSERT(scope # NIL)
  3482. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3483. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3484. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3485. IF type IS SyntaxTree.EnumerationType THEN
  3486. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3487. ELSE
  3488. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3489. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3490. END;
  3491. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3492. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3493. ELSE
  3494. Error(selectorDesignator.position,"field on non-record type designator");
  3495. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3496. END;
  3497. symbol := NIL;
  3498. IF scope # NIL THEN
  3499. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3500. IF symbol # NIL THEN
  3501. ResolveSymbol(symbol);
  3502. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3503. symbol.MarkUsed
  3504. ELSE
  3505. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3506. IF VerboseErrorMessage THEN
  3507. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3508. Printout.Info("scope", scope);
  3509. Printout.Info("left", left);
  3510. Printout.Info("undeclared identifier",selectorDesignator);
  3511. Printout.Info("left resolved designator",left);
  3512. END
  3513. END;
  3514. END;
  3515. END;
  3516. resolvedExpression := result;
  3517. END VisitSelectorDesignator;
  3518. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3519. VAR len,idx: LONGINT;
  3520. BEGIN
  3521. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3522. IF idx < 0 THEN
  3523. Error(index.position,"index out of bounds (too small)")
  3524. ELSE
  3525. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3526. Error(index.position,"index out of bounds (too large)");
  3527. END;
  3528. END;
  3529. END;
  3530. END IndexCheck;
  3531. (*
  3532. - if index designator has not type, use newBaseType as its type
  3533. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3534. - special rule: if static array of dynamic array occurs, make it all dynamic
  3535. index designator type: new base type: new index designator type:
  3536. NIL z z
  3537. ARRAY [x, y] z ARRAY [x, y] OF z
  3538. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3539. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3540. *)
  3541. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3542. VAR
  3543. mathArrayType: SyntaxTree.MathArrayType;
  3544. makeDynamic: BOOLEAN;
  3545. BEGIN
  3546. IF indexDesignator.type = NIL THEN
  3547. indexDesignator.SetType(newBaseType)
  3548. ELSE
  3549. (* index designator must be a of math array type *)
  3550. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3551. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3552. (* determine if all arrays have to be made dynamic *)
  3553. makeDynamic :=
  3554. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3555. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3556. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3557. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3558. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3559. END;
  3560. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3561. mathArrayType.SetArrayBase(newBaseType)
  3562. END
  3563. END SetIndexBaseType;
  3564. (** check and append index list element to index designator of math array
  3565. - check validity of single index or array range
  3566. - compute new type
  3567. - if range then create new array type (calculate length of resulting array)
  3568. - otherwise take sourceArray.arrayBase as new type
  3569. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3570. **)
  3571. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3572. VAR
  3573. targetArray: SyntaxTree.MathArrayType;
  3574. first, last, step: SyntaxTree.Expression;
  3575. firstValue, lastValue, stepValue, length: LONGINT;
  3576. rangeExpression: SyntaxTree.RangeExpression;
  3577. BEGIN
  3578. IF indexListItem.type = SyntaxTree.invalidType THEN
  3579. (* error already handled *)
  3580. indexDesignator.parameters.AddExpression(indexListItem)
  3581. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3582. indexDesignator.HasRange;
  3583. indexDesignator.HasTensorRange;
  3584. indexDesignator.parameters.AddExpression(indexListItem);
  3585. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3586. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3587. IndexCheck(indexListItem, sourceArray.length);
  3588. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3589. indexDesignator.parameters.AddExpression(indexListItem)
  3590. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3591. indexDesignator.HasRange;
  3592. (* if the range is given as an array range expression, check the validity of its components *)
  3593. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3594. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3595. first := rangeExpression.first;
  3596. last := rangeExpression.last;
  3597. step := rangeExpression.step;
  3598. (* perform static checks on range components *)
  3599. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3600. Error(indexListItem.position,"lower bound of array range too small")
  3601. END;
  3602. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3603. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3604. Error(indexListItem.position,"upper bound of array range too large")
  3605. END
  3606. END;
  3607. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3608. Error(indexListItem.position,"invalid step size")
  3609. END;
  3610. (* add conversions to size type *)
  3611. (* TODO: needed? *)
  3612. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3613. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3614. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3615. END;
  3616. IF indexDesignator.hasTensorRange THEN
  3617. (* the index designator's base type is a tensor: leave it as is *)
  3618. ELSE
  3619. (* append a new math array to the index designator's base type *)
  3620. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3621. IF ~error THEN
  3622. (*
  3623. (* optimization: calculate length of target array for static ranges *)
  3624. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3625. IF IsStaticallyOpenRange(rangeExpression) THEN
  3626. (* range is open ('*'): reuse source array length as target array length *)
  3627. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3628. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3629. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3630. IF lastValue = MAX(LONGINT) THEN
  3631. IF IsIntegerValue(sourceArray.length, length) THEN
  3632. lastValue := length - 1;
  3633. isStaticTargetArrayLength := TRUE
  3634. ELSE
  3635. isStaticTargetArrayLength := FALSE
  3636. END
  3637. ELSE
  3638. isStaticTargetArrayLength := TRUE
  3639. END;
  3640. IF isStaticTargetArrayLength THEN
  3641. (* calculate static target array length *)
  3642. IF firstValue > lastValue THEN
  3643. length := 0
  3644. ELSE
  3645. length := 1 + lastValue - firstValue;
  3646. IF length MOD stepValue = 0 THEN
  3647. length := length DIV stepValue
  3648. ELSE
  3649. length := length DIV stepValue + 1
  3650. END
  3651. END;
  3652. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3653. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3654. ASSERT(targetArray.form = SyntaxTree.Static)
  3655. END
  3656. END
  3657. END
  3658. *)
  3659. END;
  3660. SetIndexBaseType(indexDesignator, targetArray)
  3661. END;
  3662. indexDesignator.parameters.AddExpression(indexListItem)
  3663. ELSE
  3664. Error(position,"invalid index list item");
  3665. END;
  3666. END AppendMathIndex;
  3667. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3668. VAR parameters: SyntaxTree.ExpressionList;
  3669. BEGIN
  3670. parameters := index.parameters;
  3671. IF (expression.type = NIL) THEN
  3672. Error(position, "invalid index");
  3673. ELSIF IsIntegerType(expression.type.resolved) THEN
  3674. IF over IS SyntaxTree.ArrayType THEN
  3675. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3676. ELSIF over IS SyntaxTree.StringType THEN
  3677. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3678. END;
  3679. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3680. parameters.AddExpression(expression);
  3681. ELSE
  3682. Error(position, "invalid index");
  3683. END;
  3684. END AppendIndex;
  3685. (** convert an expression to math array type
  3686. if expression is of math array type: return expression itself
  3687. if expression is of array-structured object type: return an index operator call on it
  3688. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3689. otherwise: return invalid expression
  3690. **)
  3691. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3692. VAR
  3693. result: SyntaxTree.Expression;
  3694. mathArrayType: SyntaxTree.MathArrayType;
  3695. BEGIN
  3696. IF expression.type = NIL THEN
  3697. result := SyntaxTree.invalidExpression
  3698. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3699. (* expression of math array type *)
  3700. result := expression
  3701. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3702. (* expression of array-structured object type *)
  3703. mathArrayType := MathArrayStructureOfType(expression.type);
  3704. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3705. ELSE
  3706. result := SyntaxTree.invalidExpression
  3707. END;
  3708. RETURN result
  3709. END ConvertToMathArray;
  3710. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3711. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3712. VAR
  3713. result: SyntaxTree.ExpressionList;
  3714. i: LONGINT;
  3715. BEGIN
  3716. result := SyntaxTree.NewExpressionList();
  3717. FOR i := 1 TO itemCount DO
  3718. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3719. END;
  3720. RETURN result
  3721. END ListOfOpenRanges;
  3722. (** create a procedure call designator for an index operator call on an array-structured object type
  3723. - use given index list as actual parameters
  3724. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3725. **)
  3726. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3727. VAR
  3728. operator: SyntaxTree.Operator;
  3729. expression: SyntaxTree.Expression;
  3730. actualParameters, tempList: SyntaxTree.ExpressionList;
  3731. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3732. result, tempDesignator: SyntaxTree.Designator;
  3733. recordType: SyntaxTree.RecordType;
  3734. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3735. i, hashValue, indexListSize, indexListKind: LONGINT;
  3736. castReturnType: SyntaxTree.MathArrayType;
  3737. BEGIN
  3738. ASSERT(IsArrayStructuredObjectType(left.type));
  3739. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3740. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3741. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3742. indexListSize := indexList.Length();
  3743. indexListKind := 0;
  3744. containsNonRange := FALSE;
  3745. FOR i := 0 TO indexList.Length() - 1 DO
  3746. indexListKind := indexListKind * 2;
  3747. expression := indexList.GetExpression(i);
  3748. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3749. INC(indexListKind)
  3750. ELSE
  3751. containsNonRange := TRUE
  3752. END
  3753. END;
  3754. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3755. (* select applicable index operator
  3756. - try to look up optimal index operator
  3757. - if not present, use operator on ranges
  3758. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3759. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3760. *)
  3761. usesGeneralOperator := FALSE;
  3762. IF rhs # NIL THEN
  3763. (* write operator *)
  3764. IF hashValue = -1 THEN
  3765. operator := NIL
  3766. ELSE
  3767. operator := recordType.arrayAccessOperators.write[hashValue];
  3768. END;
  3769. IF operator = NIL THEN
  3770. usesPureRangeOperator := TRUE;
  3771. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3772. operator := recordType.arrayAccessOperators.generalWrite;
  3773. usesGeneralOperator := TRUE
  3774. ELSE
  3775. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3776. operator := recordType.arrayAccessOperators.write[hashValue];
  3777. END
  3778. END
  3779. ELSE
  3780. (* read operator *)
  3781. IF hashValue = -1 THEN
  3782. operator := NIL
  3783. ELSE
  3784. operator := recordType.arrayAccessOperators.read[hashValue];
  3785. END;
  3786. IF operator = NIL THEN
  3787. usesPureRangeOperator := TRUE;
  3788. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3789. operator := recordType.arrayAccessOperators.generalRead;
  3790. usesGeneralOperator := TRUE
  3791. ELSE
  3792. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3793. operator := recordType.arrayAccessOperators.read[hashValue];
  3794. END
  3795. END
  3796. END;
  3797. IF operator = NIL THEN
  3798. Error(position, "call of undeclared [] operator");
  3799. result := SyntaxTree.invalidDesignator;
  3800. ELSE
  3801. (* determine if reshaping is needed *)
  3802. needsReshaping := containsNonRange & usesPureRangeOperator;
  3803. (* import OCArrayBase if reshaping is needed *)
  3804. IF needsReshaping & ~arrayBaseImported THEN
  3805. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3806. arrayBaseImported := TRUE
  3807. END;
  3808. (* add the index list item to the list of actual parameters
  3809. - for general operators: add a single inline array containing the index list items as parameter
  3810. - otherwise: add all index list items as individual parameters
  3811. *)
  3812. actualParameters := SyntaxTree.NewExpressionList();
  3813. IF usesGeneralOperator THEN
  3814. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3815. END;
  3816. FOR i := 0 TO indexListSize - 1 DO
  3817. expression := indexList.GetExpression(i);
  3818. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3819. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3820. tempList := SyntaxTree.NewExpressionList();
  3821. tempList.AddExpression(expression);
  3822. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3823. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3824. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3825. END;
  3826. IF usesGeneralOperator THEN
  3827. tempMathArrayExpression.elements.AddExpression(expression);
  3828. ELSE
  3829. actualParameters.AddExpression(expression)
  3830. END
  3831. END;
  3832. IF usesGeneralOperator THEN
  3833. actualParameters.AddExpression(tempMathArrayExpression)
  3834. END;
  3835. IF rhs # NIL THEN
  3836. (* add actual parameter for RHS *)
  3837. IF needsReshaping THEN
  3838. (* reshape using OCArrayBase.ExpandDimensions *)
  3839. tempList := SyntaxTree.NewExpressionList();
  3840. (* source array *)
  3841. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3842. tempList.AddExpression(rhs);
  3843. ELSE
  3844. (* convert scalar to one-dimensional array *)
  3845. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3846. tempMathArrayExpression.elements.AddExpression(rhs);
  3847. tempList.AddExpression(tempMathArrayExpression)
  3848. END;
  3849. (* list of kept dimensions *)
  3850. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3851. FOR i := 0 TO indexListSize - 1 DO
  3852. expression := indexList.GetExpression(i);
  3853. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3854. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3855. ELSE
  3856. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3857. END
  3858. END;
  3859. tempList.AddExpression(tempMathArrayExpression);
  3860. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3861. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3862. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3863. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3864. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3865. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3866. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3867. expression.SetType(castReturnType);
  3868. ELSE
  3869. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3870. END;
  3871. actualParameters.AddExpression(expression)
  3872. ELSE
  3873. actualParameters.AddExpression(rhs)
  3874. END
  3875. END;
  3876. (* add dereference operator and create procedure call designator *)
  3877. ASSERT(left IS SyntaxTree.Designator);
  3878. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3879. ASSERT(expression IS SyntaxTree.Designator);
  3880. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3881. IF (rhs = NIL) & needsReshaping THEN
  3882. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3883. tempList := SyntaxTree.NewExpressionList();
  3884. FOR i := 0 TO indexList.Length() - 1 DO
  3885. expression := indexList.GetExpression(i);
  3886. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3887. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3888. ELSE
  3889. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3890. END
  3891. END;
  3892. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3893. END;
  3894. IF rhs = NIL THEN
  3895. (* special rule: index read operator calls are considered to be assignable *)
  3896. result.SetAssignable(TRUE)
  3897. END;
  3898. (* put information about this index operator call into the resulting designator *)
  3899. result.SetRelatedAsot(left);
  3900. result.SetRelatedIndexList(indexList)
  3901. END;
  3902. RETURN result
  3903. END NewIndexOperatorCall;
  3904. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3905. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3906. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3907. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3908. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3909. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3910. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3911. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3912. CONST trace = FALSE;
  3913. BEGIN
  3914. IF trace THEN
  3915. FOR i := 0 TO actualParameters.Length()-1 DO
  3916. Printout.Info("par", actualParameters.GetExpression(i));
  3917. END;
  3918. END;
  3919. operator := scope.firstOperator;
  3920. WHILE(operator # NIL) DO
  3921. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3922. procedureType := operator.type(SyntaxTree.ProcedureType);
  3923. distance := Distance(system, procedureType,actualParameters);
  3924. IF trace THEN Printout.Info("check op ",operator) END;
  3925. IF distance < bestDistance THEN
  3926. IF trace THEN Printout.Info("taken op",operator) END;
  3927. bestDistance := distance;
  3928. bestOperator := operator;
  3929. END;
  3930. END;
  3931. operator := operator.nextOperator;
  3932. END;
  3933. END FindInScope;
  3934. BEGIN
  3935. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3936. IF oper = 0 THEN (* index *)
  3937. identifier := SyntaxTree.NewIdentifier("[]");
  3938. ELSE
  3939. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3940. END;
  3941. WHILE (recordType # NIL) DO
  3942. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3943. recordType := recordType.GetBaseRecord();
  3944. END;
  3945. RETURN bestOperator
  3946. END FindOperator;
  3947. BEGIN
  3948. type := left.type.resolved;
  3949. IF type IS SyntaxTree.RecordType THEN
  3950. pointer := FALSE;
  3951. recordType := type(SyntaxTree.RecordType);
  3952. ELSE
  3953. pointer := TRUE;
  3954. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3955. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3956. END;
  3957. actualParameters := SyntaxTree.NewExpressionList();
  3958. IF parameters # NIL THEN
  3959. FOR i := 0 TO parameters.Length()-1 DO
  3960. expression := ResolveExpression(parameters.GetExpression(i));
  3961. actualParameters.AddExpression(expression);
  3962. END;
  3963. END;
  3964. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3965. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3966. IF op # NIL THEN
  3967. designator := left(SyntaxTree.Designator);
  3968. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3969. expression := NewSymbolDesignator(position, designator , op);
  3970. ASSERT(expression IS SyntaxTree.Designator);
  3971. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3972. result.SetRelatedAsot(left);
  3973. result.SetRelatedIndexList(parameters);
  3974. (* check if write operator exists, for var parameters *)
  3975. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3976. actualParameters := SyntaxTree.NewExpressionList();
  3977. FOR i := 0 TO parameters.Length()-1 DO
  3978. expression := ResolveExpression(parameters.GetExpression(i));
  3979. actualParameters.AddExpression(expression);
  3980. END;
  3981. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3982. actualParameters.AddExpression(rhs);
  3983. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3984. IF op = NIL THEN rhs := NIL END;
  3985. END;
  3986. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3987. ELSE
  3988. result := NIL;
  3989. END;
  3990. RETURN result;
  3991. END NewObjectOperatorCall;
  3992. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3993. 1. convert bracket designator chains into a single one that contains separators
  3994. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3995. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3996. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3997. - if an array or math array is indexed over, create index designator
  3998. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3999. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4000. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4001. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4002. - if an array-structured object type is indexed over, create procedure call designator
  4003. e.g.: a[x, y] -> a^."[]"(x, y)
  4004. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4005. - a[i, *] = a[i][*]
  4006. - a[*, i] # a[*][i]
  4007. Because:
  4008. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4009. - 'i-th column' = a[*, i]
  4010. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4011. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4012. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4013. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4014. **)
  4015. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  4016. VAR
  4017. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4018. indexDesignator: SyntaxTree.IndexDesignator;
  4019. designator: SyntaxTree.Designator;
  4020. type: SyntaxTree.Type;
  4021. recordType: SyntaxTree.RecordType;
  4022. expression, rhs: SyntaxTree.Expression;
  4023. indexList: SyntaxTree.ExpressionList;
  4024. i: LONGINT;
  4025. hasError, done: BOOLEAN;
  4026. PROCEDURE FinalizeIndexDesignator;
  4027. BEGIN
  4028. IF indexDesignator # NIL THEN
  4029. (* the end of a tensor has been reached: *)
  4030. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4031. SetIndexBaseType(indexDesignator, type);
  4032. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4033. designator := indexDesignator;
  4034. type := designator.type.resolved;
  4035. indexDesignator := NIL;
  4036. ASSERT(SyntaxTree.Resolved IN type.state)
  4037. END
  4038. END FinalizeIndexDesignator;
  4039. BEGIN
  4040. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4041. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4042. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4043. (* copy all index list entries including a separator to the left bracket designator *)
  4044. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4045. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4046. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4047. END;
  4048. (* propagate the related RHS *)
  4049. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4050. (* only resolve left bracket designator and use as final result *)
  4051. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4052. ELSE
  4053. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4054. designator := ResolveDesignator(bracketDesignator.left);
  4055. type := designator.type.resolved;
  4056. indexDesignator := NIL;
  4057. (*!!! clean up *)
  4058. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4059. OR (type IS SyntaxTree.RecordType)
  4060. THEN
  4061. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4062. IF resolvedExpression = NIL THEN
  4063. Error(bracketDesignator.position,"undefined operator");
  4064. resolvedExpression := SyntaxTree.invalidDesignator
  4065. END;
  4066. RETURN;
  4067. END;
  4068. i := 0;
  4069. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4070. expression := bracketDesignator.parameters.GetExpression(i);
  4071. expression := ResolveExpression(expression);
  4072. bracketDesignator.parameters.SetExpression(i, expression);
  4073. IF expression = SyntaxTree.indexListSeparator THEN
  4074. (* finalize an existing index designator if needed *)
  4075. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4076. INC(i)
  4077. ELSE
  4078. (* do auto-dereferencing if needed *)
  4079. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4080. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4081. & (i=0)*)
  4082. THEN
  4083. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4084. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4085. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4086. designator := SyntaxTree.invalidDesignator;
  4087. type := SyntaxTree.invalidType
  4088. ELSE
  4089. FinalizeIndexDesignator;
  4090. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4091. type := designator.type.resolved
  4092. END
  4093. END;
  4094. (* create a new index designator, if needed *)
  4095. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4096. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4097. indexDesignator.SetAssignable(designator.assignable);
  4098. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4099. (* designator := indexDesignator *)
  4100. END;
  4101. IF type = SyntaxTree.invalidType THEN
  4102. (* error already handled *)
  4103. INC(i)
  4104. ELSIF type IS SyntaxTree.ArrayType THEN
  4105. (* indexing over an array *)
  4106. ASSERT(indexDesignator # NIL);
  4107. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4108. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4109. INC(i)
  4110. ELSIF type IS SyntaxTree.StringType THEN
  4111. (* indexing over an array *)
  4112. ASSERT(indexDesignator # NIL);
  4113. AppendIndex(expression.position, indexDesignator, expression, type);
  4114. type := type(SyntaxTree.StringType).baseType.resolved;
  4115. INC(i)
  4116. ELSIF type IS SyntaxTree.MathArrayType THEN
  4117. (* indexing over a math array *)
  4118. ASSERT(indexDesignator # NIL);
  4119. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4120. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4121. INC(i)
  4122. ELSIF IsArrayStructuredObjectType(type) THEN
  4123. (* indexing over ASOTs *)
  4124. FinalizeIndexDesignator;
  4125. ASSERT(type IS SyntaxTree.PointerType);
  4126. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4127. (*
  4128. - collect index list items from bracket designator that belong to ASOT
  4129. - check for errors
  4130. *)
  4131. indexList := SyntaxTree.NewExpressionList();
  4132. hasError := FALSE;
  4133. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4134. (* indexing over tensor ASOT:
  4135. - stop at index list end or separator
  4136. - dimensionality is given by number of index list items
  4137. *)
  4138. done := FALSE;
  4139. WHILE ~done DO
  4140. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4141. done := TRUE;
  4142. ELSE
  4143. expression := bracketDesignator.parameters.GetExpression(i);
  4144. IF expression = SyntaxTree.indexListSeparator THEN
  4145. done := TRUE;
  4146. ELSE
  4147. expression := ResolveExpression(expression);
  4148. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4149. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4150. hasError := TRUE
  4151. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4152. Error(expression.position, "integer or range expected");
  4153. expression := SyntaxTree.invalidExpression;
  4154. hasError := TRUE
  4155. END;
  4156. indexList.AddExpression(expression)
  4157. END;
  4158. INC(i)
  4159. END
  4160. END
  4161. ELSE
  4162. (* indexing over non-tensor ASOT:
  4163. - ignore separators
  4164. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4165. *)
  4166. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4167. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4168. expression := bracketDesignator.parameters.GetExpression(i);
  4169. ELSE
  4170. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4171. END;
  4172. IF expression # SyntaxTree.indexListSeparator THEN
  4173. expression := ResolveExpression(expression);
  4174. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4175. Error(expression.position, "integer or range expected");
  4176. expression := SyntaxTree.invalidExpression;
  4177. hasError := TRUE
  4178. END;
  4179. indexList.AddExpression(expression)
  4180. END;
  4181. INC(i)
  4182. END;
  4183. END;
  4184. IF hasError THEN
  4185. designator := SyntaxTree.invalidDesignator;
  4186. type := SyntaxTree.invalidType;
  4187. ELSE
  4188. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4189. and the last entry in the index list belongs to the array-structured object type in question.
  4190. E.g.: for a 2-dimensional array-structured object type:
  4191. - 'lhs := asot[1, 2]' -> read mode
  4192. - 'asot[1, 2] := rhs' -> write mode
  4193. - 'asot[1, 2, 3] := rhs' -> read mode
  4194. *)
  4195. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4196. rhs := bracketDesignator.relatedRhs
  4197. ELSE
  4198. rhs := NIL
  4199. END;
  4200. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4201. type := designator.type
  4202. END
  4203. ELSE
  4204. Error(expression.position,"indexing over non-array type");
  4205. designator := SyntaxTree.invalidDesignator;
  4206. type := SyntaxTree.invalidType;
  4207. INC(i)
  4208. END
  4209. END
  4210. END;
  4211. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4212. resolvedExpression := designator
  4213. END
  4214. END VisitBracketDesignator;
  4215. (** check and resolve expression list
  4216. - resolve each expression in an expression list
  4217. - returns true if and only if all statements could have successfully been resolved
  4218. **)
  4219. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4220. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4221. BEGIN
  4222. result := TRUE;
  4223. FOR i := 0 TO expressionList.Length()-1 DO
  4224. expression := ResolveExpression(expressionList.GetExpression(i));
  4225. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4226. expressionList.SetExpression(i,expression);
  4227. END;
  4228. RETURN result
  4229. END ExpressionList;
  4230. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4231. BEGIN
  4232. type := type.resolved;
  4233. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4234. RETURN TRUE
  4235. ELSIF system.CanPassInRegister # NIL THEN
  4236. RETURN system.CanPassInRegister(type);
  4237. ELSE
  4238. RETURN FALSE
  4239. END;
  4240. END CanPassInRegister;
  4241. (** return procedure call designator left(actualParameters)
  4242. - check realtime procedure call in realtime procedure
  4243. - check number of parameters
  4244. - check parameter compatibility
  4245. return invalidDesignator if error
  4246. **)
  4247. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4248. VAR result: SyntaxTree.Designator;
  4249. numberFormalParameters, numberActualParameters: LONGINT;
  4250. formalType: SyntaxTree.ProcedureType;
  4251. formalParameter: SyntaxTree.Parameter;
  4252. actualParameter: SyntaxTree.Expression;
  4253. i: LONGINT;
  4254. self: SyntaxTree.Expression;
  4255. BEGIN
  4256. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4257. result := SyntaxTree.invalidDesignator;
  4258. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4259. numberFormalParameters := formalType.numberParameters;
  4260. numberActualParameters := actualParameters.Length();
  4261. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4262. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4263. END;
  4264. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4265. self := left.left;
  4266. IF (self # NIL) & ~IsVariable(self) THEN
  4267. Error(self.position, "Non-variable expression on variable receiver");
  4268. END;
  4269. END;
  4270. IF ~ExpressionList(actualParameters) THEN
  4271. result := SyntaxTree.invalidDesignator
  4272. ELSE
  4273. IF numberActualParameters <= numberFormalParameters THEN
  4274. formalParameter := formalType.firstParameter;
  4275. FOR i := 0 TO numberActualParameters-1 DO
  4276. actualParameter := actualParameters.GetExpression(i);
  4277. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4278. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4279. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4280. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4281. ELSE
  4282. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4283. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4284. END;
  4285. actualParameters.SetExpression(i,actualParameter);
  4286. END;
  4287. formalParameter := formalParameter.nextParameter;
  4288. END;
  4289. WHILE (formalParameter # NIL) DO
  4290. IF formalParameter.defaultValue # NIL THEN
  4291. actualParameters.AddExpression(formalParameter.defaultValue);
  4292. formalParameter := formalParameter.nextParameter
  4293. ELSE
  4294. Error(position, "less actual than formal parameters");
  4295. formalParameter := NIL;
  4296. END;
  4297. END;
  4298. ELSE
  4299. Error(position, "more actual than formal parameters")
  4300. END;
  4301. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4302. result.SetAssignable(FALSE);
  4303. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4304. END;
  4305. RETURN result
  4306. END NewProcedureCallDesignator;
  4307. (**
  4308. builtin call designator generated in VisitParameterDesignator
  4309. -> nothing to be resolved
  4310. **)
  4311. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4312. BEGIN
  4313. resolvedExpression := x;
  4314. END VisitTypeGuardDesignator;
  4315. (**
  4316. builtin call designator generated in VisitParameterDesignator
  4317. -> nothing to be resolved
  4318. **)
  4319. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4320. BEGIN
  4321. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4322. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4323. ASSERT(resolvedExpression.type # NIL);
  4324. ELSIF ExpressionList(x.parameters) THEN
  4325. resolvedExpression := x;
  4326. END;
  4327. END VisitBuiltinCallDesignator;
  4328. (**
  4329. procedure call designator generated in VisitParameterDesignator
  4330. -> nothing to be resolved
  4331. **)
  4332. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4333. BEGIN
  4334. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4335. resolvedExpression := x;
  4336. END VisitProcedureCallDesignator;
  4337. (** return true if x is a variable else return false and report error **)
  4338. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4339. VAR result: BOOLEAN;
  4340. BEGIN
  4341. result := TRUE;
  4342. IF x = SyntaxTree.invalidExpression THEN
  4343. result := FALSE;
  4344. ELSIF ~IsVariable(x) THEN
  4345. Error(x.position,"non variable expression");
  4346. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4347. result := FALSE;
  4348. END;
  4349. RETURN result
  4350. END CheckVariable;
  4351. (**
  4352. if expression x is of basic type then return true else report error and return false
  4353. **)
  4354. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4355. VAR result: BOOLEAN;
  4356. BEGIN
  4357. result := FALSE;
  4358. IF x = SyntaxTree.invalidExpression THEN
  4359. ELSIF ~IsBasicType(x.type) THEN
  4360. Error(x.position,"is no basic type");
  4361. result := FALSE
  4362. ELSE result := TRUE
  4363. END;
  4364. RETURN result
  4365. END CheckBasicType;
  4366. (**
  4367. if expression x is of number type then return true else report error and return false
  4368. **)
  4369. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4370. VAR result: BOOLEAN;
  4371. BEGIN
  4372. result := FALSE;
  4373. IF x = SyntaxTree.invalidExpression THEN
  4374. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4375. Error(x.position,"is non number type");
  4376. ELSE result := TRUE
  4377. END;
  4378. RETURN result
  4379. END CheckNumberType;
  4380. (**
  4381. if expression x is of number or size type but not complex then return true else report error and return false
  4382. **)
  4383. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4384. VAR result: BOOLEAN;
  4385. BEGIN
  4386. result := FALSE;
  4387. IF x = SyntaxTree.invalidExpression THEN
  4388. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4389. Error(x.position,"is complex type");
  4390. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4391. Error(x.position,"is non number type");
  4392. ELSE result := TRUE
  4393. END;
  4394. RETURN result
  4395. END CheckNonComplexNumberSizeType;
  4396. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4397. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4398. BEGIN
  4399. result := FALSE; type := x.type.resolved;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4402. TRACE(type.sizeInBits);
  4403. TRACE(system.addressType.sizeInBits);
  4404. Error(x.position,"is no address type");
  4405. ELSE result := TRUE
  4406. END;
  4407. RETURN result
  4408. END CheckAddressType;
  4409. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4411. BEGIN
  4412. result := FALSE; type := x.type.resolved;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4415. Error(x.position,"is no size type");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckSizeType;
  4420. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4421. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4422. BEGIN
  4423. result := FALSE; type := x.type.resolved;
  4424. IF x = SyntaxTree.invalidExpression THEN
  4425. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4426. Error(x.position,"is no object type");
  4427. ELSE result := TRUE
  4428. END;
  4429. RETURN result
  4430. END CheckObjectType;
  4431. (**
  4432. if expression x is of integer type then return true else report error and return false
  4433. **)
  4434. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4435. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4436. BEGIN
  4437. result := FALSE; type := x.type.resolved;
  4438. IF x = SyntaxTree.invalidExpression THEN
  4439. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4440. Error(x.position,"is no integer type");
  4441. ELSE result := TRUE
  4442. END;
  4443. RETURN result
  4444. END CheckIntegerType;
  4445. (**
  4446. if expression x is of character type then return true else report error and return false
  4447. **)
  4448. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4449. VAR result: BOOLEAN;
  4450. BEGIN
  4451. result := FALSE;
  4452. IF x = SyntaxTree.invalidExpression THEN
  4453. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4454. Error(x.position,"is no character type");
  4455. ELSE result := TRUE
  4456. END;
  4457. RETURN result
  4458. END CheckCharacterType;
  4459. (**
  4460. if expression x is of real type then return true else report error and return false
  4461. **)
  4462. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4463. VAR result: BOOLEAN;
  4464. BEGIN
  4465. result := FALSE;
  4466. IF x = SyntaxTree.invalidExpression THEN
  4467. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4468. Error(x.position,"is no float type");
  4469. ELSE result := TRUE
  4470. END;
  4471. RETURN result
  4472. END CheckRealType;
  4473. (**
  4474. if expression x is of range type then return true else report error and return false
  4475. **)
  4476. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4477. VAR result: BOOLEAN;
  4478. BEGIN
  4479. result := FALSE;
  4480. IF x = SyntaxTree.invalidExpression THEN
  4481. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4482. Error(x.position,"is no range type");
  4483. ELSE result := TRUE
  4484. END;
  4485. RETURN result
  4486. END CheckRangeType;
  4487. (**
  4488. if expression x is of boolean type then return true else report error and return false
  4489. **)
  4490. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4496. Error(x.position,"is no boolean type");
  4497. ELSE result := TRUE
  4498. END;
  4499. RETURN result
  4500. END CheckBooleanType;
  4501. (**
  4502. if expression x is of set type then return true else report error and return false
  4503. **)
  4504. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4505. VAR result: BOOLEAN;
  4506. BEGIN
  4507. result := FALSE;
  4508. IF x = SyntaxTree.invalidExpression THEN
  4509. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4510. Error(x.position,"is no set type");
  4511. ELSE result := TRUE
  4512. END;
  4513. RETURN result
  4514. END CheckSetType;
  4515. (**
  4516. if expression x is of string or array of character type then return true else report error and return false
  4517. **)
  4518. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF ~IsStringType(x.type.resolved) THEN
  4524. Error(x.position,"is no string type");
  4525. ELSE result := TRUE
  4526. END;
  4527. RETURN result
  4528. END CheckStringType;
  4529. (**
  4530. if expression x is a type declaration type return true else report error and return false
  4531. **)
  4532. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4533. VAR result: BOOLEAN;
  4534. BEGIN
  4535. result := FALSE;
  4536. IF x = SyntaxTree.invalidExpression THEN
  4537. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4538. Error(x.position,"is not a type declaration");
  4539. ELSE result := TRUE
  4540. END;
  4541. RETURN result
  4542. END CheckTypeDeclarationType;
  4543. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4544. VAR result: BOOLEAN;
  4545. BEGIN
  4546. result := FALSE;
  4547. IF x = SyntaxTree.invalidExpression THEN
  4548. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4549. result := TRUE;
  4550. value := x.resolved(SyntaxTree.IntegerValue).value;
  4551. ELSE
  4552. Error(x.position,"expression is not an integer constant");
  4553. END;
  4554. RETURN result;
  4555. END CheckIntegerValue;
  4556. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4557. VAR result: BOOLEAN;
  4558. BEGIN
  4559. result := FALSE;
  4560. IF x = SyntaxTree.invalidExpression THEN
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4562. result := TRUE;
  4563. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4564. ELSE
  4565. Error(x.position,"expression is not an integer constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckStringValue;
  4569. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4570. BEGIN
  4571. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4572. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4573. ELSE
  4574. RETURN FALSE
  4575. END;
  4576. END IsUnsignedValue;
  4577. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4578. BEGIN
  4579. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4580. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4581. ELSE
  4582. RETURN FALSE
  4583. END
  4584. END IsAddressValue;
  4585. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4586. BEGIN
  4587. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4588. END IsAddressExpression;
  4589. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4590. BEGIN
  4591. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4592. END IsSizeExpression;
  4593. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4594. VAR result: BOOLEAN;
  4595. BEGIN
  4596. result := FALSE;
  4597. IF x = SyntaxTree.invalidExpression THEN
  4598. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4599. result := TRUE;
  4600. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4601. ELSE
  4602. Error(x.position,"expression is not an integer constant");
  4603. END;
  4604. RETURN result;
  4605. END CheckEnumerationValue;
  4606. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4607. VAR result: BOOLEAN;
  4608. BEGIN
  4609. result := FALSE;
  4610. IF x = SyntaxTree.invalidExpression THEN
  4611. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4612. result := TRUE;
  4613. value := x.resolved(SyntaxTree.CharacterValue).value;
  4614. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4615. result := TRUE;
  4616. value := x.resolved(SyntaxTree.StringValue).value[0];
  4617. ELSE
  4618. Error(x.position,"expression is not a character constant");
  4619. END;
  4620. RETURN result;
  4621. END CheckCharacterValue;
  4622. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4623. VAR result: BOOLEAN;
  4624. BEGIN
  4625. result := FALSE;
  4626. IF x = SyntaxTree.invalidExpression THEN
  4627. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4628. value := x.resolved(SyntaxTree.IntegerValue).value;
  4629. IF (value > 0) OR includeZero & (value = 0) THEN
  4630. result := TRUE;
  4631. ELSE
  4632. Error(x.position,"integer is not positive");
  4633. END
  4634. ELSE
  4635. Error(x.position,"expression is not an integer constant");
  4636. END;
  4637. RETURN result;
  4638. END CheckPositiveIntegerValue;
  4639. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4640. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4641. BEGIN
  4642. result := FALSE;
  4643. IF x = SyntaxTree.invalidExpression THEN
  4644. ELSE
  4645. type := x.type.resolved;
  4646. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4647. portType := type(SyntaxTree.PortType);
  4648. result := TRUE
  4649. ELSE
  4650. Error(x.position,"no port type");
  4651. END;
  4652. END;
  4653. RETURN result
  4654. END CheckPortType;
  4655. (* move to builtin procedure call statement ?
  4656. remove builtin procedure call designator ?
  4657. *)
  4658. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4659. VAR
  4660. numberActualParameters,numberFormalParameters: LONGINT;
  4661. formalParameter: SyntaxTree.Parameter;
  4662. actualParameter: SyntaxTree.Expression;
  4663. procedureType: SyntaxTree.ProcedureType;
  4664. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4665. inPort, outPort: SyntaxTree.PortType;
  4666. constructor: SyntaxTree.Procedure;
  4667. type0,type1,type2: SyntaxTree.Type;
  4668. type,base,parameterType: SyntaxTree.Type;
  4669. arrayType: SyntaxTree.ArrayType;
  4670. i,i0,i1: LONGINT;
  4671. r,r0,r1,im: LONGREAL;
  4672. c: CHAR;
  4673. id: LONGINT;
  4674. b: BOOLEAN;
  4675. first: LONGINT;
  4676. mathArrayType: SyntaxTree.MathArrayType;
  4677. customBuiltin: SyntaxTree.CustomBuiltin;
  4678. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4679. VAR resultB: BOOLEAN;
  4680. BEGIN
  4681. IF numberActualParameters < from THEN
  4682. Error(position, "less actual than formal parameters");
  4683. result := SyntaxTree.invalidExpression;
  4684. resultB := FALSE;
  4685. ELSIF numberActualParameters > to THEN
  4686. Error(position, "more actual than formal parameters");
  4687. result := SyntaxTree.invalidExpression;
  4688. resultB := FALSE;
  4689. ELSE
  4690. resultB := TRUE;
  4691. END;
  4692. RETURN resultB
  4693. END CheckArity;
  4694. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4695. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4696. BEGIN
  4697. WHILE modifier # NIL DO
  4698. symbol := cellType.FindProperty(modifier.identifier);
  4699. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4700. propertyType := symbol.type.resolved;
  4701. modifierType := modifier.expression.type.resolved;
  4702. IF ~CompatibleTo(system, modifierType, propertyType) &
  4703. ~(
  4704. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4705. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4706. Error(modifier.position,"incompatible to cell property");
  4707. END;
  4708. ELSE
  4709. Error(modifier.position, "undefined property");
  4710. END;
  4711. modifier := modifier.nextModifier;
  4712. END;
  4713. END CheckModifiers;
  4714. BEGIN
  4715. type := NIL; result := NIL;
  4716. type0 := NIL; type1 := NIL; type2 := NIL;
  4717. numberActualParameters := actualParameters.Length();
  4718. IF numberActualParameters>0 THEN
  4719. parameter0 := actualParameters.GetExpression(0);
  4720. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4721. Error(parameter0.position,"forbidden type-less argument");
  4722. result := SyntaxTree.invalidExpression
  4723. END
  4724. END;
  4725. IF numberActualParameters >1 THEN
  4726. parameter1 := actualParameters.GetExpression(1);
  4727. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4728. ELSE
  4729. Error(parameter1.position,"forbidden type-less argument");
  4730. result := SyntaxTree.invalidExpression
  4731. END
  4732. END;
  4733. IF numberActualParameters >2 THEN
  4734. parameter2 := actualParameters.GetExpression(2);
  4735. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4736. ELSE
  4737. Error(parameter2.position,"forbidden type-less argument");
  4738. result := SyntaxTree.invalidExpression
  4739. END
  4740. END;
  4741. IF returnType # NIL THEN
  4742. id := Global.New;
  4743. result := NIL;
  4744. ELSE
  4745. id := builtin.id;
  4746. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4747. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4748. END;
  4749. END;
  4750. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4751. ELSIF result # NIL THEN type := result.type (* operator *)
  4752. ELSE
  4753. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4754. result(SyntaxTree.Designator).SetLeft(left);
  4755. IF returnType # NIL THEN
  4756. type := returnType;
  4757. END;
  4758. (* ---- ASSERT ----- *)
  4759. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4760. IF CheckBooleanType(parameter0) THEN
  4761. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4762. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4763. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4764. *)
  4765. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4766. Error(position, "assert failed");
  4767. END;
  4768. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4769. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4770. rules imposed by the architecture / current runtime
  4771. *)
  4772. END;
  4773. END;
  4774. (* ---- COPY ----- *)
  4775. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4776. IF~IsStringType(type0) THEN
  4777. Error(parameter0.position,"no string type");
  4778. END;
  4779. IF ~IsStringType(type1) THEN
  4780. Error(parameter1.position,"no string type");
  4781. ELSIF CheckVariable(parameter1) THEN
  4782. IF (type0 IS SyntaxTree.StringType) THEN
  4783. arrayType := type1(SyntaxTree.ArrayType);
  4784. IF arrayType.form = SyntaxTree.Static THEN
  4785. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4786. Error(position, "destination length smaller than source length")
  4787. END;
  4788. END;
  4789. END;
  4790. END;
  4791. (* ---- INC, DEC----- *)
  4792. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4793. IF numberActualParameters = 1 THEN
  4794. parameter1 :=Global.NewIntegerValue(system,position,1);
  4795. actualParameters.AddExpression(parameter1);
  4796. END;
  4797. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4798. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4799. Error(position, "incompatible increment");
  4800. ELSE
  4801. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4802. actualParameters.SetExpression(1,parameter1);
  4803. END;
  4804. END;
  4805. (* ---- EXCL, INCL----- *)
  4806. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4807. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4808. IF IsIntegerValue(parameter1,i0) THEN
  4809. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4810. Error(position, "parameter out of SET range")
  4811. END;
  4812. END;
  4813. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4814. actualParameters.SetExpression(1,parameter1);
  4815. END;
  4816. (* ---- HALT, SYSTEM.HALT ----- *)
  4817. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4818. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4819. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4820. rules imposed by the architecture / current runtime
  4821. *)
  4822. END;
  4823. (* ---- WAIT ----- *)
  4824. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4825. IF CheckObjectType(parameter0) THEN
  4826. END;
  4827. (* ---- NEW ----- *)
  4828. ELSIF (id = Global.New) THEN
  4829. IF returnType # NIL THEN
  4830. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4831. ELSE
  4832. first := 1;
  4833. END;
  4834. IF CheckArity(first,Infinity) THEN
  4835. IF currentIsRealtime THEN
  4836. Error(position, "forbidden new in realtime block");
  4837. END;
  4838. (* check constructor *)
  4839. IF (first =0) OR CheckVariable(parameter0) THEN
  4840. IF type0 IS SyntaxTree.PointerType THEN
  4841. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4842. ELSIF type0 IS SyntaxTree.CellType THEN
  4843. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4844. ELSE
  4845. Error(position, "forbidden new on value type");
  4846. END;
  4847. IF type0 IS SyntaxTree.ArrayType THEN
  4848. arrayType := type0(SyntaxTree.ArrayType);
  4849. IF arrayType.form = SyntaxTree.Static THEN
  4850. i := first
  4851. ELSIF arrayType.form = SyntaxTree.Open THEN
  4852. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4853. ELSE HALT(100)
  4854. END;
  4855. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4856. i := first;
  4857. REPEAT
  4858. actualParameter := actualParameters.GetExpression(i);
  4859. IF CheckSizeType(actualParameter) THEN
  4860. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4861. actualParameters.SetExpression(i,actualParameter);
  4862. END;
  4863. INC(i);
  4864. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4865. END;
  4866. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4867. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4868. IF constructor = NIL THEN
  4869. IF CheckArity(first,first) THEN END;
  4870. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4871. Error(position, "new on object with hidden constructor");
  4872. ELSE
  4873. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4874. numberFormalParameters := procedureType.numberParameters;
  4875. IF numberActualParameters-first <= numberFormalParameters THEN
  4876. formalParameter := procedureType.firstParameter;
  4877. FOR i := first TO numberActualParameters-1 DO
  4878. actualParameter := actualParameters.GetExpression(i);
  4879. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4880. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4881. ELSE
  4882. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4883. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4884. END;
  4885. actualParameters.SetExpression(i,actualParameter);
  4886. END;
  4887. formalParameter := formalParameter.nextParameter;
  4888. END;
  4889. WHILE (formalParameter # NIL) DO
  4890. IF formalParameter.defaultValue # NIL THEN
  4891. actualParameters.AddExpression(formalParameter.defaultValue);
  4892. formalParameter := formalParameter.nextParameter
  4893. ELSE
  4894. Error(position, "less actual than formal parameters");
  4895. formalParameter := NIL;
  4896. END;
  4897. END;
  4898. ELSE
  4899. Error(position, "more actual than formal parameters")
  4900. END;
  4901. END;
  4902. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4903. mathArrayType := type0(SyntaxTree.MathArrayType);
  4904. IF mathArrayType.form = SyntaxTree.Static THEN
  4905. Error(position, "new on static array");
  4906. ELSE
  4907. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4908. i0 := first+1; i1 := Infinity;
  4909. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4910. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4911. i1 := i0;
  4912. ELSE HALT(100);
  4913. END;
  4914. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4915. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4916. base := ArrayBase(type0,MAX(LONGINT));
  4917. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4918. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4919. IF ~CompatibleTo(system,type0,parameterType) THEN
  4920. Error(parameter0.position,"incompatible parameter in new");
  4921. result := SyntaxTree.invalidExpression;
  4922. ELSE
  4923. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4924. END;
  4925. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4926. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4927. IF ~CompatibleTo(system,type1,parameterType) THEN
  4928. Error(parameter1.position,"parameter incompatible to math array of size");
  4929. result := SyntaxTree.invalidExpression;
  4930. ELSE
  4931. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4932. END;
  4933. ELSE
  4934. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4935. i := first;
  4936. REPEAT
  4937. actualParameter := actualParameters.GetExpression(i);
  4938. IF CheckSizeType(actualParameter) THEN
  4939. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4940. actualParameters.SetExpression(i,actualParameter);
  4941. END;
  4942. INC(i);
  4943. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4944. END;
  4945. END;
  4946. END;
  4947. ELSIF type0 IS SyntaxTree.CellType THEN
  4948. IF ~(currentIsCellNet) THEN
  4949. Error(position, "cell allocation outside activeCells ");
  4950. ELSE
  4951. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4952. IF (constructor = NIL) & CheckArity(1,1) THEN
  4953. (* ok *)
  4954. ELSE
  4955. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4956. numberFormalParameters := procedureType.numberParameters;
  4957. DEC(numberActualParameters);
  4958. IF numberActualParameters <= numberFormalParameters THEN
  4959. formalParameter := procedureType.firstParameter;
  4960. FOR i := first TO numberActualParameters DO
  4961. actualParameter := actualParameters.GetExpression(i);
  4962. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4963. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4964. ELSE
  4965. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4966. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4967. END;
  4968. actualParameters.SetExpression(i,actualParameter);
  4969. END;
  4970. formalParameter := formalParameter.nextParameter;
  4971. END;
  4972. WHILE (formalParameter # NIL) DO
  4973. IF formalParameter.defaultValue # NIL THEN
  4974. actualParameters.AddExpression(formalParameter.defaultValue);
  4975. formalParameter := formalParameter.nextParameter
  4976. ELSE
  4977. Error(position, "less actual than formal parameters");
  4978. formalParameter := NIL;
  4979. END;
  4980. END;
  4981. ELSE
  4982. Error(position, "more actual than formal parameters")
  4983. END;
  4984. END;
  4985. END;
  4986. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4987. activeCellsStatement := TRUE;
  4988. ELSE
  4989. Error(position, "cannot be allocated");
  4990. END;
  4991. END;
  4992. END;
  4993. (* ---- DISPOSE ----- *)
  4994. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4995. IF ~IsPointerType(parameter0.type) THEN
  4996. Error(parameter0.position,"is not a pointer")
  4997. ELSIF ~IsDisposable(parameter0.type) THEN
  4998. Error(parameter0.position,"is not disposable")
  4999. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5000. END
  5001. (* ---- GETPROCEDURE ----- *)
  5002. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5003. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5004. IF CheckVariable(parameter2) THEN
  5005. IF ~GetProcedureAllowed(parameter2.type) THEN
  5006. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5007. END;
  5008. END;
  5009. END;
  5010. (* ---- ABS ----- *)
  5011. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5012. (* note: ABS on complex numbers is done using overloading *)
  5013. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5014. type := type0;
  5015. IF IsIntegerValue(parameter0,i0) THEN
  5016. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5017. type := Global.GetIntegerType(system,ABS(i0));
  5018. ELSIF IsRealValue(parameter0,r) THEN
  5019. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5020. END;
  5021. ELSE
  5022. type := SyntaxTree.invalidType;
  5023. END;
  5024. (* ---- ASH, ASR ----- *)
  5025. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5026. type := type0;
  5027. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5028. (*
  5029. ConvertOperands(parameter0,parameter1); (* same type *)
  5030. *)
  5031. type := parameter0.type;
  5032. IF IsIntegerValue(parameter0,i0) THEN
  5033. IF IsIntegerValue(parameter1,i1) THEN
  5034. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5035. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5036. result := ResolveExpression(result);
  5037. type := Global.GetIntegerType(system,i0);
  5038. END;
  5039. END;
  5040. IF type.resolved.sizeInBits < 32 THEN
  5041. type := system.longintType;
  5042. END;
  5043. (*!compatibility with release, remove when resolved
  5044. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5045. *)
  5046. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5047. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5048. actualParameters.SetExpression(0,parameter0);
  5049. actualParameters.SetExpression(1,parameter1);
  5050. END;
  5051. (* ---- CAP ----- *)
  5052. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5053. type := system.characterType;
  5054. IF CheckCharacterType (parameter0) THEN
  5055. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5056. actualParameters.SetExpression(0,parameter0);
  5057. IF IsCharacterValue(parameter0,c) THEN
  5058. IF (c <= "z") & (c >= "a") THEN
  5059. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5060. ELSE
  5061. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5062. END;
  5063. END;
  5064. END;
  5065. (* ---- CHR ----- *)
  5066. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5067. IF id = Global.Chr THEN
  5068. type := system.characterType
  5069. ELSE
  5070. type := system.characterType32
  5071. END;
  5072. IF CheckIntegerType(parameter0) THEN
  5073. IF IsIntegerValue(parameter0,i0) THEN
  5074. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5075. result := ResolveExpression(result);
  5076. ELSE
  5077. (*
  5078. result := NewConversion(parameter0.position,parameter0,type);
  5079. *)
  5080. END;
  5081. END
  5082. (* ---- ENTIER ----- *)
  5083. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5084. type := system.longintType;
  5085. IF CheckRealType(parameter0) THEN
  5086. IF IsRealValue(parameter0,r) THEN
  5087. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5088. type := Global.GetIntegerType(system,ENTIER(r));
  5089. END
  5090. END;
  5091. (* ---- ENTIERH ----- *)
  5092. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5093. type := system.hugeintType;
  5094. IF CheckRealType(parameter0) THEN
  5095. IF IsRealValue(parameter0,r) THEN
  5096. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5097. END
  5098. END;
  5099. (* ---- LEN ----- *)
  5100. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5101. type := system.longintType;
  5102. base := type0;
  5103. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5104. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5105. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5106. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5107. Error(position, "forbidden len on unsafe pointer");
  5108. END;
  5109. type0 := base;
  5110. ELSE
  5111. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5112. type0 := parameter0.type.resolved;
  5113. actualParameters.SetExpression(0,parameter0);
  5114. base := type0;
  5115. END;
  5116. END;
  5117. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5118. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5119. IF i1 < 0 THEN
  5120. Error(position, "invalid dimension");
  5121. base := SyntaxTree.invalidType;
  5122. ELSE
  5123. base := ArrayBase(base,i1);
  5124. IF (base # NIL) & Indexable(base) THEN
  5125. ELSE
  5126. Error(position, "len on no array");
  5127. IF VerboseErrorMessage THEN
  5128. Printout.Info("base",base);
  5129. END;
  5130. base := SyntaxTree.invalidType;
  5131. END;
  5132. END;
  5133. IF numberActualParameters=2 THEN
  5134. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5135. actualParameters.SetExpression(1,parameter1);
  5136. ELSIF base IS SyntaxTree.MathArrayType THEN
  5137. Error(position, "missing dimension specification");
  5138. END;
  5139. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5140. IF base IS SyntaxTree.ArrayType THEN
  5141. arrayType := base(SyntaxTree.ArrayType);
  5142. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5143. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5144. result := Global.NewIntegerValue(system,position,i);
  5145. type := result.type;(* arrayType.length.type;*)
  5146. ASSERT(type # NIL);
  5147. END;
  5148. ELSIF base IS SyntaxTree.MathArrayType THEN
  5149. mathArrayType := base(SyntaxTree.MathArrayType);
  5150. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5151. result := Global.NewIntegerValue(system,position,i);
  5152. type := result.type;
  5153. (*
  5154. type := mathArrayType.length.type;
  5155. *)
  5156. ASSERT(type # NIL);
  5157. END;
  5158. END;
  5159. END;
  5160. ELSE
  5161. type := system.longintType;
  5162. END;
  5163. (* ---- FIRST ---- *)
  5164. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5165. type := system.longintType;
  5166. IF CheckRangeType(parameter0) THEN END;
  5167. result.SetAssignable(parameter0.assignable)
  5168. (* ---- LAST ---- *)
  5169. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5170. type := system.longintType;
  5171. IF CheckRangeType(parameter0) THEN END;
  5172. result.SetAssignable(parameter0.assignable)
  5173. (* ---- STEP ---- *)
  5174. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5175. type := system.longintType;
  5176. IF CheckRangeType(parameter0) THEN END;
  5177. result.SetAssignable(parameter0.assignable)
  5178. (* ---- RE ---- *)
  5179. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5180. IF CheckNumberType(parameter0) THEN
  5181. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5182. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5183. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5184. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5185. type := parameter0.type
  5186. ELSE
  5187. type := system.realType
  5188. END
  5189. END;
  5190. result.SetAssignable(parameter0.assignable)
  5191. (* ---- IM ---- *)
  5192. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5193. IF CheckNumberType(parameter0) THEN
  5194. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5195. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5196. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5197. ELSE
  5198. type := system.realType;
  5199. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5200. END
  5201. END;
  5202. result.SetAssignable(parameter0.assignable)
  5203. (* ---- MAX ----- *)
  5204. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5205. IF numberActualParameters = 1 THEN
  5206. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5207. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5208. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5209. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5210. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5211. *)
  5212. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5213. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5214. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5215. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5216. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5217. END;
  5218. ELSE
  5219. Error(parameter0.position,"is not a type symbol");
  5220. END
  5221. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5222. ConvertOperands(parameter0,parameter1);
  5223. actualParameters.SetExpression(0,parameter0);
  5224. actualParameters.SetExpression(1,parameter1);
  5225. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5226. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5227. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5228. END;
  5229. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5230. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5231. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5232. END;
  5233. END;
  5234. type := parameter0.type;
  5235. ELSE type := SyntaxTree.invalidType;
  5236. END;
  5237. (* ---- MIN ----- *)
  5238. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5239. IF numberActualParameters = 1 THEN
  5240. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5241. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5242. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5243. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5244. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5245. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5246. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5247. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5248. END;
  5249. ELSE
  5250. Error(parameter0.position,"is not a type symbol");
  5251. END
  5252. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5253. ConvertOperands(parameter0,parameter1);
  5254. actualParameters.SetExpression(0,parameter0);
  5255. actualParameters.SetExpression(1,parameter1);
  5256. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5257. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5258. ELSE result.SetResolved(parameter1.resolved)
  5259. END;
  5260. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5261. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5262. ELSE result.SetResolved(parameter1.resolved)
  5263. END;
  5264. END;
  5265. type := parameter0.type;
  5266. ELSE type := SyntaxTree.invalidType;
  5267. END;
  5268. (* ---- ODD ----- *)
  5269. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5270. type := system.booleanType;
  5271. IF CheckIntegerType(parameter0) THEN
  5272. IF IsIntegerValue(parameter0,i0) THEN
  5273. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5274. type := system.booleanType;
  5275. END;
  5276. END;
  5277. (* ---- ORD ----- *)
  5278. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5279. IF id = Global.Ord THEN
  5280. type := system.integerType;
  5281. ELSE
  5282. type := system.longintType;
  5283. END;
  5284. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5285. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5286. actualParameters.SetExpression(0,parameter0);
  5287. (* IF CheckCharacterType(parameter0) THEN*)
  5288. IF IsCharacterValue(parameter0,c)THEN
  5289. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5290. type := Global.GetSignedIntegerType(system,ORD(c));
  5291. END;
  5292. ELSE Error(parameter0.position, "incompatible parameter");
  5293. END;
  5294. (* ---- SHORT ----- *)
  5295. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5296. type := type0;
  5297. IF IsSignedIntegerType(type) THEN
  5298. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5299. ELSIF type = system.integerType THEN type := system.shortintType
  5300. ELSIF type = system.longintType THEN type := system.integerType
  5301. ELSIF type = system.hugeintType THEN type:= system.longintType
  5302. ELSE
  5303. CASE type.sizeInBits OF
  5304. 16: type := Global.Integer8
  5305. |32: type := Global.Integer16
  5306. |64: type := Global.Integer32
  5307. END;
  5308. END;
  5309. ELSIF type IS SyntaxTree.FloatType THEN
  5310. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5311. ELSIF type = system.longrealType THEN type := system.realType
  5312. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5313. END;
  5314. ELSIF type IS SyntaxTree.ComplexType THEN
  5315. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5316. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5317. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5318. END;
  5319. ELSE
  5320. Error(parameter0.position,"short not applicable")
  5321. END;
  5322. IF (parameter0.resolved # NIL) THEN
  5323. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5324. IF parameter0 IS SyntaxTree.Value THEN
  5325. result.SetResolved(parameter0(SyntaxTree.Value));
  5326. END;
  5327. END;
  5328. (* ---- LONG ----- *)
  5329. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5330. type := type0;
  5331. IF IsSignedIntegerType(type) THEN
  5332. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5333. ELSIF type = system.longintType THEN type := system.hugeintType
  5334. ELSIF type = system.integerType THEN type := system.longintType
  5335. ELSIF type = system.shortintType THEN type := system.integerType
  5336. ELSE
  5337. CASE type.sizeInBits OF
  5338. 8: type := Global.Integer16
  5339. |16: type := Global.Integer32
  5340. |32: type := Global.Integer64
  5341. END;
  5342. END;
  5343. ELSIF type IS SyntaxTree.FloatType THEN
  5344. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5345. ELSIF type= system.realType THEN type := system.longrealType
  5346. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5347. END;
  5348. ELSIF type IS SyntaxTree.ComplexType THEN
  5349. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5350. ELSIF type = system.complexType THEN type := system.longcomplexType
  5351. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5352. END;
  5353. ELSE
  5354. Error(parameter0.position,"long not applicable")
  5355. END;
  5356. IF (parameter0.resolved # NIL) THEN
  5357. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5358. IF parameter0 IS SyntaxTree.Value THEN
  5359. result.SetResolved(parameter0(SyntaxTree.Value));
  5360. END;
  5361. END;
  5362. (* ---- SIZE OF ----- *)
  5363. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5364. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5365. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5366. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5367. type := system.integerType;
  5368. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5369. ELSE
  5370. (* for variables, system sizeof could represent the physically occupied size
  5371. determined via the type descriptor, implement that ? *)
  5372. Error(parameter0.position,"is not a type symbol");
  5373. END
  5374. (* ---- SYSTEM.TRACE -----*)
  5375. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5376. FOR i := 0 TO numberActualParameters-1 DO
  5377. parameter0 := actualParameters.GetExpression(i);
  5378. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5379. Error(parameter0.position,"incompatible parameter");
  5380. END;
  5381. END;
  5382. (* remaining issues can only be tested in backend *)
  5383. (* ---- ADDRESSOF----- *)
  5384. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5385. IF HasAddress(parameter0) THEN
  5386. type := system.addressType;
  5387. ELSE
  5388. type := SyntaxTree.invalidType;
  5389. Error(parameter0.position,"has no address");
  5390. END;
  5391. (* ---- BIT ----- *)
  5392. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5393. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5394. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5395. actualParameters.SetExpression(0,parameter0);
  5396. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5397. actualParameters.SetExpression(1,parameter1);
  5398. END;
  5399. type := system.booleanType;
  5400. (* ----- MSK ---- *)
  5401. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5402. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5403. ConvertOperands(parameter0,parameter1);
  5404. actualParameters.SetExpression(0,parameter0);
  5405. actualParameters.SetExpression(1,parameter1);
  5406. END;
  5407. type := parameter0.type;
  5408. (* ---- SYSTEM.GET64 ----- *)
  5409. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5410. IF CheckAddressType(parameter0) THEN
  5411. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5412. actualParameters.SetExpression(0,parameter0);
  5413. END;
  5414. type := system.hugeintType;
  5415. (* ---- SYSTEM.GET32 ----- *)
  5416. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5417. IF CheckAddressType(parameter0) THEN
  5418. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5419. actualParameters.SetExpression(0,parameter0);
  5420. END;
  5421. type := system.longintType;
  5422. (* ---- SYSTEM.GET16 ----- *)
  5423. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5424. IF CheckAddressType(parameter0) THEN
  5425. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5426. actualParameters.SetExpression(0,parameter0);
  5427. END;
  5428. type := system.integerType;
  5429. (* ---- SYSTEM.GET8 ----- *)
  5430. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5431. IF CheckAddressType(parameter0) THEN
  5432. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5433. actualParameters.SetExpression(0,parameter0);
  5434. END;
  5435. type := system.shortintType;
  5436. (* ---- SYSTEM.GetStackPointer ----- *)
  5437. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5438. type := system.addressType;
  5439. (* ---- SYSTEM.GetFramePointer ----- *)
  5440. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5441. type := system.addressType;
  5442. (* ---- SYSTEM.GetActivity ----- *)
  5443. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5444. type := system.objectType;
  5445. (* ---- SYSTEM.SetStackPointer ----- *)
  5446. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5447. IF CheckAddressType(parameter0) THEN
  5448. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5449. actualParameters.SetExpression(0,parameter0);
  5450. END;
  5451. (* ---- SYSTEM.SetFramePointer ----- *)
  5452. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5453. IF CheckAddressType(parameter0) THEN
  5454. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5455. actualParameters.SetExpression(0,parameter0);
  5456. END;
  5457. (* ---- SYSTEM.SetActivity ----- *)
  5458. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5459. IF CheckObjectType(parameter0) THEN
  5460. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5461. actualParameters.SetExpression(0,parameter0);
  5462. END;
  5463. (* ---- LSH, LSL, ROT, ROR ----- *)
  5464. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5465. type := type0;
  5466. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5467. actualParameters.SetExpression(1, parameter1);
  5468. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5469. IF id = Global.Lsh THEN
  5470. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5471. ELSIF id = Global.Rot THEN
  5472. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5473. ELSIF id = Global.Ror THEN
  5474. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5475. END;
  5476. END;
  5477. (* ---- SYSTEM.VAL ----- *)
  5478. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5479. IF CheckTypeDeclarationType(parameter0) THEN
  5480. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5481. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5482. result := SyntaxTree.invalidExpression;
  5483. Error(parameter0.position,"is no basic type");
  5484. ELSE
  5485. IF (parameter1.resolved # NIL) THEN
  5486. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5487. IF parameter0 IS SyntaxTree.Value THEN
  5488. result.SetResolved(parameter0(SyntaxTree.Value));
  5489. END;
  5490. END;
  5491. result.SetAssignable(parameter1.assignable);
  5492. END;
  5493. END;
  5494. (* ---- SYSTEM.GET ----- *)
  5495. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5496. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5497. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5498. actualParameters.SetExpression(0,parameter0);
  5499. END;
  5500. (* ---- SYSTEM.PUT ----- *)
  5501. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5502. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5503. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5504. actualParameters.SetExpression(0,parameter0);
  5505. END;
  5506. (* ---- SYSTEM.PUT64 ----- *)
  5507. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5508. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5509. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5510. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5511. actualParameters.SetExpression(0,parameter0);
  5512. actualParameters.SetExpression(1,parameter1);
  5513. END;
  5514. (* ---- SYSTEM.PUT32 ----- *)
  5515. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5516. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5517. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5518. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5519. actualParameters.SetExpression(0,parameter0);
  5520. actualParameters.SetExpression(1,parameter1);
  5521. END;
  5522. (* ---- SYSTEM.PUT16 ----- *)
  5523. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5524. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5525. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5526. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5527. actualParameters.SetExpression(0,parameter0);
  5528. actualParameters.SetExpression(1,parameter1);
  5529. END;
  5530. (* ---- SYSTEM.PUT8 ----- *)
  5531. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5532. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5533. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5534. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5535. actualParameters.SetExpression(0,parameter0);
  5536. actualParameters.SetExpression(1,parameter1);
  5537. END;
  5538. (* ---- SYSTEM.MOVE ----- *)
  5539. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5540. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5541. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5542. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5543. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5544. actualParameters.SetExpression(0,parameter0);
  5545. actualParameters.SetExpression(1,parameter1);
  5546. actualParameters.SetExpression(2,parameter2);
  5547. END;
  5548. (* ---- SYSTEM.NEW ----- *)
  5549. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5550. IF ~IsPointerType(parameter0.type) THEN
  5551. Error(parameter0.position,"is not a pointer")
  5552. ELSIF CheckSizeType(parameter1) THEN
  5553. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5554. actualParameters.SetExpression(1,parameter1);
  5555. END;
  5556. (* ----SYSTEM.REF ---- *)
  5557. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5558. type := system.addressType
  5559. (* ---- INCR ----- *)
  5560. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5561. type := system.sizeType;
  5562. base := type0;
  5563. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5564. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5565. IF i1 < 0 THEN
  5566. Error(position, "invalid dimension");
  5567. base := SyntaxTree.invalidType;
  5568. ELSE
  5569. base := ArrayBase(base,i1);
  5570. IF (base # NIL) & Indexable(base) THEN
  5571. ELSE
  5572. Error(position, "len on no array");
  5573. IF VerboseErrorMessage THEN
  5574. Printout.Info("base",base);
  5575. END;
  5576. base := SyntaxTree.invalidType;
  5577. END;
  5578. END;
  5579. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5580. actualParameters.SetExpression(1,parameter1);
  5581. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5582. mathArrayType := base(SyntaxTree.MathArrayType);
  5583. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5584. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5585. type := system.longintType;
  5586. END;
  5587. END;
  5588. ELSE
  5589. type := system.longintType;
  5590. END;
  5591. (* ---- SUM ----- *)
  5592. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5593. Error(position, "sum operator not applicable");
  5594. (* ---- ALL ----- *)
  5595. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5596. Error(position, "all operator not applicable");
  5597. (* ---- DIM ----- *)
  5598. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5599. type := system.sizeType;
  5600. IF type0 IS SyntaxTree.MathArrayType THEN
  5601. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5602. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5603. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5604. END;
  5605. ELSE
  5606. Error(position, "dimension on non math array type");
  5607. END;
  5608. (* ---- CAS ----- *)
  5609. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5610. IF type0.IsComposite () THEN
  5611. Error(position, "first parameter of composite type");
  5612. result := SyntaxTree.invalidExpression;
  5613. ELSIF ~IsVariable (parameter0) THEN
  5614. Error(position, "first parameter not assignable");
  5615. result := SyntaxTree.invalidExpression;
  5616. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5617. Error(position, "second parameter incompatible");
  5618. result := SyntaxTree.invalidExpression;
  5619. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5620. Error(position, "third parameter incompatible");
  5621. result := SyntaxTree.invalidExpression;
  5622. ELSE
  5623. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5624. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5625. type := type0;
  5626. END;
  5627. (* ---- RESHAPE ----- *)
  5628. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5629. IF type0 IS SyntaxTree.MathArrayType THEN
  5630. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5631. base := ArrayBase(type0,MAX(LONGINT));
  5632. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5633. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5634. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5635. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5636. IF ~CompatibleTo(system,type0,parameterType) THEN
  5637. Error(parameter0.position,"incompatible parameter in reshape");
  5638. result := SyntaxTree.invalidExpression;
  5639. ELSE
  5640. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5641. END;
  5642. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5643. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5644. IF ~CompatibleTo(system,type1,parameterType) THEN
  5645. Error(parameter1.position,"parameter incompatible to math array of longint");
  5646. result := SyntaxTree.invalidExpression;
  5647. ELSE
  5648. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5649. END;
  5650. ELSE
  5651. Error(position,"reshape on non math array type");
  5652. result := SyntaxTree.invalidExpression;
  5653. END;
  5654. (* ---- SYSTEM.TYPECODE ----- *)
  5655. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5656. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5657. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5658. type := type.resolved;
  5659. IF type IS SyntaxTree.PointerType THEN
  5660. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5661. END;
  5662. IF ~(type IS SyntaxTree.RecordType) THEN
  5663. Error(parameter0.position,"must be type with type descriptor");
  5664. END;
  5665. ELSE
  5666. Error(parameter0.position,"is not a type symbol");
  5667. END;
  5668. type := system.addressType;
  5669. (* -------- FLT --------- *)
  5670. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5671. type := system.realType;
  5672. IF IsRealValue(parameter0, r) THEN
  5673. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5674. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5675. i0 := i; i := ABS(i);
  5676. IF i # 0 THEN
  5677. i1 := 23;
  5678. IF i >= 2*800000H THEN
  5679. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5680. ELSIF i < 800000H THEN
  5681. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5682. END;
  5683. i := (i1 + 127)*800000H - 800000H + i;
  5684. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5685. END;
  5686. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5687. END;
  5688. (* ------- CONNECT -------*)
  5689. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5690. (*IF ~(currentIsCellNet) THEN
  5691. Error(position, "connection outside activeCells body block");
  5692. END;*)
  5693. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5694. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5695. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5696. END;
  5697. IF numberActualParameters = 3 THEN
  5698. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5699. Error(position, "incompatible channel size parameter");
  5700. END;
  5701. *)
  5702. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5703. actualParameters.SetExpression(2,parameter2);
  5704. END;
  5705. activeCellsStatement := TRUE;
  5706. (* ---------- DELEGATE --------*)
  5707. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5708. (*
  5709. IF ~(currentIsCellNet) THEN
  5710. Error(position, "connection delegation outside activeCells body block");
  5711. END;
  5712. *)
  5713. IF ~CheckPortType(parameter1, inPort) THEN
  5714. Error(parameter0.position,"not a port")
  5715. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5716. Error(parameter1.position,"not a port")
  5717. ELSIF (outPort.direction # inPort.direction) THEN
  5718. Error(parameter0.position,"invalid port direction");
  5719. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5720. Error(position, "incompatible port sizes");
  5721. END;
  5722. activeCellsStatement := TRUE;
  5723. (* --------- RECEIVE ---------*)
  5724. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5725. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5726. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5727. IF inPort.direction # SyntaxTree.InPort THEN
  5728. Error(parameter0.position,"not an in-port")
  5729. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5730. Error(parameter1.position,"incompatible to port type");
  5731. END;
  5732. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5733. IF ~SameType(parameter2.type, system.integerType) THEN
  5734. Error(parameter2.position,"incompatible to integer type");
  5735. END;
  5736. END;
  5737. END;
  5738. (* --------- SEND ---------*)
  5739. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5740. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5741. IF CheckPortType(parameter0,outPort) THEN
  5742. IF outPort.direction # SyntaxTree.OutPort THEN
  5743. Error(parameter1.position,"not an out-port")
  5744. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5745. Error(parameter1.position,"incompatible to port type");
  5746. ELSE
  5747. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5748. actualParameters.SetExpression(1,parameter1);
  5749. END;
  5750. END;
  5751. (* ------- custom builtins ----- *)
  5752. ELSIF id = Global.systemSpecial THEN
  5753. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5754. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5755. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5756. type := procedureType.returnType;
  5757. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5758. (* go through all formal parameters *)
  5759. formalParameter := procedureType.firstParameter;
  5760. FOR i := 0 TO actualParameters.Length() - 1 DO
  5761. actualParameter := actualParameters.GetExpression(i);
  5762. IF actualParameter = SyntaxTree.invalidExpression THEN
  5763. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5764. Error(position, "incompatible parameter")
  5765. ELSE
  5766. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5767. END;
  5768. actualParameters.SetExpression(i, actualParameter);
  5769. formalParameter := formalParameter.nextParameter
  5770. END
  5771. END
  5772. ELSE
  5773. Error(position, "builtin not implemented");
  5774. result := SyntaxTree.invalidExpression;
  5775. END;
  5776. END;
  5777. IF result # SyntaxTree.invalidExpression THEN
  5778. type := ResolveType(type);
  5779. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5780. result.SetType(type);
  5781. END;
  5782. RETURN result
  5783. END NewBuiltinCallDesignator;
  5784. (** return type guard designator left(type)
  5785. - check if type can be extended (i.e. is no static record)
  5786. - check if type is a type extension of left.type
  5787. - returns new type guard designator
  5788. returns invalidDesignator = invalidExpression if error
  5789. **)
  5790. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5791. VAR result: SyntaxTree.Designator;
  5792. BEGIN
  5793. result := SyntaxTree.invalidDesignator;
  5794. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5795. Error(position, "no type extension of type");
  5796. IF VerboseErrorMessage THEN
  5797. Printout.Info("left",left);
  5798. Printout.Info("type",type);
  5799. END;
  5800. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5801. Error(position, "variable cannot be extended");
  5802. ELSIF IsUnsafePointer(left.type) THEN
  5803. Error(position, "forbidden type guard on unsafe pointer");
  5804. ELSE
  5805. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5806. result.SetType(type);
  5807. result.SetAssignable(left.assignable);
  5808. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5809. END;
  5810. RETURN result
  5811. END NewTypeGuardDesignator;
  5812. (** check and resolve parameter designator left(expression list)
  5813. - check expression list
  5814. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5815. - elsif left is a procedure type then
  5816. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5817. - else return is a procedure call then return ProcedureCallDesignator
  5818. returns invalidDesignator = invalidExpression if error
  5819. **)
  5820. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5821. VAR
  5822. parameters: SyntaxTree.ExpressionList;
  5823. left: SyntaxTree.Designator;
  5824. result,expression: SyntaxTree.Expression;
  5825. typeDeclaration: SyntaxTree.TypeDeclaration;
  5826. type, expressionType: SyntaxTree.Type;
  5827. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5828. BEGIN
  5829. type := type.resolved;
  5830. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5831. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5832. END;
  5833. RETURN type
  5834. END BaseType;
  5835. BEGIN
  5836. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5837. result := SyntaxTree.invalidDesignator;
  5838. left := ResolveDesignator(designator.left);
  5839. IF left # SyntaxTree.invalidDesignator THEN
  5840. parameters := designator.parameters;
  5841. IF ExpressionList(parameters) THEN
  5842. IF (left.type = NIL) THEN
  5843. Error(left.position,"object is not a procedure or cannot be extended");
  5844. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5845. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5846. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5847. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5848. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5849. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5850. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5851. ELSE
  5852. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5853. END
  5854. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5855. expression := parameters.GetExpression(0);
  5856. type := typeDeclaration.declaredType.resolved;
  5857. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5858. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5859. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5860. OR (expressionType IS SyntaxTree.EnumerationType)
  5861. ) THEN
  5862. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5863. ELSE
  5864. Error(left.position,"invalid type in explicit conversion");
  5865. END;
  5866. ELSE
  5867. Error(left.position,"called object is not a procedure or cannot be extended");
  5868. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5869. result := SyntaxTree.invalidDesignator;
  5870. END;
  5871. ELSE
  5872. result := SyntaxTree.invalidDesignator
  5873. END;
  5874. END;
  5875. resolvedExpression := result;
  5876. END VisitParameterDesignator;
  5877. (** check dereference designator left^
  5878. - check if left is pointer type or left is object type
  5879. - return new dereference designator with type = left.baseType.type (if appropriate)
  5880. with error handling
  5881. returns invalidDesignator = invalidExpression if error
  5882. **)
  5883. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5884. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5885. BEGIN
  5886. result := SyntaxTree.invalidDesignator;
  5887. type := left.type;
  5888. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5889. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5890. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5891. result.SetAssignable(TRUE);
  5892. result.SetType(type);
  5893. result.SetHidden(left.isHidden);
  5894. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5895. type := type.resolved;
  5896. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5897. result.SetAssignable(TRUE);
  5898. result.SetType(type);
  5899. result.SetHidden(left.isHidden);
  5900. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5901. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5902. result.SetAssignable(TRUE);
  5903. result.SetType(type);
  5904. result.SetHidden(left.isHidden);
  5905. ELSE
  5906. Error(position, "dereference on no pointer");
  5907. IF VerboseErrorMessage THEN
  5908. Printout.Info("pointer", type);
  5909. Printout.Info("scope", currentScope);
  5910. END;
  5911. END;
  5912. RETURN result
  5913. END NewDereferenceDesignator;
  5914. (** check supercall designator left^
  5915. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5916. - return new supercall designator with type = left.type
  5917. with error handling
  5918. **)
  5919. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5920. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5921. objectScope: SyntaxTree.Scope;
  5922. BEGIN
  5923. result := SyntaxTree.invalidDesignator;
  5924. IF left = SyntaxTree.invalidDesignator THEN
  5925. (* error already handled *)
  5926. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5927. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5928. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5929. IF symbol IS SyntaxTree.Procedure THEN
  5930. procedure := symbol(SyntaxTree.Procedure);
  5931. objectScope := currentScope;
  5932. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5933. objectScope := objectScope.outerScope;
  5934. END;
  5935. IF (left.left = NIL) OR ~
  5936. (
  5937. (left.left IS SyntaxTree.SelfDesignator) OR
  5938. (left.left IS SyntaxTree.DereferenceDesignator)
  5939. & (left.left(SyntaxTree.Designator).left # NIL)
  5940. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5941. Error(position, "procedure not in immediate object scope");
  5942. IF VerboseErrorMessage THEN
  5943. Printout.Info("left.left",left.left);
  5944. END;
  5945. ELSIF procedure.super # NIL THEN
  5946. result := SyntaxTree.NewSupercallDesignator(position,left);
  5947. result.SetType(left.type.resolved)
  5948. ELSE
  5949. Error(position, "no supermethod for this procedure");
  5950. END;
  5951. ELSE
  5952. Error(position, "symbol is not a procedure");
  5953. END;
  5954. ELSE
  5955. Error(position, "is no symbol designator");
  5956. END;
  5957. RETURN result
  5958. END NewSupercallDesignator;
  5959. (** check and semantically resolve arrow designator left^
  5960. - if left is procedure type -> result := SupercallDesignator
  5961. - else result := DereferenceDesignator
  5962. returns result via global variable resolvedExpression
  5963. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5964. **)
  5965. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5966. VAR left: SyntaxTree.Designator;
  5967. BEGIN
  5968. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5969. left := ResolveDesignator(arrowDesignator.left);
  5970. IF left # NIL THEN
  5971. IF (left.type = NIL) THEN
  5972. Error(arrowDesignator.position,"Invalid arrow designator");
  5973. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5974. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5975. ELSE
  5976. IF IsPointerToObject(left.type) THEN
  5977. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5978. END;
  5979. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5980. END
  5981. END
  5982. END VisitArrowDesignator;
  5983. (** check and return expression
  5984. - if expression has no type then resolve expression
  5985. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5986. - return result
  5987. **)
  5988. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5989. VAR result,prev: SyntaxTree.Expression;
  5990. BEGIN
  5991. IF expression = NIL THEN result := NIL
  5992. ELSIF (expression.type = NIL) THEN
  5993. prev := resolvedExpression;
  5994. resolvedExpression := SyntaxTree.invalidExpression;
  5995. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5996. expression.SetType(SyntaxTree.invalidType);
  5997. END;
  5998. expression.Accept(SELF);
  5999. result := resolvedExpression;
  6000. IF currentIsRealtime THEN
  6001. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6002. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6003. END;
  6004. END;
  6005. (* designator modifiers for backends if they support it ...*)
  6006. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6007. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6008. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6009. END;
  6010. resolvedExpression := prev
  6011. ELSE
  6012. result := expression
  6013. END;
  6014. RETURN result
  6015. END ResolveExpression;
  6016. (**
  6017. check expression to be constant expression
  6018. - resolve expression
  6019. - if valid then check that of value type
  6020. report error and return invalidExpression if anything fails
  6021. **)
  6022. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6023. VAR position: Position;
  6024. BEGIN
  6025. position := expression.position;
  6026. expression := ResolveExpression(expression);
  6027. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6028. ELSIF (expression.resolved = NIL) THEN
  6029. Error(position, "expression is not constant");
  6030. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6031. expression := SyntaxTree.invalidExpression;
  6032. END;
  6033. RETURN expression
  6034. END ConstantExpression;
  6035. (** check expression to be constant integer
  6036. - resolve expresssion
  6037. - if valid then check that of integer value type
  6038. report error and return invalidExpression if anything fails
  6039. **)
  6040. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6041. VAR position: Position;
  6042. BEGIN
  6043. position := expression.position;
  6044. expression := ResolveExpression(expression);
  6045. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6046. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6047. expression := SyntaxTree.invalidExpression;
  6048. Error(position, "expression is not a constant integer");
  6049. END;
  6050. RETURN expression
  6051. END ConstantInteger;
  6052. (** check expression as positive (>=0) constant integer
  6053. - resolve expression
  6054. - if valid then check that integer value
  6055. - if integer value then check that value >= 0
  6056. report error and return invalidExpression if anything fails
  6057. **)
  6058. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6059. VAR position: Position;
  6060. BEGIN
  6061. position := expression.position;
  6062. expression := ConstantExpression(expression);
  6063. IF expression = SyntaxTree.invalidExpression THEN
  6064. (* error already reported *)
  6065. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6066. Error(position, "expression is not integer valued");
  6067. expression := SyntaxTree.invalidExpression
  6068. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6069. Error(position, "integer is not greater or equal zero");
  6070. END;
  6071. RETURN expression
  6072. END ConstantIntegerGeq0;
  6073. (** check expression as condition
  6074. - resolve expression
  6075. - if valid expression then check that result type is boolean
  6076. report error and return invalidExpression if anything fails
  6077. **)
  6078. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6079. VAR position: Position;
  6080. BEGIN
  6081. position := expression.position;
  6082. expression := ResolveExpression(expression);
  6083. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6084. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6085. expression := SyntaxTree.invalidExpression;
  6086. Error(position, "expression is not boolean");
  6087. END;
  6088. RETURN expression
  6089. END ResolveCondition;
  6090. (*** symbols ***)
  6091. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6092. BEGIN
  6093. x.Accept(SELF);
  6094. END ResolveSymbol;
  6095. (** check a symbol
  6096. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6097. **)
  6098. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6099. VAR scope: SyntaxTree.Scope;
  6100. BEGIN
  6101. (* visibility *)
  6102. scope := symbol.scope;
  6103. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6104. scope := scope.outerScope;
  6105. END;
  6106. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6107. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6108. Error(symbol.position,"cannot be exported");
  6109. IF VerboseErrorMessage THEN
  6110. Printout.Info("symbol",symbol);
  6111. END;
  6112. END;
  6113. END;
  6114. END CheckSymbolVisibility;
  6115. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6116. If node is currently being resolved then emit a cyclic definition error.
  6117. Return TRUE only if node is fully resolved.
  6118. **)
  6119. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6120. VAR result: BOOLEAN;
  6121. BEGIN
  6122. IF SyntaxTree.Resolved IN x.state THEN
  6123. result := FALSE
  6124. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6125. Error(x.position,"cyclic definition");
  6126. result := FALSE;
  6127. ELSE
  6128. result := TRUE;
  6129. x.SetState(SyntaxTree.BeingResolved)
  6130. END;
  6131. RETURN result
  6132. END SymbolNeedsResolution;
  6133. (** check and resolve a type declaration symbol = Type
  6134. - set type to declaration type
  6135. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6136. This is so because the type declaration itself does not have a type but it only stands for a type.
  6137. In the implementation of the compiler this made a lot much easier.
  6138. - resolve and set declared type
  6139. - check symbol
  6140. **)
  6141. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6142. VAR prevScope: SyntaxTree.Scope;
  6143. BEGIN
  6144. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6145. IF SymbolNeedsResolution(typeDeclaration) THEN
  6146. typeDeclaration.SetState(SyntaxTree.Resolved);
  6147. prevScope := currentScope;
  6148. currentScope := typeDeclaration.scope;
  6149. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6150. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6151. CheckSymbolVisibility(typeDeclaration);
  6152. typeDeclaration.SetState(SyntaxTree.Resolved);
  6153. currentScope := prevScope;
  6154. END;
  6155. END VisitTypeDeclaration;
  6156. (** check and resolve a constant declaration symbol = (constant) expression
  6157. - check expression
  6158. - set type and value
  6159. - check symbol
  6160. **)
  6161. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6162. VAR
  6163. expression: SyntaxTree.Expression;
  6164. type: SyntaxTree.Type;
  6165. name: Basic.SegmentedName;
  6166. replacement: Replacement;
  6167. BEGIN
  6168. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6169. IF SymbolNeedsResolution(constant) THEN
  6170. expression := constant.value;
  6171. IF replacements # NIL THEN
  6172. Global.GetSymbolSegmentedName(constant, name);
  6173. replacement := replacements;
  6174. WHILE (replacement # NIL) & (replacement.name # name) DO
  6175. replacement := replacement.next;
  6176. END;
  6177. IF replacement # NIL THEN
  6178. InfoSS(constant.position, "replacing constant", constant.name);
  6179. (*
  6180. NEW(stringReader, Strings.Length(replacement.string^));
  6181. stringReader.Set(replacement.string^);
  6182. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6183. NEW(parser, scanner, diagnostics);
  6184. expression := parser.Expression();
  6185. *)
  6186. expression := replacement.expression;
  6187. replacement.used := TRUE;
  6188. END;
  6189. END;
  6190. constant.SetType(SyntaxTree.invalidType);
  6191. expression := ConstantExpression(expression);
  6192. ASSERT(expression.type # NIL);
  6193. type := expression.type.resolved;
  6194. constant.SetType(type);
  6195. constant.SetValue(expression);
  6196. CheckSymbolVisibility(constant);
  6197. constant.SetState(SyntaxTree.Resolved);
  6198. END;
  6199. END VisitConstant;
  6200. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6201. VAR procedureAlignment: LONGINT;
  6202. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6203. (* least common multiple *)
  6204. VAR a,b: LONGINT;
  6205. BEGIN
  6206. a := a0; b := b0;
  6207. WHILE (a # b) DO
  6208. IF a < b THEN a := a+a0
  6209. ELSE b := b + b0
  6210. END;
  6211. END;
  6212. RETURN a
  6213. END LCM;
  6214. BEGIN
  6215. IF alignment > 1 THEN
  6216. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6217. IF (procedureAlignment > 1) THEN
  6218. alignment := LCM(alignment, procedureAlignment);
  6219. END;
  6220. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6221. END;
  6222. END AdaptStackAlignment;
  6223. (** check and resolve a variable / field
  6224. - check and set type
  6225. - negative check on open array type
  6226. - check symbol
  6227. **)
  6228. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6229. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6230. BEGIN
  6231. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6232. IF SymbolNeedsResolution(variable) THEN
  6233. modifiers := variable.modifiers;
  6234. (*
  6235. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6236. variable.AddFlags(flags);
  6237. *)
  6238. variable.SetType(ResolveType(variable.type));
  6239. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6240. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6241. Error(variable.position,"forbidden open array variable");
  6242. END;
  6243. END;
  6244. CheckSymbolVisibility(variable);
  6245. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6246. variable.SetUntraced(TRUE);
  6247. IF ~ContainsPointer(variable.type) THEN
  6248. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6249. Error(position, "untraced flag on non-pointer variable");
  6250. END;
  6251. END;
  6252. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6253. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6254. IF ~PowerOf2(value) THEN
  6255. Error(position, "forbidden alignment - must be power of two");
  6256. ELSE
  6257. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6258. END;
  6259. END;
  6260. variable.SetAlignment(FALSE,value);
  6261. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6262. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6263. Error(position, "fixed position not possible in procedure");
  6264. END;
  6265. variable.SetAlignment(TRUE, value);
  6266. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6267. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6268. Error(position,"fictive offset not possible in procedure");
  6269. END;
  6270. variable.SetFictive(value);
  6271. variable.SetOffset(value*system.dataUnit);
  6272. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6273. END;
  6274. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6275. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6276. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6277. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6278. END;
  6279. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6280. IF variable.initializer # NIL THEN
  6281. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6282. END;
  6283. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6284. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6285. pointerType.SetPointerBase(variable.type);
  6286. pointerType.SetHidden(TRUE);
  6287. variable.SetType(ResolveType(pointerType));
  6288. END;
  6289. variable.SetState(SyntaxTree.Resolved);
  6290. END;
  6291. END VisitVariable;
  6292. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6293. BEGIN
  6294. VisitVariable(property)
  6295. END VisitProperty;
  6296. (** check and resolve a (procedure) parameter
  6297. - check and set type
  6298. - check symbol
  6299. - check parameter kind and set read-only flags if appropriate
  6300. **)
  6301. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6302. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6303. BEGIN
  6304. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6305. IF SymbolNeedsResolution(parameter) THEN
  6306. modifiers := parameter.modifiers;
  6307. parameter.SetType(ResolveType(parameter.type));
  6308. ASSERT(parameter.type.resolved # NIL);
  6309. CheckSymbolVisibility(parameter);
  6310. IF parameter.defaultValue # NIL THEN
  6311. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6312. Error(parameter.position,"forbidden default value on non-value parameter");
  6313. ELSE
  6314. expression := ConstantExpression(parameter.defaultValue);
  6315. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6316. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6317. parameter.SetDefaultValue(expression);
  6318. END;
  6319. END;
  6320. END;
  6321. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6322. Error(parameter.position, "forbidden value parameter of math array type ");
  6323. END;
  6324. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6325. parameter.SetUntraced(TRUE);
  6326. IF ~ContainsPointer(parameter.type) THEN
  6327. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6328. Error(position, "untraced flag on non-pointer variable");
  6329. END;
  6330. END;
  6331. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6332. parameter.SetMoveable(TRUE);
  6333. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6334. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6335. Error(position, "illegal movable flag on non-address variable");
  6336. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6337. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6338. Error(position, "unnecessary movable flag on variable variable");
  6339. END;
  6340. END;
  6341. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6342. parameter.SetState(SyntaxTree.Resolved);
  6343. END;
  6344. END VisitParameter;
  6345. (** check and resolve a procedure (with declaration and implementation scope)
  6346. - check the procedure type
  6347. - check if method (i.e. in record scope), if so then
  6348. - check if (unique) constructor
  6349. - check if (unique) finalizer
  6350. - check if super method available, if so then check signature
  6351. - of not in record scope then negative check on constructor flag
  6352. - of not in record scope then negative check on finalizer flag
  6353. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6354. - check procedure symbol
  6355. **)
  6356. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6357. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6358. procedureType: SyntaxTree.ProcedureType;
  6359. type: SyntaxTree.Type;
  6360. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6361. qualifiedType: SyntaxTree.QualifiedType;
  6362. value: LONGINT;
  6363. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6364. position: Position;
  6365. fp: SyntaxTree.FingerPrint;
  6366. BEGIN
  6367. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6368. IF IsOberonInline(procedure) THEN
  6369. IF SyntaxTree.Public * procedure.access # {} THEN
  6370. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6371. END;
  6372. procedure.SetInline(FALSE);
  6373. procedure.SetOberonInline(TRUE);
  6374. END;
  6375. IF SymbolNeedsResolution(procedure) THEN
  6376. recentIsRealtime := currentIsRealtime;
  6377. recentIsBodyProcedure := currentIsBodyProcedure;
  6378. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6379. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6380. modifiers := procedureType.modifiers;
  6381. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6382. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6383. IF useDarwinCCalls THEN (*fld*)
  6384. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6385. ELSE
  6386. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6387. END
  6388. END;
  6389. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6390. procedureType.SetInterrupt(TRUE);
  6391. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6392. END;
  6393. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6394. procedureType.SetNoReturn(TRUE);
  6395. END;
  6396. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6397. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6398. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6399. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6400. END;
  6401. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6402. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6403. END;
  6404. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6405. IF ~PowerOf2(value) THEN
  6406. Error(position, "forbidden stack alignment - must be power of two");
  6407. ELSE
  6408. procedureType.SetStackAlignment(value)
  6409. END;
  6410. END;
  6411. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6412. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6413. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6414. END;
  6415. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6416. SyntaxTree.InitFingerPrint(fp);
  6417. fp.shallow := value;
  6418. fp.public := value;
  6419. fp.private := value;
  6420. fp.shallowAvailable := TRUE;
  6421. procedure.SetFingerPrint(fp);
  6422. END;
  6423. CheckModifiers(modifiers, TRUE);
  6424. modifiers := procedureType.returnTypeModifiers;
  6425. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6426. CheckModifiers(modifiers, TRUE);
  6427. procedure.SetState(SyntaxTree.Resolved);
  6428. FixProcedureType(procedureType);
  6429. currentIsRealtime := procedureType.isRealtime;
  6430. currentIsBodyProcedure := procedure.isBodyProcedure;
  6431. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6432. THEN
  6433. Error(procedure.position,"problems during parameter offset computation");
  6434. END;
  6435. CheckSymbolVisibility(procedure);
  6436. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6437. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6438. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6439. Error(procedure.position,"problem during parameter offset generation");
  6440. END;
  6441. END;
  6442. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6443. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6444. procedureType.SetDelegate(TRUE);
  6445. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6446. (* add auto-self *)
  6447. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6448. IF (record.pointerType.typeDeclaration = NIL) THEN
  6449. selfParameter.SetType(record.pointerType);
  6450. ELSE
  6451. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6452. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6453. qualifiedType.SetResolved(record.pointerType);
  6454. selfParameter.SetType(qualifiedType);
  6455. END;
  6456. selfParameter.SetAccess(SyntaxTree.Hidden);
  6457. END;
  6458. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6459. IF procedure.isConstructor THEN
  6460. (*! constructor is always visible, compatibility to paco
  6461. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6462. *)
  6463. procedure.MarkUsed;
  6464. IF procedureType.returnType # NIL THEN
  6465. Error(procedure.position,"constructor with forbidden return type");
  6466. END;
  6467. proc := procedure.scope.firstProcedure;
  6468. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6469. proc := proc.nextProcedure;
  6470. END;
  6471. IF proc # NIL THEN
  6472. Error(procedure.position,"duplicate constructor")
  6473. ELSE
  6474. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6475. END;
  6476. END;
  6477. IF procedure.isFinalizer THEN
  6478. procedure.MarkUsed;
  6479. IF procedureType.returnType # NIL THEN
  6480. Error(procedure.position,"finalizer with forbidden return type");
  6481. END;
  6482. IF procedureType.numberParameters # 0 THEN
  6483. Error(procedure.position,"finalizer with formal parameters");
  6484. END;
  6485. proc := procedure.scope.firstProcedure;
  6486. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6487. proc := proc.nextProcedure;
  6488. END;
  6489. IF proc # NIL THEN
  6490. Error(procedure.position,"duplicate finalizer")
  6491. ELSE
  6492. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6493. END;
  6494. END;
  6495. super := FindSuperProcedure(record.recordScope, procedure);
  6496. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6497. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6498. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6499. END;
  6500. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6501. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6502. END;
  6503. IF super.isFinal THEN
  6504. Error(procedure.position,"forbidden method extending final method");
  6505. END;
  6506. procedure.SetSuper(super);
  6507. super.SetOverwritten(TRUE);
  6508. procedure.SetAccess(procedure.access+super.access);
  6509. procedure.MarkUsed;
  6510. END;
  6511. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6512. THEN
  6513. Error(procedure.position,"problems during parameter offset computation");
  6514. END;
  6515. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6516. IF cellsAreObjects THEN
  6517. procedureType.SetDelegate(TRUE);
  6518. END;
  6519. IF procedure.isConstructor THEN
  6520. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6521. END;
  6522. ELSIF procedure.isConstructor THEN
  6523. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6524. END;
  6525. Declarations(procedure.procedureScope, FALSE, {0,1});
  6526. (* body resolution part done as late fix of the procedure type *)
  6527. procedure.SetState(SyntaxTree.Resolved);
  6528. currentIsRealtime := recentIsRealtime;
  6529. currentIsBodyProcedure := recentIsBodyProcedure;
  6530. END;
  6531. END VisitProcedure;
  6532. (**
  6533. a builtin procedure is a global item that may not be modified locally
  6534. instead the resolving of builtin procedure calls are done in the esignator
  6535. **)
  6536. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6537. VAR type: SyntaxTree.Type;
  6538. BEGIN
  6539. type := ResolveType(builtinProcedure.type);
  6540. END VisitBuiltin;
  6541. (* nopov *)
  6542. (** check and resolve operator
  6543. - operators are first checked as procedures
  6544. - then additional operator-specific checks are done
  6545. - note that only module-scope operators are checked here
  6546. (operators in a record scope are only allowed in the context of
  6547. array-structured object types and checked in 'ResolveArrayStructure')
  6548. - also note that inter-operator conformity is not checked here
  6549. **)
  6550. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6551. VAR
  6552. procedureType: SyntaxTree.ProcedureType;
  6553. leftType, rightType: SyntaxTree.Type;
  6554. identifierNumber: LONGINT; position: Position;
  6555. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6556. modifiers: SyntaxTree.Modifier;
  6557. (** whether a type is locally defined in the current module scope
  6558. for arrays, the base type must be locally defined **)
  6559. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6560. BEGIN
  6561. IF type = NIL THEN
  6562. RETURN FALSE
  6563. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6564. RETURN TRUE
  6565. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6566. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6567. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6568. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6569. ELSE
  6570. RETURN FALSE
  6571. END
  6572. END IsLocallyDefined;
  6573. BEGIN
  6574. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6575. procedureType := operator.type(SyntaxTree.ProcedureType);
  6576. modifiers := procedureType.modifiers;
  6577. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6578. CheckModifiers(modifiers, TRUE);
  6579. VisitProcedure(operator);
  6580. IF operator.scope IS SyntaxTree.RecordScope THEN
  6581. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6582. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6583. IF identifierNumber = -1 THEN
  6584. Error(operator.position, "operator with unknown identifier")
  6585. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6586. Error(operator.position, "identifier may not be used for operator")
  6587. ELSE
  6588. IF procedureType.numberParameters < 1 THEN
  6589. Error(operator.position, "operator without operand");
  6590. ELSIF procedureType.numberParameters > 2 THEN
  6591. Error(operator.position, "operator with more than two operands");
  6592. ELSE
  6593. (* determine operand types *)
  6594. leftType := procedureType.firstParameter.type;
  6595. IF procedureType.numberParameters > 1 THEN
  6596. rightType := procedureType.firstParameter.nextParameter.type
  6597. ELSE
  6598. rightType := NIL
  6599. END;
  6600. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6601. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6602. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6603. Error(operator.position, "none of the operands is declared in the same module")
  6604. END
  6605. END;
  6606. (* TODO: refine the checks, think about how restrictive the checks should be
  6607. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6608. They might be used for intersection, union, complement of custom object types *)
  6609. (* defaults *)
  6610. hasReturnType := TRUE;
  6611. mustBeUnary := FALSE;
  6612. mustBeBinary := FALSE;
  6613. mustReturnBoolean := FALSE;
  6614. mustReturnInteger := FALSE;
  6615. mustHaveEquitypedOperands := FALSE;
  6616. (* operator-specific exceptions *)
  6617. CASE identifierNumber OF
  6618. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6619. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6620. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6621. mustBeBinary := TRUE
  6622. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6623. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6624. | Scanner.Times: mustBeBinary := TRUE
  6625. | Scanner.TimesTimes: mustBeBinary := TRUE
  6626. | Scanner.DotTimes: mustBeBinary := TRUE
  6627. | Scanner.PlusTimes: mustBeBinary := TRUE
  6628. | Scanner.Slash: mustBeBinary := TRUE
  6629. | Scanner.Backslash: mustBeBinary := TRUE
  6630. | Scanner.DotSlash: mustBeBinary := TRUE
  6631. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6632. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6633. | Scanner.Not: mustBeUnary := TRUE
  6634. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6635. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6636. | Scanner.Transpose: mustBeUnary := TRUE;
  6637. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6638. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6639. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6640. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6641. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6642. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6643. | Global.Abs: mustBeUnary := TRUE;
  6644. | Global.Ash: (* TODO: arity? *)
  6645. | Global.Cap: (* TODO: arity? *)
  6646. | Global.Chr: mustBeUnary := TRUE;
  6647. | Global.Entier: (* TODO: arity? *)
  6648. | Global.EntierH: (* TODO: arity? *)
  6649. | Global.Len: (* unary and binary *)
  6650. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6651. | Global.Max, Global.Min: (* unary and binary *)
  6652. | Global.Odd: (* TODO: arity? *)
  6653. | Global.Sum: (* TODO: arity? *)
  6654. | Global.All: (* TODO: arity? *)
  6655. | Global.Re, Global.Im:
  6656. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6657. | Scanner.Alias:
  6658. | Scanner.GreaterGreater, Scanner.LessLess:
  6659. mustBeBinary := TRUE; hasReturnType := FALSE;
  6660. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6661. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6662. END;
  6663. (* check parameter count *)
  6664. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6665. Error(operator.position,"operator is not unary")
  6666. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6667. Error(operator.position,"operator is not binary")
  6668. END;
  6669. (* check parameter types *)
  6670. (* TODO: is this used at all? *)
  6671. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6672. leftType := procedureType.firstParameter.type;
  6673. rightType := procedureType.firstParameter.nextParameter.type;
  6674. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6675. Error(operator.position, "the two operands are not of the same type")
  6676. END
  6677. END;
  6678. (* check return type *)
  6679. IF hasReturnType THEN
  6680. IF procedureType.returnType = NIL THEN
  6681. Error(operator.position, "return type required")
  6682. ELSIF mustReturnBoolean THEN
  6683. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6684. Error(operator.position,"return type is not Boolean")
  6685. END
  6686. ELSIF mustReturnInteger THEN
  6687. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6688. Error(operator.position,"return type is not integer")
  6689. END
  6690. END
  6691. ELSIF procedureType.returnType # NIL THEN
  6692. Error(operator.position, "return type not allowed")
  6693. END
  6694. END
  6695. END
  6696. END
  6697. END VisitOperator;
  6698. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6699. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6700. BEGIN
  6701. IF error THEN RETURN FALSE END;
  6702. prevScope := currentScope;
  6703. prevDiagnostics := diagnostics;
  6704. diagnostics := NIL; (* suppress error output *)
  6705. currentScope := module.moduleScope;
  6706. VisitImport(x);
  6707. IF ~error THEN
  6708. module.moduleScope.AddImport(x);
  6709. x.SetScope(module.moduleScope);
  6710. END;
  6711. currentScope := prevScope;
  6712. diagnostics := prevDiagnostics;
  6713. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6714. END AddImport;
  6715. (** check and resolve import
  6716. - check for name = SYSTEM
  6717. - check for forbidden self import
  6718. - search through global import cache: already imported?
  6719. - check if already imported indirectly
  6720. - import if necessary -> set module and enter into import cache
  6721. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6722. - after this import this direct import and all indirect imports are stored in the current module's import list
  6723. **)
  6724. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6725. VAR
  6726. module: SyntaxTree.Module;
  6727. moduleScope: SyntaxTree.ModuleScope;
  6728. import,reimport: SyntaxTree.Import;
  6729. filename: FileName;
  6730. prevScope: SyntaxTree.Scope;
  6731. BEGIN
  6732. IF SymbolNeedsResolution(x) THEN
  6733. prevScope := currentScope;
  6734. x.SetType(SyntaxTree.importType);
  6735. moduleScope := currentScope.ownerModule.moduleScope;
  6736. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6737. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6738. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6739. Error(x.position,"forbidden self import");
  6740. ELSE
  6741. (* search through global import list: already imported ? *)
  6742. IF (x.module = NIL) & (importCache # NIL) THEN
  6743. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6744. ELSE import := NIL
  6745. END;
  6746. IF x.module # NIL THEN (* already imported indirectly *)
  6747. module := x.module;
  6748. ELSIF import # NIL THEN (* already in module list *)
  6749. module := import.module;
  6750. ASSERT(module # NIL);
  6751. x.SetModule(module);
  6752. ELSE (* must be imported *)
  6753. Global.ModuleFileName(x.moduleName,x.context,filename);
  6754. IF symbolFileFormat # NIL THEN
  6755. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6756. IF module = NIL THEN
  6757. ErrorSS(x.position,"could not import",filename);
  6758. IF VerboseErrorMessage THEN
  6759. Printout.Info("import",x)
  6760. END
  6761. ELSE
  6762. (*
  6763. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6764. (*! should rather be done by importer *)
  6765. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6766. checker.importCache := importCache;
  6767. checker.arrayBaseImported := arrayBaseImported;
  6768. checker.global := global;
  6769. checker.Module(module); (* semantic check *)
  6770. error := error OR checker.error;
  6771. END;
  6772. *)
  6773. (*
  6774. ASSERT(SyntaxTree.Resolved IN module.state);
  6775. *)
  6776. x.SetModule(module);
  6777. IF importCache # NIL THEN
  6778. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6779. import.SetContext(x.context);
  6780. import.SetModule(module);
  6781. importCache.AddImport(import);
  6782. END;
  6783. END;
  6784. ELSE
  6785. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6786. END;
  6787. END;
  6788. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6789. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6790. END;
  6791. import := module.moduleScope.firstImport;
  6792. WHILE(import # NIL) DO
  6793. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6794. ASSERT(currentScope # NIL);
  6795. ASSERT(currentScope.ownerModule # NIL);
  6796. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6797. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6798. Error(x.position,"recursive import");
  6799. ELSE
  6800. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6801. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6802. IF reimport = NIL THEN (* indirect import *)
  6803. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6804. reimport.SetContext(import.context);
  6805. reimport.SetModule(import.module);
  6806. moduleScope.AddImport(reimport);
  6807. reimport.SetScope(moduleScope);
  6808. ELSE
  6809. ASSERT(import.module # NIL);
  6810. reimport.SetModule(import.module); (* direct or indirect import *)
  6811. END;
  6812. END;
  6813. import := import.nextImport;
  6814. END;
  6815. END;
  6816. END;
  6817. currentScope := prevScope;
  6818. (* ELSE nothing to be done *)
  6819. x.SetState(SyntaxTree.Resolved);
  6820. END;
  6821. END VisitImport;
  6822. (*** statements ***)
  6823. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6824. VAR prev,resolved: SyntaxTree.Statement;
  6825. BEGIN
  6826. prev := resolvedStatement;
  6827. resolvedStatement := x;
  6828. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6829. activeCellsStatement := FALSE;
  6830. x.Accept(SELF);
  6831. (* removed this, implementation restriction should be resolved by backend
  6832. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6833. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6834. END;
  6835. *)
  6836. resolved := resolvedStatement;
  6837. resolvedStatement := prev;
  6838. RETURN resolved
  6839. END ResolveStatement;
  6840. (** check and resolve statement sequence
  6841. - check all statements, replace if necessary
  6842. **)
  6843. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6844. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6845. BEGIN
  6846. IF statementSequence # NIL THEN (* else empty *)
  6847. FOR i := 0 TO statementSequence.Length()-1 DO
  6848. statement := statementSequence.GetStatement(i);
  6849. resolved := ResolveStatement(statement);
  6850. IF (resolved # statement) THEN
  6851. statementSequence.SetStatement(i,resolved);
  6852. END;
  6853. END;
  6854. END;
  6855. END StatementSequence;
  6856. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6857. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6858. - check if procedure is callable
  6859. - check return type = NIL (otherwise must be assignment statement)
  6860. **)
  6861. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6862. VAR call: SyntaxTree.Designator;
  6863. BEGIN
  6864. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6865. call := procedureCall.call;
  6866. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6867. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6868. END;
  6869. call := ResolveDesignator(call);
  6870. IF call = SyntaxTree.invalidDesignator THEN
  6871. (* error already handled *)
  6872. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6873. (* inline call in a statement *)
  6874. ELSIF ~IsCallable(call) THEN
  6875. Error(procedureCall.position,"called object is not a procedure");
  6876. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6877. Error(procedureCall.position,"calling procedure with non-void return type");
  6878. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6879. END;
  6880. procedureCall.SetCall(call);
  6881. (*
  6882. IF call = SyntaxTree.invalidDesignator THEN
  6883. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6884. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6885. IF IsOberonInline(procedure) THEN
  6886. Warning(procedure.position,"call to inline proc");
  6887. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6888. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6889. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6890. resolvedStatement := block;
  6891. RETURN;
  6892. END;
  6893. END;
  6894. *)
  6895. END VisitProcedureCallStatement;
  6896. (** check and resolve assignment LHS := RHS
  6897. - resolve LHS and RHS
  6898. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6899. - check if assignment is compatible
  6900. - check if LHS is variable (i.e. assignable)
  6901. - convert RHS if necessary
  6902. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6903. - assignment between different ASOTs
  6904. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6905. - assignment to ASOT elements:
  6906. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6907. **)
  6908. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6909. VAR
  6910. left: SyntaxTree.Designator;
  6911. right, expression: SyntaxTree.Expression;
  6912. designator: SyntaxTree.Designator;
  6913. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6914. mathArrayType: SyntaxTree.MathArrayType;
  6915. BEGIN
  6916. right := ResolveExpression(assignment.right);
  6917. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6918. left := ResolveDesignator(assignment.left);
  6919. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6920. (* error already handled *)
  6921. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6922. (* LHS is index write operator call on ASOT *)
  6923. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6924. (* necessary ?
  6925. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6926. type := procedureType.firstParameter.type;
  6927. expression := procedureCallDesignator.parameters.GetExpression(0);
  6928. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6929. *)
  6930. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6931. ELSIF CheckVariable(left) THEN
  6932. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6933. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6934. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6935. (* conversion done by procedure call
  6936. (* try to convert to left argument *)
  6937. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6938. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6939. procedureCallDesignator.parameters.SetExpression(1, right);
  6940. END;
  6941. *)
  6942. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6943. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6944. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6945. ELSIF AssignmentCompatible(left, right) THEN
  6946. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6947. mathArrayType := MathArrayStructureOfType(left.type);
  6948. right := NewConversion(right.position, right, mathArrayType, NIL);
  6949. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6950. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6951. ELSE
  6952. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6953. assignment.SetLeft(left);
  6954. assignment.SetRight(right);
  6955. resolvedStatement := assignment
  6956. END
  6957. END
  6958. END
  6959. END VisitAssignment;
  6960. (** check and resolve assignment LHS := RHS
  6961. - resolve LHS and RHS
  6962. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6963. - check if assignment is compatible
  6964. - check if LHS is variable (i.e. assignable)
  6965. - convert RHS if necessary
  6966. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6967. - assignment between different ASOTs
  6968. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6969. - assignment to ASOT elements:
  6970. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6971. **)
  6972. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6973. VAR
  6974. left: SyntaxTree.Designator;
  6975. right: SyntaxTree.Expression;
  6976. inPort, outPort: SyntaxTree.PortType;
  6977. expression: SyntaxTree.Expression;
  6978. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6979. BEGIN
  6980. right := ResolveExpression(communication.right);
  6981. left := ResolveDesignator(communication.left);
  6982. communication.SetLeft(left);
  6983. communication.SetRight(right);
  6984. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6985. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6986. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6987. (* conversion done by procedure call
  6988. (* try to convert to left argument *)
  6989. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6990. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6991. procedureCallDesignator.parameters.SetExpression(1, right);
  6992. END;
  6993. *)
  6994. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6995. ELSE
  6996. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6997. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6998. (* error already handled *)
  6999. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7000. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7001. IF outPort.direction # SyntaxTree.OutPort THEN
  7002. Error(left.position,"not an out-port")
  7003. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7004. Error(left.position,"incompatible to port type");
  7005. ELSE
  7006. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7007. communication.SetRight(right)
  7008. END;
  7009. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7010. IF CheckVariable(left) THEN
  7011. IF inPort.direction # SyntaxTree.InPort THEN
  7012. Error(left.position,"not an in-port")
  7013. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7014. Error(right.position,"incompatible to port type");
  7015. END;
  7016. END;
  7017. ELSE
  7018. Error(communication.position,"unsupported stream operation");
  7019. END;
  7020. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7021. IF outPort.direction # SyntaxTree.OutPort THEN
  7022. Error(left.position,"not an out-port")
  7023. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7024. Error(left.position,"incompatible to port type");
  7025. ELSE
  7026. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7027. communication.SetRight(right)
  7028. END;
  7029. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7030. IF CheckVariable(right) THEN
  7031. IF inPort.direction # SyntaxTree.InPort THEN
  7032. Error(left.position,"not an in-port")
  7033. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7034. Error(right.position,"incompatible to port type");
  7035. END;
  7036. END;
  7037. ELSE
  7038. Error(communication.position, "unsupported operation");
  7039. END;
  7040. END;
  7041. END VisitCommunicationStatement;
  7042. (** check and resolve if/eslif part
  7043. - check condition
  7044. - check statement sequence
  7045. **)
  7046. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7047. VAR prevUnreachable, b: BOOLEAN;
  7048. BEGIN
  7049. prevUnreachable := currentIsUnreachable;
  7050. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7051. IF IsBooleanValue(ifPart.condition,b) THEN
  7052. IF b=FALSE THEN
  7053. currentIsUnreachable := TRUE
  7054. ELSIF b=TRUE THEN
  7055. true := TRUE
  7056. END;
  7057. END;
  7058. StatementSequence(ifPart.statements);
  7059. currentIsUnreachable := prevUnreachable;
  7060. END IfPart;
  7061. (** check and resolve if statement
  7062. - check if parts and else part statement sequence
  7063. **)
  7064. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7065. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7066. BEGIN
  7067. prevUnreachable := currentIsUnreachable;
  7068. ifPartTrue := FALSE;
  7069. IfPart(ifStatement.ifPart,ifPartTrue);
  7070. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7071. elsif := ifStatement.GetElsifPart(i);
  7072. IfPart(elsif,ifPartTrue);
  7073. END;
  7074. IF ifStatement.elsePart # NIL THEN
  7075. IF ifPartTrue THEN
  7076. currentIsUnreachable := TRUE
  7077. END;
  7078. StatementSequence(ifStatement.elsePart)
  7079. END;
  7080. currentIsUnreachable := prevUnreachable;
  7081. END VisitIfStatement;
  7082. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7083. VAR variable: SyntaxTree.Designator;
  7084. type,variableType: SyntaxTree.Type;
  7085. withEntry: WithEntry;
  7086. BEGIN
  7087. variable := ResolveDesignator(withPart.variable);
  7088. variableType := variable.type.resolved;
  7089. withPart.SetVariable(variable);
  7090. type := ResolveType(withPart.type);
  7091. withPart.SetType(type);
  7092. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7093. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7094. END;
  7095. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7096. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7097. Error(variable.position,"is not extensible designator");
  7098. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7099. Error(variable.position,"is no local variable ");
  7100. IF VerboseErrorMessage THEN
  7101. Printout.Info("variable",variable)
  7102. END;
  7103. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7104. Error(variable.position,"withguarded symbol is no type extension of ");
  7105. IF VerboseErrorMessage THEN
  7106. Printout.Info("variable",variable);
  7107. Printout.Info("type",type);
  7108. END;
  7109. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7110. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7111. Error(variable.position,"withguarded symbol is no variable ");
  7112. IF VerboseErrorMessage THEN
  7113. Printout.Info("variable",variable);
  7114. Printout.Info("type",type);
  7115. END;
  7116. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7117. Error(variable.position,"invalid change of withguarded symbol");
  7118. ELSE
  7119. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7120. NEW(withEntry);
  7121. withEntry.previous := withEntries;
  7122. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7123. withEntry.type := type;
  7124. withEntries := withEntry;
  7125. StatementSequence(withPart.statements);
  7126. withEntries := withEntries.previous;
  7127. END;
  7128. END WithPart;
  7129. (** check and resolve with statement WITH variable: type DO ... END;
  7130. - check type and variable
  7131. - check that variable type is type extension of type
  7132. - check that variable is a variable
  7133. - enter new with scope and enter guardedVariable with same name and reference to variable
  7134. - create if statement:
  7135. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7136. **)
  7137. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7138. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7139. BEGIN
  7140. prevScope := currentScope; symbol := NIL;
  7141. FOR i := 0 TO withStatement.WithParts()-1 DO
  7142. WithPart(withStatement.GetWithPart(i),symbol);
  7143. END;
  7144. IF withStatement.elsePart # NIL THEN
  7145. StatementSequence(withStatement.elsePart)
  7146. END;
  7147. currentScope := prevScope;
  7148. END VisitWithStatement;
  7149. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7150. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7151. - check 'first' < 'last' and no overlaps between different case labels
  7152. - check statement sequence
  7153. **)
  7154. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7155. VAR
  7156. i: LONGINT;
  7157. position: Position;
  7158. expression, left, right: SyntaxTree.Expression;
  7159. expressionType: SyntaxTree.Type;
  7160. l, r: LONGINT;
  7161. cl, cr: CHAR;
  7162. thiscases: SyntaxTree.CaseConstant;
  7163. BEGIN
  7164. thiscases := NIL;
  7165. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7166. expression := casePart.elements.GetExpression(i);
  7167. position := expression.position;
  7168. (* set context of range *)
  7169. IF expression IS SyntaxTree.RangeExpression THEN
  7170. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7171. END;
  7172. expression := ResolveExpression(expression);
  7173. IF expression = SyntaxTree.invalidExpression THEN
  7174. (* error already reported *)
  7175. expressionType := SyntaxTree.invalidType;
  7176. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7177. (* read out 'first' and 'last' *)
  7178. left := expression(SyntaxTree.RangeExpression).first;
  7179. right := expression(SyntaxTree.RangeExpression).last;
  7180. (* guaranteed by VisitRangeExpression: *)
  7181. ASSERT((left # NIL) & (right # NIL));
  7182. ASSERT(left.type.resolved = right.type.resolved);
  7183. left := CompatibleConversion(left.position, left, type);
  7184. right := CompatibleConversion(right.position, right, type);
  7185. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7186. expression(SyntaxTree.RangeExpression).SetLast(right);
  7187. expressionType := RegularType(position,left.type);
  7188. ELSE
  7189. expression := ConstantExpression(expression);
  7190. expression := CompatibleConversion(expression.position, expression, type);
  7191. (*
  7192. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7193. left := Global.NewCharacterValue(system,expression.position,cl);
  7194. expression := casePart.elements.GetExpression(i);
  7195. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7196. expression := left
  7197. END;
  7198. *)
  7199. casePart.elements.SetExpression(i,expression);
  7200. left := expression; right := expression;
  7201. expressionType := RegularType(position,expression.type)
  7202. END;
  7203. IF (expressionType = SyntaxTree.invalidType) THEN
  7204. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7205. Error(position, "inadmissible case label");
  7206. expression := SyntaxTree.invalidExpression;
  7207. ELSE
  7208. l := 0; r := 0;
  7209. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7210. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7211. l := ORD(cl); r := ORD(cr);
  7212. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7213. ELSE
  7214. expression := SyntaxTree.invalidExpression
  7215. END;
  7216. IF expression # SyntaxTree.invalidExpression THEN
  7217. IF l>r THEN
  7218. Error(position, "empty case label")
  7219. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7220. Error(position, "duplicate case label");
  7221. ELSE
  7222. IF l < min THEN min := l END;
  7223. IF r > max THEN max := r END;
  7224. END;
  7225. END;
  7226. END;
  7227. casePart.elements.SetExpression(i,expression);
  7228. END;
  7229. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7230. casePart.SetConstants(thiscases);
  7231. StatementSequence(casePart.statements);
  7232. END CasePart;
  7233. (** check and resolve case statement CASE variable OF ... END;
  7234. - check variable
  7235. - check case parts
  7236. **)
  7237. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7238. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7239. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7240. BEGIN
  7241. expression := ResolveExpression(caseStatement.variable);
  7242. type := RegularType(expression.position,expression.type);
  7243. IF type = SyntaxTree.invalidType THEN
  7244. expression := SyntaxTree.invalidExpression;
  7245. ELSIF IsIntegerType(type) THEN
  7246. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7247. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7248. (*
  7249. expression := Global.NewCharacterValue(system,expression.position,ch);
  7250. *)
  7251. type := expression.type;
  7252. ELSIF IsCharacterType(type) THEN
  7253. ELSIF IsEnumerationType(type) THEN
  7254. ELSE
  7255. Error(caseStatement.variable.position,"variable must be integer or character type");
  7256. expression := SyntaxTree.invalidExpression;
  7257. END;
  7258. caseStatement.SetVariable(expression);
  7259. caseList := NIL;
  7260. min := MAX(LONGINT); max := MIN(LONGINT);
  7261. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7262. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7263. END;
  7264. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7265. msg := "huge sparse case table ";
  7266. Strings.AppendInt(msg, max-min);
  7267. Strings.Append(msg,"/");
  7268. Strings.AppendInt(msg, caseStatement.CaseParts());
  7269. Warning(caseStatement.position,msg);
  7270. END;
  7271. caseStatement.SetMinMax(min,max);
  7272. StatementSequence(caseStatement.elsePart);
  7273. IF expression.resolved # NIL THEN
  7274. IF IsCharacterValue(expression,ch) THEN
  7275. l := ORD(ch)
  7276. ELSIF IsIntegerValue(expression,l) THEN
  7277. END;
  7278. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7279. END;
  7280. END VisitCaseStatement;
  7281. (** check and resolve while statement
  7282. - check condition
  7283. - check statement sequence
  7284. **)
  7285. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7286. VAR prevIsUnreachable,b: BOOLEAN;
  7287. BEGIN
  7288. prevIsUnreachable := currentIsUnreachable;
  7289. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7290. IF IsBooleanValue(whileStatement.condition,b) THEN
  7291. IF b=FALSE THEN
  7292. currentIsUnreachable := TRUE
  7293. END;
  7294. END;
  7295. StatementSequence(whileStatement.statements);
  7296. currentIsUnreachable := prevIsUnreachable
  7297. END VisitWhileStatement;
  7298. (** check and resolve repeat statement
  7299. - check condition
  7300. - check statement sequence
  7301. **)
  7302. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7303. BEGIN
  7304. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7305. StatementSequence(repeatStatement.statements);
  7306. END VisitRepeatStatement;
  7307. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7308. VAR withEntry: WithEntry;
  7309. BEGIN
  7310. withEntry := withEntries;
  7311. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7312. withEntry := withEntry.previous
  7313. END;
  7314. IF withEntry = NIL THEN RETURN FALSE
  7315. ELSE
  7316. type := withEntry.type;
  7317. RETURN TRUE
  7318. END;
  7319. END GetGuard;
  7320. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7321. - check that variable is an integer variable
  7322. - check that from is integer typed with compatible type
  7323. - check that to has compatible type
  7324. - check that by is constant integer with compatible type
  7325. **)
  7326. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7327. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7328. BEGIN
  7329. designator := ResolveDesignator(forStatement.variable);
  7330. type := SyntaxTree.invalidType;
  7331. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7332. designator := SyntaxTree.invalidDesignator;
  7333. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7334. Error(designator.position,"control variable of non-integer type");
  7335. designator := SyntaxTree.invalidDesignator;
  7336. ELSIF CheckVariable(designator) THEN
  7337. type := designator.type;
  7338. END;
  7339. forStatement.SetVariable(designator);
  7340. expression := ResolveExpression(forStatement.from);
  7341. IF expression = SyntaxTree.invalidExpression THEN
  7342. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7343. Error(expression.position,"start value of incompatible type");
  7344. expression := SyntaxTree.invalidExpression;
  7345. ELSIF type # SyntaxTree.invalidType THEN
  7346. expression := NewConversion(expression.position,expression,type,NIL)
  7347. END;
  7348. forStatement.SetFrom(expression);
  7349. expression := ResolveExpression(forStatement.to);
  7350. IF expression = SyntaxTree.invalidExpression THEN
  7351. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7352. Error(expression.position,"end value of incompatible type");
  7353. expression := SyntaxTree.invalidExpression;
  7354. ELSIF type # SyntaxTree.invalidType THEN
  7355. expression := NewConversion(expression.position,expression,type,NIL)
  7356. END;
  7357. forStatement.SetTo(expression);
  7358. IF forStatement.by # NIL THEN
  7359. expression := ConstantInteger(forStatement.by);
  7360. ELSE
  7361. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7362. END;
  7363. IF expression = SyntaxTree.invalidExpression THEN
  7364. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7365. Error(expression.position,"step value of incompatible type");
  7366. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7367. Error(expression.position,"invalid step value");
  7368. ELSIF type # SyntaxTree.invalidType THEN
  7369. expression := NewConversion(expression.position,expression,type,NIL)
  7370. END;
  7371. forStatement.SetBy(expression);
  7372. StatementSequence(forStatement.statements);
  7373. END VisitForStatement;
  7374. (** check and resolve loop statement LOOP StatementSequence END
  7375. - check statement sequence
  7376. **)
  7377. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7378. BEGIN
  7379. StatementSequence(loopStatement.statements)
  7380. END VisitLoopStatement;
  7381. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7382. BEGIN
  7383. StatementSequence(exitableBlock.statements);
  7384. END VisitExitableBlock;
  7385. (** check and resolve exit statement EXIT
  7386. - check that exit is within LOOP statement block
  7387. **)
  7388. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7389. VAR outer: SyntaxTree.Statement;
  7390. BEGIN
  7391. outer := exitStatement.outer;
  7392. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7393. outer := outer.outer;
  7394. END;
  7395. IF outer = NIL THEN
  7396. Error(exitStatement.position,"exit statement not within loop statement");
  7397. END;
  7398. END VisitExitStatement;
  7399. (** check and resolve return statement RETURN [expression]
  7400. - check expression (if any)
  7401. - check if in procedure scope
  7402. - if in procedure scope then check expression compatibility
  7403. - if not in procecdure scope then check on return without expression
  7404. **)
  7405. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7406. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7407. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7408. BEGIN
  7409. position := returnStatement.position;
  7410. expression := returnStatement.returnValue;
  7411. IF expression # NIL THEN
  7412. expression := ResolveExpression(expression);
  7413. returnStatement.SetReturnValue(expression);
  7414. END;
  7415. outer := returnStatement.outer;
  7416. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7417. outer := outer.outer
  7418. END;
  7419. IF (outer # NIL) THEN
  7420. scope := outer(SyntaxTree.Body).inScope;
  7421. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7422. IF (expression # NIL) THEN
  7423. Error(position, "return statement with parameter not in procedure scope");
  7424. END;
  7425. ELSE
  7426. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7427. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7428. Error(position, "return statement in procedure that does not return");
  7429. END;
  7430. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7431. IF returnType # NIL THEN
  7432. returnType := returnType.resolved;
  7433. IF expression = NIL THEN
  7434. Error(position, "empty return type in procedure providing a return type")
  7435. ELSIF expression.type = NIL THEN
  7436. Error(position,"returned type incompatible: expression has no type");
  7437. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7438. Error(position, "return type not compatible");
  7439. IF VerboseErrorMessage THEN
  7440. Printout.Info("returnType",returnType);
  7441. Printout.Info("expression",expression);
  7442. END;
  7443. ELSE
  7444. expression := NewConversion(expression.position,expression,returnType,NIL);
  7445. returnStatement.SetReturnValue(expression);
  7446. END;
  7447. ELSIF expression # NIL THEN
  7448. Error(position, "non-empty return type in procedure providing no return type");
  7449. END;
  7450. END;
  7451. END;
  7452. END VisitReturnStatement;
  7453. (** check and resolve await statement AWAIT(condition: Expression)
  7454. - check await condition
  7455. **)
  7456. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7457. VAR condition: SyntaxTree.Expression;
  7458. BEGIN
  7459. condition := ResolveCondition(awaitStatement.condition);
  7460. IF currentIsRealtime THEN
  7461. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7462. END;
  7463. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7464. Error(awaitStatement.position,"senseless await statement with constant condition");
  7465. END;
  7466. awaitStatement.SetCondition(condition);
  7467. END VisitAwaitStatement;
  7468. PROCEDURE CheckSystemImport(position: Position);
  7469. VAR import: SyntaxTree.Import;
  7470. BEGIN
  7471. import := currentScope.ownerModule.moduleScope.firstImport;
  7472. WHILE(import # NIL) DO
  7473. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7474. RETURN;
  7475. END;
  7476. import := import.nextImport;
  7477. END;
  7478. Error(position, "forbidden code without system import");
  7479. END CheckSystemImport;
  7480. (** check and resolve code statement: do nothing, must be done by assembler
  7481. **)
  7482. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7483. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7484. BEGIN
  7485. CheckSystemImport(code.position);
  7486. FOR i := 0 TO code.inRules.Length()-1 DO
  7487. statement := code.inRules.GetStatement(i);
  7488. IF statement IS SyntaxTree.Assignment THEN
  7489. WITH statement: SyntaxTree.Assignment DO
  7490. statement.SetRight(ResolveExpression(statement.right));
  7491. END;
  7492. ELSE
  7493. Error(statement.position, "can only be assignment")
  7494. END;
  7495. END;
  7496. FOR i := 0 TO code.outRules.Length()-1 DO
  7497. statement := code.outRules.GetStatement(i);
  7498. IF statement IS SyntaxTree.Assignment THEN
  7499. WITH statement: SyntaxTree.Assignment DO
  7500. statement.SetLeft(ResolveDesignator(statement.left));
  7501. END;
  7502. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7503. (* must be a reference to some register *)
  7504. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7505. ELSE
  7506. Printout.Info("out statement ", statement);
  7507. Error(statement.position, "(out) can only be assignment")
  7508. END;
  7509. END;
  7510. END VisitCode;
  7511. (** check and set flags of a statement block
  7512. - check for multiply occurence of a flag
  7513. - check and set priority only in bodies
  7514. - check for valid names
  7515. **)
  7516. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7517. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7518. flag: LONGINT; recordBody: SyntaxTree.Body;
  7519. PROCEDURE SetProtectedRecord;
  7520. VAR scope: SyntaxTree.Scope;
  7521. BEGIN
  7522. scope := currentScope;
  7523. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7524. scope := scope.outerScope
  7525. END;
  7526. IF scope # NIL THEN
  7527. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7528. END;
  7529. END SetProtectedRecord;
  7530. BEGIN
  7531. flags := {};
  7532. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7533. recordBody := block(SyntaxTree.Body)
  7534. ELSE
  7535. recordBody := NIL
  7536. END;
  7537. blockModifier := block.blockModifiers;
  7538. WHILE(blockModifier # NIL) DO
  7539. name := blockModifier.identifier;
  7540. expression := blockModifier.expression;
  7541. position := blockModifier.position;
  7542. flag := -1;
  7543. IF name=Global.NamePriority THEN
  7544. IF expression = NIL THEN
  7545. Error(position, "missing priority expression");
  7546. ELSIF recordBody = NIL THEN
  7547. Error(position, "priority not on record body");
  7548. ELSIF recordBody.priority # NIL THEN
  7549. Error(position, "duplicate priority expression");
  7550. ELSE
  7551. recordBody.SetPriority(expression);
  7552. END;
  7553. ELSIF expression # NIL THEN
  7554. Error(expression.position,"expression not in connection with priority")
  7555. ELSIF name=Global.NameExclusive THEN
  7556. IF block.isExclusive THEN
  7557. Error(position, "duplicate exclusive flag")
  7558. END;
  7559. block.SetExclusive(TRUE); SetProtectedRecord;
  7560. ELSIF name=Global.NameActive THEN
  7561. IF recordBody = NIL THEN
  7562. Error(position, "active not in record body");
  7563. ELSIF recordBody.isActive THEN
  7564. Error(position, "duplicate active flag")
  7565. ELSE
  7566. recordBody.SetActive(TRUE); SetProtectedRecord;
  7567. END;
  7568. ELSIF name=Global.NameSafe THEN
  7569. IF recordBody = NIL THEN
  7570. Error(position, "safe not in record body");
  7571. ELSIF recordBody.isSafe THEN
  7572. Error(position, "duplicate safe flag")
  7573. ELSE
  7574. recordBody.SetSafe(TRUE);
  7575. SetProtectedRecord;
  7576. END;
  7577. ELSIF name=Global.NameRealtime THEN
  7578. IF recordBody = NIL THEN
  7579. Error(position, "realtime not in record body");
  7580. ELSIF recordBody.isRealtime THEN
  7581. Error(position, "duplicate realtime flag")
  7582. ELSE
  7583. recordBody.SetRealtime(TRUE);
  7584. block.SetRealtime(TRUE);
  7585. END;
  7586. ELSIF name=Global.NameUnchecked THEN
  7587. IF block.isUnchecked THEN
  7588. Error(position, "duplicate unchecked flag")
  7589. ELSE
  7590. block.SetUnchecked(TRUE);
  7591. END;
  7592. ELSIF (name=Global.NameUncooperative) THEN
  7593. IF block.isUncooperative THEN
  7594. Error(position, "duplicate uncooperative flag")
  7595. ELSE
  7596. block.SetUncooperative(TRUE);
  7597. END;
  7598. ELSE
  7599. Error(position, "unknown block modifier");
  7600. END;
  7601. blockModifier := blockModifier.nextModifier;
  7602. END;
  7603. END BlockFlags;
  7604. (** check and resolve statement block
  7605. - check flags (exclusive)
  7606. - check statement sequence
  7607. **)
  7608. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7609. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7610. BEGIN
  7611. BlockFlags(statementBlock);
  7612. IF statementBlock.isExclusive THEN
  7613. (* check that not in exclusive block *)
  7614. IF currentIsExclusive THEN
  7615. Error (statementBlock.position,"forbidden recursive exclusive")
  7616. ELSIF currentIsRealtime THEN
  7617. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7618. END;
  7619. END;
  7620. recentExclusive := currentIsExclusive;
  7621. recentUnreachable := currentIsUnreachable;
  7622. recentRealtime := currentIsRealtime;
  7623. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7624. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7625. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7626. StatementSequence(statementBlock.statements);
  7627. currentIsRealtime := recentRealtime;
  7628. currentIsExclusive := recentExclusive;
  7629. currentIsUnreachable := recentUnreachable;
  7630. END VisitStatementBlock;
  7631. (** check and resolve body
  7632. - check flags (active, priority, safe)
  7633. - check body and finally part
  7634. **)
  7635. PROCEDURE Body(body: SyntaxTree.Body);
  7636. BEGIN
  7637. VisitStatementBlock(body);
  7638. IF body.isActive THEN
  7639. IF ~currentIsBodyProcedure THEN
  7640. Error(body.position,"active flag not in object body");
  7641. ELSIF body.priority # NIL THEN
  7642. body.SetPriority(ConstantInteger(body.priority));
  7643. END;
  7644. ELSIF body.isSafe THEN
  7645. Error(body.position,"safe flag not in active body");
  7646. ELSIF body.priority # NIL THEN
  7647. Error(body.position,"priority flag not in active body");
  7648. END;
  7649. IF body.code # NIL THEN
  7650. CheckSystemImport(body.position);
  7651. END;
  7652. StatementSequence(body.finally)
  7653. END Body;
  7654. (*** scopes ***)
  7655. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7656. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7657. VAR duplicateSymbol: BOOLEAN;
  7658. BEGIN
  7659. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7660. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7661. Error(symbol.position,"globally defined keyword")
  7662. END;
  7663. scope.EnterSymbol(symbol,duplicateSymbol);
  7664. IF ~allowDuplicate & duplicateSymbol THEN
  7665. Error(symbol.position,"Multiply defined identifier.");
  7666. IF VerboseErrorMessage THEN
  7667. Printout.Info("multiply defined identifier",symbol);
  7668. Printout.Info("in scope",scope);
  7669. END;
  7670. END;
  7671. END Register;
  7672. (**
  7673. implementation: check and resolve an implementation part
  7674. **)
  7675. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7676. move implementation checker to a separate object ? *)
  7677. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7678. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7679. BEGIN
  7680. prevIsRealtime := currentIsRealtime;
  7681. prevIsBodyProcedure := currentIsBodyProcedure;
  7682. prevIsCellNet := currentIsCellNet;
  7683. prevScope := currentScope;
  7684. currentScope := scope;
  7685. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7686. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7687. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7688. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7689. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7690. (*
  7691. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7692. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7693. END;
  7694. *)
  7695. END;
  7696. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7697. (* module body, record bodies are wrapped into an artifical procedure *)
  7698. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7699. Body(scope(SyntaxTree.ProcedureScope).body)
  7700. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7701. Body(scope(SyntaxTree.ProcedureScope).body)
  7702. END;
  7703. END;
  7704. currentScope := prevScope;
  7705. currentIsRealtime := prevIsRealtime;
  7706. currentIsBodyProcedure := prevIsBodyProcedure;
  7707. currentIsCellNet := prevIsCellNet;
  7708. END Implementation;
  7709. (** implementation phase:
  7710. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7711. **)
  7712. PROCEDURE Implementations(x: SyntaxTree.Module);
  7713. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7714. BEGIN
  7715. prevPhase := phase;
  7716. phase := InlinePhase;
  7717. scope := x.firstScope;
  7718. WHILE(scope # NIL) DO
  7719. Implementation(scope);
  7720. scope := scope.nextScope;
  7721. END;
  7722. phase := ImplementationPhase;
  7723. scope := x.firstScope;
  7724. WHILE(scope # NIL) DO
  7725. Implementation(scope);
  7726. scope := scope.nextScope;
  7727. END;
  7728. phase := prevPhase;
  7729. END Implementations;
  7730. (** declaration phase:
  7731. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7732. - import lists (for module scopes)
  7733. - parameter list (for procedure scopes)
  7734. - constant declarations
  7735. - type declarations
  7736. - variable declarations
  7737. - procedure declarations
  7738. preformed in two stages:
  7739. - first all symbols are entered into the symbol table (with uniqueness check),
  7740. - then all symbols are resolved
  7741. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7742. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7743. phases :
  7744. 0 = before procedures
  7745. 1 = procedures and later
  7746. **)
  7747. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7748. VAR
  7749. constant: SyntaxTree.Constant;
  7750. typeDeclaration: SyntaxTree.TypeDeclaration;
  7751. variable: SyntaxTree.Variable;
  7752. procedure: SyntaxTree.Procedure;
  7753. procedureType : SyntaxTree.ProcedureType;
  7754. prevScope: SyntaxTree.Scope;
  7755. parameter: SyntaxTree.Parameter;
  7756. import: SyntaxTree.Import;
  7757. symbol: SyntaxTree.Symbol;
  7758. prevPhase: LONGINT;
  7759. prevError : BOOLEAN;
  7760. i: LONGINT;
  7761. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7762. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7763. BEGIN
  7764. IF type.baseType # NIL THEN
  7765. baseType := type.baseType.resolved;
  7766. IF baseType IS SyntaxTree.PointerType THEN
  7767. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7768. END;
  7769. (*
  7770. IF baseType IS SyntaxTree.CellType THEN
  7771. DeclareCell(baseType(SyntaxTree.CellType));
  7772. END;
  7773. *)
  7774. END;
  7775. parameter := type.firstParameter;
  7776. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7777. (*
  7778. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7779. variable.SetType(parameter.type);
  7780. variable.SetAccess(SyntaxTree.Hidden);
  7781. variable.SetModifiers(parameter.modifiers);
  7782. currentScope.PushVariable(variable);
  7783. *)
  7784. Register(parameter,scope, FALSE);
  7785. parameter := parameter.nextParameter;
  7786. END;
  7787. property := type.firstProperty;
  7788. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7789. (*
  7790. variable := currentScope.FindVariable(property.name);
  7791. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7792. prop := variable(SyntaxTree.Property);
  7793. ELSE (* add, duplicate symbols detection later *)
  7794. prop := SyntaxTree.NewProperty(property.position, property.name);
  7795. currentScope.PushVariable(prop);
  7796. END;
  7797. prop.SetType(property.type);
  7798. prop.SetValue(property.value);
  7799. prop.SetAccess(SyntaxTree.Hidden);
  7800. *)
  7801. Register(property, scope, FALSE);
  7802. property := property.nextProperty;
  7803. END;
  7804. END DeclareCell;
  7805. BEGIN
  7806. prevError := error;
  7807. prevPhase := phase;
  7808. phase := DeclarationPhase;
  7809. prevScope := currentScope;
  7810. currentScope := scope;
  7811. error := FALSE;
  7812. IF 0 IN phases THEN
  7813. (* first enter all symbols in scope *)
  7814. IF scope IS SyntaxTree.ModuleScope THEN
  7815. (* treat imports first for a module scope, , set default context if necessary *)
  7816. import := scope(SyntaxTree.ModuleScope).firstImport;
  7817. WHILE(import # NIL) DO
  7818. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7819. Register(import, currentScope, FALSE);
  7820. import := import.nextImport;
  7821. END;
  7822. import := scope(SyntaxTree.ModuleScope).firstImport;
  7823. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7824. ResolveSymbol(import);
  7825. import := import.nextImport;
  7826. END;
  7827. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7828. (* enter parameters for a procedure scope *)
  7829. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7830. parameter := procedureType.firstParameter;
  7831. WHILE(parameter # NIL) DO
  7832. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7833. END;
  7834. parameter := procedureType.returnParameter;
  7835. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7836. parameter := procedureType.selfParameter;
  7837. IF parameter # NIL THEN
  7838. Register(parameter, currentScope, FALSE);
  7839. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7840. END;
  7841. ELSIF scope IS SyntaxTree.CellScope THEN
  7842. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7843. IF~skipImplementation THEN
  7844. import := scope(SyntaxTree.CellScope).firstImport;
  7845. WHILE(import # NIL) DO
  7846. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7847. Register(import, currentScope, FALSE);
  7848. import := import.nextImport;
  7849. END;
  7850. import := scope(SyntaxTree.CellScope).firstImport;
  7851. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7852. ResolveSymbol(import);
  7853. import := import.nextImport;
  7854. END;
  7855. END;
  7856. END;
  7857. IF error THEN RETURN END;
  7858. IF skipImplementation THEN
  7859. scope.Clear;
  7860. END;
  7861. (* constants *)
  7862. constant := scope.firstConstant;
  7863. WHILE (constant # NIL) DO
  7864. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7865. END;
  7866. (* type declarations *)
  7867. typeDeclaration := scope.firstTypeDeclaration;
  7868. WHILE (typeDeclaration # NIL) DO
  7869. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7870. END;
  7871. (* variables *)
  7872. variable := scope.firstVariable;
  7873. WHILE (variable # NIL) DO
  7874. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7875. END;
  7876. (* procedures *)
  7877. IF scope.procedures # NIL THEN
  7878. FOR i := 0 TO scope.procedures.Length()-1 DO
  7879. procedure := scope.procedures.GetProcedure(i);
  7880. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7881. IF procedureType.selfParameter = NIL THEN
  7882. scope.AddProcedure(procedure);
  7883. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7884. ELSE
  7885. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7886. IF typeDeclaration = NIL THEN
  7887. Error(procedureType.selfParameter.position, "No such type declaration");
  7888. ELSE
  7889. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7890. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7891. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7892. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7893. END;
  7894. END;
  7895. END;
  7896. END;
  7897. END;
  7898. (* now process all symbols without any presumption on the order *)
  7899. symbol := scope.firstSymbol;
  7900. WHILE(symbol # NIL) DO
  7901. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7902. IF (symbol IS SyntaxTree.Procedure) THEN
  7903. IF 1 IN phases THEN
  7904. ResolveSymbol(symbol);
  7905. END;
  7906. ELSE
  7907. IF 0 IN phases THEN
  7908. ResolveSymbol(symbol);
  7909. END;
  7910. END;
  7911. END;
  7912. symbol := symbol.nextSymbol;
  7913. END;
  7914. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7915. symbol := scope.firstSymbol;
  7916. WHILE symbol # NIL DO
  7917. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7918. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7919. pointerFixes.Add(symbol, currentScope);
  7920. END;
  7921. IF ~symbol.type.resolved.isRealtime THEN
  7922. Error(symbol.position,"symbol has no realtime type");
  7923. END;
  7924. END;
  7925. symbol := symbol.nextSymbol
  7926. END;
  7927. END;
  7928. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7929. Error(Basic.invalidPosition,"problems during offset computation in module");
  7930. END;
  7931. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7932. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7933. scope.ownerModule.AddScope(scope);
  7934. END;
  7935. phase := prevPhase;
  7936. currentScope := prevScope;
  7937. error := error OR prevError;
  7938. END Declarations;
  7939. (* nopov *)
  7940. (** check if all operators from one module are compatible to the ones in the other module
  7941. - check if there are not multiple operators with the same signature
  7942. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7943. - check for all operators whose signatures are compatible, whether the return types are compatible
  7944. note that:
  7945. - the return type is not considered to be part of the signature
  7946. - two signatures are considered compatible, if all of the operands are compatible
  7947. **)
  7948. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7949. VAR
  7950. thisOperator, thatOperator: SyntaxTree.Operator;
  7951. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7952. thisParameter, thatParameter: SyntaxTree.Parameter;
  7953. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7954. i: LONGINT;
  7955. BEGIN
  7956. currentScope := thisModuleScope;
  7957. hasError := FALSE;
  7958. (* go through all operators in the other module *)
  7959. thatOperator := thatModuleScope.firstOperator;
  7960. WHILE (thatOperator # NIL) & ~hasError DO
  7961. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7962. (* the other operator is accessible *)
  7963. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7964. (* the other operator is not the conversion operator *)
  7965. (* go through all operators in this module *)
  7966. thisOperator := thisModuleScope.firstOperator;
  7967. WHILE (thisOperator # NIL) & ~hasError DO
  7968. IF thisOperator # thatOperator THEN
  7969. (* the operators are not the same *)
  7970. IF thisOperator.name = thatOperator.name THEN
  7971. (* the operators share the same identifier *)
  7972. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7973. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7974. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7975. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7976. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7977. (* both operators have the same paramter count *)
  7978. thisParameter := thisProcedureType.firstParameter;
  7979. thatParameter := thatProcedureType.firstParameter;
  7980. operandsAreEqual := TRUE;
  7981. operandsAreCompatible := TRUE;
  7982. (* go through all parameters *)
  7983. FOR i := 1 TO thisProcedureType.numberParameters DO
  7984. ASSERT(thatParameter # NIL);
  7985. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7986. operandsAreEqual := FALSE;
  7987. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7988. operandsAreCompatible := FALSE
  7989. END
  7990. END;
  7991. thisParameter := thisParameter.nextParameter;
  7992. thatParameter := thatParameter.nextParameter
  7993. END;
  7994. IF operandsAreEqual THEN
  7995. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7996. hasError := TRUE
  7997. ELSIF operandsAreCompatible THEN
  7998. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7999. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8000. hasError := TRUE
  8001. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8002. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8003. hasError := TRUE
  8004. END
  8005. END
  8006. END
  8007. END
  8008. END;
  8009. thisOperator := thisOperator.nextOperator
  8010. END
  8011. END
  8012. END;
  8013. thatOperator := thatOperator.nextOperator
  8014. END
  8015. END CheckInterOperatorConformity;
  8016. (** check module:
  8017. - check module declaration
  8018. - add context, if necessary
  8019. - remove module from import cache, if necessary
  8020. - check declarations
  8021. - resolve all type fixes
  8022. - check implementation (bodies)
  8023. **)
  8024. PROCEDURE Module*(x: SyntaxTree.Module);
  8025. VAR (* nopov *)
  8026. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8027. BEGIN
  8028. prevScope := currentScope;
  8029. prevIsCellNet := currentIsCellNet;
  8030. module := x;
  8031. ASSERT(x # NIL);
  8032. global := system.globalScope[x.case];
  8033. x.moduleScope.SetGlobalScope(global);
  8034. currentScope := global;
  8035. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8036. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8037. RemoveModuleFromCache(importCache,x);
  8038. Declarations(x.moduleScope, FALSE, {0,1});
  8039. FixTypes();
  8040. IF module.isCellNet THEN
  8041. currentIsCellNet := TRUE;
  8042. modifier := x.modifiers;
  8043. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8044. CheckModifiers(modifier, FALSE);
  8045. END;
  8046. (* nopov *)
  8047. IF ~error THEN
  8048. (* check if operators conform to each other within this module *)
  8049. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8050. (* go through all imports *)
  8051. import := x.moduleScope.firstImport;
  8052. WHILE import # NIL DO
  8053. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8054. (* check if all operators in this module conform to the ones of the imported module *)
  8055. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8056. END;
  8057. import := import.nextImport
  8058. END;
  8059. END;
  8060. Implementations(x);
  8061. module := NIL;
  8062. currentIsCellNet := prevIsCellNet;
  8063. currentScope := prevScope;
  8064. END Module;
  8065. END Checker;
  8066. Warnings*=OBJECT (SyntaxTree.Visitor)
  8067. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8068. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8069. BEGIN
  8070. SELF.diagnostics := diagnostics
  8071. END InitWarnings;
  8072. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8073. BEGIN END VisitPortType;
  8074. (** types *)
  8075. PROCEDURE Type(x: SyntaxTree.Type);
  8076. BEGIN x.Accept(SELF)
  8077. END Type;
  8078. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8079. BEGIN END VisitType;
  8080. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8081. BEGIN END VisitBasicType;
  8082. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8083. BEGIN END VisitCharacterType;
  8084. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8085. BEGIN END VisitIntegerType;
  8086. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8087. BEGIN END VisitFloatType;
  8088. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8089. BEGIN END VisitQualifiedType;
  8090. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8091. BEGIN END VisitStringType;
  8092. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8093. BEGIN END VisitEnumerationType;
  8094. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8095. BEGIN END VisitRangeType;
  8096. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8097. BEGIN
  8098. IF ~(SyntaxTree.Warned IN x.state) THEN
  8099. x.SetState(SyntaxTree.Warned);
  8100. Type(x.arrayBase);
  8101. END;
  8102. END VisitArrayType;
  8103. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8104. BEGIN
  8105. IF ~(SyntaxTree.Warned IN x.state) THEN
  8106. x.SetState(SyntaxTree.Warned);
  8107. Type(x.arrayBase);
  8108. END;
  8109. END VisitMathArrayType;
  8110. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8111. BEGIN
  8112. IF ~(SyntaxTree.Warned IN x.state) THEN
  8113. x.SetState(SyntaxTree.Warned);
  8114. Type(x.pointerBase);
  8115. END;
  8116. END VisitPointerType;
  8117. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8118. BEGIN Scope(x.recordScope) END VisitRecordType;
  8119. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8120. BEGIN Scope(x.cellScope) END VisitCellType;
  8121. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8122. BEGIN END VisitProcedureType;
  8123. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8124. VAR msg: ARRAY 256 OF CHAR;
  8125. BEGIN
  8126. Global.GetSymbolName(x,msg);
  8127. Strings.Append(msg," ");
  8128. Strings.Append(msg,text);
  8129. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8130. END Warning;
  8131. (** symbols *)
  8132. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8133. BEGIN
  8134. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8135. IF ~(x IS SyntaxTree.Parameter) THEN
  8136. Warning(x,"never used");
  8137. END;
  8138. END;
  8139. x.Accept(SELF);
  8140. END Symbol;
  8141. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8142. BEGIN END VisitSymbol;
  8143. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8144. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8145. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8146. BEGIN END VisitConstant;
  8147. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8148. BEGIN END VisitVariable;
  8149. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8150. BEGIN END VisitProperty;
  8151. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8152. BEGIN END VisitParameter;
  8153. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8154. BEGIN
  8155. Scope(x.procedureScope)
  8156. END VisitProcedure;
  8157. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8158. BEGIN END VisitOperator;
  8159. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8160. BEGIN END VisitImport;
  8161. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8162. VAR
  8163. symbol: SyntaxTree.Symbol;
  8164. BEGIN
  8165. symbol := scope.firstSymbol;
  8166. WHILE(symbol # NIL) DO
  8167. Symbol(symbol);
  8168. symbol := symbol.nextSymbol;
  8169. END;
  8170. END Scope;
  8171. PROCEDURE Module*(x: SyntaxTree.Module);
  8172. BEGIN
  8173. SELF.module := x;
  8174. Scope(x.moduleScope);
  8175. END Module;
  8176. END Warnings;
  8177. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8178. BEGIN
  8179. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8180. END IsOberonInline;
  8181. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8182. BEGIN
  8183. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8184. END Resolved;
  8185. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8186. VAR i: LONGINT;
  8187. BEGIN
  8188. i := 1;
  8189. WHILE i < x DO
  8190. i := i *2
  8191. END;
  8192. RETURN i=x
  8193. END PowerOf2;
  8194. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8195. BEGIN
  8196. RETURN
  8197. (scope # NIL) &
  8198. (scope IS SyntaxTree.ModuleScope)
  8199. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8200. OR
  8201. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8202. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8203. END IsCellNetScope;
  8204. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8205. BEGIN
  8206. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8207. END IsCellScope;
  8208. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8209. BEGIN
  8210. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8211. RETURN (scope # NIL) & IsCellNetScope(scope)
  8212. END InCellNetScope;
  8213. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8214. BEGIN
  8215. ASSERT(size MOD system.dataUnit = 0);
  8216. RETURN size DIV system.dataUnit
  8217. END ToMemoryUnits;
  8218. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8219. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8220. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8221. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8222. BEGIN
  8223. IF t = NIL THEN
  8224. RETURN TRUE
  8225. ELSE
  8226. t := t.resolved;
  8227. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8228. END;
  8229. END TypeAllowed;
  8230. BEGIN
  8231. type := type.resolved;
  8232. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8233. RETURN FALSE
  8234. ELSE
  8235. procedureType := type(SyntaxTree.ProcedureType);
  8236. numberParameters := procedureType.numberParameters;
  8237. RETURN
  8238. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8239. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8240. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8241. END;
  8242. END GetProcedureAllowed;
  8243. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8244. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8245. VAR import: SyntaxTree.Import;
  8246. BEGIN
  8247. import := importCache.ImportByModuleName(x.name,x.context);
  8248. IF import # NIL THEN
  8249. importCache.RemoveImporters(x.name,x.context);
  8250. END;
  8251. END RemoveModuleFromCache;
  8252. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8253. (* to <- this assignment compatibility *)
  8254. VAR result: BOOLEAN;
  8255. BEGIN
  8256. IF this= NIL THEN result := (to=NIL)
  8257. ELSIF to=NIL THEN result := FALSE
  8258. ELSE
  8259. (*! will be replaced by this:
  8260. ELSE result := this.CompatibleTo(to.resolved);
  8261. *)
  8262. this := this.resolved; to := to.resolved;
  8263. IF to=SyntaxTree.invalidType THEN result := FALSE
  8264. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8265. ELSIF to = this THEN
  8266. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8267. ELSIF to IS SyntaxTree.BasicType THEN
  8268. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8269. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8270. result := this.CompatibleTo(to.resolved)
  8271. ELSE
  8272. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8273. END
  8274. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8275. result := to.sizeInBits = this.sizeInBits;
  8276. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8277. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8278. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8279. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8280. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8281. result := TRUE;
  8282. ELSIF to IS SyntaxTree.AnyType THEN
  8283. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  8284. ELSIF to IS SyntaxTree.ObjectType THEN
  8285. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  8286. ELSIF to IS SyntaxTree.ByteType THEN
  8287. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8288. ELSIF to IS SyntaxTree.CharacterType THEN
  8289. result := IsCharacterType(this)
  8290. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8291. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8292. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8293. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8294. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8295. result := TRUE;
  8296. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8297. result := TRUE;
  8298. ELSE
  8299. result := FALSE
  8300. END;
  8301. ELSIF to IS SyntaxTree.PointerType THEN
  8302. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8303. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8304. & (~to.isRealtime OR this.isRealtime);
  8305. ELSIF to IS SyntaxTree.ProcedureType THEN
  8306. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8307. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8308. & (~to.isRealtime OR this.isRealtime)
  8309. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8310. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8311. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8312. ELSIF to IS SyntaxTree.RecordType THEN
  8313. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8314. ELSIF to IS SyntaxTree.ArrayType THEN
  8315. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8316. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8317. ELSIF StaticArrayCompatible(to, this) THEN
  8318. result := TRUE
  8319. ELSE
  8320. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8321. END;
  8322. ELSIF to IS SyntaxTree.MathArrayType THEN
  8323. IF this IS SyntaxTree.MathArrayType THEN
  8324. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8325. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8326. result := TRUE;
  8327. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8328. result := TRUE;
  8329. ELSE
  8330. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8331. END;
  8332. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8333. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8334. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8335. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8336. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8337. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8338. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8339. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8340. ELSE
  8341. result := FALSE
  8342. END;
  8343. (* an array-structured object type is compatible to the type of its array structure *)
  8344. ELSIF IsArrayStructuredObjectType(this) THEN
  8345. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8346. ELSE
  8347. result := FALSE;
  8348. END;
  8349. ELSIF to IS SyntaxTree.StringType THEN
  8350. result := FALSE;
  8351. ELSIF to IS SyntaxTree.EnumerationType THEN
  8352. result := IsEnumerationExtension(this,to);
  8353. ELSIF to IS SyntaxTree.PortType THEN
  8354. result := SameType(to, this)
  8355. ELSE
  8356. Printout.Info("CompatibleTo",to);
  8357. HALT(100); (* implement missing type check *)
  8358. END;
  8359. END;
  8360. RETURN result
  8361. END CompatibleTo;
  8362. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8363. VAR actualBase, formalBase: SyntaxTree.Type;
  8364. BEGIN
  8365. IF SameType(formal,actual) THEN
  8366. RETURN TRUE
  8367. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8368. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8369. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8370. RETURN
  8371. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8372. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8373. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8374. & StaticArrayCompatible(formalBase,actualBase)
  8375. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8376. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8377. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8378. RETURN
  8379. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8380. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8381. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8382. & StaticArrayCompatible(formalBase,actualBase)
  8383. ELSE RETURN FALSE
  8384. END;
  8385. END StaticArrayCompatible;
  8386. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8387. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8388. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8389. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8390. BEGIN
  8391. result := SameType(formal,actual);
  8392. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8393. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8394. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8395. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8396. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8397. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8398. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8399. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8400. & TC(formalBase, actualBase);
  8401. END;
  8402. RETURN result
  8403. END TC;
  8404. BEGIN
  8405. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8406. ELSE
  8407. arrayBase := formalType.arrayBase.resolved;
  8408. IF (actualType IS SyntaxTree.StringType) THEN
  8409. result := arrayBase IS SyntaxTree.CharacterType
  8410. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8411. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8412. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8413. result := TC(formalType, actualType);
  8414. ELSE
  8415. result := (arrayBase IS SyntaxTree.ByteType)
  8416. END;
  8417. END;
  8418. RETURN result
  8419. END OpenArrayCompatible;
  8420. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8421. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8422. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8423. BEGIN
  8424. IF actualType IS SyntaxTree.MathArrayType THEN
  8425. actualArray := actualType(SyntaxTree.MathArrayType);
  8426. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8427. (*
  8428. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8429. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8430. *)
  8431. actualBase := ArrayBase(actualType,Infinity);
  8432. formalBase := ArrayBase(formalType,Infinity);
  8433. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8434. ELSE
  8435. (*
  8436. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8437. *)
  8438. formalBase := Resolved(formalType.arrayBase);
  8439. actualBase := Resolved(actualArray.arrayBase);
  8440. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8441. (*
  8442. ARRAY [k] -> ARRAY [n]
  8443. *)
  8444. result := (formalType.staticLength = actualArray.staticLength)
  8445. ELSE
  8446. result := TRUE
  8447. END;
  8448. IF ~result THEN
  8449. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8450. ELSIF actualBase = NIL THEN result := FALSE
  8451. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8452. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8453. ELSE
  8454. result := SameType(formalBase,actualBase)
  8455. END;
  8456. END;
  8457. ELSE
  8458. result := FALSE
  8459. END;
  8460. RETURN result
  8461. END MathArrayCompatible;
  8462. (**
  8463. Math Array Type distance for assignments / parameter passings of the form
  8464. from -> to
  8465. variants:
  8466. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8467. allowed:
  8468. static -> static (& size match)
  8469. static -> open
  8470. static -> tensor
  8471. open -> open
  8472. open -> tensor
  8473. open -> static
  8474. tensor -> tensor
  8475. tensor -> open
  8476. tensor -> static
  8477. **)
  8478. (*! think about the metric here: is form matching more important than element type matching? *)
  8479. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8480. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8481. BEGIN
  8482. fromBase := Resolved(from.arrayBase);
  8483. toBase := Resolved(to.arrayBase);
  8484. i := Infinity;
  8485. IF from = to THEN
  8486. i := 0;
  8487. ELSIF (from.form = to.form) THEN
  8488. (* static -> static, open -> open, tensor -> tensor *)
  8489. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8490. IF fromBase = toBase THEN i := 0
  8491. ELSIF toBase = NIL THEN i := 1
  8492. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8493. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8494. ELSE
  8495. i := TypeDistance(system,fromBase, toBase, varpar);
  8496. END;
  8497. END;
  8498. ELSIF (to.form = SyntaxTree.Static) THEN
  8499. (* forbidden *)
  8500. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8501. (* static -> tensor, open -> tensor, tensor -> open *)
  8502. IF toBase=fromBase THEN i := 0;
  8503. ELSIF toBase = NIL THEN i := 1;
  8504. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8505. toBase := ArrayBase(toBase,Infinity);
  8506. IF (fromBase=toBase) THEN i := 0
  8507. ELSIF (toBase = NIL) THEN i:= 1
  8508. ELSIF (fromBase = NIL) THEN i := Infinity;
  8509. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8510. END;
  8511. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8512. fromBase := ArrayBase(fromBase,Infinity);
  8513. IF (fromBase=toBase) THEN i := 0
  8514. ELSIF (toBase = NIL) THEN i := 1
  8515. ELSIF (fromBase = NIL) THEN i := Infinity;
  8516. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8517. END;
  8518. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8519. END;
  8520. IF i # Infinity THEN INC(i,2) END;
  8521. ELSIF (from.form = SyntaxTree.Static) THEN
  8522. (* static -> open *)
  8523. IF toBase=fromBase THEN i := 0
  8524. ELSIF toBase = NIL THEN i := 1
  8525. ELSIF fromBase = NIL THEN i := Infinity
  8526. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8527. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8528. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8529. END;
  8530. IF i # Infinity THEN INC(i,1) END;
  8531. ELSE HALT(100); (* unknown case *)
  8532. END;
  8533. RETURN i;
  8534. END MathArrayTypeDistance;
  8535. (** compute and return the distance of two array types
  8536. - return the distance of the base types
  8537. **)
  8538. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8539. VAR i: LONGINT;
  8540. BEGIN
  8541. i := Infinity;
  8542. IF from = to THEN
  8543. i := 0
  8544. ELSE
  8545. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8546. (*
  8547. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8548. i := TypeDistance(from.base, to.base);
  8549. IF i >= 0 THEN INC(i) END
  8550. ELSIF (from.mode = open) & (to.mode = open) THEN
  8551. i := TypeDistance(from.base, to.base);
  8552. *)
  8553. END;
  8554. RETURN i
  8555. END ArrayTypeDistance;
  8556. (** compute the signature distance of a procedure and an actual parameter list
  8557. - if any of the parameters are not compatible, the result is infinite
  8558. - add up and return the distance over all parameters
  8559. **)
  8560. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8561. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8562. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8563. BEGIN
  8564. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8565. result := Infinity
  8566. ELSE
  8567. formalParameter := procedureType.firstParameter;
  8568. i := 0;
  8569. result := 0;
  8570. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8571. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8572. actualParameter := actualParameters.GetExpression(i);
  8573. ASSERT(formalParameter.type # NIL);
  8574. IF (actualParameter.type = NIL) THEN distance := Infinity
  8575. ELSE
  8576. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8577. END;
  8578. IF distance = Infinity THEN
  8579. result := Infinity;
  8580. ELSE
  8581. to := formalParameter.type.resolved;
  8582. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8583. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8584. (* already handled varpar *)
  8585. (*
  8586. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8587. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8588. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8589. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8590. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8591. END;
  8592. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8593. result := Infinity
  8594. END;
  8595. *)
  8596. INC(result, distance);
  8597. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8598. INC(result, distance);
  8599. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8600. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8601. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8602. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8603. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8604. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8605. END;
  8606. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8607. result := Infinity
  8608. END;
  8609. ELSE
  8610. result := Infinity
  8611. END;
  8612. ELSE
  8613. INC(result,distance);
  8614. END;
  8615. END;
  8616. (*
  8617. Printout.Info("actual=", actualParameter);
  8618. Printout.Info("formal=", formalParameter);
  8619. TRACE(result);
  8620. *)
  8621. formalParameter := formalParameter.nextParameter; INC(i);
  8622. END;
  8623. END;
  8624. ASSERT(result >= 0);
  8625. RETURN result
  8626. END Distance;
  8627. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8628. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8629. BEGIN
  8630. IF right.numberParameters # (procedureType.numberParameters) THEN
  8631. result := Infinity
  8632. ELSE
  8633. formalParameter := procedureType.firstParameter;
  8634. rightParameter := right.firstParameter;
  8635. i := 0;
  8636. result := 0;
  8637. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8638. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8639. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8640. IF distance = Infinity THEN
  8641. result := Infinity;
  8642. ELSE
  8643. INC(result,distance);
  8644. END;
  8645. formalParameter := formalParameter.nextParameter;
  8646. rightParameter := rightParameter.nextParameter;
  8647. END;
  8648. END;
  8649. ASSERT(result >= 0);
  8650. RETURN result
  8651. END ProcedureTypeDistance;
  8652. (** compute and return the distance between two types, used for computation of signature distance
  8653. from -> to
  8654. **)
  8655. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8656. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8657. BEGIN
  8658. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8659. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8660. END;
  8661. i := Infinity;
  8662. IF from = to THEN
  8663. i := 0
  8664. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8665. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8666. i := Infinity;
  8667. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8668. i := 10;
  8669. ELSIF (from IS SyntaxTree.StringType) THEN
  8670. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8671. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8672. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8673. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8674. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8675. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8676. i := 1
  8677. ELSIF (from IS SyntaxTree.NilType) THEN
  8678. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8679. (*
  8680. ELSIF (from = NoType) THEN
  8681. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8682. *)
  8683. ELSIF (from IS SyntaxTree.BasicType) THEN
  8684. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8685. IF varpar & (i # 0) THEN i := Infinity END;
  8686. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8687. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8688. ELSIF (from IS SyntaxTree.RecordType) THEN
  8689. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8690. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8691. IF to IS SyntaxTree.MathArrayType THEN
  8692. (*
  8693. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8694. i := Infinity;
  8695. ELSE
  8696. *)
  8697. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8698. (*
  8699. END;
  8700. *)
  8701. END
  8702. ELSIF (from IS SyntaxTree.PointerType) THEN
  8703. ptr := from(SyntaxTree.PointerType);
  8704. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8705. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8706. (* ELSE i := TypeDistance(ptr.base, to); *)
  8707. END
  8708. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8709. IF (to IS SyntaxTree.ProcedureType) THEN
  8710. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8711. END;
  8712. ELSIF (from IS SyntaxTree.PortType) THEN
  8713. IF (to IS SyntaxTree.PortType) THEN
  8714. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8715. i := 0;
  8716. END;
  8717. END;
  8718. (*no procedure test, procedure must be the same*)
  8719. END;
  8720. RETURN i
  8721. END TypeDistance;
  8722. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8723. BEGIN
  8724. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8725. END IsIntegerType;
  8726. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8727. BEGIN
  8728. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8729. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8730. OR IsPointerType(type)
  8731. )
  8732. END IsAddressType;
  8733. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8734. BEGIN
  8735. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8736. END IsSizeType;
  8737. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8738. BEGIN
  8739. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8740. END IsSignedIntegerType;
  8741. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8742. BEGIN
  8743. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8744. END IsUnsignedIntegerType;
  8745. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8746. VAR result: BOOLEAN;
  8747. BEGIN
  8748. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8749. value := x.resolved(SyntaxTree.IntegerValue).value;
  8750. result := TRUE
  8751. ELSE
  8752. result := FALSE
  8753. END;
  8754. RETURN result
  8755. END IsIntegerValue;
  8756. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8757. VAR result: BOOLEAN;
  8758. BEGIN
  8759. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8760. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8761. result := TRUE
  8762. ELSE
  8763. result := FALSE
  8764. END;
  8765. RETURN result
  8766. END IsEnumerationValue;
  8767. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8768. VAR result: BOOLEAN;
  8769. BEGIN
  8770. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8771. value := x.resolved(SyntaxTree.RealValue).value;
  8772. result := TRUE
  8773. ELSE
  8774. result := FALSE
  8775. END;
  8776. RETURN result
  8777. END IsRealValue;
  8778. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8779. VAR result: BOOLEAN;
  8780. BEGIN
  8781. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8782. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8783. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8784. result := TRUE
  8785. ELSE
  8786. result := FALSE
  8787. END;
  8788. RETURN result
  8789. END IsComplexValue;
  8790. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8791. VAR result: BOOLEAN;
  8792. BEGIN
  8793. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8794. value := x.resolved(SyntaxTree.CharacterValue).value;
  8795. result := TRUE
  8796. ELSE
  8797. result := FALSE
  8798. END;
  8799. RETURN result
  8800. END IsCharacterValue;
  8801. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8802. VAR result: BOOLEAN;
  8803. BEGIN
  8804. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8805. value := x.resolved(SyntaxTree.BooleanValue).value;
  8806. result := TRUE
  8807. ELSE
  8808. result := FALSE
  8809. END;
  8810. RETURN result
  8811. END IsBooleanValue;
  8812. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8813. VAR result: BOOLEAN;
  8814. BEGIN
  8815. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8816. value := x.resolved(SyntaxTree.SetValue).value;
  8817. result := TRUE
  8818. ELSE
  8819. result := FALSE
  8820. END;
  8821. RETURN result
  8822. END IsSetValue;
  8823. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8824. VAR result: BOOLEAN;
  8825. BEGIN
  8826. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8827. value := x.resolved(SyntaxTree.StringValue).value;
  8828. result := TRUE
  8829. ELSE
  8830. result := FALSE
  8831. END;
  8832. RETURN result
  8833. END IsStringValue;
  8834. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8835. BEGIN
  8836. x := x.resolved;
  8837. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8838. END Indexable;
  8839. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8840. BEGIN
  8841. RETURN t1.SameType(t2.resolved);
  8842. END SameType;
  8843. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8844. BEGIN
  8845. IF t IS SyntaxTree.MathArrayType THEN
  8846. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8847. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8848. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8849. DEC(max);
  8850. END;
  8851. ELSIF t IS SyntaxTree.ArrayType THEN
  8852. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8853. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8854. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8855. END;
  8856. END;
  8857. RETURN t;
  8858. END ArrayBase;
  8859. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8860. BEGIN
  8861. type := type.resolved;
  8862. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8863. base := type(SyntaxTree.ArrayType).arrayBase;
  8864. RETURN TRUE;
  8865. END;
  8866. RETURN FALSE;
  8867. END IsOpenArray;
  8868. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8869. BEGIN
  8870. type := type.resolved;
  8871. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8872. base := type(SyntaxTree.ArrayType).arrayBase;
  8873. dim := type(SyntaxTree.ArrayType).staticLength;
  8874. RETURN TRUE
  8875. ELSE
  8876. RETURN FALSE
  8877. END;
  8878. END IsStaticArray;
  8879. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8880. BEGIN
  8881. type := type.resolved;
  8882. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8883. base := type(SyntaxTree.ArrayType).arrayBase;
  8884. RETURN TRUE
  8885. ELSE
  8886. RETURN FALSE
  8887. END;
  8888. END IsDynamicArray;
  8889. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8890. VAR i: LONGINT;
  8891. BEGIN
  8892. i := 0;
  8893. t := t.resolved;
  8894. IF t IS SyntaxTree.MathArrayType THEN
  8895. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8896. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8897. END;
  8898. ELSIF t IS SyntaxTree.ArrayType THEN
  8899. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8900. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8901. END;
  8902. END;
  8903. RETURN i
  8904. END Dimension;
  8905. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8906. BEGIN
  8907. RETURN expression.assignable;
  8908. END IsVariable;
  8909. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8910. BEGIN
  8911. IF (symbol IS SyntaxTree.Parameter) THEN
  8912. WITH symbol: SyntaxTree.Parameter DO
  8913. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8914. END;
  8915. ELSE
  8916. RETURN FALSE
  8917. END;
  8918. END IsVariableParameter;
  8919. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8920. VAR result: BOOLEAN;
  8921. BEGIN
  8922. IF type = NIL THEN result := FALSE
  8923. ELSE
  8924. type := type.resolved;
  8925. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8926. END;
  8927. RETURN result
  8928. END IsPointerType;
  8929. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8930. VAR result: BOOLEAN;
  8931. BEGIN
  8932. IF type = NIL THEN result := FALSE
  8933. ELSE
  8934. type := type.resolved;
  8935. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8936. END;
  8937. RETURN result
  8938. END IsUnsafePointer;
  8939. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8940. BEGIN
  8941. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8942. END IsDisposable;
  8943. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8944. VAR result: BOOLEAN;
  8945. BEGIN
  8946. IF type = NIL THEN result := FALSE
  8947. ELSE
  8948. type := type.resolved;
  8949. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8950. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8951. result := result OR (type IS SyntaxTree.ObjectType);
  8952. END;
  8953. RETURN result
  8954. END IsPointerToRecord;
  8955. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8956. VAR result: BOOLEAN;
  8957. BEGIN
  8958. IF type = NIL THEN result := FALSE
  8959. ELSE
  8960. type := type.resolved;
  8961. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8962. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8963. ;
  8964. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8965. result := result OR (type IS SyntaxTree.ObjectType);
  8966. END;
  8967. RETURN result
  8968. END IsPointerToObject;
  8969. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8970. BEGIN
  8971. IF type # NIL THEN
  8972. RETURN type.resolved.hasPointers
  8973. ELSE
  8974. RETURN FALSE
  8975. END;
  8976. END ContainsPointer;
  8977. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8978. BEGIN
  8979. IF type = NIL THEN RETURN FALSE END;
  8980. type := type.resolved;
  8981. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8982. END IsStringType;
  8983. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8984. BEGIN
  8985. IF type = NIL THEN RETURN FALSE END;
  8986. type := type.resolved;
  8987. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8988. END IsCharacterType;
  8989. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8990. BEGIN
  8991. IF type = NIL THEN RETURN FALSE END;
  8992. type := type.resolved;
  8993. RETURN (type IS SyntaxTree.EnumerationType)
  8994. END IsEnumerationType;
  8995. (** cf. section "Type extension (base type)" in the language report **)
  8996. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8997. VAR result: BOOLEAN;
  8998. BEGIN
  8999. ASSERT(base # NIL); ASSERT(extension # NIL);
  9000. base := base.resolved; extension := extension.resolved;
  9001. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9002. result := TRUE;
  9003. ELSE
  9004. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9005. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9006. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9007. END;
  9008. WHILE (extension # NIL) & (extension # base) DO
  9009. IF extension IS SyntaxTree.RecordType THEN
  9010. extension := extension(SyntaxTree.RecordType).baseType;
  9011. IF (extension # NIL) THEN extension := extension.resolved END;
  9012. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9013. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9014. END;
  9015. ELSE extension := NIL;
  9016. END;
  9017. END;
  9018. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9019. END;
  9020. RETURN result
  9021. END IsTypeExtension;
  9022. (** check if base is the base enumeration type of extension **)
  9023. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9024. BEGIN
  9025. base := base.resolved; extension := extension.resolved;
  9026. WHILE (extension # NIL) & (extension # base) DO
  9027. IF extension IS SyntaxTree.EnumerationType THEN
  9028. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9029. IF extension # NIL THEN extension := extension.resolved END;
  9030. ELSE
  9031. extension := NIL
  9032. END;
  9033. END;
  9034. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9035. END IsEnumerationExtension;
  9036. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9037. BEGIN
  9038. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9039. RETURN TRUE
  9040. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9041. RETURN TRUE
  9042. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9043. RETURN TRUE
  9044. ELSE
  9045. RETURN FALSE
  9046. END
  9047. END IsCallable;
  9048. (** compute and return the distance of two record types
  9049. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9050. **)
  9051. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9052. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9053. BEGIN
  9054. i := 0;
  9055. WHILE (from # NIL) & (from # to) DO
  9056. baseType := from.baseType;
  9057. IF (baseType # NIL) THEN
  9058. baseType := baseType.resolved;
  9059. IF baseType IS SyntaxTree.PointerType THEN
  9060. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9061. END;
  9062. IF baseType IS SyntaxTree.RecordType THEN
  9063. from := baseType(SyntaxTree.RecordType);
  9064. ELSE
  9065. from := NIL;
  9066. END;
  9067. ELSE
  9068. from := NIL
  9069. END;
  9070. INC(i)
  9071. END;
  9072. IF from = NIL THEN i := Infinity END;
  9073. RETURN i
  9074. END RecordTypeDistance;
  9075. (** compute and return the distance of two pointer types **)
  9076. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9077. BEGIN
  9078. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9079. RETURN Infinity;
  9080. ELSE
  9081. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9082. END;
  9083. END PointerTypeDistance;
  9084. (** check if expression contains a symbol designator pointing to a type declaration.
  9085. - if so then enter type declaration into typeDeclaration and return true else return false
  9086. **)
  9087. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9088. VAR result: BOOLEAN;
  9089. BEGIN
  9090. result := FALSE;
  9091. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9092. result := TRUE;
  9093. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9094. END;
  9095. RETURN result
  9096. END IsTypeDesignator;
  9097. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9098. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9099. VAR result: BOOLEAN;
  9100. BEGIN
  9101. type := type.resolved;
  9102. IF type IS SyntaxTree.PointerType THEN
  9103. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9104. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9105. result := TRUE
  9106. ELSE
  9107. result := type IS SyntaxTree.RecordType
  9108. END;
  9109. RETURN result
  9110. END IsExtensibleType;
  9111. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9112. BEGIN
  9113. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9114. (d IS SyntaxTree.SymbolDesignator) &
  9115. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9116. OR
  9117. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9118. END IsUnextensibleRecord;
  9119. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9120. BEGIN
  9121. IF IsUnextensibleRecord(d) THEN
  9122. RETURN FALSE
  9123. ELSE RETURN IsExtensibleType(d.type.resolved)
  9124. END;
  9125. END IsExtensibleDesignator;
  9126. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9127. BEGIN
  9128. type := type.resolved;
  9129. IF (type IS SyntaxTree.PointerType) THEN
  9130. RETURN TRUE
  9131. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9132. RETURN TRUE
  9133. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9134. RETURN TRUE
  9135. ELSIF (type IS SyntaxTree.BasicType) THEN
  9136. RETURN TRUE
  9137. END;
  9138. RETURN FALSE
  9139. END IsBasicType;
  9140. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9141. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9142. BEGIN
  9143. baseType := record.baseType;
  9144. IF (baseType # NIL) THEN
  9145. baseType := baseType.resolved;
  9146. IF (baseType IS SyntaxTree.PointerType) THEN
  9147. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9148. END;
  9149. END;
  9150. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9151. recordType := baseType(SyntaxTree.RecordType);
  9152. ELSE
  9153. recordType := NIL;
  9154. END;
  9155. RETURN recordType
  9156. END RecordBase;
  9157. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9158. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9159. BEGIN
  9160. baseRecord := RecordBase(scope.ownerRecord);
  9161. IF baseRecord = NIL THEN RETURN NIL END;
  9162. scope := baseRecord.recordScope;
  9163. procedureType := procedure.type.resolved;
  9164. IF procedure IS SyntaxTree.Operator THEN
  9165. operator := scope.firstOperator;
  9166. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9167. (*
  9168. Printout.Info("not same ",procedureType);
  9169. Printout.Info("with ",operator.type);
  9170. *)
  9171. operator := operator.nextOperator;
  9172. END;
  9173. super := operator;
  9174. ELSE
  9175. super := scope.firstProcedure;
  9176. WHILE (super # NIL) & (super.name # procedure.name) DO
  9177. super := super.nextProcedure;
  9178. END;
  9179. END;
  9180. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9181. RETURN super
  9182. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9183. RETURN super
  9184. ELSE
  9185. RETURN FindSuperProcedure(scope,procedure);
  9186. END;
  9187. END FindSuperProcedure;
  9188. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9189. VAR procedure: SyntaxTree.Procedure;
  9190. BEGIN
  9191. procedure := record.recordScope.constructor;
  9192. IF procedure = NIL THEN
  9193. record := RecordBase(record);
  9194. IF record # NIL THEN
  9195. procedure := GetConstructor(record)
  9196. END;
  9197. END;
  9198. RETURN procedure;
  9199. END GetConstructor;
  9200. (* enter a case into a list of cases in a sorted way and check for collision *)
  9201. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9202. VAR prev,this,new: SyntaxTree.CaseConstant;
  9203. BEGIN
  9204. this := root;
  9205. prev := NIL;
  9206. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9207. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9208. RETURN FALSE
  9209. ELSE
  9210. IF (this # NIL) & (this.min = max+1) THEN
  9211. this.min := min
  9212. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9213. prev.max := min
  9214. ELSE
  9215. NEW(new); new.min := min; new.max := max;
  9216. new.next := this;
  9217. IF prev = NIL THEN
  9218. root := new;
  9219. ELSE
  9220. prev.next := new
  9221. END
  9222. END;
  9223. RETURN TRUE
  9224. END;
  9225. END EnterCase;
  9226. (** generate and return a new checker object, errors are entered into diagnostics **)
  9227. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9228. VAR checker: Checker;
  9229. BEGIN
  9230. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9231. RETURN checker
  9232. END NewChecker;
  9233. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9234. VAR warnings: Warnings;
  9235. BEGIN
  9236. NEW(warnings, diagnostics); RETURN warnings;
  9237. END NewWarnings;
  9238. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9239. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9240. END IsRangeType;
  9241. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9242. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9243. END IsMathArrayType;
  9244. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9245. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9246. END IsArrayType;
  9247. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9248. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9249. END IsComplexType;
  9250. (** if a type is an array-structured object type *)
  9251. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9252. VAR recordType: SyntaxTree.RecordType;
  9253. BEGIN
  9254. IF type = NIL THEN
  9255. RETURN FALSE
  9256. ELSE
  9257. type := type.resolved;
  9258. IF type IS SyntaxTree.PointerType THEN
  9259. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9260. IF type IS SyntaxTree.RecordType THEN
  9261. recordType := type(SyntaxTree.RecordType);
  9262. RETURN recordType.isObject & recordType.HasArrayStructure()
  9263. ELSE
  9264. RETURN FALSE
  9265. END
  9266. ELSE
  9267. RETURN FALSE
  9268. END
  9269. END
  9270. END IsArrayStructuredObjectType;
  9271. (** the math array structure of a type
  9272. - for math arrays: the array itself
  9273. - for pointers: the math array structure of the pointer base
  9274. - for array-structured object types: the underlying structure
  9275. - for non-math arrays and all other types: NIL
  9276. **)
  9277. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9278. VAR
  9279. result: SyntaxTree.MathArrayType;
  9280. BEGIN
  9281. IF type = NIL THEN
  9282. result := NIL
  9283. ELSE
  9284. type := type.resolved;
  9285. IF type IS SyntaxTree.PointerType THEN
  9286. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9287. END;
  9288. IF type IS SyntaxTree.MathArrayType THEN
  9289. result := type(SyntaxTree.MathArrayType)
  9290. ELSIF type IS SyntaxTree.RecordType THEN
  9291. result := type(SyntaxTree.RecordType).arrayStructure
  9292. ELSE
  9293. result := NIL
  9294. END
  9295. END;
  9296. RETURN result
  9297. END MathArrayStructureOfType;
  9298. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9299. VAR
  9300. result: BOOLEAN;
  9301. rangeExpression: SyntaxTree.RangeExpression;
  9302. BEGIN
  9303. IF x IS SyntaxTree.RangeExpression THEN
  9304. rangeExpression := x(SyntaxTree.RangeExpression);
  9305. result := TRUE;
  9306. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9307. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9308. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9309. ELSE
  9310. result := FALSE
  9311. END;
  9312. RETURN result
  9313. END IsStaticRange;
  9314. (** whether a type is a math array of tensor form **)
  9315. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9316. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9317. END IsTensor;
  9318. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9319. BEGIN
  9320. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9321. length := type(SyntaxTree.MathArrayType).staticLength;
  9322. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9323. RETURN TRUE
  9324. ELSE
  9325. RETURN FALSE
  9326. END;
  9327. END IsStaticMathArray;
  9328. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9329. BEGIN
  9330. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9331. END SymbolHasAddress;
  9332. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9333. BEGIN
  9334. RETURN
  9335. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9336. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9337. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9338. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9339. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9340. ;
  9341. END HasAddress;
  9342. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9343. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9344. BEGIN
  9345. IF (e IS SyntaxTree.Designator) THEN
  9346. d := e(SyntaxTree.Designator);
  9347. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9348. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9349. e := d.left;
  9350. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9351. END;
  9352. IF d # NIL THEN
  9353. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9354. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9355. END;
  9356. END;
  9357. RETURN FALSE;
  9358. END IsLocalVariable;
  9359. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9360. BEGIN
  9361. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9362. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9363. ELSE
  9364. RETURN TRUE
  9365. END;
  9366. END IsStaticProcedure;
  9367. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9368. CONST OptimizeMethodTable = FALSE;
  9369. BEGIN
  9370. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9371. END InMethodTable;
  9372. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9373. BEGIN
  9374. IF type = NIL THEN RETURN FALSE
  9375. ELSE
  9376. type := type.resolved;
  9377. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9378. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9379. END
  9380. END ReturnedAsParameter;
  9381. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9382. BEGIN
  9383. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9384. END StructuredReturnType;
  9385. END FoxSemanticChecker.
  9386. SystemTools.FreeDownTo FoxSemanticChecker ~