FoxSemanticChecker.Mod 402 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224
  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. nextHighest := highest;
  379. WHILE (e # NIL) DO
  380. Register(e,x,FALSE);
  381. IF SymbolNeedsResolution(e) THEN
  382. IF e.value # NIL THEN
  383. value := ConstantExpression(e.value);
  384. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  385. ELSE
  386. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  387. value.SetType(x.ownerEnumeration);
  388. END;
  389. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  390. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  391. IF nextHighest > highest THEN highest := nextHighest END;
  392. END;
  393. e.SetValue(value);
  394. CheckSymbolVisibility(e);
  395. e.SetType(x.ownerEnumeration);
  396. e.SetState(SyntaxTree.Resolved);
  397. END;
  398. e := e.nextConstant;
  399. END;
  400. currentScope := prevScope;
  401. END CheckEnumerationScope;
  402. (**
  403. resolve enumeration type: check enumeration scope
  404. **)
  405. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  406. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  407. lowest, highest: LONGINT;
  408. BEGIN
  409. IF TypeNeedsResolution(x) THEN
  410. IF x.enumerationBase # NIL THEN
  411. position := x.enumerationBase.position;
  412. baseType := ResolveType(x.enumerationBase);
  413. resolved := baseType.resolved;
  414. baseScope := NIL;
  415. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  416. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  417. Error(position, "base type is no enumeration type");
  418. ELSE
  419. enumerationBase := resolved(SyntaxTree.EnumerationType);
  420. lowest := enumerationBase.rangeHighest+1;
  421. END;
  422. x.SetEnumerationBase(baseType);
  423. ELSE lowest := 0;
  424. END;
  425. highest := lowest-1;
  426. CheckEnumerationScope(x.enumerationScope, highest);
  427. x.SetRange(lowest, highest);
  428. x.SetState(SyntaxTree.Resolved);
  429. END;
  430. resolvedType := ResolvedType(x);
  431. END VisitEnumerationType;
  432. (**
  433. resolve range type: nothing to be done
  434. **)
  435. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  436. BEGIN
  437. IF TypeNeedsResolution(x) THEN
  438. x.SetState(SyntaxTree.Resolved);
  439. END;
  440. resolvedType := ResolvedType(x)
  441. END VisitRangeType;
  442. (**
  443. resolve qualified type
  444. - find and resolve named type and set resolved type
  445. **)
  446. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  447. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  448. BEGIN
  449. IF TypeNeedsResolution(x) THEN
  450. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  451. x.SetResolved(type.resolved);
  452. x.SetState(SyntaxTree.Resolved);
  453. x.SetTypeDeclaration (typeDeclaration);
  454. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  455. x.SetResolved(SyntaxTree.invalidType);
  456. END;
  457. resolvedType := x;
  458. END VisitQualifiedType;
  459. (**
  460. resolve array type
  461. - check base type
  462. - array of math array forbidden
  463. - static array of open array forbidden
  464. **)
  465. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  466. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  467. BEGIN
  468. IF TypeNeedsResolution(x) THEN
  469. x.SetArrayBase(ResolveType(x.arrayBase));
  470. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  471. arrayBase := x.arrayBase.resolved;
  472. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  473. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  474. pointerType.SetPointerBase(arrayBase);
  475. pointerType.SetHidden(TRUE);
  476. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  477. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  478. ELSE
  479. x.SetArrayBase(pointerType);
  480. END;
  481. END;
  482. IF x.length # NIL THEN
  483. variableAccessed := FALSE;
  484. e := ResolveExpression(x.length);
  485. IF (e.resolved = NIL) THEN
  486. IF variableAccessed THEN
  487. Error(e.position, "forbidden variable access");
  488. END;
  489. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  490. ELSE
  491. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  492. END;
  493. END;
  494. IF arrayBase IS SyntaxTree.ArrayType THEN
  495. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  496. Error(x.position,"forbidden static array of dynamic array");
  497. END;
  498. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  499. Error(x.position,"forbidden array mixed form");
  500. END;
  501. x.SetHasPointers(arrayBase.hasPointers);
  502. x.SetState(SyntaxTree.Resolved);
  503. END;
  504. resolvedType := ResolvedType(x);
  505. END VisitArrayType;
  506. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  507. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  508. BEGIN
  509. module := currentScope.ownerModule;
  510. IF module.name=name THEN
  511. (* do nothing *)
  512. ELSE
  513. moduleScope := module.moduleScope;
  514. import := moduleScope.FindImport(name);
  515. IF import = NIL THEN
  516. import := SyntaxTree.NewImport(position,name,name,TRUE);
  517. moduleScope.AddImport(import);
  518. Register(import,moduleScope,FALSE);
  519. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  520. VisitImport(import);
  521. ELSIF import.direct=FALSE THEN
  522. import.SetScope(module.moduleScope);
  523. import.SetDirect(TRUE);
  524. IF moduleScope.FindSymbol(import.name) = NIL THEN
  525. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  526. duplicate.SetContext(import.context);
  527. duplicate.SetModule(import.module);
  528. Register(duplicate,moduleScope,TRUE);
  529. VisitImport(duplicate);
  530. END;
  531. END;
  532. import.MarkUsed
  533. END;
  534. END ImportModule;
  535. (**
  536. resolve math array type
  537. - check base type
  538. - open math array of array forbidden
  539. - math array of tensor forbidden
  540. - static array of open array forbidden
  541. **)
  542. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  543. VAR arrayBase: SyntaxTree.Type;
  544. BEGIN
  545. IF TypeNeedsResolution(x) THEN
  546. x.SetArrayBase(ResolveType(x.arrayBase));
  547. IF x.length # NIL THEN
  548. x.SetLength(ConstantIntegerGeq0(x.length));
  549. END;
  550. arrayBase := x.arrayBase;
  551. IF arrayBase # NIL THEN
  552. arrayBase := arrayBase.resolved;
  553. IF arrayBase = SyntaxTree.invalidType THEN
  554. (* error already handled *)
  555. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  556. Error(x.position,"forbidden array mixed form");
  557. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  558. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  559. Error(x.position,"forbidden Tensor Array mix")
  560. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  561. Error(x.position,"forbidden static array of dynamic array")
  562. END;
  563. END;
  564. IF x.form = SyntaxTree.Static THEN
  565. x.SetIncrement(system.SizeOf(arrayBase));
  566. END;
  567. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  568. END;
  569. x.SetState(SyntaxTree.Resolved);
  570. END;
  571. resolvedType := ResolvedType(x);
  572. END VisitMathArrayType;
  573. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  574. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  575. (1) Rec = RECORD ... END; Ptr <---> Rec
  576. Ptr = POINTER TO Rec; ^ |
  577. | |
  578. TypeDesc TypeDesc
  579. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  580. ^ /
  581. | /
  582. TypeDesc <-- /
  583. *)
  584. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  585. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  586. BEGIN
  587. Strings.IntToStr(x.position.start,number);
  588. COPY(prefix,name);
  589. Strings.Append(name,"@");
  590. Strings.Append(name,number);
  591. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  592. typeDeclaration.SetDeclaredType(x);
  593. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  594. x.SetTypeDeclaration(typeDeclaration);
  595. currentScope.AddTypeDeclaration(typeDeclaration);
  596. typeDeclaration.SetScope(currentScope);
  597. END AnonymousTypeDeclaration;
  598. (**
  599. deferred pointer type resolving
  600. - resolve base type
  601. - check that base type is a record or array type
  602. - if error then set base type to invalid type
  603. **)
  604. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  605. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  606. BEGIN
  607. ASSERT(type.pointerBase # NIL);
  608. position := type.pointerBase.position;
  609. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  610. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  611. (* not for pointers, a type is needed for the records only
  612. IF type.typeDeclaration = NIL THEN
  613. AnonymousTypeDeclaration(type);
  614. END;
  615. *)
  616. END;
  617. resolved := ResolveType(type.pointerBase);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  619. type.SetPointerBase(resolved);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  621. recordType := resolved.resolved(SyntaxTree.RecordType);
  622. IF recordType.isObject & (recordType.baseType # NIL) THEN
  623. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  624. Error(position, "base type of object must be a realtime object");
  625. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  626. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  627. END;
  628. END;
  629. END;
  630. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  631. Error(position, "realtime object contains references to non-realtime objects");
  632. END;
  633. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  634. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  635. Error(position, "forbidden unsafe at static array");
  636. ELS
  637. *)
  638. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  639. Error(position, "forbidden unsafe at multidimensional array");
  640. END;
  641. END;
  642. ELSE
  643. Error(position, "forbidden pointer base type");
  644. type.SetPointerBase(SyntaxTree.invalidType)
  645. END
  646. END FixPointerType;
  647. (**
  648. resolve pointer type
  649. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  650. **)
  651. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  652. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  653. modifiers: SyntaxTree.Modifier; position: Position;
  654. BEGIN
  655. IF TypeNeedsResolution(x) THEN
  656. modifiers := x.modifiers;
  657. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  658. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  659. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  660. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  661. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  662. (* inheritance cycle check
  663. example:
  664. A=POINTER TO RECORD(B) END;
  665. B=POINTER TO RECORD(A) END;
  666. *)
  667. IF x.pointerBase IS SyntaxTree.RecordType THEN
  668. recordType := x.pointerBase(SyntaxTree.RecordType);
  669. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  670. recordBaseType := ResolveType(recordType.baseType);
  671. recordType.SetBaseType(recordBaseType);
  672. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  673. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  674. END;
  675. CheckModifiers(modifiers, TRUE);
  676. typeFixes.Add(x,currentScope);
  677. x.SetState(SyntaxTree.Resolved);
  678. END;
  679. resolvedType := ResolvedType(x)
  680. END VisitPointerType;
  681. (**
  682. resolve port type
  683. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  684. **)
  685. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  686. VAR value: LONGINT;
  687. BEGIN
  688. IF TypeNeedsResolution(x) THEN
  689. x.SetCellsAreObjects(cellsAreObjects);
  690. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  691. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  692. x.SetSize(value)
  693. ELSE
  694. x.SetSize(system.SizeOf(system.longintType));
  695. END;
  696. x.SetState(SyntaxTree.Resolved);
  697. END;
  698. resolvedType := ResolvedType(x)
  699. END VisitPortType;
  700. (**
  701. deferred procedure type resolving
  702. - resolve return type
  703. - traverse and resolve parameters
  704. **)
  705. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  706. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  707. BEGIN
  708. resolved := ResolveType(procedureType.returnType);
  709. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  710. Error(procedureType.position,"forbidden open array return type");
  711. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  712. Error(procedureType.position,"procedure with return type does not return");
  713. END;
  714. procedureType.SetReturnType(resolved);
  715. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  716. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  717. parameter.SetType(procedureType.returnType);
  718. parameter.SetAccess(SyntaxTree.Hidden);
  719. parameter.SetUntraced(procedureType.hasUntracedReturn);
  720. VisitParameter(parameter);
  721. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  722. END;
  723. (* process parameters *)
  724. parameter :=procedureType.firstParameter;
  725. WHILE (parameter # NIL) DO
  726. VisitParameter(parameter);
  727. parameter := parameter.nextParameter;
  728. END;
  729. parameter := procedureType.selfParameter;
  730. IF parameter # NIL THEN
  731. VisitParameter(parameter)
  732. END;
  733. END FixProcedureType;
  734. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  735. VAR prev,this: SyntaxTree.Modifier;
  736. BEGIN
  737. this := modifiers;prev := NIL;
  738. WHILE (this # NIL) & (this.identifier # name) DO
  739. prev := this; this := this.nextModifier;
  740. END;
  741. IF this # NIL THEN
  742. IF this.expression # NIL THEN
  743. Error(this.position,"unexpected expression");
  744. END;
  745. this.Resolved;
  746. position := this.position;
  747. RETURN TRUE
  748. ELSE
  749. RETURN FALSE
  750. END;
  751. END HasFlag;
  752. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  753. VAR prev,this: SyntaxTree.Modifier;
  754. BEGIN
  755. this := modifiers;prev := NIL;
  756. WHILE (this # NIL) & (this.identifier # name) DO
  757. prev := this; this := this.nextModifier;
  758. END;
  759. IF this # NIL THEN
  760. IF this.expression = NIL THEN
  761. Error(this.position,"expected expression value");
  762. ELSE
  763. this.SetExpression(ConstantExpression(this.expression));
  764. IF CheckIntegerValue(this.expression,value) THEN END;
  765. END;
  766. this.Resolved;
  767. position := this.position;
  768. RETURN TRUE
  769. ELSE RETURN FALSE
  770. END;
  771. END HasValue;
  772. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  773. VAR prev,this: SyntaxTree.Modifier;
  774. BEGIN
  775. this := modifiers;prev := NIL;
  776. WHILE (this # NIL) & (this.identifier # name) DO
  777. prev := this; this := this.nextModifier;
  778. END;
  779. IF this # NIL THEN
  780. IF this.expression = NIL THEN
  781. Error(this.position,"expected expression value");
  782. ELSE
  783. this.SetExpression(ConstantExpression(this.expression));
  784. IF CheckStringValue(this.expression,value) THEN END;
  785. END;
  786. this.Resolved;
  787. position := this.position;
  788. RETURN TRUE
  789. ELSE RETURN FALSE
  790. END;
  791. END HasStringValue;
  792. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  793. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  794. BEGIN
  795. IF cellsAreObjects THEN RETURN FALSE END;
  796. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  797. IF svalue = "A2" THEN
  798. RETURN TRUE
  799. END;
  800. END;
  801. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  802. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  803. END;
  804. RETURN FALSE;
  805. (*
  806. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  807. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  808. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  809. IF svalue[0] = "~" THEN
  810. Strings.TrimLeft(svalue, "~");
  811. IF svalue = backendName THEN
  812. RETURN TRUE;
  813. END;
  814. ELSIF svalue # backendName THEN
  815. RETURN TRUE;
  816. END;
  817. END;
  818. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  819. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  820. END;
  821. RETURN FALSE;
  822. *)
  823. END SkipImplementation;
  824. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  825. VAR this: SyntaxTree.Modifier;
  826. BEGIN
  827. this := modifiers;
  828. WHILE this # NIL DO
  829. IF ~this.resolved THEN
  830. IF checkUse THEN
  831. Error(this.position,"unexpected modifier");
  832. ELSE
  833. this.SetExpression(ResolveExpression(this.expression));
  834. this.Resolved;
  835. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  836. END;
  837. END;
  838. this := this.nextModifier
  839. END;
  840. END CheckModifiers;
  841. (**
  842. resolve procedure type
  843. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  844. **)
  845. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  846. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  847. BEGIN
  848. IF TypeNeedsResolution(procedureType) THEN
  849. modifiers := procedureType.modifiers;
  850. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  851. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  852. procedureType.SetInterrupt(TRUE);
  853. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  854. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  855. IF useDarwinCCalls THEN (*fld*)
  856. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  857. ELSE
  858. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  859. END
  860. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  861. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  862. Error(position, "undefined platform calling convention");
  863. ELSE
  864. procedureType.SetCallingConvention(system.platformCallingConvention);
  865. END;
  866. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  867. procedureType.SetNoReturn(TRUE);
  868. END;
  869. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  870. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  871. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  872. CheckModifiers(modifiers, TRUE);
  873. modifiers := procedureType.returnTypeModifiers;
  874. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  875. CheckModifiers(modifiers, TRUE);
  876. typeFixes.Add(procedureType,currentScope);
  877. procedureType.SetHasPointers(procedureType.isDelegate);
  878. procedureType.SetState(SyntaxTree.Resolved);
  879. END;
  880. resolvedType := ResolvedType(procedureType)
  881. END VisitProcedureType;
  882. (** check and resolve record type
  883. - check base type: must be record, math array or array-structured object type
  884. - check declarations
  885. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  886. **)
  887. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  888. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  889. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  890. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  891. hasPointers: BOOLEAN;
  892. modifiers: SyntaxTree.Modifier;
  893. value: LONGINT;
  894. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  895. BEGIN
  896. type := type.resolved;
  897. IF (type IS SyntaxTree.PointerType) &
  898. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  899. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  900. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  901. RETURN TRUE
  902. ELSE
  903. RETURN FALSE
  904. END;
  905. END IsPointerToRecord;
  906. BEGIN
  907. IF TypeNeedsResolution(x) THEN
  908. hasPointers := FALSE;
  909. modifiers := x.modifiers;
  910. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit)
  911. END;
  912. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  913. CheckModifiers(modifiers, TRUE);
  914. IF x.baseType # NIL THEN
  915. position := x.baseType.position;
  916. baseType := ResolveType(x.baseType);
  917. resolved := baseType.resolved;
  918. hasPointers := hasPointers OR resolved.hasPointers;
  919. IF x.isObject THEN (* object *)
  920. ASSERT(x.pointerType # NIL);
  921. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  922. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  923. baseType := NIL
  924. ELSIF IsPointerToRecord(resolved,recordType) THEN
  925. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  926. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  927. ELSE
  928. Error(position,"object does not extend pointer to record, object or math array ")
  929. END;
  930. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  931. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  932. ELSIF IsPointerToRecord(resolved,recordType) THEN
  933. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  934. ELSIF resolved IS SyntaxTree.RecordType THEN
  935. ELSE
  936. Error(position,"pointer to record does not extend pointer to record or record")
  937. END;
  938. ELSE
  939. IF resolved IS SyntaxTree.RecordType THEN
  940. ELSE
  941. Error(position,"record does not extend record")
  942. END;
  943. END;
  944. x.SetBaseType(baseType);
  945. IF x.Level() > 15 THEN
  946. Error(position, "record/object inheritance level too high");
  947. (* note:
  948. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  949. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  950. inheritance history of a type.
  951. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  952. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  953. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  954. *)
  955. END;
  956. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  957. Error(position, "invalid inheritance of disposable types");
  958. END;
  959. END;
  960. Declarations(x.recordScope, FALSE, {0});
  961. x.SetState(SyntaxTree.Resolved);
  962. Declarations(x.recordScope, FALSE, {1});
  963. ResolveArrayStructure(x);
  964. (* computation of sizes and offsets skipped -> done in backend / system *)
  965. recordBase := x.GetBaseRecord();
  966. IF recordBase = NIL THEN numberMethods := 0
  967. ELSE numberMethods := recordBase.recordScope.numberMethods
  968. END;
  969. isRealtime := TRUE;
  970. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  971. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  972. END;
  973. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  974. WHILE symbol # NIL DO
  975. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  976. IF symbol IS SyntaxTree.Variable THEN
  977. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  978. END;
  979. IF symbol IS SyntaxTree.Procedure THEN
  980. procedure := symbol(SyntaxTree.Procedure);
  981. IF procedure.super # NIL THEN
  982. procedure.SetMethodNumber(procedure.super.methodNumber);
  983. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  984. procedure.SetMethodNumber(numberMethods);
  985. INC(numberMethods);
  986. END;
  987. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  988. Error(procedure.position,"realtime procedure in non-realtime object")
  989. END;
  990. END;
  991. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  992. Error(symbol.position,"non-realtime symbol in realtime object")
  993. END;
  994. symbol := symbol.nextSymbol;
  995. END;
  996. IF isRealtime THEN x.SetRealtime(TRUE) END;
  997. x.recordScope.SetNumberMethods(numberMethods);
  998. (* TODO: is this needed anymore? *)
  999. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1000. Error(x.position,"object extends a record")
  1001. END;
  1002. IF ~x.isAbstract THEN
  1003. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1004. Error(x.position, "non-abstract object contains abstract procedure");
  1005. END;
  1006. ELSE
  1007. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1008. Error(x.position, "abstract object does not contain an abstract method");
  1009. END;
  1010. END;
  1011. IF (x.typeDeclaration = NIL) THEN
  1012. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1013. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1014. (*
  1015. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1016. AnonymousTypeDeclaration(x,name);
  1017. *)
  1018. ELSE
  1019. AnonymousTypeDeclaration(x,"Anonymous");
  1020. END;
  1021. END;
  1022. x.SetHasPointers(hasPointers);
  1023. x.SetState(SyntaxTree.Resolved);
  1024. END;
  1025. resolvedType := ResolvedType(x);
  1026. END VisitRecordType;
  1027. (** check and resolve cell type
  1028. - check base type: must be cell
  1029. - check declarations
  1030. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1031. **)
  1032. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1033. VAR
  1034. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1035. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1036. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1037. recordBase: SyntaxTree.RecordType;
  1038. numberMethods, int: LONGINT;
  1039. real: LONGREAL;
  1040. bool: BOOLEAN;
  1041. set: SET;
  1042. v: SyntaxTree.Expression;
  1043. str: Scanner.StringType;
  1044. atype: SyntaxTree.ArrayType;
  1045. prev: SyntaxTree.Scope;
  1046. skip: BOOLEAN;
  1047. svalue: ARRAY 32 OF CHAR;
  1048. BEGIN
  1049. IF TypeNeedsResolution(x) THEN
  1050. recordBase := NIL;
  1051. IF cellsAreObjects THEN
  1052. IF x.baseType = NIL THEN
  1053. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1054. ImportModule(qualifiedIdentifier.prefix, x.position);
  1055. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1056. x.SetBaseType(ResolveType(x.baseType));
  1057. recordBase := x.GetBaseRecord();
  1058. IF recordBase = NIL THEN
  1059. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1060. END;
  1061. ELSE
  1062. x.SetBaseType(ResolveType(x.baseType));
  1063. END;
  1064. ELSE
  1065. x.SetBaseType(ResolveType(x.baseType));
  1066. END;
  1067. IF recordBase = NIL THEN numberMethods := 0
  1068. ELSE numberMethods := recordBase.recordScope.numberMethods
  1069. END;
  1070. modifier := x.modifiers;
  1071. (*IF ~x.isCellNet THEN*)
  1072. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1073. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1074. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1075. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1076. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1077. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1078. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1079. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1080. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1081. symbol := system.activeCellsCapabilities;
  1082. WHILE symbol # NIL DO
  1083. IF HasFlag(modifier, symbol.name, position) THEN END;
  1084. symbol := symbol.nextSymbol;
  1085. END;
  1086. modifier := x.modifiers;
  1087. WHILE (modifier # NIL) DO
  1088. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1089. IF modifier.expression # NIL THEN
  1090. v := ConstantExpression(modifier.expression);
  1091. property.SetValue(v);
  1092. IF IsIntegerValue(modifier.expression, int) THEN
  1093. (*property.SetValue(modifier.expression);*)
  1094. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1095. property.SetType(system.longintType);
  1096. ELSIF IsRealValue(modifier.expression, real) THEN
  1097. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1098. property.SetType(system.longrealType);
  1099. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1100. property.SetType(system.booleanType);
  1101. ELSIF IsSetValue(modifier.expression, set) THEN
  1102. property.SetType(system.setType);
  1103. ELSIF IsStringValue(modifier.expression, str) THEN
  1104. (*property.SetValue(modifier.expression);*)
  1105. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1106. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1107. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1108. property.SetType(atype);
  1109. ELSE
  1110. Error(modifier.position, "unsupported property type");
  1111. END;
  1112. ELSE (* flag property *)
  1113. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1114. property.SetType(system.booleanType);
  1115. END;
  1116. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1117. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1118. x.AddProperty(property);
  1119. modifier := modifier.nextModifier;
  1120. END;
  1121. CheckModifiers(modifier, FALSE);
  1122. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1123. (* process parameters *)
  1124. prev := currentScope;
  1125. currentScope := x.cellScope;
  1126. parameter :=x.firstParameter;
  1127. WHILE (parameter # NIL) DO
  1128. VisitParameter(parameter);
  1129. type := parameter.type.resolved;
  1130. IF ~(type IS SyntaxTree.PortType) THEN
  1131. WHILE IsStaticArray(type, type, len) DO
  1132. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1133. END;
  1134. WHILE IsDynamicArray(type, type) DO
  1135. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1136. END;
  1137. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1138. Error(parameter.position, "invalid type, must be port or static array of port ");
  1139. END;
  1140. END;
  1141. parameter := parameter.nextParameter;
  1142. END;
  1143. currentScope := prev;
  1144. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1145. WHILE symbol # NIL DO
  1146. IF symbol IS SyntaxTree.Variable THEN
  1147. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1148. END;
  1149. symbol := symbol.nextSymbol;
  1150. END;
  1151. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1152. IF (x.typeDeclaration = NIL) THEN
  1153. AnonymousTypeDeclaration(x,"Anonymous");
  1154. END;
  1155. x.SetState(SyntaxTree.Resolved);
  1156. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1157. Warning(x.position, "Forbidden empty Body.");
  1158. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1159. Warning(x.position, "Non-empty body for an engine?");
  1160. END;
  1161. END;
  1162. resolvedType := ResolvedType(x);
  1163. END VisitCellType;
  1164. (* check if an object is an array-structured object type
  1165. - determine the array structure
  1166. - collect operators from top to bottom in the inheritance hierarchy
  1167. - check if LEN operator is declared
  1168. - determine number of possible index operators
  1169. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1170. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1171. *)
  1172. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1173. VAR
  1174. indexOperatorCount, i: LONGINT;
  1175. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1176. isTensor: BOOLEAN;
  1177. BEGIN
  1178. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1179. (* determine array structure *)
  1180. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1181. END;
  1182. IF recordType.HasArrayStructure() THEN
  1183. (* the object is an ASOT *)
  1184. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1185. (* reset array access operators *)
  1186. arrayAccessOperators.len := NIL;
  1187. arrayAccessOperators.generalRead := NIL;
  1188. arrayAccessOperators.generalWrite := NIL;
  1189. IF isTensor THEN
  1190. (* all operators of dimensionalities 1 to max *)
  1191. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1192. ELSE
  1193. (* all operators of certain dimensionality *)
  1194. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1195. END;
  1196. NEW(arrayAccessOperators.read, indexOperatorCount);
  1197. NEW(arrayAccessOperators.write, indexOperatorCount);
  1198. FOR i := 0 TO indexOperatorCount - 1 DO
  1199. arrayAccessOperators.read[i] := NIL;
  1200. arrayAccessOperators.write[i] := NIL
  1201. END;
  1202. (* collect access operators in the record scope *)
  1203. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1204. IF arrayAccessOperators.len = NIL THEN
  1205. (* TODO: think about making this operator optional for static array structures *)
  1206. Error(recordType.position, "LEN operator missing")
  1207. END;
  1208. (* show error messages *)
  1209. IF isTensor THEN
  1210. (* require ARRAY [*] OF RANGE *)
  1211. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1212. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1213. ELSE
  1214. (* forbid ARRAY [*] OF RANGE *)
  1215. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1216. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1217. (* require RANGE, RANGE, ... RANGE *)
  1218. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1219. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1220. END;
  1221. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1222. ELSE
  1223. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1224. IF recordType.recordScope.firstOperator # NIL THEN
  1225. RETURN;
  1226. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1227. END
  1228. END
  1229. END ResolveArrayStructure;
  1230. (** collect array access operators in a record scope **)
  1231. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1232. VAR
  1233. baseType: SyntaxTree.Type;
  1234. operator: SyntaxTree.Operator;
  1235. isReadOperator, isGeneralOperator: BOOLEAN;
  1236. indexListSize, indexListKind, hashValue: LONGINT;
  1237. BEGIN
  1238. (* if a parent record scope exists, collect the operators there first *)
  1239. baseType := recordScope.ownerRecord.baseType;
  1240. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1241. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1242. END;
  1243. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1244. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1245. END;
  1246. (* go through all operators in the current record scope *)
  1247. operator := recordScope.firstOperator;
  1248. WHILE operator # NIL DO
  1249. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1250. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1251. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1252. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1253. IF isGeneralOperator THEN
  1254. IF isReadOperator THEN
  1255. arrayAccessOperators.generalRead := operator
  1256. ELSE
  1257. arrayAccessOperators.generalWrite := operator
  1258. END
  1259. ELSE
  1260. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1261. IF isReadOperator THEN
  1262. arrayAccessOperators.read[hashValue] := operator
  1263. ELSE
  1264. arrayAccessOperators.write[hashValue] := operator
  1265. END
  1266. END
  1267. END
  1268. ELSE
  1269. Error(operator.position, 'invalid operator')
  1270. END;
  1271. operator := operator.nextOperator
  1272. END
  1273. END CollectArrayAccessOperators;
  1274. (** the hash value of an index operator **)
  1275. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1276. VAR result: LONGINT;
  1277. BEGIN
  1278. IF isTensor THEN
  1279. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1280. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1281. ELSE
  1282. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1283. END
  1284. ELSE
  1285. result := indexListKind
  1286. END;
  1287. RETURN result
  1288. END IndexOperatorHash;
  1289. (** 2 to the power of exponent **)
  1290. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1291. VAR result, i: LONGINT;
  1292. BEGIN
  1293. result := 1;
  1294. FOR i := 1 TO exponent DO
  1295. result := result * 2;
  1296. END;
  1297. RETURN result
  1298. END TwoToThePowerOf;
  1299. (** check if a LEN operator has a correct signature. i.e.
  1300. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1301. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1302. **)
  1303. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1304. VAR
  1305. procedureType: SyntaxTree.ProcedureType;
  1306. returnedArrayType: SyntaxTree.MathArrayType;
  1307. result: BOOLEAN;
  1308. BEGIN
  1309. result := FALSE;
  1310. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1311. IF (procedureType.numberParameters = 0) THEN
  1312. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1313. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1314. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1315. IF returnedArrayType.form = SyntaxTree.Open THEN
  1316. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1317. result := TRUE
  1318. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1319. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1320. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1321. result := TRUE
  1322. END
  1323. END
  1324. END
  1325. END
  1326. END;
  1327. IF result THEN
  1328. (* export symbol automatically *)
  1329. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1330. ELSE
  1331. Error(operator.position, "LEN operator with invalid signature");
  1332. END;
  1333. RETURN result
  1334. END CheckLenOperator;
  1335. (** check if an index operator has a correct signature. i.e.
  1336. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1337. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1338. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1339. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1340. - determine if it is a read or write operator (existance of return type)
  1341. - check index parameters
  1342. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1343. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1344. [LONGINT] -> binary 0 -> 0
  1345. [RANGE] -> binary 1 -> 1
  1346. [LONGINT, LONGINT] -> binary 00 -> 0
  1347. [LONGINT, RANGE] -> binary 01 -> 1
  1348. [RANGE, LONGINT] -> binary 10 -> 2
  1349. [RANGE, RANGE] -> binary 11 -> 3
  1350. etc.
  1351. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1352. - for read operators, check if return type matches the type of data that is read
  1353. - for write operators, check if last parameter type matches the type of data that is written
  1354. **)
  1355. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1356. VAR
  1357. elementType, otherElementType, dataType: SyntaxTree.Type;
  1358. procedureType: SyntaxTree.ProcedureType;
  1359. mathArrayType: SyntaxTree.MathArrayType;
  1360. parameter: SyntaxTree.Parameter;
  1361. parameterCount, rangeCount, i: LONGINT;
  1362. hasTypeError: BOOLEAN;
  1363. BEGIN
  1364. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1365. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1366. (* determine if it is a read or write operator *)
  1367. isReadOperator := (procedureType.returnType # NIL);
  1368. IF isReadOperator THEN
  1369. indexListSize := parameterCount;
  1370. ELSE
  1371. indexListSize := parameterCount - 1;
  1372. END;
  1373. IF indexListSize < 1 THEN
  1374. Error(operator.position, "index operator with too few parameters");
  1375. RETURN FALSE
  1376. END;
  1377. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1378. (* general operator *)
  1379. isGeneralOperator := TRUE;
  1380. IF indexListSize > 1 THEN
  1381. Error(operator.position, "index operator with too many parameters");
  1382. RETURN FALSE
  1383. END;
  1384. (* ARRAY [*] OF RANGE*)
  1385. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1386. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1387. Error(operator.position, "index parameter not dynamic math array of range");
  1388. RETURN FALSE
  1389. END;
  1390. parameter := procedureType.firstParameter.nextParameter
  1391. ELSE
  1392. (* fixed-dim. operator *)
  1393. isGeneralOperator := FALSE;
  1394. (* check number of index parameters *)
  1395. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1396. (* for tensors, limited to a certain size *)
  1397. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1398. Error(operator.position, "too many index parameters for tensor");
  1399. RETURN FALSE
  1400. END
  1401. ELSE
  1402. (* for non-tensors, depends on dimensionality *)
  1403. IF indexListSize # arrayStructure.Dimensionality() THEN
  1404. Error(operator.position, "index parameter count does not match dimensionality");
  1405. RETURN FALSE
  1406. END
  1407. END;
  1408. (* go through all index parameters
  1409. - count the number of ranges
  1410. - determine the index list kind number
  1411. *)
  1412. indexListKind := 0;
  1413. rangeCount := 0;
  1414. parameter := procedureType.firstParameter;
  1415. FOR i := 1 TO indexListSize DO
  1416. indexListKind := indexListKind * 2;
  1417. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1418. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1419. INC(indexListKind);
  1420. INC(rangeCount)
  1421. ELSE
  1422. Error(parameter.position, "integer or range expected");
  1423. RETURN FALSE
  1424. END;
  1425. parameter := parameter.nextParameter
  1426. END;
  1427. END;
  1428. (*
  1429. - for read operators: check type of last parameter
  1430. - for write operators: check return type
  1431. *)
  1432. IF isReadOperator THEN
  1433. dataType := procedureType.returnType (* the return type *)
  1434. ELSE
  1435. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1436. END;
  1437. elementType := arrayStructure.ElementType();
  1438. hasTypeError := FALSE;
  1439. IF isGeneralOperator THEN
  1440. (* ARRAY [?] OF <Element> *)
  1441. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1442. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1443. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1444. hasTypeError := TRUE
  1445. END
  1446. ELSE
  1447. hasTypeError := TRUE
  1448. END
  1449. ELSE
  1450. IF rangeCount = 0 THEN
  1451. (* <Element> *)
  1452. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1453. ELSE
  1454. (* ARRAY [*, *, ..., *] OF <Element> *)
  1455. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1456. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1457. IF mathArrayType.IsFullyDynamic() THEN
  1458. IF mathArrayType.Dimensionality() = rangeCount THEN
  1459. otherElementType := mathArrayType.ElementType();
  1460. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1461. ELSE
  1462. hasTypeError := TRUE
  1463. END
  1464. ELSE
  1465. hasTypeError := TRUE
  1466. END
  1467. ELSE
  1468. hasTypeError := TRUE
  1469. END
  1470. END
  1471. END;
  1472. IF hasTypeError THEN
  1473. IF isReadOperator THEN
  1474. Error(operator.position, "return type does not match")
  1475. ELSE
  1476. Error(parameter.position, "type of last parameter does not match")
  1477. END;
  1478. RETURN FALSE
  1479. END;
  1480. (* export symbol automatically *)
  1481. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1482. RETURN TRUE
  1483. END CheckIndexOperator;
  1484. (** resolve all pending types (late resolving).
  1485. - type fixes are resolved at the end of the declaration phase
  1486. - type fixes may imply new type fixes that are also entered at the end of the list
  1487. **)
  1488. PROCEDURE FixTypes;
  1489. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1490. BEGIN
  1491. prevScope := currentScope;
  1492. p := typeFixes.Get(currentScope);
  1493. WHILE p # NIL DO
  1494. ASSERT(currentScope # NIL);
  1495. ASSERT(p IS SyntaxTree.Type);
  1496. IF p IS SyntaxTree.PointerType THEN
  1497. FixPointerType(p(SyntaxTree.PointerType))
  1498. ELSIF p IS SyntaxTree.ProcedureType THEN
  1499. FixProcedureType(p(SyntaxTree.ProcedureType))
  1500. ELSE
  1501. HALT(100);
  1502. END;
  1503. p := typeFixes.Get(currentScope);
  1504. END;
  1505. currentScope :=prevScope;
  1506. END FixTypes;
  1507. (**
  1508. resolve type x
  1509. - if x is nil then return nil
  1510. - if x cannot be resolved then the result is invalidType else the result is x
  1511. - the resolved type is entered into x.resolved
  1512. **)
  1513. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1514. VAR prev,resolved: SyntaxTree.Type;
  1515. BEGIN
  1516. prev := resolvedType;
  1517. resolvedType := SyntaxTree.invalidType;
  1518. IF x = NIL THEN resolvedType := NIL
  1519. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1520. END;
  1521. resolved := resolvedType;
  1522. resolvedType := prev;
  1523. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1524. RETURN resolved
  1525. END ResolveType;
  1526. (*** compatibility rules ***)
  1527. (**
  1528. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1529. **)
  1530. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1531. VAR result: SyntaxTree.Type;
  1532. BEGIN
  1533. result := SyntaxTree.invalidType;
  1534. IF type = NIL THEN Error(position, "expression of type NIL");
  1535. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1536. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1537. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1538. ELSE result := type.resolved
  1539. END;
  1540. RETURN result
  1541. END RegularType;
  1542. (** returns signature compatibility of procedure types this and to
  1543. - if not compatible then error is reported
  1544. - compatibility means type equality
  1545. **)
  1546. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1547. VAR result: BOOLEAN;
  1548. BEGIN
  1549. result := SameType(to,this);
  1550. IF ~result THEN
  1551. Error(position, "signature incompatible");
  1552. IF VerboseErrorMessage THEN
  1553. Printout.Info("this",this);
  1554. Printout.Info("to",to);
  1555. END;
  1556. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1557. Error(position, "signature incompatible: realtime flag must be inherited");
  1558. END;
  1559. RETURN result
  1560. END SignatureCompatible;
  1561. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1562. - for var parameters compatibility means same type except for
  1563. - formal is of open array of system byte
  1564. - formal is of record type
  1565. - formal is of open array type
  1566. - formal is of open math array type
  1567. - for value parameters compatibllity means assignment compatibility except for
  1568. - formal is of open array type
  1569. if compatible the return true else report error and return false
  1570. **)
  1571. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1572. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1573. BEGIN
  1574. formalType := RegularType(formal.position,formal.type);
  1575. actualType := RegularType(actual.position,actual.type);
  1576. error := FALSE;
  1577. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1578. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1579. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1580. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1581. END;
  1582. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1583. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1584. ELSIF ~IsVariable(actual) THEN
  1585. result := FALSE; error := TRUE;
  1586. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1587. Error(actual.position,"not a variable: no operator for writing");
  1588. ELSE
  1589. Error(actual.position,"is not a variable");
  1590. END;
  1591. IF VerboseErrorMessage THEN
  1592. Printout.Info("actual",actual);
  1593. Printout.Info("formal",formal);
  1594. END;
  1595. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1596. result := CompatibleTo(system,actualType,formalType);
  1597. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1598. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1599. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1600. IF IsArrayStructuredObjectType(actualType) THEN
  1601. actualType := MathArrayStructureOfType(actualType)
  1602. END;
  1603. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1604. IF result & (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. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1608. Error(actual.position,"incompatible tensor (use a range expression)");
  1609. END;
  1610. ELSE
  1611. result := SameType(actualType,formalType)
  1612. END
  1613. ELSE
  1614. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1615. actualType := system.characterType;
  1616. END;
  1617. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1618. result := TRUE; (* special rule for WINAPI parameters *)
  1619. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1620. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1621. ELSE
  1622. result := CompatibleTo(system,actualType,formalType);
  1623. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1624. Error(actual.position,"incompatible non-static actual type");
  1625. END;
  1626. END;
  1627. END;
  1628. IF ~result & ~error THEN
  1629. Error(actual.position,"incompatible parameter");
  1630. IF VerboseErrorMessage THEN
  1631. Printout.Info("actual",actual);
  1632. Printout.Info("formal",formal);
  1633. END;
  1634. END;
  1635. RETURN result
  1636. END ParameterCompatible;
  1637. (** check compatibility for expressions of the form left := right
  1638. - if compatible then return true else error report and return false
  1639. - check if left is variable
  1640. - check compatiblity
  1641. **)
  1642. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1643. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1644. BEGIN
  1645. result := FALSE;
  1646. leftType := RegularType(left.position,left.type);
  1647. rightType := RegularType(right.position,right.type);
  1648. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1649. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1650. END;
  1651. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1652. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1653. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1654. (* error already handled *)
  1655. result := TRUE;
  1656. ELSIF ~IsVariable(left) THEN
  1657. Error(left.position,"is not a variable");
  1658. IF VerboseErrorMessage THEN
  1659. Printout.Info("left",left);
  1660. Printout.Info("right",right);
  1661. END;
  1662. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1663. result := TRUE;
  1664. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1665. result := TRUE
  1666. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1667. Error(left.position,"incompatible assignment");
  1668. IF VerboseErrorMessage THEN
  1669. Printout.Info("left",left);
  1670. Printout.Info("right",right);
  1671. END;
  1672. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1673. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1674. Error(right.position,"forbidden assignment of a nested procedure");
  1675. ELSE
  1676. result := TRUE
  1677. END;
  1678. RETURN result
  1679. END AssignmentCompatible;
  1680. (*** values ***)
  1681. (** check and resolve integer value **)
  1682. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1683. VAR hugeint: HUGEINT;
  1684. BEGIN
  1685. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1686. value.SetType(Global.GetIntegerType(system,hugeint));
  1687. resolvedExpression := value
  1688. END VisitIntegerValue;
  1689. (** check and resolve real value **)
  1690. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1691. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1692. BEGIN
  1693. subtype := value(SyntaxTree.RealValue).subtype;
  1694. IF subtype = Scanner.Real THEN
  1695. type := system.realType
  1696. ELSIF subtype = Scanner.Longreal THEN
  1697. type := system.longrealType
  1698. ELSE
  1699. HALT(100)
  1700. END;
  1701. value.SetType(type);
  1702. resolvedExpression := value
  1703. END VisitRealValue;
  1704. (** check and resolve complex value **)
  1705. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1706. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1707. BEGIN
  1708. subtype := value(SyntaxTree.ComplexValue).subtype;
  1709. IF subtype = Scanner.Real THEN
  1710. type := system.complexType
  1711. ELSIF subtype = Scanner.Longreal THEN
  1712. type := system.longcomplexType
  1713. ELSE
  1714. HALT(100)
  1715. END;
  1716. value.SetType(type);
  1717. resolvedExpression := value
  1718. END VisitComplexValue;
  1719. (** check and resolve set value **)
  1720. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1721. BEGIN
  1722. value.SetType(system.setType);
  1723. resolvedExpression := value
  1724. END VisitSetValue;
  1725. (** check and resolve set value **)
  1726. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1727. BEGIN
  1728. value.SetType(SyntaxTree.invalidType);
  1729. resolvedExpression := value
  1730. END VisitMathArrayValue;
  1731. (** check and resolve boolean value **)
  1732. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1733. BEGIN
  1734. value.SetType(system.booleanType);
  1735. resolvedExpression := value
  1736. END VisitBooleanValue;
  1737. (** check and resolve string value **)
  1738. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1739. BEGIN
  1740. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1741. resolvedExpression := value
  1742. END VisitStringValue;
  1743. (** check and resolve character value **)
  1744. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1745. BEGIN
  1746. value.SetType(system.characterType);
  1747. resolvedExpression := value
  1748. END VisitCharacterValue;
  1749. (** check and resolve nil value **)
  1750. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1751. BEGIN
  1752. value.SetType(system.nilType);
  1753. resolvedExpression := value
  1754. END VisitNilValue;
  1755. (** check and resolve enumerator value **)
  1756. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1757. BEGIN
  1758. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1759. ASSERT(value.type # NIL);
  1760. resolvedExpression := value
  1761. END VisitEnumerationValue;
  1762. (*** expressions ***)
  1763. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1764. - check all elements on integer type
  1765. - if element range is constant, then check lower and upper bound
  1766. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1767. if an error occurs then report error and return invalidExpression
  1768. **)
  1769. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1770. VAR
  1771. i: LONGINT;
  1772. element: SyntaxTree.Expression;
  1773. constant: BOOLEAN;
  1774. elements: SyntaxTree.ExpressionList;
  1775. s: SET;
  1776. result: SyntaxTree.Expression;
  1777. value: SyntaxTree.Value;
  1778. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1779. VAR
  1780. left, right: SyntaxTree.Expression;
  1781. elementResult: SyntaxTree.Expression;
  1782. leftInteger, rightInteger, temp: LONGINT;
  1783. BEGIN
  1784. (* set context of range *)
  1785. IF element IS SyntaxTree.RangeExpression THEN
  1786. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1787. END;
  1788. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1789. IF elementResult = SyntaxTree.invalidExpression THEN
  1790. (* error already reported *)
  1791. constant := FALSE
  1792. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1793. (* the element is a range expression *)
  1794. (* extract left and right hand side of range *)
  1795. left := elementResult(SyntaxTree.RangeExpression).first;
  1796. right := elementResult(SyntaxTree.RangeExpression).last;
  1797. (* guaranteed by VisitRangeExpression: *)
  1798. ASSERT((left # NIL) & (right # NIL));
  1799. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1800. ELSE
  1801. (* the element is not a range expression *)
  1802. (* check type and add conversion if needed *)
  1803. IF IsIntegerType(elementResult.type.resolved) THEN
  1804. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1805. ELSE
  1806. Error(elementResult.position, "non integer element in set");
  1807. elementResult := SyntaxTree.invalidExpression;
  1808. constant := FALSE
  1809. END;
  1810. left := elementResult;
  1811. right := elementResult
  1812. END;
  1813. IF elementResult # SyntaxTree.invalidExpression THEN
  1814. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1815. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1816. Error(left.position,"not allowed set integer value");
  1817. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1818. Error(right.position,"not allowed set integer value");
  1819. END
  1820. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1821. Error(right.position,"not allowed set integer value");
  1822. ELSE
  1823. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1824. s := {};
  1825. ELSE
  1826. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1827. IF leftInteger < 0 THEN leftInteger := 0 END;
  1828. (*!!!!!!!!! this is a hack !!!!!!! *)
  1829. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1830. extends the range x..31 to x..63 !!!!!! *)
  1831. s := s + {leftInteger..rightInteger};
  1832. END;
  1833. END;
  1834. ELSE
  1835. constant := FALSE;
  1836. END
  1837. END;
  1838. RETURN elementResult
  1839. END CheckElement;
  1840. BEGIN
  1841. result := set; constant := TRUE; s := {}; elements := set.elements;
  1842. IF elements # NIL THEN
  1843. FOR i := 0 TO elements.Length()-1 DO
  1844. element := elements.GetExpression(i);
  1845. element := CheckElement(element);
  1846. IF element = SyntaxTree.invalidExpression THEN
  1847. result := SyntaxTree.invalidExpression
  1848. END;
  1849. elements.SetExpression(i,element);
  1850. END;
  1851. END;
  1852. IF constant THEN
  1853. value := Global.NewSetValue(system,set.position,s);
  1854. result.SetResolved(value);
  1855. result.SetType(value.type);
  1856. ELSE
  1857. result.SetType(system.setType);
  1858. END;
  1859. (* optimization possible
  1860. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1861. left this to the programmer...
  1862. *)
  1863. resolvedExpression := result;
  1864. END VisitSet;
  1865. (*
  1866. old variant: quite generic but needs better conversion handling, do this?
  1867. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1868. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1869. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1870. BEGIN
  1871. type := NIL;
  1872. numberElements := x.elements.Length();
  1873. FOR i := 0 TO numberElements-1 DO
  1874. expression := x.elements.GetExpression(i);
  1875. position := expression.position;
  1876. expression := ResolveExpression(x.elements.GetExpression(i));
  1877. x.elements.SetExpression(i,de);
  1878. IF type = NIL THEN
  1879. type := expression.type;
  1880. ELSIF CompatibleTo(system,expression.type,type) THEN
  1881. (* ok *)
  1882. ELSIF CompatibleTo(system,type,expression.type) THEN
  1883. type := expression.type
  1884. ELSE
  1885. Error(expression.position, "incompatible element types");
  1886. type := SyntaxTree.invalidType;
  1887. END;
  1888. END;
  1889. isValue := TRUE;
  1890. FOR i := 0 TO numberElements-1 DO
  1891. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1892. x.elements.SetExpression(i,expression);
  1893. isValue := isValue & (expression.resolved # NIL);
  1894. END;
  1895. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1896. arrayType.SetArrayBase(type);
  1897. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1898. IF isValue THEN
  1899. value := SyntaxTree.NewMathArrayValue(position);
  1900. value.SetElements(x.elements);
  1901. x.SetResolved(value);
  1902. END;
  1903. x.SetType(arrayType);
  1904. resolvedExpression := x;
  1905. END VisitMathArrayExpression;
  1906. *)
  1907. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1908. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1909. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1910. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1911. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. FOR i := 0 TO numberElements-1 DO
  1915. expression := x.elements.GetExpression(i);
  1916. IF expression IS SyntaxTree.MathArrayExpression THEN
  1917. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1918. ELSE
  1919. position := expression.position;
  1920. expression := ResolveExpression(x.elements.GetExpression(i));
  1921. x.elements.SetExpression(i,expression);
  1922. IF type = NIL THEN
  1923. type := expression.type;
  1924. ELSIF CompatibleTo(system,expression.type,type) THEN
  1925. (* ok *)
  1926. ELSIF CompatibleTo(system,type,expression.type) THEN
  1927. type := expression.type
  1928. ELSE
  1929. Error(expression.position, "incompatible element types");
  1930. type := SyntaxTree.invalidType;
  1931. END;
  1932. END;
  1933. END;
  1934. END RecursivelyFindType;
  1935. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1936. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1937. BEGIN
  1938. numberElements := x.elements.Length();
  1939. FOR i := 0 TO numberElements-1 DO
  1940. expression := x.elements.GetExpression(i);
  1941. IF expression IS SyntaxTree.MathArrayExpression THEN
  1942. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1943. ELSE
  1944. position := expression.position;
  1945. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1946. x.elements.SetExpression(i,expression);
  1947. isValue := isValue & (expression.resolved # NIL);
  1948. END;
  1949. END;
  1950. END RecursivelySetExpression;
  1951. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1952. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1953. arrayType: SyntaxTree.MathArrayType;
  1954. BEGIN
  1955. numberElements := x.elements.Length();
  1956. baseType := NIL;
  1957. gsize := 0;
  1958. FOR i := 0 TO numberElements-1 DO
  1959. expression := x.elements.GetExpression(i);
  1960. IF expression IS SyntaxTree.MathArrayExpression THEN
  1961. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1962. IF i=0 THEN
  1963. gsize := size;
  1964. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1965. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1966. ELSE expression.SetType(baseType)
  1967. END;
  1968. ELSIF baseType = NIL THEN baseType := type;
  1969. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1970. END;
  1971. END;
  1972. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1973. arrayType.SetArrayBase(baseType);
  1974. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1975. RETURN ResolveType(arrayType);
  1976. END RecursivelySetType;
  1977. BEGIN
  1978. type := NIL;
  1979. RecursivelyFindType(x);
  1980. isValue := TRUE;
  1981. RecursivelySetExpression(x);
  1982. arrayType := RecursivelySetType(x);
  1983. x.SetType(arrayType);
  1984. IF isValue THEN
  1985. value := SyntaxTree.NewMathArrayValue(x.position);
  1986. value.SetArray(x);
  1987. x.SetResolved(value);
  1988. value.SetType(arrayType);
  1989. END;
  1990. x.SetType(arrayType);
  1991. resolvedExpression := x;
  1992. END VisitMathArrayExpression;
  1993. (** check and resolve unary expression **)
  1994. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1995. VAR
  1996. left: SyntaxTree.Expression;
  1997. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1998. bool: BOOLEAN;
  1999. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2000. value: SyntaxTree.Value;
  2001. BEGIN
  2002. type := SyntaxTree.invalidType;
  2003. left := ResolveExpression(unaryExpression.left);
  2004. unaryExpression.SetLeft(left);
  2005. operator := unaryExpression.operator;
  2006. result := unaryExpression;
  2007. IF ~system.operatorDefined[operator] THEN
  2008. Error(left.position,"Operator Not Defined");
  2009. RETURN
  2010. ELSIF left.type = NIL THEN
  2011. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2012. resolvedExpression := SyntaxTree.invalidExpression;
  2013. RETURN
  2014. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2015. RETURN
  2016. END;
  2017. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2018. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2019. END;
  2020. IF operatorCall # NIL THEN
  2021. result := operatorCall;
  2022. type := operatorCall.type;
  2023. (* admissible operators
  2024. Minus number, set
  2025. Not boolean
  2026. *)
  2027. ELSE
  2028. CASE unaryExpression.operator OF
  2029. |Scanner.Minus:
  2030. IF IsIntegerType(left.type.resolved) THEN
  2031. IF left.resolved # NIL THEN
  2032. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2033. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2034. result.SetResolved(value);
  2035. type := Global.GetIntegerType(system,int);
  2036. value.SetType(type);
  2037. ELSE
  2038. type := left.type
  2039. END
  2040. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2041. IF IsRealValue(left,real) THEN
  2042. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2043. result.SetResolved(value);
  2044. type := left.type;
  2045. value.SetType(type);
  2046. ELSE
  2047. type := left.type;
  2048. END;
  2049. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2050. IF IsSetValue(left,set) THEN
  2051. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2052. result.SetResolved(value);
  2053. type := Global.GetSetType(system,-set);
  2054. value.SetType(type);
  2055. ELSE
  2056. type := left.type;
  2057. END;
  2058. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2059. IF IsComplexValue(left, real, imaginary) THEN
  2060. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2061. result.SetResolved(value);
  2062. type := left.type;
  2063. value.SetType(type);
  2064. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2065. ELSE
  2066. type := left.type;
  2067. END
  2068. ELSE
  2069. Error(left.position,"unary operator not applicable");
  2070. END;
  2071. |Scanner.Not:
  2072. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2073. IF IsBooleanValue(left,bool) THEN
  2074. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2075. result.SetResolved(value);
  2076. type := system.booleanType;
  2077. value.SetType(type);
  2078. ELSE
  2079. type := system.booleanType;
  2080. END;
  2081. ELSE
  2082. Error(left.position,"unary operator not applicable");
  2083. END;
  2084. |Scanner.Plus:
  2085. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2086. result := left; type := left.type;
  2087. ELSE
  2088. Error(left.position,"unary operator not applicable");
  2089. END;
  2090. (* ADDRESS OF *)
  2091. |Scanner.Address:
  2092. IF HasAddress(left) THEN
  2093. type := system.addressType;
  2094. ELSE
  2095. type := SyntaxTree.invalidType;
  2096. Error(left.position,"has no address");
  2097. Printout.Info("par", left);
  2098. END;
  2099. (* SIZE OF *)
  2100. |Scanner.Size:
  2101. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2102. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2103. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2104. value := SyntaxTree.NewIntegerValue(left.position, int);
  2105. result.SetResolved(value);
  2106. type := Global.GetIntegerType(system,int);
  2107. value.SetType(type)
  2108. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2109. ELSE
  2110. (* for variables, system sizeof could represent the physically occupied size
  2111. determined via the type descriptor, implement that ? *)
  2112. Error(left.position,"is not a type symbol");
  2113. END
  2114. (* ALIAS OF *)
  2115. |Scanner.Alias:
  2116. type := left.type.resolved;
  2117. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2118. type := SyntaxTree.invalidType;
  2119. Error(left.position,"alias on non math array type");
  2120. END;
  2121. ELSE
  2122. Error(left.position,"unary operator not defined");
  2123. END;
  2124. END;
  2125. result.SetType(type);
  2126. resolvedExpression := result
  2127. END VisitUnaryExpression;
  2128. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2129. VAR
  2130. result: SyntaxTree.Expression;
  2131. array: SyntaxTree.MathArrayExpression;
  2132. value: SyntaxTree.MathArrayValue;
  2133. isValue: BOOLEAN;
  2134. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2135. BEGIN
  2136. type := type.resolved;
  2137. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2138. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2139. END;
  2140. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2141. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2142. END;
  2143. RETURN type
  2144. END BaseType;
  2145. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2146. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2147. BEGIN
  2148. numberElements := x.elements.Length();
  2149. FOR i := 0 TO numberElements-1 DO
  2150. expression := x.elements.GetExpression(i);
  2151. IF expression IS SyntaxTree.MathArrayExpression THEN
  2152. array := SyntaxTree.NewMathArrayExpression(position);
  2153. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2154. expression := array;
  2155. ELSE
  2156. position := expression.position;
  2157. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2158. isValue := isValue & (expression.resolved # NIL);
  2159. END;
  2160. to.elements.AddExpression(expression);
  2161. END;
  2162. END RecursivelyConvert;
  2163. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2164. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2165. arrayType: SyntaxTree.MathArrayType;
  2166. BEGIN
  2167. numberElements := x.elements.Length();
  2168. baseType := NIL;
  2169. gsize := 0;
  2170. FOR i := 0 TO numberElements-1 DO
  2171. expression := x.elements.GetExpression(i);
  2172. IF expression IS SyntaxTree.MathArrayExpression THEN
  2173. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2174. IF i=0 THEN
  2175. gsize := size;
  2176. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2177. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2178. ELSE expression.SetType(baseType)
  2179. END;
  2180. ELSIF baseType = NIL THEN baseType := type;
  2181. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2182. END;
  2183. END;
  2184. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2185. arrayType.SetArrayBase(baseType);
  2186. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2187. RETURN ResolveType(arrayType);
  2188. END RecursivelySetType;
  2189. BEGIN
  2190. result := SyntaxTree.invalidExpression;
  2191. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2192. result := expression (* do not convert *)
  2193. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2194. isValue := TRUE;
  2195. type := BaseType(type);
  2196. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2197. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2198. value := SyntaxTree.NewMathArrayValue(array.position);
  2199. value.SetArray(array);
  2200. value.SetType(RecursivelySetType(array));
  2201. result := value;
  2202. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2203. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2204. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2205. IF result = NIL THEN
  2206. result := SyntaxTree.invalidExpression;
  2207. Error(position, "incompatible conversion");
  2208. IF VerboseErrorMessage THEN
  2209. Printout.Info("expression",expression);
  2210. Printout.Info("type",type);
  2211. END;
  2212. END;
  2213. END;
  2214. RETURN result
  2215. END MathArrayConversion;
  2216. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2217. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2218. BEGIN
  2219. result := expression; type := type.resolved;
  2220. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2221. (* skip, no conversion *)
  2222. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2223. int := expression(SyntaxTree.IntegerValue).hvalue;
  2224. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2225. int := Global.ConvertSigned(int,system.SizeOf(type));
  2226. result := SyntaxTree.NewIntegerValue(position,int);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2229. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2230. result := SyntaxTree.NewIntegerValue(position,int);
  2231. result.SetType(type);
  2232. ELSIF (type IS SyntaxTree.FloatType) THEN
  2233. result := SyntaxTree.NewRealValue(expression.position,int);
  2234. result.SetType(type);
  2235. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2236. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2237. result.SetType(type);
  2238. ELSIF (type IS SyntaxTree.SetType) THEN
  2239. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2240. result.SetType(type);
  2241. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2242. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2245. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2246. Error(position, "huge integer value incompatible to enumeration");
  2247. END;
  2248. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2249. result.SetType(type);
  2250. ELSIF (type IS SyntaxTree.PortType) THEN
  2251. result := ConvertValue(position, expression, system.integerType);
  2252. ELSE
  2253. Error(position, "integer value cannot be converted");
  2254. result := SyntaxTree.invalidExpression;
  2255. IF VerboseErrorMessage THEN
  2256. Printout.Info("expression",expression);
  2257. Printout.Info("type",type);
  2258. END;
  2259. END;
  2260. ELSIF IsRealValue(expression,real) THEN
  2261. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2262. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2263. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2264. result.SetType(type);
  2265. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2266. int := ENTIERH(real);
  2267. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2268. result.SetType(type);
  2269. ELSIF (type IS SyntaxTree.FloatType) THEN
  2270. result := SyntaxTree.NewRealValue(position,real);
  2271. result.SetType(type);
  2272. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2273. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2274. result.SetType(type);
  2275. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2276. ELSIF (type IS SyntaxTree.PortType) THEN
  2277. result := ConvertValue(position, expression, system.integerType);
  2278. ELSE
  2279. Error(position, "real value cannot be converted");
  2280. result := SyntaxTree.invalidExpression;
  2281. END
  2282. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2283. IF (type IS SyntaxTree.ComplexType) THEN
  2284. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2285. result.SetType(type);
  2286. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2287. ELSE
  2288. Error(position, "complex value cannot be converted");
  2289. result := SyntaxTree.invalidExpression;
  2290. END
  2291. ELSIF IsSetValue(expression,set) THEN
  2292. IF (type IS SyntaxTree.IntegerType) THEN
  2293. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2294. result.SetType(type);
  2295. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2296. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2297. result.SetType(type);
  2298. ELSIF (type IS SyntaxTree.PortType) THEN
  2299. result := ConvertValue(position, expression, system.integerType);
  2300. ELSIF (type IS SyntaxTree.SetType) THEN
  2301. set := Global.ConvertSet(set,system.SizeOf(type));
  2302. result := SyntaxTree.NewSetValue(expression.position,set);
  2303. result.SetType(type);
  2304. ELSE
  2305. Error(position, "set value cannot be converted");
  2306. result := SyntaxTree.invalidExpression;
  2307. END;
  2308. ELSIF IsStringValue(expression,string) THEN
  2309. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2310. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2311. result.SetType(type);
  2312. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2313. ELSE
  2314. Error(position, "string value cannot be converted");
  2315. result := SyntaxTree.invalidExpression;
  2316. END;
  2317. ELSIF IsCharacterValue(expression,char) THEN
  2318. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2319. string[0] := char; string[1] := 0X;
  2320. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2321. result := SyntaxTree.NewStringValue(expression.position,string);
  2322. result.SetType(type);
  2323. ELSIF (type IS SyntaxTree.ByteType) THEN
  2324. (* do not simply set the new type as this could invalidate types of constants *)
  2325. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2326. result.SetType(type)
  2327. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2328. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2329. result.SetType(type);
  2330. ELSIF (type IS SyntaxTree.SetType) THEN
  2331. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2332. result.SetType(type);
  2333. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2334. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2335. result.SetType(type);
  2336. ELSIF (type IS SyntaxTree.PortType) THEN
  2337. result := ConvertValue(position, expression, system.integerType);
  2338. ELSE
  2339. Error(position, "character value cannot be converted");
  2340. result := SyntaxTree.invalidExpression;
  2341. END;
  2342. ELSIF expression IS SyntaxTree.NilValue THEN
  2343. IF type IS SyntaxTree.AddressType THEN
  2344. result := SyntaxTree.NewIntegerValue(position,0);
  2345. result.SetType(type);
  2346. ELSE
  2347. result := expression;
  2348. END;
  2349. (* nothing to be done *)
  2350. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2351. result := MathArrayConversion(position, expression,type);
  2352. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2353. int := expression(SyntaxTree.EnumerationValue).value;
  2354. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2355. int := Global.ConvertSigned(int,system.SizeOf(type));
  2356. result := SyntaxTree.NewIntegerValue(position,int);
  2357. result.SetType(type);
  2358. ELSE
  2359. result := expression;
  2360. END;
  2361. (* nothing to be done *)
  2362. ELSE
  2363. Error(position, "expression cannot be converted");
  2364. IF VerboseErrorMessage THEN
  2365. Printout.Info("expression",expression);
  2366. Printout.Info("type",type);
  2367. END;
  2368. result := SyntaxTree.invalidExpression;
  2369. END;
  2370. RETURN result
  2371. END ConvertValue;
  2372. (**
  2373. return a conversion of an expression to a given type
  2374. - if expression is already of same type then return expression
  2375. - if incompatible conversion then report error and return invalidExpression
  2376. **)
  2377. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2378. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2379. BEGIN
  2380. type := type.resolved;
  2381. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2382. result := expression;
  2383. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2384. ELSIF expression = NIL THEN (* NIL expression *)
  2385. ELSIF expression.type = NIL THEN
  2386. Error(position, "expression of type NIL cannot be converted");
  2387. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2388. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2389. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2390. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2391. ELSIF expression.resolved # NIL THEN (* value *)
  2392. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2393. IF value IS SyntaxTree.Value THEN
  2394. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2395. result.SetResolved(value(SyntaxTree.Value));
  2396. result.SetType(value.type);
  2397. ELSE
  2398. result := value
  2399. END;
  2400. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2401. expressionList := SyntaxTree.NewExpressionList();
  2402. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2403. typeDeclaration.SetDeclaredType(type);
  2404. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2405. typeSymbol.SetType(typeDeclaration.type);
  2406. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2407. expressionList.AddExpression(expression);
  2408. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2409. result.SetType(type);
  2410. ELSIF IsArrayStructuredObjectType(type) THEN
  2411. (* no type can be converted to an array-structured object type *)
  2412. HALT(100)
  2413. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2414. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2415. result := MathArrayConversion(position, expression,type);
  2416. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2417. expression := ConvertToMathArray(expression);
  2418. type := MathArrayStructureOfType(type);
  2419. result := MathArrayConversion(position, expression, type)
  2420. ELSE
  2421. Error(expression.position,"cannot convert non array type to array type")
  2422. END;
  2423. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2424. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2425. OR ~(type IS SyntaxTree.ArrayType) THEN
  2426. Error(expression.position,"cannot convert array type to non-array type")
  2427. END;
  2428. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2429. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2430. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2431. (*skip, no conversion*)
  2432. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2433. (* skip, no conversion *)
  2434. ELSE
  2435. ASSERT(~(type IS SyntaxTree.RangeType));
  2436. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2437. ASSERT(type # NIL);
  2438. END;
  2439. RETURN result
  2440. END NewConversion;
  2441. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2442. BEGIN
  2443. IF CompatibleTo(system,expression.type, type) THEN
  2444. RETURN NewConversion(position, expression, type, NIL);
  2445. ELSE
  2446. Error(expression.position, "incompatible expression");
  2447. RETURN SyntaxTree.invalidExpression
  2448. END;
  2449. END CompatibleConversion;
  2450. (**
  2451. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2452. **)
  2453. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2454. VAR leftType,rightType: SyntaxTree.Type;
  2455. BEGIN
  2456. IF left.type = NIL THEN Error(left.position,"no type")
  2457. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2458. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2459. (* error already handled *)
  2460. ELSE
  2461. leftType := left.type.resolved; rightType := right.type.resolved;
  2462. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2463. right := NewConversion(right.position, right, leftType, NIL);
  2464. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2465. left := NewConversion(left.position,left,rightType,NIL);
  2466. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2467. right := NewConversion(right.position, right, leftType, NIL);
  2468. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2469. left := NewConversion(left.position,left,rightType,NIL);
  2470. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2471. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2472. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2473. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2474. ELSIF
  2475. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2476. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2477. (* must be the case LONGREAL / COMPLEX ) *)
  2478. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2479. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2480. ELSE
  2481. Error(left.position,"incompatible operands");
  2482. END;
  2483. END;
  2484. END ConvertOperands;
  2485. (** find and return best operator matching to parameter list (nil, if none)
  2486. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2487. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2488. **)
  2489. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2490. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2491. identifier: SyntaxTree.Identifier;
  2492. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2493. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2494. BEGIN
  2495. operator := scope.firstOperator;
  2496. WHILE(operator # NIL) DO
  2497. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2498. procedureType := operator.type(SyntaxTree.ProcedureType);
  2499. distance := Distance(system, procedureType,actualParameters);
  2500. IF (distance < Infinity) THEN
  2501. IF returnType # NIL THEN
  2502. IF procedureType.returnType = NIL THEN
  2503. distance := Infinity
  2504. ELSE
  2505. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2506. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2507. END;
  2508. END;
  2509. END;
  2510. (*
  2511. IF distance < Infinity THEN
  2512. TRACE(distance, operator);
  2513. Printout.Info("potential operator",operator);
  2514. ELSE
  2515. Printout.Info("operator not possible",operator);
  2516. END;
  2517. *)
  2518. IF distance < bestDistance THEN
  2519. bestDistance := distance;
  2520. bestOperator := operator;
  2521. END;
  2522. END;
  2523. operator := operator.nextOperator;
  2524. END;
  2525. (*
  2526. Printout.Info("taken operator",bestOperator);
  2527. *)
  2528. END FindInScope;
  2529. BEGIN
  2530. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2531. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2532. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2533. import := currentScope.ownerModule.moduleScope.firstImport;
  2534. WHILE (bestDistance > 0) & (import # NIL) DO
  2535. IF import.module # NIL THEN
  2536. identifier := Global.GetIdentifier(operator,import.module.case);
  2537. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2538. END;
  2539. import := import.nextImport;
  2540. END;
  2541. RETURN bestOperator
  2542. END FindOperator;
  2543. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2544. BEGIN
  2545. currentScope := scope;
  2546. END SetCurrentScope;
  2547. (**
  2548. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2549. - handle LEN and DIM operator for array-structured object types
  2550. - find operator, if found then
  2551. - if in other module then add import designator
  2552. - create symbol designator for operator
  2553. - if error then return invalidExpression, if no operator then return NIL
  2554. **)
  2555. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2556. VAR
  2557. operator: SyntaxTree.Operator;
  2558. import: SyntaxTree.Import;
  2559. expression, result: SyntaxTree.Expression;
  2560. designator: SyntaxTree.Designator;
  2561. actualParameters, tempList: SyntaxTree.ExpressionList;
  2562. recordType: SyntaxTree.RecordType;
  2563. castReturnType : SyntaxTree.MathArrayType;
  2564. BEGIN
  2565. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2566. result := SyntaxTree.invalidExpression
  2567. ELSIF leftExpression = NIL THEN
  2568. result := NIL
  2569. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2570. (* LEN or DIM operator on array-structured object type *)
  2571. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2572. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2573. IF recordType.arrayAccessOperators.len = NIL THEN
  2574. Error(position, "call of undeclared LEN operator");
  2575. result := SyntaxTree.invalidExpression
  2576. ELSE
  2577. ASSERT(leftExpression IS SyntaxTree.Designator);
  2578. designator := leftExpression(SyntaxTree.Designator);
  2579. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2580. ASSERT(expression IS SyntaxTree.Designator);
  2581. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2582. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2583. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2584. result := designator
  2585. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2586. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2587. tempList := SyntaxTree.NewExpressionList();
  2588. tempList.AddExpression(rightExpression);
  2589. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2590. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2591. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2592. tempList := SyntaxTree.NewExpressionList();
  2593. tempList.AddExpression(designator);
  2594. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2595. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2596. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2597. END
  2598. END;
  2599. ELSE
  2600. IF ~complexNumbersImported THEN
  2601. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2602. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2603. THEN
  2604. (* operators on complex numbers *)
  2605. ImportModule(Global.ComplexNumbersName,position);
  2606. complexNumbersImported := TRUE;
  2607. END;
  2608. END;
  2609. (* import OCArrayBase if needed *)
  2610. IF ~arrayBaseImported THEN
  2611. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2612. IF op = Global.Dim THEN
  2613. (* not existing in OCArrayBase *)
  2614. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2615. (* not existing in OCArrayBase *)
  2616. ELSE
  2617. ImportModule(Global.ArrayBaseName,position);
  2618. arrayBaseImported := TRUE;
  2619. END
  2620. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2621. ImportModule(Global.ArrayBaseName,position);
  2622. arrayBaseImported := TRUE
  2623. END;
  2624. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2625. (* LEN(RANGE) *)
  2626. ImportModule(Global.ArrayBaseName,position);
  2627. arrayBaseImported := TRUE;
  2628. END;
  2629. END;
  2630. actualParameters := SyntaxTree.NewExpressionList();
  2631. actualParameters.AddExpression(leftExpression);
  2632. IF rightExpression # NIL THEN
  2633. actualParameters.AddExpression(rightExpression)
  2634. END;
  2635. operator := FindOperator(system,op,actualParameters,resultType);
  2636. IF operator # NIL THEN
  2637. designator := NIL;
  2638. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2639. import := currentScope.ownerModule.moduleScope.firstImport;
  2640. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2641. import := import.nextImport;
  2642. END;
  2643. expression := NewSymbolDesignator(position,NIL,import);
  2644. designator := expression(SyntaxTree.Designator);
  2645. END;
  2646. expression := NewSymbolDesignator(position,designator,operator);
  2647. designator := expression(SyntaxTree.Designator);
  2648. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2649. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2650. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2651. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2652. result.SetType(castReturnType);
  2653. END;
  2654. ELSE
  2655. result := NIL;
  2656. END;
  2657. END;
  2658. RETURN result
  2659. END NewOperatorCall;
  2660. (** check and resolve binary expression **)
  2661. (*! clean up *)
  2662. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2663. VAR left,right,result: SyntaxTree.Expression;
  2664. leftType, rightType: SyntaxTree.Type;
  2665. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2666. cl,cr: CHAR;
  2667. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2668. type: SyntaxTree.Type;
  2669. value: SyntaxTree.Value;
  2670. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2671. integerConstantFolding: BOOLEAN;
  2672. list: SyntaxTree.ExpressionList;
  2673. PROCEDURE NewBool(v: BOOLEAN);
  2674. BEGIN
  2675. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2676. value.SetType(system.booleanType);
  2677. result.SetResolved(value);
  2678. type := system.booleanType
  2679. END NewBool;
  2680. PROCEDURE NewSet(v: SET);
  2681. BEGIN
  2682. value := Global.NewSetValue(system,binaryExpression.position,v);
  2683. result.SetResolved(value);
  2684. type := value.type;
  2685. END NewSet;
  2686. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2687. BEGIN
  2688. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2689. (* type cast to "larger" type only if the value is still in the range *)
  2690. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2691. value.SetType(t);
  2692. END;
  2693. result.SetResolved(value);
  2694. type := value.type;
  2695. END NewInteger;
  2696. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2697. BEGIN
  2698. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2699. value.SetType(t);
  2700. result.SetResolved(value);
  2701. type := t;
  2702. END NewReal;
  2703. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2704. BEGIN
  2705. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2706. value.SetType(t);
  2707. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2708. result.SetResolved(value);
  2709. type := t;
  2710. END NewComplex;
  2711. BEGIN
  2712. type := SyntaxTree.invalidType;
  2713. left := ResolveExpression(binaryExpression.left);
  2714. right := ResolveExpression(binaryExpression.right);
  2715. binaryExpression.SetLeft(left);
  2716. binaryExpression.SetRight(right);
  2717. result := binaryExpression;
  2718. operator := binaryExpression.operator;
  2719. IF ~system.operatorDefined[operator] THEN
  2720. Error(left.position,"Operator Not Defined");
  2721. result := SyntaxTree.invalidExpression;
  2722. RETURN
  2723. END;
  2724. IF left.type = NIL THEN
  2725. Error(left.position,"Expression has no result type");
  2726. result := SyntaxTree.invalidExpression;
  2727. RETURN;
  2728. END;
  2729. IF right.type = NIL THEN
  2730. Error(right.position,"Expression has no result type");
  2731. result := SyntaxTree.invalidExpression;
  2732. RETURN;
  2733. END;
  2734. leftType := left.type.resolved; rightType := right.type.resolved;
  2735. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2736. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2737. END;
  2738. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2739. list := SyntaxTree.NewExpressionList();
  2740. list.AddExpression(right);
  2741. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2742. END;
  2743. IF operatorCall # NIL THEN
  2744. result := operatorCall;
  2745. type := operatorCall.type;
  2746. (* admissible operators:
  2747. Times, Plus, Minus numeric numeric numeric
  2748. set set set
  2749. Slash numeric numeric real /complex
  2750. set set set
  2751. Div , Mod integer integer integer
  2752. And, Or bool bool bool
  2753. Equal, Unequal basic basic bool
  2754. pointer pointer bool
  2755. object object bool
  2756. record record bool
  2757. string string bool
  2758. enumerator enumerator bool
  2759. Less, LessEqual,
  2760. Greater, GreaterEqual integer/real integer/real bool
  2761. enumerator enumerator bool
  2762. In integer set bool
  2763. Is pointer type bool
  2764. object type bool
  2765. record type bool
  2766. Upto: special abbreviation for a..b
  2767. *)
  2768. ELSIF (left.type = NIL) THEN
  2769. Error(left.position,"type (left operand) = NIL in binary expression");
  2770. D.Str("nil type in "); D.Type(left); D.Ln;
  2771. result := SyntaxTree.invalidExpression;
  2772. ELSIF (right.type = NIL) THEN
  2773. Error(right.position,"type (right operand) = NIL in binary expression");
  2774. result := SyntaxTree.invalidExpression;
  2775. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2776. result := SyntaxTree.invalidExpression;
  2777. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2778. HALT(100);
  2779. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2780. type := system.booleanType;
  2781. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2782. Error(right.position,"is not a type ");
  2783. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2784. Error(binaryExpression.position,"is not a type extension of ");
  2785. IF VerboseErrorMessage THEN
  2786. Printout.Info("left",left);
  2787. Printout.Info("right",right);
  2788. END;
  2789. ELSIF IsUnsafePointer(left.type) THEN
  2790. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2791. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2792. NewBool(TRUE)
  2793. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2794. NewBool(TRUE);
  2795. ELSIF IsUnextensibleRecord(left) THEN
  2796. NewBool(FALSE)
  2797. END
  2798. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2799. Error(right.position,"must not be a type");
  2800. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2801. Error(left.position,"must not be a type");
  2802. ELSIF operator = Scanner.In THEN (* left IN right *)
  2803. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2804. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2805. NewBool(il IN sr);
  2806. ELSE
  2807. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2808. left := NewConversion(left.position, left, system.longintType,NIL);
  2809. binaryExpression.SetLeft(left)
  2810. END;
  2811. type := system.booleanType;
  2812. END
  2813. ELSE
  2814. Error(binaryExpression.position, "incompatible operands");
  2815. END
  2816. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2817. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2818. Error(binaryExpression.position,"incompatible operands");
  2819. END;
  2820. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2821. ELSE Error(binaryExpression.position,"operator not defined 1")
  2822. END
  2823. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2824. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2825. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2826. Error(binaryExpression.position,"incompatible operands");
  2827. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2828. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2829. left := NewConversion(left.position, left, system.addressType, NIL);
  2830. right := NewConversion(right.position, right, system.addressType, NIL);
  2831. binaryExpression.SetLeft(left);
  2832. binaryExpression.SetRight(right);
  2833. type := system.addressType;
  2834. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2835. ConvertOperands(left, right);
  2836. binaryExpression.SetLeft(left);
  2837. binaryExpression.SetRight(right);
  2838. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2839. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2840. END;
  2841. type := system.booleanType;
  2842. ELSE
  2843. Error(binaryExpression.position,"operator not defined 3");
  2844. END
  2845. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2846. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2847. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2848. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2849. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2850. CASE operator OF
  2851. |Scanner.Equal: NewBool(strl^=strr^);
  2852. |Scanner.Unequal:NewBool(strl^#strr^);
  2853. |Scanner.Less: NewBool(strl^<strr^);
  2854. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2855. |Scanner.Greater: NewBool(strl^>strr^);
  2856. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2857. ELSE
  2858. Error(binaryExpression.position,"operator not defined 4");
  2859. END;
  2860. END;
  2861. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2862. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2863. type := system.booleanType
  2864. ELSE
  2865. Error(binaryExpression.position,"operator not defined 5");
  2866. END;
  2867. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2868. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2869. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2870. type := system.booleanType;
  2871. ELSE
  2872. Error(binaryExpression.position,"operator not defined 6");
  2873. END
  2874. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2875. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2876. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2877. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2878. type := system.booleanType
  2879. ELSE
  2880. Error(binaryExpression.position,"operator not defined for enumerators");
  2881. END;
  2882. ELSE
  2883. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2884. END;
  2885. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2886. type := system.booleanType;
  2887. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2888. type := system.booleanType;
  2889. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2890. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2891. THEN
  2892. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2893. IF (leftType # rightType) THEN
  2894. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2895. ConvertOperands(left,right); (* operands must be of the same type here *)
  2896. END;
  2897. binaryExpression.SetLeft(left);
  2898. binaryExpression.SetRight(right);
  2899. leftType := left.type.resolved;
  2900. rightType := right.type.resolved;
  2901. END;
  2902. type := leftType;
  2903. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2904. Error(binaryExpression.position,"conversion failed ?");
  2905. IF VerboseErrorMessage THEN
  2906. Printout.Info("left",left);
  2907. Printout.Info("right",right);
  2908. END;
  2909. ELSIF IsIntegerType(leftType) THEN
  2910. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2911. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2912. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2913. Error(binaryExpression.position,"division by zero");
  2914. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2915. Error(binaryExpression.position,"integer division by negative number");
  2916. END;
  2917. END;
  2918. (* constant folding *)
  2919. (* bootstrap64
  2920. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2921. CASE operator OF
  2922. |Scanner.Plus: NewInteger(il+ir,left.type);
  2923. |Scanner.Minus: NewInteger(il-ir,left.type);
  2924. |Scanner.Times: NewInteger(il*ir,left.type);
  2925. |Scanner.Slash:
  2926. IF ir # 0 THEN
  2927. NewReal(il/ir, system.realType);
  2928. END;
  2929. |Scanner.Mod:
  2930. IF ir > 0 THEN
  2931. NewInteger(il MOD ir,left.type);
  2932. END;
  2933. |Scanner.Div:
  2934. IF ir > 0 THEN
  2935. NewInteger(il DIV ir,left.type);
  2936. END;
  2937. |Scanner.Equal: NewBool(il=ir);
  2938. |Scanner.Unequal:NewBool(il#ir);
  2939. |Scanner.Less: NewBool(il<ir);
  2940. |Scanner.LessEqual: NewBool(il<=ir);
  2941. |Scanner.Greater: NewBool(il>ir);
  2942. |Scanner.GreaterEqual: NewBool(il>=ir);
  2943. ELSE Error(binaryExpression.position,"operator not defined 7");
  2944. END;
  2945. ELS*)
  2946. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2947. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2948. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2949. CASE operator OF
  2950. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2951. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2952. |Scanner.Times: NewInteger(hl*hr,left.type);
  2953. |Scanner.Slash:
  2954. IF hr = 0 THEN
  2955. Error(binaryExpression.position,"division by zero");
  2956. ELSE
  2957. IF type.sizeInBits = 64 THEN
  2958. NewReal(hl/hr,system.longrealType);
  2959. ELSE
  2960. NewReal(hl/hr,system.realType)
  2961. END
  2962. END;
  2963. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2964. |Scanner.Mod:
  2965. IF hr = 0 THEN
  2966. Error(binaryExpression.position,"division by zero");
  2967. ELSE
  2968. NewInteger(hl MOD hr, left.type);
  2969. (* bootstrap64
  2970. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2971. *)
  2972. END;
  2973. |Scanner.Div:
  2974. IF hr = 0 THEN
  2975. Error(binaryExpression.position,"division by zero");
  2976. ELSE
  2977. NewInteger(hl DIV hr, left.type);
  2978. (* bootstrap64
  2979. NewInteger(Machine.DivH(hl,hr),left.type);
  2980. *)
  2981. END;
  2982. (* *)
  2983. |Scanner.Equal: NewBool(hl=hr);
  2984. |Scanner.Unequal: NewBool(hl#hr);
  2985. |Scanner.Less: NewBool(hl<hr);
  2986. |Scanner.LessEqual: NewBool(hl<=hr);
  2987. |Scanner.Greater: NewBool(hl>hr);
  2988. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2989. ELSE Error(binaryExpression.position,"operator not defined 8");
  2990. END;
  2991. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2992. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2993. type := left.type
  2994. ELSIF (operator = Scanner.Slash) THEN
  2995. left := NewConversion(left.position,left,system.realType,NIL);
  2996. right := NewConversion(right.position,right,system.realType,NIL);
  2997. binaryExpression.SetLeft(left);
  2998. binaryExpression.SetRight(right);
  2999. type := system.realType
  3000. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3001. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3002. type := system.booleanType
  3003. ELSE
  3004. Error(binaryExpression.position,"operator not defined 9");
  3005. END;
  3006. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3007. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3008. CASE operator OF
  3009. |Scanner.Plus: NewReal(rl+rr,leftType);
  3010. |Scanner.Minus: NewReal(rl-rr,leftType);
  3011. |Scanner.Times:NewReal(rl*rr,leftType);
  3012. |Scanner.Slash:
  3013. IF rr = 0 THEN
  3014. Error(binaryExpression.position,"division by zero");
  3015. ELSE
  3016. NewReal(rl/rr,leftType);
  3017. END
  3018. |Scanner.Equal: NewBool(rl=rr);
  3019. |Scanner.Unequal: NewBool(rl#rr);
  3020. |Scanner.Less: NewBool(rl<rr);
  3021. |Scanner.LessEqual: NewBool(rl<=rr);
  3022. |Scanner.Greater: NewBool(rl>rr);
  3023. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3024. ELSE Error(binaryExpression.position,"operator not defined 10");
  3025. END;
  3026. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3027. type := left.type
  3028. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3029. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3030. type := system.booleanType
  3031. ELSE
  3032. Error(binaryExpression.position,"operator not defined 11");
  3033. IF VerboseErrorMessage THEN
  3034. Printout.Info("left",left);
  3035. Printout.Info("right",right);
  3036. END;
  3037. END;
  3038. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3039. CASE operator OF
  3040. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3041. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3042. ELSE
  3043. Error(binaryExpression.position,"operator not defined");
  3044. IF VerboseErrorMessage THEN
  3045. Printout.Info("left", left);
  3046. Printout.Info("right", right)
  3047. END;
  3048. END;
  3049. IF ~error THEN
  3050. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3051. Error(binaryExpression.position,"division by zero")
  3052. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3053. (* do constant folding *)
  3054. CASE operator OF
  3055. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3056. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3057. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3058. |Scanner.Slash:
  3059. divisor := c * c + d * d;
  3060. ASSERT(divisor # 0);
  3061. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3062. |Scanner.Equal: NewBool((a = c) & (b = d))
  3063. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3064. END
  3065. END
  3066. END
  3067. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3068. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3069. CASE operator OF
  3070. |Scanner.And: NewBool(bl & br);
  3071. |Scanner.Or: NewBool(bl OR br);
  3072. |Scanner.Equal: NewBool(bl = br);
  3073. |Scanner.Unequal: NewBool(bl # br);
  3074. ELSE Error(binaryExpression.position,"operator not defined 12");
  3075. END;
  3076. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3077. type := system.booleanType
  3078. ELSE
  3079. Error(binaryExpression.position,"operator not defined 13");
  3080. END;
  3081. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3082. (* constant folding *)
  3083. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3084. IF operator = Scanner.Equal THEN
  3085. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3086. ELSIF operator = Scanner.Unequal THEN
  3087. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3088. END;
  3089. END;
  3090. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3091. type := system.booleanType;
  3092. ELSE
  3093. Error(binaryExpression.position, "operator not defined");
  3094. END;
  3095. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3096. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3097. CASE operator OF
  3098. |Scanner.Plus: NewSet(sl + sr);
  3099. |Scanner.Minus: NewSet(sl - sr);
  3100. |Scanner.Times: NewSet(sl * sr);
  3101. |Scanner.Slash: NewSet(sl / sr);
  3102. |Scanner.Equal: NewBool(sl=sr);
  3103. |Scanner.Unequal: NewBool(sl#sr);
  3104. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3105. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3106. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3107. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3108. ELSE Error(binaryExpression.position,"operator not defined 14");
  3109. END;
  3110. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3111. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3112. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3113. THEN
  3114. type := system.booleanType
  3115. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3116. type := left.type
  3117. ELSE
  3118. Error(binaryExpression.position,"operator not defined 15");
  3119. END;
  3120. ELSIF IsCharacterType(left.type) THEN
  3121. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3122. CASE operator OF
  3123. |Scanner.Equal: NewBool(cl=cr);
  3124. |Scanner.Unequal: NewBool(cl#cr);
  3125. |Scanner.Less: NewBool(cl<cr);
  3126. |Scanner.LessEqual: NewBool(cl<=cr);
  3127. |Scanner.Greater: NewBool(cl>cr);
  3128. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3129. ELSE Error(binaryExpression.position,"operator not defined 16");
  3130. END;
  3131. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3132. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3133. type := system.booleanType
  3134. ELSE
  3135. Error(binaryExpression.position,"operator not defined 17");
  3136. END;
  3137. ELSE
  3138. Error(binaryExpression.position,"operator not defined 18");
  3139. END;
  3140. ELSE
  3141. Error(binaryExpression.position,"operator not defined 19");
  3142. END;
  3143. IF type = SyntaxTree.invalidType THEN
  3144. result := SyntaxTree.invalidExpression
  3145. ELSE
  3146. result.SetType(type)
  3147. END;
  3148. resolvedExpression := result
  3149. END VisitBinaryExpression;
  3150. (** resolve a range expression of the from <<first .. last BY step>>
  3151. - depending on the context different things are checked:
  3152. ArrayIndex:
  3153. - components must be integers
  3154. - replace missing lower bound with 0
  3155. - replace missing upper bound with MAX(LONGINT)
  3156. - replace missing step size with 1
  3157. SetElement:
  3158. - components must be integers
  3159. - replace missing lower bound with 0
  3160. - replace missing upper bound with MAX(SET)
  3161. - must not have step size
  3162. CaseGuard:
  3163. - components must be constant
  3164. - components must be integers or characters
  3165. - must have lower and upper bound present
  3166. - components are made compatible
  3167. - must not have step size
  3168. - if error: return invalidExpression
  3169. **)
  3170. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3171. VAR
  3172. hasError: BOOLEAN;
  3173. first, last, step: SyntaxTree.Expression;
  3174. BEGIN
  3175. hasError := FALSE;
  3176. first := x.first;
  3177. last := x.last;
  3178. step := x.step;
  3179. (* check lower bound *)
  3180. IF x.context = SyntaxTree.CaseGuard THEN
  3181. IF first = NIL THEN
  3182. Error(x.position, "missing lower bound");
  3183. hasError := TRUE
  3184. ELSE
  3185. first := ResolveExpression(first);
  3186. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3187. Error(first.position, "lower bound not integer or character");
  3188. hasError := TRUE
  3189. ELSE
  3190. IF first IS SyntaxTree.StringValue THEN
  3191. (* add conversion from string to character *)
  3192. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3193. END
  3194. END;
  3195. (* check if expression is constant *)
  3196. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3197. (* error already reported *)
  3198. hasError := TRUE
  3199. END
  3200. END
  3201. ELSE (* ArrayIndex, SetElement *)
  3202. IF first = NIL THEN
  3203. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3204. END;
  3205. first := ResolveExpression(first);
  3206. IF IsIntegerType(first.type.resolved) THEN
  3207. first := NewConversion(first.position, first, system.lenType, NIL)
  3208. ELSE
  3209. Error(first.position, "lower bound not integer");
  3210. hasError := TRUE
  3211. END
  3212. END;
  3213. (* check upper bound *)
  3214. IF x.context = SyntaxTree.CaseGuard THEN
  3215. IF last = NIL THEN
  3216. Error(x.position, "missing upper bound");
  3217. hasError := TRUE
  3218. ELSE
  3219. last := ResolveExpression(last);
  3220. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3221. Error(last.position, "lower bound not integer or character");
  3222. hasError := TRUE
  3223. ELSE
  3224. IF last IS SyntaxTree.StringValue THEN
  3225. (* add conversion from string to character *)
  3226. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3227. END
  3228. END;
  3229. (* check if expression is constant *)
  3230. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3231. (* error already reported *)
  3232. hasError := TRUE
  3233. ELSE
  3234. (* try to make lower and upper bound compatible *)
  3235. ConvertOperands(first, last);
  3236. IF first.type.resolved # last.type.resolved THEN
  3237. Error(x.position, "lower and upper bounds incompatible");
  3238. hasError := TRUE
  3239. END
  3240. END
  3241. END
  3242. ELSE (* ArrayIndex, SetElement *)
  3243. IF last = NIL THEN
  3244. IF x.context = SyntaxTree.ArrayIndex THEN
  3245. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3246. ELSE
  3247. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3248. END
  3249. END;
  3250. last := ResolveExpression(last);
  3251. IF IsIntegerType(last.type.resolved) THEN
  3252. last := NewConversion(last.position, last, system.lenType, NIL)
  3253. ELSE
  3254. Error(last.position, "upper bound not integer");
  3255. hasError := TRUE
  3256. END
  3257. END;
  3258. (* check step size *)
  3259. IF x.context = SyntaxTree.ArrayIndex THEN
  3260. IF step = NIL THEN
  3261. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3262. END;
  3263. step := ResolveExpression(step);
  3264. IF IsIntegerType(step.type.resolved) THEN
  3265. step := NewConversion(step.position, step, system.lenType, NIL)
  3266. ELSE
  3267. Error(step.position, "step size not integer");
  3268. hasError := TRUE
  3269. END
  3270. ELSE (* SetElement, CaseGuard *)
  3271. IF step # NIL THEN
  3272. Error(last.position, "step size not allowed in this context");
  3273. hasError := TRUE
  3274. END
  3275. END;
  3276. IF hasError THEN
  3277. resolvedExpression := SyntaxTree.invalidExpression
  3278. ELSE
  3279. x.SetFirst(first);
  3280. x.SetLast(last);
  3281. x.SetStep(step);
  3282. x.SetType(system.rangeType);
  3283. resolvedExpression := x;
  3284. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3285. END
  3286. END VisitRangeExpression;
  3287. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3288. BEGIN
  3289. x.SetType(NIL);
  3290. resolvedExpression := x;
  3291. END VisitTensorRangeExpression;
  3292. (** resolve the expression d and return result as designator
  3293. - resolve expression
  3294. - if expression is a designator then return designator else error message and return invalidDesignator
  3295. **)
  3296. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3297. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3298. BEGIN
  3299. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3300. resolved := ResolveExpression(d);
  3301. IF resolved = SyntaxTree.invalidExpression THEN
  3302. (* error should already have been reported *)
  3303. result := SyntaxTree.invalidDesignator;
  3304. ELSIF resolved IS SyntaxTree.Designator THEN
  3305. result := resolved(SyntaxTree.Designator);
  3306. ELSE
  3307. Error(d.position,"is no designator ! ");
  3308. result := SyntaxTree.invalidDesignator;
  3309. END;
  3310. (* result.type might be nil. *)
  3311. RETURN result
  3312. END ResolveDesignator;
  3313. (**
  3314. symbol designator generated in this module
  3315. nothing to be resolved
  3316. **)
  3317. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3318. BEGIN
  3319. resolvedExpression := x;
  3320. END VisitSymbolDesignator;
  3321. (**
  3322. self designator generated in this module
  3323. nothing to be resolved
  3324. **)
  3325. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3326. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3327. BEGIN
  3328. (* check if in record scope *)
  3329. scope := currentScope;
  3330. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3331. scope := scope.outerScope;
  3332. END;
  3333. IF scope = NIL THEN (* in module scope *)
  3334. x.SetType(system.anyType);
  3335. ELSIF scope IS SyntaxTree.CellScope THEN
  3336. cell := scope(SyntaxTree.CellScope).ownerCell;
  3337. x.SetType(cell);
  3338. ELSE (* in record scope *)
  3339. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3340. IF (record # NIL) & (record.pointerType # NIL) THEN
  3341. type := ResolveType(record.pointerType);
  3342. x.SetType(type);
  3343. ELSE
  3344. x.SetType(record);
  3345. END;
  3346. END;
  3347. resolvedExpression := x;
  3348. END VisitSelfDesignator;
  3349. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3350. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3351. BEGIN
  3352. scope := currentScope;
  3353. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3354. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3355. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3356. returnType := procedureType.returnType;
  3357. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3358. THEN
  3359. x.SetType(returnType);
  3360. ELSE
  3361. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3362. x.SetType(SyntaxTree.invalidType);
  3363. END;
  3364. ELSE
  3365. Error(x.position,"forbidden access to result designator");
  3366. x.SetType(SyntaxTree.invalidType);
  3367. END;
  3368. x.SetAssignable(TRUE);
  3369. resolvedExpression := x;
  3370. END VisitResultDesignator;
  3371. (**
  3372. return symbol designator as an expression
  3373. - if symbol is a constant then return the constant value expression
  3374. - else
  3375. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3376. - if symbol is a guarded variable then return a TypeGuardDesignator
  3377. - else return a symbol designator
  3378. **)
  3379. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3380. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3381. guardType: SyntaxTree.Type;
  3382. BEGIN
  3383. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3384. result := SyntaxTree.invalidExpression;
  3385. ASSERT(symbol # NIL);
  3386. (*
  3387. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3388. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3389. Error(position, "type not allowed here");
  3390. ELS *)
  3391. (* not needed any more as values are stored in the expression
  3392. IF symbol IS SyntaxTree.Constant THEN
  3393. result := symbol(SyntaxTree.Constant).value
  3394. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3395. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3396. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3397. ELSE
  3398. result := symbol(SyntaxTree.Constant).value
  3399. END;
  3400. ELSE
  3401. *)
  3402. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3403. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3404. THEN
  3405. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3406. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3407. left := NewDereferenceDesignator(position,left);
  3408. left.SetHidden(TRUE);
  3409. END;
  3410. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3411. scope := currentScope;
  3412. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3413. scope := scope.outerScope;
  3414. END;
  3415. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3416. Error(position, "forbidden access to symbol in parent procedure scope");
  3417. END;
  3418. END;
  3419. 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);
  3420. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3421. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3422. ELSE
  3423. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3424. END;
  3425. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3426. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3427. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3428. result.SetType(symbol.type);
  3429. result.SetAssignable(assignable);
  3430. symbol.MarkUsed;
  3431. IF symbol IS SyntaxTree.Constant THEN
  3432. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3433. END;
  3434. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3435. variableAccessed := TRUE
  3436. END;
  3437. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3438. IF GetGuard(symbol,guardType) THEN
  3439. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3440. END;
  3441. END;
  3442. ASSERT(result.type # NIL);
  3443. RETURN result
  3444. END NewSymbolDesignator;
  3445. (** check and resolve an identifier designator "identifier"
  3446. - if identifier = self then return SelfDesignator
  3447. - else find symbol in current scope
  3448. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3449. **)
  3450. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3451. VAR symbol: SyntaxTree.Symbol;
  3452. BEGIN
  3453. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3454. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3455. IF symbol # NIL THEN
  3456. ResolveSymbol(symbol);
  3457. ASSERT(symbol.type # NIL);
  3458. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3459. ELSE
  3460. Error(identifierDesignator.position,"Undeclared Identifier");
  3461. IF VerboseErrorMessage THEN
  3462. Printout.Info("undeclared identifier designator",identifierDesignator);
  3463. END;
  3464. resolvedExpression := SyntaxTree.invalidDesignator;
  3465. END;
  3466. END VisitIdentifierDesignator;
  3467. (** check and resolve a selector designator of the form left.designator
  3468. - if left is a pointer type then do auto dereferenciation
  3469. - left denotes a search scope:
  3470. - if left type is import type then set search scope to respective module
  3471. - if left type is enumeration type then set search scope to respective enumeration scope
  3472. - elsif left type is record type then set search scope to record scope
  3473. - search symbol in computed scope
  3474. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3475. **)
  3476. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3477. VAR
  3478. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3479. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3480. BEGIN
  3481. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3482. left := ResolveDesignator(selectorDesignator.left);
  3483. result := SyntaxTree.invalidDesignator;
  3484. IF left # NIL THEN
  3485. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3486. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3487. END;
  3488. scope := NIL;
  3489. IF left.type = NIL THEN
  3490. Error(selectorDesignator.position,"field on nil typed designator");
  3491. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3492. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3493. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3494. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3495. module := symbol(SyntaxTree.Import).module;
  3496. IF module # NIL THEN
  3497. scope := module.moduleScope
  3498. ELSE
  3499. Error(left.position,"module not loaded");
  3500. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3501. END;
  3502. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3503. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3504. ASSERT(scope # NIL)
  3505. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3506. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3507. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3508. IF type IS SyntaxTree.EnumerationType THEN
  3509. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3510. ELSE
  3511. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3512. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3513. END;
  3514. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3515. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3516. ELSE
  3517. Error(selectorDesignator.position,"field on non-record type designator");
  3518. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3519. END;
  3520. symbol := NIL;
  3521. IF scope # NIL THEN
  3522. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3523. IF symbol # NIL THEN
  3524. ResolveSymbol(symbol);
  3525. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3526. symbol.MarkUsed
  3527. ELSE
  3528. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3529. IF VerboseErrorMessage THEN
  3530. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3531. Printout.Info("scope", scope);
  3532. Printout.Info("left", left);
  3533. Printout.Info("undeclared identifier",selectorDesignator);
  3534. Printout.Info("left resolved designator",left);
  3535. END
  3536. END;
  3537. END;
  3538. END;
  3539. resolvedExpression := result;
  3540. END VisitSelectorDesignator;
  3541. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3542. VAR len,idx: LONGINT;
  3543. BEGIN
  3544. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3545. IF idx < 0 THEN
  3546. Error(index.position,"index out of bounds (too small)")
  3547. ELSE
  3548. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3549. Error(index.position,"index out of bounds (too large)");
  3550. END;
  3551. END;
  3552. END;
  3553. END IndexCheck;
  3554. (*
  3555. - if index designator has not type, use newBaseType as its type
  3556. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3557. - special rule: if static array of dynamic array occurs, make it all dynamic
  3558. index designator type: new base type: new index designator type:
  3559. NIL z z
  3560. ARRAY [x, y] z ARRAY [x, y] OF z
  3561. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3562. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3563. *)
  3564. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3565. VAR
  3566. mathArrayType: SyntaxTree.MathArrayType;
  3567. makeDynamic: BOOLEAN;
  3568. BEGIN
  3569. IF indexDesignator.type = NIL THEN
  3570. indexDesignator.SetType(newBaseType)
  3571. ELSE
  3572. (* index designator must be a of math array type *)
  3573. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3574. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3575. (* determine if all arrays have to be made dynamic *)
  3576. makeDynamic :=
  3577. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3578. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3579. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3580. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3581. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3582. END;
  3583. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3584. mathArrayType.SetArrayBase(newBaseType)
  3585. END
  3586. END SetIndexBaseType;
  3587. (** check and append index list element to index designator of math array
  3588. - check validity of single index or array range
  3589. - compute new type
  3590. - if range then create new array type (calculate length of resulting array)
  3591. - otherwise take sourceArray.arrayBase as new type
  3592. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3593. **)
  3594. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3595. VAR
  3596. targetArray: SyntaxTree.MathArrayType;
  3597. first, last, step: SyntaxTree.Expression;
  3598. firstValue, lastValue, stepValue, length: LONGINT;
  3599. rangeExpression: SyntaxTree.RangeExpression;
  3600. BEGIN
  3601. IF indexListItem.type = SyntaxTree.invalidType THEN
  3602. (* error already handled *)
  3603. indexDesignator.parameters.AddExpression(indexListItem)
  3604. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3605. indexDesignator.HasRange;
  3606. indexDesignator.HasTensorRange;
  3607. indexDesignator.parameters.AddExpression(indexListItem);
  3608. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3609. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3610. IndexCheck(indexListItem, sourceArray.length);
  3611. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3612. indexDesignator.parameters.AddExpression(indexListItem)
  3613. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3614. indexDesignator.HasRange;
  3615. (* if the range is given as an array range expression, check the validity of its components *)
  3616. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3617. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3618. first := rangeExpression.first;
  3619. last := rangeExpression.last;
  3620. step := rangeExpression.step;
  3621. (* perform static checks on range components *)
  3622. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3623. Error(indexListItem.position,"lower bound of array range too small")
  3624. END;
  3625. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3626. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3627. Error(indexListItem.position,"upper bound of array range too large")
  3628. END
  3629. END;
  3630. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3631. Error(indexListItem.position,"invalid step size")
  3632. END;
  3633. (* add conversions to size type *)
  3634. (* TODO: needed? *)
  3635. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3636. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3637. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3638. END;
  3639. IF indexDesignator.hasTensorRange THEN
  3640. (* the index designator's base type is a tensor: leave it as is *)
  3641. ELSE
  3642. (* append a new math array to the index designator's base type *)
  3643. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3644. IF ~error THEN
  3645. (*
  3646. (* optimization: calculate length of target array for static ranges *)
  3647. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3648. IF IsStaticallyOpenRange(rangeExpression) THEN
  3649. (* range is open ('*'): reuse source array length as target array length *)
  3650. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3651. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3652. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3653. IF lastValue = MAX(LONGINT) THEN
  3654. IF IsIntegerValue(sourceArray.length, length) THEN
  3655. lastValue := length - 1;
  3656. isStaticTargetArrayLength := TRUE
  3657. ELSE
  3658. isStaticTargetArrayLength := FALSE
  3659. END
  3660. ELSE
  3661. isStaticTargetArrayLength := TRUE
  3662. END;
  3663. IF isStaticTargetArrayLength THEN
  3664. (* calculate static target array length *)
  3665. IF firstValue > lastValue THEN
  3666. length := 0
  3667. ELSE
  3668. length := 1 + lastValue - firstValue;
  3669. IF length MOD stepValue = 0 THEN
  3670. length := length DIV stepValue
  3671. ELSE
  3672. length := length DIV stepValue + 1
  3673. END
  3674. END;
  3675. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3676. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3677. ASSERT(targetArray.form = SyntaxTree.Static)
  3678. END
  3679. END
  3680. END
  3681. *)
  3682. END;
  3683. SetIndexBaseType(indexDesignator, targetArray)
  3684. END;
  3685. indexDesignator.parameters.AddExpression(indexListItem)
  3686. ELSE
  3687. Error(position,"invalid index list item");
  3688. END;
  3689. END AppendMathIndex;
  3690. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3691. VAR parameters: SyntaxTree.ExpressionList;
  3692. BEGIN
  3693. parameters := index.parameters;
  3694. IF (expression.type = NIL) THEN
  3695. Error(position, "invalid index");
  3696. ELSIF IsIntegerType(expression.type.resolved) THEN
  3697. IF over IS SyntaxTree.ArrayType THEN
  3698. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3699. ELSIF over IS SyntaxTree.StringType THEN
  3700. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3701. END;
  3702. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3703. parameters.AddExpression(expression);
  3704. ELSE
  3705. Error(position, "invalid index");
  3706. END;
  3707. END AppendIndex;
  3708. (** convert an expression to math array type
  3709. if expression is of math array type: return expression itself
  3710. if expression is of array-structured object type: return an index operator call on it
  3711. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3712. otherwise: return invalid expression
  3713. **)
  3714. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3715. VAR
  3716. result: SyntaxTree.Expression;
  3717. mathArrayType: SyntaxTree.MathArrayType;
  3718. BEGIN
  3719. IF expression.type = NIL THEN
  3720. result := SyntaxTree.invalidExpression
  3721. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3722. (* expression of math array type *)
  3723. result := expression
  3724. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3725. (* expression of array-structured object type *)
  3726. mathArrayType := MathArrayStructureOfType(expression.type);
  3727. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3728. ELSE
  3729. result := SyntaxTree.invalidExpression
  3730. END;
  3731. RETURN result
  3732. END ConvertToMathArray;
  3733. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3734. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3735. VAR
  3736. result: SyntaxTree.ExpressionList;
  3737. i: LONGINT;
  3738. BEGIN
  3739. result := SyntaxTree.NewExpressionList();
  3740. FOR i := 1 TO itemCount DO
  3741. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3742. END;
  3743. RETURN result
  3744. END ListOfOpenRanges;
  3745. (** create a procedure call designator for an index operator call on an array-structured object type
  3746. - use given index list as actual parameters
  3747. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3748. **)
  3749. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3750. VAR
  3751. operator: SyntaxTree.Operator;
  3752. expression: SyntaxTree.Expression;
  3753. actualParameters, tempList: SyntaxTree.ExpressionList;
  3754. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3755. result, tempDesignator: SyntaxTree.Designator;
  3756. recordType: SyntaxTree.RecordType;
  3757. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3758. i, hashValue, indexListSize, indexListKind: LONGINT;
  3759. castReturnType: SyntaxTree.MathArrayType;
  3760. BEGIN
  3761. ASSERT(IsArrayStructuredObjectType(left.type));
  3762. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3763. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3764. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3765. indexListSize := indexList.Length();
  3766. indexListKind := 0;
  3767. containsNonRange := FALSE;
  3768. FOR i := 0 TO indexList.Length() - 1 DO
  3769. indexListKind := indexListKind * 2;
  3770. expression := indexList.GetExpression(i);
  3771. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3772. INC(indexListKind)
  3773. ELSE
  3774. containsNonRange := TRUE
  3775. END
  3776. END;
  3777. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3778. (* select applicable index operator
  3779. - try to look up optimal index operator
  3780. - if not present, use operator on ranges
  3781. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3782. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3783. *)
  3784. usesGeneralOperator := FALSE;
  3785. IF rhs # NIL THEN
  3786. (* write operator *)
  3787. IF hashValue = -1 THEN
  3788. operator := NIL
  3789. ELSE
  3790. operator := recordType.arrayAccessOperators.write[hashValue];
  3791. END;
  3792. IF operator = NIL THEN
  3793. usesPureRangeOperator := TRUE;
  3794. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3795. operator := recordType.arrayAccessOperators.generalWrite;
  3796. usesGeneralOperator := TRUE
  3797. ELSE
  3798. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3799. operator := recordType.arrayAccessOperators.write[hashValue];
  3800. END
  3801. END
  3802. ELSE
  3803. (* read operator *)
  3804. IF hashValue = -1 THEN
  3805. operator := NIL
  3806. ELSE
  3807. operator := recordType.arrayAccessOperators.read[hashValue];
  3808. END;
  3809. IF operator = NIL THEN
  3810. usesPureRangeOperator := TRUE;
  3811. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3812. operator := recordType.arrayAccessOperators.generalRead;
  3813. usesGeneralOperator := TRUE
  3814. ELSE
  3815. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3816. operator := recordType.arrayAccessOperators.read[hashValue];
  3817. END
  3818. END
  3819. END;
  3820. IF operator = NIL THEN
  3821. Error(position, "call of undeclared [] operator");
  3822. result := SyntaxTree.invalidDesignator;
  3823. ELSE
  3824. (* determine if reshaping is needed *)
  3825. needsReshaping := containsNonRange & usesPureRangeOperator;
  3826. (* import OCArrayBase if reshaping is needed *)
  3827. IF needsReshaping & ~arrayBaseImported THEN
  3828. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3829. arrayBaseImported := TRUE
  3830. END;
  3831. (* add the index list item to the list of actual parameters
  3832. - for general operators: add a single inline array containing the index list items as parameter
  3833. - otherwise: add all index list items as individual parameters
  3834. *)
  3835. actualParameters := SyntaxTree.NewExpressionList();
  3836. IF usesGeneralOperator THEN
  3837. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3838. END;
  3839. FOR i := 0 TO indexListSize - 1 DO
  3840. expression := indexList.GetExpression(i);
  3841. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3842. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3843. tempList := SyntaxTree.NewExpressionList();
  3844. tempList.AddExpression(expression);
  3845. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3846. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3847. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3848. END;
  3849. IF usesGeneralOperator THEN
  3850. tempMathArrayExpression.elements.AddExpression(expression);
  3851. ELSE
  3852. actualParameters.AddExpression(expression)
  3853. END
  3854. END;
  3855. IF usesGeneralOperator THEN
  3856. actualParameters.AddExpression(tempMathArrayExpression)
  3857. END;
  3858. IF rhs # NIL THEN
  3859. (* add actual parameter for RHS *)
  3860. IF needsReshaping THEN
  3861. (* reshape using OCArrayBase.ExpandDimensions *)
  3862. tempList := SyntaxTree.NewExpressionList();
  3863. (* source array *)
  3864. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3865. tempList.AddExpression(rhs);
  3866. ELSE
  3867. (* convert scalar to one-dimensional array *)
  3868. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3869. tempMathArrayExpression.elements.AddExpression(rhs);
  3870. tempList.AddExpression(tempMathArrayExpression)
  3871. END;
  3872. (* list of kept dimensions *)
  3873. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3874. FOR i := 0 TO indexListSize - 1 DO
  3875. expression := indexList.GetExpression(i);
  3876. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3877. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3878. ELSE
  3879. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3880. END
  3881. END;
  3882. tempList.AddExpression(tempMathArrayExpression);
  3883. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3884. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3885. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3886. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3887. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3888. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3889. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3890. expression.SetType(castReturnType);
  3891. ELSE
  3892. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3893. END;
  3894. actualParameters.AddExpression(expression)
  3895. ELSE
  3896. actualParameters.AddExpression(rhs)
  3897. END
  3898. END;
  3899. (* add dereference operator and create procedure call designator *)
  3900. ASSERT(left IS SyntaxTree.Designator);
  3901. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3902. ASSERT(expression IS SyntaxTree.Designator);
  3903. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3904. IF (rhs = NIL) & needsReshaping THEN
  3905. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3906. tempList := SyntaxTree.NewExpressionList();
  3907. FOR i := 0 TO indexList.Length() - 1 DO
  3908. expression := indexList.GetExpression(i);
  3909. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3910. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3911. ELSE
  3912. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3913. END
  3914. END;
  3915. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3916. END;
  3917. IF rhs = NIL THEN
  3918. (* special rule: index read operator calls are considered to be assignable *)
  3919. result.SetAssignable(TRUE)
  3920. END;
  3921. (* put information about this index operator call into the resulting designator *)
  3922. result.SetRelatedAsot(left);
  3923. result.SetRelatedIndexList(indexList)
  3924. END;
  3925. RETURN result
  3926. END NewIndexOperatorCall;
  3927. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3928. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3929. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3930. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3931. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3932. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3933. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3934. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3935. CONST trace = FALSE;
  3936. BEGIN
  3937. IF trace THEN
  3938. FOR i := 0 TO actualParameters.Length()-1 DO
  3939. Printout.Info("par", actualParameters.GetExpression(i));
  3940. END;
  3941. END;
  3942. operator := scope.firstOperator;
  3943. WHILE(operator # NIL) DO
  3944. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3945. procedureType := operator.type(SyntaxTree.ProcedureType);
  3946. distance := Distance(system, procedureType,actualParameters);
  3947. IF trace THEN Printout.Info("check op ",operator) END;
  3948. IF distance < bestDistance THEN
  3949. IF trace THEN Printout.Info("taken op",operator) END;
  3950. bestDistance := distance;
  3951. bestOperator := operator;
  3952. END;
  3953. END;
  3954. operator := operator.nextOperator;
  3955. END;
  3956. END FindInScope;
  3957. BEGIN
  3958. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3959. IF oper = 0 THEN (* index *)
  3960. identifier := SyntaxTree.NewIdentifier("[]");
  3961. ELSE
  3962. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3963. END;
  3964. WHILE (recordType # NIL) DO
  3965. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3966. recordType := recordType.GetBaseRecord();
  3967. END;
  3968. RETURN bestOperator
  3969. END FindOperator;
  3970. BEGIN
  3971. type := left.type.resolved;
  3972. IF type IS SyntaxTree.RecordType THEN
  3973. pointer := FALSE;
  3974. recordType := type(SyntaxTree.RecordType);
  3975. ELSE
  3976. pointer := TRUE;
  3977. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3978. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3979. END;
  3980. actualParameters := SyntaxTree.NewExpressionList();
  3981. IF parameters # NIL THEN
  3982. FOR i := 0 TO parameters.Length()-1 DO
  3983. expression := ResolveExpression(parameters.GetExpression(i));
  3984. actualParameters.AddExpression(expression);
  3985. END;
  3986. END;
  3987. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3988. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3989. IF op # NIL THEN
  3990. designator := left(SyntaxTree.Designator);
  3991. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3992. expression := NewSymbolDesignator(position, designator , op);
  3993. ASSERT(expression IS SyntaxTree.Designator);
  3994. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3995. result.SetRelatedAsot(left);
  3996. result.SetRelatedIndexList(parameters);
  3997. (* check if write operator exists, for var parameters *)
  3998. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3999. actualParameters := SyntaxTree.NewExpressionList();
  4000. FOR i := 0 TO parameters.Length()-1 DO
  4001. expression := ResolveExpression(parameters.GetExpression(i));
  4002. actualParameters.AddExpression(expression);
  4003. END;
  4004. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4005. actualParameters.AddExpression(rhs);
  4006. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4007. IF op = NIL THEN rhs := NIL END;
  4008. END;
  4009. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4010. ELSE
  4011. result := NIL;
  4012. END;
  4013. RETURN result;
  4014. END NewObjectOperatorCall;
  4015. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4016. 1. convert bracket designator chains into a single one that contains separators
  4017. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4018. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4019. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4020. - if an array or math array is indexed over, create index designator
  4021. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4022. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4023. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4024. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4025. - if an array-structured object type is indexed over, create procedure call designator
  4026. e.g.: a[x, y] -> a^."[]"(x, y)
  4027. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4028. - a[i, *] = a[i][*]
  4029. - a[*, i] # a[*][i]
  4030. Because:
  4031. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4032. - 'i-th column' = a[*, i]
  4033. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4034. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4035. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4036. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4037. **)
  4038. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4039. VAR
  4040. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4041. indexDesignator: SyntaxTree.IndexDesignator;
  4042. designator: SyntaxTree.Designator;
  4043. type: SyntaxTree.Type;
  4044. recordType: SyntaxTree.RecordType;
  4045. expression, rhs: SyntaxTree.Expression;
  4046. indexList: SyntaxTree.ExpressionList;
  4047. i: LONGINT;
  4048. hasError, done: BOOLEAN;
  4049. PROCEDURE FinalizeIndexDesignator;
  4050. BEGIN
  4051. IF indexDesignator # NIL THEN
  4052. (* the end of a tensor has been reached: *)
  4053. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4054. SetIndexBaseType(indexDesignator, type);
  4055. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4056. designator := indexDesignator;
  4057. type := designator.type.resolved;
  4058. indexDesignator := NIL;
  4059. ASSERT(SyntaxTree.Resolved IN type.state)
  4060. END
  4061. END FinalizeIndexDesignator;
  4062. BEGIN
  4063. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4064. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4065. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4066. (* copy all index list entries including a separator to the left bracket designator *)
  4067. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4068. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4069. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4070. END;
  4071. (* propagate the related RHS *)
  4072. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4073. (* only resolve left bracket designator and use as final result *)
  4074. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4075. ELSE
  4076. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4077. designator := ResolveDesignator(bracketDesignator.left);
  4078. type := designator.type.resolved;
  4079. indexDesignator := NIL;
  4080. (*!!! clean up *)
  4081. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4082. OR (type IS SyntaxTree.RecordType)
  4083. THEN
  4084. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4085. IF resolvedExpression = NIL THEN
  4086. Error(bracketDesignator.position,"undefined operator");
  4087. resolvedExpression := SyntaxTree.invalidDesignator
  4088. END;
  4089. RETURN;
  4090. END;
  4091. i := 0;
  4092. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4093. expression := bracketDesignator.parameters.GetExpression(i);
  4094. expression := ResolveExpression(expression);
  4095. bracketDesignator.parameters.SetExpression(i, expression);
  4096. IF expression = SyntaxTree.indexListSeparator THEN
  4097. (* finalize an existing index designator if needed *)
  4098. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4099. INC(i)
  4100. ELSE
  4101. (* do auto-dereferencing if needed *)
  4102. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4103. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4104. & (i=0)*)
  4105. THEN
  4106. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4107. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4108. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4109. designator := SyntaxTree.invalidDesignator;
  4110. type := SyntaxTree.invalidType
  4111. ELSE
  4112. FinalizeIndexDesignator;
  4113. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4114. type := designator.type.resolved
  4115. END
  4116. END;
  4117. (* create a new index designator, if needed *)
  4118. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4119. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4120. indexDesignator.SetAssignable(designator.assignable);
  4121. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4122. (* designator := indexDesignator *)
  4123. END;
  4124. IF type = SyntaxTree.invalidType THEN
  4125. (* error already handled *)
  4126. INC(i)
  4127. ELSIF type IS SyntaxTree.ArrayType THEN
  4128. (* indexing over an array *)
  4129. ASSERT(indexDesignator # NIL);
  4130. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4131. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4132. INC(i)
  4133. ELSIF type IS SyntaxTree.StringType THEN
  4134. (* indexing over an array *)
  4135. ASSERT(indexDesignator # NIL);
  4136. AppendIndex(expression.position, indexDesignator, expression, type);
  4137. type := type(SyntaxTree.StringType).baseType.resolved;
  4138. INC(i)
  4139. ELSIF type IS SyntaxTree.MathArrayType THEN
  4140. (* indexing over a math array *)
  4141. ASSERT(indexDesignator # NIL);
  4142. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4143. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4144. INC(i)
  4145. ELSIF IsArrayStructuredObjectType(type) THEN
  4146. (* indexing over ASOTs *)
  4147. FinalizeIndexDesignator;
  4148. ASSERT(type IS SyntaxTree.PointerType);
  4149. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4150. (*
  4151. - collect index list items from bracket designator that belong to ASOT
  4152. - check for errors
  4153. *)
  4154. indexList := SyntaxTree.NewExpressionList();
  4155. hasError := FALSE;
  4156. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4157. (* indexing over tensor ASOT:
  4158. - stop at index list end or separator
  4159. - dimensionality is given by number of index list items
  4160. *)
  4161. done := FALSE;
  4162. WHILE ~done DO
  4163. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4164. done := TRUE;
  4165. ELSE
  4166. expression := bracketDesignator.parameters.GetExpression(i);
  4167. IF expression = SyntaxTree.indexListSeparator THEN
  4168. done := TRUE;
  4169. ELSE
  4170. expression := ResolveExpression(expression);
  4171. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4172. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4173. hasError := TRUE
  4174. ELSIF ~(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. ELSE
  4185. (* indexing over non-tensor ASOT:
  4186. - ignore separators
  4187. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4188. *)
  4189. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4190. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4191. expression := bracketDesignator.parameters.GetExpression(i);
  4192. ELSE
  4193. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4194. END;
  4195. IF expression # SyntaxTree.indexListSeparator THEN
  4196. expression := ResolveExpression(expression);
  4197. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4198. Error(expression.position, "integer or range expected");
  4199. expression := SyntaxTree.invalidExpression;
  4200. hasError := TRUE
  4201. END;
  4202. indexList.AddExpression(expression)
  4203. END;
  4204. INC(i)
  4205. END;
  4206. END;
  4207. IF hasError THEN
  4208. designator := SyntaxTree.invalidDesignator;
  4209. type := SyntaxTree.invalidType;
  4210. ELSE
  4211. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4212. and the last entry in the index list belongs to the array-structured object type in question.
  4213. E.g.: for a 2-dimensional array-structured object type:
  4214. - 'lhs := asot[1, 2]' -> read mode
  4215. - 'asot[1, 2] := rhs' -> write mode
  4216. - 'asot[1, 2, 3] := rhs' -> read mode
  4217. *)
  4218. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4219. rhs := bracketDesignator.relatedRhs
  4220. ELSE
  4221. rhs := NIL
  4222. END;
  4223. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4224. type := designator.type
  4225. END
  4226. ELSE
  4227. Error(expression.position,"indexing over non-array type");
  4228. designator := SyntaxTree.invalidDesignator;
  4229. type := SyntaxTree.invalidType;
  4230. INC(i)
  4231. END
  4232. END
  4233. END;
  4234. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4235. resolvedExpression := designator
  4236. END
  4237. END VisitBracketDesignator;
  4238. (** check and resolve expression list
  4239. - resolve each expression in an expression list
  4240. - returns true if and only if all statements could have successfully been resolved
  4241. **)
  4242. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4243. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4244. BEGIN
  4245. result := TRUE;
  4246. FOR i := 0 TO expressionList.Length()-1 DO
  4247. expression := ResolveExpression(expressionList.GetExpression(i));
  4248. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4249. expressionList.SetExpression(i,expression);
  4250. END;
  4251. RETURN result
  4252. END ExpressionList;
  4253. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4254. BEGIN
  4255. type := type.resolved;
  4256. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4257. RETURN TRUE
  4258. ELSIF system.CanPassInRegister # NIL THEN
  4259. RETURN system.CanPassInRegister(type);
  4260. ELSE
  4261. RETURN FALSE
  4262. END;
  4263. END CanPassInRegister;
  4264. (** return procedure call designator left(actualParameters)
  4265. - check realtime procedure call in realtime procedure
  4266. - check number of parameters
  4267. - check parameter compatibility
  4268. return invalidDesignator if error
  4269. **)
  4270. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4271. VAR result: SyntaxTree.Designator;
  4272. numberFormalParameters, numberActualParameters: LONGINT;
  4273. formalType: SyntaxTree.ProcedureType;
  4274. formalParameter: SyntaxTree.Parameter;
  4275. actualParameter: SyntaxTree.Expression;
  4276. i: LONGINT;
  4277. self: SyntaxTree.Expression;
  4278. BEGIN
  4279. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4280. result := SyntaxTree.invalidDesignator;
  4281. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4282. numberFormalParameters := formalType.numberParameters;
  4283. numberActualParameters := actualParameters.Length();
  4284. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4285. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4286. END;
  4287. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4288. self := left.left;
  4289. IF (self # NIL) & ~IsVariable(self) THEN
  4290. Error(self.position, "Non-variable expression on variable receiver");
  4291. END;
  4292. END;
  4293. IF ~ExpressionList(actualParameters) THEN
  4294. result := SyntaxTree.invalidDesignator
  4295. ELSE
  4296. IF numberActualParameters <= numberFormalParameters THEN
  4297. formalParameter := formalType.firstParameter;
  4298. FOR i := 0 TO numberActualParameters-1 DO
  4299. actualParameter := actualParameters.GetExpression(i);
  4300. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4301. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4302. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4303. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4304. ELSE
  4305. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4306. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4307. END;
  4308. actualParameters.SetExpression(i,actualParameter);
  4309. END;
  4310. formalParameter := formalParameter.nextParameter;
  4311. END;
  4312. WHILE (formalParameter # NIL) DO
  4313. IF formalParameter.defaultValue # NIL THEN
  4314. actualParameters.AddExpression(formalParameter.defaultValue);
  4315. formalParameter := formalParameter.nextParameter
  4316. ELSE
  4317. Error(position, "less actual than formal parameters");
  4318. formalParameter := NIL;
  4319. END;
  4320. END;
  4321. ELSE
  4322. Error(position, "more actual than formal parameters")
  4323. END;
  4324. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4325. result.SetAssignable(FALSE);
  4326. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4327. END;
  4328. RETURN result
  4329. END NewProcedureCallDesignator;
  4330. (**
  4331. builtin call designator generated in VisitParameterDesignator
  4332. -> nothing to be resolved
  4333. **)
  4334. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4335. BEGIN
  4336. resolvedExpression := x;
  4337. END VisitTypeGuardDesignator;
  4338. (**
  4339. builtin call designator generated in VisitParameterDesignator
  4340. -> nothing to be resolved
  4341. **)
  4342. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4343. BEGIN
  4344. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4345. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4346. ASSERT(resolvedExpression.type # NIL);
  4347. ELSIF ExpressionList(x.parameters) THEN
  4348. resolvedExpression := x;
  4349. END;
  4350. END VisitBuiltinCallDesignator;
  4351. (**
  4352. procedure call designator generated in VisitParameterDesignator
  4353. -> nothing to be resolved
  4354. **)
  4355. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4356. BEGIN
  4357. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4358. resolvedExpression := x;
  4359. END VisitProcedureCallDesignator;
  4360. (** return true if x is a variable else return false and report error **)
  4361. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4362. VAR result: BOOLEAN;
  4363. BEGIN
  4364. result := TRUE;
  4365. IF x = SyntaxTree.invalidExpression THEN
  4366. result := FALSE;
  4367. ELSIF ~IsVariable(x) THEN
  4368. Error(x.position,"non variable expression");
  4369. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4370. result := FALSE;
  4371. END;
  4372. RETURN result
  4373. END CheckVariable;
  4374. (**
  4375. if expression x is of basic type then return true else report error and return false
  4376. **)
  4377. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4378. VAR result: BOOLEAN;
  4379. BEGIN
  4380. result := FALSE;
  4381. IF x = SyntaxTree.invalidExpression THEN
  4382. ELSIF ~IsBasicType(x.type) THEN
  4383. Error(x.position,"is no basic type");
  4384. result := FALSE
  4385. ELSE result := TRUE
  4386. END;
  4387. RETURN result
  4388. END CheckBasicType;
  4389. (**
  4390. if expression x is of number type then return true else report error and return false
  4391. **)
  4392. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4393. VAR result: BOOLEAN;
  4394. BEGIN
  4395. result := FALSE;
  4396. IF x = SyntaxTree.invalidExpression THEN
  4397. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4398. Error(x.position,"is non number type");
  4399. ELSE result := TRUE
  4400. END;
  4401. RETURN result
  4402. END CheckNumberType;
  4403. (**
  4404. if expression x is of number or size type but not complex then return true else report error and return false
  4405. **)
  4406. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4407. VAR result: BOOLEAN;
  4408. BEGIN
  4409. result := FALSE;
  4410. IF x = SyntaxTree.invalidExpression THEN
  4411. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4412. Error(x.position,"is complex type");
  4413. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4414. Error(x.position,"is non number type");
  4415. ELSE result := TRUE
  4416. END;
  4417. RETURN result
  4418. END CheckNonComplexNumberSizeType;
  4419. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4420. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4421. BEGIN
  4422. result := FALSE; type := x.type.resolved;
  4423. IF x = SyntaxTree.invalidExpression THEN
  4424. 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
  4425. TRACE(type.sizeInBits);
  4426. TRACE(system.addressType.sizeInBits);
  4427. Error(x.position,"is no address type");
  4428. ELSE result := TRUE
  4429. END;
  4430. RETURN result
  4431. END CheckAddressType;
  4432. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4433. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4434. BEGIN
  4435. result := FALSE; type := x.type.resolved;
  4436. IF x = SyntaxTree.invalidExpression THEN
  4437. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4438. Error(x.position,"is no size type");
  4439. ELSE result := TRUE
  4440. END;
  4441. RETURN result
  4442. END CheckSizeType;
  4443. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4444. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4445. BEGIN
  4446. result := FALSE; type := x.type.resolved;
  4447. IF x = SyntaxTree.invalidExpression THEN
  4448. 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
  4449. Error(x.position,"is no object type");
  4450. ELSE result := TRUE
  4451. END;
  4452. RETURN result
  4453. END CheckObjectType;
  4454. (**
  4455. if expression x is of integer type then return true else report error and return false
  4456. **)
  4457. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4458. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4459. BEGIN
  4460. result := FALSE; type := x.type.resolved;
  4461. IF x = SyntaxTree.invalidExpression THEN
  4462. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4463. Error(x.position,"is no integer type");
  4464. ELSE result := TRUE
  4465. END;
  4466. RETURN result
  4467. END CheckIntegerType;
  4468. (**
  4469. if expression x is of character type then return true else report error and return false
  4470. **)
  4471. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4472. VAR result: BOOLEAN;
  4473. BEGIN
  4474. result := FALSE;
  4475. IF x = SyntaxTree.invalidExpression THEN
  4476. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4477. Error(x.position,"is no character type");
  4478. ELSE result := TRUE
  4479. END;
  4480. RETURN result
  4481. END CheckCharacterType;
  4482. (**
  4483. if expression x is of real type then return true else report error and return false
  4484. **)
  4485. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4486. VAR result: BOOLEAN;
  4487. BEGIN
  4488. result := FALSE;
  4489. IF x = SyntaxTree.invalidExpression THEN
  4490. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4491. Error(x.position,"is no float type");
  4492. ELSE result := TRUE
  4493. END;
  4494. RETURN result
  4495. END CheckRealType;
  4496. (**
  4497. if expression x is of range type then return true else report error and return false
  4498. **)
  4499. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4500. VAR result: BOOLEAN;
  4501. BEGIN
  4502. result := FALSE;
  4503. IF x = SyntaxTree.invalidExpression THEN
  4504. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4505. Error(x.position,"is no range type");
  4506. ELSE result := TRUE
  4507. END;
  4508. RETURN result
  4509. END CheckRangeType;
  4510. (**
  4511. if expression x is of boolean type then return true else report error and return false
  4512. **)
  4513. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4514. VAR result: BOOLEAN;
  4515. BEGIN
  4516. result := FALSE;
  4517. IF x = SyntaxTree.invalidExpression THEN
  4518. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4519. Error(x.position,"is no boolean type");
  4520. ELSE result := TRUE
  4521. END;
  4522. RETURN result
  4523. END CheckBooleanType;
  4524. (**
  4525. if expression x is of set type then return true else report error and return false
  4526. **)
  4527. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4528. VAR result: BOOLEAN;
  4529. BEGIN
  4530. result := FALSE;
  4531. IF x = SyntaxTree.invalidExpression THEN
  4532. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4533. Error(x.position,"is no set type");
  4534. ELSE result := TRUE
  4535. END;
  4536. RETURN result
  4537. END CheckSetType;
  4538. (**
  4539. if expression x is of string or array of character type then return true else report error and return false
  4540. **)
  4541. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4542. VAR result: BOOLEAN;
  4543. BEGIN
  4544. result := FALSE;
  4545. IF x = SyntaxTree.invalidExpression THEN
  4546. ELSIF ~IsStringType(x.type.resolved) THEN
  4547. Error(x.position,"is no string type");
  4548. ELSE result := TRUE
  4549. END;
  4550. RETURN result
  4551. END CheckStringType;
  4552. (**
  4553. if expression x is a type declaration type return true else report error and return false
  4554. **)
  4555. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4556. VAR result: BOOLEAN;
  4557. BEGIN
  4558. result := FALSE;
  4559. IF x = SyntaxTree.invalidExpression THEN
  4560. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4561. Error(x.position,"is not a type declaration");
  4562. ELSE result := TRUE
  4563. END;
  4564. RETURN result
  4565. END CheckTypeDeclarationType;
  4566. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4567. VAR result: BOOLEAN;
  4568. BEGIN
  4569. result := FALSE;
  4570. IF x = SyntaxTree.invalidExpression THEN
  4571. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4572. result := TRUE;
  4573. value := x.resolved(SyntaxTree.IntegerValue).value;
  4574. ELSE
  4575. Error(x.position,"expression is not an integer constant");
  4576. END;
  4577. RETURN result;
  4578. END CheckIntegerValue;
  4579. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4585. result := TRUE;
  4586. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4587. ELSE
  4588. Error(x.position,"expression is not an integer constant");
  4589. END;
  4590. RETURN result;
  4591. END CheckStringValue;
  4592. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4593. BEGIN
  4594. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4595. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4596. ELSE
  4597. RETURN FALSE
  4598. END;
  4599. END IsUnsignedValue;
  4600. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4601. BEGIN
  4602. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4603. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4604. ELSE
  4605. RETURN FALSE
  4606. END
  4607. END IsAddressValue;
  4608. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4609. BEGIN
  4610. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4611. END IsAddressExpression;
  4612. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4613. BEGIN
  4614. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4615. END IsSizeExpression;
  4616. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4617. VAR result: BOOLEAN;
  4618. BEGIN
  4619. result := FALSE;
  4620. IF x = SyntaxTree.invalidExpression THEN
  4621. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4622. result := TRUE;
  4623. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4624. ELSE
  4625. Error(x.position,"expression is not an integer constant");
  4626. END;
  4627. RETURN result;
  4628. END CheckEnumerationValue;
  4629. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4630. VAR result: BOOLEAN;
  4631. BEGIN
  4632. result := FALSE;
  4633. IF x = SyntaxTree.invalidExpression THEN
  4634. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4635. result := TRUE;
  4636. value := x.resolved(SyntaxTree.CharacterValue).value;
  4637. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4638. result := TRUE;
  4639. value := x.resolved(SyntaxTree.StringValue).value[0];
  4640. ELSE
  4641. Error(x.position,"expression is not a character constant");
  4642. END;
  4643. RETURN result;
  4644. END CheckCharacterValue;
  4645. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4646. VAR result: BOOLEAN;
  4647. BEGIN
  4648. result := FALSE;
  4649. IF x = SyntaxTree.invalidExpression THEN
  4650. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4651. value := x.resolved(SyntaxTree.IntegerValue).value;
  4652. IF (value > 0) OR includeZero & (value = 0) THEN
  4653. result := TRUE;
  4654. ELSE
  4655. Error(x.position,"integer is not positive");
  4656. END
  4657. ELSE
  4658. Error(x.position,"expression is not an integer constant");
  4659. END;
  4660. RETURN result;
  4661. END CheckPositiveIntegerValue;
  4662. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4663. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4664. BEGIN
  4665. result := FALSE;
  4666. IF x = SyntaxTree.invalidExpression THEN
  4667. ELSE
  4668. type := x.type.resolved;
  4669. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4670. portType := type(SyntaxTree.PortType);
  4671. result := TRUE
  4672. ELSE
  4673. Error(x.position,"no port type");
  4674. END;
  4675. END;
  4676. RETURN result
  4677. END CheckPortType;
  4678. (* move to builtin procedure call statement ?
  4679. remove builtin procedure call designator ?
  4680. *)
  4681. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4682. VAR
  4683. numberActualParameters,numberFormalParameters: LONGINT;
  4684. formalParameter: SyntaxTree.Parameter;
  4685. actualParameter: SyntaxTree.Expression;
  4686. procedureType: SyntaxTree.ProcedureType;
  4687. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4688. inPort, outPort: SyntaxTree.PortType;
  4689. constructor: SyntaxTree.Procedure;
  4690. type0,type1,type2: SyntaxTree.Type;
  4691. type,base,parameterType: SyntaxTree.Type;
  4692. arrayType: SyntaxTree.ArrayType;
  4693. i, i0, i1, value: LONGINT;
  4694. r,r0,r1,im: LONGREAL;
  4695. c: CHAR;
  4696. id: LONGINT;
  4697. b: BOOLEAN;
  4698. first: LONGINT;
  4699. mathArrayType: SyntaxTree.MathArrayType;
  4700. customBuiltin: SyntaxTree.CustomBuiltin;
  4701. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4702. VAR resultB: BOOLEAN;
  4703. BEGIN
  4704. IF numberActualParameters < from THEN
  4705. Error(position, "less actual than formal parameters");
  4706. result := SyntaxTree.invalidExpression;
  4707. resultB := FALSE;
  4708. ELSIF numberActualParameters > to THEN
  4709. Error(position, "more actual than formal parameters");
  4710. result := SyntaxTree.invalidExpression;
  4711. resultB := FALSE;
  4712. ELSE
  4713. resultB := TRUE;
  4714. END;
  4715. RETURN resultB
  4716. END CheckArity;
  4717. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4718. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4719. BEGIN
  4720. WHILE modifier # NIL DO
  4721. symbol := cellType.FindProperty(modifier.identifier);
  4722. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4723. propertyType := symbol.type.resolved;
  4724. modifierType := modifier.expression.type.resolved;
  4725. IF ~CompatibleTo(system, modifierType, propertyType) &
  4726. ~(
  4727. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4728. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4729. Error(modifier.position,"incompatible to cell property");
  4730. END;
  4731. ELSE
  4732. Error(modifier.position, "undefined property");
  4733. END;
  4734. modifier := modifier.nextModifier;
  4735. END;
  4736. END CheckModifiers;
  4737. BEGIN
  4738. type := NIL; result := NIL;
  4739. type0 := NIL; type1 := NIL; type2 := NIL;
  4740. numberActualParameters := actualParameters.Length();
  4741. IF numberActualParameters>0 THEN
  4742. parameter0 := actualParameters.GetExpression(0);
  4743. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4744. Error(parameter0.position,"forbidden type-less argument");
  4745. result := SyntaxTree.invalidExpression
  4746. END
  4747. END;
  4748. IF numberActualParameters >1 THEN
  4749. parameter1 := actualParameters.GetExpression(1);
  4750. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4751. ELSE
  4752. Error(parameter1.position,"forbidden type-less argument");
  4753. result := SyntaxTree.invalidExpression
  4754. END
  4755. END;
  4756. IF numberActualParameters >2 THEN
  4757. parameter2 := actualParameters.GetExpression(2);
  4758. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4759. ELSE
  4760. Error(parameter2.position,"forbidden type-less argument");
  4761. result := SyntaxTree.invalidExpression
  4762. END
  4763. END;
  4764. IF returnType # NIL THEN
  4765. id := Global.New;
  4766. result := NIL;
  4767. ELSE
  4768. id := builtin.id;
  4769. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4770. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4771. END;
  4772. END;
  4773. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4774. ELSIF result # NIL THEN type := result.type (* operator *)
  4775. ELSE
  4776. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4777. result(SyntaxTree.Designator).SetLeft(left);
  4778. IF returnType # NIL THEN
  4779. type := returnType;
  4780. END;
  4781. (* ---- ASSERT ----- *)
  4782. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4783. IF CheckBooleanType(parameter0) THEN
  4784. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4785. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4786. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4787. *)
  4788. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4789. Error(position, "assert failed");
  4790. END;
  4791. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4792. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4793. rules imposed by the architecture / current runtime
  4794. *)
  4795. END;
  4796. END;
  4797. (* ---- COPY ----- *)
  4798. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4799. IF~IsStringType(type0) THEN
  4800. Error(parameter0.position,"no string type");
  4801. END;
  4802. IF ~IsStringType(type1) THEN
  4803. Error(parameter1.position,"no string type");
  4804. ELSIF CheckVariable(parameter1) THEN
  4805. IF (type0 IS SyntaxTree.StringType) THEN
  4806. arrayType := type1(SyntaxTree.ArrayType);
  4807. IF arrayType.form = SyntaxTree.Static THEN
  4808. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4809. Error(position, "destination length smaller than source length")
  4810. END;
  4811. END;
  4812. END;
  4813. END;
  4814. (* ---- INC, DEC----- *)
  4815. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4816. IF numberActualParameters = 1 THEN
  4817. parameter1 :=Global.NewIntegerValue(system,position,1);
  4818. actualParameters.AddExpression(parameter1);
  4819. END;
  4820. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4821. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4822. Error(position, "incompatible increment");
  4823. ELSE
  4824. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4825. actualParameters.SetExpression(1,parameter1);
  4826. END;
  4827. END;
  4828. (* ---- EXCL, INCL----- *)
  4829. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4830. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4831. IF IsIntegerValue(parameter1,i0) THEN
  4832. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4833. Error(position, "parameter out of SET range")
  4834. END;
  4835. END;
  4836. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4837. actualParameters.SetExpression(1,parameter1);
  4838. END;
  4839. (* ---- HALT, SYSTEM.HALT ----- *)
  4840. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4841. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4842. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4843. rules imposed by the architecture / current runtime
  4844. *)
  4845. END;
  4846. (* ---- WAIT ----- *)
  4847. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4848. IF CheckObjectType(parameter0) THEN
  4849. END;
  4850. (* ---- NEW ----- *)
  4851. ELSIF (id = Global.New) THEN
  4852. IF returnType # NIL THEN
  4853. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4854. ELSE
  4855. first := 1;
  4856. END;
  4857. IF CheckArity(first,Infinity) THEN
  4858. IF currentIsRealtime THEN
  4859. Error(position, "forbidden new in realtime block");
  4860. END;
  4861. (* check constructor *)
  4862. IF (first =0) OR CheckVariable(parameter0) THEN
  4863. IF type0 IS SyntaxTree.PointerType THEN
  4864. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4865. ELSIF type0 IS SyntaxTree.CellType THEN
  4866. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4867. ELSE
  4868. Error(position, "forbidden new on value type");
  4869. END;
  4870. IF type0 IS SyntaxTree.ArrayType THEN
  4871. arrayType := type0(SyntaxTree.ArrayType);
  4872. IF arrayType.form = SyntaxTree.Static THEN
  4873. i := first
  4874. ELSIF arrayType.form = SyntaxTree.Open THEN
  4875. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4876. ELSE HALT(100)
  4877. END;
  4878. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4879. i := first;
  4880. REPEAT
  4881. actualParameter := actualParameters.GetExpression(i);
  4882. IF CheckSizeType(actualParameter) THEN
  4883. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4884. actualParameters.SetExpression(i,actualParameter);
  4885. END;
  4886. INC(i);
  4887. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4888. END;
  4889. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4890. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4891. Error(position, "forbidden new on abstract object");
  4892. END;
  4893. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4894. IF constructor = NIL THEN
  4895. IF CheckArity(first,first) THEN END;
  4896. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4897. Error(position, "new on object with hidden constructor");
  4898. ELSE
  4899. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4900. numberFormalParameters := procedureType.numberParameters;
  4901. IF numberActualParameters-first <= numberFormalParameters THEN
  4902. formalParameter := procedureType.firstParameter;
  4903. FOR i := first TO numberActualParameters-1 DO
  4904. actualParameter := actualParameters.GetExpression(i);
  4905. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4906. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4907. ELSE
  4908. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4909. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4910. END;
  4911. actualParameters.SetExpression(i,actualParameter);
  4912. END;
  4913. formalParameter := formalParameter.nextParameter;
  4914. END;
  4915. WHILE (formalParameter # NIL) DO
  4916. IF formalParameter.defaultValue # NIL THEN
  4917. actualParameters.AddExpression(formalParameter.defaultValue);
  4918. formalParameter := formalParameter.nextParameter
  4919. ELSE
  4920. Error(position, "less actual than formal parameters");
  4921. formalParameter := NIL;
  4922. END;
  4923. END;
  4924. ELSE
  4925. Error(position, "more actual than formal parameters")
  4926. END;
  4927. END;
  4928. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4929. mathArrayType := type0(SyntaxTree.MathArrayType);
  4930. IF mathArrayType.form = SyntaxTree.Static THEN
  4931. Error(position, "new on static array");
  4932. ELSE
  4933. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4934. i0 := first+1; i1 := Infinity;
  4935. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4936. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4937. i1 := i0;
  4938. ELSE HALT(100);
  4939. END;
  4940. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4941. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4942. base := ArrayBase(type0,MAX(LONGINT));
  4943. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4944. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4945. IF ~CompatibleTo(system,type0,parameterType) THEN
  4946. Error(parameter0.position,"incompatible parameter in new");
  4947. result := SyntaxTree.invalidExpression;
  4948. ELSE
  4949. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4950. END;
  4951. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4952. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4953. IF ~CompatibleTo(system,type1,parameterType) THEN
  4954. Error(parameter1.position,"parameter incompatible to math array len type");
  4955. result := SyntaxTree.invalidExpression;
  4956. ELSE
  4957. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4958. END;
  4959. ELSE
  4960. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4961. i := first;
  4962. REPEAT
  4963. actualParameter := actualParameters.GetExpression(i);
  4964. IF CheckSizeType(actualParameter) THEN
  4965. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4966. actualParameters.SetExpression(i,actualParameter);
  4967. END;
  4968. INC(i);
  4969. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4970. END;
  4971. END;
  4972. END;
  4973. ELSIF type0 IS SyntaxTree.CellType THEN
  4974. IF ~(currentIsCellNet) THEN
  4975. Error(position, "cell allocation outside activeCells ");
  4976. ELSE
  4977. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4978. IF (constructor = NIL) & CheckArity(1,1) THEN
  4979. (* ok *)
  4980. ELSE
  4981. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4982. numberFormalParameters := procedureType.numberParameters;
  4983. DEC(numberActualParameters);
  4984. IF numberActualParameters <= numberFormalParameters THEN
  4985. formalParameter := procedureType.firstParameter;
  4986. FOR i := first TO numberActualParameters DO
  4987. actualParameter := actualParameters.GetExpression(i);
  4988. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4989. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4990. ELSE
  4991. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4992. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4993. END;
  4994. actualParameters.SetExpression(i,actualParameter);
  4995. END;
  4996. formalParameter := formalParameter.nextParameter;
  4997. END;
  4998. WHILE (formalParameter # NIL) DO
  4999. IF formalParameter.defaultValue # NIL THEN
  5000. actualParameters.AddExpression(formalParameter.defaultValue);
  5001. formalParameter := formalParameter.nextParameter
  5002. ELSE
  5003. Error(position, "less actual than formal parameters");
  5004. formalParameter := NIL;
  5005. END;
  5006. END;
  5007. ELSE
  5008. Error(position, "more actual than formal parameters")
  5009. END;
  5010. END;
  5011. END;
  5012. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5013. activeCellsStatement := TRUE;
  5014. ELSE
  5015. Error(position, "cannot be allocated");
  5016. END;
  5017. END;
  5018. END;
  5019. (* ---- DISPOSE ----- *)
  5020. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5021. IF ~IsPointerType(parameter0.type) THEN
  5022. Error(parameter0.position,"is not a pointer")
  5023. ELSIF ~IsDisposable(parameter0.type) THEN
  5024. Error(parameter0.position,"is not disposable")
  5025. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5026. END
  5027. (* ---- GETPROCEDURE ----- *)
  5028. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5029. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5030. IF CheckVariable(parameter2) THEN
  5031. IF ~GetProcedureAllowed(parameter2.type) THEN
  5032. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5033. END;
  5034. END;
  5035. END;
  5036. (* ---- ABS ----- *)
  5037. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5038. (* note: ABS on complex numbers is done using overloading *)
  5039. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5040. type := type0;
  5041. IF IsIntegerValue(parameter0,i0) THEN
  5042. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5043. type := Global.GetIntegerType(system,ABS(i0));
  5044. ELSIF IsRealValue(parameter0,r) THEN
  5045. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5046. END;
  5047. ELSE
  5048. type := SyntaxTree.invalidType;
  5049. END;
  5050. (* ---- ASH, ASR ----- *)
  5051. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5052. type := type0;
  5053. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5054. (*
  5055. ConvertOperands(parameter0,parameter1); (* same type *)
  5056. *)
  5057. type := parameter0.type;
  5058. IF IsIntegerValue(parameter0,i0) THEN
  5059. IF IsIntegerValue(parameter1,i1) THEN
  5060. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5061. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5062. result := ResolveExpression(result);
  5063. type := Global.GetIntegerType(system,i0);
  5064. END;
  5065. END;
  5066. IF type.resolved.sizeInBits < 32 THEN
  5067. type := system.longintType;
  5068. END;
  5069. (*!compatibility with release, remove when resolved
  5070. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5071. *)
  5072. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5073. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5074. actualParameters.SetExpression(0,parameter0);
  5075. actualParameters.SetExpression(1,parameter1);
  5076. END;
  5077. (* ---- CAP ----- *)
  5078. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5079. type := system.characterType;
  5080. IF CheckCharacterType (parameter0) THEN
  5081. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5082. actualParameters.SetExpression(0,parameter0);
  5083. IF IsCharacterValue(parameter0,c) THEN
  5084. IF (c <= "z") & (c >= "a") THEN
  5085. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5086. ELSE
  5087. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5088. END;
  5089. END;
  5090. END;
  5091. (* ---- CHR ----- *)
  5092. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5093. IF id = Global.Chr THEN
  5094. type := system.characterType
  5095. ELSE
  5096. type := system.characterType32
  5097. END;
  5098. IF CheckIntegerType(parameter0) THEN
  5099. IF IsIntegerValue(parameter0,i0) THEN
  5100. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5101. result := ResolveExpression(result);
  5102. ELSE
  5103. (*
  5104. result := NewConversion(parameter0.position,parameter0,type);
  5105. *)
  5106. END;
  5107. END
  5108. (* ---- ENTIER ----- *)
  5109. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5110. type := system.longintType;
  5111. IF CheckRealType(parameter0) THEN
  5112. IF IsRealValue(parameter0,r) THEN
  5113. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5114. type := Global.GetIntegerType(system,ENTIER(r));
  5115. END
  5116. END;
  5117. (* ---- ENTIERH ----- *)
  5118. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5119. type := system.hugeintType;
  5120. IF CheckRealType(parameter0) THEN
  5121. IF IsRealValue(parameter0,r) THEN
  5122. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5123. END
  5124. END;
  5125. (* ---- LEN ----- *)
  5126. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5127. type := system.lenType;
  5128. base := type0;
  5129. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5130. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5131. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5132. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5133. Error(position, "forbidden len on unsafe pointer");
  5134. END;
  5135. type0 := base;
  5136. ELSE
  5137. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5138. type0 := parameter0.type.resolved;
  5139. actualParameters.SetExpression(0,parameter0);
  5140. base := type0;
  5141. END;
  5142. END;
  5143. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5144. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5145. IF i1 < 0 THEN
  5146. Error(position, "invalid dimension");
  5147. base := SyntaxTree.invalidType;
  5148. ELSE
  5149. base := ArrayBase(base,i1);
  5150. IF (base # NIL) & Indexable(base) THEN
  5151. ELSE
  5152. Error(position, "len on no array");
  5153. IF VerboseErrorMessage THEN
  5154. Printout.Info("base",base);
  5155. END;
  5156. base := SyntaxTree.invalidType;
  5157. END;
  5158. END;
  5159. IF numberActualParameters=2 THEN
  5160. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5161. actualParameters.SetExpression(1,parameter1);
  5162. ELSIF base IS SyntaxTree.MathArrayType THEN
  5163. Error(position, "missing dimension specification");
  5164. END;
  5165. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5166. IF base IS SyntaxTree.ArrayType THEN
  5167. arrayType := base(SyntaxTree.ArrayType);
  5168. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5169. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5170. result := Global.NewIntegerValue(system,position,value);
  5171. type := result.type;(* arrayType.length.type;*)
  5172. ASSERT(type # NIL);
  5173. END;
  5174. ELSIF base IS SyntaxTree.MathArrayType THEN
  5175. mathArrayType := base(SyntaxTree.MathArrayType);
  5176. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5177. result := Global.NewIntegerValue(system,position,value);
  5178. type := result.type;
  5179. (*
  5180. type := mathArrayType.length.type;
  5181. *)
  5182. ASSERT(type # NIL);
  5183. END;
  5184. END;
  5185. END;
  5186. ELSE
  5187. type := system.lenType;
  5188. END;
  5189. (* ---- FIRST ---- *)
  5190. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5191. type := system.lenType;
  5192. IF CheckRangeType(parameter0) THEN END;
  5193. result.SetAssignable(parameter0.assignable)
  5194. (* ---- LAST ---- *)
  5195. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5196. type := system.lenType;
  5197. IF CheckRangeType(parameter0) THEN END;
  5198. result.SetAssignable(parameter0.assignable)
  5199. (* ---- STEP ---- *)
  5200. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5201. type := system.lenType;
  5202. IF CheckRangeType(parameter0) THEN END;
  5203. result.SetAssignable(parameter0.assignable)
  5204. (* ---- RE ---- *)
  5205. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5206. IF CheckNumberType(parameter0) THEN
  5207. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5208. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5209. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5210. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5211. type := parameter0.type
  5212. ELSE
  5213. type := system.realType
  5214. END
  5215. END;
  5216. result.SetAssignable(parameter0.assignable)
  5217. (* ---- IM ---- *)
  5218. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5219. IF CheckNumberType(parameter0) THEN
  5220. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5221. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5222. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5223. ELSE
  5224. type := system.realType;
  5225. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5226. END
  5227. END;
  5228. result.SetAssignable(parameter0.assignable)
  5229. (* ---- MAX ----- *)
  5230. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5231. IF numberActualParameters = 1 THEN
  5232. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5233. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5234. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5235. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5236. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5237. *)
  5238. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5239. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5240. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5241. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5242. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5243. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5244. END;
  5245. ELSE
  5246. Error(parameter0.position,"is not a type symbol");
  5247. END
  5248. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5249. ConvertOperands(parameter0,parameter1);
  5250. actualParameters.SetExpression(0,parameter0);
  5251. actualParameters.SetExpression(1,parameter1);
  5252. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5253. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5254. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5255. END;
  5256. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5257. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5258. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5259. END;
  5260. END;
  5261. type := parameter0.type;
  5262. ELSE type := SyntaxTree.invalidType;
  5263. END;
  5264. (* ---- MIN ----- *)
  5265. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5266. IF numberActualParameters = 1 THEN
  5267. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5268. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5269. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5270. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5271. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5272. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5273. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5274. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5275. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5276. END;
  5277. ELSE
  5278. Error(parameter0.position,"is not a type symbol");
  5279. END
  5280. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5281. ConvertOperands(parameter0,parameter1);
  5282. actualParameters.SetExpression(0,parameter0);
  5283. actualParameters.SetExpression(1,parameter1);
  5284. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5285. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5286. ELSE result.SetResolved(parameter1.resolved)
  5287. END;
  5288. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5289. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5290. ELSE result.SetResolved(parameter1.resolved)
  5291. END;
  5292. END;
  5293. type := parameter0.type;
  5294. ELSE type := SyntaxTree.invalidType;
  5295. END;
  5296. (* ---- ODD ----- *)
  5297. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5298. type := system.booleanType;
  5299. IF CheckIntegerType(parameter0) THEN
  5300. IF IsIntegerValue(parameter0,i0) THEN
  5301. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5302. type := system.booleanType;
  5303. END;
  5304. END;
  5305. (* ---- ORD ----- *)
  5306. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5307. IF id = Global.Ord THEN
  5308. type := system.integerType;
  5309. ELSE
  5310. type := system.longintType;
  5311. END;
  5312. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5313. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5314. actualParameters.SetExpression(0,parameter0);
  5315. (* IF CheckCharacterType(parameter0) THEN*)
  5316. IF IsCharacterValue(parameter0,c)THEN
  5317. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5318. type := Global.GetSignedIntegerType(system,ORD(c));
  5319. END;
  5320. ELSE Error(parameter0.position, "incompatible parameter");
  5321. END;
  5322. (* ---- SHORT ----- *)
  5323. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5324. type := type0;
  5325. IF IsSignedIntegerType(type) THEN
  5326. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5327. ELSIF type = system.integerType THEN type := system.shortintType
  5328. ELSIF type = system.longintType THEN type := system.integerType
  5329. ELSIF type = system.hugeintType THEN type:= system.longintType
  5330. ELSE
  5331. CASE type.sizeInBits OF
  5332. 16: type := Global.Integer8
  5333. |32: type := Global.Integer16
  5334. |64: type := Global.Integer32
  5335. END;
  5336. END;
  5337. ELSIF type IS SyntaxTree.FloatType THEN
  5338. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5339. ELSIF type = system.longrealType THEN type := system.realType
  5340. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5341. END;
  5342. ELSIF type IS SyntaxTree.ComplexType THEN
  5343. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5344. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5345. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5346. END;
  5347. ELSE
  5348. Error(parameter0.position,"short not applicable")
  5349. END;
  5350. IF (parameter0.resolved # NIL) THEN
  5351. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5352. IF parameter0 IS SyntaxTree.Value THEN
  5353. result.SetResolved(parameter0(SyntaxTree.Value));
  5354. END;
  5355. END;
  5356. (* ---- LONG ----- *)
  5357. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5358. type := type0;
  5359. IF IsSignedIntegerType(type) THEN
  5360. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5361. ELSIF type = system.longintType THEN type := system.hugeintType
  5362. ELSIF type = system.integerType THEN type := system.longintType
  5363. ELSIF type = system.shortintType THEN type := system.integerType
  5364. ELSE
  5365. CASE type.sizeInBits OF
  5366. 8: type := Global.Integer16
  5367. |16: type := Global.Integer32
  5368. |32: type := Global.Integer64
  5369. END;
  5370. END;
  5371. ELSIF type IS SyntaxTree.FloatType THEN
  5372. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5373. ELSIF type= system.realType THEN type := system.longrealType
  5374. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5375. END;
  5376. ELSIF type IS SyntaxTree.ComplexType THEN
  5377. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5378. ELSIF type = system.complexType THEN type := system.longcomplexType
  5379. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5380. END;
  5381. ELSE
  5382. Error(parameter0.position,"long not applicable")
  5383. END;
  5384. IF (parameter0.resolved # NIL) THEN
  5385. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5386. IF parameter0 IS SyntaxTree.Value THEN
  5387. result.SetResolved(parameter0(SyntaxTree.Value));
  5388. END;
  5389. END;
  5390. (* ---- SIZE OF ----- *)
  5391. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5392. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5393. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5394. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5395. type := system.integerType;
  5396. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5397. ELSE
  5398. (* for variables, system sizeof could represent the physically occupied size
  5399. determined via the type descriptor, implement that ? *)
  5400. Error(parameter0.position,"is not a type symbol");
  5401. END
  5402. (* ---- SYSTEM.TRACE -----*)
  5403. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5404. FOR i := 0 TO numberActualParameters-1 DO
  5405. parameter0 := actualParameters.GetExpression(i);
  5406. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5407. Error(parameter0.position,"incompatible parameter");
  5408. END;
  5409. END;
  5410. (* remaining issues can only be tested in backend *)
  5411. (* ---- ADDRESSOF----- *)
  5412. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5413. IF HasAddress(parameter0) THEN
  5414. type := system.addressType;
  5415. ELSE
  5416. type := SyntaxTree.invalidType;
  5417. Error(parameter0.position,"has no address");
  5418. END;
  5419. (* ---- BIT ----- *)
  5420. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5421. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5422. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5423. actualParameters.SetExpression(0,parameter0);
  5424. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5425. actualParameters.SetExpression(1,parameter1);
  5426. END;
  5427. type := system.booleanType;
  5428. (* ----- MSK ---- *)
  5429. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5430. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5431. ConvertOperands(parameter0,parameter1);
  5432. actualParameters.SetExpression(0,parameter0);
  5433. actualParameters.SetExpression(1,parameter1);
  5434. END;
  5435. type := parameter0.type;
  5436. (* ---- SYSTEM.GET64 ----- *)
  5437. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5438. IF CheckAddressType(parameter0) THEN
  5439. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5440. actualParameters.SetExpression(0,parameter0);
  5441. END;
  5442. type := system.hugeintType;
  5443. (* ---- SYSTEM.GET32 ----- *)
  5444. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5445. IF CheckAddressType(parameter0) THEN
  5446. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5447. actualParameters.SetExpression(0,parameter0);
  5448. END;
  5449. type := system.longintType;
  5450. (* ---- SYSTEM.GET16 ----- *)
  5451. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5452. IF CheckAddressType(parameter0) THEN
  5453. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5454. actualParameters.SetExpression(0,parameter0);
  5455. END;
  5456. type := system.integerType;
  5457. (* ---- SYSTEM.GET8 ----- *)
  5458. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5459. IF CheckAddressType(parameter0) THEN
  5460. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5461. actualParameters.SetExpression(0,parameter0);
  5462. END;
  5463. type := system.shortintType;
  5464. (* ---- SYSTEM.GetStackPointer ----- *)
  5465. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5466. type := system.addressType;
  5467. (* ---- SYSTEM.GetFramePointer ----- *)
  5468. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5469. type := system.addressType;
  5470. (* ---- SYSTEM.GetActivity ----- *)
  5471. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5472. type := system.objectType;
  5473. (* ---- SYSTEM.SetStackPointer ----- *)
  5474. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5475. IF CheckAddressType(parameter0) THEN
  5476. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. END;
  5479. (* ---- SYSTEM.SetFramePointer ----- *)
  5480. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5481. IF CheckAddressType(parameter0) THEN
  5482. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5483. actualParameters.SetExpression(0,parameter0);
  5484. END;
  5485. (* ---- SYSTEM.SetActivity ----- *)
  5486. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5487. IF CheckObjectType(parameter0) THEN
  5488. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5489. actualParameters.SetExpression(0,parameter0);
  5490. END;
  5491. (* ---- LSH, LSL, ROT, ROR ----- *)
  5492. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5493. type := type0;
  5494. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5495. actualParameters.SetExpression(1, parameter1);
  5496. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5497. IF id = Global.Lsh THEN
  5498. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5499. ELSIF id = Global.Rot THEN
  5500. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5501. ELSIF id = Global.Ror THEN
  5502. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5503. END;
  5504. END;
  5505. (* ---- SYSTEM.VAL ----- *)
  5506. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5507. IF CheckTypeDeclarationType(parameter0) THEN
  5508. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5509. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5510. result := SyntaxTree.invalidExpression;
  5511. Error(parameter0.position,"is no basic type");
  5512. ELSE
  5513. IF (parameter1.resolved # NIL) THEN
  5514. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5515. IF parameter0 IS SyntaxTree.Value THEN
  5516. result.SetResolved(parameter0(SyntaxTree.Value));
  5517. END;
  5518. END;
  5519. result.SetAssignable(parameter1.assignable);
  5520. END;
  5521. END;
  5522. (* ---- SYSTEM.GET ----- *)
  5523. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5524. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5525. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5526. actualParameters.SetExpression(0,parameter0);
  5527. END;
  5528. (* ---- SYSTEM.PUT ----- *)
  5529. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5530. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5531. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5532. actualParameters.SetExpression(0,parameter0);
  5533. END;
  5534. (* ---- SYSTEM.PUT64 ----- *)
  5535. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5536. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5537. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5538. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5539. actualParameters.SetExpression(0,parameter0);
  5540. actualParameters.SetExpression(1,parameter1);
  5541. END;
  5542. (* ---- SYSTEM.PUT32 ----- *)
  5543. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5544. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5545. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5546. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5547. actualParameters.SetExpression(0,parameter0);
  5548. actualParameters.SetExpression(1,parameter1);
  5549. END;
  5550. (* ---- SYSTEM.PUT16 ----- *)
  5551. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5552. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5553. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5554. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5555. actualParameters.SetExpression(0,parameter0);
  5556. actualParameters.SetExpression(1,parameter1);
  5557. END;
  5558. (* ---- SYSTEM.PUT8 ----- *)
  5559. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5560. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5561. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5562. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5563. actualParameters.SetExpression(0,parameter0);
  5564. actualParameters.SetExpression(1,parameter1);
  5565. END;
  5566. (* ---- SYSTEM.MOVE ----- *)
  5567. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5568. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5569. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5570. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5571. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5572. actualParameters.SetExpression(0,parameter0);
  5573. actualParameters.SetExpression(1,parameter1);
  5574. actualParameters.SetExpression(2,parameter2);
  5575. END;
  5576. (* ---- SYSTEM.NEW ----- *)
  5577. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5578. IF ~IsPointerType(parameter0.type) THEN
  5579. Error(parameter0.position,"is not a pointer")
  5580. ELSIF CheckSizeType(parameter1) THEN
  5581. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5582. actualParameters.SetExpression(1,parameter1);
  5583. END;
  5584. (* ----SYSTEM.REF ---- *)
  5585. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5586. type := system.addressType
  5587. (* ---- INCR ----- *)
  5588. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5589. type := system.lenType;
  5590. base := type0;
  5591. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5592. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5593. IF i1 < 0 THEN
  5594. Error(position, "invalid dimension");
  5595. base := SyntaxTree.invalidType;
  5596. ELSE
  5597. base := ArrayBase(base,i1);
  5598. IF (base # NIL) & Indexable(base) THEN
  5599. ELSE
  5600. Error(position, "len on no array");
  5601. IF VerboseErrorMessage THEN
  5602. Printout.Info("base",base);
  5603. END;
  5604. base := SyntaxTree.invalidType;
  5605. END;
  5606. END;
  5607. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5608. actualParameters.SetExpression(1,parameter1);
  5609. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5610. mathArrayType := base(SyntaxTree.MathArrayType);
  5611. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5612. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5613. type := system.lenType;
  5614. END;
  5615. END;
  5616. ELSE
  5617. type := system.lenType;
  5618. END;
  5619. (* ---- SUM ----- *)
  5620. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5621. Error(position, "sum operator not applicable");
  5622. (* ---- ALL ----- *)
  5623. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5624. Error(position, "all operator not applicable");
  5625. (* ---- DIM ----- *)
  5626. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5627. type := system.lenType;
  5628. IF type0 IS SyntaxTree.MathArrayType THEN
  5629. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5630. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5631. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5632. END;
  5633. ELSE
  5634. Error(position, "dimension on non math array type");
  5635. END;
  5636. (* ---- CAS ----- *)
  5637. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5638. IF type0.IsComposite () THEN
  5639. Error(position, "first parameter of composite type");
  5640. result := SyntaxTree.invalidExpression;
  5641. ELSIF ~IsVariable (parameter0) THEN
  5642. Error(position, "first parameter not assignable");
  5643. result := SyntaxTree.invalidExpression;
  5644. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5645. Error(position, "second parameter incompatible");
  5646. result := SyntaxTree.invalidExpression;
  5647. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5648. Error(position, "third parameter incompatible");
  5649. result := SyntaxTree.invalidExpression;
  5650. ELSE
  5651. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5652. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5653. type := type0;
  5654. END;
  5655. (* ---- RESHAPE ----- *)
  5656. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5657. IF type0 IS SyntaxTree.MathArrayType THEN
  5658. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5659. base := ArrayBase(type0,MAX(LONGINT));
  5660. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5661. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5662. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5663. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5664. IF ~CompatibleTo(system,type0,parameterType) THEN
  5665. Error(parameter0.position,"incompatible parameter in reshape");
  5666. result := SyntaxTree.invalidExpression;
  5667. ELSE
  5668. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5669. END;
  5670. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5671. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5672. IF ~CompatibleTo(system,type1,parameterType) THEN
  5673. Error(parameter1.position,"parameter incompatible to math array of longint");
  5674. result := SyntaxTree.invalidExpression;
  5675. ELSE
  5676. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5677. END;
  5678. ELSE
  5679. Error(position,"reshape on non math array type");
  5680. result := SyntaxTree.invalidExpression;
  5681. END;
  5682. (* ---- SYSTEM.TYPECODE ----- *)
  5683. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5684. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5685. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5686. type := type.resolved;
  5687. IF type IS SyntaxTree.PointerType THEN
  5688. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5689. END;
  5690. IF ~(type IS SyntaxTree.RecordType) THEN
  5691. Error(parameter0.position,"must be type with type descriptor");
  5692. END;
  5693. ELSE
  5694. Error(parameter0.position,"is not a type symbol");
  5695. END;
  5696. type := system.addressType;
  5697. (* -------- FLT --------- *)
  5698. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5699. type := system.realType;
  5700. IF IsRealValue(parameter0, r) THEN
  5701. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5702. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5703. value := ABS(i0);
  5704. IF value # 0 THEN
  5705. i1 := 23;
  5706. IF value >= 2*800000H THEN
  5707. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5708. ELSIF value < 800000H THEN
  5709. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5710. END;
  5711. value := (i1 + 127)*800000H - 800000H + value;
  5712. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5713. END;
  5714. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5715. END;
  5716. (* ------- CONNECT -------*)
  5717. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5718. (*IF ~(currentIsCellNet) THEN
  5719. Error(position, "connection outside activeCells body block");
  5720. END;*)
  5721. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5722. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5723. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5724. END;
  5725. IF numberActualParameters = 3 THEN
  5726. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5727. Error(position, "incompatible channel size parameter");
  5728. END;
  5729. *)
  5730. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5731. actualParameters.SetExpression(2,parameter2);
  5732. END;
  5733. activeCellsStatement := TRUE;
  5734. (* ---------- DELEGATE --------*)
  5735. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5736. (*
  5737. IF ~(currentIsCellNet) THEN
  5738. Error(position, "connection delegation outside activeCells body block");
  5739. END;
  5740. *)
  5741. IF ~CheckPortType(parameter1, inPort) THEN
  5742. Error(parameter0.position,"not a port")
  5743. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5744. Error(parameter1.position,"not a port")
  5745. ELSIF (outPort.direction # inPort.direction) THEN
  5746. Error(parameter0.position,"invalid port direction");
  5747. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5748. Error(position, "incompatible port sizes");
  5749. END;
  5750. activeCellsStatement := TRUE;
  5751. (* --------- RECEIVE ---------*)
  5752. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5753. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5754. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5755. IF inPort.direction # SyntaxTree.InPort THEN
  5756. Error(parameter0.position,"not an in-port")
  5757. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5758. Error(parameter1.position,"incompatible to port type");
  5759. END;
  5760. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5761. IF ~SameType(parameter2.type, system.integerType) THEN
  5762. Error(parameter2.position,"incompatible to integer type");
  5763. END;
  5764. END;
  5765. END;
  5766. (* --------- SEND ---------*)
  5767. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5768. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5769. IF CheckPortType(parameter0,outPort) THEN
  5770. IF outPort.direction # SyntaxTree.OutPort THEN
  5771. Error(parameter1.position,"not an out-port")
  5772. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5773. Error(parameter1.position,"incompatible to port type");
  5774. ELSE
  5775. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5776. actualParameters.SetExpression(1,parameter1);
  5777. END;
  5778. END;
  5779. (* ------- custom builtins ----- *)
  5780. ELSIF id = Global.systemSpecial THEN
  5781. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5782. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5783. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5784. type := procedureType.returnType;
  5785. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5786. (* go through all formal parameters *)
  5787. formalParameter := procedureType.firstParameter;
  5788. FOR i := 0 TO actualParameters.Length() - 1 DO
  5789. actualParameter := actualParameters.GetExpression(i);
  5790. IF actualParameter = SyntaxTree.invalidExpression THEN
  5791. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5792. Error(position, "incompatible parameter")
  5793. ELSE
  5794. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5795. END;
  5796. actualParameters.SetExpression(i, actualParameter);
  5797. formalParameter := formalParameter.nextParameter
  5798. END
  5799. END
  5800. ELSE
  5801. Error(position, "builtin not implemented");
  5802. result := SyntaxTree.invalidExpression;
  5803. END;
  5804. END;
  5805. IF result # SyntaxTree.invalidExpression THEN
  5806. type := ResolveType(type);
  5807. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5808. result.SetType(type);
  5809. END;
  5810. RETURN result
  5811. END NewBuiltinCallDesignator;
  5812. (** return type guard designator left(type)
  5813. - check if type can be extended (i.e. is no static record)
  5814. - check if type is a type extension of left.type
  5815. - returns new type guard designator
  5816. returns invalidDesignator = invalidExpression if error
  5817. **)
  5818. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5819. VAR result: SyntaxTree.Designator;
  5820. BEGIN
  5821. result := SyntaxTree.invalidDesignator;
  5822. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5823. Error(position, "no type extension of type");
  5824. IF VerboseErrorMessage THEN
  5825. Printout.Info("left",left);
  5826. Printout.Info("type",type);
  5827. END;
  5828. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5829. Error(position, "variable cannot be extended");
  5830. ELSIF IsUnsafePointer(left.type) THEN
  5831. Error(position, "forbidden type guard on unsafe pointer");
  5832. ELSE
  5833. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5834. result.SetType(type);
  5835. result.SetAssignable(left.assignable);
  5836. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5837. END;
  5838. RETURN result
  5839. END NewTypeGuardDesignator;
  5840. (** check and resolve parameter designator left(expression list)
  5841. - check expression list
  5842. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5843. - elsif left is a procedure type then
  5844. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5845. - else return is a procedure call then return ProcedureCallDesignator
  5846. returns invalidDesignator = invalidExpression if error
  5847. **)
  5848. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5849. VAR
  5850. parameters: SyntaxTree.ExpressionList;
  5851. left: SyntaxTree.Designator;
  5852. result,expression: SyntaxTree.Expression;
  5853. typeDeclaration: SyntaxTree.TypeDeclaration;
  5854. type, expressionType: SyntaxTree.Type;
  5855. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5856. BEGIN
  5857. type := type.resolved;
  5858. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5859. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5860. END;
  5861. RETURN type
  5862. END BaseType;
  5863. BEGIN
  5864. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5865. result := SyntaxTree.invalidDesignator;
  5866. left := ResolveDesignator(designator.left);
  5867. IF left # SyntaxTree.invalidDesignator THEN
  5868. parameters := designator.parameters;
  5869. IF ExpressionList(parameters) THEN
  5870. IF (left.type = NIL) THEN
  5871. Error(left.position,"object is not a procedure or cannot be extended");
  5872. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5873. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5874. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5875. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5876. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5877. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5878. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5879. ELSE
  5880. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5881. END
  5882. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5883. expression := parameters.GetExpression(0);
  5884. type := typeDeclaration.declaredType.resolved;
  5885. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5886. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5887. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5888. OR (expressionType IS SyntaxTree.EnumerationType)
  5889. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5890. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5891. Error(left.position,"invalid unsigned type in explicit conversion");
  5892. ELSE
  5893. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5894. END;
  5895. ELSE
  5896. Error(left.position,"invalid type in explicit conversion");
  5897. END;
  5898. ELSE
  5899. Error(left.position,"called object is not a procedure or cannot be extended");
  5900. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5901. result := SyntaxTree.invalidDesignator;
  5902. END;
  5903. ELSE
  5904. result := SyntaxTree.invalidDesignator
  5905. END;
  5906. END;
  5907. resolvedExpression := result;
  5908. END VisitParameterDesignator;
  5909. (** check dereference designator left^
  5910. - check if left is pointer type or left is object type
  5911. - return new dereference designator with type = left.baseType.type (if appropriate)
  5912. with error handling
  5913. returns invalidDesignator = invalidExpression if error
  5914. **)
  5915. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5916. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5917. BEGIN
  5918. result := SyntaxTree.invalidDesignator;
  5919. type := left.type;
  5920. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5921. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5922. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5923. result.SetAssignable(TRUE);
  5924. result.SetType(type);
  5925. result.SetHidden(left.isHidden);
  5926. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5927. type := type.resolved;
  5928. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5929. result.SetAssignable(TRUE);
  5930. result.SetType(type);
  5931. result.SetHidden(left.isHidden);
  5932. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5933. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5934. result.SetAssignable(TRUE);
  5935. result.SetType(type);
  5936. result.SetHidden(left.isHidden);
  5937. ELSE
  5938. Error(position, "dereference on no pointer");
  5939. IF VerboseErrorMessage THEN
  5940. Printout.Info("pointer", type);
  5941. Printout.Info("scope", currentScope);
  5942. END;
  5943. END;
  5944. RETURN result
  5945. END NewDereferenceDesignator;
  5946. (** check supercall designator left^
  5947. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5948. - return new supercall designator with type = left.type
  5949. with error handling
  5950. **)
  5951. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5952. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5953. objectScope: SyntaxTree.Scope;
  5954. BEGIN
  5955. result := SyntaxTree.invalidDesignator;
  5956. IF left = SyntaxTree.invalidDesignator THEN
  5957. (* error already handled *)
  5958. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5959. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5960. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5961. IF symbol IS SyntaxTree.Procedure THEN
  5962. procedure := symbol(SyntaxTree.Procedure);
  5963. objectScope := currentScope;
  5964. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5965. objectScope := objectScope.outerScope;
  5966. END;
  5967. IF (left.left = NIL) OR ~
  5968. (
  5969. (left.left IS SyntaxTree.SelfDesignator) OR
  5970. (left.left IS SyntaxTree.DereferenceDesignator)
  5971. & (left.left(SyntaxTree.Designator).left # NIL)
  5972. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5973. Error(position, "procedure not in immediate object scope");
  5974. IF VerboseErrorMessage THEN
  5975. Printout.Info("left.left",left.left);
  5976. END;
  5977. ELSIF procedure.super # NIL THEN
  5978. result := SyntaxTree.NewSupercallDesignator(position,left);
  5979. result.SetType(left.type.resolved)
  5980. ELSE
  5981. Error(position, "no supermethod for this procedure");
  5982. END;
  5983. ELSE
  5984. Error(position, "symbol is not a procedure");
  5985. END;
  5986. ELSE
  5987. Error(position, "is no symbol designator");
  5988. END;
  5989. RETURN result
  5990. END NewSupercallDesignator;
  5991. (** check and semantically resolve arrow designator left^
  5992. - if left is procedure type -> result := SupercallDesignator
  5993. - else result := DereferenceDesignator
  5994. returns result via global variable resolvedExpression
  5995. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5996. **)
  5997. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  5998. VAR left: SyntaxTree.Designator;
  5999. BEGIN
  6000. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6001. left := ResolveDesignator(arrowDesignator.left);
  6002. IF left # NIL THEN
  6003. IF (left.type = NIL) THEN
  6004. Error(arrowDesignator.position,"Invalid arrow designator");
  6005. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6006. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6007. ELSE
  6008. IF IsPointerToObject(left.type) THEN
  6009. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6010. END;
  6011. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6012. END
  6013. END
  6014. END VisitArrowDesignator;
  6015. (** check and return expression
  6016. - if expression has no type then resolve expression
  6017. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6018. - return result
  6019. **)
  6020. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6021. VAR result,prev: SyntaxTree.Expression;
  6022. BEGIN
  6023. IF expression = NIL THEN result := NIL
  6024. ELSIF (expression.type = NIL) THEN
  6025. prev := resolvedExpression;
  6026. resolvedExpression := SyntaxTree.invalidExpression;
  6027. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6028. expression.SetType(SyntaxTree.invalidType);
  6029. END;
  6030. expression.Accept(SELF);
  6031. result := resolvedExpression;
  6032. IF currentIsRealtime THEN
  6033. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6034. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6035. END;
  6036. END;
  6037. (* designator modifiers for backends if they support it ...*)
  6038. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6039. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6040. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6041. END;
  6042. resolvedExpression := prev
  6043. ELSE
  6044. result := expression
  6045. END;
  6046. RETURN result
  6047. END ResolveExpression;
  6048. (**
  6049. check expression to be constant expression
  6050. - resolve expression
  6051. - if valid then check that of value type
  6052. report error and return invalidExpression if anything fails
  6053. **)
  6054. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6055. VAR position: Position;
  6056. BEGIN
  6057. position := expression.position;
  6058. expression := ResolveExpression(expression);
  6059. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6060. ELSIF (expression.resolved = NIL) THEN
  6061. Error(position, "expression is not constant");
  6062. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6063. expression := SyntaxTree.invalidExpression;
  6064. END;
  6065. RETURN expression
  6066. END ConstantExpression;
  6067. (** check expression to be constant integer
  6068. - resolve expresssion
  6069. - if valid then check that of integer value type
  6070. report error and return invalidExpression if anything fails
  6071. **)
  6072. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6073. VAR position: Position;
  6074. BEGIN
  6075. position := expression.position;
  6076. expression := ResolveExpression(expression);
  6077. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6078. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6079. expression := SyntaxTree.invalidExpression;
  6080. Error(position, "expression is not a constant integer");
  6081. END;
  6082. RETURN expression
  6083. END ConstantInteger;
  6084. (** check expression as positive (>=0) constant integer
  6085. - resolve expression
  6086. - if valid then check that integer value
  6087. - if integer value then check that value >= 0
  6088. report error and return invalidExpression if anything fails
  6089. **)
  6090. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6091. VAR position: Position;
  6092. BEGIN
  6093. position := expression.position;
  6094. expression := ConstantExpression(expression);
  6095. IF expression = SyntaxTree.invalidExpression THEN
  6096. (* error already reported *)
  6097. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6098. Error(position, "expression is not integer valued");
  6099. expression := SyntaxTree.invalidExpression
  6100. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6101. Error(position, "integer is not greater or equal zero");
  6102. END;
  6103. RETURN expression
  6104. END ConstantIntegerGeq0;
  6105. (** check expression as condition
  6106. - resolve expression
  6107. - if valid expression then check that result type is boolean
  6108. report error and return invalidExpression if anything fails
  6109. **)
  6110. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6111. VAR position: Position;
  6112. BEGIN
  6113. position := expression.position;
  6114. expression := ResolveExpression(expression);
  6115. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6116. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6117. expression := SyntaxTree.invalidExpression;
  6118. Error(position, "expression is not boolean");
  6119. END;
  6120. RETURN expression
  6121. END ResolveCondition;
  6122. (*** symbols ***)
  6123. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6124. BEGIN
  6125. x.Accept(SELF);
  6126. END ResolveSymbol;
  6127. (** check a symbol
  6128. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6129. **)
  6130. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6131. VAR scope: SyntaxTree.Scope;
  6132. BEGIN
  6133. (* visibility *)
  6134. scope := symbol.scope;
  6135. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6136. scope := scope.outerScope;
  6137. END;
  6138. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6139. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6140. Error(symbol.position,"cannot be exported");
  6141. IF VerboseErrorMessage THEN
  6142. Printout.Info("symbol",symbol);
  6143. END;
  6144. END;
  6145. END;
  6146. END CheckSymbolVisibility;
  6147. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6148. If node is currently being resolved then emit a cyclic definition error.
  6149. Return TRUE only if node is fully resolved.
  6150. **)
  6151. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6152. VAR result: BOOLEAN;
  6153. BEGIN
  6154. IF SyntaxTree.Resolved IN x.state THEN
  6155. result := FALSE
  6156. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6157. Error(x.position,"cyclic definition");
  6158. result := FALSE;
  6159. ELSE
  6160. result := TRUE;
  6161. x.SetState(SyntaxTree.BeingResolved)
  6162. END;
  6163. RETURN result
  6164. END SymbolNeedsResolution;
  6165. (** check and resolve a type declaration symbol = Type
  6166. - set type to declaration type
  6167. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6168. This is so because the type declaration itself does not have a type but it only stands for a type.
  6169. In the implementation of the compiler this made a lot much easier.
  6170. - resolve and set declared type
  6171. - check symbol
  6172. **)
  6173. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6174. VAR prevScope: SyntaxTree.Scope;
  6175. BEGIN
  6176. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6177. IF SymbolNeedsResolution(typeDeclaration) THEN
  6178. typeDeclaration.SetState(SyntaxTree.Resolved);
  6179. prevScope := currentScope;
  6180. currentScope := typeDeclaration.scope;
  6181. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6182. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6183. CheckSymbolVisibility(typeDeclaration);
  6184. typeDeclaration.SetState(SyntaxTree.Resolved);
  6185. currentScope := prevScope;
  6186. END;
  6187. END VisitTypeDeclaration;
  6188. (** check and resolve a constant declaration symbol = (constant) expression
  6189. - check expression
  6190. - set type and value
  6191. - check symbol
  6192. **)
  6193. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6194. VAR
  6195. expression: SyntaxTree.Expression;
  6196. type: SyntaxTree.Type;
  6197. name: Basic.SegmentedName;
  6198. replacement: Replacement;
  6199. BEGIN
  6200. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6201. IF SymbolNeedsResolution(constant) THEN
  6202. expression := constant.value;
  6203. IF replacements # NIL THEN
  6204. Global.GetSymbolSegmentedName(constant, name);
  6205. replacement := replacements;
  6206. WHILE (replacement # NIL) & (replacement.name # name) DO
  6207. replacement := replacement.next;
  6208. END;
  6209. IF replacement # NIL THEN
  6210. InfoSS(constant.position, "replacing constant", constant.name);
  6211. (*
  6212. NEW(stringReader, Strings.Length(replacement.string^));
  6213. stringReader.Set(replacement.string^);
  6214. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6215. NEW(parser, scanner, diagnostics);
  6216. expression := parser.Expression();
  6217. *)
  6218. expression := replacement.expression;
  6219. replacement.used := TRUE;
  6220. END;
  6221. END;
  6222. constant.SetType(SyntaxTree.invalidType);
  6223. expression := ConstantExpression(expression);
  6224. ASSERT(expression.type # NIL);
  6225. type := expression.type.resolved;
  6226. constant.SetType(type);
  6227. constant.SetValue(expression);
  6228. CheckSymbolVisibility(constant);
  6229. constant.SetState(SyntaxTree.Resolved);
  6230. END;
  6231. END VisitConstant;
  6232. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6233. VAR procedureAlignment: LONGINT;
  6234. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6235. (* least common multiple *)
  6236. VAR a,b: LONGINT;
  6237. BEGIN
  6238. a := a0; b := b0;
  6239. WHILE (a # b) DO
  6240. IF a < b THEN a := a+a0
  6241. ELSE b := b + b0
  6242. END;
  6243. END;
  6244. RETURN a
  6245. END LCM;
  6246. BEGIN
  6247. IF alignment > 1 THEN
  6248. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6249. IF (procedureAlignment > 1) THEN
  6250. alignment := LCM(alignment, procedureAlignment);
  6251. END;
  6252. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6253. END;
  6254. END AdaptStackAlignment;
  6255. (** check and resolve a variable / field
  6256. - check and set type
  6257. - negative check on open array type
  6258. - check symbol
  6259. **)
  6260. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6261. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6262. BEGIN
  6263. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6264. IF SymbolNeedsResolution(variable) THEN
  6265. modifiers := variable.modifiers;
  6266. (*
  6267. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6268. variable.AddFlags(flags);
  6269. *)
  6270. variable.SetType(ResolveType(variable.type));
  6271. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6272. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6273. Error(variable.position,"forbidden open array variable");
  6274. END;
  6275. END;
  6276. CheckSymbolVisibility(variable);
  6277. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6278. variable.SetUntraced(TRUE);
  6279. IF ~ContainsPointer(variable.type) THEN
  6280. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6281. Error(position, "untraced flag on non-pointer variable");
  6282. END;
  6283. END;
  6284. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6285. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6286. IF ~PowerOf2(value) THEN
  6287. Error(position, "forbidden alignment - must be power of two");
  6288. ELSE
  6289. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6290. END;
  6291. END;
  6292. variable.SetAlignment(FALSE,value);
  6293. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6294. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6295. Error(position, "fixed position not possible in procedure");
  6296. END;
  6297. variable.SetAlignment(TRUE, value);
  6298. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6299. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6300. Error(position,"fictive offset not possible in procedure");
  6301. END;
  6302. variable.SetFictive(value);
  6303. variable.SetOffset(value*system.dataUnit);
  6304. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6305. END;
  6306. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6307. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6308. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6309. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6310. END;
  6311. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6312. IF variable.initializer # NIL THEN
  6313. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6314. END;
  6315. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6316. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6317. pointerType.SetPointerBase(variable.type);
  6318. pointerType.SetHidden(TRUE);
  6319. variable.SetType(ResolveType(pointerType));
  6320. END;
  6321. variable.SetState(SyntaxTree.Resolved);
  6322. END;
  6323. END VisitVariable;
  6324. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6325. BEGIN
  6326. VisitVariable(property)
  6327. END VisitProperty;
  6328. (** check and resolve a (procedure) parameter
  6329. - check and set type
  6330. - check symbol
  6331. - check parameter kind and set read-only flags if appropriate
  6332. **)
  6333. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6334. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6335. BEGIN
  6336. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6337. IF SymbolNeedsResolution(parameter) THEN
  6338. modifiers := parameter.modifiers;
  6339. parameter.SetType(ResolveType(parameter.type));
  6340. ASSERT(parameter.type.resolved # NIL);
  6341. CheckSymbolVisibility(parameter);
  6342. IF parameter.defaultValue # NIL THEN
  6343. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6344. Error(parameter.position,"forbidden default value on non-value parameter");
  6345. ELSE
  6346. expression := ConstantExpression(parameter.defaultValue);
  6347. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6348. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6349. parameter.SetDefaultValue(expression);
  6350. END;
  6351. END;
  6352. END;
  6353. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6354. Error(parameter.position, "forbidden value parameter of math array type ");
  6355. END;
  6356. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6357. parameter.SetUntraced(TRUE);
  6358. IF ~ContainsPointer(parameter.type) THEN
  6359. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6360. Error(position, "untraced flag on non-pointer variable");
  6361. END;
  6362. END;
  6363. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6364. parameter.SetMoveable(TRUE);
  6365. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6366. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6367. Error(position, "illegal movable flag on non-address variable");
  6368. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6369. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6370. Error(position, "unnecessary movable flag on variable variable");
  6371. END;
  6372. END;
  6373. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6374. parameter.SetState(SyntaxTree.Resolved);
  6375. END;
  6376. END VisitParameter;
  6377. (** check and resolve a procedure (with declaration and implementation scope)
  6378. - check the procedure type
  6379. - check if method (i.e. in record scope), if so then
  6380. - check if (unique) constructor
  6381. - check if (unique) finalizer
  6382. - check if super method available, if so then check signature
  6383. - of not in record scope then negative check on constructor flag
  6384. - of not in record scope then negative check on finalizer flag
  6385. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6386. - check procedure symbol
  6387. **)
  6388. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6389. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6390. procedureType: SyntaxTree.ProcedureType;
  6391. type: SyntaxTree.Type;
  6392. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6393. qualifiedType: SyntaxTree.QualifiedType;
  6394. value: LONGINT;
  6395. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6396. position: Position;
  6397. fp: SyntaxTree.FingerPrint;
  6398. BEGIN
  6399. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6400. IF IsOberonInline(procedure) THEN
  6401. IF SyntaxTree.Public * procedure.access # {} THEN
  6402. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6403. END;
  6404. procedure.SetInline(FALSE);
  6405. procedure.SetOberonInline(TRUE);
  6406. END;
  6407. IF SymbolNeedsResolution(procedure) THEN
  6408. recentIsRealtime := currentIsRealtime;
  6409. recentIsBodyProcedure := currentIsBodyProcedure;
  6410. CheckSymbolVisibility(procedure);
  6411. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6412. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6413. modifiers := procedureType.modifiers;
  6414. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6415. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6416. IF useDarwinCCalls THEN (*fld*)
  6417. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6418. ELSE
  6419. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6420. END
  6421. END;
  6422. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6423. procedureType.SetInterrupt(TRUE);
  6424. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6425. END;
  6426. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6427. procedureType.SetNoReturn(TRUE);
  6428. END;
  6429. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6430. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6431. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6432. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6433. END;
  6434. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6435. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6436. END;
  6437. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6438. IF ~PowerOf2(value) THEN
  6439. Error(position, "forbidden stack alignment - must be power of two");
  6440. ELSE
  6441. procedureType.SetStackAlignment(value)
  6442. END;
  6443. END;
  6444. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6445. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6446. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6447. END;
  6448. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6449. SyntaxTree.InitFingerPrint(fp);
  6450. fp.shallow := value;
  6451. fp.public := value;
  6452. fp.private := value;
  6453. fp.shallowAvailable := TRUE;
  6454. procedure.SetFingerPrint(fp);
  6455. END;
  6456. CheckModifiers(modifiers, TRUE);
  6457. modifiers := procedureType.returnTypeModifiers;
  6458. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6459. CheckModifiers(modifiers, TRUE);
  6460. procedure.SetState(SyntaxTree.Resolved);
  6461. FixProcedureType(procedureType);
  6462. currentIsRealtime := procedureType.isRealtime;
  6463. currentIsBodyProcedure := procedure.isBodyProcedure;
  6464. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6465. THEN
  6466. Error(procedure.position,"problems during parameter offset computation");
  6467. END;
  6468. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6469. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6470. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6471. Error(procedure.position,"problem during parameter offset generation");
  6472. END;
  6473. END;
  6474. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6475. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6476. procedureType.SetDelegate(TRUE);
  6477. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6478. (* add auto-self *)
  6479. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6480. IF (record.pointerType.typeDeclaration = NIL) THEN
  6481. selfParameter.SetType(record.pointerType);
  6482. ELSE
  6483. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6484. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6485. qualifiedType.SetResolved(record.pointerType);
  6486. selfParameter.SetType(qualifiedType);
  6487. END;
  6488. selfParameter.SetAccess(SyntaxTree.Hidden);
  6489. END;
  6490. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6491. IF procedure.isConstructor THEN
  6492. (*! constructor is always visible, compatibility to paco
  6493. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6494. *)
  6495. procedure.MarkUsed;
  6496. IF procedureType.returnType # NIL THEN
  6497. Error(procedure.position,"constructor with forbidden return type");
  6498. END;
  6499. proc := procedure.scope.firstProcedure;
  6500. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6501. proc := proc.nextProcedure;
  6502. END;
  6503. IF proc # NIL THEN
  6504. Error(procedure.position,"duplicate constructor")
  6505. ELSE
  6506. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6507. END;
  6508. END;
  6509. IF procedure.isFinalizer THEN
  6510. procedure.MarkUsed;
  6511. IF procedureType.returnType # NIL THEN
  6512. Error(procedure.position,"finalizer with forbidden return type");
  6513. END;
  6514. IF procedureType.numberParameters # 0 THEN
  6515. Error(procedure.position,"finalizer with formal parameters");
  6516. END;
  6517. proc := procedure.scope.firstProcedure;
  6518. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6519. proc := proc.nextProcedure;
  6520. END;
  6521. IF proc # NIL THEN
  6522. Error(procedure.position,"duplicate finalizer")
  6523. ELSE
  6524. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6525. END;
  6526. END;
  6527. super := FindSuperProcedure(record.recordScope, procedure);
  6528. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6529. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6530. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6531. END;
  6532. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6533. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6534. END;
  6535. IF super.isFinal THEN
  6536. Error(procedure.position,"forbidden method extending final method");
  6537. END;
  6538. (*
  6539. IF super.access # procedure.access THEN
  6540. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6541. END;
  6542. *)
  6543. procedure.SetSuper(super);
  6544. super.SetOverwritten(TRUE);
  6545. procedure.SetAccess(procedure.access+super.access);
  6546. procedure.MarkUsed;
  6547. END;
  6548. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6549. THEN
  6550. Error(procedure.position,"problems during parameter offset computation");
  6551. END;
  6552. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6553. IF cellsAreObjects THEN
  6554. procedureType.SetDelegate(TRUE);
  6555. END;
  6556. IF procedure.isConstructor THEN
  6557. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6558. END;
  6559. ELSIF procedure.isConstructor THEN
  6560. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6561. END;
  6562. Declarations(procedure.procedureScope, FALSE, {0,1});
  6563. (* body resolution part done as late fix of the procedure type *)
  6564. procedure.SetState(SyntaxTree.Resolved);
  6565. currentIsRealtime := recentIsRealtime;
  6566. currentIsBodyProcedure := recentIsBodyProcedure;
  6567. END;
  6568. END VisitProcedure;
  6569. (**
  6570. a builtin procedure is a global item that may not be modified locally
  6571. instead the resolving of builtin procedure calls are done in the esignator
  6572. **)
  6573. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6574. VAR type: SyntaxTree.Type;
  6575. BEGIN
  6576. type := ResolveType(builtinProcedure.type);
  6577. END VisitBuiltin;
  6578. (* nopov *)
  6579. (** check and resolve operator
  6580. - operators are first checked as procedures
  6581. - then additional operator-specific checks are done
  6582. - note that only module-scope operators are checked here
  6583. (operators in a record scope are only allowed in the context of
  6584. array-structured object types and checked in 'ResolveArrayStructure')
  6585. - also note that inter-operator conformity is not checked here
  6586. **)
  6587. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6588. VAR
  6589. procedureType: SyntaxTree.ProcedureType;
  6590. leftType, rightType: SyntaxTree.Type;
  6591. identifierNumber: LONGINT; position: Position;
  6592. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6593. modifiers: SyntaxTree.Modifier;
  6594. (** whether a type is locally defined in the current module scope
  6595. for arrays, the base type must be locally defined **)
  6596. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6597. BEGIN
  6598. IF type = NIL THEN
  6599. RETURN FALSE
  6600. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6601. RETURN TRUE
  6602. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6603. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6604. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6605. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6606. ELSE
  6607. RETURN FALSE
  6608. END
  6609. END IsLocallyDefined;
  6610. BEGIN
  6611. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6612. procedureType := operator.type(SyntaxTree.ProcedureType);
  6613. modifiers := procedureType.modifiers;
  6614. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6615. CheckModifiers(modifiers, TRUE);
  6616. VisitProcedure(operator);
  6617. IF operator.scope IS SyntaxTree.RecordScope THEN
  6618. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6619. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6620. IF identifierNumber = -1 THEN
  6621. Error(operator.position, "operator with unknown identifier")
  6622. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6623. Error(operator.position, "identifier may not be used for operator")
  6624. ELSE
  6625. IF procedureType.numberParameters < 1 THEN
  6626. Error(operator.position, "operator without operand");
  6627. ELSIF procedureType.numberParameters > 2 THEN
  6628. Error(operator.position, "operator with more than two operands");
  6629. ELSE
  6630. (* determine operand types *)
  6631. leftType := procedureType.firstParameter.type;
  6632. IF procedureType.numberParameters > 1 THEN
  6633. rightType := procedureType.firstParameter.nextParameter.type
  6634. ELSE
  6635. rightType := NIL
  6636. END;
  6637. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6638. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6639. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6640. Error(operator.position, "none of the operands is declared in the same module")
  6641. END
  6642. END;
  6643. (* TODO: refine the checks, think about how restrictive the checks should be
  6644. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6645. They might be used for intersection, union, complement of custom object types *)
  6646. (* defaults *)
  6647. hasReturnType := TRUE;
  6648. mustBeUnary := FALSE;
  6649. mustBeBinary := FALSE;
  6650. mustReturnBoolean := FALSE;
  6651. mustReturnInteger := FALSE;
  6652. mustHaveEquitypedOperands := FALSE;
  6653. (* operator-specific exceptions *)
  6654. CASE identifierNumber OF
  6655. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6656. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6657. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6658. mustBeBinary := TRUE
  6659. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6660. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6661. | Scanner.Times: mustBeBinary := TRUE
  6662. | Scanner.TimesTimes: mustBeBinary := TRUE
  6663. | Scanner.DotTimes: mustBeBinary := TRUE
  6664. | Scanner.PlusTimes: mustBeBinary := TRUE
  6665. | Scanner.Slash: mustBeBinary := TRUE
  6666. | Scanner.Backslash: mustBeBinary := TRUE
  6667. | Scanner.DotSlash: mustBeBinary := TRUE
  6668. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6669. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6670. | Scanner.Not: mustBeUnary := TRUE
  6671. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6672. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6673. | Scanner.Transpose: mustBeUnary := TRUE;
  6674. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6675. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6676. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6677. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6678. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6679. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6680. | Global.Abs: mustBeUnary := TRUE;
  6681. | Global.Ash: (* TODO: arity? *)
  6682. | Global.Cap: (* TODO: arity? *)
  6683. | Global.Chr: mustBeUnary := TRUE;
  6684. | Global.Entier: (* TODO: arity? *)
  6685. | Global.EntierH: (* TODO: arity? *)
  6686. | Global.Len: (* unary and binary *)
  6687. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6688. | Global.Max, Global.Min: (* unary and binary *)
  6689. | Global.Odd: (* TODO: arity? *)
  6690. | Global.Sum: (* TODO: arity? *)
  6691. | Global.All: (* TODO: arity? *)
  6692. | Global.Re, Global.Im:
  6693. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6694. | Scanner.Alias:
  6695. | Scanner.GreaterGreater, Scanner.LessLess:
  6696. mustBeBinary := TRUE; hasReturnType := FALSE;
  6697. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6698. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6699. END;
  6700. (* check parameter count *)
  6701. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6702. Error(operator.position,"operator is not unary")
  6703. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6704. Error(operator.position,"operator is not binary")
  6705. END;
  6706. (* check parameter types *)
  6707. (* TODO: is this used at all? *)
  6708. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6709. leftType := procedureType.firstParameter.type;
  6710. rightType := procedureType.firstParameter.nextParameter.type;
  6711. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6712. Error(operator.position, "the two operands are not of the same type")
  6713. END
  6714. END;
  6715. (* check return type *)
  6716. IF hasReturnType THEN
  6717. IF procedureType.returnType = NIL THEN
  6718. Error(operator.position, "return type required")
  6719. ELSIF mustReturnBoolean THEN
  6720. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6721. Error(operator.position,"return type is not Boolean")
  6722. END
  6723. ELSIF mustReturnInteger THEN
  6724. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6725. Error(operator.position,"return type is not integer")
  6726. END
  6727. END
  6728. ELSIF procedureType.returnType # NIL THEN
  6729. Error(operator.position, "return type not allowed")
  6730. END
  6731. END
  6732. END
  6733. END
  6734. END VisitOperator;
  6735. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6736. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6737. BEGIN
  6738. IF error THEN RETURN FALSE END;
  6739. prevScope := currentScope;
  6740. prevDiagnostics := diagnostics;
  6741. diagnostics := NIL; (* suppress error output *)
  6742. currentScope := module.moduleScope;
  6743. VisitImport(x);
  6744. IF ~error THEN
  6745. module.moduleScope.AddImport(x);
  6746. x.SetScope(module.moduleScope);
  6747. END;
  6748. currentScope := prevScope;
  6749. diagnostics := prevDiagnostics;
  6750. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6751. END AddImport;
  6752. (** check and resolve import
  6753. - check for name = SYSTEM
  6754. - check for forbidden self import
  6755. - search through global import cache: already imported?
  6756. - check if already imported indirectly
  6757. - import if necessary -> set module and enter into import cache
  6758. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6759. - after this import this direct import and all indirect imports are stored in the current module's import list
  6760. **)
  6761. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6762. VAR
  6763. module: SyntaxTree.Module;
  6764. moduleScope: SyntaxTree.ModuleScope;
  6765. import,reimport: SyntaxTree.Import;
  6766. filename: FileName;
  6767. prevScope: SyntaxTree.Scope;
  6768. BEGIN
  6769. IF SymbolNeedsResolution(x) THEN
  6770. prevScope := currentScope;
  6771. x.SetType(SyntaxTree.importType);
  6772. moduleScope := currentScope.ownerModule.moduleScope;
  6773. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6774. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6775. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6776. Error(x.position,"forbidden self import");
  6777. ELSE
  6778. (* search through global import list: already imported ? *)
  6779. IF (x.module = NIL) & (importCache # NIL) THEN
  6780. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6781. ELSE import := NIL
  6782. END;
  6783. IF x.module # NIL THEN (* already imported indirectly *)
  6784. module := x.module;
  6785. ELSIF import # NIL THEN (* already in module list *)
  6786. module := import.module;
  6787. ASSERT(module # NIL);
  6788. x.SetModule(module);
  6789. ELSE (* must be imported *)
  6790. Global.ModuleFileName(x.moduleName,x.context,filename);
  6791. IF symbolFileFormat # NIL THEN
  6792. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6793. IF module = NIL THEN
  6794. ErrorSS(x.position,"could not import",filename);
  6795. IF VerboseErrorMessage THEN
  6796. Printout.Info("import",x)
  6797. END
  6798. ELSE
  6799. (*
  6800. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6801. (*! should rather be done by importer *)
  6802. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6803. checker.importCache := importCache;
  6804. checker.arrayBaseImported := arrayBaseImported;
  6805. checker.global := global;
  6806. checker.Module(module); (* semantic check *)
  6807. error := error OR checker.error;
  6808. END;
  6809. *)
  6810. (*
  6811. ASSERT(SyntaxTree.Resolved IN module.state);
  6812. *)
  6813. x.SetModule(module);
  6814. IF importCache # NIL THEN
  6815. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6816. import.SetContext(x.context);
  6817. import.SetModule(module);
  6818. importCache.AddImport(import);
  6819. END;
  6820. END;
  6821. ELSE
  6822. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6823. END;
  6824. END;
  6825. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6826. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6827. END;
  6828. import := module.moduleScope.firstImport;
  6829. WHILE(import # NIL) DO
  6830. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6831. ASSERT(currentScope # NIL);
  6832. ASSERT(currentScope.ownerModule # NIL);
  6833. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6834. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6835. Error(x.position,"recursive import");
  6836. ELSE
  6837. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6838. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6839. IF reimport = NIL THEN (* indirect import *)
  6840. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6841. reimport.SetContext(import.context);
  6842. reimport.SetModule(import.module);
  6843. moduleScope.AddImport(reimport);
  6844. reimport.SetScope(moduleScope);
  6845. ELSE
  6846. ASSERT(import.module # NIL);
  6847. reimport.SetModule(import.module); (* direct or indirect import *)
  6848. END;
  6849. END;
  6850. import := import.nextImport;
  6851. END;
  6852. END;
  6853. END;
  6854. currentScope := prevScope;
  6855. (* ELSE nothing to be done *)
  6856. x.SetState(SyntaxTree.Resolved);
  6857. END;
  6858. END VisitImport;
  6859. (*** statements ***)
  6860. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6861. VAR prev,resolved: SyntaxTree.Statement;
  6862. BEGIN
  6863. prev := resolvedStatement;
  6864. resolvedStatement := x;
  6865. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6866. activeCellsStatement := FALSE;
  6867. x.Accept(SELF);
  6868. (* removed this, implementation restriction should be resolved by backend
  6869. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6870. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6871. END;
  6872. *)
  6873. resolved := resolvedStatement;
  6874. resolvedStatement := prev;
  6875. RETURN resolved
  6876. END ResolveStatement;
  6877. (** check and resolve statement sequence
  6878. - check all statements, replace if necessary
  6879. **)
  6880. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6881. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6882. BEGIN
  6883. IF statementSequence # NIL THEN (* else empty *)
  6884. FOR i := 0 TO statementSequence.Length()-1 DO
  6885. statement := statementSequence.GetStatement(i);
  6886. resolved := ResolveStatement(statement);
  6887. IF (resolved # statement) THEN
  6888. statementSequence.SetStatement(i,resolved);
  6889. END;
  6890. END;
  6891. END;
  6892. END StatementSequence;
  6893. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6894. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6895. - check if procedure is callable
  6896. - check return type = NIL (otherwise must be assignment statement)
  6897. **)
  6898. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6899. VAR call: SyntaxTree.Designator;
  6900. BEGIN
  6901. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6902. call := procedureCall.call;
  6903. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6904. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6905. END;
  6906. call := ResolveDesignator(call);
  6907. IF call = SyntaxTree.invalidDesignator THEN
  6908. (* error already handled *)
  6909. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6910. (* inline call in a statement *)
  6911. ELSIF ~IsCallable(call) THEN
  6912. Error(procedureCall.position,"called object is not a procedure");
  6913. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6914. Error(procedureCall.position,"calling procedure with non-void return type");
  6915. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6916. END;
  6917. procedureCall.SetCall(call);
  6918. (*
  6919. IF call = SyntaxTree.invalidDesignator THEN
  6920. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6921. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6922. IF IsOberonInline(procedure) THEN
  6923. Warning(procedure.position,"call to inline proc");
  6924. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6925. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6926. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6927. resolvedStatement := block;
  6928. RETURN;
  6929. END;
  6930. END;
  6931. *)
  6932. END VisitProcedureCallStatement;
  6933. (** check and resolve assignment LHS := RHS
  6934. - resolve LHS and RHS
  6935. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6936. - check if assignment is compatible
  6937. - check if LHS is variable (i.e. assignable)
  6938. - convert RHS if necessary
  6939. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6940. - assignment between different ASOTs
  6941. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6942. - assignment to ASOT elements:
  6943. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6944. **)
  6945. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6946. VAR
  6947. left: SyntaxTree.Designator;
  6948. right, expression: SyntaxTree.Expression;
  6949. designator: SyntaxTree.Designator;
  6950. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6951. mathArrayType: SyntaxTree.MathArrayType;
  6952. BEGIN
  6953. right := ResolveExpression(assignment.right);
  6954. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6955. left := ResolveDesignator(assignment.left);
  6956. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6957. (* error already handled *)
  6958. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6959. (* LHS is index write operator call on ASOT *)
  6960. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6961. (* necessary ?
  6962. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6963. type := procedureType.firstParameter.type;
  6964. expression := procedureCallDesignator.parameters.GetExpression(0);
  6965. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6966. *)
  6967. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6968. ELSIF CheckVariable(left) THEN
  6969. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6970. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6971. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6972. (* conversion done by procedure call
  6973. (* try to convert to left argument *)
  6974. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6975. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6976. procedureCallDesignator.parameters.SetExpression(1, right);
  6977. END;
  6978. *)
  6979. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6980. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6981. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6982. ELSIF AssignmentCompatible(left, right) THEN
  6983. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6984. mathArrayType := MathArrayStructureOfType(left.type);
  6985. right := NewConversion(right.position, right, mathArrayType, NIL);
  6986. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6987. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6988. ELSE
  6989. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6990. assignment.SetLeft(left);
  6991. assignment.SetRight(right);
  6992. resolvedStatement := assignment
  6993. END
  6994. END
  6995. END
  6996. END VisitAssignment;
  6997. (** check and resolve assignment LHS := RHS
  6998. - resolve LHS and RHS
  6999. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7000. - check if assignment is compatible
  7001. - check if LHS is variable (i.e. assignable)
  7002. - convert RHS if necessary
  7003. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7004. - assignment between different ASOTs
  7005. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7006. - assignment to ASOT elements:
  7007. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7008. **)
  7009. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7010. VAR
  7011. left: SyntaxTree.Designator;
  7012. right: SyntaxTree.Expression;
  7013. inPort, outPort: SyntaxTree.PortType;
  7014. expression: SyntaxTree.Expression;
  7015. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7016. BEGIN
  7017. right := ResolveExpression(communication.right);
  7018. left := ResolveDesignator(communication.left);
  7019. communication.SetLeft(left);
  7020. communication.SetRight(right);
  7021. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7022. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7023. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7024. (* conversion done by procedure call
  7025. (* try to convert to left argument *)
  7026. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7027. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7028. procedureCallDesignator.parameters.SetExpression(1, right);
  7029. END;
  7030. *)
  7031. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7032. ELSE
  7033. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7034. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7035. (* error already handled *)
  7036. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7037. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7038. IF outPort.direction # SyntaxTree.OutPort THEN
  7039. Error(left.position,"not an out-port")
  7040. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7041. Error(left.position,"incompatible to port type");
  7042. ELSE
  7043. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7044. communication.SetRight(right)
  7045. END;
  7046. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7047. IF CheckVariable(left) THEN
  7048. IF inPort.direction # SyntaxTree.InPort THEN
  7049. Error(left.position,"not an in-port")
  7050. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7051. Error(right.position,"incompatible to port type");
  7052. END;
  7053. END;
  7054. ELSE
  7055. Error(communication.position,"unsupported stream operation");
  7056. END;
  7057. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7058. IF outPort.direction # SyntaxTree.OutPort THEN
  7059. Error(left.position,"not an out-port")
  7060. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7061. Error(left.position,"incompatible to port type");
  7062. ELSE
  7063. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7064. communication.SetRight(right)
  7065. END;
  7066. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7067. IF CheckVariable(right) THEN
  7068. IF inPort.direction # SyntaxTree.InPort THEN
  7069. Error(left.position,"not an in-port")
  7070. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7071. Error(right.position,"incompatible to port type");
  7072. END;
  7073. END;
  7074. ELSE
  7075. Error(communication.position, "unsupported operation");
  7076. END;
  7077. END;
  7078. END VisitCommunicationStatement;
  7079. (** check and resolve if/eslif part
  7080. - check condition
  7081. - check statement sequence
  7082. **)
  7083. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7084. VAR prevUnreachable, b: BOOLEAN;
  7085. BEGIN
  7086. prevUnreachable := currentIsUnreachable;
  7087. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7088. IF IsBooleanValue(ifPart.condition,b) THEN
  7089. IF b=FALSE THEN
  7090. currentIsUnreachable := TRUE
  7091. ELSIF b=TRUE THEN
  7092. true := TRUE
  7093. END;
  7094. END;
  7095. StatementSequence(ifPart.statements);
  7096. currentIsUnreachable := prevUnreachable;
  7097. END IfPart;
  7098. (** check and resolve if statement
  7099. - check if parts and else part statement sequence
  7100. **)
  7101. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7102. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7103. BEGIN
  7104. prevUnreachable := currentIsUnreachable;
  7105. ifPartTrue := FALSE;
  7106. IfPart(ifStatement.ifPart,ifPartTrue);
  7107. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7108. elsif := ifStatement.GetElsifPart(i);
  7109. IfPart(elsif,ifPartTrue);
  7110. END;
  7111. IF ifStatement.elsePart # NIL THEN
  7112. IF ifPartTrue THEN
  7113. currentIsUnreachable := TRUE
  7114. END;
  7115. StatementSequence(ifStatement.elsePart)
  7116. END;
  7117. currentIsUnreachable := prevUnreachable;
  7118. END VisitIfStatement;
  7119. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7120. VAR variable: SyntaxTree.Designator;
  7121. type,variableType: SyntaxTree.Type;
  7122. withEntry: WithEntry;
  7123. BEGIN
  7124. variable := ResolveDesignator(withPart.variable);
  7125. variableType := variable.type.resolved;
  7126. withPart.SetVariable(variable);
  7127. type := ResolveType(withPart.type);
  7128. withPart.SetType(type);
  7129. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7130. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7131. END;
  7132. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7133. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7134. Error(variable.position,"is not extensible designator");
  7135. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7136. Error(variable.position,"is no local variable ");
  7137. IF VerboseErrorMessage THEN
  7138. Printout.Info("variable",variable)
  7139. END;
  7140. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7141. Error(variable.position,"withguarded symbol is no type extension of ");
  7142. IF VerboseErrorMessage THEN
  7143. Printout.Info("variable",variable);
  7144. Printout.Info("type",type);
  7145. END;
  7146. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7147. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7148. Error(variable.position,"withguarded symbol is no variable ");
  7149. IF VerboseErrorMessage THEN
  7150. Printout.Info("variable",variable);
  7151. Printout.Info("type",type);
  7152. END;
  7153. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7154. Error(variable.position,"invalid change of withguarded symbol");
  7155. ELSE
  7156. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7157. NEW(withEntry);
  7158. withEntry.previous := withEntries;
  7159. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7160. withEntry.type := type;
  7161. withEntries := withEntry;
  7162. StatementSequence(withPart.statements);
  7163. withEntries := withEntries.previous;
  7164. END;
  7165. END WithPart;
  7166. (** check and resolve with statement WITH variable: type DO ... END;
  7167. - check type and variable
  7168. - check that variable type is type extension of type
  7169. - check that variable is a variable
  7170. - enter new with scope and enter guardedVariable with same name and reference to variable
  7171. - create if statement:
  7172. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7173. **)
  7174. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7175. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7176. BEGIN
  7177. prevScope := currentScope; symbol := NIL;
  7178. FOR i := 0 TO withStatement.WithParts()-1 DO
  7179. WithPart(withStatement.GetWithPart(i),symbol);
  7180. END;
  7181. IF withStatement.elsePart # NIL THEN
  7182. StatementSequence(withStatement.elsePart)
  7183. END;
  7184. currentScope := prevScope;
  7185. END VisitWithStatement;
  7186. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7187. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7188. - check 'first' < 'last' and no overlaps between different case labels
  7189. - check statement sequence
  7190. **)
  7191. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7192. VAR
  7193. i: LONGINT;
  7194. position: Position;
  7195. expression, left, right: SyntaxTree.Expression;
  7196. expressionType: SyntaxTree.Type;
  7197. l, r: LONGINT;
  7198. cl, cr: CHAR;
  7199. thiscases: SyntaxTree.CaseConstant;
  7200. BEGIN
  7201. thiscases := NIL;
  7202. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7203. expression := casePart.elements.GetExpression(i);
  7204. position := expression.position;
  7205. (* set context of range *)
  7206. IF expression IS SyntaxTree.RangeExpression THEN
  7207. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7208. END;
  7209. expression := ResolveExpression(expression);
  7210. IF expression = SyntaxTree.invalidExpression THEN
  7211. (* error already reported *)
  7212. expressionType := SyntaxTree.invalidType;
  7213. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7214. (* read out 'first' and 'last' *)
  7215. left := expression(SyntaxTree.RangeExpression).first;
  7216. right := expression(SyntaxTree.RangeExpression).last;
  7217. (* guaranteed by VisitRangeExpression: *)
  7218. ASSERT((left # NIL) & (right # NIL));
  7219. ASSERT(left.type.resolved = right.type.resolved);
  7220. left := CompatibleConversion(left.position, left, type);
  7221. right := CompatibleConversion(right.position, right, type);
  7222. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7223. expression(SyntaxTree.RangeExpression).SetLast(right);
  7224. expressionType := RegularType(position,left.type);
  7225. ELSE
  7226. expression := ConstantExpression(expression);
  7227. expression := CompatibleConversion(expression.position, expression, type);
  7228. (*
  7229. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7230. left := Global.NewCharacterValue(system,expression.position,cl);
  7231. expression := casePart.elements.GetExpression(i);
  7232. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7233. expression := left
  7234. END;
  7235. *)
  7236. casePart.elements.SetExpression(i,expression);
  7237. left := expression; right := expression;
  7238. expressionType := RegularType(position,expression.type)
  7239. END;
  7240. IF (expressionType = SyntaxTree.invalidType) THEN
  7241. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7242. Error(position, "inadmissible case label");
  7243. expression := SyntaxTree.invalidExpression;
  7244. ELSE
  7245. l := 0; r := 0;
  7246. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7247. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7248. l := ORD(cl); r := ORD(cr);
  7249. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7250. ELSE
  7251. expression := SyntaxTree.invalidExpression
  7252. END;
  7253. IF expression # SyntaxTree.invalidExpression THEN
  7254. IF l>r THEN
  7255. Error(position, "empty case label")
  7256. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7257. Error(position, "duplicate case label");
  7258. ELSE
  7259. IF l < min THEN min := l END;
  7260. IF r > max THEN max := r END;
  7261. END;
  7262. END;
  7263. END;
  7264. casePart.elements.SetExpression(i,expression);
  7265. END;
  7266. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7267. casePart.SetConstants(thiscases);
  7268. StatementSequence(casePart.statements);
  7269. END CasePart;
  7270. (** check and resolve case statement CASE variable OF ... END;
  7271. - check variable
  7272. - check case parts
  7273. **)
  7274. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7275. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7276. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7277. BEGIN
  7278. expression := ResolveExpression(caseStatement.variable);
  7279. type := RegularType(expression.position,expression.type);
  7280. IF type = SyntaxTree.invalidType THEN
  7281. expression := SyntaxTree.invalidExpression;
  7282. ELSIF IsIntegerType(type) THEN
  7283. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7284. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7285. (*
  7286. expression := Global.NewCharacterValue(system,expression.position,ch);
  7287. *)
  7288. type := expression.type;
  7289. ELSIF IsCharacterType(type) THEN
  7290. ELSIF IsEnumerationType(type) THEN
  7291. ELSE
  7292. Error(caseStatement.variable.position,"variable must be integer or character type");
  7293. expression := SyntaxTree.invalidExpression;
  7294. END;
  7295. caseStatement.SetVariable(expression);
  7296. caseList := NIL;
  7297. min := MAX(LONGINT); max := MIN(LONGINT);
  7298. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7299. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7300. END;
  7301. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7302. msg := "huge sparse case table ";
  7303. Strings.AppendInt(msg, max-min);
  7304. Strings.Append(msg,"/");
  7305. Strings.AppendInt(msg, caseStatement.CaseParts());
  7306. Warning(caseStatement.position,msg);
  7307. END;
  7308. caseStatement.SetMinMax(min,max);
  7309. StatementSequence(caseStatement.elsePart);
  7310. IF expression.resolved # NIL THEN
  7311. IF IsCharacterValue(expression,ch) THEN
  7312. l := ORD(ch)
  7313. ELSIF IsIntegerValue(expression,l) THEN
  7314. END;
  7315. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7316. END;
  7317. END VisitCaseStatement;
  7318. (** check and resolve while statement
  7319. - check condition
  7320. - check statement sequence
  7321. **)
  7322. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7323. VAR prevIsUnreachable,b: BOOLEAN;
  7324. BEGIN
  7325. prevIsUnreachable := currentIsUnreachable;
  7326. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7327. IF IsBooleanValue(whileStatement.condition,b) THEN
  7328. IF b=FALSE THEN
  7329. currentIsUnreachable := TRUE
  7330. END;
  7331. END;
  7332. StatementSequence(whileStatement.statements);
  7333. currentIsUnreachable := prevIsUnreachable
  7334. END VisitWhileStatement;
  7335. (** check and resolve repeat statement
  7336. - check condition
  7337. - check statement sequence
  7338. **)
  7339. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7340. BEGIN
  7341. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7342. StatementSequence(repeatStatement.statements);
  7343. END VisitRepeatStatement;
  7344. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7345. VAR withEntry: WithEntry;
  7346. BEGIN
  7347. withEntry := withEntries;
  7348. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7349. withEntry := withEntry.previous
  7350. END;
  7351. IF withEntry = NIL THEN RETURN FALSE
  7352. ELSE
  7353. type := withEntry.type;
  7354. RETURN TRUE
  7355. END;
  7356. END GetGuard;
  7357. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7358. - check that variable is an integer variable
  7359. - check that from is integer typed with compatible type
  7360. - check that to has compatible type
  7361. - check that by is constant integer with compatible type
  7362. **)
  7363. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7364. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7365. BEGIN
  7366. designator := ResolveDesignator(forStatement.variable);
  7367. type := SyntaxTree.invalidType;
  7368. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7369. designator := SyntaxTree.invalidDesignator;
  7370. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7371. Error(designator.position,"control variable of non-integer type");
  7372. designator := SyntaxTree.invalidDesignator;
  7373. ELSIF CheckVariable(designator) THEN
  7374. type := designator.type;
  7375. END;
  7376. forStatement.SetVariable(designator);
  7377. expression := ResolveExpression(forStatement.from);
  7378. IF expression = SyntaxTree.invalidExpression THEN
  7379. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7380. Error(expression.position,"start value of incompatible type");
  7381. expression := SyntaxTree.invalidExpression;
  7382. ELSIF type # SyntaxTree.invalidType THEN
  7383. expression := NewConversion(expression.position,expression,type,NIL)
  7384. END;
  7385. forStatement.SetFrom(expression);
  7386. expression := ResolveExpression(forStatement.to);
  7387. IF expression = SyntaxTree.invalidExpression THEN
  7388. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7389. Error(expression.position,"end value of incompatible type");
  7390. expression := SyntaxTree.invalidExpression;
  7391. ELSIF type # SyntaxTree.invalidType THEN
  7392. expression := NewConversion(expression.position,expression,type,NIL)
  7393. END;
  7394. forStatement.SetTo(expression);
  7395. IF forStatement.by # NIL THEN
  7396. expression := ConstantInteger(forStatement.by);
  7397. ELSE
  7398. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7399. END;
  7400. IF expression = SyntaxTree.invalidExpression THEN
  7401. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7402. Error(expression.position,"step value of incompatible type");
  7403. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7404. Error(expression.position,"invalid step value");
  7405. ELSIF type # SyntaxTree.invalidType THEN
  7406. expression := NewConversion(expression.position,expression,type,NIL)
  7407. END;
  7408. forStatement.SetBy(expression);
  7409. StatementSequence(forStatement.statements);
  7410. END VisitForStatement;
  7411. (** check and resolve loop statement LOOP StatementSequence END
  7412. - check statement sequence
  7413. **)
  7414. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7415. BEGIN
  7416. StatementSequence(loopStatement.statements)
  7417. END VisitLoopStatement;
  7418. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7419. BEGIN
  7420. StatementSequence(exitableBlock.statements);
  7421. END VisitExitableBlock;
  7422. (** check and resolve exit statement EXIT
  7423. - check that exit is within LOOP statement block
  7424. **)
  7425. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7426. VAR outer: SyntaxTree.Statement;
  7427. BEGIN
  7428. outer := exitStatement.outer;
  7429. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7430. outer := outer.outer;
  7431. END;
  7432. IF outer = NIL THEN
  7433. Error(exitStatement.position,"exit statement not within loop statement");
  7434. END;
  7435. END VisitExitStatement;
  7436. (** check and resolve return statement RETURN [expression]
  7437. - check expression (if any)
  7438. - check if in procedure scope
  7439. - if in procedure scope then check expression compatibility
  7440. - if not in procecdure scope then check on return without expression
  7441. **)
  7442. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7443. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7444. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7445. BEGIN
  7446. position := returnStatement.position;
  7447. expression := returnStatement.returnValue;
  7448. IF expression # NIL THEN
  7449. expression := ResolveExpression(expression);
  7450. returnStatement.SetReturnValue(expression);
  7451. END;
  7452. outer := returnStatement.outer;
  7453. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7454. outer := outer.outer
  7455. END;
  7456. IF (outer # NIL) THEN
  7457. scope := outer(SyntaxTree.Body).inScope;
  7458. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7459. IF (expression # NIL) THEN
  7460. Error(position, "return statement with parameter not in procedure scope");
  7461. END;
  7462. ELSE
  7463. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7464. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7465. Error(position, "return statement in procedure that does not return");
  7466. END;
  7467. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7468. IF returnType # NIL THEN
  7469. returnType := returnType.resolved;
  7470. IF expression = NIL THEN
  7471. Error(position, "empty return type in procedure providing a return type")
  7472. ELSIF expression.type = NIL THEN
  7473. Error(position,"returned type incompatible: expression has no type");
  7474. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7475. Error(position, "return type not compatible");
  7476. IF VerboseErrorMessage THEN
  7477. Printout.Info("returnType",returnType);
  7478. Printout.Info("expression",expression);
  7479. END;
  7480. ELSE
  7481. expression := NewConversion(expression.position,expression,returnType,NIL);
  7482. returnStatement.SetReturnValue(expression);
  7483. END;
  7484. ELSIF expression # NIL THEN
  7485. Error(position, "non-empty return type in procedure providing no return type");
  7486. END;
  7487. END;
  7488. END;
  7489. END VisitReturnStatement;
  7490. (** check and resolve await statement AWAIT(condition: Expression)
  7491. - check await condition
  7492. **)
  7493. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7494. VAR condition: SyntaxTree.Expression;
  7495. BEGIN
  7496. condition := ResolveCondition(awaitStatement.condition);
  7497. IF currentIsRealtime THEN
  7498. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7499. END;
  7500. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7501. Error(awaitStatement.position,"senseless await statement with constant condition");
  7502. END;
  7503. awaitStatement.SetCondition(condition);
  7504. END VisitAwaitStatement;
  7505. PROCEDURE CheckSystemImport(position: Position);
  7506. VAR import: SyntaxTree.Import;
  7507. BEGIN
  7508. import := currentScope.ownerModule.moduleScope.firstImport;
  7509. WHILE(import # NIL) DO
  7510. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7511. RETURN;
  7512. END;
  7513. import := import.nextImport;
  7514. END;
  7515. Error(position, "forbidden code without system import");
  7516. END CheckSystemImport;
  7517. (** check and resolve code statement: do nothing, must be done by assembler
  7518. **)
  7519. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7520. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7521. BEGIN
  7522. CheckSystemImport(code.position);
  7523. FOR i := 0 TO code.inRules.Length()-1 DO
  7524. statement := code.inRules.GetStatement(i);
  7525. IF statement IS SyntaxTree.Assignment THEN
  7526. WITH statement: SyntaxTree.Assignment DO
  7527. statement.SetRight(ResolveExpression(statement.right));
  7528. END;
  7529. ELSE
  7530. Error(statement.position, "can only be assignment")
  7531. END;
  7532. END;
  7533. FOR i := 0 TO code.outRules.Length()-1 DO
  7534. statement := code.outRules.GetStatement(i);
  7535. IF statement IS SyntaxTree.Assignment THEN
  7536. WITH statement: SyntaxTree.Assignment DO
  7537. statement.SetLeft(ResolveDesignator(statement.left));
  7538. END;
  7539. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7540. (* must be a reference to some register *)
  7541. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7542. ELSE
  7543. Printout.Info("out statement ", statement);
  7544. Error(statement.position, "(out) can only be assignment")
  7545. END;
  7546. END;
  7547. END VisitCode;
  7548. (** check and set flags of a statement block
  7549. - check for multiply occurence of a flag
  7550. - check and set priority only in bodies
  7551. - check for valid names
  7552. **)
  7553. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7554. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7555. flag: LONGINT; recordBody: SyntaxTree.Body;
  7556. PROCEDURE SetProtectedRecord;
  7557. VAR scope: SyntaxTree.Scope;
  7558. BEGIN
  7559. scope := currentScope;
  7560. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7561. scope := scope.outerScope
  7562. END;
  7563. IF scope # NIL THEN
  7564. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7565. END;
  7566. END SetProtectedRecord;
  7567. BEGIN
  7568. flags := {};
  7569. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7570. recordBody := block(SyntaxTree.Body)
  7571. ELSE
  7572. recordBody := NIL
  7573. END;
  7574. blockModifier := block.blockModifiers;
  7575. WHILE(blockModifier # NIL) DO
  7576. name := blockModifier.identifier;
  7577. expression := blockModifier.expression;
  7578. position := blockModifier.position;
  7579. flag := -1;
  7580. IF name=Global.NamePriority THEN
  7581. IF expression = NIL THEN
  7582. Error(position, "missing priority expression");
  7583. ELSIF recordBody = NIL THEN
  7584. Error(position, "priority not on record body");
  7585. ELSIF recordBody.priority # NIL THEN
  7586. Error(position, "duplicate priority expression");
  7587. ELSE
  7588. recordBody.SetPriority(expression);
  7589. END;
  7590. ELSIF expression # NIL THEN
  7591. Error(expression.position,"expression not in connection with priority")
  7592. ELSIF name=Global.NameExclusive THEN
  7593. IF block.isExclusive THEN
  7594. Error(position, "duplicate exclusive flag")
  7595. END;
  7596. block.SetExclusive(TRUE); SetProtectedRecord;
  7597. ELSIF name=Global.NameActive THEN
  7598. IF recordBody = NIL THEN
  7599. Error(position, "active not in record body");
  7600. ELSIF recordBody.isActive THEN
  7601. Error(position, "duplicate active flag")
  7602. ELSE
  7603. recordBody.SetActive(TRUE); SetProtectedRecord;
  7604. END;
  7605. ELSIF name=Global.NameSafe THEN
  7606. IF recordBody = NIL THEN
  7607. Error(position, "safe not in record body");
  7608. ELSIF recordBody.isSafe THEN
  7609. Error(position, "duplicate safe flag")
  7610. ELSE
  7611. recordBody.SetSafe(TRUE);
  7612. SetProtectedRecord;
  7613. END;
  7614. ELSIF name=Global.NameRealtime THEN
  7615. IF recordBody = NIL THEN
  7616. Error(position, "realtime not in record body");
  7617. ELSIF recordBody.isRealtime THEN
  7618. Error(position, "duplicate realtime flag")
  7619. ELSE
  7620. recordBody.SetRealtime(TRUE);
  7621. block.SetRealtime(TRUE);
  7622. END;
  7623. ELSIF name=Global.NameUnchecked THEN
  7624. IF block.isUnchecked THEN
  7625. Error(position, "duplicate unchecked flag")
  7626. ELSE
  7627. block.SetUnchecked(TRUE);
  7628. END;
  7629. ELSIF (name=Global.NameUncooperative) THEN
  7630. IF block.isUncooperative THEN
  7631. Error(position, "duplicate uncooperative flag")
  7632. ELSE
  7633. block.SetUncooperative(TRUE);
  7634. END;
  7635. ELSE
  7636. Error(position, "unknown block modifier");
  7637. END;
  7638. blockModifier := blockModifier.nextModifier;
  7639. END;
  7640. END BlockFlags;
  7641. (** check and resolve statement block
  7642. - check flags (exclusive)
  7643. - check statement sequence
  7644. **)
  7645. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7646. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7647. BEGIN
  7648. BlockFlags(statementBlock);
  7649. IF statementBlock.isExclusive THEN
  7650. (* check that not in exclusive block *)
  7651. IF currentIsExclusive THEN
  7652. Error (statementBlock.position,"forbidden recursive exclusive")
  7653. ELSIF currentIsRealtime THEN
  7654. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7655. END;
  7656. END;
  7657. recentExclusive := currentIsExclusive;
  7658. recentUnreachable := currentIsUnreachable;
  7659. recentRealtime := currentIsRealtime;
  7660. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7661. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7662. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7663. StatementSequence(statementBlock.statements);
  7664. currentIsRealtime := recentRealtime;
  7665. currentIsExclusive := recentExclusive;
  7666. currentIsUnreachable := recentUnreachable;
  7667. END VisitStatementBlock;
  7668. (** check and resolve body
  7669. - check flags (active, priority, safe)
  7670. - check body and finally part
  7671. **)
  7672. PROCEDURE Body(body: SyntaxTree.Body);
  7673. BEGIN
  7674. VisitStatementBlock(body);
  7675. IF body.isActive THEN
  7676. IF ~currentIsBodyProcedure THEN
  7677. Error(body.position,"active flag not in object body");
  7678. ELSIF body.priority # NIL THEN
  7679. body.SetPriority(ConstantInteger(body.priority));
  7680. END;
  7681. ELSIF body.isSafe THEN
  7682. Error(body.position,"safe flag not in active body");
  7683. ELSIF body.priority # NIL THEN
  7684. Error(body.position,"priority flag not in active body");
  7685. END;
  7686. IF body.code # NIL THEN
  7687. CheckSystemImport(body.position);
  7688. END;
  7689. StatementSequence(body.finally)
  7690. END Body;
  7691. (*** scopes ***)
  7692. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7693. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7694. VAR duplicateSymbol: BOOLEAN;
  7695. BEGIN
  7696. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7697. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7698. Error(symbol.position,"globally defined keyword")
  7699. END;
  7700. scope.EnterSymbol(symbol,duplicateSymbol);
  7701. IF ~allowDuplicate & duplicateSymbol THEN
  7702. Error(symbol.position,"Multiply defined identifier.");
  7703. IF VerboseErrorMessage THEN
  7704. Printout.Info("multiply defined identifier",symbol);
  7705. Printout.Info("in scope",scope);
  7706. END;
  7707. END;
  7708. END Register;
  7709. (**
  7710. implementation: check and resolve an implementation part
  7711. **)
  7712. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7713. move implementation checker to a separate object ? *)
  7714. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7715. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7716. BEGIN
  7717. prevIsRealtime := currentIsRealtime;
  7718. prevIsBodyProcedure := currentIsBodyProcedure;
  7719. prevIsCellNet := currentIsCellNet;
  7720. prevScope := currentScope;
  7721. currentScope := scope;
  7722. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7723. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7724. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7725. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7726. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7727. (*
  7728. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7729. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7730. END;
  7731. *)
  7732. END;
  7733. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7734. (* module body, record bodies are wrapped into an artifical procedure *)
  7735. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7736. Body(scope(SyntaxTree.ProcedureScope).body)
  7737. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7738. Body(scope(SyntaxTree.ProcedureScope).body)
  7739. END;
  7740. END;
  7741. currentScope := prevScope;
  7742. currentIsRealtime := prevIsRealtime;
  7743. currentIsBodyProcedure := prevIsBodyProcedure;
  7744. currentIsCellNet := prevIsCellNet;
  7745. END Implementation;
  7746. (** implementation phase:
  7747. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7748. **)
  7749. PROCEDURE Implementations(x: SyntaxTree.Module);
  7750. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7751. BEGIN
  7752. prevPhase := phase;
  7753. phase := InlinePhase;
  7754. scope := x.firstScope;
  7755. WHILE(scope # NIL) DO
  7756. Implementation(scope);
  7757. scope := scope.nextScope;
  7758. END;
  7759. phase := ImplementationPhase;
  7760. scope := x.firstScope;
  7761. WHILE(scope # NIL) DO
  7762. Implementation(scope);
  7763. scope := scope.nextScope;
  7764. END;
  7765. phase := prevPhase;
  7766. END Implementations;
  7767. (** declaration phase:
  7768. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7769. - import lists (for module scopes)
  7770. - parameter list (for procedure scopes)
  7771. - constant declarations
  7772. - type declarations
  7773. - variable declarations
  7774. - procedure declarations
  7775. preformed in two stages:
  7776. - first all symbols are entered into the symbol table (with uniqueness check),
  7777. - then all symbols are resolved
  7778. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7779. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7780. phases :
  7781. 0 = before procedures
  7782. 1 = procedures and later
  7783. **)
  7784. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7785. VAR
  7786. constant: SyntaxTree.Constant;
  7787. typeDeclaration: SyntaxTree.TypeDeclaration;
  7788. declaredType: SyntaxTree.Type;
  7789. variable: SyntaxTree.Variable;
  7790. procedure: SyntaxTree.Procedure;
  7791. procedureType : SyntaxTree.ProcedureType;
  7792. prevScope: SyntaxTree.Scope;
  7793. parameter: SyntaxTree.Parameter;
  7794. import: SyntaxTree.Import;
  7795. symbol: SyntaxTree.Symbol;
  7796. prevPhase: LONGINT;
  7797. prevError : BOOLEAN;
  7798. i: LONGINT;
  7799. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7800. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7801. BEGIN
  7802. IF type.baseType # NIL THEN
  7803. baseType := type.baseType.resolved;
  7804. IF baseType IS SyntaxTree.PointerType THEN
  7805. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7806. END;
  7807. (*
  7808. IF baseType IS SyntaxTree.CellType THEN
  7809. DeclareCell(baseType(SyntaxTree.CellType));
  7810. END;
  7811. *)
  7812. END;
  7813. parameter := type.firstParameter;
  7814. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7815. (*
  7816. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7817. variable.SetType(parameter.type);
  7818. variable.SetAccess(SyntaxTree.Hidden);
  7819. variable.SetModifiers(parameter.modifiers);
  7820. currentScope.PushVariable(variable);
  7821. *)
  7822. Register(parameter,scope, FALSE);
  7823. parameter := parameter.nextParameter;
  7824. END;
  7825. property := type.firstProperty;
  7826. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7827. (*
  7828. variable := currentScope.FindVariable(property.name);
  7829. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7830. prop := variable(SyntaxTree.Property);
  7831. ELSE (* add, duplicate symbols detection later *)
  7832. prop := SyntaxTree.NewProperty(property.position, property.name);
  7833. currentScope.PushVariable(prop);
  7834. END;
  7835. prop.SetType(property.type);
  7836. prop.SetValue(property.value);
  7837. prop.SetAccess(SyntaxTree.Hidden);
  7838. *)
  7839. Register(property, scope, FALSE);
  7840. property := property.nextProperty;
  7841. END;
  7842. END DeclareCell;
  7843. BEGIN
  7844. prevError := error;
  7845. prevPhase := phase;
  7846. phase := DeclarationPhase;
  7847. prevScope := currentScope;
  7848. currentScope := scope;
  7849. error := FALSE;
  7850. IF 0 IN phases THEN
  7851. (* first enter all symbols in scope *)
  7852. IF scope IS SyntaxTree.ModuleScope THEN
  7853. (* treat imports first for a module scope, , set default context if necessary *)
  7854. import := scope(SyntaxTree.ModuleScope).firstImport;
  7855. WHILE(import # NIL) DO
  7856. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7857. Register(import, currentScope, FALSE);
  7858. import := import.nextImport;
  7859. END;
  7860. import := scope(SyntaxTree.ModuleScope).firstImport;
  7861. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7862. ResolveSymbol(import);
  7863. import := import.nextImport;
  7864. END;
  7865. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7866. (* enter parameters for a procedure scope *)
  7867. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7868. parameter := procedureType.firstParameter;
  7869. WHILE(parameter # NIL) DO
  7870. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7871. END;
  7872. parameter := procedureType.returnParameter;
  7873. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7874. parameter := procedureType.selfParameter;
  7875. IF parameter # NIL THEN
  7876. Register(parameter, currentScope, FALSE);
  7877. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7878. END;
  7879. ELSIF scope IS SyntaxTree.CellScope THEN
  7880. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7881. IF~skipImplementation THEN
  7882. import := scope(SyntaxTree.CellScope).firstImport;
  7883. WHILE(import # NIL) DO
  7884. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7885. Register(import, currentScope, FALSE);
  7886. import := import.nextImport;
  7887. END;
  7888. import := scope(SyntaxTree.CellScope).firstImport;
  7889. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7890. ResolveSymbol(import);
  7891. import := import.nextImport;
  7892. END;
  7893. END;
  7894. END;
  7895. IF error THEN RETURN END;
  7896. IF skipImplementation THEN
  7897. scope.Clear;
  7898. END;
  7899. (* constants *)
  7900. constant := scope.firstConstant;
  7901. WHILE (constant # NIL) DO
  7902. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7903. END;
  7904. (* type declarations *)
  7905. typeDeclaration := scope.firstTypeDeclaration;
  7906. WHILE (typeDeclaration # NIL) DO
  7907. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7908. END;
  7909. (* variables *)
  7910. variable := scope.firstVariable;
  7911. WHILE (variable # NIL) DO
  7912. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7913. END;
  7914. (* procedures *)
  7915. IF scope.procedures # NIL THEN
  7916. FOR i := 0 TO scope.procedures.Length()-1 DO
  7917. procedure := scope.procedures.GetProcedure(i);
  7918. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7919. IF procedureType.selfParameter = NIL THEN
  7920. scope.AddProcedure(procedure);
  7921. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7922. ELSE
  7923. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7924. IF typeDeclaration = NIL THEN
  7925. Error(procedureType.selfParameter.position, "No such type declaration");
  7926. ELSE
  7927. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7928. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7929. declaredType := typeDeclaration.declaredType;
  7930. IF declaredType IS SyntaxTree.PointerType THEN
  7931. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7932. END;
  7933. IF declaredType IS SyntaxTree.RecordType THEN
  7934. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7935. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7936. ELSE
  7937. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7938. END;
  7939. END;
  7940. END;
  7941. END;
  7942. END;
  7943. END;
  7944. (* now process all symbols without any presumption on the order *)
  7945. symbol := scope.firstSymbol;
  7946. WHILE(symbol # NIL) DO
  7947. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7948. IF (symbol IS SyntaxTree.Procedure) THEN
  7949. IF 1 IN phases THEN
  7950. ResolveSymbol(symbol);
  7951. END;
  7952. ELSE
  7953. IF 0 IN phases THEN
  7954. ResolveSymbol(symbol);
  7955. END;
  7956. END;
  7957. END;
  7958. symbol := symbol.nextSymbol;
  7959. END;
  7960. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7961. symbol := scope.firstSymbol;
  7962. WHILE symbol # NIL DO
  7963. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7964. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7965. pointerFixes.Add(symbol, currentScope);
  7966. END;
  7967. IF ~symbol.type.resolved.isRealtime THEN
  7968. Error(symbol.position,"symbol has no realtime type");
  7969. END;
  7970. END;
  7971. symbol := symbol.nextSymbol
  7972. END;
  7973. END;
  7974. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7975. Error(Basic.invalidPosition,"problems during offset computation in module");
  7976. END;
  7977. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7978. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7979. scope.ownerModule.AddScope(scope);
  7980. END;
  7981. phase := prevPhase;
  7982. currentScope := prevScope;
  7983. error := error OR prevError;
  7984. END Declarations;
  7985. (* nopov *)
  7986. (** check if all operators from one module are compatible to the ones in the other module
  7987. - check if there are not multiple operators with the same signature
  7988. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7989. - check for all operators whose signatures are compatible, whether the return types are compatible
  7990. note that:
  7991. - the return type is not considered to be part of the signature
  7992. - two signatures are considered compatible, if all of the operands are compatible
  7993. **)
  7994. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7995. VAR
  7996. thisOperator, thatOperator: SyntaxTree.Operator;
  7997. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7998. thisParameter, thatParameter: SyntaxTree.Parameter;
  7999. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8000. i: LONGINT;
  8001. BEGIN
  8002. currentScope := thisModuleScope;
  8003. hasError := FALSE;
  8004. (* go through all operators in the other module *)
  8005. thatOperator := thatModuleScope.firstOperator;
  8006. WHILE (thatOperator # NIL) & ~hasError DO
  8007. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8008. (* the other operator is accessible *)
  8009. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8010. (* the other operator is not the conversion operator *)
  8011. (* go through all operators in this module *)
  8012. thisOperator := thisModuleScope.firstOperator;
  8013. WHILE (thisOperator # NIL) & ~hasError DO
  8014. IF thisOperator # thatOperator THEN
  8015. (* the operators are not the same *)
  8016. IF thisOperator.name = thatOperator.name THEN
  8017. (* the operators share the same identifier *)
  8018. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8019. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8020. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8021. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8022. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8023. (* both operators have the same paramter count *)
  8024. thisParameter := thisProcedureType.firstParameter;
  8025. thatParameter := thatProcedureType.firstParameter;
  8026. operandsAreEqual := TRUE;
  8027. operandsAreCompatible := TRUE;
  8028. (* go through all parameters *)
  8029. FOR i := 1 TO thisProcedureType.numberParameters DO
  8030. ASSERT(thatParameter # NIL);
  8031. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8032. operandsAreEqual := FALSE;
  8033. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8034. operandsAreCompatible := FALSE
  8035. END
  8036. END;
  8037. thisParameter := thisParameter.nextParameter;
  8038. thatParameter := thatParameter.nextParameter
  8039. END;
  8040. IF operandsAreEqual THEN
  8041. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8042. hasError := TRUE
  8043. ELSIF operandsAreCompatible THEN
  8044. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8045. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8046. hasError := TRUE
  8047. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8048. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8049. hasError := TRUE
  8050. END
  8051. END
  8052. END
  8053. END
  8054. END;
  8055. thisOperator := thisOperator.nextOperator
  8056. END
  8057. END
  8058. END;
  8059. thatOperator := thatOperator.nextOperator
  8060. END
  8061. END CheckInterOperatorConformity;
  8062. (** check module:
  8063. - check module declaration
  8064. - add context, if necessary
  8065. - remove module from import cache, if necessary
  8066. - check declarations
  8067. - resolve all type fixes
  8068. - check implementation (bodies)
  8069. **)
  8070. PROCEDURE Module*(x: SyntaxTree.Module);
  8071. VAR (* nopov *)
  8072. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8073. BEGIN
  8074. prevScope := currentScope;
  8075. prevIsCellNet := currentIsCellNet;
  8076. module := x;
  8077. ASSERT(x # NIL);
  8078. global := system.globalScope[x.case];
  8079. x.moduleScope.SetGlobalScope(global);
  8080. currentScope := global;
  8081. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8082. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8083. RemoveModuleFromCache(importCache,x);
  8084. Declarations(x.moduleScope, FALSE, {0,1});
  8085. FixTypes();
  8086. IF module.isCellNet THEN
  8087. currentIsCellNet := TRUE;
  8088. modifier := x.modifiers;
  8089. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8090. CheckModifiers(modifier, FALSE);
  8091. END;
  8092. (* nopov *)
  8093. IF ~error THEN
  8094. (* check if operators conform to each other within this module *)
  8095. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8096. (* go through all imports *)
  8097. import := x.moduleScope.firstImport;
  8098. WHILE import # NIL DO
  8099. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8100. (* check if all operators in this module conform to the ones of the imported module *)
  8101. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8102. END;
  8103. import := import.nextImport
  8104. END;
  8105. END;
  8106. Implementations(x);
  8107. module := NIL;
  8108. currentIsCellNet := prevIsCellNet;
  8109. currentScope := prevScope;
  8110. END Module;
  8111. END Checker;
  8112. Warnings*=OBJECT (SyntaxTree.Visitor)
  8113. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8114. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8115. BEGIN
  8116. SELF.diagnostics := diagnostics
  8117. END InitWarnings;
  8118. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8119. BEGIN END VisitPortType;
  8120. (** types *)
  8121. PROCEDURE Type(x: SyntaxTree.Type);
  8122. BEGIN x.Accept(SELF)
  8123. END Type;
  8124. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8125. BEGIN END VisitType;
  8126. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8127. BEGIN END VisitBasicType;
  8128. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8129. BEGIN END VisitCharacterType;
  8130. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8131. BEGIN END VisitIntegerType;
  8132. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8133. BEGIN END VisitFloatType;
  8134. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8135. BEGIN END VisitQualifiedType;
  8136. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8137. BEGIN END VisitStringType;
  8138. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8139. BEGIN END VisitEnumerationType;
  8140. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8141. BEGIN END VisitRangeType;
  8142. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8143. BEGIN
  8144. IF ~(SyntaxTree.Warned IN x.state) THEN
  8145. x.SetState(SyntaxTree.Warned);
  8146. Type(x.arrayBase);
  8147. END;
  8148. END VisitArrayType;
  8149. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8150. BEGIN
  8151. IF ~(SyntaxTree.Warned IN x.state) THEN
  8152. x.SetState(SyntaxTree.Warned);
  8153. Type(x.arrayBase);
  8154. END;
  8155. END VisitMathArrayType;
  8156. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8157. BEGIN
  8158. IF ~(SyntaxTree.Warned IN x.state) THEN
  8159. x.SetState(SyntaxTree.Warned);
  8160. Type(x.pointerBase);
  8161. END;
  8162. END VisitPointerType;
  8163. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8164. BEGIN Scope(x.recordScope) END VisitRecordType;
  8165. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8166. BEGIN Scope(x.cellScope) END VisitCellType;
  8167. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8168. BEGIN END VisitProcedureType;
  8169. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8170. VAR msg: ARRAY 256 OF CHAR;
  8171. BEGIN
  8172. Global.GetSymbolName(x,msg);
  8173. Strings.Append(msg," ");
  8174. Strings.Append(msg,text);
  8175. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8176. END Warning;
  8177. (** symbols *)
  8178. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8179. BEGIN
  8180. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8181. IF ~(x IS SyntaxTree.Parameter) THEN
  8182. Warning(x,"never used");
  8183. END;
  8184. END;
  8185. x.Accept(SELF);
  8186. END Symbol;
  8187. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8188. BEGIN END VisitSymbol;
  8189. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8190. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8191. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8192. BEGIN END VisitConstant;
  8193. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8194. BEGIN END VisitVariable;
  8195. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8196. BEGIN END VisitProperty;
  8197. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8198. BEGIN END VisitParameter;
  8199. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8200. BEGIN
  8201. Scope(x.procedureScope)
  8202. END VisitProcedure;
  8203. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8204. BEGIN END VisitOperator;
  8205. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8206. BEGIN END VisitImport;
  8207. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8208. VAR
  8209. symbol: SyntaxTree.Symbol;
  8210. BEGIN
  8211. symbol := scope.firstSymbol;
  8212. WHILE(symbol # NIL) DO
  8213. Symbol(symbol);
  8214. symbol := symbol.nextSymbol;
  8215. END;
  8216. END Scope;
  8217. PROCEDURE Module*(x: SyntaxTree.Module);
  8218. BEGIN
  8219. SELF.module := x;
  8220. Scope(x.moduleScope);
  8221. END Module;
  8222. END Warnings;
  8223. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8224. BEGIN
  8225. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8226. END IsOberonInline;
  8227. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8228. BEGIN
  8229. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8230. END Resolved;
  8231. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8232. VAR i: LONGINT;
  8233. BEGIN
  8234. i := 1;
  8235. WHILE i < x DO
  8236. i := i *2
  8237. END;
  8238. RETURN i=x
  8239. END PowerOf2;
  8240. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8241. BEGIN
  8242. RETURN
  8243. (scope # NIL) &
  8244. (scope IS SyntaxTree.ModuleScope)
  8245. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8246. OR
  8247. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8248. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8249. END IsCellNetScope;
  8250. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8251. BEGIN
  8252. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8253. END IsCellScope;
  8254. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8255. BEGIN
  8256. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8257. RETURN (scope # NIL) & IsCellNetScope(scope)
  8258. END InCellNetScope;
  8259. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8260. BEGIN
  8261. ASSERT(size MOD system.dataUnit = 0);
  8262. RETURN size DIV system.dataUnit
  8263. END ToMemoryUnits;
  8264. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8265. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8266. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8267. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8268. BEGIN
  8269. IF t = NIL THEN
  8270. RETURN TRUE
  8271. ELSE
  8272. t := t.resolved;
  8273. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8274. END;
  8275. END TypeAllowed;
  8276. BEGIN
  8277. type := type.resolved;
  8278. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8279. RETURN FALSE
  8280. ELSE
  8281. procedureType := type(SyntaxTree.ProcedureType);
  8282. numberParameters := procedureType.numberParameters;
  8283. RETURN
  8284. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8285. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8286. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8287. END;
  8288. END GetProcedureAllowed;
  8289. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8290. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8291. VAR import: SyntaxTree.Import;
  8292. BEGIN
  8293. import := importCache.ImportByModuleName(x.name,x.context);
  8294. IF import # NIL THEN
  8295. importCache.RemoveImporters(x.name,x.context);
  8296. END;
  8297. END RemoveModuleFromCache;
  8298. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8299. (* to <- this assignment compatibility *)
  8300. VAR result: BOOLEAN;
  8301. BEGIN
  8302. IF this= NIL THEN result := (to=NIL)
  8303. ELSIF to=NIL THEN result := FALSE
  8304. ELSE
  8305. (*! will be replaced by this:
  8306. ELSE result := this.CompatibleTo(to.resolved);
  8307. *)
  8308. this := this.resolved; to := to.resolved;
  8309. IF to=SyntaxTree.invalidType THEN result := FALSE
  8310. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8311. ELSIF (to = this) OR (to.SameType(this)) THEN
  8312. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8313. ELSIF to IS SyntaxTree.BasicType THEN
  8314. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8315. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8316. result := this.CompatibleTo(to.resolved)
  8317. ELSE
  8318. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8319. END
  8320. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8321. result := to.sizeInBits >= this.sizeInBits;
  8322. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8323. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8324. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8325. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8326. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8327. result := TRUE;
  8328. ELSIF to IS SyntaxTree.AnyType THEN
  8329. 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);
  8330. ELSIF to IS SyntaxTree.ObjectType THEN
  8331. 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 *) ;
  8332. ELSIF to IS SyntaxTree.ByteType THEN
  8333. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8334. ELSIF to IS SyntaxTree.CharacterType THEN
  8335. result := IsCharacterType(this)
  8336. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8337. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8338. 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
  8339. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8340. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8341. result := TRUE;
  8342. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8343. result := TRUE;
  8344. ELSE
  8345. result := FALSE
  8346. END;
  8347. ELSIF to IS SyntaxTree.PointerType THEN
  8348. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8349. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8350. & (~to.isRealtime OR this.isRealtime);
  8351. ELSIF to IS SyntaxTree.ProcedureType THEN
  8352. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8353. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8354. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8355. ELSIF to IS SyntaxTree.RecordType THEN
  8356. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8357. ELSIF to IS SyntaxTree.ArrayType THEN
  8358. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8359. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8360. ELSIF StaticArrayCompatible(to, this) THEN
  8361. result := TRUE
  8362. ELSE
  8363. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8364. END;
  8365. ELSIF to IS SyntaxTree.MathArrayType THEN
  8366. IF this IS SyntaxTree.MathArrayType THEN
  8367. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8368. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8369. result := TRUE;
  8370. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8371. result := TRUE;
  8372. ELSE
  8373. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8374. END;
  8375. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8376. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8377. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8378. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8379. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8380. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8381. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8382. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8383. ELSE
  8384. result := FALSE
  8385. END;
  8386. (* an array-structured object type is compatible to the type of its array structure *)
  8387. ELSIF IsArrayStructuredObjectType(this) THEN
  8388. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8389. ELSE
  8390. result := FALSE;
  8391. END;
  8392. ELSIF to IS SyntaxTree.StringType THEN
  8393. result := FALSE;
  8394. ELSIF to IS SyntaxTree.EnumerationType THEN
  8395. result := IsEnumerationExtension(this,to);
  8396. ELSIF to IS SyntaxTree.PortType THEN
  8397. result := SameType(to, this)
  8398. ELSE
  8399. Printout.Info("CompatibleTo",to);
  8400. HALT(100); (* implement missing type check *)
  8401. END;
  8402. END;
  8403. RETURN result
  8404. END CompatibleTo;
  8405. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8406. VAR actualBase, formalBase: SyntaxTree.Type;
  8407. BEGIN
  8408. IF SameType(formal,actual) THEN
  8409. RETURN TRUE
  8410. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8411. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8412. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8413. RETURN
  8414. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8415. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8416. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8417. & StaticArrayCompatible(formalBase,actualBase)
  8418. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8419. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8420. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8421. RETURN
  8422. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8423. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8424. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8425. & StaticArrayCompatible(formalBase,actualBase)
  8426. ELSE RETURN FALSE
  8427. END;
  8428. END StaticArrayCompatible;
  8429. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8430. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8431. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8432. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8433. BEGIN
  8434. result := SameType(formal,actual);
  8435. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8436. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8437. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8438. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8439. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8440. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8442. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8443. & TC(formalBase, actualBase);
  8444. END;
  8445. RETURN result
  8446. END TC;
  8447. BEGIN
  8448. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8449. ELSE
  8450. arrayBase := formalType.arrayBase.resolved;
  8451. IF (actualType IS SyntaxTree.StringType) THEN
  8452. result := arrayBase IS SyntaxTree.CharacterType
  8453. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8454. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8455. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8456. result := TC(formalType, actualType);
  8457. ELSE
  8458. result := (arrayBase IS SyntaxTree.ByteType)
  8459. END;
  8460. END;
  8461. RETURN result
  8462. END OpenArrayCompatible;
  8463. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8464. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8465. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8466. BEGIN
  8467. IF actualType IS SyntaxTree.MathArrayType THEN
  8468. actualArray := actualType(SyntaxTree.MathArrayType);
  8469. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8470. (*
  8471. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8472. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8473. *)
  8474. actualBase := ArrayBase(actualType,Infinity);
  8475. formalBase := ArrayBase(formalType,Infinity);
  8476. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8477. ELSE
  8478. (*
  8479. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8480. *)
  8481. formalBase := Resolved(formalType.arrayBase);
  8482. actualBase := Resolved(actualArray.arrayBase);
  8483. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8484. (*
  8485. ARRAY [k] -> ARRAY [n]
  8486. *)
  8487. result := (formalType.staticLength = actualArray.staticLength)
  8488. ELSE
  8489. result := TRUE
  8490. END;
  8491. IF ~result THEN
  8492. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8493. ELSIF actualBase = NIL THEN result := FALSE
  8494. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8495. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8496. ELSE
  8497. result := SameType(formalBase,actualBase)
  8498. END;
  8499. END;
  8500. ELSE
  8501. result := FALSE
  8502. END;
  8503. RETURN result
  8504. END MathArrayCompatible;
  8505. (**
  8506. Math Array Type distance for assignments / parameter passings of the form
  8507. from -> to
  8508. variants:
  8509. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8510. allowed:
  8511. static -> static (& size match)
  8512. static -> open
  8513. static -> tensor
  8514. open -> open
  8515. open -> tensor
  8516. open -> static
  8517. tensor -> tensor
  8518. tensor -> open
  8519. tensor -> static
  8520. **)
  8521. (*! think about the metric here: is form matching more important than element type matching? *)
  8522. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8523. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8524. BEGIN
  8525. fromBase := Resolved(from.arrayBase);
  8526. toBase := Resolved(to.arrayBase);
  8527. i := Infinity;
  8528. IF (from = to) OR (from.SameType(to)) THEN
  8529. i := 0;
  8530. ELSIF (from.form = to.form) THEN
  8531. (* static -> static, open -> open, tensor -> tensor *)
  8532. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8533. IF fromBase = toBase THEN i := 0
  8534. ELSIF toBase = NIL THEN i := 1
  8535. ELSIF toBase.SameType(fromBase) THEN i := 0
  8536. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8537. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8538. ELSE
  8539. i := TypeDistance(system,fromBase, toBase, varpar);
  8540. IF i < Infinity THEN i := i * 5 END;
  8541. END;
  8542. END;
  8543. ELSIF (to.form = SyntaxTree.Static) THEN
  8544. (* forbidden *)
  8545. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8546. (* static -> tensor, open -> tensor, tensor -> open *)
  8547. IF (toBase=fromBase) THEN i := 0;
  8548. ELSIF toBase = NIL THEN i := 1;
  8549. ELSIF toBase.SameType(fromBase) THEN i := 0
  8550. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8551. toBase := ArrayBase(toBase,Infinity);
  8552. IF (fromBase=toBase) THEN i := 0
  8553. ELSIF (toBase = NIL) THEN i:= 1
  8554. ELSIF (fromBase = NIL) THEN i := Infinity;
  8555. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8556. IF i < Infinity THEN i := i * 5 END;
  8557. END;
  8558. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8559. fromBase := ArrayBase(fromBase,Infinity);
  8560. IF (fromBase=toBase) THEN i := 0
  8561. ELSIF (toBase = NIL) THEN i := 1
  8562. ELSIF (fromBase = NIL) THEN i := Infinity;
  8563. ELSIF toBase.SameType(fromBase) THEN i := 0
  8564. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8565. IF i < Infinity THEN i := i * 5 END;
  8566. END;
  8567. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8568. IF i < Infinity THEN i := i * 5 END;
  8569. END;
  8570. IF i # Infinity THEN INC(i,2) END;
  8571. ELSIF (from.form = SyntaxTree.Static) THEN
  8572. (* static -> open *)
  8573. IF (toBase=fromBase) THEN i := 0;
  8574. ELSIF toBase = NIL THEN i := 1
  8575. ELSIF fromBase = NIL THEN i := Infinity
  8576. ELSIF toBase.SameType(fromBase) THEN i := 0
  8577. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8578. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8579. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8580. IF i < Infinity THEN i := i * 5 END;
  8581. END;
  8582. IF i # Infinity THEN INC(i,1) END;
  8583. ELSE HALT(100); (* unknown case *)
  8584. END;
  8585. RETURN i;
  8586. END MathArrayTypeDistance;
  8587. (** compute and return the distance of two array types
  8588. - return the distance of the base types
  8589. **)
  8590. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8591. VAR i: LONGINT;
  8592. BEGIN
  8593. i := Infinity;
  8594. IF from = to THEN
  8595. i := 0
  8596. ELSE
  8597. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8598. (*
  8599. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8600. i := TypeDistance(from.base, to.base);
  8601. IF i >= 0 THEN INC(i) END
  8602. ELSIF (from.mode = open) & (to.mode = open) THEN
  8603. i := TypeDistance(from.base, to.base);
  8604. *)
  8605. END;
  8606. RETURN i
  8607. END ArrayTypeDistance;
  8608. (** compute the signature distance of a procedure and an actual parameter list
  8609. - if any of the parameters are not compatible, the result is infinite
  8610. - add up and return the distance over all parameters
  8611. **)
  8612. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8613. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8614. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8615. BEGIN
  8616. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8617. result := Infinity
  8618. ELSE
  8619. formalParameter := procedureType.firstParameter;
  8620. i := 0;
  8621. result := 0;
  8622. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8623. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8624. actualParameter := actualParameters.GetExpression(i);
  8625. ASSERT(formalParameter.type # NIL);
  8626. IF (actualParameter.type = NIL) THEN distance := Infinity
  8627. ELSE
  8628. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8629. END;
  8630. IF distance = Infinity THEN
  8631. result := Infinity;
  8632. ELSE
  8633. to := formalParameter.type.resolved;
  8634. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8635. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8636. (* already handled varpar *)
  8637. (*
  8638. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8639. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8640. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8641. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8642. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8643. END;
  8644. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8645. result := Infinity
  8646. END;
  8647. *)
  8648. INC(result, distance);
  8649. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8650. INC(result, distance);
  8651. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8652. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8653. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8654. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8655. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8656. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8657. END;
  8658. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8659. result := Infinity
  8660. END;
  8661. ELSE
  8662. result := Infinity
  8663. END;
  8664. ELSE
  8665. INC(result,distance);
  8666. END;
  8667. END;
  8668. (*
  8669. Printout.Info("actual=", actualParameter);
  8670. Printout.Info("formal=", formalParameter);
  8671. TRACE(result);
  8672. *)
  8673. formalParameter := formalParameter.nextParameter; INC(i);
  8674. END;
  8675. END;
  8676. ASSERT(result >= 0);
  8677. RETURN result
  8678. END Distance;
  8679. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8680. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8681. BEGIN
  8682. IF right.numberParameters # (procedureType.numberParameters) THEN
  8683. result := Infinity
  8684. ELSE
  8685. formalParameter := procedureType.firstParameter;
  8686. rightParameter := right.firstParameter;
  8687. i := 0;
  8688. result := 0;
  8689. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8690. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8691. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8692. IF distance = Infinity THEN
  8693. result := Infinity;
  8694. ELSE
  8695. INC(result,distance);
  8696. END;
  8697. formalParameter := formalParameter.nextParameter;
  8698. rightParameter := rightParameter.nextParameter;
  8699. END;
  8700. END;
  8701. ASSERT(result >= 0);
  8702. RETURN result
  8703. END ProcedureTypeDistance;
  8704. (** compute and return the distance between two types, used for computation of signature distance
  8705. from -> to
  8706. **)
  8707. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8708. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8709. BEGIN
  8710. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8711. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8712. END;
  8713. i := Infinity;
  8714. IF from = to THEN
  8715. i := 0
  8716. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8717. ELSIF to.SameType(from) THEN
  8718. i := 0;
  8719. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8720. i := Infinity;
  8721. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8722. i := 10;
  8723. ELSIF (from IS SyntaxTree.StringType) THEN
  8724. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8725. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8726. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8727. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8728. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8729. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8730. i := 1
  8731. ELSIF (from IS SyntaxTree.NilType) THEN
  8732. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8733. (*
  8734. ELSIF (from = NoType) THEN
  8735. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8736. *)
  8737. ELSIF (from IS SyntaxTree.BasicType) THEN
  8738. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8739. IF varpar & (i # 0) THEN i := Infinity END;
  8740. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8741. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8742. ELSIF (from IS SyntaxTree.RecordType) THEN
  8743. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8744. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8745. IF to IS SyntaxTree.MathArrayType THEN
  8746. (*
  8747. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8748. i := Infinity;
  8749. ELSE
  8750. *)
  8751. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8752. (*
  8753. END;
  8754. *)
  8755. END
  8756. ELSIF (from IS SyntaxTree.PointerType) THEN
  8757. ptr := from(SyntaxTree.PointerType);
  8758. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8759. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8760. (* ELSE i := TypeDistance(ptr.base, to); *)
  8761. END
  8762. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8763. IF (to IS SyntaxTree.ProcedureType) THEN
  8764. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8765. END;
  8766. ELSIF (from IS SyntaxTree.PortType) THEN
  8767. IF (to IS SyntaxTree.PortType) THEN
  8768. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8769. i := 0;
  8770. END;
  8771. END;
  8772. (*no procedure test, procedure must be the same*)
  8773. END;
  8774. RETURN i
  8775. END TypeDistance;
  8776. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8777. BEGIN
  8778. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8779. END IsIntegerType;
  8780. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8781. BEGIN
  8782. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8783. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8784. OR IsPointerType(type)
  8785. )
  8786. END IsAddressType;
  8787. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8788. BEGIN
  8789. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8790. END IsSizeType;
  8791. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8792. BEGIN
  8793. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8794. END IsSignedIntegerType;
  8795. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8796. BEGIN
  8797. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8798. END IsUnsignedIntegerType;
  8799. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8800. VAR result: BOOLEAN;
  8801. BEGIN
  8802. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8803. value := x.resolved(SyntaxTree.IntegerValue).value;
  8804. result := TRUE
  8805. ELSE
  8806. result := FALSE
  8807. END;
  8808. RETURN result
  8809. END IsIntegerValue;
  8810. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8811. VAR result: BOOLEAN;
  8812. BEGIN
  8813. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8814. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8815. result := TRUE
  8816. ELSE
  8817. result := FALSE
  8818. END;
  8819. RETURN result
  8820. END IsEnumerationValue;
  8821. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8822. VAR result: BOOLEAN;
  8823. BEGIN
  8824. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8825. value := x.resolved(SyntaxTree.RealValue).value;
  8826. result := TRUE
  8827. ELSE
  8828. result := FALSE
  8829. END;
  8830. RETURN result
  8831. END IsRealValue;
  8832. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8833. VAR result: BOOLEAN;
  8834. BEGIN
  8835. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8836. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8837. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8838. result := TRUE
  8839. ELSE
  8840. result := FALSE
  8841. END;
  8842. RETURN result
  8843. END IsComplexValue;
  8844. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8845. VAR result: BOOLEAN;
  8846. BEGIN
  8847. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8848. value := x.resolved(SyntaxTree.CharacterValue).value;
  8849. result := TRUE
  8850. ELSE
  8851. result := FALSE
  8852. END;
  8853. RETURN result
  8854. END IsCharacterValue;
  8855. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8856. VAR result: BOOLEAN;
  8857. BEGIN
  8858. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8859. value := x.resolved(SyntaxTree.BooleanValue).value;
  8860. result := TRUE
  8861. ELSE
  8862. result := FALSE
  8863. END;
  8864. RETURN result
  8865. END IsBooleanValue;
  8866. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8867. VAR result: BOOLEAN;
  8868. BEGIN
  8869. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8870. value := x.resolved(SyntaxTree.SetValue).value;
  8871. result := TRUE
  8872. ELSE
  8873. result := FALSE
  8874. END;
  8875. RETURN result
  8876. END IsSetValue;
  8877. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8878. VAR result: BOOLEAN;
  8879. BEGIN
  8880. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8881. value := x.resolved(SyntaxTree.StringValue).value;
  8882. result := TRUE
  8883. ELSE
  8884. result := FALSE
  8885. END;
  8886. RETURN result
  8887. END IsStringValue;
  8888. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8889. BEGIN
  8890. x := x.resolved;
  8891. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8892. END Indexable;
  8893. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8894. BEGIN
  8895. RETURN t1.SameType(t2.resolved);
  8896. END SameType;
  8897. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8898. BEGIN
  8899. IF t IS SyntaxTree.MathArrayType THEN
  8900. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8901. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8902. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8903. DEC(max);
  8904. END;
  8905. ELSIF t IS SyntaxTree.ArrayType THEN
  8906. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8907. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8908. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8909. END;
  8910. END;
  8911. RETURN t;
  8912. END ArrayBase;
  8913. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8914. BEGIN
  8915. type := type.resolved;
  8916. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8917. base := type(SyntaxTree.ArrayType).arrayBase;
  8918. RETURN TRUE;
  8919. END;
  8920. RETURN FALSE;
  8921. END IsOpenArray;
  8922. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8923. BEGIN
  8924. type := type.resolved;
  8925. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8926. base := type(SyntaxTree.ArrayType).arrayBase;
  8927. dim := type(SyntaxTree.ArrayType).staticLength;
  8928. RETURN TRUE
  8929. ELSE
  8930. RETURN FALSE
  8931. END;
  8932. END IsStaticArray;
  8933. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8934. BEGIN
  8935. type := type.resolved;
  8936. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8937. base := type(SyntaxTree.ArrayType).arrayBase;
  8938. RETURN TRUE
  8939. ELSE
  8940. RETURN FALSE
  8941. END;
  8942. END IsDynamicArray;
  8943. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8944. VAR i: LONGINT;
  8945. BEGIN
  8946. i := 0;
  8947. t := t.resolved;
  8948. IF t IS SyntaxTree.MathArrayType THEN
  8949. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8950. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8951. END;
  8952. ELSIF t IS SyntaxTree.ArrayType THEN
  8953. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8954. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8955. END;
  8956. END;
  8957. RETURN i
  8958. END Dimension;
  8959. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8960. BEGIN
  8961. RETURN expression.assignable;
  8962. END IsVariable;
  8963. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8964. BEGIN
  8965. IF (symbol IS SyntaxTree.Parameter) THEN
  8966. WITH symbol: SyntaxTree.Parameter DO
  8967. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8968. END;
  8969. ELSE
  8970. RETURN FALSE
  8971. END;
  8972. END IsVariableParameter;
  8973. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8974. VAR result: BOOLEAN;
  8975. BEGIN
  8976. IF type = NIL THEN result := FALSE
  8977. ELSE
  8978. type := type.resolved;
  8979. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8980. END;
  8981. RETURN result
  8982. END IsPointerType;
  8983. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8984. VAR result: BOOLEAN;
  8985. BEGIN
  8986. IF type = NIL THEN result := FALSE
  8987. ELSE
  8988. type := type.resolved;
  8989. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8990. END;
  8991. RETURN result
  8992. END IsUnsafePointer;
  8993. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8994. BEGIN
  8995. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8996. END IsDisposable;
  8997. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8998. VAR result: BOOLEAN;
  8999. BEGIN
  9000. IF type = NIL THEN result := FALSE
  9001. ELSE
  9002. type := type.resolved;
  9003. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9004. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9005. result := result OR (type IS SyntaxTree.ObjectType);
  9006. END;
  9007. RETURN result
  9008. END IsPointerToRecord;
  9009. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9010. VAR result: BOOLEAN;
  9011. BEGIN
  9012. IF type = NIL THEN result := FALSE
  9013. ELSE
  9014. type := type.resolved;
  9015. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9016. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9017. ;
  9018. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9019. result := result OR (type IS SyntaxTree.ObjectType);
  9020. END;
  9021. RETURN result
  9022. END IsPointerToObject;
  9023. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9024. BEGIN
  9025. IF type # NIL THEN
  9026. RETURN type.resolved.hasPointers
  9027. ELSE
  9028. RETURN FALSE
  9029. END;
  9030. END ContainsPointer;
  9031. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9032. BEGIN
  9033. IF type = NIL THEN RETURN FALSE END;
  9034. type := type.resolved;
  9035. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9036. END IsStringType;
  9037. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9038. BEGIN
  9039. IF type = NIL THEN RETURN FALSE END;
  9040. type := type.resolved;
  9041. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9042. END IsCharacterType;
  9043. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9044. BEGIN
  9045. IF type = NIL THEN RETURN FALSE END;
  9046. type := type.resolved;
  9047. RETURN (type IS SyntaxTree.EnumerationType)
  9048. END IsEnumerationType;
  9049. (** cf. section "Type extension (base type)" in the language report **)
  9050. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9051. VAR result: BOOLEAN;
  9052. BEGIN
  9053. ASSERT(base # NIL); ASSERT(extension # NIL);
  9054. base := base.resolved; extension := extension.resolved;
  9055. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9056. result := TRUE;
  9057. ELSE
  9058. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9059. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9060. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9061. END;
  9062. WHILE (extension # NIL) & (extension # base) DO
  9063. IF extension IS SyntaxTree.RecordType THEN
  9064. extension := extension(SyntaxTree.RecordType).baseType;
  9065. IF (extension # NIL) THEN extension := extension.resolved END;
  9066. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9067. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9068. END;
  9069. ELSE extension := NIL;
  9070. END;
  9071. END;
  9072. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9073. END;
  9074. RETURN result
  9075. END IsTypeExtension;
  9076. (** check if base is the base enumeration type of extension **)
  9077. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9078. BEGIN
  9079. base := base.resolved; extension := extension.resolved;
  9080. WHILE (extension # NIL) & (extension # base) DO
  9081. IF extension IS SyntaxTree.EnumerationType THEN
  9082. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9083. IF extension # NIL THEN extension := extension.resolved END;
  9084. ELSE
  9085. extension := NIL
  9086. END;
  9087. END;
  9088. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9089. END IsEnumerationExtension;
  9090. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9091. BEGIN
  9092. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9093. RETURN TRUE
  9094. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9095. RETURN TRUE
  9096. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9097. RETURN TRUE
  9098. ELSE
  9099. RETURN FALSE
  9100. END
  9101. END IsCallable;
  9102. (** compute and return the distance of two record types
  9103. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9104. **)
  9105. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9106. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9107. BEGIN
  9108. i := 0;
  9109. WHILE (from # NIL) & (from # to) DO
  9110. baseType := from.baseType;
  9111. IF (baseType # NIL) THEN
  9112. baseType := baseType.resolved;
  9113. IF baseType IS SyntaxTree.PointerType THEN
  9114. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9115. END;
  9116. IF baseType IS SyntaxTree.RecordType THEN
  9117. from := baseType(SyntaxTree.RecordType);
  9118. ELSE
  9119. from := NIL;
  9120. END;
  9121. ELSE
  9122. from := NIL
  9123. END;
  9124. INC(i)
  9125. END;
  9126. IF from = NIL THEN i := Infinity END;
  9127. RETURN i
  9128. END RecordTypeDistance;
  9129. (** compute and return the distance of two pointer types **)
  9130. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9131. BEGIN
  9132. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9133. RETURN Infinity;
  9134. ELSE
  9135. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9136. END;
  9137. END PointerTypeDistance;
  9138. (** check if expression contains a symbol designator pointing to a type declaration.
  9139. - if so then enter type declaration into typeDeclaration and return true else return false
  9140. **)
  9141. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9142. VAR result: BOOLEAN;
  9143. BEGIN
  9144. result := FALSE;
  9145. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9146. result := TRUE;
  9147. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9148. END;
  9149. RETURN result
  9150. END IsTypeDesignator;
  9151. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9152. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9153. VAR result: BOOLEAN;
  9154. BEGIN
  9155. type := type.resolved;
  9156. IF type IS SyntaxTree.PointerType THEN
  9157. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9158. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9159. result := TRUE
  9160. ELSE
  9161. result := type IS SyntaxTree.RecordType
  9162. END;
  9163. RETURN result
  9164. END IsExtensibleType;
  9165. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9166. BEGIN
  9167. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9168. (d IS SyntaxTree.SymbolDesignator) &
  9169. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9170. OR
  9171. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9172. END IsUnextensibleRecord;
  9173. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9174. BEGIN
  9175. IF IsUnextensibleRecord(d) THEN
  9176. RETURN FALSE
  9177. ELSE RETURN IsExtensibleType(d.type.resolved)
  9178. END;
  9179. END IsExtensibleDesignator;
  9180. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9181. BEGIN
  9182. type := type.resolved;
  9183. IF (type IS SyntaxTree.PointerType) THEN
  9184. RETURN TRUE
  9185. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9186. RETURN TRUE
  9187. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9188. RETURN TRUE
  9189. ELSIF (type IS SyntaxTree.BasicType) THEN
  9190. RETURN TRUE
  9191. END;
  9192. RETURN FALSE
  9193. END IsBasicType;
  9194. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9195. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9196. BEGIN
  9197. baseType := record.baseType;
  9198. IF (baseType # NIL) THEN
  9199. baseType := baseType.resolved;
  9200. IF (baseType IS SyntaxTree.PointerType) THEN
  9201. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9202. END;
  9203. END;
  9204. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9205. recordType := baseType(SyntaxTree.RecordType);
  9206. ELSE
  9207. recordType := NIL;
  9208. END;
  9209. RETURN recordType
  9210. END RecordBase;
  9211. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9212. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9213. BEGIN
  9214. baseRecord := RecordBase(scope.ownerRecord);
  9215. IF baseRecord = NIL THEN RETURN NIL END;
  9216. scope := baseRecord.recordScope;
  9217. procedureType := procedure.type.resolved;
  9218. IF procedure IS SyntaxTree.Operator THEN
  9219. operator := scope.firstOperator;
  9220. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9221. (*
  9222. Printout.Info("not same ",procedureType);
  9223. Printout.Info("with ",operator.type);
  9224. *)
  9225. operator := operator.nextOperator;
  9226. END;
  9227. super := operator;
  9228. ELSE
  9229. super := scope.firstProcedure;
  9230. WHILE (super # NIL) & (super.name # procedure.name) DO
  9231. super := super.nextProcedure;
  9232. END;
  9233. END;
  9234. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9235. RETURN super
  9236. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9237. RETURN super
  9238. ELSE
  9239. RETURN FindSuperProcedure(scope,procedure);
  9240. END;
  9241. END FindSuperProcedure;
  9242. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9243. VAR procedure: SyntaxTree.Procedure;
  9244. BEGIN
  9245. procedure := record.recordScope.constructor;
  9246. IF procedure = NIL THEN
  9247. record := RecordBase(record);
  9248. IF record # NIL THEN
  9249. procedure := GetConstructor(record)
  9250. END;
  9251. END;
  9252. RETURN procedure;
  9253. END GetConstructor;
  9254. (* enter a case into a list of cases in a sorted way and check for collision *)
  9255. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9256. VAR prev,this,new: SyntaxTree.CaseConstant;
  9257. BEGIN
  9258. this := root;
  9259. prev := NIL;
  9260. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9261. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9262. RETURN FALSE
  9263. ELSE
  9264. IF (this # NIL) & (this.min = max+1) THEN
  9265. this.min := min
  9266. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9267. prev.max := min
  9268. ELSE
  9269. NEW(new); new.min := min; new.max := max;
  9270. new.next := this;
  9271. IF prev = NIL THEN
  9272. root := new;
  9273. ELSE
  9274. prev.next := new
  9275. END
  9276. END;
  9277. RETURN TRUE
  9278. END;
  9279. END EnterCase;
  9280. (** generate and return a new checker object, errors are entered into diagnostics **)
  9281. 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;
  9282. VAR checker: Checker;
  9283. BEGIN
  9284. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9285. RETURN checker
  9286. END NewChecker;
  9287. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9288. VAR warnings: Warnings;
  9289. BEGIN
  9290. NEW(warnings, diagnostics); RETURN warnings;
  9291. END NewWarnings;
  9292. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9293. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9294. END IsRangeType;
  9295. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9296. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9297. END IsMathArrayType;
  9298. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9299. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9300. END IsArrayType;
  9301. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9302. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9303. END IsComplexType;
  9304. (** if a type is an array-structured object type *)
  9305. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9306. VAR recordType: SyntaxTree.RecordType;
  9307. BEGIN
  9308. IF type = NIL THEN
  9309. RETURN FALSE
  9310. ELSE
  9311. type := type.resolved;
  9312. IF type IS SyntaxTree.PointerType THEN
  9313. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9314. IF type IS SyntaxTree.RecordType THEN
  9315. recordType := type(SyntaxTree.RecordType);
  9316. RETURN recordType.isObject & recordType.HasArrayStructure()
  9317. ELSE
  9318. RETURN FALSE
  9319. END
  9320. ELSE
  9321. RETURN FALSE
  9322. END
  9323. END
  9324. END IsArrayStructuredObjectType;
  9325. (** the math array structure of a type
  9326. - for math arrays: the array itself
  9327. - for pointers: the math array structure of the pointer base
  9328. - for array-structured object types: the underlying structure
  9329. - for non-math arrays and all other types: NIL
  9330. **)
  9331. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9332. VAR
  9333. result: SyntaxTree.MathArrayType;
  9334. BEGIN
  9335. IF type = NIL THEN
  9336. result := NIL
  9337. ELSE
  9338. type := type.resolved;
  9339. IF type IS SyntaxTree.PointerType THEN
  9340. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9341. END;
  9342. IF type IS SyntaxTree.MathArrayType THEN
  9343. result := type(SyntaxTree.MathArrayType)
  9344. ELSIF type IS SyntaxTree.RecordType THEN
  9345. result := type(SyntaxTree.RecordType).arrayStructure
  9346. ELSE
  9347. result := NIL
  9348. END
  9349. END;
  9350. RETURN result
  9351. END MathArrayStructureOfType;
  9352. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9353. VAR
  9354. result: BOOLEAN;
  9355. rangeExpression: SyntaxTree.RangeExpression;
  9356. BEGIN
  9357. IF x IS SyntaxTree.RangeExpression THEN
  9358. rangeExpression := x(SyntaxTree.RangeExpression);
  9359. result := TRUE;
  9360. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9361. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9362. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9363. ELSE
  9364. result := FALSE
  9365. END;
  9366. RETURN result
  9367. END IsStaticRange;
  9368. (** whether a type is a math array of tensor form **)
  9369. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9370. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9371. END IsTensor;
  9372. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9373. BEGIN
  9374. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9375. length := type(SyntaxTree.MathArrayType).staticLength;
  9376. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9377. RETURN TRUE
  9378. ELSE
  9379. RETURN FALSE
  9380. END;
  9381. END IsStaticMathArray;
  9382. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9383. BEGIN
  9384. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9385. END SymbolHasAddress;
  9386. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9387. BEGIN
  9388. RETURN
  9389. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9390. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9391. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9392. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9393. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9394. ;
  9395. END HasAddress;
  9396. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9397. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9398. BEGIN
  9399. IF (e IS SyntaxTree.Designator) THEN
  9400. d := e(SyntaxTree.Designator);
  9401. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9402. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9403. e := d.left;
  9404. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9405. END;
  9406. IF d # NIL THEN
  9407. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9408. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9409. END;
  9410. END;
  9411. RETURN FALSE;
  9412. END IsLocalVariable;
  9413. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9414. BEGIN
  9415. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9416. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9417. ELSE
  9418. RETURN TRUE
  9419. END;
  9420. END IsStaticProcedure;
  9421. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9422. CONST OptimizeMethodTable = FALSE;
  9423. BEGIN
  9424. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9425. END InMethodTable;
  9426. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9427. BEGIN
  9428. IF type = NIL THEN RETURN FALSE
  9429. ELSE
  9430. type := type.resolved;
  9431. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9432. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9433. END
  9434. END ReturnedAsParameter;
  9435. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9436. BEGIN
  9437. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9438. END StructuredReturnType;
  9439. END FoxSemanticChecker.
  9440. SystemTools.FreeDownTo FoxSemanticChecker ~