FoxSemanticChecker.Mod 407 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185
  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. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  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. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. x.SetArrayBase(pointerType);
  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, Diagnostics.Invalid, "forbidden variable access");
  490. END;
  491. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. x.SetState(SyntaxTree.Resolved);
  572. END;
  573. resolvedType := ResolvedType(x);
  574. END VisitMathArrayType;
  575. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  576. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  577. (1) Rec = RECORD ... END; Ptr <---> Rec
  578. Ptr = POINTER TO Rec; ^ |
  579. | |
  580. TypeDesc TypeDesc
  581. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  582. ^ /
  583. | /
  584. TypeDesc <-- /
  585. *)
  586. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  587. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  588. BEGIN
  589. Strings.IntToStr(x.position,number);
  590. COPY(prefix,name);
  591. Strings.Append(name,"@");
  592. Strings.Append(name,number);
  593. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  594. typeDeclaration.SetDeclaredType(x);
  595. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  596. x.SetTypeDeclaration(typeDeclaration);
  597. currentScope.AddTypeDeclaration(typeDeclaration);
  598. typeDeclaration.SetScope(currentScope);
  599. END AnonymousTypeDeclaration;
  600. (**
  601. deferred pointer type resolving
  602. - resolve base type
  603. - check that base type is a record or array type
  604. - if error then set base type to invalid type
  605. **)
  606. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  607. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  608. BEGIN
  609. ASSERT(type.pointerBase # NIL);
  610. position := type.pointerBase.position;
  611. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  612. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  613. (* not for pointers, a type is needed for the records only
  614. IF type.typeDeclaration = NIL THEN
  615. AnonymousTypeDeclaration(type);
  616. END;
  617. *)
  618. END;
  619. resolved := ResolveType(type.pointerBase);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  621. type.SetPointerBase(resolved);
  622. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  623. recordType := resolved.resolved(SyntaxTree.RecordType);
  624. IF recordType.isObject & (recordType.baseType # NIL) THEN
  625. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  626. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  627. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  628. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  629. END;
  630. END;
  631. END;
  632. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  633. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  634. END;
  635. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  636. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  638. ELS
  639. *)
  640. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  642. END;
  643. END;
  644. ELSE
  645. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  646. type.SetPointerBase(SyntaxTree.invalidType)
  647. END
  648. END FixPointerType;
  649. (**
  650. resolve pointer type
  651. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  652. **)
  653. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  654. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  655. modifiers: SyntaxTree.Modifier; position: LONGINT;
  656. BEGIN
  657. IF TypeNeedsResolution(x) THEN
  658. modifiers := x.modifiers;
  659. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  660. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  661. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  662. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  663. (* inheritance cycle check
  664. example:
  665. A=POINTER TO RECORD(B) END;
  666. B=POINTER TO RECORD(A) END;
  667. *)
  668. IF x.pointerBase IS SyntaxTree.RecordType THEN
  669. recordType := x.pointerBase(SyntaxTree.RecordType);
  670. recordBaseType := ResolveType(recordType.baseType);
  671. recordType.SetBaseType(recordBaseType);
  672. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  673. END;
  674. CheckModifiers(modifiers, TRUE);
  675. typeFixes.Add(x,currentScope);
  676. x.SetState(SyntaxTree.Resolved);
  677. END;
  678. resolvedType := ResolvedType(x)
  679. END VisitPointerType;
  680. (**
  681. resolve port type
  682. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  683. **)
  684. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  685. VAR value: LONGINT;
  686. BEGIN
  687. IF TypeNeedsResolution(x) THEN
  688. x.SetCellsAreObjects(cellsAreObjects);
  689. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  690. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  691. x.SetSize(value)
  692. ELSE
  693. x.SetSize(system.SizeOf(system.longintType));
  694. END;
  695. x.SetState(SyntaxTree.Resolved);
  696. END;
  697. resolvedType := ResolvedType(x)
  698. END VisitPortType;
  699. (**
  700. deferred procedure type resolving
  701. - resolve return type
  702. - traverse and resolve parameters
  703. **)
  704. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  705. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  706. BEGIN
  707. resolved := ResolveType(procedureType.returnType);
  708. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  709. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  710. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  711. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  712. END;
  713. procedureType.SetReturnType(resolved);
  714. IF (resolved # NIL) THEN
  715. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  716. parameter.SetType(procedureType.returnType);
  717. parameter.SetAccess(SyntaxTree.Hidden);
  718. parameter.SetUntraced(procedureType.hasUntracedReturn);
  719. VisitParameter(parameter);
  720. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  721. END;
  722. (* process parameters *)
  723. parameter :=procedureType.firstParameter;
  724. WHILE (parameter # NIL) DO
  725. VisitParameter(parameter);
  726. parameter := parameter.nextParameter;
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,Diagnostics.Invalid,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,Diagnostics.Invalid,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF (backendName = "TRM") & x.isCellNet THEN D.TraceBack; RETURN TRUE END;
  792. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  793. IF svalue[0] = "~" THEN
  794. Strings.TrimLeft(svalue, "~");
  795. IF svalue = backendName THEN
  796. RETURN TRUE;
  797. END;
  798. ELSIF svalue # backendName THEN
  799. RETURN TRUE;
  800. END;
  801. END;
  802. IF x.baseType # NIL THEN
  803. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  804. END;
  805. RETURN FALSE;
  806. END SkipImplementation;
  807. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  808. VAR this: SyntaxTree.Modifier;
  809. BEGIN
  810. this := modifiers;
  811. WHILE this # NIL DO
  812. IF ~this.resolved THEN
  813. IF checkUse THEN
  814. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  815. ELSE
  816. this.SetExpression(ResolveExpression(this.expression));
  817. this.Resolved;
  818. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  819. END;
  820. END;
  821. this := this.nextModifier
  822. END;
  823. END CheckModifiers;
  824. (**
  825. resolve procedure type
  826. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  827. **)
  828. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  829. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  830. BEGIN
  831. IF TypeNeedsResolution(procedureType) THEN
  832. modifiers := procedureType.modifiers;
  833. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  834. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  835. procedureType.SetInterrupt(TRUE);
  836. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  837. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  838. IF useDarwinCCalls THEN (*fld*)
  839. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  840. ELSE
  841. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  842. END
  843. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  844. procedureType.SetNoReturn(TRUE);
  845. END;
  846. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  847. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  848. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  849. CheckModifiers(modifiers, TRUE);
  850. modifiers := procedureType.returnTypeModifiers;
  851. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  852. CheckModifiers(modifiers, TRUE);
  853. typeFixes.Add(procedureType,currentScope);
  854. procedureType.SetHasPointers(procedureType.isDelegate);
  855. procedureType.SetState(SyntaxTree.Resolved);
  856. END;
  857. resolvedType := ResolvedType(procedureType)
  858. END VisitProcedureType;
  859. (** check and resolve record type
  860. - check base type: must be record, math array or array-structured object type
  861. - check declarations
  862. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  863. **)
  864. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  865. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  866. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  867. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  868. hasPointers: BOOLEAN;
  869. modifiers: SyntaxTree.Modifier;
  870. value: LONGINT;
  871. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  872. BEGIN
  873. type := type.resolved;
  874. IF (type IS SyntaxTree.PointerType) &
  875. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  876. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  877. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  878. RETURN TRUE
  879. ELSE
  880. RETURN FALSE
  881. END;
  882. END IsPointerToRecord;
  883. BEGIN
  884. IF TypeNeedsResolution(x) THEN
  885. hasPointers := FALSE;
  886. modifiers := x.modifiers;
  887. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  888. CheckModifiers(modifiers, TRUE);
  889. IF x.baseType # NIL THEN
  890. position := x.baseType.position;
  891. baseType := ResolveType(x.baseType);
  892. resolved := baseType.resolved;
  893. hasPointers := hasPointers OR resolved.hasPointers;
  894. IF x.isObject THEN (* object *)
  895. ASSERT(x.pointerType # NIL);
  896. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  897. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  898. baseType := NIL
  899. ELSIF IsPointerToRecord(resolved,recordType) THEN
  900. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  901. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  902. ELSE
  903. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  904. END;
  905. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  906. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  907. ELSIF IsPointerToRecord(resolved,recordType) THEN
  908. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  909. ELSIF resolved IS SyntaxTree.RecordType THEN
  910. ELSE
  911. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  912. END;
  913. ELSE
  914. IF resolved IS SyntaxTree.RecordType THEN
  915. ELSE
  916. Error(position, Diagnostics.Invalid,"record does not extend record")
  917. END;
  918. END;
  919. x.SetBaseType(baseType);
  920. IF x.Level() > 15 THEN
  921. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  922. (* note:
  923. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  924. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  925. inheritance history of a type.
  926. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  927. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  928. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  929. *)
  930. END;
  931. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  932. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  933. END;
  934. END;
  935. Declarations(x.recordScope);
  936. ResolveArrayStructure(x);
  937. (* computation of sizes and offsets skipped -> done in backend / system *)
  938. recordBase := x.GetBaseRecord();
  939. IF recordBase = NIL THEN numberMethods := 0
  940. ELSE numberMethods := recordBase.recordScope.numberMethods
  941. END;
  942. isRealtime := TRUE;
  943. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  944. WHILE symbol # NIL DO
  945. IF symbol IS SyntaxTree.Variable THEN
  946. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  947. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  948. END;
  949. IF symbol IS SyntaxTree.Procedure THEN
  950. procedure := symbol(SyntaxTree.Procedure);
  951. IF procedure.super # NIL THEN
  952. procedure.SetMethodNumber(procedure.super.methodNumber)
  953. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  954. procedure.SetMethodNumber(numberMethods);
  955. INC(numberMethods);
  956. END;
  957. END;
  958. symbol := symbol.nextSymbol;
  959. END;
  960. IF isRealtime THEN x.SetRealtime(TRUE) END;
  961. x.recordScope.SetNumberMethods(numberMethods);
  962. (* TODO: is this needed anymore? *)
  963. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  964. Error(x.position,Diagnostics.Invalid,"object extends a record")
  965. END;
  966. IF (x.typeDeclaration = NIL) THEN
  967. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  968. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  969. (*
  970. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  971. AnonymousTypeDeclaration(x,name);
  972. *)
  973. ELSE
  974. AnonymousTypeDeclaration(x,"Anonymous");
  975. END;
  976. END;
  977. x.SetHasPointers(hasPointers);
  978. x.SetState(SyntaxTree.Resolved);
  979. END;
  980. resolvedType := ResolvedType(x);
  981. END VisitRecordType;
  982. (** declaration phase:
  983. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  984. - import lists (for module scopes)
  985. - parameter list (for procedure scopes)
  986. - constant declarations
  987. - type declarations
  988. - variable declarations
  989. - procedure declarations
  990. preformed in two stages:
  991. - first all symbols are entered into the symbol table (with uniqueness check),
  992. - then all symbols are resolved
  993. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  994. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  995. **)
  996. PROCEDURE Declarations2(scope: SyntaxTree.Scope);
  997. VAR
  998. constant: SyntaxTree.Constant;
  999. typeDeclaration: SyntaxTree.TypeDeclaration;
  1000. variable: SyntaxTree.Variable;
  1001. procedure: SyntaxTree.Procedure;
  1002. prevScope: SyntaxTree.Scope;
  1003. parameter: SyntaxTree.Parameter;
  1004. import: SyntaxTree.Import;
  1005. symbol: SyntaxTree.Symbol;
  1006. prevPhase: LONGINT;
  1007. prevError : BOOLEAN;
  1008. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  1009. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  1010. BEGIN
  1011. IF type.baseType # NIL THEN
  1012. baseType := type.baseType.resolved;
  1013. IF baseType IS SyntaxTree.PointerType THEN
  1014. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  1015. END;
  1016. IF baseType IS SyntaxTree.CellType THEN
  1017. DeclareCell(baseType(SyntaxTree.CellType));
  1018. END;
  1019. END;
  1020. parameter := type.firstParameter;
  1021. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  1022. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  1023. variable.SetType(parameter.type);
  1024. variable.SetAccess(SyntaxTree.Hidden);
  1025. variable.SetModifiers(parameter.modifiers);
  1026. currentScope.PushVariable(variable);
  1027. (*
  1028. Register(parameter,scope, FALSE);
  1029. *)
  1030. parameter := parameter.nextParameter;
  1031. END;
  1032. property := type.firstProperty;
  1033. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  1034. variable := currentScope.FindVariable(property.name);
  1035. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  1036. prop := variable(SyntaxTree.Property);
  1037. ELSE (* add, duplicate symbols detection later *)
  1038. prop := SyntaxTree.NewProperty(property.position, property.name);
  1039. currentScope.PushVariable(prop);
  1040. END;
  1041. prop.SetType(property.type);
  1042. prop.SetValue(property.value);
  1043. prop.SetAccess(SyntaxTree.Hidden);
  1044. property := property.nextProperty;
  1045. END;
  1046. END DeclareCell;
  1047. BEGIN
  1048. prevError := error;
  1049. prevPhase := phase;
  1050. phase := DeclarationPhase;
  1051. prevScope := currentScope;
  1052. currentScope := scope;
  1053. error := FALSE;
  1054. IF scope IS SyntaxTree.CellScope THEN
  1055. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  1056. END;
  1057. IF error THEN RETURN END;
  1058. (* constants *)
  1059. constant := scope.firstConstant;
  1060. WHILE (constant # NIL) DO
  1061. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  1062. END;
  1063. (* type declarations *)
  1064. typeDeclaration := scope.firstTypeDeclaration;
  1065. WHILE (typeDeclaration # NIL) DO
  1066. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  1067. END;
  1068. (* variables *)
  1069. variable := scope.firstVariable;
  1070. WHILE (variable # NIL) DO
  1071. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  1072. END;
  1073. (* procedures *)
  1074. procedure := scope.firstProcedure;
  1075. WHILE (procedure # NIL) DO
  1076. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  1077. END;
  1078. (*
  1079. (* now process all symbols without any presumption on the order *)
  1080. symbol := scope.firstSymbol;
  1081. WHILE(symbol # NIL) DO
  1082. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  1083. ResolveSymbol(symbol);
  1084. END;
  1085. symbol := symbol.nextSymbol;
  1086. END;
  1087. *)
  1088. (*
  1089. IF scope.ownerModule # NIL THEN
  1090. IF ~(scope IS SyntaxTree.CellScope) OR ~SkipImplementation(scope(SyntaxTree.CellScope).ownerCell) THEN
  1091. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  1092. scope.ownerModule.AddScope(scope);
  1093. END;
  1094. END;
  1095. *)
  1096. phase := prevPhase;
  1097. currentScope := prevScope;
  1098. error := error OR prevError;
  1099. END Declarations2;
  1100. (** check and resolve cell type
  1101. - check base type: must be cell
  1102. - check declarations
  1103. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1104. **)
  1105. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1106. VAR
  1107. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1108. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1109. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1110. recordBase: SyntaxTree.RecordType;
  1111. numberMethods, int: LONGINT;
  1112. real: LONGREAL;
  1113. bool: BOOLEAN;
  1114. set: SET;
  1115. v: SyntaxTree.Expression;
  1116. str: Scanner.StringType;
  1117. atype: SyntaxTree.ArrayType;
  1118. prev: SyntaxTree.Scope;
  1119. skip: BOOLEAN;
  1120. svalue: ARRAY 32 OF CHAR;
  1121. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  1122. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  1123. BEGIN
  1124. IF type.baseType # NIL THEN
  1125. baseType := type.baseType.resolved;
  1126. IF baseType IS SyntaxTree.PointerType THEN
  1127. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  1128. END;
  1129. IF baseType IS SyntaxTree.CellType THEN
  1130. DeclareCell(baseType(SyntaxTree.CellType));
  1131. END;
  1132. END;
  1133. parameter := type.firstParameter;
  1134. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  1135. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  1136. variable.SetType(parameter.type);
  1137. variable.SetAccess(SyntaxTree.Hidden);
  1138. variable.SetModifiers(parameter.modifiers);
  1139. currentScope.PushVariable(variable);
  1140. (*
  1141. Register(parameter,scope, FALSE);
  1142. *)
  1143. parameter := parameter.nextParameter;
  1144. END;
  1145. (*
  1146. property := type.firstProperty;
  1147. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  1148. variable := currentScope.FindVariable(property.name);
  1149. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  1150. prop := variable(SyntaxTree.Property);
  1151. ELSE (* add, duplicate symbols detection later *)
  1152. prop := SyntaxTree.NewProperty(property.position, property.name);
  1153. currentScope.PushVariable(prop);
  1154. END;
  1155. prop.SetType(property.type);
  1156. prop.SetValue(property.value);
  1157. prop.SetAccess(SyntaxTree.Hidden);
  1158. (*Register(prop,currentScope,FALSE);*)
  1159. property := property.nextProperty;
  1160. END;
  1161. *)
  1162. variable := currentScope.firstVariable;
  1163. WHILE (variable # NIL) DO
  1164. D.Str0(variable.name);D.Ln;
  1165. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  1166. END;
  1167. (* now process all symbols without any presumption on the order *)
  1168. symbol := currentScope.firstSymbol;
  1169. WHILE(symbol # NIL) DO
  1170. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  1171. ResolveSymbol(symbol);
  1172. END;
  1173. symbol := symbol.nextSymbol;
  1174. END;
  1175. END DeclareCell;
  1176. BEGIN
  1177. IF TypeNeedsResolution(x) THEN
  1178. recordBase := NIL;
  1179. IF cellsAreObjects THEN
  1180. IF x.baseType = NIL THEN
  1181. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1182. ImportModule(qualifiedIdentifier.prefix, x.position);
  1183. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1184. x.SetBaseType(ResolveType(x.baseType));
  1185. recordBase := x.GetBaseRecord();
  1186. IF recordBase = NIL THEN
  1187. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1188. END;
  1189. ELSE
  1190. x.SetBaseType(ResolveType(x.baseType));
  1191. END;
  1192. ELSE
  1193. x.SetBaseType(ResolveType(x.baseType));
  1194. END;
  1195. IF recordBase = NIL THEN numberMethods := 0
  1196. ELSE numberMethods := recordBase.recordScope.numberMethods
  1197. END;
  1198. modifier := x.modifiers;
  1199. (*IF ~x.isCellNet THEN*)
  1200. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1201. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1202. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1203. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1204. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1205. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1206. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1207. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1208. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1209. symbol := system.activeCellsCapabilities;
  1210. WHILE symbol # NIL DO
  1211. IF HasFlag(modifier, symbol.name, position) THEN END;
  1212. symbol := symbol.nextSymbol;
  1213. END;
  1214. modifier := x.modifiers;
  1215. WHILE (modifier # NIL) DO
  1216. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1217. IF modifier.expression # NIL THEN
  1218. v := ConstantExpression(modifier.expression);
  1219. property.SetValue(v);
  1220. IF IsIntegerValue(modifier.expression, int) THEN
  1221. (*property.SetValue(modifier.expression);*)
  1222. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1223. property.SetType(system.longintType);
  1224. ELSIF IsRealValue(modifier.expression, real) THEN
  1225. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1226. property.SetType(system.longrealType);
  1227. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1228. property.SetType(system.booleanType);
  1229. ELSIF IsSetValue(modifier.expression, set) THEN
  1230. property.SetType(system.setType);
  1231. ELSIF IsStringValue(modifier.expression, str) THEN
  1232. (*property.SetValue(modifier.expression);*)
  1233. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1234. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1235. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1236. property.SetType(atype);
  1237. ELSE
  1238. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1239. END;
  1240. ELSE (* flag property *)
  1241. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1242. property.SetType(system.booleanType);
  1243. END;
  1244. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1245. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1246. x.AddProperty(property);
  1247. modifier := modifier.nextModifier;
  1248. END;
  1249. (*ELSE
  1250. (* no: this should not be allowed on cell net types or check against global scope ...
  1251. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1252. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1253. END;
  1254. END
  1255. *)
  1256. END;*)
  1257. CheckModifiers(modifier, FALSE);
  1258. IF ~SkipImplementation(x) THEN
  1259. Declarations(x.cellScope);
  1260. ELSE
  1261. Declarations2(x.cellScope);
  1262. (*
  1263. prev := currentScope;
  1264. currentScope := x.cellScope;
  1265. DeclareCell(x);
  1266. currentScope := prev;
  1267. parameter :=x.firstParameter;
  1268. WHILE(parameter # NIL) DO
  1269. parameter.SetScope(x.cellScope);
  1270. parameter := parameter.nextParameter;
  1271. END;
  1272. *)
  1273. END;
  1274. (* process parameters *)
  1275. prev := currentScope;
  1276. currentScope := x.cellScope;
  1277. parameter :=x.firstParameter;
  1278. WHILE (parameter # NIL) DO
  1279. VisitParameter(parameter);
  1280. type := parameter.type.resolved;
  1281. IF ~(type IS SyntaxTree.PortType) THEN
  1282. WHILE IsStaticArray(type, type, len) DO
  1283. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1284. END;
  1285. WHILE IsDynamicArray(type, type) DO
  1286. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1287. END;
  1288. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1289. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1290. END;
  1291. END;
  1292. parameter := parameter.nextParameter;
  1293. END;
  1294. currentScope := prev;
  1295. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1296. WHILE symbol # NIL DO
  1297. IF symbol IS SyntaxTree.Variable THEN
  1298. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1299. END;
  1300. symbol := symbol.nextSymbol;
  1301. END;
  1302. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1303. IF (x.typeDeclaration = NIL) THEN
  1304. AnonymousTypeDeclaration(x,"Anonymous");
  1305. END;
  1306. x.SetState(SyntaxTree.Resolved);
  1307. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1308. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1309. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1310. Warning(x.position, "Non-empty body for an engine?");
  1311. END;
  1312. END;
  1313. resolvedType := ResolvedType(x);
  1314. END VisitCellType;
  1315. (* check if an object is an array-structured object type
  1316. - determine the array structure
  1317. - collect operators from top to bottom in the inheritance hierarchy
  1318. - check if LEN operator is declared
  1319. - determine number of possible index operators
  1320. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1321. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1322. *)
  1323. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1324. VAR
  1325. indexOperatorCount, i: LONGINT;
  1326. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1327. isTensor: BOOLEAN;
  1328. BEGIN
  1329. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1330. (* determine array structure *)
  1331. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1332. END;
  1333. IF recordType.HasArrayStructure() THEN
  1334. (* the object is an ASOT *)
  1335. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1336. (* reset array access operators *)
  1337. arrayAccessOperators.len := NIL;
  1338. arrayAccessOperators.generalRead := NIL;
  1339. arrayAccessOperators.generalWrite := NIL;
  1340. IF isTensor THEN
  1341. (* all operators of dimensionalities 1 to max *)
  1342. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1343. ELSE
  1344. (* all operators of certain dimensionality *)
  1345. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1346. END;
  1347. NEW(arrayAccessOperators.read, indexOperatorCount);
  1348. NEW(arrayAccessOperators.write, indexOperatorCount);
  1349. FOR i := 0 TO indexOperatorCount - 1 DO
  1350. arrayAccessOperators.read[i] := NIL;
  1351. arrayAccessOperators.write[i] := NIL
  1352. END;
  1353. (* collect access operators in the record scope *)
  1354. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1355. IF arrayAccessOperators.len = NIL THEN
  1356. (* TODO: think about making this operator optional for static array structures *)
  1357. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1358. END;
  1359. (* show error messages *)
  1360. IF isTensor THEN
  1361. (* require ARRAY [*] OF RANGE *)
  1362. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1363. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1364. ELSE
  1365. (* forbid ARRAY [*] OF RANGE *)
  1366. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1367. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1368. (* require RANGE, RANGE, ... RANGE *)
  1369. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1370. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1371. END;
  1372. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1373. ELSE
  1374. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1375. IF recordType.recordScope.firstOperator # NIL THEN
  1376. RETURN;
  1377. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1378. END
  1379. END
  1380. END ResolveArrayStructure;
  1381. (** collect array access operators in a record scope **)
  1382. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1383. VAR
  1384. baseType: SyntaxTree.Type;
  1385. operator: SyntaxTree.Operator;
  1386. isReadOperator, isGeneralOperator: BOOLEAN;
  1387. indexListSize, indexListKind, hashValue: LONGINT;
  1388. BEGIN
  1389. (* if a parent record scope exists, collect the operators there first *)
  1390. baseType := recordScope.ownerRecord.baseType;
  1391. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1392. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1393. END;
  1394. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1395. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1396. END;
  1397. (* go through all operators in the current record scope *)
  1398. operator := recordScope.firstOperator;
  1399. WHILE operator # NIL DO
  1400. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1401. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1402. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1403. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1404. IF isGeneralOperator THEN
  1405. IF isReadOperator THEN
  1406. arrayAccessOperators.generalRead := operator
  1407. ELSE
  1408. arrayAccessOperators.generalWrite := operator
  1409. END
  1410. ELSE
  1411. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1412. IF isReadOperator THEN
  1413. arrayAccessOperators.read[hashValue] := operator
  1414. ELSE
  1415. arrayAccessOperators.write[hashValue] := operator
  1416. END
  1417. END
  1418. END
  1419. ELSE
  1420. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1421. END;
  1422. operator := operator.nextOperator
  1423. END
  1424. END CollectArrayAccessOperators;
  1425. (** the hash value of an index operator **)
  1426. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1427. VAR result: LONGINT;
  1428. BEGIN
  1429. IF isTensor THEN
  1430. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1431. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1432. ELSE
  1433. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1434. END
  1435. ELSE
  1436. result := indexListKind
  1437. END;
  1438. RETURN result
  1439. END IndexOperatorHash;
  1440. (** 2 to the power of exponent **)
  1441. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1442. VAR result, i: LONGINT;
  1443. BEGIN
  1444. result := 1;
  1445. FOR i := 1 TO exponent DO
  1446. result := result * 2;
  1447. END;
  1448. RETURN result
  1449. END TwoToThePowerOf;
  1450. (** check if a LEN operator has a correct signature. i.e.
  1451. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1452. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1453. **)
  1454. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1455. VAR
  1456. procedureType: SyntaxTree.ProcedureType;
  1457. returnedArrayType: SyntaxTree.MathArrayType;
  1458. result: BOOLEAN;
  1459. BEGIN
  1460. result := FALSE;
  1461. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1462. IF (procedureType.numberParameters = 0) THEN
  1463. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1464. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1465. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1466. IF returnedArrayType.form = SyntaxTree.Open THEN
  1467. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1468. result := TRUE
  1469. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1470. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1471. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1472. result := TRUE
  1473. END
  1474. END
  1475. END
  1476. END
  1477. END;
  1478. IF result THEN
  1479. (* export symbol automatically *)
  1480. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1481. ELSE
  1482. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1483. END;
  1484. RETURN result
  1485. END CheckLenOperator;
  1486. (** check if an index operator has a correct signature. i.e.
  1487. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1488. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1489. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1490. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1491. - determine if it is a read or write operator (existance of return type)
  1492. - check index parameters
  1493. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1494. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1495. [LONGINT] -> binary 0 -> 0
  1496. [RANGE] -> binary 1 -> 1
  1497. [LONGINT, LONGINT] -> binary 00 -> 0
  1498. [LONGINT, RANGE] -> binary 01 -> 1
  1499. [RANGE, LONGINT] -> binary 10 -> 2
  1500. [RANGE, RANGE] -> binary 11 -> 3
  1501. etc.
  1502. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1503. - for read operators, check if return type matches the type of data that is read
  1504. - for write operators, check if last parameter type matches the type of data that is written
  1505. **)
  1506. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1507. VAR
  1508. elementType, otherElementType, dataType: SyntaxTree.Type;
  1509. procedureType: SyntaxTree.ProcedureType;
  1510. mathArrayType: SyntaxTree.MathArrayType;
  1511. parameter: SyntaxTree.Parameter;
  1512. parameterCount, rangeCount, i: LONGINT;
  1513. hasTypeError: BOOLEAN;
  1514. BEGIN
  1515. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1516. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1517. (* determine if it is a read or write operator *)
  1518. isReadOperator := (procedureType.returnType # NIL);
  1519. IF isReadOperator THEN
  1520. indexListSize := parameterCount;
  1521. ELSE
  1522. indexListSize := parameterCount - 1;
  1523. END;
  1524. IF indexListSize < 1 THEN
  1525. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1526. RETURN FALSE
  1527. END;
  1528. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1529. (* general operator *)
  1530. isGeneralOperator := TRUE;
  1531. IF indexListSize > 1 THEN
  1532. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1533. RETURN FALSE
  1534. END;
  1535. (* ARRAY [*] OF RANGE*)
  1536. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1537. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1538. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1539. RETURN FALSE
  1540. END;
  1541. parameter := procedureType.firstParameter.nextParameter
  1542. ELSE
  1543. (* fixed-dim. operator *)
  1544. isGeneralOperator := FALSE;
  1545. (* check number of index parameters *)
  1546. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1547. (* for tensors, limited to a certain size *)
  1548. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1549. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1550. RETURN FALSE
  1551. END
  1552. ELSE
  1553. (* for non-tensors, depends on dimensionality *)
  1554. IF indexListSize # arrayStructure.Dimensionality() THEN
  1555. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1556. RETURN FALSE
  1557. END
  1558. END;
  1559. (* go through all index parameters
  1560. - count the number of ranges
  1561. - determine the index list kind number
  1562. *)
  1563. indexListKind := 0;
  1564. rangeCount := 0;
  1565. parameter := procedureType.firstParameter;
  1566. FOR i := 1 TO indexListSize DO
  1567. indexListKind := indexListKind * 2;
  1568. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1569. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1570. INC(indexListKind);
  1571. INC(rangeCount)
  1572. ELSE
  1573. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1574. RETURN FALSE
  1575. END;
  1576. parameter := parameter.nextParameter
  1577. END;
  1578. END;
  1579. (*
  1580. - for read operators: check type of last parameter
  1581. - for write operators: check return type
  1582. *)
  1583. IF isReadOperator THEN
  1584. dataType := procedureType.returnType (* the return type *)
  1585. ELSE
  1586. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1587. END;
  1588. elementType := arrayStructure.ElementType();
  1589. hasTypeError := FALSE;
  1590. IF isGeneralOperator THEN
  1591. (* ARRAY [?] OF <Element> *)
  1592. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1593. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1594. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1595. hasTypeError := TRUE
  1596. END
  1597. ELSE
  1598. hasTypeError := TRUE
  1599. END
  1600. ELSE
  1601. IF rangeCount = 0 THEN
  1602. (* <Element> *)
  1603. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1604. ELSE
  1605. (* ARRAY [*, *, ..., *] OF <Element> *)
  1606. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1607. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1608. IF mathArrayType.IsFullyDynamic() THEN
  1609. IF mathArrayType.Dimensionality() = rangeCount THEN
  1610. otherElementType := mathArrayType.ElementType();
  1611. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1612. ELSE
  1613. hasTypeError := TRUE
  1614. END
  1615. ELSE
  1616. hasTypeError := TRUE
  1617. END
  1618. ELSE
  1619. hasTypeError := TRUE
  1620. END
  1621. END
  1622. END;
  1623. IF hasTypeError THEN
  1624. IF isReadOperator THEN
  1625. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1626. ELSE
  1627. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1628. END;
  1629. RETURN FALSE
  1630. END;
  1631. (* export symbol automatically *)
  1632. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1633. RETURN TRUE
  1634. END CheckIndexOperator;
  1635. (** resolve all pending types (late resolving).
  1636. - type fixes are resolved at the end of the declaration phase
  1637. - type fixes may imply new type fixes that are also entered at the end of the list
  1638. **)
  1639. PROCEDURE FixTypes;
  1640. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1641. BEGIN
  1642. prevScope := currentScope;
  1643. p := typeFixes.Get(currentScope);
  1644. WHILE p # NIL DO
  1645. ASSERT(currentScope # NIL);
  1646. ASSERT(p IS SyntaxTree.Type);
  1647. IF p IS SyntaxTree.PointerType THEN
  1648. FixPointerType(p(SyntaxTree.PointerType))
  1649. ELSIF p IS SyntaxTree.ProcedureType THEN
  1650. FixProcedureType(p(SyntaxTree.ProcedureType))
  1651. ELSE
  1652. HALT(100);
  1653. END;
  1654. p := typeFixes.Get(currentScope);
  1655. END;
  1656. currentScope :=prevScope;
  1657. END FixTypes;
  1658. (**
  1659. resolve type x
  1660. - if x is nil then return nil
  1661. - if x cannot be resolved then the result is invalidType else the result is x
  1662. - the resolved type is entered into x.resolved
  1663. **)
  1664. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1665. VAR prev,resolved: SyntaxTree.Type;
  1666. BEGIN
  1667. prev := resolvedType;
  1668. resolvedType := SyntaxTree.invalidType;
  1669. IF x = NIL THEN resolvedType := NIL
  1670. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1671. END;
  1672. resolved := resolvedType;
  1673. resolvedType := prev;
  1674. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1675. RETURN resolved
  1676. END ResolveType;
  1677. (*** compatibility rules ***)
  1678. (**
  1679. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1680. **)
  1681. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1682. VAR result: SyntaxTree.Type;
  1683. BEGIN
  1684. result := SyntaxTree.invalidType;
  1685. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1686. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1687. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1688. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1689. ELSE result := type.resolved
  1690. END;
  1691. RETURN result
  1692. END RegularType;
  1693. (** returns signature compatibility of procedure types this and to
  1694. - if not compatible then error is reported
  1695. - compatibility means type equality
  1696. **)
  1697. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1698. VAR result: BOOLEAN;
  1699. BEGIN
  1700. result := SameType(to,this);
  1701. IF ~result THEN
  1702. Error(position,Diagnostics.Invalid,"signature incompatible");
  1703. IF VerboseErrorMessage THEN
  1704. Printout.Info("this",this);
  1705. Printout.Info("to",to);
  1706. END;
  1707. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1708. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1709. END;
  1710. RETURN result
  1711. END SignatureCompatible;
  1712. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1713. - for var parameters compatibility means same type except for
  1714. - formal is of open array of system byte
  1715. - formal is of record type
  1716. - formal is of open array type
  1717. - formal is of open math array type
  1718. - for value parameters compatibllity means assignment compatibility except for
  1719. - formal is of open array type
  1720. if compatible the return true else report error and return false
  1721. **)
  1722. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1723. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1724. BEGIN
  1725. formalType := RegularType(formal.position,formal.type);
  1726. actualType := RegularType(actual.position,actual.type);
  1727. error := FALSE;
  1728. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1729. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1730. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1731. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1732. END;
  1733. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1734. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1735. ELSIF ~IsVariable(actual) THEN
  1736. result := FALSE; error := TRUE;
  1737. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1738. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1739. ELSE
  1740. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1741. END;
  1742. IF VerboseErrorMessage THEN
  1743. Printout.Info("actual",actual);
  1744. Printout.Info("formal",formal);
  1745. END;
  1746. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1747. result := CompatibleTo(system,actualType,formalType);
  1748. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1749. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1750. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1751. IF IsArrayStructuredObjectType(actualType) THEN
  1752. actualType := MathArrayStructureOfType(actualType)
  1753. END;
  1754. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1755. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1756. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1757. END;
  1758. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1759. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1760. END;
  1761. ELSE
  1762. result := SameType(actualType,formalType)
  1763. END
  1764. ELSE
  1765. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1766. actualType := system.characterType;
  1767. END;
  1768. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1769. result := TRUE; (* special rule for WINAPI parameters *)
  1770. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1771. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1772. ELSE
  1773. result := CompatibleTo(system,actualType,formalType);
  1774. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1775. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1776. END;
  1777. END;
  1778. END;
  1779. IF ~result & ~error THEN
  1780. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1781. IF VerboseErrorMessage THEN
  1782. Printout.Info("actual",actual);
  1783. Printout.Info("formal",formal);
  1784. END;
  1785. END;
  1786. RETURN result
  1787. END ParameterCompatible;
  1788. (** check compatibility for expressions of the form left := right
  1789. - if compatible then return true else error report and return false
  1790. - check if left is variable
  1791. - check compatiblity
  1792. **)
  1793. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1794. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1795. BEGIN
  1796. result := FALSE;
  1797. leftType := RegularType(left.position,left.type);
  1798. rightType := RegularType(right.position,right.type);
  1799. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1800. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1801. END;
  1802. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1803. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1804. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1805. (* error already handled *)
  1806. result := TRUE;
  1807. ELSIF ~IsVariable(left) THEN
  1808. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1809. IF VerboseErrorMessage THEN
  1810. Printout.Info("left",left);
  1811. Printout.Info("right",right);
  1812. END;
  1813. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1814. result := TRUE;
  1815. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1816. result := TRUE
  1817. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1818. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1819. IF VerboseErrorMessage THEN
  1820. Printout.Info("left",left);
  1821. Printout.Info("right",right);
  1822. END;
  1823. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1824. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1825. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1826. ELSE
  1827. result := TRUE
  1828. END;
  1829. RETURN result
  1830. END AssignmentCompatible;
  1831. (*** values ***)
  1832. (** check and resolve integer value **)
  1833. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1834. VAR hugeint: HUGEINT;
  1835. BEGIN
  1836. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1837. value.SetType(Global.GetIntegerType(system,hugeint));
  1838. resolvedExpression := value
  1839. END VisitIntegerValue;
  1840. (** check and resolve real value **)
  1841. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1842. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1843. BEGIN
  1844. subtype := value(SyntaxTree.RealValue).subtype;
  1845. IF subtype = Scanner.Real THEN
  1846. type := system.realType
  1847. ELSIF subtype = Scanner.Longreal THEN
  1848. type := system.longrealType
  1849. ELSE
  1850. HALT(100)
  1851. END;
  1852. value.SetType(type);
  1853. resolvedExpression := value
  1854. END VisitRealValue;
  1855. (** check and resolve complex value **)
  1856. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1857. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1858. BEGIN
  1859. subtype := value(SyntaxTree.ComplexValue).subtype;
  1860. IF subtype = Scanner.Real THEN
  1861. type := system.complexType
  1862. ELSIF subtype = Scanner.Longreal THEN
  1863. type := system.longcomplexType
  1864. ELSE
  1865. HALT(100)
  1866. END;
  1867. value.SetType(type);
  1868. resolvedExpression := value
  1869. END VisitComplexValue;
  1870. (** check and resolve set value **)
  1871. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1872. BEGIN
  1873. value.SetType(system.setType);
  1874. resolvedExpression := value
  1875. END VisitSetValue;
  1876. (** check and resolve set value **)
  1877. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1878. BEGIN
  1879. value.SetType(SyntaxTree.invalidType);
  1880. resolvedExpression := value
  1881. END VisitMathArrayValue;
  1882. (** check and resolve boolean value **)
  1883. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1884. BEGIN
  1885. value.SetType(system.booleanType);
  1886. resolvedExpression := value
  1887. END VisitBooleanValue;
  1888. (** check and resolve string value **)
  1889. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1890. BEGIN
  1891. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1892. resolvedExpression := value
  1893. END VisitStringValue;
  1894. (** check and resolve character value **)
  1895. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1896. BEGIN
  1897. value.SetType(system.characterType);
  1898. resolvedExpression := value
  1899. END VisitCharacterValue;
  1900. (** check and resolve nil value **)
  1901. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1902. BEGIN
  1903. value.SetType(system.nilType);
  1904. resolvedExpression := value
  1905. END VisitNilValue;
  1906. (** check and resolve enumerator value **)
  1907. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1908. BEGIN
  1909. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1910. ASSERT(value.type # NIL);
  1911. resolvedExpression := value
  1912. END VisitEnumerationValue;
  1913. (*** expressions ***)
  1914. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1915. - check all elements on integer type
  1916. - if element range is constant, then check lower and upper bound
  1917. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1918. if an error occurs then report error and return invalidExpression
  1919. **)
  1920. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1921. VAR
  1922. i: LONGINT;
  1923. element: SyntaxTree.Expression;
  1924. constant: BOOLEAN;
  1925. elements: SyntaxTree.ExpressionList;
  1926. s: SET;
  1927. result: SyntaxTree.Expression;
  1928. value: SyntaxTree.Value;
  1929. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1930. VAR
  1931. left, right: SyntaxTree.Expression;
  1932. elementResult: SyntaxTree.Expression;
  1933. leftInteger, rightInteger, temp: LONGINT;
  1934. BEGIN
  1935. (* set context of range *)
  1936. IF element IS SyntaxTree.RangeExpression THEN
  1937. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1938. END;
  1939. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1940. IF elementResult = SyntaxTree.invalidExpression THEN
  1941. (* error already reported *)
  1942. constant := FALSE
  1943. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1944. (* the element is a range expression *)
  1945. (* extract left and right hand side of range *)
  1946. left := elementResult(SyntaxTree.RangeExpression).first;
  1947. right := elementResult(SyntaxTree.RangeExpression).last;
  1948. (* guaranteed by VisitRangeExpression: *)
  1949. ASSERT((left # NIL) & (right # NIL));
  1950. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1951. ELSE
  1952. (* the element is not a range expression *)
  1953. (* check type and add conversion if needed *)
  1954. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1955. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1956. ELSE
  1957. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1958. elementResult := SyntaxTree.invalidExpression;
  1959. constant := FALSE
  1960. END;
  1961. left := elementResult;
  1962. right := elementResult
  1963. END;
  1964. IF elementResult # SyntaxTree.invalidExpression THEN
  1965. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1966. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1967. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1968. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1969. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1970. END
  1971. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1972. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1973. ELSE
  1974. IF leftInteger > rightInteger THEN
  1975. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1976. END;
  1977. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1978. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1979. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1980. extends the range x..31 to x..63 !!!!!! *)
  1981. s := s + {leftInteger..rightInteger};
  1982. (* same but more explicit:
  1983. WHILE (leftInteger <= rightInteger) DO
  1984. INCL(s,leftInteger);
  1985. INC(leftInteger);
  1986. END;
  1987. *)
  1988. END;
  1989. ELSE
  1990. constant := FALSE;
  1991. END
  1992. END;
  1993. RETURN elementResult
  1994. END CheckElement;
  1995. BEGIN
  1996. result := set; constant := TRUE; s := {}; elements := set.elements;
  1997. IF elements # NIL THEN
  1998. FOR i := 0 TO elements.Length()-1 DO
  1999. element := elements.GetExpression(i);
  2000. element := CheckElement(element);
  2001. IF element = SyntaxTree.invalidExpression THEN
  2002. result := SyntaxTree.invalidExpression
  2003. END;
  2004. elements.SetExpression(i,element);
  2005. END;
  2006. END;
  2007. IF constant THEN
  2008. value := SyntaxTree.NewSetValue(set.position,s);
  2009. value.SetType(system.setType);
  2010. result.SetResolved(value);
  2011. END;
  2012. (* optimization possible
  2013. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  2014. left this to the programmer...
  2015. *)
  2016. result.SetType(system.setType);
  2017. resolvedExpression := result;
  2018. END VisitSet;
  2019. (*
  2020. old variant: quite generic but needs better conversion handling, do this?
  2021. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  2022. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  2023. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  2024. BEGIN
  2025. type := NIL;
  2026. numberElements := x.elements.Length();
  2027. FOR i := 0 TO numberElements-1 DO
  2028. expression := x.elements.GetExpression(i);
  2029. position := expression.position;
  2030. expression := ResolveExpression(x.elements.GetExpression(i));
  2031. x.elements.SetExpression(i,de);
  2032. IF type = NIL THEN
  2033. type := expression.type;
  2034. ELSIF CompatibleTo(system,expression.type,type) THEN
  2035. (* ok *)
  2036. ELSIF CompatibleTo(system,type,expression.type) THEN
  2037. type := expression.type
  2038. ELSE
  2039. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  2040. type := SyntaxTree.invalidType;
  2041. END;
  2042. END;
  2043. isValue := TRUE;
  2044. FOR i := 0 TO numberElements-1 DO
  2045. expression := NewConversion(position,x.elements.GetExpression(i),type);
  2046. x.elements.SetExpression(i,expression);
  2047. isValue := isValue & (expression.resolved # NIL);
  2048. END;
  2049. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2050. arrayType.SetArrayBase(type);
  2051. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  2052. IF isValue THEN
  2053. value := SyntaxTree.NewMathArrayValue(position);
  2054. value.SetElements(x.elements);
  2055. x.SetResolved(value);
  2056. END;
  2057. x.SetType(arrayType);
  2058. resolvedExpression := x;
  2059. END VisitMathArrayExpression;
  2060. *)
  2061. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  2062. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  2063. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  2064. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  2065. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  2066. BEGIN
  2067. numberElements := x.elements.Length();
  2068. FOR i := 0 TO numberElements-1 DO
  2069. expression := x.elements.GetExpression(i);
  2070. IF expression IS SyntaxTree.MathArrayExpression THEN
  2071. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  2072. ELSE
  2073. position := expression.position;
  2074. expression := ResolveExpression(x.elements.GetExpression(i));
  2075. x.elements.SetExpression(i,expression);
  2076. IF type = NIL THEN
  2077. type := expression.type;
  2078. ELSIF CompatibleTo(system,expression.type,type) THEN
  2079. (* ok *)
  2080. ELSIF CompatibleTo(system,type,expression.type) THEN
  2081. type := expression.type
  2082. ELSE
  2083. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  2084. type := SyntaxTree.invalidType;
  2085. END;
  2086. END;
  2087. END;
  2088. END RecursivelyFindType;
  2089. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  2090. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  2091. BEGIN
  2092. numberElements := x.elements.Length();
  2093. FOR i := 0 TO numberElements-1 DO
  2094. expression := x.elements.GetExpression(i);
  2095. IF expression IS SyntaxTree.MathArrayExpression THEN
  2096. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  2097. ELSE
  2098. position := expression.position;
  2099. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2100. x.elements.SetExpression(i,expression);
  2101. isValue := isValue & (expression.resolved # NIL);
  2102. END;
  2103. END;
  2104. END RecursivelySetExpression;
  2105. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2106. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2107. arrayType: SyntaxTree.MathArrayType;
  2108. BEGIN
  2109. numberElements := x.elements.Length();
  2110. baseType := NIL;
  2111. gsize := 0;
  2112. FOR i := 0 TO numberElements-1 DO
  2113. expression := x.elements.GetExpression(i);
  2114. IF expression IS SyntaxTree.MathArrayExpression THEN
  2115. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2116. IF i=0 THEN
  2117. gsize := size;
  2118. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2119. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2120. ELSE expression.SetType(baseType)
  2121. END;
  2122. ELSIF baseType = NIL THEN baseType := type;
  2123. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2124. END;
  2125. END;
  2126. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2127. arrayType.SetArrayBase(baseType);
  2128. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2129. RETURN ResolveType(arrayType);
  2130. END RecursivelySetType;
  2131. BEGIN
  2132. type := NIL;
  2133. RecursivelyFindType(x);
  2134. isValue := TRUE;
  2135. RecursivelySetExpression(x);
  2136. arrayType := RecursivelySetType(x);
  2137. x.SetType(arrayType);
  2138. IF isValue THEN
  2139. value := SyntaxTree.NewMathArrayValue(x.position);
  2140. value.SetArray(x);
  2141. x.SetResolved(value);
  2142. value.SetType(arrayType);
  2143. END;
  2144. x.SetType(arrayType);
  2145. resolvedExpression := x;
  2146. END VisitMathArrayExpression;
  2147. (** check and resolve unary expression **)
  2148. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  2149. VAR
  2150. left: SyntaxTree.Expression;
  2151. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  2152. bool: BOOLEAN;
  2153. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2154. value: SyntaxTree.Value;
  2155. BEGIN
  2156. type := SyntaxTree.invalidType;
  2157. left := ResolveExpression(unaryExpression.left);
  2158. unaryExpression.SetLeft(left);
  2159. operator := unaryExpression.operator;
  2160. result := unaryExpression;
  2161. IF ~system.operatorDefined[operator] THEN
  2162. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2163. RETURN
  2164. ELSIF left.type = NIL THEN
  2165. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  2166. resolvedExpression := SyntaxTree.invalidExpression;
  2167. RETURN
  2168. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2169. RETURN
  2170. END;
  2171. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2172. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2173. END;
  2174. IF operatorCall # NIL THEN
  2175. result := operatorCall;
  2176. type := operatorCall.type;
  2177. (* admissible operators
  2178. Minus number, set
  2179. Not boolean
  2180. *)
  2181. ELSE
  2182. CASE unaryExpression.operator OF
  2183. |Scanner.Minus:
  2184. IF IsIntegerType(left.type.resolved) THEN
  2185. IF left.resolved # NIL THEN
  2186. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2187. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2188. result.SetResolved(value);
  2189. type := Global.GetIntegerType(system,int);
  2190. value.SetType(type);
  2191. ELSE
  2192. type := left.type
  2193. END
  2194. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2195. IF IsRealValue(left,real) THEN
  2196. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2197. result.SetResolved(value);
  2198. type := left.type;
  2199. value.SetType(type);
  2200. ELSE
  2201. type := left.type;
  2202. END;
  2203. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2204. IF IsSetValue(left,set) THEN
  2205. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2206. result.SetResolved(value);
  2207. type := left.type;
  2208. value.SetType(type);
  2209. ELSE
  2210. type := left.type;
  2211. END;
  2212. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2213. IF IsComplexValue(left, real, imaginary) THEN
  2214. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2215. result.SetResolved(value);
  2216. type := left.type;
  2217. value.SetType(type);
  2218. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2219. ELSE
  2220. type := left.type;
  2221. END
  2222. ELSE
  2223. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2224. END;
  2225. |Scanner.Not:
  2226. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2227. IF IsBooleanValue(left,bool) THEN
  2228. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2229. result.SetResolved(value);
  2230. type := system.booleanType;
  2231. value.SetType(type);
  2232. ELSE
  2233. type := system.booleanType;
  2234. END;
  2235. ELSE
  2236. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2237. END;
  2238. |Scanner.Plus:
  2239. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2240. result := left; type := left.type;
  2241. ELSE
  2242. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2243. END;
  2244. (* ADDRESS OF *)
  2245. |Scanner.Address:
  2246. IF HasAddress(left) THEN
  2247. type := system.addressType;
  2248. ELSE
  2249. type := SyntaxTree.invalidType;
  2250. Error(left.position,Diagnostics.Invalid,"has no address");
  2251. Printout.Info("par", left);
  2252. END;
  2253. (* SIZE OF *)
  2254. |Scanner.Size:
  2255. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2256. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2257. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2258. value := SyntaxTree.NewIntegerValue(left.position, int);
  2259. result.SetResolved(value);
  2260. type := Global.GetIntegerType(system,int);
  2261. value.SetType(type)
  2262. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2263. ELSE
  2264. (* for variables, system sizeof could represent the physically occupied size
  2265. determined via the type descriptor, implement that ? *)
  2266. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2267. END
  2268. (* ALIAS OF *)
  2269. |Scanner.Alias:
  2270. type := left.type.resolved;
  2271. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2272. type := SyntaxTree.invalidType;
  2273. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2274. END;
  2275. ELSE
  2276. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2277. END;
  2278. END;
  2279. result.SetType(type);
  2280. resolvedExpression := result
  2281. END VisitUnaryExpression;
  2282. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2283. VAR
  2284. result: SyntaxTree.Expression;
  2285. array: SyntaxTree.MathArrayExpression;
  2286. value: SyntaxTree.MathArrayValue;
  2287. isValue: BOOLEAN;
  2288. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2289. BEGIN
  2290. type := type.resolved;
  2291. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2292. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2293. END;
  2294. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2295. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2296. END;
  2297. RETURN type
  2298. END BaseType;
  2299. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2300. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2301. BEGIN
  2302. numberElements := x.elements.Length();
  2303. FOR i := 0 TO numberElements-1 DO
  2304. expression := x.elements.GetExpression(i);
  2305. IF expression IS SyntaxTree.MathArrayExpression THEN
  2306. array := SyntaxTree.NewMathArrayExpression(position);
  2307. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2308. expression := array;
  2309. ELSE
  2310. position := expression.position;
  2311. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2312. isValue := isValue & (expression.resolved # NIL);
  2313. END;
  2314. to.elements.AddExpression(expression);
  2315. END;
  2316. END RecursivelyConvert;
  2317. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2318. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2319. arrayType: SyntaxTree.MathArrayType;
  2320. BEGIN
  2321. numberElements := x.elements.Length();
  2322. baseType := NIL;
  2323. gsize := 0;
  2324. FOR i := 0 TO numberElements-1 DO
  2325. expression := x.elements.GetExpression(i);
  2326. IF expression IS SyntaxTree.MathArrayExpression THEN
  2327. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2328. IF i=0 THEN
  2329. gsize := size;
  2330. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2331. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2332. ELSE expression.SetType(baseType)
  2333. END;
  2334. ELSIF baseType = NIL THEN baseType := type;
  2335. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2336. END;
  2337. END;
  2338. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2339. arrayType.SetArrayBase(baseType);
  2340. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2341. RETURN ResolveType(arrayType);
  2342. END RecursivelySetType;
  2343. BEGIN
  2344. result := SyntaxTree.invalidExpression;
  2345. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2346. result := expression (* do not convert *)
  2347. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2348. isValue := TRUE;
  2349. type := BaseType(type);
  2350. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2351. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2352. value := SyntaxTree.NewMathArrayValue(array.position);
  2353. value.SetArray(array);
  2354. value.SetType(RecursivelySetType(array));
  2355. result := value;
  2356. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2357. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2358. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2359. IF result = NIL THEN
  2360. result := SyntaxTree.invalidExpression;
  2361. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2362. IF VerboseErrorMessage THEN
  2363. Printout.Info("expression",expression);
  2364. Printout.Info("type",type);
  2365. END;
  2366. END;
  2367. END;
  2368. RETURN result
  2369. END MathArrayConversion;
  2370. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2371. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2372. BEGIN
  2373. result := expression; type := type.resolved;
  2374. IF (expression IS SyntaxTree.IntegerValue) THEN
  2375. int := expression(SyntaxTree.IntegerValue).hvalue;
  2376. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2377. int := Global.ConvertSigned(int,system.SizeOf(type));
  2378. result := SyntaxTree.NewIntegerValue(position,int);
  2379. result.SetType(type);
  2380. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2381. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2382. result := SyntaxTree.NewIntegerValue(position,int);
  2383. result.SetType(type);
  2384. ELSIF (type IS SyntaxTree.FloatType) THEN
  2385. result := SyntaxTree.NewRealValue(expression.position,int);
  2386. result.SetType(type);
  2387. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2388. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2389. result.SetType(type);
  2390. ELSIF (type IS SyntaxTree.SetType) THEN
  2391. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2392. result.SetType(type);
  2393. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2394. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2395. result.SetType(type);
  2396. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2397. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2398. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2399. END;
  2400. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2401. result.SetType(type);
  2402. ELSIF (type IS SyntaxTree.PortType) THEN
  2403. result := ConvertValue(position, expression, system.integerType);
  2404. ELSE
  2405. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2406. result := SyntaxTree.invalidExpression;
  2407. IF VerboseErrorMessage THEN
  2408. Printout.Info("expression",expression);
  2409. Printout.Info("type",type);
  2410. END;
  2411. END;
  2412. ELSIF IsRealValue(expression,real) THEN
  2413. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2414. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2415. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2416. result.SetType(type);
  2417. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2418. int := ENTIERH(real);
  2419. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2420. result.SetType(type);
  2421. ELSIF (type IS SyntaxTree.FloatType) THEN
  2422. result := SyntaxTree.NewRealValue(position,real);
  2423. result.SetType(type);
  2424. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2425. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2426. result.SetType(type);
  2427. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2428. ELSIF (type IS SyntaxTree.PortType) THEN
  2429. result := ConvertValue(position, expression, system.integerType);
  2430. ELSE
  2431. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2432. result := SyntaxTree.invalidExpression;
  2433. END
  2434. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2435. IF (type IS SyntaxTree.ComplexType) THEN
  2436. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2437. result.SetType(type);
  2438. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2439. ELSE
  2440. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2441. result := SyntaxTree.invalidExpression;
  2442. END
  2443. ELSIF IsSetValue(expression,set) THEN
  2444. IF (type IS SyntaxTree.IntegerType) THEN
  2445. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2446. result.SetType(type);
  2447. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2448. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2449. result.SetType(type);
  2450. ELSIF (type IS SyntaxTree.PortType) THEN
  2451. result := ConvertValue(position, expression, system.integerType);
  2452. ELSE
  2453. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2454. result := SyntaxTree.invalidExpression;
  2455. END;
  2456. ELSIF IsStringValue(expression,string) THEN
  2457. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2458. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2459. result.SetType(type);
  2460. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2461. ELSE
  2462. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2463. result := SyntaxTree.invalidExpression;
  2464. END;
  2465. ELSIF IsCharacterValue(expression,char) THEN
  2466. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2467. string[0] := char; string[1] := 0X;
  2468. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2469. result := SyntaxTree.NewStringValue(expression.position,string);
  2470. result.SetType(type);
  2471. ELSIF (type IS SyntaxTree.ByteType) THEN
  2472. (* do not simply set the new type as this could invalidate types of constants *)
  2473. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2474. result.SetType(type)
  2475. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2476. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2477. result.SetType(type);
  2478. ELSIF (type IS SyntaxTree.SetType) THEN
  2479. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2480. result.SetType(type);
  2481. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2482. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2483. result.SetType(type);
  2484. ELSIF (type IS SyntaxTree.PortType) THEN
  2485. result := ConvertValue(position, expression, system.integerType);
  2486. ELSE
  2487. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2488. result := SyntaxTree.invalidExpression;
  2489. END;
  2490. ELSIF expression IS SyntaxTree.NilValue THEN
  2491. IF type IS SyntaxTree.AddressType THEN
  2492. result := SyntaxTree.NewIntegerValue(position,0);
  2493. result.SetType(type);
  2494. ELSE
  2495. result := expression;
  2496. END;
  2497. (* nothing to be done *)
  2498. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2499. result := MathArrayConversion(position, expression,type);
  2500. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2501. int := expression(SyntaxTree.EnumerationValue).value;
  2502. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2503. int := Global.ConvertSigned(int,system.SizeOf(type));
  2504. result := SyntaxTree.NewIntegerValue(position,int);
  2505. result.SetType(type);
  2506. ELSE
  2507. result := expression;
  2508. END;
  2509. (* nothing to be done *)
  2510. ELSE
  2511. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2512. IF VerboseErrorMessage THEN
  2513. Printout.Info("expression",expression);
  2514. Printout.Info("type",type);
  2515. END;
  2516. result := SyntaxTree.invalidExpression;
  2517. END;
  2518. RETURN result
  2519. END ConvertValue;
  2520. (**
  2521. return a conversion of an expression to a given type
  2522. - if expression is already of same type then return expression
  2523. - if incompatible conversion then report error and return invalidExpression
  2524. **)
  2525. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2526. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2527. BEGIN
  2528. type := type.resolved;
  2529. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2530. result := expression;
  2531. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2532. ELSIF expression = NIL THEN (* NIL expression *)
  2533. ELSIF expression.type = NIL THEN
  2534. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2535. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2536. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2537. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2538. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2539. ELSIF expression.resolved # NIL THEN (* value *)
  2540. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2541. IF value IS SyntaxTree.Value THEN
  2542. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2543. result.SetResolved(value(SyntaxTree.Value));
  2544. result.SetType(value.type);
  2545. ELSE
  2546. result := value
  2547. END;
  2548. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2549. expressionList := SyntaxTree.NewExpressionList();
  2550. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2551. typeDeclaration.SetDeclaredType(type);
  2552. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2553. typeSymbol.SetType(typeDeclaration.type);
  2554. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2555. expressionList.AddExpression(expression);
  2556. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2557. result.SetType(type);
  2558. ELSIF IsArrayStructuredObjectType(type) THEN
  2559. (* no type can be converted to an array-structured object type *)
  2560. HALT(100)
  2561. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2562. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2563. result := MathArrayConversion(position, expression,type);
  2564. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2565. expression := ConvertToMathArray(expression);
  2566. type := MathArrayStructureOfType(type);
  2567. result := MathArrayConversion(position, expression, type)
  2568. ELSE
  2569. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2570. END;
  2571. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2572. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2573. OR ~(type IS SyntaxTree.ArrayType) THEN
  2574. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2575. END;
  2576. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2577. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2578. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2579. (*skip, no conversion*)
  2580. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2581. (* skip, no conversion *)
  2582. ELSE
  2583. ASSERT(~(type IS SyntaxTree.RangeType));
  2584. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2585. ASSERT(type # NIL);
  2586. END;
  2587. RETURN result
  2588. END NewConversion;
  2589. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2590. BEGIN
  2591. IF CompatibleTo(system,expression.type, type) THEN
  2592. RETURN NewConversion(position, expression, type, NIL);
  2593. ELSE
  2594. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2595. RETURN SyntaxTree.invalidExpression
  2596. END;
  2597. END CompatibleConversion;
  2598. (**
  2599. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2600. **)
  2601. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2602. VAR leftType,rightType: SyntaxTree.Type;
  2603. BEGIN
  2604. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2605. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2606. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2607. (* error already handled *)
  2608. ELSE
  2609. leftType := left.type.resolved; rightType := right.type.resolved;
  2610. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2611. right := NewConversion(right.position, right, leftType, NIL);
  2612. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2613. left := NewConversion(left.position,left,rightType,NIL);
  2614. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2615. right := NewConversion(right.position, right, leftType, NIL);
  2616. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2617. left := NewConversion(left.position,left,rightType,NIL);
  2618. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2619. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2620. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2621. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2622. ELSIF
  2623. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2624. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2625. (* must be the case LONGREAL / COMPLEX ) *)
  2626. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2627. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2628. ELSE
  2629. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2630. END;
  2631. END;
  2632. END ConvertOperands;
  2633. (** find and return best operator matching to parameter list (nil, if none)
  2634. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2635. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2636. **)
  2637. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2638. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2639. identifier: SyntaxTree.Identifier;
  2640. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2641. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2642. BEGIN
  2643. operator := scope.firstOperator;
  2644. WHILE(operator # NIL) DO
  2645. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2646. procedureType := operator.type(SyntaxTree.ProcedureType);
  2647. distance := Distance(system, procedureType,actualParameters);
  2648. IF (distance < Infinity) THEN
  2649. IF returnType # NIL THEN
  2650. IF procedureType.returnType = NIL THEN
  2651. distance := Infinity
  2652. ELSE
  2653. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2654. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2655. END;
  2656. END;
  2657. END;
  2658. (*
  2659. IF distance < Infinity THEN
  2660. TRACE(distance, operator);
  2661. Printout.Info("potential operator",operator);
  2662. ELSE
  2663. Printout.Info("operator not possible",operator);
  2664. END;
  2665. *)
  2666. IF distance < bestDistance THEN
  2667. bestDistance := distance;
  2668. bestOperator := operator;
  2669. END;
  2670. END;
  2671. operator := operator.nextOperator;
  2672. END;
  2673. (*
  2674. Printout.Info("taken operator",bestOperator);
  2675. *)
  2676. END FindInScope;
  2677. BEGIN
  2678. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2679. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2680. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2681. import := currentScope.ownerModule.moduleScope.firstImport;
  2682. WHILE (bestDistance > 0) & (import # NIL) DO
  2683. IF import.module # NIL THEN
  2684. identifier := Global.GetIdentifier(operator,import.module.case);
  2685. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2686. END;
  2687. import := import.nextImport;
  2688. END;
  2689. RETURN bestOperator
  2690. END FindOperator;
  2691. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2692. BEGIN
  2693. currentScope := scope;
  2694. END SetCurrentScope;
  2695. (**
  2696. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2697. - handle LEN and DIM operator for array-structured object types
  2698. - find operator, if found then
  2699. - if in other module then add import designator
  2700. - create symbol designator for operator
  2701. - if error then return invalidExpression, if no operator then return NIL
  2702. **)
  2703. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2704. VAR
  2705. operator: SyntaxTree.Operator;
  2706. import: SyntaxTree.Import;
  2707. expression, result: SyntaxTree.Expression;
  2708. designator: SyntaxTree.Designator;
  2709. actualParameters, tempList: SyntaxTree.ExpressionList;
  2710. recordType: SyntaxTree.RecordType;
  2711. castReturnType : SyntaxTree.MathArrayType;
  2712. BEGIN
  2713. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2714. result := SyntaxTree.invalidExpression
  2715. ELSIF leftExpression = NIL THEN
  2716. result := NIL
  2717. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2718. (* LEN or DIM operator on array-structured object type *)
  2719. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2720. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2721. IF recordType.arrayAccessOperators.len = NIL THEN
  2722. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2723. result := SyntaxTree.invalidExpression
  2724. ELSE
  2725. ASSERT(leftExpression IS SyntaxTree.Designator);
  2726. designator := leftExpression(SyntaxTree.Designator);
  2727. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2728. ASSERT(expression IS SyntaxTree.Designator);
  2729. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2730. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2731. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2732. result := designator
  2733. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2734. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2735. tempList := SyntaxTree.NewExpressionList();
  2736. tempList.AddExpression(rightExpression);
  2737. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2738. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2739. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2740. tempList := SyntaxTree.NewExpressionList();
  2741. tempList.AddExpression(designator);
  2742. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2743. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2744. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2745. END
  2746. END;
  2747. ELSE
  2748. (* import OCArrayBase if needed *)
  2749. IF ~arrayBaseImported THEN
  2750. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2751. (* operators on complex numbers *)
  2752. ImportModule(Global.ArrayBaseName,position);
  2753. arrayBaseImported := TRUE;
  2754. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2755. IF op = Global.Dim THEN
  2756. (* not existing in OCArrayBase *)
  2757. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2758. (* not existing in OCArrayBase *)
  2759. ELSE
  2760. ImportModule(Global.ArrayBaseName,position);
  2761. arrayBaseImported := TRUE;
  2762. END
  2763. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2764. ImportModule(Global.ArrayBaseName,position);
  2765. arrayBaseImported := TRUE
  2766. END;
  2767. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2768. (* LEN(RANGE) *)
  2769. ImportModule(Global.ArrayBaseName,position);
  2770. arrayBaseImported := TRUE;
  2771. END;
  2772. END;
  2773. actualParameters := SyntaxTree.NewExpressionList();
  2774. actualParameters.AddExpression(leftExpression);
  2775. IF rightExpression # NIL THEN
  2776. actualParameters.AddExpression(rightExpression)
  2777. END;
  2778. operator := FindOperator(system,op,actualParameters,resultType);
  2779. IF operator # NIL THEN
  2780. designator := NIL;
  2781. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2782. import := currentScope.ownerModule.moduleScope.firstImport;
  2783. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2784. import := import.nextImport;
  2785. END;
  2786. expression := NewSymbolDesignator(position,NIL,import);
  2787. designator := expression(SyntaxTree.Designator);
  2788. END;
  2789. expression := NewSymbolDesignator(position,designator,operator);
  2790. designator := expression(SyntaxTree.Designator);
  2791. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2792. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2793. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2794. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2795. result.SetType(castReturnType);
  2796. END;
  2797. ELSE
  2798. result := NIL;
  2799. END;
  2800. END;
  2801. RETURN result
  2802. END NewOperatorCall;
  2803. (** check and resolve binary expression **)
  2804. (*! clean up *)
  2805. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2806. VAR left,right,result: SyntaxTree.Expression;
  2807. leftType, rightType: SyntaxTree.Type;
  2808. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2809. cl,cr: CHAR;
  2810. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2811. type: SyntaxTree.Type;
  2812. value: SyntaxTree.Value;
  2813. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2814. integerConstantFolding: BOOLEAN;
  2815. list: SyntaxTree.ExpressionList;
  2816. PROCEDURE NewBool(v: BOOLEAN);
  2817. BEGIN
  2818. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2819. value.SetType(system.booleanType);
  2820. result.SetResolved(value);
  2821. type := system.booleanType
  2822. END NewBool;
  2823. PROCEDURE NewSet(v: SET);
  2824. BEGIN
  2825. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2826. value.SetType(system.setType);
  2827. result.SetResolved(value);
  2828. type := system.setType;
  2829. END NewSet;
  2830. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2831. BEGIN
  2832. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2833. (* type cast to "larger" type only if the value is still in the range *)
  2834. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2835. value.SetType(t);
  2836. END;
  2837. result.SetResolved(value);
  2838. type := value.type;
  2839. END NewInteger;
  2840. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2841. BEGIN
  2842. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2843. value.SetType(t);
  2844. result.SetResolved(value);
  2845. type := t;
  2846. END NewReal;
  2847. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2848. BEGIN
  2849. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2850. value.SetType(t);
  2851. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2852. result.SetResolved(value);
  2853. type := t;
  2854. END NewComplex;
  2855. BEGIN
  2856. type := SyntaxTree.invalidType;
  2857. left := ResolveExpression(binaryExpression.left);
  2858. right := ResolveExpression(binaryExpression.right);
  2859. binaryExpression.SetLeft(left);
  2860. binaryExpression.SetRight(right);
  2861. result := binaryExpression;
  2862. operator := binaryExpression.operator;
  2863. IF ~system.operatorDefined[operator] THEN
  2864. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2865. result := SyntaxTree.invalidExpression;
  2866. RETURN
  2867. END;
  2868. IF left.type = NIL THEN
  2869. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2870. result := SyntaxTree.invalidExpression;
  2871. RETURN;
  2872. END;
  2873. IF right.type = NIL THEN
  2874. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2875. result := SyntaxTree.invalidExpression;
  2876. RETURN;
  2877. END;
  2878. leftType := left.type.resolved; rightType := right.type.resolved;
  2879. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2880. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2881. END;
  2882. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2883. list := SyntaxTree.NewExpressionList();
  2884. list.AddExpression(right);
  2885. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2886. END;
  2887. IF operatorCall # NIL THEN
  2888. result := operatorCall;
  2889. type := operatorCall.type;
  2890. (* admissible operators:
  2891. Times, Plus, Minus numeric numeric numeric
  2892. set set set
  2893. Slash numeric numeric real /complex
  2894. set set set
  2895. Div , Mod integer integer integer
  2896. And, Or bool bool bool
  2897. Equal, Unequal basic basic bool
  2898. pointer pointer bool
  2899. object object bool
  2900. record record bool
  2901. string string bool
  2902. enumerator enumerator bool
  2903. Less, LessEqual,
  2904. Greater, GreaterEqual integer/real integer/real bool
  2905. enumerator enumerator bool
  2906. In integer set bool
  2907. Is pointer type bool
  2908. object type bool
  2909. record type bool
  2910. Upto: special abbreviation for a..b
  2911. *)
  2912. ELSIF (left.type = NIL) THEN
  2913. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2914. D.Str("nil type in "); D.Type(left); D.Ln;
  2915. result := SyntaxTree.invalidExpression;
  2916. ELSIF (right.type = NIL) THEN
  2917. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2918. result := SyntaxTree.invalidExpression;
  2919. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2920. result := SyntaxTree.invalidExpression;
  2921. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2922. HALT(100);
  2923. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2924. type := system.booleanType;
  2925. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2926. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2927. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2928. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2929. IF VerboseErrorMessage THEN
  2930. Printout.Info("left",left);
  2931. Printout.Info("right",right);
  2932. END;
  2933. ELSIF IsUnsafePointer(left.type) THEN
  2934. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2935. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2936. NewBool(TRUE)
  2937. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2938. NewBool(TRUE);
  2939. ELSIF IsUnextensibleRecord(left) THEN
  2940. NewBool(FALSE)
  2941. END
  2942. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2943. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2944. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2945. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2946. ELSIF operator = Scanner.In THEN (* left IN right *)
  2947. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2948. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2949. NewBool(il IN sr);
  2950. ELSE
  2951. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2952. left := NewConversion(left.position, left, system.longintType,NIL);
  2953. binaryExpression.SetLeft(left)
  2954. END;
  2955. type := system.booleanType;
  2956. END
  2957. ELSE
  2958. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2959. END
  2960. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2961. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2962. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2963. END;
  2964. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2965. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2966. END
  2967. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2968. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2969. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2970. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2971. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2972. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2973. ConvertOperands(left, right);
  2974. binaryExpression.SetLeft(left);
  2975. binaryExpression.SetRight(right);
  2976. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2977. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2978. END;
  2979. type := system.booleanType;
  2980. ELSE
  2981. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2982. END
  2983. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2984. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2985. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2986. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2987. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2988. CASE operator OF
  2989. |Scanner.Equal: NewBool(strl^=strr^);
  2990. |Scanner.Unequal:NewBool(strl^#strr^);
  2991. |Scanner.Less: NewBool(strl^<strr^);
  2992. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2993. |Scanner.Greater: NewBool(strl^>strr^);
  2994. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2995. ELSE
  2996. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2997. END;
  2998. END;
  2999. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  3000. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3001. type := system.booleanType
  3002. ELSE
  3003. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  3004. END;
  3005. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  3006. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3007. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3008. type := system.booleanType;
  3009. ELSE
  3010. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  3011. END
  3012. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  3013. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  3014. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3015. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3016. type := system.booleanType
  3017. ELSE
  3018. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  3019. END;
  3020. ELSE
  3021. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  3022. END;
  3023. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  3024. type := system.booleanType;
  3025. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  3026. type := system.booleanType;
  3027. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  3028. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  3029. THEN
  3030. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  3031. IF (leftType # rightType) THEN
  3032. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  3033. ConvertOperands(left,right); (* operands must be of the same type here *)
  3034. END;
  3035. binaryExpression.SetLeft(left);
  3036. binaryExpression.SetRight(right);
  3037. leftType := left.type.resolved;
  3038. rightType := right.type.resolved;
  3039. END;
  3040. type := leftType;
  3041. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  3042. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  3043. IF VerboseErrorMessage THEN
  3044. Printout.Info("left",left);
  3045. Printout.Info("right",right);
  3046. END;
  3047. ELSIF IsIntegerType(leftType) THEN
  3048. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  3049. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  3050. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  3051. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  3052. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  3053. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  3054. END;
  3055. END;
  3056. (* constant folding *)
  3057. (* bootstrap64
  3058. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  3059. CASE operator OF
  3060. |Scanner.Plus: NewInteger(il+ir,left.type);
  3061. |Scanner.Minus: NewInteger(il-ir,left.type);
  3062. |Scanner.Times: NewInteger(il*ir,left.type);
  3063. |Scanner.Slash:
  3064. IF ir # 0 THEN
  3065. NewReal(il/ir, system.realType);
  3066. END;
  3067. |Scanner.Mod:
  3068. IF ir > 0 THEN
  3069. NewInteger(il MOD ir,left.type);
  3070. END;
  3071. |Scanner.Div:
  3072. IF ir > 0 THEN
  3073. NewInteger(il DIV ir,left.type);
  3074. END;
  3075. |Scanner.Equal: NewBool(il=ir);
  3076. |Scanner.Unequal:NewBool(il#ir);
  3077. |Scanner.Less: NewBool(il<ir);
  3078. |Scanner.LessEqual: NewBool(il<=ir);
  3079. |Scanner.Greater: NewBool(il>ir);
  3080. |Scanner.GreaterEqual: NewBool(il>=ir);
  3081. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  3082. END;
  3083. ELS*)
  3084. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  3085. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  3086. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  3087. CASE operator OF
  3088. |Scanner.Plus: NewInteger(hl+hr,left.type);
  3089. |Scanner.Minus: NewInteger(hl-hr,left.type);
  3090. |Scanner.Times: NewInteger(hl*hr,left.type);
  3091. |Scanner.Slash:
  3092. IF hr = 0 THEN
  3093. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  3094. ELSE
  3095. IF type.sizeInBits = 64 THEN
  3096. NewReal(hl/hr,system.longrealType);
  3097. ELSE
  3098. NewReal(hl/hr,system.realType)
  3099. END
  3100. END;
  3101. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  3102. |Scanner.Mod:
  3103. IF hr = 0 THEN
  3104. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  3105. ELSE
  3106. NewInteger(hl MOD hr, left.type);
  3107. (* bootstrap64
  3108. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  3109. *)
  3110. END;
  3111. |Scanner.Div:
  3112. IF hr = 0 THEN
  3113. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  3114. ELSE
  3115. NewInteger(hl DIV hr, left.type);
  3116. (* bootstrap64
  3117. NewInteger(Machine.DivH(hl,hr),left.type);
  3118. *)
  3119. END;
  3120. (* *)
  3121. |Scanner.Equal: NewBool(hl=hr);
  3122. |Scanner.Unequal: NewBool(hl#hr);
  3123. |Scanner.Less: NewBool(hl<hr);
  3124. |Scanner.LessEqual: NewBool(hl<=hr);
  3125. |Scanner.Greater: NewBool(hl>hr);
  3126. |Scanner.GreaterEqual:NewBool(hl>=hr);
  3127. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  3128. END;
  3129. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3130. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3131. type := left.type
  3132. ELSIF (operator = Scanner.Slash) THEN
  3133. left := NewConversion(left.position,left,system.realType,NIL);
  3134. right := NewConversion(right.position,right,system.realType,NIL);
  3135. binaryExpression.SetLeft(left);
  3136. binaryExpression.SetRight(right);
  3137. type := system.realType
  3138. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3139. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3140. type := system.booleanType
  3141. ELSE
  3142. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  3143. END;
  3144. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3145. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3146. CASE operator OF
  3147. |Scanner.Plus: NewReal(rl+rr,leftType);
  3148. |Scanner.Minus: NewReal(rl-rr,leftType);
  3149. |Scanner.Times:NewReal(rl*rr,leftType);
  3150. |Scanner.Slash:
  3151. IF rr = 0 THEN
  3152. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  3153. ELSE
  3154. NewReal(rl/rr,leftType);
  3155. END
  3156. |Scanner.Equal: NewBool(rl=rr);
  3157. |Scanner.Unequal: NewBool(rl#rr);
  3158. |Scanner.Less: NewBool(rl<rr);
  3159. |Scanner.LessEqual: NewBool(rl<=rr);
  3160. |Scanner.Greater: NewBool(rl>rr);
  3161. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3162. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  3163. END;
  3164. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3165. type := left.type
  3166. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3167. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3168. type := system.booleanType
  3169. ELSE
  3170. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  3171. IF VerboseErrorMessage THEN
  3172. Printout.Info("left",left);
  3173. Printout.Info("right",right);
  3174. END;
  3175. END;
  3176. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3177. CASE operator OF
  3178. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3179. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3180. ELSE
  3181. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3182. IF VerboseErrorMessage THEN
  3183. Printout.Info("left", left);
  3184. Printout.Info("right", right)
  3185. END;
  3186. END;
  3187. IF ~error THEN
  3188. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3189. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3190. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3191. (* do constant folding *)
  3192. CASE operator OF
  3193. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3194. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3195. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3196. |Scanner.Slash:
  3197. divisor := c * c + d * d;
  3198. ASSERT(divisor # 0);
  3199. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3200. |Scanner.Equal: NewBool((a = c) & (b = d))
  3201. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3202. END
  3203. END
  3204. END
  3205. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3206. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3207. CASE operator OF
  3208. |Scanner.And: NewBool(bl & br);
  3209. |Scanner.Or: NewBool(bl OR br);
  3210. |Scanner.Equal: NewBool(bl = br);
  3211. |Scanner.Unequal: NewBool(bl # br);
  3212. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3213. END;
  3214. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3215. type := system.booleanType
  3216. ELSE
  3217. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3218. END;
  3219. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3220. (* constant folding *)
  3221. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3222. IF operator = Scanner.Equal THEN
  3223. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3224. ELSIF operator = Scanner.Unequal THEN
  3225. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3226. END;
  3227. END;
  3228. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3229. type := system.booleanType;
  3230. ELSE
  3231. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3232. END;
  3233. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3234. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3235. CASE operator OF
  3236. |Scanner.Plus: NewSet(sl + sr);
  3237. |Scanner.Minus: NewSet(sl - sr);
  3238. |Scanner.Times: NewSet(sl * sr);
  3239. |Scanner.Slash: NewSet(sl / sr);
  3240. |Scanner.Equal: NewBool(sl=sr);
  3241. |Scanner.Unequal: NewBool(sl#sr);
  3242. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3243. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3244. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3245. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3246. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3247. END;
  3248. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3249. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3250. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3251. THEN
  3252. type := system.booleanType
  3253. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3254. type := left.type
  3255. ELSE
  3256. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3257. END;
  3258. ELSIF IsCharacterType(left.type) THEN
  3259. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3260. CASE operator OF
  3261. |Scanner.Equal: NewBool(cl=cr);
  3262. |Scanner.Unequal: NewBool(cl#cr);
  3263. |Scanner.Less: NewBool(cl<cr);
  3264. |Scanner.LessEqual: NewBool(cl<=cr);
  3265. |Scanner.Greater: NewBool(cl>cr);
  3266. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3267. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3268. END;
  3269. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3270. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3271. type := system.booleanType
  3272. ELSE
  3273. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3274. END;
  3275. ELSE
  3276. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3277. END;
  3278. ELSE
  3279. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3280. END;
  3281. IF type = SyntaxTree.invalidType THEN
  3282. result := SyntaxTree.invalidExpression
  3283. ELSE
  3284. result.SetType(type)
  3285. END;
  3286. resolvedExpression := result
  3287. END VisitBinaryExpression;
  3288. (** resolve a range expression of the from <<first .. last BY step>>
  3289. - depending on the context different things are checked:
  3290. ArrayIndex:
  3291. - components must be integers
  3292. - replace missing lower bound with 0
  3293. - replace missing upper bound with MAX(LONGINT)
  3294. - replace missing step size with 1
  3295. SetElement:
  3296. - components must be integers
  3297. - replace missing lower bound with 0
  3298. - replace missing upper bound with MAX(SET)
  3299. - must not have step size
  3300. CaseGuard:
  3301. - components must be constant
  3302. - components must be integers or characters
  3303. - must have lower and upper bound present
  3304. - components are made compatible
  3305. - must not have step size
  3306. - if error: return invalidExpression
  3307. **)
  3308. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3309. VAR
  3310. hasError: BOOLEAN;
  3311. first, last, step: SyntaxTree.Expression;
  3312. BEGIN
  3313. hasError := FALSE;
  3314. first := x.first;
  3315. last := x.last;
  3316. step := x.step;
  3317. (* check lower bound *)
  3318. IF x.context = SyntaxTree.CaseGuard THEN
  3319. IF first = NIL THEN
  3320. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3321. hasError := TRUE
  3322. ELSE
  3323. first := ResolveExpression(first);
  3324. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3325. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3326. hasError := TRUE
  3327. ELSE
  3328. IF first IS SyntaxTree.StringValue THEN
  3329. (* add conversion from string to character *)
  3330. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3331. END
  3332. END;
  3333. (* check if expression is constant *)
  3334. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3335. (* error already reported *)
  3336. hasError := TRUE
  3337. END
  3338. END
  3339. ELSE (* ArrayIndex, SetElement *)
  3340. IF first = NIL THEN
  3341. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3342. END;
  3343. first := ResolveExpression(first);
  3344. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3345. first := NewConversion(first.position, first, system.longintType, NIL)
  3346. ELSE
  3347. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3348. hasError := TRUE
  3349. END
  3350. END;
  3351. (* check upper bound *)
  3352. IF x.context = SyntaxTree.CaseGuard THEN
  3353. IF last = NIL THEN
  3354. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3355. hasError := TRUE
  3356. ELSE
  3357. last := ResolveExpression(last);
  3358. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3359. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3360. hasError := TRUE
  3361. ELSE
  3362. IF last IS SyntaxTree.StringValue THEN
  3363. (* add conversion from string to character *)
  3364. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3365. END
  3366. END;
  3367. (* check if expression is constant *)
  3368. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3369. (* error already reported *)
  3370. hasError := TRUE
  3371. ELSE
  3372. (* try to make lower and upper bound compatible *)
  3373. ConvertOperands(first, last);
  3374. IF first.type.resolved # last.type.resolved THEN
  3375. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3376. hasError := TRUE
  3377. END
  3378. END
  3379. END
  3380. ELSE (* ArrayIndex, SetElement *)
  3381. IF last = NIL THEN
  3382. IF x.context = SyntaxTree.ArrayIndex THEN
  3383. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3384. ELSE
  3385. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3386. END
  3387. END;
  3388. last := ResolveExpression(last);
  3389. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3390. last := NewConversion(last.position, last, system.longintType, NIL)
  3391. ELSE
  3392. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3393. hasError := TRUE
  3394. END
  3395. END;
  3396. (* check step size *)
  3397. IF x.context = SyntaxTree.ArrayIndex THEN
  3398. IF step = NIL THEN
  3399. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3400. END;
  3401. step := ResolveExpression(step);
  3402. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3403. step := NewConversion(step.position, step, system.longintType, NIL)
  3404. ELSE
  3405. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3406. hasError := TRUE
  3407. END
  3408. ELSE (* SetElement, CaseGuard *)
  3409. IF step # NIL THEN
  3410. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3411. hasError := TRUE
  3412. END
  3413. END;
  3414. IF hasError THEN
  3415. resolvedExpression := SyntaxTree.invalidExpression
  3416. ELSE
  3417. x.SetFirst(first);
  3418. x.SetLast(last);
  3419. x.SetStep(step);
  3420. x.SetType(system.rangeType);
  3421. resolvedExpression := x;
  3422. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3423. END
  3424. END VisitRangeExpression;
  3425. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3426. BEGIN
  3427. x.SetType(NIL);
  3428. resolvedExpression := x;
  3429. END VisitTensorRangeExpression;
  3430. (** resolve the expression d and return result as designator
  3431. - resolve expression
  3432. - if expression is a designator then return designator else error message and return invalidDesignator
  3433. **)
  3434. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3435. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3436. BEGIN
  3437. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3438. resolved := ResolveExpression(d);
  3439. IF resolved = SyntaxTree.invalidExpression THEN
  3440. (* error should already have been reported *)
  3441. result := SyntaxTree.invalidDesignator;
  3442. ELSIF resolved IS SyntaxTree.Designator THEN
  3443. result := resolved(SyntaxTree.Designator);
  3444. ELSE
  3445. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3446. result := SyntaxTree.invalidDesignator;
  3447. END;
  3448. (* result.type might be nil. *)
  3449. RETURN result
  3450. END ResolveDesignator;
  3451. (**
  3452. symbol designator generated in this module
  3453. nothing to be resolved
  3454. **)
  3455. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3456. BEGIN
  3457. resolvedExpression := x;
  3458. END VisitSymbolDesignator;
  3459. (**
  3460. self designator generated in this module
  3461. nothing to be resolved
  3462. **)
  3463. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3464. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3465. BEGIN
  3466. (* check if in record scope *)
  3467. scope := currentScope;
  3468. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3469. scope := scope.outerScope;
  3470. END;
  3471. IF scope = NIL THEN (* in module scope *)
  3472. x.SetType(system.anyType);
  3473. ELSIF scope IS SyntaxTree.CellScope THEN
  3474. cell := scope(SyntaxTree.CellScope).ownerCell;
  3475. x.SetType(cell);
  3476. ELSE (* in record scope *)
  3477. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3478. IF (record # NIL) & (record.pointerType # NIL) THEN
  3479. type := ResolveType(record.pointerType);
  3480. x.SetType(type);
  3481. ELSE
  3482. x.SetType(record);
  3483. END;
  3484. END;
  3485. resolvedExpression := x;
  3486. END VisitSelfDesignator;
  3487. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3488. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3489. BEGIN
  3490. scope := currentScope;
  3491. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3492. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3493. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3494. returnType := procedureType.returnType;
  3495. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3496. THEN
  3497. x.SetType(returnType);
  3498. ELSE
  3499. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3500. x.SetType(SyntaxTree.invalidType);
  3501. END;
  3502. ELSE
  3503. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3504. x.SetType(SyntaxTree.invalidType);
  3505. END;
  3506. x.SetAssignable(TRUE);
  3507. resolvedExpression := x;
  3508. END VisitResultDesignator;
  3509. (**
  3510. return symbol designator as an expression
  3511. - if symbol is a constant then return the constant value expression
  3512. - else
  3513. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3514. - if symbol is a guarded variable then return a TypeGuardDesignator
  3515. - else return a symbol designator
  3516. **)
  3517. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3518. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3519. guardType: SyntaxTree.Type;
  3520. BEGIN
  3521. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3522. result := SyntaxTree.invalidExpression;
  3523. ASSERT(symbol # NIL);
  3524. (*
  3525. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3526. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3527. Error(position,Diagnostics.Invalid,"type not allowed here");
  3528. ELS *)
  3529. (* not needed any more as values are stored in the expression
  3530. IF symbol IS SyntaxTree.Constant THEN
  3531. result := symbol(SyntaxTree.Constant).value
  3532. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3533. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3534. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3535. ELSE
  3536. result := symbol(SyntaxTree.Constant).value
  3537. END;
  3538. ELSE
  3539. *)
  3540. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3541. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3542. THEN
  3543. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3544. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3545. left := NewDereferenceDesignator(position,left);
  3546. left.SetHidden(TRUE);
  3547. END;
  3548. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3549. scope := currentScope;
  3550. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3551. scope := scope.outerScope;
  3552. END;
  3553. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3554. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3555. END;
  3556. END;
  3557. 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);
  3558. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3559. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3560. ELSE
  3561. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3562. END;
  3563. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3564. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3565. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3566. result.SetType(symbol.type);
  3567. result.SetAssignable(assignable);
  3568. symbol.MarkUsed;
  3569. IF symbol IS SyntaxTree.Constant THEN
  3570. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3571. END;
  3572. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3573. variableAccessed := TRUE
  3574. END;
  3575. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3576. IF GetGuard(symbol,guardType) THEN
  3577. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3578. END;
  3579. END;
  3580. ASSERT(result.type # NIL);
  3581. RETURN result
  3582. END NewSymbolDesignator;
  3583. (** check and resolve an identifier designator "identifier"
  3584. - if identifier = self then return SelfDesignator
  3585. - else find symbol in current scope
  3586. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3587. **)
  3588. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3589. VAR symbol: SyntaxTree.Symbol;
  3590. BEGIN
  3591. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3592. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3593. IF symbol # NIL THEN
  3594. ResolveSymbol(symbol);
  3595. ASSERT(symbol.type # NIL);
  3596. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3597. ELSE
  3598. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3599. IF VerboseErrorMessage THEN
  3600. Printout.Info("undeclared identifier designator",identifierDesignator);
  3601. END;
  3602. resolvedExpression := SyntaxTree.invalidDesignator;
  3603. END;
  3604. END VisitIdentifierDesignator;
  3605. (** check and resolve a selector designator of the form left.designator
  3606. - if left is a pointer type then do auto dereferenciation
  3607. - left denotes a search scope:
  3608. - if left type is import type then set search scope to respective module
  3609. - if left type is enumeration type then set search scope to respective enumeration scope
  3610. - elsif left type is record type then set search scope to record scope
  3611. - search symbol in computed scope
  3612. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3613. **)
  3614. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3615. VAR
  3616. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3617. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3618. BEGIN
  3619. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3620. left := ResolveDesignator(selectorDesignator.left);
  3621. result := SyntaxTree.invalidDesignator;
  3622. IF left # NIL THEN
  3623. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3624. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3625. END;
  3626. scope := NIL;
  3627. IF left.type = NIL THEN
  3628. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3629. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3630. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3631. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3632. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3633. module := symbol(SyntaxTree.Import).module;
  3634. IF module # NIL THEN
  3635. scope := module.moduleScope
  3636. ELSE
  3637. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3638. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3639. END;
  3640. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3641. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3642. ASSERT(scope # NIL)
  3643. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3644. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3645. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3646. IF type IS SyntaxTree.EnumerationType THEN
  3647. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3648. ELSE
  3649. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3650. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3651. END;
  3652. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3653. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3654. ELSE
  3655. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3656. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3657. END;
  3658. symbol := NIL;
  3659. IF scope # NIL THEN
  3660. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3661. IF symbol # NIL THEN
  3662. ResolveSymbol(symbol);
  3663. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3664. symbol.MarkUsed
  3665. ELSE
  3666. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3667. IF VerboseErrorMessage THEN
  3668. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3669. Printout.Info("scope", scope);
  3670. Printout.Info("left", left);
  3671. Printout.Info("undeclared identifier",selectorDesignator);
  3672. Printout.Info("left resolved designator",left);
  3673. END
  3674. END;
  3675. END;
  3676. END;
  3677. resolvedExpression := result;
  3678. END VisitSelectorDesignator;
  3679. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3680. VAR len,idx: LONGINT;
  3681. BEGIN
  3682. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3683. IF idx < 0 THEN
  3684. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3685. ELSE
  3686. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3687. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3688. END;
  3689. END;
  3690. END;
  3691. END IndexCheck;
  3692. (*
  3693. - if index designator has not type, use newBaseType as its type
  3694. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3695. - special rule: if static array of dynamic array occurs, make it all dynamic
  3696. index designator type: new base type: new index designator type:
  3697. NIL z z
  3698. ARRAY [x, y] z ARRAY [x, y] OF z
  3699. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3700. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3701. *)
  3702. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3703. VAR
  3704. mathArrayType: SyntaxTree.MathArrayType;
  3705. makeDynamic: BOOLEAN;
  3706. BEGIN
  3707. IF indexDesignator.type = NIL THEN
  3708. indexDesignator.SetType(newBaseType)
  3709. ELSE
  3710. (* index designator must be a of math array type *)
  3711. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3712. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3713. (* determine if all arrays have to be made dynamic *)
  3714. makeDynamic :=
  3715. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3716. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3717. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3718. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3719. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3720. END;
  3721. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3722. mathArrayType.SetArrayBase(newBaseType)
  3723. END
  3724. END SetIndexBaseType;
  3725. (** check and append index list element to index designator of math array
  3726. - check validity of single index or array range
  3727. - compute new type
  3728. - if range then create new array type (calculate length of resulting array)
  3729. - otherwise take sourceArray.arrayBase as new type
  3730. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3731. **)
  3732. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3733. VAR
  3734. targetArray: SyntaxTree.MathArrayType;
  3735. first, last, step: SyntaxTree.Expression;
  3736. firstValue, lastValue, stepValue, length: LONGINT;
  3737. rangeExpression: SyntaxTree.RangeExpression;
  3738. BEGIN
  3739. IF indexListItem.type = SyntaxTree.invalidType THEN
  3740. (* error already handled *)
  3741. indexDesignator.parameters.AddExpression(indexListItem)
  3742. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3743. indexDesignator.HasRange;
  3744. indexDesignator.HasTensorRange;
  3745. indexDesignator.parameters.AddExpression(indexListItem);
  3746. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3747. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3748. IndexCheck(indexListItem, sourceArray.length);
  3749. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3750. indexDesignator.parameters.AddExpression(indexListItem)
  3751. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3752. indexDesignator.HasRange;
  3753. (* if the range is given as an array range expression, check the validity of its components *)
  3754. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3755. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3756. first := rangeExpression.first;
  3757. last := rangeExpression.last;
  3758. step := rangeExpression.step;
  3759. (* perform static checks on range components *)
  3760. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3761. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3762. END;
  3763. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3764. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3765. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3766. END
  3767. END;
  3768. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3769. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3770. END;
  3771. (* add conversions to size type *)
  3772. (* TODO: needed? *)
  3773. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3774. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3775. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3776. END;
  3777. IF indexDesignator.hasTensorRange THEN
  3778. (* the index designator's base type is a tensor: leave it as is *)
  3779. ELSE
  3780. (* append a new math array to the index designator's base type *)
  3781. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3782. IF ~error THEN
  3783. (*
  3784. (* optimization: calculate length of target array for static ranges *)
  3785. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3786. IF IsStaticallyOpenRange(rangeExpression) THEN
  3787. (* range is open ('*'): reuse source array length as target array length *)
  3788. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3789. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3790. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3791. IF lastValue = MAX(LONGINT) THEN
  3792. IF IsIntegerValue(sourceArray.length, length) THEN
  3793. lastValue := length - 1;
  3794. isStaticTargetArrayLength := TRUE
  3795. ELSE
  3796. isStaticTargetArrayLength := FALSE
  3797. END
  3798. ELSE
  3799. isStaticTargetArrayLength := TRUE
  3800. END;
  3801. IF isStaticTargetArrayLength THEN
  3802. (* calculate static target array length *)
  3803. IF firstValue > lastValue THEN
  3804. length := 0
  3805. ELSE
  3806. length := 1 + lastValue - firstValue;
  3807. IF length MOD stepValue = 0 THEN
  3808. length := length DIV stepValue
  3809. ELSE
  3810. length := length DIV stepValue + 1
  3811. END
  3812. END;
  3813. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3814. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3815. ASSERT(targetArray.form = SyntaxTree.Static)
  3816. END
  3817. END
  3818. END
  3819. *)
  3820. END;
  3821. SetIndexBaseType(indexDesignator, targetArray)
  3822. END;
  3823. indexDesignator.parameters.AddExpression(indexListItem)
  3824. ELSE
  3825. Error(position, Diagnostics.Invalid,"invalid index list item");
  3826. END;
  3827. END AppendMathIndex;
  3828. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3829. VAR parameters: SyntaxTree.ExpressionList;
  3830. BEGIN
  3831. parameters := index.parameters;
  3832. IF (expression.type = NIL) THEN
  3833. Error(position,Diagnostics.Invalid,"invalid index");
  3834. ELSIF IsIntegerType(expression.type.resolved) THEN
  3835. IF over IS SyntaxTree.ArrayType THEN
  3836. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3837. ELSIF over IS SyntaxTree.StringType THEN
  3838. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3839. END;
  3840. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3841. parameters.AddExpression(expression);
  3842. ELSE
  3843. Error(position,Diagnostics.Invalid,"invalid index");
  3844. END;
  3845. END AppendIndex;
  3846. (** convert an expression to math array type
  3847. if expression is of math array type: return expression itself
  3848. if expression is of array-structured object type: return an index operator call on it
  3849. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3850. otherwise: return invalid expression
  3851. **)
  3852. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3853. VAR
  3854. result: SyntaxTree.Expression;
  3855. mathArrayType: SyntaxTree.MathArrayType;
  3856. BEGIN
  3857. IF expression.type = NIL THEN
  3858. result := SyntaxTree.invalidExpression
  3859. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3860. (* expression of math array type *)
  3861. result := expression
  3862. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3863. (* expression of array-structured object type *)
  3864. mathArrayType := MathArrayStructureOfType(expression.type);
  3865. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3866. ELSE
  3867. result := SyntaxTree.invalidExpression
  3868. END;
  3869. RETURN result
  3870. END ConvertToMathArray;
  3871. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3872. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3873. VAR
  3874. result: SyntaxTree.ExpressionList;
  3875. i: LONGINT;
  3876. BEGIN
  3877. result := SyntaxTree.NewExpressionList();
  3878. FOR i := 1 TO itemCount DO
  3879. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3880. END;
  3881. RETURN result
  3882. END ListOfOpenRanges;
  3883. (** create a procedure call designator for an index operator call on an array-structured object type
  3884. - use given index list as actual parameters
  3885. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3886. **)
  3887. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3888. VAR
  3889. operator: SyntaxTree.Operator;
  3890. expression: SyntaxTree.Expression;
  3891. actualParameters, tempList: SyntaxTree.ExpressionList;
  3892. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3893. result, tempDesignator: SyntaxTree.Designator;
  3894. recordType: SyntaxTree.RecordType;
  3895. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3896. i, hashValue, indexListSize, indexListKind: LONGINT;
  3897. castReturnType: SyntaxTree.MathArrayType;
  3898. BEGIN
  3899. ASSERT(IsArrayStructuredObjectType(left.type));
  3900. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3901. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3902. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3903. indexListSize := indexList.Length();
  3904. indexListKind := 0;
  3905. containsNonRange := FALSE;
  3906. FOR i := 0 TO indexList.Length() - 1 DO
  3907. indexListKind := indexListKind * 2;
  3908. expression := indexList.GetExpression(i);
  3909. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3910. INC(indexListKind)
  3911. ELSE
  3912. containsNonRange := TRUE
  3913. END
  3914. END;
  3915. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3916. (* select applicable index operator
  3917. - try to look up optimal index operator
  3918. - if not present, use operator on ranges
  3919. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3920. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3921. *)
  3922. usesGeneralOperator := FALSE;
  3923. IF rhs # NIL THEN
  3924. (* write operator *)
  3925. IF hashValue = -1 THEN
  3926. operator := NIL
  3927. ELSE
  3928. operator := recordType.arrayAccessOperators.write[hashValue];
  3929. END;
  3930. IF operator = NIL THEN
  3931. usesPureRangeOperator := TRUE;
  3932. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3933. operator := recordType.arrayAccessOperators.generalWrite;
  3934. usesGeneralOperator := TRUE
  3935. ELSE
  3936. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3937. operator := recordType.arrayAccessOperators.write[hashValue];
  3938. END
  3939. END
  3940. ELSE
  3941. (* read operator *)
  3942. IF hashValue = -1 THEN
  3943. operator := NIL
  3944. ELSE
  3945. operator := recordType.arrayAccessOperators.read[hashValue];
  3946. END;
  3947. IF operator = NIL THEN
  3948. usesPureRangeOperator := TRUE;
  3949. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3950. operator := recordType.arrayAccessOperators.generalRead;
  3951. usesGeneralOperator := TRUE
  3952. ELSE
  3953. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3954. operator := recordType.arrayAccessOperators.read[hashValue];
  3955. END
  3956. END
  3957. END;
  3958. IF operator = NIL THEN
  3959. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3960. result := SyntaxTree.invalidDesignator;
  3961. ELSE
  3962. (* determine if reshaping is needed *)
  3963. needsReshaping := containsNonRange & usesPureRangeOperator;
  3964. (* import OCArrayBase if reshaping is needed *)
  3965. IF needsReshaping & ~arrayBaseImported THEN
  3966. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3967. arrayBaseImported := TRUE
  3968. END;
  3969. (* add the index list item to the list of actual parameters
  3970. - for general operators: add a single inline array containing the index list items as parameter
  3971. - otherwise: add all index list items as individual parameters
  3972. *)
  3973. actualParameters := SyntaxTree.NewExpressionList();
  3974. IF usesGeneralOperator THEN
  3975. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3976. END;
  3977. FOR i := 0 TO indexListSize - 1 DO
  3978. expression := indexList.GetExpression(i);
  3979. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3980. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3981. tempList := SyntaxTree.NewExpressionList();
  3982. tempList.AddExpression(expression);
  3983. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3984. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3985. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3986. END;
  3987. IF usesGeneralOperator THEN
  3988. tempMathArrayExpression.elements.AddExpression(expression);
  3989. ELSE
  3990. actualParameters.AddExpression(expression)
  3991. END
  3992. END;
  3993. IF usesGeneralOperator THEN
  3994. actualParameters.AddExpression(tempMathArrayExpression)
  3995. END;
  3996. IF rhs # NIL THEN
  3997. (* add actual parameter for RHS *)
  3998. IF needsReshaping THEN
  3999. (* reshape using OCArrayBase.ExpandDimensions *)
  4000. tempList := SyntaxTree.NewExpressionList();
  4001. (* source array *)
  4002. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  4003. tempList.AddExpression(rhs);
  4004. ELSE
  4005. (* convert scalar to one-dimensional array *)
  4006. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  4007. tempMathArrayExpression.elements.AddExpression(rhs);
  4008. tempList.AddExpression(tempMathArrayExpression)
  4009. END;
  4010. (* list of kept dimensions *)
  4011. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  4012. FOR i := 0 TO indexListSize - 1 DO
  4013. expression := indexList.GetExpression(i);
  4014. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  4015. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  4016. ELSE
  4017. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  4018. END
  4019. END;
  4020. tempList.AddExpression(tempMathArrayExpression);
  4021. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  4022. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  4023. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  4024. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  4025. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  4026. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  4027. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  4028. expression.SetType(castReturnType);
  4029. ELSE
  4030. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  4031. END;
  4032. actualParameters.AddExpression(expression)
  4033. ELSE
  4034. actualParameters.AddExpression(rhs)
  4035. END
  4036. END;
  4037. (* add dereference operator and create procedure call designator *)
  4038. ASSERT(left IS SyntaxTree.Designator);
  4039. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  4040. ASSERT(expression IS SyntaxTree.Designator);
  4041. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  4042. IF (rhs = NIL) & needsReshaping THEN
  4043. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  4044. tempList := SyntaxTree.NewExpressionList();
  4045. FOR i := 0 TO indexList.Length() - 1 DO
  4046. expression := indexList.GetExpression(i);
  4047. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  4048. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  4049. ELSE
  4050. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  4051. END
  4052. END;
  4053. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  4054. END;
  4055. IF rhs = NIL THEN
  4056. (* special rule: index read operator calls are considered to be assignable *)
  4057. result.SetAssignable(TRUE)
  4058. END;
  4059. (* put information about this index operator call into the resulting designator *)
  4060. result.SetRelatedAsot(left);
  4061. result.SetRelatedIndexList(indexList)
  4062. END;
  4063. RETURN result
  4064. END NewIndexOperatorCall;
  4065. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  4066. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  4067. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  4068. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  4069. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  4070. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  4071. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  4072. CONST trace = FALSE;
  4073. BEGIN
  4074. IF trace THEN
  4075. FOR i := 0 TO actualParameters.Length()-1 DO
  4076. Printout.Info("par", actualParameters.GetExpression(i));
  4077. END;
  4078. END;
  4079. operator := scope.firstOperator;
  4080. WHILE(operator # NIL) DO
  4081. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  4082. procedureType := operator.type(SyntaxTree.ProcedureType);
  4083. distance := Distance(system, procedureType,actualParameters);
  4084. IF trace THEN Printout.Info("check op ",operator) END;
  4085. IF distance < bestDistance THEN
  4086. IF trace THEN Printout.Info("taken op",operator) END;
  4087. bestDistance := distance;
  4088. bestOperator := operator;
  4089. END;
  4090. END;
  4091. operator := operator.nextOperator;
  4092. END;
  4093. END FindInScope;
  4094. BEGIN
  4095. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  4096. IF oper = 0 THEN (* index *)
  4097. identifier := SyntaxTree.NewIdentifier("[]");
  4098. ELSE
  4099. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  4100. END;
  4101. WHILE (recordType # NIL) DO
  4102. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  4103. recordType := recordType.GetBaseRecord();
  4104. END;
  4105. RETURN bestOperator
  4106. END FindOperator;
  4107. BEGIN
  4108. type := left.type.resolved;
  4109. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  4110. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4111. actualParameters := SyntaxTree.NewExpressionList();
  4112. IF parameters # NIL THEN
  4113. FOR i := 0 TO parameters.Length()-1 DO
  4114. expression := ResolveExpression(parameters.GetExpression(i));
  4115. actualParameters.AddExpression(expression);
  4116. END;
  4117. END;
  4118. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  4119. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4120. IF op # NIL THEN
  4121. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  4122. ASSERT(expression IS SyntaxTree.Designator);
  4123. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4124. result.SetRelatedAsot(left);
  4125. result.SetRelatedIndexList(parameters);
  4126. (* check if write operator exists, for var parameters *)
  4127. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4128. actualParameters := SyntaxTree.NewExpressionList();
  4129. FOR i := 0 TO parameters.Length()-1 DO
  4130. expression := ResolveExpression(parameters.GetExpression(i));
  4131. actualParameters.AddExpression(expression);
  4132. END;
  4133. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4134. actualParameters.AddExpression(rhs);
  4135. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4136. IF op = NIL THEN rhs := NIL END;
  4137. END;
  4138. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4139. ELSE
  4140. result := NIL;
  4141. END;
  4142. RETURN result;
  4143. END NewObjectOperatorCall;
  4144. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4145. 1. convert bracket designator chains into a single one that contains separators
  4146. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4147. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4148. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4149. - if an array or math array is indexed over, create index designator
  4150. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4151. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4152. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4153. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4154. - if an array-structured object type is indexed over, create procedure call designator
  4155. e.g.: a[x, y] -> a^."[]"(x, y)
  4156. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4157. - a[i, *] = a[i][*]
  4158. - a[*, i] # a[*][i]
  4159. Because:
  4160. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4161. - 'i-th column' = a[*, i]
  4162. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4163. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4164. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4165. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4166. **)
  4167. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  4168. VAR
  4169. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4170. indexDesignator: SyntaxTree.IndexDesignator;
  4171. designator: SyntaxTree.Designator;
  4172. type: SyntaxTree.Type;
  4173. recordType: SyntaxTree.RecordType;
  4174. expression, rhs: SyntaxTree.Expression;
  4175. indexList: SyntaxTree.ExpressionList;
  4176. i: LONGINT;
  4177. hasError, done: BOOLEAN;
  4178. PROCEDURE FinalizeIndexDesignator;
  4179. BEGIN
  4180. IF indexDesignator # NIL THEN
  4181. (* the end of a tensor has been reached: *)
  4182. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4183. SetIndexBaseType(indexDesignator, type);
  4184. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4185. designator := indexDesignator;
  4186. type := designator.type.resolved;
  4187. indexDesignator := NIL;
  4188. ASSERT(SyntaxTree.Resolved IN type.state)
  4189. END
  4190. END FinalizeIndexDesignator;
  4191. BEGIN
  4192. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4193. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4194. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4195. (* copy all index list entries including a separator to the left bracket designator *)
  4196. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4197. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4198. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4199. END;
  4200. (* propagate the related RHS *)
  4201. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4202. (* only resolve left bracket designator and use as final result *)
  4203. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4204. ELSE
  4205. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4206. designator := ResolveDesignator(bracketDesignator.left);
  4207. type := designator.type.resolved;
  4208. indexDesignator := NIL;
  4209. (*!!! clean up *)
  4210. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4211. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4212. IF resolvedExpression = NIL THEN
  4213. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4214. resolvedExpression := SyntaxTree.invalidDesignator
  4215. END;
  4216. RETURN;
  4217. END;
  4218. i := 0;
  4219. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4220. expression := bracketDesignator.parameters.GetExpression(i);
  4221. expression := ResolveExpression(expression);
  4222. bracketDesignator.parameters.SetExpression(i, expression);
  4223. IF expression = SyntaxTree.indexListSeparator THEN
  4224. (* finalize an existing index designator if needed *)
  4225. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4226. INC(i)
  4227. ELSE
  4228. (* do auto-dereferencing if needed *)
  4229. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4230. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4231. & (i=0)*)
  4232. THEN
  4233. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4234. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4235. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4236. designator := SyntaxTree.invalidDesignator;
  4237. type := SyntaxTree.invalidType
  4238. ELSE
  4239. FinalizeIndexDesignator;
  4240. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4241. type := designator.type.resolved
  4242. END
  4243. END;
  4244. (* create a new index designator, if needed *)
  4245. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4246. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4247. indexDesignator.SetAssignable(designator.assignable);
  4248. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4249. (* designator := indexDesignator *)
  4250. END;
  4251. IF type = SyntaxTree.invalidType THEN
  4252. (* error already handled *)
  4253. INC(i)
  4254. ELSIF type IS SyntaxTree.ArrayType THEN
  4255. (* indexing over an array *)
  4256. ASSERT(indexDesignator # NIL);
  4257. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4258. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4259. INC(i)
  4260. ELSIF type IS SyntaxTree.StringType THEN
  4261. (* indexing over an array *)
  4262. ASSERT(indexDesignator # NIL);
  4263. AppendIndex(expression.position, indexDesignator, expression, type);
  4264. type := type(SyntaxTree.StringType).baseType.resolved;
  4265. INC(i)
  4266. ELSIF type IS SyntaxTree.MathArrayType THEN
  4267. (* indexing over a math array *)
  4268. ASSERT(indexDesignator # NIL);
  4269. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4270. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4271. INC(i)
  4272. ELSIF IsArrayStructuredObjectType(type) THEN
  4273. (* indexing over ASOTs *)
  4274. FinalizeIndexDesignator;
  4275. ASSERT(type IS SyntaxTree.PointerType);
  4276. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4277. (*
  4278. - collect index list items from bracket designator that belong to ASOT
  4279. - check for errors
  4280. *)
  4281. indexList := SyntaxTree.NewExpressionList();
  4282. hasError := FALSE;
  4283. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4284. (* indexing over tensor ASOT:
  4285. - stop at index list end or separator
  4286. - dimensionality is given by number of index list items
  4287. *)
  4288. done := FALSE;
  4289. WHILE ~done DO
  4290. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4291. done := TRUE;
  4292. ELSE
  4293. expression := bracketDesignator.parameters.GetExpression(i);
  4294. IF expression = SyntaxTree.indexListSeparator THEN
  4295. done := TRUE;
  4296. ELSE
  4297. expression := ResolveExpression(expression);
  4298. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4299. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4300. hasError := TRUE
  4301. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4302. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4303. expression := SyntaxTree.invalidExpression;
  4304. hasError := TRUE
  4305. END;
  4306. indexList.AddExpression(expression)
  4307. END;
  4308. INC(i)
  4309. END
  4310. END
  4311. ELSE
  4312. (* indexing over non-tensor ASOT:
  4313. - ignore separators
  4314. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4315. *)
  4316. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4317. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4318. expression := bracketDesignator.parameters.GetExpression(i);
  4319. ELSE
  4320. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4321. END;
  4322. IF expression # SyntaxTree.indexListSeparator THEN
  4323. expression := ResolveExpression(expression);
  4324. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4325. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4326. expression := SyntaxTree.invalidExpression;
  4327. hasError := TRUE
  4328. END;
  4329. indexList.AddExpression(expression)
  4330. END;
  4331. INC(i)
  4332. END;
  4333. END;
  4334. IF hasError THEN
  4335. designator := SyntaxTree.invalidDesignator;
  4336. type := SyntaxTree.invalidType;
  4337. ELSE
  4338. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4339. and the last entry in the index list belongs to the array-structured object type in question.
  4340. E.g.: for a 2-dimensional array-structured object type:
  4341. - 'lhs := asot[1, 2]' -> read mode
  4342. - 'asot[1, 2] := rhs' -> write mode
  4343. - 'asot[1, 2, 3] := rhs' -> read mode
  4344. *)
  4345. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4346. rhs := bracketDesignator.relatedRhs
  4347. ELSE
  4348. rhs := NIL
  4349. END;
  4350. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4351. type := designator.type
  4352. END
  4353. ELSE
  4354. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4355. designator := SyntaxTree.invalidDesignator;
  4356. type := SyntaxTree.invalidType;
  4357. INC(i)
  4358. END
  4359. END
  4360. END;
  4361. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4362. resolvedExpression := designator
  4363. END
  4364. END VisitBracketDesignator;
  4365. (** check and resolve expression list
  4366. - resolve each expression in an expression list
  4367. - returns true if and only if all statements could have successfully been resolved
  4368. **)
  4369. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4370. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4371. BEGIN
  4372. result := TRUE;
  4373. FOR i := 0 TO expressionList.Length()-1 DO
  4374. expression := ResolveExpression(expressionList.GetExpression(i));
  4375. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4376. expressionList.SetExpression(i,expression);
  4377. END;
  4378. RETURN result
  4379. END ExpressionList;
  4380. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4381. BEGIN
  4382. type := type.resolved;
  4383. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4384. RETURN TRUE
  4385. ELSIF system.CanPassInRegister # NIL THEN
  4386. RETURN system.CanPassInRegister(type);
  4387. ELSE
  4388. RETURN FALSE
  4389. END;
  4390. END CanPassInRegister;
  4391. (** return procedure call designator left(actualParameters)
  4392. - check realtime procedure call in realtime procedure
  4393. - check number of parameters
  4394. - check parameter compatibility
  4395. return invalidDesignator if error
  4396. **)
  4397. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4398. VAR result: SyntaxTree.Designator;
  4399. numberFormalParameters, numberActualParameters: LONGINT;
  4400. formalType: SyntaxTree.ProcedureType;
  4401. formalParameter: SyntaxTree.Parameter;
  4402. actualParameter: SyntaxTree.Expression;
  4403. i: LONGINT;
  4404. BEGIN
  4405. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4406. result := SyntaxTree.invalidDesignator;
  4407. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4408. numberFormalParameters := formalType.numberParameters;
  4409. numberActualParameters := actualParameters.Length();
  4410. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4411. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4412. END;
  4413. IF ~ExpressionList(actualParameters) THEN
  4414. result := SyntaxTree.invalidDesignator
  4415. ELSE
  4416. IF numberActualParameters <= numberFormalParameters THEN
  4417. formalParameter := formalType.firstParameter;
  4418. FOR i := 0 TO numberActualParameters-1 DO
  4419. actualParameter := actualParameters.GetExpression(i);
  4420. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4421. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4422. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4423. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4424. ELSE
  4425. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4426. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4427. END;
  4428. actualParameters.SetExpression(i,actualParameter);
  4429. END;
  4430. formalParameter := formalParameter.nextParameter;
  4431. END;
  4432. WHILE (formalParameter # NIL) DO
  4433. IF formalParameter.defaultValue # NIL THEN
  4434. actualParameters.AddExpression(formalParameter.defaultValue);
  4435. formalParameter := formalParameter.nextParameter
  4436. ELSE
  4437. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4438. formalParameter := NIL;
  4439. END;
  4440. END;
  4441. ELSE
  4442. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4443. END;
  4444. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4445. result.SetAssignable(FALSE);
  4446. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4447. END;
  4448. RETURN result
  4449. END NewProcedureCallDesignator;
  4450. (**
  4451. builtin call designator generated in VisitParameterDesignator
  4452. -> nothing to be resolved
  4453. **)
  4454. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4455. BEGIN
  4456. resolvedExpression := x;
  4457. END VisitTypeGuardDesignator;
  4458. (**
  4459. builtin call designator generated in VisitParameterDesignator
  4460. -> nothing to be resolved
  4461. **)
  4462. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4463. BEGIN
  4464. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4465. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4466. ASSERT(resolvedExpression.type # NIL);
  4467. ELSIF ExpressionList(x.parameters) THEN
  4468. resolvedExpression := x;
  4469. END;
  4470. END VisitBuiltinCallDesignator;
  4471. (**
  4472. procedure call designator generated in VisitParameterDesignator
  4473. -> nothing to be resolved
  4474. **)
  4475. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4476. BEGIN
  4477. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4478. resolvedExpression := x;
  4479. END VisitProcedureCallDesignator;
  4480. (** return true if x is a variable else return false and report error **)
  4481. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4482. VAR result: BOOLEAN;
  4483. BEGIN
  4484. result := TRUE;
  4485. IF x = SyntaxTree.invalidExpression THEN
  4486. result := FALSE;
  4487. ELSIF ~IsVariable(x) THEN
  4488. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4489. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4490. result := FALSE;
  4491. END;
  4492. RETURN result
  4493. END CheckVariable;
  4494. (**
  4495. if expression x is of basic type then return true else report error and return false
  4496. **)
  4497. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4498. VAR result: BOOLEAN;
  4499. BEGIN
  4500. result := FALSE;
  4501. IF x = SyntaxTree.invalidExpression THEN
  4502. ELSIF ~IsBasicType(x.type) THEN
  4503. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4504. result := FALSE
  4505. ELSE result := TRUE
  4506. END;
  4507. RETURN result
  4508. END CheckBasicType;
  4509. (**
  4510. if expression x is of number type then return true else report error and return false
  4511. **)
  4512. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4513. VAR result: BOOLEAN;
  4514. BEGIN
  4515. result := FALSE;
  4516. IF x = SyntaxTree.invalidExpression THEN
  4517. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4518. Error(x.position,Diagnostics.Invalid,"is non number type");
  4519. ELSE result := TRUE
  4520. END;
  4521. RETURN result
  4522. END CheckNumberType;
  4523. (**
  4524. if expression x is of number or size type but not complex then return true else report error and return false
  4525. **)
  4526. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4527. VAR result: BOOLEAN;
  4528. BEGIN
  4529. result := FALSE;
  4530. IF x = SyntaxTree.invalidExpression THEN
  4531. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4532. Error(x.position,Diagnostics.Invalid,"is complex type");
  4533. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4534. Error(x.position,Diagnostics.Invalid,"is non number type");
  4535. ELSE result := TRUE
  4536. END;
  4537. RETURN result
  4538. END CheckNonComplexNumberSizeType;
  4539. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4540. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4541. BEGIN
  4542. result := FALSE; type := x.type.resolved;
  4543. IF x = SyntaxTree.invalidExpression THEN
  4544. 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
  4545. TRACE(type.sizeInBits);
  4546. TRACE(system.addressType.sizeInBits);
  4547. Error(x.position,Diagnostics.Invalid,"is no address type");
  4548. ELSE result := TRUE
  4549. END;
  4550. RETURN result
  4551. END CheckAddressType;
  4552. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4553. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4554. BEGIN
  4555. result := FALSE; type := x.type.resolved;
  4556. IF x = SyntaxTree.invalidExpression THEN
  4557. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4558. Error(x.position,Diagnostics.Invalid,"is no size type");
  4559. ELSE result := TRUE
  4560. END;
  4561. RETURN result
  4562. END CheckSizeType;
  4563. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4564. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4565. BEGIN
  4566. result := FALSE; type := x.type.resolved;
  4567. IF x = SyntaxTree.invalidExpression THEN
  4568. 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
  4569. Error(x.position,Diagnostics.Invalid,"is no object type");
  4570. ELSE result := TRUE
  4571. END;
  4572. RETURN result
  4573. END CheckObjectType;
  4574. (**
  4575. if expression x is of integer type then return true else report error and return false
  4576. **)
  4577. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4578. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4579. BEGIN
  4580. result := FALSE; type := x.type.resolved;
  4581. IF x = SyntaxTree.invalidExpression THEN
  4582. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4583. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4584. ELSE result := TRUE
  4585. END;
  4586. RETURN result
  4587. END CheckIntegerType;
  4588. (**
  4589. if expression x is of character type then return true else report error and return false
  4590. **)
  4591. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4592. VAR result: BOOLEAN;
  4593. BEGIN
  4594. result := FALSE;
  4595. IF x = SyntaxTree.invalidExpression THEN
  4596. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4597. Error(x.position,Diagnostics.Invalid,"is no character type");
  4598. ELSE result := TRUE
  4599. END;
  4600. RETURN result
  4601. END CheckCharacterType;
  4602. (**
  4603. if expression x is of real type then return true else report error and return false
  4604. **)
  4605. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4606. VAR result: BOOLEAN;
  4607. BEGIN
  4608. result := FALSE;
  4609. IF x = SyntaxTree.invalidExpression THEN
  4610. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4611. Error(x.position,Diagnostics.Invalid,"is no float type");
  4612. ELSE result := TRUE
  4613. END;
  4614. RETURN result
  4615. END CheckRealType;
  4616. (**
  4617. if expression x is of range type then return true else report error and return false
  4618. **)
  4619. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4620. VAR result: BOOLEAN;
  4621. BEGIN
  4622. result := FALSE;
  4623. IF x = SyntaxTree.invalidExpression THEN
  4624. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4625. Error(x.position,Diagnostics.Invalid,"is no range type");
  4626. ELSE result := TRUE
  4627. END;
  4628. RETURN result
  4629. END CheckRangeType;
  4630. (**
  4631. if expression x is of boolean type then return true else report error and return false
  4632. **)
  4633. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4634. VAR result: BOOLEAN;
  4635. BEGIN
  4636. result := FALSE;
  4637. IF x = SyntaxTree.invalidExpression THEN
  4638. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4639. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4640. ELSE result := TRUE
  4641. END;
  4642. RETURN result
  4643. END CheckBooleanType;
  4644. (**
  4645. if expression x is of set type then return true else report error and return false
  4646. **)
  4647. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4648. VAR result: BOOLEAN;
  4649. BEGIN
  4650. result := FALSE;
  4651. IF x = SyntaxTree.invalidExpression THEN
  4652. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4653. Error(x.position,Diagnostics.Invalid,"is no set type");
  4654. ELSE result := TRUE
  4655. END;
  4656. RETURN result
  4657. END CheckSetType;
  4658. (**
  4659. if expression x is of string or array of character type then return true else report error and return false
  4660. **)
  4661. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4662. VAR result: BOOLEAN;
  4663. BEGIN
  4664. result := FALSE;
  4665. IF x = SyntaxTree.invalidExpression THEN
  4666. ELSIF ~IsStringType(x.type.resolved) THEN
  4667. Error(x.position,Diagnostics.Invalid,"is no string type");
  4668. ELSE result := TRUE
  4669. END;
  4670. RETURN result
  4671. END CheckStringType;
  4672. (**
  4673. if expression x is a type declaration type return true else report error and return false
  4674. **)
  4675. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4676. VAR result: BOOLEAN;
  4677. BEGIN
  4678. result := FALSE;
  4679. IF x = SyntaxTree.invalidExpression THEN
  4680. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4681. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4682. ELSE result := TRUE
  4683. END;
  4684. RETURN result
  4685. END CheckTypeDeclarationType;
  4686. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4687. VAR result: BOOLEAN;
  4688. BEGIN
  4689. result := FALSE;
  4690. IF x = SyntaxTree.invalidExpression THEN
  4691. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4692. result := TRUE;
  4693. value := x.resolved(SyntaxTree.IntegerValue).value;
  4694. ELSE
  4695. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4696. END;
  4697. RETURN result;
  4698. END CheckIntegerValue;
  4699. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4700. VAR result: BOOLEAN;
  4701. BEGIN
  4702. result := FALSE;
  4703. IF x = SyntaxTree.invalidExpression THEN
  4704. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4705. result := TRUE;
  4706. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4707. ELSE
  4708. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4709. END;
  4710. RETURN result;
  4711. END CheckStringValue;
  4712. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4713. BEGIN
  4714. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4715. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4716. ELSE
  4717. RETURN FALSE
  4718. END;
  4719. END IsUnsignedValue;
  4720. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4721. BEGIN
  4722. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4723. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4724. ELSE
  4725. RETURN FALSE
  4726. END
  4727. END IsAddressValue;
  4728. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4729. BEGIN
  4730. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4731. END IsAddressExpression;
  4732. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4733. BEGIN
  4734. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4735. END IsSizeExpression;
  4736. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4737. VAR result: BOOLEAN;
  4738. BEGIN
  4739. result := FALSE;
  4740. IF x = SyntaxTree.invalidExpression THEN
  4741. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4742. result := TRUE;
  4743. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4744. ELSE
  4745. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4746. END;
  4747. RETURN result;
  4748. END CheckEnumerationValue;
  4749. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4750. VAR result: BOOLEAN;
  4751. BEGIN
  4752. result := FALSE;
  4753. IF x = SyntaxTree.invalidExpression THEN
  4754. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4755. result := TRUE;
  4756. value := x.resolved(SyntaxTree.CharacterValue).value;
  4757. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4758. result := TRUE;
  4759. value := x.resolved(SyntaxTree.StringValue).value[0];
  4760. ELSE
  4761. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4762. END;
  4763. RETURN result;
  4764. END CheckCharacterValue;
  4765. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4766. VAR result: BOOLEAN;
  4767. BEGIN
  4768. result := FALSE;
  4769. IF x = SyntaxTree.invalidExpression THEN
  4770. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4771. value := x.resolved(SyntaxTree.IntegerValue).value;
  4772. IF (value > 0) OR includeZero & (value = 0) THEN
  4773. result := TRUE;
  4774. ELSE
  4775. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4776. END
  4777. ELSE
  4778. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4779. END;
  4780. RETURN result;
  4781. END CheckPositiveIntegerValue;
  4782. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4783. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4784. BEGIN
  4785. result := FALSE;
  4786. IF x = SyntaxTree.invalidExpression THEN
  4787. ELSE
  4788. type := x.type.resolved;
  4789. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4790. portType := type(SyntaxTree.PortType);
  4791. result := TRUE
  4792. ELSE
  4793. Error(x.position,Diagnostics.Invalid,"no port type");
  4794. END;
  4795. END;
  4796. RETURN result
  4797. END CheckPortType;
  4798. (* move to builtin procedure call statement ?
  4799. remove builtin procedure call designator ?
  4800. *)
  4801. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4802. VAR
  4803. numberActualParameters,numberFormalParameters: LONGINT;
  4804. formalParameter: SyntaxTree.Parameter;
  4805. actualParameter: SyntaxTree.Expression;
  4806. procedureType: SyntaxTree.ProcedureType;
  4807. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4808. inPort, outPort: SyntaxTree.PortType;
  4809. constructor: SyntaxTree.Procedure;
  4810. type0,type1,type2: SyntaxTree.Type;
  4811. type,base,parameterType: SyntaxTree.Type;
  4812. arrayType: SyntaxTree.ArrayType;
  4813. i,i0,i1: LONGINT;
  4814. r,r0,r1,im: LONGREAL;
  4815. c: CHAR;
  4816. id: LONGINT;
  4817. b: BOOLEAN;
  4818. first: LONGINT;
  4819. mathArrayType: SyntaxTree.MathArrayType;
  4820. customBuiltin: SyntaxTree.CustomBuiltin;
  4821. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4822. VAR resultB: BOOLEAN;
  4823. BEGIN
  4824. IF numberActualParameters < from THEN
  4825. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4826. result := SyntaxTree.invalidExpression;
  4827. resultB := FALSE;
  4828. ELSIF numberActualParameters > to THEN
  4829. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4830. result := SyntaxTree.invalidExpression;
  4831. resultB := FALSE;
  4832. ELSE
  4833. resultB := TRUE;
  4834. END;
  4835. RETURN resultB
  4836. END CheckArity;
  4837. BEGIN
  4838. type := NIL; result := NIL;
  4839. type0 := NIL; type1 := NIL; type2 := NIL;
  4840. numberActualParameters := actualParameters.Length();
  4841. IF numberActualParameters>0 THEN
  4842. parameter0 := actualParameters.GetExpression(0);
  4843. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4844. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4845. result := SyntaxTree.invalidExpression
  4846. END
  4847. END;
  4848. IF numberActualParameters >1 THEN
  4849. parameter1 := actualParameters.GetExpression(1);
  4850. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4851. ELSE
  4852. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4853. result := SyntaxTree.invalidExpression
  4854. END
  4855. END;
  4856. IF numberActualParameters >2 THEN
  4857. parameter2 := actualParameters.GetExpression(2);
  4858. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4859. ELSE
  4860. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4861. result := SyntaxTree.invalidExpression
  4862. END
  4863. END;
  4864. IF returnType # NIL THEN
  4865. id := Global.New;
  4866. result := NIL;
  4867. ELSE
  4868. id := builtin.id;
  4869. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4870. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4871. END;
  4872. END;
  4873. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4874. ELSIF result # NIL THEN type := result.type (* operator *)
  4875. ELSE
  4876. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4877. result(SyntaxTree.Designator).SetLeft(left);
  4878. IF returnType # NIL THEN
  4879. type := returnType;
  4880. END;
  4881. (* ---- ASSERT ----- *)
  4882. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4883. IF CheckBooleanType(parameter0) THEN
  4884. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4885. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4886. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4887. *)
  4888. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4889. Error(position,Diagnostics.Invalid,"assert failed");
  4890. END;
  4891. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4892. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4893. rules imposed by the architecture / current runtime
  4894. *)
  4895. END;
  4896. END;
  4897. (* ---- COPY ----- *)
  4898. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4899. IF~IsStringType(type0) THEN
  4900. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4901. END;
  4902. IF ~IsStringType(type1) THEN
  4903. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4904. ELSIF CheckVariable(parameter1) THEN
  4905. IF (type0 IS SyntaxTree.StringType) THEN
  4906. arrayType := type1(SyntaxTree.ArrayType);
  4907. IF arrayType.form = SyntaxTree.Static THEN
  4908. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4909. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4910. END;
  4911. END;
  4912. END;
  4913. END;
  4914. (* ---- INC, DEC----- *)
  4915. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4916. IF numberActualParameters = 1 THEN
  4917. parameter1 :=Global.NewIntegerValue(system,position,1);
  4918. actualParameters.AddExpression(parameter1);
  4919. END;
  4920. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4921. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4922. Error(position,Diagnostics.Invalid,"incompatible increment");
  4923. ELSE
  4924. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4925. actualParameters.SetExpression(1,parameter1);
  4926. END;
  4927. END;
  4928. (* ---- EXCL, INCL----- *)
  4929. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4930. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4931. IF IsIntegerValue(parameter1,i0) THEN
  4932. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4933. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4934. END;
  4935. END;
  4936. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4937. actualParameters.SetExpression(1,parameter1);
  4938. END;
  4939. (* ---- HALT, SYSTEM.HALT ----- *)
  4940. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4941. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4942. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4943. rules imposed by the architecture / current runtime
  4944. *)
  4945. END;
  4946. (* ---- WAIT ----- *)
  4947. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4948. IF CheckObjectType(parameter0) THEN
  4949. END;
  4950. (* ---- NEW ----- *)
  4951. ELSIF (id = Global.New) THEN
  4952. IF returnType # NIL THEN
  4953. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4954. ELSE
  4955. first := 1;
  4956. END;
  4957. IF CheckArity(first,Infinity) THEN
  4958. IF currentIsRealtime THEN
  4959. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4960. END;
  4961. (* check constructor *)
  4962. IF (first =0) OR CheckVariable(parameter0) THEN
  4963. IF type0 IS SyntaxTree.PointerType THEN
  4964. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4965. END;
  4966. IF type0 IS SyntaxTree.ArrayType THEN
  4967. arrayType := type0(SyntaxTree.ArrayType);
  4968. IF arrayType.form = SyntaxTree.Static THEN
  4969. i := first
  4970. ELSIF arrayType.form = SyntaxTree.Open THEN
  4971. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4972. ELSE HALT(100)
  4973. END;
  4974. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4975. i := first;
  4976. REPEAT
  4977. actualParameter := actualParameters.GetExpression(i);
  4978. IF CheckSizeType(actualParameter) THEN
  4979. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4980. actualParameters.SetExpression(i,actualParameter);
  4981. END;
  4982. INC(i);
  4983. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4984. END;
  4985. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4986. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4987. IF constructor = NIL THEN
  4988. IF CheckArity(first,first) THEN END;
  4989. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4990. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4991. ELSE
  4992. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4993. numberFormalParameters := procedureType.numberParameters;
  4994. IF numberActualParameters-first <= numberFormalParameters THEN
  4995. formalParameter := procedureType.firstParameter;
  4996. FOR i := first TO numberActualParameters-1 DO
  4997. actualParameter := actualParameters.GetExpression(i);
  4998. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4999. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5000. ELSE
  5001. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5002. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5003. END;
  5004. actualParameters.SetExpression(i,actualParameter);
  5005. END;
  5006. formalParameter := formalParameter.nextParameter;
  5007. END;
  5008. WHILE (formalParameter # NIL) DO
  5009. IF formalParameter.defaultValue # NIL THEN
  5010. actualParameters.AddExpression(formalParameter.defaultValue);
  5011. formalParameter := formalParameter.nextParameter
  5012. ELSE
  5013. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  5014. formalParameter := NIL;
  5015. END;
  5016. END;
  5017. ELSE
  5018. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  5019. END;
  5020. END;
  5021. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  5022. mathArrayType := type0(SyntaxTree.MathArrayType);
  5023. IF mathArrayType.form = SyntaxTree.Static THEN
  5024. Error(position,Diagnostics.Invalid,"new on static array");
  5025. ELSE
  5026. IF mathArrayType.form = SyntaxTree.Tensor THEN
  5027. i0 := first+1; i1 := Infinity;
  5028. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  5029. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  5030. i1 := i0;
  5031. ELSE HALT(100);
  5032. END;
  5033. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  5034. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5035. base := ArrayBase(type0,MAX(LONGINT));
  5036. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5037. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5038. IF ~CompatibleTo(system,type0,parameterType) THEN
  5039. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  5040. result := SyntaxTree.invalidExpression;
  5041. ELSE
  5042. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5043. END;
  5044. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5045. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5046. IF ~CompatibleTo(system,type1,parameterType) THEN
  5047. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5048. result := SyntaxTree.invalidExpression;
  5049. ELSE
  5050. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5051. END;
  5052. ELSE
  5053. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  5054. i := first;
  5055. REPEAT
  5056. actualParameter := actualParameters.GetExpression(i);
  5057. IF CheckSizeType(actualParameter) THEN
  5058. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  5059. actualParameters.SetExpression(i,actualParameter);
  5060. END;
  5061. INC(i);
  5062. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  5063. END;
  5064. END;
  5065. END;
  5066. ELSIF type0 IS SyntaxTree.CellType THEN
  5067. IF ~(currentIsCellNet) THEN
  5068. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  5069. ELSE
  5070. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  5071. IF (constructor = NIL) & CheckArity(1,1) THEN
  5072. (* ok *)
  5073. ELSE
  5074. procedureType := constructor.type(SyntaxTree.ProcedureType);
  5075. numberFormalParameters := procedureType.numberParameters;
  5076. DEC(numberActualParameters);
  5077. IF numberActualParameters <= numberFormalParameters THEN
  5078. formalParameter := procedureType.firstParameter;
  5079. FOR i := first TO numberActualParameters DO
  5080. actualParameter := actualParameters.GetExpression(i);
  5081. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5082. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5083. ELSE
  5084. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5085. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5086. END;
  5087. actualParameters.SetExpression(i,actualParameter);
  5088. END;
  5089. formalParameter := formalParameter.nextParameter;
  5090. END;
  5091. WHILE (formalParameter # NIL) DO
  5092. IF formalParameter.defaultValue # NIL THEN
  5093. actualParameters.AddExpression(formalParameter.defaultValue);
  5094. formalParameter := formalParameter.nextParameter
  5095. ELSE
  5096. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  5097. formalParameter := NIL;
  5098. END;
  5099. END;
  5100. ELSE
  5101. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  5102. END;
  5103. END;
  5104. END;
  5105. activeCellsStatement := TRUE;
  5106. ELSE
  5107. Error(position,Diagnostics.Invalid,"cannot be allocated");
  5108. END;
  5109. END;
  5110. END;
  5111. (* ---- DISPOSE ----- *)
  5112. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5113. IF ~IsPointerType(parameter0.type) THEN
  5114. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5115. ELSIF ~IsDisposable(parameter0.type) THEN
  5116. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  5117. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5118. END
  5119. (* ---- GETPROCEDURE ----- *)
  5120. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5121. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5122. IF CheckVariable(parameter2) THEN
  5123. IF ~GetProcedureAllowed(parameter2.type) THEN
  5124. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  5125. END;
  5126. END;
  5127. END;
  5128. (* ---- ABS ----- *)
  5129. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5130. (* note: ABS on complex numbers is done using overloading *)
  5131. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5132. type := type0;
  5133. IF IsIntegerValue(parameter0,i0) THEN
  5134. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5135. type := Global.GetIntegerType(system,ABS(i0));
  5136. ELSIF IsRealValue(parameter0,r) THEN
  5137. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5138. END;
  5139. ELSE
  5140. type := SyntaxTree.invalidType;
  5141. END;
  5142. (* ---- ASH, ASR ----- *)
  5143. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5144. type := type0;
  5145. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5146. (*
  5147. ConvertOperands(parameter0,parameter1); (* same type *)
  5148. *)
  5149. type := parameter0.type;
  5150. IF IsIntegerValue(parameter0,i0) THEN
  5151. IF IsIntegerValue(parameter1,i1) THEN
  5152. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5153. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5154. result := ResolveExpression(result);
  5155. type := Global.GetIntegerType(system,i0);
  5156. END;
  5157. END;
  5158. IF type.resolved.sizeInBits < 32 THEN
  5159. type := system.longintType;
  5160. END;
  5161. (*!compatibility with release, remove when resolved
  5162. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5163. *)
  5164. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5165. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5166. actualParameters.SetExpression(0,parameter0);
  5167. actualParameters.SetExpression(1,parameter1);
  5168. END;
  5169. (* ---- CAP ----- *)
  5170. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5171. type := system.characterType;
  5172. IF CheckCharacterType (parameter0) THEN
  5173. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5174. actualParameters.SetExpression(0,parameter0);
  5175. IF IsCharacterValue(parameter0,c) THEN
  5176. IF (c <= "z") & (c >= "a") THEN
  5177. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5178. ELSE
  5179. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5180. END;
  5181. END;
  5182. END;
  5183. (* ---- CHR ----- *)
  5184. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5185. IF id = Global.Chr THEN
  5186. type := system.characterType
  5187. ELSE
  5188. type := system.characterType32
  5189. END;
  5190. IF CheckIntegerType(parameter0) THEN
  5191. IF IsIntegerValue(parameter0,i0) THEN
  5192. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5193. result := ResolveExpression(result);
  5194. ELSE
  5195. (*
  5196. result := NewConversion(parameter0.position,parameter0,type);
  5197. *)
  5198. END;
  5199. END
  5200. (* ---- ENTIER ----- *)
  5201. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5202. type := system.longintType;
  5203. IF CheckRealType(parameter0) THEN
  5204. IF IsRealValue(parameter0,r) THEN
  5205. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5206. type := Global.GetIntegerType(system,ENTIER(r));
  5207. END
  5208. END;
  5209. (* ---- ENTIERH ----- *)
  5210. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5211. type := system.hugeintType;
  5212. IF CheckRealType(parameter0) THEN
  5213. IF IsRealValue(parameter0,r) THEN
  5214. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5215. END
  5216. END;
  5217. (* ---- LEN ----- *)
  5218. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5219. type := system.longintType;
  5220. base := type0;
  5221. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5222. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5223. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5224. END;
  5225. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5226. actualParameters.SetExpression(0,parameter0);
  5227. type0 := parameter0.type.resolved;
  5228. base := type0;
  5229. END;
  5230. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5231. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5232. IF i1 < 0 THEN
  5233. Error(position,Diagnostics.Invalid,"invalid dimension");
  5234. base := SyntaxTree.invalidType;
  5235. ELSE
  5236. base := ArrayBase(base,i1);
  5237. IF (base # NIL) & Indexable(base) THEN
  5238. ELSE
  5239. Error(position,Diagnostics.Invalid,"len on no array");
  5240. IF VerboseErrorMessage THEN
  5241. Printout.Info("base",base);
  5242. END;
  5243. base := SyntaxTree.invalidType;
  5244. END;
  5245. END;
  5246. IF numberActualParameters=2 THEN
  5247. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5248. actualParameters.SetExpression(1,parameter1);
  5249. ELSIF base IS SyntaxTree.MathArrayType THEN
  5250. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5251. END;
  5252. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5253. IF base IS SyntaxTree.ArrayType THEN
  5254. arrayType := base(SyntaxTree.ArrayType);
  5255. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5256. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5257. result := Global.NewIntegerValue(system,position,i);
  5258. type := result.type;(* arrayType.length.type;*)
  5259. ASSERT(type # NIL);
  5260. END;
  5261. ELSIF base IS SyntaxTree.MathArrayType THEN
  5262. mathArrayType := base(SyntaxTree.MathArrayType);
  5263. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5264. result := Global.NewIntegerValue(system,position,i);
  5265. type := result.type;
  5266. (*
  5267. type := mathArrayType.length.type;
  5268. *)
  5269. ASSERT(type # NIL);
  5270. END;
  5271. END;
  5272. END;
  5273. ELSE
  5274. type := system.longintType;
  5275. END;
  5276. (* ---- FIRST ---- *)
  5277. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5278. type := system.longintType;
  5279. IF CheckRangeType(parameter0) THEN END;
  5280. result.SetAssignable(parameter0.assignable)
  5281. (* ---- LAST ---- *)
  5282. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5283. type := system.longintType;
  5284. IF CheckRangeType(parameter0) THEN END;
  5285. result.SetAssignable(parameter0.assignable)
  5286. (* ---- STEP ---- *)
  5287. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5288. type := system.longintType;
  5289. IF CheckRangeType(parameter0) THEN END;
  5290. result.SetAssignable(parameter0.assignable)
  5291. (* ---- RE ---- *)
  5292. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5293. IF CheckNumberType(parameter0) THEN
  5294. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5295. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5296. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5297. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5298. type := parameter0.type
  5299. ELSE
  5300. type := system.realType
  5301. END
  5302. END;
  5303. result.SetAssignable(parameter0.assignable)
  5304. (* ---- IM ---- *)
  5305. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5306. IF CheckNumberType(parameter0) THEN
  5307. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5308. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5309. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5310. ELSE
  5311. type := system.realType;
  5312. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5313. END
  5314. END;
  5315. result.SetAssignable(parameter0.assignable)
  5316. (* ---- MAX ----- *)
  5317. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5318. IF numberActualParameters = 1 THEN
  5319. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5320. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5321. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5322. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5323. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5324. *)
  5325. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5326. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5327. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5328. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5329. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5330. END;
  5331. ELSE
  5332. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5333. END
  5334. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5335. ConvertOperands(parameter0,parameter1);
  5336. actualParameters.SetExpression(0,parameter0);
  5337. actualParameters.SetExpression(1,parameter1);
  5338. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5339. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5340. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5341. END;
  5342. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5343. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5344. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5345. END;
  5346. END;
  5347. type := parameter0.type;
  5348. ELSE type := SyntaxTree.invalidType;
  5349. END;
  5350. (* ---- MIN ----- *)
  5351. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5352. IF numberActualParameters = 1 THEN
  5353. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5354. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5355. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5356. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5357. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5358. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5359. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5360. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5361. END;
  5362. ELSE
  5363. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5364. END
  5365. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5366. ConvertOperands(parameter0,parameter1);
  5367. actualParameters.SetExpression(0,parameter0);
  5368. actualParameters.SetExpression(1,parameter1);
  5369. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5370. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5371. ELSE result.SetResolved(parameter1.resolved)
  5372. END;
  5373. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5374. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5375. ELSE result.SetResolved(parameter1.resolved)
  5376. END;
  5377. END;
  5378. type := parameter0.type;
  5379. ELSE type := SyntaxTree.invalidType;
  5380. END;
  5381. (* ---- ODD ----- *)
  5382. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5383. type := system.booleanType;
  5384. IF CheckIntegerType(parameter0) THEN
  5385. IF IsIntegerValue(parameter0,i0) THEN
  5386. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5387. type := system.booleanType;
  5388. END;
  5389. END;
  5390. (* ---- ORD ----- *)
  5391. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5392. IF id = Global.Ord THEN
  5393. type := system.integerType;
  5394. ELSE
  5395. type := system.longintType;
  5396. END;
  5397. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5398. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5399. actualParameters.SetExpression(0,parameter0);
  5400. (* IF CheckCharacterType(parameter0) THEN*)
  5401. IF IsCharacterValue(parameter0,c)THEN
  5402. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5403. type := Global.GetSignedIntegerType(system,ORD(c));
  5404. END;
  5405. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5406. END;
  5407. (* ---- SHORT ----- *)
  5408. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5409. type := type0;
  5410. IF IsSignedIntegerType(type) THEN
  5411. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5412. ELSIF type = system.integerType THEN type := system.shortintType
  5413. ELSIF type = system.longintType THEN type := system.integerType
  5414. ELSIF type = system.hugeintType THEN type:= system.longintType
  5415. ELSE
  5416. CASE type.sizeInBits OF
  5417. 16: type := Global.Integer8
  5418. |32: type := Global.Integer16
  5419. |64: type := Global.Integer32
  5420. END;
  5421. END;
  5422. ELSIF type IS SyntaxTree.FloatType THEN
  5423. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5424. ELSIF type = system.longrealType THEN type := system.realType
  5425. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5426. END;
  5427. ELSIF type IS SyntaxTree.ComplexType THEN
  5428. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5429. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5430. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5431. END;
  5432. ELSE
  5433. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5434. END;
  5435. IF (parameter0.resolved # NIL) THEN
  5436. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5437. IF parameter0 IS SyntaxTree.Value THEN
  5438. result.SetResolved(parameter0(SyntaxTree.Value));
  5439. END;
  5440. END;
  5441. (* ---- LONG ----- *)
  5442. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5443. type := type0;
  5444. IF IsSignedIntegerType(type) THEN
  5445. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5446. ELSIF type = system.longintType THEN type := system.hugeintType
  5447. ELSIF type = system.integerType THEN type := system.longintType
  5448. ELSIF type = system.shortintType THEN type := system.integerType
  5449. ELSE
  5450. CASE type.sizeInBits OF
  5451. 8: type := Global.Integer16
  5452. |16: type := Global.Integer32
  5453. |32: type := Global.Integer64
  5454. END;
  5455. END;
  5456. ELSIF type IS SyntaxTree.FloatType THEN
  5457. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5458. ELSIF type= system.realType THEN type := system.longrealType
  5459. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5460. END;
  5461. ELSIF type IS SyntaxTree.ComplexType THEN
  5462. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5463. ELSIF type = system.complexType THEN type := system.longcomplexType
  5464. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5465. END;
  5466. ELSE
  5467. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5468. END;
  5469. IF (parameter0.resolved # NIL) THEN
  5470. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5471. IF parameter0 IS SyntaxTree.Value THEN
  5472. result.SetResolved(parameter0(SyntaxTree.Value));
  5473. END;
  5474. END;
  5475. (* ---- SIZE OF ----- *)
  5476. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5477. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5478. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5479. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5480. type := system.integerType;
  5481. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5482. ELSE
  5483. (* for variables, system sizeof could represent the physically occupied size
  5484. determined via the type descriptor, implement that ? *)
  5485. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5486. END
  5487. (* ---- SYSTEM.TRACE -----*)
  5488. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5489. FOR i := 0 TO numberActualParameters-1 DO
  5490. parameter0 := actualParameters.GetExpression(i);
  5491. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5492. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5493. END;
  5494. END;
  5495. (* remaining issues can only be tested in backend *)
  5496. (* ---- ADDRESSOF----- *)
  5497. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5498. IF HasAddress(parameter0) THEN
  5499. type := system.addressType;
  5500. ELSE
  5501. type := SyntaxTree.invalidType;
  5502. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5503. END;
  5504. (* ---- BIT ----- *)
  5505. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5506. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5507. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5508. actualParameters.SetExpression(0,parameter0);
  5509. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5510. actualParameters.SetExpression(1,parameter1);
  5511. END;
  5512. type := system.booleanType;
  5513. (* ----- MSK ---- *)
  5514. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5515. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5516. ConvertOperands(parameter0,parameter1);
  5517. actualParameters.SetExpression(0,parameter0);
  5518. actualParameters.SetExpression(1,parameter1);
  5519. END;
  5520. type := parameter0.type;
  5521. (* ---- SYSTEM.GET64 ----- *)
  5522. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5523. IF CheckAddressType(parameter0) THEN
  5524. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5525. actualParameters.SetExpression(0,parameter0);
  5526. END;
  5527. type := system.hugeintType;
  5528. (* ---- SYSTEM.GET32 ----- *)
  5529. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5530. IF CheckAddressType(parameter0) THEN
  5531. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5532. actualParameters.SetExpression(0,parameter0);
  5533. END;
  5534. type := system.longintType;
  5535. (* ---- SYSTEM.GET16 ----- *)
  5536. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5537. IF CheckAddressType(parameter0) THEN
  5538. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5539. actualParameters.SetExpression(0,parameter0);
  5540. END;
  5541. type := system.integerType;
  5542. (* ---- SYSTEM.GET8 ----- *)
  5543. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5544. IF CheckAddressType(parameter0) THEN
  5545. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5546. actualParameters.SetExpression(0,parameter0);
  5547. END;
  5548. type := system.shortintType;
  5549. (* ---- SYSTEM.GetStackPointer ----- *)
  5550. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5551. type := system.addressType;
  5552. (* ---- SYSTEM.GetFramePointer ----- *)
  5553. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5554. type := system.addressType;
  5555. (* ---- SYSTEM.GetActivity ----- *)
  5556. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5557. type := system.objectType;
  5558. (* ---- SYSTEM.SetStackPointer ----- *)
  5559. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5560. IF CheckAddressType(parameter0) THEN
  5561. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5562. actualParameters.SetExpression(0,parameter0);
  5563. END;
  5564. (* ---- SYSTEM.SetFramePointer ----- *)
  5565. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5566. IF CheckAddressType(parameter0) THEN
  5567. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5568. actualParameters.SetExpression(0,parameter0);
  5569. END;
  5570. (* ---- SYSTEM.SetActivity ----- *)
  5571. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5572. IF CheckObjectType(parameter0) THEN
  5573. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5574. actualParameters.SetExpression(0,parameter0);
  5575. END;
  5576. (* ---- LSH, LSL, ROT, ROR ----- *)
  5577. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5578. type := type0;
  5579. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5580. actualParameters.SetExpression(1, parameter1);
  5581. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5582. IF id = Global.Lsh THEN
  5583. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5584. ELSIF id = Global.Rot THEN
  5585. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5586. ELSIF id = Global.Ror THEN
  5587. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5588. END;
  5589. END;
  5590. (* ---- SYSTEM.VAL ----- *)
  5591. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5592. IF CheckTypeDeclarationType(parameter0) THEN
  5593. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5594. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5595. result := SyntaxTree.invalidExpression;
  5596. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5597. ELSE
  5598. IF (parameter1.resolved # NIL) THEN
  5599. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5600. IF parameter0 IS SyntaxTree.Value THEN
  5601. result.SetResolved(parameter0(SyntaxTree.Value));
  5602. END;
  5603. END;
  5604. result.SetAssignable(parameter1.assignable);
  5605. END;
  5606. END;
  5607. (* ---- SYSTEM.GET ----- *)
  5608. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5609. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5610. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5611. actualParameters.SetExpression(0,parameter0);
  5612. END;
  5613. (* ---- SYSTEM.PUT ----- *)
  5614. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5615. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5616. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5617. actualParameters.SetExpression(0,parameter0);
  5618. END;
  5619. (* ---- SYSTEM.PUT64 ----- *)
  5620. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5621. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5622. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5623. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5624. actualParameters.SetExpression(0,parameter0);
  5625. actualParameters.SetExpression(1,parameter1);
  5626. END;
  5627. (* ---- SYSTEM.PUT32 ----- *)
  5628. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5629. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5630. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5631. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5632. actualParameters.SetExpression(0,parameter0);
  5633. actualParameters.SetExpression(1,parameter1);
  5634. END;
  5635. (* ---- SYSTEM.PUT16 ----- *)
  5636. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5637. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5638. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5639. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5640. actualParameters.SetExpression(0,parameter0);
  5641. actualParameters.SetExpression(1,parameter1);
  5642. END;
  5643. (* ---- SYSTEM.PUT8 ----- *)
  5644. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5645. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5646. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5647. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5648. actualParameters.SetExpression(0,parameter0);
  5649. actualParameters.SetExpression(1,parameter1);
  5650. END;
  5651. (* ---- SYSTEM.MOVE ----- *)
  5652. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5653. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5654. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5655. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5656. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5657. actualParameters.SetExpression(0,parameter0);
  5658. actualParameters.SetExpression(1,parameter1);
  5659. actualParameters.SetExpression(2,parameter2);
  5660. END;
  5661. (* ---- SYSTEM.NEW ----- *)
  5662. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5663. IF ~IsPointerType(parameter0.type) THEN
  5664. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5665. ELSIF CheckSizeType(parameter1) THEN
  5666. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5667. actualParameters.SetExpression(1,parameter1);
  5668. END;
  5669. (* ----SYSTEM.REF ---- *)
  5670. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5671. type := system.addressType
  5672. (* ---- INCR ----- *)
  5673. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5674. type := system.sizeType;
  5675. base := type0;
  5676. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5677. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5678. IF i1 < 0 THEN
  5679. Error(position,Diagnostics.Invalid,"invalid dimension");
  5680. base := SyntaxTree.invalidType;
  5681. ELSE
  5682. base := ArrayBase(base,i1);
  5683. IF (base # NIL) & Indexable(base) THEN
  5684. ELSE
  5685. Error(position,Diagnostics.Invalid,"len on no array");
  5686. IF VerboseErrorMessage THEN
  5687. Printout.Info("base",base);
  5688. END;
  5689. base := SyntaxTree.invalidType;
  5690. END;
  5691. END;
  5692. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5693. actualParameters.SetExpression(1,parameter1);
  5694. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5695. mathArrayType := base(SyntaxTree.MathArrayType);
  5696. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5697. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5698. type := system.longintType;
  5699. END;
  5700. END;
  5701. ELSE
  5702. type := system.longintType;
  5703. END;
  5704. (* ---- SUM ----- *)
  5705. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5706. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5707. (* ---- ALL ----- *)
  5708. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5709. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5710. (* ---- DIM ----- *)
  5711. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5712. type := system.sizeType;
  5713. IF type0 IS SyntaxTree.MathArrayType THEN
  5714. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5715. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5716. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5717. END;
  5718. ELSE
  5719. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5720. END;
  5721. (* ---- CAS ----- *)
  5722. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5723. IF type0.IsComposite () THEN
  5724. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5725. result := SyntaxTree.invalidExpression;
  5726. ELSIF ~IsVariable (parameter0) THEN
  5727. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5728. result := SyntaxTree.invalidExpression;
  5729. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5730. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5731. result := SyntaxTree.invalidExpression;
  5732. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5733. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5734. result := SyntaxTree.invalidExpression;
  5735. ELSE
  5736. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5737. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5738. type := type0;
  5739. END;
  5740. (* ---- RESHAPE ----- *)
  5741. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5742. IF type0 IS SyntaxTree.MathArrayType THEN
  5743. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5744. base := ArrayBase(type0,MAX(LONGINT));
  5745. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5746. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5747. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5748. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5749. IF ~CompatibleTo(system,type0,parameterType) THEN
  5750. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5751. result := SyntaxTree.invalidExpression;
  5752. ELSE
  5753. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5754. END;
  5755. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5756. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5757. IF ~CompatibleTo(system,type1,parameterType) THEN
  5758. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5759. result := SyntaxTree.invalidExpression;
  5760. ELSE
  5761. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5762. END;
  5763. ELSE
  5764. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5765. result := SyntaxTree.invalidExpression;
  5766. END;
  5767. (* ---- SYSTEM.TYPECODE ----- *)
  5768. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5769. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5770. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5771. type := type.resolved;
  5772. IF type IS SyntaxTree.PointerType THEN
  5773. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5774. END;
  5775. IF ~(type IS SyntaxTree.RecordType) THEN
  5776. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5777. END;
  5778. ELSE
  5779. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5780. END;
  5781. type := system.addressType;
  5782. (* -------- FLT --------- *)
  5783. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5784. type := system.realType;
  5785. IF IsRealValue(parameter0, r) THEN
  5786. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5787. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5788. i0 := i; i := ABS(i);
  5789. IF i # 0 THEN
  5790. i1 := 23;
  5791. IF i >= 2*800000H THEN
  5792. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5793. ELSIF i < 800000H THEN
  5794. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5795. END;
  5796. i := (i1 + 127)*800000H - 800000H + i;
  5797. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5798. END;
  5799. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5800. END;
  5801. (* ------- CONNECT -------*)
  5802. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5803. IF ~(currentIsCellNet) THEN
  5804. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5805. END;
  5806. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5807. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5808. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5809. END;
  5810. IF numberActualParameters = 3 THEN
  5811. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5812. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5813. END;
  5814. *)
  5815. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5816. actualParameters.SetExpression(2,parameter2);
  5817. END;
  5818. activeCellsStatement := TRUE;
  5819. (* ---------- DELEGATE --------*)
  5820. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5821. IF ~(currentIsCellNet) THEN
  5822. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5823. END;
  5824. IF ~CheckPortType(parameter1, inPort) THEN
  5825. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5826. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5827. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5828. ELSIF (outPort.direction # inPort.direction) THEN
  5829. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5830. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5831. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5832. END;
  5833. activeCellsStatement := TRUE;
  5834. (* --------- RECEIVE ---------*)
  5835. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5836. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5837. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5838. IF inPort.direction # SyntaxTree.InPort THEN
  5839. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5840. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5841. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5842. END;
  5843. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5844. IF ~SameType(parameter2.type, system.integerType) THEN
  5845. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5846. END;
  5847. END;
  5848. END;
  5849. (* --------- SEND ---------*)
  5850. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5851. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5852. IF CheckPortType(parameter0,outPort) THEN
  5853. IF outPort.direction # SyntaxTree.OutPort THEN
  5854. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5855. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5856. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5857. ELSE
  5858. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5859. actualParameters.SetExpression(1,parameter1);
  5860. END;
  5861. END;
  5862. (* ------- custom builtins ----- *)
  5863. ELSIF id = Global.systemSpecial THEN
  5864. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5865. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5866. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5867. type := procedureType.returnType;
  5868. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5869. (* go through all formal parameters *)
  5870. formalParameter := procedureType.firstParameter;
  5871. FOR i := 0 TO actualParameters.Length() - 1 DO
  5872. actualParameter := actualParameters.GetExpression(i);
  5873. IF actualParameter = SyntaxTree.invalidExpression THEN
  5874. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5875. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5876. ELSE
  5877. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5878. END;
  5879. actualParameters.SetExpression(i, actualParameter);
  5880. formalParameter := formalParameter.nextParameter
  5881. END
  5882. END
  5883. ELSE
  5884. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5885. result := SyntaxTree.invalidExpression;
  5886. END;
  5887. END;
  5888. IF result # SyntaxTree.invalidExpression THEN
  5889. type := ResolveType(type);
  5890. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5891. result.SetType(type);
  5892. END;
  5893. RETURN result
  5894. END NewBuiltinCallDesignator;
  5895. (** return type guard designator left(type)
  5896. - check if type can be extended (i.e. is no static record)
  5897. - check if type is a type extension of left.type
  5898. - returns new type guard designator
  5899. returns invalidDesignator = invalidExpression if error
  5900. **)
  5901. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5902. VAR result: SyntaxTree.Designator;
  5903. BEGIN
  5904. result := SyntaxTree.invalidDesignator;
  5905. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5906. Error(position,Diagnostics.Invalid,"no type extension of type");
  5907. IF VerboseErrorMessage THEN
  5908. Printout.Info("left",left);
  5909. Printout.Info("type",type);
  5910. END;
  5911. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5912. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5913. ELSIF IsUnsafePointer(left.type) THEN
  5914. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5915. ELSE
  5916. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5917. result.SetType(type);
  5918. result.SetAssignable(left.assignable);
  5919. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5920. END;
  5921. RETURN result
  5922. END NewTypeGuardDesignator;
  5923. (** check and resolve parameter designator left(expression list)
  5924. - check expression list
  5925. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5926. - elsif left is a procedure type then
  5927. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5928. - else return is a procedure call then return ProcedureCallDesignator
  5929. returns invalidDesignator = invalidExpression if error
  5930. **)
  5931. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5932. VAR
  5933. parameters: SyntaxTree.ExpressionList;
  5934. left: SyntaxTree.Designator;
  5935. result,expression: SyntaxTree.Expression;
  5936. typeDeclaration: SyntaxTree.TypeDeclaration;
  5937. type, expressionType: SyntaxTree.Type;
  5938. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5939. BEGIN
  5940. type := type.resolved;
  5941. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5942. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5943. END;
  5944. RETURN type
  5945. END BaseType;
  5946. BEGIN
  5947. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5948. result := SyntaxTree.invalidDesignator;
  5949. left := ResolveDesignator(designator.left);
  5950. IF left # SyntaxTree.invalidDesignator THEN
  5951. parameters := designator.parameters;
  5952. IF ExpressionList(parameters) THEN
  5953. IF (left.type = NIL) THEN
  5954. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5955. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5956. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5957. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5958. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5959. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5960. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5961. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5962. ELSE
  5963. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5964. END
  5965. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5966. expression := parameters.GetExpression(0);
  5967. type := typeDeclaration.declaredType.resolved;
  5968. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5969. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5970. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5971. OR (expressionType IS SyntaxTree.EnumerationType)
  5972. ) THEN
  5973. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5974. ELSE
  5975. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5976. END;
  5977. ELSE
  5978. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5979. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5980. result := SyntaxTree.invalidDesignator;
  5981. END;
  5982. ELSE
  5983. result := SyntaxTree.invalidDesignator
  5984. END;
  5985. END;
  5986. resolvedExpression := result;
  5987. END VisitParameterDesignator;
  5988. (** check dereference designator left^
  5989. - check if left is pointer type or left is object type
  5990. - return new dereference designator with type = left.baseType.type (if appropriate)
  5991. with error handling
  5992. returns invalidDesignator = invalidExpression if error
  5993. **)
  5994. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5995. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5996. BEGIN
  5997. result := SyntaxTree.invalidDesignator;
  5998. type := left.type;
  5999. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  6000. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6001. result := SyntaxTree.NewDereferenceDesignator(position,left);
  6002. result.SetAssignable(TRUE);
  6003. result.SetType(type);
  6004. result.SetHidden(left.isHidden);
  6005. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  6006. type := type.resolved;
  6007. result := SyntaxTree.NewDereferenceDesignator(position,left);
  6008. result.SetAssignable(TRUE);
  6009. result.SetType(type);
  6010. result.SetHidden(left.isHidden);
  6011. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  6012. result := SyntaxTree.NewDereferenceDesignator(position,left);
  6013. result.SetAssignable(TRUE);
  6014. result.SetType(type);
  6015. result.SetHidden(left.isHidden);
  6016. ELSE
  6017. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  6018. IF VerboseErrorMessage THEN
  6019. Printout.Info("pointer", type);
  6020. Printout.Info("scope", currentScope);
  6021. END;
  6022. END;
  6023. RETURN result
  6024. END NewDereferenceDesignator;
  6025. (** check supercall designator left^
  6026. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  6027. - return new supercall designator with type = left.type
  6028. with error handling
  6029. **)
  6030. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  6031. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  6032. objectScope: SyntaxTree.Scope;
  6033. BEGIN
  6034. result := SyntaxTree.invalidDesignator;
  6035. IF left = SyntaxTree.invalidDesignator THEN
  6036. (* error already handled *)
  6037. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  6038. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  6039. ASSERT(symbol # SyntaxTree.invalidSymbol);
  6040. IF symbol IS SyntaxTree.Procedure THEN
  6041. procedure := symbol(SyntaxTree.Procedure);
  6042. objectScope := currentScope;
  6043. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  6044. objectScope := objectScope.outerScope;
  6045. END;
  6046. IF (left.left = NIL) OR ~
  6047. (
  6048. (left.left IS SyntaxTree.SelfDesignator) OR
  6049. (left.left IS SyntaxTree.DereferenceDesignator)
  6050. & (left.left(SyntaxTree.Designator).left # NIL)
  6051. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  6052. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  6053. IF VerboseErrorMessage THEN
  6054. Printout.Info("left.left",left.left);
  6055. END;
  6056. ELSIF procedure.super # NIL THEN
  6057. result := SyntaxTree.NewSupercallDesignator(position,left);
  6058. result.SetType(left.type.resolved)
  6059. ELSE
  6060. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  6061. END;
  6062. ELSE
  6063. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  6064. END;
  6065. ELSE
  6066. Error(position,Diagnostics.Invalid,"is no symbol designator");
  6067. END;
  6068. RETURN result
  6069. END NewSupercallDesignator;
  6070. (** check and semantically resolve arrow designator left^
  6071. - if left is procedure type -> result := SupercallDesignator
  6072. - else result := DereferenceDesignator
  6073. returns result via global variable resolvedExpression
  6074. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6075. **)
  6076. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  6077. VAR left: SyntaxTree.Designator;
  6078. BEGIN
  6079. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6080. left := ResolveDesignator(arrowDesignator.left);
  6081. IF left # NIL THEN
  6082. IF (left.type = NIL) THEN
  6083. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  6084. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6085. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6086. ELSE
  6087. IF IsPointerToObject(left.type) THEN
  6088. Warning(arrowDesignator.position, "forbidden dereference on object");
  6089. END;
  6090. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6091. END
  6092. END
  6093. END VisitArrowDesignator;
  6094. (** check and return expression
  6095. - if expression has no type then resolve expression
  6096. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6097. - return result
  6098. **)
  6099. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6100. VAR result,prev: SyntaxTree.Expression;
  6101. BEGIN
  6102. IF expression = NIL THEN result := NIL
  6103. ELSIF (expression.type = NIL) THEN
  6104. prev := resolvedExpression;
  6105. resolvedExpression := SyntaxTree.invalidExpression;
  6106. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6107. expression.SetType(SyntaxTree.invalidType);
  6108. END;
  6109. expression.Accept(SELF);
  6110. result := resolvedExpression;
  6111. IF currentIsRealtime THEN
  6112. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6113. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  6114. END;
  6115. END;
  6116. (* designator modifiers for backends if they support it ...*)
  6117. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6118. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6119. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6120. END;
  6121. resolvedExpression := prev
  6122. ELSE
  6123. result := expression
  6124. END;
  6125. RETURN result
  6126. END ResolveExpression;
  6127. (**
  6128. check expression to be constant expression
  6129. - resolve expression
  6130. - if valid then check that of value type
  6131. report error and return invalidExpression if anything fails
  6132. **)
  6133. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6134. VAR position: LONGINT;
  6135. BEGIN
  6136. position := expression.position;
  6137. expression := ResolveExpression(expression);
  6138. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6139. ELSIF (expression.resolved = NIL) THEN
  6140. Error(position,Diagnostics.Invalid,"expression is not constant");
  6141. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6142. expression := SyntaxTree.invalidExpression;
  6143. END;
  6144. RETURN expression
  6145. END ConstantExpression;
  6146. (** check expression to be constant integer
  6147. - resolve expresssion
  6148. - if valid then check that of integer value type
  6149. report error and return invalidExpression if anything fails
  6150. **)
  6151. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6152. VAR position: LONGINT;
  6153. BEGIN
  6154. position := expression.position;
  6155. expression := ResolveExpression(expression);
  6156. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6157. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6158. expression := SyntaxTree.invalidExpression;
  6159. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  6160. END;
  6161. RETURN expression
  6162. END ConstantInteger;
  6163. (** check expression as positive (>=0) constant integer
  6164. - resolve expression
  6165. - if valid then check that integer value
  6166. - if integer value then check that value >= 0
  6167. report error and return invalidExpression if anything fails
  6168. **)
  6169. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6170. VAR position: LONGINT;
  6171. BEGIN
  6172. position := expression.position;
  6173. expression := ConstantExpression(expression);
  6174. IF expression = SyntaxTree.invalidExpression THEN
  6175. (* error already reported *)
  6176. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6177. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  6178. expression := SyntaxTree.invalidExpression
  6179. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6180. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6181. END;
  6182. RETURN expression
  6183. END ConstantIntegerGeq0;
  6184. (** check expression as condition
  6185. - resolve expression
  6186. - if valid expression then check that result type is boolean
  6187. report error and return invalidExpression if anything fails
  6188. **)
  6189. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6190. VAR position: LONGINT;
  6191. BEGIN
  6192. position := expression.position;
  6193. expression := ResolveExpression(expression);
  6194. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6195. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6196. expression := SyntaxTree.invalidExpression;
  6197. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6198. END;
  6199. RETURN expression
  6200. END ResolveCondition;
  6201. (*** symbols ***)
  6202. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6203. BEGIN
  6204. x.Accept(SELF);
  6205. END ResolveSymbol;
  6206. (** check a symbol
  6207. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6208. **)
  6209. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6210. VAR scope: SyntaxTree.Scope;
  6211. BEGIN
  6212. (* visibility *)
  6213. scope := symbol.scope;
  6214. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6215. scope := scope.outerScope;
  6216. END;
  6217. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6218. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6219. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6220. IF VerboseErrorMessage THEN
  6221. Printout.Info("symbol",symbol);
  6222. END;
  6223. END;
  6224. END;
  6225. END CheckSymbolVisibility;
  6226. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6227. If node is currently being resolved then emit a cyclic definition error.
  6228. Return TRUE only if node is fully resolved.
  6229. **)
  6230. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6231. VAR result: BOOLEAN;
  6232. BEGIN
  6233. IF SyntaxTree.Resolved IN x.state THEN
  6234. result := FALSE
  6235. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6236. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6237. result := FALSE;
  6238. ELSE
  6239. result := TRUE;
  6240. x.SetState(SyntaxTree.BeingResolved)
  6241. END;
  6242. RETURN result
  6243. END SymbolNeedsResolution;
  6244. (** check and resolve a type declaration symbol = Type
  6245. - set type to declaration type
  6246. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6247. This is so because the type declaration itself does not have a type but it only stands for a type.
  6248. In the implementation of the compiler this made a lot much easier.
  6249. - resolve and set declared type
  6250. - check symbol
  6251. **)
  6252. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6253. VAR prevScope: SyntaxTree.Scope;
  6254. BEGIN
  6255. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6256. IF SymbolNeedsResolution(typeDeclaration) THEN
  6257. prevScope := currentScope;
  6258. currentScope := typeDeclaration.scope;
  6259. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6260. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6261. CheckSymbolVisibility(typeDeclaration);
  6262. typeDeclaration.SetState(SyntaxTree.Resolved);
  6263. currentScope := prevScope;
  6264. END;
  6265. END VisitTypeDeclaration;
  6266. (** check and resolve a constant declaration symbol = (constant) expression
  6267. - check expression
  6268. - set type and value
  6269. - check symbol
  6270. **)
  6271. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6272. VAR
  6273. expression: SyntaxTree.Expression;
  6274. type: SyntaxTree.Type;
  6275. name: Basic.SegmentedName;
  6276. replacement: Replacement;
  6277. BEGIN
  6278. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6279. IF SymbolNeedsResolution(constant) THEN
  6280. expression := constant.value;
  6281. IF replacements # NIL THEN
  6282. Global.GetSymbolSegmentedName(constant, name);
  6283. replacement := replacements;
  6284. WHILE (replacement # NIL) & (replacement.name # name) DO
  6285. replacement := replacement.next;
  6286. END;
  6287. IF replacement # NIL THEN
  6288. InfoSS(constant.position, "replacing constant", constant.name);
  6289. (*
  6290. NEW(stringReader, Strings.Length(replacement.string^));
  6291. stringReader.Set(replacement.string^);
  6292. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6293. NEW(parser, scanner, diagnostics);
  6294. expression := parser.Expression();
  6295. *)
  6296. expression := replacement.expression;
  6297. replacement.used := TRUE;
  6298. END;
  6299. END;
  6300. constant.SetType(SyntaxTree.invalidType);
  6301. expression := ConstantExpression(expression);
  6302. ASSERT(expression.type # NIL);
  6303. type := expression.type.resolved;
  6304. constant.SetType(type);
  6305. constant.SetValue(expression);
  6306. CheckSymbolVisibility(constant);
  6307. constant.SetState(SyntaxTree.Resolved);
  6308. END;
  6309. END VisitConstant;
  6310. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6311. VAR procedureAlignment: LONGINT;
  6312. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6313. (* least common multiple *)
  6314. VAR a,b: LONGINT;
  6315. BEGIN
  6316. a := a0; b := b0;
  6317. WHILE (a # b) DO
  6318. IF a < b THEN a := a+a0
  6319. ELSE b := b + b0
  6320. END;
  6321. END;
  6322. RETURN a
  6323. END LCM;
  6324. BEGIN
  6325. IF alignment > 1 THEN
  6326. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6327. IF (procedureAlignment > 1) THEN
  6328. alignment := LCM(alignment, procedureAlignment);
  6329. END;
  6330. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6331. END;
  6332. END AdaptStackAlignment;
  6333. (** check and resolve a variable / field
  6334. - check and set type
  6335. - negative check on open array type
  6336. - check symbol
  6337. **)
  6338. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6339. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6340. BEGIN
  6341. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6342. IF SymbolNeedsResolution(variable) THEN
  6343. modifiers := variable.modifiers;
  6344. (*
  6345. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6346. variable.AddFlags(flags);
  6347. *)
  6348. variable.SetType(ResolveType(variable.type));
  6349. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6350. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6351. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6352. END;
  6353. END;
  6354. CheckSymbolVisibility(variable);
  6355. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6356. variable.SetUntraced(TRUE);
  6357. IF ~ContainsPointer(variable.type) THEN
  6358. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6359. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6360. END;
  6361. END;
  6362. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6363. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6364. IF ~PowerOf2(value) THEN
  6365. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6366. ELSE
  6367. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6368. END;
  6369. END;
  6370. variable.SetAlignment(FALSE,value);
  6371. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6372. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6373. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6374. END;
  6375. variable.SetAlignment(TRUE, value);
  6376. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6377. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6378. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6379. END;
  6380. variable.SetFictive(value);
  6381. variable.SetOffset(value*system.dataUnit);
  6382. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6383. END;
  6384. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6385. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6386. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6387. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6388. END;
  6389. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6390. IF variable.initializer # NIL THEN
  6391. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6392. END;
  6393. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6394. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6395. pointerType.SetPointerBase(variable.type);
  6396. pointerType.SetHidden(TRUE);
  6397. variable.SetType(ResolveType(pointerType));
  6398. END;
  6399. variable.SetState(SyntaxTree.Resolved);
  6400. END;
  6401. END VisitVariable;
  6402. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6403. BEGIN
  6404. VisitVariable(property)
  6405. END VisitProperty;
  6406. (** check and resolve a (procedure) parameter
  6407. - check and set type
  6408. - check symbol
  6409. - check parameter kind and set read-only flags if appropriate
  6410. **)
  6411. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6412. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6413. BEGIN
  6414. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6415. IF SymbolNeedsResolution(parameter) THEN
  6416. modifiers := parameter.modifiers;
  6417. parameter.SetType(ResolveType(parameter.type));
  6418. ASSERT(parameter.type.resolved # NIL);
  6419. CheckSymbolVisibility(parameter);
  6420. IF parameter.defaultValue # NIL THEN
  6421. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6422. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6423. ELSE
  6424. expression := ConstantExpression(parameter.defaultValue);
  6425. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6426. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6427. parameter.SetDefaultValue(expression);
  6428. END;
  6429. END;
  6430. END;
  6431. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6432. parameter.SetUntraced(TRUE);
  6433. IF ~ContainsPointer(parameter.type) THEN
  6434. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6435. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6436. END;
  6437. END;
  6438. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6439. parameter.SetMoveable(TRUE);
  6440. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6441. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6442. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6443. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6444. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6445. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6446. END;
  6447. END;
  6448. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6449. parameter.SetState(SyntaxTree.Resolved);
  6450. END;
  6451. END VisitParameter;
  6452. (** check and resolve a procedure (with declaration and implementation scope)
  6453. - check the procedure type
  6454. - check if method (i.e. in record scope), if so then
  6455. - check if (unique) constructor
  6456. - check if (unique) finalizer
  6457. - check if super method available, if so then check signature
  6458. - of not in record scope then negative check on constructor flag
  6459. - of not in record scope then negative check on finalizer flag
  6460. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6461. - check procedure symbol
  6462. **)
  6463. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6464. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6465. procedureType: SyntaxTree.ProcedureType;
  6466. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6467. qualifiedType: SyntaxTree.QualifiedType;
  6468. value: LONGINT;
  6469. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6470. position: LONGINT;
  6471. BEGIN
  6472. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6473. IF IsOberonInline(procedure) THEN
  6474. IF SyntaxTree.Public * procedure.access # {} THEN
  6475. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6476. END;
  6477. procedure.SetInline(FALSE);
  6478. procedure.SetOberonInline(TRUE);
  6479. END;
  6480. IF SymbolNeedsResolution(procedure) THEN
  6481. recentIsRealtime := currentIsRealtime;
  6482. recentIsBodyProcedure := currentIsBodyProcedure;
  6483. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6484. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6485. modifiers := procedureType.modifiers;
  6486. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6487. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6488. IF useDarwinCCalls THEN (*fld*)
  6489. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6490. ELSE
  6491. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6492. END
  6493. END;
  6494. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6495. procedureType.SetInterrupt(TRUE);
  6496. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6497. END;
  6498. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6499. procedureType.SetNoReturn(TRUE);
  6500. END;
  6501. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6502. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6503. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6504. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6505. END;
  6506. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6507. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6508. END;
  6509. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6510. IF ~PowerOf2(value) THEN
  6511. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6512. ELSE
  6513. procedureType.SetStackAlignment(value)
  6514. END;
  6515. END;
  6516. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6517. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6518. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6519. END;
  6520. CheckModifiers(modifiers, TRUE);
  6521. modifiers := procedureType.returnTypeModifiers;
  6522. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6523. CheckModifiers(modifiers, TRUE);
  6524. FixProcedureType(procedureType);
  6525. currentIsRealtime := procedureType.isRealtime;
  6526. currentIsBodyProcedure := procedure.isBodyProcedure;
  6527. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6528. THEN
  6529. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6530. END;
  6531. CheckSymbolVisibility(procedure);
  6532. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6533. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6534. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6535. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6536. END;
  6537. END;
  6538. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6539. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6540. procedureType.SetDelegate(TRUE);
  6541. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6542. IF record.pointerType.typeDeclaration = NIL THEN
  6543. selfParameter.SetType(record.pointerType);
  6544. ELSE
  6545. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6546. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6547. qualifiedType.SetResolved(record.pointerType);
  6548. selfParameter.SetType(qualifiedType);
  6549. END;
  6550. selfParameter.SetAccess(SyntaxTree.Hidden);
  6551. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6552. IF procedure.isConstructor THEN
  6553. (*! constructor is always visible, compatibility to paco
  6554. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6555. *)
  6556. procedure.MarkUsed;
  6557. IF procedureType.returnType # NIL THEN
  6558. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6559. END;
  6560. proc := procedure.scope.firstProcedure;
  6561. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6562. proc := proc.nextProcedure;
  6563. END;
  6564. IF proc # NIL THEN
  6565. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6566. ELSE
  6567. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6568. END;
  6569. END;
  6570. IF procedure.isFinalizer THEN
  6571. procedure.MarkUsed;
  6572. IF procedureType.returnType # NIL THEN
  6573. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6574. END;
  6575. IF procedureType.numberParameters # 0 THEN
  6576. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6577. END;
  6578. proc := procedure.scope.firstProcedure;
  6579. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6580. proc := proc.nextProcedure;
  6581. END;
  6582. IF proc # NIL THEN
  6583. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6584. ELSE
  6585. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6586. END;
  6587. END;
  6588. super := FindSuperProcedure(record.recordScope, procedure);
  6589. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6590. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6591. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6592. END;
  6593. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6594. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6595. END;
  6596. IF super.isFinal THEN
  6597. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6598. END;
  6599. procedure.SetSuper(super);
  6600. super.SetOverwritten(TRUE);
  6601. procedure.SetAccess(procedure.access+super.access);
  6602. procedure.MarkUsed;
  6603. END;
  6604. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6605. THEN
  6606. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6607. END;
  6608. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6609. IF cellsAreObjects THEN
  6610. procedureType.SetDelegate(TRUE);
  6611. END;
  6612. IF procedure.isConstructor THEN
  6613. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6614. END;
  6615. ELSIF procedure.isConstructor THEN
  6616. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6617. END;
  6618. Declarations(procedure.procedureScope);
  6619. (* body resolution part done as late fix of the procedure type *)
  6620. procedure.SetState(SyntaxTree.Resolved);
  6621. currentIsRealtime := recentIsRealtime;
  6622. currentIsBodyProcedure := recentIsBodyProcedure;
  6623. END;
  6624. END VisitProcedure;
  6625. (**
  6626. a builtin procedure is a global item that may not be modified locally
  6627. instead the resolving of builtin procedure calls are done in the esignator
  6628. **)
  6629. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6630. VAR type: SyntaxTree.Type;
  6631. BEGIN
  6632. type := ResolveType(builtinProcedure.type);
  6633. END VisitBuiltin;
  6634. (* nopov *)
  6635. (** check and resolve operator
  6636. - operators are first checked as procedures
  6637. - then additional operator-specific checks are done
  6638. - note that only module-scope operators are checked here
  6639. (operators in a record scope are only allowed in the context of
  6640. array-structured object types and checked in 'ResolveArrayStructure')
  6641. - also note that inter-operator conformity is not checked here
  6642. **)
  6643. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6644. VAR
  6645. procedureType: SyntaxTree.ProcedureType;
  6646. leftType, rightType: SyntaxTree.Type;
  6647. identifierNumber, position: LONGINT;
  6648. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6649. modifiers: SyntaxTree.Modifier;
  6650. (** whether a type is locally defined in the current module scope
  6651. for arrays, the base type must be locally defined **)
  6652. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6653. BEGIN
  6654. IF type = NIL THEN
  6655. RETURN FALSE
  6656. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6657. RETURN TRUE
  6658. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6659. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6660. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6661. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6662. ELSE
  6663. RETURN FALSE
  6664. END
  6665. END IsLocallyDefined;
  6666. BEGIN
  6667. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6668. procedureType := operator.type(SyntaxTree.ProcedureType);
  6669. modifiers := procedureType.modifiers;
  6670. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6671. CheckModifiers(modifiers, TRUE);
  6672. VisitProcedure(operator);
  6673. IF operator.scope IS SyntaxTree.RecordScope THEN
  6674. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6675. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6676. IF identifierNumber = -1 THEN
  6677. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6678. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6679. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6680. ELSE
  6681. IF procedureType.numberParameters < 1 THEN
  6682. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6683. ELSIF procedureType.numberParameters > 2 THEN
  6684. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6685. ELSE
  6686. (* determine operand types *)
  6687. leftType := procedureType.firstParameter.type;
  6688. IF procedureType.numberParameters > 1 THEN
  6689. rightType := procedureType.firstParameter.nextParameter.type
  6690. ELSE
  6691. rightType := NIL
  6692. END;
  6693. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6694. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6695. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6696. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6697. END
  6698. END;
  6699. (* TODO: refine the checks, think about how restrictive the checks should be
  6700. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6701. They might be used for intersection, union, complement of custom object types *)
  6702. (* defaults *)
  6703. hasReturnType := TRUE;
  6704. mustBeUnary := FALSE;
  6705. mustBeBinary := FALSE;
  6706. mustReturnBoolean := FALSE;
  6707. mustReturnInteger := FALSE;
  6708. mustHaveEquitypedOperands := FALSE;
  6709. (* operator-specific exceptions *)
  6710. CASE identifierNumber OF
  6711. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6712. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6713. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6714. mustBeBinary := TRUE
  6715. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6716. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6717. | Scanner.Times: mustBeBinary := TRUE
  6718. | Scanner.TimesTimes: mustBeBinary := TRUE
  6719. | Scanner.DotTimes: mustBeBinary := TRUE
  6720. | Scanner.PlusTimes: mustBeBinary := TRUE
  6721. | Scanner.Slash: mustBeBinary := TRUE
  6722. | Scanner.Backslash: mustBeBinary := TRUE
  6723. | Scanner.DotSlash: mustBeBinary := TRUE
  6724. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6725. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6726. | Scanner.Not: mustBeUnary := TRUE
  6727. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6728. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6729. | Scanner.Transpose: mustBeUnary := TRUE;
  6730. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6731. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6732. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6733. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6734. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6735. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6736. | Global.Abs: mustBeUnary := TRUE;
  6737. | Global.Ash: (* TODO: arity? *)
  6738. | Global.Cap: (* TODO: arity? *)
  6739. | Global.Chr: mustBeUnary := TRUE;
  6740. | Global.Entier: (* TODO: arity? *)
  6741. | Global.EntierH: (* TODO: arity? *)
  6742. | Global.Len: (* unary and binary *)
  6743. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6744. | Global.Max, Global.Min: (* unary and binary *)
  6745. | Global.Odd: (* TODO: arity? *)
  6746. | Global.Sum: (* TODO: arity? *)
  6747. | Global.All: (* TODO: arity? *)
  6748. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6749. | Scanner.Alias:
  6750. | Scanner.GreaterGreater, Scanner.LessLess:
  6751. mustBeBinary := TRUE; hasReturnType := FALSE;
  6752. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6753. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6754. END;
  6755. (* check parameter count *)
  6756. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6757. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6758. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6759. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6760. END;
  6761. (* check parameter types *)
  6762. (* TODO: is this used at all? *)
  6763. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6764. leftType := procedureType.firstParameter.type;
  6765. rightType := procedureType.firstParameter.nextParameter.type;
  6766. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6767. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6768. END
  6769. END;
  6770. (* check return type *)
  6771. IF hasReturnType THEN
  6772. IF procedureType.returnType = NIL THEN
  6773. Error(operator.position, Diagnostics.Invalid, "return type required")
  6774. ELSIF mustReturnBoolean THEN
  6775. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6776. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6777. END
  6778. ELSIF mustReturnInteger THEN
  6779. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6780. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6781. END
  6782. END
  6783. ELSIF procedureType.returnType # NIL THEN
  6784. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6785. END
  6786. END
  6787. END
  6788. END
  6789. END VisitOperator;
  6790. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6791. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6792. BEGIN
  6793. IF error THEN RETURN FALSE END;
  6794. prevScope := currentScope;
  6795. prevDiagnostics := diagnostics;
  6796. diagnostics := NIL; (* suppress error output *)
  6797. currentScope := module.moduleScope;
  6798. VisitImport(x);
  6799. IF ~error THEN
  6800. module.moduleScope.AddImport(x);
  6801. x.SetScope(module.moduleScope);
  6802. END;
  6803. currentScope := prevScope;
  6804. diagnostics := prevDiagnostics;
  6805. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6806. END AddImport;
  6807. (** check and resolve import
  6808. - check for name = SYSTEM
  6809. - check for forbidden self import
  6810. - search through global import cache: already imported?
  6811. - check if already imported indirectly
  6812. - import if necessary -> set module and enter into import cache
  6813. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6814. - after this import this direct import and all indirect imports are stored in the current module's import list
  6815. **)
  6816. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6817. VAR
  6818. module: SyntaxTree.Module;
  6819. moduleScope: SyntaxTree.ModuleScope;
  6820. import,reimport: SyntaxTree.Import;
  6821. filename: FileName;
  6822. prevScope: SyntaxTree.Scope;
  6823. BEGIN
  6824. IF SymbolNeedsResolution(x) THEN
  6825. prevScope := currentScope;
  6826. x.SetType(SyntaxTree.importType);
  6827. moduleScope := currentScope.ownerModule.moduleScope;
  6828. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6829. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6830. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6831. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6832. ELSE
  6833. (* search through global import list: already imported ? *)
  6834. IF (x.module = NIL) & (importCache # NIL) THEN
  6835. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6836. ELSE import := NIL
  6837. END;
  6838. IF x.module # NIL THEN (* already imported indirectly *)
  6839. module := x.module;
  6840. ELSIF import # NIL THEN (* already in module list *)
  6841. module := import.module;
  6842. ASSERT(module # NIL);
  6843. x.SetModule(module);
  6844. ELSE (* must be imported *)
  6845. Global.ModuleFileName(x.moduleName,x.context,filename);
  6846. IF symbolFileFormat # NIL THEN
  6847. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6848. IF module = NIL THEN
  6849. ErrorSS(x.position,"could not import",filename);
  6850. IF VerboseErrorMessage THEN
  6851. Printout.Info("import",x)
  6852. END
  6853. ELSE
  6854. (*
  6855. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6856. (*! should rather be done by importer *)
  6857. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6858. checker.importCache := importCache;
  6859. checker.arrayBaseImported := arrayBaseImported;
  6860. checker.global := global;
  6861. checker.Module(module); (* semantic check *)
  6862. error := error OR checker.error;
  6863. END;
  6864. *)
  6865. (*
  6866. ASSERT(SyntaxTree.Resolved IN module.state);
  6867. *)
  6868. x.SetModule(module);
  6869. IF importCache # NIL THEN
  6870. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6871. import.SetContext(x.context);
  6872. import.SetModule(module);
  6873. importCache.AddImport(import);
  6874. END;
  6875. END;
  6876. ELSE
  6877. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6878. END;
  6879. END;
  6880. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6881. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6882. END;
  6883. import := module.moduleScope.firstImport;
  6884. WHILE(import # NIL) DO
  6885. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6886. ASSERT(currentScope # NIL);
  6887. ASSERT(currentScope.ownerModule # NIL);
  6888. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6889. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6890. Error(x.position,Diagnostics.Invalid,"recursive import");
  6891. ELSE
  6892. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6893. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6894. IF reimport = NIL THEN (* indirect import *)
  6895. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6896. reimport.SetContext(import.context);
  6897. reimport.SetModule(import.module);
  6898. moduleScope.AddImport(reimport);
  6899. reimport.SetScope(moduleScope);
  6900. ELSE
  6901. ASSERT(import.module # NIL);
  6902. reimport.SetModule(import.module); (* direct or indirect import *)
  6903. END;
  6904. END;
  6905. import := import.nextImport;
  6906. END;
  6907. END;
  6908. END;
  6909. currentScope := prevScope;
  6910. (* ELSE nothing to be done *)
  6911. x.SetState(SyntaxTree.Resolved);
  6912. END;
  6913. END VisitImport;
  6914. (*** statements ***)
  6915. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6916. VAR prev,resolved: SyntaxTree.Statement;
  6917. BEGIN
  6918. prev := resolvedStatement;
  6919. resolvedStatement := x;
  6920. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6921. activeCellsStatement := FALSE;
  6922. x.Accept(SELF);
  6923. (* removed this, implementation restriction should be resolved by backend
  6924. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6925. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6926. END;
  6927. *)
  6928. resolved := resolvedStatement;
  6929. resolvedStatement := prev;
  6930. RETURN resolved
  6931. END ResolveStatement;
  6932. (** check and resolve statement sequence
  6933. - check all statements, replace if necessary
  6934. **)
  6935. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6936. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6937. BEGIN
  6938. IF statementSequence # NIL THEN (* else empty *)
  6939. FOR i := 0 TO statementSequence.Length()-1 DO
  6940. statement := statementSequence.GetStatement(i);
  6941. resolved := ResolveStatement(statement);
  6942. IF (resolved # statement) THEN
  6943. statementSequence.SetStatement(i,resolved);
  6944. END;
  6945. END;
  6946. END;
  6947. END StatementSequence;
  6948. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6949. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6950. - check if procedure is callable
  6951. - check return type = NIL (otherwise must be assignment statement)
  6952. **)
  6953. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6954. VAR call: SyntaxTree.Designator;
  6955. BEGIN
  6956. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6957. call := procedureCall.call;
  6958. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6959. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6960. END;
  6961. call := ResolveDesignator(call);
  6962. IF call = SyntaxTree.invalidDesignator THEN
  6963. (* error already handled *)
  6964. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6965. (* inline call in a statement *)
  6966. ELSIF ~IsCallable(call) THEN
  6967. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6968. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6969. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6970. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6971. END;
  6972. procedureCall.SetCall(call);
  6973. (*
  6974. IF call = SyntaxTree.invalidDesignator THEN
  6975. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6976. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6977. IF IsOberonInline(procedure) THEN
  6978. Warning(procedure.position,"call to inline proc");
  6979. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6980. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6981. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6982. resolvedStatement := block;
  6983. RETURN;
  6984. END;
  6985. END;
  6986. *)
  6987. END VisitProcedureCallStatement;
  6988. (** check and resolve assignment LHS := RHS
  6989. - resolve LHS and RHS
  6990. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6991. - check if assignment is compatible
  6992. - check if LHS is variable (i.e. assignable)
  6993. - convert RHS if necessary
  6994. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6995. - assignment between different ASOTs
  6996. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6997. - assignment to ASOT elements:
  6998. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6999. **)
  7000. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  7001. VAR
  7002. left: SyntaxTree.Designator;
  7003. right, expression: SyntaxTree.Expression;
  7004. designator: SyntaxTree.Designator;
  7005. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7006. mathArrayType: SyntaxTree.MathArrayType;
  7007. BEGIN
  7008. right := ResolveExpression(assignment.right);
  7009. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  7010. left := ResolveDesignator(assignment.left);
  7011. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7012. (* error already handled *)
  7013. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  7014. (* LHS is index write operator call on ASOT *)
  7015. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  7016. (* necessary ?
  7017. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  7018. type := procedureType.firstParameter.type;
  7019. expression := procedureCallDesignator.parameters.GetExpression(0);
  7020. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  7021. *)
  7022. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7023. ELSIF CheckVariable(left) THEN
  7024. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  7025. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7026. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7027. (* conversion done by procedure call
  7028. (* try to convert to left argument *)
  7029. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7030. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7031. procedureCallDesignator.parameters.SetExpression(1, right);
  7032. END;
  7033. *)
  7034. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7035. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7036. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7037. ELSIF AssignmentCompatible(left, right) THEN
  7038. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7039. mathArrayType := MathArrayStructureOfType(left.type);
  7040. right := NewConversion(right.position, right, mathArrayType, NIL);
  7041. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7042. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7043. ELSE
  7044. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7045. assignment.SetLeft(left);
  7046. assignment.SetRight(right);
  7047. resolvedStatement := assignment
  7048. END
  7049. END
  7050. END
  7051. END VisitAssignment;
  7052. (** check and resolve assignment LHS := RHS
  7053. - resolve LHS and RHS
  7054. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7055. - check if assignment is compatible
  7056. - check if LHS is variable (i.e. assignable)
  7057. - convert RHS if necessary
  7058. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7059. - assignment between different ASOTs
  7060. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7061. - assignment to ASOT elements:
  7062. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7063. **)
  7064. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  7065. VAR
  7066. left: SyntaxTree.Designator;
  7067. right: SyntaxTree.Expression;
  7068. inPort, outPort: SyntaxTree.PortType;
  7069. expression: SyntaxTree.Expression;
  7070. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7071. BEGIN
  7072. right := ResolveExpression(communication.right);
  7073. left := ResolveDesignator(communication.left);
  7074. communication.SetLeft(left);
  7075. communication.SetRight(right);
  7076. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7077. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7078. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7079. (* conversion done by procedure call
  7080. (* try to convert to left argument *)
  7081. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7082. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7083. procedureCallDesignator.parameters.SetExpression(1, right);
  7084. END;
  7085. *)
  7086. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7087. ELSE
  7088. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7089. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7090. (* error already handled *)
  7091. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7092. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7093. IF outPort.direction # SyntaxTree.OutPort THEN
  7094. Error(left.position,Diagnostics.Invalid,"not an out-port")
  7095. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7096. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  7097. ELSE
  7098. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7099. communication.SetRight(right)
  7100. END;
  7101. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7102. IF CheckVariable(left) THEN
  7103. IF inPort.direction # SyntaxTree.InPort THEN
  7104. Error(left.position,Diagnostics.Invalid,"not an in-port")
  7105. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7106. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  7107. END;
  7108. END;
  7109. ELSE
  7110. Error(communication.position, -1, "unsupported stream operation");
  7111. END;
  7112. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7113. IF outPort.direction # SyntaxTree.OutPort THEN
  7114. Error(left.position,Diagnostics.Invalid,"not an out-port")
  7115. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7116. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  7117. ELSE
  7118. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7119. communication.SetRight(right)
  7120. END;
  7121. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7122. IF CheckVariable(right) THEN
  7123. IF inPort.direction # SyntaxTree.InPort THEN
  7124. Error(left.position,Diagnostics.Invalid,"not an in-port")
  7125. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7126. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  7127. END;
  7128. END;
  7129. ELSE
  7130. Error(communication.position, -1, "unsupported operation");
  7131. END;
  7132. END;
  7133. END VisitCommunicationStatement;
  7134. (** check and resolve if/eslif part
  7135. - check condition
  7136. - check statement sequence
  7137. **)
  7138. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7139. VAR prevUnreachable, b: BOOLEAN;
  7140. BEGIN
  7141. prevUnreachable := currentIsUnreachable;
  7142. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7143. IF IsBooleanValue(ifPart.condition,b) THEN
  7144. IF b=FALSE THEN
  7145. currentIsUnreachable := TRUE
  7146. ELSIF b=TRUE THEN
  7147. true := TRUE
  7148. END;
  7149. END;
  7150. StatementSequence(ifPart.statements);
  7151. currentIsUnreachable := prevUnreachable;
  7152. END IfPart;
  7153. (** check and resolve if statement
  7154. - check if parts and else part statement sequence
  7155. **)
  7156. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7157. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7158. BEGIN
  7159. prevUnreachable := currentIsUnreachable;
  7160. ifPartTrue := FALSE;
  7161. IfPart(ifStatement.ifPart,ifPartTrue);
  7162. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7163. elsif := ifStatement.GetElsifPart(i);
  7164. IfPart(elsif,ifPartTrue);
  7165. END;
  7166. IF ifStatement.elsePart # NIL THEN
  7167. IF ifPartTrue THEN
  7168. currentIsUnreachable := TRUE
  7169. END;
  7170. StatementSequence(ifStatement.elsePart)
  7171. END;
  7172. currentIsUnreachable := prevUnreachable;
  7173. END VisitIfStatement;
  7174. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7175. VAR variable: SyntaxTree.Designator;
  7176. type,variableType: SyntaxTree.Type;
  7177. withEntry: WithEntry;
  7178. BEGIN
  7179. variable := ResolveDesignator(withPart.variable);
  7180. variableType := variable.type.resolved;
  7181. withPart.SetVariable(variable);
  7182. type := ResolveType(withPart.type);
  7183. withPart.SetType(type);
  7184. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7185. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7186. END;
  7187. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7188. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7189. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7190. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7191. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7192. IF VerboseErrorMessage THEN
  7193. Printout.Info("variable",variable)
  7194. END;
  7195. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7196. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7197. IF VerboseErrorMessage THEN
  7198. Printout.Info("variable",variable);
  7199. Printout.Info("type",type);
  7200. END;
  7201. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7202. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7203. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7204. IF VerboseErrorMessage THEN
  7205. Printout.Info("variable",variable);
  7206. Printout.Info("type",type);
  7207. END;
  7208. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7209. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7210. ELSE
  7211. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7212. NEW(withEntry);
  7213. withEntry.previous := withEntries;
  7214. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7215. withEntry.type := type;
  7216. withEntries := withEntry;
  7217. StatementSequence(withPart.statements);
  7218. withEntries := withEntries.previous;
  7219. END;
  7220. END WithPart;
  7221. (** check and resolve with statement WITH variable: type DO ... END;
  7222. - check type and variable
  7223. - check that variable type is type extension of type
  7224. - check that variable is a variable
  7225. - enter new with scope and enter guardedVariable with same name and reference to variable
  7226. - create if statement:
  7227. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7228. **)
  7229. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7230. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7231. BEGIN
  7232. prevScope := currentScope; symbol := NIL;
  7233. FOR i := 0 TO withStatement.WithParts()-1 DO
  7234. WithPart(withStatement.GetWithPart(i),symbol);
  7235. END;
  7236. IF withStatement.elsePart # NIL THEN
  7237. StatementSequence(withStatement.elsePart)
  7238. END;
  7239. currentScope := prevScope;
  7240. END VisitWithStatement;
  7241. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7242. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7243. - check 'first' < 'last' and no overlaps between different case labels
  7244. - check statement sequence
  7245. **)
  7246. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7247. VAR
  7248. i: LONGINT;
  7249. position: LONGINT;
  7250. expression, left, right: SyntaxTree.Expression;
  7251. expressionType: SyntaxTree.Type;
  7252. l, r: LONGINT;
  7253. cl, cr: CHAR;
  7254. thiscases: SyntaxTree.CaseConstant;
  7255. BEGIN
  7256. thiscases := NIL;
  7257. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7258. expression := casePart.elements.GetExpression(i);
  7259. position := expression.position;
  7260. (* set context of range *)
  7261. IF expression IS SyntaxTree.RangeExpression THEN
  7262. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7263. END;
  7264. expression := ResolveExpression(expression);
  7265. IF expression = SyntaxTree.invalidExpression THEN
  7266. (* error already reported *)
  7267. expressionType := SyntaxTree.invalidType;
  7268. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7269. (* read out 'first' and 'last' *)
  7270. left := expression(SyntaxTree.RangeExpression).first;
  7271. right := expression(SyntaxTree.RangeExpression).last;
  7272. (* guaranteed by VisitRangeExpression: *)
  7273. ASSERT((left # NIL) & (right # NIL));
  7274. ASSERT(left.type.resolved = right.type.resolved);
  7275. left := CompatibleConversion(left.position, left, type);
  7276. right := CompatibleConversion(right.position, right, type);
  7277. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7278. expression(SyntaxTree.RangeExpression).SetLast(right);
  7279. expressionType := RegularType(position,left.type);
  7280. ELSE
  7281. expression := ConstantExpression(expression);
  7282. expression := CompatibleConversion(expression.position, expression, type);
  7283. (*
  7284. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7285. left := Global.NewCharacterValue(system,expression.position,cl);
  7286. expression := casePart.elements.GetExpression(i);
  7287. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7288. expression := left
  7289. END;
  7290. *)
  7291. casePart.elements.SetExpression(i,expression);
  7292. left := expression; right := expression;
  7293. expressionType := RegularType(position,expression.type)
  7294. END;
  7295. IF (expressionType = SyntaxTree.invalidType) THEN
  7296. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7297. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7298. expression := SyntaxTree.invalidExpression;
  7299. ELSE
  7300. l := 0; r := 0;
  7301. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7302. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7303. l := ORD(cl); r := ORD(cr);
  7304. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7305. ELSE
  7306. expression := SyntaxTree.invalidExpression
  7307. END;
  7308. IF expression # SyntaxTree.invalidExpression THEN
  7309. IF l>r THEN
  7310. Error(position,Diagnostics.Invalid,"empty case label")
  7311. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7312. Error(position,Diagnostics.Invalid,"duplicate case label");
  7313. ELSE
  7314. IF l < min THEN min := l END;
  7315. IF r > max THEN max := r END;
  7316. END;
  7317. END;
  7318. END;
  7319. casePart.elements.SetExpression(i,expression);
  7320. END;
  7321. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7322. casePart.SetConstants(thiscases);
  7323. StatementSequence(casePart.statements);
  7324. END CasePart;
  7325. (** check and resolve case statement CASE variable OF ... END;
  7326. - check variable
  7327. - check case parts
  7328. **)
  7329. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7330. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7331. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7332. BEGIN
  7333. expression := ResolveExpression(caseStatement.variable);
  7334. type := RegularType(expression.position,expression.type);
  7335. IF type = SyntaxTree.invalidType THEN
  7336. expression := SyntaxTree.invalidExpression;
  7337. ELSIF IsIntegerType(type) THEN
  7338. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7339. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7340. (*
  7341. expression := Global.NewCharacterValue(system,expression.position,ch);
  7342. *)
  7343. type := expression.type;
  7344. ELSIF IsCharacterType(type) THEN
  7345. ELSIF IsEnumerationType(type) THEN
  7346. ELSE
  7347. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7348. expression := SyntaxTree.invalidExpression;
  7349. END;
  7350. caseStatement.SetVariable(expression);
  7351. caseList := NIL;
  7352. min := MAX(LONGINT); max := MIN(LONGINT);
  7353. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7354. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7355. END;
  7356. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7357. msg := "huge sparse case table ";
  7358. Strings.AppendInt(msg, max-min);
  7359. Strings.Append(msg,"/");
  7360. Strings.AppendInt(msg, caseStatement.CaseParts());
  7361. Warning(caseStatement.position,msg);
  7362. END;
  7363. caseStatement.SetMinMax(min,max);
  7364. StatementSequence(caseStatement.elsePart);
  7365. IF expression.resolved # NIL THEN
  7366. IF IsCharacterValue(expression,ch) THEN
  7367. l := ORD(ch)
  7368. ELSIF IsIntegerValue(expression,l) THEN
  7369. END;
  7370. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7371. END;
  7372. END VisitCaseStatement;
  7373. (** check and resolve while statement
  7374. - check condition
  7375. - check statement sequence
  7376. **)
  7377. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7378. VAR prevIsUnreachable,b: BOOLEAN;
  7379. BEGIN
  7380. prevIsUnreachable := currentIsUnreachable;
  7381. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7382. IF IsBooleanValue(whileStatement.condition,b) THEN
  7383. IF b=FALSE THEN
  7384. currentIsUnreachable := TRUE
  7385. END;
  7386. END;
  7387. StatementSequence(whileStatement.statements);
  7388. currentIsUnreachable := prevIsUnreachable
  7389. END VisitWhileStatement;
  7390. (** check and resolve repeat statement
  7391. - check condition
  7392. - check statement sequence
  7393. **)
  7394. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7395. BEGIN
  7396. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7397. StatementSequence(repeatStatement.statements);
  7398. END VisitRepeatStatement;
  7399. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7400. VAR withEntry: WithEntry;
  7401. BEGIN
  7402. withEntry := withEntries;
  7403. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7404. withEntry := withEntry.previous
  7405. END;
  7406. IF withEntry = NIL THEN RETURN FALSE
  7407. ELSE
  7408. type := withEntry.type;
  7409. RETURN TRUE
  7410. END;
  7411. END GetGuard;
  7412. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7413. - check that variable is an integer variable
  7414. - check that from is integer typed with compatible type
  7415. - check that to has compatible type
  7416. - check that by is constant integer with compatible type
  7417. **)
  7418. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7419. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7420. BEGIN
  7421. designator := ResolveDesignator(forStatement.variable);
  7422. type := SyntaxTree.invalidType;
  7423. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7424. designator := SyntaxTree.invalidDesignator;
  7425. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7426. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7427. designator := SyntaxTree.invalidDesignator;
  7428. ELSIF CheckVariable(designator) THEN
  7429. type := designator.type;
  7430. END;
  7431. forStatement.SetVariable(designator);
  7432. expression := ResolveExpression(forStatement.from);
  7433. IF expression = SyntaxTree.invalidExpression THEN
  7434. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7435. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7436. expression := SyntaxTree.invalidExpression;
  7437. ELSIF type # SyntaxTree.invalidType THEN
  7438. expression := NewConversion(expression.position,expression,type,NIL)
  7439. END;
  7440. forStatement.SetFrom(expression);
  7441. expression := ResolveExpression(forStatement.to);
  7442. IF expression = SyntaxTree.invalidExpression THEN
  7443. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7444. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7445. expression := SyntaxTree.invalidExpression;
  7446. ELSIF type # SyntaxTree.invalidType THEN
  7447. expression := NewConversion(expression.position,expression,type,NIL)
  7448. END;
  7449. forStatement.SetTo(expression);
  7450. IF forStatement.by # NIL THEN
  7451. expression := ConstantInteger(forStatement.by);
  7452. ELSE
  7453. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7454. END;
  7455. IF expression = SyntaxTree.invalidExpression THEN
  7456. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7457. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7458. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7459. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7460. ELSIF type # SyntaxTree.invalidType THEN
  7461. expression := NewConversion(expression.position,expression,type,NIL)
  7462. END;
  7463. forStatement.SetBy(expression);
  7464. StatementSequence(forStatement.statements);
  7465. END VisitForStatement;
  7466. (** check and resolve loop statement LOOP StatementSequence END
  7467. - check statement sequence
  7468. **)
  7469. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7470. BEGIN
  7471. StatementSequence(loopStatement.statements)
  7472. END VisitLoopStatement;
  7473. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7474. BEGIN
  7475. StatementSequence(exitableBlock.statements);
  7476. END VisitExitableBlock;
  7477. (** check and resolve exit statement EXIT
  7478. - check that exit is within LOOP statement block
  7479. **)
  7480. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7481. VAR outer: SyntaxTree.Statement;
  7482. BEGIN
  7483. outer := exitStatement.outer;
  7484. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7485. outer := outer.outer;
  7486. END;
  7487. IF outer = NIL THEN
  7488. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7489. END;
  7490. END VisitExitStatement;
  7491. (** check and resolve return statement RETURN [expression]
  7492. - check expression (if any)
  7493. - check if in procedure scope
  7494. - if in procedure scope then check expression compatibility
  7495. - if not in procecdure scope then check on return without expression
  7496. **)
  7497. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7498. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7499. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7500. BEGIN
  7501. position := returnStatement.position;
  7502. expression := returnStatement.returnValue;
  7503. IF expression # NIL THEN
  7504. expression := ResolveExpression(expression);
  7505. returnStatement.SetReturnValue(expression);
  7506. END;
  7507. outer := returnStatement.outer;
  7508. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7509. outer := outer.outer
  7510. END;
  7511. IF (outer # NIL) THEN
  7512. scope := outer(SyntaxTree.Body).inScope;
  7513. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7514. IF (expression # NIL) THEN
  7515. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7516. END;
  7517. ELSE
  7518. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7519. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7520. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7521. END;
  7522. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7523. IF returnType # NIL THEN
  7524. returnType := returnType.resolved;
  7525. IF expression = NIL THEN
  7526. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7527. ELSIF expression.type = NIL THEN
  7528. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7529. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7530. Error(position,Diagnostics.Invalid,"return type not compatible");
  7531. IF VerboseErrorMessage THEN
  7532. Printout.Info("returnType",returnType);
  7533. Printout.Info("expression",expression);
  7534. END;
  7535. ELSE
  7536. expression := NewConversion(expression.position,expression,returnType,NIL);
  7537. returnStatement.SetReturnValue(expression);
  7538. END;
  7539. ELSIF expression # NIL THEN
  7540. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7541. END;
  7542. END;
  7543. END;
  7544. END VisitReturnStatement;
  7545. (** check and resolve await statement AWAIT(condition: Expression)
  7546. - check await condition
  7547. **)
  7548. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7549. VAR condition: SyntaxTree.Expression;
  7550. BEGIN
  7551. condition := ResolveCondition(awaitStatement.condition);
  7552. IF currentIsRealtime THEN
  7553. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7554. END;
  7555. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7556. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7557. END;
  7558. awaitStatement.SetCondition(condition);
  7559. END VisitAwaitStatement;
  7560. PROCEDURE CheckSystemImport(position: LONGINT);
  7561. VAR import: SyntaxTree.Import;
  7562. BEGIN
  7563. import := currentScope.ownerModule.moduleScope.firstImport;
  7564. WHILE(import # NIL) DO
  7565. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7566. RETURN;
  7567. END;
  7568. import := import.nextImport;
  7569. END;
  7570. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7571. END CheckSystemImport;
  7572. (** check and resolve code statement: do nothing, must be done by assembler
  7573. **)
  7574. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7575. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7576. BEGIN
  7577. CheckSystemImport(code.position);
  7578. FOR i := 0 TO code.inRules.Length()-1 DO
  7579. statement := code.inRules.GetStatement(i);
  7580. IF statement IS SyntaxTree.Assignment THEN
  7581. WITH statement: SyntaxTree.Assignment DO
  7582. statement.SetRight(ResolveExpression(statement.right));
  7583. END;
  7584. ELSE
  7585. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7586. END;
  7587. END;
  7588. FOR i := 0 TO code.outRules.Length()-1 DO
  7589. statement := code.outRules.GetStatement(i);
  7590. IF statement IS SyntaxTree.Assignment THEN
  7591. WITH statement: SyntaxTree.Assignment DO
  7592. statement.SetLeft(ResolveDesignator(statement.left));
  7593. END;
  7594. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7595. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7596. ELSE
  7597. Printout.Info("out statement ", statement);
  7598. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7599. END;
  7600. END;
  7601. END VisitCode;
  7602. (** check and set flags of a statement block
  7603. - check for multiply occurence of a flag
  7604. - check and set priority only in bodies
  7605. - check for valid names
  7606. **)
  7607. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7608. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7609. flag: LONGINT; recordBody: SyntaxTree.Body;
  7610. PROCEDURE SetProtectedRecord;
  7611. VAR scope: SyntaxTree.Scope;
  7612. BEGIN
  7613. scope := currentScope;
  7614. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7615. scope := scope.outerScope
  7616. END;
  7617. IF scope # NIL THEN
  7618. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7619. END;
  7620. END SetProtectedRecord;
  7621. BEGIN
  7622. flags := {};
  7623. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7624. recordBody := block(SyntaxTree.Body)
  7625. ELSE
  7626. recordBody := NIL
  7627. END;
  7628. blockModifier := block.blockModifiers;
  7629. WHILE(blockModifier # NIL) DO
  7630. name := blockModifier.identifier;
  7631. expression := blockModifier.expression;
  7632. position := blockModifier.position;
  7633. flag := -1;
  7634. IF name=Global.NamePriority THEN
  7635. IF expression = NIL THEN
  7636. Error(position,Diagnostics.Invalid,"missing priority expression");
  7637. ELSIF recordBody = NIL THEN
  7638. Error(position,Diagnostics.Invalid,"priority not on record body");
  7639. ELSIF recordBody.priority # NIL THEN
  7640. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7641. ELSE
  7642. recordBody.SetPriority(expression);
  7643. END;
  7644. ELSIF expression # NIL THEN
  7645. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7646. ELSIF name=Global.NameExclusive THEN
  7647. IF block.isExclusive THEN
  7648. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7649. END;
  7650. block.SetExclusive(TRUE); SetProtectedRecord;
  7651. ELSIF name=Global.NameActive THEN
  7652. IF recordBody = NIL THEN
  7653. Error(position,Diagnostics.Invalid,"active not in record body");
  7654. ELSIF recordBody.isActive THEN
  7655. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7656. ELSE
  7657. recordBody.SetActive(TRUE); SetProtectedRecord;
  7658. END;
  7659. ELSIF name=Global.NameSafe THEN
  7660. IF recordBody = NIL THEN
  7661. Error(position,Diagnostics.Invalid,"safe not in record body");
  7662. ELSIF recordBody.isSafe THEN
  7663. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7664. ELSE
  7665. recordBody.SetSafe(TRUE);
  7666. SetProtectedRecord;
  7667. END;
  7668. ELSIF name=Global.NameRealtime THEN
  7669. IF recordBody = NIL THEN
  7670. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7671. ELSIF recordBody.isRealtime THEN
  7672. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7673. ELSE
  7674. recordBody.SetRealtime(TRUE);
  7675. block.SetRealtime(TRUE);
  7676. END;
  7677. ELSIF name=Global.NameUnchecked THEN
  7678. IF block.isUnchecked THEN
  7679. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7680. ELSE
  7681. block.SetUnchecked(TRUE);
  7682. END;
  7683. ELSIF (name=Global.NameUncooperative) THEN
  7684. IF block.isUncooperative THEN
  7685. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7686. ELSE
  7687. block.SetUncooperative(TRUE);
  7688. END;
  7689. ELSE
  7690. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7691. END;
  7692. blockModifier := blockModifier.nextModifier;
  7693. END;
  7694. END BlockFlags;
  7695. (** check and resolve statement block
  7696. - check flags (exclusive)
  7697. - check statement sequence
  7698. **)
  7699. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7700. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7701. BEGIN
  7702. BlockFlags(statementBlock);
  7703. IF statementBlock.isExclusive THEN
  7704. (* check that not in exclusive block *)
  7705. IF currentIsExclusive THEN
  7706. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7707. ELSIF currentIsRealtime THEN
  7708. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7709. END;
  7710. END;
  7711. recentExclusive := currentIsExclusive;
  7712. recentUnreachable := currentIsUnreachable;
  7713. recentRealtime := currentIsRealtime;
  7714. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7715. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7716. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7717. StatementSequence(statementBlock.statements);
  7718. currentIsRealtime := recentRealtime;
  7719. currentIsExclusive := recentExclusive;
  7720. currentIsUnreachable := recentUnreachable;
  7721. END VisitStatementBlock;
  7722. (** check and resolve body
  7723. - check flags (active, priority, safe)
  7724. - check body and finally part
  7725. **)
  7726. PROCEDURE Body(body: SyntaxTree.Body);
  7727. BEGIN
  7728. VisitStatementBlock(body);
  7729. IF body.isActive THEN
  7730. IF ~currentIsBodyProcedure THEN
  7731. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7732. ELSIF body.priority # NIL THEN
  7733. body.SetPriority(ConstantInteger(body.priority));
  7734. END;
  7735. ELSIF body.isSafe THEN
  7736. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7737. ELSIF body.priority # NIL THEN
  7738. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7739. END;
  7740. IF body.code # NIL THEN
  7741. CheckSystemImport(body.position);
  7742. END;
  7743. StatementSequence(body.finally)
  7744. END Body;
  7745. (*** scopes ***)
  7746. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7747. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7748. VAR duplicateSymbol: BOOLEAN;
  7749. BEGIN
  7750. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7751. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7752. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7753. END;
  7754. scope.EnterSymbol(symbol,duplicateSymbol);
  7755. IF ~allowDuplicate & duplicateSymbol THEN
  7756. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7757. IF VerboseErrorMessage THEN
  7758. Printout.Info("multiply defined identifier",symbol);
  7759. Printout.Info("in scope",scope);
  7760. END;
  7761. END;
  7762. END Register;
  7763. (**
  7764. implementation: check and resolve an implementation part
  7765. **)
  7766. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7767. move implementation checker to a separate object ? *)
  7768. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7769. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7770. BEGIN
  7771. prevIsRealtime := currentIsRealtime;
  7772. prevIsBodyProcedure := currentIsBodyProcedure;
  7773. prevIsCellNet := currentIsCellNet;
  7774. prevScope := currentScope;
  7775. currentScope := scope;
  7776. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7777. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7778. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7779. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7780. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7781. (*
  7782. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7783. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7784. END;
  7785. *)
  7786. END;
  7787. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7788. (* module body, record bodies are wrapped into an artifical procedure *)
  7789. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7790. Body(scope(SyntaxTree.ProcedureScope).body)
  7791. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7792. Body(scope(SyntaxTree.ProcedureScope).body)
  7793. END;
  7794. END;
  7795. currentScope := prevScope;
  7796. currentIsRealtime := prevIsRealtime;
  7797. currentIsBodyProcedure := prevIsBodyProcedure;
  7798. currentIsCellNet := prevIsCellNet;
  7799. END Implementation;
  7800. (** implementation phase:
  7801. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7802. **)
  7803. PROCEDURE Implementations(x: SyntaxTree.Module);
  7804. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7805. BEGIN
  7806. prevPhase := phase;
  7807. phase := InlinePhase;
  7808. scope := x.firstScope;
  7809. WHILE(scope # NIL) DO
  7810. Implementation(scope);
  7811. scope := scope.nextScope;
  7812. END;
  7813. phase := ImplementationPhase;
  7814. scope := x.firstScope;
  7815. WHILE(scope # NIL) DO
  7816. Implementation(scope);
  7817. scope := scope.nextScope;
  7818. END;
  7819. phase := prevPhase;
  7820. END Implementations;
  7821. (** declaration phase:
  7822. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7823. - import lists (for module scopes)
  7824. - parameter list (for procedure scopes)
  7825. - constant declarations
  7826. - type declarations
  7827. - variable declarations
  7828. - procedure declarations
  7829. preformed in two stages:
  7830. - first all symbols are entered into the symbol table (with uniqueness check),
  7831. - then all symbols are resolved
  7832. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7833. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7834. **)
  7835. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7836. VAR
  7837. constant: SyntaxTree.Constant;
  7838. typeDeclaration: SyntaxTree.TypeDeclaration;
  7839. variable: SyntaxTree.Variable;
  7840. procedure: SyntaxTree.Procedure;
  7841. prevScope: SyntaxTree.Scope;
  7842. parameter: SyntaxTree.Parameter;
  7843. import: SyntaxTree.Import;
  7844. symbol: SyntaxTree.Symbol;
  7845. prevPhase: LONGINT;
  7846. prevError : BOOLEAN;
  7847. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7848. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7849. BEGIN
  7850. IF type.baseType # NIL THEN
  7851. baseType := type.baseType.resolved;
  7852. IF baseType IS SyntaxTree.PointerType THEN
  7853. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7854. END;
  7855. IF baseType IS SyntaxTree.CellType THEN
  7856. DeclareCell(baseType(SyntaxTree.CellType));
  7857. END;
  7858. END;
  7859. parameter := type.firstParameter;
  7860. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7861. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7862. variable.SetType(parameter.type);
  7863. variable.SetAccess(SyntaxTree.Hidden);
  7864. variable.SetModifiers(parameter.modifiers);
  7865. currentScope.PushVariable(variable);
  7866. (*
  7867. Register(parameter,scope, FALSE);
  7868. *)
  7869. parameter := parameter.nextParameter;
  7870. END;
  7871. property := type.firstProperty;
  7872. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7873. variable := currentScope.FindVariable(property.name);
  7874. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7875. prop := variable(SyntaxTree.Property);
  7876. ELSE (* add, duplicate symbols detection later *)
  7877. prop := SyntaxTree.NewProperty(property.position, property.name);
  7878. currentScope.PushVariable(prop);
  7879. END;
  7880. prop.SetType(property.type);
  7881. prop.SetValue(property.value);
  7882. prop.SetAccess(SyntaxTree.Hidden);
  7883. property := property.nextProperty;
  7884. END;
  7885. END DeclareCell;
  7886. BEGIN
  7887. prevError := error;
  7888. prevPhase := phase;
  7889. phase := DeclarationPhase;
  7890. prevScope := currentScope;
  7891. currentScope := scope;
  7892. error := FALSE;
  7893. (* first enter all symbols in scope *)
  7894. IF scope IS SyntaxTree.ModuleScope THEN
  7895. (* treat imports first for a module scope, , set default context if necessary *)
  7896. import := scope(SyntaxTree.ModuleScope).firstImport;
  7897. WHILE(import # NIL) DO
  7898. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7899. Register(import, currentScope, FALSE);
  7900. import := import.nextImport;
  7901. END;
  7902. import := scope(SyntaxTree.ModuleScope).firstImport;
  7903. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7904. ResolveSymbol(import);
  7905. import := import.nextImport;
  7906. END;
  7907. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7908. (* enter parameters for a procedure scope *)
  7909. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7910. WHILE(parameter # NIL) DO
  7911. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7912. END;
  7913. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7914. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7915. ELSIF scope IS SyntaxTree.CellScope THEN
  7916. import := scope(SyntaxTree.CellScope).firstImport;
  7917. WHILE(import # NIL) DO
  7918. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7919. Register(import, currentScope, FALSE);
  7920. import := import.nextImport;
  7921. END;
  7922. import := scope(SyntaxTree.CellScope).firstImport;
  7923. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7924. ResolveSymbol(import);
  7925. import := import.nextImport;
  7926. END;
  7927. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7928. END;
  7929. IF error THEN RETURN END;
  7930. (* constants *)
  7931. constant := scope.firstConstant;
  7932. WHILE (constant # NIL) DO
  7933. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7934. END;
  7935. (* type declarations *)
  7936. typeDeclaration := scope.firstTypeDeclaration;
  7937. WHILE (typeDeclaration # NIL) DO
  7938. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7939. END;
  7940. (* variables *)
  7941. variable := scope.firstVariable;
  7942. WHILE (variable # NIL) DO
  7943. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7944. END;
  7945. (* procedures *)
  7946. procedure := scope.firstProcedure;
  7947. WHILE (procedure # NIL) DO
  7948. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7949. END;
  7950. (* now process all symbols without any presumption on the order *)
  7951. symbol := scope.firstSymbol;
  7952. WHILE(symbol # NIL) DO
  7953. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7954. ResolveSymbol(symbol);
  7955. END;
  7956. symbol := symbol.nextSymbol;
  7957. END;
  7958. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7959. symbol := scope.firstSymbol;
  7960. WHILE symbol # NIL DO
  7961. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7962. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7963. pointerFixes.Add(symbol, currentScope);
  7964. END;
  7965. IF ~symbol.type.resolved.isRealtime THEN
  7966. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7967. END;
  7968. END;
  7969. symbol := symbol.nextSymbol
  7970. END;
  7971. END;
  7972. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7973. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7974. END;
  7975. IF scope.ownerModule # NIL THEN
  7976. IF ~(scope IS SyntaxTree.CellScope) OR ~SkipImplementation(scope(SyntaxTree.CellScope).ownerCell) THEN
  7977. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7978. scope.ownerModule.AddScope(scope);
  7979. END;
  7980. END;
  7981. phase := prevPhase;
  7982. currentScope := prevScope;
  7983. error := error OR prevError;
  7984. END Declarations;
  7985. (* nopov *)
  7986. (** check if all operators from one module are compatible to the ones in the other module
  7987. - check if there are not multiple operators with the same signature
  7988. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7989. - check for all operators whose signatures are compatible, whether the return types are compatible
  7990. note that:
  7991. - the return type is not considered to be part of the signature
  7992. - two signatures are considered compatible, if all of the operands are compatible
  7993. **)
  7994. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7995. VAR
  7996. thisOperator, thatOperator: SyntaxTree.Operator;
  7997. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7998. thisParameter, thatParameter: SyntaxTree.Parameter;
  7999. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8000. i: LONGINT;
  8001. BEGIN
  8002. currentScope := thisModuleScope;
  8003. hasError := FALSE;
  8004. (* go through all operators in the other module *)
  8005. thatOperator := thatModuleScope.firstOperator;
  8006. WHILE (thatOperator # NIL) & ~hasError DO
  8007. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8008. (* the other operator is accessible *)
  8009. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8010. (* the other operator is not the conversion operator *)
  8011. (* go through all operators in this module *)
  8012. thisOperator := thisModuleScope.firstOperator;
  8013. WHILE (thisOperator # NIL) & ~hasError DO
  8014. IF thisOperator # thatOperator THEN
  8015. (* the operators are not the same *)
  8016. IF thisOperator.name = thatOperator.name THEN
  8017. (* the operators share the same identifier *)
  8018. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8019. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8020. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8021. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8022. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8023. (* both operators have the same paramter count *)
  8024. thisParameter := thisProcedureType.firstParameter;
  8025. thatParameter := thatProcedureType.firstParameter;
  8026. operandsAreEqual := TRUE;
  8027. operandsAreCompatible := TRUE;
  8028. (* go through all parameters *)
  8029. FOR i := 1 TO thisProcedureType.numberParameters DO
  8030. ASSERT(thatParameter # NIL);
  8031. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8032. operandsAreEqual := FALSE;
  8033. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8034. operandsAreCompatible := FALSE
  8035. END
  8036. END;
  8037. thisParameter := thisParameter.nextParameter;
  8038. thatParameter := thatParameter.nextParameter
  8039. END;
  8040. IF operandsAreEqual THEN
  8041. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  8042. hasError := TRUE
  8043. ELSIF operandsAreCompatible THEN
  8044. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8045. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  8046. hasError := TRUE
  8047. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8048. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8049. hasError := TRUE
  8050. END
  8051. END
  8052. END
  8053. END
  8054. END;
  8055. thisOperator := thisOperator.nextOperator
  8056. END
  8057. END
  8058. END;
  8059. thatOperator := thatOperator.nextOperator
  8060. END
  8061. END CheckInterOperatorConformity;
  8062. (** check module:
  8063. - check module declaration
  8064. - add context, if necessary
  8065. - remove module from import cache, if necessary
  8066. - check declarations
  8067. - resolve all type fixes
  8068. - check implementation (bodies)
  8069. **)
  8070. PROCEDURE Module*(x: SyntaxTree.Module);
  8071. VAR (* nopov *)
  8072. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8073. BEGIN
  8074. prevScope := currentScope;
  8075. prevIsCellNet := currentIsCellNet;
  8076. module := x;
  8077. ASSERT(x # NIL);
  8078. global := system.globalScope[x.case];
  8079. x.moduleScope.SetGlobalScope(global);
  8080. currentScope := global;
  8081. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  8082. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8083. RemoveModuleFromCache(importCache,x);
  8084. Declarations(x.moduleScope);
  8085. FixTypes();
  8086. IF module.isCellNet THEN
  8087. currentIsCellNet := TRUE;
  8088. modifier := x.modifiers;
  8089. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8090. CheckModifiers(modifier, FALSE);
  8091. END;
  8092. (* nopov *)
  8093. IF ~error THEN
  8094. (* check if operators conform to each other within this module *)
  8095. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8096. (* go through all imports *)
  8097. import := x.moduleScope.firstImport;
  8098. WHILE import # NIL DO
  8099. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8100. (* check if all operators in this module conform to the ones of the imported module *)
  8101. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8102. END;
  8103. import := import.nextImport
  8104. END;
  8105. END;
  8106. Implementations(x);
  8107. module := NIL;
  8108. currentIsCellNet := prevIsCellNet;
  8109. currentScope := prevScope;
  8110. END Module;
  8111. END Checker;
  8112. Warnings*=OBJECT (SyntaxTree.Visitor)
  8113. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8114. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8115. BEGIN
  8116. SELF.diagnostics := diagnostics
  8117. END InitWarnings;
  8118. (** types *)
  8119. PROCEDURE Type(x: SyntaxTree.Type);
  8120. BEGIN x.Accept(SELF)
  8121. END Type;
  8122. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8123. BEGIN END VisitType;
  8124. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8125. BEGIN END VisitBasicType;
  8126. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8127. BEGIN END VisitCharacterType;
  8128. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8129. BEGIN END VisitIntegerType;
  8130. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8131. BEGIN END VisitFloatType;
  8132. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8133. BEGIN END VisitQualifiedType;
  8134. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8135. BEGIN END VisitStringType;
  8136. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8137. BEGIN END VisitEnumerationType;
  8138. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8139. BEGIN END VisitRangeType;
  8140. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8141. BEGIN
  8142. IF ~(SyntaxTree.Warned IN x.state) THEN
  8143. x.SetState(SyntaxTree.Warned);
  8144. Type(x.arrayBase);
  8145. END;
  8146. END VisitArrayType;
  8147. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8148. BEGIN
  8149. IF ~(SyntaxTree.Warned IN x.state) THEN
  8150. x.SetState(SyntaxTree.Warned);
  8151. Type(x.arrayBase);
  8152. END;
  8153. END VisitMathArrayType;
  8154. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8155. BEGIN
  8156. IF ~(SyntaxTree.Warned IN x.state) THEN
  8157. x.SetState(SyntaxTree.Warned);
  8158. Type(x.pointerBase);
  8159. END;
  8160. END VisitPointerType;
  8161. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8162. BEGIN Scope(x.recordScope) END VisitRecordType;
  8163. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8164. BEGIN Scope(x.cellScope) END VisitCellType;
  8165. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8166. BEGIN END VisitProcedureType;
  8167. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8168. VAR msg: ARRAY 256 OF CHAR;
  8169. BEGIN
  8170. Global.GetSymbolName(x,msg);
  8171. Strings.Append(msg," ");
  8172. Strings.Append(msg,text);
  8173. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  8174. END Warning;
  8175. (** symbols *)
  8176. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8177. BEGIN
  8178. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8179. IF ~(x IS SyntaxTree.Parameter) THEN
  8180. Warning(x,"never used");
  8181. END;
  8182. END;
  8183. x.Accept(SELF);
  8184. END Symbol;
  8185. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8186. BEGIN END VisitSymbol;
  8187. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8188. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8189. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8190. BEGIN END VisitConstant;
  8191. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8192. BEGIN END VisitVariable;
  8193. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8194. BEGIN END VisitProperty;
  8195. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8196. BEGIN END VisitParameter;
  8197. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8198. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8199. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8200. BEGIN END VisitOperator;
  8201. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8202. BEGIN END VisitImport;
  8203. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8204. VAR
  8205. symbol: SyntaxTree.Symbol;
  8206. BEGIN
  8207. symbol := scope.firstSymbol;
  8208. WHILE(symbol # NIL) DO
  8209. Symbol(symbol);
  8210. symbol := symbol.nextSymbol;
  8211. END;
  8212. END Scope;
  8213. PROCEDURE Module*(x: SyntaxTree.Module);
  8214. BEGIN
  8215. SELF.module := x;
  8216. Scope(x.moduleScope);
  8217. END Module;
  8218. END Warnings;
  8219. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8220. BEGIN
  8221. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8222. END IsOberonInline;
  8223. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8224. BEGIN
  8225. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8226. END Resolved;
  8227. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8228. VAR i: LONGINT;
  8229. BEGIN
  8230. i := 1;
  8231. WHILE i < x DO
  8232. i := i *2
  8233. END;
  8234. RETURN i=x
  8235. END PowerOf2;
  8236. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8237. BEGIN
  8238. RETURN
  8239. (scope # NIL) &
  8240. (scope IS SyntaxTree.ModuleScope)
  8241. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8242. OR
  8243. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8244. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8245. END IsCellNetScope;
  8246. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8247. BEGIN
  8248. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8249. END IsCellScope;
  8250. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8251. BEGIN
  8252. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8253. RETURN (scope # NIL) & IsCellNetScope(scope)
  8254. END InCellNetScope;
  8255. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8256. BEGIN
  8257. ASSERT(size MOD system.dataUnit = 0);
  8258. RETURN size DIV system.dataUnit
  8259. END ToMemoryUnits;
  8260. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8261. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8262. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8263. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8264. BEGIN
  8265. IF t = NIL THEN
  8266. RETURN TRUE
  8267. ELSE
  8268. t := t.resolved;
  8269. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8270. END;
  8271. END TypeAllowed;
  8272. BEGIN
  8273. type := type.resolved;
  8274. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8275. RETURN FALSE
  8276. ELSE
  8277. procedureType := type(SyntaxTree.ProcedureType);
  8278. numberParameters := procedureType.numberParameters;
  8279. RETURN
  8280. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8281. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8282. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8283. END;
  8284. END GetProcedureAllowed;
  8285. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8286. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8287. VAR import: SyntaxTree.Import;
  8288. BEGIN
  8289. import := importCache.ImportByModuleName(x.name,x.context);
  8290. IF import # NIL THEN
  8291. importCache.RemoveImporters(x.name,x.context);
  8292. END;
  8293. END RemoveModuleFromCache;
  8294. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8295. (* to <- this assignment compatibility *)
  8296. VAR result: BOOLEAN;
  8297. BEGIN
  8298. IF this= NIL THEN result := (to=NIL)
  8299. ELSIF to=NIL THEN result := FALSE
  8300. ELSE
  8301. (*! will be replaced by this:
  8302. ELSE result := this.CompatibleTo(to.resolved);
  8303. *)
  8304. this := this.resolved; to := to.resolved;
  8305. IF to=SyntaxTree.invalidType THEN result := FALSE
  8306. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8307. ELSIF to = this THEN
  8308. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8309. ELSIF to IS SyntaxTree.BasicType THEN
  8310. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8311. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8312. result := this.CompatibleTo(to.resolved)
  8313. ELSE
  8314. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8315. END
  8316. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8317. result := to.sizeInBits = this.sizeInBits;
  8318. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8319. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8320. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8321. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8322. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8323. result := TRUE;
  8324. ELSIF to IS SyntaxTree.AnyType THEN
  8325. 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);
  8326. ELSIF to IS SyntaxTree.ObjectType THEN
  8327. 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 *) ;
  8328. ELSIF to IS SyntaxTree.ByteType THEN
  8329. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8330. ELSIF to IS SyntaxTree.CharacterType THEN
  8331. result := IsCharacterType(this)
  8332. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8333. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8334. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8335. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8336. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8337. result := TRUE;
  8338. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8339. result := TRUE;
  8340. ELSE
  8341. result := FALSE
  8342. END;
  8343. ELSIF to IS SyntaxTree.PointerType THEN
  8344. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8345. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8346. & (~to.isRealtime OR this.isRealtime);
  8347. ELSIF to IS SyntaxTree.ProcedureType THEN
  8348. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8349. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8350. & (~to.isRealtime OR this.isRealtime)
  8351. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8352. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8353. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8354. ELSIF to IS SyntaxTree.RecordType THEN
  8355. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8356. ELSIF to IS SyntaxTree.ArrayType THEN
  8357. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8358. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8359. ELSIF StaticArrayCompatible(to, this) THEN
  8360. result := TRUE
  8361. ELSE
  8362. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8363. END;
  8364. ELSIF to IS SyntaxTree.MathArrayType THEN
  8365. IF this IS SyntaxTree.MathArrayType THEN
  8366. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8367. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8368. result := TRUE;
  8369. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8370. result := TRUE;
  8371. ELSE
  8372. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8373. END;
  8374. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8375. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8376. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8377. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8378. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8379. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8380. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8381. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8382. ELSE
  8383. result := FALSE
  8384. END;
  8385. (* an array-structured object type is compatible to the type of its array structure *)
  8386. ELSIF IsArrayStructuredObjectType(this) THEN
  8387. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8388. ELSE
  8389. result := FALSE;
  8390. END;
  8391. ELSIF to IS SyntaxTree.StringType THEN
  8392. result := FALSE;
  8393. ELSIF to IS SyntaxTree.EnumerationType THEN
  8394. result := IsEnumerationExtension(this,to);
  8395. ELSIF to IS SyntaxTree.PortType THEN
  8396. result := SameType(to, this)
  8397. ELSE
  8398. Printout.Info("CompatibleTo",to);
  8399. HALT(100); (* implement missing type check *)
  8400. END;
  8401. END;
  8402. RETURN result
  8403. END CompatibleTo;
  8404. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8405. VAR actualBase, formalBase: SyntaxTree.Type;
  8406. BEGIN
  8407. IF SameType(formal,actual) THEN
  8408. RETURN TRUE
  8409. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8410. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8411. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8412. RETURN
  8413. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8414. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8415. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8416. & StaticArrayCompatible(formalBase,actualBase)
  8417. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8418. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8419. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8420. RETURN
  8421. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8422. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8423. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8424. & StaticArrayCompatible(formalBase,actualBase)
  8425. ELSE RETURN FALSE
  8426. END;
  8427. END StaticArrayCompatible;
  8428. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8429. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8430. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8431. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8432. BEGIN
  8433. result := SameType(formal,actual);
  8434. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8435. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8436. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8437. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8438. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8439. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8440. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8441. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8442. & TC(formalBase, actualBase);
  8443. END;
  8444. RETURN result
  8445. END TC;
  8446. BEGIN
  8447. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8448. ELSE
  8449. arrayBase := formalType.arrayBase.resolved;
  8450. IF (actualType IS SyntaxTree.StringType) THEN
  8451. result := arrayBase IS SyntaxTree.CharacterType
  8452. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8453. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8454. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8455. result := TC(formalType, actualType);
  8456. ELSE
  8457. result := (arrayBase IS SyntaxTree.ByteType)
  8458. END;
  8459. END;
  8460. RETURN result
  8461. END OpenArrayCompatible;
  8462. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8463. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8464. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8465. BEGIN
  8466. IF actualType IS SyntaxTree.MathArrayType THEN
  8467. actualArray := actualType(SyntaxTree.MathArrayType);
  8468. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8469. (*
  8470. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8471. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8472. *)
  8473. actualBase := ArrayBase(actualType,Infinity);
  8474. formalBase := ArrayBase(formalType,Infinity);
  8475. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8476. ELSE
  8477. (*
  8478. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8479. *)
  8480. formalBase := Resolved(formalType.arrayBase);
  8481. actualBase := Resolved(actualArray.arrayBase);
  8482. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8483. (*
  8484. ARRAY [k] -> ARRAY [n]
  8485. *)
  8486. result := (formalType.staticLength = actualArray.staticLength)
  8487. ELSE
  8488. result := TRUE
  8489. END;
  8490. IF ~result THEN
  8491. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8492. ELSIF actualBase = NIL THEN result := FALSE
  8493. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8494. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8495. ELSE
  8496. result := SameType(formalBase,actualBase)
  8497. END;
  8498. END;
  8499. ELSE
  8500. result := FALSE
  8501. END;
  8502. RETURN result
  8503. END MathArrayCompatible;
  8504. (**
  8505. Math Array Type distance for assignments / parameter passings of the form
  8506. from -> to
  8507. variants:
  8508. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8509. allowed:
  8510. static -> static (& size match)
  8511. static -> open
  8512. static -> tensor
  8513. open -> open
  8514. open -> tensor
  8515. open -> static
  8516. tensor -> tensor
  8517. tensor -> open
  8518. tensor -> static
  8519. **)
  8520. (*! think about the metric here: is form matching more important than element type matching? *)
  8521. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8522. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8523. BEGIN
  8524. fromBase := Resolved(from.arrayBase);
  8525. toBase := Resolved(to.arrayBase);
  8526. i := Infinity;
  8527. IF from = to THEN
  8528. i := 0;
  8529. ELSIF (from.form = to.form) THEN
  8530. (* static -> static, open -> open, tensor -> tensor *)
  8531. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8532. IF fromBase = toBase THEN i := 0
  8533. ELSIF toBase = NIL THEN i := 1
  8534. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8535. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8536. ELSE
  8537. i := TypeDistance(system,fromBase, toBase, varpar);
  8538. END;
  8539. END;
  8540. ELSIF (to.form = SyntaxTree.Static) THEN
  8541. (* forbidden *)
  8542. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8543. (* static -> tensor, open -> tensor, tensor -> open *)
  8544. IF toBase=fromBase THEN i := 0;
  8545. ELSIF toBase = NIL THEN i := 1;
  8546. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8547. toBase := ArrayBase(toBase,Infinity);
  8548. IF (fromBase=toBase) THEN i := 0
  8549. ELSIF (toBase = NIL) THEN i:= 1
  8550. ELSIF (fromBase = NIL) THEN i := Infinity;
  8551. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8552. END;
  8553. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8554. fromBase := ArrayBase(fromBase,Infinity);
  8555. IF (fromBase=toBase) THEN i := 0
  8556. ELSIF (toBase = NIL) THEN i := 1
  8557. ELSIF (fromBase = NIL) THEN i := Infinity;
  8558. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8559. END;
  8560. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8561. END;
  8562. IF i # Infinity THEN INC(i,2) END;
  8563. ELSIF (from.form = SyntaxTree.Static) THEN
  8564. (* static -> open *)
  8565. IF toBase=fromBase THEN i := 0
  8566. ELSIF toBase = NIL THEN i := 1
  8567. ELSIF fromBase = NIL THEN i := Infinity
  8568. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8569. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8570. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8571. END;
  8572. IF i # Infinity THEN INC(i,1) END;
  8573. ELSE HALT(100); (* unknown case *)
  8574. END;
  8575. RETURN i;
  8576. END MathArrayTypeDistance;
  8577. (** compute and return the distance of two array types
  8578. - return the distance of the base types
  8579. **)
  8580. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8581. VAR i: LONGINT;
  8582. BEGIN
  8583. i := Infinity;
  8584. IF from = to THEN
  8585. i := 0
  8586. ELSE
  8587. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8588. (*
  8589. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8590. i := TypeDistance(from.base, to.base);
  8591. IF i >= 0 THEN INC(i) END
  8592. ELSIF (from.mode = open) & (to.mode = open) THEN
  8593. i := TypeDistance(from.base, to.base);
  8594. *)
  8595. END;
  8596. RETURN i
  8597. END ArrayTypeDistance;
  8598. (** compute the signature distance of a procedure and an actual parameter list
  8599. - if any of the parameters are not compatible, the result is infinite
  8600. - add up and return the distance over all parameters
  8601. **)
  8602. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8603. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8604. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8605. BEGIN
  8606. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8607. result := Infinity
  8608. ELSE
  8609. formalParameter := procedureType.firstParameter;
  8610. i := 0;
  8611. result := 0;
  8612. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8613. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8614. actualParameter := actualParameters.GetExpression(i);
  8615. ASSERT(formalParameter.type # NIL);
  8616. IF (actualParameter.type = NIL) THEN distance := Infinity
  8617. ELSE
  8618. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8619. END;
  8620. IF distance = Infinity THEN
  8621. result := Infinity;
  8622. ELSE
  8623. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8624. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8625. (* already handled varpar *)
  8626. (*
  8627. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8628. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8629. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8630. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8631. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8632. END;
  8633. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8634. result := Infinity
  8635. END;
  8636. *)
  8637. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8638. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8639. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8640. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8641. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8642. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8643. END;
  8644. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8645. result := Infinity
  8646. END;
  8647. ELSE
  8648. result := Infinity
  8649. END;
  8650. ELSE
  8651. INC(result,distance);
  8652. END;
  8653. END;
  8654. formalParameter := formalParameter.nextParameter; INC(i);
  8655. END;
  8656. END;
  8657. ASSERT(result >= 0);
  8658. RETURN result
  8659. END Distance;
  8660. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8661. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8662. BEGIN
  8663. IF right.numberParameters # (procedureType.numberParameters) THEN
  8664. result := Infinity
  8665. ELSE
  8666. formalParameter := procedureType.firstParameter;
  8667. rightParameter := right.firstParameter;
  8668. i := 0;
  8669. result := 0;
  8670. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8671. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8672. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8673. IF distance = Infinity THEN
  8674. result := Infinity;
  8675. ELSE
  8676. INC(result,distance);
  8677. END;
  8678. formalParameter := formalParameter.nextParameter;
  8679. rightParameter := rightParameter.nextParameter;
  8680. END;
  8681. END;
  8682. ASSERT(result >= 0);
  8683. RETURN result
  8684. END ProcedureTypeDistance;
  8685. (** compute and return the distance between two types, used for computation of signature distance
  8686. from -> to
  8687. **)
  8688. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8689. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8690. BEGIN
  8691. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8692. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8693. END;
  8694. i := Infinity;
  8695. IF from = to THEN
  8696. i := 0
  8697. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8698. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8699. i := Infinity;
  8700. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8701. i := 1
  8702. ELSIF (from IS SyntaxTree.StringType) THEN
  8703. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8704. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8705. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8706. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8707. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8708. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8709. i := 1
  8710. ELSIF (from IS SyntaxTree.NilType) THEN
  8711. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8712. (*
  8713. ELSIF (from = NoType) THEN
  8714. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8715. *)
  8716. ELSIF (from IS SyntaxTree.BasicType) THEN
  8717. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8718. IF varpar & (i # 0) THEN i := Infinity END;
  8719. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8720. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8721. ELSIF (from IS SyntaxTree.RecordType) THEN
  8722. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8723. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8724. IF to IS SyntaxTree.MathArrayType THEN
  8725. (*
  8726. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8727. i := Infinity;
  8728. ELSE
  8729. *)
  8730. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8731. (*
  8732. END;
  8733. *)
  8734. END
  8735. ELSIF (from IS SyntaxTree.PointerType) THEN
  8736. ptr := from(SyntaxTree.PointerType);
  8737. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8738. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8739. (* ELSE i := TypeDistance(ptr.base, to); *)
  8740. END
  8741. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8742. IF (to IS SyntaxTree.ProcedureType) THEN
  8743. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8744. END;
  8745. ELSIF (from IS SyntaxTree.PortType) THEN
  8746. IF (to IS SyntaxTree.PortType) THEN
  8747. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8748. i := 0;
  8749. END;
  8750. END;
  8751. (*no procedure test, procedure must be the same*)
  8752. END;
  8753. RETURN i
  8754. END TypeDistance;
  8755. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8756. BEGIN
  8757. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8758. END IsIntegerType;
  8759. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8760. BEGIN
  8761. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8762. END IsAddressType;
  8763. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8764. BEGIN
  8765. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8766. END IsSizeType;
  8767. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8768. BEGIN
  8769. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8770. END IsSignedIntegerType;
  8771. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8772. BEGIN
  8773. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8774. END IsUnsignedIntegerType;
  8775. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8776. VAR result: BOOLEAN;
  8777. BEGIN
  8778. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8779. value := x.resolved(SyntaxTree.IntegerValue).value;
  8780. result := TRUE
  8781. ELSE
  8782. result := FALSE
  8783. END;
  8784. RETURN result
  8785. END IsIntegerValue;
  8786. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8787. VAR result: BOOLEAN;
  8788. BEGIN
  8789. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8790. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8791. result := TRUE
  8792. ELSE
  8793. result := FALSE
  8794. END;
  8795. RETURN result
  8796. END IsEnumerationValue;
  8797. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8798. VAR result: BOOLEAN;
  8799. BEGIN
  8800. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8801. value := x.resolved(SyntaxTree.RealValue).value;
  8802. result := TRUE
  8803. ELSE
  8804. result := FALSE
  8805. END;
  8806. RETURN result
  8807. END IsRealValue;
  8808. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8809. VAR result: BOOLEAN;
  8810. BEGIN
  8811. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8812. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8813. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8814. result := TRUE
  8815. ELSE
  8816. result := FALSE
  8817. END;
  8818. RETURN result
  8819. END IsComplexValue;
  8820. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8821. VAR result: BOOLEAN;
  8822. BEGIN
  8823. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8824. value := x.resolved(SyntaxTree.CharacterValue).value;
  8825. result := TRUE
  8826. ELSE
  8827. result := FALSE
  8828. END;
  8829. RETURN result
  8830. END IsCharacterValue;
  8831. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8832. VAR result: BOOLEAN;
  8833. BEGIN
  8834. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8835. value := x.resolved(SyntaxTree.BooleanValue).value;
  8836. result := TRUE
  8837. ELSE
  8838. result := FALSE
  8839. END;
  8840. RETURN result
  8841. END IsBooleanValue;
  8842. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8843. VAR result: BOOLEAN;
  8844. BEGIN
  8845. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8846. value := x.resolved(SyntaxTree.SetValue).value;
  8847. result := TRUE
  8848. ELSE
  8849. result := FALSE
  8850. END;
  8851. RETURN result
  8852. END IsSetValue;
  8853. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8854. VAR result: BOOLEAN;
  8855. BEGIN
  8856. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8857. value := x.resolved(SyntaxTree.StringValue).value;
  8858. result := TRUE
  8859. ELSE
  8860. result := FALSE
  8861. END;
  8862. RETURN result
  8863. END IsStringValue;
  8864. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8865. BEGIN
  8866. x := x.resolved;
  8867. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8868. END Indexable;
  8869. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8870. BEGIN
  8871. RETURN t1.SameType(t2.resolved);
  8872. END SameType;
  8873. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8874. BEGIN
  8875. IF t IS SyntaxTree.MathArrayType THEN
  8876. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8877. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8878. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8879. DEC(max);
  8880. END;
  8881. ELSIF t IS SyntaxTree.ArrayType THEN
  8882. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8883. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8884. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8885. END;
  8886. END;
  8887. RETURN t;
  8888. END ArrayBase;
  8889. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8890. BEGIN
  8891. type := type.resolved;
  8892. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8893. base := type(SyntaxTree.ArrayType).arrayBase;
  8894. RETURN TRUE;
  8895. END;
  8896. RETURN FALSE;
  8897. END IsOpenArray;
  8898. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8899. BEGIN
  8900. type := type.resolved;
  8901. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8902. base := type(SyntaxTree.ArrayType).arrayBase;
  8903. dim := type(SyntaxTree.ArrayType).staticLength;
  8904. RETURN TRUE
  8905. ELSE
  8906. RETURN FALSE
  8907. END;
  8908. END IsStaticArray;
  8909. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8910. BEGIN
  8911. type := type.resolved;
  8912. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8913. base := type(SyntaxTree.ArrayType).arrayBase;
  8914. RETURN TRUE
  8915. ELSE
  8916. RETURN FALSE
  8917. END;
  8918. END IsDynamicArray;
  8919. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8920. VAR i: LONGINT;
  8921. BEGIN
  8922. i := 0;
  8923. t := t.resolved;
  8924. IF t IS SyntaxTree.MathArrayType THEN
  8925. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8926. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8927. END;
  8928. ELSIF t IS SyntaxTree.ArrayType THEN
  8929. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8930. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8931. END;
  8932. END;
  8933. RETURN i
  8934. END Dimension;
  8935. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8936. BEGIN
  8937. RETURN expression.assignable;
  8938. END IsVariable;
  8939. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8940. BEGIN
  8941. IF (symbol IS SyntaxTree.Parameter) THEN
  8942. WITH symbol: SyntaxTree.Parameter DO
  8943. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8944. END;
  8945. ELSE
  8946. RETURN FALSE
  8947. END;
  8948. END IsVariableParameter;
  8949. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8950. VAR result: BOOLEAN;
  8951. BEGIN
  8952. IF type = NIL THEN result := FALSE
  8953. ELSE
  8954. type := type.resolved;
  8955. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8956. END;
  8957. RETURN result
  8958. END IsPointerType;
  8959. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8960. VAR result: BOOLEAN;
  8961. BEGIN
  8962. IF type = NIL THEN result := FALSE
  8963. ELSE
  8964. type := type.resolved;
  8965. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8966. END;
  8967. RETURN result
  8968. END IsUnsafePointer;
  8969. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8970. BEGIN
  8971. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8972. END IsDisposable;
  8973. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8974. VAR result: BOOLEAN;
  8975. BEGIN
  8976. IF type = NIL THEN result := FALSE
  8977. ELSE
  8978. type := type.resolved;
  8979. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8980. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8981. result := result OR (type IS SyntaxTree.ObjectType);
  8982. END;
  8983. RETURN result
  8984. END IsPointerToRecord;
  8985. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8986. VAR result: BOOLEAN;
  8987. BEGIN
  8988. IF type = NIL THEN result := FALSE
  8989. ELSE
  8990. type := type.resolved;
  8991. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8992. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8993. ;
  8994. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8995. result := result OR (type IS SyntaxTree.ObjectType);
  8996. END;
  8997. RETURN result
  8998. END IsPointerToObject;
  8999. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9000. BEGIN
  9001. IF type # NIL THEN
  9002. RETURN type.resolved.hasPointers
  9003. ELSE
  9004. RETURN FALSE
  9005. END;
  9006. END ContainsPointer;
  9007. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9008. BEGIN
  9009. IF type = NIL THEN RETURN FALSE END;
  9010. type := type.resolved;
  9011. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9012. END IsStringType;
  9013. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9014. BEGIN
  9015. IF type = NIL THEN RETURN FALSE END;
  9016. type := type.resolved;
  9017. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9018. END IsCharacterType;
  9019. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9020. BEGIN
  9021. IF type = NIL THEN RETURN FALSE END;
  9022. type := type.resolved;
  9023. RETURN (type IS SyntaxTree.EnumerationType)
  9024. END IsEnumerationType;
  9025. (** cf. section "Type extension (base type)" in the language report **)
  9026. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9027. VAR result: BOOLEAN;
  9028. BEGIN
  9029. ASSERT(base # NIL); ASSERT(extension # NIL);
  9030. base := base.resolved; extension := extension.resolved;
  9031. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9032. result := TRUE;
  9033. ELSE
  9034. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9035. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9036. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9037. END;
  9038. WHILE (extension # NIL) & (extension # base) DO
  9039. IF extension IS SyntaxTree.RecordType THEN
  9040. extension := extension(SyntaxTree.RecordType).baseType;
  9041. IF (extension # NIL) THEN extension := extension.resolved END;
  9042. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9043. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9044. END;
  9045. ELSE extension := NIL;
  9046. END;
  9047. END;
  9048. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9049. END;
  9050. RETURN result
  9051. END IsTypeExtension;
  9052. (** check if base is the base enumeration type of extension **)
  9053. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9054. BEGIN
  9055. base := base.resolved; extension := extension.resolved;
  9056. WHILE (extension # NIL) & (extension # base) DO
  9057. IF extension IS SyntaxTree.EnumerationType THEN
  9058. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9059. IF extension # NIL THEN extension := extension.resolved END;
  9060. ELSE
  9061. extension := NIL
  9062. END;
  9063. END;
  9064. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9065. END IsEnumerationExtension;
  9066. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9067. BEGIN
  9068. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9069. RETURN TRUE
  9070. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9071. RETURN TRUE
  9072. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9073. RETURN TRUE
  9074. ELSE
  9075. RETURN FALSE
  9076. END
  9077. END IsCallable;
  9078. (** compute and return the distance of two record types
  9079. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9080. **)
  9081. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9082. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9083. BEGIN
  9084. i := 0;
  9085. WHILE (from # NIL) & (from # to) DO
  9086. baseType := from.baseType;
  9087. IF (baseType # NIL) THEN
  9088. baseType := baseType.resolved;
  9089. IF baseType IS SyntaxTree.PointerType THEN
  9090. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9091. END;
  9092. IF baseType IS SyntaxTree.RecordType THEN
  9093. from := baseType(SyntaxTree.RecordType);
  9094. ELSE
  9095. from := NIL;
  9096. END;
  9097. ELSE
  9098. from := NIL
  9099. END;
  9100. INC(i)
  9101. END;
  9102. IF from = NIL THEN i := Infinity END;
  9103. RETURN i
  9104. END RecordTypeDistance;
  9105. (** compute and return the distance of two pointer types **)
  9106. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9107. BEGIN
  9108. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9109. RETURN Infinity;
  9110. ELSE
  9111. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9112. END;
  9113. END PointerTypeDistance;
  9114. (** check if expression contains a symbol designator pointing to a type declaration.
  9115. - if so then enter type declaration into typeDeclaration and return true else return false
  9116. **)
  9117. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9118. VAR result: BOOLEAN;
  9119. BEGIN
  9120. result := FALSE;
  9121. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9122. result := TRUE;
  9123. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9124. END;
  9125. RETURN result
  9126. END IsTypeDesignator;
  9127. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9128. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9129. VAR result: BOOLEAN;
  9130. BEGIN
  9131. type := type.resolved;
  9132. IF type IS SyntaxTree.PointerType THEN
  9133. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9134. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9135. result := TRUE
  9136. ELSE
  9137. result := type IS SyntaxTree.RecordType
  9138. END;
  9139. RETURN result
  9140. END IsExtensibleType;
  9141. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9142. BEGIN
  9143. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9144. (d IS SyntaxTree.SymbolDesignator) &
  9145. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9146. OR
  9147. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9148. END IsUnextensibleRecord;
  9149. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9150. BEGIN
  9151. IF IsUnextensibleRecord(d) THEN
  9152. RETURN FALSE
  9153. ELSE RETURN IsExtensibleType(d.type.resolved)
  9154. END;
  9155. END IsExtensibleDesignator;
  9156. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9157. BEGIN
  9158. type := type.resolved;
  9159. IF (type IS SyntaxTree.PointerType) THEN
  9160. RETURN TRUE
  9161. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9162. RETURN TRUE
  9163. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9164. RETURN TRUE
  9165. ELSIF (type IS SyntaxTree.BasicType) THEN
  9166. RETURN TRUE
  9167. END;
  9168. RETURN FALSE
  9169. END IsBasicType;
  9170. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9171. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9172. BEGIN
  9173. baseType := record.baseType;
  9174. IF (baseType # NIL) THEN
  9175. baseType := baseType.resolved;
  9176. IF (baseType IS SyntaxTree.PointerType) THEN
  9177. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9178. END;
  9179. END;
  9180. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9181. recordType := baseType(SyntaxTree.RecordType);
  9182. ELSE
  9183. recordType := NIL;
  9184. END;
  9185. RETURN recordType
  9186. END RecordBase;
  9187. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9188. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9189. BEGIN
  9190. baseRecord := RecordBase(scope.ownerRecord);
  9191. IF baseRecord = NIL THEN RETURN NIL END;
  9192. scope := baseRecord.recordScope;
  9193. procedureType := procedure.type.resolved;
  9194. IF procedure IS SyntaxTree.Operator THEN
  9195. operator := scope.firstOperator;
  9196. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9197. (*
  9198. Printout.Info("not same ",procedureType);
  9199. Printout.Info("with ",operator.type);
  9200. *)
  9201. operator := operator.nextOperator;
  9202. END;
  9203. super := operator;
  9204. ELSE
  9205. super := scope.firstProcedure;
  9206. WHILE (super # NIL) & (super.name # procedure.name) DO
  9207. super := super.nextProcedure;
  9208. END;
  9209. END;
  9210. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9211. RETURN super
  9212. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9213. RETURN super
  9214. ELSE
  9215. RETURN FindSuperProcedure(scope,procedure);
  9216. END;
  9217. END FindSuperProcedure;
  9218. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9219. VAR procedure: SyntaxTree.Procedure;
  9220. BEGIN
  9221. procedure := record.recordScope.constructor;
  9222. IF procedure = NIL THEN
  9223. record := RecordBase(record);
  9224. IF record # NIL THEN
  9225. procedure := GetConstructor(record)
  9226. END;
  9227. END;
  9228. RETURN procedure;
  9229. END GetConstructor;
  9230. (* enter a case into a list of cases in a sorted way and check for collision *)
  9231. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9232. VAR prev,this,new: SyntaxTree.CaseConstant;
  9233. BEGIN
  9234. this := root;
  9235. prev := NIL;
  9236. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9237. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9238. RETURN FALSE
  9239. ELSE
  9240. IF (this # NIL) & (this.min = max+1) THEN
  9241. this.min := min
  9242. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9243. prev.max := min
  9244. ELSE
  9245. NEW(new); new.min := min; new.max := max;
  9246. new.next := this;
  9247. IF prev = NIL THEN
  9248. root := new;
  9249. ELSE
  9250. prev.next := new
  9251. END
  9252. END;
  9253. RETURN TRUE
  9254. END;
  9255. END EnterCase;
  9256. (** generate and return a new checker object, errors are entered into diagnostics **)
  9257. 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;
  9258. VAR checker: Checker;
  9259. BEGIN
  9260. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9261. RETURN checker
  9262. END NewChecker;
  9263. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9264. VAR warnings: Warnings;
  9265. BEGIN
  9266. NEW(warnings, diagnostics); RETURN warnings;
  9267. END NewWarnings;
  9268. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9269. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9270. END IsRangeType;
  9271. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9272. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9273. END IsMathArrayType;
  9274. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9275. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9276. END IsArrayType;
  9277. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9278. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9279. END IsComplexType;
  9280. (** if a type is an array-structured object type *)
  9281. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9282. VAR recordType: SyntaxTree.RecordType;
  9283. BEGIN
  9284. IF type = NIL THEN
  9285. RETURN FALSE
  9286. ELSE
  9287. type := type.resolved;
  9288. IF type IS SyntaxTree.PointerType THEN
  9289. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9290. IF type IS SyntaxTree.RecordType THEN
  9291. recordType := type(SyntaxTree.RecordType);
  9292. RETURN recordType.isObject & recordType.HasArrayStructure()
  9293. ELSE
  9294. RETURN FALSE
  9295. END
  9296. ELSE
  9297. RETURN FALSE
  9298. END
  9299. END
  9300. END IsArrayStructuredObjectType;
  9301. (** the math array structure of a type
  9302. - for math arrays: the array itself
  9303. - for pointers: the math array structure of the pointer base
  9304. - for array-structured object types: the underlying structure
  9305. - for non-math arrays and all other types: NIL
  9306. **)
  9307. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9308. VAR
  9309. result: SyntaxTree.MathArrayType;
  9310. BEGIN
  9311. IF type = NIL THEN
  9312. result := NIL
  9313. ELSE
  9314. type := type.resolved;
  9315. IF type IS SyntaxTree.PointerType THEN
  9316. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9317. END;
  9318. IF type IS SyntaxTree.MathArrayType THEN
  9319. result := type(SyntaxTree.MathArrayType)
  9320. ELSIF type IS SyntaxTree.RecordType THEN
  9321. result := type(SyntaxTree.RecordType).arrayStructure
  9322. ELSE
  9323. result := NIL
  9324. END
  9325. END;
  9326. RETURN result
  9327. END MathArrayStructureOfType;
  9328. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9329. VAR
  9330. result: BOOLEAN;
  9331. rangeExpression: SyntaxTree.RangeExpression;
  9332. BEGIN
  9333. IF x IS SyntaxTree.RangeExpression THEN
  9334. rangeExpression := x(SyntaxTree.RangeExpression);
  9335. result := TRUE;
  9336. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9337. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9338. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9339. ELSE
  9340. result := FALSE
  9341. END;
  9342. RETURN result
  9343. END IsStaticRange;
  9344. (** whether a type is a math array of tensor form **)
  9345. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9346. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9347. END IsTensor;
  9348. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9349. BEGIN
  9350. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9351. length := type(SyntaxTree.MathArrayType).staticLength;
  9352. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9353. RETURN TRUE
  9354. ELSE
  9355. RETURN FALSE
  9356. END;
  9357. END IsStaticMathArray;
  9358. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9359. BEGIN
  9360. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9361. END SymbolHasAddress;
  9362. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9363. BEGIN
  9364. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9365. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9366. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9367. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9368. END HasAddress;
  9369. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9370. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9371. BEGIN
  9372. IF (e IS SyntaxTree.Designator) THEN
  9373. d := e(SyntaxTree.Designator);
  9374. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9375. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9376. e := d.left;
  9377. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9378. END;
  9379. IF d # NIL THEN
  9380. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9381. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9382. END;
  9383. END;
  9384. RETURN FALSE;
  9385. END IsLocalVariable;
  9386. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9387. BEGIN
  9388. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9389. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9390. ELSE
  9391. RETURN TRUE
  9392. END;
  9393. END IsStaticProcedure;
  9394. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9395. CONST OptimizeMethodTable = FALSE;
  9396. BEGIN
  9397. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9398. END InMethodTable;
  9399. END FoxSemanticChecker.