FoxSemanticChecker.Mod 401 KB

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