FoxSemanticChecker.Mod 398 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. 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: Position; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position, "base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position, "base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position, "realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position, "forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position, "forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position, "forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. parameter := procedureType.selfParameter;
  725. IF parameter # NIL THEN
  726. VisitParameter(parameter)
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  792. IF svalue = "A2" THEN
  793. RETURN TRUE
  794. END;
  795. END;
  796. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  797. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  798. END;
  799. RETURN FALSE;
  800. (*
  801. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  802. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  803. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  804. IF svalue[0] = "~" THEN
  805. Strings.TrimLeft(svalue, "~");
  806. IF svalue = backendName THEN
  807. RETURN TRUE;
  808. END;
  809. ELSIF svalue # backendName THEN
  810. RETURN TRUE;
  811. END;
  812. END;
  813. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  814. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  815. END;
  816. RETURN FALSE;
  817. *)
  818. END SkipImplementation;
  819. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  820. VAR this: SyntaxTree.Modifier;
  821. BEGIN
  822. this := modifiers;
  823. WHILE this # NIL DO
  824. IF ~this.resolved THEN
  825. IF checkUse THEN
  826. Error(this.position,"unexpected modifier");
  827. ELSE
  828. this.SetExpression(ResolveExpression(this.expression));
  829. this.Resolved;
  830. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  831. END;
  832. END;
  833. this := this.nextModifier
  834. END;
  835. END CheckModifiers;
  836. (**
  837. resolve procedure type
  838. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  839. **)
  840. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  841. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  842. BEGIN
  843. IF TypeNeedsResolution(procedureType) THEN
  844. modifiers := procedureType.modifiers;
  845. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  846. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  847. procedureType.SetInterrupt(TRUE);
  848. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  849. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  850. IF useDarwinCCalls THEN (*fld*)
  851. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  852. ELSE
  853. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  854. END
  855. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  856. procedureType.SetNoReturn(TRUE);
  857. END;
  858. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  859. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  860. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  861. CheckModifiers(modifiers, TRUE);
  862. modifiers := procedureType.returnTypeModifiers;
  863. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  864. CheckModifiers(modifiers, TRUE);
  865. typeFixes.Add(procedureType,currentScope);
  866. procedureType.SetHasPointers(procedureType.isDelegate);
  867. procedureType.SetState(SyntaxTree.Resolved);
  868. END;
  869. resolvedType := ResolvedType(procedureType)
  870. END VisitProcedureType;
  871. (** check and resolve record type
  872. - check base type: must be record, math array or array-structured object type
  873. - check declarations
  874. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  875. **)
  876. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  877. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  878. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  879. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  880. hasPointers: BOOLEAN;
  881. modifiers: SyntaxTree.Modifier;
  882. value: LONGINT;
  883. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  884. BEGIN
  885. type := type.resolved;
  886. IF (type IS SyntaxTree.PointerType) &
  887. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  888. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  889. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  890. RETURN TRUE
  891. ELSE
  892. RETURN FALSE
  893. END;
  894. END IsPointerToRecord;
  895. BEGIN
  896. IF TypeNeedsResolution(x) THEN
  897. hasPointers := FALSE;
  898. modifiers := x.modifiers;
  899. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  900. CheckModifiers(modifiers, TRUE);
  901. IF x.baseType # NIL THEN
  902. position := x.baseType.position;
  903. baseType := ResolveType(x.baseType);
  904. resolved := baseType.resolved;
  905. hasPointers := hasPointers OR resolved.hasPointers;
  906. IF x.isObject THEN (* object *)
  907. ASSERT(x.pointerType # NIL);
  908. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  909. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  910. baseType := NIL
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  913. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  914. ELSE
  915. Error(position,"object does not extend pointer to record, object or math array ")
  916. END;
  917. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  918. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  919. ELSIF IsPointerToRecord(resolved,recordType) THEN
  920. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  921. ELSIF resolved IS SyntaxTree.RecordType THEN
  922. ELSE
  923. Error(position,"pointer to record does not extend pointer to record or record")
  924. END;
  925. ELSE
  926. IF resolved IS SyntaxTree.RecordType THEN
  927. ELSE
  928. Error(position,"record does not extend record")
  929. END;
  930. END;
  931. x.SetBaseType(baseType);
  932. IF x.Level() > 15 THEN
  933. Error(position, "record/object inheritance level too high");
  934. (* note:
  935. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  936. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  937. inheritance history of a type.
  938. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  939. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  940. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  941. *)
  942. END;
  943. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  944. Error(position, "invalid inheritance of disposable types");
  945. END;
  946. END;
  947. Declarations(x.recordScope, FALSE);
  948. ResolveArrayStructure(x);
  949. (* computation of sizes and offsets skipped -> done in backend / system *)
  950. recordBase := x.GetBaseRecord();
  951. IF recordBase = NIL THEN numberMethods := 0
  952. ELSE numberMethods := recordBase.recordScope.numberMethods
  953. END;
  954. isRealtime := TRUE;
  955. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  956. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  957. END;
  958. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  959. WHILE symbol # NIL DO
  960. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  961. IF symbol IS SyntaxTree.Variable THEN
  962. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  963. END;
  964. IF symbol IS SyntaxTree.Procedure THEN
  965. procedure := symbol(SyntaxTree.Procedure);
  966. IF procedure.super # NIL THEN
  967. procedure.SetMethodNumber(procedure.super.methodNumber)
  968. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  969. procedure.SetMethodNumber(numberMethods);
  970. INC(numberMethods);
  971. END;
  972. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  973. Error(procedure.position,"realtime procedure in non-realtime object")
  974. END;
  975. END;
  976. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  977. Error(symbol.position,"non-realtime symbol in realtime object")
  978. END;
  979. symbol := symbol.nextSymbol;
  980. END;
  981. IF isRealtime THEN x.SetRealtime(TRUE) END;
  982. x.recordScope.SetNumberMethods(numberMethods);
  983. (* TODO: is this needed anymore? *)
  984. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  985. Error(x.position,"object extends a record")
  986. END;
  987. IF (x.typeDeclaration = NIL) THEN
  988. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  989. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  990. (*
  991. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  992. AnonymousTypeDeclaration(x,name);
  993. *)
  994. ELSE
  995. AnonymousTypeDeclaration(x,"Anonymous");
  996. END;
  997. END;
  998. x.SetHasPointers(hasPointers);
  999. x.SetState(SyntaxTree.Resolved);
  1000. END;
  1001. resolvedType := ResolvedType(x);
  1002. END VisitRecordType;
  1003. (** check and resolve cell type
  1004. - check base type: must be cell
  1005. - check declarations
  1006. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1007. **)
  1008. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1009. VAR
  1010. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1011. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1012. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1013. recordBase: SyntaxTree.RecordType;
  1014. numberMethods, int: LONGINT;
  1015. real: LONGREAL;
  1016. bool: BOOLEAN;
  1017. set: SET;
  1018. v: SyntaxTree.Expression;
  1019. str: Scanner.StringType;
  1020. atype: SyntaxTree.ArrayType;
  1021. prev: SyntaxTree.Scope;
  1022. skip: BOOLEAN;
  1023. svalue: ARRAY 32 OF CHAR;
  1024. BEGIN
  1025. IF TypeNeedsResolution(x) THEN
  1026. recordBase := NIL;
  1027. IF cellsAreObjects THEN
  1028. IF x.baseType = NIL THEN
  1029. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1030. ImportModule(qualifiedIdentifier.prefix, x.position);
  1031. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1032. x.SetBaseType(ResolveType(x.baseType));
  1033. recordBase := x.GetBaseRecord();
  1034. IF recordBase = NIL THEN
  1035. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1036. END;
  1037. ELSE
  1038. x.SetBaseType(ResolveType(x.baseType));
  1039. END;
  1040. ELSE
  1041. x.SetBaseType(ResolveType(x.baseType));
  1042. END;
  1043. IF recordBase = NIL THEN numberMethods := 0
  1044. ELSE numberMethods := recordBase.recordScope.numberMethods
  1045. END;
  1046. modifier := x.modifiers;
  1047. (*IF ~x.isCellNet THEN*)
  1048. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1049. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1050. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1051. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1052. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1053. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1054. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1055. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1056. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1057. symbol := system.activeCellsCapabilities;
  1058. WHILE symbol # NIL DO
  1059. IF HasFlag(modifier, symbol.name, position) THEN END;
  1060. symbol := symbol.nextSymbol;
  1061. END;
  1062. modifier := x.modifiers;
  1063. WHILE (modifier # NIL) DO
  1064. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1065. IF modifier.expression # NIL THEN
  1066. v := ConstantExpression(modifier.expression);
  1067. property.SetValue(v);
  1068. IF IsIntegerValue(modifier.expression, int) THEN
  1069. (*property.SetValue(modifier.expression);*)
  1070. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1071. property.SetType(system.longintType);
  1072. ELSIF IsRealValue(modifier.expression, real) THEN
  1073. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1074. property.SetType(system.longrealType);
  1075. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1076. property.SetType(system.booleanType);
  1077. ELSIF IsSetValue(modifier.expression, set) THEN
  1078. property.SetType(system.setType);
  1079. ELSIF IsStringValue(modifier.expression, str) THEN
  1080. (*property.SetValue(modifier.expression);*)
  1081. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1082. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1083. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1084. property.SetType(atype);
  1085. ELSE
  1086. Error(modifier.position, "unsupported property type");
  1087. END;
  1088. ELSE (* flag property *)
  1089. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1090. property.SetType(system.booleanType);
  1091. END;
  1092. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1093. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1094. x.AddProperty(property);
  1095. modifier := modifier.nextModifier;
  1096. END;
  1097. CheckModifiers(modifier, FALSE);
  1098. Declarations(x.cellScope, SkipImplementation(x));
  1099. (* process parameters *)
  1100. prev := currentScope;
  1101. currentScope := x.cellScope;
  1102. parameter :=x.firstParameter;
  1103. WHILE (parameter # NIL) DO
  1104. VisitParameter(parameter);
  1105. type := parameter.type.resolved;
  1106. IF ~(type IS SyntaxTree.PortType) THEN
  1107. WHILE IsStaticArray(type, type, len) DO
  1108. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1109. END;
  1110. WHILE IsDynamicArray(type, type) DO
  1111. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1112. END;
  1113. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1114. Error(parameter.position, "invalid type, must be port or static array of port ");
  1115. END;
  1116. END;
  1117. parameter := parameter.nextParameter;
  1118. END;
  1119. currentScope := prev;
  1120. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1121. WHILE symbol # NIL DO
  1122. IF symbol IS SyntaxTree.Variable THEN
  1123. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1124. END;
  1125. symbol := symbol.nextSymbol;
  1126. END;
  1127. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1128. IF (x.typeDeclaration = NIL) THEN
  1129. AnonymousTypeDeclaration(x,"Anonymous");
  1130. END;
  1131. x.SetState(SyntaxTree.Resolved);
  1132. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1133. Warning(x.position, "Forbidden empty Body.");
  1134. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1135. Warning(x.position, "Non-empty body for an engine?");
  1136. END;
  1137. END;
  1138. resolvedType := ResolvedType(x);
  1139. END VisitCellType;
  1140. (* check if an object is an array-structured object type
  1141. - determine the array structure
  1142. - collect operators from top to bottom in the inheritance hierarchy
  1143. - check if LEN operator is declared
  1144. - determine number of possible index operators
  1145. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1146. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1147. *)
  1148. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1149. VAR
  1150. indexOperatorCount, i: LONGINT;
  1151. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1152. isTensor: BOOLEAN;
  1153. BEGIN
  1154. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1155. (* determine array structure *)
  1156. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1157. END;
  1158. IF recordType.HasArrayStructure() THEN
  1159. (* the object is an ASOT *)
  1160. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1161. (* reset array access operators *)
  1162. arrayAccessOperators.len := NIL;
  1163. arrayAccessOperators.generalRead := NIL;
  1164. arrayAccessOperators.generalWrite := NIL;
  1165. IF isTensor THEN
  1166. (* all operators of dimensionalities 1 to max *)
  1167. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1168. ELSE
  1169. (* all operators of certain dimensionality *)
  1170. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1171. END;
  1172. NEW(arrayAccessOperators.read, indexOperatorCount);
  1173. NEW(arrayAccessOperators.write, indexOperatorCount);
  1174. FOR i := 0 TO indexOperatorCount - 1 DO
  1175. arrayAccessOperators.read[i] := NIL;
  1176. arrayAccessOperators.write[i] := NIL
  1177. END;
  1178. (* collect access operators in the record scope *)
  1179. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1180. IF arrayAccessOperators.len = NIL THEN
  1181. (* TODO: think about making this operator optional for static array structures *)
  1182. Error(recordType.position, "LEN operator missing")
  1183. END;
  1184. (* show error messages *)
  1185. IF isTensor THEN
  1186. (* require ARRAY [*] OF RANGE *)
  1187. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1188. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1189. ELSE
  1190. (* forbid ARRAY [*] OF RANGE *)
  1191. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1192. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1193. (* require RANGE, RANGE, ... RANGE *)
  1194. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1195. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1196. END;
  1197. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1198. ELSE
  1199. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1200. IF recordType.recordScope.firstOperator # NIL THEN
  1201. RETURN;
  1202. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1203. END
  1204. END
  1205. END ResolveArrayStructure;
  1206. (** collect array access operators in a record scope **)
  1207. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1208. VAR
  1209. baseType: SyntaxTree.Type;
  1210. operator: SyntaxTree.Operator;
  1211. isReadOperator, isGeneralOperator: BOOLEAN;
  1212. indexListSize, indexListKind, hashValue: LONGINT;
  1213. BEGIN
  1214. (* if a parent record scope exists, collect the operators there first *)
  1215. baseType := recordScope.ownerRecord.baseType;
  1216. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1217. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1218. END;
  1219. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1220. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1221. END;
  1222. (* go through all operators in the current record scope *)
  1223. operator := recordScope.firstOperator;
  1224. WHILE operator # NIL DO
  1225. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1226. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1227. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1228. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1229. IF isGeneralOperator THEN
  1230. IF isReadOperator THEN
  1231. arrayAccessOperators.generalRead := operator
  1232. ELSE
  1233. arrayAccessOperators.generalWrite := operator
  1234. END
  1235. ELSE
  1236. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1237. IF isReadOperator THEN
  1238. arrayAccessOperators.read[hashValue] := operator
  1239. ELSE
  1240. arrayAccessOperators.write[hashValue] := operator
  1241. END
  1242. END
  1243. END
  1244. ELSE
  1245. Error(operator.position, 'invalid operator')
  1246. END;
  1247. operator := operator.nextOperator
  1248. END
  1249. END CollectArrayAccessOperators;
  1250. (** the hash value of an index operator **)
  1251. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1252. VAR result: LONGINT;
  1253. BEGIN
  1254. IF isTensor THEN
  1255. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1256. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1257. ELSE
  1258. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1259. END
  1260. ELSE
  1261. result := indexListKind
  1262. END;
  1263. RETURN result
  1264. END IndexOperatorHash;
  1265. (** 2 to the power of exponent **)
  1266. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1267. VAR result, i: LONGINT;
  1268. BEGIN
  1269. result := 1;
  1270. FOR i := 1 TO exponent DO
  1271. result := result * 2;
  1272. END;
  1273. RETURN result
  1274. END TwoToThePowerOf;
  1275. (** check if a LEN operator has a correct signature. i.e.
  1276. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1277. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1278. **)
  1279. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1280. VAR
  1281. procedureType: SyntaxTree.ProcedureType;
  1282. returnedArrayType: SyntaxTree.MathArrayType;
  1283. result: BOOLEAN;
  1284. BEGIN
  1285. result := FALSE;
  1286. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1287. IF (procedureType.numberParameters = 0) THEN
  1288. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1289. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1290. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1291. IF returnedArrayType.form = SyntaxTree.Open THEN
  1292. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1293. result := TRUE
  1294. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1295. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1296. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1297. result := TRUE
  1298. END
  1299. END
  1300. END
  1301. END
  1302. END;
  1303. IF result THEN
  1304. (* export symbol automatically *)
  1305. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1306. ELSE
  1307. Error(operator.position, "LEN operator with invalid signature");
  1308. END;
  1309. RETURN result
  1310. END CheckLenOperator;
  1311. (** check if an index operator has a correct signature. i.e.
  1312. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1313. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1314. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1315. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1316. - determine if it is a read or write operator (existance of return type)
  1317. - check index parameters
  1318. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1319. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1320. [LONGINT] -> binary 0 -> 0
  1321. [RANGE] -> binary 1 -> 1
  1322. [LONGINT, LONGINT] -> binary 00 -> 0
  1323. [LONGINT, RANGE] -> binary 01 -> 1
  1324. [RANGE, LONGINT] -> binary 10 -> 2
  1325. [RANGE, RANGE] -> binary 11 -> 3
  1326. etc.
  1327. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1328. - for read operators, check if return type matches the type of data that is read
  1329. - for write operators, check if last parameter type matches the type of data that is written
  1330. **)
  1331. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1332. VAR
  1333. elementType, otherElementType, dataType: SyntaxTree.Type;
  1334. procedureType: SyntaxTree.ProcedureType;
  1335. mathArrayType: SyntaxTree.MathArrayType;
  1336. parameter: SyntaxTree.Parameter;
  1337. parameterCount, rangeCount, i: LONGINT;
  1338. hasTypeError: BOOLEAN;
  1339. BEGIN
  1340. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1341. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1342. (* determine if it is a read or write operator *)
  1343. isReadOperator := (procedureType.returnType # NIL);
  1344. IF isReadOperator THEN
  1345. indexListSize := parameterCount;
  1346. ELSE
  1347. indexListSize := parameterCount - 1;
  1348. END;
  1349. IF indexListSize < 1 THEN
  1350. Error(operator.position, "index operator with too few parameters");
  1351. RETURN FALSE
  1352. END;
  1353. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1354. (* general operator *)
  1355. isGeneralOperator := TRUE;
  1356. IF indexListSize > 1 THEN
  1357. Error(operator.position, "index operator with too many parameters");
  1358. RETURN FALSE
  1359. END;
  1360. (* ARRAY [*] OF RANGE*)
  1361. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1362. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1363. Error(operator.position, "index parameter not dynamic math array of range");
  1364. RETURN FALSE
  1365. END;
  1366. parameter := procedureType.firstParameter.nextParameter
  1367. ELSE
  1368. (* fixed-dim. operator *)
  1369. isGeneralOperator := FALSE;
  1370. (* check number of index parameters *)
  1371. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1372. (* for tensors, limited to a certain size *)
  1373. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1374. Error(operator.position, "too many index parameters for tensor");
  1375. RETURN FALSE
  1376. END
  1377. ELSE
  1378. (* for non-tensors, depends on dimensionality *)
  1379. IF indexListSize # arrayStructure.Dimensionality() THEN
  1380. Error(operator.position, "index parameter count does not match dimensionality");
  1381. RETURN FALSE
  1382. END
  1383. END;
  1384. (* go through all index parameters
  1385. - count the number of ranges
  1386. - determine the index list kind number
  1387. *)
  1388. indexListKind := 0;
  1389. rangeCount := 0;
  1390. parameter := procedureType.firstParameter;
  1391. FOR i := 1 TO indexListSize DO
  1392. indexListKind := indexListKind * 2;
  1393. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1394. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1395. INC(indexListKind);
  1396. INC(rangeCount)
  1397. ELSE
  1398. Error(parameter.position, "integer or range expected");
  1399. RETURN FALSE
  1400. END;
  1401. parameter := parameter.nextParameter
  1402. END;
  1403. END;
  1404. (*
  1405. - for read operators: check type of last parameter
  1406. - for write operators: check return type
  1407. *)
  1408. IF isReadOperator THEN
  1409. dataType := procedureType.returnType (* the return type *)
  1410. ELSE
  1411. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1412. END;
  1413. elementType := arrayStructure.ElementType();
  1414. hasTypeError := FALSE;
  1415. IF isGeneralOperator THEN
  1416. (* ARRAY [?] OF <Element> *)
  1417. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1418. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1419. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1420. hasTypeError := TRUE
  1421. END
  1422. ELSE
  1423. hasTypeError := TRUE
  1424. END
  1425. ELSE
  1426. IF rangeCount = 0 THEN
  1427. (* <Element> *)
  1428. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1429. ELSE
  1430. (* ARRAY [*, *, ..., *] OF <Element> *)
  1431. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1432. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1433. IF mathArrayType.IsFullyDynamic() THEN
  1434. IF mathArrayType.Dimensionality() = rangeCount THEN
  1435. otherElementType := mathArrayType.ElementType();
  1436. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1437. ELSE
  1438. hasTypeError := TRUE
  1439. END
  1440. ELSE
  1441. hasTypeError := TRUE
  1442. END
  1443. ELSE
  1444. hasTypeError := TRUE
  1445. END
  1446. END
  1447. END;
  1448. IF hasTypeError THEN
  1449. IF isReadOperator THEN
  1450. Error(operator.position, "return type does not match")
  1451. ELSE
  1452. Error(parameter.position, "type of last parameter does not match")
  1453. END;
  1454. RETURN FALSE
  1455. END;
  1456. (* export symbol automatically *)
  1457. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1458. RETURN TRUE
  1459. END CheckIndexOperator;
  1460. (** resolve all pending types (late resolving).
  1461. - type fixes are resolved at the end of the declaration phase
  1462. - type fixes may imply new type fixes that are also entered at the end of the list
  1463. **)
  1464. PROCEDURE FixTypes;
  1465. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1466. BEGIN
  1467. prevScope := currentScope;
  1468. p := typeFixes.Get(currentScope);
  1469. WHILE p # NIL DO
  1470. ASSERT(currentScope # NIL);
  1471. ASSERT(p IS SyntaxTree.Type);
  1472. IF p IS SyntaxTree.PointerType THEN
  1473. FixPointerType(p(SyntaxTree.PointerType))
  1474. ELSIF p IS SyntaxTree.ProcedureType THEN
  1475. FixProcedureType(p(SyntaxTree.ProcedureType))
  1476. ELSE
  1477. HALT(100);
  1478. END;
  1479. p := typeFixes.Get(currentScope);
  1480. END;
  1481. currentScope :=prevScope;
  1482. END FixTypes;
  1483. (**
  1484. resolve type x
  1485. - if x is nil then return nil
  1486. - if x cannot be resolved then the result is invalidType else the result is x
  1487. - the resolved type is entered into x.resolved
  1488. **)
  1489. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR prev,resolved: SyntaxTree.Type;
  1491. BEGIN
  1492. prev := resolvedType;
  1493. resolvedType := SyntaxTree.invalidType;
  1494. IF x = NIL THEN resolvedType := NIL
  1495. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1496. END;
  1497. resolved := resolvedType;
  1498. resolvedType := prev;
  1499. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1500. RETURN resolved
  1501. END ResolveType;
  1502. (*** compatibility rules ***)
  1503. (**
  1504. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1505. **)
  1506. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1507. VAR result: SyntaxTree.Type;
  1508. BEGIN
  1509. result := SyntaxTree.invalidType;
  1510. IF type = NIL THEN Error(position, "expression of type NIL");
  1511. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1512. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1513. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1514. ELSE result := type.resolved
  1515. END;
  1516. RETURN result
  1517. END RegularType;
  1518. (** returns signature compatibility of procedure types this and to
  1519. - if not compatible then error is reported
  1520. - compatibility means type equality
  1521. **)
  1522. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1523. VAR result: BOOLEAN;
  1524. BEGIN
  1525. result := SameType(to,this);
  1526. IF ~result THEN
  1527. Error(position, "signature incompatible");
  1528. IF VerboseErrorMessage THEN
  1529. Printout.Info("this",this);
  1530. Printout.Info("to",to);
  1531. END;
  1532. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1533. Error(position, "signature incompatible: realtime flag must be inherited");
  1534. END;
  1535. RETURN result
  1536. END SignatureCompatible;
  1537. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1538. - for var parameters compatibility means same type except for
  1539. - formal is of open array of system byte
  1540. - formal is of record type
  1541. - formal is of open array type
  1542. - formal is of open math array type
  1543. - for value parameters compatibllity means assignment compatibility except for
  1544. - formal is of open array type
  1545. if compatible the return true else report error and return false
  1546. **)
  1547. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1548. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1549. BEGIN
  1550. formalType := RegularType(formal.position,formal.type);
  1551. actualType := RegularType(actual.position,actual.type);
  1552. error := FALSE;
  1553. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1554. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1555. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1556. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1557. END;
  1558. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1559. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1560. ELSIF ~IsVariable(actual) THEN
  1561. result := FALSE; error := TRUE;
  1562. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1563. Error(actual.position,"not a variable: no operator for writing");
  1564. ELSE
  1565. Error(actual.position,"is not a variable");
  1566. END;
  1567. IF VerboseErrorMessage THEN
  1568. Printout.Info("actual",actual);
  1569. Printout.Info("formal",formal);
  1570. END;
  1571. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1572. result := CompatibleTo(system,actualType,formalType);
  1573. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1574. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1575. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1576. IF IsArrayStructuredObjectType(actualType) THEN
  1577. actualType := MathArrayStructureOfType(actualType)
  1578. END;
  1579. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1580. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1581. Error(actual.position,"incompatible non-static actual type");
  1582. END;
  1583. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1584. Error(actual.position,"incompatible tensor (use a range expression)");
  1585. END;
  1586. ELSE
  1587. result := SameType(actualType,formalType)
  1588. END
  1589. ELSE
  1590. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1591. actualType := system.characterType;
  1592. END;
  1593. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1594. result := TRUE; (* special rule for WINAPI parameters *)
  1595. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1596. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1597. ELSE
  1598. result := CompatibleTo(system,actualType,formalType);
  1599. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1600. Error(actual.position,"incompatible non-static actual type");
  1601. END;
  1602. END;
  1603. END;
  1604. IF ~result & ~error THEN
  1605. Error(actual.position,"incompatible parameter");
  1606. IF VerboseErrorMessage THEN
  1607. Printout.Info("actual",actual);
  1608. Printout.Info("formal",formal);
  1609. END;
  1610. END;
  1611. RETURN result
  1612. END ParameterCompatible;
  1613. (** check compatibility for expressions of the form left := right
  1614. - if compatible then return true else error report and return false
  1615. - check if left is variable
  1616. - check compatiblity
  1617. **)
  1618. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1619. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1620. BEGIN
  1621. result := FALSE;
  1622. leftType := RegularType(left.position,left.type);
  1623. rightType := RegularType(right.position,right.type);
  1624. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1625. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1626. END;
  1627. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1628. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1629. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1630. (* error already handled *)
  1631. result := TRUE;
  1632. ELSIF ~IsVariable(left) THEN
  1633. Error(left.position,"is not a variable");
  1634. IF VerboseErrorMessage THEN
  1635. Printout.Info("left",left);
  1636. Printout.Info("right",right);
  1637. END;
  1638. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1639. result := TRUE;
  1640. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1641. result := TRUE
  1642. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1643. Error(left.position,"incompatible assignment");
  1644. IF VerboseErrorMessage THEN
  1645. Printout.Info("left",left);
  1646. Printout.Info("right",right);
  1647. END;
  1648. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1649. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1650. Error(right.position,"forbidden assignment of a nested procedure");
  1651. ELSE
  1652. result := TRUE
  1653. END;
  1654. RETURN result
  1655. END AssignmentCompatible;
  1656. (*** values ***)
  1657. (** check and resolve integer value **)
  1658. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1659. VAR hugeint: HUGEINT;
  1660. BEGIN
  1661. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1662. value.SetType(Global.GetIntegerType(system,hugeint));
  1663. resolvedExpression := value
  1664. END VisitIntegerValue;
  1665. (** check and resolve real value **)
  1666. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1667. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1668. BEGIN
  1669. subtype := value(SyntaxTree.RealValue).subtype;
  1670. IF subtype = Scanner.Real THEN
  1671. type := system.realType
  1672. ELSIF subtype = Scanner.Longreal THEN
  1673. type := system.longrealType
  1674. ELSE
  1675. HALT(100)
  1676. END;
  1677. value.SetType(type);
  1678. resolvedExpression := value
  1679. END VisitRealValue;
  1680. (** check and resolve complex value **)
  1681. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1682. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1683. BEGIN
  1684. subtype := value(SyntaxTree.ComplexValue).subtype;
  1685. IF subtype = Scanner.Real THEN
  1686. type := system.complexType
  1687. ELSIF subtype = Scanner.Longreal THEN
  1688. type := system.longcomplexType
  1689. ELSE
  1690. HALT(100)
  1691. END;
  1692. value.SetType(type);
  1693. resolvedExpression := value
  1694. END VisitComplexValue;
  1695. (** check and resolve set value **)
  1696. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1697. BEGIN
  1698. value.SetType(system.setType);
  1699. resolvedExpression := value
  1700. END VisitSetValue;
  1701. (** check and resolve set value **)
  1702. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1703. BEGIN
  1704. value.SetType(SyntaxTree.invalidType);
  1705. resolvedExpression := value
  1706. END VisitMathArrayValue;
  1707. (** check and resolve boolean value **)
  1708. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1709. BEGIN
  1710. value.SetType(system.booleanType);
  1711. resolvedExpression := value
  1712. END VisitBooleanValue;
  1713. (** check and resolve string value **)
  1714. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1715. BEGIN
  1716. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1717. resolvedExpression := value
  1718. END VisitStringValue;
  1719. (** check and resolve character value **)
  1720. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1721. BEGIN
  1722. value.SetType(system.characterType);
  1723. resolvedExpression := value
  1724. END VisitCharacterValue;
  1725. (** check and resolve nil value **)
  1726. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1727. BEGIN
  1728. value.SetType(system.nilType);
  1729. resolvedExpression := value
  1730. END VisitNilValue;
  1731. (** check and resolve enumerator value **)
  1732. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1733. BEGIN
  1734. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1735. ASSERT(value.type # NIL);
  1736. resolvedExpression := value
  1737. END VisitEnumerationValue;
  1738. (*** expressions ***)
  1739. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1740. - check all elements on integer type
  1741. - if element range is constant, then check lower and upper bound
  1742. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1743. if an error occurs then report error and return invalidExpression
  1744. **)
  1745. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1746. VAR
  1747. i: LONGINT;
  1748. element: SyntaxTree.Expression;
  1749. constant: BOOLEAN;
  1750. elements: SyntaxTree.ExpressionList;
  1751. s: SET;
  1752. result: SyntaxTree.Expression;
  1753. value: SyntaxTree.Value;
  1754. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1755. VAR
  1756. left, right: SyntaxTree.Expression;
  1757. elementResult: SyntaxTree.Expression;
  1758. leftInteger, rightInteger, temp: LONGINT;
  1759. BEGIN
  1760. (* set context of range *)
  1761. IF element IS SyntaxTree.RangeExpression THEN
  1762. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1763. END;
  1764. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1765. IF elementResult = SyntaxTree.invalidExpression THEN
  1766. (* error already reported *)
  1767. constant := FALSE
  1768. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1769. (* the element is a range expression *)
  1770. (* extract left and right hand side of range *)
  1771. left := elementResult(SyntaxTree.RangeExpression).first;
  1772. right := elementResult(SyntaxTree.RangeExpression).last;
  1773. (* guaranteed by VisitRangeExpression: *)
  1774. ASSERT((left # NIL) & (right # NIL));
  1775. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1776. ELSE
  1777. (* the element is not a range expression *)
  1778. (* check type and add conversion if needed *)
  1779. IF IsIntegerType(elementResult.type.resolved) THEN
  1780. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1781. ELSE
  1782. Error(elementResult.position, "non integer element in set");
  1783. elementResult := SyntaxTree.invalidExpression;
  1784. constant := FALSE
  1785. END;
  1786. left := elementResult;
  1787. right := elementResult
  1788. END;
  1789. IF elementResult # SyntaxTree.invalidExpression THEN
  1790. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1791. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1792. Error(left.position,"not allowed set integer value");
  1793. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1794. Error(right.position,"not allowed set integer value");
  1795. END
  1796. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1797. Error(right.position,"not allowed set integer value");
  1798. ELSE
  1799. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1800. s := {};
  1801. ELSE
  1802. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1803. IF leftInteger < 0 THEN leftInteger := 0 END;
  1804. (*!!!!!!!!! this is a hack !!!!!!! *)
  1805. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1806. extends the range x..31 to x..63 !!!!!! *)
  1807. s := s + {leftInteger..rightInteger};
  1808. END;
  1809. END;
  1810. ELSE
  1811. constant := FALSE;
  1812. END
  1813. END;
  1814. RETURN elementResult
  1815. END CheckElement;
  1816. BEGIN
  1817. result := set; constant := TRUE; s := {}; elements := set.elements;
  1818. IF elements # NIL THEN
  1819. FOR i := 0 TO elements.Length()-1 DO
  1820. element := elements.GetExpression(i);
  1821. element := CheckElement(element);
  1822. IF element = SyntaxTree.invalidExpression THEN
  1823. result := SyntaxTree.invalidExpression
  1824. END;
  1825. elements.SetExpression(i,element);
  1826. END;
  1827. END;
  1828. IF constant THEN
  1829. value := SyntaxTree.NewSetValue(set.position,s);
  1830. value.SetType(system.setType);
  1831. result.SetResolved(value);
  1832. END;
  1833. (* optimization possible
  1834. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1835. left this to the programmer...
  1836. *)
  1837. result.SetType(system.setType);
  1838. resolvedExpression := result;
  1839. END VisitSet;
  1840. (*
  1841. old variant: quite generic but needs better conversion handling, do this?
  1842. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1843. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1844. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1845. BEGIN
  1846. type := NIL;
  1847. numberElements := x.elements.Length();
  1848. FOR i := 0 TO numberElements-1 DO
  1849. expression := x.elements.GetExpression(i);
  1850. position := expression.position;
  1851. expression := ResolveExpression(x.elements.GetExpression(i));
  1852. x.elements.SetExpression(i,de);
  1853. IF type = NIL THEN
  1854. type := expression.type;
  1855. ELSIF CompatibleTo(system,expression.type,type) THEN
  1856. (* ok *)
  1857. ELSIF CompatibleTo(system,type,expression.type) THEN
  1858. type := expression.type
  1859. ELSE
  1860. Error(expression.position, "incompatible element types");
  1861. type := SyntaxTree.invalidType;
  1862. END;
  1863. END;
  1864. isValue := TRUE;
  1865. FOR i := 0 TO numberElements-1 DO
  1866. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1867. x.elements.SetExpression(i,expression);
  1868. isValue := isValue & (expression.resolved # NIL);
  1869. END;
  1870. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1871. arrayType.SetArrayBase(type);
  1872. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1873. IF isValue THEN
  1874. value := SyntaxTree.NewMathArrayValue(position);
  1875. value.SetElements(x.elements);
  1876. x.SetResolved(value);
  1877. END;
  1878. x.SetType(arrayType);
  1879. resolvedExpression := x;
  1880. END VisitMathArrayExpression;
  1881. *)
  1882. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1883. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1884. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1885. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1886. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1887. BEGIN
  1888. numberElements := x.elements.Length();
  1889. FOR i := 0 TO numberElements-1 DO
  1890. expression := x.elements.GetExpression(i);
  1891. IF expression IS SyntaxTree.MathArrayExpression THEN
  1892. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1893. ELSE
  1894. position := expression.position;
  1895. expression := ResolveExpression(x.elements.GetExpression(i));
  1896. x.elements.SetExpression(i,expression);
  1897. IF type = NIL THEN
  1898. type := expression.type;
  1899. ELSIF CompatibleTo(system,expression.type,type) THEN
  1900. (* ok *)
  1901. ELSIF CompatibleTo(system,type,expression.type) THEN
  1902. type := expression.type
  1903. ELSE
  1904. Error(expression.position, "incompatible element types");
  1905. type := SyntaxTree.invalidType;
  1906. END;
  1907. END;
  1908. END;
  1909. END RecursivelyFindType;
  1910. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1911. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. FOR i := 0 TO numberElements-1 DO
  1915. expression := x.elements.GetExpression(i);
  1916. IF expression IS SyntaxTree.MathArrayExpression THEN
  1917. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1918. ELSE
  1919. position := expression.position;
  1920. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1921. x.elements.SetExpression(i,expression);
  1922. isValue := isValue & (expression.resolved # NIL);
  1923. END;
  1924. END;
  1925. END RecursivelySetExpression;
  1926. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1927. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1928. arrayType: SyntaxTree.MathArrayType;
  1929. BEGIN
  1930. numberElements := x.elements.Length();
  1931. baseType := NIL;
  1932. gsize := 0;
  1933. FOR i := 0 TO numberElements-1 DO
  1934. expression := x.elements.GetExpression(i);
  1935. IF expression IS SyntaxTree.MathArrayExpression THEN
  1936. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1937. IF i=0 THEN
  1938. gsize := size;
  1939. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1940. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1941. ELSE expression.SetType(baseType)
  1942. END;
  1943. ELSIF baseType = NIL THEN baseType := type;
  1944. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1945. END;
  1946. END;
  1947. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1948. arrayType.SetArrayBase(baseType);
  1949. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1950. RETURN ResolveType(arrayType);
  1951. END RecursivelySetType;
  1952. BEGIN
  1953. type := NIL;
  1954. RecursivelyFindType(x);
  1955. isValue := TRUE;
  1956. RecursivelySetExpression(x);
  1957. arrayType := RecursivelySetType(x);
  1958. x.SetType(arrayType);
  1959. IF isValue THEN
  1960. value := SyntaxTree.NewMathArrayValue(x.position);
  1961. value.SetArray(x);
  1962. x.SetResolved(value);
  1963. value.SetType(arrayType);
  1964. END;
  1965. x.SetType(arrayType);
  1966. resolvedExpression := x;
  1967. END VisitMathArrayExpression;
  1968. (** check and resolve unary expression **)
  1969. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1970. VAR
  1971. left: SyntaxTree.Expression;
  1972. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1973. bool: BOOLEAN;
  1974. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1975. value: SyntaxTree.Value;
  1976. BEGIN
  1977. type := SyntaxTree.invalidType;
  1978. left := ResolveExpression(unaryExpression.left);
  1979. unaryExpression.SetLeft(left);
  1980. operator := unaryExpression.operator;
  1981. result := unaryExpression;
  1982. IF ~system.operatorDefined[operator] THEN
  1983. Error(left.position,"Operator Not Defined");
  1984. RETURN
  1985. ELSIF left.type = NIL THEN
  1986. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1987. resolvedExpression := SyntaxTree.invalidExpression;
  1988. RETURN
  1989. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1990. RETURN
  1991. END;
  1992. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1993. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1994. END;
  1995. IF operatorCall # NIL THEN
  1996. result := operatorCall;
  1997. type := operatorCall.type;
  1998. (* admissible operators
  1999. Minus number, set
  2000. Not boolean
  2001. *)
  2002. ELSE
  2003. CASE unaryExpression.operator OF
  2004. |Scanner.Minus:
  2005. IF IsIntegerType(left.type.resolved) THEN
  2006. IF left.resolved # NIL THEN
  2007. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2008. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2009. result.SetResolved(value);
  2010. type := Global.GetIntegerType(system,int);
  2011. value.SetType(type);
  2012. ELSE
  2013. type := left.type
  2014. END
  2015. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2016. IF IsRealValue(left,real) THEN
  2017. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2018. result.SetResolved(value);
  2019. type := left.type;
  2020. value.SetType(type);
  2021. ELSE
  2022. type := left.type;
  2023. END;
  2024. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2025. IF IsSetValue(left,set) THEN
  2026. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2027. result.SetResolved(value);
  2028. type := left.type;
  2029. value.SetType(type);
  2030. ELSE
  2031. type := left.type;
  2032. END;
  2033. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2034. IF IsComplexValue(left, real, imaginary) THEN
  2035. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2036. result.SetResolved(value);
  2037. type := left.type;
  2038. value.SetType(type);
  2039. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2040. ELSE
  2041. type := left.type;
  2042. END
  2043. ELSE
  2044. Error(left.position,"unary operator not applicable");
  2045. END;
  2046. |Scanner.Not:
  2047. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2048. IF IsBooleanValue(left,bool) THEN
  2049. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2050. result.SetResolved(value);
  2051. type := system.booleanType;
  2052. value.SetType(type);
  2053. ELSE
  2054. type := system.booleanType;
  2055. END;
  2056. ELSE
  2057. Error(left.position,"unary operator not applicable");
  2058. END;
  2059. |Scanner.Plus:
  2060. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2061. result := left; type := left.type;
  2062. ELSE
  2063. Error(left.position,"unary operator not applicable");
  2064. END;
  2065. (* ADDRESS OF *)
  2066. |Scanner.Address:
  2067. IF HasAddress(left) THEN
  2068. type := system.addressType;
  2069. ELSE
  2070. type := SyntaxTree.invalidType;
  2071. Error(left.position,"has no address");
  2072. Printout.Info("par", left);
  2073. END;
  2074. (* SIZE OF *)
  2075. |Scanner.Size:
  2076. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2077. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2078. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2079. value := SyntaxTree.NewIntegerValue(left.position, int);
  2080. result.SetResolved(value);
  2081. type := Global.GetIntegerType(system,int);
  2082. value.SetType(type)
  2083. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2084. ELSE
  2085. (* for variables, system sizeof could represent the physically occupied size
  2086. determined via the type descriptor, implement that ? *)
  2087. Error(left.position,"is not a type symbol");
  2088. END
  2089. (* ALIAS OF *)
  2090. |Scanner.Alias:
  2091. type := left.type.resolved;
  2092. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2093. type := SyntaxTree.invalidType;
  2094. Error(left.position,"alias on non math array type");
  2095. END;
  2096. ELSE
  2097. Error(left.position,"unary operator not defined");
  2098. END;
  2099. END;
  2100. result.SetType(type);
  2101. resolvedExpression := result
  2102. END VisitUnaryExpression;
  2103. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2104. VAR
  2105. result: SyntaxTree.Expression;
  2106. array: SyntaxTree.MathArrayExpression;
  2107. value: SyntaxTree.MathArrayValue;
  2108. isValue: BOOLEAN;
  2109. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2110. BEGIN
  2111. type := type.resolved;
  2112. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2113. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2114. END;
  2115. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2116. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2117. END;
  2118. RETURN type
  2119. END BaseType;
  2120. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2121. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2122. BEGIN
  2123. numberElements := x.elements.Length();
  2124. FOR i := 0 TO numberElements-1 DO
  2125. expression := x.elements.GetExpression(i);
  2126. IF expression IS SyntaxTree.MathArrayExpression THEN
  2127. array := SyntaxTree.NewMathArrayExpression(position);
  2128. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2129. expression := array;
  2130. ELSE
  2131. position := expression.position;
  2132. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2133. isValue := isValue & (expression.resolved # NIL);
  2134. END;
  2135. to.elements.AddExpression(expression);
  2136. END;
  2137. END RecursivelyConvert;
  2138. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2139. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2140. arrayType: SyntaxTree.MathArrayType;
  2141. BEGIN
  2142. numberElements := x.elements.Length();
  2143. baseType := NIL;
  2144. gsize := 0;
  2145. FOR i := 0 TO numberElements-1 DO
  2146. expression := x.elements.GetExpression(i);
  2147. IF expression IS SyntaxTree.MathArrayExpression THEN
  2148. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2149. IF i=0 THEN
  2150. gsize := size;
  2151. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2152. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2153. ELSE expression.SetType(baseType)
  2154. END;
  2155. ELSIF baseType = NIL THEN baseType := type;
  2156. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2157. END;
  2158. END;
  2159. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2160. arrayType.SetArrayBase(baseType);
  2161. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2162. RETURN ResolveType(arrayType);
  2163. END RecursivelySetType;
  2164. BEGIN
  2165. result := SyntaxTree.invalidExpression;
  2166. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2167. result := expression (* do not convert *)
  2168. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2169. isValue := TRUE;
  2170. type := BaseType(type);
  2171. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2172. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2173. value := SyntaxTree.NewMathArrayValue(array.position);
  2174. value.SetArray(array);
  2175. value.SetType(RecursivelySetType(array));
  2176. result := value;
  2177. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2178. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2179. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2180. IF result = NIL THEN
  2181. result := SyntaxTree.invalidExpression;
  2182. Error(position, "incompatible conversion");
  2183. IF VerboseErrorMessage THEN
  2184. Printout.Info("expression",expression);
  2185. Printout.Info("type",type);
  2186. END;
  2187. END;
  2188. END;
  2189. RETURN result
  2190. END MathArrayConversion;
  2191. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2192. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2193. BEGIN
  2194. result := expression; type := type.resolved;
  2195. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2196. (* skip, no conversion *)
  2197. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2198. int := expression(SyntaxTree.IntegerValue).hvalue;
  2199. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2200. int := Global.ConvertSigned(int,system.SizeOf(type));
  2201. result := SyntaxTree.NewIntegerValue(position,int);
  2202. result.SetType(type);
  2203. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2204. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2205. result := SyntaxTree.NewIntegerValue(position,int);
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.FloatType) THEN
  2208. result := SyntaxTree.NewRealValue(expression.position,int);
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2211. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.SetType) THEN
  2214. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2217. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2218. result.SetType(type);
  2219. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2220. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2221. Error(position, "huge integer value incompatible to enumeration");
  2222. END;
  2223. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.PortType) THEN
  2226. result := ConvertValue(position, expression, system.integerType);
  2227. ELSE
  2228. Error(position, "integer value cannot be converted");
  2229. result := SyntaxTree.invalidExpression;
  2230. IF VerboseErrorMessage THEN
  2231. Printout.Info("expression",expression);
  2232. Printout.Info("type",type);
  2233. END;
  2234. END;
  2235. ELSIF IsRealValue(expression,real) THEN
  2236. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2237. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2238. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2239. result.SetType(type);
  2240. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2241. int := ENTIERH(real);
  2242. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.FloatType) THEN
  2245. result := SyntaxTree.NewRealValue(position,real);
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2248. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2249. result.SetType(type);
  2250. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2251. ELSIF (type IS SyntaxTree.PortType) THEN
  2252. result := ConvertValue(position, expression, system.integerType);
  2253. ELSE
  2254. Error(position, "real value cannot be converted");
  2255. result := SyntaxTree.invalidExpression;
  2256. END
  2257. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2258. IF (type IS SyntaxTree.ComplexType) THEN
  2259. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2260. result.SetType(type);
  2261. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2262. ELSE
  2263. Error(position, "complex value cannot be converted");
  2264. result := SyntaxTree.invalidExpression;
  2265. END
  2266. ELSIF IsSetValue(expression,set) THEN
  2267. IF (type IS SyntaxTree.IntegerType) THEN
  2268. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2271. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2272. result.SetType(type);
  2273. ELSIF (type IS SyntaxTree.PortType) THEN
  2274. result := ConvertValue(position, expression, system.integerType);
  2275. ELSE
  2276. Error(position, "set value cannot be converted");
  2277. result := SyntaxTree.invalidExpression;
  2278. END;
  2279. ELSIF IsStringValue(expression,string) THEN
  2280. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2281. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2282. result.SetType(type);
  2283. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2284. ELSE
  2285. Error(position, "string value cannot be converted");
  2286. result := SyntaxTree.invalidExpression;
  2287. END;
  2288. ELSIF IsCharacterValue(expression,char) THEN
  2289. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2290. string[0] := char; string[1] := 0X;
  2291. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2292. result := SyntaxTree.NewStringValue(expression.position,string);
  2293. result.SetType(type);
  2294. ELSIF (type IS SyntaxTree.ByteType) THEN
  2295. (* do not simply set the new type as this could invalidate types of constants *)
  2296. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2297. result.SetType(type)
  2298. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2299. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2300. result.SetType(type);
  2301. ELSIF (type IS SyntaxTree.SetType) THEN
  2302. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2303. result.SetType(type);
  2304. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2305. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2306. result.SetType(type);
  2307. ELSIF (type IS SyntaxTree.PortType) THEN
  2308. result := ConvertValue(position, expression, system.integerType);
  2309. ELSE
  2310. Error(position, "character value cannot be converted");
  2311. result := SyntaxTree.invalidExpression;
  2312. END;
  2313. ELSIF expression IS SyntaxTree.NilValue THEN
  2314. IF type IS SyntaxTree.AddressType THEN
  2315. result := SyntaxTree.NewIntegerValue(position,0);
  2316. result.SetType(type);
  2317. ELSE
  2318. result := expression;
  2319. END;
  2320. (* nothing to be done *)
  2321. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2322. result := MathArrayConversion(position, expression,type);
  2323. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2324. int := expression(SyntaxTree.EnumerationValue).value;
  2325. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2326. int := Global.ConvertSigned(int,system.SizeOf(type));
  2327. result := SyntaxTree.NewIntegerValue(position,int);
  2328. result.SetType(type);
  2329. ELSE
  2330. result := expression;
  2331. END;
  2332. (* nothing to be done *)
  2333. ELSE
  2334. Error(position, "expression cannot be converted");
  2335. IF VerboseErrorMessage THEN
  2336. Printout.Info("expression",expression);
  2337. Printout.Info("type",type);
  2338. END;
  2339. result := SyntaxTree.invalidExpression;
  2340. END;
  2341. RETURN result
  2342. END ConvertValue;
  2343. (**
  2344. return a conversion of an expression to a given type
  2345. - if expression is already of same type then return expression
  2346. - if incompatible conversion then report error and return invalidExpression
  2347. **)
  2348. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2349. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2350. BEGIN
  2351. type := type.resolved;
  2352. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2353. result := expression;
  2354. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2355. ELSIF expression = NIL THEN (* NIL expression *)
  2356. ELSIF expression.type = NIL THEN
  2357. Error(position, "expression of type NIL cannot be converted");
  2358. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2359. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2360. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2361. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2362. ELSIF expression.resolved # NIL THEN (* value *)
  2363. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2364. IF value IS SyntaxTree.Value THEN
  2365. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2366. result.SetResolved(value(SyntaxTree.Value));
  2367. result.SetType(value.type);
  2368. ELSE
  2369. result := value
  2370. END;
  2371. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2372. expressionList := SyntaxTree.NewExpressionList();
  2373. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2374. typeDeclaration.SetDeclaredType(type);
  2375. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2376. typeSymbol.SetType(typeDeclaration.type);
  2377. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2378. expressionList.AddExpression(expression);
  2379. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2380. result.SetType(type);
  2381. ELSIF IsArrayStructuredObjectType(type) THEN
  2382. (* no type can be converted to an array-structured object type *)
  2383. HALT(100)
  2384. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2385. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2386. result := MathArrayConversion(position, expression,type);
  2387. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2388. expression := ConvertToMathArray(expression);
  2389. type := MathArrayStructureOfType(type);
  2390. result := MathArrayConversion(position, expression, type)
  2391. ELSE
  2392. Error(expression.position,"cannot convert non array type to array type")
  2393. END;
  2394. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2395. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2396. OR ~(type IS SyntaxTree.ArrayType) THEN
  2397. Error(expression.position,"cannot convert array type to non-array type")
  2398. END;
  2399. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2400. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2401. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2402. (*skip, no conversion*)
  2403. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2404. (* skip, no conversion *)
  2405. ELSE
  2406. ASSERT(~(type IS SyntaxTree.RangeType));
  2407. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2408. ASSERT(type # NIL);
  2409. END;
  2410. RETURN result
  2411. END NewConversion;
  2412. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2413. BEGIN
  2414. IF CompatibleTo(system,expression.type, type) THEN
  2415. RETURN NewConversion(position, expression, type, NIL);
  2416. ELSE
  2417. Error(expression.position, "incompatible expression");
  2418. RETURN SyntaxTree.invalidExpression
  2419. END;
  2420. END CompatibleConversion;
  2421. (**
  2422. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2423. **)
  2424. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2425. VAR leftType,rightType: SyntaxTree.Type;
  2426. BEGIN
  2427. IF left.type = NIL THEN Error(left.position,"no type")
  2428. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2429. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2430. (* error already handled *)
  2431. ELSE
  2432. leftType := left.type.resolved; rightType := right.type.resolved;
  2433. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2434. right := NewConversion(right.position, right, leftType, NIL);
  2435. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2436. left := NewConversion(left.position,left,rightType,NIL);
  2437. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2438. right := NewConversion(right.position, right, leftType, NIL);
  2439. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2440. left := NewConversion(left.position,left,rightType,NIL);
  2441. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2442. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2443. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2444. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2445. ELSIF
  2446. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2447. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2448. (* must be the case LONGREAL / COMPLEX ) *)
  2449. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2450. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2451. ELSE
  2452. Error(left.position,"incompatible operands");
  2453. END;
  2454. END;
  2455. END ConvertOperands;
  2456. (** find and return best operator matching to parameter list (nil, if none)
  2457. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2458. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2459. **)
  2460. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2461. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2462. identifier: SyntaxTree.Identifier;
  2463. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2464. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2465. BEGIN
  2466. operator := scope.firstOperator;
  2467. WHILE(operator # NIL) DO
  2468. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2469. procedureType := operator.type(SyntaxTree.ProcedureType);
  2470. distance := Distance(system, procedureType,actualParameters);
  2471. IF (distance < Infinity) THEN
  2472. IF returnType # NIL THEN
  2473. IF procedureType.returnType = NIL THEN
  2474. distance := Infinity
  2475. ELSE
  2476. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2477. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2478. END;
  2479. END;
  2480. END;
  2481. (*
  2482. IF distance < Infinity THEN
  2483. TRACE(distance, operator);
  2484. Printout.Info("potential operator",operator);
  2485. ELSE
  2486. Printout.Info("operator not possible",operator);
  2487. END;
  2488. *)
  2489. IF distance < bestDistance THEN
  2490. bestDistance := distance;
  2491. bestOperator := operator;
  2492. END;
  2493. END;
  2494. operator := operator.nextOperator;
  2495. END;
  2496. (*
  2497. Printout.Info("taken operator",bestOperator);
  2498. *)
  2499. END FindInScope;
  2500. BEGIN
  2501. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2502. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2503. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2504. import := currentScope.ownerModule.moduleScope.firstImport;
  2505. WHILE (bestDistance > 0) & (import # NIL) DO
  2506. IF import.module # NIL THEN
  2507. identifier := Global.GetIdentifier(operator,import.module.case);
  2508. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2509. END;
  2510. import := import.nextImport;
  2511. END;
  2512. RETURN bestOperator
  2513. END FindOperator;
  2514. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2515. BEGIN
  2516. currentScope := scope;
  2517. END SetCurrentScope;
  2518. (**
  2519. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2520. - handle LEN and DIM operator for array-structured object types
  2521. - find operator, if found then
  2522. - if in other module then add import designator
  2523. - create symbol designator for operator
  2524. - if error then return invalidExpression, if no operator then return NIL
  2525. **)
  2526. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2527. VAR
  2528. operator: SyntaxTree.Operator;
  2529. import: SyntaxTree.Import;
  2530. expression, result: SyntaxTree.Expression;
  2531. designator: SyntaxTree.Designator;
  2532. actualParameters, tempList: SyntaxTree.ExpressionList;
  2533. recordType: SyntaxTree.RecordType;
  2534. castReturnType : SyntaxTree.MathArrayType;
  2535. BEGIN
  2536. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2537. result := SyntaxTree.invalidExpression
  2538. ELSIF leftExpression = NIL THEN
  2539. result := NIL
  2540. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2541. (* LEN or DIM operator on array-structured object type *)
  2542. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2543. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2544. IF recordType.arrayAccessOperators.len = NIL THEN
  2545. Error(position, "call of undeclared LEN operator");
  2546. result := SyntaxTree.invalidExpression
  2547. ELSE
  2548. ASSERT(leftExpression IS SyntaxTree.Designator);
  2549. designator := leftExpression(SyntaxTree.Designator);
  2550. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2551. ASSERT(expression IS SyntaxTree.Designator);
  2552. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2553. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2554. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2555. result := designator
  2556. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2557. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2558. tempList := SyntaxTree.NewExpressionList();
  2559. tempList.AddExpression(rightExpression);
  2560. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2561. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2562. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2563. tempList := SyntaxTree.NewExpressionList();
  2564. tempList.AddExpression(designator);
  2565. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2566. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2567. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2568. END
  2569. END;
  2570. ELSE
  2571. (* import OCArrayBase if needed *)
  2572. IF ~arrayBaseImported THEN
  2573. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2574. (* operators on complex numbers *)
  2575. ImportModule(Global.ArrayBaseName,position);
  2576. arrayBaseImported := TRUE;
  2577. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2578. IF op = Global.Dim THEN
  2579. (* not existing in OCArrayBase *)
  2580. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2581. (* not existing in OCArrayBase *)
  2582. ELSE
  2583. ImportModule(Global.ArrayBaseName,position);
  2584. arrayBaseImported := TRUE;
  2585. END
  2586. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2587. ImportModule(Global.ArrayBaseName,position);
  2588. arrayBaseImported := TRUE
  2589. END;
  2590. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2591. (* LEN(RANGE) *)
  2592. ImportModule(Global.ArrayBaseName,position);
  2593. arrayBaseImported := TRUE;
  2594. END;
  2595. END;
  2596. actualParameters := SyntaxTree.NewExpressionList();
  2597. actualParameters.AddExpression(leftExpression);
  2598. IF rightExpression # NIL THEN
  2599. actualParameters.AddExpression(rightExpression)
  2600. END;
  2601. operator := FindOperator(system,op,actualParameters,resultType);
  2602. IF operator # NIL THEN
  2603. designator := NIL;
  2604. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2605. import := currentScope.ownerModule.moduleScope.firstImport;
  2606. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2607. import := import.nextImport;
  2608. END;
  2609. expression := NewSymbolDesignator(position,NIL,import);
  2610. designator := expression(SyntaxTree.Designator);
  2611. END;
  2612. expression := NewSymbolDesignator(position,designator,operator);
  2613. designator := expression(SyntaxTree.Designator);
  2614. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2615. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2616. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2617. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2618. result.SetType(castReturnType);
  2619. END;
  2620. ELSE
  2621. result := NIL;
  2622. END;
  2623. END;
  2624. RETURN result
  2625. END NewOperatorCall;
  2626. (** check and resolve binary expression **)
  2627. (*! clean up *)
  2628. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2629. VAR left,right,result: SyntaxTree.Expression;
  2630. leftType, rightType: SyntaxTree.Type;
  2631. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2632. cl,cr: CHAR;
  2633. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2634. type: SyntaxTree.Type;
  2635. value: SyntaxTree.Value;
  2636. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2637. integerConstantFolding: BOOLEAN;
  2638. list: SyntaxTree.ExpressionList;
  2639. PROCEDURE NewBool(v: BOOLEAN);
  2640. BEGIN
  2641. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2642. value.SetType(system.booleanType);
  2643. result.SetResolved(value);
  2644. type := system.booleanType
  2645. END NewBool;
  2646. PROCEDURE NewSet(v: SET);
  2647. BEGIN
  2648. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2649. value.SetType(system.setType);
  2650. result.SetResolved(value);
  2651. type := system.setType;
  2652. END NewSet;
  2653. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2654. BEGIN
  2655. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2656. (* type cast to "larger" type only if the value is still in the range *)
  2657. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2658. value.SetType(t);
  2659. END;
  2660. result.SetResolved(value);
  2661. type := value.type;
  2662. END NewInteger;
  2663. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2664. BEGIN
  2665. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2666. value.SetType(t);
  2667. result.SetResolved(value);
  2668. type := t;
  2669. END NewReal;
  2670. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2671. BEGIN
  2672. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2673. value.SetType(t);
  2674. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2675. result.SetResolved(value);
  2676. type := t;
  2677. END NewComplex;
  2678. BEGIN
  2679. type := SyntaxTree.invalidType;
  2680. left := ResolveExpression(binaryExpression.left);
  2681. right := ResolveExpression(binaryExpression.right);
  2682. binaryExpression.SetLeft(left);
  2683. binaryExpression.SetRight(right);
  2684. result := binaryExpression;
  2685. operator := binaryExpression.operator;
  2686. IF ~system.operatorDefined[operator] THEN
  2687. Error(left.position,"Operator Not Defined");
  2688. result := SyntaxTree.invalidExpression;
  2689. RETURN
  2690. END;
  2691. IF left.type = NIL THEN
  2692. Error(left.position,"Expression has no result type");
  2693. result := SyntaxTree.invalidExpression;
  2694. RETURN;
  2695. END;
  2696. IF right.type = NIL THEN
  2697. Error(right.position,"Expression has no result type");
  2698. result := SyntaxTree.invalidExpression;
  2699. RETURN;
  2700. END;
  2701. leftType := left.type.resolved; rightType := right.type.resolved;
  2702. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2703. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2704. END;
  2705. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2706. list := SyntaxTree.NewExpressionList();
  2707. list.AddExpression(right);
  2708. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2709. END;
  2710. IF operatorCall # NIL THEN
  2711. result := operatorCall;
  2712. type := operatorCall.type;
  2713. (* admissible operators:
  2714. Times, Plus, Minus numeric numeric numeric
  2715. set set set
  2716. Slash numeric numeric real /complex
  2717. set set set
  2718. Div , Mod integer integer integer
  2719. And, Or bool bool bool
  2720. Equal, Unequal basic basic bool
  2721. pointer pointer bool
  2722. object object bool
  2723. record record bool
  2724. string string bool
  2725. enumerator enumerator bool
  2726. Less, LessEqual,
  2727. Greater, GreaterEqual integer/real integer/real bool
  2728. enumerator enumerator bool
  2729. In integer set bool
  2730. Is pointer type bool
  2731. object type bool
  2732. record type bool
  2733. Upto: special abbreviation for a..b
  2734. *)
  2735. ELSIF (left.type = NIL) THEN
  2736. Error(left.position,"type (left operand) = NIL in binary expression");
  2737. D.Str("nil type in "); D.Type(left); D.Ln;
  2738. result := SyntaxTree.invalidExpression;
  2739. ELSIF (right.type = NIL) THEN
  2740. Error(right.position,"type (right operand) = NIL in binary expression");
  2741. result := SyntaxTree.invalidExpression;
  2742. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2743. result := SyntaxTree.invalidExpression;
  2744. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2745. HALT(100);
  2746. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2747. type := system.booleanType;
  2748. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2749. Error(right.position,"is not a type ");
  2750. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2751. Error(binaryExpression.position,"is not a type extension of ");
  2752. IF VerboseErrorMessage THEN
  2753. Printout.Info("left",left);
  2754. Printout.Info("right",right);
  2755. END;
  2756. ELSIF IsUnsafePointer(left.type) THEN
  2757. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2758. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2759. NewBool(TRUE)
  2760. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2761. NewBool(TRUE);
  2762. ELSIF IsUnextensibleRecord(left) THEN
  2763. NewBool(FALSE)
  2764. END
  2765. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2766. Error(right.position,"must not be a type");
  2767. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2768. Error(left.position,"must not be a type");
  2769. ELSIF operator = Scanner.In THEN (* left IN right *)
  2770. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2771. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2772. NewBool(il IN sr);
  2773. ELSE
  2774. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2775. left := NewConversion(left.position, left, system.longintType,NIL);
  2776. binaryExpression.SetLeft(left)
  2777. END;
  2778. type := system.booleanType;
  2779. END
  2780. ELSE
  2781. Error(binaryExpression.position, "incompatible operands");
  2782. END
  2783. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2784. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2785. Error(binaryExpression.position,"incompatible operands");
  2786. END;
  2787. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2788. ELSE Error(binaryExpression.position,"operator not defined 1")
  2789. END
  2790. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2791. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2792. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2793. Error(binaryExpression.position,"incompatible operands");
  2794. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2795. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2796. ConvertOperands(left, right);
  2797. binaryExpression.SetLeft(left);
  2798. binaryExpression.SetRight(right);
  2799. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2800. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2801. END;
  2802. type := system.booleanType;
  2803. ELSE
  2804. Error(binaryExpression.position,"operator not defined 3");
  2805. END
  2806. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2807. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2808. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2809. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2810. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2811. CASE operator OF
  2812. |Scanner.Equal: NewBool(strl^=strr^);
  2813. |Scanner.Unequal:NewBool(strl^#strr^);
  2814. |Scanner.Less: NewBool(strl^<strr^);
  2815. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2816. |Scanner.Greater: NewBool(strl^>strr^);
  2817. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2818. ELSE
  2819. Error(binaryExpression.position,"operator not defined 4");
  2820. END;
  2821. END;
  2822. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2823. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2824. type := system.booleanType
  2825. ELSE
  2826. Error(binaryExpression.position,"operator not defined 5");
  2827. END;
  2828. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2829. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2830. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2831. type := system.booleanType;
  2832. ELSE
  2833. Error(binaryExpression.position,"operator not defined 6");
  2834. END
  2835. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2836. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2837. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2838. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2839. type := system.booleanType
  2840. ELSE
  2841. Error(binaryExpression.position,"operator not defined for enumerators");
  2842. END;
  2843. ELSE
  2844. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2845. END;
  2846. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2847. type := system.booleanType;
  2848. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2849. type := system.booleanType;
  2850. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2851. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2852. THEN
  2853. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2854. IF (leftType # rightType) THEN
  2855. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2856. ConvertOperands(left,right); (* operands must be of the same type here *)
  2857. END;
  2858. binaryExpression.SetLeft(left);
  2859. binaryExpression.SetRight(right);
  2860. leftType := left.type.resolved;
  2861. rightType := right.type.resolved;
  2862. END;
  2863. type := leftType;
  2864. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2865. Error(binaryExpression.position,"conversion failed ?");
  2866. IF VerboseErrorMessage THEN
  2867. Printout.Info("left",left);
  2868. Printout.Info("right",right);
  2869. END;
  2870. ELSIF IsIntegerType(leftType) THEN
  2871. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2872. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2873. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2874. Error(binaryExpression.position,"division by zero");
  2875. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2876. Error(binaryExpression.position,"integer division by negative number");
  2877. END;
  2878. END;
  2879. (* constant folding *)
  2880. (* bootstrap64
  2881. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2882. CASE operator OF
  2883. |Scanner.Plus: NewInteger(il+ir,left.type);
  2884. |Scanner.Minus: NewInteger(il-ir,left.type);
  2885. |Scanner.Times: NewInteger(il*ir,left.type);
  2886. |Scanner.Slash:
  2887. IF ir # 0 THEN
  2888. NewReal(il/ir, system.realType);
  2889. END;
  2890. |Scanner.Mod:
  2891. IF ir > 0 THEN
  2892. NewInteger(il MOD ir,left.type);
  2893. END;
  2894. |Scanner.Div:
  2895. IF ir > 0 THEN
  2896. NewInteger(il DIV ir,left.type);
  2897. END;
  2898. |Scanner.Equal: NewBool(il=ir);
  2899. |Scanner.Unequal:NewBool(il#ir);
  2900. |Scanner.Less: NewBool(il<ir);
  2901. |Scanner.LessEqual: NewBool(il<=ir);
  2902. |Scanner.Greater: NewBool(il>ir);
  2903. |Scanner.GreaterEqual: NewBool(il>=ir);
  2904. ELSE Error(binaryExpression.position,"operator not defined 7");
  2905. END;
  2906. ELS*)
  2907. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2908. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2909. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2910. CASE operator OF
  2911. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2912. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2913. |Scanner.Times: NewInteger(hl*hr,left.type);
  2914. |Scanner.Slash:
  2915. IF hr = 0 THEN
  2916. Error(binaryExpression.position,"division by zero");
  2917. ELSE
  2918. IF type.sizeInBits = 64 THEN
  2919. NewReal(hl/hr,system.longrealType);
  2920. ELSE
  2921. NewReal(hl/hr,system.realType)
  2922. END
  2923. END;
  2924. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2925. |Scanner.Mod:
  2926. IF hr = 0 THEN
  2927. Error(binaryExpression.position,"division by zero");
  2928. ELSE
  2929. NewInteger(hl MOD hr, left.type);
  2930. (* bootstrap64
  2931. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2932. *)
  2933. END;
  2934. |Scanner.Div:
  2935. IF hr = 0 THEN
  2936. Error(binaryExpression.position,"division by zero");
  2937. ELSE
  2938. NewInteger(hl DIV hr, left.type);
  2939. (* bootstrap64
  2940. NewInteger(Machine.DivH(hl,hr),left.type);
  2941. *)
  2942. END;
  2943. (* *)
  2944. |Scanner.Equal: NewBool(hl=hr);
  2945. |Scanner.Unequal: NewBool(hl#hr);
  2946. |Scanner.Less: NewBool(hl<hr);
  2947. |Scanner.LessEqual: NewBool(hl<=hr);
  2948. |Scanner.Greater: NewBool(hl>hr);
  2949. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2950. ELSE Error(binaryExpression.position,"operator not defined 8");
  2951. END;
  2952. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2953. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2954. type := left.type
  2955. ELSIF (operator = Scanner.Slash) THEN
  2956. left := NewConversion(left.position,left,system.realType,NIL);
  2957. right := NewConversion(right.position,right,system.realType,NIL);
  2958. binaryExpression.SetLeft(left);
  2959. binaryExpression.SetRight(right);
  2960. type := system.realType
  2961. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2962. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2963. type := system.booleanType
  2964. ELSE
  2965. Error(binaryExpression.position,"operator not defined 9");
  2966. END;
  2967. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2968. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2969. CASE operator OF
  2970. |Scanner.Plus: NewReal(rl+rr,leftType);
  2971. |Scanner.Minus: NewReal(rl-rr,leftType);
  2972. |Scanner.Times:NewReal(rl*rr,leftType);
  2973. |Scanner.Slash:
  2974. IF rr = 0 THEN
  2975. Error(binaryExpression.position,"division by zero");
  2976. ELSE
  2977. NewReal(rl/rr,leftType);
  2978. END
  2979. |Scanner.Equal: NewBool(rl=rr);
  2980. |Scanner.Unequal: NewBool(rl#rr);
  2981. |Scanner.Less: NewBool(rl<rr);
  2982. |Scanner.LessEqual: NewBool(rl<=rr);
  2983. |Scanner.Greater: NewBool(rl>rr);
  2984. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2985. ELSE Error(binaryExpression.position,"operator not defined 10");
  2986. END;
  2987. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2988. type := left.type
  2989. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2990. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2991. type := system.booleanType
  2992. ELSE
  2993. Error(binaryExpression.position,"operator not defined 11");
  2994. IF VerboseErrorMessage THEN
  2995. Printout.Info("left",left);
  2996. Printout.Info("right",right);
  2997. END;
  2998. END;
  2999. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3000. CASE operator OF
  3001. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3002. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3003. ELSE
  3004. Error(binaryExpression.position,"operator not defined");
  3005. IF VerboseErrorMessage THEN
  3006. Printout.Info("left", left);
  3007. Printout.Info("right", right)
  3008. END;
  3009. END;
  3010. IF ~error THEN
  3011. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3012. Error(binaryExpression.position,"division by zero")
  3013. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3014. (* do constant folding *)
  3015. CASE operator OF
  3016. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3017. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3018. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3019. |Scanner.Slash:
  3020. divisor := c * c + d * d;
  3021. ASSERT(divisor # 0);
  3022. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3023. |Scanner.Equal: NewBool((a = c) & (b = d))
  3024. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3025. END
  3026. END
  3027. END
  3028. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3029. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3030. CASE operator OF
  3031. |Scanner.And: NewBool(bl & br);
  3032. |Scanner.Or: NewBool(bl OR br);
  3033. |Scanner.Equal: NewBool(bl = br);
  3034. |Scanner.Unequal: NewBool(bl # br);
  3035. ELSE Error(binaryExpression.position,"operator not defined 12");
  3036. END;
  3037. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3038. type := system.booleanType
  3039. ELSE
  3040. Error(binaryExpression.position,"operator not defined 13");
  3041. END;
  3042. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3043. (* constant folding *)
  3044. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3045. IF operator = Scanner.Equal THEN
  3046. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3047. ELSIF operator = Scanner.Unequal THEN
  3048. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3049. END;
  3050. END;
  3051. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3052. type := system.booleanType;
  3053. ELSE
  3054. Error(binaryExpression.position, "operator not defined");
  3055. END;
  3056. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3057. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3058. CASE operator OF
  3059. |Scanner.Plus: NewSet(sl + sr);
  3060. |Scanner.Minus: NewSet(sl - sr);
  3061. |Scanner.Times: NewSet(sl * sr);
  3062. |Scanner.Slash: NewSet(sl / sr);
  3063. |Scanner.Equal: NewBool(sl=sr);
  3064. |Scanner.Unequal: NewBool(sl#sr);
  3065. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3066. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3067. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3068. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3069. ELSE Error(binaryExpression.position,"operator not defined 14");
  3070. END;
  3071. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3072. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3073. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3074. THEN
  3075. type := system.booleanType
  3076. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3077. type := left.type
  3078. ELSE
  3079. Error(binaryExpression.position,"operator not defined 15");
  3080. END;
  3081. ELSIF IsCharacterType(left.type) THEN
  3082. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3083. CASE operator OF
  3084. |Scanner.Equal: NewBool(cl=cr);
  3085. |Scanner.Unequal: NewBool(cl#cr);
  3086. |Scanner.Less: NewBool(cl<cr);
  3087. |Scanner.LessEqual: NewBool(cl<=cr);
  3088. |Scanner.Greater: NewBool(cl>cr);
  3089. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3090. ELSE Error(binaryExpression.position,"operator not defined 16");
  3091. END;
  3092. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3093. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3094. type := system.booleanType
  3095. ELSE
  3096. Error(binaryExpression.position,"operator not defined 17");
  3097. END;
  3098. ELSE
  3099. Error(binaryExpression.position,"operator not defined 18");
  3100. END;
  3101. ELSE
  3102. Error(binaryExpression.position,"operator not defined 19");
  3103. END;
  3104. IF type = SyntaxTree.invalidType THEN
  3105. result := SyntaxTree.invalidExpression
  3106. ELSE
  3107. result.SetType(type)
  3108. END;
  3109. resolvedExpression := result
  3110. END VisitBinaryExpression;
  3111. (** resolve a range expression of the from <<first .. last BY step>>
  3112. - depending on the context different things are checked:
  3113. ArrayIndex:
  3114. - components must be integers
  3115. - replace missing lower bound with 0
  3116. - replace missing upper bound with MAX(LONGINT)
  3117. - replace missing step size with 1
  3118. SetElement:
  3119. - components must be integers
  3120. - replace missing lower bound with 0
  3121. - replace missing upper bound with MAX(SET)
  3122. - must not have step size
  3123. CaseGuard:
  3124. - components must be constant
  3125. - components must be integers or characters
  3126. - must have lower and upper bound present
  3127. - components are made compatible
  3128. - must not have step size
  3129. - if error: return invalidExpression
  3130. **)
  3131. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3132. VAR
  3133. hasError: BOOLEAN;
  3134. first, last, step: SyntaxTree.Expression;
  3135. BEGIN
  3136. hasError := FALSE;
  3137. first := x.first;
  3138. last := x.last;
  3139. step := x.step;
  3140. (* check lower bound *)
  3141. IF x.context = SyntaxTree.CaseGuard THEN
  3142. IF first = NIL THEN
  3143. Error(x.position, "missing lower bound");
  3144. hasError := TRUE
  3145. ELSE
  3146. first := ResolveExpression(first);
  3147. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3148. Error(first.position, "lower bound not integer or character");
  3149. hasError := TRUE
  3150. ELSE
  3151. IF first IS SyntaxTree.StringValue THEN
  3152. (* add conversion from string to character *)
  3153. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3154. END
  3155. END;
  3156. (* check if expression is constant *)
  3157. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3158. (* error already reported *)
  3159. hasError := TRUE
  3160. END
  3161. END
  3162. ELSE (* ArrayIndex, SetElement *)
  3163. IF first = NIL THEN
  3164. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3165. END;
  3166. first := ResolveExpression(first);
  3167. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3168. first := NewConversion(first.position, first, system.longintType, NIL)
  3169. ELSE
  3170. Error(first.position, "lower bound not integer");
  3171. hasError := TRUE
  3172. END
  3173. END;
  3174. (* check upper bound *)
  3175. IF x.context = SyntaxTree.CaseGuard THEN
  3176. IF last = NIL THEN
  3177. Error(x.position, "missing upper bound");
  3178. hasError := TRUE
  3179. ELSE
  3180. last := ResolveExpression(last);
  3181. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3182. Error(last.position, "lower bound not integer or character");
  3183. hasError := TRUE
  3184. ELSE
  3185. IF last IS SyntaxTree.StringValue THEN
  3186. (* add conversion from string to character *)
  3187. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3188. END
  3189. END;
  3190. (* check if expression is constant *)
  3191. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3192. (* error already reported *)
  3193. hasError := TRUE
  3194. ELSE
  3195. (* try to make lower and upper bound compatible *)
  3196. ConvertOperands(first, last);
  3197. IF first.type.resolved # last.type.resolved THEN
  3198. Error(x.position, "lower and upper bounds incompatible");
  3199. hasError := TRUE
  3200. END
  3201. END
  3202. END
  3203. ELSE (* ArrayIndex, SetElement *)
  3204. IF last = NIL THEN
  3205. IF x.context = SyntaxTree.ArrayIndex THEN
  3206. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3207. ELSE
  3208. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3209. END
  3210. END;
  3211. last := ResolveExpression(last);
  3212. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3213. last := NewConversion(last.position, last, system.longintType, NIL)
  3214. ELSE
  3215. Error(last.position, "upper bound not integer");
  3216. hasError := TRUE
  3217. END
  3218. END;
  3219. (* check step size *)
  3220. IF x.context = SyntaxTree.ArrayIndex THEN
  3221. IF step = NIL THEN
  3222. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3223. END;
  3224. step := ResolveExpression(step);
  3225. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3226. step := NewConversion(step.position, step, system.longintType, NIL)
  3227. ELSE
  3228. Error(step.position, "step size not integer");
  3229. hasError := TRUE
  3230. END
  3231. ELSE (* SetElement, CaseGuard *)
  3232. IF step # NIL THEN
  3233. Error(last.position, "step size not allowed in this context");
  3234. hasError := TRUE
  3235. END
  3236. END;
  3237. IF hasError THEN
  3238. resolvedExpression := SyntaxTree.invalidExpression
  3239. ELSE
  3240. x.SetFirst(first);
  3241. x.SetLast(last);
  3242. x.SetStep(step);
  3243. x.SetType(system.rangeType);
  3244. resolvedExpression := x;
  3245. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3246. END
  3247. END VisitRangeExpression;
  3248. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3249. BEGIN
  3250. x.SetType(NIL);
  3251. resolvedExpression := x;
  3252. END VisitTensorRangeExpression;
  3253. (** resolve the expression d and return result as designator
  3254. - resolve expression
  3255. - if expression is a designator then return designator else error message and return invalidDesignator
  3256. **)
  3257. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3258. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3259. BEGIN
  3260. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3261. resolved := ResolveExpression(d);
  3262. IF resolved = SyntaxTree.invalidExpression THEN
  3263. (* error should already have been reported *)
  3264. result := SyntaxTree.invalidDesignator;
  3265. ELSIF resolved IS SyntaxTree.Designator THEN
  3266. result := resolved(SyntaxTree.Designator);
  3267. ELSE
  3268. Error(d.position,"is no designator ! ");
  3269. result := SyntaxTree.invalidDesignator;
  3270. END;
  3271. (* result.type might be nil. *)
  3272. RETURN result
  3273. END ResolveDesignator;
  3274. (**
  3275. symbol designator generated in this module
  3276. nothing to be resolved
  3277. **)
  3278. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3279. BEGIN
  3280. resolvedExpression := x;
  3281. END VisitSymbolDesignator;
  3282. (**
  3283. self designator generated in this module
  3284. nothing to be resolved
  3285. **)
  3286. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3287. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3288. BEGIN
  3289. (* check if in record scope *)
  3290. scope := currentScope;
  3291. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3292. scope := scope.outerScope;
  3293. END;
  3294. IF scope = NIL THEN (* in module scope *)
  3295. x.SetType(system.anyType);
  3296. ELSIF scope IS SyntaxTree.CellScope THEN
  3297. cell := scope(SyntaxTree.CellScope).ownerCell;
  3298. x.SetType(cell);
  3299. ELSE (* in record scope *)
  3300. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3301. IF (record # NIL) & (record.pointerType # NIL) THEN
  3302. type := ResolveType(record.pointerType);
  3303. x.SetType(type);
  3304. ELSE
  3305. x.SetType(record);
  3306. END;
  3307. END;
  3308. resolvedExpression := x;
  3309. END VisitSelfDesignator;
  3310. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3311. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3312. BEGIN
  3313. scope := currentScope;
  3314. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3315. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3316. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3317. returnType := procedureType.returnType;
  3318. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3319. THEN
  3320. x.SetType(returnType);
  3321. ELSE
  3322. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3323. x.SetType(SyntaxTree.invalidType);
  3324. END;
  3325. ELSE
  3326. Error(x.position,"forbidden access to result designator");
  3327. x.SetType(SyntaxTree.invalidType);
  3328. END;
  3329. x.SetAssignable(TRUE);
  3330. resolvedExpression := x;
  3331. END VisitResultDesignator;
  3332. (**
  3333. return symbol designator as an expression
  3334. - if symbol is a constant then return the constant value expression
  3335. - else
  3336. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3337. - if symbol is a guarded variable then return a TypeGuardDesignator
  3338. - else return a symbol designator
  3339. **)
  3340. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3341. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3342. guardType: SyntaxTree.Type;
  3343. BEGIN
  3344. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3345. result := SyntaxTree.invalidExpression;
  3346. ASSERT(symbol # NIL);
  3347. (*
  3348. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3349. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3350. Error(position, "type not allowed here");
  3351. ELS *)
  3352. (* not needed any more as values are stored in the expression
  3353. IF symbol IS SyntaxTree.Constant THEN
  3354. result := symbol(SyntaxTree.Constant).value
  3355. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3356. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3357. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3358. ELSE
  3359. result := symbol(SyntaxTree.Constant).value
  3360. END;
  3361. ELSE
  3362. *)
  3363. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3364. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3365. THEN
  3366. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3367. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3368. left := NewDereferenceDesignator(position,left);
  3369. left.SetHidden(TRUE);
  3370. END;
  3371. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3372. scope := currentScope;
  3373. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3374. scope := scope.outerScope;
  3375. END;
  3376. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3377. Error(position, "forbidden access to symbol in parent procedure scope");
  3378. END;
  3379. END;
  3380. 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);
  3381. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3382. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3383. ELSE
  3384. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3385. END;
  3386. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3387. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3388. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3389. result.SetType(symbol.type);
  3390. result.SetAssignable(assignable);
  3391. symbol.MarkUsed;
  3392. IF symbol IS SyntaxTree.Constant THEN
  3393. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3394. END;
  3395. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3396. variableAccessed := TRUE
  3397. END;
  3398. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3399. IF GetGuard(symbol,guardType) THEN
  3400. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3401. END;
  3402. END;
  3403. ASSERT(result.type # NIL);
  3404. RETURN result
  3405. END NewSymbolDesignator;
  3406. (** check and resolve an identifier designator "identifier"
  3407. - if identifier = self then return SelfDesignator
  3408. - else find symbol in current scope
  3409. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3410. **)
  3411. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3412. VAR symbol: SyntaxTree.Symbol;
  3413. BEGIN
  3414. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3415. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3416. IF symbol # NIL THEN
  3417. ResolveSymbol(symbol);
  3418. ASSERT(symbol.type # NIL);
  3419. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3420. ELSE
  3421. Error(identifierDesignator.position,"Undeclared Identifier");
  3422. IF VerboseErrorMessage THEN
  3423. Printout.Info("undeclared identifier designator",identifierDesignator);
  3424. END;
  3425. resolvedExpression := SyntaxTree.invalidDesignator;
  3426. END;
  3427. END VisitIdentifierDesignator;
  3428. (** check and resolve a selector designator of the form left.designator
  3429. - if left is a pointer type then do auto dereferenciation
  3430. - left denotes a search scope:
  3431. - if left type is import type then set search scope to respective module
  3432. - if left type is enumeration type then set search scope to respective enumeration scope
  3433. - elsif left type is record type then set search scope to record scope
  3434. - search symbol in computed scope
  3435. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3436. **)
  3437. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3438. VAR
  3439. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3440. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3441. BEGIN
  3442. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3443. left := ResolveDesignator(selectorDesignator.left);
  3444. result := SyntaxTree.invalidDesignator;
  3445. IF left # NIL THEN
  3446. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3447. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3448. END;
  3449. scope := NIL;
  3450. IF left.type = NIL THEN
  3451. Error(selectorDesignator.position,"field on nil typed designator");
  3452. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3453. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3454. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3455. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3456. module := symbol(SyntaxTree.Import).module;
  3457. IF module # NIL THEN
  3458. scope := module.moduleScope
  3459. ELSE
  3460. Error(left.position,"module not loaded");
  3461. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3462. END;
  3463. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3464. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3465. ASSERT(scope # NIL)
  3466. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3467. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3468. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3469. IF type IS SyntaxTree.EnumerationType THEN
  3470. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3471. ELSE
  3472. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3473. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3474. END;
  3475. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3476. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3477. ELSE
  3478. Error(selectorDesignator.position,"field on non-record type designator");
  3479. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3480. END;
  3481. symbol := NIL;
  3482. IF scope # NIL THEN
  3483. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3484. IF symbol # NIL THEN
  3485. ResolveSymbol(symbol);
  3486. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3487. symbol.MarkUsed
  3488. ELSE
  3489. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3490. IF VerboseErrorMessage THEN
  3491. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3492. Printout.Info("scope", scope);
  3493. Printout.Info("left", left);
  3494. Printout.Info("undeclared identifier",selectorDesignator);
  3495. Printout.Info("left resolved designator",left);
  3496. END
  3497. END;
  3498. END;
  3499. END;
  3500. resolvedExpression := result;
  3501. END VisitSelectorDesignator;
  3502. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3503. VAR len,idx: LONGINT;
  3504. BEGIN
  3505. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3506. IF idx < 0 THEN
  3507. Error(index.position,"index out of bounds (too small)")
  3508. ELSE
  3509. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3510. Error(index.position,"index out of bounds (too large)");
  3511. END;
  3512. END;
  3513. END;
  3514. END IndexCheck;
  3515. (*
  3516. - if index designator has not type, use newBaseType as its type
  3517. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3518. - special rule: if static array of dynamic array occurs, make it all dynamic
  3519. index designator type: new base type: new index designator type:
  3520. NIL z z
  3521. ARRAY [x, y] z ARRAY [x, y] OF z
  3522. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3523. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3524. *)
  3525. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3526. VAR
  3527. mathArrayType: SyntaxTree.MathArrayType;
  3528. makeDynamic: BOOLEAN;
  3529. BEGIN
  3530. IF indexDesignator.type = NIL THEN
  3531. indexDesignator.SetType(newBaseType)
  3532. ELSE
  3533. (* index designator must be a of math array type *)
  3534. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3535. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3536. (* determine if all arrays have to be made dynamic *)
  3537. makeDynamic :=
  3538. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3539. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3540. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3541. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3542. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3543. END;
  3544. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3545. mathArrayType.SetArrayBase(newBaseType)
  3546. END
  3547. END SetIndexBaseType;
  3548. (** check and append index list element to index designator of math array
  3549. - check validity of single index or array range
  3550. - compute new type
  3551. - if range then create new array type (calculate length of resulting array)
  3552. - otherwise take sourceArray.arrayBase as new type
  3553. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3554. **)
  3555. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3556. VAR
  3557. targetArray: SyntaxTree.MathArrayType;
  3558. first, last, step: SyntaxTree.Expression;
  3559. firstValue, lastValue, stepValue, length: LONGINT;
  3560. rangeExpression: SyntaxTree.RangeExpression;
  3561. BEGIN
  3562. IF indexListItem.type = SyntaxTree.invalidType THEN
  3563. (* error already handled *)
  3564. indexDesignator.parameters.AddExpression(indexListItem)
  3565. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3566. indexDesignator.HasRange;
  3567. indexDesignator.HasTensorRange;
  3568. indexDesignator.parameters.AddExpression(indexListItem);
  3569. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3570. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3571. IndexCheck(indexListItem, sourceArray.length);
  3572. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3573. indexDesignator.parameters.AddExpression(indexListItem)
  3574. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3575. indexDesignator.HasRange;
  3576. (* if the range is given as an array range expression, check the validity of its components *)
  3577. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3578. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3579. first := rangeExpression.first;
  3580. last := rangeExpression.last;
  3581. step := rangeExpression.step;
  3582. (* perform static checks on range components *)
  3583. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3584. Error(indexListItem.position,"lower bound of array range too small")
  3585. END;
  3586. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3587. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3588. Error(indexListItem.position,"upper bound of array range too large")
  3589. END
  3590. END;
  3591. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3592. Error(indexListItem.position,"invalid step size")
  3593. END;
  3594. (* add conversions to size type *)
  3595. (* TODO: needed? *)
  3596. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3597. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3598. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3599. END;
  3600. IF indexDesignator.hasTensorRange THEN
  3601. (* the index designator's base type is a tensor: leave it as is *)
  3602. ELSE
  3603. (* append a new math array to the index designator's base type *)
  3604. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3605. IF ~error THEN
  3606. (*
  3607. (* optimization: calculate length of target array for static ranges *)
  3608. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3609. IF IsStaticallyOpenRange(rangeExpression) THEN
  3610. (* range is open ('*'): reuse source array length as target array length *)
  3611. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3612. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3613. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3614. IF lastValue = MAX(LONGINT) THEN
  3615. IF IsIntegerValue(sourceArray.length, length) THEN
  3616. lastValue := length - 1;
  3617. isStaticTargetArrayLength := TRUE
  3618. ELSE
  3619. isStaticTargetArrayLength := FALSE
  3620. END
  3621. ELSE
  3622. isStaticTargetArrayLength := TRUE
  3623. END;
  3624. IF isStaticTargetArrayLength THEN
  3625. (* calculate static target array length *)
  3626. IF firstValue > lastValue THEN
  3627. length := 0
  3628. ELSE
  3629. length := 1 + lastValue - firstValue;
  3630. IF length MOD stepValue = 0 THEN
  3631. length := length DIV stepValue
  3632. ELSE
  3633. length := length DIV stepValue + 1
  3634. END
  3635. END;
  3636. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3637. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3638. ASSERT(targetArray.form = SyntaxTree.Static)
  3639. END
  3640. END
  3641. END
  3642. *)
  3643. END;
  3644. SetIndexBaseType(indexDesignator, targetArray)
  3645. END;
  3646. indexDesignator.parameters.AddExpression(indexListItem)
  3647. ELSE
  3648. Error(position,"invalid index list item");
  3649. END;
  3650. END AppendMathIndex;
  3651. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3652. VAR parameters: SyntaxTree.ExpressionList;
  3653. BEGIN
  3654. parameters := index.parameters;
  3655. IF (expression.type = NIL) THEN
  3656. Error(position, "invalid index");
  3657. ELSIF IsIntegerType(expression.type.resolved) THEN
  3658. IF over IS SyntaxTree.ArrayType THEN
  3659. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3660. ELSIF over IS SyntaxTree.StringType THEN
  3661. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3662. END;
  3663. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3664. parameters.AddExpression(expression);
  3665. ELSE
  3666. Error(position, "invalid index");
  3667. END;
  3668. END AppendIndex;
  3669. (** convert an expression to math array type
  3670. if expression is of math array type: return expression itself
  3671. if expression is of array-structured object type: return an index operator call on it
  3672. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3673. otherwise: return invalid expression
  3674. **)
  3675. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3676. VAR
  3677. result: SyntaxTree.Expression;
  3678. mathArrayType: SyntaxTree.MathArrayType;
  3679. BEGIN
  3680. IF expression.type = NIL THEN
  3681. result := SyntaxTree.invalidExpression
  3682. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3683. (* expression of math array type *)
  3684. result := expression
  3685. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3686. (* expression of array-structured object type *)
  3687. mathArrayType := MathArrayStructureOfType(expression.type);
  3688. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3689. ELSE
  3690. result := SyntaxTree.invalidExpression
  3691. END;
  3692. RETURN result
  3693. END ConvertToMathArray;
  3694. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3695. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3696. VAR
  3697. result: SyntaxTree.ExpressionList;
  3698. i: LONGINT;
  3699. BEGIN
  3700. result := SyntaxTree.NewExpressionList();
  3701. FOR i := 1 TO itemCount DO
  3702. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3703. END;
  3704. RETURN result
  3705. END ListOfOpenRanges;
  3706. (** create a procedure call designator for an index operator call on an array-structured object type
  3707. - use given index list as actual parameters
  3708. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3709. **)
  3710. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3711. VAR
  3712. operator: SyntaxTree.Operator;
  3713. expression: SyntaxTree.Expression;
  3714. actualParameters, tempList: SyntaxTree.ExpressionList;
  3715. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3716. result, tempDesignator: SyntaxTree.Designator;
  3717. recordType: SyntaxTree.RecordType;
  3718. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3719. i, hashValue, indexListSize, indexListKind: LONGINT;
  3720. castReturnType: SyntaxTree.MathArrayType;
  3721. BEGIN
  3722. ASSERT(IsArrayStructuredObjectType(left.type));
  3723. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3724. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3725. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3726. indexListSize := indexList.Length();
  3727. indexListKind := 0;
  3728. containsNonRange := FALSE;
  3729. FOR i := 0 TO indexList.Length() - 1 DO
  3730. indexListKind := indexListKind * 2;
  3731. expression := indexList.GetExpression(i);
  3732. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3733. INC(indexListKind)
  3734. ELSE
  3735. containsNonRange := TRUE
  3736. END
  3737. END;
  3738. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3739. (* select applicable index operator
  3740. - try to look up optimal index operator
  3741. - if not present, use operator on ranges
  3742. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3743. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3744. *)
  3745. usesGeneralOperator := FALSE;
  3746. IF rhs # NIL THEN
  3747. (* write operator *)
  3748. IF hashValue = -1 THEN
  3749. operator := NIL
  3750. ELSE
  3751. operator := recordType.arrayAccessOperators.write[hashValue];
  3752. END;
  3753. IF operator = NIL THEN
  3754. usesPureRangeOperator := TRUE;
  3755. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3756. operator := recordType.arrayAccessOperators.generalWrite;
  3757. usesGeneralOperator := TRUE
  3758. ELSE
  3759. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3760. operator := recordType.arrayAccessOperators.write[hashValue];
  3761. END
  3762. END
  3763. ELSE
  3764. (* read operator *)
  3765. IF hashValue = -1 THEN
  3766. operator := NIL
  3767. ELSE
  3768. operator := recordType.arrayAccessOperators.read[hashValue];
  3769. END;
  3770. IF operator = NIL THEN
  3771. usesPureRangeOperator := TRUE;
  3772. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3773. operator := recordType.arrayAccessOperators.generalRead;
  3774. usesGeneralOperator := TRUE
  3775. ELSE
  3776. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3777. operator := recordType.arrayAccessOperators.read[hashValue];
  3778. END
  3779. END
  3780. END;
  3781. IF operator = NIL THEN
  3782. Error(position, "call of undeclared [] operator");
  3783. result := SyntaxTree.invalidDesignator;
  3784. ELSE
  3785. (* determine if reshaping is needed *)
  3786. needsReshaping := containsNonRange & usesPureRangeOperator;
  3787. (* import OCArrayBase if reshaping is needed *)
  3788. IF needsReshaping & ~arrayBaseImported THEN
  3789. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3790. arrayBaseImported := TRUE
  3791. END;
  3792. (* add the index list item to the list of actual parameters
  3793. - for general operators: add a single inline array containing the index list items as parameter
  3794. - otherwise: add all index list items as individual parameters
  3795. *)
  3796. actualParameters := SyntaxTree.NewExpressionList();
  3797. IF usesGeneralOperator THEN
  3798. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3799. END;
  3800. FOR i := 0 TO indexListSize - 1 DO
  3801. expression := indexList.GetExpression(i);
  3802. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3803. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3804. tempList := SyntaxTree.NewExpressionList();
  3805. tempList.AddExpression(expression);
  3806. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3807. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3808. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3809. END;
  3810. IF usesGeneralOperator THEN
  3811. tempMathArrayExpression.elements.AddExpression(expression);
  3812. ELSE
  3813. actualParameters.AddExpression(expression)
  3814. END
  3815. END;
  3816. IF usesGeneralOperator THEN
  3817. actualParameters.AddExpression(tempMathArrayExpression)
  3818. END;
  3819. IF rhs # NIL THEN
  3820. (* add actual parameter for RHS *)
  3821. IF needsReshaping THEN
  3822. (* reshape using OCArrayBase.ExpandDimensions *)
  3823. tempList := SyntaxTree.NewExpressionList();
  3824. (* source array *)
  3825. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3826. tempList.AddExpression(rhs);
  3827. ELSE
  3828. (* convert scalar to one-dimensional array *)
  3829. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3830. tempMathArrayExpression.elements.AddExpression(rhs);
  3831. tempList.AddExpression(tempMathArrayExpression)
  3832. END;
  3833. (* list of kept dimensions *)
  3834. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3835. FOR i := 0 TO indexListSize - 1 DO
  3836. expression := indexList.GetExpression(i);
  3837. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3838. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3839. ELSE
  3840. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3841. END
  3842. END;
  3843. tempList.AddExpression(tempMathArrayExpression);
  3844. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3845. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3846. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3847. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3848. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3849. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3850. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3851. expression.SetType(castReturnType);
  3852. ELSE
  3853. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3854. END;
  3855. actualParameters.AddExpression(expression)
  3856. ELSE
  3857. actualParameters.AddExpression(rhs)
  3858. END
  3859. END;
  3860. (* add dereference operator and create procedure call designator *)
  3861. ASSERT(left IS SyntaxTree.Designator);
  3862. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3863. ASSERT(expression IS SyntaxTree.Designator);
  3864. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3865. IF (rhs = NIL) & needsReshaping THEN
  3866. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3867. tempList := SyntaxTree.NewExpressionList();
  3868. FOR i := 0 TO indexList.Length() - 1 DO
  3869. expression := indexList.GetExpression(i);
  3870. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3871. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3872. ELSE
  3873. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3874. END
  3875. END;
  3876. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3877. END;
  3878. IF rhs = NIL THEN
  3879. (* special rule: index read operator calls are considered to be assignable *)
  3880. result.SetAssignable(TRUE)
  3881. END;
  3882. (* put information about this index operator call into the resulting designator *)
  3883. result.SetRelatedAsot(left);
  3884. result.SetRelatedIndexList(indexList)
  3885. END;
  3886. RETURN result
  3887. END NewIndexOperatorCall;
  3888. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3889. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3890. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3891. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3892. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3893. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3894. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3895. CONST trace = FALSE;
  3896. BEGIN
  3897. IF trace THEN
  3898. FOR i := 0 TO actualParameters.Length()-1 DO
  3899. Printout.Info("par", actualParameters.GetExpression(i));
  3900. END;
  3901. END;
  3902. operator := scope.firstOperator;
  3903. WHILE(operator # NIL) DO
  3904. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3905. procedureType := operator.type(SyntaxTree.ProcedureType);
  3906. distance := Distance(system, procedureType,actualParameters);
  3907. IF trace THEN Printout.Info("check op ",operator) END;
  3908. IF distance < bestDistance THEN
  3909. IF trace THEN Printout.Info("taken op",operator) END;
  3910. bestDistance := distance;
  3911. bestOperator := operator;
  3912. END;
  3913. END;
  3914. operator := operator.nextOperator;
  3915. END;
  3916. END FindInScope;
  3917. BEGIN
  3918. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3919. IF oper = 0 THEN (* index *)
  3920. identifier := SyntaxTree.NewIdentifier("[]");
  3921. ELSE
  3922. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3923. END;
  3924. WHILE (recordType # NIL) DO
  3925. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3926. recordType := recordType.GetBaseRecord();
  3927. END;
  3928. RETURN bestOperator
  3929. END FindOperator;
  3930. BEGIN
  3931. type := left.type.resolved;
  3932. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3933. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3934. actualParameters := SyntaxTree.NewExpressionList();
  3935. IF parameters # NIL THEN
  3936. FOR i := 0 TO parameters.Length()-1 DO
  3937. expression := ResolveExpression(parameters.GetExpression(i));
  3938. actualParameters.AddExpression(expression);
  3939. END;
  3940. END;
  3941. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3942. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3943. IF op # NIL THEN
  3944. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3945. ASSERT(expression IS SyntaxTree.Designator);
  3946. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3947. result.SetRelatedAsot(left);
  3948. result.SetRelatedIndexList(parameters);
  3949. (* check if write operator exists, for var parameters *)
  3950. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3951. actualParameters := SyntaxTree.NewExpressionList();
  3952. FOR i := 0 TO parameters.Length()-1 DO
  3953. expression := ResolveExpression(parameters.GetExpression(i));
  3954. actualParameters.AddExpression(expression);
  3955. END;
  3956. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3957. actualParameters.AddExpression(rhs);
  3958. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3959. IF op = NIL THEN rhs := NIL END;
  3960. END;
  3961. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3962. ELSE
  3963. result := NIL;
  3964. END;
  3965. RETURN result;
  3966. END NewObjectOperatorCall;
  3967. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3968. 1. convert bracket designator chains into a single one that contains separators
  3969. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3970. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3971. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3972. - if an array or math array is indexed over, create index designator
  3973. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3974. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3975. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3976. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3977. - if an array-structured object type is indexed over, create procedure call designator
  3978. e.g.: a[x, y] -> a^."[]"(x, y)
  3979. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3980. - a[i, *] = a[i][*]
  3981. - a[*, i] # a[*][i]
  3982. Because:
  3983. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3984. - 'i-th column' = a[*, i]
  3985. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3986. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3987. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3988. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3989. **)
  3990. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3991. VAR
  3992. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3993. indexDesignator: SyntaxTree.IndexDesignator;
  3994. designator: SyntaxTree.Designator;
  3995. type: SyntaxTree.Type;
  3996. recordType: SyntaxTree.RecordType;
  3997. expression, rhs: SyntaxTree.Expression;
  3998. indexList: SyntaxTree.ExpressionList;
  3999. i: LONGINT;
  4000. hasError, done: BOOLEAN;
  4001. PROCEDURE FinalizeIndexDesignator;
  4002. BEGIN
  4003. IF indexDesignator # NIL THEN
  4004. (* the end of a tensor has been reached: *)
  4005. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4006. SetIndexBaseType(indexDesignator, type);
  4007. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4008. designator := indexDesignator;
  4009. type := designator.type.resolved;
  4010. indexDesignator := NIL;
  4011. ASSERT(SyntaxTree.Resolved IN type.state)
  4012. END
  4013. END FinalizeIndexDesignator;
  4014. BEGIN
  4015. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4016. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4017. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4018. (* copy all index list entries including a separator to the left bracket designator *)
  4019. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4020. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4021. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4022. END;
  4023. (* propagate the related RHS *)
  4024. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4025. (* only resolve left bracket designator and use as final result *)
  4026. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4027. ELSE
  4028. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4029. designator := ResolveDesignator(bracketDesignator.left);
  4030. type := designator.type.resolved;
  4031. indexDesignator := NIL;
  4032. (*!!! clean up *)
  4033. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4034. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4035. IF resolvedExpression = NIL THEN
  4036. Error(bracketDesignator.position,"undefined operator");
  4037. resolvedExpression := SyntaxTree.invalidDesignator
  4038. END;
  4039. RETURN;
  4040. END;
  4041. i := 0;
  4042. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4043. expression := bracketDesignator.parameters.GetExpression(i);
  4044. expression := ResolveExpression(expression);
  4045. bracketDesignator.parameters.SetExpression(i, expression);
  4046. IF expression = SyntaxTree.indexListSeparator THEN
  4047. (* finalize an existing index designator if needed *)
  4048. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4049. INC(i)
  4050. ELSE
  4051. (* do auto-dereferencing if needed *)
  4052. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4053. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4054. & (i=0)*)
  4055. THEN
  4056. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4057. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4058. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4059. designator := SyntaxTree.invalidDesignator;
  4060. type := SyntaxTree.invalidType
  4061. ELSE
  4062. FinalizeIndexDesignator;
  4063. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4064. type := designator.type.resolved
  4065. END
  4066. END;
  4067. (* create a new index designator, if needed *)
  4068. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4069. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4070. indexDesignator.SetAssignable(designator.assignable);
  4071. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4072. (* designator := indexDesignator *)
  4073. END;
  4074. IF type = SyntaxTree.invalidType THEN
  4075. (* error already handled *)
  4076. INC(i)
  4077. ELSIF type IS SyntaxTree.ArrayType THEN
  4078. (* indexing over an array *)
  4079. ASSERT(indexDesignator # NIL);
  4080. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4081. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4082. INC(i)
  4083. ELSIF type IS SyntaxTree.StringType THEN
  4084. (* indexing over an array *)
  4085. ASSERT(indexDesignator # NIL);
  4086. AppendIndex(expression.position, indexDesignator, expression, type);
  4087. type := type(SyntaxTree.StringType).baseType.resolved;
  4088. INC(i)
  4089. ELSIF type IS SyntaxTree.MathArrayType THEN
  4090. (* indexing over a math array *)
  4091. ASSERT(indexDesignator # NIL);
  4092. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4093. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4094. INC(i)
  4095. ELSIF IsArrayStructuredObjectType(type) THEN
  4096. (* indexing over ASOTs *)
  4097. FinalizeIndexDesignator;
  4098. ASSERT(type IS SyntaxTree.PointerType);
  4099. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4100. (*
  4101. - collect index list items from bracket designator that belong to ASOT
  4102. - check for errors
  4103. *)
  4104. indexList := SyntaxTree.NewExpressionList();
  4105. hasError := FALSE;
  4106. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4107. (* indexing over tensor ASOT:
  4108. - stop at index list end or separator
  4109. - dimensionality is given by number of index list items
  4110. *)
  4111. done := FALSE;
  4112. WHILE ~done DO
  4113. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4114. done := TRUE;
  4115. ELSE
  4116. expression := bracketDesignator.parameters.GetExpression(i);
  4117. IF expression = SyntaxTree.indexListSeparator THEN
  4118. done := TRUE;
  4119. ELSE
  4120. expression := ResolveExpression(expression);
  4121. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4122. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4123. hasError := TRUE
  4124. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4125. Error(expression.position, "integer or range expected");
  4126. expression := SyntaxTree.invalidExpression;
  4127. hasError := TRUE
  4128. END;
  4129. indexList.AddExpression(expression)
  4130. END;
  4131. INC(i)
  4132. END
  4133. END
  4134. ELSE
  4135. (* indexing over non-tensor ASOT:
  4136. - ignore separators
  4137. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4138. *)
  4139. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4140. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4141. expression := bracketDesignator.parameters.GetExpression(i);
  4142. ELSE
  4143. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4144. END;
  4145. IF expression # SyntaxTree.indexListSeparator THEN
  4146. expression := ResolveExpression(expression);
  4147. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4148. Error(expression.position, "integer or range expected");
  4149. expression := SyntaxTree.invalidExpression;
  4150. hasError := TRUE
  4151. END;
  4152. indexList.AddExpression(expression)
  4153. END;
  4154. INC(i)
  4155. END;
  4156. END;
  4157. IF hasError THEN
  4158. designator := SyntaxTree.invalidDesignator;
  4159. type := SyntaxTree.invalidType;
  4160. ELSE
  4161. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4162. and the last entry in the index list belongs to the array-structured object type in question.
  4163. E.g.: for a 2-dimensional array-structured object type:
  4164. - 'lhs := asot[1, 2]' -> read mode
  4165. - 'asot[1, 2] := rhs' -> write mode
  4166. - 'asot[1, 2, 3] := rhs' -> read mode
  4167. *)
  4168. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4169. rhs := bracketDesignator.relatedRhs
  4170. ELSE
  4171. rhs := NIL
  4172. END;
  4173. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4174. type := designator.type
  4175. END
  4176. ELSE
  4177. Error(expression.position,"indexing over non-array type");
  4178. designator := SyntaxTree.invalidDesignator;
  4179. type := SyntaxTree.invalidType;
  4180. INC(i)
  4181. END
  4182. END
  4183. END;
  4184. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4185. resolvedExpression := designator
  4186. END
  4187. END VisitBracketDesignator;
  4188. (** check and resolve expression list
  4189. - resolve each expression in an expression list
  4190. - returns true if and only if all statements could have successfully been resolved
  4191. **)
  4192. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4193. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4194. BEGIN
  4195. result := TRUE;
  4196. FOR i := 0 TO expressionList.Length()-1 DO
  4197. expression := ResolveExpression(expressionList.GetExpression(i));
  4198. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4199. expressionList.SetExpression(i,expression);
  4200. END;
  4201. RETURN result
  4202. END ExpressionList;
  4203. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4204. BEGIN
  4205. type := type.resolved;
  4206. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4207. RETURN TRUE
  4208. ELSIF system.CanPassInRegister # NIL THEN
  4209. RETURN system.CanPassInRegister(type);
  4210. ELSE
  4211. RETURN FALSE
  4212. END;
  4213. END CanPassInRegister;
  4214. (** return procedure call designator left(actualParameters)
  4215. - check realtime procedure call in realtime procedure
  4216. - check number of parameters
  4217. - check parameter compatibility
  4218. return invalidDesignator if error
  4219. **)
  4220. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4221. VAR result: SyntaxTree.Designator;
  4222. numberFormalParameters, numberActualParameters: LONGINT;
  4223. formalType: SyntaxTree.ProcedureType;
  4224. formalParameter: SyntaxTree.Parameter;
  4225. actualParameter: SyntaxTree.Expression;
  4226. i: LONGINT;
  4227. BEGIN
  4228. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4229. result := SyntaxTree.invalidDesignator;
  4230. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4231. numberFormalParameters := formalType.numberParameters;
  4232. numberActualParameters := actualParameters.Length();
  4233. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4234. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4235. END;
  4236. IF ~ExpressionList(actualParameters) THEN
  4237. result := SyntaxTree.invalidDesignator
  4238. ELSE
  4239. IF numberActualParameters <= numberFormalParameters THEN
  4240. formalParameter := formalType.firstParameter;
  4241. FOR i := 0 TO numberActualParameters-1 DO
  4242. actualParameter := actualParameters.GetExpression(i);
  4243. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4244. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4245. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4246. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4247. ELSE
  4248. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4249. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4250. END;
  4251. actualParameters.SetExpression(i,actualParameter);
  4252. END;
  4253. formalParameter := formalParameter.nextParameter;
  4254. END;
  4255. WHILE (formalParameter # NIL) DO
  4256. IF formalParameter.defaultValue # NIL THEN
  4257. actualParameters.AddExpression(formalParameter.defaultValue);
  4258. formalParameter := formalParameter.nextParameter
  4259. ELSE
  4260. Error(position, "less actual than formal parameters");
  4261. formalParameter := NIL;
  4262. END;
  4263. END;
  4264. ELSE
  4265. Error(position, "more actual than formal parameters")
  4266. END;
  4267. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4268. result.SetAssignable(FALSE);
  4269. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4270. END;
  4271. RETURN result
  4272. END NewProcedureCallDesignator;
  4273. (**
  4274. builtin call designator generated in VisitParameterDesignator
  4275. -> nothing to be resolved
  4276. **)
  4277. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4278. BEGIN
  4279. resolvedExpression := x;
  4280. END VisitTypeGuardDesignator;
  4281. (**
  4282. builtin call designator generated in VisitParameterDesignator
  4283. -> nothing to be resolved
  4284. **)
  4285. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4286. BEGIN
  4287. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4288. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4289. ASSERT(resolvedExpression.type # NIL);
  4290. ELSIF ExpressionList(x.parameters) THEN
  4291. resolvedExpression := x;
  4292. END;
  4293. END VisitBuiltinCallDesignator;
  4294. (**
  4295. procedure call designator generated in VisitParameterDesignator
  4296. -> nothing to be resolved
  4297. **)
  4298. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4299. BEGIN
  4300. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4301. resolvedExpression := x;
  4302. END VisitProcedureCallDesignator;
  4303. (** return true if x is a variable else return false and report error **)
  4304. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4305. VAR result: BOOLEAN;
  4306. BEGIN
  4307. result := TRUE;
  4308. IF x = SyntaxTree.invalidExpression THEN
  4309. result := FALSE;
  4310. ELSIF ~IsVariable(x) THEN
  4311. Error(x.position,"non variable expression");
  4312. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4313. result := FALSE;
  4314. END;
  4315. RETURN result
  4316. END CheckVariable;
  4317. (**
  4318. if expression x is of basic type then return true else report error and return false
  4319. **)
  4320. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4321. VAR result: BOOLEAN;
  4322. BEGIN
  4323. result := FALSE;
  4324. IF x = SyntaxTree.invalidExpression THEN
  4325. ELSIF ~IsBasicType(x.type) THEN
  4326. Error(x.position,"is no basic type");
  4327. result := FALSE
  4328. ELSE result := TRUE
  4329. END;
  4330. RETURN result
  4331. END CheckBasicType;
  4332. (**
  4333. if expression x is of number type then return true else report error and return false
  4334. **)
  4335. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4336. VAR result: BOOLEAN;
  4337. BEGIN
  4338. result := FALSE;
  4339. IF x = SyntaxTree.invalidExpression THEN
  4340. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4341. Error(x.position,"is non number type");
  4342. ELSE result := TRUE
  4343. END;
  4344. RETURN result
  4345. END CheckNumberType;
  4346. (**
  4347. if expression x is of number or size type but not complex then return true else report error and return false
  4348. **)
  4349. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4350. VAR result: BOOLEAN;
  4351. BEGIN
  4352. result := FALSE;
  4353. IF x = SyntaxTree.invalidExpression THEN
  4354. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4355. Error(x.position,"is complex type");
  4356. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4357. Error(x.position,"is non number type");
  4358. ELSE result := TRUE
  4359. END;
  4360. RETURN result
  4361. END CheckNonComplexNumberSizeType;
  4362. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4363. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4364. BEGIN
  4365. result := FALSE; type := x.type.resolved;
  4366. IF x = SyntaxTree.invalidExpression THEN
  4367. 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
  4368. TRACE(type.sizeInBits);
  4369. TRACE(system.addressType.sizeInBits);
  4370. Error(x.position,"is no address type");
  4371. ELSE result := TRUE
  4372. END;
  4373. RETURN result
  4374. END CheckAddressType;
  4375. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4376. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4377. BEGIN
  4378. result := FALSE; type := x.type.resolved;
  4379. IF x = SyntaxTree.invalidExpression THEN
  4380. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4381. Error(x.position,"is no size type");
  4382. ELSE result := TRUE
  4383. END;
  4384. RETURN result
  4385. END CheckSizeType;
  4386. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4387. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4388. BEGIN
  4389. result := FALSE; type := x.type.resolved;
  4390. IF x = SyntaxTree.invalidExpression THEN
  4391. 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
  4392. Error(x.position,"is no object type");
  4393. ELSE result := TRUE
  4394. END;
  4395. RETURN result
  4396. END CheckObjectType;
  4397. (**
  4398. if expression x is of integer type then return true else report error and return false
  4399. **)
  4400. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4401. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4402. BEGIN
  4403. result := FALSE; type := x.type.resolved;
  4404. IF x = SyntaxTree.invalidExpression THEN
  4405. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4406. Error(x.position,"is no integer type");
  4407. ELSE result := TRUE
  4408. END;
  4409. RETURN result
  4410. END CheckIntegerType;
  4411. (**
  4412. if expression x is of character type then return true else report error and return false
  4413. **)
  4414. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4415. VAR result: BOOLEAN;
  4416. BEGIN
  4417. result := FALSE;
  4418. IF x = SyntaxTree.invalidExpression THEN
  4419. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4420. Error(x.position,"is no character type");
  4421. ELSE result := TRUE
  4422. END;
  4423. RETURN result
  4424. END CheckCharacterType;
  4425. (**
  4426. if expression x is of real type then return true else report error and return false
  4427. **)
  4428. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4429. VAR result: BOOLEAN;
  4430. BEGIN
  4431. result := FALSE;
  4432. IF x = SyntaxTree.invalidExpression THEN
  4433. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4434. Error(x.position,"is no float type");
  4435. ELSE result := TRUE
  4436. END;
  4437. RETURN result
  4438. END CheckRealType;
  4439. (**
  4440. if expression x is of range type then return true else report error and return false
  4441. **)
  4442. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4443. VAR result: BOOLEAN;
  4444. BEGIN
  4445. result := FALSE;
  4446. IF x = SyntaxTree.invalidExpression THEN
  4447. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4448. Error(x.position,"is no range type");
  4449. ELSE result := TRUE
  4450. END;
  4451. RETURN result
  4452. END CheckRangeType;
  4453. (**
  4454. if expression x is of boolean type then return true else report error and return false
  4455. **)
  4456. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4457. VAR result: BOOLEAN;
  4458. BEGIN
  4459. result := FALSE;
  4460. IF x = SyntaxTree.invalidExpression THEN
  4461. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4462. Error(x.position,"is no boolean type");
  4463. ELSE result := TRUE
  4464. END;
  4465. RETURN result
  4466. END CheckBooleanType;
  4467. (**
  4468. if expression x is of set type then return true else report error and return false
  4469. **)
  4470. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4471. VAR result: BOOLEAN;
  4472. BEGIN
  4473. result := FALSE;
  4474. IF x = SyntaxTree.invalidExpression THEN
  4475. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4476. Error(x.position,"is no set type");
  4477. ELSE result := TRUE
  4478. END;
  4479. RETURN result
  4480. END CheckSetType;
  4481. (**
  4482. if expression x is of string or array of character type then return true else report error and return false
  4483. **)
  4484. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4485. VAR result: BOOLEAN;
  4486. BEGIN
  4487. result := FALSE;
  4488. IF x = SyntaxTree.invalidExpression THEN
  4489. ELSIF ~IsStringType(x.type.resolved) THEN
  4490. Error(x.position,"is no string type");
  4491. ELSE result := TRUE
  4492. END;
  4493. RETURN result
  4494. END CheckStringType;
  4495. (**
  4496. if expression x is a type declaration type return true else report error and return false
  4497. **)
  4498. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4499. VAR result: BOOLEAN;
  4500. BEGIN
  4501. result := FALSE;
  4502. IF x = SyntaxTree.invalidExpression THEN
  4503. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4504. Error(x.position,"is not a type declaration");
  4505. ELSE result := TRUE
  4506. END;
  4507. RETURN result
  4508. END CheckTypeDeclarationType;
  4509. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4510. VAR result: BOOLEAN;
  4511. BEGIN
  4512. result := FALSE;
  4513. IF x = SyntaxTree.invalidExpression THEN
  4514. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4515. result := TRUE;
  4516. value := x.resolved(SyntaxTree.IntegerValue).value;
  4517. ELSE
  4518. Error(x.position,"expression is not an integer constant");
  4519. END;
  4520. RETURN result;
  4521. END CheckIntegerValue;
  4522. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4523. VAR result: BOOLEAN;
  4524. BEGIN
  4525. result := FALSE;
  4526. IF x = SyntaxTree.invalidExpression THEN
  4527. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4528. result := TRUE;
  4529. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4530. ELSE
  4531. Error(x.position,"expression is not an integer constant");
  4532. END;
  4533. RETURN result;
  4534. END CheckStringValue;
  4535. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4536. BEGIN
  4537. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4538. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4539. ELSE
  4540. RETURN FALSE
  4541. END;
  4542. END IsUnsignedValue;
  4543. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4544. BEGIN
  4545. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4546. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4547. ELSE
  4548. RETURN FALSE
  4549. END
  4550. END IsAddressValue;
  4551. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4552. BEGIN
  4553. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4554. END IsAddressExpression;
  4555. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4556. BEGIN
  4557. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4558. END IsSizeExpression;
  4559. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4560. VAR result: BOOLEAN;
  4561. BEGIN
  4562. result := FALSE;
  4563. IF x = SyntaxTree.invalidExpression THEN
  4564. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4565. result := TRUE;
  4566. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4567. ELSE
  4568. Error(x.position,"expression is not an integer constant");
  4569. END;
  4570. RETURN result;
  4571. END CheckEnumerationValue;
  4572. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4573. VAR result: BOOLEAN;
  4574. BEGIN
  4575. result := FALSE;
  4576. IF x = SyntaxTree.invalidExpression THEN
  4577. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4578. result := TRUE;
  4579. value := x.resolved(SyntaxTree.CharacterValue).value;
  4580. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4581. result := TRUE;
  4582. value := x.resolved(SyntaxTree.StringValue).value[0];
  4583. ELSE
  4584. Error(x.position,"expression is not a character constant");
  4585. END;
  4586. RETURN result;
  4587. END CheckCharacterValue;
  4588. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4589. VAR result: BOOLEAN;
  4590. BEGIN
  4591. result := FALSE;
  4592. IF x = SyntaxTree.invalidExpression THEN
  4593. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4594. value := x.resolved(SyntaxTree.IntegerValue).value;
  4595. IF (value > 0) OR includeZero & (value = 0) THEN
  4596. result := TRUE;
  4597. ELSE
  4598. Error(x.position,"integer is not positive");
  4599. END
  4600. ELSE
  4601. Error(x.position,"expression is not an integer constant");
  4602. END;
  4603. RETURN result;
  4604. END CheckPositiveIntegerValue;
  4605. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4606. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4607. BEGIN
  4608. result := FALSE;
  4609. IF x = SyntaxTree.invalidExpression THEN
  4610. ELSE
  4611. type := x.type.resolved;
  4612. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4613. portType := type(SyntaxTree.PortType);
  4614. result := TRUE
  4615. ELSE
  4616. Error(x.position,"no port type");
  4617. END;
  4618. END;
  4619. RETURN result
  4620. END CheckPortType;
  4621. (* move to builtin procedure call statement ?
  4622. remove builtin procedure call designator ?
  4623. *)
  4624. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4625. VAR
  4626. numberActualParameters,numberFormalParameters: LONGINT;
  4627. formalParameter: SyntaxTree.Parameter;
  4628. actualParameter: SyntaxTree.Expression;
  4629. procedureType: SyntaxTree.ProcedureType;
  4630. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4631. inPort, outPort: SyntaxTree.PortType;
  4632. constructor: SyntaxTree.Procedure;
  4633. type0,type1,type2: SyntaxTree.Type;
  4634. type,base,parameterType: SyntaxTree.Type;
  4635. arrayType: SyntaxTree.ArrayType;
  4636. i,i0,i1: LONGINT;
  4637. r,r0,r1,im: LONGREAL;
  4638. c: CHAR;
  4639. id: LONGINT;
  4640. b: BOOLEAN;
  4641. first: LONGINT;
  4642. mathArrayType: SyntaxTree.MathArrayType;
  4643. customBuiltin: SyntaxTree.CustomBuiltin;
  4644. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4645. VAR resultB: BOOLEAN;
  4646. BEGIN
  4647. IF numberActualParameters < from THEN
  4648. Error(position, "less actual than formal parameters");
  4649. result := SyntaxTree.invalidExpression;
  4650. resultB := FALSE;
  4651. ELSIF numberActualParameters > to THEN
  4652. Error(position, "more actual than formal parameters");
  4653. result := SyntaxTree.invalidExpression;
  4654. resultB := FALSE;
  4655. ELSE
  4656. resultB := TRUE;
  4657. END;
  4658. RETURN resultB
  4659. END CheckArity;
  4660. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4661. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4662. BEGIN
  4663. WHILE modifier # NIL DO
  4664. symbol := cellType.FindProperty(modifier.identifier);
  4665. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4666. propertyType := symbol.type.resolved;
  4667. modifierType := modifier.expression.type.resolved;
  4668. IF ~CompatibleTo(system, modifierType, propertyType) &
  4669. ~(
  4670. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4671. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4672. Error(modifier.position,"incompatible to cell property");
  4673. END;
  4674. ELSE
  4675. Error(modifier.position, "undefined property");
  4676. END;
  4677. modifier := modifier.nextModifier;
  4678. END;
  4679. END CheckModifiers;
  4680. BEGIN
  4681. type := NIL; result := NIL;
  4682. type0 := NIL; type1 := NIL; type2 := NIL;
  4683. numberActualParameters := actualParameters.Length();
  4684. IF numberActualParameters>0 THEN
  4685. parameter0 := actualParameters.GetExpression(0);
  4686. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4687. Error(parameter0.position,"forbidden type-less argument");
  4688. result := SyntaxTree.invalidExpression
  4689. END
  4690. END;
  4691. IF numberActualParameters >1 THEN
  4692. parameter1 := actualParameters.GetExpression(1);
  4693. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4694. ELSE
  4695. Error(parameter1.position,"forbidden type-less argument");
  4696. result := SyntaxTree.invalidExpression
  4697. END
  4698. END;
  4699. IF numberActualParameters >2 THEN
  4700. parameter2 := actualParameters.GetExpression(2);
  4701. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4702. ELSE
  4703. Error(parameter2.position,"forbidden type-less argument");
  4704. result := SyntaxTree.invalidExpression
  4705. END
  4706. END;
  4707. IF returnType # NIL THEN
  4708. id := Global.New;
  4709. result := NIL;
  4710. ELSE
  4711. id := builtin.id;
  4712. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4713. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4714. END;
  4715. END;
  4716. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4717. ELSIF result # NIL THEN type := result.type (* operator *)
  4718. ELSE
  4719. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4720. result(SyntaxTree.Designator).SetLeft(left);
  4721. IF returnType # NIL THEN
  4722. type := returnType;
  4723. END;
  4724. (* ---- ASSERT ----- *)
  4725. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4726. IF CheckBooleanType(parameter0) THEN
  4727. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4728. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4729. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4730. *)
  4731. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4732. Error(position, "assert failed");
  4733. END;
  4734. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4735. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4736. rules imposed by the architecture / current runtime
  4737. *)
  4738. END;
  4739. END;
  4740. (* ---- COPY ----- *)
  4741. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4742. IF~IsStringType(type0) THEN
  4743. Error(parameter0.position,"no string type");
  4744. END;
  4745. IF ~IsStringType(type1) THEN
  4746. Error(parameter1.position,"no string type");
  4747. ELSIF CheckVariable(parameter1) THEN
  4748. IF (type0 IS SyntaxTree.StringType) THEN
  4749. arrayType := type1(SyntaxTree.ArrayType);
  4750. IF arrayType.form = SyntaxTree.Static THEN
  4751. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4752. Error(position, "destination length smaller than source length")
  4753. END;
  4754. END;
  4755. END;
  4756. END;
  4757. (* ---- INC, DEC----- *)
  4758. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4759. IF numberActualParameters = 1 THEN
  4760. parameter1 :=Global.NewIntegerValue(system,position,1);
  4761. actualParameters.AddExpression(parameter1);
  4762. END;
  4763. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4764. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4765. Error(position, "incompatible increment");
  4766. ELSE
  4767. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4768. actualParameters.SetExpression(1,parameter1);
  4769. END;
  4770. END;
  4771. (* ---- EXCL, INCL----- *)
  4772. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4773. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4774. IF IsIntegerValue(parameter1,i0) THEN
  4775. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4776. Error(position, "parameter out of SET range")
  4777. END;
  4778. END;
  4779. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4780. actualParameters.SetExpression(1,parameter1);
  4781. END;
  4782. (* ---- HALT, SYSTEM.HALT ----- *)
  4783. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4784. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4785. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4786. rules imposed by the architecture / current runtime
  4787. *)
  4788. END;
  4789. (* ---- WAIT ----- *)
  4790. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4791. IF CheckObjectType(parameter0) THEN
  4792. END;
  4793. (* ---- NEW ----- *)
  4794. ELSIF (id = Global.New) THEN
  4795. IF returnType # NIL THEN
  4796. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4797. ELSE
  4798. first := 1;
  4799. END;
  4800. IF CheckArity(first,Infinity) THEN
  4801. IF currentIsRealtime THEN
  4802. Error(position, "forbidden new in realtime block");
  4803. END;
  4804. (* check constructor *)
  4805. IF (first =0) OR CheckVariable(parameter0) THEN
  4806. IF type0 IS SyntaxTree.PointerType THEN
  4807. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4808. ELSIF type0 IS SyntaxTree.CellType THEN
  4809. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4810. ELSE
  4811. Error(position, "forbidden new on value type");
  4812. END;
  4813. IF type0 IS SyntaxTree.ArrayType THEN
  4814. arrayType := type0(SyntaxTree.ArrayType);
  4815. IF arrayType.form = SyntaxTree.Static THEN
  4816. i := first
  4817. ELSIF arrayType.form = SyntaxTree.Open THEN
  4818. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4819. ELSE HALT(100)
  4820. END;
  4821. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4822. i := first;
  4823. REPEAT
  4824. actualParameter := actualParameters.GetExpression(i);
  4825. IF CheckSizeType(actualParameter) THEN
  4826. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4827. actualParameters.SetExpression(i,actualParameter);
  4828. END;
  4829. INC(i);
  4830. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4831. END;
  4832. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4833. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4834. IF constructor = NIL THEN
  4835. IF CheckArity(first,first) THEN END;
  4836. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4837. Error(position, "new on object with hidden constructor");
  4838. ELSE
  4839. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4840. numberFormalParameters := procedureType.numberParameters;
  4841. IF numberActualParameters-first <= numberFormalParameters THEN
  4842. formalParameter := procedureType.firstParameter;
  4843. FOR i := first TO numberActualParameters-1 DO
  4844. actualParameter := actualParameters.GetExpression(i);
  4845. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4846. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4847. ELSE
  4848. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4849. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4850. END;
  4851. actualParameters.SetExpression(i,actualParameter);
  4852. END;
  4853. formalParameter := formalParameter.nextParameter;
  4854. END;
  4855. WHILE (formalParameter # NIL) DO
  4856. IF formalParameter.defaultValue # NIL THEN
  4857. actualParameters.AddExpression(formalParameter.defaultValue);
  4858. formalParameter := formalParameter.nextParameter
  4859. ELSE
  4860. Error(position, "less actual than formal parameters");
  4861. formalParameter := NIL;
  4862. END;
  4863. END;
  4864. ELSE
  4865. Error(position, "more actual than formal parameters")
  4866. END;
  4867. END;
  4868. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4869. mathArrayType := type0(SyntaxTree.MathArrayType);
  4870. IF mathArrayType.form = SyntaxTree.Static THEN
  4871. Error(position, "new on static array");
  4872. ELSE
  4873. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4874. i0 := first+1; i1 := Infinity;
  4875. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4876. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4877. i1 := i0;
  4878. ELSE HALT(100);
  4879. END;
  4880. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4881. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4882. base := ArrayBase(type0,MAX(LONGINT));
  4883. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4884. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4885. IF ~CompatibleTo(system,type0,parameterType) THEN
  4886. Error(parameter0.position,"incompatible parameter in new");
  4887. result := SyntaxTree.invalidExpression;
  4888. ELSE
  4889. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4890. END;
  4891. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4892. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4893. IF ~CompatibleTo(system,type1,parameterType) THEN
  4894. Error(parameter1.position,"parameter incompatible to math array of longint");
  4895. result := SyntaxTree.invalidExpression;
  4896. ELSE
  4897. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4898. END;
  4899. ELSE
  4900. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4901. i := first;
  4902. REPEAT
  4903. actualParameter := actualParameters.GetExpression(i);
  4904. IF CheckSizeType(actualParameter) THEN
  4905. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4906. actualParameters.SetExpression(i,actualParameter);
  4907. END;
  4908. INC(i);
  4909. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4910. END;
  4911. END;
  4912. END;
  4913. ELSIF type0 IS SyntaxTree.CellType THEN
  4914. IF ~(currentIsCellNet) THEN
  4915. Error(position, "cell allocation outside activeCells ");
  4916. ELSE
  4917. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4918. IF (constructor = NIL) & CheckArity(1,1) THEN
  4919. (* ok *)
  4920. ELSE
  4921. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4922. numberFormalParameters := procedureType.numberParameters;
  4923. DEC(numberActualParameters);
  4924. IF numberActualParameters <= numberFormalParameters THEN
  4925. formalParameter := procedureType.firstParameter;
  4926. FOR i := first TO numberActualParameters DO
  4927. actualParameter := actualParameters.GetExpression(i);
  4928. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4929. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4930. ELSE
  4931. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4932. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4933. END;
  4934. actualParameters.SetExpression(i,actualParameter);
  4935. END;
  4936. formalParameter := formalParameter.nextParameter;
  4937. END;
  4938. WHILE (formalParameter # NIL) DO
  4939. IF formalParameter.defaultValue # NIL THEN
  4940. actualParameters.AddExpression(formalParameter.defaultValue);
  4941. formalParameter := formalParameter.nextParameter
  4942. ELSE
  4943. Error(position, "less actual than formal parameters");
  4944. formalParameter := NIL;
  4945. END;
  4946. END;
  4947. ELSE
  4948. Error(position, "more actual than formal parameters")
  4949. END;
  4950. END;
  4951. END;
  4952. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4953. activeCellsStatement := TRUE;
  4954. ELSE
  4955. Error(position, "cannot be allocated");
  4956. END;
  4957. END;
  4958. END;
  4959. (* ---- DISPOSE ----- *)
  4960. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4961. IF ~IsPointerType(parameter0.type) THEN
  4962. Error(parameter0.position,"is not a pointer")
  4963. ELSIF ~IsDisposable(parameter0.type) THEN
  4964. Error(parameter0.position,"is not disposable")
  4965. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4966. END
  4967. (* ---- GETPROCEDURE ----- *)
  4968. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4969. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4970. IF CheckVariable(parameter2) THEN
  4971. IF ~GetProcedureAllowed(parameter2.type) THEN
  4972. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4973. END;
  4974. END;
  4975. END;
  4976. (* ---- ABS ----- *)
  4977. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4978. (* note: ABS on complex numbers is done using overloading *)
  4979. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4980. type := type0;
  4981. IF IsIntegerValue(parameter0,i0) THEN
  4982. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4983. type := Global.GetIntegerType(system,ABS(i0));
  4984. ELSIF IsRealValue(parameter0,r) THEN
  4985. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4986. END;
  4987. ELSE
  4988. type := SyntaxTree.invalidType;
  4989. END;
  4990. (* ---- ASH, ASR ----- *)
  4991. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4992. type := type0;
  4993. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4994. (*
  4995. ConvertOperands(parameter0,parameter1); (* same type *)
  4996. *)
  4997. type := parameter0.type;
  4998. IF IsIntegerValue(parameter0,i0) THEN
  4999. IF IsIntegerValue(parameter1,i1) THEN
  5000. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5001. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5002. result := ResolveExpression(result);
  5003. type := Global.GetIntegerType(system,i0);
  5004. END;
  5005. END;
  5006. IF type.resolved.sizeInBits < 32 THEN
  5007. type := system.longintType;
  5008. END;
  5009. (*!compatibility with release, remove when resolved
  5010. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5011. *)
  5012. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5013. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5014. actualParameters.SetExpression(0,parameter0);
  5015. actualParameters.SetExpression(1,parameter1);
  5016. END;
  5017. (* ---- CAP ----- *)
  5018. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5019. type := system.characterType;
  5020. IF CheckCharacterType (parameter0) THEN
  5021. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5022. actualParameters.SetExpression(0,parameter0);
  5023. IF IsCharacterValue(parameter0,c) THEN
  5024. IF (c <= "z") & (c >= "a") THEN
  5025. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5026. ELSE
  5027. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5028. END;
  5029. END;
  5030. END;
  5031. (* ---- CHR ----- *)
  5032. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5033. IF id = Global.Chr THEN
  5034. type := system.characterType
  5035. ELSE
  5036. type := system.characterType32
  5037. END;
  5038. IF CheckIntegerType(parameter0) THEN
  5039. IF IsIntegerValue(parameter0,i0) THEN
  5040. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5041. result := ResolveExpression(result);
  5042. ELSE
  5043. (*
  5044. result := NewConversion(parameter0.position,parameter0,type);
  5045. *)
  5046. END;
  5047. END
  5048. (* ---- ENTIER ----- *)
  5049. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5050. type := system.longintType;
  5051. IF CheckRealType(parameter0) THEN
  5052. IF IsRealValue(parameter0,r) THEN
  5053. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5054. type := Global.GetIntegerType(system,ENTIER(r));
  5055. END
  5056. END;
  5057. (* ---- ENTIERH ----- *)
  5058. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5059. type := system.hugeintType;
  5060. IF CheckRealType(parameter0) THEN
  5061. IF IsRealValue(parameter0,r) THEN
  5062. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5063. END
  5064. END;
  5065. (* ---- LEN ----- *)
  5066. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5067. type := system.longintType;
  5068. base := type0;
  5069. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5070. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5071. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5072. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5073. Error(position, "forbidden len on unsafe pointer");
  5074. END;
  5075. type0 := base;
  5076. ELSE
  5077. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5078. type0 := parameter0.type.resolved;
  5079. actualParameters.SetExpression(0,parameter0);
  5080. base := type0;
  5081. END;
  5082. END;
  5083. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5084. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5085. IF i1 < 0 THEN
  5086. Error(position, "invalid dimension");
  5087. base := SyntaxTree.invalidType;
  5088. ELSE
  5089. base := ArrayBase(base,i1);
  5090. IF (base # NIL) & Indexable(base) THEN
  5091. ELSE
  5092. Error(position, "len on no array");
  5093. IF VerboseErrorMessage THEN
  5094. Printout.Info("base",base);
  5095. END;
  5096. base := SyntaxTree.invalidType;
  5097. END;
  5098. END;
  5099. IF numberActualParameters=2 THEN
  5100. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5101. actualParameters.SetExpression(1,parameter1);
  5102. ELSIF base IS SyntaxTree.MathArrayType THEN
  5103. Error(position, "missing dimension specification");
  5104. END;
  5105. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5106. IF base IS SyntaxTree.ArrayType THEN
  5107. arrayType := base(SyntaxTree.ArrayType);
  5108. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5109. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5110. result := Global.NewIntegerValue(system,position,i);
  5111. type := result.type;(* arrayType.length.type;*)
  5112. ASSERT(type # NIL);
  5113. END;
  5114. ELSIF base IS SyntaxTree.MathArrayType THEN
  5115. mathArrayType := base(SyntaxTree.MathArrayType);
  5116. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5117. result := Global.NewIntegerValue(system,position,i);
  5118. type := result.type;
  5119. (*
  5120. type := mathArrayType.length.type;
  5121. *)
  5122. ASSERT(type # NIL);
  5123. END;
  5124. END;
  5125. END;
  5126. ELSE
  5127. type := system.longintType;
  5128. END;
  5129. (* ---- FIRST ---- *)
  5130. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5131. type := system.longintType;
  5132. IF CheckRangeType(parameter0) THEN END;
  5133. result.SetAssignable(parameter0.assignable)
  5134. (* ---- LAST ---- *)
  5135. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5136. type := system.longintType;
  5137. IF CheckRangeType(parameter0) THEN END;
  5138. result.SetAssignable(parameter0.assignable)
  5139. (* ---- STEP ---- *)
  5140. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5141. type := system.longintType;
  5142. IF CheckRangeType(parameter0) THEN END;
  5143. result.SetAssignable(parameter0.assignable)
  5144. (* ---- RE ---- *)
  5145. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5146. IF CheckNumberType(parameter0) THEN
  5147. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5148. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5149. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5150. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5151. type := parameter0.type
  5152. ELSE
  5153. type := system.realType
  5154. END
  5155. END;
  5156. result.SetAssignable(parameter0.assignable)
  5157. (* ---- IM ---- *)
  5158. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5159. IF CheckNumberType(parameter0) THEN
  5160. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5161. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5162. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5163. ELSE
  5164. type := system.realType;
  5165. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5166. END
  5167. END;
  5168. result.SetAssignable(parameter0.assignable)
  5169. (* ---- MAX ----- *)
  5170. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5171. IF numberActualParameters = 1 THEN
  5172. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5173. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5174. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5175. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5176. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5177. *)
  5178. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5179. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5180. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5181. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5182. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5183. END;
  5184. ELSE
  5185. Error(parameter0.position,"is not a type symbol");
  5186. END
  5187. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5188. ConvertOperands(parameter0,parameter1);
  5189. actualParameters.SetExpression(0,parameter0);
  5190. actualParameters.SetExpression(1,parameter1);
  5191. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5192. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5193. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5194. END;
  5195. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5196. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5197. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5198. END;
  5199. END;
  5200. type := parameter0.type;
  5201. ELSE type := SyntaxTree.invalidType;
  5202. END;
  5203. (* ---- MIN ----- *)
  5204. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5205. IF numberActualParameters = 1 THEN
  5206. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5207. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5208. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5209. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5210. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5211. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5212. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5213. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5214. END;
  5215. ELSE
  5216. Error(parameter0.position,"is not a type symbol");
  5217. END
  5218. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5219. ConvertOperands(parameter0,parameter1);
  5220. actualParameters.SetExpression(0,parameter0);
  5221. actualParameters.SetExpression(1,parameter1);
  5222. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5223. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5224. ELSE result.SetResolved(parameter1.resolved)
  5225. END;
  5226. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5227. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5228. ELSE result.SetResolved(parameter1.resolved)
  5229. END;
  5230. END;
  5231. type := parameter0.type;
  5232. ELSE type := SyntaxTree.invalidType;
  5233. END;
  5234. (* ---- ODD ----- *)
  5235. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5236. type := system.booleanType;
  5237. IF CheckIntegerType(parameter0) THEN
  5238. IF IsIntegerValue(parameter0,i0) THEN
  5239. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5240. type := system.booleanType;
  5241. END;
  5242. END;
  5243. (* ---- ORD ----- *)
  5244. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5245. IF id = Global.Ord THEN
  5246. type := system.integerType;
  5247. ELSE
  5248. type := system.longintType;
  5249. END;
  5250. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5251. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5252. actualParameters.SetExpression(0,parameter0);
  5253. (* IF CheckCharacterType(parameter0) THEN*)
  5254. IF IsCharacterValue(parameter0,c)THEN
  5255. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5256. type := Global.GetSignedIntegerType(system,ORD(c));
  5257. END;
  5258. ELSE Error(parameter0.position, "incompatible parameter");
  5259. END;
  5260. (* ---- SHORT ----- *)
  5261. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5262. type := type0;
  5263. IF IsSignedIntegerType(type) THEN
  5264. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5265. ELSIF type = system.integerType THEN type := system.shortintType
  5266. ELSIF type = system.longintType THEN type := system.integerType
  5267. ELSIF type = system.hugeintType THEN type:= system.longintType
  5268. ELSE
  5269. CASE type.sizeInBits OF
  5270. 16: type := Global.Integer8
  5271. |32: type := Global.Integer16
  5272. |64: type := Global.Integer32
  5273. END;
  5274. END;
  5275. ELSIF type IS SyntaxTree.FloatType THEN
  5276. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5277. ELSIF type = system.longrealType THEN type := system.realType
  5278. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5279. END;
  5280. ELSIF type IS SyntaxTree.ComplexType THEN
  5281. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5282. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5283. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5284. END;
  5285. ELSE
  5286. Error(parameter0.position,"short not applicable")
  5287. END;
  5288. IF (parameter0.resolved # NIL) THEN
  5289. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5290. IF parameter0 IS SyntaxTree.Value THEN
  5291. result.SetResolved(parameter0(SyntaxTree.Value));
  5292. END;
  5293. END;
  5294. (* ---- LONG ----- *)
  5295. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5296. type := type0;
  5297. IF IsSignedIntegerType(type) THEN
  5298. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5299. ELSIF type = system.longintType THEN type := system.hugeintType
  5300. ELSIF type = system.integerType THEN type := system.longintType
  5301. ELSIF type = system.shortintType THEN type := system.integerType
  5302. ELSE
  5303. CASE type.sizeInBits OF
  5304. 8: type := Global.Integer16
  5305. |16: type := Global.Integer32
  5306. |32: type := Global.Integer64
  5307. END;
  5308. END;
  5309. ELSIF type IS SyntaxTree.FloatType THEN
  5310. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5311. ELSIF type= system.realType THEN type := system.longrealType
  5312. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5313. END;
  5314. ELSIF type IS SyntaxTree.ComplexType THEN
  5315. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5316. ELSIF type = system.complexType THEN type := system.longcomplexType
  5317. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5318. END;
  5319. ELSE
  5320. Error(parameter0.position,"long not applicable")
  5321. END;
  5322. IF (parameter0.resolved # NIL) THEN
  5323. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5324. IF parameter0 IS SyntaxTree.Value THEN
  5325. result.SetResolved(parameter0(SyntaxTree.Value));
  5326. END;
  5327. END;
  5328. (* ---- SIZE OF ----- *)
  5329. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5330. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5331. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5332. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5333. type := system.integerType;
  5334. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5335. ELSE
  5336. (* for variables, system sizeof could represent the physically occupied size
  5337. determined via the type descriptor, implement that ? *)
  5338. Error(parameter0.position,"is not a type symbol");
  5339. END
  5340. (* ---- SYSTEM.TRACE -----*)
  5341. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5342. FOR i := 0 TO numberActualParameters-1 DO
  5343. parameter0 := actualParameters.GetExpression(i);
  5344. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5345. Error(parameter0.position,"incompatible parameter");
  5346. END;
  5347. END;
  5348. (* remaining issues can only be tested in backend *)
  5349. (* ---- ADDRESSOF----- *)
  5350. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5351. IF HasAddress(parameter0) THEN
  5352. type := system.addressType;
  5353. ELSE
  5354. type := SyntaxTree.invalidType;
  5355. Error(parameter0.position,"has no address");
  5356. END;
  5357. (* ---- BIT ----- *)
  5358. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5359. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5360. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5361. actualParameters.SetExpression(0,parameter0);
  5362. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5363. actualParameters.SetExpression(1,parameter1);
  5364. END;
  5365. type := system.booleanType;
  5366. (* ----- MSK ---- *)
  5367. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5368. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5369. ConvertOperands(parameter0,parameter1);
  5370. actualParameters.SetExpression(0,parameter0);
  5371. actualParameters.SetExpression(1,parameter1);
  5372. END;
  5373. type := parameter0.type;
  5374. (* ---- SYSTEM.GET64 ----- *)
  5375. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5376. IF CheckAddressType(parameter0) THEN
  5377. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5378. actualParameters.SetExpression(0,parameter0);
  5379. END;
  5380. type := system.hugeintType;
  5381. (* ---- SYSTEM.GET32 ----- *)
  5382. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5383. IF CheckAddressType(parameter0) THEN
  5384. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5385. actualParameters.SetExpression(0,parameter0);
  5386. END;
  5387. type := system.longintType;
  5388. (* ---- SYSTEM.GET16 ----- *)
  5389. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5390. IF CheckAddressType(parameter0) THEN
  5391. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5392. actualParameters.SetExpression(0,parameter0);
  5393. END;
  5394. type := system.integerType;
  5395. (* ---- SYSTEM.GET8 ----- *)
  5396. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5397. IF CheckAddressType(parameter0) THEN
  5398. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5399. actualParameters.SetExpression(0,parameter0);
  5400. END;
  5401. type := system.shortintType;
  5402. (* ---- SYSTEM.GetStackPointer ----- *)
  5403. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5404. type := system.addressType;
  5405. (* ---- SYSTEM.GetFramePointer ----- *)
  5406. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5407. type := system.addressType;
  5408. (* ---- SYSTEM.GetActivity ----- *)
  5409. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5410. type := system.objectType;
  5411. (* ---- SYSTEM.SetStackPointer ----- *)
  5412. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5413. IF CheckAddressType(parameter0) THEN
  5414. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5415. actualParameters.SetExpression(0,parameter0);
  5416. END;
  5417. (* ---- SYSTEM.SetFramePointer ----- *)
  5418. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5419. IF CheckAddressType(parameter0) THEN
  5420. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5421. actualParameters.SetExpression(0,parameter0);
  5422. END;
  5423. (* ---- SYSTEM.SetActivity ----- *)
  5424. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5425. IF CheckObjectType(parameter0) THEN
  5426. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5427. actualParameters.SetExpression(0,parameter0);
  5428. END;
  5429. (* ---- LSH, LSL, ROT, ROR ----- *)
  5430. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5431. type := type0;
  5432. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5433. actualParameters.SetExpression(1, parameter1);
  5434. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5435. IF id = Global.Lsh THEN
  5436. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5437. ELSIF id = Global.Rot THEN
  5438. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5439. ELSIF id = Global.Ror THEN
  5440. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5441. END;
  5442. END;
  5443. (* ---- SYSTEM.VAL ----- *)
  5444. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5445. IF CheckTypeDeclarationType(parameter0) THEN
  5446. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5447. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5448. result := SyntaxTree.invalidExpression;
  5449. Error(parameter0.position,"is no basic type");
  5450. ELSE
  5451. IF (parameter1.resolved # NIL) THEN
  5452. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5453. IF parameter0 IS SyntaxTree.Value THEN
  5454. result.SetResolved(parameter0(SyntaxTree.Value));
  5455. END;
  5456. END;
  5457. result.SetAssignable(parameter1.assignable);
  5458. END;
  5459. END;
  5460. (* ---- SYSTEM.GET ----- *)
  5461. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5462. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5463. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5464. actualParameters.SetExpression(0,parameter0);
  5465. END;
  5466. (* ---- SYSTEM.PUT ----- *)
  5467. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5468. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5469. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5470. actualParameters.SetExpression(0,parameter0);
  5471. END;
  5472. (* ---- SYSTEM.PUT64 ----- *)
  5473. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5474. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5475. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5476. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. actualParameters.SetExpression(1,parameter1);
  5479. END;
  5480. (* ---- SYSTEM.PUT32 ----- *)
  5481. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5482. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5483. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5484. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5485. actualParameters.SetExpression(0,parameter0);
  5486. actualParameters.SetExpression(1,parameter1);
  5487. END;
  5488. (* ---- SYSTEM.PUT16 ----- *)
  5489. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5490. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5491. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5492. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5493. actualParameters.SetExpression(0,parameter0);
  5494. actualParameters.SetExpression(1,parameter1);
  5495. END;
  5496. (* ---- SYSTEM.PUT8 ----- *)
  5497. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5498. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5499. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5500. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5501. actualParameters.SetExpression(0,parameter0);
  5502. actualParameters.SetExpression(1,parameter1);
  5503. END;
  5504. (* ---- SYSTEM.MOVE ----- *)
  5505. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5506. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5507. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5508. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5509. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5510. actualParameters.SetExpression(0,parameter0);
  5511. actualParameters.SetExpression(1,parameter1);
  5512. actualParameters.SetExpression(2,parameter2);
  5513. END;
  5514. (* ---- SYSTEM.NEW ----- *)
  5515. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5516. IF ~IsPointerType(parameter0.type) THEN
  5517. Error(parameter0.position,"is not a pointer")
  5518. ELSIF CheckSizeType(parameter1) THEN
  5519. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5520. actualParameters.SetExpression(1,parameter1);
  5521. END;
  5522. (* ----SYSTEM.REF ---- *)
  5523. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5524. type := system.addressType
  5525. (* ---- INCR ----- *)
  5526. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5527. type := system.sizeType;
  5528. base := type0;
  5529. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5530. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5531. IF i1 < 0 THEN
  5532. Error(position, "invalid dimension");
  5533. base := SyntaxTree.invalidType;
  5534. ELSE
  5535. base := ArrayBase(base,i1);
  5536. IF (base # NIL) & Indexable(base) THEN
  5537. ELSE
  5538. Error(position, "len on no array");
  5539. IF VerboseErrorMessage THEN
  5540. Printout.Info("base",base);
  5541. END;
  5542. base := SyntaxTree.invalidType;
  5543. END;
  5544. END;
  5545. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5546. actualParameters.SetExpression(1,parameter1);
  5547. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5548. mathArrayType := base(SyntaxTree.MathArrayType);
  5549. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5550. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5551. type := system.longintType;
  5552. END;
  5553. END;
  5554. ELSE
  5555. type := system.longintType;
  5556. END;
  5557. (* ---- SUM ----- *)
  5558. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5559. Error(position, "sum operator not applicable");
  5560. (* ---- ALL ----- *)
  5561. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5562. Error(position, "all operator not applicable");
  5563. (* ---- DIM ----- *)
  5564. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5565. type := system.sizeType;
  5566. IF type0 IS SyntaxTree.MathArrayType THEN
  5567. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5568. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5569. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5570. END;
  5571. ELSE
  5572. Error(position, "dimension on non math array type");
  5573. END;
  5574. (* ---- CAS ----- *)
  5575. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5576. IF type0.IsComposite () THEN
  5577. Error(position, "first parameter of composite type");
  5578. result := SyntaxTree.invalidExpression;
  5579. ELSIF ~IsVariable (parameter0) THEN
  5580. Error(position, "first parameter not assignable");
  5581. result := SyntaxTree.invalidExpression;
  5582. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5583. Error(position, "second parameter incompatible");
  5584. result := SyntaxTree.invalidExpression;
  5585. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5586. Error(position, "third parameter incompatible");
  5587. result := SyntaxTree.invalidExpression;
  5588. ELSE
  5589. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5590. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5591. type := type0;
  5592. END;
  5593. (* ---- RESHAPE ----- *)
  5594. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5595. IF type0 IS SyntaxTree.MathArrayType THEN
  5596. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5597. base := ArrayBase(type0,MAX(LONGINT));
  5598. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5599. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5600. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5601. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5602. IF ~CompatibleTo(system,type0,parameterType) THEN
  5603. Error(parameter0.position,"incompatible parameter in reshape");
  5604. result := SyntaxTree.invalidExpression;
  5605. ELSE
  5606. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5607. END;
  5608. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5609. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5610. IF ~CompatibleTo(system,type1,parameterType) THEN
  5611. Error(parameter1.position,"parameter incompatible to math array of longint");
  5612. result := SyntaxTree.invalidExpression;
  5613. ELSE
  5614. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5615. END;
  5616. ELSE
  5617. Error(position,"reshape on non math array type");
  5618. result := SyntaxTree.invalidExpression;
  5619. END;
  5620. (* ---- SYSTEM.TYPECODE ----- *)
  5621. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5622. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5623. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5624. type := type.resolved;
  5625. IF type IS SyntaxTree.PointerType THEN
  5626. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5627. END;
  5628. IF ~(type IS SyntaxTree.RecordType) THEN
  5629. Error(parameter0.position,"must be type with type descriptor");
  5630. END;
  5631. ELSE
  5632. Error(parameter0.position,"is not a type symbol");
  5633. END;
  5634. type := system.addressType;
  5635. (* -------- FLT --------- *)
  5636. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5637. type := system.realType;
  5638. IF IsRealValue(parameter0, r) THEN
  5639. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5640. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5641. i0 := i; i := ABS(i);
  5642. IF i # 0 THEN
  5643. i1 := 23;
  5644. IF i >= 2*800000H THEN
  5645. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5646. ELSIF i < 800000H THEN
  5647. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5648. END;
  5649. i := (i1 + 127)*800000H - 800000H + i;
  5650. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5651. END;
  5652. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5653. END;
  5654. (* ------- CONNECT -------*)
  5655. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5656. (*IF ~(currentIsCellNet) THEN
  5657. Error(position, "connection outside activeCells body block");
  5658. END;*)
  5659. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5660. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5661. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5662. END;
  5663. IF numberActualParameters = 3 THEN
  5664. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5665. Error(position, "incompatible channel size parameter");
  5666. END;
  5667. *)
  5668. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5669. actualParameters.SetExpression(2,parameter2);
  5670. END;
  5671. activeCellsStatement := TRUE;
  5672. (* ---------- DELEGATE --------*)
  5673. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5674. (*
  5675. IF ~(currentIsCellNet) THEN
  5676. Error(position, "connection delegation outside activeCells body block");
  5677. END;
  5678. *)
  5679. IF ~CheckPortType(parameter1, inPort) THEN
  5680. Error(parameter0.position,"not a port")
  5681. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5682. Error(parameter1.position,"not a port")
  5683. ELSIF (outPort.direction # inPort.direction) THEN
  5684. Error(parameter0.position,"invalid port direction");
  5685. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5686. Error(position, "incompatible port sizes");
  5687. END;
  5688. activeCellsStatement := TRUE;
  5689. (* --------- RECEIVE ---------*)
  5690. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5691. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5692. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5693. IF inPort.direction # SyntaxTree.InPort THEN
  5694. Error(parameter0.position,"not an in-port")
  5695. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5696. Error(parameter1.position,"incompatible to port type");
  5697. END;
  5698. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5699. IF ~SameType(parameter2.type, system.integerType) THEN
  5700. Error(parameter2.position,"incompatible to integer type");
  5701. END;
  5702. END;
  5703. END;
  5704. (* --------- SEND ---------*)
  5705. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5706. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5707. IF CheckPortType(parameter0,outPort) THEN
  5708. IF outPort.direction # SyntaxTree.OutPort THEN
  5709. Error(parameter1.position,"not an out-port")
  5710. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5711. Error(parameter1.position,"incompatible to port type");
  5712. ELSE
  5713. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5714. actualParameters.SetExpression(1,parameter1);
  5715. END;
  5716. END;
  5717. (* ------- custom builtins ----- *)
  5718. ELSIF id = Global.systemSpecial THEN
  5719. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5720. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5721. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5722. type := procedureType.returnType;
  5723. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5724. (* go through all formal parameters *)
  5725. formalParameter := procedureType.firstParameter;
  5726. FOR i := 0 TO actualParameters.Length() - 1 DO
  5727. actualParameter := actualParameters.GetExpression(i);
  5728. IF actualParameter = SyntaxTree.invalidExpression THEN
  5729. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5730. Error(position, "incompatible parameter")
  5731. ELSE
  5732. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5733. END;
  5734. actualParameters.SetExpression(i, actualParameter);
  5735. formalParameter := formalParameter.nextParameter
  5736. END
  5737. END
  5738. ELSE
  5739. Error(position, "builtin not implemented");
  5740. result := SyntaxTree.invalidExpression;
  5741. END;
  5742. END;
  5743. IF result # SyntaxTree.invalidExpression THEN
  5744. type := ResolveType(type);
  5745. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5746. result.SetType(type);
  5747. END;
  5748. RETURN result
  5749. END NewBuiltinCallDesignator;
  5750. (** return type guard designator left(type)
  5751. - check if type can be extended (i.e. is no static record)
  5752. - check if type is a type extension of left.type
  5753. - returns new type guard designator
  5754. returns invalidDesignator = invalidExpression if error
  5755. **)
  5756. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5757. VAR result: SyntaxTree.Designator;
  5758. BEGIN
  5759. result := SyntaxTree.invalidDesignator;
  5760. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5761. Error(position, "no type extension of type");
  5762. IF VerboseErrorMessage THEN
  5763. Printout.Info("left",left);
  5764. Printout.Info("type",type);
  5765. END;
  5766. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5767. Error(position, "variable cannot be extended");
  5768. ELSIF IsUnsafePointer(left.type) THEN
  5769. Error(position, "forbidden type guard on unsafe pointer");
  5770. ELSE
  5771. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5772. result.SetType(type);
  5773. result.SetAssignable(left.assignable);
  5774. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5775. END;
  5776. RETURN result
  5777. END NewTypeGuardDesignator;
  5778. (** check and resolve parameter designator left(expression list)
  5779. - check expression list
  5780. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5781. - elsif left is a procedure type then
  5782. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5783. - else return is a procedure call then return ProcedureCallDesignator
  5784. returns invalidDesignator = invalidExpression if error
  5785. **)
  5786. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5787. VAR
  5788. parameters: SyntaxTree.ExpressionList;
  5789. left: SyntaxTree.Designator;
  5790. result,expression: SyntaxTree.Expression;
  5791. typeDeclaration: SyntaxTree.TypeDeclaration;
  5792. type, expressionType: SyntaxTree.Type;
  5793. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5794. BEGIN
  5795. type := type.resolved;
  5796. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5797. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5798. END;
  5799. RETURN type
  5800. END BaseType;
  5801. BEGIN
  5802. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5803. result := SyntaxTree.invalidDesignator;
  5804. left := ResolveDesignator(designator.left);
  5805. IF left # SyntaxTree.invalidDesignator THEN
  5806. parameters := designator.parameters;
  5807. IF ExpressionList(parameters) THEN
  5808. IF (left.type = NIL) THEN
  5809. Error(left.position,"object is not a procedure or cannot be extended");
  5810. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5811. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5812. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5813. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5814. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5815. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5816. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5817. ELSE
  5818. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5819. END
  5820. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5821. expression := parameters.GetExpression(0);
  5822. type := typeDeclaration.declaredType.resolved;
  5823. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5824. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5825. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5826. OR (expressionType IS SyntaxTree.EnumerationType)
  5827. ) THEN
  5828. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5829. ELSE
  5830. Error(left.position,"invalid type in explicit conversion");
  5831. END;
  5832. ELSE
  5833. Error(left.position,"called object is not a procedure or cannot be extended");
  5834. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5835. result := SyntaxTree.invalidDesignator;
  5836. END;
  5837. ELSE
  5838. result := SyntaxTree.invalidDesignator
  5839. END;
  5840. END;
  5841. resolvedExpression := result;
  5842. END VisitParameterDesignator;
  5843. (** check dereference designator left^
  5844. - check if left is pointer type or left is object type
  5845. - return new dereference designator with type = left.baseType.type (if appropriate)
  5846. with error handling
  5847. returns invalidDesignator = invalidExpression if error
  5848. **)
  5849. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5850. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5851. BEGIN
  5852. result := SyntaxTree.invalidDesignator;
  5853. type := left.type;
  5854. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5855. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5856. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5857. result.SetAssignable(TRUE);
  5858. result.SetType(type);
  5859. result.SetHidden(left.isHidden);
  5860. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5861. type := type.resolved;
  5862. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5863. result.SetAssignable(TRUE);
  5864. result.SetType(type);
  5865. result.SetHidden(left.isHidden);
  5866. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5867. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5868. result.SetAssignable(TRUE);
  5869. result.SetType(type);
  5870. result.SetHidden(left.isHidden);
  5871. ELSE
  5872. Error(position, "dereference on no pointer");
  5873. IF VerboseErrorMessage THEN
  5874. Printout.Info("pointer", type);
  5875. Printout.Info("scope", currentScope);
  5876. END;
  5877. END;
  5878. RETURN result
  5879. END NewDereferenceDesignator;
  5880. (** check supercall designator left^
  5881. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5882. - return new supercall designator with type = left.type
  5883. with error handling
  5884. **)
  5885. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5886. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5887. objectScope: SyntaxTree.Scope;
  5888. BEGIN
  5889. result := SyntaxTree.invalidDesignator;
  5890. IF left = SyntaxTree.invalidDesignator THEN
  5891. (* error already handled *)
  5892. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5893. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5894. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5895. IF symbol IS SyntaxTree.Procedure THEN
  5896. procedure := symbol(SyntaxTree.Procedure);
  5897. objectScope := currentScope;
  5898. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5899. objectScope := objectScope.outerScope;
  5900. END;
  5901. IF (left.left = NIL) OR ~
  5902. (
  5903. (left.left IS SyntaxTree.SelfDesignator) OR
  5904. (left.left IS SyntaxTree.DereferenceDesignator)
  5905. & (left.left(SyntaxTree.Designator).left # NIL)
  5906. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5907. Error(position, "procedure not in immediate object scope");
  5908. IF VerboseErrorMessage THEN
  5909. Printout.Info("left.left",left.left);
  5910. END;
  5911. ELSIF procedure.super # NIL THEN
  5912. result := SyntaxTree.NewSupercallDesignator(position,left);
  5913. result.SetType(left.type.resolved)
  5914. ELSE
  5915. Error(position, "no supermethod for this procedure");
  5916. END;
  5917. ELSE
  5918. Error(position, "symbol is not a procedure");
  5919. END;
  5920. ELSE
  5921. Error(position, "is no symbol designator");
  5922. END;
  5923. RETURN result
  5924. END NewSupercallDesignator;
  5925. (** check and semantically resolve arrow designator left^
  5926. - if left is procedure type -> result := SupercallDesignator
  5927. - else result := DereferenceDesignator
  5928. returns result via global variable resolvedExpression
  5929. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5930. **)
  5931. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5932. VAR left: SyntaxTree.Designator;
  5933. BEGIN
  5934. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5935. left := ResolveDesignator(arrowDesignator.left);
  5936. IF left # NIL THEN
  5937. IF (left.type = NIL) THEN
  5938. Error(arrowDesignator.position,"Invalid arrow designator");
  5939. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5940. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5941. ELSE
  5942. IF IsPointerToObject(left.type) THEN
  5943. Warning(arrowDesignator.position, "forbidden dereference on object");
  5944. END;
  5945. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5946. END
  5947. END
  5948. END VisitArrowDesignator;
  5949. (** check and return expression
  5950. - if expression has no type then resolve expression
  5951. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5952. - return result
  5953. **)
  5954. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5955. VAR result,prev: SyntaxTree.Expression;
  5956. BEGIN
  5957. IF expression = NIL THEN result := NIL
  5958. ELSIF (expression.type = NIL) THEN
  5959. prev := resolvedExpression;
  5960. resolvedExpression := SyntaxTree.invalidExpression;
  5961. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5962. expression.SetType(SyntaxTree.invalidType);
  5963. END;
  5964. expression.Accept(SELF);
  5965. result := resolvedExpression;
  5966. IF currentIsRealtime THEN
  5967. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5968. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  5969. END;
  5970. END;
  5971. (* designator modifiers for backends if they support it ...*)
  5972. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5973. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5974. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5975. END;
  5976. resolvedExpression := prev
  5977. ELSE
  5978. result := expression
  5979. END;
  5980. RETURN result
  5981. END ResolveExpression;
  5982. (**
  5983. check expression to be constant expression
  5984. - resolve expression
  5985. - if valid then check that of value type
  5986. report error and return invalidExpression if anything fails
  5987. **)
  5988. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5989. VAR position: Position;
  5990. BEGIN
  5991. position := expression.position;
  5992. expression := ResolveExpression(expression);
  5993. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5994. ELSIF (expression.resolved = NIL) THEN
  5995. Error(position, "expression is not constant");
  5996. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5997. expression := SyntaxTree.invalidExpression;
  5998. END;
  5999. RETURN expression
  6000. END ConstantExpression;
  6001. (** check expression to be constant integer
  6002. - resolve expresssion
  6003. - if valid then check that of integer value type
  6004. report error and return invalidExpression if anything fails
  6005. **)
  6006. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6007. VAR position: Position;
  6008. BEGIN
  6009. position := expression.position;
  6010. expression := ResolveExpression(expression);
  6011. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6012. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6013. expression := SyntaxTree.invalidExpression;
  6014. Error(position, "expression is not a constant integer");
  6015. END;
  6016. RETURN expression
  6017. END ConstantInteger;
  6018. (** check expression as positive (>=0) constant integer
  6019. - resolve expression
  6020. - if valid then check that integer value
  6021. - if integer value then check that value >= 0
  6022. report error and return invalidExpression if anything fails
  6023. **)
  6024. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6025. VAR position: Position;
  6026. BEGIN
  6027. position := expression.position;
  6028. expression := ConstantExpression(expression);
  6029. IF expression = SyntaxTree.invalidExpression THEN
  6030. (* error already reported *)
  6031. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6032. Error(position, "expression is not integer valued");
  6033. expression := SyntaxTree.invalidExpression
  6034. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6035. Error(position, "integer is not greater or equal zero");
  6036. END;
  6037. RETURN expression
  6038. END ConstantIntegerGeq0;
  6039. (** check expression as condition
  6040. - resolve expression
  6041. - if valid expression then check that result type is boolean
  6042. report error and return invalidExpression if anything fails
  6043. **)
  6044. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6045. VAR position: Position;
  6046. BEGIN
  6047. position := expression.position;
  6048. expression := ResolveExpression(expression);
  6049. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6050. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6051. expression := SyntaxTree.invalidExpression;
  6052. Error(position, "expression is not boolean");
  6053. END;
  6054. RETURN expression
  6055. END ResolveCondition;
  6056. (*** symbols ***)
  6057. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6058. BEGIN
  6059. x.Accept(SELF);
  6060. END ResolveSymbol;
  6061. (** check a symbol
  6062. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6063. **)
  6064. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6065. VAR scope: SyntaxTree.Scope;
  6066. BEGIN
  6067. (* visibility *)
  6068. scope := symbol.scope;
  6069. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6070. scope := scope.outerScope;
  6071. END;
  6072. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6073. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6074. Error(symbol.position,"cannot be exported");
  6075. IF VerboseErrorMessage THEN
  6076. Printout.Info("symbol",symbol);
  6077. END;
  6078. END;
  6079. END;
  6080. END CheckSymbolVisibility;
  6081. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6082. If node is currently being resolved then emit a cyclic definition error.
  6083. Return TRUE only if node is fully resolved.
  6084. **)
  6085. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6086. VAR result: BOOLEAN;
  6087. BEGIN
  6088. IF SyntaxTree.Resolved IN x.state THEN
  6089. result := FALSE
  6090. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6091. Error(x.position,"cyclic definition");
  6092. result := FALSE;
  6093. ELSE
  6094. result := TRUE;
  6095. x.SetState(SyntaxTree.BeingResolved)
  6096. END;
  6097. RETURN result
  6098. END SymbolNeedsResolution;
  6099. (** check and resolve a type declaration symbol = Type
  6100. - set type to declaration type
  6101. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6102. This is so because the type declaration itself does not have a type but it only stands for a type.
  6103. In the implementation of the compiler this made a lot much easier.
  6104. - resolve and set declared type
  6105. - check symbol
  6106. **)
  6107. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6108. VAR prevScope: SyntaxTree.Scope;
  6109. BEGIN
  6110. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6111. IF SymbolNeedsResolution(typeDeclaration) THEN
  6112. prevScope := currentScope;
  6113. currentScope := typeDeclaration.scope;
  6114. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6115. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6116. CheckSymbolVisibility(typeDeclaration);
  6117. typeDeclaration.SetState(SyntaxTree.Resolved);
  6118. currentScope := prevScope;
  6119. END;
  6120. END VisitTypeDeclaration;
  6121. (** check and resolve a constant declaration symbol = (constant) expression
  6122. - check expression
  6123. - set type and value
  6124. - check symbol
  6125. **)
  6126. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6127. VAR
  6128. expression: SyntaxTree.Expression;
  6129. type: SyntaxTree.Type;
  6130. name: Basic.SegmentedName;
  6131. replacement: Replacement;
  6132. BEGIN
  6133. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6134. IF SymbolNeedsResolution(constant) THEN
  6135. expression := constant.value;
  6136. IF replacements # NIL THEN
  6137. Global.GetSymbolSegmentedName(constant, name);
  6138. replacement := replacements;
  6139. WHILE (replacement # NIL) & (replacement.name # name) DO
  6140. replacement := replacement.next;
  6141. END;
  6142. IF replacement # NIL THEN
  6143. InfoSS(constant.position, "replacing constant", constant.name);
  6144. (*
  6145. NEW(stringReader, Strings.Length(replacement.string^));
  6146. stringReader.Set(replacement.string^);
  6147. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6148. NEW(parser, scanner, diagnostics);
  6149. expression := parser.Expression();
  6150. *)
  6151. expression := replacement.expression;
  6152. replacement.used := TRUE;
  6153. END;
  6154. END;
  6155. constant.SetType(SyntaxTree.invalidType);
  6156. expression := ConstantExpression(expression);
  6157. ASSERT(expression.type # NIL);
  6158. type := expression.type.resolved;
  6159. constant.SetType(type);
  6160. constant.SetValue(expression);
  6161. CheckSymbolVisibility(constant);
  6162. constant.SetState(SyntaxTree.Resolved);
  6163. END;
  6164. END VisitConstant;
  6165. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6166. VAR procedureAlignment: LONGINT;
  6167. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6168. (* least common multiple *)
  6169. VAR a,b: LONGINT;
  6170. BEGIN
  6171. a := a0; b := b0;
  6172. WHILE (a # b) DO
  6173. IF a < b THEN a := a+a0
  6174. ELSE b := b + b0
  6175. END;
  6176. END;
  6177. RETURN a
  6178. END LCM;
  6179. BEGIN
  6180. IF alignment > 1 THEN
  6181. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6182. IF (procedureAlignment > 1) THEN
  6183. alignment := LCM(alignment, procedureAlignment);
  6184. END;
  6185. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6186. END;
  6187. END AdaptStackAlignment;
  6188. (** check and resolve a variable / field
  6189. - check and set type
  6190. - negative check on open array type
  6191. - check symbol
  6192. **)
  6193. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6194. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6195. BEGIN
  6196. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6197. IF SymbolNeedsResolution(variable) THEN
  6198. modifiers := variable.modifiers;
  6199. (*
  6200. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6201. variable.AddFlags(flags);
  6202. *)
  6203. variable.SetType(ResolveType(variable.type));
  6204. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6205. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6206. Error(variable.position,"forbidden open array variable");
  6207. END;
  6208. END;
  6209. CheckSymbolVisibility(variable);
  6210. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6211. variable.SetUntraced(TRUE);
  6212. IF ~ContainsPointer(variable.type) THEN
  6213. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6214. Error(position, "untraced flag on non-pointer variable");
  6215. END;
  6216. END;
  6217. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6218. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6219. IF ~PowerOf2(value) THEN
  6220. Error(position, "forbidden alignment - must be power of two");
  6221. ELSE
  6222. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6223. END;
  6224. END;
  6225. variable.SetAlignment(FALSE,value);
  6226. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6227. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6228. Error(position, "fixed position not possible in procedure");
  6229. END;
  6230. variable.SetAlignment(TRUE, value);
  6231. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6232. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6233. Error(position,"fictive offset not possible in procedure");
  6234. END;
  6235. variable.SetFictive(value);
  6236. variable.SetOffset(value*system.dataUnit);
  6237. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6238. END;
  6239. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6240. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6241. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6242. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6243. END;
  6244. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6245. IF variable.initializer # NIL THEN
  6246. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6247. END;
  6248. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6249. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6250. pointerType.SetPointerBase(variable.type);
  6251. pointerType.SetHidden(TRUE);
  6252. variable.SetType(ResolveType(pointerType));
  6253. END;
  6254. variable.SetState(SyntaxTree.Resolved);
  6255. END;
  6256. END VisitVariable;
  6257. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6258. BEGIN
  6259. VisitVariable(property)
  6260. END VisitProperty;
  6261. (** check and resolve a (procedure) parameter
  6262. - check and set type
  6263. - check symbol
  6264. - check parameter kind and set read-only flags if appropriate
  6265. **)
  6266. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6267. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6268. BEGIN
  6269. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6270. IF SymbolNeedsResolution(parameter) THEN
  6271. modifiers := parameter.modifiers;
  6272. parameter.SetType(ResolveType(parameter.type));
  6273. ASSERT(parameter.type.resolved # NIL);
  6274. CheckSymbolVisibility(parameter);
  6275. IF parameter.defaultValue # NIL THEN
  6276. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6277. Error(parameter.position,"forbidden default value on non-value parameter");
  6278. ELSE
  6279. expression := ConstantExpression(parameter.defaultValue);
  6280. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6281. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6282. parameter.SetDefaultValue(expression);
  6283. END;
  6284. END;
  6285. END;
  6286. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6287. parameter.SetUntraced(TRUE);
  6288. IF ~ContainsPointer(parameter.type) THEN
  6289. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6290. Error(position, "untraced flag on non-pointer variable");
  6291. END;
  6292. END;
  6293. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6294. parameter.SetMoveable(TRUE);
  6295. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6296. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6297. Error(position, "illegal movable flag on non-address variable");
  6298. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6299. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6300. Error(position, "unnecessary movable flag on variable variable");
  6301. END;
  6302. END;
  6303. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6304. parameter.SetState(SyntaxTree.Resolved);
  6305. END;
  6306. END VisitParameter;
  6307. (** check and resolve a procedure (with declaration and implementation scope)
  6308. - check the procedure type
  6309. - check if method (i.e. in record scope), if so then
  6310. - check if (unique) constructor
  6311. - check if (unique) finalizer
  6312. - check if super method available, if so then check signature
  6313. - of not in record scope then negative check on constructor flag
  6314. - of not in record scope then negative check on finalizer flag
  6315. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6316. - check procedure symbol
  6317. **)
  6318. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6319. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6320. procedureType: SyntaxTree.ProcedureType;
  6321. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6322. qualifiedType: SyntaxTree.QualifiedType;
  6323. value: LONGINT;
  6324. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6325. position: Position;
  6326. BEGIN
  6327. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6328. IF IsOberonInline(procedure) THEN
  6329. IF SyntaxTree.Public * procedure.access # {} THEN
  6330. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6331. END;
  6332. procedure.SetInline(FALSE);
  6333. procedure.SetOberonInline(TRUE);
  6334. END;
  6335. IF SymbolNeedsResolution(procedure) THEN
  6336. recentIsRealtime := currentIsRealtime;
  6337. recentIsBodyProcedure := currentIsBodyProcedure;
  6338. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6339. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6340. modifiers := procedureType.modifiers;
  6341. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6342. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6343. IF useDarwinCCalls THEN (*fld*)
  6344. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6345. ELSE
  6346. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6347. END
  6348. END;
  6349. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6350. procedureType.SetInterrupt(TRUE);
  6351. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6352. END;
  6353. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6354. procedureType.SetNoReturn(TRUE);
  6355. END;
  6356. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6357. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6358. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6359. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6360. END;
  6361. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6362. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6363. END;
  6364. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6365. IF ~PowerOf2(value) THEN
  6366. Error(position, "forbidden stack alignment - must be power of two");
  6367. ELSE
  6368. procedureType.SetStackAlignment(value)
  6369. END;
  6370. END;
  6371. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6372. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6373. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6374. END;
  6375. CheckModifiers(modifiers, TRUE);
  6376. modifiers := procedureType.returnTypeModifiers;
  6377. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6378. CheckModifiers(modifiers, TRUE);
  6379. FixProcedureType(procedureType);
  6380. currentIsRealtime := procedureType.isRealtime;
  6381. currentIsBodyProcedure := procedure.isBodyProcedure;
  6382. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6383. THEN
  6384. Error(procedure.position,"problems during parameter offset computation");
  6385. END;
  6386. CheckSymbolVisibility(procedure);
  6387. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6388. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6389. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6390. Error(procedure.position,"problem during parameter offset generation");
  6391. END;
  6392. END;
  6393. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6394. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6395. procedureType.SetDelegate(TRUE);
  6396. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6397. (* add auto-self *)
  6398. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6399. IF (record.pointerType.typeDeclaration = NIL) THEN
  6400. selfParameter.SetType(record.pointerType);
  6401. ELSE
  6402. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6403. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6404. qualifiedType.SetResolved(record.pointerType);
  6405. selfParameter.SetType(qualifiedType);
  6406. END;
  6407. selfParameter.SetAccess(SyntaxTree.Hidden);
  6408. END;
  6409. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6410. IF procedure.isConstructor THEN
  6411. (*! constructor is always visible, compatibility to paco
  6412. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6413. *)
  6414. procedure.MarkUsed;
  6415. IF procedureType.returnType # NIL THEN
  6416. Error(procedure.position,"constructor with forbidden return type");
  6417. END;
  6418. proc := procedure.scope.firstProcedure;
  6419. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6420. proc := proc.nextProcedure;
  6421. END;
  6422. IF proc # NIL THEN
  6423. Error(procedure.position,"duplicate constructor")
  6424. ELSE
  6425. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6426. END;
  6427. END;
  6428. IF procedure.isFinalizer THEN
  6429. procedure.MarkUsed;
  6430. IF procedureType.returnType # NIL THEN
  6431. Error(procedure.position,"finalizer with forbidden return type");
  6432. END;
  6433. IF procedureType.numberParameters # 0 THEN
  6434. Error(procedure.position,"finalizer with formal parameters");
  6435. END;
  6436. proc := procedure.scope.firstProcedure;
  6437. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6438. proc := proc.nextProcedure;
  6439. END;
  6440. IF proc # NIL THEN
  6441. Error(procedure.position,"duplicate finalizer")
  6442. ELSE
  6443. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6444. END;
  6445. END;
  6446. super := FindSuperProcedure(record.recordScope, procedure);
  6447. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6448. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6449. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6450. END;
  6451. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6452. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6453. END;
  6454. IF super.isFinal THEN
  6455. Error(procedure.position,"forbidden method extending final method");
  6456. END;
  6457. procedure.SetSuper(super);
  6458. super.SetOverwritten(TRUE);
  6459. procedure.SetAccess(procedure.access+super.access);
  6460. procedure.MarkUsed;
  6461. END;
  6462. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6463. THEN
  6464. Error(procedure.position,"problems during parameter offset computation");
  6465. END;
  6466. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6467. IF cellsAreObjects THEN
  6468. procedureType.SetDelegate(TRUE);
  6469. END;
  6470. IF procedure.isConstructor THEN
  6471. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6472. END;
  6473. ELSIF procedure.isConstructor THEN
  6474. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6475. END;
  6476. Declarations(procedure.procedureScope, FALSE);
  6477. (* body resolution part done as late fix of the procedure type *)
  6478. procedure.SetState(SyntaxTree.Resolved);
  6479. currentIsRealtime := recentIsRealtime;
  6480. currentIsBodyProcedure := recentIsBodyProcedure;
  6481. END;
  6482. END VisitProcedure;
  6483. (**
  6484. a builtin procedure is a global item that may not be modified locally
  6485. instead the resolving of builtin procedure calls are done in the esignator
  6486. **)
  6487. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6488. VAR type: SyntaxTree.Type;
  6489. BEGIN
  6490. type := ResolveType(builtinProcedure.type);
  6491. END VisitBuiltin;
  6492. (* nopov *)
  6493. (** check and resolve operator
  6494. - operators are first checked as procedures
  6495. - then additional operator-specific checks are done
  6496. - note that only module-scope operators are checked here
  6497. (operators in a record scope are only allowed in the context of
  6498. array-structured object types and checked in 'ResolveArrayStructure')
  6499. - also note that inter-operator conformity is not checked here
  6500. **)
  6501. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6502. VAR
  6503. procedureType: SyntaxTree.ProcedureType;
  6504. leftType, rightType: SyntaxTree.Type;
  6505. identifierNumber: LONGINT; position: Position;
  6506. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6507. modifiers: SyntaxTree.Modifier;
  6508. (** whether a type is locally defined in the current module scope
  6509. for arrays, the base type must be locally defined **)
  6510. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6511. BEGIN
  6512. IF type = NIL THEN
  6513. RETURN FALSE
  6514. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6515. RETURN TRUE
  6516. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6517. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6518. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6519. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6520. ELSE
  6521. RETURN FALSE
  6522. END
  6523. END IsLocallyDefined;
  6524. BEGIN
  6525. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6526. procedureType := operator.type(SyntaxTree.ProcedureType);
  6527. modifiers := procedureType.modifiers;
  6528. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6529. CheckModifiers(modifiers, TRUE);
  6530. VisitProcedure(operator);
  6531. IF operator.scope IS SyntaxTree.RecordScope THEN
  6532. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6533. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6534. IF identifierNumber = -1 THEN
  6535. Error(operator.position, "operator with unknown identifier")
  6536. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6537. Error(operator.position, "identifier may not be used for operator")
  6538. ELSE
  6539. IF procedureType.numberParameters < 1 THEN
  6540. Error(operator.position, "operator without operand");
  6541. ELSIF procedureType.numberParameters > 2 THEN
  6542. Error(operator.position, "operator with more than two operands");
  6543. ELSE
  6544. (* determine operand types *)
  6545. leftType := procedureType.firstParameter.type;
  6546. IF procedureType.numberParameters > 1 THEN
  6547. rightType := procedureType.firstParameter.nextParameter.type
  6548. ELSE
  6549. rightType := NIL
  6550. END;
  6551. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6552. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6553. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6554. Error(operator.position, "none of the operands is declared in the same module")
  6555. END
  6556. END;
  6557. (* TODO: refine the checks, think about how restrictive the checks should be
  6558. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6559. They might be used for intersection, union, complement of custom object types *)
  6560. (* defaults *)
  6561. hasReturnType := TRUE;
  6562. mustBeUnary := FALSE;
  6563. mustBeBinary := FALSE;
  6564. mustReturnBoolean := FALSE;
  6565. mustReturnInteger := FALSE;
  6566. mustHaveEquitypedOperands := FALSE;
  6567. (* operator-specific exceptions *)
  6568. CASE identifierNumber OF
  6569. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6570. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6571. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6572. mustBeBinary := TRUE
  6573. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6574. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6575. | Scanner.Times: mustBeBinary := TRUE
  6576. | Scanner.TimesTimes: mustBeBinary := TRUE
  6577. | Scanner.DotTimes: mustBeBinary := TRUE
  6578. | Scanner.PlusTimes: mustBeBinary := TRUE
  6579. | Scanner.Slash: mustBeBinary := TRUE
  6580. | Scanner.Backslash: mustBeBinary := TRUE
  6581. | Scanner.DotSlash: mustBeBinary := TRUE
  6582. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6583. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6584. | Scanner.Not: mustBeUnary := TRUE
  6585. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6586. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6587. | Scanner.Transpose: mustBeUnary := TRUE;
  6588. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6589. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6590. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6591. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6592. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6593. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6594. | Global.Abs: mustBeUnary := TRUE;
  6595. | Global.Ash: (* TODO: arity? *)
  6596. | Global.Cap: (* TODO: arity? *)
  6597. | Global.Chr: mustBeUnary := TRUE;
  6598. | Global.Entier: (* TODO: arity? *)
  6599. | Global.EntierH: (* TODO: arity? *)
  6600. | Global.Len: (* unary and binary *)
  6601. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6602. | Global.Max, Global.Min: (* unary and binary *)
  6603. | Global.Odd: (* TODO: arity? *)
  6604. | Global.Sum: (* TODO: arity? *)
  6605. | Global.All: (* TODO: arity? *)
  6606. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6607. | Scanner.Alias:
  6608. | Scanner.GreaterGreater, Scanner.LessLess:
  6609. mustBeBinary := TRUE; hasReturnType := FALSE;
  6610. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6611. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6612. END;
  6613. (* check parameter count *)
  6614. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6615. Error(operator.position,"operator is not unary")
  6616. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6617. Error(operator.position,"operator is not binary")
  6618. END;
  6619. (* check parameter types *)
  6620. (* TODO: is this used at all? *)
  6621. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6622. leftType := procedureType.firstParameter.type;
  6623. rightType := procedureType.firstParameter.nextParameter.type;
  6624. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6625. Error(operator.position, "the two operands are not of the same type")
  6626. END
  6627. END;
  6628. (* check return type *)
  6629. IF hasReturnType THEN
  6630. IF procedureType.returnType = NIL THEN
  6631. Error(operator.position, "return type required")
  6632. ELSIF mustReturnBoolean THEN
  6633. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6634. Error(operator.position,"return type is not Boolean")
  6635. END
  6636. ELSIF mustReturnInteger THEN
  6637. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6638. Error(operator.position,"return type is not integer")
  6639. END
  6640. END
  6641. ELSIF procedureType.returnType # NIL THEN
  6642. Error(operator.position, "return type not allowed")
  6643. END
  6644. END
  6645. END
  6646. END
  6647. END VisitOperator;
  6648. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6649. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6650. BEGIN
  6651. IF error THEN RETURN FALSE END;
  6652. prevScope := currentScope;
  6653. prevDiagnostics := diagnostics;
  6654. diagnostics := NIL; (* suppress error output *)
  6655. currentScope := module.moduleScope;
  6656. VisitImport(x);
  6657. IF ~error THEN
  6658. module.moduleScope.AddImport(x);
  6659. x.SetScope(module.moduleScope);
  6660. END;
  6661. currentScope := prevScope;
  6662. diagnostics := prevDiagnostics;
  6663. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6664. END AddImport;
  6665. (** check and resolve import
  6666. - check for name = SYSTEM
  6667. - check for forbidden self import
  6668. - search through global import cache: already imported?
  6669. - check if already imported indirectly
  6670. - import if necessary -> set module and enter into import cache
  6671. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6672. - after this import this direct import and all indirect imports are stored in the current module's import list
  6673. **)
  6674. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6675. VAR
  6676. module: SyntaxTree.Module;
  6677. moduleScope: SyntaxTree.ModuleScope;
  6678. import,reimport: SyntaxTree.Import;
  6679. filename: FileName;
  6680. prevScope: SyntaxTree.Scope;
  6681. BEGIN
  6682. IF SymbolNeedsResolution(x) THEN
  6683. prevScope := currentScope;
  6684. x.SetType(SyntaxTree.importType);
  6685. moduleScope := currentScope.ownerModule.moduleScope;
  6686. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6687. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6688. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6689. Error(x.position,"forbidden self import");
  6690. ELSE
  6691. (* search through global import list: already imported ? *)
  6692. IF (x.module = NIL) & (importCache # NIL) THEN
  6693. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6694. ELSE import := NIL
  6695. END;
  6696. IF x.module # NIL THEN (* already imported indirectly *)
  6697. module := x.module;
  6698. ELSIF import # NIL THEN (* already in module list *)
  6699. module := import.module;
  6700. ASSERT(module # NIL);
  6701. x.SetModule(module);
  6702. ELSE (* must be imported *)
  6703. Global.ModuleFileName(x.moduleName,x.context,filename);
  6704. IF symbolFileFormat # NIL THEN
  6705. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6706. IF module = NIL THEN
  6707. ErrorSS(x.position,"could not import",filename);
  6708. IF VerboseErrorMessage THEN
  6709. Printout.Info("import",x)
  6710. END
  6711. ELSE
  6712. (*
  6713. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6714. (*! should rather be done by importer *)
  6715. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6716. checker.importCache := importCache;
  6717. checker.arrayBaseImported := arrayBaseImported;
  6718. checker.global := global;
  6719. checker.Module(module); (* semantic check *)
  6720. error := error OR checker.error;
  6721. END;
  6722. *)
  6723. (*
  6724. ASSERT(SyntaxTree.Resolved IN module.state);
  6725. *)
  6726. x.SetModule(module);
  6727. IF importCache # NIL THEN
  6728. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6729. import.SetContext(x.context);
  6730. import.SetModule(module);
  6731. importCache.AddImport(import);
  6732. END;
  6733. END;
  6734. ELSE
  6735. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6736. END;
  6737. END;
  6738. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6739. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6740. END;
  6741. import := module.moduleScope.firstImport;
  6742. WHILE(import # NIL) DO
  6743. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6744. ASSERT(currentScope # NIL);
  6745. ASSERT(currentScope.ownerModule # NIL);
  6746. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6747. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6748. Error(x.position,"recursive import");
  6749. ELSE
  6750. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6751. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6752. IF reimport = NIL THEN (* indirect import *)
  6753. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6754. reimport.SetContext(import.context);
  6755. reimport.SetModule(import.module);
  6756. moduleScope.AddImport(reimport);
  6757. reimport.SetScope(moduleScope);
  6758. ELSE
  6759. ASSERT(import.module # NIL);
  6760. reimport.SetModule(import.module); (* direct or indirect import *)
  6761. END;
  6762. END;
  6763. import := import.nextImport;
  6764. END;
  6765. END;
  6766. END;
  6767. currentScope := prevScope;
  6768. (* ELSE nothing to be done *)
  6769. x.SetState(SyntaxTree.Resolved);
  6770. END;
  6771. END VisitImport;
  6772. (*** statements ***)
  6773. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6774. VAR prev,resolved: SyntaxTree.Statement;
  6775. BEGIN
  6776. prev := resolvedStatement;
  6777. resolvedStatement := x;
  6778. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6779. activeCellsStatement := FALSE;
  6780. x.Accept(SELF);
  6781. (* removed this, implementation restriction should be resolved by backend
  6782. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6783. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6784. END;
  6785. *)
  6786. resolved := resolvedStatement;
  6787. resolvedStatement := prev;
  6788. RETURN resolved
  6789. END ResolveStatement;
  6790. (** check and resolve statement sequence
  6791. - check all statements, replace if necessary
  6792. **)
  6793. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6794. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6795. BEGIN
  6796. IF statementSequence # NIL THEN (* else empty *)
  6797. FOR i := 0 TO statementSequence.Length()-1 DO
  6798. statement := statementSequence.GetStatement(i);
  6799. resolved := ResolveStatement(statement);
  6800. IF (resolved # statement) THEN
  6801. statementSequence.SetStatement(i,resolved);
  6802. END;
  6803. END;
  6804. END;
  6805. END StatementSequence;
  6806. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6807. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6808. - check if procedure is callable
  6809. - check return type = NIL (otherwise must be assignment statement)
  6810. **)
  6811. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6812. VAR call: SyntaxTree.Designator;
  6813. BEGIN
  6814. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6815. call := procedureCall.call;
  6816. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6817. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6818. END;
  6819. call := ResolveDesignator(call);
  6820. IF call = SyntaxTree.invalidDesignator THEN
  6821. (* error already handled *)
  6822. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6823. (* inline call in a statement *)
  6824. ELSIF ~IsCallable(call) THEN
  6825. Error(procedureCall.position,"called object is not a procedure");
  6826. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6827. Error(procedureCall.position,"calling procedure with non-void return type");
  6828. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6829. END;
  6830. procedureCall.SetCall(call);
  6831. (*
  6832. IF call = SyntaxTree.invalidDesignator THEN
  6833. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6834. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6835. IF IsOberonInline(procedure) THEN
  6836. Warning(procedure.position,"call to inline proc");
  6837. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6838. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6839. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6840. resolvedStatement := block;
  6841. RETURN;
  6842. END;
  6843. END;
  6844. *)
  6845. END VisitProcedureCallStatement;
  6846. (** check and resolve assignment LHS := RHS
  6847. - resolve LHS and RHS
  6848. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6849. - check if assignment is compatible
  6850. - check if LHS is variable (i.e. assignable)
  6851. - convert RHS if necessary
  6852. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6853. - assignment between different ASOTs
  6854. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6855. - assignment to ASOT elements:
  6856. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6857. **)
  6858. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6859. VAR
  6860. left: SyntaxTree.Designator;
  6861. right, expression: SyntaxTree.Expression;
  6862. designator: SyntaxTree.Designator;
  6863. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6864. mathArrayType: SyntaxTree.MathArrayType;
  6865. BEGIN
  6866. right := ResolveExpression(assignment.right);
  6867. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6868. left := ResolveDesignator(assignment.left);
  6869. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6870. (* error already handled *)
  6871. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6872. (* LHS is index write operator call on ASOT *)
  6873. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6874. (* necessary ?
  6875. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6876. type := procedureType.firstParameter.type;
  6877. expression := procedureCallDesignator.parameters.GetExpression(0);
  6878. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6879. *)
  6880. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6881. ELSIF CheckVariable(left) THEN
  6882. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6883. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6884. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6885. (* conversion done by procedure call
  6886. (* try to convert to left argument *)
  6887. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6888. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6889. procedureCallDesignator.parameters.SetExpression(1, right);
  6890. END;
  6891. *)
  6892. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6893. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6894. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6895. ELSIF AssignmentCompatible(left, right) THEN
  6896. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6897. mathArrayType := MathArrayStructureOfType(left.type);
  6898. right := NewConversion(right.position, right, mathArrayType, NIL);
  6899. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6900. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6901. ELSE
  6902. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6903. assignment.SetLeft(left);
  6904. assignment.SetRight(right);
  6905. resolvedStatement := assignment
  6906. END
  6907. END
  6908. END
  6909. END VisitAssignment;
  6910. (** check and resolve assignment LHS := RHS
  6911. - resolve LHS and RHS
  6912. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6913. - check if assignment is compatible
  6914. - check if LHS is variable (i.e. assignable)
  6915. - convert RHS if necessary
  6916. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6917. - assignment between different ASOTs
  6918. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6919. - assignment to ASOT elements:
  6920. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6921. **)
  6922. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6923. VAR
  6924. left: SyntaxTree.Designator;
  6925. right: SyntaxTree.Expression;
  6926. inPort, outPort: SyntaxTree.PortType;
  6927. expression: SyntaxTree.Expression;
  6928. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6929. BEGIN
  6930. right := ResolveExpression(communication.right);
  6931. left := ResolveDesignator(communication.left);
  6932. communication.SetLeft(left);
  6933. communication.SetRight(right);
  6934. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6935. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6936. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6937. (* conversion done by procedure call
  6938. (* try to convert to left argument *)
  6939. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6940. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6941. procedureCallDesignator.parameters.SetExpression(1, right);
  6942. END;
  6943. *)
  6944. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6945. ELSE
  6946. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6947. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6948. (* error already handled *)
  6949. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6950. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6951. IF outPort.direction # SyntaxTree.OutPort THEN
  6952. Error(left.position,"not an out-port")
  6953. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6954. Error(left.position,"incompatible to port type");
  6955. ELSE
  6956. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6957. communication.SetRight(right)
  6958. END;
  6959. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6960. IF CheckVariable(left) THEN
  6961. IF inPort.direction # SyntaxTree.InPort THEN
  6962. Error(left.position,"not an in-port")
  6963. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6964. Error(right.position,"incompatible to port type");
  6965. END;
  6966. END;
  6967. ELSE
  6968. Error(communication.position,"unsupported stream operation");
  6969. END;
  6970. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6971. IF outPort.direction # SyntaxTree.OutPort THEN
  6972. Error(left.position,"not an out-port")
  6973. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6974. Error(left.position,"incompatible to port type");
  6975. ELSE
  6976. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6977. communication.SetRight(right)
  6978. END;
  6979. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6980. IF CheckVariable(right) THEN
  6981. IF inPort.direction # SyntaxTree.InPort THEN
  6982. Error(left.position,"not an in-port")
  6983. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6984. Error(right.position,"incompatible to port type");
  6985. END;
  6986. END;
  6987. ELSE
  6988. Error(communication.position, "unsupported operation");
  6989. END;
  6990. END;
  6991. END VisitCommunicationStatement;
  6992. (** check and resolve if/eslif part
  6993. - check condition
  6994. - check statement sequence
  6995. **)
  6996. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6997. VAR prevUnreachable, b: BOOLEAN;
  6998. BEGIN
  6999. prevUnreachable := currentIsUnreachable;
  7000. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7001. IF IsBooleanValue(ifPart.condition,b) THEN
  7002. IF b=FALSE THEN
  7003. currentIsUnreachable := TRUE
  7004. ELSIF b=TRUE THEN
  7005. true := TRUE
  7006. END;
  7007. END;
  7008. StatementSequence(ifPart.statements);
  7009. currentIsUnreachable := prevUnreachable;
  7010. END IfPart;
  7011. (** check and resolve if statement
  7012. - check if parts and else part statement sequence
  7013. **)
  7014. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7015. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7016. BEGIN
  7017. prevUnreachable := currentIsUnreachable;
  7018. ifPartTrue := FALSE;
  7019. IfPart(ifStatement.ifPart,ifPartTrue);
  7020. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7021. elsif := ifStatement.GetElsifPart(i);
  7022. IfPart(elsif,ifPartTrue);
  7023. END;
  7024. IF ifStatement.elsePart # NIL THEN
  7025. IF ifPartTrue THEN
  7026. currentIsUnreachable := TRUE
  7027. END;
  7028. StatementSequence(ifStatement.elsePart)
  7029. END;
  7030. currentIsUnreachable := prevUnreachable;
  7031. END VisitIfStatement;
  7032. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7033. VAR variable: SyntaxTree.Designator;
  7034. type,variableType: SyntaxTree.Type;
  7035. withEntry: WithEntry;
  7036. BEGIN
  7037. variable := ResolveDesignator(withPart.variable);
  7038. variableType := variable.type.resolved;
  7039. withPart.SetVariable(variable);
  7040. type := ResolveType(withPart.type);
  7041. withPart.SetType(type);
  7042. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7043. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7044. END;
  7045. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7046. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7047. Error(variable.position,"is not extensible designator");
  7048. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7049. Error(variable.position,"is no local variable ");
  7050. IF VerboseErrorMessage THEN
  7051. Printout.Info("variable",variable)
  7052. END;
  7053. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7054. Error(variable.position,"withguarded symbol is no type extension of ");
  7055. IF VerboseErrorMessage THEN
  7056. Printout.Info("variable",variable);
  7057. Printout.Info("type",type);
  7058. END;
  7059. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7060. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7061. Error(variable.position,"withguarded symbol is no variable ");
  7062. IF VerboseErrorMessage THEN
  7063. Printout.Info("variable",variable);
  7064. Printout.Info("type",type);
  7065. END;
  7066. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7067. Error(variable.position,"invalid change of withguarded symbol");
  7068. ELSE
  7069. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7070. NEW(withEntry);
  7071. withEntry.previous := withEntries;
  7072. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7073. withEntry.type := type;
  7074. withEntries := withEntry;
  7075. StatementSequence(withPart.statements);
  7076. withEntries := withEntries.previous;
  7077. END;
  7078. END WithPart;
  7079. (** check and resolve with statement WITH variable: type DO ... END;
  7080. - check type and variable
  7081. - check that variable type is type extension of type
  7082. - check that variable is a variable
  7083. - enter new with scope and enter guardedVariable with same name and reference to variable
  7084. - create if statement:
  7085. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7086. **)
  7087. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7088. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7089. BEGIN
  7090. prevScope := currentScope; symbol := NIL;
  7091. FOR i := 0 TO withStatement.WithParts()-1 DO
  7092. WithPart(withStatement.GetWithPart(i),symbol);
  7093. END;
  7094. IF withStatement.elsePart # NIL THEN
  7095. StatementSequence(withStatement.elsePart)
  7096. END;
  7097. currentScope := prevScope;
  7098. END VisitWithStatement;
  7099. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7100. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7101. - check 'first' < 'last' and no overlaps between different case labels
  7102. - check statement sequence
  7103. **)
  7104. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7105. VAR
  7106. i: LONGINT;
  7107. position: Position;
  7108. expression, left, right: SyntaxTree.Expression;
  7109. expressionType: SyntaxTree.Type;
  7110. l, r: LONGINT;
  7111. cl, cr: CHAR;
  7112. thiscases: SyntaxTree.CaseConstant;
  7113. BEGIN
  7114. thiscases := NIL;
  7115. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7116. expression := casePart.elements.GetExpression(i);
  7117. position := expression.position;
  7118. (* set context of range *)
  7119. IF expression IS SyntaxTree.RangeExpression THEN
  7120. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7121. END;
  7122. expression := ResolveExpression(expression);
  7123. IF expression = SyntaxTree.invalidExpression THEN
  7124. (* error already reported *)
  7125. expressionType := SyntaxTree.invalidType;
  7126. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7127. (* read out 'first' and 'last' *)
  7128. left := expression(SyntaxTree.RangeExpression).first;
  7129. right := expression(SyntaxTree.RangeExpression).last;
  7130. (* guaranteed by VisitRangeExpression: *)
  7131. ASSERT((left # NIL) & (right # NIL));
  7132. ASSERT(left.type.resolved = right.type.resolved);
  7133. left := CompatibleConversion(left.position, left, type);
  7134. right := CompatibleConversion(right.position, right, type);
  7135. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7136. expression(SyntaxTree.RangeExpression).SetLast(right);
  7137. expressionType := RegularType(position,left.type);
  7138. ELSE
  7139. expression := ConstantExpression(expression);
  7140. expression := CompatibleConversion(expression.position, expression, type);
  7141. (*
  7142. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7143. left := Global.NewCharacterValue(system,expression.position,cl);
  7144. expression := casePart.elements.GetExpression(i);
  7145. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7146. expression := left
  7147. END;
  7148. *)
  7149. casePart.elements.SetExpression(i,expression);
  7150. left := expression; right := expression;
  7151. expressionType := RegularType(position,expression.type)
  7152. END;
  7153. IF (expressionType = SyntaxTree.invalidType) THEN
  7154. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7155. Error(position, "inadmissible case label");
  7156. expression := SyntaxTree.invalidExpression;
  7157. ELSE
  7158. l := 0; r := 0;
  7159. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7160. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7161. l := ORD(cl); r := ORD(cr);
  7162. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7163. ELSE
  7164. expression := SyntaxTree.invalidExpression
  7165. END;
  7166. IF expression # SyntaxTree.invalidExpression THEN
  7167. IF l>r THEN
  7168. Error(position, "empty case label")
  7169. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7170. Error(position, "duplicate case label");
  7171. ELSE
  7172. IF l < min THEN min := l END;
  7173. IF r > max THEN max := r END;
  7174. END;
  7175. END;
  7176. END;
  7177. casePart.elements.SetExpression(i,expression);
  7178. END;
  7179. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7180. casePart.SetConstants(thiscases);
  7181. StatementSequence(casePart.statements);
  7182. END CasePart;
  7183. (** check and resolve case statement CASE variable OF ... END;
  7184. - check variable
  7185. - check case parts
  7186. **)
  7187. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7188. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7189. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7190. BEGIN
  7191. expression := ResolveExpression(caseStatement.variable);
  7192. type := RegularType(expression.position,expression.type);
  7193. IF type = SyntaxTree.invalidType THEN
  7194. expression := SyntaxTree.invalidExpression;
  7195. ELSIF IsIntegerType(type) THEN
  7196. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7197. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7198. (*
  7199. expression := Global.NewCharacterValue(system,expression.position,ch);
  7200. *)
  7201. type := expression.type;
  7202. ELSIF IsCharacterType(type) THEN
  7203. ELSIF IsEnumerationType(type) THEN
  7204. ELSE
  7205. Error(caseStatement.variable.position,"variable must be integer or character type");
  7206. expression := SyntaxTree.invalidExpression;
  7207. END;
  7208. caseStatement.SetVariable(expression);
  7209. caseList := NIL;
  7210. min := MAX(LONGINT); max := MIN(LONGINT);
  7211. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7212. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7213. END;
  7214. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7215. msg := "huge sparse case table ";
  7216. Strings.AppendInt(msg, max-min);
  7217. Strings.Append(msg,"/");
  7218. Strings.AppendInt(msg, caseStatement.CaseParts());
  7219. Warning(caseStatement.position,msg);
  7220. END;
  7221. caseStatement.SetMinMax(min,max);
  7222. StatementSequence(caseStatement.elsePart);
  7223. IF expression.resolved # NIL THEN
  7224. IF IsCharacterValue(expression,ch) THEN
  7225. l := ORD(ch)
  7226. ELSIF IsIntegerValue(expression,l) THEN
  7227. END;
  7228. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7229. END;
  7230. END VisitCaseStatement;
  7231. (** check and resolve while statement
  7232. - check condition
  7233. - check statement sequence
  7234. **)
  7235. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7236. VAR prevIsUnreachable,b: BOOLEAN;
  7237. BEGIN
  7238. prevIsUnreachable := currentIsUnreachable;
  7239. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7240. IF IsBooleanValue(whileStatement.condition,b) THEN
  7241. IF b=FALSE THEN
  7242. currentIsUnreachable := TRUE
  7243. END;
  7244. END;
  7245. StatementSequence(whileStatement.statements);
  7246. currentIsUnreachable := prevIsUnreachable
  7247. END VisitWhileStatement;
  7248. (** check and resolve repeat statement
  7249. - check condition
  7250. - check statement sequence
  7251. **)
  7252. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7253. BEGIN
  7254. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7255. StatementSequence(repeatStatement.statements);
  7256. END VisitRepeatStatement;
  7257. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7258. VAR withEntry: WithEntry;
  7259. BEGIN
  7260. withEntry := withEntries;
  7261. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7262. withEntry := withEntry.previous
  7263. END;
  7264. IF withEntry = NIL THEN RETURN FALSE
  7265. ELSE
  7266. type := withEntry.type;
  7267. RETURN TRUE
  7268. END;
  7269. END GetGuard;
  7270. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7271. - check that variable is an integer variable
  7272. - check that from is integer typed with compatible type
  7273. - check that to has compatible type
  7274. - check that by is constant integer with compatible type
  7275. **)
  7276. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7277. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7278. BEGIN
  7279. designator := ResolveDesignator(forStatement.variable);
  7280. type := SyntaxTree.invalidType;
  7281. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7282. designator := SyntaxTree.invalidDesignator;
  7283. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7284. Error(designator.position,"control variable of non-integer type");
  7285. designator := SyntaxTree.invalidDesignator;
  7286. ELSIF CheckVariable(designator) THEN
  7287. type := designator.type;
  7288. END;
  7289. forStatement.SetVariable(designator);
  7290. expression := ResolveExpression(forStatement.from);
  7291. IF expression = SyntaxTree.invalidExpression THEN
  7292. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7293. Error(expression.position,"start value of incompatible type");
  7294. expression := SyntaxTree.invalidExpression;
  7295. ELSIF type # SyntaxTree.invalidType THEN
  7296. expression := NewConversion(expression.position,expression,type,NIL)
  7297. END;
  7298. forStatement.SetFrom(expression);
  7299. expression := ResolveExpression(forStatement.to);
  7300. IF expression = SyntaxTree.invalidExpression THEN
  7301. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7302. Error(expression.position,"end value of incompatible type");
  7303. expression := SyntaxTree.invalidExpression;
  7304. ELSIF type # SyntaxTree.invalidType THEN
  7305. expression := NewConversion(expression.position,expression,type,NIL)
  7306. END;
  7307. forStatement.SetTo(expression);
  7308. IF forStatement.by # NIL THEN
  7309. expression := ConstantInteger(forStatement.by);
  7310. ELSE
  7311. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7312. END;
  7313. IF expression = SyntaxTree.invalidExpression THEN
  7314. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7315. Error(expression.position,"step value of incompatible type");
  7316. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7317. Error(expression.position,"invalid step value");
  7318. ELSIF type # SyntaxTree.invalidType THEN
  7319. expression := NewConversion(expression.position,expression,type,NIL)
  7320. END;
  7321. forStatement.SetBy(expression);
  7322. StatementSequence(forStatement.statements);
  7323. END VisitForStatement;
  7324. (** check and resolve loop statement LOOP StatementSequence END
  7325. - check statement sequence
  7326. **)
  7327. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7328. BEGIN
  7329. StatementSequence(loopStatement.statements)
  7330. END VisitLoopStatement;
  7331. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7332. BEGIN
  7333. StatementSequence(exitableBlock.statements);
  7334. END VisitExitableBlock;
  7335. (** check and resolve exit statement EXIT
  7336. - check that exit is within LOOP statement block
  7337. **)
  7338. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7339. VAR outer: SyntaxTree.Statement;
  7340. BEGIN
  7341. outer := exitStatement.outer;
  7342. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7343. outer := outer.outer;
  7344. END;
  7345. IF outer = NIL THEN
  7346. Error(exitStatement.position,"exit statement not within loop statement");
  7347. END;
  7348. END VisitExitStatement;
  7349. (** check and resolve return statement RETURN [expression]
  7350. - check expression (if any)
  7351. - check if in procedure scope
  7352. - if in procedure scope then check expression compatibility
  7353. - if not in procecdure scope then check on return without expression
  7354. **)
  7355. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7356. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7357. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7358. BEGIN
  7359. position := returnStatement.position;
  7360. expression := returnStatement.returnValue;
  7361. IF expression # NIL THEN
  7362. expression := ResolveExpression(expression);
  7363. returnStatement.SetReturnValue(expression);
  7364. END;
  7365. outer := returnStatement.outer;
  7366. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7367. outer := outer.outer
  7368. END;
  7369. IF (outer # NIL) THEN
  7370. scope := outer(SyntaxTree.Body).inScope;
  7371. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7372. IF (expression # NIL) THEN
  7373. Error(position, "return statement with parameter not in procedure scope");
  7374. END;
  7375. ELSE
  7376. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7377. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7378. Error(position, "return statement in procedure that does not return");
  7379. END;
  7380. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7381. IF returnType # NIL THEN
  7382. returnType := returnType.resolved;
  7383. IF expression = NIL THEN
  7384. Error(position, "empty return type in procedure providing a return type")
  7385. ELSIF expression.type = NIL THEN
  7386. Error(position,"returned type incompatible: expression has no type");
  7387. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7388. Error(position, "return type not compatible");
  7389. IF VerboseErrorMessage THEN
  7390. Printout.Info("returnType",returnType);
  7391. Printout.Info("expression",expression);
  7392. END;
  7393. ELSE
  7394. expression := NewConversion(expression.position,expression,returnType,NIL);
  7395. returnStatement.SetReturnValue(expression);
  7396. END;
  7397. ELSIF expression # NIL THEN
  7398. Error(position, "non-empty return type in procedure providing no return type");
  7399. END;
  7400. END;
  7401. END;
  7402. END VisitReturnStatement;
  7403. (** check and resolve await statement AWAIT(condition: Expression)
  7404. - check await condition
  7405. **)
  7406. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7407. VAR condition: SyntaxTree.Expression;
  7408. BEGIN
  7409. condition := ResolveCondition(awaitStatement.condition);
  7410. IF currentIsRealtime THEN
  7411. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7412. END;
  7413. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7414. Error(awaitStatement.position,"senseless await statement with constant condition");
  7415. END;
  7416. awaitStatement.SetCondition(condition);
  7417. END VisitAwaitStatement;
  7418. PROCEDURE CheckSystemImport(position: Position);
  7419. VAR import: SyntaxTree.Import;
  7420. BEGIN
  7421. import := currentScope.ownerModule.moduleScope.firstImport;
  7422. WHILE(import # NIL) DO
  7423. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7424. RETURN;
  7425. END;
  7426. import := import.nextImport;
  7427. END;
  7428. Error(position, "forbidden code without system import");
  7429. END CheckSystemImport;
  7430. (** check and resolve code statement: do nothing, must be done by assembler
  7431. **)
  7432. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7433. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7434. BEGIN
  7435. CheckSystemImport(code.position);
  7436. FOR i := 0 TO code.inRules.Length()-1 DO
  7437. statement := code.inRules.GetStatement(i);
  7438. IF statement IS SyntaxTree.Assignment THEN
  7439. WITH statement: SyntaxTree.Assignment DO
  7440. statement.SetRight(ResolveExpression(statement.right));
  7441. END;
  7442. ELSE
  7443. Error(statement.position, "can only be assignment")
  7444. END;
  7445. END;
  7446. FOR i := 0 TO code.outRules.Length()-1 DO
  7447. statement := code.outRules.GetStatement(i);
  7448. IF statement IS SyntaxTree.Assignment THEN
  7449. WITH statement: SyntaxTree.Assignment DO
  7450. statement.SetLeft(ResolveDesignator(statement.left));
  7451. END;
  7452. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7453. (* must be a reference to some register *)
  7454. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7455. ELSE
  7456. Printout.Info("out statement ", statement);
  7457. Error(statement.position, "(out) can only be assignment")
  7458. END;
  7459. END;
  7460. END VisitCode;
  7461. (** check and set flags of a statement block
  7462. - check for multiply occurence of a flag
  7463. - check and set priority only in bodies
  7464. - check for valid names
  7465. **)
  7466. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7467. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7468. flag: LONGINT; recordBody: SyntaxTree.Body;
  7469. PROCEDURE SetProtectedRecord;
  7470. VAR scope: SyntaxTree.Scope;
  7471. BEGIN
  7472. scope := currentScope;
  7473. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7474. scope := scope.outerScope
  7475. END;
  7476. IF scope # NIL THEN
  7477. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7478. END;
  7479. END SetProtectedRecord;
  7480. BEGIN
  7481. flags := {};
  7482. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7483. recordBody := block(SyntaxTree.Body)
  7484. ELSE
  7485. recordBody := NIL
  7486. END;
  7487. blockModifier := block.blockModifiers;
  7488. WHILE(blockModifier # NIL) DO
  7489. name := blockModifier.identifier;
  7490. expression := blockModifier.expression;
  7491. position := blockModifier.position;
  7492. flag := -1;
  7493. IF name=Global.NamePriority THEN
  7494. IF expression = NIL THEN
  7495. Error(position, "missing priority expression");
  7496. ELSIF recordBody = NIL THEN
  7497. Error(position, "priority not on record body");
  7498. ELSIF recordBody.priority # NIL THEN
  7499. Error(position, "duplicate priority expression");
  7500. ELSE
  7501. recordBody.SetPriority(expression);
  7502. END;
  7503. ELSIF expression # NIL THEN
  7504. Error(expression.position,"expression not in connection with priority")
  7505. ELSIF name=Global.NameExclusive THEN
  7506. IF block.isExclusive THEN
  7507. Error(position, "duplicate exclusive flag")
  7508. END;
  7509. block.SetExclusive(TRUE); SetProtectedRecord;
  7510. ELSIF name=Global.NameActive THEN
  7511. IF recordBody = NIL THEN
  7512. Error(position, "active not in record body");
  7513. ELSIF recordBody.isActive THEN
  7514. Error(position, "duplicate active flag")
  7515. ELSE
  7516. recordBody.SetActive(TRUE); SetProtectedRecord;
  7517. END;
  7518. ELSIF name=Global.NameSafe THEN
  7519. IF recordBody = NIL THEN
  7520. Error(position, "safe not in record body");
  7521. ELSIF recordBody.isSafe THEN
  7522. Error(position, "duplicate safe flag")
  7523. ELSE
  7524. recordBody.SetSafe(TRUE);
  7525. SetProtectedRecord;
  7526. END;
  7527. ELSIF name=Global.NameRealtime THEN
  7528. IF recordBody = NIL THEN
  7529. Error(position, "realtime not in record body");
  7530. ELSIF recordBody.isRealtime THEN
  7531. Error(position, "duplicate realtime flag")
  7532. ELSE
  7533. recordBody.SetRealtime(TRUE);
  7534. block.SetRealtime(TRUE);
  7535. END;
  7536. ELSIF name=Global.NameUnchecked THEN
  7537. IF block.isUnchecked THEN
  7538. Error(position, "duplicate unchecked flag")
  7539. ELSE
  7540. block.SetUnchecked(TRUE);
  7541. END;
  7542. ELSIF (name=Global.NameUncooperative) THEN
  7543. IF block.isUncooperative THEN
  7544. Error(position, "duplicate uncooperative flag")
  7545. ELSE
  7546. block.SetUncooperative(TRUE);
  7547. END;
  7548. ELSE
  7549. Error(position, "unknown block modifier");
  7550. END;
  7551. blockModifier := blockModifier.nextModifier;
  7552. END;
  7553. END BlockFlags;
  7554. (** check and resolve statement block
  7555. - check flags (exclusive)
  7556. - check statement sequence
  7557. **)
  7558. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7559. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7560. BEGIN
  7561. BlockFlags(statementBlock);
  7562. IF statementBlock.isExclusive THEN
  7563. (* check that not in exclusive block *)
  7564. IF currentIsExclusive THEN
  7565. Error (statementBlock.position,"forbidden recursive exclusive")
  7566. ELSIF currentIsRealtime THEN
  7567. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7568. END;
  7569. END;
  7570. recentExclusive := currentIsExclusive;
  7571. recentUnreachable := currentIsUnreachable;
  7572. recentRealtime := currentIsRealtime;
  7573. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7574. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7575. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7576. StatementSequence(statementBlock.statements);
  7577. currentIsRealtime := recentRealtime;
  7578. currentIsExclusive := recentExclusive;
  7579. currentIsUnreachable := recentUnreachable;
  7580. END VisitStatementBlock;
  7581. (** check and resolve body
  7582. - check flags (active, priority, safe)
  7583. - check body and finally part
  7584. **)
  7585. PROCEDURE Body(body: SyntaxTree.Body);
  7586. BEGIN
  7587. VisitStatementBlock(body);
  7588. IF body.isActive THEN
  7589. IF ~currentIsBodyProcedure THEN
  7590. Error(body.position,"active flag not in object body");
  7591. ELSIF body.priority # NIL THEN
  7592. body.SetPriority(ConstantInteger(body.priority));
  7593. END;
  7594. ELSIF body.isSafe THEN
  7595. Error(body.position,"safe flag not in active body");
  7596. ELSIF body.priority # NIL THEN
  7597. Error(body.position,"priority flag not in active body");
  7598. END;
  7599. IF body.code # NIL THEN
  7600. CheckSystemImport(body.position);
  7601. END;
  7602. StatementSequence(body.finally)
  7603. END Body;
  7604. (*** scopes ***)
  7605. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7606. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7607. VAR duplicateSymbol: BOOLEAN;
  7608. BEGIN
  7609. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7610. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7611. Error(symbol.position,"globally defined keyword")
  7612. END;
  7613. scope.EnterSymbol(symbol,duplicateSymbol);
  7614. IF ~allowDuplicate & duplicateSymbol THEN
  7615. Error(symbol.position,"Multiply defined identifier.");
  7616. IF VerboseErrorMessage THEN
  7617. Printout.Info("multiply defined identifier",symbol);
  7618. Printout.Info("in scope",scope);
  7619. END;
  7620. END;
  7621. END Register;
  7622. (**
  7623. implementation: check and resolve an implementation part
  7624. **)
  7625. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7626. move implementation checker to a separate object ? *)
  7627. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7628. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7629. BEGIN
  7630. prevIsRealtime := currentIsRealtime;
  7631. prevIsBodyProcedure := currentIsBodyProcedure;
  7632. prevIsCellNet := currentIsCellNet;
  7633. prevScope := currentScope;
  7634. currentScope := scope;
  7635. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7636. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7637. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7638. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7639. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7640. (*
  7641. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7642. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7643. END;
  7644. *)
  7645. END;
  7646. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7647. (* module body, record bodies are wrapped into an artifical procedure *)
  7648. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7649. Body(scope(SyntaxTree.ProcedureScope).body)
  7650. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7651. Body(scope(SyntaxTree.ProcedureScope).body)
  7652. END;
  7653. END;
  7654. currentScope := prevScope;
  7655. currentIsRealtime := prevIsRealtime;
  7656. currentIsBodyProcedure := prevIsBodyProcedure;
  7657. currentIsCellNet := prevIsCellNet;
  7658. END Implementation;
  7659. (** implementation phase:
  7660. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7661. **)
  7662. PROCEDURE Implementations(x: SyntaxTree.Module);
  7663. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7664. BEGIN
  7665. prevPhase := phase;
  7666. phase := InlinePhase;
  7667. scope := x.firstScope;
  7668. WHILE(scope # NIL) DO
  7669. Implementation(scope);
  7670. scope := scope.nextScope;
  7671. END;
  7672. phase := ImplementationPhase;
  7673. scope := x.firstScope;
  7674. WHILE(scope # NIL) DO
  7675. Implementation(scope);
  7676. scope := scope.nextScope;
  7677. END;
  7678. phase := prevPhase;
  7679. END Implementations;
  7680. (** declaration phase:
  7681. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7682. - import lists (for module scopes)
  7683. - parameter list (for procedure scopes)
  7684. - constant declarations
  7685. - type declarations
  7686. - variable declarations
  7687. - procedure declarations
  7688. preformed in two stages:
  7689. - first all symbols are entered into the symbol table (with uniqueness check),
  7690. - then all symbols are resolved
  7691. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7692. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7693. **)
  7694. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7695. VAR
  7696. constant: SyntaxTree.Constant;
  7697. typeDeclaration: SyntaxTree.TypeDeclaration;
  7698. variable: SyntaxTree.Variable;
  7699. procedure: SyntaxTree.Procedure;
  7700. procedureType : SyntaxTree.ProcedureType;
  7701. prevScope: SyntaxTree.Scope;
  7702. parameter: SyntaxTree.Parameter;
  7703. import: SyntaxTree.Import;
  7704. symbol: SyntaxTree.Symbol;
  7705. prevPhase: LONGINT;
  7706. prevError : BOOLEAN;
  7707. i: LONGINT;
  7708. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7709. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7710. BEGIN
  7711. IF type.baseType # NIL THEN
  7712. baseType := type.baseType.resolved;
  7713. IF baseType IS SyntaxTree.PointerType THEN
  7714. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7715. END;
  7716. (*
  7717. IF baseType IS SyntaxTree.CellType THEN
  7718. DeclareCell(baseType(SyntaxTree.CellType));
  7719. END;
  7720. *)
  7721. END;
  7722. parameter := type.firstParameter;
  7723. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7724. (*
  7725. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7726. variable.SetType(parameter.type);
  7727. variable.SetAccess(SyntaxTree.Hidden);
  7728. variable.SetModifiers(parameter.modifiers);
  7729. currentScope.PushVariable(variable);
  7730. *)
  7731. Register(parameter,scope, FALSE);
  7732. parameter := parameter.nextParameter;
  7733. END;
  7734. property := type.firstProperty;
  7735. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7736. (*
  7737. variable := currentScope.FindVariable(property.name);
  7738. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7739. prop := variable(SyntaxTree.Property);
  7740. ELSE (* add, duplicate symbols detection later *)
  7741. prop := SyntaxTree.NewProperty(property.position, property.name);
  7742. currentScope.PushVariable(prop);
  7743. END;
  7744. prop.SetType(property.type);
  7745. prop.SetValue(property.value);
  7746. prop.SetAccess(SyntaxTree.Hidden);
  7747. *)
  7748. Register(property, scope, FALSE);
  7749. property := property.nextProperty;
  7750. END;
  7751. END DeclareCell;
  7752. BEGIN
  7753. prevError := error;
  7754. prevPhase := phase;
  7755. phase := DeclarationPhase;
  7756. prevScope := currentScope;
  7757. currentScope := scope;
  7758. error := FALSE;
  7759. (* first enter all symbols in scope *)
  7760. IF scope IS SyntaxTree.ModuleScope THEN
  7761. (* treat imports first for a module scope, , set default context if necessary *)
  7762. import := scope(SyntaxTree.ModuleScope).firstImport;
  7763. WHILE(import # NIL) DO
  7764. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7765. Register(import, currentScope, FALSE);
  7766. import := import.nextImport;
  7767. END;
  7768. import := scope(SyntaxTree.ModuleScope).firstImport;
  7769. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7770. ResolveSymbol(import);
  7771. import := import.nextImport;
  7772. END;
  7773. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7774. (* enter parameters for a procedure scope *)
  7775. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7776. parameter := procedureType.firstParameter;
  7777. WHILE(parameter # NIL) DO
  7778. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7779. END;
  7780. parameter := procedureType.returnParameter;
  7781. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7782. parameter := procedureType.selfParameter;
  7783. IF parameter # NIL THEN
  7784. Register(parameter, currentScope, FALSE);
  7785. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7786. END;
  7787. ELSIF scope IS SyntaxTree.CellScope THEN
  7788. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7789. IF~skipImplementation THEN
  7790. import := scope(SyntaxTree.CellScope).firstImport;
  7791. WHILE(import # NIL) DO
  7792. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7793. Register(import, currentScope, FALSE);
  7794. import := import.nextImport;
  7795. END;
  7796. import := scope(SyntaxTree.CellScope).firstImport;
  7797. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7798. ResolveSymbol(import);
  7799. import := import.nextImport;
  7800. END;
  7801. END;
  7802. END;
  7803. IF error THEN RETURN END;
  7804. IF skipImplementation THEN
  7805. scope.Clear;
  7806. END;
  7807. (* constants *)
  7808. constant := scope.firstConstant;
  7809. WHILE (constant # NIL) DO
  7810. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7811. END;
  7812. (* type declarations *)
  7813. typeDeclaration := scope.firstTypeDeclaration;
  7814. WHILE (typeDeclaration # NIL) DO
  7815. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7816. END;
  7817. (* variables *)
  7818. variable := scope.firstVariable;
  7819. WHILE (variable # NIL) DO
  7820. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7821. END;
  7822. (* procedures *)
  7823. IF scope.procedures # NIL THEN
  7824. FOR i := 0 TO scope.procedures.Length()-1 DO
  7825. procedure := scope.procedures.GetProcedure(i);
  7826. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7827. IF procedureType.selfParameter = NIL THEN
  7828. scope.AddProcedure(procedure);
  7829. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7830. ELSE
  7831. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7832. IF typeDeclaration = NIL THEN
  7833. Error(procedureType.selfParameter.position, "No such type declaration");
  7834. ELSE
  7835. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7836. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7837. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7838. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7839. END;
  7840. END;
  7841. END;
  7842. END;
  7843. (*
  7844. procedure := scope.firstProcedure;
  7845. WHILE (procedure # NIL) DO
  7846. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7847. IF procedureType.selfParameter = NIL THEN
  7848. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7849. END;
  7850. procedure := procedure.nextProcedure;
  7851. END;
  7852. *)
  7853. (* type bound procedures *)
  7854. (*
  7855. procedure := scope.firstProcedure;
  7856. WHILE (procedure # NIL) DO
  7857. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7858. IF procedureType.selfParameter # NIL THEN
  7859. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7860. IF typeDeclaration = NIL THEN
  7861. Error(procedureType.selfParameter.position, "No such type declaration");
  7862. ELSE
  7863. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7864. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7865. END;
  7866. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7867. END;
  7868. procedure := procedure.nextProcedure;
  7869. END;
  7870. *)
  7871. (* now process all symbols without any presumption on the order *)
  7872. symbol := scope.firstSymbol;
  7873. WHILE(symbol # NIL) DO
  7874. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7875. ResolveSymbol(symbol);
  7876. END;
  7877. symbol := symbol.nextSymbol;
  7878. END;
  7879. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7880. symbol := scope.firstSymbol;
  7881. WHILE symbol # NIL DO
  7882. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7883. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7884. pointerFixes.Add(symbol, currentScope);
  7885. END;
  7886. IF ~symbol.type.resolved.isRealtime THEN
  7887. Error(symbol.position,"symbol has no realtime type");
  7888. END;
  7889. END;
  7890. symbol := symbol.nextSymbol
  7891. END;
  7892. END;
  7893. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7894. Error(Basic.invalidPosition,"problems during offset computation in module");
  7895. END;
  7896. IF (scope.ownerModule # NIL) THEN
  7897. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7898. scope.ownerModule.AddScope(scope);
  7899. END;
  7900. phase := prevPhase;
  7901. currentScope := prevScope;
  7902. error := error OR prevError;
  7903. END Declarations;
  7904. (* nopov *)
  7905. (** check if all operators from one module are compatible to the ones in the other module
  7906. - check if there are not multiple operators with the same signature
  7907. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7908. - check for all operators whose signatures are compatible, whether the return types are compatible
  7909. note that:
  7910. - the return type is not considered to be part of the signature
  7911. - two signatures are considered compatible, if all of the operands are compatible
  7912. **)
  7913. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7914. VAR
  7915. thisOperator, thatOperator: SyntaxTree.Operator;
  7916. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7917. thisParameter, thatParameter: SyntaxTree.Parameter;
  7918. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7919. i: LONGINT;
  7920. BEGIN
  7921. currentScope := thisModuleScope;
  7922. hasError := FALSE;
  7923. (* go through all operators in the other module *)
  7924. thatOperator := thatModuleScope.firstOperator;
  7925. WHILE (thatOperator # NIL) & ~hasError DO
  7926. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7927. (* the other operator is accessible *)
  7928. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7929. (* the other operator is not the conversion operator *)
  7930. (* go through all operators in this module *)
  7931. thisOperator := thisModuleScope.firstOperator;
  7932. WHILE (thisOperator # NIL) & ~hasError DO
  7933. IF thisOperator # thatOperator THEN
  7934. (* the operators are not the same *)
  7935. IF thisOperator.name = thatOperator.name THEN
  7936. (* the operators share the same identifier *)
  7937. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7938. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7939. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7940. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7941. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7942. (* both operators have the same paramter count *)
  7943. thisParameter := thisProcedureType.firstParameter;
  7944. thatParameter := thatProcedureType.firstParameter;
  7945. operandsAreEqual := TRUE;
  7946. operandsAreCompatible := TRUE;
  7947. (* go through all parameters *)
  7948. FOR i := 1 TO thisProcedureType.numberParameters DO
  7949. ASSERT(thatParameter # NIL);
  7950. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7951. operandsAreEqual := FALSE;
  7952. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7953. operandsAreCompatible := FALSE
  7954. END
  7955. END;
  7956. thisParameter := thisParameter.nextParameter;
  7957. thatParameter := thatParameter.nextParameter
  7958. END;
  7959. IF operandsAreEqual THEN
  7960. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7961. hasError := TRUE
  7962. ELSIF operandsAreCompatible THEN
  7963. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7964. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7965. hasError := TRUE
  7966. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7967. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7968. hasError := TRUE
  7969. END
  7970. END
  7971. END
  7972. END
  7973. END;
  7974. thisOperator := thisOperator.nextOperator
  7975. END
  7976. END
  7977. END;
  7978. thatOperator := thatOperator.nextOperator
  7979. END
  7980. END CheckInterOperatorConformity;
  7981. (** check module:
  7982. - check module declaration
  7983. - add context, if necessary
  7984. - remove module from import cache, if necessary
  7985. - check declarations
  7986. - resolve all type fixes
  7987. - check implementation (bodies)
  7988. **)
  7989. PROCEDURE Module*(x: SyntaxTree.Module);
  7990. VAR (* nopov *)
  7991. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7992. BEGIN
  7993. prevScope := currentScope;
  7994. prevIsCellNet := currentIsCellNet;
  7995. module := x;
  7996. ASSERT(x # NIL);
  7997. global := system.globalScope[x.case];
  7998. x.moduleScope.SetGlobalScope(global);
  7999. currentScope := global;
  8000. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8001. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8002. RemoveModuleFromCache(importCache,x);
  8003. Declarations(x.moduleScope, FALSE);
  8004. FixTypes();
  8005. IF module.isCellNet THEN
  8006. currentIsCellNet := TRUE;
  8007. modifier := x.modifiers;
  8008. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8009. CheckModifiers(modifier, FALSE);
  8010. END;
  8011. (* nopov *)
  8012. IF ~error THEN
  8013. (* check if operators conform to each other within this module *)
  8014. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8015. (* go through all imports *)
  8016. import := x.moduleScope.firstImport;
  8017. WHILE import # NIL DO
  8018. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8019. (* check if all operators in this module conform to the ones of the imported module *)
  8020. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8021. END;
  8022. import := import.nextImport
  8023. END;
  8024. END;
  8025. Implementations(x);
  8026. module := NIL;
  8027. currentIsCellNet := prevIsCellNet;
  8028. currentScope := prevScope;
  8029. END Module;
  8030. END Checker;
  8031. Warnings*=OBJECT (SyntaxTree.Visitor)
  8032. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8033. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8034. BEGIN
  8035. SELF.diagnostics := diagnostics
  8036. END InitWarnings;
  8037. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8038. BEGIN END VisitPortType;
  8039. (** types *)
  8040. PROCEDURE Type(x: SyntaxTree.Type);
  8041. BEGIN x.Accept(SELF)
  8042. END Type;
  8043. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8044. BEGIN END VisitType;
  8045. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8046. BEGIN END VisitBasicType;
  8047. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8048. BEGIN END VisitCharacterType;
  8049. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8050. BEGIN END VisitIntegerType;
  8051. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8052. BEGIN END VisitFloatType;
  8053. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8054. BEGIN END VisitQualifiedType;
  8055. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8056. BEGIN END VisitStringType;
  8057. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8058. BEGIN END VisitEnumerationType;
  8059. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8060. BEGIN END VisitRangeType;
  8061. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8062. BEGIN
  8063. IF ~(SyntaxTree.Warned IN x.state) THEN
  8064. x.SetState(SyntaxTree.Warned);
  8065. Type(x.arrayBase);
  8066. END;
  8067. END VisitArrayType;
  8068. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8069. BEGIN
  8070. IF ~(SyntaxTree.Warned IN x.state) THEN
  8071. x.SetState(SyntaxTree.Warned);
  8072. Type(x.arrayBase);
  8073. END;
  8074. END VisitMathArrayType;
  8075. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8076. BEGIN
  8077. IF ~(SyntaxTree.Warned IN x.state) THEN
  8078. x.SetState(SyntaxTree.Warned);
  8079. Type(x.pointerBase);
  8080. END;
  8081. END VisitPointerType;
  8082. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8083. BEGIN Scope(x.recordScope) END VisitRecordType;
  8084. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8085. BEGIN Scope(x.cellScope) END VisitCellType;
  8086. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8087. BEGIN END VisitProcedureType;
  8088. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8089. VAR msg: ARRAY 256 OF CHAR;
  8090. BEGIN
  8091. Global.GetSymbolName(x,msg);
  8092. Strings.Append(msg," ");
  8093. Strings.Append(msg,text);
  8094. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8095. END Warning;
  8096. (** symbols *)
  8097. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8098. BEGIN
  8099. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8100. IF ~(x IS SyntaxTree.Parameter) THEN
  8101. Warning(x,"never used");
  8102. END;
  8103. END;
  8104. x.Accept(SELF);
  8105. END Symbol;
  8106. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8107. BEGIN END VisitSymbol;
  8108. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8109. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8110. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8111. BEGIN END VisitConstant;
  8112. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8113. BEGIN END VisitVariable;
  8114. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8115. BEGIN END VisitProperty;
  8116. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8117. BEGIN END VisitParameter;
  8118. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8119. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8120. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8121. BEGIN END VisitOperator;
  8122. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8123. BEGIN END VisitImport;
  8124. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8125. VAR
  8126. symbol: SyntaxTree.Symbol;
  8127. BEGIN
  8128. symbol := scope.firstSymbol;
  8129. WHILE(symbol # NIL) DO
  8130. Symbol(symbol);
  8131. symbol := symbol.nextSymbol;
  8132. END;
  8133. END Scope;
  8134. PROCEDURE Module*(x: SyntaxTree.Module);
  8135. BEGIN
  8136. SELF.module := x;
  8137. Scope(x.moduleScope);
  8138. END Module;
  8139. END Warnings;
  8140. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8141. BEGIN
  8142. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8143. END IsOberonInline;
  8144. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8145. BEGIN
  8146. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8147. END Resolved;
  8148. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8149. VAR i: LONGINT;
  8150. BEGIN
  8151. i := 1;
  8152. WHILE i < x DO
  8153. i := i *2
  8154. END;
  8155. RETURN i=x
  8156. END PowerOf2;
  8157. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8158. BEGIN
  8159. RETURN
  8160. (scope # NIL) &
  8161. (scope IS SyntaxTree.ModuleScope)
  8162. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8163. OR
  8164. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8165. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8166. END IsCellNetScope;
  8167. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8168. BEGIN
  8169. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8170. END IsCellScope;
  8171. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8172. BEGIN
  8173. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8174. RETURN (scope # NIL) & IsCellNetScope(scope)
  8175. END InCellNetScope;
  8176. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8177. BEGIN
  8178. ASSERT(size MOD system.dataUnit = 0);
  8179. RETURN size DIV system.dataUnit
  8180. END ToMemoryUnits;
  8181. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8182. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8183. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8184. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8185. BEGIN
  8186. IF t = NIL THEN
  8187. RETURN TRUE
  8188. ELSE
  8189. t := t.resolved;
  8190. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8191. END;
  8192. END TypeAllowed;
  8193. BEGIN
  8194. type := type.resolved;
  8195. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8196. RETURN FALSE
  8197. ELSE
  8198. procedureType := type(SyntaxTree.ProcedureType);
  8199. numberParameters := procedureType.numberParameters;
  8200. RETURN
  8201. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8202. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8203. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8204. END;
  8205. END GetProcedureAllowed;
  8206. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8207. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8208. VAR import: SyntaxTree.Import;
  8209. BEGIN
  8210. import := importCache.ImportByModuleName(x.name,x.context);
  8211. IF import # NIL THEN
  8212. importCache.RemoveImporters(x.name,x.context);
  8213. END;
  8214. END RemoveModuleFromCache;
  8215. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8216. (* to <- this assignment compatibility *)
  8217. VAR result: BOOLEAN;
  8218. BEGIN
  8219. IF this= NIL THEN result := (to=NIL)
  8220. ELSIF to=NIL THEN result := FALSE
  8221. ELSE
  8222. (*! will be replaced by this:
  8223. ELSE result := this.CompatibleTo(to.resolved);
  8224. *)
  8225. this := this.resolved; to := to.resolved;
  8226. IF to=SyntaxTree.invalidType THEN result := FALSE
  8227. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8228. ELSIF to = this THEN
  8229. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8230. ELSIF to IS SyntaxTree.BasicType THEN
  8231. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8232. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8233. result := this.CompatibleTo(to.resolved)
  8234. ELSE
  8235. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8236. END
  8237. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8238. result := to.sizeInBits = this.sizeInBits;
  8239. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8240. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8241. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8242. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8243. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8244. result := TRUE;
  8245. ELSIF to IS SyntaxTree.AnyType THEN
  8246. 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);
  8247. ELSIF to IS SyntaxTree.ObjectType THEN
  8248. 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 *) ;
  8249. ELSIF to IS SyntaxTree.ByteType THEN
  8250. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8251. ELSIF to IS SyntaxTree.CharacterType THEN
  8252. result := IsCharacterType(this)
  8253. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8254. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8255. 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
  8256. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8257. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8258. result := TRUE;
  8259. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8260. result := TRUE;
  8261. ELSE
  8262. result := FALSE
  8263. END;
  8264. ELSIF to IS SyntaxTree.PointerType THEN
  8265. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8266. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8267. & (~to.isRealtime OR this.isRealtime);
  8268. ELSIF to IS SyntaxTree.ProcedureType THEN
  8269. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8270. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8271. & (~to.isRealtime OR this.isRealtime)
  8272. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8273. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8274. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8275. ELSIF to IS SyntaxTree.RecordType THEN
  8276. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8277. ELSIF to IS SyntaxTree.ArrayType THEN
  8278. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8279. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8280. ELSIF StaticArrayCompatible(to, this) THEN
  8281. result := TRUE
  8282. ELSE
  8283. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8284. END;
  8285. ELSIF to IS SyntaxTree.MathArrayType THEN
  8286. IF this IS SyntaxTree.MathArrayType THEN
  8287. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8288. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8289. result := TRUE;
  8290. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8291. result := TRUE;
  8292. ELSE
  8293. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8294. END;
  8295. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8296. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8297. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8298. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8299. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8300. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8301. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8302. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8303. ELSE
  8304. result := FALSE
  8305. END;
  8306. (* an array-structured object type is compatible to the type of its array structure *)
  8307. ELSIF IsArrayStructuredObjectType(this) THEN
  8308. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8309. ELSE
  8310. result := FALSE;
  8311. END;
  8312. ELSIF to IS SyntaxTree.StringType THEN
  8313. result := FALSE;
  8314. ELSIF to IS SyntaxTree.EnumerationType THEN
  8315. result := IsEnumerationExtension(this,to);
  8316. ELSIF to IS SyntaxTree.PortType THEN
  8317. result := SameType(to, this)
  8318. ELSE
  8319. Printout.Info("CompatibleTo",to);
  8320. HALT(100); (* implement missing type check *)
  8321. END;
  8322. END;
  8323. RETURN result
  8324. END CompatibleTo;
  8325. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8326. VAR actualBase, formalBase: SyntaxTree.Type;
  8327. BEGIN
  8328. IF SameType(formal,actual) THEN
  8329. RETURN TRUE
  8330. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8331. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8332. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8333. RETURN
  8334. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8335. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8336. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8337. & StaticArrayCompatible(formalBase,actualBase)
  8338. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8339. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8340. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8341. RETURN
  8342. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8343. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8344. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8345. & StaticArrayCompatible(formalBase,actualBase)
  8346. ELSE RETURN FALSE
  8347. END;
  8348. END StaticArrayCompatible;
  8349. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8350. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8351. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8352. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8353. BEGIN
  8354. result := SameType(formal,actual);
  8355. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8356. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8357. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8358. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8359. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8360. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8361. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8362. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8363. & TC(formalBase, actualBase);
  8364. END;
  8365. RETURN result
  8366. END TC;
  8367. BEGIN
  8368. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8369. ELSE
  8370. arrayBase := formalType.arrayBase.resolved;
  8371. IF (actualType IS SyntaxTree.StringType) THEN
  8372. result := arrayBase IS SyntaxTree.CharacterType
  8373. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8374. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8375. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8376. result := TC(formalType, actualType);
  8377. ELSE
  8378. result := (arrayBase IS SyntaxTree.ByteType)
  8379. END;
  8380. END;
  8381. RETURN result
  8382. END OpenArrayCompatible;
  8383. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8384. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8385. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8386. BEGIN
  8387. IF actualType IS SyntaxTree.MathArrayType THEN
  8388. actualArray := actualType(SyntaxTree.MathArrayType);
  8389. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8390. (*
  8391. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8392. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8393. *)
  8394. actualBase := ArrayBase(actualType,Infinity);
  8395. formalBase := ArrayBase(formalType,Infinity);
  8396. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8397. ELSE
  8398. (*
  8399. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8400. *)
  8401. formalBase := Resolved(formalType.arrayBase);
  8402. actualBase := Resolved(actualArray.arrayBase);
  8403. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8404. (*
  8405. ARRAY [k] -> ARRAY [n]
  8406. *)
  8407. result := (formalType.staticLength = actualArray.staticLength)
  8408. ELSE
  8409. result := TRUE
  8410. END;
  8411. IF ~result THEN
  8412. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8413. ELSIF actualBase = NIL THEN result := FALSE
  8414. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8415. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8416. ELSE
  8417. result := SameType(formalBase,actualBase)
  8418. END;
  8419. END;
  8420. ELSE
  8421. result := FALSE
  8422. END;
  8423. RETURN result
  8424. END MathArrayCompatible;
  8425. (**
  8426. Math Array Type distance for assignments / parameter passings of the form
  8427. from -> to
  8428. variants:
  8429. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8430. allowed:
  8431. static -> static (& size match)
  8432. static -> open
  8433. static -> tensor
  8434. open -> open
  8435. open -> tensor
  8436. open -> static
  8437. tensor -> tensor
  8438. tensor -> open
  8439. tensor -> static
  8440. **)
  8441. (*! think about the metric here: is form matching more important than element type matching? *)
  8442. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8443. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8444. BEGIN
  8445. fromBase := Resolved(from.arrayBase);
  8446. toBase := Resolved(to.arrayBase);
  8447. i := Infinity;
  8448. IF from = to THEN
  8449. i := 0;
  8450. ELSIF (from.form = to.form) THEN
  8451. (* static -> static, open -> open, tensor -> tensor *)
  8452. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8453. IF fromBase = toBase THEN i := 0
  8454. ELSIF toBase = NIL THEN i := 1
  8455. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8456. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8457. ELSE
  8458. i := TypeDistance(system,fromBase, toBase, varpar);
  8459. END;
  8460. END;
  8461. ELSIF (to.form = SyntaxTree.Static) THEN
  8462. (* forbidden *)
  8463. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8464. (* static -> tensor, open -> tensor, tensor -> open *)
  8465. IF toBase=fromBase THEN i := 0;
  8466. ELSIF toBase = NIL THEN i := 1;
  8467. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8468. toBase := ArrayBase(toBase,Infinity);
  8469. IF (fromBase=toBase) THEN i := 0
  8470. ELSIF (toBase = NIL) THEN i:= 1
  8471. ELSIF (fromBase = NIL) THEN i := Infinity;
  8472. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8473. END;
  8474. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8475. fromBase := ArrayBase(fromBase,Infinity);
  8476. IF (fromBase=toBase) THEN i := 0
  8477. ELSIF (toBase = NIL) THEN i := 1
  8478. ELSIF (fromBase = NIL) THEN i := Infinity;
  8479. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8480. END;
  8481. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8482. END;
  8483. IF i # Infinity THEN INC(i,2) END;
  8484. ELSIF (from.form = SyntaxTree.Static) THEN
  8485. (* static -> open *)
  8486. IF toBase=fromBase THEN i := 0
  8487. ELSIF toBase = NIL THEN i := 1
  8488. ELSIF fromBase = NIL THEN i := Infinity
  8489. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8490. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8491. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8492. END;
  8493. IF i # Infinity THEN INC(i,1) END;
  8494. ELSE HALT(100); (* unknown case *)
  8495. END;
  8496. RETURN i;
  8497. END MathArrayTypeDistance;
  8498. (** compute and return the distance of two array types
  8499. - return the distance of the base types
  8500. **)
  8501. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8502. VAR i: LONGINT;
  8503. BEGIN
  8504. i := Infinity;
  8505. IF from = to THEN
  8506. i := 0
  8507. ELSE
  8508. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8509. (*
  8510. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8511. i := TypeDistance(from.base, to.base);
  8512. IF i >= 0 THEN INC(i) END
  8513. ELSIF (from.mode = open) & (to.mode = open) THEN
  8514. i := TypeDistance(from.base, to.base);
  8515. *)
  8516. END;
  8517. RETURN i
  8518. END ArrayTypeDistance;
  8519. (** compute the signature distance of a procedure and an actual parameter list
  8520. - if any of the parameters are not compatible, the result is infinite
  8521. - add up and return the distance over all parameters
  8522. **)
  8523. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8524. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8525. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8526. BEGIN
  8527. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8528. result := Infinity
  8529. ELSE
  8530. formalParameter := procedureType.firstParameter;
  8531. i := 0;
  8532. result := 0;
  8533. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8534. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8535. actualParameter := actualParameters.GetExpression(i);
  8536. ASSERT(formalParameter.type # NIL);
  8537. IF (actualParameter.type = NIL) THEN distance := Infinity
  8538. ELSE
  8539. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8540. END;
  8541. IF distance = Infinity THEN
  8542. result := Infinity;
  8543. ELSE
  8544. to := formalParameter.type.resolved;
  8545. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8546. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8547. (* already handled varpar *)
  8548. (*
  8549. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8550. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8551. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8552. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8553. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8554. END;
  8555. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8556. result := Infinity
  8557. END;
  8558. *)
  8559. INC(result, distance);
  8560. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8561. INC(result, distance);
  8562. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8563. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8564. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8565. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8566. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8567. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8568. END;
  8569. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8570. result := Infinity
  8571. END;
  8572. ELSE
  8573. result := Infinity
  8574. END;
  8575. ELSE
  8576. INC(result,distance);
  8577. END;
  8578. END;
  8579. (*
  8580. Printout.Info("actual=", actualParameter);
  8581. Printout.Info("formal=", formalParameter);
  8582. TRACE(result);
  8583. *)
  8584. formalParameter := formalParameter.nextParameter; INC(i);
  8585. END;
  8586. END;
  8587. ASSERT(result >= 0);
  8588. RETURN result
  8589. END Distance;
  8590. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8591. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8592. BEGIN
  8593. IF right.numberParameters # (procedureType.numberParameters) THEN
  8594. result := Infinity
  8595. ELSE
  8596. formalParameter := procedureType.firstParameter;
  8597. rightParameter := right.firstParameter;
  8598. i := 0;
  8599. result := 0;
  8600. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8601. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8602. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8603. IF distance = Infinity THEN
  8604. result := Infinity;
  8605. ELSE
  8606. INC(result,distance);
  8607. END;
  8608. formalParameter := formalParameter.nextParameter;
  8609. rightParameter := rightParameter.nextParameter;
  8610. END;
  8611. END;
  8612. ASSERT(result >= 0);
  8613. RETURN result
  8614. END ProcedureTypeDistance;
  8615. (** compute and return the distance between two types, used for computation of signature distance
  8616. from -> to
  8617. **)
  8618. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8619. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8620. BEGIN
  8621. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8622. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8623. END;
  8624. i := Infinity;
  8625. IF from = to THEN
  8626. i := 0
  8627. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8628. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8629. i := Infinity;
  8630. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8631. i := 10;
  8632. ELSIF (from IS SyntaxTree.StringType) THEN
  8633. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8634. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8635. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8636. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8637. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8638. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8639. i := 1
  8640. ELSIF (from IS SyntaxTree.NilType) THEN
  8641. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8642. (*
  8643. ELSIF (from = NoType) THEN
  8644. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8645. *)
  8646. ELSIF (from IS SyntaxTree.BasicType) THEN
  8647. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8648. IF varpar & (i # 0) THEN i := Infinity END;
  8649. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8650. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8651. ELSIF (from IS SyntaxTree.RecordType) THEN
  8652. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8653. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8654. IF to IS SyntaxTree.MathArrayType THEN
  8655. (*
  8656. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8657. i := Infinity;
  8658. ELSE
  8659. *)
  8660. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8661. (*
  8662. END;
  8663. *)
  8664. END
  8665. ELSIF (from IS SyntaxTree.PointerType) THEN
  8666. ptr := from(SyntaxTree.PointerType);
  8667. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8668. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8669. (* ELSE i := TypeDistance(ptr.base, to); *)
  8670. END
  8671. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8672. IF (to IS SyntaxTree.ProcedureType) THEN
  8673. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8674. END;
  8675. ELSIF (from IS SyntaxTree.PortType) THEN
  8676. IF (to IS SyntaxTree.PortType) THEN
  8677. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8678. i := 0;
  8679. END;
  8680. END;
  8681. (*no procedure test, procedure must be the same*)
  8682. END;
  8683. RETURN i
  8684. END TypeDistance;
  8685. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8686. BEGIN
  8687. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8688. END IsIntegerType;
  8689. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8690. BEGIN
  8691. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8692. END IsAddressType;
  8693. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8694. BEGIN
  8695. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8696. END IsSizeType;
  8697. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8698. BEGIN
  8699. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8700. END IsSignedIntegerType;
  8701. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8702. BEGIN
  8703. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8704. END IsUnsignedIntegerType;
  8705. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8706. VAR result: BOOLEAN;
  8707. BEGIN
  8708. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8709. value := x.resolved(SyntaxTree.IntegerValue).value;
  8710. result := TRUE
  8711. ELSE
  8712. result := FALSE
  8713. END;
  8714. RETURN result
  8715. END IsIntegerValue;
  8716. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8717. VAR result: BOOLEAN;
  8718. BEGIN
  8719. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8720. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8721. result := TRUE
  8722. ELSE
  8723. result := FALSE
  8724. END;
  8725. RETURN result
  8726. END IsEnumerationValue;
  8727. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8728. VAR result: BOOLEAN;
  8729. BEGIN
  8730. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8731. value := x.resolved(SyntaxTree.RealValue).value;
  8732. result := TRUE
  8733. ELSE
  8734. result := FALSE
  8735. END;
  8736. RETURN result
  8737. END IsRealValue;
  8738. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8739. VAR result: BOOLEAN;
  8740. BEGIN
  8741. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8742. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8743. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8744. result := TRUE
  8745. ELSE
  8746. result := FALSE
  8747. END;
  8748. RETURN result
  8749. END IsComplexValue;
  8750. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8751. VAR result: BOOLEAN;
  8752. BEGIN
  8753. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8754. value := x.resolved(SyntaxTree.CharacterValue).value;
  8755. result := TRUE
  8756. ELSE
  8757. result := FALSE
  8758. END;
  8759. RETURN result
  8760. END IsCharacterValue;
  8761. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8762. VAR result: BOOLEAN;
  8763. BEGIN
  8764. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8765. value := x.resolved(SyntaxTree.BooleanValue).value;
  8766. result := TRUE
  8767. ELSE
  8768. result := FALSE
  8769. END;
  8770. RETURN result
  8771. END IsBooleanValue;
  8772. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8773. VAR result: BOOLEAN;
  8774. BEGIN
  8775. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8776. value := x.resolved(SyntaxTree.SetValue).value;
  8777. result := TRUE
  8778. ELSE
  8779. result := FALSE
  8780. END;
  8781. RETURN result
  8782. END IsSetValue;
  8783. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8784. VAR result: BOOLEAN;
  8785. BEGIN
  8786. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8787. value := x.resolved(SyntaxTree.StringValue).value;
  8788. result := TRUE
  8789. ELSE
  8790. result := FALSE
  8791. END;
  8792. RETURN result
  8793. END IsStringValue;
  8794. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8795. BEGIN
  8796. x := x.resolved;
  8797. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8798. END Indexable;
  8799. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8800. BEGIN
  8801. RETURN t1.SameType(t2.resolved);
  8802. END SameType;
  8803. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8804. BEGIN
  8805. IF t IS SyntaxTree.MathArrayType THEN
  8806. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8807. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8808. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8809. DEC(max);
  8810. END;
  8811. ELSIF t IS SyntaxTree.ArrayType THEN
  8812. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8813. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8814. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8815. END;
  8816. END;
  8817. RETURN t;
  8818. END ArrayBase;
  8819. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8820. BEGIN
  8821. type := type.resolved;
  8822. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8823. base := type(SyntaxTree.ArrayType).arrayBase;
  8824. RETURN TRUE;
  8825. END;
  8826. RETURN FALSE;
  8827. END IsOpenArray;
  8828. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8829. BEGIN
  8830. type := type.resolved;
  8831. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8832. base := type(SyntaxTree.ArrayType).arrayBase;
  8833. dim := type(SyntaxTree.ArrayType).staticLength;
  8834. RETURN TRUE
  8835. ELSE
  8836. RETURN FALSE
  8837. END;
  8838. END IsStaticArray;
  8839. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8840. BEGIN
  8841. type := type.resolved;
  8842. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8843. base := type(SyntaxTree.ArrayType).arrayBase;
  8844. RETURN TRUE
  8845. ELSE
  8846. RETURN FALSE
  8847. END;
  8848. END IsDynamicArray;
  8849. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8850. VAR i: LONGINT;
  8851. BEGIN
  8852. i := 0;
  8853. t := t.resolved;
  8854. IF t IS SyntaxTree.MathArrayType THEN
  8855. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8856. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8857. END;
  8858. ELSIF t IS SyntaxTree.ArrayType THEN
  8859. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8860. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8861. END;
  8862. END;
  8863. RETURN i
  8864. END Dimension;
  8865. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8866. BEGIN
  8867. RETURN expression.assignable;
  8868. END IsVariable;
  8869. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8870. BEGIN
  8871. IF (symbol IS SyntaxTree.Parameter) THEN
  8872. WITH symbol: SyntaxTree.Parameter DO
  8873. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8874. END;
  8875. ELSE
  8876. RETURN FALSE
  8877. END;
  8878. END IsVariableParameter;
  8879. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8880. VAR result: BOOLEAN;
  8881. BEGIN
  8882. IF type = NIL THEN result := FALSE
  8883. ELSE
  8884. type := type.resolved;
  8885. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8886. END;
  8887. RETURN result
  8888. END IsPointerType;
  8889. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8890. VAR result: BOOLEAN;
  8891. BEGIN
  8892. IF type = NIL THEN result := FALSE
  8893. ELSE
  8894. type := type.resolved;
  8895. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8896. END;
  8897. RETURN result
  8898. END IsUnsafePointer;
  8899. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8900. BEGIN
  8901. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8902. END IsDisposable;
  8903. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8904. VAR result: BOOLEAN;
  8905. BEGIN
  8906. IF type = NIL THEN result := FALSE
  8907. ELSE
  8908. type := type.resolved;
  8909. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8910. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8911. result := result OR (type IS SyntaxTree.ObjectType);
  8912. END;
  8913. RETURN result
  8914. END IsPointerToRecord;
  8915. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8916. VAR result: BOOLEAN;
  8917. BEGIN
  8918. IF type = NIL THEN result := FALSE
  8919. ELSE
  8920. type := type.resolved;
  8921. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8922. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8923. ;
  8924. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8925. result := result OR (type IS SyntaxTree.ObjectType);
  8926. END;
  8927. RETURN result
  8928. END IsPointerToObject;
  8929. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8930. BEGIN
  8931. IF type # NIL THEN
  8932. RETURN type.resolved.hasPointers
  8933. ELSE
  8934. RETURN FALSE
  8935. END;
  8936. END ContainsPointer;
  8937. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8938. BEGIN
  8939. IF type = NIL THEN RETURN FALSE END;
  8940. type := type.resolved;
  8941. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8942. END IsStringType;
  8943. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8944. BEGIN
  8945. IF type = NIL THEN RETURN FALSE END;
  8946. type := type.resolved;
  8947. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8948. END IsCharacterType;
  8949. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8950. BEGIN
  8951. IF type = NIL THEN RETURN FALSE END;
  8952. type := type.resolved;
  8953. RETURN (type IS SyntaxTree.EnumerationType)
  8954. END IsEnumerationType;
  8955. (** cf. section "Type extension (base type)" in the language report **)
  8956. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8957. VAR result: BOOLEAN;
  8958. BEGIN
  8959. ASSERT(base # NIL); ASSERT(extension # NIL);
  8960. base := base.resolved; extension := extension.resolved;
  8961. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8962. result := TRUE;
  8963. ELSE
  8964. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8965. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8966. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8967. END;
  8968. WHILE (extension # NIL) & (extension # base) DO
  8969. IF extension IS SyntaxTree.RecordType THEN
  8970. extension := extension(SyntaxTree.RecordType).baseType;
  8971. IF (extension # NIL) THEN extension := extension.resolved END;
  8972. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8973. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8974. END;
  8975. ELSE extension := NIL;
  8976. END;
  8977. END;
  8978. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8979. END;
  8980. RETURN result
  8981. END IsTypeExtension;
  8982. (** check if base is the base enumeration type of extension **)
  8983. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8984. BEGIN
  8985. base := base.resolved; extension := extension.resolved;
  8986. WHILE (extension # NIL) & (extension # base) DO
  8987. IF extension IS SyntaxTree.EnumerationType THEN
  8988. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8989. IF extension # NIL THEN extension := extension.resolved END;
  8990. ELSE
  8991. extension := NIL
  8992. END;
  8993. END;
  8994. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8995. END IsEnumerationExtension;
  8996. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8997. BEGIN
  8998. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8999. RETURN TRUE
  9000. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9001. RETURN TRUE
  9002. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9003. RETURN TRUE
  9004. ELSE
  9005. RETURN FALSE
  9006. END
  9007. END IsCallable;
  9008. (** compute and return the distance of two record types
  9009. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9010. **)
  9011. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9012. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9013. BEGIN
  9014. i := 0;
  9015. WHILE (from # NIL) & (from # to) DO
  9016. baseType := from.baseType;
  9017. IF (baseType # NIL) THEN
  9018. baseType := baseType.resolved;
  9019. IF baseType IS SyntaxTree.PointerType THEN
  9020. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9021. END;
  9022. IF baseType IS SyntaxTree.RecordType THEN
  9023. from := baseType(SyntaxTree.RecordType);
  9024. ELSE
  9025. from := NIL;
  9026. END;
  9027. ELSE
  9028. from := NIL
  9029. END;
  9030. INC(i)
  9031. END;
  9032. IF from = NIL THEN i := Infinity END;
  9033. RETURN i
  9034. END RecordTypeDistance;
  9035. (** compute and return the distance of two pointer types **)
  9036. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9037. BEGIN
  9038. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9039. RETURN Infinity;
  9040. ELSE
  9041. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9042. END;
  9043. END PointerTypeDistance;
  9044. (** check if expression contains a symbol designator pointing to a type declaration.
  9045. - if so then enter type declaration into typeDeclaration and return true else return false
  9046. **)
  9047. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9048. VAR result: BOOLEAN;
  9049. BEGIN
  9050. result := FALSE;
  9051. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9052. result := TRUE;
  9053. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9054. END;
  9055. RETURN result
  9056. END IsTypeDesignator;
  9057. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9058. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9059. VAR result: BOOLEAN;
  9060. BEGIN
  9061. type := type.resolved;
  9062. IF type IS SyntaxTree.PointerType THEN
  9063. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9064. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9065. result := TRUE
  9066. ELSE
  9067. result := type IS SyntaxTree.RecordType
  9068. END;
  9069. RETURN result
  9070. END IsExtensibleType;
  9071. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9072. BEGIN
  9073. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9074. (d IS SyntaxTree.SymbolDesignator) &
  9075. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9076. OR
  9077. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9078. END IsUnextensibleRecord;
  9079. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9080. BEGIN
  9081. IF IsUnextensibleRecord(d) THEN
  9082. RETURN FALSE
  9083. ELSE RETURN IsExtensibleType(d.type.resolved)
  9084. END;
  9085. END IsExtensibleDesignator;
  9086. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9087. BEGIN
  9088. type := type.resolved;
  9089. IF (type IS SyntaxTree.PointerType) THEN
  9090. RETURN TRUE
  9091. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9092. RETURN TRUE
  9093. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9094. RETURN TRUE
  9095. ELSIF (type IS SyntaxTree.BasicType) THEN
  9096. RETURN TRUE
  9097. END;
  9098. RETURN FALSE
  9099. END IsBasicType;
  9100. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9101. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9102. BEGIN
  9103. baseType := record.baseType;
  9104. IF (baseType # NIL) THEN
  9105. baseType := baseType.resolved;
  9106. IF (baseType IS SyntaxTree.PointerType) THEN
  9107. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9108. END;
  9109. END;
  9110. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9111. recordType := baseType(SyntaxTree.RecordType);
  9112. ELSE
  9113. recordType := NIL;
  9114. END;
  9115. RETURN recordType
  9116. END RecordBase;
  9117. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9118. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9119. BEGIN
  9120. baseRecord := RecordBase(scope.ownerRecord);
  9121. IF baseRecord = NIL THEN RETURN NIL END;
  9122. scope := baseRecord.recordScope;
  9123. procedureType := procedure.type.resolved;
  9124. IF procedure IS SyntaxTree.Operator THEN
  9125. operator := scope.firstOperator;
  9126. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9127. (*
  9128. Printout.Info("not same ",procedureType);
  9129. Printout.Info("with ",operator.type);
  9130. *)
  9131. operator := operator.nextOperator;
  9132. END;
  9133. super := operator;
  9134. ELSE
  9135. super := scope.firstProcedure;
  9136. WHILE (super # NIL) & (super.name # procedure.name) DO
  9137. super := super.nextProcedure;
  9138. END;
  9139. END;
  9140. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9141. RETURN super
  9142. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9143. RETURN super
  9144. ELSE
  9145. RETURN FindSuperProcedure(scope,procedure);
  9146. END;
  9147. END FindSuperProcedure;
  9148. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9149. VAR procedure: SyntaxTree.Procedure;
  9150. BEGIN
  9151. procedure := record.recordScope.constructor;
  9152. IF procedure = NIL THEN
  9153. record := RecordBase(record);
  9154. IF record # NIL THEN
  9155. procedure := GetConstructor(record)
  9156. END;
  9157. END;
  9158. RETURN procedure;
  9159. END GetConstructor;
  9160. (* enter a case into a list of cases in a sorted way and check for collision *)
  9161. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9162. VAR prev,this,new: SyntaxTree.CaseConstant;
  9163. BEGIN
  9164. this := root;
  9165. prev := NIL;
  9166. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9167. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9168. RETURN FALSE
  9169. ELSE
  9170. IF (this # NIL) & (this.min = max+1) THEN
  9171. this.min := min
  9172. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9173. prev.max := min
  9174. ELSE
  9175. NEW(new); new.min := min; new.max := max;
  9176. new.next := this;
  9177. IF prev = NIL THEN
  9178. root := new;
  9179. ELSE
  9180. prev.next := new
  9181. END
  9182. END;
  9183. RETURN TRUE
  9184. END;
  9185. END EnterCase;
  9186. (** generate and return a new checker object, errors are entered into diagnostics **)
  9187. 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;
  9188. VAR checker: Checker;
  9189. BEGIN
  9190. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9191. RETURN checker
  9192. END NewChecker;
  9193. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9194. VAR warnings: Warnings;
  9195. BEGIN
  9196. NEW(warnings, diagnostics); RETURN warnings;
  9197. END NewWarnings;
  9198. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9199. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9200. END IsRangeType;
  9201. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9202. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9203. END IsMathArrayType;
  9204. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9205. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9206. END IsArrayType;
  9207. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9208. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9209. END IsComplexType;
  9210. (** if a type is an array-structured object type *)
  9211. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9212. VAR recordType: SyntaxTree.RecordType;
  9213. BEGIN
  9214. IF type = NIL THEN
  9215. RETURN FALSE
  9216. ELSE
  9217. type := type.resolved;
  9218. IF type IS SyntaxTree.PointerType THEN
  9219. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9220. IF type IS SyntaxTree.RecordType THEN
  9221. recordType := type(SyntaxTree.RecordType);
  9222. RETURN recordType.isObject & recordType.HasArrayStructure()
  9223. ELSE
  9224. RETURN FALSE
  9225. END
  9226. ELSE
  9227. RETURN FALSE
  9228. END
  9229. END
  9230. END IsArrayStructuredObjectType;
  9231. (** the math array structure of a type
  9232. - for math arrays: the array itself
  9233. - for pointers: the math array structure of the pointer base
  9234. - for array-structured object types: the underlying structure
  9235. - for non-math arrays and all other types: NIL
  9236. **)
  9237. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9238. VAR
  9239. result: SyntaxTree.MathArrayType;
  9240. BEGIN
  9241. IF type = NIL THEN
  9242. result := NIL
  9243. ELSE
  9244. type := type.resolved;
  9245. IF type IS SyntaxTree.PointerType THEN
  9246. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9247. END;
  9248. IF type IS SyntaxTree.MathArrayType THEN
  9249. result := type(SyntaxTree.MathArrayType)
  9250. ELSIF type IS SyntaxTree.RecordType THEN
  9251. result := type(SyntaxTree.RecordType).arrayStructure
  9252. ELSE
  9253. result := NIL
  9254. END
  9255. END;
  9256. RETURN result
  9257. END MathArrayStructureOfType;
  9258. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9259. VAR
  9260. result: BOOLEAN;
  9261. rangeExpression: SyntaxTree.RangeExpression;
  9262. BEGIN
  9263. IF x IS SyntaxTree.RangeExpression THEN
  9264. rangeExpression := x(SyntaxTree.RangeExpression);
  9265. result := TRUE;
  9266. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9267. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9268. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9269. ELSE
  9270. result := FALSE
  9271. END;
  9272. RETURN result
  9273. END IsStaticRange;
  9274. (** whether a type is a math array of tensor form **)
  9275. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9276. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9277. END IsTensor;
  9278. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9279. BEGIN
  9280. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9281. length := type(SyntaxTree.MathArrayType).staticLength;
  9282. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9283. RETURN TRUE
  9284. ELSE
  9285. RETURN FALSE
  9286. END;
  9287. END IsStaticMathArray;
  9288. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9289. BEGIN
  9290. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9291. END SymbolHasAddress;
  9292. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9293. BEGIN
  9294. RETURN
  9295. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9296. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9297. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9298. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9299. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9300. ;
  9301. END HasAddress;
  9302. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9303. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9304. BEGIN
  9305. IF (e IS SyntaxTree.Designator) THEN
  9306. d := e(SyntaxTree.Designator);
  9307. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9308. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9309. e := d.left;
  9310. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9311. END;
  9312. IF d # NIL THEN
  9313. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9314. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9315. END;
  9316. END;
  9317. RETURN FALSE;
  9318. END IsLocalVariable;
  9319. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9320. BEGIN
  9321. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9322. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9323. ELSE
  9324. RETURN TRUE
  9325. END;
  9326. END IsStaticProcedure;
  9327. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9328. CONST OptimizeMethodTable = FALSE;
  9329. BEGIN
  9330. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9331. END InMethodTable;
  9332. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9333. BEGIN
  9334. IF type = NIL THEN RETURN FALSE
  9335. ELSE
  9336. type := type.resolved;
  9337. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9338. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9339. END
  9340. END ReturnedAsParameter;
  9341. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9342. BEGIN
  9343. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9344. END StructuredReturnType;
  9345. END FoxSemanticChecker.
  9346. SystemTools.FreeDownTo FoxSemanticChecker ~