FoxSemanticChecker.Mod 399 KB

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