FoxSemanticChecker.Mod 405 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269
  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. inConversion: LONGINT;
  78. (* temporary variables for the visitors
  79. they replace variables on a stack during use of the visitor pattern and may only be
  80. - set in AcceptXXX procedures
  81. - set and read in ResolveXXX procedures
  82. *)
  83. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  84. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  85. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  86. currentScope-: SyntaxTree.Scope;
  87. currentIsRealtime: BOOLEAN;
  88. currentIsUnreachable: BOOLEAN;
  89. currentIsCellNet: BOOLEAN;
  90. currentIsBodyProcedure: BOOLEAN;
  91. currentIsExclusive: BOOLEAN;
  92. global: SyntaxTree.ModuleScope;
  93. withEntries: WithEntry;
  94. activeCellsStatement: BOOLEAN;
  95. replacements*: Replacement;
  96. cellsAreObjects: BOOLEAN;
  97. variableAccessed: BOOLEAN;
  98. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  99. BEGIN
  100. SELF.diagnostics := diagnostics;
  101. SELF.useDarwinCCalls := useDarwinCCalls;
  102. SELF.cooperative := cooperative;
  103. SELF.system := system;
  104. SELF.symbolFileFormat := symbolFileFormat;
  105. error := FALSE;
  106. NEW(typeFixes);
  107. NEW(pointerFixes);
  108. resolvedType := NIL;
  109. resolvedExpression := NIL;
  110. resolvedStatement := NIL;
  111. currentScope := NIL;
  112. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  113. SELF.importCache := importCache;
  114. arrayBaseImported := FALSE;
  115. complexNumbersImported := FALSE;
  116. SELF.VerboseErrorMessage := verboseErrorMessage;
  117. global := NIL;
  118. phase := UndefinedPhase;
  119. currentIsRealtime := FALSE;
  120. currentIsUnreachable := FALSE;
  121. currentIsCellNet := FALSE;
  122. currentIsBodyProcedure := FALSE;
  123. currentIsExclusive := FALSE;
  124. withEntries := NIL;
  125. SELF.cellsAreObjects := system.cellsAreObjects;
  126. COPY(backend, backendName);
  127. inConversion := 0;
  128. END InitChecker;
  129. (** report error **)
  130. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  131. VAR errModule: SyntaxTree.Module;
  132. BEGIN
  133. ASSERT(currentScope # NIL);
  134. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  135. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  136. error := TRUE;
  137. END Error;
  138. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  139. VAR errModule: SyntaxTree.Module;
  140. BEGIN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  143. END Warning;
  144. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  145. VAR errorMessage: ARRAY 256 OF CHAR;
  146. BEGIN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  149. error := TRUE;
  150. END ErrorSS;
  151. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  152. VAR msg, msg2: ARRAY 256 OF CHAR;
  153. BEGIN
  154. COPY(msg1, msg);
  155. Strings.Append(msg, " = ");
  156. Basic.GetString(s, msg2);
  157. Strings.Append(msg, msg2);
  158. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  159. END InfoSS;
  160. (*** symbol lookup ***)
  161. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  162. **)
  163. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  164. VAR
  165. scope,baseScope: SyntaxTree.Scope;
  166. symbol, s: SyntaxTree.Symbol;
  167. ownerRecord,base: SyntaxTree.RecordType;
  168. BEGIN
  169. scope := inScope;
  170. symbol := NIL;
  171. WHILE (scope # NIL) & (symbol = NIL) DO
  172. symbol := scope.FindSymbol(name);
  173. s := NIL;
  174. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  175. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  177. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  178. END;
  179. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  180. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  181. ELSE
  182. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  183. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  184. symbol.MarkUsed;
  185. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  186. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  187. base := RecordBase(ownerRecord);
  188. IF (base # NIL) THEN
  189. baseScope := base.recordScope;
  190. symbol := Find(baseScope,name,FALSE);
  191. ELSE
  192. symbol := NIL;
  193. END;
  194. ELSE
  195. symbol := NIL;
  196. END;
  197. END;
  198. END;
  199. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  200. END;
  201. IF (symbol # NIL) THEN
  202. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  203. ASSERT(phase = DeclarationPhase);
  204. ResolveSymbol(symbol)
  205. END;
  206. symbol.MarkUsed;
  207. END;
  208. RETURN symbol
  209. END Find;
  210. (*** types ***)
  211. (** find type declaration with name qualifiedIdentifier and return resolved type
  212. - check qualified identifier prefix, set scope to module scope if appropriate
  213. - check suffix in scope
  214. **)
  215. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  216. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  217. BEGIN
  218. result := NIL;
  219. prevScope := currentScope;
  220. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  221. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  222. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  223. IF symbol(SyntaxTree.Import).module = NIL THEN
  224. Error(qualifiedIdentifier.position,"module not loaded");
  225. result := SyntaxTree.invalidType;
  226. symbol := NIL;
  227. ELSE
  228. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  229. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  230. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  231. IF VerboseErrorMessage THEN
  232. Printout.Info("scope", currentScope);
  233. Printout.Info("symbol", symbol);
  234. END;
  235. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  236. END;
  237. END;
  238. ELSE
  239. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  240. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  241. symbol := NIL;
  242. END;
  243. ELSE
  244. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  245. IF symbol = NIL THEN
  246. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  247. IF VerboseErrorMessage THEN
  248. Printout.Info("Qualident",qualifiedIdentifier);
  249. Printout.Info("in scope",currentScope) ;
  250. END;
  251. END;
  252. END;
  253. IF symbol = NIL THEN (* error already handled *)
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  257. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  258. typeDeclaration := NIL;
  259. result := SyntaxTree.invalidType;
  260. ELSE
  261. currentScope := symbol.scope;
  262. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  263. result := ResolveType(typeDeclaration.declaredType);
  264. symbol.MarkUsed;
  265. ASSERT(result # NIL);
  266. END;
  267. currentScope := prevScope;
  268. RETURN result
  269. END ResolveNamedType;
  270. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  271. If node is currently being resolved then emit a cyclic definition error.
  272. Return TRUE only if node is fully resolved.
  273. **)
  274. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  275. VAR result: BOOLEAN;
  276. BEGIN
  277. IF SyntaxTree.Resolved IN x.state THEN
  278. result := FALSE
  279. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  280. Error(x.position,"cyclic definition");
  281. result := FALSE;
  282. ELSE
  283. result := TRUE;
  284. x.SetState(SyntaxTree.BeingResolved)
  285. END;
  286. RETURN result
  287. END TypeNeedsResolution;
  288. (** Return invalid type if x is currently being resolved, return x otherwise**)
  289. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  290. BEGIN
  291. IF SyntaxTree.Resolved IN x.state THEN
  292. RETURN x
  293. ELSE
  294. RETURN SyntaxTree.invalidType
  295. END;
  296. END ResolvedType;
  297. PROCEDURE VisitType*(x: SyntaxTree.Type);
  298. BEGIN
  299. ASSERT(x = SyntaxTree.invalidType);
  300. END VisitType;
  301. (** resolve basic type **)
  302. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  303. BEGIN
  304. IF TypeNeedsResolution(x) THEN
  305. x.SetState(SyntaxTree.Resolved);
  306. END;
  307. resolvedType := ResolvedType(x)
  308. END VisitBasicType;
  309. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  310. BEGIN
  311. VisitBasicType(x);
  312. END VisitByteType;
  313. (** resolve character type **)
  314. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitCharacterType;
  318. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitBooleanType;
  322. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitSetType;
  326. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitAddressType;
  330. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitSizeType;
  334. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitAnyType;
  338. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitObjectType;
  342. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  343. BEGIN
  344. VisitBasicType(x);
  345. END VisitNilType;
  346. (** resolve integer type **)
  347. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitIntegerType;
  351. (** resolve real type **)
  352. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitFloatType;
  356. (** resolve complex type **)
  357. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitComplexType;
  361. (**
  362. resolve string type: nothing to be done
  363. **)
  364. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  365. BEGIN
  366. IF TypeNeedsResolution(x) THEN
  367. x.SetState(SyntaxTree.Resolved);
  368. END;
  369. resolvedType := ResolvedType(x)
  370. END VisitStringType;
  371. (**
  372. check enumeration scope: enter symbols and check for duplicate names
  373. **)
  374. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  375. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  376. BEGIN
  377. prevScope := currentScope;
  378. currentScope := x;
  379. e := x.firstConstant;
  380. nextHighest := highest;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  408. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: Basic.Integer;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position, "base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  479. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  480. ELSE
  481. x.SetArrayBase(pointerType);
  482. END;
  483. END;
  484. IF x.length # NIL THEN
  485. variableAccessed := FALSE;
  486. e := ResolveExpression(x.length);
  487. IF (e.resolved = NIL) THEN
  488. IF variableAccessed THEN
  489. Error(e.position, "forbidden variable access");
  490. END;
  491. IF CheckSizeType(e) THEN
  492. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  493. END;
  494. ELSE
  495. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  496. END;
  497. END;
  498. IF arrayBase IS SyntaxTree.ArrayType THEN
  499. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  500. Error(x.position,"forbidden static array of dynamic array");
  501. END;
  502. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  503. Error(x.position,"forbidden array mixed form");
  504. END;
  505. x.SetHasPointers(arrayBase.hasPointers);
  506. x.SetState(SyntaxTree.Resolved);
  507. END;
  508. resolvedType := ResolvedType(x);
  509. END VisitArrayType;
  510. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  511. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  512. BEGIN
  513. module := currentScope.ownerModule;
  514. IF module.name=name THEN
  515. (* do nothing *)
  516. ELSE
  517. moduleScope := module.moduleScope;
  518. import := moduleScope.FindImport(name);
  519. IF import = NIL THEN
  520. import := SyntaxTree.NewImport(position,name,name,TRUE);
  521. moduleScope.AddImport(import);
  522. Register(import,moduleScope,FALSE);
  523. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  524. VisitImport(import);
  525. ELSIF import.direct=FALSE THEN
  526. import.SetScope(module.moduleScope);
  527. import.SetDirect(TRUE);
  528. IF moduleScope.FindSymbol(import.name) = NIL THEN
  529. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  530. duplicate.SetContext(import.context);
  531. duplicate.SetModule(import.module);
  532. Register(duplicate,moduleScope,TRUE);
  533. VisitImport(duplicate);
  534. END;
  535. END;
  536. import.MarkUsed
  537. END;
  538. END ImportModule;
  539. (**
  540. resolve math array type
  541. - check base type
  542. - open math array of array forbidden
  543. - math array of tensor forbidden
  544. - static array of open array forbidden
  545. **)
  546. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  547. VAR arrayBase: SyntaxTree.Type; modifiers: SyntaxTree.Modifier; position: SyntaxTree.Position;
  548. BEGIN
  549. IF TypeNeedsResolution(x) THEN
  550. x.SetArrayBase(ResolveType(x.arrayBase));
  551. IF x.length # NIL THEN
  552. x.SetLength(ConstantIntegerGeq0(x.length));
  553. END;
  554. arrayBase := x.arrayBase;
  555. IF arrayBase # NIL THEN
  556. arrayBase := arrayBase.resolved;
  557. IF arrayBase = SyntaxTree.invalidType THEN
  558. (* error already handled *)
  559. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  560. Error(x.position,"forbidden array mixed form");
  561. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  562. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  563. Error(x.position,"forbidden Tensor Array mix")
  564. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  565. Error(x.position,"forbidden static array of dynamic array")
  566. END;
  567. END;
  568. IF x.form = SyntaxTree.Static THEN
  569. x.SetIncrement(system.SizeOf(arrayBase));
  570. END;
  571. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  572. END;
  573. modifiers := x.modifiers;
  574. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  575. CheckModifiers(modifiers, TRUE);
  576. x.SetState(SyntaxTree.Resolved);
  577. END;
  578. resolvedType := ResolvedType(x);
  579. END VisitMathArrayType;
  580. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  581. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  582. (1) Rec = RECORD ... END; Ptr <---> Rec
  583. Ptr = POINTER TO Rec; ^ |
  584. | |
  585. TypeDesc TypeDesc
  586. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  587. ^ /
  588. | /
  589. TypeDesc <-- /
  590. *)
  591. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  592. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  593. BEGIN
  594. Strings.IntToStr(x.position.start,number);
  595. COPY(prefix,name);
  596. Strings.Append(name,"@");
  597. Strings.Append(name,number);
  598. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  599. typeDeclaration.SetDeclaredType(x);
  600. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  601. x.SetTypeDeclaration(typeDeclaration);
  602. currentScope.AddTypeDeclaration(typeDeclaration);
  603. typeDeclaration.SetScope(currentScope);
  604. END AnonymousTypeDeclaration;
  605. (**
  606. deferred pointer type resolving
  607. - resolve base type
  608. - check that base type is a record or array type
  609. - if error then set base type to invalid type
  610. **)
  611. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  612. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  613. BEGIN
  614. ASSERT(type.pointerBase # NIL);
  615. position := type.pointerBase.position;
  616. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  617. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  618. (* not for pointers, a type is needed for the records only
  619. IF type.typeDeclaration = NIL THEN
  620. AnonymousTypeDeclaration(type);
  621. END;
  622. *)
  623. END;
  624. resolved := ResolveType(type.pointerBase);
  625. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  626. type.SetPointerBase(resolved);
  627. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  628. recordType := resolved.resolved(SyntaxTree.RecordType);
  629. IF recordType.isObject & (recordType.baseType # NIL) THEN
  630. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  631. Error(position, "base type of object must be a realtime object");
  632. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  633. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  634. END;
  635. END;
  636. END;
  637. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  638. Error(position, "realtime object contains references to non-realtime objects");
  639. END;
  640. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  641. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  642. Error(position, "forbidden unsafe at static array");
  643. ELS
  644. *)
  645. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  646. Error(position, "forbidden unsafe at multidimensional array");
  647. END;
  648. END;
  649. ELSE
  650. Error(position, "forbidden pointer base type");
  651. type.SetPointerBase(SyntaxTree.invalidType)
  652. END
  653. END FixPointerType;
  654. (**
  655. resolve pointer type
  656. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  657. **)
  658. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  659. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  660. modifiers: SyntaxTree.Modifier; position: Position;
  661. BEGIN
  662. IF TypeNeedsResolution(x) THEN
  663. modifiers := x.modifiers;
  664. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  665. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  666. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  667. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  668. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  669. (* inheritance cycle check
  670. example:
  671. A=POINTER TO RECORD(B) END;
  672. B=POINTER TO RECORD(A) END;
  673. *)
  674. IF x.pointerBase IS SyntaxTree.RecordType THEN
  675. recordType := x.pointerBase(SyntaxTree.RecordType);
  676. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  677. recordBaseType := ResolveType(recordType.baseType);
  678. recordType.SetBaseType(recordBaseType);
  679. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  680. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  681. END;
  682. CheckModifiers(modifiers, TRUE);
  683. typeFixes.Add(x,currentScope);
  684. x.SetState(SyntaxTree.Resolved);
  685. END;
  686. resolvedType := ResolvedType(x)
  687. END VisitPointerType;
  688. (**
  689. resolve port type
  690. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  691. **)
  692. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  693. VAR value: Basic.Integer;
  694. BEGIN
  695. IF TypeNeedsResolution(x) THEN
  696. x.SetCellsAreObjects(cellsAreObjects);
  697. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  698. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  699. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  700. ELSE
  701. x.SetSize(system.SizeOf(system.longintType));
  702. END;
  703. x.SetState(SyntaxTree.Resolved);
  704. END;
  705. resolvedType := ResolvedType(x)
  706. END VisitPortType;
  707. (**
  708. deferred procedure type resolving
  709. - resolve return type
  710. - traverse and resolve parameters
  711. **)
  712. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  713. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  714. BEGIN
  715. resolved := ResolveType(procedureType.returnType);
  716. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  717. Error(procedureType.position,"forbidden open array return type");
  718. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  719. Error(procedureType.position,"procedure with return type does not return");
  720. END;
  721. procedureType.SetReturnType(resolved);
  722. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  723. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  724. parameter.SetType(procedureType.returnType);
  725. parameter.SetAccess(SyntaxTree.Hidden);
  726. parameter.SetUntraced(procedureType.hasUntracedReturn);
  727. VisitParameter(parameter);
  728. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  729. END;
  730. (* process parameters *)
  731. parameter :=procedureType.firstParameter;
  732. WHILE (parameter # NIL) DO
  733. VisitParameter(parameter);
  734. parameter := parameter.nextParameter;
  735. END;
  736. parameter := procedureType.selfParameter;
  737. IF parameter # NIL THEN
  738. VisitParameter(parameter)
  739. END;
  740. END FixProcedureType;
  741. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  742. VAR prev,this: SyntaxTree.Modifier;
  743. BEGIN
  744. this := modifiers;prev := NIL;
  745. WHILE (this # NIL) & (this.identifier # name) DO
  746. prev := this; this := this.nextModifier;
  747. END;
  748. IF this # NIL THEN
  749. IF this.expression # NIL THEN
  750. Error(this.position,"unexpected expression");
  751. END;
  752. this.Resolved;
  753. position := this.position;
  754. RETURN TRUE
  755. ELSE
  756. RETURN FALSE
  757. END;
  758. END HasFlag;
  759. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  760. VAR prev,this: SyntaxTree.Modifier;
  761. BEGIN
  762. this := modifiers;prev := NIL;
  763. WHILE (this # NIL) & (this.identifier # name) DO
  764. prev := this; this := this.nextModifier;
  765. END;
  766. IF this # NIL THEN
  767. IF this.expression = NIL THEN
  768. Error(this.position,"expected expression value");
  769. ELSE
  770. this.SetExpression(ConstantExpression(this.expression));
  771. IF CheckIntegerValue(this.expression,value) THEN END;
  772. END;
  773. this.Resolved;
  774. position := this.position;
  775. RETURN TRUE
  776. ELSE RETURN FALSE
  777. END;
  778. END HasValue;
  779. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  780. VAR prev,this: SyntaxTree.Modifier;
  781. BEGIN
  782. this := modifiers;prev := NIL;
  783. WHILE (this # NIL) & (this.identifier # name) DO
  784. prev := this; this := this.nextModifier;
  785. END;
  786. IF this # NIL THEN
  787. IF this.expression = NIL THEN
  788. Error(this.position,"expected expression value");
  789. ELSE
  790. this.SetExpression(ConstantExpression(this.expression));
  791. IF CheckStringValue(this.expression,value) THEN END;
  792. END;
  793. this.Resolved;
  794. position := this.position;
  795. RETURN TRUE
  796. ELSE RETURN FALSE
  797. END;
  798. END HasStringValue;
  799. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  800. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  801. BEGIN
  802. IF cellsAreObjects THEN RETURN FALSE END;
  803. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  804. IF svalue = "A2" THEN
  805. RETURN TRUE
  806. END;
  807. END;
  808. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  809. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  810. END;
  811. RETURN FALSE;
  812. (*
  813. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  814. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  815. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  816. IF svalue[0] = "~" THEN
  817. Strings.TrimLeft(svalue, "~");
  818. IF svalue = backendName THEN
  819. RETURN TRUE;
  820. END;
  821. ELSIF svalue # backendName THEN
  822. RETURN TRUE;
  823. END;
  824. END;
  825. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  826. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  827. END;
  828. RETURN FALSE;
  829. *)
  830. END SkipImplementation;
  831. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  832. VAR this: SyntaxTree.Modifier;
  833. BEGIN
  834. this := modifiers;
  835. WHILE this # NIL DO
  836. IF ~this.resolved THEN
  837. IF checkUse THEN
  838. Error(this.position,"unexpected modifier");
  839. ELSE
  840. this.SetExpression(ResolveExpression(this.expression));
  841. this.Resolved;
  842. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  843. END;
  844. END;
  845. this := this.nextModifier
  846. END;
  847. END CheckModifiers;
  848. (**
  849. resolve procedure type
  850. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  851. **)
  852. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  853. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  854. BEGIN
  855. IF TypeNeedsResolution(procedureType) THEN
  856. modifiers := procedureType.modifiers;
  857. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  858. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  859. procedureType.SetInterrupt(TRUE);
  860. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  861. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  862. IF useDarwinCCalls THEN (*fld*)
  863. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  864. ELSE
  865. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  866. END
  867. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  868. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  869. Error(position, "undefined platform calling convention");
  870. ELSE
  871. procedureType.SetCallingConvention(system.platformCallingConvention);
  872. END;
  873. END;
  874. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  875. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  876. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  877. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  878. CheckModifiers(modifiers, TRUE);
  879. modifiers := procedureType.returnTypeModifiers;
  880. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  881. CheckModifiers(modifiers, TRUE);
  882. typeFixes.Add(procedureType,currentScope);
  883. procedureType.SetHasPointers(procedureType.isDelegate);
  884. procedureType.SetState(SyntaxTree.Resolved);
  885. END;
  886. resolvedType := ResolvedType(procedureType)
  887. END VisitProcedureType;
  888. (** check and resolve record type
  889. - check base type: must be record, math array or array-structured object type
  890. - check declarations
  891. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  892. **)
  893. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  894. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  895. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  896. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  897. hasPointers: BOOLEAN;
  898. modifiers: SyntaxTree.Modifier;
  899. value: Basic.Integer;
  900. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  901. BEGIN
  902. type := type.resolved;
  903. IF (type IS SyntaxTree.PointerType) &
  904. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  905. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  906. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  907. RETURN TRUE
  908. ELSE
  909. RETURN FALSE
  910. END;
  911. END IsPointerToRecord;
  912. BEGIN
  913. IF TypeNeedsResolution(x) THEN
  914. hasPointers := FALSE;
  915. modifiers := x.modifiers;
  916. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  917. END;
  918. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  919. CheckModifiers(modifiers, TRUE);
  920. IF x.baseType # NIL THEN
  921. position := x.baseType.position;
  922. baseType := ResolveType(x.baseType);
  923. resolved := baseType.resolved;
  924. hasPointers := hasPointers OR resolved.hasPointers;
  925. IF x.isObject THEN (* object *)
  926. ASSERT(x.pointerType # NIL);
  927. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  928. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  929. baseType := NIL
  930. ELSIF IsPointerToRecord(resolved,recordType) THEN
  931. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  932. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  933. ELSE
  934. Error(position,"object does not extend pointer to record, object or math array ")
  935. END;
  936. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  937. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  938. ELSIF IsPointerToRecord(resolved,recordType) THEN
  939. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  940. ELSIF resolved IS SyntaxTree.RecordType THEN
  941. ELSE
  942. Error(position,"pointer to record does not extend pointer to record or record")
  943. END;
  944. ELSE
  945. IF resolved IS SyntaxTree.RecordType THEN
  946. ELSE
  947. Error(position,"record does not extend record")
  948. END;
  949. END;
  950. x.SetBaseType(baseType);
  951. IF x.Level() > 15 THEN
  952. Error(position, "record/object inheritance level too high");
  953. (* note:
  954. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  955. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  956. inheritance history of a type.
  957. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  958. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  959. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  960. *)
  961. END;
  962. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  963. Error(position, "invalid inheritance of disposable types");
  964. END;
  965. END;
  966. Declarations(x.recordScope, FALSE, {0});
  967. x.SetState(SyntaxTree.Resolved);
  968. Declarations(x.recordScope, FALSE, {1});
  969. ResolveArrayStructure(x);
  970. (* computation of sizes and offsets skipped -> done in backend / system *)
  971. recordBase := x.GetBaseRecord();
  972. IF recordBase = NIL THEN numberMethods := 0
  973. ELSE numberMethods := recordBase.recordScope.numberMethods
  974. END;
  975. isRealtime := TRUE;
  976. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  977. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  978. END;
  979. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  980. WHILE symbol # NIL DO
  981. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  982. IF symbol IS SyntaxTree.Variable THEN
  983. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  984. END;
  985. IF symbol IS SyntaxTree.Procedure THEN
  986. procedure := symbol(SyntaxTree.Procedure);
  987. IF procedure.super # NIL THEN
  988. procedure.SetMethodNumber(procedure.super.methodNumber);
  989. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  990. procedure.SetMethodNumber(numberMethods);
  991. INC(numberMethods);
  992. END;
  993. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  994. Error(procedure.position,"realtime procedure in non-realtime object")
  995. END;
  996. END;
  997. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  998. Error(symbol.position,"non-realtime symbol in realtime object")
  999. END;
  1000. symbol := symbol.nextSymbol;
  1001. END;
  1002. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1003. x.recordScope.SetNumberMethods(numberMethods);
  1004. (* TODO: is this needed anymore? *)
  1005. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1006. Error(x.position,"object extends a record")
  1007. END;
  1008. IF ~x.isAbstract THEN
  1009. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1010. Error(x.position, "non-abstract object contains abstract procedure");
  1011. END;
  1012. ELSE
  1013. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1014. Error(x.position, "abstract object does not contain an abstract method");
  1015. END;
  1016. END;
  1017. IF (x.typeDeclaration = NIL) THEN
  1018. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1019. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1020. (*
  1021. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1022. AnonymousTypeDeclaration(x,name);
  1023. *)
  1024. ELSE
  1025. AnonymousTypeDeclaration(x,"Anonymous");
  1026. END;
  1027. END;
  1028. x.SetHasPointers(hasPointers);
  1029. x.SetState(SyntaxTree.Resolved);
  1030. END;
  1031. resolvedType := ResolvedType(x);
  1032. END VisitRecordType;
  1033. (** check and resolve cell type
  1034. - check base type: must be cell
  1035. - check declarations
  1036. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1037. **)
  1038. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1039. VAR
  1040. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1041. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1042. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1043. recordBase: SyntaxTree.RecordType;
  1044. numberMethods: LONGINT;
  1045. int: Basic.Integer;
  1046. real: LONGREAL;
  1047. bool: BOOLEAN;
  1048. set: Basic.Set;
  1049. v: SyntaxTree.Expression;
  1050. str: Scanner.StringType;
  1051. atype: SyntaxTree.ArrayType;
  1052. prev: SyntaxTree.Scope;
  1053. BEGIN
  1054. IF TypeNeedsResolution(x) THEN
  1055. recordBase := NIL;
  1056. IF cellsAreObjects THEN
  1057. IF x.baseType = NIL THEN
  1058. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1059. ImportModule(qualifiedIdentifier.prefix, x.position);
  1060. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1061. x.SetBaseType(ResolveType(x.baseType));
  1062. recordBase := x.GetBaseRecord();
  1063. IF recordBase = NIL THEN
  1064. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1065. END;
  1066. ELSE
  1067. x.SetBaseType(ResolveType(x.baseType));
  1068. END;
  1069. ELSE
  1070. x.SetBaseType(ResolveType(x.baseType));
  1071. END;
  1072. IF recordBase = NIL THEN numberMethods := 0
  1073. ELSE numberMethods := recordBase.recordScope.numberMethods
  1074. END;
  1075. modifier := x.modifiers;
  1076. (*IF ~x.isCellNet THEN*)
  1077. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1078. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1079. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1080. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1081. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1082. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1083. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1084. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1085. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1086. symbol := system.activeCellsCapabilities;
  1087. WHILE symbol # NIL DO
  1088. IF HasFlag(modifier, symbol.name, position) THEN END;
  1089. symbol := symbol.nextSymbol;
  1090. END;
  1091. modifier := x.modifiers;
  1092. WHILE (modifier # NIL) DO
  1093. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1094. IF modifier.expression # NIL THEN
  1095. v := ConstantExpression(modifier.expression);
  1096. property.SetValue(v);
  1097. IF IsIntegerValue(modifier.expression, int) THEN
  1098. (*property.SetValue(modifier.expression);*)
  1099. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1100. property.SetType(system.longintType);
  1101. ELSIF IsRealValue(modifier.expression, real) THEN
  1102. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1103. property.SetType(system.longrealType);
  1104. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1105. property.SetType(system.booleanType);
  1106. ELSIF IsSetValue(modifier.expression, set) THEN
  1107. property.SetType(system.setType);
  1108. ELSIF IsStringValue(modifier.expression, str) THEN
  1109. (*property.SetValue(modifier.expression);*)
  1110. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1111. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1112. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1113. property.SetType(atype);
  1114. ELSE
  1115. Error(modifier.position, "unsupported property type");
  1116. END;
  1117. ELSE (* flag property *)
  1118. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1119. property.SetType(system.booleanType);
  1120. END;
  1121. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1122. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1123. x.AddProperty(property);
  1124. modifier := modifier.nextModifier;
  1125. END;
  1126. CheckModifiers(modifier, FALSE);
  1127. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1128. (* process parameters *)
  1129. prev := currentScope;
  1130. currentScope := x.cellScope;
  1131. parameter :=x.firstParameter;
  1132. WHILE (parameter # NIL) DO
  1133. VisitParameter(parameter);
  1134. type := parameter.type.resolved;
  1135. IF ~(type IS SyntaxTree.PortType) THEN
  1136. WHILE IsStaticArray(type, type, len) DO
  1137. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1138. END;
  1139. WHILE IsDynamicArray(type, type) DO
  1140. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1141. END;
  1142. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1143. Error(parameter.position, "invalid type, must be port or static array of port ");
  1144. END;
  1145. END;
  1146. parameter := parameter.nextParameter;
  1147. END;
  1148. currentScope := prev;
  1149. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1150. WHILE symbol # NIL DO
  1151. IF symbol IS SyntaxTree.Variable THEN
  1152. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1153. END;
  1154. symbol := symbol.nextSymbol;
  1155. END;
  1156. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1157. IF (x.typeDeclaration = NIL) THEN
  1158. AnonymousTypeDeclaration(x,"Anonymous");
  1159. END;
  1160. x.SetState(SyntaxTree.Resolved);
  1161. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1162. Warning(x.position, "Forbidden empty Body.");
  1163. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1164. Warning(x.position, "Non-empty body for an engine?");
  1165. END;
  1166. END;
  1167. resolvedType := ResolvedType(x);
  1168. END VisitCellType;
  1169. (* check if an object is an array-structured object type
  1170. - determine the array structure
  1171. - collect operators from top to bottom in the inheritance hierarchy
  1172. - check if LEN operator is declared
  1173. - determine number of possible index operators
  1174. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1175. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1176. *)
  1177. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1178. VAR
  1179. indexOperatorCount, i: LONGINT;
  1180. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1181. isTensor: BOOLEAN;
  1182. BEGIN
  1183. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1184. (* determine array structure *)
  1185. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1186. END;
  1187. IF recordType.HasArrayStructure() THEN
  1188. (* the object is an ASOT *)
  1189. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1190. (* reset array access operators *)
  1191. arrayAccessOperators.len := NIL;
  1192. arrayAccessOperators.generalRead := NIL;
  1193. arrayAccessOperators.generalWrite := NIL;
  1194. IF isTensor THEN
  1195. (* all operators of dimensionalities 1 to max *)
  1196. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1197. ELSE
  1198. (* all operators of certain dimensionality *)
  1199. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1200. END;
  1201. NEW(arrayAccessOperators.read, indexOperatorCount);
  1202. NEW(arrayAccessOperators.write, indexOperatorCount);
  1203. FOR i := 0 TO indexOperatorCount - 1 DO
  1204. arrayAccessOperators.read[i] := NIL;
  1205. arrayAccessOperators.write[i] := NIL
  1206. END;
  1207. (* collect access operators in the record scope *)
  1208. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1209. IF arrayAccessOperators.len = NIL THEN
  1210. (* TODO: think about making this operator optional for static array structures *)
  1211. Error(recordType.position, "LEN operator missing")
  1212. END;
  1213. (* show error messages *)
  1214. IF isTensor THEN
  1215. (* require ARRAY [*] OF RANGE *)
  1216. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1217. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1218. ELSE
  1219. (* forbid ARRAY [*] OF RANGE *)
  1220. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1221. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1222. (* require RANGE, RANGE, ... RANGE *)
  1223. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1224. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1225. END;
  1226. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1227. ELSE
  1228. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1229. IF recordType.recordScope.firstOperator # NIL THEN
  1230. RETURN;
  1231. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1232. END
  1233. END
  1234. END ResolveArrayStructure;
  1235. (** collect array access operators in a record scope **)
  1236. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1237. VAR
  1238. baseType: SyntaxTree.Type;
  1239. operator: SyntaxTree.Operator;
  1240. isReadOperator, isGeneralOperator: BOOLEAN;
  1241. indexListSize, indexListKind, hashValue: LONGINT;
  1242. BEGIN
  1243. (* if a parent record scope exists, collect the operators there first *)
  1244. baseType := recordScope.ownerRecord.baseType;
  1245. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1246. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1247. END;
  1248. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1249. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1250. END;
  1251. (* go through all operators in the current record scope *)
  1252. operator := recordScope.firstOperator;
  1253. WHILE operator # NIL DO
  1254. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1255. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1256. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1257. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1258. IF isGeneralOperator THEN
  1259. IF isReadOperator THEN
  1260. arrayAccessOperators.generalRead := operator
  1261. ELSE
  1262. arrayAccessOperators.generalWrite := operator
  1263. END
  1264. ELSE
  1265. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1266. IF isReadOperator THEN
  1267. arrayAccessOperators.read[hashValue] := operator
  1268. ELSE
  1269. arrayAccessOperators.write[hashValue] := operator
  1270. END
  1271. END
  1272. END
  1273. ELSE
  1274. Error(operator.position, 'invalid operator')
  1275. END;
  1276. operator := operator.nextOperator
  1277. END
  1278. END CollectArrayAccessOperators;
  1279. (** the hash value of an index operator **)
  1280. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1281. VAR result: LONGINT;
  1282. BEGIN
  1283. IF isTensor THEN
  1284. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1285. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1286. ELSE
  1287. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1288. END
  1289. ELSE
  1290. result := indexListKind
  1291. END;
  1292. RETURN result
  1293. END IndexOperatorHash;
  1294. (** 2 to the power of exponent **)
  1295. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1296. VAR result, i: LONGINT;
  1297. BEGIN
  1298. result := 1;
  1299. FOR i := 1 TO exponent DO
  1300. result := result * 2;
  1301. END;
  1302. RETURN result
  1303. END TwoToThePowerOf;
  1304. (** check if a LEN operator has a correct signature. i.e.
  1305. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1306. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1307. **)
  1308. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1309. VAR
  1310. procedureType: SyntaxTree.ProcedureType;
  1311. returnedArrayType: SyntaxTree.MathArrayType;
  1312. result: BOOLEAN;
  1313. BEGIN
  1314. result := FALSE;
  1315. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1316. IF (procedureType.numberParameters = 0) THEN
  1317. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1318. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1319. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1320. IF returnedArrayType.form = SyntaxTree.Open THEN
  1321. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1322. result := TRUE
  1323. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1324. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1325. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1326. result := TRUE
  1327. END
  1328. END
  1329. END
  1330. END
  1331. END;
  1332. IF result THEN
  1333. (* export symbol automatically *)
  1334. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1335. ELSE
  1336. Error(operator.position, "LEN operator with invalid signature");
  1337. END;
  1338. RETURN result
  1339. END CheckLenOperator;
  1340. (** check if an index operator has a correct signature. i.e.
  1341. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1342. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1343. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1344. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1345. - determine if it is a read or write operator (existance of return type)
  1346. - check index parameters
  1347. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1348. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1349. [LONGINT] -> binary 0 -> 0
  1350. [RANGE] -> binary 1 -> 1
  1351. [LONGINT, LONGINT] -> binary 00 -> 0
  1352. [LONGINT, RANGE] -> binary 01 -> 1
  1353. [RANGE, LONGINT] -> binary 10 -> 2
  1354. [RANGE, RANGE] -> binary 11 -> 3
  1355. etc.
  1356. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1357. - for read operators, check if return type matches the type of data that is read
  1358. - for write operators, check if last parameter type matches the type of data that is written
  1359. **)
  1360. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1361. VAR
  1362. elementType, otherElementType, dataType: SyntaxTree.Type;
  1363. procedureType: SyntaxTree.ProcedureType;
  1364. mathArrayType: SyntaxTree.MathArrayType;
  1365. parameter: SyntaxTree.Parameter;
  1366. parameterCount, rangeCount, i: LONGINT;
  1367. hasTypeError: BOOLEAN;
  1368. BEGIN
  1369. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1370. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1371. (* determine if it is a read or write operator *)
  1372. isReadOperator := (procedureType.returnType # NIL);
  1373. IF isReadOperator THEN
  1374. indexListSize := parameterCount;
  1375. ELSE
  1376. indexListSize := parameterCount - 1;
  1377. END;
  1378. IF indexListSize < 1 THEN
  1379. Error(operator.position, "index operator with too few parameters");
  1380. RETURN FALSE
  1381. END;
  1382. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1383. (* general operator *)
  1384. isGeneralOperator := TRUE;
  1385. IF indexListSize > 1 THEN
  1386. Error(operator.position, "index operator with too many parameters");
  1387. RETURN FALSE
  1388. END;
  1389. (* ARRAY [*] OF RANGE*)
  1390. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1391. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1392. Error(operator.position, "index parameter not dynamic math array of range");
  1393. RETURN FALSE
  1394. END;
  1395. parameter := procedureType.firstParameter.nextParameter
  1396. ELSE
  1397. (* fixed-dim. operator *)
  1398. isGeneralOperator := FALSE;
  1399. (* check number of index parameters *)
  1400. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1401. (* for tensors, limited to a certain size *)
  1402. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1403. Error(operator.position, "too many index parameters for tensor");
  1404. RETURN FALSE
  1405. END
  1406. ELSE
  1407. (* for non-tensors, depends on dimensionality *)
  1408. IF indexListSize # arrayStructure.Dimensionality() THEN
  1409. Error(operator.position, "index parameter count does not match dimensionality");
  1410. RETURN FALSE
  1411. END
  1412. END;
  1413. (* go through all index parameters
  1414. - count the number of ranges
  1415. - determine the index list kind number
  1416. *)
  1417. indexListKind := 0;
  1418. rangeCount := 0;
  1419. parameter := procedureType.firstParameter;
  1420. FOR i := 1 TO indexListSize DO
  1421. indexListKind := indexListKind * 2;
  1422. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1423. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1424. INC(indexListKind);
  1425. INC(rangeCount)
  1426. ELSE
  1427. Error(parameter.position, "integer or range expected");
  1428. RETURN FALSE
  1429. END;
  1430. parameter := parameter.nextParameter
  1431. END;
  1432. END;
  1433. (*
  1434. - for read operators: check type of last parameter
  1435. - for write operators: check return type
  1436. *)
  1437. IF isReadOperator THEN
  1438. dataType := procedureType.returnType (* the return type *)
  1439. ELSE
  1440. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1441. END;
  1442. elementType := arrayStructure.ElementType();
  1443. hasTypeError := FALSE;
  1444. IF isGeneralOperator THEN
  1445. (* ARRAY [?] OF <Element> *)
  1446. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1447. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1448. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1449. hasTypeError := TRUE
  1450. END
  1451. ELSE
  1452. hasTypeError := TRUE
  1453. END
  1454. ELSE
  1455. IF rangeCount = 0 THEN
  1456. (* <Element> *)
  1457. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1458. ELSE
  1459. (* ARRAY [*, *, ..., *] OF <Element> *)
  1460. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1461. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1462. IF mathArrayType.IsFullyDynamic() THEN
  1463. IF mathArrayType.Dimensionality() = rangeCount THEN
  1464. otherElementType := mathArrayType.ElementType();
  1465. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1466. ELSE
  1467. hasTypeError := TRUE
  1468. END
  1469. ELSE
  1470. hasTypeError := TRUE
  1471. END
  1472. ELSE
  1473. hasTypeError := TRUE
  1474. END
  1475. END
  1476. END;
  1477. IF hasTypeError THEN
  1478. IF isReadOperator THEN
  1479. Error(operator.position, "return type does not match")
  1480. ELSE
  1481. Error(parameter.position, "type of last parameter does not match")
  1482. END;
  1483. RETURN FALSE
  1484. END;
  1485. (* export symbol automatically *)
  1486. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1487. RETURN TRUE
  1488. END CheckIndexOperator;
  1489. (** resolve all pending types (late resolving).
  1490. - type fixes are resolved at the end of the declaration phase
  1491. - type fixes may imply new type fixes that are also entered at the end of the list
  1492. **)
  1493. PROCEDURE FixTypes;
  1494. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1495. BEGIN
  1496. prevScope := currentScope;
  1497. p := typeFixes.Get(currentScope);
  1498. WHILE p # NIL DO
  1499. ASSERT(currentScope # NIL);
  1500. ASSERT(p IS SyntaxTree.Type);
  1501. IF p IS SyntaxTree.PointerType THEN
  1502. FixPointerType(p(SyntaxTree.PointerType))
  1503. ELSIF p IS SyntaxTree.ProcedureType THEN
  1504. FixProcedureType(p(SyntaxTree.ProcedureType))
  1505. ELSE
  1506. HALT(100);
  1507. END;
  1508. p := typeFixes.Get(currentScope);
  1509. END;
  1510. currentScope :=prevScope;
  1511. END FixTypes;
  1512. (**
  1513. resolve type x
  1514. - if x is nil then return nil
  1515. - if x cannot be resolved then the result is invalidType else the result is x
  1516. - the resolved type is entered into x.resolved
  1517. **)
  1518. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1519. VAR prev,resolved: SyntaxTree.Type;
  1520. BEGIN
  1521. prev := resolvedType;
  1522. resolvedType := SyntaxTree.invalidType;
  1523. IF x = NIL THEN resolvedType := NIL
  1524. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1525. END;
  1526. resolved := resolvedType;
  1527. resolvedType := prev;
  1528. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1529. RETURN resolved
  1530. END ResolveType;
  1531. (*** compatibility rules ***)
  1532. (**
  1533. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1534. **)
  1535. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1536. VAR result: SyntaxTree.Type;
  1537. BEGIN
  1538. result := SyntaxTree.invalidType;
  1539. IF type = NIL THEN Error(position, "expression of type NIL");
  1540. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1541. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1542. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1543. ELSE result := type.resolved
  1544. END;
  1545. RETURN result
  1546. END RegularType;
  1547. (** returns signature compatibility of procedure types this and to
  1548. - if not compatible then error is reported
  1549. - compatibility means type equality
  1550. **)
  1551. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1552. VAR result: BOOLEAN;
  1553. BEGIN
  1554. result := SameType(to,this);
  1555. IF ~result THEN
  1556. Error(position, "signature incompatible");
  1557. IF VerboseErrorMessage THEN
  1558. Printout.Info("this",this);
  1559. Printout.Info("to",to);
  1560. END;
  1561. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1562. Error(position, "signature incompatible: realtime flag must be inherited");
  1563. END;
  1564. RETURN result
  1565. END SignatureCompatible;
  1566. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1567. - for var parameters compatibility means same type except for
  1568. - formal is of open array of system byte
  1569. - formal is of record type
  1570. - formal is of open array type
  1571. - formal is of open math array type
  1572. - for value parameters compatibllity means assignment compatibility except for
  1573. - formal is of open array type
  1574. if compatible the return true else report error and return false
  1575. **)
  1576. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1577. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1578. BEGIN
  1579. formalType := RegularType(formal.position,formal.type);
  1580. actualType := RegularType(actual.position,actual.type);
  1581. error := FALSE;
  1582. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1583. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1584. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1585. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1586. END;
  1587. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1588. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1589. ELSIF ~IsVariable(actual) THEN
  1590. result := FALSE; error := TRUE;
  1591. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1592. Error(actual.position,"not a variable: no operator for writing");
  1593. ELSE
  1594. Error(actual.position,"is not a variable");
  1595. END;
  1596. IF VerboseErrorMessage THEN
  1597. Printout.Info("actual",actual);
  1598. Printout.Info("formal",formal);
  1599. END;
  1600. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1601. result := CompatibleTo(system,actualType,formalType);
  1602. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1603. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1604. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1605. result := TRUE;
  1606. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1607. IF IsArrayStructuredObjectType(actualType) THEN
  1608. actualType := MathArrayStructureOfType(actualType)
  1609. END;
  1610. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1611. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1612. Error(actual.position,"incompatible non-static actual type");
  1613. END;
  1614. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1615. Error(actual.position,"incompatible tensor (use a range expression)");
  1616. END;
  1617. ELSE
  1618. result := SameType(actualType,formalType)
  1619. END
  1620. ELSE
  1621. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1622. actualType := system.characterType;
  1623. END;
  1624. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1625. result := TRUE; (* special rule for WINAPI parameters *)
  1626. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1627. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1628. ELSE
  1629. result := CompatibleTo(system,actualType,formalType);
  1630. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1631. Error(actual.position,"incompatible non-static actual type");
  1632. END;
  1633. END;
  1634. END;
  1635. IF ~result & ~error THEN
  1636. Error(actual.position,"incompatible parameter");
  1637. IF VerboseErrorMessage THEN
  1638. Printout.Info("actual",actual);
  1639. Printout.Info("formal",formal);
  1640. END;
  1641. END;
  1642. RETURN result
  1643. END ParameterCompatible;
  1644. (** check compatibility for expressions of the form left := right
  1645. - if compatible then return true else error report and return false
  1646. - check if left is variable
  1647. - check compatiblity
  1648. **)
  1649. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1650. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1651. BEGIN
  1652. result := FALSE;
  1653. leftType := RegularType(left.position,left.type);
  1654. rightType := RegularType(right.position,right.type);
  1655. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1656. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1657. END;
  1658. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1659. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1660. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1661. (* error already handled *)
  1662. result := TRUE;
  1663. ELSIF ~IsVariable(left) THEN
  1664. Error(left.position,"is not a variable");
  1665. IF VerboseErrorMessage THEN
  1666. Printout.Info("left",left);
  1667. Printout.Info("right",right);
  1668. END;
  1669. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1670. result := TRUE;
  1671. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1672. result := TRUE
  1673. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1674. Error(left.position,"incompatible assignment");
  1675. IF VerboseErrorMessage THEN
  1676. Printout.Info("left",left);
  1677. Printout.Info("right",right);
  1678. END;
  1679. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1680. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1681. Error(right.position,"forbidden assignment of a nested procedure");
  1682. ELSE
  1683. result := TRUE
  1684. END;
  1685. RETURN result
  1686. END AssignmentCompatible;
  1687. (*** values ***)
  1688. (** check and resolve integer value **)
  1689. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1690. VAR integer: Basic.Integer;
  1691. BEGIN
  1692. integer := value(SyntaxTree.IntegerValue).value;
  1693. value.SetType(Global.GetIntegerType(system,integer));
  1694. resolvedExpression := value
  1695. END VisitIntegerValue;
  1696. (** check and resolve real value **)
  1697. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1698. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1699. BEGIN
  1700. subtype := value(SyntaxTree.RealValue).subtype;
  1701. IF subtype = Scanner.Real THEN
  1702. type := system.realType
  1703. ELSIF subtype = Scanner.Longreal THEN
  1704. type := system.longrealType
  1705. ELSE
  1706. HALT(100)
  1707. END;
  1708. value.SetType(type);
  1709. resolvedExpression := value
  1710. END VisitRealValue;
  1711. (** check and resolve complex value **)
  1712. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1713. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1714. BEGIN
  1715. subtype := value(SyntaxTree.ComplexValue).subtype;
  1716. IF subtype = Scanner.Real THEN
  1717. type := system.complexType
  1718. ELSIF subtype = Scanner.Longreal THEN
  1719. type := system.longcomplexType
  1720. ELSE
  1721. HALT(100)
  1722. END;
  1723. value.SetType(type);
  1724. resolvedExpression := value
  1725. END VisitComplexValue;
  1726. (** check and resolve set value **)
  1727. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1728. BEGIN
  1729. value.SetType(system.setType);
  1730. resolvedExpression := value
  1731. END VisitSetValue;
  1732. (** check and resolve set value **)
  1733. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1734. BEGIN
  1735. value.SetType(SyntaxTree.invalidType);
  1736. resolvedExpression := value
  1737. END VisitMathArrayValue;
  1738. (** check and resolve boolean value **)
  1739. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1740. BEGIN
  1741. value.SetType(system.booleanType);
  1742. resolvedExpression := value
  1743. END VisitBooleanValue;
  1744. (** check and resolve string value **)
  1745. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1746. BEGIN
  1747. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1748. resolvedExpression := value
  1749. END VisitStringValue;
  1750. (** check and resolve character value **)
  1751. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1752. BEGIN
  1753. value.SetType(system.characterType);
  1754. resolvedExpression := value
  1755. END VisitCharacterValue;
  1756. (** check and resolve nil value **)
  1757. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1758. BEGIN
  1759. value.SetType(system.nilType);
  1760. resolvedExpression := value
  1761. END VisitNilValue;
  1762. (** check and resolve enumerator value **)
  1763. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1764. BEGIN
  1765. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1766. ASSERT(value.type # NIL);
  1767. resolvedExpression := value
  1768. END VisitEnumerationValue;
  1769. (*** expressions ***)
  1770. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1771. - check all elements on integer type
  1772. - if element range is constant, then check lower and upper bound
  1773. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1774. if an error occurs then report error and return invalidExpression
  1775. **)
  1776. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1777. VAR
  1778. i: LONGINT;
  1779. element: SyntaxTree.Expression;
  1780. constant: BOOLEAN;
  1781. elements: SyntaxTree.ExpressionList;
  1782. s: Basic.Set;
  1783. result: SyntaxTree.Expression;
  1784. value: SyntaxTree.Value;
  1785. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1786. VAR
  1787. left, right: SyntaxTree.Expression;
  1788. elementResult: SyntaxTree.Expression;
  1789. leftInteger, rightInteger: Basic.Integer;
  1790. BEGIN
  1791. (* set context of range *)
  1792. IF element IS SyntaxTree.RangeExpression THEN
  1793. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1794. END;
  1795. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1796. IF elementResult = SyntaxTree.invalidExpression THEN
  1797. (* error already reported *)
  1798. constant := FALSE
  1799. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1800. (* the element is a range expression *)
  1801. (* extract left and right hand side of range *)
  1802. left := elementResult(SyntaxTree.RangeExpression).first;
  1803. right := elementResult(SyntaxTree.RangeExpression).last;
  1804. (* guaranteed by VisitRangeExpression: *)
  1805. ASSERT((left # NIL) & (right # NIL));
  1806. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1807. ELSE
  1808. (* the element is not a range expression *)
  1809. (* check type and add conversion if needed *)
  1810. IF IsIntegerType(elementResult.type.resolved) THEN
  1811. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1812. ELSE
  1813. Error(elementResult.position, "non integer element in set");
  1814. elementResult := SyntaxTree.invalidExpression;
  1815. constant := FALSE
  1816. END;
  1817. left := elementResult;
  1818. right := elementResult
  1819. END;
  1820. IF elementResult # SyntaxTree.invalidExpression THEN
  1821. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1822. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1823. Error(left.position,"not allowed set integer value");
  1824. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1825. Error(right.position,"not allowed set integer value");
  1826. END
  1827. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1828. Error(right.position,"not allowed set integer value");
  1829. ELSE
  1830. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1831. s := {};
  1832. ELSE
  1833. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1834. IF leftInteger < 0 THEN leftInteger := 0 END;
  1835. (*!!!!!!!!! this is a hack !!!!!!! *)
  1836. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1837. extends the range x..31 to x..63 !!!!!! *)
  1838. s := s + {leftInteger..rightInteger};
  1839. END;
  1840. END;
  1841. ELSE
  1842. constant := FALSE;
  1843. END
  1844. END;
  1845. RETURN elementResult
  1846. END CheckElement;
  1847. BEGIN
  1848. result := set; constant := TRUE; s := {}; elements := set.elements;
  1849. IF elements # NIL THEN
  1850. FOR i := 0 TO elements.Length()-1 DO
  1851. element := elements.GetExpression(i);
  1852. element := CheckElement(element);
  1853. IF element = SyntaxTree.invalidExpression THEN
  1854. result := SyntaxTree.invalidExpression
  1855. END;
  1856. elements.SetExpression(i,element);
  1857. END;
  1858. END;
  1859. IF constant THEN
  1860. value := Global.NewSetValue(system,set.position,s);
  1861. result.SetResolved(value);
  1862. result.SetType(value.type);
  1863. ELSE
  1864. result.SetType(system.setType);
  1865. END;
  1866. (* optimization possible
  1867. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1868. left this to the programmer...
  1869. *)
  1870. resolvedExpression := result;
  1871. END VisitSet;
  1872. (*
  1873. old variant: quite generic but needs better conversion handling, do this?
  1874. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1875. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1876. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1877. BEGIN
  1878. type := NIL;
  1879. numberElements := x.elements.Length();
  1880. FOR i := 0 TO numberElements-1 DO
  1881. expression := x.elements.GetExpression(i);
  1882. position := expression.position;
  1883. expression := ResolveExpression(x.elements.GetExpression(i));
  1884. x.elements.SetExpression(i,de);
  1885. IF type = NIL THEN
  1886. type := expression.type;
  1887. ELSIF CompatibleTo(system,expression.type,type) THEN
  1888. (* ok *)
  1889. ELSIF CompatibleTo(system,type,expression.type) THEN
  1890. type := expression.type
  1891. ELSE
  1892. Error(expression.position, "incompatible element types");
  1893. type := SyntaxTree.invalidType;
  1894. END;
  1895. END;
  1896. isValue := TRUE;
  1897. FOR i := 0 TO numberElements-1 DO
  1898. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1899. x.elements.SetExpression(i,expression);
  1900. isValue := isValue & (expression.resolved # NIL);
  1901. END;
  1902. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1903. arrayType.SetArrayBase(type);
  1904. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1905. IF isValue THEN
  1906. value := SyntaxTree.NewMathArrayValue(position);
  1907. value.SetElements(x.elements);
  1908. x.SetResolved(value);
  1909. END;
  1910. x.SetType(arrayType);
  1911. resolvedExpression := x;
  1912. END VisitMathArrayExpression;
  1913. *)
  1914. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1915. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1916. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1917. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1918. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1919. BEGIN
  1920. numberElements := x.elements.Length();
  1921. FOR i := 0 TO numberElements-1 DO
  1922. expression := x.elements.GetExpression(i);
  1923. IF expression IS SyntaxTree.MathArrayExpression THEN
  1924. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1925. ELSE
  1926. position := expression.position;
  1927. expression := ResolveExpression(x.elements.GetExpression(i));
  1928. x.elements.SetExpression(i,expression);
  1929. IF type = NIL THEN
  1930. type := expression.type;
  1931. ELSIF CompatibleTo(system,expression.type,type) THEN
  1932. (* ok *)
  1933. ELSIF CompatibleTo(system,type,expression.type) THEN
  1934. type := expression.type
  1935. ELSE
  1936. Error(expression.position, "incompatible element types");
  1937. type := SyntaxTree.invalidType;
  1938. END;
  1939. END;
  1940. END;
  1941. END RecursivelyFindType;
  1942. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1943. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1944. BEGIN
  1945. numberElements := x.elements.Length();
  1946. FOR i := 0 TO numberElements-1 DO
  1947. expression := x.elements.GetExpression(i);
  1948. IF expression IS SyntaxTree.MathArrayExpression THEN
  1949. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1950. ELSE
  1951. position := expression.position;
  1952. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1953. x.elements.SetExpression(i,expression);
  1954. isValue := isValue & (expression.resolved # NIL);
  1955. END;
  1956. END;
  1957. END RecursivelySetExpression;
  1958. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1959. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1960. arrayType: SyntaxTree.MathArrayType;
  1961. BEGIN
  1962. numberElements := x.elements.Length();
  1963. baseType := NIL;
  1964. gsize := 0;
  1965. FOR i := 0 TO numberElements-1 DO
  1966. expression := x.elements.GetExpression(i);
  1967. IF expression IS SyntaxTree.MathArrayExpression THEN
  1968. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1969. IF i=0 THEN
  1970. gsize := size;
  1971. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1972. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1973. ELSE expression.SetType(baseType)
  1974. END;
  1975. ELSIF baseType = NIL THEN baseType := type;
  1976. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1977. END;
  1978. END;
  1979. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1980. arrayType.SetArrayBase(baseType);
  1981. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1982. RETURN ResolveType(arrayType);
  1983. END RecursivelySetType;
  1984. BEGIN
  1985. type := NIL;
  1986. RecursivelyFindType(x);
  1987. isValue := TRUE;
  1988. RecursivelySetExpression(x);
  1989. arrayType := RecursivelySetType(x);
  1990. x.SetType(arrayType);
  1991. IF isValue THEN
  1992. value := SyntaxTree.NewMathArrayValue(x.position);
  1993. value.SetArray(x);
  1994. x.SetResolved(value);
  1995. value.SetType(arrayType);
  1996. END;
  1997. x.SetType(arrayType);
  1998. resolvedExpression := x;
  1999. END VisitMathArrayExpression;
  2000. (** check and resolve unary expression **)
  2001. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  2002. VAR
  2003. left: SyntaxTree.Expression;
  2004. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  2005. bool: BOOLEAN;
  2006. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2007. value: SyntaxTree.Value;
  2008. BEGIN
  2009. type := SyntaxTree.invalidType;
  2010. left := ResolveExpression(unaryExpression.left);
  2011. unaryExpression.SetLeft(left);
  2012. operator := unaryExpression.operator;
  2013. result := unaryExpression;
  2014. IF ~system.operatorDefined[operator] THEN
  2015. Error(left.position,"Operator Not Defined");
  2016. RETURN
  2017. ELSIF left.type = NIL THEN
  2018. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2019. resolvedExpression := SyntaxTree.invalidExpression;
  2020. RETURN
  2021. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2022. RETURN
  2023. END;
  2024. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2025. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2026. END;
  2027. IF operatorCall # NIL THEN
  2028. result := operatorCall;
  2029. type := operatorCall.type;
  2030. (* admissible operators
  2031. Minus number, set
  2032. Not boolean
  2033. *)
  2034. ELSE
  2035. CASE unaryExpression.operator OF
  2036. |Scanner.Minus:
  2037. IF IsIntegerType(left.type.resolved) THEN
  2038. IF left.resolved # NIL THEN
  2039. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2040. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2041. result.SetResolved(value);
  2042. type := Global.GetIntegerType(system,int);
  2043. value.SetType(type);
  2044. ELSE
  2045. type := left.type
  2046. END
  2047. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2048. IF IsRealValue(left,real) THEN
  2049. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2050. result.SetResolved(value);
  2051. type := left.type;
  2052. value.SetType(type);
  2053. ELSE
  2054. type := left.type;
  2055. END;
  2056. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2057. IF IsSetValue(left,set) THEN
  2058. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2059. result.SetResolved(value);
  2060. type := Global.GetSetType(system,-set);
  2061. value.SetType(type);
  2062. ELSE
  2063. type := left.type;
  2064. END;
  2065. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2066. IF IsComplexValue(left, real, imaginary) THEN
  2067. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2068. result.SetResolved(value);
  2069. type := left.type;
  2070. value.SetType(type);
  2071. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2072. ELSE
  2073. type := left.type;
  2074. END
  2075. ELSE
  2076. Error(left.position,"unary operator not applicable");
  2077. END;
  2078. |Scanner.Not:
  2079. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2080. IF IsBooleanValue(left,bool) THEN
  2081. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2082. result.SetResolved(value);
  2083. type := system.booleanType;
  2084. value.SetType(type);
  2085. ELSE
  2086. type := system.booleanType;
  2087. END;
  2088. ELSE
  2089. Error(left.position,"unary operator not applicable");
  2090. END;
  2091. |Scanner.Plus:
  2092. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2093. result := left; type := left.type;
  2094. ELSE
  2095. Error(left.position,"unary operator not applicable");
  2096. END;
  2097. (* ADDRESS OF *)
  2098. |Scanner.Address:
  2099. IF HasAddress(left) THEN
  2100. type := system.addressType;
  2101. ELSE
  2102. type := SyntaxTree.invalidType;
  2103. Error(left.position,"has no address");
  2104. Printout.Info("par", left);
  2105. END;
  2106. (* SIZE OF *)
  2107. |Scanner.Size:
  2108. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2109. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2110. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2111. value := SyntaxTree.NewIntegerValue(left.position, int);
  2112. result.SetResolved(value);
  2113. type := Global.GetIntegerType(system,int);
  2114. value.SetType(type)
  2115. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2116. ELSE
  2117. (* for variables, system sizeof could represent the physically occupied size
  2118. determined via the type descriptor, implement that ? *)
  2119. Error(left.position,"is not a type symbol");
  2120. END
  2121. (* ALIAS OF *)
  2122. |Scanner.Alias:
  2123. type := left.type.resolved;
  2124. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2125. type := SyntaxTree.invalidType;
  2126. Error(left.position,"alias on non math array type");
  2127. END;
  2128. ELSE
  2129. Error(left.position,"unary operator not defined");
  2130. END;
  2131. END;
  2132. result.SetType(type);
  2133. resolvedExpression := result
  2134. END VisitUnaryExpression;
  2135. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2136. VAR
  2137. result: SyntaxTree.Expression;
  2138. array: SyntaxTree.MathArrayExpression;
  2139. value: SyntaxTree.MathArrayValue;
  2140. isValue: BOOLEAN;
  2141. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2142. BEGIN
  2143. type := type.resolved;
  2144. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2145. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2146. END;
  2147. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2148. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2149. END;
  2150. RETURN type
  2151. END BaseType;
  2152. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2153. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2154. BEGIN
  2155. numberElements := x.elements.Length();
  2156. FOR i := 0 TO numberElements-1 DO
  2157. expression := x.elements.GetExpression(i);
  2158. IF expression IS SyntaxTree.MathArrayExpression THEN
  2159. array := SyntaxTree.NewMathArrayExpression(position);
  2160. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2161. expression := array;
  2162. ELSE
  2163. position := expression.position;
  2164. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2165. isValue := isValue & (expression.resolved # NIL);
  2166. END;
  2167. to.elements.AddExpression(expression);
  2168. END;
  2169. END RecursivelyConvert;
  2170. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2171. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2172. arrayType: SyntaxTree.MathArrayType;
  2173. BEGIN
  2174. numberElements := x.elements.Length();
  2175. baseType := NIL;
  2176. gsize := 0;
  2177. FOR i := 0 TO numberElements-1 DO
  2178. expression := x.elements.GetExpression(i);
  2179. IF expression IS SyntaxTree.MathArrayExpression THEN
  2180. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2181. IF i=0 THEN
  2182. gsize := size;
  2183. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2184. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2185. ELSE expression.SetType(baseType)
  2186. END;
  2187. ELSIF baseType = NIL THEN baseType := type;
  2188. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2189. END;
  2190. END;
  2191. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2192. arrayType.SetArrayBase(baseType);
  2193. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2194. RETURN ResolveType(arrayType);
  2195. END RecursivelySetType;
  2196. BEGIN
  2197. result := SyntaxTree.invalidExpression;
  2198. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2199. result := expression (* do not convert *)
  2200. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2201. isValue := TRUE;
  2202. type := BaseType(type);
  2203. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2204. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2205. value := SyntaxTree.NewMathArrayValue(array.position);
  2206. value.SetArray(array);
  2207. value.SetType(RecursivelySetType(array));
  2208. result := value;
  2209. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2210. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2211. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2212. IF result = NIL THEN
  2213. result := SyntaxTree.invalidExpression;
  2214. Error(position, "incompatible conversion");
  2215. IF VerboseErrorMessage THEN
  2216. Printout.Info("expression",expression);
  2217. Printout.Info("type",type);
  2218. END;
  2219. END;
  2220. END;
  2221. RETURN result
  2222. END MathArrayConversion;
  2223. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2224. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2225. BEGIN
  2226. result := expression; type := type.resolved;
  2227. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2228. (* skip, no conversion *)
  2229. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2230. int := expression(SyntaxTree.IntegerValue).value;
  2231. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2232. int := Global.ConvertSigned(int,system.SizeOf(type));
  2233. result := SyntaxTree.NewIntegerValue(position,int);
  2234. result.SetType(type);
  2235. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2236. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2237. result := SyntaxTree.NewIntegerValue(position,int);
  2238. result.SetType(type);
  2239. ELSIF (type IS SyntaxTree.FloatType) THEN
  2240. result := SyntaxTree.NewRealValue(expression.position,int);
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2243. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.SetType) THEN
  2246. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2249. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2252. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2253. result.SetType(type);
  2254. ELSIF (type IS SyntaxTree.PortType) THEN
  2255. result := ConvertValue(position, expression, system.integerType);
  2256. ELSE
  2257. Error(position, "integer value cannot be converted");
  2258. result := SyntaxTree.invalidExpression;
  2259. IF VerboseErrorMessage THEN
  2260. Printout.Info("expression",expression);
  2261. Printout.Info("type",type);
  2262. END;
  2263. END;
  2264. ELSIF IsRealValue(expression,real) THEN
  2265. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2266. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2267. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2268. result.SetType(type);
  2269. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2270. int := ENTIERH(real);
  2271. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2272. result.SetType(type);
  2273. ELSIF (type IS SyntaxTree.FloatType) THEN
  2274. result := SyntaxTree.NewRealValue(position,real);
  2275. result.SetType(type);
  2276. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2277. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2278. result.SetType(type);
  2279. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2280. ELSIF (type IS SyntaxTree.PortType) THEN
  2281. result := ConvertValue(position, expression, system.integerType);
  2282. ELSE
  2283. Error(position, "real value cannot be converted");
  2284. result := SyntaxTree.invalidExpression;
  2285. END
  2286. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2287. IF (type IS SyntaxTree.ComplexType) THEN
  2288. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2289. result.SetType(type);
  2290. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2291. ELSE
  2292. Error(position, "complex value cannot be converted");
  2293. result := SyntaxTree.invalidExpression;
  2294. END
  2295. ELSIF IsSetValue(expression,set) THEN
  2296. IF (type IS SyntaxTree.IntegerType) THEN
  2297. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2298. result.SetType(type);
  2299. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2300. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2301. result.SetType(type);
  2302. ELSIF (type IS SyntaxTree.PortType) THEN
  2303. result := ConvertValue(position, expression, system.integerType);
  2304. ELSIF (type IS SyntaxTree.SetType) THEN
  2305. set := Global.ConvertSet(set,system.SizeOf(type));
  2306. result := SyntaxTree.NewSetValue(expression.position,set);
  2307. result.SetType(type);
  2308. ELSE
  2309. Error(position, "set value cannot be converted");
  2310. result := SyntaxTree.invalidExpression;
  2311. END;
  2312. ELSIF IsStringValue(expression,string) THEN
  2313. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2314. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2315. result.SetType(type);
  2316. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2317. ELSE
  2318. Error(position, "string value cannot be converted");
  2319. result := SyntaxTree.invalidExpression;
  2320. END;
  2321. ELSIF IsCharacterValue(expression,char) THEN
  2322. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2323. string[0] := char; string[1] := 0X;
  2324. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2325. result := SyntaxTree.NewStringValue(expression.position,string);
  2326. result.SetType(type);
  2327. ELSIF (type IS SyntaxTree.ByteType) THEN
  2328. (* do not simply set the new type as this could invalidate types of constants *)
  2329. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2330. result.SetType(type)
  2331. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2332. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2333. result.SetType(type);
  2334. ELSIF (type IS SyntaxTree.SetType) THEN
  2335. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2336. result.SetType(type);
  2337. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2338. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2339. result.SetType(type);
  2340. ELSIF (type IS SyntaxTree.PortType) THEN
  2341. result := ConvertValue(position, expression, system.integerType);
  2342. ELSE
  2343. Error(position, "character value cannot be converted");
  2344. result := SyntaxTree.invalidExpression;
  2345. END;
  2346. ELSIF expression IS SyntaxTree.NilValue THEN
  2347. IF type IS SyntaxTree.AddressType THEN
  2348. result := SyntaxTree.NewIntegerValue(position,0);
  2349. result.SetType(type);
  2350. ELSE
  2351. result := expression;
  2352. END;
  2353. (* nothing to be done *)
  2354. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2355. result := MathArrayConversion(position, expression,type);
  2356. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2357. int := expression(SyntaxTree.EnumerationValue).value;
  2358. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2359. int := Global.ConvertSigned(int,system.SizeOf(type));
  2360. result := SyntaxTree.NewIntegerValue(position,int);
  2361. result.SetType(type);
  2362. ELSE
  2363. result := expression;
  2364. END;
  2365. (* nothing to be done *)
  2366. ELSE
  2367. Error(position, "expression cannot be converted");
  2368. IF VerboseErrorMessage THEN
  2369. Printout.Info("expression",expression);
  2370. Printout.Info("type",type);
  2371. END;
  2372. result := SyntaxTree.invalidExpression;
  2373. END;
  2374. RETURN result
  2375. END ConvertValue;
  2376. (**
  2377. return a conversion of an expression to a given type
  2378. - if expression is already of same type then return expression
  2379. - if incompatible conversion then report error and return invalidExpression
  2380. **)
  2381. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2382. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2383. BEGIN
  2384. type := type.resolved;
  2385. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2386. result := expression;
  2387. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2388. ELSIF expression = NIL THEN (* NIL expression *)
  2389. ELSIF expression.type = NIL THEN
  2390. Error(position, "expression of type NIL cannot be converted");
  2391. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2392. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2393. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2394. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2395. ELSIF expression.resolved # NIL THEN (* value *)
  2396. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2397. IF value IS SyntaxTree.Value THEN
  2398. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2399. result.SetResolved(value(SyntaxTree.Value));
  2400. result.SetType(value.type);
  2401. ELSE
  2402. result := value
  2403. END;
  2404. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2405. expressionList := SyntaxTree.NewExpressionList();
  2406. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2407. typeDeclaration.SetDeclaredType(type);
  2408. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2409. typeSymbol.SetType(typeDeclaration.type);
  2410. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2411. expressionList.AddExpression(expression);
  2412. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2413. result.SetType(type);
  2414. ELSIF IsArrayStructuredObjectType(type) THEN
  2415. (* no type can be converted to an array-structured object type *)
  2416. HALT(100)
  2417. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2418. IF inConversion>5 THEN
  2419. Error(expression.position,"recursive Conversion");
  2420. IF VerboseErrorMessage THEN
  2421. Printout.Info("expression",expression);
  2422. Printout.Info("type",type);
  2423. END;
  2424. END;
  2425. INC(inConversion);
  2426. IF inConversion < 10 THEN
  2427. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2428. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2429. result := expression
  2430. ELSE
  2431. result := MathArrayConversion(position, expression,type);
  2432. END;
  2433. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2434. expression := ConvertToMathArray(expression);
  2435. type := MathArrayStructureOfType(type);
  2436. result := MathArrayConversion(position, expression, type)
  2437. ELSE
  2438. Error(expression.position,"cannot convert non array type to array type")
  2439. END;
  2440. END;
  2441. DEC(inConversion);
  2442. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2443. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2444. result := expression; result.SetType(type);
  2445. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2446. OR ~(type IS SyntaxTree.ArrayType) THEN
  2447. Error(expression.position,"cannot convert array type to non-array type")
  2448. END;
  2449. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2450. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2451. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2452. (*skip, no conversion*)
  2453. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2454. (* skip, no conversion *)
  2455. ELSE
  2456. ASSERT(~(type IS SyntaxTree.RangeType));
  2457. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2458. ASSERT(type # NIL);
  2459. END;
  2460. RETURN result
  2461. END NewConversion;
  2462. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2463. BEGIN
  2464. IF CompatibleTo(system,expression.type, type) THEN
  2465. RETURN NewConversion(position, expression, type, NIL);
  2466. ELSE
  2467. Error(expression.position, "incompatible expression");
  2468. RETURN SyntaxTree.invalidExpression
  2469. END;
  2470. END CompatibleConversion;
  2471. (**
  2472. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2473. **)
  2474. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2475. VAR leftType,rightType: SyntaxTree.Type;
  2476. BEGIN
  2477. IF left.type = NIL THEN Error(left.position,"no type")
  2478. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2479. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2480. (* error already handled *)
  2481. ELSE
  2482. leftType := left.type.resolved; rightType := right.type.resolved;
  2483. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2484. right := NewConversion(right.position, right, leftType, NIL);
  2485. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2486. left := NewConversion(left.position,left,rightType,NIL);
  2487. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2488. right := NewConversion(right.position, right, leftType, NIL);
  2489. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2490. left := NewConversion(left.position,left,rightType,NIL);
  2491. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2492. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2493. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2494. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2495. ELSIF
  2496. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2497. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2498. (* must be the case LONGREAL / COMPLEX ) *)
  2499. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2500. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2501. ELSE
  2502. Error(left.position,"incompatible operands");
  2503. END;
  2504. END;
  2505. END ConvertOperands;
  2506. (** find and return best operator matching to parameter list (nil, if none)
  2507. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2508. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2509. **)
  2510. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2511. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2512. identifier: SyntaxTree.Identifier;
  2513. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2514. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2515. BEGIN
  2516. operator := scope.firstOperator;
  2517. WHILE(operator # NIL) DO
  2518. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2519. procedureType := operator.type(SyntaxTree.ProcedureType);
  2520. distance := Distance(system, procedureType,actualParameters);
  2521. IF (distance < Infinity) THEN
  2522. IF returnType # NIL THEN
  2523. IF procedureType.returnType = NIL THEN
  2524. distance := Infinity
  2525. ELSE
  2526. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2527. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2528. END;
  2529. END;
  2530. END;
  2531. (*
  2532. IF distance < Infinity THEN
  2533. TRACE(distance, operator);
  2534. Printout.Info("potential operator",operator);
  2535. ELSE
  2536. Printout.Info("operator not possible",operator);
  2537. END;
  2538. *)
  2539. IF distance < bestDistance THEN
  2540. bestDistance := distance;
  2541. bestOperator := operator;
  2542. END;
  2543. END;
  2544. operator := operator.nextOperator;
  2545. END;
  2546. (*
  2547. Printout.Info("taken operator",bestOperator);
  2548. *)
  2549. END FindInScope;
  2550. BEGIN
  2551. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2552. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2553. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2554. import := currentScope.ownerModule.moduleScope.firstImport;
  2555. WHILE (bestDistance > 0) & (import # NIL) DO
  2556. IF import.module # NIL THEN
  2557. identifier := Global.GetIdentifier(operator,import.module.case);
  2558. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2559. END;
  2560. import := import.nextImport;
  2561. END;
  2562. RETURN bestOperator
  2563. END FindOperator;
  2564. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2565. BEGIN
  2566. currentScope := scope;
  2567. END SetCurrentScope;
  2568. (**
  2569. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2570. - handle LEN and DIM operator for array-structured object types
  2571. - find operator, if found then
  2572. - if in other module then add import designator
  2573. - create symbol designator for operator
  2574. - if error then return invalidExpression, if no operator then return NIL
  2575. **)
  2576. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2577. VAR
  2578. operator: SyntaxTree.Operator;
  2579. import: SyntaxTree.Import;
  2580. expression, result: SyntaxTree.Expression;
  2581. designator: SyntaxTree.Designator;
  2582. actualParameters, tempList: SyntaxTree.ExpressionList;
  2583. recordType: SyntaxTree.RecordType;
  2584. castReturnType : SyntaxTree.MathArrayType;
  2585. BEGIN
  2586. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2587. result := SyntaxTree.invalidExpression
  2588. ELSIF leftExpression = NIL THEN
  2589. result := NIL
  2590. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2591. (* LEN or DIM operator on array-structured object type *)
  2592. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2593. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2594. IF recordType.arrayAccessOperators.len = NIL THEN
  2595. Error(position, "call of undeclared LEN operator");
  2596. result := SyntaxTree.invalidExpression
  2597. ELSE
  2598. ASSERT(leftExpression IS SyntaxTree.Designator);
  2599. designator := leftExpression(SyntaxTree.Designator);
  2600. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2601. ASSERT(expression IS SyntaxTree.Designator);
  2602. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2603. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2604. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2605. result := designator
  2606. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2607. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2608. tempList := SyntaxTree.NewExpressionList();
  2609. tempList.AddExpression(rightExpression);
  2610. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2611. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2612. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2613. tempList := SyntaxTree.NewExpressionList();
  2614. tempList.AddExpression(designator);
  2615. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2616. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2617. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2618. END
  2619. END;
  2620. ELSE
  2621. IF ~complexNumbersImported THEN
  2622. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2623. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2624. THEN
  2625. (* operators on complex numbers *)
  2626. ImportModule(Global.ComplexNumbersName,position);
  2627. complexNumbersImported := TRUE;
  2628. END;
  2629. END;
  2630. (* import OCArrayBase if needed *)
  2631. IF ~arrayBaseImported THEN
  2632. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2633. IF op = Global.Dim THEN
  2634. (* not existing in OCArrayBase *)
  2635. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2636. (* not existing in OCArrayBase *)
  2637. ELSE
  2638. ImportModule(Global.ArrayBaseName,position);
  2639. arrayBaseImported := TRUE;
  2640. END
  2641. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2642. ImportModule(Global.ArrayBaseName,position);
  2643. arrayBaseImported := TRUE
  2644. END;
  2645. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2646. (* LEN(RANGE) *)
  2647. ImportModule(Global.ArrayBaseName,position);
  2648. arrayBaseImported := TRUE;
  2649. END;
  2650. END;
  2651. actualParameters := SyntaxTree.NewExpressionList();
  2652. actualParameters.AddExpression(leftExpression);
  2653. IF rightExpression # NIL THEN
  2654. actualParameters.AddExpression(rightExpression)
  2655. END;
  2656. operator := FindOperator(system,op,actualParameters,resultType);
  2657. IF operator # NIL THEN
  2658. designator := NIL;
  2659. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2660. import := currentScope.ownerModule.moduleScope.firstImport;
  2661. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2662. import := import.nextImport;
  2663. END;
  2664. expression := NewSymbolDesignator(position,NIL,import);
  2665. designator := expression(SyntaxTree.Designator);
  2666. END;
  2667. expression := NewSymbolDesignator(position,designator,operator);
  2668. designator := expression(SyntaxTree.Designator);
  2669. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2670. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2671. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2672. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2673. result.SetType(castReturnType);
  2674. END;
  2675. ELSE
  2676. result := NIL;
  2677. END;
  2678. END;
  2679. RETURN result
  2680. END NewOperatorCall;
  2681. (** check and resolve binary expression **)
  2682. (*! clean up *)
  2683. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2684. VAR left,right,result: SyntaxTree.Expression;
  2685. leftType, rightType: SyntaxTree.Type;
  2686. il,ir: Basic.Integer; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: Basic.Integer;bl,br: BOOLEAN; sl,sr: Basic.Set; strl,strr: Scanner.StringType;
  2687. cl,cr: CHAR;
  2688. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2689. type: SyntaxTree.Type;
  2690. value: SyntaxTree.Value;
  2691. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2692. integerConstantFolding: BOOLEAN;
  2693. list: SyntaxTree.ExpressionList;
  2694. PROCEDURE NewBool(v: BOOLEAN);
  2695. BEGIN
  2696. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2697. value.SetType(system.booleanType);
  2698. result.SetResolved(value);
  2699. type := system.booleanType
  2700. END NewBool;
  2701. PROCEDURE NewSet(v: Basic.Set);
  2702. BEGIN
  2703. value := Global.NewSetValue(system,binaryExpression.position,v);
  2704. result.SetResolved(value);
  2705. type := value.type;
  2706. END NewSet;
  2707. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2708. BEGIN
  2709. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2710. (* type cast to "larger" type only if the value is still in the range *)
  2711. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2712. value.SetType(t);
  2713. END;
  2714. result.SetResolved(value);
  2715. type := value.type;
  2716. END NewInteger;
  2717. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2718. BEGIN
  2719. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2720. value.SetType(t);
  2721. result.SetResolved(value);
  2722. type := t;
  2723. END NewReal;
  2724. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2725. BEGIN
  2726. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2727. value.SetType(t);
  2728. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2729. result.SetResolved(value);
  2730. type := t;
  2731. END NewComplex;
  2732. BEGIN
  2733. type := SyntaxTree.invalidType;
  2734. left := ResolveExpression(binaryExpression.left);
  2735. right := ResolveExpression(binaryExpression.right);
  2736. binaryExpression.SetLeft(left);
  2737. binaryExpression.SetRight(right);
  2738. result := binaryExpression;
  2739. operator := binaryExpression.operator;
  2740. IF ~system.operatorDefined[operator] THEN
  2741. Error(left.position,"Operator Not Defined");
  2742. result := SyntaxTree.invalidExpression;
  2743. RETURN
  2744. END;
  2745. IF left.type = NIL THEN
  2746. Error(left.position,"Expression has no result type");
  2747. result := SyntaxTree.invalidExpression;
  2748. RETURN;
  2749. END;
  2750. IF right.type = NIL THEN
  2751. Error(right.position,"Expression has no result type");
  2752. result := SyntaxTree.invalidExpression;
  2753. RETURN;
  2754. END;
  2755. leftType := left.type.resolved; rightType := right.type.resolved;
  2756. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2757. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2758. END;
  2759. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2760. list := SyntaxTree.NewExpressionList();
  2761. list.AddExpression(right);
  2762. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2763. END;
  2764. IF operatorCall # NIL THEN
  2765. result := operatorCall;
  2766. type := operatorCall.type;
  2767. (* admissible operators:
  2768. Times, Plus, Minus numeric numeric numeric
  2769. set set set
  2770. Slash numeric numeric real /complex
  2771. set set set
  2772. Div , Mod integer integer integer
  2773. And, Or bool bool bool
  2774. Equal, Unequal basic basic bool
  2775. pointer pointer bool
  2776. object object bool
  2777. record record bool
  2778. string string bool
  2779. enumerator enumerator bool
  2780. Less, LessEqual,
  2781. Greater, GreaterEqual integer/real integer/real bool
  2782. enumerator enumerator bool
  2783. In integer set bool
  2784. Is pointer type bool
  2785. object type bool
  2786. record type bool
  2787. Upto: special abbreviation for a..b
  2788. *)
  2789. ELSIF (left.type = NIL) THEN
  2790. Error(left.position,"type (left operand) = NIL in binary expression");
  2791. D.Str("nil type in "); D.Type(left); D.Ln;
  2792. result := SyntaxTree.invalidExpression;
  2793. ELSIF (right.type = NIL) THEN
  2794. Error(right.position,"type (right operand) = NIL in binary expression");
  2795. result := SyntaxTree.invalidExpression;
  2796. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2797. result := SyntaxTree.invalidExpression;
  2798. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2799. HALT(100);
  2800. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2801. type := system.booleanType;
  2802. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2803. Error(right.position,"is not a type ");
  2804. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2805. Error(binaryExpression.position,"is not a type extension of ");
  2806. IF VerboseErrorMessage THEN
  2807. Printout.Info("left",left);
  2808. Printout.Info("right",right);
  2809. END;
  2810. ELSIF IsUnsafePointer(left.type) THEN
  2811. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2812. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2813. NewBool(TRUE)
  2814. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2815. NewBool(TRUE);
  2816. ELSIF IsUnextensibleRecord(left) THEN
  2817. NewBool(FALSE)
  2818. END
  2819. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2820. Error(right.position,"must not be a type");
  2821. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2822. Error(left.position,"must not be a type");
  2823. ELSIF operator = Scanner.In THEN (* left IN right *)
  2824. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2825. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2826. NewBool(il IN sr);
  2827. ELSE
  2828. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2829. left := NewConversion(left.position, left, system.longintType,NIL);
  2830. binaryExpression.SetLeft(left)
  2831. END;
  2832. type := system.booleanType;
  2833. END
  2834. ELSE
  2835. Error(binaryExpression.position, "incompatible operands");
  2836. END
  2837. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2838. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2839. Error(binaryExpression.position,"incompatible operands");
  2840. END;
  2841. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2842. ELSE Error(binaryExpression.position,"operator not defined 1")
  2843. END
  2844. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2845. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2846. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2847. Error(binaryExpression.position,"incompatible operands");
  2848. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2849. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2850. left := NewConversion(left.position, left, system.addressType, NIL);
  2851. right := NewConversion(right.position, right, system.addressType, NIL);
  2852. binaryExpression.SetLeft(left);
  2853. binaryExpression.SetRight(right);
  2854. type := system.addressType;
  2855. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2856. ConvertOperands(left, right);
  2857. binaryExpression.SetLeft(left);
  2858. binaryExpression.SetRight(right);
  2859. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2860. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2861. END;
  2862. type := system.booleanType;
  2863. ELSE
  2864. Error(binaryExpression.position,"operator not defined 3");
  2865. END
  2866. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2867. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2868. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2869. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2870. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2871. CASE operator OF
  2872. |Scanner.Equal: NewBool(strl^=strr^);
  2873. |Scanner.Unequal:NewBool(strl^#strr^);
  2874. |Scanner.Less: NewBool(strl^<strr^);
  2875. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2876. |Scanner.Greater: NewBool(strl^>strr^);
  2877. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2878. ELSE
  2879. Error(binaryExpression.position,"operator not defined 4");
  2880. END;
  2881. END;
  2882. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2883. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2884. type := system.booleanType
  2885. ELSE
  2886. Error(binaryExpression.position,"operator not defined 5");
  2887. END;
  2888. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2889. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2890. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2891. type := system.booleanType;
  2892. ELSE
  2893. Error(binaryExpression.position,"operator not defined 6");
  2894. END
  2895. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2896. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2897. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2898. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2899. type := system.booleanType
  2900. ELSE
  2901. Error(binaryExpression.position,"operator not defined for enumerators");
  2902. END;
  2903. ELSE
  2904. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2905. END;
  2906. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2907. type := system.booleanType;
  2908. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2909. type := system.booleanType;
  2910. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2911. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2912. THEN
  2913. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2914. IF (leftType # rightType) THEN
  2915. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2916. ConvertOperands(left,right); (* operands must be of the same type here *)
  2917. END;
  2918. binaryExpression.SetLeft(left);
  2919. binaryExpression.SetRight(right);
  2920. leftType := left.type.resolved;
  2921. rightType := right.type.resolved;
  2922. END;
  2923. type := leftType;
  2924. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2925. Error(binaryExpression.position,"conversion failed ?");
  2926. IF VerboseErrorMessage THEN
  2927. Printout.Info("left",left);
  2928. Printout.Info("right",right);
  2929. END;
  2930. ELSIF IsIntegerType(leftType) THEN
  2931. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2932. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2933. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2934. Error(binaryExpression.position,"division by zero");
  2935. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2936. Error(binaryExpression.position,"integer division by negative number");
  2937. END;
  2938. END;
  2939. (* constant folding *)
  2940. (* bootstrap64
  2941. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2942. CASE operator OF
  2943. |Scanner.Plus: NewInteger(il+ir,left.type);
  2944. |Scanner.Minus: NewInteger(il-ir,left.type);
  2945. |Scanner.Times: NewInteger(il*ir,left.type);
  2946. |Scanner.Slash:
  2947. IF ir # 0 THEN
  2948. NewReal(il/ir, system.realType);
  2949. END;
  2950. |Scanner.Mod:
  2951. IF ir > 0 THEN
  2952. NewInteger(il MOD ir,left.type);
  2953. END;
  2954. |Scanner.Div:
  2955. IF ir > 0 THEN
  2956. NewInteger(il DIV ir,left.type);
  2957. END;
  2958. |Scanner.Equal: NewBool(il=ir);
  2959. |Scanner.Unequal:NewBool(il#ir);
  2960. |Scanner.Less: NewBool(il<ir);
  2961. |Scanner.LessEqual: NewBool(il<=ir);
  2962. |Scanner.Greater: NewBool(il>ir);
  2963. |Scanner.GreaterEqual: NewBool(il>=ir);
  2964. ELSE Error(binaryExpression.position,"operator not defined 7");
  2965. END;
  2966. ELS*)
  2967. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2968. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2969. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2970. CASE operator OF
  2971. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2972. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2973. |Scanner.Times: NewInteger(hl*hr,left.type);
  2974. |Scanner.Slash:
  2975. IF hr = 0 THEN
  2976. Error(binaryExpression.position,"division by zero");
  2977. ELSE
  2978. IF type.sizeInBits = 64 THEN
  2979. NewReal(hl/hr,system.longrealType);
  2980. ELSE
  2981. NewReal(hl/hr,system.realType)
  2982. END
  2983. END;
  2984. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2985. |Scanner.Mod:
  2986. IF hr = 0 THEN
  2987. Error(binaryExpression.position,"division by zero");
  2988. ELSE
  2989. NewInteger(hl MOD hr, left.type);
  2990. (* bootstrap64
  2991. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2992. *)
  2993. END;
  2994. |Scanner.Div:
  2995. IF hr = 0 THEN
  2996. Error(binaryExpression.position,"division by zero");
  2997. ELSE
  2998. NewInteger(hl DIV hr, left.type);
  2999. (* bootstrap64
  3000. NewInteger(Machine.DivH(hl,hr),left.type);
  3001. *)
  3002. END;
  3003. (* *)
  3004. |Scanner.Equal: NewBool(hl=hr);
  3005. |Scanner.Unequal: NewBool(hl#hr);
  3006. |Scanner.Less: NewBool(hl<hr);
  3007. |Scanner.LessEqual: NewBool(hl<=hr);
  3008. |Scanner.Greater: NewBool(hl>hr);
  3009. |Scanner.GreaterEqual:NewBool(hl>=hr);
  3010. ELSE Error(binaryExpression.position,"operator not defined 8");
  3011. END;
  3012. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3013. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3014. type := left.type
  3015. ELSIF (operator = Scanner.Slash) THEN
  3016. left := NewConversion(left.position,left,system.realType,NIL);
  3017. right := NewConversion(right.position,right,system.realType,NIL);
  3018. binaryExpression.SetLeft(left);
  3019. binaryExpression.SetRight(right);
  3020. type := system.realType
  3021. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3022. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3023. type := system.booleanType
  3024. ELSE
  3025. Error(binaryExpression.position,"operator not defined 9");
  3026. END;
  3027. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3028. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3029. CASE operator OF
  3030. |Scanner.Plus: NewReal(rl+rr,leftType);
  3031. |Scanner.Minus: NewReal(rl-rr,leftType);
  3032. |Scanner.Times:NewReal(rl*rr,leftType);
  3033. |Scanner.Slash:
  3034. IF rr = 0 THEN
  3035. Error(binaryExpression.position,"division by zero");
  3036. ELSE
  3037. NewReal(rl/rr,leftType);
  3038. END
  3039. |Scanner.Equal: NewBool(rl=rr);
  3040. |Scanner.Unequal: NewBool(rl#rr);
  3041. |Scanner.Less: NewBool(rl<rr);
  3042. |Scanner.LessEqual: NewBool(rl<=rr);
  3043. |Scanner.Greater: NewBool(rl>rr);
  3044. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3045. ELSE Error(binaryExpression.position,"operator not defined 10");
  3046. END;
  3047. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3048. type := left.type
  3049. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3050. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3051. type := system.booleanType
  3052. ELSE
  3053. Error(binaryExpression.position,"operator not defined 11");
  3054. IF VerboseErrorMessage THEN
  3055. Printout.Info("left",left);
  3056. Printout.Info("right",right);
  3057. END;
  3058. END;
  3059. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3060. CASE operator OF
  3061. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3062. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3063. ELSE
  3064. Error(binaryExpression.position,"operator not defined");
  3065. IF VerboseErrorMessage THEN
  3066. Printout.Info("left", left);
  3067. Printout.Info("right", right)
  3068. END;
  3069. END;
  3070. IF ~error THEN
  3071. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3072. Error(binaryExpression.position,"division by zero")
  3073. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3074. (* do constant folding *)
  3075. CASE operator OF
  3076. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3077. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3078. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3079. |Scanner.Slash:
  3080. divisor := c * c + d * d;
  3081. ASSERT(divisor # 0);
  3082. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3083. |Scanner.Equal: NewBool((a = c) & (b = d))
  3084. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3085. END
  3086. END
  3087. END
  3088. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3089. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3090. CASE operator OF
  3091. |Scanner.And: NewBool(bl & br);
  3092. |Scanner.Or: NewBool(bl OR br);
  3093. |Scanner.Equal: NewBool(bl = br);
  3094. |Scanner.Unequal: NewBool(bl # br);
  3095. ELSE Error(binaryExpression.position,"operator not defined 12");
  3096. END;
  3097. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3098. type := system.booleanType
  3099. ELSE
  3100. Error(binaryExpression.position,"operator not defined 13");
  3101. END;
  3102. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3103. (* constant folding *)
  3104. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3105. IF operator = Scanner.Equal THEN
  3106. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3107. ELSIF operator = Scanner.Unequal THEN
  3108. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3109. END;
  3110. END;
  3111. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3112. type := system.booleanType;
  3113. ELSE
  3114. Error(binaryExpression.position, "operator not defined");
  3115. END;
  3116. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3117. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3118. CASE operator OF
  3119. |Scanner.Plus: NewSet(sl + sr);
  3120. |Scanner.Minus: NewSet(sl - sr);
  3121. |Scanner.Times: NewSet(sl * sr);
  3122. |Scanner.Slash: NewSet(sl / sr);
  3123. |Scanner.Equal: NewBool(sl=sr);
  3124. |Scanner.Unequal: NewBool(sl#sr);
  3125. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3126. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3127. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3128. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3129. ELSE Error(binaryExpression.position,"operator not defined 14");
  3130. END;
  3131. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3132. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3133. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3134. THEN
  3135. type := system.booleanType
  3136. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3137. type := left.type
  3138. ELSE
  3139. Error(binaryExpression.position,"operator not defined 15");
  3140. END;
  3141. ELSIF IsCharacterType(left.type) THEN
  3142. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3143. CASE operator OF
  3144. |Scanner.Equal: NewBool(cl=cr);
  3145. |Scanner.Unequal: NewBool(cl#cr);
  3146. |Scanner.Less: NewBool(cl<cr);
  3147. |Scanner.LessEqual: NewBool(cl<=cr);
  3148. |Scanner.Greater: NewBool(cl>cr);
  3149. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3150. ELSE Error(binaryExpression.position,"operator not defined 16");
  3151. END;
  3152. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3153. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3154. type := system.booleanType
  3155. ELSE
  3156. Error(binaryExpression.position,"operator not defined 17");
  3157. END;
  3158. ELSE
  3159. Error(binaryExpression.position,"operator not defined 18");
  3160. END;
  3161. ELSE
  3162. Error(binaryExpression.position,"operator not defined 19");
  3163. END;
  3164. IF type = SyntaxTree.invalidType THEN
  3165. result := SyntaxTree.invalidExpression
  3166. ELSE
  3167. result.SetType(type)
  3168. END;
  3169. resolvedExpression := result
  3170. END VisitBinaryExpression;
  3171. (** resolve a range expression of the from <<first .. last BY step>>
  3172. - depending on the context different things are checked:
  3173. ArrayIndex:
  3174. - components must be integers
  3175. - replace missing lower bound with 0
  3176. - replace missing upper bound with MAX(LONGINT)
  3177. - replace missing step size with 1
  3178. SetElement:
  3179. - components must be integers
  3180. - replace missing lower bound with 0
  3181. - replace missing upper bound with MAX(SET)
  3182. - must not have step size
  3183. CaseGuard:
  3184. - components must be constant
  3185. - components must be integers or characters
  3186. - must have lower and upper bound present
  3187. - components are made compatible
  3188. - must not have step size
  3189. - if error: return invalidExpression
  3190. **)
  3191. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3192. VAR
  3193. hasError: BOOLEAN;
  3194. first, last, step: SyntaxTree.Expression;
  3195. BEGIN
  3196. hasError := FALSE;
  3197. first := x.first;
  3198. last := x.last;
  3199. step := x.step;
  3200. (* check lower bound *)
  3201. IF x.context = SyntaxTree.CaseGuard THEN
  3202. IF first = NIL THEN
  3203. Error(x.position, "missing lower bound");
  3204. hasError := TRUE
  3205. ELSE
  3206. first := ResolveExpression(first);
  3207. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3208. Error(first.position, "lower bound not integer or character");
  3209. hasError := TRUE
  3210. ELSE
  3211. IF first IS SyntaxTree.StringValue THEN
  3212. (* add conversion from string to character *)
  3213. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3214. END
  3215. END;
  3216. (* check if expression is constant *)
  3217. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3218. (* error already reported *)
  3219. hasError := TRUE
  3220. END
  3221. END
  3222. ELSE (* ArrayIndex, SetElement *)
  3223. IF first = NIL THEN
  3224. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3225. END;
  3226. first := ResolveExpression(first);
  3227. IF IsIntegerType(first.type.resolved) THEN
  3228. first := NewConversion(first.position, first, system.lenType, NIL)
  3229. ELSE
  3230. Error(first.position, "lower bound not integer");
  3231. hasError := TRUE
  3232. END
  3233. END;
  3234. (* check upper bound *)
  3235. IF x.context = SyntaxTree.CaseGuard THEN
  3236. IF last = NIL THEN
  3237. Error(x.position, "missing upper bound");
  3238. hasError := TRUE
  3239. ELSE
  3240. last := ResolveExpression(last);
  3241. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3242. Error(last.position, "lower bound not integer or character");
  3243. hasError := TRUE
  3244. ELSE
  3245. IF last IS SyntaxTree.StringValue THEN
  3246. (* add conversion from string to character *)
  3247. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3248. END
  3249. END;
  3250. (* check if expression is constant *)
  3251. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3252. (* error already reported *)
  3253. hasError := TRUE
  3254. ELSE
  3255. (* try to make lower and upper bound compatible *)
  3256. ConvertOperands(first, last);
  3257. IF first.type.resolved # last.type.resolved THEN
  3258. Error(x.position, "lower and upper bounds incompatible");
  3259. hasError := TRUE
  3260. END
  3261. END
  3262. END
  3263. ELSE (* ArrayIndex, SetElement *)
  3264. IF last = NIL THEN
  3265. IF x.context = SyntaxTree.ArrayIndex THEN
  3266. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3267. ELSE
  3268. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3269. END
  3270. END;
  3271. last := ResolveExpression(last);
  3272. IF IsIntegerType(last.type.resolved) THEN
  3273. last := NewConversion(last.position, last, system.lenType, NIL)
  3274. ELSE
  3275. Error(last.position, "upper bound not integer");
  3276. hasError := TRUE
  3277. END
  3278. END;
  3279. (* check step size *)
  3280. IF x.context = SyntaxTree.ArrayIndex THEN
  3281. IF step = NIL THEN
  3282. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3283. END;
  3284. step := ResolveExpression(step);
  3285. IF IsIntegerType(step.type.resolved) THEN
  3286. step := NewConversion(step.position, step, system.lenType, NIL)
  3287. ELSE
  3288. Error(step.position, "step size not integer");
  3289. hasError := TRUE
  3290. END
  3291. ELSE (* SetElement, CaseGuard *)
  3292. IF step # NIL THEN
  3293. Error(last.position, "step size not allowed in this context");
  3294. hasError := TRUE
  3295. END
  3296. END;
  3297. IF hasError THEN
  3298. resolvedExpression := SyntaxTree.invalidExpression
  3299. ELSE
  3300. x.SetFirst(first);
  3301. x.SetLast(last);
  3302. x.SetStep(step);
  3303. x.SetType(system.rangeType);
  3304. resolvedExpression := x;
  3305. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3306. END
  3307. END VisitRangeExpression;
  3308. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3309. BEGIN
  3310. x.SetType(NIL);
  3311. resolvedExpression := x;
  3312. END VisitTensorRangeExpression;
  3313. (** resolve the expression d and return result as designator
  3314. - resolve expression
  3315. - if expression is a designator then return designator else error message and return invalidDesignator
  3316. **)
  3317. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3318. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3319. BEGIN
  3320. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3321. resolved := ResolveExpression(d);
  3322. IF resolved = SyntaxTree.invalidExpression THEN
  3323. (* error should already have been reported *)
  3324. result := SyntaxTree.invalidDesignator;
  3325. ELSIF resolved IS SyntaxTree.Designator THEN
  3326. result := resolved(SyntaxTree.Designator);
  3327. ELSE
  3328. Error(d.position,"is no designator ! ");
  3329. result := SyntaxTree.invalidDesignator;
  3330. END;
  3331. (* result.type might be nil. *)
  3332. RETURN result
  3333. END ResolveDesignator;
  3334. (**
  3335. symbol designator generated in this module
  3336. nothing to be resolved
  3337. **)
  3338. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3339. BEGIN
  3340. resolvedExpression := x;
  3341. END VisitSymbolDesignator;
  3342. (**
  3343. self designator generated in this module
  3344. nothing to be resolved
  3345. **)
  3346. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3347. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3348. BEGIN
  3349. (* check if in record scope *)
  3350. scope := currentScope;
  3351. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3352. scope := scope.outerScope;
  3353. END;
  3354. IF scope = NIL THEN (* in module scope *)
  3355. x.SetType(system.anyType);
  3356. ELSIF scope IS SyntaxTree.CellScope THEN
  3357. cell := scope(SyntaxTree.CellScope).ownerCell;
  3358. x.SetType(cell);
  3359. ELSE (* in record scope *)
  3360. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3361. IF (record # NIL) & (record.pointerType # NIL) THEN
  3362. type := ResolveType(record.pointerType);
  3363. x.SetType(type);
  3364. ELSE
  3365. x.SetType(record);
  3366. END;
  3367. END;
  3368. resolvedExpression := x;
  3369. END VisitSelfDesignator;
  3370. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3371. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3372. BEGIN
  3373. scope := currentScope;
  3374. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3375. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3376. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3377. returnType := procedureType.returnType;
  3378. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3379. THEN
  3380. x.SetType(returnType);
  3381. ELSE
  3382. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3383. x.SetType(SyntaxTree.invalidType);
  3384. END;
  3385. ELSE
  3386. Error(x.position,"forbidden access to result designator");
  3387. x.SetType(SyntaxTree.invalidType);
  3388. END;
  3389. x.SetAssignable(TRUE);
  3390. resolvedExpression := x;
  3391. END VisitResultDesignator;
  3392. (**
  3393. return symbol designator as an expression
  3394. - if symbol is a constant then return the constant value expression
  3395. - else
  3396. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3397. - if symbol is a guarded variable then return a TypeGuardDesignator
  3398. - else return a symbol designator
  3399. **)
  3400. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3401. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3402. guardType: SyntaxTree.Type;
  3403. BEGIN
  3404. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3405. result := SyntaxTree.invalidExpression;
  3406. ASSERT(symbol # NIL);
  3407. (*
  3408. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3409. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3410. Error(position, "type not allowed here");
  3411. ELS *)
  3412. (* not needed any more as values are stored in the expression
  3413. IF symbol IS SyntaxTree.Constant THEN
  3414. result := symbol(SyntaxTree.Constant).value
  3415. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3416. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3417. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3418. ELSE
  3419. result := symbol(SyntaxTree.Constant).value
  3420. END;
  3421. ELSE
  3422. *)
  3423. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3424. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3425. THEN
  3426. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3427. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3428. left := NewDereferenceDesignator(position,left);
  3429. left.SetHidden(TRUE);
  3430. END;
  3431. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3432. scope := currentScope;
  3433. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3434. scope := scope.outerScope;
  3435. END;
  3436. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3437. Error(position, "forbidden access to symbol in parent procedure scope");
  3438. END;
  3439. END;
  3440. 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);
  3441. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3442. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3443. ELSE
  3444. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3445. END;
  3446. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3447. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3448. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3449. result.SetType(symbol.type);
  3450. result.SetAssignable(assignable);
  3451. symbol.MarkUsed;
  3452. IF symbol IS SyntaxTree.Constant THEN
  3453. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3454. END;
  3455. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3456. variableAccessed := TRUE
  3457. END;
  3458. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3459. IF GetGuard(symbol,guardType) THEN
  3460. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3461. END;
  3462. END;
  3463. ASSERT(result.type # NIL);
  3464. RETURN result
  3465. END NewSymbolDesignator;
  3466. (** check and resolve an identifier designator "identifier"
  3467. - if identifier = self then return SelfDesignator
  3468. - else find symbol in current scope
  3469. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3470. **)
  3471. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3472. VAR symbol: SyntaxTree.Symbol;
  3473. BEGIN
  3474. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3475. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3476. IF symbol # NIL THEN
  3477. ResolveSymbol(symbol);
  3478. ASSERT(symbol.type # NIL);
  3479. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3480. ELSE
  3481. Error(identifierDesignator.position,"Undeclared Identifier");
  3482. IF VerboseErrorMessage THEN
  3483. Printout.Info("undeclared identifier designator",identifierDesignator);
  3484. END;
  3485. resolvedExpression := SyntaxTree.invalidDesignator;
  3486. END;
  3487. END VisitIdentifierDesignator;
  3488. (** check and resolve a selector designator of the form left.designator
  3489. - if left is a pointer type then do auto dereferenciation
  3490. - left denotes a search scope:
  3491. - if left type is import type then set search scope to respective module
  3492. - if left type is enumeration type then set search scope to respective enumeration scope
  3493. - elsif left type is record type then set search scope to record scope
  3494. - search symbol in computed scope
  3495. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3496. **)
  3497. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3498. VAR
  3499. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3500. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3501. BEGIN
  3502. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3503. left := ResolveDesignator(selectorDesignator.left);
  3504. result := SyntaxTree.invalidDesignator;
  3505. IF left # NIL THEN
  3506. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3507. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3508. END;
  3509. scope := NIL;
  3510. IF left.type = NIL THEN
  3511. Error(selectorDesignator.position,"field on nil typed designator");
  3512. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3513. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3514. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3515. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3516. module := symbol(SyntaxTree.Import).module;
  3517. IF module # NIL THEN
  3518. scope := module.moduleScope
  3519. ELSE
  3520. Error(left.position,"module not loaded");
  3521. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3522. END;
  3523. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3524. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3525. ASSERT(scope # NIL)
  3526. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3527. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3528. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3529. IF type IS SyntaxTree.EnumerationType THEN
  3530. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3531. ELSE
  3532. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3533. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3534. END;
  3535. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3536. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3537. ELSE
  3538. Error(selectorDesignator.position,"field on non-record type designator");
  3539. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3540. END;
  3541. symbol := NIL;
  3542. IF scope # NIL THEN
  3543. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3544. IF symbol # NIL THEN
  3545. ResolveSymbol(symbol);
  3546. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3547. symbol.MarkUsed
  3548. ELSE
  3549. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3550. IF VerboseErrorMessage THEN
  3551. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3552. Printout.Info("scope", scope);
  3553. Printout.Info("left", left);
  3554. Printout.Info("undeclared identifier",selectorDesignator);
  3555. Printout.Info("left resolved designator",left);
  3556. END
  3557. END;
  3558. END;
  3559. END;
  3560. resolvedExpression := result;
  3561. END VisitSelectorDesignator;
  3562. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3563. VAR len,idx: Basic.Integer;
  3564. BEGIN
  3565. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3566. IF idx < 0 THEN
  3567. Error(index.position,"index out of bounds (too small)")
  3568. ELSE
  3569. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3570. Error(index.position,"index out of bounds (too large)");
  3571. END;
  3572. END;
  3573. END;
  3574. END IndexCheck;
  3575. (*
  3576. - if index designator has not type, use newBaseType as its type
  3577. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3578. - special rule: if static array of dynamic array occurs, make it all dynamic
  3579. index designator type: new base type: new index designator type:
  3580. NIL z z
  3581. ARRAY [x, y] z ARRAY [x, y] OF z
  3582. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3583. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3584. *)
  3585. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3586. VAR
  3587. mathArrayType: SyntaxTree.MathArrayType;
  3588. makeDynamic: BOOLEAN;
  3589. BEGIN
  3590. IF indexDesignator.type = NIL THEN
  3591. indexDesignator.SetType(newBaseType)
  3592. ELSE
  3593. (* index designator must be a of math array type *)
  3594. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3595. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3596. (* determine if all arrays have to be made dynamic *)
  3597. makeDynamic :=
  3598. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3599. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3600. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3601. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3602. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3603. END;
  3604. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3605. mathArrayType.SetArrayBase(newBaseType)
  3606. END
  3607. END SetIndexBaseType;
  3608. (** check and append index list element to index designator of math array
  3609. - check validity of single index or array range
  3610. - compute new type
  3611. - if range then create new array type (calculate length of resulting array)
  3612. - otherwise take sourceArray.arrayBase as new type
  3613. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3614. **)
  3615. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3616. VAR
  3617. targetArray: SyntaxTree.MathArrayType;
  3618. first, last, step: SyntaxTree.Expression;
  3619. firstValue, lastValue, stepValue, length: Basic.Integer;
  3620. rangeExpression: SyntaxTree.RangeExpression;
  3621. BEGIN
  3622. IF indexListItem.type = SyntaxTree.invalidType THEN
  3623. (* error already handled *)
  3624. indexDesignator.parameters.AddExpression(indexListItem)
  3625. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3626. indexDesignator.HasRange;
  3627. indexDesignator.HasTensorRange;
  3628. indexDesignator.parameters.AddExpression(indexListItem);
  3629. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3630. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3631. IndexCheck(indexListItem, sourceArray.length);
  3632. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3633. indexDesignator.parameters.AddExpression(indexListItem)
  3634. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3635. indexDesignator.HasRange;
  3636. (* if the range is given as an array range expression, check the validity of its components *)
  3637. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3638. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3639. first := rangeExpression.first;
  3640. last := rangeExpression.last;
  3641. step := rangeExpression.step;
  3642. (* perform static checks on range components *)
  3643. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3644. Error(indexListItem.position,"lower bound of array range too small")
  3645. END;
  3646. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3647. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3648. Error(indexListItem.position,"upper bound of array range too large")
  3649. END
  3650. END;
  3651. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3652. Error(indexListItem.position,"invalid step size")
  3653. END;
  3654. (* add conversions to size type *)
  3655. (* TODO: needed? *)
  3656. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3657. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3658. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3659. END;
  3660. IF indexDesignator.hasTensorRange THEN
  3661. (* the index designator's base type is a tensor: leave it as is *)
  3662. ELSE
  3663. (* append a new math array to the index designator's base type *)
  3664. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3665. IF ~error THEN
  3666. (*
  3667. (* optimization: calculate length of target array for static ranges *)
  3668. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3669. IF IsStaticallyOpenRange(rangeExpression) THEN
  3670. (* range is open ('*'): reuse source array length as target array length *)
  3671. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3672. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3673. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3674. IF lastValue = MAX(LONGINT) THEN
  3675. IF IsIntegerValue(sourceArray.length, length) THEN
  3676. lastValue := length - 1;
  3677. isStaticTargetArrayLength := TRUE
  3678. ELSE
  3679. isStaticTargetArrayLength := FALSE
  3680. END
  3681. ELSE
  3682. isStaticTargetArrayLength := TRUE
  3683. END;
  3684. IF isStaticTargetArrayLength THEN
  3685. (* calculate static target array length *)
  3686. IF firstValue > lastValue THEN
  3687. length := 0
  3688. ELSE
  3689. length := 1 + lastValue - firstValue;
  3690. IF length MOD stepValue = 0 THEN
  3691. length := length DIV stepValue
  3692. ELSE
  3693. length := length DIV stepValue + 1
  3694. END
  3695. END;
  3696. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3697. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3698. ASSERT(targetArray.form = SyntaxTree.Static)
  3699. END
  3700. END
  3701. END
  3702. *)
  3703. END;
  3704. SetIndexBaseType(indexDesignator, targetArray)
  3705. END;
  3706. indexDesignator.parameters.AddExpression(indexListItem)
  3707. ELSE
  3708. Error(position,"invalid index list item");
  3709. END;
  3710. END AppendMathIndex;
  3711. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3712. VAR parameters: SyntaxTree.ExpressionList;
  3713. BEGIN
  3714. parameters := index.parameters;
  3715. IF (expression.type = NIL) THEN
  3716. Error(position, "invalid index");
  3717. ELSIF IsIntegerType(expression.type.resolved) THEN
  3718. IF over IS SyntaxTree.ArrayType THEN
  3719. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3720. ELSIF over IS SyntaxTree.StringType THEN
  3721. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3722. END;
  3723. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3724. parameters.AddExpression(expression);
  3725. ELSE
  3726. Error(position, "invalid index");
  3727. END;
  3728. END AppendIndex;
  3729. (** convert an expression to math array type
  3730. if expression is of math array type: return expression itself
  3731. if expression is of array-structured object type: return an index operator call on it
  3732. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3733. otherwise: return invalid expression
  3734. **)
  3735. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3736. VAR
  3737. result: SyntaxTree.Expression;
  3738. mathArrayType: SyntaxTree.MathArrayType;
  3739. BEGIN
  3740. IF expression.type = NIL THEN
  3741. result := SyntaxTree.invalidExpression
  3742. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3743. (* expression of math array type *)
  3744. result := expression
  3745. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3746. (* expression of array-structured object type *)
  3747. mathArrayType := MathArrayStructureOfType(expression.type);
  3748. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3749. ELSE
  3750. result := SyntaxTree.invalidExpression
  3751. END;
  3752. RETURN result
  3753. END ConvertToMathArray;
  3754. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3755. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3756. VAR
  3757. result: SyntaxTree.ExpressionList;
  3758. i: LONGINT;
  3759. BEGIN
  3760. result := SyntaxTree.NewExpressionList();
  3761. FOR i := 1 TO itemCount DO
  3762. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3763. END;
  3764. RETURN result
  3765. END ListOfOpenRanges;
  3766. (** create a procedure call designator for an index operator call on an array-structured object type
  3767. - use given index list as actual parameters
  3768. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3769. **)
  3770. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3771. VAR
  3772. operator: SyntaxTree.Operator;
  3773. expression: SyntaxTree.Expression;
  3774. actualParameters, tempList: SyntaxTree.ExpressionList;
  3775. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3776. result, tempDesignator: SyntaxTree.Designator;
  3777. recordType: SyntaxTree.RecordType;
  3778. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3779. i, hashValue, indexListSize, indexListKind: LONGINT;
  3780. castReturnType: SyntaxTree.MathArrayType;
  3781. BEGIN
  3782. ASSERT(IsArrayStructuredObjectType(left.type));
  3783. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3784. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3785. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3786. indexListSize := indexList.Length();
  3787. indexListKind := 0;
  3788. containsNonRange := FALSE;
  3789. FOR i := 0 TO indexList.Length() - 1 DO
  3790. indexListKind := indexListKind * 2;
  3791. expression := indexList.GetExpression(i);
  3792. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3793. INC(indexListKind)
  3794. ELSE
  3795. containsNonRange := TRUE
  3796. END
  3797. END;
  3798. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3799. (* select applicable index operator
  3800. - try to look up optimal index operator
  3801. - if not present, use operator on ranges
  3802. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3803. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3804. *)
  3805. usesGeneralOperator := FALSE;
  3806. IF rhs # NIL THEN
  3807. (* write operator *)
  3808. IF hashValue = -1 THEN
  3809. operator := NIL
  3810. ELSE
  3811. operator := recordType.arrayAccessOperators.write[hashValue];
  3812. END;
  3813. IF operator = NIL THEN
  3814. usesPureRangeOperator := TRUE;
  3815. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3816. operator := recordType.arrayAccessOperators.generalWrite;
  3817. usesGeneralOperator := TRUE
  3818. ELSE
  3819. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3820. operator := recordType.arrayAccessOperators.write[hashValue];
  3821. END
  3822. END
  3823. ELSE
  3824. (* read operator *)
  3825. IF hashValue = -1 THEN
  3826. operator := NIL
  3827. ELSE
  3828. operator := recordType.arrayAccessOperators.read[hashValue];
  3829. END;
  3830. IF operator = NIL THEN
  3831. usesPureRangeOperator := TRUE;
  3832. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3833. operator := recordType.arrayAccessOperators.generalRead;
  3834. usesGeneralOperator := TRUE
  3835. ELSE
  3836. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3837. operator := recordType.arrayAccessOperators.read[hashValue];
  3838. END
  3839. END
  3840. END;
  3841. IF operator = NIL THEN
  3842. Error(position, "call of undeclared [] operator");
  3843. result := SyntaxTree.invalidDesignator;
  3844. ELSE
  3845. (* determine if reshaping is needed *)
  3846. needsReshaping := containsNonRange & usesPureRangeOperator;
  3847. (* import OCArrayBase if reshaping is needed *)
  3848. IF needsReshaping & ~arrayBaseImported THEN
  3849. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3850. arrayBaseImported := TRUE
  3851. END;
  3852. (* add the index list item to the list of actual parameters
  3853. - for general operators: add a single inline array containing the index list items as parameter
  3854. - otherwise: add all index list items as individual parameters
  3855. *)
  3856. actualParameters := SyntaxTree.NewExpressionList();
  3857. IF usesGeneralOperator THEN
  3858. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3859. END;
  3860. FOR i := 0 TO indexListSize - 1 DO
  3861. expression := indexList.GetExpression(i);
  3862. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3863. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3864. tempList := SyntaxTree.NewExpressionList();
  3865. tempList.AddExpression(expression);
  3866. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3867. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3868. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3869. END;
  3870. IF usesGeneralOperator THEN
  3871. tempMathArrayExpression.elements.AddExpression(expression);
  3872. ELSE
  3873. actualParameters.AddExpression(expression)
  3874. END
  3875. END;
  3876. IF usesGeneralOperator THEN
  3877. actualParameters.AddExpression(tempMathArrayExpression)
  3878. END;
  3879. IF rhs # NIL THEN
  3880. (* add actual parameter for RHS *)
  3881. IF needsReshaping THEN
  3882. (* reshape using OCArrayBase.ExpandDimensions *)
  3883. tempList := SyntaxTree.NewExpressionList();
  3884. (* source array *)
  3885. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3886. tempList.AddExpression(rhs);
  3887. ELSE
  3888. (* convert scalar to one-dimensional array *)
  3889. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3890. tempMathArrayExpression.elements.AddExpression(rhs);
  3891. tempList.AddExpression(tempMathArrayExpression)
  3892. END;
  3893. (* list of kept dimensions *)
  3894. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3895. FOR i := 0 TO indexListSize - 1 DO
  3896. expression := indexList.GetExpression(i);
  3897. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3898. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3899. ELSE
  3900. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3901. END
  3902. END;
  3903. tempList.AddExpression(tempMathArrayExpression);
  3904. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3905. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3906. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3907. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3908. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3909. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3910. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3911. expression.SetType(castReturnType);
  3912. ELSE
  3913. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3914. END;
  3915. actualParameters.AddExpression(expression)
  3916. ELSE
  3917. actualParameters.AddExpression(rhs)
  3918. END
  3919. END;
  3920. (* add dereference operator and create procedure call designator *)
  3921. ASSERT(left IS SyntaxTree.Designator);
  3922. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3923. ASSERT(expression IS SyntaxTree.Designator);
  3924. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3925. IF (rhs = NIL) & needsReshaping THEN
  3926. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3927. tempList := SyntaxTree.NewExpressionList();
  3928. FOR i := 0 TO indexList.Length() - 1 DO
  3929. expression := indexList.GetExpression(i);
  3930. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3931. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3932. ELSE
  3933. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3934. END
  3935. END;
  3936. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3937. END;
  3938. IF rhs = NIL THEN
  3939. (* special rule: index read operator calls are considered to be assignable *)
  3940. result.SetAssignable(TRUE)
  3941. END;
  3942. (* put information about this index operator call into the resulting designator *)
  3943. result.SetRelatedAsot(left);
  3944. result.SetRelatedIndexList(indexList)
  3945. END;
  3946. RETURN result
  3947. END NewIndexOperatorCall;
  3948. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3949. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3950. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3951. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3952. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3953. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3954. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3955. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3956. CONST trace = FALSE;
  3957. BEGIN
  3958. IF trace THEN
  3959. FOR i := 0 TO actualParameters.Length()-1 DO
  3960. Printout.Info("par", actualParameters.GetExpression(i));
  3961. END;
  3962. END;
  3963. operator := scope.firstOperator;
  3964. WHILE(operator # NIL) DO
  3965. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3966. procedureType := operator.type(SyntaxTree.ProcedureType);
  3967. distance := Distance(system, procedureType,actualParameters);
  3968. IF trace THEN Printout.Info("check op ",operator) END;
  3969. IF distance < bestDistance THEN
  3970. IF trace THEN Printout.Info("taken op",operator) END;
  3971. bestDistance := distance;
  3972. bestOperator := operator;
  3973. END;
  3974. END;
  3975. operator := operator.nextOperator;
  3976. END;
  3977. END FindInScope;
  3978. BEGIN
  3979. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3980. IF oper = 0 THEN (* index *)
  3981. identifier := SyntaxTree.NewIdentifier("[]");
  3982. ELSE
  3983. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3984. END;
  3985. WHILE (recordType # NIL) DO
  3986. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3987. recordType := recordType.GetBaseRecord();
  3988. END;
  3989. RETURN bestOperator
  3990. END FindOperator;
  3991. BEGIN
  3992. type := left.type.resolved;
  3993. IF type IS SyntaxTree.RecordType THEN
  3994. pointer := FALSE;
  3995. recordType := type(SyntaxTree.RecordType);
  3996. ELSE
  3997. pointer := TRUE;
  3998. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3999. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4000. END;
  4001. actualParameters := SyntaxTree.NewExpressionList();
  4002. IF parameters # NIL THEN
  4003. FOR i := 0 TO parameters.Length()-1 DO
  4004. expression := ResolveExpression(parameters.GetExpression(i));
  4005. actualParameters.AddExpression(expression);
  4006. END;
  4007. END;
  4008. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  4009. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4010. IF op # NIL THEN
  4011. designator := left(SyntaxTree.Designator);
  4012. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  4013. expression := NewSymbolDesignator(position, designator , op);
  4014. ASSERT(expression IS SyntaxTree.Designator);
  4015. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4016. result.SetRelatedAsot(left);
  4017. result.SetRelatedIndexList(parameters);
  4018. (* check if write operator exists, for var parameters *)
  4019. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4020. actualParameters := SyntaxTree.NewExpressionList();
  4021. FOR i := 0 TO parameters.Length()-1 DO
  4022. expression := ResolveExpression(parameters.GetExpression(i));
  4023. actualParameters.AddExpression(expression);
  4024. END;
  4025. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4026. actualParameters.AddExpression(rhs);
  4027. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4028. IF op = NIL THEN rhs := NIL END;
  4029. END;
  4030. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4031. ELSE
  4032. result := NIL;
  4033. END;
  4034. RETURN result;
  4035. END NewObjectOperatorCall;
  4036. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4037. 1. convert bracket designator chains into a single one that contains separators
  4038. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4039. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4040. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4041. - if an array or math array is indexed over, create index designator
  4042. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4043. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4044. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4045. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4046. - if an array-structured object type is indexed over, create procedure call designator
  4047. e.g.: a[x, y] -> a^."[]"(x, y)
  4048. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4049. - a[i, *] = a[i][*]
  4050. - a[*, i] # a[*][i]
  4051. Because:
  4052. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4053. - 'i-th column' = a[*, i]
  4054. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4055. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4056. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4057. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4058. **)
  4059. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4060. VAR
  4061. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4062. indexDesignator: SyntaxTree.IndexDesignator;
  4063. designator: SyntaxTree.Designator;
  4064. type: SyntaxTree.Type;
  4065. recordType: SyntaxTree.RecordType;
  4066. expression, rhs: SyntaxTree.Expression;
  4067. indexList: SyntaxTree.ExpressionList;
  4068. i: LONGINT;
  4069. hasError, done: BOOLEAN;
  4070. PROCEDURE FinalizeIndexDesignator;
  4071. BEGIN
  4072. IF indexDesignator # NIL THEN
  4073. (* the end of a tensor has been reached: *)
  4074. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4075. SetIndexBaseType(indexDesignator, type);
  4076. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4077. designator := indexDesignator;
  4078. type := designator.type.resolved;
  4079. indexDesignator := NIL;
  4080. ASSERT(SyntaxTree.Resolved IN type.state)
  4081. END
  4082. END FinalizeIndexDesignator;
  4083. BEGIN
  4084. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4085. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4086. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4087. (* copy all index list entries including a separator to the left bracket designator *)
  4088. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4089. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4090. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4091. END;
  4092. (* propagate the related RHS *)
  4093. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4094. (* only resolve left bracket designator and use as final result *)
  4095. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4096. ELSE
  4097. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4098. designator := ResolveDesignator(bracketDesignator.left);
  4099. type := designator.type.resolved;
  4100. indexDesignator := NIL;
  4101. (*!!! clean up *)
  4102. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4103. OR (type IS SyntaxTree.RecordType)
  4104. THEN
  4105. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4106. IF resolvedExpression = NIL THEN
  4107. Error(bracketDesignator.position,"undefined operator");
  4108. resolvedExpression := SyntaxTree.invalidDesignator
  4109. END;
  4110. RETURN;
  4111. END;
  4112. i := 0;
  4113. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4114. expression := bracketDesignator.parameters.GetExpression(i);
  4115. expression := ResolveExpression(expression);
  4116. bracketDesignator.parameters.SetExpression(i, expression);
  4117. IF expression = SyntaxTree.indexListSeparator THEN
  4118. (* finalize an existing index designator if needed *)
  4119. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4120. INC(i)
  4121. ELSE
  4122. (* do auto-dereferencing if needed *)
  4123. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4124. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4125. & (i=0)*)
  4126. THEN
  4127. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4128. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4129. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4130. designator := SyntaxTree.invalidDesignator;
  4131. type := SyntaxTree.invalidType
  4132. ELSE
  4133. FinalizeIndexDesignator;
  4134. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4135. type := designator.type.resolved
  4136. END
  4137. END;
  4138. (* create a new index designator, if needed *)
  4139. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4140. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4141. indexDesignator.SetAssignable(designator.assignable);
  4142. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4143. (* designator := indexDesignator *)
  4144. END;
  4145. IF type = SyntaxTree.invalidType THEN
  4146. (* error already handled *)
  4147. INC(i)
  4148. ELSIF type IS SyntaxTree.ArrayType THEN
  4149. (* indexing over an array *)
  4150. ASSERT(indexDesignator # NIL);
  4151. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4152. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4153. INC(i)
  4154. ELSIF type IS SyntaxTree.StringType THEN
  4155. (* indexing over an array *)
  4156. ASSERT(indexDesignator # NIL);
  4157. AppendIndex(expression.position, indexDesignator, expression, type);
  4158. type := type(SyntaxTree.StringType).baseType.resolved;
  4159. INC(i)
  4160. ELSIF type IS SyntaxTree.MathArrayType THEN
  4161. (* indexing over a math array *)
  4162. ASSERT(indexDesignator # NIL);
  4163. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4164. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4165. INC(i)
  4166. ELSIF IsArrayStructuredObjectType(type) THEN
  4167. (* indexing over ASOTs *)
  4168. FinalizeIndexDesignator;
  4169. ASSERT(type IS SyntaxTree.PointerType);
  4170. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4171. (*
  4172. - collect index list items from bracket designator that belong to ASOT
  4173. - check for errors
  4174. *)
  4175. indexList := SyntaxTree.NewExpressionList();
  4176. hasError := FALSE;
  4177. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4178. (* indexing over tensor ASOT:
  4179. - stop at index list end or separator
  4180. - dimensionality is given by number of index list items
  4181. *)
  4182. done := FALSE;
  4183. WHILE ~done DO
  4184. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4185. done := TRUE;
  4186. ELSE
  4187. expression := bracketDesignator.parameters.GetExpression(i);
  4188. IF expression = SyntaxTree.indexListSeparator THEN
  4189. done := TRUE;
  4190. ELSE
  4191. expression := ResolveExpression(expression);
  4192. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4193. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4194. hasError := TRUE
  4195. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4196. Error(expression.position, "integer or range expected");
  4197. expression := SyntaxTree.invalidExpression;
  4198. hasError := TRUE
  4199. END;
  4200. indexList.AddExpression(expression)
  4201. END;
  4202. INC(i)
  4203. END
  4204. END
  4205. ELSE
  4206. (* indexing over non-tensor ASOT:
  4207. - ignore separators
  4208. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4209. *)
  4210. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4211. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4212. expression := bracketDesignator.parameters.GetExpression(i);
  4213. ELSE
  4214. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4215. END;
  4216. IF expression # SyntaxTree.indexListSeparator THEN
  4217. expression := ResolveExpression(expression);
  4218. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4219. Error(expression.position, "integer or range expected");
  4220. expression := SyntaxTree.invalidExpression;
  4221. hasError := TRUE
  4222. END;
  4223. indexList.AddExpression(expression)
  4224. END;
  4225. INC(i)
  4226. END;
  4227. END;
  4228. IF hasError THEN
  4229. designator := SyntaxTree.invalidDesignator;
  4230. type := SyntaxTree.invalidType;
  4231. ELSE
  4232. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4233. and the last entry in the index list belongs to the array-structured object type in question.
  4234. E.g.: for a 2-dimensional array-structured object type:
  4235. - 'lhs := asot[1, 2]' -> read mode
  4236. - 'asot[1, 2] := rhs' -> write mode
  4237. - 'asot[1, 2, 3] := rhs' -> read mode
  4238. *)
  4239. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4240. rhs := bracketDesignator.relatedRhs
  4241. ELSE
  4242. rhs := NIL
  4243. END;
  4244. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4245. type := designator.type
  4246. END
  4247. ELSE
  4248. Error(expression.position,"indexing over non-array type");
  4249. designator := SyntaxTree.invalidDesignator;
  4250. type := SyntaxTree.invalidType;
  4251. INC(i)
  4252. END
  4253. END
  4254. END;
  4255. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4256. resolvedExpression := designator
  4257. END
  4258. END VisitBracketDesignator;
  4259. (** check and resolve expression list
  4260. - resolve each expression in an expression list
  4261. - returns true if and only if all statements could have successfully been resolved
  4262. **)
  4263. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4264. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4265. BEGIN
  4266. result := TRUE;
  4267. FOR i := 0 TO expressionList.Length()-1 DO
  4268. expression := ResolveExpression(expressionList.GetExpression(i));
  4269. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4270. expressionList.SetExpression(i,expression);
  4271. END;
  4272. RETURN result
  4273. END ExpressionList;
  4274. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4275. BEGIN
  4276. type := type.resolved;
  4277. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4278. RETURN TRUE
  4279. ELSIF system.CanPassInRegister # NIL THEN
  4280. RETURN system.CanPassInRegister(type);
  4281. ELSE
  4282. RETURN FALSE
  4283. END;
  4284. END CanPassInRegister;
  4285. (** return procedure call designator left(actualParameters)
  4286. - check realtime procedure call in realtime procedure
  4287. - check number of parameters
  4288. - check parameter compatibility
  4289. return invalidDesignator if error
  4290. **)
  4291. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4292. VAR result: SyntaxTree.Designator;
  4293. numberFormalParameters, numberActualParameters: LONGINT;
  4294. formalType: SyntaxTree.ProcedureType;
  4295. formalParameter: SyntaxTree.Parameter;
  4296. actualParameter: SyntaxTree.Expression;
  4297. i: LONGINT;
  4298. self: SyntaxTree.Expression;
  4299. BEGIN
  4300. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4301. result := SyntaxTree.invalidDesignator;
  4302. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4303. numberFormalParameters := formalType.numberParameters;
  4304. numberActualParameters := actualParameters.Length();
  4305. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4306. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4307. END;
  4308. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4309. self := left.left;
  4310. IF (self # NIL) & ~IsVariable(self) THEN
  4311. Error(self.position, "Non-variable expression on variable receiver");
  4312. END;
  4313. END;
  4314. IF ~ExpressionList(actualParameters) THEN
  4315. result := SyntaxTree.invalidDesignator
  4316. ELSE
  4317. IF numberActualParameters <= numberFormalParameters THEN
  4318. formalParameter := formalType.firstParameter;
  4319. FOR i := 0 TO numberActualParameters-1 DO
  4320. actualParameter := actualParameters.GetExpression(i);
  4321. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4322. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4323. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4324. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4325. ELSE
  4326. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4327. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4328. END;
  4329. actualParameters.SetExpression(i,actualParameter);
  4330. END;
  4331. formalParameter := formalParameter.nextParameter;
  4332. END;
  4333. WHILE (formalParameter # NIL) DO
  4334. IF formalParameter.defaultValue # NIL THEN
  4335. actualParameters.AddExpression(formalParameter.defaultValue);
  4336. formalParameter := formalParameter.nextParameter
  4337. ELSE
  4338. Error(position, "less actual than formal parameters");
  4339. formalParameter := NIL;
  4340. END;
  4341. END;
  4342. ELSE
  4343. Error(position, "more actual than formal parameters")
  4344. END;
  4345. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4346. result.SetAssignable(FALSE);
  4347. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4348. END;
  4349. RETURN result
  4350. END NewProcedureCallDesignator;
  4351. (**
  4352. builtin call designator generated in VisitParameterDesignator
  4353. -> nothing to be resolved
  4354. **)
  4355. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4356. BEGIN
  4357. resolvedExpression := x;
  4358. END VisitTypeGuardDesignator;
  4359. (**
  4360. builtin call designator generated in VisitParameterDesignator
  4361. -> nothing to be resolved
  4362. **)
  4363. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4364. BEGIN
  4365. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4366. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4367. ASSERT(resolvedExpression.type # NIL);
  4368. ELSIF ExpressionList(x.parameters) THEN
  4369. resolvedExpression := x;
  4370. END;
  4371. END VisitBuiltinCallDesignator;
  4372. (**
  4373. procedure call designator generated in VisitParameterDesignator
  4374. -> nothing to be resolved
  4375. **)
  4376. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4377. BEGIN
  4378. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4379. resolvedExpression := x;
  4380. END VisitProcedureCallDesignator;
  4381. (** return true if x is a variable else return false and report error **)
  4382. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN;
  4384. BEGIN
  4385. result := TRUE;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. result := FALSE;
  4388. ELSIF ~IsVariable(x) THEN
  4389. Error(x.position,"non variable expression");
  4390. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4391. result := FALSE;
  4392. END;
  4393. RETURN result
  4394. END CheckVariable;
  4395. (**
  4396. if expression x is of basic type then return true else report error and return false
  4397. **)
  4398. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4399. VAR result: BOOLEAN;
  4400. BEGIN
  4401. result := FALSE;
  4402. IF x = SyntaxTree.invalidExpression THEN
  4403. ELSIF ~IsBasicType(x.type) THEN
  4404. Error(x.position,"is no basic type");
  4405. result := FALSE
  4406. ELSE result := TRUE
  4407. END;
  4408. RETURN result
  4409. END CheckBasicType;
  4410. (**
  4411. if expression x is of number type then return true else report error and return false
  4412. **)
  4413. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4414. VAR result: BOOLEAN;
  4415. BEGIN
  4416. result := FALSE;
  4417. IF x = SyntaxTree.invalidExpression THEN
  4418. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4419. Error(x.position,"is non number type");
  4420. ELSE result := TRUE
  4421. END;
  4422. RETURN result
  4423. END CheckNumberType;
  4424. (**
  4425. if expression x is of number or size type but not complex then return true else report error and return false
  4426. **)
  4427. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4428. VAR result: BOOLEAN;
  4429. BEGIN
  4430. result := FALSE;
  4431. IF x = SyntaxTree.invalidExpression THEN
  4432. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4433. Error(x.position,"is complex type");
  4434. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4435. Error(x.position,"is non number type");
  4436. ELSE result := TRUE
  4437. END;
  4438. RETURN result
  4439. END CheckNonComplexNumberSizeType;
  4440. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4441. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4442. BEGIN
  4443. result := FALSE; type := x.type.resolved;
  4444. IF x = SyntaxTree.invalidExpression THEN
  4445. 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
  4446. TRACE(type.sizeInBits);
  4447. TRACE(system.addressType.sizeInBits);
  4448. Error(x.position,"is no address type");
  4449. ELSE result := TRUE
  4450. END;
  4451. RETURN result
  4452. END CheckAddressType;
  4453. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4454. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4455. BEGIN
  4456. result := FALSE; type := x.type.resolved;
  4457. IF x = SyntaxTree.invalidExpression THEN
  4458. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4459. Error(x.position,"is no size type");
  4460. ELSE result := TRUE
  4461. END;
  4462. RETURN result
  4463. END CheckSizeType;
  4464. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4465. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4466. BEGIN
  4467. result := FALSE; type := x.type.resolved;
  4468. IF x = SyntaxTree.invalidExpression THEN
  4469. 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
  4470. Error(x.position,"is no object type");
  4471. ELSE result := TRUE
  4472. END;
  4473. RETURN result
  4474. END CheckObjectType;
  4475. (**
  4476. if expression x is of integer type then return true else report error and return false
  4477. **)
  4478. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4479. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4480. BEGIN
  4481. result := FALSE; type := x.type.resolved;
  4482. IF x = SyntaxTree.invalidExpression THEN
  4483. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4484. Error(x.position,"is no integer type");
  4485. ELSE result := TRUE
  4486. END;
  4487. RETURN result
  4488. END CheckIntegerType;
  4489. (**
  4490. if expression x is of character type then return true else report error and return false
  4491. **)
  4492. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4493. VAR result: BOOLEAN;
  4494. BEGIN
  4495. result := FALSE;
  4496. IF x = SyntaxTree.invalidExpression THEN
  4497. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4498. Error(x.position,"is no character type");
  4499. ELSE result := TRUE
  4500. END;
  4501. RETURN result
  4502. END CheckCharacterType;
  4503. (**
  4504. if expression x is of real type then return true else report error and return false
  4505. **)
  4506. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4507. VAR result: BOOLEAN;
  4508. BEGIN
  4509. result := FALSE;
  4510. IF x = SyntaxTree.invalidExpression THEN
  4511. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4512. Error(x.position,"is no float type");
  4513. ELSE result := TRUE
  4514. END;
  4515. RETURN result
  4516. END CheckRealType;
  4517. (**
  4518. if expression x is of range type then return true else report error and return false
  4519. **)
  4520. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4521. VAR result: BOOLEAN;
  4522. BEGIN
  4523. result := FALSE;
  4524. IF x = SyntaxTree.invalidExpression THEN
  4525. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4526. Error(x.position,"is no range type");
  4527. ELSE result := TRUE
  4528. END;
  4529. RETURN result
  4530. END CheckRangeType;
  4531. (**
  4532. if expression x is of boolean type then return true else report error and return false
  4533. **)
  4534. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4535. VAR result: BOOLEAN;
  4536. BEGIN
  4537. result := FALSE;
  4538. IF x = SyntaxTree.invalidExpression THEN
  4539. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4540. Error(x.position,"is no boolean type");
  4541. ELSE result := TRUE
  4542. END;
  4543. RETURN result
  4544. END CheckBooleanType;
  4545. (**
  4546. if expression x is of set type then return true else report error and return false
  4547. **)
  4548. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4549. VAR result: BOOLEAN;
  4550. BEGIN
  4551. result := FALSE;
  4552. IF x = SyntaxTree.invalidExpression THEN
  4553. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4554. Error(x.position,"is no set type");
  4555. ELSE result := TRUE
  4556. END;
  4557. RETURN result
  4558. END CheckSetType;
  4559. (**
  4560. if expression x is of string or array of character type then return true else report error and return false
  4561. **)
  4562. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4563. VAR result: BOOLEAN;
  4564. BEGIN
  4565. result := FALSE;
  4566. IF x = SyntaxTree.invalidExpression THEN
  4567. ELSIF ~IsStringType(x.type.resolved) THEN
  4568. Error(x.position,"is no string type");
  4569. ELSE result := TRUE
  4570. END;
  4571. RETURN result
  4572. END CheckStringType;
  4573. (**
  4574. if expression x is a type declaration type return true else report error and return false
  4575. **)
  4576. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4577. VAR result: BOOLEAN;
  4578. BEGIN
  4579. result := FALSE;
  4580. IF x = SyntaxTree.invalidExpression THEN
  4581. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4582. Error(x.position,"is not a type declaration");
  4583. ELSE result := TRUE
  4584. END;
  4585. RETURN result
  4586. END CheckTypeDeclarationType;
  4587. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4588. VAR result: BOOLEAN;
  4589. BEGIN
  4590. result := FALSE;
  4591. IF x = SyntaxTree.invalidExpression THEN
  4592. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4593. result := TRUE;
  4594. value := x.resolved(SyntaxTree.IntegerValue).value;
  4595. ELSE
  4596. Error(x.position,"expression is not an integer constant");
  4597. END;
  4598. RETURN result;
  4599. END CheckIntegerValue;
  4600. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4601. VAR result: BOOLEAN;
  4602. BEGIN
  4603. result := FALSE;
  4604. IF x = SyntaxTree.invalidExpression THEN
  4605. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4606. result := TRUE;
  4607. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4608. ELSE
  4609. Error(x.position,"expression is not an integer constant");
  4610. END;
  4611. RETURN result;
  4612. END CheckStringValue;
  4613. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4614. BEGIN
  4615. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4616. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  4617. ELSE
  4618. RETURN FALSE
  4619. END;
  4620. END IsUnsignedValue;
  4621. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4622. BEGIN
  4623. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4624. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  4625. ELSE
  4626. RETURN FALSE
  4627. END
  4628. END IsAddressValue;
  4629. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4630. BEGIN
  4631. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4632. END IsAddressExpression;
  4633. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4634. BEGIN
  4635. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4636. END IsSizeExpression;
  4637. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4638. VAR result: BOOLEAN;
  4639. BEGIN
  4640. result := FALSE;
  4641. IF x = SyntaxTree.invalidExpression THEN
  4642. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4643. result := TRUE;
  4644. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4645. ELSE
  4646. Error(x.position,"expression is not an integer constant");
  4647. END;
  4648. RETURN result;
  4649. END CheckEnumerationValue;
  4650. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4651. VAR result: BOOLEAN;
  4652. BEGIN
  4653. result := FALSE;
  4654. IF x = SyntaxTree.invalidExpression THEN
  4655. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4656. result := TRUE;
  4657. value := x.resolved(SyntaxTree.CharacterValue).value;
  4658. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4659. result := TRUE;
  4660. value := x.resolved(SyntaxTree.StringValue).value[0];
  4661. ELSE
  4662. Error(x.position,"expression is not a character constant");
  4663. END;
  4664. RETURN result;
  4665. END CheckCharacterValue;
  4666. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4667. VAR result: BOOLEAN;
  4668. BEGIN
  4669. result := FALSE;
  4670. IF x = SyntaxTree.invalidExpression THEN
  4671. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4672. value := x.resolved(SyntaxTree.IntegerValue).value;
  4673. IF (value > 0) OR includeZero & (value = 0) THEN
  4674. result := TRUE;
  4675. ELSE
  4676. Error(x.position,"integer is not positive");
  4677. END
  4678. ELSE
  4679. Error(x.position,"expression is not an integer constant");
  4680. END;
  4681. RETURN result;
  4682. END CheckPositiveIntegerValue;
  4683. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4684. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4685. BEGIN
  4686. result := FALSE;
  4687. IF x = SyntaxTree.invalidExpression THEN
  4688. ELSE
  4689. type := x.type.resolved;
  4690. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4691. portType := type(SyntaxTree.PortType);
  4692. result := TRUE
  4693. ELSE
  4694. Error(x.position,"no port type");
  4695. END;
  4696. END;
  4697. RETURN result
  4698. END CheckPortType;
  4699. (* move to builtin procedure call statement ?
  4700. remove builtin procedure call designator ?
  4701. *)
  4702. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4703. VAR
  4704. numberActualParameters,numberFormalParameters: LONGINT;
  4705. formalParameter: SyntaxTree.Parameter;
  4706. actualParameter: SyntaxTree.Expression;
  4707. procedureType: SyntaxTree.ProcedureType;
  4708. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4709. inPort, outPort: SyntaxTree.PortType;
  4710. constructor: SyntaxTree.Procedure;
  4711. type0,type1,type2: SyntaxTree.Type;
  4712. type,base,parameterType: SyntaxTree.Type;
  4713. arrayType: SyntaxTree.ArrayType;
  4714. i: LONGINT; i0, i1, value: Basic.Integer;
  4715. r,r0,r1,im: LONGREAL;
  4716. c: CHAR;
  4717. id: LONGINT;
  4718. b: BOOLEAN;
  4719. first: LONGINT;
  4720. mathArrayType: SyntaxTree.MathArrayType;
  4721. customBuiltin: SyntaxTree.CustomBuiltin;
  4722. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4723. VAR resultB: BOOLEAN;
  4724. BEGIN
  4725. IF numberActualParameters < from THEN
  4726. Error(position, "less actual than formal parameters");
  4727. result := SyntaxTree.invalidExpression;
  4728. resultB := FALSE;
  4729. ELSIF numberActualParameters > to THEN
  4730. Error(position, "more actual than formal parameters");
  4731. result := SyntaxTree.invalidExpression;
  4732. resultB := FALSE;
  4733. ELSE
  4734. resultB := TRUE;
  4735. END;
  4736. RETURN resultB
  4737. END CheckArity;
  4738. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4739. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4740. BEGIN
  4741. WHILE modifier # NIL DO
  4742. symbol := cellType.FindProperty(modifier.identifier);
  4743. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4744. propertyType := symbol.type.resolved;
  4745. modifierType := modifier.expression.type.resolved;
  4746. IF ~CompatibleTo(system, modifierType, propertyType) &
  4747. ~(
  4748. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4749. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4750. Error(modifier.position,"incompatible to cell property");
  4751. END;
  4752. ELSE
  4753. Error(modifier.position, "undefined property");
  4754. END;
  4755. modifier := modifier.nextModifier;
  4756. END;
  4757. END CheckModifiers;
  4758. BEGIN
  4759. type := NIL; result := NIL;
  4760. type0 := NIL; type1 := NIL; type2 := NIL;
  4761. numberActualParameters := actualParameters.Length();
  4762. IF numberActualParameters>0 THEN
  4763. parameter0 := actualParameters.GetExpression(0);
  4764. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4765. Error(parameter0.position,"forbidden type-less argument");
  4766. result := SyntaxTree.invalidExpression
  4767. END
  4768. END;
  4769. IF numberActualParameters >1 THEN
  4770. parameter1 := actualParameters.GetExpression(1);
  4771. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4772. ELSE
  4773. Error(parameter1.position,"forbidden type-less argument");
  4774. result := SyntaxTree.invalidExpression
  4775. END
  4776. END;
  4777. IF numberActualParameters >2 THEN
  4778. parameter2 := actualParameters.GetExpression(2);
  4779. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4780. ELSE
  4781. Error(parameter2.position,"forbidden type-less argument");
  4782. result := SyntaxTree.invalidExpression
  4783. END
  4784. END;
  4785. IF returnType # NIL THEN
  4786. id := Global.New;
  4787. result := NIL;
  4788. ELSE
  4789. id := builtin.id;
  4790. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4791. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4792. END;
  4793. END;
  4794. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4795. ELSIF result # NIL THEN type := result.type (* operator *)
  4796. ELSE
  4797. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4798. result(SyntaxTree.Designator).SetLeft(left);
  4799. IF returnType # NIL THEN
  4800. type := returnType;
  4801. END;
  4802. (* ---- ASSERT ----- *)
  4803. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4804. IF CheckBooleanType(parameter0) THEN
  4805. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4806. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4807. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4808. *)
  4809. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4810. Error(position, "static assertion failed");
  4811. END;
  4812. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4813. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4814. rules imposed by the architecture / current runtime
  4815. *)
  4816. END;
  4817. END;
  4818. (* ---- COPY ----- *)
  4819. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4820. IF~IsStringType(type0) THEN
  4821. Error(parameter0.position,"no string type");
  4822. END;
  4823. IF ~IsStringType(type1) THEN
  4824. Error(parameter1.position,"no string type");
  4825. ELSIF CheckVariable(parameter1) THEN
  4826. IF (type0 IS SyntaxTree.StringType) THEN
  4827. arrayType := type1(SyntaxTree.ArrayType);
  4828. IF arrayType.form = SyntaxTree.Static THEN
  4829. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4830. Error(position, "destination length smaller than source length")
  4831. END;
  4832. END;
  4833. END;
  4834. END;
  4835. (* ---- INC, DEC----- *)
  4836. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4837. IF numberActualParameters = 1 THEN
  4838. parameter1 :=Global.NewIntegerValue(system,position,1);
  4839. actualParameters.AddExpression(parameter1);
  4840. END;
  4841. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4842. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4843. Error(position, "incompatible increment");
  4844. ELSE
  4845. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4846. actualParameters.SetExpression(1,parameter1);
  4847. END;
  4848. END;
  4849. (* ---- EXCL, INCL----- *)
  4850. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4851. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4852. IF IsIntegerValue(parameter1,i0) THEN
  4853. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4854. Error(position, "parameter out of SET range")
  4855. END;
  4856. END;
  4857. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4858. actualParameters.SetExpression(1,parameter1);
  4859. END;
  4860. (* ---- HALT, SYSTEM.HALT ----- *)
  4861. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4862. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4863. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4864. rules imposed by the architecture / current runtime
  4865. *)
  4866. END;
  4867. (* ---- WAIT ----- *)
  4868. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4869. IF CheckObjectType(parameter0) THEN
  4870. END;
  4871. (* ---- NEW ----- *)
  4872. ELSIF (id = Global.New) THEN
  4873. IF returnType # NIL THEN
  4874. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4875. ELSE
  4876. first := 1;
  4877. END;
  4878. IF CheckArity(first,Infinity) THEN
  4879. IF currentIsRealtime THEN
  4880. Error(position, "forbidden new in realtime block");
  4881. END;
  4882. IF IsUnsafePointer(type0) THEN
  4883. Error(position, "forbidden new on unsafe pointer");
  4884. END;
  4885. (* check constructor *)
  4886. IF (first =0) OR CheckVariable(parameter0) THEN
  4887. IF type0 IS SyntaxTree.PointerType THEN
  4888. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4889. ELSIF type0 IS SyntaxTree.CellType THEN
  4890. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4891. ELSE
  4892. Error(position, "forbidden new on value type");
  4893. END;
  4894. IF type0 IS SyntaxTree.ArrayType THEN
  4895. arrayType := type0(SyntaxTree.ArrayType);
  4896. IF arrayType.form = SyntaxTree.Static THEN
  4897. i := first
  4898. ELSIF arrayType.form = SyntaxTree.Open THEN
  4899. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4900. ELSE HALT(100)
  4901. END;
  4902. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4903. i := first;
  4904. REPEAT
  4905. actualParameter := actualParameters.GetExpression(i);
  4906. IF CheckSizeType(actualParameter) THEN
  4907. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4908. actualParameters.SetExpression(i,actualParameter);
  4909. END;
  4910. INC(i);
  4911. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4912. END;
  4913. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4914. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4915. Error(position, "forbidden new on abstract object");
  4916. END;
  4917. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4918. IF constructor = NIL THEN
  4919. IF CheckArity(first,first) THEN END;
  4920. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4921. Error(position, "new on object with hidden constructor");
  4922. ELSE
  4923. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4924. numberFormalParameters := procedureType.numberParameters;
  4925. IF numberActualParameters-first <= numberFormalParameters THEN
  4926. formalParameter := procedureType.firstParameter;
  4927. FOR i := first TO numberActualParameters-1 DO
  4928. actualParameter := actualParameters.GetExpression(i);
  4929. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4930. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4931. ELSE
  4932. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4933. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4934. END;
  4935. actualParameters.SetExpression(i,actualParameter);
  4936. END;
  4937. formalParameter := formalParameter.nextParameter;
  4938. END;
  4939. WHILE (formalParameter # NIL) DO
  4940. IF formalParameter.defaultValue # NIL THEN
  4941. actualParameters.AddExpression(formalParameter.defaultValue);
  4942. formalParameter := formalParameter.nextParameter
  4943. ELSE
  4944. Error(position, "less actual than formal parameters");
  4945. formalParameter := NIL;
  4946. END;
  4947. END;
  4948. ELSE
  4949. Error(position, "more actual than formal parameters")
  4950. END;
  4951. END;
  4952. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4953. mathArrayType := type0(SyntaxTree.MathArrayType);
  4954. IF mathArrayType.form = SyntaxTree.Static THEN
  4955. Error(position, "new on static array");
  4956. ELSE
  4957. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4958. i0 := first+1; i1 := Infinity;
  4959. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4960. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4961. i1 := i0;
  4962. ELSE HALT(100);
  4963. END;
  4964. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4965. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4966. base := ArrayBase(type0,MAX(LONGINT));
  4967. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4968. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4969. IF ~CompatibleTo(system,type0,parameterType) THEN
  4970. Error(parameter0.position,"incompatible parameter in new");
  4971. result := SyntaxTree.invalidExpression;
  4972. ELSE
  4973. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4974. END;
  4975. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4976. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4977. IF ~CompatibleTo(system,type1,parameterType) THEN
  4978. Error(parameter1.position,"parameter incompatible to math array len type");
  4979. result := SyntaxTree.invalidExpression;
  4980. ELSE
  4981. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4982. END;
  4983. ELSE
  4984. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4985. i := first;
  4986. REPEAT
  4987. actualParameter := actualParameters.GetExpression(i);
  4988. IF CheckSizeType(actualParameter) THEN
  4989. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4990. actualParameters.SetExpression(i,actualParameter);
  4991. END;
  4992. INC(i);
  4993. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4994. END;
  4995. END;
  4996. END;
  4997. ELSIF type0 IS SyntaxTree.CellType THEN
  4998. IF ~(currentIsCellNet) THEN
  4999. Error(position, "cell allocation outside activeCells ");
  5000. ELSE
  5001. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  5002. IF (constructor = NIL) & CheckArity(1,1) THEN
  5003. (* ok *)
  5004. ELSE
  5005. procedureType := constructor.type(SyntaxTree.ProcedureType);
  5006. numberFormalParameters := procedureType.numberParameters;
  5007. DEC(numberActualParameters);
  5008. IF numberActualParameters <= numberFormalParameters THEN
  5009. formalParameter := procedureType.firstParameter;
  5010. FOR i := first TO numberActualParameters DO
  5011. actualParameter := actualParameters.GetExpression(i);
  5012. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5013. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5014. ELSE
  5015. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5016. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5017. END;
  5018. actualParameters.SetExpression(i,actualParameter);
  5019. END;
  5020. formalParameter := formalParameter.nextParameter;
  5021. END;
  5022. WHILE (formalParameter # NIL) DO
  5023. IF formalParameter.defaultValue # NIL THEN
  5024. actualParameters.AddExpression(formalParameter.defaultValue);
  5025. formalParameter := formalParameter.nextParameter
  5026. ELSE
  5027. Error(position, "less actual than formal parameters");
  5028. formalParameter := NIL;
  5029. END;
  5030. END;
  5031. ELSE
  5032. Error(position, "more actual than formal parameters")
  5033. END;
  5034. END;
  5035. END;
  5036. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5037. activeCellsStatement := TRUE;
  5038. ELSE
  5039. Error(position, "cannot be allocated");
  5040. END;
  5041. END;
  5042. END;
  5043. (* ---- DISPOSE ----- *)
  5044. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5045. IF ~IsPointerType(parameter0.type) THEN
  5046. Error(parameter0.position,"is not a pointer")
  5047. ELSIF ~IsDisposable(parameter0.type) THEN
  5048. Error(parameter0.position,"is not disposable")
  5049. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5050. END
  5051. (* ---- GETPROCEDURE ----- *)
  5052. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5053. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5054. IF CheckVariable(parameter2) THEN
  5055. IF ~GetProcedureAllowed(parameter2.type) THEN
  5056. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5057. END;
  5058. END;
  5059. END;
  5060. (* ---- ABS ----- *)
  5061. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5062. (* note: ABS on complex numbers is done using overloading *)
  5063. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5064. type := type0;
  5065. IF IsIntegerValue(parameter0,i0) THEN
  5066. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5067. type := Global.GetIntegerType(system,ABS(i0));
  5068. ELSIF IsRealValue(parameter0,r) THEN
  5069. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5070. END;
  5071. ELSE
  5072. type := SyntaxTree.invalidType;
  5073. END;
  5074. (* ---- ASH, ASR ----- *)
  5075. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5076. type := type0;
  5077. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5078. (*
  5079. ConvertOperands(parameter0,parameter1); (* same type *)
  5080. *)
  5081. type := parameter0.type;
  5082. IF IsIntegerValue(parameter0,i0) THEN
  5083. IF IsIntegerValue(parameter1,i1) THEN
  5084. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5085. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5086. result := ResolveExpression(result);
  5087. type := Global.GetIntegerType(system,i0);
  5088. END;
  5089. END;
  5090. IF type.resolved.sizeInBits < 32 THEN
  5091. type := system.longintType;
  5092. END;
  5093. (*!compatibility with release, remove when resolved
  5094. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5095. *)
  5096. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5097. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5098. actualParameters.SetExpression(0,parameter0);
  5099. actualParameters.SetExpression(1,parameter1);
  5100. END;
  5101. (* ---- CAP ----- *)
  5102. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5103. type := system.characterType;
  5104. IF CheckCharacterType (parameter0) THEN
  5105. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5106. actualParameters.SetExpression(0,parameter0);
  5107. IF IsCharacterValue(parameter0,c) THEN
  5108. IF (c <= "z") & (c >= "a") THEN
  5109. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5110. ELSE
  5111. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5112. END;
  5113. END;
  5114. END;
  5115. (* ---- CHR ----- *)
  5116. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5117. IF id = Global.Chr THEN
  5118. type := system.characterType
  5119. ELSE
  5120. type := system.characterType32
  5121. END;
  5122. IF CheckIntegerType(parameter0) THEN
  5123. IF IsIntegerValue(parameter0,i0) THEN
  5124. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5125. result := ResolveExpression(result);
  5126. ELSE
  5127. (*
  5128. result := NewConversion(parameter0.position,parameter0,type);
  5129. *)
  5130. END;
  5131. END
  5132. (* ---- ENTIER ----- *)
  5133. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5134. type := system.longintType;
  5135. IF CheckRealType(parameter0) THEN
  5136. IF IsRealValue(parameter0,r) THEN
  5137. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5138. type := Global.GetIntegerType(system,ENTIER(r));
  5139. END
  5140. END;
  5141. (* ---- ENTIERH ----- *)
  5142. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5143. type := system.hugeintType;
  5144. IF CheckRealType(parameter0) THEN
  5145. IF IsRealValue(parameter0,r) THEN
  5146. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5147. END
  5148. END;
  5149. (* ---- LEN ----- *)
  5150. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5151. type := system.lenType;
  5152. base := type0;
  5153. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5154. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5155. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5156. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5157. Error(position, "forbidden len on unsafe pointer");
  5158. END;
  5159. type0 := base;
  5160. ELSE
  5161. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5162. type0 := parameter0.type.resolved;
  5163. actualParameters.SetExpression(0,parameter0);
  5164. base := type0;
  5165. END;
  5166. END;
  5167. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5168. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5169. IF i1 < 0 THEN
  5170. Error(position, "invalid dimension");
  5171. base := SyntaxTree.invalidType;
  5172. ELSE
  5173. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5174. IF (base # NIL) & Indexable(base) THEN
  5175. ELSE
  5176. Error(position, "len on no array");
  5177. IF VerboseErrorMessage THEN
  5178. Printout.Info("base",base);
  5179. END;
  5180. base := SyntaxTree.invalidType;
  5181. END;
  5182. END;
  5183. IF numberActualParameters=2 THEN
  5184. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5185. actualParameters.SetExpression(1,parameter1);
  5186. ELSIF base IS SyntaxTree.MathArrayType THEN
  5187. Error(position, "missing dimension specification");
  5188. END;
  5189. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5190. IF base IS SyntaxTree.ArrayType THEN
  5191. arrayType := base(SyntaxTree.ArrayType);
  5192. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5193. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5194. result := Global.NewIntegerValue(system,position,value);
  5195. type := result.type;(* arrayType.length.type;*)
  5196. ASSERT(type # NIL);
  5197. END;
  5198. ELSIF base IS SyntaxTree.MathArrayType THEN
  5199. mathArrayType := base(SyntaxTree.MathArrayType);
  5200. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5201. result := Global.NewIntegerValue(system,position,value);
  5202. type := result.type;
  5203. (*
  5204. type := mathArrayType.length.type;
  5205. *)
  5206. ASSERT(type # NIL);
  5207. END;
  5208. END;
  5209. END;
  5210. ELSE
  5211. type := system.lenType;
  5212. END;
  5213. (* ---- FIRST ---- *)
  5214. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5215. type := system.lenType;
  5216. IF CheckRangeType(parameter0) THEN END;
  5217. result.SetAssignable(parameter0.assignable)
  5218. (* ---- LAST ---- *)
  5219. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5220. type := system.lenType;
  5221. IF CheckRangeType(parameter0) THEN END;
  5222. result.SetAssignable(parameter0.assignable)
  5223. (* ---- STEP ---- *)
  5224. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5225. type := system.lenType;
  5226. IF CheckRangeType(parameter0) THEN END;
  5227. result.SetAssignable(parameter0.assignable)
  5228. (* ---- RE ---- *)
  5229. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5230. IF CheckNumberType(parameter0) THEN
  5231. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5232. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5233. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5234. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5235. type := parameter0.type
  5236. ELSE
  5237. type := system.realType
  5238. END
  5239. END;
  5240. result.SetAssignable(parameter0.assignable)
  5241. (* ---- IM ---- *)
  5242. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5243. IF CheckNumberType(parameter0) THEN
  5244. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5245. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5246. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5247. ELSE
  5248. type := system.realType;
  5249. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5250. END
  5251. END;
  5252. result.SetAssignable(parameter0.assignable)
  5253. (* ---- MAX ----- *)
  5254. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5255. IF numberActualParameters = 1 THEN
  5256. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5257. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5258. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5259. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5260. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5261. *)
  5262. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5263. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5264. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5265. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5266. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5267. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5268. END;
  5269. ELSE
  5270. Error(parameter0.position,"is not a type symbol");
  5271. END
  5272. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5273. ConvertOperands(parameter0,parameter1);
  5274. actualParameters.SetExpression(0,parameter0);
  5275. actualParameters.SetExpression(1,parameter1);
  5276. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5277. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5278. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5279. END;
  5280. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5281. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5282. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5283. END;
  5284. END;
  5285. type := parameter0.type;
  5286. ELSE type := SyntaxTree.invalidType;
  5287. END;
  5288. (* ---- MIN ----- *)
  5289. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5290. IF numberActualParameters = 1 THEN
  5291. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5292. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5293. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5294. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5295. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5296. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5297. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5298. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5299. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5300. END;
  5301. ELSE
  5302. Error(parameter0.position,"is not a type symbol");
  5303. END
  5304. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5305. ConvertOperands(parameter0,parameter1);
  5306. actualParameters.SetExpression(0,parameter0);
  5307. actualParameters.SetExpression(1,parameter1);
  5308. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5309. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5310. ELSE result.SetResolved(parameter1.resolved)
  5311. END;
  5312. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5313. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5314. ELSE result.SetResolved(parameter1.resolved)
  5315. END;
  5316. END;
  5317. type := parameter0.type;
  5318. ELSE type := SyntaxTree.invalidType;
  5319. END;
  5320. (* ---- ODD ----- *)
  5321. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5322. type := system.booleanType;
  5323. IF CheckIntegerType(parameter0) THEN
  5324. IF IsIntegerValue(parameter0,i0) THEN
  5325. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5326. type := system.booleanType;
  5327. END;
  5328. END;
  5329. (* ---- ORD ----- *)
  5330. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5331. IF id = Global.Ord THEN
  5332. type := system.integerType;
  5333. ELSE
  5334. type := system.longintType;
  5335. END;
  5336. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5337. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5338. actualParameters.SetExpression(0,parameter0);
  5339. (* IF CheckCharacterType(parameter0) THEN*)
  5340. IF IsCharacterValue(parameter0,c)THEN
  5341. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5342. type := Global.GetSignedIntegerType(system,ORD(c));
  5343. END;
  5344. ELSE Error(parameter0.position, "incompatible parameter");
  5345. END;
  5346. (* ---- SHORT ----- *)
  5347. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5348. type := type0;
  5349. IF IsSignedIntegerType(type) THEN
  5350. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5351. ELSIF type = system.integerType THEN type := system.shortintType
  5352. ELSIF type = system.longintType THEN type := system.integerType
  5353. ELSIF type = system.hugeintType THEN type:= system.longintType
  5354. ELSE
  5355. CASE type.sizeInBits OF
  5356. 16: type := Global.Integer8
  5357. |32: type := Global.Integer16
  5358. |64: type := Global.Integer32
  5359. END;
  5360. END;
  5361. ELSIF IsUnsignedIntegerType(type) THEN
  5362. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  5363. ELSE
  5364. CASE type.sizeInBits OF
  5365. 16: type := Global.Unsigned8
  5366. |32: type := Global.Unsigned16
  5367. |64: type := Global.Unsigned32
  5368. END;
  5369. END;
  5370. ELSIF type IS SyntaxTree.FloatType THEN
  5371. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5372. ELSIF type = system.longrealType THEN type := system.realType
  5373. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5374. END;
  5375. ELSIF type IS SyntaxTree.ComplexType THEN
  5376. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5377. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5378. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5379. END;
  5380. ELSE
  5381. Error(parameter0.position,"short not applicable")
  5382. END;
  5383. IF (parameter0.resolved # NIL) THEN
  5384. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5385. IF parameter0 IS SyntaxTree.Value THEN
  5386. result.SetResolved(parameter0(SyntaxTree.Value));
  5387. END;
  5388. END;
  5389. (* ---- LONG ----- *)
  5390. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5391. type := type0;
  5392. IF IsSignedIntegerType(type) THEN
  5393. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5394. ELSIF type = system.longintType THEN type := system.hugeintType
  5395. ELSIF type = system.integerType THEN type := system.longintType
  5396. ELSIF type = system.shortintType THEN type := system.integerType
  5397. ELSE
  5398. CASE type.sizeInBits OF
  5399. 8: type := Global.Integer16
  5400. |16: type := Global.Integer32
  5401. |32: type := Global.Integer64
  5402. END;
  5403. END;
  5404. ELSIF IsUnsignedIntegerType(type) THEN
  5405. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  5406. ELSE
  5407. CASE type.sizeInBits OF
  5408. 8: type := Global.Unsigned16
  5409. |16: type := Global.Unsigned32
  5410. |32: type := Global.Unsigned64
  5411. END;
  5412. END;
  5413. ELSIF type IS SyntaxTree.FloatType THEN
  5414. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5415. ELSIF type= system.realType THEN type := system.longrealType
  5416. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5417. END;
  5418. ELSIF type IS SyntaxTree.ComplexType THEN
  5419. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5420. ELSIF type = system.complexType THEN type := system.longcomplexType
  5421. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5422. END;
  5423. ELSE
  5424. Error(parameter0.position,"long not applicable")
  5425. END;
  5426. IF (parameter0.resolved # NIL) THEN
  5427. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5428. IF parameter0 IS SyntaxTree.Value THEN
  5429. result.SetResolved(parameter0(SyntaxTree.Value));
  5430. END;
  5431. END;
  5432. (* ---- SIZE OF ----- *)
  5433. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5434. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5435. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5436. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5437. type := system.integerType;
  5438. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5439. ELSE
  5440. (* for variables, system sizeof could represent the physically occupied size
  5441. determined via the type descriptor, implement that ? *)
  5442. Error(parameter0.position,"is not a type symbol");
  5443. END
  5444. (* ---- SYSTEM.TRACE -----*)
  5445. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5446. FOR i := 0 TO numberActualParameters-1 DO
  5447. parameter0 := actualParameters.GetExpression(i);
  5448. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5449. Error(parameter0.position,"incompatible parameter");
  5450. END;
  5451. END;
  5452. (* remaining issues can only be tested in backend *)
  5453. (* ---- ADDRESSOF----- *)
  5454. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5455. IF HasAddress(parameter0) THEN
  5456. type := system.addressType;
  5457. ELSE
  5458. type := SyntaxTree.invalidType;
  5459. Error(parameter0.position,"has no address");
  5460. END;
  5461. (* ---- BIT ----- *)
  5462. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5463. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5464. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5465. actualParameters.SetExpression(0,parameter0);
  5466. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5467. actualParameters.SetExpression(1,parameter1);
  5468. END;
  5469. type := system.booleanType;
  5470. (* ----- MSK ---- *)
  5471. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5472. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5473. ConvertOperands(parameter0,parameter1);
  5474. actualParameters.SetExpression(0,parameter0);
  5475. actualParameters.SetExpression(1,parameter1);
  5476. END;
  5477. type := parameter0.type;
  5478. (* ---- SYSTEM.GET64 ----- *)
  5479. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5480. IF CheckAddressType(parameter0) THEN
  5481. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5482. actualParameters.SetExpression(0,parameter0);
  5483. END;
  5484. type := system.hugeintType;
  5485. (* ---- SYSTEM.GET32 ----- *)
  5486. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5487. IF CheckAddressType(parameter0) THEN
  5488. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5489. actualParameters.SetExpression(0,parameter0);
  5490. END;
  5491. type := system.longintType;
  5492. (* ---- SYSTEM.GET16 ----- *)
  5493. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5494. IF CheckAddressType(parameter0) THEN
  5495. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5496. actualParameters.SetExpression(0,parameter0);
  5497. END;
  5498. type := system.integerType;
  5499. (* ---- SYSTEM.GET8 ----- *)
  5500. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5501. IF CheckAddressType(parameter0) THEN
  5502. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5503. actualParameters.SetExpression(0,parameter0);
  5504. END;
  5505. type := system.shortintType;
  5506. (* ---- SYSTEM.GetStackPointer ----- *)
  5507. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5508. type := system.addressType;
  5509. (* ---- SYSTEM.GetFramePointer ----- *)
  5510. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5511. type := system.addressType;
  5512. (* ---- SYSTEM.GetActivity ----- *)
  5513. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5514. type := system.objectType;
  5515. (* ---- SYSTEM.SetStackPointer ----- *)
  5516. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5517. IF CheckAddressType(parameter0) THEN
  5518. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5519. actualParameters.SetExpression(0,parameter0);
  5520. END;
  5521. (* ---- SYSTEM.SetFramePointer ----- *)
  5522. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5523. IF CheckAddressType(parameter0) THEN
  5524. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5525. actualParameters.SetExpression(0,parameter0);
  5526. END;
  5527. (* ---- SYSTEM.SetActivity ----- *)
  5528. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5529. IF CheckObjectType(parameter0) THEN
  5530. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5531. actualParameters.SetExpression(0,parameter0);
  5532. END;
  5533. (* ---- LSH, LSL, ROT, ROR ----- *)
  5534. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5535. type := type0;
  5536. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5537. actualParameters.SetExpression(1, parameter1);
  5538. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5539. IF id = Global.Lsh THEN
  5540. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5541. ELSIF id = Global.Rot THEN
  5542. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5543. ELSIF id = Global.Ror THEN
  5544. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5545. END;
  5546. END;
  5547. (* ---- SYSTEM.VAL ----- *)
  5548. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5549. IF CheckTypeDeclarationType(parameter0) THEN
  5550. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5551. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5552. result := SyntaxTree.invalidExpression;
  5553. Error(parameter0.position,"is no basic type");
  5554. ELSE
  5555. IF (parameter1.resolved # NIL) THEN
  5556. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5557. IF parameter0 IS SyntaxTree.Value THEN
  5558. result.SetResolved(parameter0(SyntaxTree.Value));
  5559. END;
  5560. END;
  5561. result.SetAssignable(parameter1.assignable);
  5562. END;
  5563. END;
  5564. (* ---- SYSTEM.GET ----- *)
  5565. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5566. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5567. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5568. actualParameters.SetExpression(0,parameter0);
  5569. END;
  5570. (* ---- SYSTEM.PUT ----- *)
  5571. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5572. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5573. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5574. actualParameters.SetExpression(0,parameter0);
  5575. END;
  5576. (* ---- SYSTEM.PUT64 ----- *)
  5577. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5578. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5579. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5580. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5581. actualParameters.SetExpression(0,parameter0);
  5582. actualParameters.SetExpression(1,parameter1);
  5583. END;
  5584. (* ---- SYSTEM.PUT32 ----- *)
  5585. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5586. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5587. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5588. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5589. actualParameters.SetExpression(0,parameter0);
  5590. actualParameters.SetExpression(1,parameter1);
  5591. END;
  5592. (* ---- SYSTEM.PUT16 ----- *)
  5593. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5594. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5595. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5596. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5597. actualParameters.SetExpression(0,parameter0);
  5598. actualParameters.SetExpression(1,parameter1);
  5599. END;
  5600. (* ---- SYSTEM.PUT8 ----- *)
  5601. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5602. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5603. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5604. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5605. actualParameters.SetExpression(0,parameter0);
  5606. actualParameters.SetExpression(1,parameter1);
  5607. END;
  5608. (* ---- SYSTEM.MOVE ----- *)
  5609. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5610. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5611. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5612. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5613. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5614. actualParameters.SetExpression(0,parameter0);
  5615. actualParameters.SetExpression(1,parameter1);
  5616. actualParameters.SetExpression(2,parameter2);
  5617. END;
  5618. (* ---- SYSTEM.NEW ----- *)
  5619. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5620. IF ~IsPointerType(parameter0.type) THEN
  5621. Error(parameter0.position,"is not a pointer")
  5622. ELSIF CheckSizeType(parameter1) THEN
  5623. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5624. actualParameters.SetExpression(1,parameter1);
  5625. END;
  5626. (* ----SYSTEM.REF ---- *)
  5627. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5628. type := system.addressType
  5629. (* ---- INCR ----- *)
  5630. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5631. type := system.lenType;
  5632. base := type0;
  5633. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5634. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5635. IF i1 < 0 THEN
  5636. Error(position, "invalid dimension");
  5637. base := SyntaxTree.invalidType;
  5638. ELSE
  5639. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5640. IF (base # NIL) & Indexable(base) THEN
  5641. ELSE
  5642. Error(position, "len on no array");
  5643. IF VerboseErrorMessage THEN
  5644. Printout.Info("base",base);
  5645. END;
  5646. base := SyntaxTree.invalidType;
  5647. END;
  5648. END;
  5649. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5650. actualParameters.SetExpression(1,parameter1);
  5651. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5652. mathArrayType := base(SyntaxTree.MathArrayType);
  5653. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5654. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5655. type := system.lenType;
  5656. END;
  5657. END;
  5658. ELSE
  5659. type := system.lenType;
  5660. END;
  5661. (* ---- SUM ----- *)
  5662. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5663. Error(position, "sum operator not applicable");
  5664. (* ---- ALL ----- *)
  5665. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5666. Error(position, "all operator not applicable");
  5667. (* ---- DIM ----- *)
  5668. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5669. type := system.lenType;
  5670. IF type0 IS SyntaxTree.MathArrayType THEN
  5671. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5672. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5673. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5674. END;
  5675. ELSE
  5676. Error(position, "dimension on non math array type");
  5677. END;
  5678. (* ---- CAS ----- *)
  5679. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5680. IF type0.IsComposite () THEN
  5681. Error(position, "first parameter of composite type");
  5682. result := SyntaxTree.invalidExpression;
  5683. ELSIF ~IsVariable (parameter0) THEN
  5684. Error(position, "first parameter not assignable");
  5685. result := SyntaxTree.invalidExpression;
  5686. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5687. Error(position, "second parameter incompatible");
  5688. result := SyntaxTree.invalidExpression;
  5689. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5690. Error(position, "third parameter incompatible");
  5691. result := SyntaxTree.invalidExpression;
  5692. ELSE
  5693. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5694. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5695. type := type0;
  5696. END;
  5697. (* ---- RESHAPE ----- *)
  5698. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5699. IF type0 IS SyntaxTree.MathArrayType THEN
  5700. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5701. base := ArrayBase(type0,MAX(LONGINT));
  5702. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5703. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5704. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5705. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5706. IF ~CompatibleTo(system,type0,parameterType) THEN
  5707. Error(parameter0.position,"incompatible parameter in reshape");
  5708. result := SyntaxTree.invalidExpression;
  5709. ELSE
  5710. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5711. END;
  5712. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5713. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5714. IF ~CompatibleTo(system,type1,parameterType) THEN
  5715. Error(parameter1.position,"parameter incompatible to math array of longint");
  5716. result := SyntaxTree.invalidExpression;
  5717. ELSE
  5718. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5719. END;
  5720. ELSE
  5721. Error(position,"reshape on non math array type");
  5722. result := SyntaxTree.invalidExpression;
  5723. END;
  5724. (* ---- SYSTEM.TYPECODE ----- *)
  5725. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5726. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5727. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5728. type := type.resolved;
  5729. IF type IS SyntaxTree.PointerType THEN
  5730. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5731. END;
  5732. IF ~(type IS SyntaxTree.RecordType) THEN
  5733. Error(parameter0.position,"must be type with type descriptor");
  5734. END;
  5735. ELSE
  5736. Error(parameter0.position,"is not a type symbol");
  5737. END;
  5738. type := system.addressType;
  5739. (* -------- FLT --------- *)
  5740. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5741. type := system.realType;
  5742. IF IsRealValue(parameter0, r) THEN
  5743. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5744. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5745. value := ABS(i0);
  5746. IF value # 0 THEN
  5747. i1 := 23;
  5748. IF value >= 2*800000H THEN
  5749. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5750. ELSIF value < 800000H THEN
  5751. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5752. END;
  5753. value := (i1 + 127)*800000H - 800000H + value;
  5754. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5755. END;
  5756. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5757. END;
  5758. (* ------- CONNECT -------*)
  5759. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5760. (*IF ~(currentIsCellNet) THEN
  5761. Error(position, "connection outside activeCells body block");
  5762. END;*)
  5763. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5764. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5765. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5766. END;
  5767. IF numberActualParameters = 3 THEN
  5768. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5769. Error(position, "incompatible channel size parameter");
  5770. END;
  5771. *)
  5772. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5773. actualParameters.SetExpression(2,parameter2);
  5774. END;
  5775. activeCellsStatement := TRUE;
  5776. (* ---------- DELEGATE --------*)
  5777. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5778. (*
  5779. IF ~(currentIsCellNet) THEN
  5780. Error(position, "connection delegation outside activeCells body block");
  5781. END;
  5782. *)
  5783. IF ~CheckPortType(parameter1, inPort) THEN
  5784. Error(parameter0.position,"not a port")
  5785. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5786. Error(parameter1.position,"not a port")
  5787. ELSIF (outPort.direction # inPort.direction) THEN
  5788. Error(parameter0.position,"invalid port direction");
  5789. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5790. Error(position, "incompatible port sizes");
  5791. END;
  5792. activeCellsStatement := TRUE;
  5793. (* --------- RECEIVE ---------*)
  5794. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5795. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5796. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5797. IF inPort.direction # SyntaxTree.InPort THEN
  5798. Error(parameter0.position,"not an in-port")
  5799. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5800. Error(parameter1.position,"incompatible to port type");
  5801. END;
  5802. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5803. IF ~SameType(parameter2.type, system.integerType) THEN
  5804. Error(parameter2.position,"incompatible to integer type");
  5805. END;
  5806. END;
  5807. END;
  5808. (* --------- SEND ---------*)
  5809. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5810. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5811. IF CheckPortType(parameter0,outPort) THEN
  5812. IF outPort.direction # SyntaxTree.OutPort THEN
  5813. Error(parameter1.position,"not an out-port")
  5814. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5815. Error(parameter1.position,"incompatible to port type");
  5816. ELSE
  5817. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5818. actualParameters.SetExpression(1,parameter1);
  5819. END;
  5820. END;
  5821. (* ------- custom builtins ----- *)
  5822. ELSIF id = Global.systemSpecial THEN
  5823. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5824. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5825. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5826. type := procedureType.returnType;
  5827. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5828. (* go through all formal parameters *)
  5829. formalParameter := procedureType.firstParameter;
  5830. FOR i := 0 TO actualParameters.Length() - 1 DO
  5831. actualParameter := actualParameters.GetExpression(i);
  5832. IF actualParameter = SyntaxTree.invalidExpression THEN
  5833. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5834. Error(position, "incompatible parameter")
  5835. ELSE
  5836. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5837. END;
  5838. actualParameters.SetExpression(i, actualParameter);
  5839. formalParameter := formalParameter.nextParameter
  5840. END
  5841. END
  5842. ELSE
  5843. Error(position, "builtin not implemented");
  5844. result := SyntaxTree.invalidExpression;
  5845. END;
  5846. END;
  5847. IF result # SyntaxTree.invalidExpression THEN
  5848. type := ResolveType(type);
  5849. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5850. result.SetType(type);
  5851. END;
  5852. RETURN result
  5853. END NewBuiltinCallDesignator;
  5854. (** return type guard designator left(type)
  5855. - check if type can be extended (i.e. is no static record)
  5856. - check if type is a type extension of left.type
  5857. - returns new type guard designator
  5858. returns invalidDesignator = invalidExpression if error
  5859. **)
  5860. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5861. VAR result: SyntaxTree.Designator;
  5862. BEGIN
  5863. result := SyntaxTree.invalidDesignator;
  5864. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5865. Error(position, "no type extension of type");
  5866. IF VerboseErrorMessage THEN
  5867. Printout.Info("left",left);
  5868. Printout.Info("type",type);
  5869. END;
  5870. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5871. Error(position, "variable cannot be extended");
  5872. ELSIF IsUnsafePointer(left.type) THEN
  5873. Error(position, "forbidden type guard on unsafe pointer");
  5874. ELSE
  5875. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5876. result.SetType(type);
  5877. result.SetAssignable(left.assignable);
  5878. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5879. END;
  5880. RETURN result
  5881. END NewTypeGuardDesignator;
  5882. (** check and resolve parameter designator left(expression list)
  5883. - check expression list
  5884. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5885. - elsif left is a procedure type then
  5886. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5887. - else return is a procedure call then return ProcedureCallDesignator
  5888. returns invalidDesignator = invalidExpression if error
  5889. **)
  5890. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5891. VAR
  5892. parameters: SyntaxTree.ExpressionList;
  5893. left: SyntaxTree.Designator;
  5894. result,expression: SyntaxTree.Expression;
  5895. typeDeclaration: SyntaxTree.TypeDeclaration;
  5896. type, expressionType: SyntaxTree.Type;
  5897. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5898. BEGIN
  5899. type := type.resolved;
  5900. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5901. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5902. END;
  5903. RETURN type
  5904. END BaseType;
  5905. BEGIN
  5906. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5907. result := SyntaxTree.invalidDesignator;
  5908. left := ResolveDesignator(designator.left);
  5909. IF left # SyntaxTree.invalidDesignator THEN
  5910. parameters := designator.parameters;
  5911. IF ExpressionList(parameters) THEN
  5912. IF (left.type = NIL) THEN
  5913. Error(left.position,"object is not a procedure or cannot be extended");
  5914. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5915. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5916. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5917. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5918. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5919. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5920. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5921. ELSE
  5922. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5923. END
  5924. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5925. expression := parameters.GetExpression(0);
  5926. type := typeDeclaration.declaredType.resolved;
  5927. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5928. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5929. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5930. OR (expressionType IS SyntaxTree.EnumerationType)
  5931. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5932. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5933. Error(left.position,"invalid unsigned type in explicit conversion");
  5934. ELSE
  5935. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5936. END;
  5937. ELSE
  5938. Error(left.position,"invalid type in explicit conversion");
  5939. END;
  5940. ELSE
  5941. Error(left.position,"called object is not a procedure or cannot be extended");
  5942. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5943. result := SyntaxTree.invalidDesignator;
  5944. END;
  5945. ELSE
  5946. result := SyntaxTree.invalidDesignator
  5947. END;
  5948. END;
  5949. resolvedExpression := result;
  5950. END VisitParameterDesignator;
  5951. (** check dereference designator left^
  5952. - check if left is pointer type or left is object type
  5953. - return new dereference designator with type = left.baseType.type (if appropriate)
  5954. with error handling
  5955. returns invalidDesignator = invalidExpression if error
  5956. **)
  5957. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5958. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5959. BEGIN
  5960. result := SyntaxTree.invalidDesignator;
  5961. type := left.type;
  5962. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5963. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5964. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5965. result.SetAssignable(TRUE);
  5966. result.SetType(type);
  5967. result.SetHidden(left.isHidden);
  5968. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5969. type := type.resolved;
  5970. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5971. result.SetAssignable(TRUE);
  5972. result.SetType(type);
  5973. result.SetHidden(left.isHidden);
  5974. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5975. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5976. result.SetAssignable(TRUE);
  5977. result.SetType(type);
  5978. result.SetHidden(left.isHidden);
  5979. ELSE
  5980. Error(position, "dereference on no pointer");
  5981. IF VerboseErrorMessage THEN
  5982. Printout.Info("pointer", type);
  5983. Printout.Info("scope", currentScope);
  5984. END;
  5985. END;
  5986. RETURN result
  5987. END NewDereferenceDesignator;
  5988. (** check supercall designator left^
  5989. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5990. - return new supercall designator with type = left.type
  5991. with error handling
  5992. **)
  5993. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5994. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5995. objectScope: SyntaxTree.Scope;
  5996. BEGIN
  5997. result := SyntaxTree.invalidDesignator;
  5998. IF left = SyntaxTree.invalidDesignator THEN
  5999. (* error already handled *)
  6000. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  6001. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  6002. ASSERT(symbol # SyntaxTree.invalidSymbol);
  6003. IF symbol IS SyntaxTree.Procedure THEN
  6004. procedure := symbol(SyntaxTree.Procedure);
  6005. objectScope := currentScope;
  6006. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  6007. objectScope := objectScope.outerScope;
  6008. END;
  6009. IF (left.left = NIL) OR ~
  6010. (
  6011. (left.left IS SyntaxTree.SelfDesignator) OR
  6012. (left.left IS SyntaxTree.DereferenceDesignator)
  6013. & (left.left(SyntaxTree.Designator).left # NIL)
  6014. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  6015. Error(position, "procedure not in immediate object scope");
  6016. IF VerboseErrorMessage THEN
  6017. Printout.Info("left.left",left.left);
  6018. END;
  6019. ELSIF procedure.super # NIL THEN
  6020. result := SyntaxTree.NewSupercallDesignator(position,left);
  6021. result.SetType(left.type.resolved)
  6022. ELSE
  6023. Error(position, "no supermethod for this procedure");
  6024. END;
  6025. ELSE
  6026. Error(position, "symbol is not a procedure");
  6027. END;
  6028. ELSE
  6029. Error(position, "is no symbol designator");
  6030. END;
  6031. RETURN result
  6032. END NewSupercallDesignator;
  6033. (** check and semantically resolve arrow designator left^
  6034. - if left is procedure type -> result := SupercallDesignator
  6035. - else result := DereferenceDesignator
  6036. returns result via global variable resolvedExpression
  6037. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6038. **)
  6039. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6040. VAR left: SyntaxTree.Designator;
  6041. BEGIN
  6042. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6043. left := ResolveDesignator(arrowDesignator.left);
  6044. IF left # NIL THEN
  6045. IF (left.type = NIL) THEN
  6046. Error(arrowDesignator.position,"Invalid arrow designator");
  6047. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6048. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6049. ELSE
  6050. IF IsPointerToObject(left.type) THEN
  6051. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6052. END;
  6053. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6054. END
  6055. END
  6056. END VisitArrowDesignator;
  6057. (** check and return expression
  6058. - if expression has no type then resolve expression
  6059. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6060. - return result
  6061. **)
  6062. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6063. VAR result,prev: SyntaxTree.Expression;
  6064. BEGIN
  6065. IF expression = NIL THEN result := NIL
  6066. ELSIF (expression.type = NIL) THEN
  6067. prev := resolvedExpression;
  6068. resolvedExpression := SyntaxTree.invalidExpression;
  6069. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6070. expression.SetType(SyntaxTree.invalidType);
  6071. END;
  6072. expression.Accept(SELF);
  6073. result := resolvedExpression;
  6074. IF currentIsRealtime THEN
  6075. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6076. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6077. END;
  6078. END;
  6079. (* designator modifiers for backends if they support it ...*)
  6080. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6081. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6082. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6083. END;
  6084. resolvedExpression := prev
  6085. ELSE
  6086. result := expression
  6087. END;
  6088. RETURN result
  6089. END ResolveExpression;
  6090. (**
  6091. check expression to be constant expression
  6092. - resolve expression
  6093. - if valid then check that of value type
  6094. report error and return invalidExpression if anything fails
  6095. **)
  6096. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6097. VAR position: Position;
  6098. BEGIN
  6099. position := expression.position;
  6100. expression := ResolveExpression(expression);
  6101. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6102. ELSIF (expression.resolved = NIL) THEN
  6103. Error(position, "expression is not constant");
  6104. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6105. expression := SyntaxTree.invalidExpression;
  6106. END;
  6107. RETURN expression
  6108. END ConstantExpression;
  6109. (** check expression to be constant integer
  6110. - resolve expresssion
  6111. - if valid then check that of integer value type
  6112. report error and return invalidExpression if anything fails
  6113. **)
  6114. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6115. VAR position: Position;
  6116. BEGIN
  6117. position := expression.position;
  6118. expression := ResolveExpression(expression);
  6119. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6120. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6121. expression := SyntaxTree.invalidExpression;
  6122. Error(position, "expression is not a constant integer");
  6123. END;
  6124. RETURN expression
  6125. END ConstantInteger;
  6126. (** check expression as positive (>=0) constant integer
  6127. - resolve expression
  6128. - if valid then check that integer value
  6129. - if integer value then check that value >= 0
  6130. report error and return invalidExpression if anything fails
  6131. **)
  6132. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6133. VAR position: Position;
  6134. BEGIN
  6135. position := expression.position;
  6136. expression := ConstantExpression(expression);
  6137. IF expression = SyntaxTree.invalidExpression THEN
  6138. (* error already reported *)
  6139. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6140. Error(position, "expression is not integer valued");
  6141. expression := SyntaxTree.invalidExpression
  6142. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6143. Error(position, "integer is not greater or equal zero");
  6144. END;
  6145. RETURN expression
  6146. END ConstantIntegerGeq0;
  6147. (** check expression as condition
  6148. - resolve expression
  6149. - if valid expression then check that result type is boolean
  6150. report error and return invalidExpression if anything fails
  6151. **)
  6152. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6153. VAR position: Position;
  6154. BEGIN
  6155. position := expression.position;
  6156. expression := ResolveExpression(expression);
  6157. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6158. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6159. expression := SyntaxTree.invalidExpression;
  6160. Error(position, "expression is not boolean");
  6161. END;
  6162. RETURN expression
  6163. END ResolveCondition;
  6164. (*** symbols ***)
  6165. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6166. BEGIN
  6167. x.Accept(SELF);
  6168. END ResolveSymbol;
  6169. (** check a symbol
  6170. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6171. **)
  6172. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6173. VAR scope: SyntaxTree.Scope;
  6174. BEGIN
  6175. (* visibility *)
  6176. scope := symbol.scope;
  6177. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6178. scope := scope.outerScope;
  6179. END;
  6180. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6181. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6182. Error(symbol.position,"cannot be exported");
  6183. IF VerboseErrorMessage THEN
  6184. Printout.Info("symbol",symbol);
  6185. END;
  6186. END;
  6187. END;
  6188. END CheckSymbolVisibility;
  6189. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6190. If node is currently being resolved then emit a cyclic definition error.
  6191. Return TRUE only if node is fully resolved.
  6192. **)
  6193. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6194. VAR result: BOOLEAN;
  6195. BEGIN
  6196. IF SyntaxTree.Resolved IN x.state THEN
  6197. result := FALSE
  6198. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6199. Error(x.position,"cyclic definition");
  6200. result := FALSE;
  6201. ELSE
  6202. result := TRUE;
  6203. x.SetState(SyntaxTree.BeingResolved)
  6204. END;
  6205. RETURN result
  6206. END SymbolNeedsResolution;
  6207. (** check and resolve a type declaration symbol = Type
  6208. - set type to declaration type
  6209. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6210. This is so because the type declaration itself does not have a type but it only stands for a type.
  6211. In the implementation of the compiler this made a lot much easier.
  6212. - resolve and set declared type
  6213. - check symbol
  6214. **)
  6215. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6216. VAR prevScope: SyntaxTree.Scope;
  6217. BEGIN
  6218. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6219. IF SymbolNeedsResolution(typeDeclaration) THEN
  6220. typeDeclaration.SetState(SyntaxTree.Resolved);
  6221. prevScope := currentScope;
  6222. currentScope := typeDeclaration.scope;
  6223. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6224. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6225. CheckSymbolVisibility(typeDeclaration);
  6226. typeDeclaration.SetState(SyntaxTree.Resolved);
  6227. currentScope := prevScope;
  6228. END;
  6229. END VisitTypeDeclaration;
  6230. (** check and resolve a constant declaration symbol = (constant) expression
  6231. - check expression
  6232. - set type and value
  6233. - check symbol
  6234. **)
  6235. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6236. VAR
  6237. expression: SyntaxTree.Expression;
  6238. type: SyntaxTree.Type;
  6239. name: Basic.SegmentedName;
  6240. replacement: Replacement;
  6241. BEGIN
  6242. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6243. IF SymbolNeedsResolution(constant) THEN
  6244. expression := constant.value;
  6245. IF replacements # NIL THEN
  6246. Global.GetSymbolSegmentedName(constant, name);
  6247. replacement := replacements;
  6248. WHILE (replacement # NIL) & (replacement.name # name) DO
  6249. replacement := replacement.next;
  6250. END;
  6251. IF replacement # NIL THEN
  6252. InfoSS(constant.position, "replacing constant", constant.name);
  6253. (*
  6254. NEW(stringReader, Strings.Length(replacement.string^));
  6255. stringReader.Set(replacement.string^);
  6256. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6257. NEW(parser, scanner, diagnostics);
  6258. expression := parser.Expression();
  6259. *)
  6260. expression := replacement.expression;
  6261. replacement.used := TRUE;
  6262. END;
  6263. END;
  6264. constant.SetType(SyntaxTree.invalidType);
  6265. expression := ConstantExpression(expression);
  6266. ASSERT(expression.type # NIL);
  6267. type := expression.type.resolved;
  6268. constant.SetType(type);
  6269. constant.SetValue(expression);
  6270. CheckSymbolVisibility(constant);
  6271. constant.SetState(SyntaxTree.Resolved);
  6272. END;
  6273. END VisitConstant;
  6274. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6275. VAR procedureAlignment: LONGINT;
  6276. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6277. (* least common multiple *)
  6278. VAR a,b: LONGINT;
  6279. BEGIN
  6280. a := a0; b := b0;
  6281. WHILE (a # b) DO
  6282. IF a < b THEN a := a+a0
  6283. ELSE b := b + b0
  6284. END;
  6285. END;
  6286. RETURN a
  6287. END LCM;
  6288. BEGIN
  6289. IF alignment > 1 THEN
  6290. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6291. IF (procedureAlignment > 1) THEN
  6292. alignment := LCM(alignment, procedureAlignment);
  6293. END;
  6294. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6295. END;
  6296. END AdaptStackAlignment;
  6297. (** check and resolve a variable / field
  6298. - check and set type
  6299. - negative check on open array type
  6300. - check symbol
  6301. **)
  6302. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6303. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6304. BEGIN
  6305. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6306. IF SymbolNeedsResolution(variable) THEN
  6307. modifiers := variable.modifiers;
  6308. (*
  6309. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6310. variable.AddFlags(flags);
  6311. *)
  6312. variable.SetType(ResolveType(variable.type));
  6313. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6314. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6315. Error(variable.position,"forbidden open array variable");
  6316. END;
  6317. END;
  6318. CheckSymbolVisibility(variable);
  6319. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6320. variable.SetUntraced(TRUE);
  6321. IF ~ContainsPointer(variable.type) THEN
  6322. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6323. Error(position, "untraced flag on non-pointer variable");
  6324. END;
  6325. END;
  6326. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6327. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6328. IF ~PowerOf2(value) THEN
  6329. Error(position, "forbidden alignment - must be power of two");
  6330. ELSE
  6331. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6332. END;
  6333. END;
  6334. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6335. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6336. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6337. Error(position, "fixed position not possible in procedure");
  6338. END;
  6339. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6340. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6341. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6342. Error(position,"fictive offset not possible in procedure");
  6343. END;
  6344. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6345. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6346. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6347. END;
  6348. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6349. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6350. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6351. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6352. END;
  6353. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6354. IF variable.initializer # NIL THEN
  6355. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6356. END;
  6357. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6358. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6359. pointerType.SetPointerBase(variable.type);
  6360. pointerType.SetHidden(TRUE);
  6361. variable.SetType(ResolveType(pointerType));
  6362. END;
  6363. variable.SetState(SyntaxTree.Resolved);
  6364. END;
  6365. END VisitVariable;
  6366. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6367. BEGIN
  6368. VisitVariable(property)
  6369. END VisitProperty;
  6370. (** check and resolve a (procedure) parameter
  6371. - check and set type
  6372. - check symbol
  6373. - check parameter kind and set read-only flags if appropriate
  6374. **)
  6375. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6376. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6377. BEGIN
  6378. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6379. IF SymbolNeedsResolution(parameter) THEN
  6380. modifiers := parameter.modifiers;
  6381. parameter.SetType(ResolveType(parameter.type));
  6382. ASSERT(parameter.type.resolved # NIL);
  6383. CheckSymbolVisibility(parameter);
  6384. IF parameter.defaultValue # NIL THEN
  6385. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6386. Error(parameter.position,"forbidden default value on non-value parameter");
  6387. ELSE
  6388. expression := ConstantExpression(parameter.defaultValue);
  6389. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6390. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6391. parameter.SetDefaultValue(expression);
  6392. END;
  6393. END;
  6394. END;
  6395. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6396. Error(parameter.position, "forbidden value parameter of math array type ");
  6397. END;
  6398. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6399. parameter.SetUntraced(TRUE);
  6400. IF ~ContainsPointer(parameter.type) THEN
  6401. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6402. Error(position, "untraced flag on non-pointer variable");
  6403. END;
  6404. END;
  6405. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6406. parameter.SetMoveable(TRUE);
  6407. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6408. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6409. Error(position, "illegal movable flag on non-address variable");
  6410. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6411. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6412. Error(position, "unnecessary movable flag on variable variable");
  6413. END;
  6414. END;
  6415. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6416. parameter.SetState(SyntaxTree.Resolved);
  6417. END;
  6418. END VisitParameter;
  6419. (** check and resolve a procedure (with declaration and implementation scope)
  6420. - check the procedure type
  6421. - check if method (i.e. in record scope), if so then
  6422. - check if (unique) constructor
  6423. - check if (unique) finalizer
  6424. - check if super method available, if so then check signature
  6425. - of not in record scope then negative check on constructor flag
  6426. - of not in record scope then negative check on finalizer flag
  6427. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6428. - check procedure symbol
  6429. **)
  6430. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6431. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6432. procedureType: SyntaxTree.ProcedureType;
  6433. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6434. qualifiedType: SyntaxTree.QualifiedType;
  6435. value: Basic.Integer;
  6436. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6437. position: Position;
  6438. fp: SyntaxTree.FingerPrint;
  6439. BEGIN
  6440. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6441. IF IsOberonInline(procedure) THEN
  6442. IF SyntaxTree.Public * procedure.access # {} THEN
  6443. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6444. END;
  6445. procedure.SetInline(FALSE);
  6446. procedure.SetOberonInline(TRUE);
  6447. END;
  6448. IF SymbolNeedsResolution(procedure) THEN
  6449. recentIsRealtime := currentIsRealtime;
  6450. recentIsBodyProcedure := currentIsBodyProcedure;
  6451. CheckSymbolVisibility(procedure);
  6452. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6453. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6454. modifiers := procedureType.modifiers;
  6455. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6456. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6457. IF useDarwinCCalls THEN (*fld*)
  6458. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6459. ELSE
  6460. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6461. END
  6462. END;
  6463. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6464. procedureType.SetInterrupt(TRUE);
  6465. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6466. END;
  6467. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6468. procedureType.SetNoReturn(TRUE);
  6469. END;
  6470. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6471. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6472. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6473. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6474. END;
  6475. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6476. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6477. END;
  6478. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6479. IF ~PowerOf2(value) THEN
  6480. Error(position, "forbidden stack alignment - must be power of two");
  6481. ELSE
  6482. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6483. END;
  6484. END;
  6485. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6486. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6487. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6488. END;
  6489. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6490. SyntaxTree.InitFingerPrint(fp);
  6491. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6492. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6493. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6494. fp.shallowAvailable := TRUE;
  6495. procedure.SetFingerPrint(fp);
  6496. END;
  6497. CheckModifiers(modifiers, TRUE);
  6498. modifiers := procedureType.returnTypeModifiers;
  6499. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6500. CheckModifiers(modifiers, TRUE);
  6501. procedure.SetState(SyntaxTree.Resolved);
  6502. FixProcedureType(procedureType);
  6503. currentIsRealtime := procedureType.isRealtime;
  6504. currentIsBodyProcedure := procedure.isBodyProcedure;
  6505. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6506. THEN
  6507. Error(procedure.position,"problems during parameter offset computation");
  6508. END;
  6509. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6510. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6511. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6512. Error(procedure.position,"problem during parameter offset generation");
  6513. END;
  6514. END;
  6515. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6516. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6517. procedureType.SetDelegate(TRUE);
  6518. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6519. (* add auto-self *)
  6520. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6521. IF (record.pointerType.typeDeclaration = NIL) THEN
  6522. selfParameter.SetType(record.pointerType);
  6523. ELSE
  6524. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6525. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6526. qualifiedType.SetResolved(record.pointerType);
  6527. selfParameter.SetType(qualifiedType);
  6528. END;
  6529. selfParameter.SetAccess(SyntaxTree.Hidden);
  6530. END;
  6531. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6532. IF procedure.isConstructor THEN
  6533. (*! constructor is always visible, compatibility to paco
  6534. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6535. *)
  6536. procedure.MarkUsed;
  6537. IF procedureType.returnType # NIL THEN
  6538. Error(procedure.position,"constructor with forbidden return type");
  6539. END;
  6540. proc := procedure.scope.firstProcedure;
  6541. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6542. proc := proc.nextProcedure;
  6543. END;
  6544. IF proc # NIL THEN
  6545. Error(procedure.position,"duplicate constructor")
  6546. ELSE
  6547. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6548. END;
  6549. END;
  6550. IF procedure.isFinalizer THEN
  6551. procedure.MarkUsed;
  6552. IF procedureType.returnType # NIL THEN
  6553. Error(procedure.position,"finalizer with forbidden return type");
  6554. END;
  6555. IF procedureType.numberParameters # 0 THEN
  6556. Error(procedure.position,"finalizer with formal parameters");
  6557. END;
  6558. proc := procedure.scope.firstProcedure;
  6559. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6560. proc := proc.nextProcedure;
  6561. END;
  6562. IF proc # NIL THEN
  6563. Error(procedure.position,"duplicate finalizer")
  6564. ELSE
  6565. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6566. END;
  6567. END;
  6568. super := FindSuperProcedure(record.recordScope, procedure);
  6569. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6570. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6571. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6572. END;
  6573. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6574. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6575. END;
  6576. IF super.isFinal THEN
  6577. Error(procedure.position,"forbidden method extending final method");
  6578. END;
  6579. (*
  6580. IF super.access # procedure.access THEN
  6581. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6582. END;
  6583. *)
  6584. procedure.SetSuper(super);
  6585. super.SetOverwritten(TRUE);
  6586. procedure.SetAccess(procedure.access+super.access);
  6587. procedure.MarkUsed;
  6588. END;
  6589. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6590. THEN
  6591. Error(procedure.position,"problems during parameter offset computation");
  6592. END;
  6593. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6594. IF cellsAreObjects THEN
  6595. procedureType.SetDelegate(TRUE);
  6596. END;
  6597. IF procedure.isConstructor THEN
  6598. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6599. END;
  6600. ELSIF procedure.isConstructor THEN
  6601. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6602. END;
  6603. Declarations(procedure.procedureScope, FALSE, {0,1});
  6604. (* body resolution part done as late fix of the procedure type *)
  6605. procedure.SetState(SyntaxTree.Resolved);
  6606. currentIsRealtime := recentIsRealtime;
  6607. currentIsBodyProcedure := recentIsBodyProcedure;
  6608. END;
  6609. END VisitProcedure;
  6610. (**
  6611. a builtin procedure is a global item that may not be modified locally
  6612. instead the resolving of builtin procedure calls are done in the esignator
  6613. **)
  6614. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6615. VAR type: SyntaxTree.Type;
  6616. BEGIN
  6617. type := ResolveType(builtinProcedure.type);
  6618. END VisitBuiltin;
  6619. (* nopov *)
  6620. (** check and resolve operator
  6621. - operators are first checked as procedures
  6622. - then additional operator-specific checks are done
  6623. - note that only module-scope operators are checked here
  6624. (operators in a record scope are only allowed in the context of
  6625. array-structured object types and checked in 'ResolveArrayStructure')
  6626. - also note that inter-operator conformity is not checked here
  6627. **)
  6628. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6629. VAR
  6630. procedureType: SyntaxTree.ProcedureType;
  6631. leftType, rightType: SyntaxTree.Type;
  6632. identifierNumber: LONGINT; position: Position;
  6633. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6634. modifiers: SyntaxTree.Modifier;
  6635. (** whether a type is locally defined in the current module scope
  6636. for arrays, the base type must be locally defined **)
  6637. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6638. BEGIN
  6639. IF type = NIL THEN
  6640. RETURN FALSE
  6641. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6642. RETURN TRUE
  6643. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6644. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6645. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6646. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6647. ELSE
  6648. RETURN FALSE
  6649. END
  6650. END IsLocallyDefined;
  6651. BEGIN
  6652. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6653. procedureType := operator.type(SyntaxTree.ProcedureType);
  6654. modifiers := procedureType.modifiers;
  6655. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6656. CheckModifiers(modifiers, TRUE);
  6657. VisitProcedure(operator);
  6658. IF operator.scope IS SyntaxTree.RecordScope THEN
  6659. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6660. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6661. IF identifierNumber = -1 THEN
  6662. Error(operator.position, "operator with unknown identifier")
  6663. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6664. Error(operator.position, "identifier may not be used for operator")
  6665. ELSE
  6666. IF procedureType.numberParameters < 1 THEN
  6667. Error(operator.position, "operator without operand");
  6668. ELSIF procedureType.numberParameters > 2 THEN
  6669. Error(operator.position, "operator with more than two operands");
  6670. ELSE
  6671. (* determine operand types *)
  6672. leftType := procedureType.firstParameter.type;
  6673. IF procedureType.numberParameters > 1 THEN
  6674. rightType := procedureType.firstParameter.nextParameter.type
  6675. ELSE
  6676. rightType := NIL
  6677. END;
  6678. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6679. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6680. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6681. Error(operator.position, "none of the operands is declared in the same module")
  6682. END
  6683. END;
  6684. (* TODO: refine the checks, think about how restrictive the checks should be
  6685. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6686. They might be used for intersection, union, complement of custom object types *)
  6687. (* defaults *)
  6688. hasReturnType := TRUE;
  6689. mustBeUnary := FALSE;
  6690. mustBeBinary := FALSE;
  6691. mustReturnBoolean := FALSE;
  6692. mustReturnInteger := FALSE;
  6693. mustHaveEquitypedOperands := FALSE;
  6694. (* operator-specific exceptions *)
  6695. CASE identifierNumber OF
  6696. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6697. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6698. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6699. mustBeBinary := TRUE
  6700. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6701. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6702. | Scanner.Times: mustBeBinary := TRUE
  6703. | Scanner.TimesTimes: mustBeBinary := TRUE
  6704. | Scanner.DotTimes: mustBeBinary := TRUE
  6705. | Scanner.PlusTimes: mustBeBinary := TRUE
  6706. | Scanner.Slash: mustBeBinary := TRUE
  6707. | Scanner.Backslash: mustBeBinary := TRUE
  6708. | Scanner.DotSlash: mustBeBinary := TRUE
  6709. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6710. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6711. | Scanner.Not: mustBeUnary := TRUE
  6712. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6713. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6714. | Scanner.Transpose: mustBeUnary := TRUE;
  6715. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6716. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6717. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6718. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6719. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6720. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6721. | Global.Abs: mustBeUnary := TRUE;
  6722. | Global.Ash: (* TODO: arity? *)
  6723. | Global.Cap: (* TODO: arity? *)
  6724. | Global.Chr: mustBeUnary := TRUE;
  6725. | Global.Entier: (* TODO: arity? *)
  6726. | Global.EntierH: (* TODO: arity? *)
  6727. | Global.Len: (* unary and binary *)
  6728. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6729. | Global.Max, Global.Min: (* unary and binary *)
  6730. | Global.Odd: (* TODO: arity? *)
  6731. | Global.Sum: (* TODO: arity? *)
  6732. | Global.All: (* TODO: arity? *)
  6733. | Global.Re, Global.Im:
  6734. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6735. | Scanner.Alias:
  6736. | Scanner.GreaterGreater, Scanner.LessLess:
  6737. mustBeBinary := TRUE; hasReturnType := FALSE;
  6738. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6739. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6740. END;
  6741. (* check parameter count *)
  6742. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6743. Error(operator.position,"operator is not unary")
  6744. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6745. Error(operator.position,"operator is not binary")
  6746. END;
  6747. (* check parameter types *)
  6748. (* TODO: is this used at all? *)
  6749. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6750. leftType := procedureType.firstParameter.type;
  6751. rightType := procedureType.firstParameter.nextParameter.type;
  6752. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6753. Error(operator.position, "the two operands are not of the same type")
  6754. END
  6755. END;
  6756. (* check return type *)
  6757. IF hasReturnType THEN
  6758. IF procedureType.returnType = NIL THEN
  6759. Error(operator.position, "return type required")
  6760. ELSIF mustReturnBoolean THEN
  6761. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6762. Error(operator.position,"return type is not Boolean")
  6763. END
  6764. ELSIF mustReturnInteger THEN
  6765. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  6766. Error(operator.position,"return type is no size type")
  6767. END
  6768. END
  6769. ELSIF procedureType.returnType # NIL THEN
  6770. Error(operator.position, "return type not allowed")
  6771. END
  6772. END
  6773. END
  6774. END
  6775. END VisitOperator;
  6776. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6777. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6778. BEGIN
  6779. IF error THEN RETURN FALSE END;
  6780. prevScope := currentScope;
  6781. prevDiagnostics := diagnostics;
  6782. diagnostics := NIL; (* suppress error output *)
  6783. currentScope := module.moduleScope;
  6784. VisitImport(x);
  6785. IF ~error THEN
  6786. module.moduleScope.AddImport(x);
  6787. x.SetScope(module.moduleScope);
  6788. END;
  6789. currentScope := prevScope;
  6790. diagnostics := prevDiagnostics;
  6791. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6792. END AddImport;
  6793. (** check and resolve import
  6794. - check for name = SYSTEM
  6795. - check for forbidden self import
  6796. - search through global import cache: already imported?
  6797. - check if already imported indirectly
  6798. - import if necessary -> set module and enter into import cache
  6799. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6800. - after this import this direct import and all indirect imports are stored in the current module's import list
  6801. **)
  6802. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6803. VAR
  6804. module: SyntaxTree.Module;
  6805. moduleScope: SyntaxTree.ModuleScope;
  6806. import,reimport: SyntaxTree.Import;
  6807. filename: FileName;
  6808. prevScope: SyntaxTree.Scope;
  6809. BEGIN
  6810. IF SymbolNeedsResolution(x) THEN
  6811. prevScope := currentScope;
  6812. x.SetType(SyntaxTree.importType);
  6813. moduleScope := currentScope.ownerModule.moduleScope;
  6814. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6815. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6816. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6817. Error(x.position,"forbidden self import");
  6818. ELSE
  6819. (* search through global import list: already imported ? *)
  6820. IF (x.module = NIL) & (importCache # NIL) THEN
  6821. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6822. ELSE import := NIL
  6823. END;
  6824. IF x.module # NIL THEN (* already imported indirectly *)
  6825. module := x.module;
  6826. ELSIF import # NIL THEN (* already in module list *)
  6827. module := import.module;
  6828. ASSERT(module # NIL);
  6829. x.SetModule(module);
  6830. ELSE (* must be imported *)
  6831. Global.ModuleFileName(x.moduleName,x.context,filename);
  6832. IF symbolFileFormat # NIL THEN
  6833. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6834. IF module = NIL THEN
  6835. ErrorSS(x.position,"could not import",filename);
  6836. IF VerboseErrorMessage THEN
  6837. Printout.Info("import",x)
  6838. END
  6839. ELSE
  6840. (*
  6841. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6842. (*! should rather be done by importer *)
  6843. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6844. checker.importCache := importCache;
  6845. checker.arrayBaseImported := arrayBaseImported;
  6846. checker.global := global;
  6847. checker.Module(module); (* semantic check *)
  6848. error := error OR checker.error;
  6849. END;
  6850. *)
  6851. (*
  6852. ASSERT(SyntaxTree.Resolved IN module.state);
  6853. *)
  6854. x.SetModule(module);
  6855. IF importCache # NIL THEN
  6856. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6857. import.SetContext(x.context);
  6858. import.SetModule(module);
  6859. importCache.AddImport(import);
  6860. END;
  6861. END;
  6862. ELSE
  6863. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6864. END;
  6865. END;
  6866. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6867. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6868. END;
  6869. import := module.moduleScope.firstImport;
  6870. WHILE(import # NIL) DO
  6871. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6872. ASSERT(currentScope # NIL);
  6873. ASSERT(currentScope.ownerModule # NIL);
  6874. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6875. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6876. Error(x.position,"recursive import");
  6877. ELSE
  6878. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6879. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6880. IF reimport = NIL THEN (* indirect import *)
  6881. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6882. reimport.SetContext(import.context);
  6883. reimport.SetModule(import.module);
  6884. moduleScope.AddImport(reimport);
  6885. reimport.SetScope(moduleScope);
  6886. ELSE
  6887. ASSERT(import.module # NIL);
  6888. reimport.SetModule(import.module); (* direct or indirect import *)
  6889. END;
  6890. END;
  6891. import := import.nextImport;
  6892. END;
  6893. END;
  6894. END;
  6895. currentScope := prevScope;
  6896. (* ELSE nothing to be done *)
  6897. x.SetState(SyntaxTree.Resolved);
  6898. END;
  6899. END VisitImport;
  6900. (*** statements ***)
  6901. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6902. VAR prev,resolved: SyntaxTree.Statement;
  6903. BEGIN
  6904. prev := resolvedStatement;
  6905. resolvedStatement := x;
  6906. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6907. activeCellsStatement := FALSE;
  6908. x.Accept(SELF);
  6909. (* removed this, implementation restriction should be resolved by backend
  6910. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6911. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6912. END;
  6913. *)
  6914. resolved := resolvedStatement;
  6915. resolvedStatement := prev;
  6916. RETURN resolved
  6917. END ResolveStatement;
  6918. (** check and resolve statement sequence
  6919. - check all statements, replace if necessary
  6920. **)
  6921. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6922. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6923. BEGIN
  6924. IF statementSequence # NIL THEN (* else empty *)
  6925. FOR i := 0 TO statementSequence.Length()-1 DO
  6926. statement := statementSequence.GetStatement(i);
  6927. resolved := ResolveStatement(statement);
  6928. IF (resolved # statement) THEN
  6929. statementSequence.SetStatement(i,resolved);
  6930. END;
  6931. END;
  6932. END;
  6933. END StatementSequence;
  6934. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6935. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6936. - check if procedure is callable
  6937. - check return type = NIL (otherwise must be assignment statement)
  6938. **)
  6939. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6940. VAR call: SyntaxTree.Designator;
  6941. BEGIN
  6942. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6943. call := procedureCall.call;
  6944. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6945. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6946. END;
  6947. call := ResolveDesignator(call);
  6948. IF call = SyntaxTree.invalidDesignator THEN
  6949. (* error already handled *)
  6950. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6951. (* inline call in a statement *)
  6952. ELSIF ~IsCallable(call) THEN
  6953. Error(procedureCall.position,"called object is not a procedure");
  6954. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6955. Error(procedureCall.position,"calling procedure with non-void return type");
  6956. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6957. END;
  6958. procedureCall.SetCall(call);
  6959. (*
  6960. IF call = SyntaxTree.invalidDesignator THEN
  6961. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6962. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6963. IF IsOberonInline(procedure) THEN
  6964. Warning(procedure.position,"call to inline proc");
  6965. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6966. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6967. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6968. resolvedStatement := block;
  6969. RETURN;
  6970. END;
  6971. END;
  6972. *)
  6973. END VisitProcedureCallStatement;
  6974. (** check and resolve assignment LHS := RHS
  6975. - resolve LHS and RHS
  6976. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6977. - check if assignment is compatible
  6978. - check if LHS is variable (i.e. assignable)
  6979. - convert RHS if necessary
  6980. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6981. - assignment between different ASOTs
  6982. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6983. - assignment to ASOT elements:
  6984. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6985. **)
  6986. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6987. VAR
  6988. left: SyntaxTree.Designator;
  6989. right, expression: SyntaxTree.Expression;
  6990. designator: SyntaxTree.Designator;
  6991. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6992. mathArrayType: SyntaxTree.MathArrayType;
  6993. BEGIN
  6994. right := ResolveExpression(assignment.right);
  6995. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6996. left := ResolveDesignator(assignment.left);
  6997. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6998. (* error already handled *)
  6999. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  7000. (* LHS is index write operator call on ASOT *)
  7001. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  7002. (* necessary ?
  7003. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  7004. type := procedureType.firstParameter.type;
  7005. expression := procedureCallDesignator.parameters.GetExpression(0);
  7006. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  7007. *)
  7008. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7009. ELSIF CheckVariable(left) THEN
  7010. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  7011. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7012. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7013. (* conversion done by procedure call
  7014. (* try to convert to left argument *)
  7015. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7016. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7017. procedureCallDesignator.parameters.SetExpression(1, right);
  7018. END;
  7019. *)
  7020. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7021. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7022. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7023. ELSIF AssignmentCompatible(left, right) THEN
  7024. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7025. mathArrayType := MathArrayStructureOfType(left.type);
  7026. right := NewConversion(right.position, right, mathArrayType, NIL);
  7027. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7028. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7029. ELSE
  7030. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7031. assignment.SetLeft(left);
  7032. assignment.SetRight(right);
  7033. resolvedStatement := assignment
  7034. END
  7035. END
  7036. END
  7037. END VisitAssignment;
  7038. (** check and resolve assignment LHS := RHS
  7039. - resolve LHS and RHS
  7040. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7041. - check if assignment is compatible
  7042. - check if LHS is variable (i.e. assignable)
  7043. - convert RHS if necessary
  7044. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7045. - assignment between different ASOTs
  7046. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7047. - assignment to ASOT elements:
  7048. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7049. **)
  7050. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7051. VAR
  7052. left: SyntaxTree.Designator;
  7053. right: SyntaxTree.Expression;
  7054. inPort, outPort: SyntaxTree.PortType;
  7055. expression: SyntaxTree.Expression;
  7056. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7057. BEGIN
  7058. right := ResolveExpression(communication.right);
  7059. left := ResolveDesignator(communication.left);
  7060. communication.SetLeft(left);
  7061. communication.SetRight(right);
  7062. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7063. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7064. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7065. (* conversion done by procedure call
  7066. (* try to convert to left argument *)
  7067. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7068. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7069. procedureCallDesignator.parameters.SetExpression(1, right);
  7070. END;
  7071. *)
  7072. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7073. ELSE
  7074. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7075. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7076. (* error already handled *)
  7077. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7078. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7079. IF outPort.direction # SyntaxTree.OutPort THEN
  7080. Error(left.position,"not an out-port")
  7081. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7082. Error(left.position,"incompatible to port type");
  7083. ELSE
  7084. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7085. communication.SetRight(right)
  7086. END;
  7087. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7088. IF CheckVariable(left) THEN
  7089. IF inPort.direction # SyntaxTree.InPort THEN
  7090. Error(left.position,"not an in-port")
  7091. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7092. Error(right.position,"incompatible to port type");
  7093. END;
  7094. END;
  7095. ELSE
  7096. Error(communication.position,"unsupported stream operation");
  7097. END;
  7098. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7099. IF outPort.direction # SyntaxTree.OutPort THEN
  7100. Error(left.position,"not an out-port")
  7101. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7102. Error(left.position,"incompatible to port type");
  7103. ELSE
  7104. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7105. communication.SetRight(right)
  7106. END;
  7107. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7108. IF CheckVariable(right) THEN
  7109. IF inPort.direction # SyntaxTree.InPort THEN
  7110. Error(left.position,"not an in-port")
  7111. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7112. Error(right.position,"incompatible to port type");
  7113. END;
  7114. END;
  7115. ELSE
  7116. Error(communication.position, "unsupported operation");
  7117. END;
  7118. END;
  7119. END VisitCommunicationStatement;
  7120. (** check and resolve if/eslif part
  7121. - check condition
  7122. - check statement sequence
  7123. **)
  7124. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7125. VAR prevUnreachable, b: BOOLEAN;
  7126. BEGIN
  7127. prevUnreachable := currentIsUnreachable;
  7128. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7129. IF IsBooleanValue(ifPart.condition,b) THEN
  7130. IF b=FALSE THEN
  7131. currentIsUnreachable := TRUE
  7132. ELSIF b=TRUE THEN
  7133. true := TRUE
  7134. END;
  7135. END;
  7136. StatementSequence(ifPart.statements);
  7137. currentIsUnreachable := prevUnreachable;
  7138. END IfPart;
  7139. (** check and resolve if statement
  7140. - check if parts and else part statement sequence
  7141. **)
  7142. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7143. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7144. BEGIN
  7145. prevUnreachable := currentIsUnreachable;
  7146. ifPartTrue := FALSE;
  7147. IfPart(ifStatement.ifPart,ifPartTrue);
  7148. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7149. elsif := ifStatement.GetElsifPart(i);
  7150. IfPart(elsif,ifPartTrue);
  7151. END;
  7152. IF ifStatement.elsePart # NIL THEN
  7153. IF ifPartTrue THEN
  7154. currentIsUnreachable := TRUE
  7155. END;
  7156. StatementSequence(ifStatement.elsePart)
  7157. END;
  7158. currentIsUnreachable := prevUnreachable;
  7159. END VisitIfStatement;
  7160. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7161. VAR variable: SyntaxTree.Designator;
  7162. type,variableType: SyntaxTree.Type;
  7163. withEntry: WithEntry;
  7164. BEGIN
  7165. variable := ResolveDesignator(withPart.variable);
  7166. variableType := variable.type.resolved;
  7167. withPart.SetVariable(variable);
  7168. type := ResolveType(withPart.type);
  7169. withPart.SetType(type);
  7170. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7171. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7172. END;
  7173. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7174. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7175. Error(variable.position,"is not extensible designator");
  7176. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7177. Error(variable.position,"is no local variable ");
  7178. IF VerboseErrorMessage THEN
  7179. Printout.Info("variable",variable)
  7180. END;
  7181. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7182. Error(variable.position,"withguarded symbol is no type extension of ");
  7183. IF VerboseErrorMessage THEN
  7184. Printout.Info("variable",variable);
  7185. Printout.Info("type",type);
  7186. END;
  7187. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7188. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7189. Error(variable.position,"withguarded symbol is no variable ");
  7190. IF VerboseErrorMessage THEN
  7191. Printout.Info("variable",variable);
  7192. Printout.Info("type",type);
  7193. END;
  7194. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7195. Error(variable.position,"invalid change of withguarded symbol");
  7196. ELSE
  7197. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7198. NEW(withEntry);
  7199. withEntry.previous := withEntries;
  7200. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7201. withEntry.type := type;
  7202. withEntries := withEntry;
  7203. StatementSequence(withPart.statements);
  7204. withEntries := withEntries.previous;
  7205. END;
  7206. END WithPart;
  7207. (** check and resolve with statement WITH variable: type DO ... END;
  7208. - check type and variable
  7209. - check that variable type is type extension of type
  7210. - check that variable is a variable
  7211. - enter new with scope and enter guardedVariable with same name and reference to variable
  7212. - create if statement:
  7213. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7214. **)
  7215. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7216. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7217. BEGIN
  7218. prevScope := currentScope; symbol := NIL;
  7219. FOR i := 0 TO withStatement.WithParts()-1 DO
  7220. WithPart(withStatement.GetWithPart(i),symbol);
  7221. END;
  7222. IF withStatement.elsePart # NIL THEN
  7223. StatementSequence(withStatement.elsePart)
  7224. END;
  7225. currentScope := prevScope;
  7226. END VisitWithStatement;
  7227. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7228. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7229. - check 'first' < 'last' and no overlaps between different case labels
  7230. - check statement sequence
  7231. **)
  7232. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7233. VAR
  7234. i: LONGINT;
  7235. position: Position;
  7236. expression, left, right: SyntaxTree.Expression;
  7237. expressionType: SyntaxTree.Type;
  7238. l, r: Basic.Integer;
  7239. cl, cr: CHAR;
  7240. thiscases: SyntaxTree.CaseConstant;
  7241. BEGIN
  7242. thiscases := NIL;
  7243. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7244. expression := casePart.elements.GetExpression(i);
  7245. position := expression.position;
  7246. (* set context of range *)
  7247. IF expression IS SyntaxTree.RangeExpression THEN
  7248. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7249. END;
  7250. expression := ResolveExpression(expression);
  7251. IF expression = SyntaxTree.invalidExpression THEN
  7252. (* error already reported *)
  7253. expressionType := SyntaxTree.invalidType;
  7254. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7255. (* read out 'first' and 'last' *)
  7256. left := expression(SyntaxTree.RangeExpression).first;
  7257. right := expression(SyntaxTree.RangeExpression).last;
  7258. (* guaranteed by VisitRangeExpression: *)
  7259. ASSERT((left # NIL) & (right # NIL));
  7260. ASSERT(left.type.resolved = right.type.resolved);
  7261. left := CompatibleConversion(left.position, left, type);
  7262. right := CompatibleConversion(right.position, right, type);
  7263. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7264. expression(SyntaxTree.RangeExpression).SetLast(right);
  7265. expressionType := RegularType(position,left.type);
  7266. ELSE
  7267. expression := ConstantExpression(expression);
  7268. expression := CompatibleConversion(expression.position, expression, type);
  7269. (*
  7270. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7271. left := Global.NewCharacterValue(system,expression.position,cl);
  7272. expression := casePart.elements.GetExpression(i);
  7273. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7274. expression := left
  7275. END;
  7276. *)
  7277. casePart.elements.SetExpression(i,expression);
  7278. left := expression; right := expression;
  7279. expressionType := RegularType(position,expression.type)
  7280. END;
  7281. IF (expressionType = SyntaxTree.invalidType) THEN
  7282. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7283. Error(position, "inadmissible case label");
  7284. expression := SyntaxTree.invalidExpression;
  7285. ELSE
  7286. l := 0; r := 0;
  7287. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7288. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7289. l := ORD(cl); r := ORD(cr);
  7290. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7291. ELSE
  7292. expression := SyntaxTree.invalidExpression
  7293. END;
  7294. IF expression # SyntaxTree.invalidExpression THEN
  7295. IF l>r THEN
  7296. Error(position, "empty case label")
  7297. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7298. Error(position, "duplicate case label");
  7299. ELSE
  7300. IF l < min THEN min := l END;
  7301. IF r > max THEN max := r END;
  7302. END;
  7303. END;
  7304. END;
  7305. casePart.elements.SetExpression(i,expression);
  7306. END;
  7307. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7308. casePart.SetConstants(thiscases);
  7309. StatementSequence(casePart.statements);
  7310. END CasePart;
  7311. (** check and resolve case statement CASE variable OF ... END;
  7312. - check variable
  7313. - check case parts
  7314. **)
  7315. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7316. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7317. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer; msg: ARRAY 64 OF CHAR;
  7318. BEGIN
  7319. expression := ResolveExpression(caseStatement.variable);
  7320. type := RegularType(expression.position,expression.type);
  7321. IF type = SyntaxTree.invalidType THEN
  7322. expression := SyntaxTree.invalidExpression;
  7323. ELSIF IsIntegerType(type) THEN
  7324. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7325. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7326. (*
  7327. expression := Global.NewCharacterValue(system,expression.position,ch);
  7328. *)
  7329. type := expression.type;
  7330. ELSIF IsCharacterType(type) THEN
  7331. ELSIF IsEnumerationType(type) THEN
  7332. ELSE
  7333. Error(caseStatement.variable.position,"variable must be integer or character type");
  7334. expression := SyntaxTree.invalidExpression;
  7335. END;
  7336. caseStatement.SetVariable(expression);
  7337. caseList := NIL;
  7338. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7339. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7340. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7341. END;
  7342. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7343. msg := "huge sparse case table ";
  7344. Strings.AppendInt(msg, max-min);
  7345. Strings.Append(msg,"/");
  7346. Strings.AppendInt(msg, caseStatement.CaseParts());
  7347. Warning(caseStatement.position,msg);
  7348. END;
  7349. caseStatement.SetMinMax(min,max);
  7350. StatementSequence(caseStatement.elsePart);
  7351. IF expression.resolved # NIL THEN
  7352. IF IsCharacterValue(expression,ch) THEN
  7353. l := ORD(ch)
  7354. ELSIF IsIntegerValue(expression,l) THEN
  7355. END;
  7356. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7357. END;
  7358. END VisitCaseStatement;
  7359. (** check and resolve while statement
  7360. - check condition
  7361. - check statement sequence
  7362. **)
  7363. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7364. VAR prevIsUnreachable,b: BOOLEAN;
  7365. BEGIN
  7366. prevIsUnreachable := currentIsUnreachable;
  7367. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7368. IF IsBooleanValue(whileStatement.condition,b) THEN
  7369. IF b=FALSE THEN
  7370. currentIsUnreachable := TRUE
  7371. END;
  7372. END;
  7373. StatementSequence(whileStatement.statements);
  7374. currentIsUnreachable := prevIsUnreachable
  7375. END VisitWhileStatement;
  7376. (** check and resolve repeat statement
  7377. - check condition
  7378. - check statement sequence
  7379. **)
  7380. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7381. BEGIN
  7382. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7383. StatementSequence(repeatStatement.statements);
  7384. END VisitRepeatStatement;
  7385. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7386. VAR withEntry: WithEntry;
  7387. BEGIN
  7388. withEntry := withEntries;
  7389. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7390. withEntry := withEntry.previous
  7391. END;
  7392. IF withEntry = NIL THEN RETURN FALSE
  7393. ELSE
  7394. type := withEntry.type;
  7395. RETURN TRUE
  7396. END;
  7397. END GetGuard;
  7398. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7399. - check that variable is an integer variable
  7400. - check that from is integer typed with compatible type
  7401. - check that to has compatible type
  7402. - check that by is constant integer with compatible type
  7403. **)
  7404. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7405. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7406. BEGIN
  7407. designator := ResolveDesignator(forStatement.variable);
  7408. type := SyntaxTree.invalidType;
  7409. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7410. designator := SyntaxTree.invalidDesignator;
  7411. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7412. Error(designator.position,"control variable of non-integer type");
  7413. designator := SyntaxTree.invalidDesignator;
  7414. ELSIF CheckVariable(designator) THEN
  7415. type := designator.type;
  7416. END;
  7417. forStatement.SetVariable(designator);
  7418. expression := ResolveExpression(forStatement.from);
  7419. IF expression = SyntaxTree.invalidExpression THEN
  7420. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7421. Error(expression.position,"start value of incompatible type");
  7422. expression := SyntaxTree.invalidExpression;
  7423. ELSIF type # SyntaxTree.invalidType THEN
  7424. expression := NewConversion(expression.position,expression,type,NIL)
  7425. END;
  7426. forStatement.SetFrom(expression);
  7427. expression := ResolveExpression(forStatement.to);
  7428. IF expression = SyntaxTree.invalidExpression THEN
  7429. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7430. Error(expression.position,"end value of incompatible type");
  7431. expression := SyntaxTree.invalidExpression;
  7432. ELSIF type # SyntaxTree.invalidType THEN
  7433. expression := NewConversion(expression.position,expression,type,NIL)
  7434. END;
  7435. forStatement.SetTo(expression);
  7436. IF forStatement.by # NIL THEN
  7437. expression := ConstantInteger(forStatement.by);
  7438. ELSE
  7439. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7440. END;
  7441. IF expression = SyntaxTree.invalidExpression THEN
  7442. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7443. Error(expression.position,"step value of incompatible type");
  7444. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7445. Error(expression.position,"invalid step value");
  7446. ELSIF type # SyntaxTree.invalidType THEN
  7447. expression := NewConversion(expression.position,expression,type,NIL)
  7448. END;
  7449. forStatement.SetBy(expression);
  7450. StatementSequence(forStatement.statements);
  7451. END VisitForStatement;
  7452. (** check and resolve loop statement LOOP StatementSequence END
  7453. - check statement sequence
  7454. **)
  7455. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7456. BEGIN
  7457. StatementSequence(loopStatement.statements)
  7458. END VisitLoopStatement;
  7459. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7460. BEGIN
  7461. StatementSequence(exitableBlock.statements);
  7462. END VisitExitableBlock;
  7463. (** check and resolve exit statement EXIT
  7464. - check that exit is within LOOP statement block
  7465. **)
  7466. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7467. VAR outer: SyntaxTree.Statement;
  7468. BEGIN
  7469. outer := exitStatement.outer;
  7470. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7471. outer := outer.outer;
  7472. END;
  7473. IF outer = NIL THEN
  7474. Error(exitStatement.position,"exit statement not within loop statement");
  7475. END;
  7476. END VisitExitStatement;
  7477. (** check and resolve return statement RETURN [expression]
  7478. - check expression (if any)
  7479. - check if in procedure scope
  7480. - if in procedure scope then check expression compatibility
  7481. - if not in procecdure scope then check on return without expression
  7482. **)
  7483. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7484. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7485. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7486. BEGIN
  7487. position := returnStatement.position;
  7488. expression := returnStatement.returnValue;
  7489. IF expression # NIL THEN
  7490. expression := ResolveExpression(expression);
  7491. returnStatement.SetReturnValue(expression);
  7492. END;
  7493. outer := returnStatement.outer;
  7494. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7495. outer := outer.outer
  7496. END;
  7497. IF (outer # NIL) THEN
  7498. scope := outer(SyntaxTree.Body).inScope;
  7499. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7500. IF (expression # NIL) THEN
  7501. Error(position, "return statement with parameter not in procedure scope");
  7502. END;
  7503. ELSE
  7504. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7505. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7506. Error(position, "return statement in procedure that does not return");
  7507. END;
  7508. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7509. IF returnType # NIL THEN
  7510. returnType := returnType.resolved;
  7511. IF expression = NIL THEN
  7512. Error(position, "empty return type in procedure providing a return type")
  7513. ELSIF expression.type = NIL THEN
  7514. Error(position,"returned type incompatible: expression has no type");
  7515. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7516. Error(position, "return type not compatible");
  7517. IF VerboseErrorMessage THEN
  7518. Printout.Info("returnType",returnType);
  7519. Printout.Info("expression",expression);
  7520. END;
  7521. ELSE
  7522. expression := NewConversion(expression.position,expression,returnType,NIL);
  7523. returnStatement.SetReturnValue(expression);
  7524. END;
  7525. ELSIF expression # NIL THEN
  7526. Error(position, "non-empty return type in procedure providing no return type");
  7527. END;
  7528. END;
  7529. END;
  7530. END VisitReturnStatement;
  7531. (** check and resolve await statement AWAIT(condition: Expression)
  7532. - check await condition
  7533. **)
  7534. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7535. VAR condition: SyntaxTree.Expression;
  7536. BEGIN
  7537. condition := ResolveCondition(awaitStatement.condition);
  7538. IF currentIsRealtime THEN
  7539. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7540. END;
  7541. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7542. Error(awaitStatement.position,"senseless await statement with constant condition");
  7543. END;
  7544. awaitStatement.SetCondition(condition);
  7545. END VisitAwaitStatement;
  7546. PROCEDURE CheckSystemImport(position: Position);
  7547. VAR import: SyntaxTree.Import;
  7548. BEGIN
  7549. import := currentScope.ownerModule.moduleScope.firstImport;
  7550. WHILE(import # NIL) DO
  7551. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7552. RETURN;
  7553. END;
  7554. import := import.nextImport;
  7555. END;
  7556. Error(position, "forbidden code without system import");
  7557. END CheckSystemImport;
  7558. (** check and resolve code statement: do nothing, must be done by assembler
  7559. **)
  7560. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7561. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7562. BEGIN
  7563. CheckSystemImport(code.position);
  7564. FOR i := 0 TO code.inRules.Length()-1 DO
  7565. statement := code.inRules.GetStatement(i);
  7566. IF statement IS SyntaxTree.Assignment THEN
  7567. WITH statement: SyntaxTree.Assignment DO
  7568. statement.SetRight(ResolveExpression(statement.right));
  7569. END;
  7570. ELSE
  7571. Error(statement.position, "can only be assignment")
  7572. END;
  7573. END;
  7574. FOR i := 0 TO code.outRules.Length()-1 DO
  7575. statement := code.outRules.GetStatement(i);
  7576. IF statement IS SyntaxTree.Assignment THEN
  7577. WITH statement: SyntaxTree.Assignment DO
  7578. statement.SetLeft(ResolveDesignator(statement.left));
  7579. END;
  7580. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7581. (* must be a reference to some register *)
  7582. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7583. ELSE
  7584. Printout.Info("out statement ", statement);
  7585. Error(statement.position, "(out) can only be assignment")
  7586. END;
  7587. END;
  7588. END VisitCode;
  7589. (** check and set flags of a statement block
  7590. - check for multiply occurence of a flag
  7591. - check and set priority only in bodies
  7592. - check for valid names
  7593. **)
  7594. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7595. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7596. flag: LONGINT; recordBody: SyntaxTree.Body;
  7597. PROCEDURE SetProtectedRecord;
  7598. VAR scope: SyntaxTree.Scope;
  7599. BEGIN
  7600. scope := currentScope;
  7601. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7602. scope := scope.outerScope
  7603. END;
  7604. IF scope # NIL THEN
  7605. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7606. END;
  7607. END SetProtectedRecord;
  7608. BEGIN
  7609. flags := {};
  7610. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7611. recordBody := block(SyntaxTree.Body)
  7612. ELSE
  7613. recordBody := NIL
  7614. END;
  7615. blockModifier := block.blockModifiers;
  7616. WHILE(blockModifier # NIL) DO
  7617. name := blockModifier.identifier;
  7618. expression := blockModifier.expression;
  7619. position := blockModifier.position;
  7620. flag := -1;
  7621. IF name=Global.NamePriority THEN
  7622. IF expression = NIL THEN
  7623. Error(position, "missing priority expression");
  7624. ELSIF recordBody = NIL THEN
  7625. Error(position, "priority not on record body");
  7626. ELSIF recordBody.priority # NIL THEN
  7627. Error(position, "duplicate priority expression");
  7628. ELSE
  7629. recordBody.SetPriority(expression);
  7630. END;
  7631. ELSIF expression # NIL THEN
  7632. Error(expression.position,"expression not in connection with priority")
  7633. ELSIF name=Global.NameExclusive THEN
  7634. IF block.isExclusive THEN
  7635. Error(position, "duplicate exclusive flag")
  7636. END;
  7637. block.SetExclusive(TRUE); SetProtectedRecord;
  7638. ELSIF name=Global.NameActive THEN
  7639. IF recordBody = NIL THEN
  7640. Error(position, "active not in record body");
  7641. ELSIF recordBody.isActive THEN
  7642. Error(position, "duplicate active flag")
  7643. ELSE
  7644. recordBody.SetActive(TRUE); SetProtectedRecord;
  7645. END;
  7646. ELSIF name=Global.NameSafe THEN
  7647. IF recordBody = NIL THEN
  7648. Error(position, "safe not in record body");
  7649. ELSIF recordBody.isSafe THEN
  7650. Error(position, "duplicate safe flag")
  7651. ELSE
  7652. recordBody.SetSafe(TRUE);
  7653. SetProtectedRecord;
  7654. END;
  7655. ELSIF name=Global.NameRealtime THEN
  7656. IF recordBody = NIL THEN
  7657. Error(position, "realtime not in record body");
  7658. ELSIF recordBody.isRealtime THEN
  7659. Error(position, "duplicate realtime flag")
  7660. ELSE
  7661. recordBody.SetRealtime(TRUE);
  7662. block.SetRealtime(TRUE);
  7663. END;
  7664. ELSIF name=Global.NameUnchecked THEN
  7665. IF block.isUnchecked THEN
  7666. Error(position, "duplicate unchecked flag")
  7667. ELSE
  7668. block.SetUnchecked(TRUE);
  7669. END;
  7670. ELSIF (name=Global.NameUncooperative) THEN
  7671. IF block.isUncooperative THEN
  7672. Error(position, "duplicate uncooperative flag")
  7673. ELSE
  7674. block.SetUncooperative(TRUE);
  7675. END;
  7676. ELSE
  7677. Error(position, "unknown block modifier");
  7678. END;
  7679. blockModifier := blockModifier.nextModifier;
  7680. END;
  7681. END BlockFlags;
  7682. (** check and resolve statement block
  7683. - check flags (exclusive)
  7684. - check statement sequence
  7685. **)
  7686. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7687. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7688. BEGIN
  7689. BlockFlags(statementBlock);
  7690. IF statementBlock.isExclusive THEN
  7691. (* check that not in exclusive block *)
  7692. IF currentIsExclusive THEN
  7693. Error (statementBlock.position,"forbidden recursive exclusive")
  7694. ELSIF currentIsRealtime THEN
  7695. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7696. END;
  7697. END;
  7698. recentExclusive := currentIsExclusive;
  7699. recentUnreachable := currentIsUnreachable;
  7700. recentRealtime := currentIsRealtime;
  7701. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7702. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7703. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7704. StatementSequence(statementBlock.statements);
  7705. currentIsRealtime := recentRealtime;
  7706. currentIsExclusive := recentExclusive;
  7707. currentIsUnreachable := recentUnreachable;
  7708. END VisitStatementBlock;
  7709. (** check and resolve body
  7710. - check flags (active, priority, safe)
  7711. - check body and finally part
  7712. **)
  7713. PROCEDURE Body(body: SyntaxTree.Body);
  7714. BEGIN
  7715. VisitStatementBlock(body);
  7716. IF body.isActive THEN
  7717. IF ~currentIsBodyProcedure THEN
  7718. Error(body.position,"active flag not in object body");
  7719. ELSIF body.priority # NIL THEN
  7720. body.SetPriority(ConstantInteger(body.priority));
  7721. END;
  7722. ELSIF body.isSafe THEN
  7723. Error(body.position,"safe flag not in active body");
  7724. ELSIF body.priority # NIL THEN
  7725. Error(body.position,"priority flag not in active body");
  7726. END;
  7727. IF body.code # NIL THEN
  7728. CheckSystemImport(body.position);
  7729. END;
  7730. StatementSequence(body.finally)
  7731. END Body;
  7732. (*** scopes ***)
  7733. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7734. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7735. VAR duplicateSymbol: BOOLEAN;
  7736. BEGIN
  7737. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7738. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7739. Error(symbol.position,"globally defined keyword")
  7740. END;
  7741. scope.EnterSymbol(symbol,duplicateSymbol);
  7742. IF ~allowDuplicate & duplicateSymbol THEN
  7743. Error(symbol.position,"Multiply defined identifier.");
  7744. IF VerboseErrorMessage THEN
  7745. Printout.Info("multiply defined identifier",symbol);
  7746. Printout.Info("in scope",scope);
  7747. END;
  7748. END;
  7749. END Register;
  7750. (**
  7751. implementation: check and resolve an implementation part
  7752. **)
  7753. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7754. move implementation checker to a separate object ? *)
  7755. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7756. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7757. BEGIN
  7758. prevIsRealtime := currentIsRealtime;
  7759. prevIsBodyProcedure := currentIsBodyProcedure;
  7760. prevIsCellNet := currentIsCellNet;
  7761. prevScope := currentScope;
  7762. currentScope := scope;
  7763. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7764. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7765. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7766. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7767. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7768. (*
  7769. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7770. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7771. END;
  7772. *)
  7773. END;
  7774. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7775. (* module body, record bodies are wrapped into an artifical procedure *)
  7776. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7777. Body(scope(SyntaxTree.ProcedureScope).body)
  7778. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7779. Body(scope(SyntaxTree.ProcedureScope).body)
  7780. END;
  7781. END;
  7782. currentScope := prevScope;
  7783. currentIsRealtime := prevIsRealtime;
  7784. currentIsBodyProcedure := prevIsBodyProcedure;
  7785. currentIsCellNet := prevIsCellNet;
  7786. END Implementation;
  7787. (** implementation phase:
  7788. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7789. **)
  7790. PROCEDURE Implementations(x: SyntaxTree.Module);
  7791. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7792. BEGIN
  7793. prevPhase := phase;
  7794. phase := InlinePhase;
  7795. scope := x.firstScope;
  7796. WHILE(scope # NIL) DO
  7797. Implementation(scope);
  7798. scope := scope.nextScope;
  7799. END;
  7800. phase := ImplementationPhase;
  7801. scope := x.firstScope;
  7802. WHILE(scope # NIL) DO
  7803. Implementation(scope);
  7804. scope := scope.nextScope;
  7805. END;
  7806. phase := prevPhase;
  7807. END Implementations;
  7808. (** declaration phase:
  7809. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7810. - import lists (for module scopes)
  7811. - parameter list (for procedure scopes)
  7812. - constant declarations
  7813. - type declarations
  7814. - variable declarations
  7815. - procedure declarations
  7816. preformed in two stages:
  7817. - first all symbols are entered into the symbol table (with uniqueness check),
  7818. - then all symbols are resolved
  7819. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7820. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7821. phases :
  7822. 0 = before procedures
  7823. 1 = procedures and later
  7824. **)
  7825. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7826. VAR
  7827. constant: SyntaxTree.Constant;
  7828. typeDeclaration: SyntaxTree.TypeDeclaration;
  7829. declaredType: SyntaxTree.Type;
  7830. variable: SyntaxTree.Variable;
  7831. procedure: SyntaxTree.Procedure;
  7832. procedureType : SyntaxTree.ProcedureType;
  7833. prevScope: SyntaxTree.Scope;
  7834. parameter: SyntaxTree.Parameter;
  7835. import: SyntaxTree.Import;
  7836. symbol: SyntaxTree.Symbol;
  7837. prevPhase: LONGINT;
  7838. prevError : BOOLEAN;
  7839. i: LONGINT;
  7840. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7841. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7842. BEGIN
  7843. IF type.baseType # NIL THEN
  7844. baseType := type.baseType.resolved;
  7845. IF baseType IS SyntaxTree.PointerType THEN
  7846. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7847. END;
  7848. (*
  7849. IF baseType IS SyntaxTree.CellType THEN
  7850. DeclareCell(baseType(SyntaxTree.CellType));
  7851. END;
  7852. *)
  7853. END;
  7854. parameter := type.firstParameter;
  7855. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7856. (*
  7857. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7858. variable.SetType(parameter.type);
  7859. variable.SetAccess(SyntaxTree.Hidden);
  7860. variable.SetModifiers(parameter.modifiers);
  7861. currentScope.PushVariable(variable);
  7862. *)
  7863. Register(parameter,scope, FALSE);
  7864. parameter := parameter.nextParameter;
  7865. END;
  7866. property := type.firstProperty;
  7867. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7868. (*
  7869. variable := currentScope.FindVariable(property.name);
  7870. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7871. prop := variable(SyntaxTree.Property);
  7872. ELSE (* add, duplicate symbols detection later *)
  7873. prop := SyntaxTree.NewProperty(property.position, property.name);
  7874. currentScope.PushVariable(prop);
  7875. END;
  7876. prop.SetType(property.type);
  7877. prop.SetValue(property.value);
  7878. prop.SetAccess(SyntaxTree.Hidden);
  7879. *)
  7880. Register(property, scope, FALSE);
  7881. property := property.nextProperty;
  7882. END;
  7883. END DeclareCell;
  7884. BEGIN
  7885. prevError := error;
  7886. prevPhase := phase;
  7887. phase := DeclarationPhase;
  7888. prevScope := currentScope;
  7889. currentScope := scope;
  7890. error := FALSE;
  7891. IF 0 IN phases THEN
  7892. (* first enter all symbols in scope *)
  7893. IF scope IS SyntaxTree.ModuleScope THEN
  7894. (* treat imports first for a module scope, , set default context if necessary *)
  7895. import := scope(SyntaxTree.ModuleScope).firstImport;
  7896. WHILE(import # NIL) DO
  7897. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7898. Register(import, currentScope, FALSE);
  7899. import := import.nextImport;
  7900. END;
  7901. import := scope(SyntaxTree.ModuleScope).firstImport;
  7902. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7903. ResolveSymbol(import);
  7904. import := import.nextImport;
  7905. END;
  7906. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7907. (* enter parameters for a procedure scope *)
  7908. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7909. parameter := procedureType.firstParameter;
  7910. WHILE(parameter # NIL) DO
  7911. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7912. END;
  7913. parameter := procedureType.returnParameter;
  7914. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7915. parameter := procedureType.selfParameter;
  7916. IF parameter # NIL THEN
  7917. Register(parameter, currentScope, FALSE);
  7918. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7919. END;
  7920. ELSIF scope IS SyntaxTree.CellScope THEN
  7921. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7922. IF~skipImplementation THEN
  7923. import := scope(SyntaxTree.CellScope).firstImport;
  7924. WHILE(import # NIL) DO
  7925. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7926. Register(import, currentScope, FALSE);
  7927. import := import.nextImport;
  7928. END;
  7929. import := scope(SyntaxTree.CellScope).firstImport;
  7930. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7931. ResolveSymbol(import);
  7932. import := import.nextImport;
  7933. END;
  7934. END;
  7935. END;
  7936. IF error THEN RETURN END;
  7937. IF skipImplementation THEN
  7938. scope.Clear;
  7939. END;
  7940. (* constants *)
  7941. constant := scope.firstConstant;
  7942. WHILE (constant # NIL) DO
  7943. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7944. END;
  7945. (* type declarations *)
  7946. typeDeclaration := scope.firstTypeDeclaration;
  7947. WHILE (typeDeclaration # NIL) DO
  7948. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7949. END;
  7950. (* variables *)
  7951. variable := scope.firstVariable;
  7952. WHILE (variable # NIL) DO
  7953. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7954. END;
  7955. (* procedures *)
  7956. IF scope.procedures # NIL THEN
  7957. FOR i := 0 TO scope.procedures.Length()-1 DO
  7958. procedure := scope.procedures.GetProcedure(i);
  7959. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7960. IF procedureType.selfParameter = NIL THEN
  7961. scope.AddProcedure(procedure);
  7962. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7963. ELSE
  7964. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7965. IF typeDeclaration = NIL THEN
  7966. Error(procedureType.selfParameter.position, "No such type declaration");
  7967. ELSE
  7968. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7969. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7970. declaredType := typeDeclaration.declaredType;
  7971. IF declaredType IS SyntaxTree.PointerType THEN
  7972. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7973. END;
  7974. IF declaredType IS SyntaxTree.RecordType THEN
  7975. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7976. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7977. ELSE
  7978. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7979. END;
  7980. END;
  7981. END;
  7982. END;
  7983. END;
  7984. END;
  7985. (* now process all symbols without any presumption on the order *)
  7986. symbol := scope.firstSymbol;
  7987. WHILE(symbol # NIL) DO
  7988. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7989. IF (symbol IS SyntaxTree.Procedure) THEN
  7990. IF 1 IN phases THEN
  7991. ResolveSymbol(symbol);
  7992. END;
  7993. ELSE
  7994. IF 0 IN phases THEN
  7995. ResolveSymbol(symbol);
  7996. END;
  7997. END;
  7998. END;
  7999. symbol := symbol.nextSymbol;
  8000. END;
  8001. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  8002. symbol := scope.firstSymbol;
  8003. WHILE symbol # NIL DO
  8004. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  8005. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  8006. pointerFixes.Add(symbol, currentScope);
  8007. END;
  8008. IF ~symbol.type.resolved.isRealtime THEN
  8009. Error(symbol.position,"symbol has no realtime type");
  8010. END;
  8011. END;
  8012. symbol := symbol.nextSymbol
  8013. END;
  8014. END;
  8015. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  8016. Error(Basic.invalidPosition,"problems during offset computation in module");
  8017. END;
  8018. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  8019. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  8020. scope.ownerModule.AddScope(scope);
  8021. END;
  8022. phase := prevPhase;
  8023. currentScope := prevScope;
  8024. error := error OR prevError;
  8025. END Declarations;
  8026. (* nopov *)
  8027. (** check if all operators from one module are compatible to the ones in the other module
  8028. - check if there are not multiple operators with the same signature
  8029. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8030. - check for all operators whose signatures are compatible, whether the return types are compatible
  8031. note that:
  8032. - the return type is not considered to be part of the signature
  8033. - two signatures are considered compatible, if all of the operands are compatible
  8034. **)
  8035. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8036. VAR
  8037. thisOperator, thatOperator: SyntaxTree.Operator;
  8038. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8039. thisParameter, thatParameter: SyntaxTree.Parameter;
  8040. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8041. i: LONGINT;
  8042. BEGIN
  8043. currentScope := thisModuleScope;
  8044. hasError := FALSE;
  8045. (* go through all operators in the other module *)
  8046. thatOperator := thatModuleScope.firstOperator;
  8047. WHILE (thatOperator # NIL) & ~hasError DO
  8048. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8049. (* the other operator is accessible *)
  8050. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8051. (* the other operator is not the conversion operator *)
  8052. (* go through all operators in this module *)
  8053. thisOperator := thisModuleScope.firstOperator;
  8054. WHILE (thisOperator # NIL) & ~hasError DO
  8055. IF thisOperator # thatOperator THEN
  8056. (* the operators are not the same *)
  8057. IF thisOperator.name = thatOperator.name THEN
  8058. (* the operators share the same identifier *)
  8059. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8060. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8061. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8062. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8063. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8064. (* both operators have the same paramter count *)
  8065. thisParameter := thisProcedureType.firstParameter;
  8066. thatParameter := thatProcedureType.firstParameter;
  8067. operandsAreEqual := TRUE;
  8068. operandsAreCompatible := TRUE;
  8069. (* go through all parameters *)
  8070. FOR i := 1 TO thisProcedureType.numberParameters DO
  8071. ASSERT(thatParameter # NIL);
  8072. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8073. operandsAreEqual := FALSE;
  8074. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  8075. operandsAreCompatible := FALSE
  8076. END
  8077. END;
  8078. thisParameter := thisParameter.nextParameter;
  8079. thatParameter := thatParameter.nextParameter
  8080. END;
  8081. IF operandsAreEqual THEN
  8082. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8083. hasError := TRUE
  8084. ELSIF operandsAreCompatible THEN
  8085. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8086. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8087. hasError := TRUE
  8088. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8089. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8090. hasError := TRUE
  8091. END
  8092. END
  8093. END
  8094. END
  8095. END;
  8096. thisOperator := thisOperator.nextOperator
  8097. END
  8098. END
  8099. END;
  8100. thatOperator := thatOperator.nextOperator
  8101. END
  8102. END CheckInterOperatorConformity;
  8103. (** check module:
  8104. - check module declaration
  8105. - add context, if necessary
  8106. - remove module from import cache, if necessary
  8107. - check declarations
  8108. - resolve all type fixes
  8109. - check implementation (bodies)
  8110. **)
  8111. PROCEDURE Module*(x: SyntaxTree.Module);
  8112. VAR (* nopov *)
  8113. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8114. BEGIN
  8115. prevScope := currentScope;
  8116. prevIsCellNet := currentIsCellNet;
  8117. module := x;
  8118. ASSERT(x # NIL);
  8119. global := system.globalScope[x.case];
  8120. x.moduleScope.SetGlobalScope(global);
  8121. currentScope := global;
  8122. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8123. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8124. RemoveModuleFromCache(importCache,x);
  8125. Declarations(x.moduleScope, FALSE, {0,1});
  8126. FixTypes();
  8127. IF module.isCellNet THEN
  8128. currentIsCellNet := TRUE;
  8129. modifier := x.modifiers;
  8130. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8131. CheckModifiers(modifier, FALSE);
  8132. END;
  8133. (* nopov *)
  8134. IF ~error THEN
  8135. (* check if operators conform to each other within this module *)
  8136. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8137. (* go through all imports *)
  8138. import := x.moduleScope.firstImport;
  8139. WHILE import # NIL DO
  8140. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8141. (* check if all operators in this module conform to the ones of the imported module *)
  8142. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8143. END;
  8144. import := import.nextImport
  8145. END;
  8146. END;
  8147. Implementations(x);
  8148. module := NIL;
  8149. currentIsCellNet := prevIsCellNet;
  8150. currentScope := prevScope;
  8151. END Module;
  8152. END Checker;
  8153. Warnings*=OBJECT (SyntaxTree.Visitor)
  8154. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8155. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8156. BEGIN
  8157. SELF.diagnostics := diagnostics
  8158. END InitWarnings;
  8159. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8160. BEGIN END VisitPortType;
  8161. (** types *)
  8162. PROCEDURE Type(x: SyntaxTree.Type);
  8163. BEGIN x.Accept(SELF)
  8164. END Type;
  8165. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8166. BEGIN END VisitType;
  8167. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8168. BEGIN END VisitBasicType;
  8169. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8170. BEGIN END VisitCharacterType;
  8171. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8172. BEGIN END VisitIntegerType;
  8173. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8174. BEGIN END VisitFloatType;
  8175. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8176. BEGIN END VisitQualifiedType;
  8177. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8178. BEGIN END VisitStringType;
  8179. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8180. BEGIN END VisitEnumerationType;
  8181. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8182. BEGIN END VisitRangeType;
  8183. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8184. BEGIN
  8185. IF ~(SyntaxTree.Warned IN x.state) THEN
  8186. x.SetState(SyntaxTree.Warned);
  8187. Type(x.arrayBase);
  8188. END;
  8189. END VisitArrayType;
  8190. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8191. BEGIN
  8192. IF ~(SyntaxTree.Warned IN x.state) THEN
  8193. x.SetState(SyntaxTree.Warned);
  8194. Type(x.arrayBase);
  8195. END;
  8196. END VisitMathArrayType;
  8197. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8198. BEGIN
  8199. IF ~(SyntaxTree.Warned IN x.state) THEN
  8200. x.SetState(SyntaxTree.Warned);
  8201. Type(x.pointerBase);
  8202. END;
  8203. END VisitPointerType;
  8204. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8205. BEGIN Scope(x.recordScope) END VisitRecordType;
  8206. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8207. BEGIN Scope(x.cellScope) END VisitCellType;
  8208. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8209. BEGIN END VisitProcedureType;
  8210. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8211. VAR msg: ARRAY 256 OF CHAR;
  8212. BEGIN
  8213. Global.GetSymbolName(x,msg);
  8214. Strings.Append(msg," ");
  8215. Strings.Append(msg,text);
  8216. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8217. END Warning;
  8218. (** symbols *)
  8219. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8220. BEGIN
  8221. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8222. IF ~(x IS SyntaxTree.Parameter) THEN
  8223. Warning(x,"never used");
  8224. END;
  8225. END;
  8226. x.Accept(SELF);
  8227. END Symbol;
  8228. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8229. BEGIN END VisitSymbol;
  8230. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8231. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8232. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8233. BEGIN END VisitConstant;
  8234. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8235. BEGIN END VisitVariable;
  8236. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8237. BEGIN END VisitProperty;
  8238. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8239. BEGIN END VisitParameter;
  8240. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8241. BEGIN
  8242. Scope(x.procedureScope)
  8243. END VisitProcedure;
  8244. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8245. BEGIN END VisitOperator;
  8246. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8247. BEGIN END VisitImport;
  8248. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8249. VAR
  8250. symbol: SyntaxTree.Symbol;
  8251. BEGIN
  8252. symbol := scope.firstSymbol;
  8253. WHILE(symbol # NIL) DO
  8254. Symbol(symbol);
  8255. symbol := symbol.nextSymbol;
  8256. END;
  8257. END Scope;
  8258. PROCEDURE Module*(x: SyntaxTree.Module);
  8259. BEGIN
  8260. SELF.module := x;
  8261. Scope(x.moduleScope);
  8262. END Module;
  8263. END Warnings;
  8264. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8265. BEGIN
  8266. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8267. END IsOberonInline;
  8268. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8269. BEGIN
  8270. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8271. END Resolved;
  8272. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8273. VAR i: LONGINT;
  8274. BEGIN
  8275. i := 1;
  8276. WHILE i < x DO
  8277. i := i *2
  8278. END;
  8279. RETURN i=x
  8280. END PowerOf2;
  8281. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8282. BEGIN
  8283. RETURN
  8284. (scope # NIL) &
  8285. (scope IS SyntaxTree.ModuleScope)
  8286. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8287. OR
  8288. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8289. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8290. END IsCellNetScope;
  8291. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8292. BEGIN
  8293. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8294. END IsCellScope;
  8295. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8296. BEGIN
  8297. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8298. RETURN (scope # NIL) & IsCellNetScope(scope)
  8299. END InCellNetScope;
  8300. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8301. BEGIN
  8302. ASSERT(size MOD system.dataUnit = 0);
  8303. RETURN size DIV system.dataUnit
  8304. END ToMemoryUnits;
  8305. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8306. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8307. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8308. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8309. BEGIN
  8310. IF t = NIL THEN
  8311. RETURN TRUE
  8312. ELSE
  8313. t := t.resolved;
  8314. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8315. END;
  8316. END TypeAllowed;
  8317. BEGIN
  8318. type := type.resolved;
  8319. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8320. RETURN FALSE
  8321. ELSE
  8322. procedureType := type(SyntaxTree.ProcedureType);
  8323. numberParameters := procedureType.numberParameters;
  8324. RETURN
  8325. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8326. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8327. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8328. END;
  8329. END GetProcedureAllowed;
  8330. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8331. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8332. VAR import: SyntaxTree.Import;
  8333. BEGIN
  8334. import := importCache.ImportByModuleName(x.name,x.context);
  8335. IF import # NIL THEN
  8336. importCache.RemoveImporters(x.name,x.context);
  8337. END;
  8338. END RemoveModuleFromCache;
  8339. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8340. (* to <- this assignment compatibility *)
  8341. VAR result: BOOLEAN;
  8342. BEGIN
  8343. IF this= NIL THEN result := (to=NIL)
  8344. ELSIF to=NIL THEN result := FALSE
  8345. ELSE
  8346. (*! will be replaced by this:
  8347. ELSE result := this.CompatibleTo(to.resolved);
  8348. *)
  8349. this := this.resolved; to := to.resolved;
  8350. IF to=SyntaxTree.invalidType THEN result := FALSE
  8351. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8352. ELSIF (to = this) OR (to.SameType(this)) THEN
  8353. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8354. ELSIF to IS SyntaxTree.BasicType THEN
  8355. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8356. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8357. result := this.CompatibleTo(to.resolved)
  8358. ELSE
  8359. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8360. END
  8361. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8362. result := to.sizeInBits >= this.sizeInBits;
  8363. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8364. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8365. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8366. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8367. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8368. result := TRUE;
  8369. ELSIF to IS SyntaxTree.AnyType THEN
  8370. 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);
  8371. ELSIF to IS SyntaxTree.ObjectType THEN
  8372. 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 *) ;
  8373. ELSIF to IS SyntaxTree.ByteType THEN
  8374. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8375. ELSIF to IS SyntaxTree.CharacterType THEN
  8376. result := IsCharacterType(this)
  8377. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8378. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8379. 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)
  8380. OR IsTensor(this)
  8381. ) THEN
  8382. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8383. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8384. result := TRUE;
  8385. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8386. result := TRUE;
  8387. ELSE
  8388. result := FALSE
  8389. END;
  8390. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  8391. result := TRUE;
  8392. ELSIF to IS SyntaxTree.PointerType THEN
  8393. result := (this IS SyntaxTree.NilType) OR
  8394. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  8395. (IsPointerType(this) & IsTypeExtension(to,this) OR
  8396. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  8397. ELSIF to IS SyntaxTree.ProcedureType THEN
  8398. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8399. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8400. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8401. ELSIF to IS SyntaxTree.RecordType THEN
  8402. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8403. ELSIF to IS SyntaxTree.ArrayType THEN
  8404. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8405. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8406. ELSIF StaticArrayCompatible(to, this) THEN
  8407. result := TRUE
  8408. ELSE
  8409. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8410. END;
  8411. ELSIF to IS SyntaxTree.MathArrayType THEN
  8412. IF this IS SyntaxTree.MathArrayType THEN
  8413. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8414. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8415. result := TRUE;
  8416. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8417. result := TRUE;
  8418. ELSE
  8419. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8420. END;
  8421. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8422. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8423. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8424. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8425. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8426. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8427. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8428. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8429. ELSE
  8430. result := FALSE
  8431. END;
  8432. (* an array-structured object type is compatible to the type of its array structure *)
  8433. ELSIF IsArrayStructuredObjectType(this) THEN
  8434. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8435. ELSE
  8436. result := FALSE;
  8437. END;
  8438. ELSIF to IS SyntaxTree.StringType THEN
  8439. result := FALSE;
  8440. ELSIF to IS SyntaxTree.EnumerationType THEN
  8441. result := IsEnumerationExtension(this,to);
  8442. ELSIF to IS SyntaxTree.PortType THEN
  8443. result := SameType(to, this)
  8444. ELSE
  8445. Printout.Info("CompatibleTo",to);
  8446. HALT(100); (* implement missing type check *)
  8447. END;
  8448. END;
  8449. RETURN result
  8450. END CompatibleTo;
  8451. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8452. VAR actualBase, formalBase: SyntaxTree.Type;
  8453. BEGIN
  8454. IF SameType(formal,actual) THEN
  8455. RETURN TRUE
  8456. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8457. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8458. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8459. RETURN
  8460. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8461. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8462. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8463. & StaticArrayCompatible(formalBase,actualBase)
  8464. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8465. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8466. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8467. RETURN
  8468. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8469. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8470. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8471. & StaticArrayCompatible(formalBase,actualBase)
  8472. ELSE RETURN FALSE
  8473. END;
  8474. END StaticArrayCompatible;
  8475. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8476. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8477. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8478. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8479. BEGIN
  8480. result := SameType(formal,actual);
  8481. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8482. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8483. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8484. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8485. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8486. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8487. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8488. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8489. & TC(formalBase, actualBase);
  8490. END;
  8491. RETURN result
  8492. END TC;
  8493. BEGIN
  8494. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8495. ELSE
  8496. arrayBase := formalType.arrayBase.resolved;
  8497. IF (actualType IS SyntaxTree.StringType) THEN
  8498. result := arrayBase IS SyntaxTree.CharacterType
  8499. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8500. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8501. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8502. result := TC(formalType, actualType);
  8503. ELSE
  8504. result := (arrayBase IS SyntaxTree.ByteType)
  8505. END;
  8506. END;
  8507. RETURN result
  8508. END OpenArrayCompatible;
  8509. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8510. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8511. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8512. BEGIN
  8513. IF actualType IS SyntaxTree.MathArrayType THEN
  8514. actualArray := actualType(SyntaxTree.MathArrayType);
  8515. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8516. (*
  8517. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8518. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8519. *)
  8520. actualBase := ArrayBase(actualType,Infinity);
  8521. formalBase := ArrayBase(formalType,Infinity);
  8522. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8523. ELSE
  8524. (*
  8525. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8526. *)
  8527. formalBase := Resolved(formalType.arrayBase);
  8528. actualBase := Resolved(actualArray.arrayBase);
  8529. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8530. (*
  8531. ARRAY [k] -> ARRAY [n]
  8532. *)
  8533. result := (formalType.staticLength = actualArray.staticLength)
  8534. ELSE
  8535. result := TRUE
  8536. END;
  8537. IF ~result THEN
  8538. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8539. ELSIF actualBase = NIL THEN result := FALSE
  8540. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8541. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8542. ELSE
  8543. result := SameType(formalBase,actualBase)
  8544. END;
  8545. END;
  8546. ELSE
  8547. result := FALSE
  8548. END;
  8549. RETURN result
  8550. END MathArrayCompatible;
  8551. (**
  8552. Math Array Type distance for assignments / parameter passings of the form
  8553. from -> to
  8554. variants:
  8555. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8556. allowed:
  8557. static -> static (& size match)
  8558. static -> open
  8559. static -> tensor
  8560. open -> open
  8561. open -> tensor
  8562. open -> static
  8563. tensor -> tensor
  8564. tensor -> open
  8565. tensor -> static
  8566. **)
  8567. (*! think about the metric here: is form matching more important than element type matching? *)
  8568. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8569. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8570. BEGIN
  8571. fromBase := Resolved(from.arrayBase);
  8572. toBase := Resolved(to.arrayBase);
  8573. i := Infinity;
  8574. IF (from = to) OR (from.SameType(to)) THEN
  8575. i := 0;
  8576. ELSIF (from.form = to.form) THEN
  8577. (* static -> static, open -> open, tensor -> tensor *)
  8578. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8579. IF fromBase = toBase THEN i := 0
  8580. ELSIF toBase = NIL THEN i := 1
  8581. ELSIF fromBase = NIL THEN i := Infinity;
  8582. ELSIF toBase.SameType(fromBase) THEN i := 0
  8583. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8584. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8585. ELSE
  8586. i := TypeDistance(system,fromBase, toBase, varpar);
  8587. IF i < Infinity THEN i := i * 5 END;
  8588. END;
  8589. END;
  8590. ELSIF (to.form = SyntaxTree.Static) THEN
  8591. (* forbidden *)
  8592. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8593. (* static -> tensor, open -> tensor, tensor -> open *)
  8594. IF (toBase=fromBase) THEN i := 0;
  8595. ELSIF toBase = NIL THEN i := 1;
  8596. ELSIF toBase.SameType(fromBase) THEN i := 0
  8597. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8598. toBase := ArrayBase(toBase,Infinity);
  8599. IF (fromBase=toBase) THEN i := 0
  8600. ELSIF (toBase = NIL) THEN i:= 1
  8601. ELSIF (fromBase = NIL) THEN i := Infinity;
  8602. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8603. IF i < Infinity THEN i := i * 5 END;
  8604. END;
  8605. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8606. fromBase := ArrayBase(fromBase,Infinity);
  8607. IF (fromBase=toBase) THEN i := 0
  8608. ELSIF (toBase = NIL) THEN i := 1
  8609. ELSIF (fromBase = NIL) THEN i := Infinity;
  8610. ELSIF toBase.SameType(fromBase) THEN i := 0
  8611. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8612. IF i < Infinity THEN i := i * 5 END;
  8613. END;
  8614. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8615. IF i < Infinity THEN i := i * 5 END;
  8616. END;
  8617. IF i # Infinity THEN INC(i,2) END;
  8618. ELSIF (from.form = SyntaxTree.Static) THEN
  8619. (* static -> open *)
  8620. IF (toBase=fromBase) THEN i := 0;
  8621. ELSIF toBase = NIL THEN i := 1
  8622. ELSIF fromBase = NIL THEN i := Infinity
  8623. ELSIF toBase.SameType(fromBase) THEN i := 0
  8624. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8625. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8626. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8627. IF i < Infinity THEN i := i * 5 END;
  8628. END;
  8629. IF i # Infinity THEN INC(i,1) END;
  8630. ELSE HALT(100); (* unknown case *)
  8631. END;
  8632. RETURN i;
  8633. END MathArrayTypeDistance;
  8634. (** compute and return the distance of two array types
  8635. - return the distance of the base types
  8636. **)
  8637. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8638. VAR i: LONGINT;
  8639. BEGIN
  8640. i := Infinity;
  8641. IF from = to THEN
  8642. i := 0
  8643. ELSE
  8644. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8645. (*
  8646. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8647. i := TypeDistance(from.base, to.base);
  8648. IF i >= 0 THEN INC(i) END
  8649. ELSIF (from.mode = open) & (to.mode = open) THEN
  8650. i := TypeDistance(from.base, to.base);
  8651. *)
  8652. END;
  8653. RETURN i
  8654. END ArrayTypeDistance;
  8655. (** compute the signature distance of a procedure and an actual parameter list
  8656. - if any of the parameters are not compatible, the result is infinite
  8657. - add up and return the distance over all parameters
  8658. **)
  8659. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8660. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8661. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8662. BEGIN
  8663. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8664. result := Infinity
  8665. ELSE
  8666. formalParameter := procedureType.firstParameter;
  8667. i := 0;
  8668. result := 0;
  8669. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8670. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8671. actualParameter := actualParameters.GetExpression(i);
  8672. ASSERT(formalParameter.type # NIL);
  8673. IF (actualParameter.type = NIL) THEN distance := Infinity
  8674. ELSE
  8675. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8676. END;
  8677. IF distance = Infinity THEN
  8678. result := Infinity;
  8679. ELSE
  8680. to := formalParameter.type.resolved;
  8681. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8682. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8683. (* already handled varpar *)
  8684. (*
  8685. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8686. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8687. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8688. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8689. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8690. END;
  8691. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8692. result := Infinity
  8693. END;
  8694. *)
  8695. INC(result, distance);
  8696. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8697. INC(result, distance);
  8698. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8699. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8700. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8701. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8702. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8703. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8704. END;
  8705. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8706. result := Infinity
  8707. END;
  8708. ELSE
  8709. result := Infinity
  8710. END;
  8711. ELSE
  8712. INC(result,distance);
  8713. END;
  8714. END;
  8715. (*
  8716. Printout.Info("actual=", actualParameter);
  8717. Printout.Info("formal=", formalParameter);
  8718. TRACE(result);
  8719. *)
  8720. formalParameter := formalParameter.nextParameter; INC(i);
  8721. END;
  8722. END;
  8723. ASSERT(result >= 0);
  8724. RETURN result
  8725. END Distance;
  8726. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8727. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8728. BEGIN
  8729. IF right.numberParameters # (procedureType.numberParameters) THEN
  8730. result := Infinity
  8731. ELSE
  8732. formalParameter := procedureType.firstParameter;
  8733. rightParameter := right.firstParameter;
  8734. i := 0;
  8735. result := 0;
  8736. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8737. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8738. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8739. IF distance = Infinity THEN
  8740. result := Infinity;
  8741. ELSE
  8742. INC(result,distance);
  8743. END;
  8744. formalParameter := formalParameter.nextParameter;
  8745. rightParameter := rightParameter.nextParameter;
  8746. END;
  8747. END;
  8748. ASSERT(result >= 0);
  8749. RETURN result
  8750. END ProcedureTypeDistance;
  8751. (** compute and return the distance between two types, used for computation of signature distance
  8752. from -> to
  8753. **)
  8754. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8755. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8756. BEGIN
  8757. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8758. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8759. END;
  8760. i := Infinity;
  8761. IF from = to THEN
  8762. i := 0
  8763. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8764. ELSIF to.SameType(from) THEN
  8765. i := 0;
  8766. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8767. i := Infinity;
  8768. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8769. i := 10;
  8770. ELSIF (from IS SyntaxTree.StringType) THEN
  8771. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8772. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8773. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8774. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8775. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8776. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8777. i := 1
  8778. ELSIF (from IS SyntaxTree.NilType) THEN
  8779. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8780. (*
  8781. ELSIF (from = NoType) THEN
  8782. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8783. *)
  8784. ELSIF (from IS SyntaxTree.BasicType) THEN
  8785. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8786. IF varpar & (i # 0) THEN i := Infinity END;
  8787. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8788. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8789. ELSIF (from IS SyntaxTree.RecordType) THEN
  8790. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8791. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8792. IF to IS SyntaxTree.MathArrayType THEN
  8793. (*
  8794. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8795. i := Infinity;
  8796. ELSE
  8797. *)
  8798. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8799. (*
  8800. END;
  8801. *)
  8802. END
  8803. ELSIF (from IS SyntaxTree.PointerType) THEN
  8804. ptr := from(SyntaxTree.PointerType);
  8805. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8806. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8807. (* ELSE i := TypeDistance(ptr.base, to); *)
  8808. END
  8809. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8810. IF (to IS SyntaxTree.ProcedureType) THEN
  8811. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8812. END;
  8813. ELSIF (from IS SyntaxTree.PortType) THEN
  8814. IF (to IS SyntaxTree.PortType) THEN
  8815. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8816. i := 0;
  8817. END;
  8818. END;
  8819. (*no procedure test, procedure must be the same*)
  8820. END;
  8821. RETURN i
  8822. END TypeDistance;
  8823. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8824. BEGIN
  8825. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8826. END IsIntegerType;
  8827. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8828. BEGIN
  8829. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8830. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8831. OR IsPointerType(type)
  8832. )
  8833. END IsAddressType;
  8834. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8835. BEGIN
  8836. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  8837. END IsSizeType;
  8838. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8839. BEGIN
  8840. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8841. END IsSignedIntegerType;
  8842. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8843. BEGIN
  8844. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8845. END IsUnsignedIntegerType;
  8846. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8847. VAR result: BOOLEAN;
  8848. BEGIN
  8849. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8850. value := x.resolved(SyntaxTree.IntegerValue).value;
  8851. result := TRUE
  8852. ELSE
  8853. result := FALSE
  8854. END;
  8855. RETURN result
  8856. END IsIntegerValue;
  8857. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8858. VAR result: BOOLEAN;
  8859. BEGIN
  8860. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8861. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8862. result := TRUE
  8863. ELSE
  8864. result := FALSE
  8865. END;
  8866. RETURN result
  8867. END IsEnumerationValue;
  8868. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8869. VAR result: BOOLEAN;
  8870. BEGIN
  8871. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8872. value := x.resolved(SyntaxTree.RealValue).value;
  8873. result := TRUE
  8874. ELSE
  8875. result := FALSE
  8876. END;
  8877. RETURN result
  8878. END IsRealValue;
  8879. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8880. VAR result: BOOLEAN;
  8881. BEGIN
  8882. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8883. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8884. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8885. result := TRUE
  8886. ELSE
  8887. result := FALSE
  8888. END;
  8889. RETURN result
  8890. END IsComplexValue;
  8891. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8892. VAR result: BOOLEAN;
  8893. BEGIN
  8894. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8895. value := x.resolved(SyntaxTree.CharacterValue).value;
  8896. result := TRUE
  8897. ELSE
  8898. result := FALSE
  8899. END;
  8900. RETURN result
  8901. END IsCharacterValue;
  8902. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8903. VAR result: BOOLEAN;
  8904. BEGIN
  8905. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8906. value := x.resolved(SyntaxTree.BooleanValue).value;
  8907. result := TRUE
  8908. ELSE
  8909. result := FALSE
  8910. END;
  8911. RETURN result
  8912. END IsBooleanValue;
  8913. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8914. VAR result: BOOLEAN;
  8915. BEGIN
  8916. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8917. value := x.resolved(SyntaxTree.SetValue).value;
  8918. result := TRUE
  8919. ELSE
  8920. result := FALSE
  8921. END;
  8922. RETURN result
  8923. END IsSetValue;
  8924. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8925. VAR result: BOOLEAN;
  8926. BEGIN
  8927. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8928. value := x.resolved(SyntaxTree.StringValue).value;
  8929. result := TRUE
  8930. ELSE
  8931. result := FALSE
  8932. END;
  8933. RETURN result
  8934. END IsStringValue;
  8935. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8936. BEGIN
  8937. x := x.resolved;
  8938. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8939. END Indexable;
  8940. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8941. BEGIN
  8942. RETURN t1.SameType(t2.resolved);
  8943. END SameType;
  8944. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8945. BEGIN
  8946. IF t IS SyntaxTree.MathArrayType THEN
  8947. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8948. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8949. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8950. DEC(max);
  8951. END;
  8952. ELSIF t IS SyntaxTree.ArrayType THEN
  8953. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8954. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8955. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8956. END;
  8957. END;
  8958. RETURN t;
  8959. END ArrayBase;
  8960. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8961. BEGIN
  8962. type := type.resolved;
  8963. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8964. base := type(SyntaxTree.ArrayType).arrayBase;
  8965. RETURN TRUE;
  8966. END;
  8967. RETURN FALSE;
  8968. END IsOpenArray;
  8969. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8970. BEGIN
  8971. type := type.resolved;
  8972. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8973. base := type(SyntaxTree.ArrayType).arrayBase;
  8974. dim := type(SyntaxTree.ArrayType).staticLength;
  8975. RETURN TRUE
  8976. ELSE
  8977. RETURN FALSE
  8978. END;
  8979. END IsStaticArray;
  8980. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8981. BEGIN
  8982. type := type.resolved;
  8983. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8984. base := type(SyntaxTree.ArrayType).arrayBase;
  8985. RETURN TRUE
  8986. ELSE
  8987. RETURN FALSE
  8988. END;
  8989. END IsDynamicArray;
  8990. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8991. VAR i: LONGINT;
  8992. BEGIN
  8993. i := 0;
  8994. t := t.resolved;
  8995. IF t IS SyntaxTree.MathArrayType THEN
  8996. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8997. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8998. END;
  8999. ELSIF t IS SyntaxTree.ArrayType THEN
  9000. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  9001. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  9002. END;
  9003. END;
  9004. RETURN i
  9005. END Dimension;
  9006. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  9007. BEGIN
  9008. RETURN expression.assignable;
  9009. END IsVariable;
  9010. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9011. BEGIN
  9012. IF (symbol IS SyntaxTree.Parameter) THEN
  9013. WITH symbol: SyntaxTree.Parameter DO
  9014. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  9015. END;
  9016. ELSE
  9017. RETURN FALSE
  9018. END;
  9019. END IsVariableParameter;
  9020. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  9021. VAR result: BOOLEAN;
  9022. BEGIN
  9023. IF type = NIL THEN result := FALSE
  9024. ELSE
  9025. type := type.resolved;
  9026. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  9027. END;
  9028. RETURN result
  9029. END IsPointerType;
  9030. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  9031. VAR result: BOOLEAN;
  9032. BEGIN
  9033. IF type = NIL THEN result := FALSE
  9034. ELSE
  9035. type := type.resolved;
  9036. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  9037. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9038. & type(SyntaxTree.MathArrayType).isUnsafe;
  9039. END;
  9040. RETURN result
  9041. END IsUnsafePointer;
  9042. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9043. BEGIN
  9044. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9045. END IsDisposable;
  9046. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9047. VAR result: BOOLEAN;
  9048. BEGIN
  9049. IF type = NIL THEN result := FALSE
  9050. ELSE
  9051. type := type.resolved;
  9052. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9053. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9054. result := result OR (type IS SyntaxTree.ObjectType);
  9055. END;
  9056. RETURN result
  9057. END IsPointerToRecord;
  9058. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9059. VAR result: BOOLEAN;
  9060. BEGIN
  9061. IF type = NIL THEN result := FALSE
  9062. ELSE
  9063. type := type.resolved;
  9064. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9065. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9066. ;
  9067. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9068. result := result OR (type IS SyntaxTree.ObjectType);
  9069. END;
  9070. RETURN result
  9071. END IsPointerToObject;
  9072. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9073. BEGIN
  9074. IF type # NIL THEN
  9075. RETURN type.resolved.hasPointers
  9076. ELSE
  9077. RETURN FALSE
  9078. END;
  9079. END ContainsPointer;
  9080. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9081. BEGIN
  9082. IF type = NIL THEN RETURN FALSE END;
  9083. type := type.resolved;
  9084. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9085. END IsStringType;
  9086. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9087. BEGIN
  9088. IF type = NIL THEN RETURN FALSE END;
  9089. type := type.resolved;
  9090. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9091. END IsCharacterType;
  9092. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9093. BEGIN
  9094. IF type = NIL THEN RETURN FALSE END;
  9095. type := type.resolved;
  9096. RETURN (type IS SyntaxTree.EnumerationType)
  9097. END IsEnumerationType;
  9098. (** cf. section "Type extension (base type)" in the language report **)
  9099. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9100. VAR result: BOOLEAN;
  9101. BEGIN
  9102. ASSERT(base # NIL); ASSERT(extension # NIL);
  9103. base := base.resolved; extension := extension.resolved;
  9104. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9105. result := TRUE;
  9106. ELSE
  9107. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9108. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9109. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9110. END;
  9111. WHILE (extension # NIL) & (extension # base) DO
  9112. IF extension IS SyntaxTree.RecordType THEN
  9113. extension := extension(SyntaxTree.RecordType).baseType;
  9114. IF (extension # NIL) THEN extension := extension.resolved END;
  9115. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9116. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9117. END;
  9118. ELSE extension := NIL;
  9119. END;
  9120. END;
  9121. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9122. END;
  9123. RETURN result
  9124. END IsTypeExtension;
  9125. (** check if base is the base enumeration type of extension **)
  9126. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9127. BEGIN
  9128. base := base.resolved; extension := extension.resolved;
  9129. WHILE (extension # NIL) & (extension # base) DO
  9130. IF extension IS SyntaxTree.EnumerationType THEN
  9131. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9132. IF extension # NIL THEN extension := extension.resolved END;
  9133. ELSE
  9134. extension := NIL
  9135. END;
  9136. END;
  9137. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9138. END IsEnumerationExtension;
  9139. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9140. BEGIN
  9141. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9142. RETURN TRUE
  9143. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9144. RETURN TRUE
  9145. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9146. RETURN TRUE
  9147. ELSE
  9148. RETURN FALSE
  9149. END
  9150. END IsCallable;
  9151. (** compute and return the distance of two record types
  9152. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9153. **)
  9154. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9155. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9156. BEGIN
  9157. i := 0;
  9158. WHILE (from # NIL) & (from # to) DO
  9159. baseType := from.baseType;
  9160. IF (baseType # NIL) THEN
  9161. baseType := baseType.resolved;
  9162. IF baseType IS SyntaxTree.PointerType THEN
  9163. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9164. END;
  9165. IF baseType IS SyntaxTree.RecordType THEN
  9166. from := baseType(SyntaxTree.RecordType);
  9167. ELSE
  9168. from := NIL;
  9169. END;
  9170. ELSE
  9171. from := NIL
  9172. END;
  9173. INC(i)
  9174. END;
  9175. IF from = NIL THEN i := Infinity END;
  9176. RETURN i
  9177. END RecordTypeDistance;
  9178. (** compute and return the distance of two pointer types **)
  9179. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9180. BEGIN
  9181. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9182. RETURN Infinity;
  9183. ELSE
  9184. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9185. END;
  9186. END PointerTypeDistance;
  9187. (** check if expression contains a symbol designator pointing to a type declaration.
  9188. - if so then enter type declaration into typeDeclaration and return true else return false
  9189. **)
  9190. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9191. VAR result: BOOLEAN;
  9192. BEGIN
  9193. result := FALSE;
  9194. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9195. result := TRUE;
  9196. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9197. END;
  9198. RETURN result
  9199. END IsTypeDesignator;
  9200. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9201. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9202. VAR result: BOOLEAN;
  9203. BEGIN
  9204. type := type.resolved;
  9205. IF type IS SyntaxTree.PointerType THEN
  9206. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9207. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9208. result := TRUE
  9209. ELSE
  9210. result := type IS SyntaxTree.RecordType
  9211. END;
  9212. RETURN result
  9213. END IsExtensibleType;
  9214. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9215. BEGIN
  9216. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9217. (d IS SyntaxTree.SymbolDesignator) &
  9218. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9219. OR
  9220. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9221. END IsUnextensibleRecord;
  9222. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9223. BEGIN
  9224. IF IsUnextensibleRecord(d) THEN
  9225. RETURN FALSE
  9226. ELSE RETURN IsExtensibleType(d.type.resolved)
  9227. END;
  9228. END IsExtensibleDesignator;
  9229. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9230. BEGIN
  9231. type := type.resolved;
  9232. IF (type IS SyntaxTree.PointerType) THEN
  9233. RETURN TRUE
  9234. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9235. RETURN TRUE
  9236. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9237. RETURN TRUE
  9238. ELSIF (type IS SyntaxTree.BasicType) THEN
  9239. RETURN TRUE
  9240. END;
  9241. RETURN FALSE
  9242. END IsBasicType;
  9243. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9244. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9245. BEGIN
  9246. baseType := record.baseType;
  9247. IF (baseType # NIL) THEN
  9248. baseType := baseType.resolved;
  9249. IF (baseType IS SyntaxTree.PointerType) THEN
  9250. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9251. END;
  9252. END;
  9253. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9254. recordType := baseType(SyntaxTree.RecordType);
  9255. ELSE
  9256. recordType := NIL;
  9257. END;
  9258. RETURN recordType
  9259. END RecordBase;
  9260. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9261. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9262. BEGIN
  9263. baseRecord := RecordBase(scope.ownerRecord);
  9264. IF baseRecord = NIL THEN RETURN NIL END;
  9265. scope := baseRecord.recordScope;
  9266. procedureType := procedure.type.resolved;
  9267. IF procedure IS SyntaxTree.Operator THEN
  9268. operator := scope.firstOperator;
  9269. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9270. (*
  9271. Printout.Info("not same ",procedureType);
  9272. Printout.Info("with ",operator.type);
  9273. *)
  9274. operator := operator.nextOperator;
  9275. END;
  9276. super := operator;
  9277. ELSE
  9278. super := scope.firstProcedure;
  9279. WHILE (super # NIL) & (super.name # procedure.name) DO
  9280. super := super.nextProcedure;
  9281. END;
  9282. END;
  9283. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9284. RETURN super
  9285. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9286. RETURN super
  9287. ELSE
  9288. RETURN FindSuperProcedure(scope,procedure);
  9289. END;
  9290. END FindSuperProcedure;
  9291. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9292. VAR procedure: SyntaxTree.Procedure;
  9293. BEGIN
  9294. procedure := record.recordScope.constructor;
  9295. IF procedure = NIL THEN
  9296. record := RecordBase(record);
  9297. IF record # NIL THEN
  9298. procedure := GetConstructor(record)
  9299. END;
  9300. END;
  9301. RETURN procedure;
  9302. END GetConstructor;
  9303. (* enter a case into a list of cases in a sorted way and check for collision *)
  9304. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9305. VAR prev,this,new: SyntaxTree.CaseConstant;
  9306. BEGIN
  9307. this := root;
  9308. prev := NIL;
  9309. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9310. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9311. RETURN FALSE
  9312. ELSE
  9313. IF (this # NIL) & (this.min = max+1) THEN
  9314. this.min := min
  9315. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9316. prev.max := min
  9317. ELSE
  9318. NEW(new); new.min := min; new.max := max;
  9319. new.next := this;
  9320. IF prev = NIL THEN
  9321. root := new;
  9322. ELSE
  9323. prev.next := new
  9324. END
  9325. END;
  9326. RETURN TRUE
  9327. END;
  9328. END EnterCase;
  9329. (** generate and return a new checker object, errors are entered into diagnostics **)
  9330. 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;
  9331. VAR checker: Checker;
  9332. BEGIN
  9333. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9334. RETURN checker
  9335. END NewChecker;
  9336. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9337. VAR warnings: Warnings;
  9338. BEGIN
  9339. NEW(warnings, diagnostics); RETURN warnings;
  9340. END NewWarnings;
  9341. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9342. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9343. END IsRangeType;
  9344. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9345. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9346. END IsMathArrayType;
  9347. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9348. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9349. END IsArrayType;
  9350. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9351. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9352. END IsComplexType;
  9353. (** if a type is an array-structured object type *)
  9354. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9355. VAR recordType: SyntaxTree.RecordType;
  9356. BEGIN
  9357. IF type = NIL THEN
  9358. RETURN FALSE
  9359. ELSE
  9360. type := type.resolved;
  9361. IF type IS SyntaxTree.PointerType THEN
  9362. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9363. IF type IS SyntaxTree.RecordType THEN
  9364. recordType := type(SyntaxTree.RecordType);
  9365. RETURN recordType.isObject & recordType.HasArrayStructure()
  9366. ELSE
  9367. RETURN FALSE
  9368. END
  9369. ELSE
  9370. RETURN FALSE
  9371. END
  9372. END
  9373. END IsArrayStructuredObjectType;
  9374. (** the math array structure of a type
  9375. - for math arrays: the array itself
  9376. - for pointers: the math array structure of the pointer base
  9377. - for array-structured object types: the underlying structure
  9378. - for non-math arrays and all other types: NIL
  9379. **)
  9380. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9381. VAR
  9382. result: SyntaxTree.MathArrayType;
  9383. BEGIN
  9384. IF type = NIL THEN
  9385. result := NIL
  9386. ELSE
  9387. type := type.resolved;
  9388. IF type IS SyntaxTree.PointerType THEN
  9389. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9390. END;
  9391. IF type IS SyntaxTree.MathArrayType THEN
  9392. result := type(SyntaxTree.MathArrayType)
  9393. ELSIF type IS SyntaxTree.RecordType THEN
  9394. result := type(SyntaxTree.RecordType).arrayStructure
  9395. ELSE
  9396. result := NIL
  9397. END
  9398. END;
  9399. RETURN result
  9400. END MathArrayStructureOfType;
  9401. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9402. VAR
  9403. result: BOOLEAN;
  9404. rangeExpression: SyntaxTree.RangeExpression;
  9405. BEGIN
  9406. IF x IS SyntaxTree.RangeExpression THEN
  9407. rangeExpression := x(SyntaxTree.RangeExpression);
  9408. result := TRUE;
  9409. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9410. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9411. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9412. ELSE
  9413. result := FALSE
  9414. END;
  9415. RETURN result
  9416. END IsStaticRange;
  9417. (** whether a type is a math array of tensor form **)
  9418. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9419. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9420. END IsTensor;
  9421. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9422. BEGIN
  9423. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9424. length := type(SyntaxTree.MathArrayType).staticLength;
  9425. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9426. RETURN TRUE
  9427. ELSE
  9428. RETURN FALSE
  9429. END;
  9430. END IsStaticMathArray;
  9431. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9432. BEGIN
  9433. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9434. END SymbolHasAddress;
  9435. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9436. BEGIN
  9437. RETURN
  9438. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9439. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9440. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9441. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9442. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9443. ;
  9444. END HasAddress;
  9445. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9446. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9447. BEGIN
  9448. IF (e IS SyntaxTree.Designator) THEN
  9449. d := e(SyntaxTree.Designator);
  9450. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9451. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9452. e := d.left;
  9453. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9454. END;
  9455. IF d # NIL THEN
  9456. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9457. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9458. END;
  9459. END;
  9460. RETURN FALSE;
  9461. END IsLocalVariable;
  9462. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9463. BEGIN
  9464. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9465. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9466. ELSE
  9467. RETURN TRUE
  9468. END;
  9469. END IsStaticProcedure;
  9470. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9471. CONST OptimizeMethodTable = FALSE;
  9472. BEGIN
  9473. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9474. END InMethodTable;
  9475. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9476. BEGIN
  9477. IF type = NIL THEN RETURN FALSE
  9478. ELSE
  9479. type := type.resolved;
  9480. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9481. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9482. END
  9483. END ReturnedAsParameter;
  9484. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9485. BEGIN
  9486. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9487. END StructuredReturnType;
  9488. END FoxSemanticChecker.
  9489. System.FreeDownTo FoxSemanticChecker ~