FoxSemanticChecker.Mod 402 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  83. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  84. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  85. currentScope-: SyntaxTree.Scope;
  86. currentIsRealtime: BOOLEAN;
  87. currentIsUnreachable: BOOLEAN;
  88. currentIsCellNet: BOOLEAN;
  89. currentIsBodyProcedure: BOOLEAN;
  90. currentIsExclusive: BOOLEAN;
  91. global: SyntaxTree.ModuleScope;
  92. withEntries: WithEntry;
  93. activeCellsStatement: BOOLEAN;
  94. replacements*: Replacement;
  95. cellsAreObjects: BOOLEAN;
  96. variableAccessed: BOOLEAN;
  97. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  98. BEGIN
  99. SELF.diagnostics := diagnostics;
  100. SELF.useDarwinCCalls := useDarwinCCalls;
  101. SELF.cooperative := cooperative;
  102. SELF.system := system;
  103. SELF.symbolFileFormat := symbolFileFormat;
  104. error := FALSE;
  105. NEW(typeFixes);
  106. NEW(pointerFixes);
  107. resolvedType := NIL;
  108. resolvedExpression := NIL;
  109. resolvedStatement := NIL;
  110. currentScope := NIL;
  111. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  112. SELF.importCache := importCache;
  113. arrayBaseImported := FALSE;
  114. complexNumbersImported := FALSE;
  115. SELF.VerboseErrorMessage := verboseErrorMessage;
  116. global := NIL;
  117. phase := UndefinedPhase;
  118. currentIsRealtime := FALSE;
  119. currentIsUnreachable := FALSE;
  120. currentIsCellNet := FALSE;
  121. currentIsBodyProcedure := FALSE;
  122. currentIsExclusive := FALSE;
  123. withEntries := NIL;
  124. SELF.cellsAreObjects := system.cellsAreObjects;
  125. COPY(backend, backendName);
  126. END InitChecker;
  127. (** report error **)
  128. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  129. VAR errModule: SyntaxTree.Module;
  130. BEGIN
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  141. END Warning;
  142. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  143. VAR errorMessage: ARRAY 256 OF CHAR;
  144. BEGIN
  145. Basic.Concat(errorMessage,msg," ", msg2);
  146. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  147. error := TRUE;
  148. END ErrorSS;
  149. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  150. VAR msg, msg2: ARRAY 256 OF CHAR;
  151. BEGIN
  152. COPY(msg1, msg);
  153. Strings.Append(msg, " = ");
  154. Basic.GetString(s, msg2);
  155. Strings.Append(msg, msg2);
  156. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  157. END InfoSS;
  158. (*** symbol lookup ***)
  159. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  160. **)
  161. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  162. VAR
  163. scope,baseScope: SyntaxTree.Scope;
  164. symbol, s: SyntaxTree.Symbol;
  165. ownerRecord,base: SyntaxTree.RecordType;
  166. BEGIN
  167. scope := inScope;
  168. symbol := NIL;
  169. WHILE (scope # NIL) & (symbol = NIL) DO
  170. symbol := scope.FindSymbol(name);
  171. s := NIL;
  172. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  173. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  175. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. END;
  177. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  178. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  179. ELSE
  180. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  181. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  182. symbol.MarkUsed;
  183. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  184. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  185. base := RecordBase(ownerRecord);
  186. IF (base # NIL) THEN
  187. baseScope := base.recordScope;
  188. symbol := Find(baseScope,name,FALSE);
  189. ELSE
  190. symbol := NIL;
  191. END;
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. END;
  196. END;
  197. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  198. END;
  199. IF (symbol # NIL) THEN
  200. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  201. ASSERT(phase = DeclarationPhase);
  202. ResolveSymbol(symbol)
  203. END;
  204. symbol.MarkUsed;
  205. END;
  206. RETURN symbol
  207. END Find;
  208. (*** types ***)
  209. (** find type declaration with name qualifiedIdentifier and return resolved type
  210. - check qualified identifier prefix, set scope to module scope if appropriate
  211. - check suffix in scope
  212. **)
  213. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  214. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  215. BEGIN
  216. result := NIL;
  217. prevScope := currentScope;
  218. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  219. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  220. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  221. IF symbol(SyntaxTree.Import).module = NIL THEN
  222. Error(qualifiedIdentifier.position,"module not loaded");
  223. result := SyntaxTree.invalidType;
  224. symbol := NIL;
  225. ELSE
  226. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  227. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  228. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  229. IF VerboseErrorMessage THEN
  230. Printout.Info("scope", currentScope);
  231. Printout.Info("symbol", symbol);
  232. END;
  233. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  234. END;
  235. END;
  236. ELSE
  237. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  238. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  239. symbol := NIL;
  240. END;
  241. ELSE
  242. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  243. IF symbol = NIL THEN
  244. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  245. IF VerboseErrorMessage THEN
  246. Printout.Info("Qualident",qualifiedIdentifier);
  247. Printout.Info("in scope",currentScope) ;
  248. END;
  249. END;
  250. END;
  251. IF symbol = NIL THEN (* error already handled *)
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  255. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  256. typeDeclaration := NIL;
  257. result := SyntaxTree.invalidType;
  258. ELSE
  259. currentScope := symbol.scope;
  260. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  261. result := ResolveType(typeDeclaration.declaredType);
  262. symbol.MarkUsed;
  263. ASSERT(result # NIL);
  264. END;
  265. currentScope := prevScope;
  266. RETURN result
  267. END ResolveNamedType;
  268. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  269. If node is currently being resolved then emit a cyclic definition error.
  270. Return TRUE only if node is fully resolved.
  271. **)
  272. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  273. VAR result: BOOLEAN;
  274. BEGIN
  275. IF SyntaxTree.Resolved IN x.state THEN
  276. result := FALSE
  277. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  278. Error(x.position,"cyclic definition");
  279. result := FALSE;
  280. ELSE
  281. result := TRUE;
  282. x.SetState(SyntaxTree.BeingResolved)
  283. END;
  284. RETURN result
  285. END TypeNeedsResolution;
  286. (** Return invalid type if x is currently being resolved, return x otherwise**)
  287. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  288. BEGIN
  289. IF SyntaxTree.Resolved IN x.state THEN
  290. RETURN x
  291. ELSE
  292. RETURN SyntaxTree.invalidType
  293. END;
  294. END ResolvedType;
  295. PROCEDURE VisitType*(x: SyntaxTree.Type);
  296. BEGIN
  297. ASSERT(x = SyntaxTree.invalidType);
  298. END VisitType;
  299. (** resolve basic type **)
  300. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  301. BEGIN
  302. IF TypeNeedsResolution(x) THEN
  303. x.SetState(SyntaxTree.Resolved);
  304. END;
  305. resolvedType := ResolvedType(x)
  306. END VisitBasicType;
  307. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitByteType;
  311. (** resolve character type **)
  312. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  313. BEGIN
  314. VisitBasicType(x);
  315. END VisitCharacterType;
  316. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitBooleanType;
  320. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  321. BEGIN
  322. VisitBasicType(x);
  323. END VisitSetType;
  324. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  325. BEGIN
  326. VisitBasicType(x);
  327. END VisitAddressType;
  328. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  329. BEGIN
  330. VisitBasicType(x);
  331. END VisitSizeType;
  332. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  333. BEGIN
  334. VisitBasicType(x);
  335. END VisitAnyType;
  336. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitObjectType;
  340. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  341. BEGIN
  342. VisitBasicType(x);
  343. END VisitNilType;
  344. (** resolve integer type **)
  345. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitIntegerType;
  349. (** resolve real type **)
  350. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  351. BEGIN
  352. VisitBasicType(x);
  353. END VisitFloatType;
  354. (** resolve complex type **)
  355. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  356. BEGIN
  357. VisitBasicType(x);
  358. END VisitComplexType;
  359. (**
  360. resolve string type: nothing to be done
  361. **)
  362. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  363. BEGIN
  364. IF TypeNeedsResolution(x) THEN
  365. x.SetState(SyntaxTree.Resolved);
  366. END;
  367. resolvedType := ResolvedType(x)
  368. END VisitStringType;
  369. (**
  370. check enumeration scope: enter symbols and check for duplicate names
  371. **)
  372. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  373. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  374. BEGIN
  375. prevScope := currentScope;
  376. currentScope := x;
  377. e := x.firstConstant;
  378. nextHighest := highest;
  379. WHILE (e # NIL) DO
  380. Register(e,x,FALSE);
  381. IF SymbolNeedsResolution(e) THEN
  382. IF e.value # NIL THEN
  383. value := ConstantExpression(e.value);
  384. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  385. ELSE
  386. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  387. value.SetType(x.ownerEnumeration);
  388. END;
  389. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  390. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  391. IF nextHighest > highest THEN highest := nextHighest END;
  392. END;
  393. e.SetValue(value);
  394. CheckSymbolVisibility(e);
  395. e.SetType(x.ownerEnumeration);
  396. e.SetState(SyntaxTree.Resolved);
  397. END;
  398. e := e.nextConstant;
  399. END;
  400. currentScope := prevScope;
  401. END CheckEnumerationScope;
  402. (**
  403. resolve enumeration type: check enumeration scope
  404. **)
  405. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  406. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  407. lowest, highest: LONGINT;
  408. BEGIN
  409. IF TypeNeedsResolution(x) THEN
  410. IF x.enumerationBase # NIL THEN
  411. position := x.enumerationBase.position;
  412. baseType := ResolveType(x.enumerationBase);
  413. resolved := baseType.resolved;
  414. baseScope := NIL;
  415. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  416. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  417. Error(position, "base type is no enumeration type");
  418. ELSE
  419. enumerationBase := resolved(SyntaxTree.EnumerationType);
  420. lowest := enumerationBase.rangeHighest+1;
  421. END;
  422. x.SetEnumerationBase(baseType);
  423. ELSE lowest := 0;
  424. END;
  425. highest := lowest-1;
  426. CheckEnumerationScope(x.enumerationScope, highest);
  427. x.SetRange(lowest, highest);
  428. x.SetState(SyntaxTree.Resolved);
  429. END;
  430. resolvedType := ResolvedType(x);
  431. END VisitEnumerationType;
  432. (**
  433. resolve range type: nothing to be done
  434. **)
  435. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  436. BEGIN
  437. IF TypeNeedsResolution(x) THEN
  438. x.SetState(SyntaxTree.Resolved);
  439. END;
  440. resolvedType := ResolvedType(x)
  441. END VisitRangeType;
  442. (**
  443. resolve qualified type
  444. - find and resolve named type and set resolved type
  445. **)
  446. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  447. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  448. BEGIN
  449. IF TypeNeedsResolution(x) THEN
  450. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  451. x.SetResolved(type.resolved);
  452. x.SetState(SyntaxTree.Resolved);
  453. x.SetTypeDeclaration (typeDeclaration);
  454. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  455. x.SetResolved(SyntaxTree.invalidType);
  456. END;
  457. resolvedType := x;
  458. END VisitQualifiedType;
  459. (**
  460. resolve array type
  461. - check base type
  462. - array of math array forbidden
  463. - static array of open array forbidden
  464. **)
  465. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  466. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  467. BEGIN
  468. IF TypeNeedsResolution(x) THEN
  469. x.SetArrayBase(ResolveType(x.arrayBase));
  470. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  471. arrayBase := x.arrayBase.resolved;
  472. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  473. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  474. pointerType.SetPointerBase(arrayBase);
  475. pointerType.SetHidden(TRUE);
  476. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  477. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  478. ELSE
  479. x.SetArrayBase(pointerType);
  480. END;
  481. END;
  482. IF x.length # NIL THEN
  483. variableAccessed := FALSE;
  484. e := ResolveExpression(x.length);
  485. IF (e.resolved = NIL) THEN
  486. IF variableAccessed THEN
  487. Error(e.position, "forbidden variable access");
  488. END;
  489. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  490. ELSE
  491. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  492. END;
  493. END;
  494. IF arrayBase IS SyntaxTree.ArrayType THEN
  495. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  496. Error(x.position,"forbidden static array of dynamic array");
  497. END;
  498. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  499. Error(x.position,"forbidden array mixed form");
  500. END;
  501. x.SetHasPointers(arrayBase.hasPointers);
  502. x.SetState(SyntaxTree.Resolved);
  503. END;
  504. resolvedType := ResolvedType(x);
  505. END VisitArrayType;
  506. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  507. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  508. BEGIN
  509. module := currentScope.ownerModule;
  510. IF module.name=name THEN
  511. (* do nothing *)
  512. ELSE
  513. moduleScope := module.moduleScope;
  514. import := moduleScope.FindImport(name);
  515. IF import = NIL THEN
  516. import := SyntaxTree.NewImport(position,name,name,TRUE);
  517. moduleScope.AddImport(import);
  518. Register(import,moduleScope,FALSE);
  519. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  520. VisitImport(import);
  521. ELSIF import.direct=FALSE THEN
  522. import.SetScope(module.moduleScope);
  523. import.SetDirect(TRUE);
  524. IF moduleScope.FindSymbol(import.name) = NIL THEN
  525. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  526. duplicate.SetContext(import.context);
  527. duplicate.SetModule(import.module);
  528. Register(duplicate,moduleScope,TRUE);
  529. VisitImport(duplicate);
  530. END;
  531. END;
  532. import.MarkUsed
  533. END;
  534. END ImportModule;
  535. (**
  536. resolve math array type
  537. - check base type
  538. - open math array of array forbidden
  539. - math array of tensor forbidden
  540. - static array of open array forbidden
  541. **)
  542. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  543. VAR arrayBase: SyntaxTree.Type;
  544. BEGIN
  545. IF TypeNeedsResolution(x) THEN
  546. x.SetArrayBase(ResolveType(x.arrayBase));
  547. IF x.length # NIL THEN
  548. x.SetLength(ConstantIntegerGeq0(x.length));
  549. END;
  550. arrayBase := x.arrayBase;
  551. IF arrayBase # NIL THEN
  552. arrayBase := arrayBase.resolved;
  553. IF arrayBase = SyntaxTree.invalidType THEN
  554. (* error already handled *)
  555. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  556. Error(x.position,"forbidden array mixed form");
  557. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  558. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  559. Error(x.position,"forbidden Tensor Array mix")
  560. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  561. Error(x.position,"forbidden static array of dynamic array")
  562. END;
  563. END;
  564. IF x.form = SyntaxTree.Static THEN
  565. x.SetIncrement(system.SizeOf(arrayBase));
  566. END;
  567. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  568. END;
  569. x.SetState(SyntaxTree.Resolved);
  570. END;
  571. resolvedType := ResolvedType(x);
  572. END VisitMathArrayType;
  573. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  574. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  575. (1) Rec = RECORD ... END; Ptr <---> Rec
  576. Ptr = POINTER TO Rec; ^ |
  577. | |
  578. TypeDesc TypeDesc
  579. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  580. ^ /
  581. | /
  582. TypeDesc <-- /
  583. *)
  584. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  585. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  586. BEGIN
  587. Strings.IntToStr(x.position.start,number);
  588. COPY(prefix,name);
  589. Strings.Append(name,"@");
  590. Strings.Append(name,number);
  591. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  592. typeDeclaration.SetDeclaredType(x);
  593. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  594. x.SetTypeDeclaration(typeDeclaration);
  595. currentScope.AddTypeDeclaration(typeDeclaration);
  596. typeDeclaration.SetScope(currentScope);
  597. END AnonymousTypeDeclaration;
  598. (**
  599. deferred pointer type resolving
  600. - resolve base type
  601. - check that base type is a record or array type
  602. - if error then set base type to invalid type
  603. **)
  604. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  605. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  606. BEGIN
  607. ASSERT(type.pointerBase # NIL);
  608. position := type.pointerBase.position;
  609. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  610. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  611. (* not for pointers, a type is needed for the records only
  612. IF type.typeDeclaration = NIL THEN
  613. AnonymousTypeDeclaration(type);
  614. END;
  615. *)
  616. END;
  617. resolved := ResolveType(type.pointerBase);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  619. type.SetPointerBase(resolved);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  621. recordType := resolved.resolved(SyntaxTree.RecordType);
  622. IF recordType.isObject & (recordType.baseType # NIL) THEN
  623. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  624. Error(position, "base type of object must be a realtime object");
  625. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  626. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  627. END;
  628. END;
  629. END;
  630. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  631. Error(position, "realtime object contains references to non-realtime objects");
  632. END;
  633. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  634. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  635. Error(position, "forbidden unsafe at static array");
  636. ELS
  637. *)
  638. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  639. Error(position, "forbidden unsafe at multidimensional array");
  640. END;
  641. END;
  642. ELSE
  643. Error(position, "forbidden pointer base type");
  644. type.SetPointerBase(SyntaxTree.invalidType)
  645. END
  646. END FixPointerType;
  647. (**
  648. resolve pointer type
  649. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  650. **)
  651. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  652. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  653. modifiers: SyntaxTree.Modifier; position: Position;
  654. BEGIN
  655. IF TypeNeedsResolution(x) THEN
  656. modifiers := x.modifiers;
  657. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  658. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  659. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  660. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  661. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  662. (* inheritance cycle check
  663. example:
  664. A=POINTER TO RECORD(B) END;
  665. B=POINTER TO RECORD(A) END;
  666. *)
  667. IF x.pointerBase IS SyntaxTree.RecordType THEN
  668. recordType := x.pointerBase(SyntaxTree.RecordType);
  669. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  670. recordBaseType := ResolveType(recordType.baseType);
  671. recordType.SetBaseType(recordBaseType);
  672. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  673. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  674. END;
  675. CheckModifiers(modifiers, TRUE);
  676. typeFixes.Add(x,currentScope);
  677. x.SetState(SyntaxTree.Resolved);
  678. END;
  679. resolvedType := ResolvedType(x)
  680. END VisitPointerType;
  681. (**
  682. resolve port type
  683. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  684. **)
  685. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  686. VAR value: LONGINT;
  687. BEGIN
  688. IF TypeNeedsResolution(x) THEN
  689. x.SetCellsAreObjects(cellsAreObjects);
  690. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  691. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  692. x.SetSize(value)
  693. ELSE
  694. x.SetSize(system.SizeOf(system.longintType));
  695. END;
  696. x.SetState(SyntaxTree.Resolved);
  697. END;
  698. resolvedType := ResolvedType(x)
  699. END VisitPortType;
  700. (**
  701. deferred procedure type resolving
  702. - resolve return type
  703. - traverse and resolve parameters
  704. **)
  705. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  706. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  707. BEGIN
  708. resolved := ResolveType(procedureType.returnType);
  709. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  710. Error(procedureType.position,"forbidden open array return type");
  711. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  712. Error(procedureType.position,"procedure with return type does not return");
  713. END;
  714. procedureType.SetReturnType(resolved);
  715. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  716. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  717. parameter.SetType(procedureType.returnType);
  718. parameter.SetAccess(SyntaxTree.Hidden);
  719. parameter.SetUntraced(procedureType.hasUntracedReturn);
  720. VisitParameter(parameter);
  721. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  722. END;
  723. (* process parameters *)
  724. parameter :=procedureType.firstParameter;
  725. WHILE (parameter # NIL) DO
  726. VisitParameter(parameter);
  727. parameter := parameter.nextParameter;
  728. END;
  729. parameter := procedureType.selfParameter;
  730. IF parameter # NIL THEN
  731. VisitParameter(parameter)
  732. END;
  733. END FixProcedureType;
  734. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  735. VAR prev,this: SyntaxTree.Modifier;
  736. BEGIN
  737. this := modifiers;prev := NIL;
  738. WHILE (this # NIL) & (this.identifier # name) DO
  739. prev := this; this := this.nextModifier;
  740. END;
  741. IF this # NIL THEN
  742. IF this.expression # NIL THEN
  743. Error(this.position,"unexpected expression");
  744. END;
  745. this.Resolved;
  746. position := this.position;
  747. RETURN TRUE
  748. ELSE
  749. RETURN FALSE
  750. END;
  751. END HasFlag;
  752. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  753. VAR prev,this: SyntaxTree.Modifier;
  754. BEGIN
  755. this := modifiers;prev := NIL;
  756. WHILE (this # NIL) & (this.identifier # name) DO
  757. prev := this; this := this.nextModifier;
  758. END;
  759. IF this # NIL THEN
  760. IF this.expression = NIL THEN
  761. Error(this.position,"expected expression value");
  762. ELSE
  763. this.SetExpression(ConstantExpression(this.expression));
  764. IF CheckIntegerValue(this.expression,value) THEN END;
  765. END;
  766. this.Resolved;
  767. position := this.position;
  768. RETURN TRUE
  769. ELSE RETURN FALSE
  770. END;
  771. END HasValue;
  772. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  773. VAR prev,this: SyntaxTree.Modifier;
  774. BEGIN
  775. this := modifiers;prev := NIL;
  776. WHILE (this # NIL) & (this.identifier # name) DO
  777. prev := this; this := this.nextModifier;
  778. END;
  779. IF this # NIL THEN
  780. IF this.expression = NIL THEN
  781. Error(this.position,"expected expression value");
  782. ELSE
  783. this.SetExpression(ConstantExpression(this.expression));
  784. IF CheckStringValue(this.expression,value) THEN END;
  785. END;
  786. this.Resolved;
  787. position := this.position;
  788. RETURN TRUE
  789. ELSE RETURN FALSE
  790. END;
  791. END HasStringValue;
  792. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  793. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  794. BEGIN
  795. IF cellsAreObjects THEN RETURN FALSE END;
  796. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  797. IF svalue = "A2" THEN
  798. RETURN TRUE
  799. END;
  800. END;
  801. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  802. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  803. END;
  804. RETURN FALSE;
  805. (*
  806. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  807. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  808. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  809. IF svalue[0] = "~" THEN
  810. Strings.TrimLeft(svalue, "~");
  811. IF svalue = backendName THEN
  812. RETURN TRUE;
  813. END;
  814. ELSIF svalue # backendName THEN
  815. RETURN TRUE;
  816. END;
  817. END;
  818. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  819. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  820. END;
  821. RETURN FALSE;
  822. *)
  823. END SkipImplementation;
  824. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  825. VAR this: SyntaxTree.Modifier;
  826. BEGIN
  827. this := modifiers;
  828. WHILE this # NIL DO
  829. IF ~this.resolved THEN
  830. IF checkUse THEN
  831. Error(this.position,"unexpected modifier");
  832. ELSE
  833. this.SetExpression(ResolveExpression(this.expression));
  834. this.Resolved;
  835. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  836. END;
  837. END;
  838. this := this.nextModifier
  839. END;
  840. END CheckModifiers;
  841. (**
  842. resolve procedure type
  843. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  844. **)
  845. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  846. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  847. BEGIN
  848. IF TypeNeedsResolution(procedureType) THEN
  849. modifiers := procedureType.modifiers;
  850. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  851. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  852. procedureType.SetInterrupt(TRUE);
  853. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  854. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  855. IF useDarwinCCalls THEN (*fld*)
  856. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  857. ELSE
  858. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  859. END
  860. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  861. procedureType.SetNoReturn(TRUE);
  862. END;
  863. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  864. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  865. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  866. CheckModifiers(modifiers, TRUE);
  867. modifiers := procedureType.returnTypeModifiers;
  868. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  869. CheckModifiers(modifiers, TRUE);
  870. typeFixes.Add(procedureType,currentScope);
  871. procedureType.SetHasPointers(procedureType.isDelegate);
  872. procedureType.SetState(SyntaxTree.Resolved);
  873. END;
  874. resolvedType := ResolvedType(procedureType)
  875. END VisitProcedureType;
  876. (** check and resolve record type
  877. - check base type: must be record, math array or array-structured object type
  878. - check declarations
  879. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  880. **)
  881. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  882. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  883. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  884. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  885. hasPointers: BOOLEAN;
  886. modifiers: SyntaxTree.Modifier;
  887. value: LONGINT;
  888. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  889. BEGIN
  890. type := type.resolved;
  891. IF (type IS SyntaxTree.PointerType) &
  892. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  893. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  894. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  895. RETURN TRUE
  896. ELSE
  897. RETURN FALSE
  898. END;
  899. END IsPointerToRecord;
  900. BEGIN
  901. IF TypeNeedsResolution(x) THEN
  902. hasPointers := FALSE;
  903. modifiers := x.modifiers;
  904. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit)
  905. END;
  906. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  907. CheckModifiers(modifiers, TRUE);
  908. IF x.baseType # NIL THEN
  909. position := x.baseType.position;
  910. baseType := ResolveType(x.baseType);
  911. resolved := baseType.resolved;
  912. hasPointers := hasPointers OR resolved.hasPointers;
  913. IF x.isObject THEN (* object *)
  914. ASSERT(x.pointerType # NIL);
  915. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  916. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  917. baseType := NIL
  918. ELSIF IsPointerToRecord(resolved,recordType) THEN
  919. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  920. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  921. ELSE
  922. Error(position,"object does not extend pointer to record, object or math array ")
  923. END;
  924. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  925. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  926. ELSIF IsPointerToRecord(resolved,recordType) THEN
  927. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  928. ELSIF resolved IS SyntaxTree.RecordType THEN
  929. ELSE
  930. Error(position,"pointer to record does not extend pointer to record or record")
  931. END;
  932. ELSE
  933. IF resolved IS SyntaxTree.RecordType THEN
  934. ELSE
  935. Error(position,"record does not extend record")
  936. END;
  937. END;
  938. x.SetBaseType(baseType);
  939. IF x.Level() > 15 THEN
  940. Error(position, "record/object inheritance level too high");
  941. (* note:
  942. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  943. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  944. inheritance history of a type.
  945. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  946. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  947. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  948. *)
  949. END;
  950. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  951. Error(position, "invalid inheritance of disposable types");
  952. END;
  953. END;
  954. Declarations(x.recordScope, FALSE, {0});
  955. x.SetState(SyntaxTree.Resolved);
  956. Declarations(x.recordScope, FALSE, {1});
  957. ResolveArrayStructure(x);
  958. (* computation of sizes and offsets skipped -> done in backend / system *)
  959. recordBase := x.GetBaseRecord();
  960. IF recordBase = NIL THEN numberMethods := 0
  961. ELSE numberMethods := recordBase.recordScope.numberMethods
  962. END;
  963. isRealtime := TRUE;
  964. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  965. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  966. END;
  967. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  968. WHILE symbol # NIL DO
  969. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  970. IF symbol IS SyntaxTree.Variable THEN
  971. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  972. END;
  973. IF symbol IS SyntaxTree.Procedure THEN
  974. procedure := symbol(SyntaxTree.Procedure);
  975. IF procedure.super # NIL THEN
  976. procedure.SetMethodNumber(procedure.super.methodNumber);
  977. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  978. procedure.SetMethodNumber(numberMethods);
  979. INC(numberMethods);
  980. END;
  981. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  982. Error(procedure.position,"realtime procedure in non-realtime object")
  983. END;
  984. END;
  985. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  986. Error(symbol.position,"non-realtime symbol in realtime object")
  987. END;
  988. symbol := symbol.nextSymbol;
  989. END;
  990. IF isRealtime THEN x.SetRealtime(TRUE) END;
  991. x.recordScope.SetNumberMethods(numberMethods);
  992. (* TODO: is this needed anymore? *)
  993. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  994. Error(x.position,"object extends a record")
  995. END;
  996. IF ~x.isAbstract THEN
  997. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  998. Error(x.position, "non-abstract object contains abstract procedure");
  999. END;
  1000. ELSE
  1001. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1002. Error(x.position, "abstract object does not contain an abstract method");
  1003. END;
  1004. END;
  1005. IF (x.typeDeclaration = NIL) THEN
  1006. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1007. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1008. (*
  1009. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1010. AnonymousTypeDeclaration(x,name);
  1011. *)
  1012. ELSE
  1013. AnonymousTypeDeclaration(x,"Anonymous");
  1014. END;
  1015. END;
  1016. x.SetHasPointers(hasPointers);
  1017. x.SetState(SyntaxTree.Resolved);
  1018. END;
  1019. resolvedType := ResolvedType(x);
  1020. END VisitRecordType;
  1021. (** check and resolve cell type
  1022. - check base type: must be cell
  1023. - check declarations
  1024. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1025. **)
  1026. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1027. VAR
  1028. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1029. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1030. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1031. recordBase: SyntaxTree.RecordType;
  1032. numberMethods, int: LONGINT;
  1033. real: LONGREAL;
  1034. bool: BOOLEAN;
  1035. set: SET;
  1036. v: SyntaxTree.Expression;
  1037. str: Scanner.StringType;
  1038. atype: SyntaxTree.ArrayType;
  1039. prev: SyntaxTree.Scope;
  1040. skip: BOOLEAN;
  1041. svalue: ARRAY 32 OF CHAR;
  1042. BEGIN
  1043. IF TypeNeedsResolution(x) THEN
  1044. recordBase := NIL;
  1045. IF cellsAreObjects THEN
  1046. IF x.baseType = NIL THEN
  1047. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1048. ImportModule(qualifiedIdentifier.prefix, x.position);
  1049. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1050. x.SetBaseType(ResolveType(x.baseType));
  1051. recordBase := x.GetBaseRecord();
  1052. IF recordBase = NIL THEN
  1053. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1054. END;
  1055. ELSE
  1056. x.SetBaseType(ResolveType(x.baseType));
  1057. END;
  1058. ELSE
  1059. x.SetBaseType(ResolveType(x.baseType));
  1060. END;
  1061. IF recordBase = NIL THEN numberMethods := 0
  1062. ELSE numberMethods := recordBase.recordScope.numberMethods
  1063. END;
  1064. modifier := x.modifiers;
  1065. (*IF ~x.isCellNet THEN*)
  1066. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1067. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1068. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1069. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1070. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1071. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1072. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1073. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1074. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1075. symbol := system.activeCellsCapabilities;
  1076. WHILE symbol # NIL DO
  1077. IF HasFlag(modifier, symbol.name, position) THEN END;
  1078. symbol := symbol.nextSymbol;
  1079. END;
  1080. modifier := x.modifiers;
  1081. WHILE (modifier # NIL) DO
  1082. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1083. IF modifier.expression # NIL THEN
  1084. v := ConstantExpression(modifier.expression);
  1085. property.SetValue(v);
  1086. IF IsIntegerValue(modifier.expression, int) THEN
  1087. (*property.SetValue(modifier.expression);*)
  1088. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1089. property.SetType(system.longintType);
  1090. ELSIF IsRealValue(modifier.expression, real) THEN
  1091. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1092. property.SetType(system.longrealType);
  1093. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1094. property.SetType(system.booleanType);
  1095. ELSIF IsSetValue(modifier.expression, set) THEN
  1096. property.SetType(system.setType);
  1097. ELSIF IsStringValue(modifier.expression, str) THEN
  1098. (*property.SetValue(modifier.expression);*)
  1099. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1100. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1101. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1102. property.SetType(atype);
  1103. ELSE
  1104. Error(modifier.position, "unsupported property type");
  1105. END;
  1106. ELSE (* flag property *)
  1107. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1108. property.SetType(system.booleanType);
  1109. END;
  1110. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1111. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1112. x.AddProperty(property);
  1113. modifier := modifier.nextModifier;
  1114. END;
  1115. CheckModifiers(modifier, FALSE);
  1116. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1117. (* process parameters *)
  1118. prev := currentScope;
  1119. currentScope := x.cellScope;
  1120. parameter :=x.firstParameter;
  1121. WHILE (parameter # NIL) DO
  1122. VisitParameter(parameter);
  1123. type := parameter.type.resolved;
  1124. IF ~(type IS SyntaxTree.PortType) THEN
  1125. WHILE IsStaticArray(type, type, len) DO
  1126. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1127. END;
  1128. WHILE IsDynamicArray(type, type) DO
  1129. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1130. END;
  1131. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1132. Error(parameter.position, "invalid type, must be port or static array of port ");
  1133. END;
  1134. END;
  1135. parameter := parameter.nextParameter;
  1136. END;
  1137. currentScope := prev;
  1138. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1139. WHILE symbol # NIL DO
  1140. IF symbol IS SyntaxTree.Variable THEN
  1141. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1142. END;
  1143. symbol := symbol.nextSymbol;
  1144. END;
  1145. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1146. IF (x.typeDeclaration = NIL) THEN
  1147. AnonymousTypeDeclaration(x,"Anonymous");
  1148. END;
  1149. x.SetState(SyntaxTree.Resolved);
  1150. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1151. Warning(x.position, "Forbidden empty Body.");
  1152. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1153. Warning(x.position, "Non-empty body for an engine?");
  1154. END;
  1155. END;
  1156. resolvedType := ResolvedType(x);
  1157. END VisitCellType;
  1158. (* check if an object is an array-structured object type
  1159. - determine the array structure
  1160. - collect operators from top to bottom in the inheritance hierarchy
  1161. - check if LEN operator is declared
  1162. - determine number of possible index operators
  1163. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1164. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1165. *)
  1166. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1167. VAR
  1168. indexOperatorCount, i: LONGINT;
  1169. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1170. isTensor: BOOLEAN;
  1171. BEGIN
  1172. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1173. (* determine array structure *)
  1174. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1175. END;
  1176. IF recordType.HasArrayStructure() THEN
  1177. (* the object is an ASOT *)
  1178. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1179. (* reset array access operators *)
  1180. arrayAccessOperators.len := NIL;
  1181. arrayAccessOperators.generalRead := NIL;
  1182. arrayAccessOperators.generalWrite := NIL;
  1183. IF isTensor THEN
  1184. (* all operators of dimensionalities 1 to max *)
  1185. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1186. ELSE
  1187. (* all operators of certain dimensionality *)
  1188. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1189. END;
  1190. NEW(arrayAccessOperators.read, indexOperatorCount);
  1191. NEW(arrayAccessOperators.write, indexOperatorCount);
  1192. FOR i := 0 TO indexOperatorCount - 1 DO
  1193. arrayAccessOperators.read[i] := NIL;
  1194. arrayAccessOperators.write[i] := NIL
  1195. END;
  1196. (* collect access operators in the record scope *)
  1197. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1198. IF arrayAccessOperators.len = NIL THEN
  1199. (* TODO: think about making this operator optional for static array structures *)
  1200. Error(recordType.position, "LEN operator missing")
  1201. END;
  1202. (* show error messages *)
  1203. IF isTensor THEN
  1204. (* require ARRAY [*] OF RANGE *)
  1205. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1206. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1207. ELSE
  1208. (* forbid ARRAY [*] OF RANGE *)
  1209. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1210. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1211. (* require RANGE, RANGE, ... RANGE *)
  1212. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1213. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1214. END;
  1215. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1216. ELSE
  1217. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1218. IF recordType.recordScope.firstOperator # NIL THEN
  1219. RETURN;
  1220. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1221. END
  1222. END
  1223. END ResolveArrayStructure;
  1224. (** collect array access operators in a record scope **)
  1225. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1226. VAR
  1227. baseType: SyntaxTree.Type;
  1228. operator: SyntaxTree.Operator;
  1229. isReadOperator, isGeneralOperator: BOOLEAN;
  1230. indexListSize, indexListKind, hashValue: LONGINT;
  1231. BEGIN
  1232. (* if a parent record scope exists, collect the operators there first *)
  1233. baseType := recordScope.ownerRecord.baseType;
  1234. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1235. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1236. END;
  1237. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1238. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1239. END;
  1240. (* go through all operators in the current record scope *)
  1241. operator := recordScope.firstOperator;
  1242. WHILE operator # NIL DO
  1243. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1244. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1245. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1246. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1247. IF isGeneralOperator THEN
  1248. IF isReadOperator THEN
  1249. arrayAccessOperators.generalRead := operator
  1250. ELSE
  1251. arrayAccessOperators.generalWrite := operator
  1252. END
  1253. ELSE
  1254. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1255. IF isReadOperator THEN
  1256. arrayAccessOperators.read[hashValue] := operator
  1257. ELSE
  1258. arrayAccessOperators.write[hashValue] := operator
  1259. END
  1260. END
  1261. END
  1262. ELSE
  1263. Error(operator.position, 'invalid operator')
  1264. END;
  1265. operator := operator.nextOperator
  1266. END
  1267. END CollectArrayAccessOperators;
  1268. (** the hash value of an index operator **)
  1269. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1270. VAR result: LONGINT;
  1271. BEGIN
  1272. IF isTensor THEN
  1273. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1274. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1275. ELSE
  1276. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1277. END
  1278. ELSE
  1279. result := indexListKind
  1280. END;
  1281. RETURN result
  1282. END IndexOperatorHash;
  1283. (** 2 to the power of exponent **)
  1284. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1285. VAR result, i: LONGINT;
  1286. BEGIN
  1287. result := 1;
  1288. FOR i := 1 TO exponent DO
  1289. result := result * 2;
  1290. END;
  1291. RETURN result
  1292. END TwoToThePowerOf;
  1293. (** check if a LEN operator has a correct signature. i.e.
  1294. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1295. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1296. **)
  1297. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1298. VAR
  1299. procedureType: SyntaxTree.ProcedureType;
  1300. returnedArrayType: SyntaxTree.MathArrayType;
  1301. result: BOOLEAN;
  1302. BEGIN
  1303. result := FALSE;
  1304. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1305. IF (procedureType.numberParameters = 0) THEN
  1306. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1307. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1308. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1309. IF returnedArrayType.form = SyntaxTree.Open THEN
  1310. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1311. result := TRUE
  1312. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1313. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1314. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1315. result := TRUE
  1316. END
  1317. END
  1318. END
  1319. END
  1320. END;
  1321. IF result THEN
  1322. (* export symbol automatically *)
  1323. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1324. ELSE
  1325. Error(operator.position, "LEN operator with invalid signature");
  1326. END;
  1327. RETURN result
  1328. END CheckLenOperator;
  1329. (** check if an index operator has a correct signature. i.e.
  1330. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1331. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1332. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1333. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1334. - determine if it is a read or write operator (existance of return type)
  1335. - check index parameters
  1336. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1337. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1338. [LONGINT] -> binary 0 -> 0
  1339. [RANGE] -> binary 1 -> 1
  1340. [LONGINT, LONGINT] -> binary 00 -> 0
  1341. [LONGINT, RANGE] -> binary 01 -> 1
  1342. [RANGE, LONGINT] -> binary 10 -> 2
  1343. [RANGE, RANGE] -> binary 11 -> 3
  1344. etc.
  1345. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1346. - for read operators, check if return type matches the type of data that is read
  1347. - for write operators, check if last parameter type matches the type of data that is written
  1348. **)
  1349. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1350. VAR
  1351. elementType, otherElementType, dataType: SyntaxTree.Type;
  1352. procedureType: SyntaxTree.ProcedureType;
  1353. mathArrayType: SyntaxTree.MathArrayType;
  1354. parameter: SyntaxTree.Parameter;
  1355. parameterCount, rangeCount, i: LONGINT;
  1356. hasTypeError: BOOLEAN;
  1357. BEGIN
  1358. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1359. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1360. (* determine if it is a read or write operator *)
  1361. isReadOperator := (procedureType.returnType # NIL);
  1362. IF isReadOperator THEN
  1363. indexListSize := parameterCount;
  1364. ELSE
  1365. indexListSize := parameterCount - 1;
  1366. END;
  1367. IF indexListSize < 1 THEN
  1368. Error(operator.position, "index operator with too few parameters");
  1369. RETURN FALSE
  1370. END;
  1371. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1372. (* general operator *)
  1373. isGeneralOperator := TRUE;
  1374. IF indexListSize > 1 THEN
  1375. Error(operator.position, "index operator with too many parameters");
  1376. RETURN FALSE
  1377. END;
  1378. (* ARRAY [*] OF RANGE*)
  1379. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1380. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1381. Error(operator.position, "index parameter not dynamic math array of range");
  1382. RETURN FALSE
  1383. END;
  1384. parameter := procedureType.firstParameter.nextParameter
  1385. ELSE
  1386. (* fixed-dim. operator *)
  1387. isGeneralOperator := FALSE;
  1388. (* check number of index parameters *)
  1389. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1390. (* for tensors, limited to a certain size *)
  1391. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1392. Error(operator.position, "too many index parameters for tensor");
  1393. RETURN FALSE
  1394. END
  1395. ELSE
  1396. (* for non-tensors, depends on dimensionality *)
  1397. IF indexListSize # arrayStructure.Dimensionality() THEN
  1398. Error(operator.position, "index parameter count does not match dimensionality");
  1399. RETURN FALSE
  1400. END
  1401. END;
  1402. (* go through all index parameters
  1403. - count the number of ranges
  1404. - determine the index list kind number
  1405. *)
  1406. indexListKind := 0;
  1407. rangeCount := 0;
  1408. parameter := procedureType.firstParameter;
  1409. FOR i := 1 TO indexListSize DO
  1410. indexListKind := indexListKind * 2;
  1411. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1412. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1413. INC(indexListKind);
  1414. INC(rangeCount)
  1415. ELSE
  1416. Error(parameter.position, "integer or range expected");
  1417. RETURN FALSE
  1418. END;
  1419. parameter := parameter.nextParameter
  1420. END;
  1421. END;
  1422. (*
  1423. - for read operators: check type of last parameter
  1424. - for write operators: check return type
  1425. *)
  1426. IF isReadOperator THEN
  1427. dataType := procedureType.returnType (* the return type *)
  1428. ELSE
  1429. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1430. END;
  1431. elementType := arrayStructure.ElementType();
  1432. hasTypeError := FALSE;
  1433. IF isGeneralOperator THEN
  1434. (* ARRAY [?] OF <Element> *)
  1435. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1436. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1437. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1438. hasTypeError := TRUE
  1439. END
  1440. ELSE
  1441. hasTypeError := TRUE
  1442. END
  1443. ELSE
  1444. IF rangeCount = 0 THEN
  1445. (* <Element> *)
  1446. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1447. ELSE
  1448. (* ARRAY [*, *, ..., *] OF <Element> *)
  1449. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1450. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1451. IF mathArrayType.IsFullyDynamic() THEN
  1452. IF mathArrayType.Dimensionality() = rangeCount THEN
  1453. otherElementType := mathArrayType.ElementType();
  1454. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1455. ELSE
  1456. hasTypeError := TRUE
  1457. END
  1458. ELSE
  1459. hasTypeError := TRUE
  1460. END
  1461. ELSE
  1462. hasTypeError := TRUE
  1463. END
  1464. END
  1465. END;
  1466. IF hasTypeError THEN
  1467. IF isReadOperator THEN
  1468. Error(operator.position, "return type does not match")
  1469. ELSE
  1470. Error(parameter.position, "type of last parameter does not match")
  1471. END;
  1472. RETURN FALSE
  1473. END;
  1474. (* export symbol automatically *)
  1475. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1476. RETURN TRUE
  1477. END CheckIndexOperator;
  1478. (** resolve all pending types (late resolving).
  1479. - type fixes are resolved at the end of the declaration phase
  1480. - type fixes may imply new type fixes that are also entered at the end of the list
  1481. **)
  1482. PROCEDURE FixTypes;
  1483. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1484. BEGIN
  1485. prevScope := currentScope;
  1486. p := typeFixes.Get(currentScope);
  1487. WHILE p # NIL DO
  1488. ASSERT(currentScope # NIL);
  1489. ASSERT(p IS SyntaxTree.Type);
  1490. IF p IS SyntaxTree.PointerType THEN
  1491. FixPointerType(p(SyntaxTree.PointerType))
  1492. ELSIF p IS SyntaxTree.ProcedureType THEN
  1493. FixProcedureType(p(SyntaxTree.ProcedureType))
  1494. ELSE
  1495. HALT(100);
  1496. END;
  1497. p := typeFixes.Get(currentScope);
  1498. END;
  1499. currentScope :=prevScope;
  1500. END FixTypes;
  1501. (**
  1502. resolve type x
  1503. - if x is nil then return nil
  1504. - if x cannot be resolved then the result is invalidType else the result is x
  1505. - the resolved type is entered into x.resolved
  1506. **)
  1507. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1508. VAR prev,resolved: SyntaxTree.Type;
  1509. BEGIN
  1510. prev := resolvedType;
  1511. resolvedType := SyntaxTree.invalidType;
  1512. IF x = NIL THEN resolvedType := NIL
  1513. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1514. END;
  1515. resolved := resolvedType;
  1516. resolvedType := prev;
  1517. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1518. RETURN resolved
  1519. END ResolveType;
  1520. (*** compatibility rules ***)
  1521. (**
  1522. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1523. **)
  1524. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1525. VAR result: SyntaxTree.Type;
  1526. BEGIN
  1527. result := SyntaxTree.invalidType;
  1528. IF type = NIL THEN Error(position, "expression of type NIL");
  1529. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1530. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1531. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1532. ELSE result := type.resolved
  1533. END;
  1534. RETURN result
  1535. END RegularType;
  1536. (** returns signature compatibility of procedure types this and to
  1537. - if not compatible then error is reported
  1538. - compatibility means type equality
  1539. **)
  1540. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1541. VAR result: BOOLEAN;
  1542. BEGIN
  1543. result := SameType(to,this);
  1544. IF ~result THEN
  1545. Error(position, "signature incompatible");
  1546. IF VerboseErrorMessage THEN
  1547. Printout.Info("this",this);
  1548. Printout.Info("to",to);
  1549. END;
  1550. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1551. Error(position, "signature incompatible: realtime flag must be inherited");
  1552. END;
  1553. RETURN result
  1554. END SignatureCompatible;
  1555. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1556. - for var parameters compatibility means same type except for
  1557. - formal is of open array of system byte
  1558. - formal is of record type
  1559. - formal is of open array type
  1560. - formal is of open math array type
  1561. - for value parameters compatibllity means assignment compatibility except for
  1562. - formal is of open array type
  1563. if compatible the return true else report error and return false
  1564. **)
  1565. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1566. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1567. BEGIN
  1568. formalType := RegularType(formal.position,formal.type);
  1569. actualType := RegularType(actual.position,actual.type);
  1570. error := FALSE;
  1571. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1572. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1573. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1574. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1575. END;
  1576. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1577. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1578. ELSIF ~IsVariable(actual) THEN
  1579. result := FALSE; error := TRUE;
  1580. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1581. Error(actual.position,"not a variable: no operator for writing");
  1582. ELSE
  1583. Error(actual.position,"is not a variable");
  1584. END;
  1585. IF VerboseErrorMessage THEN
  1586. Printout.Info("actual",actual);
  1587. Printout.Info("formal",formal);
  1588. END;
  1589. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1590. result := CompatibleTo(system,actualType,formalType);
  1591. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1592. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1593. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1594. IF IsArrayStructuredObjectType(actualType) THEN
  1595. actualType := MathArrayStructureOfType(actualType)
  1596. END;
  1597. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1598. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1599. Error(actual.position,"incompatible non-static actual type");
  1600. END;
  1601. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1602. Error(actual.position,"incompatible tensor (use a range expression)");
  1603. END;
  1604. ELSE
  1605. result := SameType(actualType,formalType)
  1606. END
  1607. ELSE
  1608. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1609. actualType := system.characterType;
  1610. END;
  1611. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1612. result := TRUE; (* special rule for WINAPI parameters *)
  1613. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1614. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1615. ELSE
  1616. result := CompatibleTo(system,actualType,formalType);
  1617. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1618. Error(actual.position,"incompatible non-static actual type");
  1619. END;
  1620. END;
  1621. END;
  1622. IF ~result & ~error THEN
  1623. Error(actual.position,"incompatible parameter");
  1624. IF VerboseErrorMessage THEN
  1625. Printout.Info("actual",actual);
  1626. Printout.Info("formal",formal);
  1627. END;
  1628. END;
  1629. RETURN result
  1630. END ParameterCompatible;
  1631. (** check compatibility for expressions of the form left := right
  1632. - if compatible then return true else error report and return false
  1633. - check if left is variable
  1634. - check compatiblity
  1635. **)
  1636. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1637. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1638. BEGIN
  1639. result := FALSE;
  1640. leftType := RegularType(left.position,left.type);
  1641. rightType := RegularType(right.position,right.type);
  1642. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1643. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1644. END;
  1645. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1646. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1647. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1648. (* error already handled *)
  1649. result := TRUE;
  1650. ELSIF ~IsVariable(left) THEN
  1651. Error(left.position,"is not a variable");
  1652. IF VerboseErrorMessage THEN
  1653. Printout.Info("left",left);
  1654. Printout.Info("right",right);
  1655. END;
  1656. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1657. result := TRUE;
  1658. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1659. result := TRUE
  1660. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1661. Error(left.position,"incompatible assignment");
  1662. IF VerboseErrorMessage THEN
  1663. Printout.Info("left",left);
  1664. Printout.Info("right",right);
  1665. END;
  1666. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1667. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1668. Error(right.position,"forbidden assignment of a nested procedure");
  1669. ELSE
  1670. result := TRUE
  1671. END;
  1672. RETURN result
  1673. END AssignmentCompatible;
  1674. (*** values ***)
  1675. (** check and resolve integer value **)
  1676. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1677. VAR hugeint: HUGEINT;
  1678. BEGIN
  1679. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1680. value.SetType(Global.GetIntegerType(system,hugeint));
  1681. resolvedExpression := value
  1682. END VisitIntegerValue;
  1683. (** check and resolve real value **)
  1684. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1685. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1686. BEGIN
  1687. subtype := value(SyntaxTree.RealValue).subtype;
  1688. IF subtype = Scanner.Real THEN
  1689. type := system.realType
  1690. ELSIF subtype = Scanner.Longreal THEN
  1691. type := system.longrealType
  1692. ELSE
  1693. HALT(100)
  1694. END;
  1695. value.SetType(type);
  1696. resolvedExpression := value
  1697. END VisitRealValue;
  1698. (** check and resolve complex value **)
  1699. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1700. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1701. BEGIN
  1702. subtype := value(SyntaxTree.ComplexValue).subtype;
  1703. IF subtype = Scanner.Real THEN
  1704. type := system.complexType
  1705. ELSIF subtype = Scanner.Longreal THEN
  1706. type := system.longcomplexType
  1707. ELSE
  1708. HALT(100)
  1709. END;
  1710. value.SetType(type);
  1711. resolvedExpression := value
  1712. END VisitComplexValue;
  1713. (** check and resolve set value **)
  1714. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1715. BEGIN
  1716. value.SetType(system.setType);
  1717. resolvedExpression := value
  1718. END VisitSetValue;
  1719. (** check and resolve set value **)
  1720. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1721. BEGIN
  1722. value.SetType(SyntaxTree.invalidType);
  1723. resolvedExpression := value
  1724. END VisitMathArrayValue;
  1725. (** check and resolve boolean value **)
  1726. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1727. BEGIN
  1728. value.SetType(system.booleanType);
  1729. resolvedExpression := value
  1730. END VisitBooleanValue;
  1731. (** check and resolve string value **)
  1732. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1733. BEGIN
  1734. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1735. resolvedExpression := value
  1736. END VisitStringValue;
  1737. (** check and resolve character value **)
  1738. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1739. BEGIN
  1740. value.SetType(system.characterType);
  1741. resolvedExpression := value
  1742. END VisitCharacterValue;
  1743. (** check and resolve nil value **)
  1744. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1745. BEGIN
  1746. value.SetType(system.nilType);
  1747. resolvedExpression := value
  1748. END VisitNilValue;
  1749. (** check and resolve enumerator value **)
  1750. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1751. BEGIN
  1752. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1753. ASSERT(value.type # NIL);
  1754. resolvedExpression := value
  1755. END VisitEnumerationValue;
  1756. (*** expressions ***)
  1757. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1758. - check all elements on integer type
  1759. - if element range is constant, then check lower and upper bound
  1760. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1761. if an error occurs then report error and return invalidExpression
  1762. **)
  1763. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1764. VAR
  1765. i: LONGINT;
  1766. element: SyntaxTree.Expression;
  1767. constant: BOOLEAN;
  1768. elements: SyntaxTree.ExpressionList;
  1769. s: SET;
  1770. result: SyntaxTree.Expression;
  1771. value: SyntaxTree.Value;
  1772. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1773. VAR
  1774. left, right: SyntaxTree.Expression;
  1775. elementResult: SyntaxTree.Expression;
  1776. leftInteger, rightInteger, temp: LONGINT;
  1777. BEGIN
  1778. (* set context of range *)
  1779. IF element IS SyntaxTree.RangeExpression THEN
  1780. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1781. END;
  1782. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1783. IF elementResult = SyntaxTree.invalidExpression THEN
  1784. (* error already reported *)
  1785. constant := FALSE
  1786. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1787. (* the element is a range expression *)
  1788. (* extract left and right hand side of range *)
  1789. left := elementResult(SyntaxTree.RangeExpression).first;
  1790. right := elementResult(SyntaxTree.RangeExpression).last;
  1791. (* guaranteed by VisitRangeExpression: *)
  1792. ASSERT((left # NIL) & (right # NIL));
  1793. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1794. ELSE
  1795. (* the element is not a range expression *)
  1796. (* check type and add conversion if needed *)
  1797. IF IsIntegerType(elementResult.type.resolved) THEN
  1798. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1799. ELSE
  1800. Error(elementResult.position, "non integer element in set");
  1801. elementResult := SyntaxTree.invalidExpression;
  1802. constant := FALSE
  1803. END;
  1804. left := elementResult;
  1805. right := elementResult
  1806. END;
  1807. IF elementResult # SyntaxTree.invalidExpression THEN
  1808. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1809. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1810. Error(left.position,"not allowed set integer value");
  1811. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1812. Error(right.position,"not allowed set integer value");
  1813. END
  1814. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1815. Error(right.position,"not allowed set integer value");
  1816. ELSE
  1817. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1818. s := {};
  1819. ELSE
  1820. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1821. IF leftInteger < 0 THEN leftInteger := 0 END;
  1822. (*!!!!!!!!! this is a hack !!!!!!! *)
  1823. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1824. extends the range x..31 to x..63 !!!!!! *)
  1825. s := s + {leftInteger..rightInteger};
  1826. END;
  1827. END;
  1828. ELSE
  1829. constant := FALSE;
  1830. END
  1831. END;
  1832. RETURN elementResult
  1833. END CheckElement;
  1834. BEGIN
  1835. result := set; constant := TRUE; s := {}; elements := set.elements;
  1836. IF elements # NIL THEN
  1837. FOR i := 0 TO elements.Length()-1 DO
  1838. element := elements.GetExpression(i);
  1839. element := CheckElement(element);
  1840. IF element = SyntaxTree.invalidExpression THEN
  1841. result := SyntaxTree.invalidExpression
  1842. END;
  1843. elements.SetExpression(i,element);
  1844. END;
  1845. END;
  1846. IF constant THEN
  1847. value := SyntaxTree.NewSetValue(set.position,s);
  1848. value.SetType(system.setType);
  1849. result.SetResolved(value);
  1850. END;
  1851. (* optimization possible
  1852. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1853. left this to the programmer...
  1854. *)
  1855. result.SetType(system.setType);
  1856. resolvedExpression := result;
  1857. END VisitSet;
  1858. (*
  1859. old variant: quite generic but needs better conversion handling, do this?
  1860. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1861. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1862. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1863. BEGIN
  1864. type := NIL;
  1865. numberElements := x.elements.Length();
  1866. FOR i := 0 TO numberElements-1 DO
  1867. expression := x.elements.GetExpression(i);
  1868. position := expression.position;
  1869. expression := ResolveExpression(x.elements.GetExpression(i));
  1870. x.elements.SetExpression(i,de);
  1871. IF type = NIL THEN
  1872. type := expression.type;
  1873. ELSIF CompatibleTo(system,expression.type,type) THEN
  1874. (* ok *)
  1875. ELSIF CompatibleTo(system,type,expression.type) THEN
  1876. type := expression.type
  1877. ELSE
  1878. Error(expression.position, "incompatible element types");
  1879. type := SyntaxTree.invalidType;
  1880. END;
  1881. END;
  1882. isValue := TRUE;
  1883. FOR i := 0 TO numberElements-1 DO
  1884. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1885. x.elements.SetExpression(i,expression);
  1886. isValue := isValue & (expression.resolved # NIL);
  1887. END;
  1888. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1889. arrayType.SetArrayBase(type);
  1890. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1891. IF isValue THEN
  1892. value := SyntaxTree.NewMathArrayValue(position);
  1893. value.SetElements(x.elements);
  1894. x.SetResolved(value);
  1895. END;
  1896. x.SetType(arrayType);
  1897. resolvedExpression := x;
  1898. END VisitMathArrayExpression;
  1899. *)
  1900. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1901. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1902. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1903. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1904. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1905. BEGIN
  1906. numberElements := x.elements.Length();
  1907. FOR i := 0 TO numberElements-1 DO
  1908. expression := x.elements.GetExpression(i);
  1909. IF expression IS SyntaxTree.MathArrayExpression THEN
  1910. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1911. ELSE
  1912. position := expression.position;
  1913. expression := ResolveExpression(x.elements.GetExpression(i));
  1914. x.elements.SetExpression(i,expression);
  1915. IF type = NIL THEN
  1916. type := expression.type;
  1917. ELSIF CompatibleTo(system,expression.type,type) THEN
  1918. (* ok *)
  1919. ELSIF CompatibleTo(system,type,expression.type) THEN
  1920. type := expression.type
  1921. ELSE
  1922. Error(expression.position, "incompatible element types");
  1923. type := SyntaxTree.invalidType;
  1924. END;
  1925. END;
  1926. END;
  1927. END RecursivelyFindType;
  1928. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1929. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1930. BEGIN
  1931. numberElements := x.elements.Length();
  1932. FOR i := 0 TO numberElements-1 DO
  1933. expression := x.elements.GetExpression(i);
  1934. IF expression IS SyntaxTree.MathArrayExpression THEN
  1935. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1936. ELSE
  1937. position := expression.position;
  1938. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1939. x.elements.SetExpression(i,expression);
  1940. isValue := isValue & (expression.resolved # NIL);
  1941. END;
  1942. END;
  1943. END RecursivelySetExpression;
  1944. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1945. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1946. arrayType: SyntaxTree.MathArrayType;
  1947. BEGIN
  1948. numberElements := x.elements.Length();
  1949. baseType := NIL;
  1950. gsize := 0;
  1951. FOR i := 0 TO numberElements-1 DO
  1952. expression := x.elements.GetExpression(i);
  1953. IF expression IS SyntaxTree.MathArrayExpression THEN
  1954. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1955. IF i=0 THEN
  1956. gsize := size;
  1957. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1958. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1959. ELSE expression.SetType(baseType)
  1960. END;
  1961. ELSIF baseType = NIL THEN baseType := type;
  1962. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1963. END;
  1964. END;
  1965. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1966. arrayType.SetArrayBase(baseType);
  1967. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1968. RETURN ResolveType(arrayType);
  1969. END RecursivelySetType;
  1970. BEGIN
  1971. type := NIL;
  1972. RecursivelyFindType(x);
  1973. isValue := TRUE;
  1974. RecursivelySetExpression(x);
  1975. arrayType := RecursivelySetType(x);
  1976. x.SetType(arrayType);
  1977. IF isValue THEN
  1978. value := SyntaxTree.NewMathArrayValue(x.position);
  1979. value.SetArray(x);
  1980. x.SetResolved(value);
  1981. value.SetType(arrayType);
  1982. END;
  1983. x.SetType(arrayType);
  1984. resolvedExpression := x;
  1985. END VisitMathArrayExpression;
  1986. (** check and resolve unary expression **)
  1987. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1988. VAR
  1989. left: SyntaxTree.Expression;
  1990. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1991. bool: BOOLEAN;
  1992. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1993. value: SyntaxTree.Value;
  1994. BEGIN
  1995. type := SyntaxTree.invalidType;
  1996. left := ResolveExpression(unaryExpression.left);
  1997. unaryExpression.SetLeft(left);
  1998. operator := unaryExpression.operator;
  1999. result := unaryExpression;
  2000. IF ~system.operatorDefined[operator] THEN
  2001. Error(left.position,"Operator Not Defined");
  2002. RETURN
  2003. ELSIF left.type = NIL THEN
  2004. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2005. resolvedExpression := SyntaxTree.invalidExpression;
  2006. RETURN
  2007. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2008. RETURN
  2009. END;
  2010. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2011. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2012. END;
  2013. IF operatorCall # NIL THEN
  2014. result := operatorCall;
  2015. type := operatorCall.type;
  2016. (* admissible operators
  2017. Minus number, set
  2018. Not boolean
  2019. *)
  2020. ELSE
  2021. CASE unaryExpression.operator OF
  2022. |Scanner.Minus:
  2023. IF IsIntegerType(left.type.resolved) THEN
  2024. IF left.resolved # NIL THEN
  2025. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2026. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2027. result.SetResolved(value);
  2028. type := Global.GetIntegerType(system,int);
  2029. value.SetType(type);
  2030. ELSE
  2031. type := left.type
  2032. END
  2033. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2034. IF IsRealValue(left,real) THEN
  2035. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2036. result.SetResolved(value);
  2037. type := left.type;
  2038. value.SetType(type);
  2039. ELSE
  2040. type := left.type;
  2041. END;
  2042. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2043. IF IsSetValue(left,set) THEN
  2044. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2045. result.SetResolved(value);
  2046. type := left.type;
  2047. value.SetType(type);
  2048. ELSE
  2049. type := left.type;
  2050. END;
  2051. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2052. IF IsComplexValue(left, real, imaginary) THEN
  2053. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2054. result.SetResolved(value);
  2055. type := left.type;
  2056. value.SetType(type);
  2057. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2058. ELSE
  2059. type := left.type;
  2060. END
  2061. ELSE
  2062. Error(left.position,"unary operator not applicable");
  2063. END;
  2064. |Scanner.Not:
  2065. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2066. IF IsBooleanValue(left,bool) THEN
  2067. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2068. result.SetResolved(value);
  2069. type := system.booleanType;
  2070. value.SetType(type);
  2071. ELSE
  2072. type := system.booleanType;
  2073. END;
  2074. ELSE
  2075. Error(left.position,"unary operator not applicable");
  2076. END;
  2077. |Scanner.Plus:
  2078. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2079. result := left; type := left.type;
  2080. ELSE
  2081. Error(left.position,"unary operator not applicable");
  2082. END;
  2083. (* ADDRESS OF *)
  2084. |Scanner.Address:
  2085. IF HasAddress(left) THEN
  2086. type := system.addressType;
  2087. ELSE
  2088. type := SyntaxTree.invalidType;
  2089. Error(left.position,"has no address");
  2090. Printout.Info("par", left);
  2091. END;
  2092. (* SIZE OF *)
  2093. |Scanner.Size:
  2094. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2095. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2096. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2097. value := SyntaxTree.NewIntegerValue(left.position, int);
  2098. result.SetResolved(value);
  2099. type := Global.GetIntegerType(system,int);
  2100. value.SetType(type)
  2101. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2102. ELSE
  2103. (* for variables, system sizeof could represent the physically occupied size
  2104. determined via the type descriptor, implement that ? *)
  2105. Error(left.position,"is not a type symbol");
  2106. END
  2107. (* ALIAS OF *)
  2108. |Scanner.Alias:
  2109. type := left.type.resolved;
  2110. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2111. type := SyntaxTree.invalidType;
  2112. Error(left.position,"alias on non math array type");
  2113. END;
  2114. ELSE
  2115. Error(left.position,"unary operator not defined");
  2116. END;
  2117. END;
  2118. result.SetType(type);
  2119. resolvedExpression := result
  2120. END VisitUnaryExpression;
  2121. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2122. VAR
  2123. result: SyntaxTree.Expression;
  2124. array: SyntaxTree.MathArrayExpression;
  2125. value: SyntaxTree.MathArrayValue;
  2126. isValue: BOOLEAN;
  2127. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2128. BEGIN
  2129. type := type.resolved;
  2130. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2131. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2132. END;
  2133. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2134. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2135. END;
  2136. RETURN type
  2137. END BaseType;
  2138. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2139. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2140. BEGIN
  2141. numberElements := x.elements.Length();
  2142. FOR i := 0 TO numberElements-1 DO
  2143. expression := x.elements.GetExpression(i);
  2144. IF expression IS SyntaxTree.MathArrayExpression THEN
  2145. array := SyntaxTree.NewMathArrayExpression(position);
  2146. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2147. expression := array;
  2148. ELSE
  2149. position := expression.position;
  2150. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2151. isValue := isValue & (expression.resolved # NIL);
  2152. END;
  2153. to.elements.AddExpression(expression);
  2154. END;
  2155. END RecursivelyConvert;
  2156. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2157. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2158. arrayType: SyntaxTree.MathArrayType;
  2159. BEGIN
  2160. numberElements := x.elements.Length();
  2161. baseType := NIL;
  2162. gsize := 0;
  2163. FOR i := 0 TO numberElements-1 DO
  2164. expression := x.elements.GetExpression(i);
  2165. IF expression IS SyntaxTree.MathArrayExpression THEN
  2166. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2167. IF i=0 THEN
  2168. gsize := size;
  2169. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2170. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2171. ELSE expression.SetType(baseType)
  2172. END;
  2173. ELSIF baseType = NIL THEN baseType := type;
  2174. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2175. END;
  2176. END;
  2177. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2178. arrayType.SetArrayBase(baseType);
  2179. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2180. RETURN ResolveType(arrayType);
  2181. END RecursivelySetType;
  2182. BEGIN
  2183. result := SyntaxTree.invalidExpression;
  2184. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2185. result := expression (* do not convert *)
  2186. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2187. isValue := TRUE;
  2188. type := BaseType(type);
  2189. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2190. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2191. value := SyntaxTree.NewMathArrayValue(array.position);
  2192. value.SetArray(array);
  2193. value.SetType(RecursivelySetType(array));
  2194. result := value;
  2195. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2196. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2197. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2198. IF result = NIL THEN
  2199. result := SyntaxTree.invalidExpression;
  2200. Error(position, "incompatible conversion");
  2201. IF VerboseErrorMessage THEN
  2202. Printout.Info("expression",expression);
  2203. Printout.Info("type",type);
  2204. END;
  2205. END;
  2206. END;
  2207. RETURN result
  2208. END MathArrayConversion;
  2209. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2210. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2211. BEGIN
  2212. result := expression; type := type.resolved;
  2213. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2214. (* skip, no conversion *)
  2215. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2216. int := expression(SyntaxTree.IntegerValue).hvalue;
  2217. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2218. int := Global.ConvertSigned(int,system.SizeOf(type));
  2219. result := SyntaxTree.NewIntegerValue(position,int);
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2222. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2223. result := SyntaxTree.NewIntegerValue(position,int);
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.FloatType) THEN
  2226. result := SyntaxTree.NewRealValue(expression.position,int);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2229. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2230. result.SetType(type);
  2231. ELSIF (type IS SyntaxTree.SetType) THEN
  2232. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2233. result.SetType(type);
  2234. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2235. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2238. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2239. Error(position, "huge integer value incompatible to enumeration");
  2240. END;
  2241. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2242. result.SetType(type);
  2243. ELSIF (type IS SyntaxTree.PortType) THEN
  2244. result := ConvertValue(position, expression, system.integerType);
  2245. ELSE
  2246. Error(position, "integer value cannot be converted");
  2247. result := SyntaxTree.invalidExpression;
  2248. IF VerboseErrorMessage THEN
  2249. Printout.Info("expression",expression);
  2250. Printout.Info("type",type);
  2251. END;
  2252. END;
  2253. ELSIF IsRealValue(expression,real) THEN
  2254. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2255. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2256. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2257. result.SetType(type);
  2258. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2259. int := ENTIERH(real);
  2260. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2261. result.SetType(type);
  2262. ELSIF (type IS SyntaxTree.FloatType) THEN
  2263. result := SyntaxTree.NewRealValue(position,real);
  2264. result.SetType(type);
  2265. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2266. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2267. result.SetType(type);
  2268. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2269. ELSIF (type IS SyntaxTree.PortType) THEN
  2270. result := ConvertValue(position, expression, system.integerType);
  2271. ELSE
  2272. Error(position, "real value cannot be converted");
  2273. result := SyntaxTree.invalidExpression;
  2274. END
  2275. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2276. IF (type IS SyntaxTree.ComplexType) THEN
  2277. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2278. result.SetType(type);
  2279. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2280. ELSE
  2281. Error(position, "complex value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END
  2284. ELSIF IsSetValue(expression,set) THEN
  2285. IF (type IS SyntaxTree.IntegerType) THEN
  2286. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2289. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2290. result.SetType(type);
  2291. ELSIF (type IS SyntaxTree.PortType) THEN
  2292. result := ConvertValue(position, expression, system.integerType);
  2293. ELSE
  2294. Error(position, "set value cannot be converted");
  2295. result := SyntaxTree.invalidExpression;
  2296. END;
  2297. ELSIF IsStringValue(expression,string) THEN
  2298. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2299. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2300. result.SetType(type);
  2301. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2302. ELSE
  2303. Error(position, "string value cannot be converted");
  2304. result := SyntaxTree.invalidExpression;
  2305. END;
  2306. ELSIF IsCharacterValue(expression,char) THEN
  2307. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2308. string[0] := char; string[1] := 0X;
  2309. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2310. result := SyntaxTree.NewStringValue(expression.position,string);
  2311. result.SetType(type);
  2312. ELSIF (type IS SyntaxTree.ByteType) THEN
  2313. (* do not simply set the new type as this could invalidate types of constants *)
  2314. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2315. result.SetType(type)
  2316. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2317. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2318. result.SetType(type);
  2319. ELSIF (type IS SyntaxTree.SetType) THEN
  2320. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2321. result.SetType(type);
  2322. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2323. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2324. result.SetType(type);
  2325. ELSIF (type IS SyntaxTree.PortType) THEN
  2326. result := ConvertValue(position, expression, system.integerType);
  2327. ELSE
  2328. Error(position, "character value cannot be converted");
  2329. result := SyntaxTree.invalidExpression;
  2330. END;
  2331. ELSIF expression IS SyntaxTree.NilValue THEN
  2332. IF type IS SyntaxTree.AddressType THEN
  2333. result := SyntaxTree.NewIntegerValue(position,0);
  2334. result.SetType(type);
  2335. ELSE
  2336. result := expression;
  2337. END;
  2338. (* nothing to be done *)
  2339. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2340. result := MathArrayConversion(position, expression,type);
  2341. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2342. int := expression(SyntaxTree.EnumerationValue).value;
  2343. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2344. int := Global.ConvertSigned(int,system.SizeOf(type));
  2345. result := SyntaxTree.NewIntegerValue(position,int);
  2346. result.SetType(type);
  2347. ELSE
  2348. result := expression;
  2349. END;
  2350. (* nothing to be done *)
  2351. ELSE
  2352. Error(position, "expression cannot be converted");
  2353. IF VerboseErrorMessage THEN
  2354. Printout.Info("expression",expression);
  2355. Printout.Info("type",type);
  2356. END;
  2357. result := SyntaxTree.invalidExpression;
  2358. END;
  2359. RETURN result
  2360. END ConvertValue;
  2361. (**
  2362. return a conversion of an expression to a given type
  2363. - if expression is already of same type then return expression
  2364. - if incompatible conversion then report error and return invalidExpression
  2365. **)
  2366. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2367. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2368. BEGIN
  2369. type := type.resolved;
  2370. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2371. result := expression;
  2372. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2373. ELSIF expression = NIL THEN (* NIL expression *)
  2374. ELSIF expression.type = NIL THEN
  2375. Error(position, "expression of type NIL cannot be converted");
  2376. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2377. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2378. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2379. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2380. ELSIF expression.resolved # NIL THEN (* value *)
  2381. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2382. IF value IS SyntaxTree.Value THEN
  2383. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2384. result.SetResolved(value(SyntaxTree.Value));
  2385. result.SetType(value.type);
  2386. ELSE
  2387. result := value
  2388. END;
  2389. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2390. expressionList := SyntaxTree.NewExpressionList();
  2391. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2392. typeDeclaration.SetDeclaredType(type);
  2393. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2394. typeSymbol.SetType(typeDeclaration.type);
  2395. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2396. expressionList.AddExpression(expression);
  2397. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2398. result.SetType(type);
  2399. ELSIF IsArrayStructuredObjectType(type) THEN
  2400. (* no type can be converted to an array-structured object type *)
  2401. HALT(100)
  2402. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2403. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2404. result := MathArrayConversion(position, expression,type);
  2405. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2406. expression := ConvertToMathArray(expression);
  2407. type := MathArrayStructureOfType(type);
  2408. result := MathArrayConversion(position, expression, type)
  2409. ELSE
  2410. Error(expression.position,"cannot convert non array type to array type")
  2411. END;
  2412. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2413. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2414. OR ~(type IS SyntaxTree.ArrayType) THEN
  2415. Error(expression.position,"cannot convert array type to non-array type")
  2416. END;
  2417. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2418. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2419. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2420. (*skip, no conversion*)
  2421. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2422. (* skip, no conversion *)
  2423. ELSE
  2424. ASSERT(~(type IS SyntaxTree.RangeType));
  2425. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2426. ASSERT(type # NIL);
  2427. END;
  2428. RETURN result
  2429. END NewConversion;
  2430. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2431. BEGIN
  2432. IF CompatibleTo(system,expression.type, type) THEN
  2433. RETURN NewConversion(position, expression, type, NIL);
  2434. ELSE
  2435. Error(expression.position, "incompatible expression");
  2436. RETURN SyntaxTree.invalidExpression
  2437. END;
  2438. END CompatibleConversion;
  2439. (**
  2440. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2441. **)
  2442. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2443. VAR leftType,rightType: SyntaxTree.Type;
  2444. BEGIN
  2445. IF left.type = NIL THEN Error(left.position,"no type")
  2446. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2447. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2448. (* error already handled *)
  2449. ELSE
  2450. leftType := left.type.resolved; rightType := right.type.resolved;
  2451. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2452. right := NewConversion(right.position, right, leftType, NIL);
  2453. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2454. left := NewConversion(left.position,left,rightType,NIL);
  2455. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2456. right := NewConversion(right.position, right, leftType, NIL);
  2457. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2458. left := NewConversion(left.position,left,rightType,NIL);
  2459. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2460. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2461. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2462. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2463. ELSIF
  2464. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2465. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2466. (* must be the case LONGREAL / COMPLEX ) *)
  2467. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2468. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2469. ELSE
  2470. Error(left.position,"incompatible operands");
  2471. END;
  2472. END;
  2473. END ConvertOperands;
  2474. (** find and return best operator matching to parameter list (nil, if none)
  2475. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2476. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2477. **)
  2478. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2479. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2480. identifier: SyntaxTree.Identifier;
  2481. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2482. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2483. BEGIN
  2484. operator := scope.firstOperator;
  2485. WHILE(operator # NIL) DO
  2486. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2487. procedureType := operator.type(SyntaxTree.ProcedureType);
  2488. distance := Distance(system, procedureType,actualParameters);
  2489. IF (distance < Infinity) THEN
  2490. IF returnType # NIL THEN
  2491. IF procedureType.returnType = NIL THEN
  2492. distance := Infinity
  2493. ELSE
  2494. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2495. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2496. END;
  2497. END;
  2498. END;
  2499. (*
  2500. IF distance < Infinity THEN
  2501. TRACE(distance, operator);
  2502. Printout.Info("potential operator",operator);
  2503. ELSE
  2504. Printout.Info("operator not possible",operator);
  2505. END;
  2506. *)
  2507. IF distance < bestDistance THEN
  2508. bestDistance := distance;
  2509. bestOperator := operator;
  2510. END;
  2511. END;
  2512. operator := operator.nextOperator;
  2513. END;
  2514. (*
  2515. Printout.Info("taken operator",bestOperator);
  2516. *)
  2517. END FindInScope;
  2518. BEGIN
  2519. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2520. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2521. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2522. import := currentScope.ownerModule.moduleScope.firstImport;
  2523. WHILE (bestDistance > 0) & (import # NIL) DO
  2524. IF import.module # NIL THEN
  2525. identifier := Global.GetIdentifier(operator,import.module.case);
  2526. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2527. END;
  2528. import := import.nextImport;
  2529. END;
  2530. RETURN bestOperator
  2531. END FindOperator;
  2532. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2533. BEGIN
  2534. currentScope := scope;
  2535. END SetCurrentScope;
  2536. (**
  2537. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2538. - handle LEN and DIM operator for array-structured object types
  2539. - find operator, if found then
  2540. - if in other module then add import designator
  2541. - create symbol designator for operator
  2542. - if error then return invalidExpression, if no operator then return NIL
  2543. **)
  2544. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2545. VAR
  2546. operator: SyntaxTree.Operator;
  2547. import: SyntaxTree.Import;
  2548. expression, result: SyntaxTree.Expression;
  2549. designator: SyntaxTree.Designator;
  2550. actualParameters, tempList: SyntaxTree.ExpressionList;
  2551. recordType: SyntaxTree.RecordType;
  2552. castReturnType : SyntaxTree.MathArrayType;
  2553. BEGIN
  2554. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2555. result := SyntaxTree.invalidExpression
  2556. ELSIF leftExpression = NIL THEN
  2557. result := NIL
  2558. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2559. (* LEN or DIM operator on array-structured object type *)
  2560. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2561. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2562. IF recordType.arrayAccessOperators.len = NIL THEN
  2563. Error(position, "call of undeclared LEN operator");
  2564. result := SyntaxTree.invalidExpression
  2565. ELSE
  2566. ASSERT(leftExpression IS SyntaxTree.Designator);
  2567. designator := leftExpression(SyntaxTree.Designator);
  2568. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2569. ASSERT(expression IS SyntaxTree.Designator);
  2570. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2571. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2572. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2573. result := designator
  2574. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2575. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2576. tempList := SyntaxTree.NewExpressionList();
  2577. tempList.AddExpression(rightExpression);
  2578. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2579. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2580. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2581. tempList := SyntaxTree.NewExpressionList();
  2582. tempList.AddExpression(designator);
  2583. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2584. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2585. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2586. END
  2587. END;
  2588. ELSE
  2589. IF ~complexNumbersImported THEN
  2590. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2591. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2592. THEN
  2593. (* operators on complex numbers *)
  2594. ImportModule(Global.ComplexNumbersName,position);
  2595. complexNumbersImported := TRUE;
  2596. END;
  2597. END;
  2598. (* import OCArrayBase if needed *)
  2599. IF ~arrayBaseImported THEN
  2600. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2601. IF op = Global.Dim THEN
  2602. (* not existing in OCArrayBase *)
  2603. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2604. (* not existing in OCArrayBase *)
  2605. ELSE
  2606. ImportModule(Global.ArrayBaseName,position);
  2607. arrayBaseImported := TRUE;
  2608. END
  2609. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2610. ImportModule(Global.ArrayBaseName,position);
  2611. arrayBaseImported := TRUE
  2612. END;
  2613. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2614. (* LEN(RANGE) *)
  2615. ImportModule(Global.ArrayBaseName,position);
  2616. arrayBaseImported := TRUE;
  2617. END;
  2618. END;
  2619. actualParameters := SyntaxTree.NewExpressionList();
  2620. actualParameters.AddExpression(leftExpression);
  2621. IF rightExpression # NIL THEN
  2622. actualParameters.AddExpression(rightExpression)
  2623. END;
  2624. operator := FindOperator(system,op,actualParameters,resultType);
  2625. IF operator # NIL THEN
  2626. designator := NIL;
  2627. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2628. import := currentScope.ownerModule.moduleScope.firstImport;
  2629. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2630. import := import.nextImport;
  2631. END;
  2632. expression := NewSymbolDesignator(position,NIL,import);
  2633. designator := expression(SyntaxTree.Designator);
  2634. END;
  2635. expression := NewSymbolDesignator(position,designator,operator);
  2636. designator := expression(SyntaxTree.Designator);
  2637. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2638. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2639. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2640. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2641. result.SetType(castReturnType);
  2642. END;
  2643. ELSE
  2644. result := NIL;
  2645. END;
  2646. END;
  2647. RETURN result
  2648. END NewOperatorCall;
  2649. (** check and resolve binary expression **)
  2650. (*! clean up *)
  2651. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2652. VAR left,right,result: SyntaxTree.Expression;
  2653. leftType, rightType: SyntaxTree.Type;
  2654. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2655. cl,cr: CHAR;
  2656. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2657. type: SyntaxTree.Type;
  2658. value: SyntaxTree.Value;
  2659. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2660. integerConstantFolding: BOOLEAN;
  2661. list: SyntaxTree.ExpressionList;
  2662. PROCEDURE NewBool(v: BOOLEAN);
  2663. BEGIN
  2664. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2665. value.SetType(system.booleanType);
  2666. result.SetResolved(value);
  2667. type := system.booleanType
  2668. END NewBool;
  2669. PROCEDURE NewSet(v: SET);
  2670. BEGIN
  2671. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2672. value.SetType(system.setType);
  2673. result.SetResolved(value);
  2674. type := system.setType;
  2675. END NewSet;
  2676. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2677. BEGIN
  2678. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2679. (* type cast to "larger" type only if the value is still in the range *)
  2680. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2681. value.SetType(t);
  2682. END;
  2683. result.SetResolved(value);
  2684. type := value.type;
  2685. END NewInteger;
  2686. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2687. BEGIN
  2688. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2689. value.SetType(t);
  2690. result.SetResolved(value);
  2691. type := t;
  2692. END NewReal;
  2693. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2694. BEGIN
  2695. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2696. value.SetType(t);
  2697. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2698. result.SetResolved(value);
  2699. type := t;
  2700. END NewComplex;
  2701. BEGIN
  2702. type := SyntaxTree.invalidType;
  2703. left := ResolveExpression(binaryExpression.left);
  2704. right := ResolveExpression(binaryExpression.right);
  2705. binaryExpression.SetLeft(left);
  2706. binaryExpression.SetRight(right);
  2707. result := binaryExpression;
  2708. operator := binaryExpression.operator;
  2709. IF ~system.operatorDefined[operator] THEN
  2710. Error(left.position,"Operator Not Defined");
  2711. result := SyntaxTree.invalidExpression;
  2712. RETURN
  2713. END;
  2714. IF left.type = NIL THEN
  2715. Error(left.position,"Expression has no result type");
  2716. result := SyntaxTree.invalidExpression;
  2717. RETURN;
  2718. END;
  2719. IF right.type = NIL THEN
  2720. Error(right.position,"Expression has no result type");
  2721. result := SyntaxTree.invalidExpression;
  2722. RETURN;
  2723. END;
  2724. leftType := left.type.resolved; rightType := right.type.resolved;
  2725. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2726. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2727. END;
  2728. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2729. list := SyntaxTree.NewExpressionList();
  2730. list.AddExpression(right);
  2731. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2732. END;
  2733. IF operatorCall # NIL THEN
  2734. result := operatorCall;
  2735. type := operatorCall.type;
  2736. (* admissible operators:
  2737. Times, Plus, Minus numeric numeric numeric
  2738. set set set
  2739. Slash numeric numeric real /complex
  2740. set set set
  2741. Div , Mod integer integer integer
  2742. And, Or bool bool bool
  2743. Equal, Unequal basic basic bool
  2744. pointer pointer bool
  2745. object object bool
  2746. record record bool
  2747. string string bool
  2748. enumerator enumerator bool
  2749. Less, LessEqual,
  2750. Greater, GreaterEqual integer/real integer/real bool
  2751. enumerator enumerator bool
  2752. In integer set bool
  2753. Is pointer type bool
  2754. object type bool
  2755. record type bool
  2756. Upto: special abbreviation for a..b
  2757. *)
  2758. ELSIF (left.type = NIL) THEN
  2759. Error(left.position,"type (left operand) = NIL in binary expression");
  2760. D.Str("nil type in "); D.Type(left); D.Ln;
  2761. result := SyntaxTree.invalidExpression;
  2762. ELSIF (right.type = NIL) THEN
  2763. Error(right.position,"type (right operand) = NIL in binary expression");
  2764. result := SyntaxTree.invalidExpression;
  2765. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2766. result := SyntaxTree.invalidExpression;
  2767. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2768. HALT(100);
  2769. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2770. type := system.booleanType;
  2771. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2772. Error(right.position,"is not a type ");
  2773. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2774. Error(binaryExpression.position,"is not a type extension of ");
  2775. IF VerboseErrorMessage THEN
  2776. Printout.Info("left",left);
  2777. Printout.Info("right",right);
  2778. END;
  2779. ELSIF IsUnsafePointer(left.type) THEN
  2780. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2781. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2782. NewBool(TRUE)
  2783. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2784. NewBool(TRUE);
  2785. ELSIF IsUnextensibleRecord(left) THEN
  2786. NewBool(FALSE)
  2787. END
  2788. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2789. Error(right.position,"must not be a type");
  2790. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2791. Error(left.position,"must not be a type");
  2792. ELSIF operator = Scanner.In THEN (* left IN right *)
  2793. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2794. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2795. NewBool(il IN sr);
  2796. ELSE
  2797. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2798. left := NewConversion(left.position, left, system.longintType,NIL);
  2799. binaryExpression.SetLeft(left)
  2800. END;
  2801. type := system.booleanType;
  2802. END
  2803. ELSE
  2804. Error(binaryExpression.position, "incompatible operands");
  2805. END
  2806. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2807. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2808. Error(binaryExpression.position,"incompatible operands");
  2809. END;
  2810. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2811. ELSE Error(binaryExpression.position,"operator not defined 1")
  2812. END
  2813. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2814. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2815. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2816. Error(binaryExpression.position,"incompatible operands");
  2817. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2818. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2819. left := NewConversion(left.position, left, system.addressType, NIL);
  2820. right := NewConversion(right.position, right, system.addressType, NIL);
  2821. binaryExpression.SetLeft(left);
  2822. binaryExpression.SetRight(right);
  2823. type := system.addressType;
  2824. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2825. ConvertOperands(left, right);
  2826. binaryExpression.SetLeft(left);
  2827. binaryExpression.SetRight(right);
  2828. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2829. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2830. END;
  2831. type := system.booleanType;
  2832. ELSE
  2833. Error(binaryExpression.position,"operator not defined 3");
  2834. END
  2835. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2836. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2837. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2838. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2839. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2840. CASE operator OF
  2841. |Scanner.Equal: NewBool(strl^=strr^);
  2842. |Scanner.Unequal:NewBool(strl^#strr^);
  2843. |Scanner.Less: NewBool(strl^<strr^);
  2844. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2845. |Scanner.Greater: NewBool(strl^>strr^);
  2846. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2847. ELSE
  2848. Error(binaryExpression.position,"operator not defined 4");
  2849. END;
  2850. END;
  2851. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2852. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2853. type := system.booleanType
  2854. ELSE
  2855. Error(binaryExpression.position,"operator not defined 5");
  2856. END;
  2857. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2858. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2859. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2860. type := system.booleanType;
  2861. ELSE
  2862. Error(binaryExpression.position,"operator not defined 6");
  2863. END
  2864. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2865. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2866. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2867. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2868. type := system.booleanType
  2869. ELSE
  2870. Error(binaryExpression.position,"operator not defined for enumerators");
  2871. END;
  2872. ELSE
  2873. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2874. END;
  2875. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2876. type := system.booleanType;
  2877. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2878. type := system.booleanType;
  2879. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2880. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2881. THEN
  2882. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2883. IF (leftType # rightType) THEN
  2884. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2885. ConvertOperands(left,right); (* operands must be of the same type here *)
  2886. END;
  2887. binaryExpression.SetLeft(left);
  2888. binaryExpression.SetRight(right);
  2889. leftType := left.type.resolved;
  2890. rightType := right.type.resolved;
  2891. END;
  2892. type := leftType;
  2893. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2894. Error(binaryExpression.position,"conversion failed ?");
  2895. IF VerboseErrorMessage THEN
  2896. Printout.Info("left",left);
  2897. Printout.Info("right",right);
  2898. END;
  2899. ELSIF IsIntegerType(leftType) THEN
  2900. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2901. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2902. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2903. Error(binaryExpression.position,"division by zero");
  2904. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2905. Error(binaryExpression.position,"integer division by negative number");
  2906. END;
  2907. END;
  2908. (* constant folding *)
  2909. (* bootstrap64
  2910. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2911. CASE operator OF
  2912. |Scanner.Plus: NewInteger(il+ir,left.type);
  2913. |Scanner.Minus: NewInteger(il-ir,left.type);
  2914. |Scanner.Times: NewInteger(il*ir,left.type);
  2915. |Scanner.Slash:
  2916. IF ir # 0 THEN
  2917. NewReal(il/ir, system.realType);
  2918. END;
  2919. |Scanner.Mod:
  2920. IF ir > 0 THEN
  2921. NewInteger(il MOD ir,left.type);
  2922. END;
  2923. |Scanner.Div:
  2924. IF ir > 0 THEN
  2925. NewInteger(il DIV ir,left.type);
  2926. END;
  2927. |Scanner.Equal: NewBool(il=ir);
  2928. |Scanner.Unequal:NewBool(il#ir);
  2929. |Scanner.Less: NewBool(il<ir);
  2930. |Scanner.LessEqual: NewBool(il<=ir);
  2931. |Scanner.Greater: NewBool(il>ir);
  2932. |Scanner.GreaterEqual: NewBool(il>=ir);
  2933. ELSE Error(binaryExpression.position,"operator not defined 7");
  2934. END;
  2935. ELS*)
  2936. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2937. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2938. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2939. CASE operator OF
  2940. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2941. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2942. |Scanner.Times: NewInteger(hl*hr,left.type);
  2943. |Scanner.Slash:
  2944. IF hr = 0 THEN
  2945. Error(binaryExpression.position,"division by zero");
  2946. ELSE
  2947. IF type.sizeInBits = 64 THEN
  2948. NewReal(hl/hr,system.longrealType);
  2949. ELSE
  2950. NewReal(hl/hr,system.realType)
  2951. END
  2952. END;
  2953. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2954. |Scanner.Mod:
  2955. IF hr = 0 THEN
  2956. Error(binaryExpression.position,"division by zero");
  2957. ELSE
  2958. NewInteger(hl MOD hr, left.type);
  2959. (* bootstrap64
  2960. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2961. *)
  2962. END;
  2963. |Scanner.Div:
  2964. IF hr = 0 THEN
  2965. Error(binaryExpression.position,"division by zero");
  2966. ELSE
  2967. NewInteger(hl DIV hr, left.type);
  2968. (* bootstrap64
  2969. NewInteger(Machine.DivH(hl,hr),left.type);
  2970. *)
  2971. END;
  2972. (* *)
  2973. |Scanner.Equal: NewBool(hl=hr);
  2974. |Scanner.Unequal: NewBool(hl#hr);
  2975. |Scanner.Less: NewBool(hl<hr);
  2976. |Scanner.LessEqual: NewBool(hl<=hr);
  2977. |Scanner.Greater: NewBool(hl>hr);
  2978. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2979. ELSE Error(binaryExpression.position,"operator not defined 8");
  2980. END;
  2981. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2982. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2983. type := left.type
  2984. ELSIF (operator = Scanner.Slash) THEN
  2985. left := NewConversion(left.position,left,system.realType,NIL);
  2986. right := NewConversion(right.position,right,system.realType,NIL);
  2987. binaryExpression.SetLeft(left);
  2988. binaryExpression.SetRight(right);
  2989. type := system.realType
  2990. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2991. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2992. type := system.booleanType
  2993. ELSE
  2994. Error(binaryExpression.position,"operator not defined 9");
  2995. END;
  2996. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2997. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2998. CASE operator OF
  2999. |Scanner.Plus: NewReal(rl+rr,leftType);
  3000. |Scanner.Minus: NewReal(rl-rr,leftType);
  3001. |Scanner.Times:NewReal(rl*rr,leftType);
  3002. |Scanner.Slash:
  3003. IF rr = 0 THEN
  3004. Error(binaryExpression.position,"division by zero");
  3005. ELSE
  3006. NewReal(rl/rr,leftType);
  3007. END
  3008. |Scanner.Equal: NewBool(rl=rr);
  3009. |Scanner.Unequal: NewBool(rl#rr);
  3010. |Scanner.Less: NewBool(rl<rr);
  3011. |Scanner.LessEqual: NewBool(rl<=rr);
  3012. |Scanner.Greater: NewBool(rl>rr);
  3013. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3014. ELSE Error(binaryExpression.position,"operator not defined 10");
  3015. END;
  3016. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3017. type := left.type
  3018. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3019. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3020. type := system.booleanType
  3021. ELSE
  3022. Error(binaryExpression.position,"operator not defined 11");
  3023. IF VerboseErrorMessage THEN
  3024. Printout.Info("left",left);
  3025. Printout.Info("right",right);
  3026. END;
  3027. END;
  3028. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3029. CASE operator OF
  3030. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3031. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3032. ELSE
  3033. Error(binaryExpression.position,"operator not defined");
  3034. IF VerboseErrorMessage THEN
  3035. Printout.Info("left", left);
  3036. Printout.Info("right", right)
  3037. END;
  3038. END;
  3039. IF ~error THEN
  3040. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3041. Error(binaryExpression.position,"division by zero")
  3042. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3043. (* do constant folding *)
  3044. CASE operator OF
  3045. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3046. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3047. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3048. |Scanner.Slash:
  3049. divisor := c * c + d * d;
  3050. ASSERT(divisor # 0);
  3051. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3052. |Scanner.Equal: NewBool((a = c) & (b = d))
  3053. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3054. END
  3055. END
  3056. END
  3057. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3058. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3059. CASE operator OF
  3060. |Scanner.And: NewBool(bl & br);
  3061. |Scanner.Or: NewBool(bl OR br);
  3062. |Scanner.Equal: NewBool(bl = br);
  3063. |Scanner.Unequal: NewBool(bl # br);
  3064. ELSE Error(binaryExpression.position,"operator not defined 12");
  3065. END;
  3066. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3067. type := system.booleanType
  3068. ELSE
  3069. Error(binaryExpression.position,"operator not defined 13");
  3070. END;
  3071. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3072. (* constant folding *)
  3073. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3074. IF operator = Scanner.Equal THEN
  3075. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3076. ELSIF operator = Scanner.Unequal THEN
  3077. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3078. END;
  3079. END;
  3080. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3081. type := system.booleanType;
  3082. ELSE
  3083. Error(binaryExpression.position, "operator not defined");
  3084. END;
  3085. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3086. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3087. CASE operator OF
  3088. |Scanner.Plus: NewSet(sl + sr);
  3089. |Scanner.Minus: NewSet(sl - sr);
  3090. |Scanner.Times: NewSet(sl * sr);
  3091. |Scanner.Slash: NewSet(sl / sr);
  3092. |Scanner.Equal: NewBool(sl=sr);
  3093. |Scanner.Unequal: NewBool(sl#sr);
  3094. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3095. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3096. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3097. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3098. ELSE Error(binaryExpression.position,"operator not defined 14");
  3099. END;
  3100. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3101. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3102. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3103. THEN
  3104. type := system.booleanType
  3105. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3106. type := left.type
  3107. ELSE
  3108. Error(binaryExpression.position,"operator not defined 15");
  3109. END;
  3110. ELSIF IsCharacterType(left.type) THEN
  3111. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3112. CASE operator OF
  3113. |Scanner.Equal: NewBool(cl=cr);
  3114. |Scanner.Unequal: NewBool(cl#cr);
  3115. |Scanner.Less: NewBool(cl<cr);
  3116. |Scanner.LessEqual: NewBool(cl<=cr);
  3117. |Scanner.Greater: NewBool(cl>cr);
  3118. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3119. ELSE Error(binaryExpression.position,"operator not defined 16");
  3120. END;
  3121. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3122. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3123. type := system.booleanType
  3124. ELSE
  3125. Error(binaryExpression.position,"operator not defined 17");
  3126. END;
  3127. ELSE
  3128. Error(binaryExpression.position,"operator not defined 18");
  3129. END;
  3130. ELSE
  3131. Error(binaryExpression.position,"operator not defined 19");
  3132. END;
  3133. IF type = SyntaxTree.invalidType THEN
  3134. result := SyntaxTree.invalidExpression
  3135. ELSE
  3136. result.SetType(type)
  3137. END;
  3138. resolvedExpression := result
  3139. END VisitBinaryExpression;
  3140. (** resolve a range expression of the from <<first .. last BY step>>
  3141. - depending on the context different things are checked:
  3142. ArrayIndex:
  3143. - components must be integers
  3144. - replace missing lower bound with 0
  3145. - replace missing upper bound with MAX(LONGINT)
  3146. - replace missing step size with 1
  3147. SetElement:
  3148. - components must be integers
  3149. - replace missing lower bound with 0
  3150. - replace missing upper bound with MAX(SET)
  3151. - must not have step size
  3152. CaseGuard:
  3153. - components must be constant
  3154. - components must be integers or characters
  3155. - must have lower and upper bound present
  3156. - components are made compatible
  3157. - must not have step size
  3158. - if error: return invalidExpression
  3159. **)
  3160. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3161. VAR
  3162. hasError: BOOLEAN;
  3163. first, last, step: SyntaxTree.Expression;
  3164. BEGIN
  3165. hasError := FALSE;
  3166. first := x.first;
  3167. last := x.last;
  3168. step := x.step;
  3169. (* check lower bound *)
  3170. IF x.context = SyntaxTree.CaseGuard THEN
  3171. IF first = NIL THEN
  3172. Error(x.position, "missing lower bound");
  3173. hasError := TRUE
  3174. ELSE
  3175. first := ResolveExpression(first);
  3176. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3177. Error(first.position, "lower bound not integer or character");
  3178. hasError := TRUE
  3179. ELSE
  3180. IF first IS SyntaxTree.StringValue THEN
  3181. (* add conversion from string to character *)
  3182. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3183. END
  3184. END;
  3185. (* check if expression is constant *)
  3186. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3187. (* error already reported *)
  3188. hasError := TRUE
  3189. END
  3190. END
  3191. ELSE (* ArrayIndex, SetElement *)
  3192. IF first = NIL THEN
  3193. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3194. END;
  3195. first := ResolveExpression(first);
  3196. IF IsIntegerType(first.type.resolved) THEN
  3197. first := NewConversion(first.position, first, system.lenType, NIL)
  3198. ELSE
  3199. Error(first.position, "lower bound not integer");
  3200. hasError := TRUE
  3201. END
  3202. END;
  3203. (* check upper bound *)
  3204. IF x.context = SyntaxTree.CaseGuard THEN
  3205. IF last = NIL THEN
  3206. Error(x.position, "missing upper bound");
  3207. hasError := TRUE
  3208. ELSE
  3209. last := ResolveExpression(last);
  3210. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3211. Error(last.position, "lower bound not integer or character");
  3212. hasError := TRUE
  3213. ELSE
  3214. IF last IS SyntaxTree.StringValue THEN
  3215. (* add conversion from string to character *)
  3216. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3217. END
  3218. END;
  3219. (* check if expression is constant *)
  3220. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3221. (* error already reported *)
  3222. hasError := TRUE
  3223. ELSE
  3224. (* try to make lower and upper bound compatible *)
  3225. ConvertOperands(first, last);
  3226. IF first.type.resolved # last.type.resolved THEN
  3227. Error(x.position, "lower and upper bounds incompatible");
  3228. hasError := TRUE
  3229. END
  3230. END
  3231. END
  3232. ELSE (* ArrayIndex, SetElement *)
  3233. IF last = NIL THEN
  3234. IF x.context = SyntaxTree.ArrayIndex THEN
  3235. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3236. ELSE
  3237. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3238. END
  3239. END;
  3240. last := ResolveExpression(last);
  3241. IF IsIntegerType(last.type.resolved) THEN
  3242. last := NewConversion(last.position, last, system.lenType, NIL)
  3243. ELSE
  3244. Error(last.position, "upper bound not integer");
  3245. hasError := TRUE
  3246. END
  3247. END;
  3248. (* check step size *)
  3249. IF x.context = SyntaxTree.ArrayIndex THEN
  3250. IF step = NIL THEN
  3251. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3252. END;
  3253. step := ResolveExpression(step);
  3254. IF IsIntegerType(step.type.resolved) THEN
  3255. step := NewConversion(step.position, step, system.lenType, NIL)
  3256. ELSE
  3257. Error(step.position, "step size not integer");
  3258. hasError := TRUE
  3259. END
  3260. ELSE (* SetElement, CaseGuard *)
  3261. IF step # NIL THEN
  3262. Error(last.position, "step size not allowed in this context");
  3263. hasError := TRUE
  3264. END
  3265. END;
  3266. IF hasError THEN
  3267. resolvedExpression := SyntaxTree.invalidExpression
  3268. ELSE
  3269. x.SetFirst(first);
  3270. x.SetLast(last);
  3271. x.SetStep(step);
  3272. x.SetType(system.rangeType);
  3273. resolvedExpression := x;
  3274. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3275. END
  3276. END VisitRangeExpression;
  3277. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3278. BEGIN
  3279. x.SetType(NIL);
  3280. resolvedExpression := x;
  3281. END VisitTensorRangeExpression;
  3282. (** resolve the expression d and return result as designator
  3283. - resolve expression
  3284. - if expression is a designator then return designator else error message and return invalidDesignator
  3285. **)
  3286. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3287. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3288. BEGIN
  3289. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3290. resolved := ResolveExpression(d);
  3291. IF resolved = SyntaxTree.invalidExpression THEN
  3292. (* error should already have been reported *)
  3293. result := SyntaxTree.invalidDesignator;
  3294. ELSIF resolved IS SyntaxTree.Designator THEN
  3295. result := resolved(SyntaxTree.Designator);
  3296. ELSE
  3297. Error(d.position,"is no designator ! ");
  3298. result := SyntaxTree.invalidDesignator;
  3299. END;
  3300. (* result.type might be nil. *)
  3301. RETURN result
  3302. END ResolveDesignator;
  3303. (**
  3304. symbol designator generated in this module
  3305. nothing to be resolved
  3306. **)
  3307. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3308. BEGIN
  3309. resolvedExpression := x;
  3310. END VisitSymbolDesignator;
  3311. (**
  3312. self designator generated in this module
  3313. nothing to be resolved
  3314. **)
  3315. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3316. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3317. BEGIN
  3318. (* check if in record scope *)
  3319. scope := currentScope;
  3320. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3321. scope := scope.outerScope;
  3322. END;
  3323. IF scope = NIL THEN (* in module scope *)
  3324. x.SetType(system.anyType);
  3325. ELSIF scope IS SyntaxTree.CellScope THEN
  3326. cell := scope(SyntaxTree.CellScope).ownerCell;
  3327. x.SetType(cell);
  3328. ELSE (* in record scope *)
  3329. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3330. IF (record # NIL) & (record.pointerType # NIL) THEN
  3331. type := ResolveType(record.pointerType);
  3332. x.SetType(type);
  3333. ELSE
  3334. x.SetType(record);
  3335. END;
  3336. END;
  3337. resolvedExpression := x;
  3338. END VisitSelfDesignator;
  3339. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3340. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3341. BEGIN
  3342. scope := currentScope;
  3343. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3344. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3345. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3346. returnType := procedureType.returnType;
  3347. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3348. THEN
  3349. x.SetType(returnType);
  3350. ELSE
  3351. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3352. x.SetType(SyntaxTree.invalidType);
  3353. END;
  3354. ELSE
  3355. Error(x.position,"forbidden access to result designator");
  3356. x.SetType(SyntaxTree.invalidType);
  3357. END;
  3358. x.SetAssignable(TRUE);
  3359. resolvedExpression := x;
  3360. END VisitResultDesignator;
  3361. (**
  3362. return symbol designator as an expression
  3363. - if symbol is a constant then return the constant value expression
  3364. - else
  3365. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3366. - if symbol is a guarded variable then return a TypeGuardDesignator
  3367. - else return a symbol designator
  3368. **)
  3369. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3370. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3371. guardType: SyntaxTree.Type;
  3372. BEGIN
  3373. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3374. result := SyntaxTree.invalidExpression;
  3375. ASSERT(symbol # NIL);
  3376. (*
  3377. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3378. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3379. Error(position, "type not allowed here");
  3380. ELS *)
  3381. (* not needed any more as values are stored in the expression
  3382. IF symbol IS SyntaxTree.Constant THEN
  3383. result := symbol(SyntaxTree.Constant).value
  3384. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3385. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3386. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3387. ELSE
  3388. result := symbol(SyntaxTree.Constant).value
  3389. END;
  3390. ELSE
  3391. *)
  3392. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3393. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3394. THEN
  3395. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3396. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3397. left := NewDereferenceDesignator(position,left);
  3398. left.SetHidden(TRUE);
  3399. END;
  3400. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3401. scope := currentScope;
  3402. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3403. scope := scope.outerScope;
  3404. END;
  3405. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3406. Error(position, "forbidden access to symbol in parent procedure scope");
  3407. END;
  3408. END;
  3409. 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);
  3410. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3411. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3412. ELSE
  3413. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3414. END;
  3415. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3416. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3417. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3418. result.SetType(symbol.type);
  3419. result.SetAssignable(assignable);
  3420. symbol.MarkUsed;
  3421. IF symbol IS SyntaxTree.Constant THEN
  3422. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3423. END;
  3424. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3425. variableAccessed := TRUE
  3426. END;
  3427. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3428. IF GetGuard(symbol,guardType) THEN
  3429. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3430. END;
  3431. END;
  3432. ASSERT(result.type # NIL);
  3433. RETURN result
  3434. END NewSymbolDesignator;
  3435. (** check and resolve an identifier designator "identifier"
  3436. - if identifier = self then return SelfDesignator
  3437. - else find symbol in current scope
  3438. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3439. **)
  3440. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3441. VAR symbol: SyntaxTree.Symbol;
  3442. BEGIN
  3443. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3444. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3445. IF symbol # NIL THEN
  3446. ResolveSymbol(symbol);
  3447. ASSERT(symbol.type # NIL);
  3448. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3449. ELSE
  3450. Error(identifierDesignator.position,"Undeclared Identifier");
  3451. IF VerboseErrorMessage THEN
  3452. Printout.Info("undeclared identifier designator",identifierDesignator);
  3453. END;
  3454. resolvedExpression := SyntaxTree.invalidDesignator;
  3455. END;
  3456. END VisitIdentifierDesignator;
  3457. (** check and resolve a selector designator of the form left.designator
  3458. - if left is a pointer type then do auto dereferenciation
  3459. - left denotes a search scope:
  3460. - if left type is import type then set search scope to respective module
  3461. - if left type is enumeration type then set search scope to respective enumeration scope
  3462. - elsif left type is record type then set search scope to record scope
  3463. - search symbol in computed scope
  3464. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3465. **)
  3466. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3467. VAR
  3468. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3469. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3470. BEGIN
  3471. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3472. left := ResolveDesignator(selectorDesignator.left);
  3473. result := SyntaxTree.invalidDesignator;
  3474. IF left # NIL THEN
  3475. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3476. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3477. END;
  3478. scope := NIL;
  3479. IF left.type = NIL THEN
  3480. Error(selectorDesignator.position,"field on nil typed designator");
  3481. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3482. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3483. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3484. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3485. module := symbol(SyntaxTree.Import).module;
  3486. IF module # NIL THEN
  3487. scope := module.moduleScope
  3488. ELSE
  3489. Error(left.position,"module not loaded");
  3490. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3491. END;
  3492. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3493. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3494. ASSERT(scope # NIL)
  3495. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3496. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3497. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3498. IF type IS SyntaxTree.EnumerationType THEN
  3499. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3500. ELSE
  3501. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3502. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3503. END;
  3504. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3505. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3506. ELSE
  3507. Error(selectorDesignator.position,"field on non-record type designator");
  3508. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3509. END;
  3510. symbol := NIL;
  3511. IF scope # NIL THEN
  3512. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3513. IF symbol # NIL THEN
  3514. ResolveSymbol(symbol);
  3515. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3516. symbol.MarkUsed
  3517. ELSE
  3518. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3519. IF VerboseErrorMessage THEN
  3520. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3521. Printout.Info("scope", scope);
  3522. Printout.Info("left", left);
  3523. Printout.Info("undeclared identifier",selectorDesignator);
  3524. Printout.Info("left resolved designator",left);
  3525. END
  3526. END;
  3527. END;
  3528. END;
  3529. resolvedExpression := result;
  3530. END VisitSelectorDesignator;
  3531. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3532. VAR len,idx: LONGINT;
  3533. BEGIN
  3534. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3535. IF idx < 0 THEN
  3536. Error(index.position,"index out of bounds (too small)")
  3537. ELSE
  3538. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3539. Error(index.position,"index out of bounds (too large)");
  3540. END;
  3541. END;
  3542. END;
  3543. END IndexCheck;
  3544. (*
  3545. - if index designator has not type, use newBaseType as its type
  3546. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3547. - special rule: if static array of dynamic array occurs, make it all dynamic
  3548. index designator type: new base type: new index designator type:
  3549. NIL z z
  3550. ARRAY [x, y] z ARRAY [x, y] OF z
  3551. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3552. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3553. *)
  3554. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3555. VAR
  3556. mathArrayType: SyntaxTree.MathArrayType;
  3557. makeDynamic: BOOLEAN;
  3558. BEGIN
  3559. IF indexDesignator.type = NIL THEN
  3560. indexDesignator.SetType(newBaseType)
  3561. ELSE
  3562. (* index designator must be a of math array type *)
  3563. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3564. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3565. (* determine if all arrays have to be made dynamic *)
  3566. makeDynamic :=
  3567. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3568. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3569. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3570. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3571. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3572. END;
  3573. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3574. mathArrayType.SetArrayBase(newBaseType)
  3575. END
  3576. END SetIndexBaseType;
  3577. (** check and append index list element to index designator of math array
  3578. - check validity of single index or array range
  3579. - compute new type
  3580. - if range then create new array type (calculate length of resulting array)
  3581. - otherwise take sourceArray.arrayBase as new type
  3582. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3583. **)
  3584. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3585. VAR
  3586. targetArray: SyntaxTree.MathArrayType;
  3587. first, last, step: SyntaxTree.Expression;
  3588. firstValue, lastValue, stepValue, length: LONGINT;
  3589. rangeExpression: SyntaxTree.RangeExpression;
  3590. BEGIN
  3591. IF indexListItem.type = SyntaxTree.invalidType THEN
  3592. (* error already handled *)
  3593. indexDesignator.parameters.AddExpression(indexListItem)
  3594. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3595. indexDesignator.HasRange;
  3596. indexDesignator.HasTensorRange;
  3597. indexDesignator.parameters.AddExpression(indexListItem);
  3598. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3599. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3600. IndexCheck(indexListItem, sourceArray.length);
  3601. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3602. indexDesignator.parameters.AddExpression(indexListItem)
  3603. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3604. indexDesignator.HasRange;
  3605. (* if the range is given as an array range expression, check the validity of its components *)
  3606. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3607. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3608. first := rangeExpression.first;
  3609. last := rangeExpression.last;
  3610. step := rangeExpression.step;
  3611. (* perform static checks on range components *)
  3612. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3613. Error(indexListItem.position,"lower bound of array range too small")
  3614. END;
  3615. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3616. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3617. Error(indexListItem.position,"upper bound of array range too large")
  3618. END
  3619. END;
  3620. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3621. Error(indexListItem.position,"invalid step size")
  3622. END;
  3623. (* add conversions to size type *)
  3624. (* TODO: needed? *)
  3625. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3626. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3627. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3628. END;
  3629. IF indexDesignator.hasTensorRange THEN
  3630. (* the index designator's base type is a tensor: leave it as is *)
  3631. ELSE
  3632. (* append a new math array to the index designator's base type *)
  3633. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3634. IF ~error THEN
  3635. (*
  3636. (* optimization: calculate length of target array for static ranges *)
  3637. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3638. IF IsStaticallyOpenRange(rangeExpression) THEN
  3639. (* range is open ('*'): reuse source array length as target array length *)
  3640. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3641. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3642. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3643. IF lastValue = MAX(LONGINT) THEN
  3644. IF IsIntegerValue(sourceArray.length, length) THEN
  3645. lastValue := length - 1;
  3646. isStaticTargetArrayLength := TRUE
  3647. ELSE
  3648. isStaticTargetArrayLength := FALSE
  3649. END
  3650. ELSE
  3651. isStaticTargetArrayLength := TRUE
  3652. END;
  3653. IF isStaticTargetArrayLength THEN
  3654. (* calculate static target array length *)
  3655. IF firstValue > lastValue THEN
  3656. length := 0
  3657. ELSE
  3658. length := 1 + lastValue - firstValue;
  3659. IF length MOD stepValue = 0 THEN
  3660. length := length DIV stepValue
  3661. ELSE
  3662. length := length DIV stepValue + 1
  3663. END
  3664. END;
  3665. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3666. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3667. ASSERT(targetArray.form = SyntaxTree.Static)
  3668. END
  3669. END
  3670. END
  3671. *)
  3672. END;
  3673. SetIndexBaseType(indexDesignator, targetArray)
  3674. END;
  3675. indexDesignator.parameters.AddExpression(indexListItem)
  3676. ELSE
  3677. Error(position,"invalid index list item");
  3678. END;
  3679. END AppendMathIndex;
  3680. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3681. VAR parameters: SyntaxTree.ExpressionList;
  3682. BEGIN
  3683. parameters := index.parameters;
  3684. IF (expression.type = NIL) THEN
  3685. Error(position, "invalid index");
  3686. ELSIF IsIntegerType(expression.type.resolved) THEN
  3687. IF over IS SyntaxTree.ArrayType THEN
  3688. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3689. ELSIF over IS SyntaxTree.StringType THEN
  3690. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3691. END;
  3692. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3693. parameters.AddExpression(expression);
  3694. ELSE
  3695. Error(position, "invalid index");
  3696. END;
  3697. END AppendIndex;
  3698. (** convert an expression to math array type
  3699. if expression is of math array type: return expression itself
  3700. if expression is of array-structured object type: return an index operator call on it
  3701. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3702. otherwise: return invalid expression
  3703. **)
  3704. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3705. VAR
  3706. result: SyntaxTree.Expression;
  3707. mathArrayType: SyntaxTree.MathArrayType;
  3708. BEGIN
  3709. IF expression.type = NIL THEN
  3710. result := SyntaxTree.invalidExpression
  3711. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3712. (* expression of math array type *)
  3713. result := expression
  3714. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3715. (* expression of array-structured object type *)
  3716. mathArrayType := MathArrayStructureOfType(expression.type);
  3717. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3718. ELSE
  3719. result := SyntaxTree.invalidExpression
  3720. END;
  3721. RETURN result
  3722. END ConvertToMathArray;
  3723. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3724. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3725. VAR
  3726. result: SyntaxTree.ExpressionList;
  3727. i: LONGINT;
  3728. BEGIN
  3729. result := SyntaxTree.NewExpressionList();
  3730. FOR i := 1 TO itemCount DO
  3731. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3732. END;
  3733. RETURN result
  3734. END ListOfOpenRanges;
  3735. (** create a procedure call designator for an index operator call on an array-structured object type
  3736. - use given index list as actual parameters
  3737. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3738. **)
  3739. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3740. VAR
  3741. operator: SyntaxTree.Operator;
  3742. expression: SyntaxTree.Expression;
  3743. actualParameters, tempList: SyntaxTree.ExpressionList;
  3744. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3745. result, tempDesignator: SyntaxTree.Designator;
  3746. recordType: SyntaxTree.RecordType;
  3747. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3748. i, hashValue, indexListSize, indexListKind: LONGINT;
  3749. castReturnType: SyntaxTree.MathArrayType;
  3750. BEGIN
  3751. ASSERT(IsArrayStructuredObjectType(left.type));
  3752. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3753. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3754. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3755. indexListSize := indexList.Length();
  3756. indexListKind := 0;
  3757. containsNonRange := FALSE;
  3758. FOR i := 0 TO indexList.Length() - 1 DO
  3759. indexListKind := indexListKind * 2;
  3760. expression := indexList.GetExpression(i);
  3761. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3762. INC(indexListKind)
  3763. ELSE
  3764. containsNonRange := TRUE
  3765. END
  3766. END;
  3767. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3768. (* select applicable index operator
  3769. - try to look up optimal index operator
  3770. - if not present, use operator on ranges
  3771. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3772. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3773. *)
  3774. usesGeneralOperator := FALSE;
  3775. IF rhs # NIL THEN
  3776. (* write operator *)
  3777. IF hashValue = -1 THEN
  3778. operator := NIL
  3779. ELSE
  3780. operator := recordType.arrayAccessOperators.write[hashValue];
  3781. END;
  3782. IF operator = NIL THEN
  3783. usesPureRangeOperator := TRUE;
  3784. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3785. operator := recordType.arrayAccessOperators.generalWrite;
  3786. usesGeneralOperator := TRUE
  3787. ELSE
  3788. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3789. operator := recordType.arrayAccessOperators.write[hashValue];
  3790. END
  3791. END
  3792. ELSE
  3793. (* read operator *)
  3794. IF hashValue = -1 THEN
  3795. operator := NIL
  3796. ELSE
  3797. operator := recordType.arrayAccessOperators.read[hashValue];
  3798. END;
  3799. IF operator = NIL THEN
  3800. usesPureRangeOperator := TRUE;
  3801. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3802. operator := recordType.arrayAccessOperators.generalRead;
  3803. usesGeneralOperator := TRUE
  3804. ELSE
  3805. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3806. operator := recordType.arrayAccessOperators.read[hashValue];
  3807. END
  3808. END
  3809. END;
  3810. IF operator = NIL THEN
  3811. Error(position, "call of undeclared [] operator");
  3812. result := SyntaxTree.invalidDesignator;
  3813. ELSE
  3814. (* determine if reshaping is needed *)
  3815. needsReshaping := containsNonRange & usesPureRangeOperator;
  3816. (* import OCArrayBase if reshaping is needed *)
  3817. IF needsReshaping & ~arrayBaseImported THEN
  3818. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3819. arrayBaseImported := TRUE
  3820. END;
  3821. (* add the index list item to the list of actual parameters
  3822. - for general operators: add a single inline array containing the index list items as parameter
  3823. - otherwise: add all index list items as individual parameters
  3824. *)
  3825. actualParameters := SyntaxTree.NewExpressionList();
  3826. IF usesGeneralOperator THEN
  3827. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3828. END;
  3829. FOR i := 0 TO indexListSize - 1 DO
  3830. expression := indexList.GetExpression(i);
  3831. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3832. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3833. tempList := SyntaxTree.NewExpressionList();
  3834. tempList.AddExpression(expression);
  3835. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3836. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3837. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3838. END;
  3839. IF usesGeneralOperator THEN
  3840. tempMathArrayExpression.elements.AddExpression(expression);
  3841. ELSE
  3842. actualParameters.AddExpression(expression)
  3843. END
  3844. END;
  3845. IF usesGeneralOperator THEN
  3846. actualParameters.AddExpression(tempMathArrayExpression)
  3847. END;
  3848. IF rhs # NIL THEN
  3849. (* add actual parameter for RHS *)
  3850. IF needsReshaping THEN
  3851. (* reshape using OCArrayBase.ExpandDimensions *)
  3852. tempList := SyntaxTree.NewExpressionList();
  3853. (* source array *)
  3854. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3855. tempList.AddExpression(rhs);
  3856. ELSE
  3857. (* convert scalar to one-dimensional array *)
  3858. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3859. tempMathArrayExpression.elements.AddExpression(rhs);
  3860. tempList.AddExpression(tempMathArrayExpression)
  3861. END;
  3862. (* list of kept dimensions *)
  3863. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3864. FOR i := 0 TO indexListSize - 1 DO
  3865. expression := indexList.GetExpression(i);
  3866. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3867. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3868. ELSE
  3869. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3870. END
  3871. END;
  3872. tempList.AddExpression(tempMathArrayExpression);
  3873. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3874. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3875. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3876. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3877. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3878. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3879. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3880. expression.SetType(castReturnType);
  3881. ELSE
  3882. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3883. END;
  3884. actualParameters.AddExpression(expression)
  3885. ELSE
  3886. actualParameters.AddExpression(rhs)
  3887. END
  3888. END;
  3889. (* add dereference operator and create procedure call designator *)
  3890. ASSERT(left IS SyntaxTree.Designator);
  3891. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3892. ASSERT(expression IS SyntaxTree.Designator);
  3893. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3894. IF (rhs = NIL) & needsReshaping THEN
  3895. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3896. tempList := SyntaxTree.NewExpressionList();
  3897. FOR i := 0 TO indexList.Length() - 1 DO
  3898. expression := indexList.GetExpression(i);
  3899. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3900. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3901. ELSE
  3902. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3903. END
  3904. END;
  3905. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3906. END;
  3907. IF rhs = NIL THEN
  3908. (* special rule: index read operator calls are considered to be assignable *)
  3909. result.SetAssignable(TRUE)
  3910. END;
  3911. (* put information about this index operator call into the resulting designator *)
  3912. result.SetRelatedAsot(left);
  3913. result.SetRelatedIndexList(indexList)
  3914. END;
  3915. RETURN result
  3916. END NewIndexOperatorCall;
  3917. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3918. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3919. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3920. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3921. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3922. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3923. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3924. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3925. CONST trace = FALSE;
  3926. BEGIN
  3927. IF trace THEN
  3928. FOR i := 0 TO actualParameters.Length()-1 DO
  3929. Printout.Info("par", actualParameters.GetExpression(i));
  3930. END;
  3931. END;
  3932. operator := scope.firstOperator;
  3933. WHILE(operator # NIL) DO
  3934. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3935. procedureType := operator.type(SyntaxTree.ProcedureType);
  3936. distance := Distance(system, procedureType,actualParameters);
  3937. IF trace THEN Printout.Info("check op ",operator) END;
  3938. IF distance < bestDistance THEN
  3939. IF trace THEN Printout.Info("taken op",operator) END;
  3940. bestDistance := distance;
  3941. bestOperator := operator;
  3942. END;
  3943. END;
  3944. operator := operator.nextOperator;
  3945. END;
  3946. END FindInScope;
  3947. BEGIN
  3948. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3949. IF oper = 0 THEN (* index *)
  3950. identifier := SyntaxTree.NewIdentifier("[]");
  3951. ELSE
  3952. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3953. END;
  3954. WHILE (recordType # NIL) DO
  3955. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3956. recordType := recordType.GetBaseRecord();
  3957. END;
  3958. RETURN bestOperator
  3959. END FindOperator;
  3960. BEGIN
  3961. type := left.type.resolved;
  3962. IF type IS SyntaxTree.RecordType THEN
  3963. pointer := FALSE;
  3964. recordType := type(SyntaxTree.RecordType);
  3965. ELSE
  3966. pointer := TRUE;
  3967. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3968. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3969. END;
  3970. actualParameters := SyntaxTree.NewExpressionList();
  3971. IF parameters # NIL THEN
  3972. FOR i := 0 TO parameters.Length()-1 DO
  3973. expression := ResolveExpression(parameters.GetExpression(i));
  3974. actualParameters.AddExpression(expression);
  3975. END;
  3976. END;
  3977. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3978. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3979. IF op # NIL THEN
  3980. designator := left(SyntaxTree.Designator);
  3981. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3982. expression := NewSymbolDesignator(position, designator , op);
  3983. ASSERT(expression IS SyntaxTree.Designator);
  3984. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3985. result.SetRelatedAsot(left);
  3986. result.SetRelatedIndexList(parameters);
  3987. (* check if write operator exists, for var parameters *)
  3988. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3989. actualParameters := SyntaxTree.NewExpressionList();
  3990. FOR i := 0 TO parameters.Length()-1 DO
  3991. expression := ResolveExpression(parameters.GetExpression(i));
  3992. actualParameters.AddExpression(expression);
  3993. END;
  3994. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3995. actualParameters.AddExpression(rhs);
  3996. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3997. IF op = NIL THEN rhs := NIL END;
  3998. END;
  3999. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4000. ELSE
  4001. result := NIL;
  4002. END;
  4003. RETURN result;
  4004. END NewObjectOperatorCall;
  4005. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4006. 1. convert bracket designator chains into a single one that contains separators
  4007. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4008. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4009. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4010. - if an array or math array is indexed over, create index designator
  4011. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4012. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4013. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4014. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4015. - if an array-structured object type is indexed over, create procedure call designator
  4016. e.g.: a[x, y] -> a^."[]"(x, y)
  4017. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4018. - a[i, *] = a[i][*]
  4019. - a[*, i] # a[*][i]
  4020. Because:
  4021. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4022. - 'i-th column' = a[*, i]
  4023. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4024. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4025. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4026. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4027. **)
  4028. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4029. VAR
  4030. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4031. indexDesignator: SyntaxTree.IndexDesignator;
  4032. designator: SyntaxTree.Designator;
  4033. type: SyntaxTree.Type;
  4034. recordType: SyntaxTree.RecordType;
  4035. expression, rhs: SyntaxTree.Expression;
  4036. indexList: SyntaxTree.ExpressionList;
  4037. i: LONGINT;
  4038. hasError, done: BOOLEAN;
  4039. PROCEDURE FinalizeIndexDesignator;
  4040. BEGIN
  4041. IF indexDesignator # NIL THEN
  4042. (* the end of a tensor has been reached: *)
  4043. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4044. SetIndexBaseType(indexDesignator, type);
  4045. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4046. designator := indexDesignator;
  4047. type := designator.type.resolved;
  4048. indexDesignator := NIL;
  4049. ASSERT(SyntaxTree.Resolved IN type.state)
  4050. END
  4051. END FinalizeIndexDesignator;
  4052. BEGIN
  4053. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4054. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4055. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4056. (* copy all index list entries including a separator to the left bracket designator *)
  4057. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4058. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4059. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4060. END;
  4061. (* propagate the related RHS *)
  4062. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4063. (* only resolve left bracket designator and use as final result *)
  4064. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4065. ELSE
  4066. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4067. designator := ResolveDesignator(bracketDesignator.left);
  4068. type := designator.type.resolved;
  4069. indexDesignator := NIL;
  4070. (*!!! clean up *)
  4071. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4072. OR (type IS SyntaxTree.RecordType)
  4073. THEN
  4074. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4075. IF resolvedExpression = NIL THEN
  4076. Error(bracketDesignator.position,"undefined operator");
  4077. resolvedExpression := SyntaxTree.invalidDesignator
  4078. END;
  4079. RETURN;
  4080. END;
  4081. i := 0;
  4082. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4083. expression := bracketDesignator.parameters.GetExpression(i);
  4084. expression := ResolveExpression(expression);
  4085. bracketDesignator.parameters.SetExpression(i, expression);
  4086. IF expression = SyntaxTree.indexListSeparator THEN
  4087. (* finalize an existing index designator if needed *)
  4088. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4089. INC(i)
  4090. ELSE
  4091. (* do auto-dereferencing if needed *)
  4092. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4093. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4094. & (i=0)*)
  4095. THEN
  4096. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4097. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4098. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4099. designator := SyntaxTree.invalidDesignator;
  4100. type := SyntaxTree.invalidType
  4101. ELSE
  4102. FinalizeIndexDesignator;
  4103. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4104. type := designator.type.resolved
  4105. END
  4106. END;
  4107. (* create a new index designator, if needed *)
  4108. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4109. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4110. indexDesignator.SetAssignable(designator.assignable);
  4111. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4112. (* designator := indexDesignator *)
  4113. END;
  4114. IF type = SyntaxTree.invalidType THEN
  4115. (* error already handled *)
  4116. INC(i)
  4117. ELSIF type IS SyntaxTree.ArrayType THEN
  4118. (* indexing over an array *)
  4119. ASSERT(indexDesignator # NIL);
  4120. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4121. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4122. INC(i)
  4123. ELSIF type IS SyntaxTree.StringType THEN
  4124. (* indexing over an array *)
  4125. ASSERT(indexDesignator # NIL);
  4126. AppendIndex(expression.position, indexDesignator, expression, type);
  4127. type := type(SyntaxTree.StringType).baseType.resolved;
  4128. INC(i)
  4129. ELSIF type IS SyntaxTree.MathArrayType THEN
  4130. (* indexing over a math array *)
  4131. ASSERT(indexDesignator # NIL);
  4132. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4133. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4134. INC(i)
  4135. ELSIF IsArrayStructuredObjectType(type) THEN
  4136. (* indexing over ASOTs *)
  4137. FinalizeIndexDesignator;
  4138. ASSERT(type IS SyntaxTree.PointerType);
  4139. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4140. (*
  4141. - collect index list items from bracket designator that belong to ASOT
  4142. - check for errors
  4143. *)
  4144. indexList := SyntaxTree.NewExpressionList();
  4145. hasError := FALSE;
  4146. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4147. (* indexing over tensor ASOT:
  4148. - stop at index list end or separator
  4149. - dimensionality is given by number of index list items
  4150. *)
  4151. done := FALSE;
  4152. WHILE ~done DO
  4153. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4154. done := TRUE;
  4155. ELSE
  4156. expression := bracketDesignator.parameters.GetExpression(i);
  4157. IF expression = SyntaxTree.indexListSeparator THEN
  4158. done := TRUE;
  4159. ELSE
  4160. expression := ResolveExpression(expression);
  4161. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4162. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4163. hasError := TRUE
  4164. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4165. Error(expression.position, "integer or range expected");
  4166. expression := SyntaxTree.invalidExpression;
  4167. hasError := TRUE
  4168. END;
  4169. indexList.AddExpression(expression)
  4170. END;
  4171. INC(i)
  4172. END
  4173. END
  4174. ELSE
  4175. (* indexing over non-tensor ASOT:
  4176. - ignore separators
  4177. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4178. *)
  4179. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4180. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4181. expression := bracketDesignator.parameters.GetExpression(i);
  4182. ELSE
  4183. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4184. END;
  4185. IF expression # SyntaxTree.indexListSeparator THEN
  4186. expression := ResolveExpression(expression);
  4187. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4188. Error(expression.position, "integer or range expected");
  4189. expression := SyntaxTree.invalidExpression;
  4190. hasError := TRUE
  4191. END;
  4192. indexList.AddExpression(expression)
  4193. END;
  4194. INC(i)
  4195. END;
  4196. END;
  4197. IF hasError THEN
  4198. designator := SyntaxTree.invalidDesignator;
  4199. type := SyntaxTree.invalidType;
  4200. ELSE
  4201. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4202. and the last entry in the index list belongs to the array-structured object type in question.
  4203. E.g.: for a 2-dimensional array-structured object type:
  4204. - 'lhs := asot[1, 2]' -> read mode
  4205. - 'asot[1, 2] := rhs' -> write mode
  4206. - 'asot[1, 2, 3] := rhs' -> read mode
  4207. *)
  4208. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4209. rhs := bracketDesignator.relatedRhs
  4210. ELSE
  4211. rhs := NIL
  4212. END;
  4213. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4214. type := designator.type
  4215. END
  4216. ELSE
  4217. Error(expression.position,"indexing over non-array type");
  4218. designator := SyntaxTree.invalidDesignator;
  4219. type := SyntaxTree.invalidType;
  4220. INC(i)
  4221. END
  4222. END
  4223. END;
  4224. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4225. resolvedExpression := designator
  4226. END
  4227. END VisitBracketDesignator;
  4228. (** check and resolve expression list
  4229. - resolve each expression in an expression list
  4230. - returns true if and only if all statements could have successfully been resolved
  4231. **)
  4232. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4233. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4234. BEGIN
  4235. result := TRUE;
  4236. FOR i := 0 TO expressionList.Length()-1 DO
  4237. expression := ResolveExpression(expressionList.GetExpression(i));
  4238. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4239. expressionList.SetExpression(i,expression);
  4240. END;
  4241. RETURN result
  4242. END ExpressionList;
  4243. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4244. BEGIN
  4245. type := type.resolved;
  4246. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4247. RETURN TRUE
  4248. ELSIF system.CanPassInRegister # NIL THEN
  4249. RETURN system.CanPassInRegister(type);
  4250. ELSE
  4251. RETURN FALSE
  4252. END;
  4253. END CanPassInRegister;
  4254. (** return procedure call designator left(actualParameters)
  4255. - check realtime procedure call in realtime procedure
  4256. - check number of parameters
  4257. - check parameter compatibility
  4258. return invalidDesignator if error
  4259. **)
  4260. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4261. VAR result: SyntaxTree.Designator;
  4262. numberFormalParameters, numberActualParameters: LONGINT;
  4263. formalType: SyntaxTree.ProcedureType;
  4264. formalParameter: SyntaxTree.Parameter;
  4265. actualParameter: SyntaxTree.Expression;
  4266. i: LONGINT;
  4267. self: SyntaxTree.Expression;
  4268. BEGIN
  4269. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4270. result := SyntaxTree.invalidDesignator;
  4271. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4272. numberFormalParameters := formalType.numberParameters;
  4273. numberActualParameters := actualParameters.Length();
  4274. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4275. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4276. END;
  4277. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4278. self := left.left;
  4279. IF (self # NIL) & ~IsVariable(self) THEN
  4280. Error(self.position, "Non-variable expression on variable receiver");
  4281. END;
  4282. END;
  4283. IF ~ExpressionList(actualParameters) THEN
  4284. result := SyntaxTree.invalidDesignator
  4285. ELSE
  4286. IF numberActualParameters <= numberFormalParameters THEN
  4287. formalParameter := formalType.firstParameter;
  4288. FOR i := 0 TO numberActualParameters-1 DO
  4289. actualParameter := actualParameters.GetExpression(i);
  4290. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4291. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4292. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4293. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4294. ELSE
  4295. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4296. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4297. END;
  4298. actualParameters.SetExpression(i,actualParameter);
  4299. END;
  4300. formalParameter := formalParameter.nextParameter;
  4301. END;
  4302. WHILE (formalParameter # NIL) DO
  4303. IF formalParameter.defaultValue # NIL THEN
  4304. actualParameters.AddExpression(formalParameter.defaultValue);
  4305. formalParameter := formalParameter.nextParameter
  4306. ELSE
  4307. Error(position, "less actual than formal parameters");
  4308. formalParameter := NIL;
  4309. END;
  4310. END;
  4311. ELSE
  4312. Error(position, "more actual than formal parameters")
  4313. END;
  4314. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4315. result.SetAssignable(FALSE);
  4316. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4317. END;
  4318. RETURN result
  4319. END NewProcedureCallDesignator;
  4320. (**
  4321. builtin call designator generated in VisitParameterDesignator
  4322. -> nothing to be resolved
  4323. **)
  4324. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4325. BEGIN
  4326. resolvedExpression := x;
  4327. END VisitTypeGuardDesignator;
  4328. (**
  4329. builtin call designator generated in VisitParameterDesignator
  4330. -> nothing to be resolved
  4331. **)
  4332. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4333. BEGIN
  4334. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4335. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4336. ASSERT(resolvedExpression.type # NIL);
  4337. ELSIF ExpressionList(x.parameters) THEN
  4338. resolvedExpression := x;
  4339. END;
  4340. END VisitBuiltinCallDesignator;
  4341. (**
  4342. procedure call designator generated in VisitParameterDesignator
  4343. -> nothing to be resolved
  4344. **)
  4345. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4346. BEGIN
  4347. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4348. resolvedExpression := x;
  4349. END VisitProcedureCallDesignator;
  4350. (** return true if x is a variable else return false and report error **)
  4351. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4352. VAR result: BOOLEAN;
  4353. BEGIN
  4354. result := TRUE;
  4355. IF x = SyntaxTree.invalidExpression THEN
  4356. result := FALSE;
  4357. ELSIF ~IsVariable(x) THEN
  4358. Error(x.position,"non variable expression");
  4359. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4360. result := FALSE;
  4361. END;
  4362. RETURN result
  4363. END CheckVariable;
  4364. (**
  4365. if expression x is of basic type then return true else report error and return false
  4366. **)
  4367. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN;
  4369. BEGIN
  4370. result := FALSE;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. ELSIF ~IsBasicType(x.type) THEN
  4373. Error(x.position,"is no basic type");
  4374. result := FALSE
  4375. ELSE result := TRUE
  4376. END;
  4377. RETURN result
  4378. END CheckBasicType;
  4379. (**
  4380. if expression x is of number type then return true else report error and return false
  4381. **)
  4382. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN;
  4384. BEGIN
  4385. result := FALSE;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4388. Error(x.position,"is non number type");
  4389. ELSE result := TRUE
  4390. END;
  4391. RETURN result
  4392. END CheckNumberType;
  4393. (**
  4394. if expression x is of number or size type but not complex then return true else report error and return false
  4395. **)
  4396. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4397. VAR result: BOOLEAN;
  4398. BEGIN
  4399. result := FALSE;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4402. Error(x.position,"is complex type");
  4403. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4404. Error(x.position,"is non number type");
  4405. ELSE result := TRUE
  4406. END;
  4407. RETURN result
  4408. END CheckNonComplexNumberSizeType;
  4409. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4411. BEGIN
  4412. result := FALSE; type := x.type.resolved;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. 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
  4415. TRACE(type.sizeInBits);
  4416. TRACE(system.addressType.sizeInBits);
  4417. Error(x.position,"is no address type");
  4418. ELSE result := TRUE
  4419. END;
  4420. RETURN result
  4421. END CheckAddressType;
  4422. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4423. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4424. BEGIN
  4425. result := FALSE; type := x.type.resolved;
  4426. IF x = SyntaxTree.invalidExpression THEN
  4427. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4428. Error(x.position,"is no size type");
  4429. ELSE result := TRUE
  4430. END;
  4431. RETURN result
  4432. END CheckSizeType;
  4433. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4434. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4435. BEGIN
  4436. result := FALSE; type := x.type.resolved;
  4437. IF x = SyntaxTree.invalidExpression THEN
  4438. 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
  4439. Error(x.position,"is no object type");
  4440. ELSE result := TRUE
  4441. END;
  4442. RETURN result
  4443. END CheckObjectType;
  4444. (**
  4445. if expression x is of integer type then return true else report error and return false
  4446. **)
  4447. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4448. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4449. BEGIN
  4450. result := FALSE; type := x.type.resolved;
  4451. IF x = SyntaxTree.invalidExpression THEN
  4452. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4453. Error(x.position,"is no integer type");
  4454. ELSE result := TRUE
  4455. END;
  4456. RETURN result
  4457. END CheckIntegerType;
  4458. (**
  4459. if expression x is of character type then return true else report error and return false
  4460. **)
  4461. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4462. VAR result: BOOLEAN;
  4463. BEGIN
  4464. result := FALSE;
  4465. IF x = SyntaxTree.invalidExpression THEN
  4466. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4467. Error(x.position,"is no character type");
  4468. ELSE result := TRUE
  4469. END;
  4470. RETURN result
  4471. END CheckCharacterType;
  4472. (**
  4473. if expression x is of real type then return true else report error and return false
  4474. **)
  4475. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4476. VAR result: BOOLEAN;
  4477. BEGIN
  4478. result := FALSE;
  4479. IF x = SyntaxTree.invalidExpression THEN
  4480. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4481. Error(x.position,"is no float type");
  4482. ELSE result := TRUE
  4483. END;
  4484. RETURN result
  4485. END CheckRealType;
  4486. (**
  4487. if expression x is of range type then return true else report error and return false
  4488. **)
  4489. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4490. VAR result: BOOLEAN;
  4491. BEGIN
  4492. result := FALSE;
  4493. IF x = SyntaxTree.invalidExpression THEN
  4494. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4495. Error(x.position,"is no range type");
  4496. ELSE result := TRUE
  4497. END;
  4498. RETURN result
  4499. END CheckRangeType;
  4500. (**
  4501. if expression x is of boolean type then return true else report error and return false
  4502. **)
  4503. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4509. Error(x.position,"is no boolean type");
  4510. ELSE result := TRUE
  4511. END;
  4512. RETURN result
  4513. END CheckBooleanType;
  4514. (**
  4515. if expression x is of set type then return true else report error and return false
  4516. **)
  4517. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4518. VAR result: BOOLEAN;
  4519. BEGIN
  4520. result := FALSE;
  4521. IF x = SyntaxTree.invalidExpression THEN
  4522. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4523. Error(x.position,"is no set type");
  4524. ELSE result := TRUE
  4525. END;
  4526. RETURN result
  4527. END CheckSetType;
  4528. (**
  4529. if expression x is of string or array of character type then return true else report error and return false
  4530. **)
  4531. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4532. VAR result: BOOLEAN;
  4533. BEGIN
  4534. result := FALSE;
  4535. IF x = SyntaxTree.invalidExpression THEN
  4536. ELSIF ~IsStringType(x.type.resolved) THEN
  4537. Error(x.position,"is no string type");
  4538. ELSE result := TRUE
  4539. END;
  4540. RETURN result
  4541. END CheckStringType;
  4542. (**
  4543. if expression x is a type declaration type return true else report error and return false
  4544. **)
  4545. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4546. VAR result: BOOLEAN;
  4547. BEGIN
  4548. result := FALSE;
  4549. IF x = SyntaxTree.invalidExpression THEN
  4550. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4551. Error(x.position,"is not a type declaration");
  4552. ELSE result := TRUE
  4553. END;
  4554. RETURN result
  4555. END CheckTypeDeclarationType;
  4556. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4557. VAR result: BOOLEAN;
  4558. BEGIN
  4559. result := FALSE;
  4560. IF x = SyntaxTree.invalidExpression THEN
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4562. result := TRUE;
  4563. value := x.resolved(SyntaxTree.IntegerValue).value;
  4564. ELSE
  4565. Error(x.position,"expression is not an integer constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckIntegerValue;
  4569. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4570. VAR result: BOOLEAN;
  4571. BEGIN
  4572. result := FALSE;
  4573. IF x = SyntaxTree.invalidExpression THEN
  4574. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4575. result := TRUE;
  4576. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4577. ELSE
  4578. Error(x.position,"expression is not an integer constant");
  4579. END;
  4580. RETURN result;
  4581. END CheckStringValue;
  4582. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4583. BEGIN
  4584. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4585. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4586. ELSE
  4587. RETURN FALSE
  4588. END;
  4589. END IsUnsignedValue;
  4590. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4591. BEGIN
  4592. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4593. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4594. ELSE
  4595. RETURN FALSE
  4596. END
  4597. END IsAddressValue;
  4598. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4599. BEGIN
  4600. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4601. END IsAddressExpression;
  4602. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4603. BEGIN
  4604. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4605. END IsSizeExpression;
  4606. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4607. VAR result: BOOLEAN;
  4608. BEGIN
  4609. result := FALSE;
  4610. IF x = SyntaxTree.invalidExpression THEN
  4611. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4612. result := TRUE;
  4613. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4614. ELSE
  4615. Error(x.position,"expression is not an integer constant");
  4616. END;
  4617. RETURN result;
  4618. END CheckEnumerationValue;
  4619. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4620. VAR result: BOOLEAN;
  4621. BEGIN
  4622. result := FALSE;
  4623. IF x = SyntaxTree.invalidExpression THEN
  4624. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4625. result := TRUE;
  4626. value := x.resolved(SyntaxTree.CharacterValue).value;
  4627. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4628. result := TRUE;
  4629. value := x.resolved(SyntaxTree.StringValue).value[0];
  4630. ELSE
  4631. Error(x.position,"expression is not a character constant");
  4632. END;
  4633. RETURN result;
  4634. END CheckCharacterValue;
  4635. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4636. VAR result: BOOLEAN;
  4637. BEGIN
  4638. result := FALSE;
  4639. IF x = SyntaxTree.invalidExpression THEN
  4640. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4641. value := x.resolved(SyntaxTree.IntegerValue).value;
  4642. IF (value > 0) OR includeZero & (value = 0) THEN
  4643. result := TRUE;
  4644. ELSE
  4645. Error(x.position,"integer is not positive");
  4646. END
  4647. ELSE
  4648. Error(x.position,"expression is not an integer constant");
  4649. END;
  4650. RETURN result;
  4651. END CheckPositiveIntegerValue;
  4652. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4653. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4654. BEGIN
  4655. result := FALSE;
  4656. IF x = SyntaxTree.invalidExpression THEN
  4657. ELSE
  4658. type := x.type.resolved;
  4659. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4660. portType := type(SyntaxTree.PortType);
  4661. result := TRUE
  4662. ELSE
  4663. Error(x.position,"no port type");
  4664. END;
  4665. END;
  4666. RETURN result
  4667. END CheckPortType;
  4668. (* move to builtin procedure call statement ?
  4669. remove builtin procedure call designator ?
  4670. *)
  4671. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4672. VAR
  4673. numberActualParameters,numberFormalParameters: LONGINT;
  4674. formalParameter: SyntaxTree.Parameter;
  4675. actualParameter: SyntaxTree.Expression;
  4676. procedureType: SyntaxTree.ProcedureType;
  4677. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4678. inPort, outPort: SyntaxTree.PortType;
  4679. constructor: SyntaxTree.Procedure;
  4680. type0,type1,type2: SyntaxTree.Type;
  4681. type,base,parameterType: SyntaxTree.Type;
  4682. arrayType: SyntaxTree.ArrayType;
  4683. i,i0,i1: LONGINT;
  4684. r,r0,r1,im: LONGREAL;
  4685. c: CHAR;
  4686. id: LONGINT;
  4687. b: BOOLEAN;
  4688. first: LONGINT;
  4689. mathArrayType: SyntaxTree.MathArrayType;
  4690. customBuiltin: SyntaxTree.CustomBuiltin;
  4691. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4692. VAR resultB: BOOLEAN;
  4693. BEGIN
  4694. IF numberActualParameters < from THEN
  4695. Error(position, "less actual than formal parameters");
  4696. result := SyntaxTree.invalidExpression;
  4697. resultB := FALSE;
  4698. ELSIF numberActualParameters > to THEN
  4699. Error(position, "more actual than formal parameters");
  4700. result := SyntaxTree.invalidExpression;
  4701. resultB := FALSE;
  4702. ELSE
  4703. resultB := TRUE;
  4704. END;
  4705. RETURN resultB
  4706. END CheckArity;
  4707. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4708. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4709. BEGIN
  4710. WHILE modifier # NIL DO
  4711. symbol := cellType.FindProperty(modifier.identifier);
  4712. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4713. propertyType := symbol.type.resolved;
  4714. modifierType := modifier.expression.type.resolved;
  4715. IF ~CompatibleTo(system, modifierType, propertyType) &
  4716. ~(
  4717. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4718. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4719. Error(modifier.position,"incompatible to cell property");
  4720. END;
  4721. ELSE
  4722. Error(modifier.position, "undefined property");
  4723. END;
  4724. modifier := modifier.nextModifier;
  4725. END;
  4726. END CheckModifiers;
  4727. BEGIN
  4728. type := NIL; result := NIL;
  4729. type0 := NIL; type1 := NIL; type2 := NIL;
  4730. numberActualParameters := actualParameters.Length();
  4731. IF numberActualParameters>0 THEN
  4732. parameter0 := actualParameters.GetExpression(0);
  4733. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4734. Error(parameter0.position,"forbidden type-less argument");
  4735. result := SyntaxTree.invalidExpression
  4736. END
  4737. END;
  4738. IF numberActualParameters >1 THEN
  4739. parameter1 := actualParameters.GetExpression(1);
  4740. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4741. ELSE
  4742. Error(parameter1.position,"forbidden type-less argument");
  4743. result := SyntaxTree.invalidExpression
  4744. END
  4745. END;
  4746. IF numberActualParameters >2 THEN
  4747. parameter2 := actualParameters.GetExpression(2);
  4748. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4749. ELSE
  4750. Error(parameter2.position,"forbidden type-less argument");
  4751. result := SyntaxTree.invalidExpression
  4752. END
  4753. END;
  4754. IF returnType # NIL THEN
  4755. id := Global.New;
  4756. result := NIL;
  4757. ELSE
  4758. id := builtin.id;
  4759. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4760. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4761. END;
  4762. END;
  4763. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4764. ELSIF result # NIL THEN type := result.type (* operator *)
  4765. ELSE
  4766. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4767. result(SyntaxTree.Designator).SetLeft(left);
  4768. IF returnType # NIL THEN
  4769. type := returnType;
  4770. END;
  4771. (* ---- ASSERT ----- *)
  4772. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4773. IF CheckBooleanType(parameter0) THEN
  4774. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4775. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4776. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4777. *)
  4778. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4779. Error(position, "assert failed");
  4780. END;
  4781. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4782. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4783. rules imposed by the architecture / current runtime
  4784. *)
  4785. END;
  4786. END;
  4787. (* ---- COPY ----- *)
  4788. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4789. IF~IsStringType(type0) THEN
  4790. Error(parameter0.position,"no string type");
  4791. END;
  4792. IF ~IsStringType(type1) THEN
  4793. Error(parameter1.position,"no string type");
  4794. ELSIF CheckVariable(parameter1) THEN
  4795. IF (type0 IS SyntaxTree.StringType) THEN
  4796. arrayType := type1(SyntaxTree.ArrayType);
  4797. IF arrayType.form = SyntaxTree.Static THEN
  4798. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4799. Error(position, "destination length smaller than source length")
  4800. END;
  4801. END;
  4802. END;
  4803. END;
  4804. (* ---- INC, DEC----- *)
  4805. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4806. IF numberActualParameters = 1 THEN
  4807. parameter1 :=Global.NewIntegerValue(system,position,1);
  4808. actualParameters.AddExpression(parameter1);
  4809. END;
  4810. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4811. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4812. Error(position, "incompatible increment");
  4813. ELSE
  4814. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4815. actualParameters.SetExpression(1,parameter1);
  4816. END;
  4817. END;
  4818. (* ---- EXCL, INCL----- *)
  4819. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4820. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4821. IF IsIntegerValue(parameter1,i0) THEN
  4822. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4823. Error(position, "parameter out of SET range")
  4824. END;
  4825. END;
  4826. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4827. actualParameters.SetExpression(1,parameter1);
  4828. END;
  4829. (* ---- HALT, SYSTEM.HALT ----- *)
  4830. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4831. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4832. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4833. rules imposed by the architecture / current runtime
  4834. *)
  4835. END;
  4836. (* ---- WAIT ----- *)
  4837. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4838. IF CheckObjectType(parameter0) THEN
  4839. END;
  4840. (* ---- NEW ----- *)
  4841. ELSIF (id = Global.New) THEN
  4842. IF returnType # NIL THEN
  4843. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4844. ELSE
  4845. first := 1;
  4846. END;
  4847. IF CheckArity(first,Infinity) THEN
  4848. IF currentIsRealtime THEN
  4849. Error(position, "forbidden new in realtime block");
  4850. END;
  4851. (* check constructor *)
  4852. IF (first =0) OR CheckVariable(parameter0) THEN
  4853. IF type0 IS SyntaxTree.PointerType THEN
  4854. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4855. ELSIF type0 IS SyntaxTree.CellType THEN
  4856. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4857. ELSE
  4858. Error(position, "forbidden new on value type");
  4859. END;
  4860. IF type0 IS SyntaxTree.ArrayType THEN
  4861. arrayType := type0(SyntaxTree.ArrayType);
  4862. IF arrayType.form = SyntaxTree.Static THEN
  4863. i := first
  4864. ELSIF arrayType.form = SyntaxTree.Open THEN
  4865. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4866. ELSE HALT(100)
  4867. END;
  4868. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4869. i := first;
  4870. REPEAT
  4871. actualParameter := actualParameters.GetExpression(i);
  4872. IF CheckSizeType(actualParameter) THEN
  4873. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4874. actualParameters.SetExpression(i,actualParameter);
  4875. END;
  4876. INC(i);
  4877. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4878. END;
  4879. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4880. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4881. Error(position, "forbidden new on abstract object");
  4882. END;
  4883. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4884. IF constructor = NIL THEN
  4885. IF CheckArity(first,first) THEN END;
  4886. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4887. Error(position, "new on object with hidden constructor");
  4888. ELSE
  4889. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4890. numberFormalParameters := procedureType.numberParameters;
  4891. IF numberActualParameters-first <= numberFormalParameters THEN
  4892. formalParameter := procedureType.firstParameter;
  4893. FOR i := first TO numberActualParameters-1 DO
  4894. actualParameter := actualParameters.GetExpression(i);
  4895. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4896. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4897. ELSE
  4898. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4899. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4900. END;
  4901. actualParameters.SetExpression(i,actualParameter);
  4902. END;
  4903. formalParameter := formalParameter.nextParameter;
  4904. END;
  4905. WHILE (formalParameter # NIL) DO
  4906. IF formalParameter.defaultValue # NIL THEN
  4907. actualParameters.AddExpression(formalParameter.defaultValue);
  4908. formalParameter := formalParameter.nextParameter
  4909. ELSE
  4910. Error(position, "less actual than formal parameters");
  4911. formalParameter := NIL;
  4912. END;
  4913. END;
  4914. ELSE
  4915. Error(position, "more actual than formal parameters")
  4916. END;
  4917. END;
  4918. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4919. mathArrayType := type0(SyntaxTree.MathArrayType);
  4920. IF mathArrayType.form = SyntaxTree.Static THEN
  4921. Error(position, "new on static array");
  4922. ELSE
  4923. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4924. i0 := first+1; i1 := Infinity;
  4925. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4926. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4927. i1 := i0;
  4928. ELSE HALT(100);
  4929. END;
  4930. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4931. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4932. base := ArrayBase(type0,MAX(LONGINT));
  4933. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4934. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4935. IF ~CompatibleTo(system,type0,parameterType) THEN
  4936. Error(parameter0.position,"incompatible parameter in new");
  4937. result := SyntaxTree.invalidExpression;
  4938. ELSE
  4939. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4940. END;
  4941. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4942. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.lenType);
  4943. IF ~CompatibleTo(system,type1,parameterType) THEN
  4944. Error(parameter1.position,"parameter incompatible to math array len type");
  4945. result := SyntaxTree.invalidExpression;
  4946. ELSE
  4947. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4948. END;
  4949. ELSE
  4950. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4951. i := first;
  4952. REPEAT
  4953. actualParameter := actualParameters.GetExpression(i);
  4954. IF CheckSizeType(actualParameter) THEN
  4955. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4956. actualParameters.SetExpression(i,actualParameter);
  4957. END;
  4958. INC(i);
  4959. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4960. END;
  4961. END;
  4962. END;
  4963. ELSIF type0 IS SyntaxTree.CellType THEN
  4964. IF ~(currentIsCellNet) THEN
  4965. Error(position, "cell allocation outside activeCells ");
  4966. ELSE
  4967. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4968. IF (constructor = NIL) & CheckArity(1,1) THEN
  4969. (* ok *)
  4970. ELSE
  4971. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4972. numberFormalParameters := procedureType.numberParameters;
  4973. DEC(numberActualParameters);
  4974. IF numberActualParameters <= numberFormalParameters THEN
  4975. formalParameter := procedureType.firstParameter;
  4976. FOR i := first TO numberActualParameters DO
  4977. actualParameter := actualParameters.GetExpression(i);
  4978. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4979. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4980. ELSE
  4981. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4982. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4983. END;
  4984. actualParameters.SetExpression(i,actualParameter);
  4985. END;
  4986. formalParameter := formalParameter.nextParameter;
  4987. END;
  4988. WHILE (formalParameter # NIL) DO
  4989. IF formalParameter.defaultValue # NIL THEN
  4990. actualParameters.AddExpression(formalParameter.defaultValue);
  4991. formalParameter := formalParameter.nextParameter
  4992. ELSE
  4993. Error(position, "less actual than formal parameters");
  4994. formalParameter := NIL;
  4995. END;
  4996. END;
  4997. ELSE
  4998. Error(position, "more actual than formal parameters")
  4999. END;
  5000. END;
  5001. END;
  5002. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5003. activeCellsStatement := TRUE;
  5004. ELSE
  5005. Error(position, "cannot be allocated");
  5006. END;
  5007. END;
  5008. END;
  5009. (* ---- DISPOSE ----- *)
  5010. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5011. IF ~IsPointerType(parameter0.type) THEN
  5012. Error(parameter0.position,"is not a pointer")
  5013. ELSIF ~IsDisposable(parameter0.type) THEN
  5014. Error(parameter0.position,"is not disposable")
  5015. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5016. END
  5017. (* ---- GETPROCEDURE ----- *)
  5018. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5019. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5020. IF CheckVariable(parameter2) THEN
  5021. IF ~GetProcedureAllowed(parameter2.type) THEN
  5022. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5023. END;
  5024. END;
  5025. END;
  5026. (* ---- ABS ----- *)
  5027. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5028. (* note: ABS on complex numbers is done using overloading *)
  5029. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5030. type := type0;
  5031. IF IsIntegerValue(parameter0,i0) THEN
  5032. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5033. type := Global.GetIntegerType(system,ABS(i0));
  5034. ELSIF IsRealValue(parameter0,r) THEN
  5035. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5036. END;
  5037. ELSE
  5038. type := SyntaxTree.invalidType;
  5039. END;
  5040. (* ---- ASH, ASR ----- *)
  5041. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5042. type := type0;
  5043. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5044. (*
  5045. ConvertOperands(parameter0,parameter1); (* same type *)
  5046. *)
  5047. type := parameter0.type;
  5048. IF IsIntegerValue(parameter0,i0) THEN
  5049. IF IsIntegerValue(parameter1,i1) THEN
  5050. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5051. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5052. result := ResolveExpression(result);
  5053. type := Global.GetIntegerType(system,i0);
  5054. END;
  5055. END;
  5056. IF type.resolved.sizeInBits < 32 THEN
  5057. type := system.longintType;
  5058. END;
  5059. (*!compatibility with release, remove when resolved
  5060. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5061. *)
  5062. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5063. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5064. actualParameters.SetExpression(0,parameter0);
  5065. actualParameters.SetExpression(1,parameter1);
  5066. END;
  5067. (* ---- CAP ----- *)
  5068. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5069. type := system.characterType;
  5070. IF CheckCharacterType (parameter0) THEN
  5071. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5072. actualParameters.SetExpression(0,parameter0);
  5073. IF IsCharacterValue(parameter0,c) THEN
  5074. IF (c <= "z") & (c >= "a") THEN
  5075. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5076. ELSE
  5077. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5078. END;
  5079. END;
  5080. END;
  5081. (* ---- CHR ----- *)
  5082. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5083. IF id = Global.Chr THEN
  5084. type := system.characterType
  5085. ELSE
  5086. type := system.characterType32
  5087. END;
  5088. IF CheckIntegerType(parameter0) THEN
  5089. IF IsIntegerValue(parameter0,i0) THEN
  5090. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5091. result := ResolveExpression(result);
  5092. ELSE
  5093. (*
  5094. result := NewConversion(parameter0.position,parameter0,type);
  5095. *)
  5096. END;
  5097. END
  5098. (* ---- ENTIER ----- *)
  5099. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5100. type := system.longintType;
  5101. IF CheckRealType(parameter0) THEN
  5102. IF IsRealValue(parameter0,r) THEN
  5103. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5104. type := Global.GetIntegerType(system,ENTIER(r));
  5105. END
  5106. END;
  5107. (* ---- ENTIERH ----- *)
  5108. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5109. type := system.hugeintType;
  5110. IF CheckRealType(parameter0) THEN
  5111. IF IsRealValue(parameter0,r) THEN
  5112. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5113. END
  5114. END;
  5115. (* ---- LEN ----- *)
  5116. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5117. type := system.lenType;
  5118. base := type0;
  5119. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5120. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5121. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5122. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5123. Error(position, "forbidden len on unsafe pointer");
  5124. END;
  5125. type0 := base;
  5126. ELSE
  5127. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5128. type0 := parameter0.type.resolved;
  5129. actualParameters.SetExpression(0,parameter0);
  5130. base := type0;
  5131. END;
  5132. END;
  5133. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5134. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5135. IF i1 < 0 THEN
  5136. Error(position, "invalid dimension");
  5137. base := SyntaxTree.invalidType;
  5138. ELSE
  5139. base := ArrayBase(base,i1);
  5140. IF (base # NIL) & Indexable(base) THEN
  5141. ELSE
  5142. Error(position, "len on no array");
  5143. IF VerboseErrorMessage THEN
  5144. Printout.Info("base",base);
  5145. END;
  5146. base := SyntaxTree.invalidType;
  5147. END;
  5148. END;
  5149. IF numberActualParameters=2 THEN
  5150. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5151. actualParameters.SetExpression(1,parameter1);
  5152. ELSIF base IS SyntaxTree.MathArrayType THEN
  5153. Error(position, "missing dimension specification");
  5154. END;
  5155. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5156. IF base IS SyntaxTree.ArrayType THEN
  5157. arrayType := base(SyntaxTree.ArrayType);
  5158. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5159. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5160. result := Global.NewIntegerValue(system,position,i);
  5161. type := result.type;(* arrayType.length.type;*)
  5162. ASSERT(type # NIL);
  5163. END;
  5164. ELSIF base IS SyntaxTree.MathArrayType THEN
  5165. mathArrayType := base(SyntaxTree.MathArrayType);
  5166. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5167. result := Global.NewIntegerValue(system,position,i);
  5168. type := result.type;
  5169. (*
  5170. type := mathArrayType.length.type;
  5171. *)
  5172. ASSERT(type # NIL);
  5173. END;
  5174. END;
  5175. END;
  5176. ELSE
  5177. type := system.lenType;
  5178. END;
  5179. (* ---- FIRST ---- *)
  5180. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5181. type := system.lenType;
  5182. IF CheckRangeType(parameter0) THEN END;
  5183. result.SetAssignable(parameter0.assignable)
  5184. (* ---- LAST ---- *)
  5185. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5186. type := system.lenType;
  5187. IF CheckRangeType(parameter0) THEN END;
  5188. result.SetAssignable(parameter0.assignable)
  5189. (* ---- STEP ---- *)
  5190. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5191. type := system.lenType;
  5192. IF CheckRangeType(parameter0) THEN END;
  5193. result.SetAssignable(parameter0.assignable)
  5194. (* ---- RE ---- *)
  5195. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5196. IF CheckNumberType(parameter0) THEN
  5197. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5198. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5199. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5200. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5201. type := parameter0.type
  5202. ELSE
  5203. type := system.realType
  5204. END
  5205. END;
  5206. result.SetAssignable(parameter0.assignable)
  5207. (* ---- IM ---- *)
  5208. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5209. IF CheckNumberType(parameter0) THEN
  5210. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5211. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5212. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5213. ELSE
  5214. type := system.realType;
  5215. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5216. END
  5217. END;
  5218. result.SetAssignable(parameter0.assignable)
  5219. (* ---- MAX ----- *)
  5220. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5221. IF numberActualParameters = 1 THEN
  5222. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5223. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5224. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5225. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5226. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5227. *)
  5228. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5229. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5230. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5231. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5232. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5233. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5234. END;
  5235. ELSE
  5236. Error(parameter0.position,"is not a type symbol");
  5237. END
  5238. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5239. ConvertOperands(parameter0,parameter1);
  5240. actualParameters.SetExpression(0,parameter0);
  5241. actualParameters.SetExpression(1,parameter1);
  5242. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5243. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5244. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5245. END;
  5246. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5247. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5248. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5249. END;
  5250. END;
  5251. type := parameter0.type;
  5252. ELSE type := SyntaxTree.invalidType;
  5253. END;
  5254. (* ---- MIN ----- *)
  5255. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5256. IF numberActualParameters = 1 THEN
  5257. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5258. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5259. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5260. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5261. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5262. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5263. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5264. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5265. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5266. END;
  5267. ELSE
  5268. Error(parameter0.position,"is not a type symbol");
  5269. END
  5270. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5271. ConvertOperands(parameter0,parameter1);
  5272. actualParameters.SetExpression(0,parameter0);
  5273. actualParameters.SetExpression(1,parameter1);
  5274. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5275. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5276. ELSE result.SetResolved(parameter1.resolved)
  5277. END;
  5278. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5279. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5280. ELSE result.SetResolved(parameter1.resolved)
  5281. END;
  5282. END;
  5283. type := parameter0.type;
  5284. ELSE type := SyntaxTree.invalidType;
  5285. END;
  5286. (* ---- ODD ----- *)
  5287. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5288. type := system.booleanType;
  5289. IF CheckIntegerType(parameter0) THEN
  5290. IF IsIntegerValue(parameter0,i0) THEN
  5291. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5292. type := system.booleanType;
  5293. END;
  5294. END;
  5295. (* ---- ORD ----- *)
  5296. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5297. IF id = Global.Ord THEN
  5298. type := system.integerType;
  5299. ELSE
  5300. type := system.longintType;
  5301. END;
  5302. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5303. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5304. actualParameters.SetExpression(0,parameter0);
  5305. (* IF CheckCharacterType(parameter0) THEN*)
  5306. IF IsCharacterValue(parameter0,c)THEN
  5307. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5308. type := Global.GetSignedIntegerType(system,ORD(c));
  5309. END;
  5310. ELSE Error(parameter0.position, "incompatible parameter");
  5311. END;
  5312. (* ---- SHORT ----- *)
  5313. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5314. type := type0;
  5315. IF IsSignedIntegerType(type) THEN
  5316. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5317. ELSIF type = system.integerType THEN type := system.shortintType
  5318. ELSIF type = system.longintType THEN type := system.integerType
  5319. ELSIF type = system.hugeintType THEN type:= system.longintType
  5320. ELSE
  5321. CASE type.sizeInBits OF
  5322. 16: type := Global.Integer8
  5323. |32: type := Global.Integer16
  5324. |64: type := Global.Integer32
  5325. END;
  5326. END;
  5327. ELSIF type IS SyntaxTree.FloatType THEN
  5328. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5329. ELSIF type = system.longrealType THEN type := system.realType
  5330. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5331. END;
  5332. ELSIF type IS SyntaxTree.ComplexType THEN
  5333. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5334. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5335. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5336. END;
  5337. ELSE
  5338. Error(parameter0.position,"short not applicable")
  5339. END;
  5340. IF (parameter0.resolved # NIL) THEN
  5341. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5342. IF parameter0 IS SyntaxTree.Value THEN
  5343. result.SetResolved(parameter0(SyntaxTree.Value));
  5344. END;
  5345. END;
  5346. (* ---- LONG ----- *)
  5347. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5348. type := type0;
  5349. IF IsSignedIntegerType(type) THEN
  5350. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5351. ELSIF type = system.longintType THEN type := system.hugeintType
  5352. ELSIF type = system.integerType THEN type := system.longintType
  5353. ELSIF type = system.shortintType THEN type := system.integerType
  5354. ELSE
  5355. CASE type.sizeInBits OF
  5356. 8: type := Global.Integer16
  5357. |16: type := Global.Integer32
  5358. |32: type := Global.Integer64
  5359. END;
  5360. END;
  5361. ELSIF type IS SyntaxTree.FloatType THEN
  5362. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5363. ELSIF type= system.realType THEN type := system.longrealType
  5364. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5365. END;
  5366. ELSIF type IS SyntaxTree.ComplexType THEN
  5367. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5368. ELSIF type = system.complexType THEN type := system.longcomplexType
  5369. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5370. END;
  5371. ELSE
  5372. Error(parameter0.position,"long not applicable")
  5373. END;
  5374. IF (parameter0.resolved # NIL) THEN
  5375. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5376. IF parameter0 IS SyntaxTree.Value THEN
  5377. result.SetResolved(parameter0(SyntaxTree.Value));
  5378. END;
  5379. END;
  5380. (* ---- SIZE OF ----- *)
  5381. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5382. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5383. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5384. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5385. type := system.integerType;
  5386. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5387. ELSE
  5388. (* for variables, system sizeof could represent the physically occupied size
  5389. determined via the type descriptor, implement that ? *)
  5390. Error(parameter0.position,"is not a type symbol");
  5391. END
  5392. (* ---- SYSTEM.TRACE -----*)
  5393. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5394. FOR i := 0 TO numberActualParameters-1 DO
  5395. parameter0 := actualParameters.GetExpression(i);
  5396. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5397. Error(parameter0.position,"incompatible parameter");
  5398. END;
  5399. END;
  5400. (* remaining issues can only be tested in backend *)
  5401. (* ---- ADDRESSOF----- *)
  5402. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5403. IF HasAddress(parameter0) THEN
  5404. type := system.addressType;
  5405. ELSE
  5406. type := SyntaxTree.invalidType;
  5407. Error(parameter0.position,"has no address");
  5408. END;
  5409. (* ---- BIT ----- *)
  5410. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5411. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5412. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5413. actualParameters.SetExpression(0,parameter0);
  5414. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5415. actualParameters.SetExpression(1,parameter1);
  5416. END;
  5417. type := system.booleanType;
  5418. (* ----- MSK ---- *)
  5419. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5420. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5421. ConvertOperands(parameter0,parameter1);
  5422. actualParameters.SetExpression(0,parameter0);
  5423. actualParameters.SetExpression(1,parameter1);
  5424. END;
  5425. type := parameter0.type;
  5426. (* ---- SYSTEM.GET64 ----- *)
  5427. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5428. IF CheckAddressType(parameter0) THEN
  5429. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5430. actualParameters.SetExpression(0,parameter0);
  5431. END;
  5432. type := system.hugeintType;
  5433. (* ---- SYSTEM.GET32 ----- *)
  5434. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5435. IF CheckAddressType(parameter0) THEN
  5436. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5437. actualParameters.SetExpression(0,parameter0);
  5438. END;
  5439. type := system.longintType;
  5440. (* ---- SYSTEM.GET16 ----- *)
  5441. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5442. IF CheckAddressType(parameter0) THEN
  5443. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5444. actualParameters.SetExpression(0,parameter0);
  5445. END;
  5446. type := system.integerType;
  5447. (* ---- SYSTEM.GET8 ----- *)
  5448. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5449. IF CheckAddressType(parameter0) THEN
  5450. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5451. actualParameters.SetExpression(0,parameter0);
  5452. END;
  5453. type := system.shortintType;
  5454. (* ---- SYSTEM.GetStackPointer ----- *)
  5455. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5456. type := system.addressType;
  5457. (* ---- SYSTEM.GetFramePointer ----- *)
  5458. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5459. type := system.addressType;
  5460. (* ---- SYSTEM.GetActivity ----- *)
  5461. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5462. type := system.objectType;
  5463. (* ---- SYSTEM.SetStackPointer ----- *)
  5464. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5465. IF CheckAddressType(parameter0) THEN
  5466. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5467. actualParameters.SetExpression(0,parameter0);
  5468. END;
  5469. (* ---- SYSTEM.SetFramePointer ----- *)
  5470. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5471. IF CheckAddressType(parameter0) THEN
  5472. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5473. actualParameters.SetExpression(0,parameter0);
  5474. END;
  5475. (* ---- SYSTEM.SetActivity ----- *)
  5476. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5477. IF CheckObjectType(parameter0) THEN
  5478. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5479. actualParameters.SetExpression(0,parameter0);
  5480. END;
  5481. (* ---- LSH, LSL, ROT, ROR ----- *)
  5482. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5483. type := type0;
  5484. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5485. actualParameters.SetExpression(1, parameter1);
  5486. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5487. IF id = Global.Lsh THEN
  5488. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5489. ELSIF id = Global.Rot THEN
  5490. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5491. ELSIF id = Global.Ror THEN
  5492. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5493. END;
  5494. END;
  5495. (* ---- SYSTEM.VAL ----- *)
  5496. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5497. IF CheckTypeDeclarationType(parameter0) THEN
  5498. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5499. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5500. result := SyntaxTree.invalidExpression;
  5501. Error(parameter0.position,"is no basic type");
  5502. ELSE
  5503. IF (parameter1.resolved # NIL) THEN
  5504. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5505. IF parameter0 IS SyntaxTree.Value THEN
  5506. result.SetResolved(parameter0(SyntaxTree.Value));
  5507. END;
  5508. END;
  5509. result.SetAssignable(parameter1.assignable);
  5510. END;
  5511. END;
  5512. (* ---- SYSTEM.GET ----- *)
  5513. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5514. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5515. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5516. actualParameters.SetExpression(0,parameter0);
  5517. END;
  5518. (* ---- SYSTEM.PUT ----- *)
  5519. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5520. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5521. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5522. actualParameters.SetExpression(0,parameter0);
  5523. END;
  5524. (* ---- SYSTEM.PUT64 ----- *)
  5525. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5526. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5527. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5528. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5529. actualParameters.SetExpression(0,parameter0);
  5530. actualParameters.SetExpression(1,parameter1);
  5531. END;
  5532. (* ---- SYSTEM.PUT32 ----- *)
  5533. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5534. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5535. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5536. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5537. actualParameters.SetExpression(0,parameter0);
  5538. actualParameters.SetExpression(1,parameter1);
  5539. END;
  5540. (* ---- SYSTEM.PUT16 ----- *)
  5541. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5542. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5543. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5544. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5545. actualParameters.SetExpression(0,parameter0);
  5546. actualParameters.SetExpression(1,parameter1);
  5547. END;
  5548. (* ---- SYSTEM.PUT8 ----- *)
  5549. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5550. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5551. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5552. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5553. actualParameters.SetExpression(0,parameter0);
  5554. actualParameters.SetExpression(1,parameter1);
  5555. END;
  5556. (* ---- SYSTEM.MOVE ----- *)
  5557. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5558. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5559. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5560. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5561. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5562. actualParameters.SetExpression(0,parameter0);
  5563. actualParameters.SetExpression(1,parameter1);
  5564. actualParameters.SetExpression(2,parameter2);
  5565. END;
  5566. (* ---- SYSTEM.NEW ----- *)
  5567. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5568. IF ~IsPointerType(parameter0.type) THEN
  5569. Error(parameter0.position,"is not a pointer")
  5570. ELSIF CheckSizeType(parameter1) THEN
  5571. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5572. actualParameters.SetExpression(1,parameter1);
  5573. END;
  5574. (* ----SYSTEM.REF ---- *)
  5575. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5576. type := system.addressType
  5577. (* ---- INCR ----- *)
  5578. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5579. type := system.lenType;
  5580. base := type0;
  5581. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5582. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5583. IF i1 < 0 THEN
  5584. Error(position, "invalid dimension");
  5585. base := SyntaxTree.invalidType;
  5586. ELSE
  5587. base := ArrayBase(base,i1);
  5588. IF (base # NIL) & Indexable(base) THEN
  5589. ELSE
  5590. Error(position, "len on no array");
  5591. IF VerboseErrorMessage THEN
  5592. Printout.Info("base",base);
  5593. END;
  5594. base := SyntaxTree.invalidType;
  5595. END;
  5596. END;
  5597. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5598. actualParameters.SetExpression(1,parameter1);
  5599. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5600. mathArrayType := base(SyntaxTree.MathArrayType);
  5601. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5602. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5603. type := system.lenType;
  5604. END;
  5605. END;
  5606. ELSE
  5607. type := system.lenType;
  5608. END;
  5609. (* ---- SUM ----- *)
  5610. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5611. Error(position, "sum operator not applicable");
  5612. (* ---- ALL ----- *)
  5613. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5614. Error(position, "all operator not applicable");
  5615. (* ---- DIM ----- *)
  5616. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5617. type := system.lenType;
  5618. IF type0 IS SyntaxTree.MathArrayType THEN
  5619. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5620. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5621. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5622. END;
  5623. ELSE
  5624. Error(position, "dimension on non math array type");
  5625. END;
  5626. (* ---- CAS ----- *)
  5627. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5628. IF type0.IsComposite () THEN
  5629. Error(position, "first parameter of composite type");
  5630. result := SyntaxTree.invalidExpression;
  5631. ELSIF ~IsVariable (parameter0) THEN
  5632. Error(position, "first parameter not assignable");
  5633. result := SyntaxTree.invalidExpression;
  5634. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5635. Error(position, "second parameter incompatible");
  5636. result := SyntaxTree.invalidExpression;
  5637. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5638. Error(position, "third parameter incompatible");
  5639. result := SyntaxTree.invalidExpression;
  5640. ELSE
  5641. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5642. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5643. type := type0;
  5644. END;
  5645. (* ---- RESHAPE ----- *)
  5646. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5647. IF type0 IS SyntaxTree.MathArrayType THEN
  5648. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5649. base := ArrayBase(type0,MAX(LONGINT));
  5650. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5651. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5652. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5653. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5654. IF ~CompatibleTo(system,type0,parameterType) THEN
  5655. Error(parameter0.position,"incompatible parameter in reshape");
  5656. result := SyntaxTree.invalidExpression;
  5657. ELSE
  5658. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5659. END;
  5660. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5661. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5662. IF ~CompatibleTo(system,type1,parameterType) THEN
  5663. Error(parameter1.position,"parameter incompatible to math array of longint");
  5664. result := SyntaxTree.invalidExpression;
  5665. ELSE
  5666. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5667. END;
  5668. ELSE
  5669. Error(position,"reshape on non math array type");
  5670. result := SyntaxTree.invalidExpression;
  5671. END;
  5672. (* ---- SYSTEM.TYPECODE ----- *)
  5673. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5674. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5675. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5676. type := type.resolved;
  5677. IF type IS SyntaxTree.PointerType THEN
  5678. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5679. END;
  5680. IF ~(type IS SyntaxTree.RecordType) THEN
  5681. Error(parameter0.position,"must be type with type descriptor");
  5682. END;
  5683. ELSE
  5684. Error(parameter0.position,"is not a type symbol");
  5685. END;
  5686. type := system.addressType;
  5687. (* -------- FLT --------- *)
  5688. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5689. type := system.realType;
  5690. IF IsRealValue(parameter0, r) THEN
  5691. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5692. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5693. i0 := i; i := ABS(i);
  5694. IF i # 0 THEN
  5695. i1 := 23;
  5696. IF i >= 2*800000H THEN
  5697. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5698. ELSIF i < 800000H THEN
  5699. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5700. END;
  5701. i := (i1 + 127)*800000H - 800000H + i;
  5702. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5703. END;
  5704. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5705. END;
  5706. (* ------- CONNECT -------*)
  5707. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5708. (*IF ~(currentIsCellNet) THEN
  5709. Error(position, "connection outside activeCells body block");
  5710. END;*)
  5711. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5712. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5713. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5714. END;
  5715. IF numberActualParameters = 3 THEN
  5716. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5717. Error(position, "incompatible channel size parameter");
  5718. END;
  5719. *)
  5720. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5721. actualParameters.SetExpression(2,parameter2);
  5722. END;
  5723. activeCellsStatement := TRUE;
  5724. (* ---------- DELEGATE --------*)
  5725. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5726. (*
  5727. IF ~(currentIsCellNet) THEN
  5728. Error(position, "connection delegation outside activeCells body block");
  5729. END;
  5730. *)
  5731. IF ~CheckPortType(parameter1, inPort) THEN
  5732. Error(parameter0.position,"not a port")
  5733. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5734. Error(parameter1.position,"not a port")
  5735. ELSIF (outPort.direction # inPort.direction) THEN
  5736. Error(parameter0.position,"invalid port direction");
  5737. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5738. Error(position, "incompatible port sizes");
  5739. END;
  5740. activeCellsStatement := TRUE;
  5741. (* --------- RECEIVE ---------*)
  5742. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5743. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5744. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5745. IF inPort.direction # SyntaxTree.InPort THEN
  5746. Error(parameter0.position,"not an in-port")
  5747. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5748. Error(parameter1.position,"incompatible to port type");
  5749. END;
  5750. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5751. IF ~SameType(parameter2.type, system.integerType) THEN
  5752. Error(parameter2.position,"incompatible to integer type");
  5753. END;
  5754. END;
  5755. END;
  5756. (* --------- SEND ---------*)
  5757. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5758. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5759. IF CheckPortType(parameter0,outPort) THEN
  5760. IF outPort.direction # SyntaxTree.OutPort THEN
  5761. Error(parameter1.position,"not an out-port")
  5762. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5763. Error(parameter1.position,"incompatible to port type");
  5764. ELSE
  5765. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5766. actualParameters.SetExpression(1,parameter1);
  5767. END;
  5768. END;
  5769. (* ------- custom builtins ----- *)
  5770. ELSIF id = Global.systemSpecial THEN
  5771. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5772. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5773. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5774. type := procedureType.returnType;
  5775. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5776. (* go through all formal parameters *)
  5777. formalParameter := procedureType.firstParameter;
  5778. FOR i := 0 TO actualParameters.Length() - 1 DO
  5779. actualParameter := actualParameters.GetExpression(i);
  5780. IF actualParameter = SyntaxTree.invalidExpression THEN
  5781. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5782. Error(position, "incompatible parameter")
  5783. ELSE
  5784. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5785. END;
  5786. actualParameters.SetExpression(i, actualParameter);
  5787. formalParameter := formalParameter.nextParameter
  5788. END
  5789. END
  5790. ELSE
  5791. Error(position, "builtin not implemented");
  5792. result := SyntaxTree.invalidExpression;
  5793. END;
  5794. END;
  5795. IF result # SyntaxTree.invalidExpression THEN
  5796. type := ResolveType(type);
  5797. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5798. result.SetType(type);
  5799. END;
  5800. RETURN result
  5801. END NewBuiltinCallDesignator;
  5802. (** return type guard designator left(type)
  5803. - check if type can be extended (i.e. is no static record)
  5804. - check if type is a type extension of left.type
  5805. - returns new type guard designator
  5806. returns invalidDesignator = invalidExpression if error
  5807. **)
  5808. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5809. VAR result: SyntaxTree.Designator;
  5810. BEGIN
  5811. result := SyntaxTree.invalidDesignator;
  5812. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5813. Error(position, "no type extension of type");
  5814. IF VerboseErrorMessage THEN
  5815. Printout.Info("left",left);
  5816. Printout.Info("type",type);
  5817. END;
  5818. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5819. Error(position, "variable cannot be extended");
  5820. ELSIF IsUnsafePointer(left.type) THEN
  5821. Error(position, "forbidden type guard on unsafe pointer");
  5822. ELSE
  5823. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5824. result.SetType(type);
  5825. result.SetAssignable(left.assignable);
  5826. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5827. END;
  5828. RETURN result
  5829. END NewTypeGuardDesignator;
  5830. (** check and resolve parameter designator left(expression list)
  5831. - check expression list
  5832. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5833. - elsif left is a procedure type then
  5834. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5835. - else return is a procedure call then return ProcedureCallDesignator
  5836. returns invalidDesignator = invalidExpression if error
  5837. **)
  5838. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5839. VAR
  5840. parameters: SyntaxTree.ExpressionList;
  5841. left: SyntaxTree.Designator;
  5842. result,expression: SyntaxTree.Expression;
  5843. typeDeclaration: SyntaxTree.TypeDeclaration;
  5844. type, expressionType: SyntaxTree.Type;
  5845. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5846. BEGIN
  5847. type := type.resolved;
  5848. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5849. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5850. END;
  5851. RETURN type
  5852. END BaseType;
  5853. BEGIN
  5854. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5855. result := SyntaxTree.invalidDesignator;
  5856. left := ResolveDesignator(designator.left);
  5857. IF left # SyntaxTree.invalidDesignator THEN
  5858. parameters := designator.parameters;
  5859. IF ExpressionList(parameters) THEN
  5860. IF (left.type = NIL) THEN
  5861. Error(left.position,"object is not a procedure or cannot be extended");
  5862. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5863. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5864. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5865. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5866. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5867. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5868. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5869. ELSE
  5870. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5871. END
  5872. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5873. expression := parameters.GetExpression(0);
  5874. type := typeDeclaration.declaredType.resolved;
  5875. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5876. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5877. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5878. OR (expressionType IS SyntaxTree.EnumerationType)
  5879. ) THEN
  5880. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5881. Error(left.position,"invalid unsigned type in explicit conversion");
  5882. ELSE
  5883. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5884. END;
  5885. ELSE
  5886. Error(left.position,"invalid type in explicit conversion");
  5887. END;
  5888. ELSE
  5889. Error(left.position,"called object is not a procedure or cannot be extended");
  5890. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5891. result := SyntaxTree.invalidDesignator;
  5892. END;
  5893. ELSE
  5894. result := SyntaxTree.invalidDesignator
  5895. END;
  5896. END;
  5897. resolvedExpression := result;
  5898. END VisitParameterDesignator;
  5899. (** check dereference designator left^
  5900. - check if left is pointer type or left is object type
  5901. - return new dereference designator with type = left.baseType.type (if appropriate)
  5902. with error handling
  5903. returns invalidDesignator = invalidExpression if error
  5904. **)
  5905. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5906. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5907. BEGIN
  5908. result := SyntaxTree.invalidDesignator;
  5909. type := left.type;
  5910. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5911. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5912. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5913. result.SetAssignable(TRUE);
  5914. result.SetType(type);
  5915. result.SetHidden(left.isHidden);
  5916. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5917. type := type.resolved;
  5918. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5919. result.SetAssignable(TRUE);
  5920. result.SetType(type);
  5921. result.SetHidden(left.isHidden);
  5922. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5923. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5924. result.SetAssignable(TRUE);
  5925. result.SetType(type);
  5926. result.SetHidden(left.isHidden);
  5927. ELSE
  5928. Error(position, "dereference on no pointer");
  5929. IF VerboseErrorMessage THEN
  5930. Printout.Info("pointer", type);
  5931. Printout.Info("scope", currentScope);
  5932. END;
  5933. END;
  5934. RETURN result
  5935. END NewDereferenceDesignator;
  5936. (** check supercall designator left^
  5937. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5938. - return new supercall designator with type = left.type
  5939. with error handling
  5940. **)
  5941. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5942. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5943. objectScope: SyntaxTree.Scope;
  5944. BEGIN
  5945. result := SyntaxTree.invalidDesignator;
  5946. IF left = SyntaxTree.invalidDesignator THEN
  5947. (* error already handled *)
  5948. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5949. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5950. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5951. IF symbol IS SyntaxTree.Procedure THEN
  5952. procedure := symbol(SyntaxTree.Procedure);
  5953. objectScope := currentScope;
  5954. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5955. objectScope := objectScope.outerScope;
  5956. END;
  5957. IF (left.left = NIL) OR ~
  5958. (
  5959. (left.left IS SyntaxTree.SelfDesignator) OR
  5960. (left.left IS SyntaxTree.DereferenceDesignator)
  5961. & (left.left(SyntaxTree.Designator).left # NIL)
  5962. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5963. Error(position, "procedure not in immediate object scope");
  5964. IF VerboseErrorMessage THEN
  5965. Printout.Info("left.left",left.left);
  5966. END;
  5967. ELSIF procedure.super # NIL THEN
  5968. result := SyntaxTree.NewSupercallDesignator(position,left);
  5969. result.SetType(left.type.resolved)
  5970. ELSE
  5971. Error(position, "no supermethod for this procedure");
  5972. END;
  5973. ELSE
  5974. Error(position, "symbol is not a procedure");
  5975. END;
  5976. ELSE
  5977. Error(position, "is no symbol designator");
  5978. END;
  5979. RETURN result
  5980. END NewSupercallDesignator;
  5981. (** check and semantically resolve arrow designator left^
  5982. - if left is procedure type -> result := SupercallDesignator
  5983. - else result := DereferenceDesignator
  5984. returns result via global variable resolvedExpression
  5985. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5986. **)
  5987. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  5988. VAR left: SyntaxTree.Designator;
  5989. BEGIN
  5990. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5991. left := ResolveDesignator(arrowDesignator.left);
  5992. IF left # NIL THEN
  5993. IF (left.type = NIL) THEN
  5994. Error(arrowDesignator.position,"Invalid arrow designator");
  5995. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5996. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5997. ELSE
  5998. IF IsPointerToObject(left.type) THEN
  5999. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6000. END;
  6001. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6002. END
  6003. END
  6004. END VisitArrowDesignator;
  6005. (** check and return expression
  6006. - if expression has no type then resolve expression
  6007. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6008. - return result
  6009. **)
  6010. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6011. VAR result,prev: SyntaxTree.Expression;
  6012. BEGIN
  6013. IF expression = NIL THEN result := NIL
  6014. ELSIF (expression.type = NIL) THEN
  6015. prev := resolvedExpression;
  6016. resolvedExpression := SyntaxTree.invalidExpression;
  6017. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6018. expression.SetType(SyntaxTree.invalidType);
  6019. END;
  6020. expression.Accept(SELF);
  6021. result := resolvedExpression;
  6022. IF currentIsRealtime THEN
  6023. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6024. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6025. END;
  6026. END;
  6027. (* designator modifiers for backends if they support it ...*)
  6028. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6029. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6030. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6031. END;
  6032. resolvedExpression := prev
  6033. ELSE
  6034. result := expression
  6035. END;
  6036. RETURN result
  6037. END ResolveExpression;
  6038. (**
  6039. check expression to be constant expression
  6040. - resolve expression
  6041. - if valid then check that of value type
  6042. report error and return invalidExpression if anything fails
  6043. **)
  6044. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6045. VAR position: Position;
  6046. BEGIN
  6047. position := expression.position;
  6048. expression := ResolveExpression(expression);
  6049. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6050. ELSIF (expression.resolved = NIL) THEN
  6051. Error(position, "expression is not constant");
  6052. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6053. expression := SyntaxTree.invalidExpression;
  6054. END;
  6055. RETURN expression
  6056. END ConstantExpression;
  6057. (** check expression to be constant integer
  6058. - resolve expresssion
  6059. - if valid then check that of integer value type
  6060. report error and return invalidExpression if anything fails
  6061. **)
  6062. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6063. VAR position: Position;
  6064. BEGIN
  6065. position := expression.position;
  6066. expression := ResolveExpression(expression);
  6067. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6068. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6069. expression := SyntaxTree.invalidExpression;
  6070. Error(position, "expression is not a constant integer");
  6071. END;
  6072. RETURN expression
  6073. END ConstantInteger;
  6074. (** check expression as positive (>=0) constant integer
  6075. - resolve expression
  6076. - if valid then check that integer value
  6077. - if integer value then check that value >= 0
  6078. report error and return invalidExpression if anything fails
  6079. **)
  6080. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6081. VAR position: Position;
  6082. BEGIN
  6083. position := expression.position;
  6084. expression := ConstantExpression(expression);
  6085. IF expression = SyntaxTree.invalidExpression THEN
  6086. (* error already reported *)
  6087. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6088. Error(position, "expression is not integer valued");
  6089. expression := SyntaxTree.invalidExpression
  6090. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6091. Error(position, "integer is not greater or equal zero");
  6092. END;
  6093. RETURN expression
  6094. END ConstantIntegerGeq0;
  6095. (** check expression as condition
  6096. - resolve expression
  6097. - if valid expression then check that result type is boolean
  6098. report error and return invalidExpression if anything fails
  6099. **)
  6100. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6101. VAR position: Position;
  6102. BEGIN
  6103. position := expression.position;
  6104. expression := ResolveExpression(expression);
  6105. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6106. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6107. expression := SyntaxTree.invalidExpression;
  6108. Error(position, "expression is not boolean");
  6109. END;
  6110. RETURN expression
  6111. END ResolveCondition;
  6112. (*** symbols ***)
  6113. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6114. BEGIN
  6115. x.Accept(SELF);
  6116. END ResolveSymbol;
  6117. (** check a symbol
  6118. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6119. **)
  6120. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6121. VAR scope: SyntaxTree.Scope;
  6122. BEGIN
  6123. (* visibility *)
  6124. scope := symbol.scope;
  6125. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6126. scope := scope.outerScope;
  6127. END;
  6128. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6129. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6130. Error(symbol.position,"cannot be exported");
  6131. IF VerboseErrorMessage THEN
  6132. Printout.Info("symbol",symbol);
  6133. END;
  6134. END;
  6135. END;
  6136. END CheckSymbolVisibility;
  6137. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6138. If node is currently being resolved then emit a cyclic definition error.
  6139. Return TRUE only if node is fully resolved.
  6140. **)
  6141. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6142. VAR result: BOOLEAN;
  6143. BEGIN
  6144. IF SyntaxTree.Resolved IN x.state THEN
  6145. result := FALSE
  6146. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6147. Error(x.position,"cyclic definition");
  6148. result := FALSE;
  6149. ELSE
  6150. result := TRUE;
  6151. x.SetState(SyntaxTree.BeingResolved)
  6152. END;
  6153. RETURN result
  6154. END SymbolNeedsResolution;
  6155. (** check and resolve a type declaration symbol = Type
  6156. - set type to declaration type
  6157. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6158. This is so because the type declaration itself does not have a type but it only stands for a type.
  6159. In the implementation of the compiler this made a lot much easier.
  6160. - resolve and set declared type
  6161. - check symbol
  6162. **)
  6163. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6164. VAR prevScope: SyntaxTree.Scope;
  6165. BEGIN
  6166. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6167. IF SymbolNeedsResolution(typeDeclaration) THEN
  6168. typeDeclaration.SetState(SyntaxTree.Resolved);
  6169. prevScope := currentScope;
  6170. currentScope := typeDeclaration.scope;
  6171. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6172. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6173. CheckSymbolVisibility(typeDeclaration);
  6174. typeDeclaration.SetState(SyntaxTree.Resolved);
  6175. currentScope := prevScope;
  6176. END;
  6177. END VisitTypeDeclaration;
  6178. (** check and resolve a constant declaration symbol = (constant) expression
  6179. - check expression
  6180. - set type and value
  6181. - check symbol
  6182. **)
  6183. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6184. VAR
  6185. expression: SyntaxTree.Expression;
  6186. type: SyntaxTree.Type;
  6187. name: Basic.SegmentedName;
  6188. replacement: Replacement;
  6189. BEGIN
  6190. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6191. IF SymbolNeedsResolution(constant) THEN
  6192. expression := constant.value;
  6193. IF replacements # NIL THEN
  6194. Global.GetSymbolSegmentedName(constant, name);
  6195. replacement := replacements;
  6196. WHILE (replacement # NIL) & (replacement.name # name) DO
  6197. replacement := replacement.next;
  6198. END;
  6199. IF replacement # NIL THEN
  6200. InfoSS(constant.position, "replacing constant", constant.name);
  6201. (*
  6202. NEW(stringReader, Strings.Length(replacement.string^));
  6203. stringReader.Set(replacement.string^);
  6204. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6205. NEW(parser, scanner, diagnostics);
  6206. expression := parser.Expression();
  6207. *)
  6208. expression := replacement.expression;
  6209. replacement.used := TRUE;
  6210. END;
  6211. END;
  6212. constant.SetType(SyntaxTree.invalidType);
  6213. expression := ConstantExpression(expression);
  6214. ASSERT(expression.type # NIL);
  6215. type := expression.type.resolved;
  6216. constant.SetType(type);
  6217. constant.SetValue(expression);
  6218. CheckSymbolVisibility(constant);
  6219. constant.SetState(SyntaxTree.Resolved);
  6220. END;
  6221. END VisitConstant;
  6222. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6223. VAR procedureAlignment: LONGINT;
  6224. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6225. (* least common multiple *)
  6226. VAR a,b: LONGINT;
  6227. BEGIN
  6228. a := a0; b := b0;
  6229. WHILE (a # b) DO
  6230. IF a < b THEN a := a+a0
  6231. ELSE b := b + b0
  6232. END;
  6233. END;
  6234. RETURN a
  6235. END LCM;
  6236. BEGIN
  6237. IF alignment > 1 THEN
  6238. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6239. IF (procedureAlignment > 1) THEN
  6240. alignment := LCM(alignment, procedureAlignment);
  6241. END;
  6242. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6243. END;
  6244. END AdaptStackAlignment;
  6245. (** check and resolve a variable / field
  6246. - check and set type
  6247. - negative check on open array type
  6248. - check symbol
  6249. **)
  6250. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6251. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6252. BEGIN
  6253. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6254. IF SymbolNeedsResolution(variable) THEN
  6255. modifiers := variable.modifiers;
  6256. (*
  6257. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6258. variable.AddFlags(flags);
  6259. *)
  6260. variable.SetType(ResolveType(variable.type));
  6261. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6262. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6263. Error(variable.position,"forbidden open array variable");
  6264. END;
  6265. END;
  6266. CheckSymbolVisibility(variable);
  6267. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6268. variable.SetUntraced(TRUE);
  6269. IF ~ContainsPointer(variable.type) THEN
  6270. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6271. Error(position, "untraced flag on non-pointer variable");
  6272. END;
  6273. END;
  6274. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6275. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6276. IF ~PowerOf2(value) THEN
  6277. Error(position, "forbidden alignment - must be power of two");
  6278. ELSE
  6279. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6280. END;
  6281. END;
  6282. variable.SetAlignment(FALSE,value);
  6283. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6284. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6285. Error(position, "fixed position not possible in procedure");
  6286. END;
  6287. variable.SetAlignment(TRUE, value);
  6288. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6289. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6290. Error(position,"fictive offset not possible in procedure");
  6291. END;
  6292. variable.SetFictive(value);
  6293. variable.SetOffset(value*system.dataUnit);
  6294. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6295. END;
  6296. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6297. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6298. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6299. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6300. END;
  6301. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6302. IF variable.initializer # NIL THEN
  6303. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6304. END;
  6305. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6306. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6307. pointerType.SetPointerBase(variable.type);
  6308. pointerType.SetHidden(TRUE);
  6309. variable.SetType(ResolveType(pointerType));
  6310. END;
  6311. variable.SetState(SyntaxTree.Resolved);
  6312. END;
  6313. END VisitVariable;
  6314. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6315. BEGIN
  6316. VisitVariable(property)
  6317. END VisitProperty;
  6318. (** check and resolve a (procedure) parameter
  6319. - check and set type
  6320. - check symbol
  6321. - check parameter kind and set read-only flags if appropriate
  6322. **)
  6323. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6324. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6325. BEGIN
  6326. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6327. IF SymbolNeedsResolution(parameter) THEN
  6328. modifiers := parameter.modifiers;
  6329. parameter.SetType(ResolveType(parameter.type));
  6330. ASSERT(parameter.type.resolved # NIL);
  6331. CheckSymbolVisibility(parameter);
  6332. IF parameter.defaultValue # NIL THEN
  6333. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6334. Error(parameter.position,"forbidden default value on non-value parameter");
  6335. ELSE
  6336. expression := ConstantExpression(parameter.defaultValue);
  6337. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6338. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6339. parameter.SetDefaultValue(expression);
  6340. END;
  6341. END;
  6342. END;
  6343. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6344. Error(parameter.position, "forbidden value parameter of math array type ");
  6345. END;
  6346. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6347. parameter.SetUntraced(TRUE);
  6348. IF ~ContainsPointer(parameter.type) THEN
  6349. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6350. Error(position, "untraced flag on non-pointer variable");
  6351. END;
  6352. END;
  6353. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6354. parameter.SetMoveable(TRUE);
  6355. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6356. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6357. Error(position, "illegal movable flag on non-address variable");
  6358. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6359. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6360. Error(position, "unnecessary movable flag on variable variable");
  6361. END;
  6362. END;
  6363. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6364. parameter.SetState(SyntaxTree.Resolved);
  6365. END;
  6366. END VisitParameter;
  6367. (** check and resolve a procedure (with declaration and implementation scope)
  6368. - check the procedure type
  6369. - check if method (i.e. in record scope), if so then
  6370. - check if (unique) constructor
  6371. - check if (unique) finalizer
  6372. - check if super method available, if so then check signature
  6373. - of not in record scope then negative check on constructor flag
  6374. - of not in record scope then negative check on finalizer flag
  6375. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6376. - check procedure symbol
  6377. **)
  6378. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6379. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6380. procedureType: SyntaxTree.ProcedureType;
  6381. type: SyntaxTree.Type;
  6382. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6383. qualifiedType: SyntaxTree.QualifiedType;
  6384. value: LONGINT;
  6385. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6386. position: Position;
  6387. fp: SyntaxTree.FingerPrint;
  6388. BEGIN
  6389. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6390. IF IsOberonInline(procedure) THEN
  6391. IF SyntaxTree.Public * procedure.access # {} THEN
  6392. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6393. END;
  6394. procedure.SetInline(FALSE);
  6395. procedure.SetOberonInline(TRUE);
  6396. END;
  6397. IF SymbolNeedsResolution(procedure) THEN
  6398. recentIsRealtime := currentIsRealtime;
  6399. recentIsBodyProcedure := currentIsBodyProcedure;
  6400. CheckSymbolVisibility(procedure);
  6401. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6402. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6403. modifiers := procedureType.modifiers;
  6404. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6405. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6406. IF useDarwinCCalls THEN (*fld*)
  6407. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6408. ELSE
  6409. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6410. END
  6411. END;
  6412. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6413. procedureType.SetInterrupt(TRUE);
  6414. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6415. END;
  6416. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6417. procedureType.SetNoReturn(TRUE);
  6418. END;
  6419. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6420. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6421. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6422. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6423. END;
  6424. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6425. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6426. END;
  6427. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6428. IF ~PowerOf2(value) THEN
  6429. Error(position, "forbidden stack alignment - must be power of two");
  6430. ELSE
  6431. procedureType.SetStackAlignment(value)
  6432. END;
  6433. END;
  6434. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6435. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6436. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6437. END;
  6438. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6439. SyntaxTree.InitFingerPrint(fp);
  6440. fp.shallow := value;
  6441. fp.public := value;
  6442. fp.private := value;
  6443. fp.shallowAvailable := TRUE;
  6444. procedure.SetFingerPrint(fp);
  6445. END;
  6446. CheckModifiers(modifiers, TRUE);
  6447. modifiers := procedureType.returnTypeModifiers;
  6448. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6449. CheckModifiers(modifiers, TRUE);
  6450. procedure.SetState(SyntaxTree.Resolved);
  6451. FixProcedureType(procedureType);
  6452. currentIsRealtime := procedureType.isRealtime;
  6453. currentIsBodyProcedure := procedure.isBodyProcedure;
  6454. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6455. THEN
  6456. Error(procedure.position,"problems during parameter offset computation");
  6457. END;
  6458. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6459. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6460. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6461. Error(procedure.position,"problem during parameter offset generation");
  6462. END;
  6463. END;
  6464. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6465. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6466. procedureType.SetDelegate(TRUE);
  6467. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6468. (* add auto-self *)
  6469. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6470. IF (record.pointerType.typeDeclaration = NIL) THEN
  6471. selfParameter.SetType(record.pointerType);
  6472. ELSE
  6473. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6474. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6475. qualifiedType.SetResolved(record.pointerType);
  6476. selfParameter.SetType(qualifiedType);
  6477. END;
  6478. selfParameter.SetAccess(SyntaxTree.Hidden);
  6479. END;
  6480. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6481. IF procedure.isConstructor THEN
  6482. (*! constructor is always visible, compatibility to paco
  6483. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6484. *)
  6485. procedure.MarkUsed;
  6486. IF procedureType.returnType # NIL THEN
  6487. Error(procedure.position,"constructor with forbidden return type");
  6488. END;
  6489. proc := procedure.scope.firstProcedure;
  6490. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6491. proc := proc.nextProcedure;
  6492. END;
  6493. IF proc # NIL THEN
  6494. Error(procedure.position,"duplicate constructor")
  6495. ELSE
  6496. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6497. END;
  6498. END;
  6499. IF procedure.isFinalizer THEN
  6500. procedure.MarkUsed;
  6501. IF procedureType.returnType # NIL THEN
  6502. Error(procedure.position,"finalizer with forbidden return type");
  6503. END;
  6504. IF procedureType.numberParameters # 0 THEN
  6505. Error(procedure.position,"finalizer with formal parameters");
  6506. END;
  6507. proc := procedure.scope.firstProcedure;
  6508. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6509. proc := proc.nextProcedure;
  6510. END;
  6511. IF proc # NIL THEN
  6512. Error(procedure.position,"duplicate finalizer")
  6513. ELSE
  6514. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6515. END;
  6516. END;
  6517. super := FindSuperProcedure(record.recordScope, procedure);
  6518. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6519. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6520. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6521. END;
  6522. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6523. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6524. END;
  6525. IF super.isFinal THEN
  6526. Error(procedure.position,"forbidden method extending final method");
  6527. END;
  6528. (*
  6529. IF super.access # procedure.access THEN
  6530. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6531. END;
  6532. *)
  6533. procedure.SetSuper(super);
  6534. super.SetOverwritten(TRUE);
  6535. procedure.SetAccess(procedure.access+super.access);
  6536. procedure.MarkUsed;
  6537. END;
  6538. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6539. THEN
  6540. Error(procedure.position,"problems during parameter offset computation");
  6541. END;
  6542. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6543. IF cellsAreObjects THEN
  6544. procedureType.SetDelegate(TRUE);
  6545. END;
  6546. IF procedure.isConstructor THEN
  6547. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6548. END;
  6549. ELSIF procedure.isConstructor THEN
  6550. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6551. END;
  6552. Declarations(procedure.procedureScope, FALSE, {0,1});
  6553. (* body resolution part done as late fix of the procedure type *)
  6554. procedure.SetState(SyntaxTree.Resolved);
  6555. currentIsRealtime := recentIsRealtime;
  6556. currentIsBodyProcedure := recentIsBodyProcedure;
  6557. END;
  6558. END VisitProcedure;
  6559. (**
  6560. a builtin procedure is a global item that may not be modified locally
  6561. instead the resolving of builtin procedure calls are done in the esignator
  6562. **)
  6563. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6564. VAR type: SyntaxTree.Type;
  6565. BEGIN
  6566. type := ResolveType(builtinProcedure.type);
  6567. END VisitBuiltin;
  6568. (* nopov *)
  6569. (** check and resolve operator
  6570. - operators are first checked as procedures
  6571. - then additional operator-specific checks are done
  6572. - note that only module-scope operators are checked here
  6573. (operators in a record scope are only allowed in the context of
  6574. array-structured object types and checked in 'ResolveArrayStructure')
  6575. - also note that inter-operator conformity is not checked here
  6576. **)
  6577. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6578. VAR
  6579. procedureType: SyntaxTree.ProcedureType;
  6580. leftType, rightType: SyntaxTree.Type;
  6581. identifierNumber: LONGINT; position: Position;
  6582. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6583. modifiers: SyntaxTree.Modifier;
  6584. (** whether a type is locally defined in the current module scope
  6585. for arrays, the base type must be locally defined **)
  6586. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6587. BEGIN
  6588. IF type = NIL THEN
  6589. RETURN FALSE
  6590. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6591. RETURN TRUE
  6592. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6593. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6594. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6595. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6596. ELSE
  6597. RETURN FALSE
  6598. END
  6599. END IsLocallyDefined;
  6600. BEGIN
  6601. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6602. procedureType := operator.type(SyntaxTree.ProcedureType);
  6603. modifiers := procedureType.modifiers;
  6604. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6605. CheckModifiers(modifiers, TRUE);
  6606. VisitProcedure(operator);
  6607. IF operator.scope IS SyntaxTree.RecordScope THEN
  6608. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6609. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6610. IF identifierNumber = -1 THEN
  6611. Error(operator.position, "operator with unknown identifier")
  6612. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6613. Error(operator.position, "identifier may not be used for operator")
  6614. ELSE
  6615. IF procedureType.numberParameters < 1 THEN
  6616. Error(operator.position, "operator without operand");
  6617. ELSIF procedureType.numberParameters > 2 THEN
  6618. Error(operator.position, "operator with more than two operands");
  6619. ELSE
  6620. (* determine operand types *)
  6621. leftType := procedureType.firstParameter.type;
  6622. IF procedureType.numberParameters > 1 THEN
  6623. rightType := procedureType.firstParameter.nextParameter.type
  6624. ELSE
  6625. rightType := NIL
  6626. END;
  6627. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6628. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6629. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6630. Error(operator.position, "none of the operands is declared in the same module")
  6631. END
  6632. END;
  6633. (* TODO: refine the checks, think about how restrictive the checks should be
  6634. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6635. They might be used for intersection, union, complement of custom object types *)
  6636. (* defaults *)
  6637. hasReturnType := TRUE;
  6638. mustBeUnary := FALSE;
  6639. mustBeBinary := FALSE;
  6640. mustReturnBoolean := FALSE;
  6641. mustReturnInteger := FALSE;
  6642. mustHaveEquitypedOperands := FALSE;
  6643. (* operator-specific exceptions *)
  6644. CASE identifierNumber OF
  6645. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6646. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6647. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6648. mustBeBinary := TRUE
  6649. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6650. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6651. | Scanner.Times: mustBeBinary := TRUE
  6652. | Scanner.TimesTimes: mustBeBinary := TRUE
  6653. | Scanner.DotTimes: mustBeBinary := TRUE
  6654. | Scanner.PlusTimes: mustBeBinary := TRUE
  6655. | Scanner.Slash: mustBeBinary := TRUE
  6656. | Scanner.Backslash: mustBeBinary := TRUE
  6657. | Scanner.DotSlash: mustBeBinary := TRUE
  6658. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6659. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6660. | Scanner.Not: mustBeUnary := TRUE
  6661. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6662. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6663. | Scanner.Transpose: mustBeUnary := TRUE;
  6664. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6665. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6666. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6667. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6668. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6669. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6670. | Global.Abs: mustBeUnary := TRUE;
  6671. | Global.Ash: (* TODO: arity? *)
  6672. | Global.Cap: (* TODO: arity? *)
  6673. | Global.Chr: mustBeUnary := TRUE;
  6674. | Global.Entier: (* TODO: arity? *)
  6675. | Global.EntierH: (* TODO: arity? *)
  6676. | Global.Len: (* unary and binary *)
  6677. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6678. | Global.Max, Global.Min: (* unary and binary *)
  6679. | Global.Odd: (* TODO: arity? *)
  6680. | Global.Sum: (* TODO: arity? *)
  6681. | Global.All: (* TODO: arity? *)
  6682. | Global.Re, Global.Im:
  6683. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6684. | Scanner.Alias:
  6685. | Scanner.GreaterGreater, Scanner.LessLess:
  6686. mustBeBinary := TRUE; hasReturnType := FALSE;
  6687. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6688. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6689. END;
  6690. (* check parameter count *)
  6691. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6692. Error(operator.position,"operator is not unary")
  6693. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6694. Error(operator.position,"operator is not binary")
  6695. END;
  6696. (* check parameter types *)
  6697. (* TODO: is this used at all? *)
  6698. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6699. leftType := procedureType.firstParameter.type;
  6700. rightType := procedureType.firstParameter.nextParameter.type;
  6701. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6702. Error(operator.position, "the two operands are not of the same type")
  6703. END
  6704. END;
  6705. (* check return type *)
  6706. IF hasReturnType THEN
  6707. IF procedureType.returnType = NIL THEN
  6708. Error(operator.position, "return type required")
  6709. ELSIF mustReturnBoolean THEN
  6710. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6711. Error(operator.position,"return type is not Boolean")
  6712. END
  6713. ELSIF mustReturnInteger THEN
  6714. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6715. Error(operator.position,"return type is not integer")
  6716. END
  6717. END
  6718. ELSIF procedureType.returnType # NIL THEN
  6719. Error(operator.position, "return type not allowed")
  6720. END
  6721. END
  6722. END
  6723. END
  6724. END VisitOperator;
  6725. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6726. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6727. BEGIN
  6728. IF error THEN RETURN FALSE END;
  6729. prevScope := currentScope;
  6730. prevDiagnostics := diagnostics;
  6731. diagnostics := NIL; (* suppress error output *)
  6732. currentScope := module.moduleScope;
  6733. VisitImport(x);
  6734. IF ~error THEN
  6735. module.moduleScope.AddImport(x);
  6736. x.SetScope(module.moduleScope);
  6737. END;
  6738. currentScope := prevScope;
  6739. diagnostics := prevDiagnostics;
  6740. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6741. END AddImport;
  6742. (** check and resolve import
  6743. - check for name = SYSTEM
  6744. - check for forbidden self import
  6745. - search through global import cache: already imported?
  6746. - check if already imported indirectly
  6747. - import if necessary -> set module and enter into import cache
  6748. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6749. - after this import this direct import and all indirect imports are stored in the current module's import list
  6750. **)
  6751. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6752. VAR
  6753. module: SyntaxTree.Module;
  6754. moduleScope: SyntaxTree.ModuleScope;
  6755. import,reimport: SyntaxTree.Import;
  6756. filename: FileName;
  6757. prevScope: SyntaxTree.Scope;
  6758. BEGIN
  6759. IF SymbolNeedsResolution(x) THEN
  6760. prevScope := currentScope;
  6761. x.SetType(SyntaxTree.importType);
  6762. moduleScope := currentScope.ownerModule.moduleScope;
  6763. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6764. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6765. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6766. Error(x.position,"forbidden self import");
  6767. ELSE
  6768. (* search through global import list: already imported ? *)
  6769. IF (x.module = NIL) & (importCache # NIL) THEN
  6770. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6771. ELSE import := NIL
  6772. END;
  6773. IF x.module # NIL THEN (* already imported indirectly *)
  6774. module := x.module;
  6775. ELSIF import # NIL THEN (* already in module list *)
  6776. module := import.module;
  6777. ASSERT(module # NIL);
  6778. x.SetModule(module);
  6779. ELSE (* must be imported *)
  6780. Global.ModuleFileName(x.moduleName,x.context,filename);
  6781. IF symbolFileFormat # NIL THEN
  6782. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6783. IF module = NIL THEN
  6784. ErrorSS(x.position,"could not import",filename);
  6785. IF VerboseErrorMessage THEN
  6786. Printout.Info("import",x)
  6787. END
  6788. ELSE
  6789. (*
  6790. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6791. (*! should rather be done by importer *)
  6792. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6793. checker.importCache := importCache;
  6794. checker.arrayBaseImported := arrayBaseImported;
  6795. checker.global := global;
  6796. checker.Module(module); (* semantic check *)
  6797. error := error OR checker.error;
  6798. END;
  6799. *)
  6800. (*
  6801. ASSERT(SyntaxTree.Resolved IN module.state);
  6802. *)
  6803. x.SetModule(module);
  6804. IF importCache # NIL THEN
  6805. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6806. import.SetContext(x.context);
  6807. import.SetModule(module);
  6808. importCache.AddImport(import);
  6809. END;
  6810. END;
  6811. ELSE
  6812. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6813. END;
  6814. END;
  6815. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6816. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6817. END;
  6818. import := module.moduleScope.firstImport;
  6819. WHILE(import # NIL) DO
  6820. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6821. ASSERT(currentScope # NIL);
  6822. ASSERT(currentScope.ownerModule # NIL);
  6823. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6824. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6825. Error(x.position,"recursive import");
  6826. ELSE
  6827. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6828. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6829. IF reimport = NIL THEN (* indirect import *)
  6830. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6831. reimport.SetContext(import.context);
  6832. reimport.SetModule(import.module);
  6833. moduleScope.AddImport(reimport);
  6834. reimport.SetScope(moduleScope);
  6835. ELSE
  6836. ASSERT(import.module # NIL);
  6837. reimport.SetModule(import.module); (* direct or indirect import *)
  6838. END;
  6839. END;
  6840. import := import.nextImport;
  6841. END;
  6842. END;
  6843. END;
  6844. currentScope := prevScope;
  6845. (* ELSE nothing to be done *)
  6846. x.SetState(SyntaxTree.Resolved);
  6847. END;
  6848. END VisitImport;
  6849. (*** statements ***)
  6850. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6851. VAR prev,resolved: SyntaxTree.Statement;
  6852. BEGIN
  6853. prev := resolvedStatement;
  6854. resolvedStatement := x;
  6855. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6856. activeCellsStatement := FALSE;
  6857. x.Accept(SELF);
  6858. (* removed this, implementation restriction should be resolved by backend
  6859. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6860. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6861. END;
  6862. *)
  6863. resolved := resolvedStatement;
  6864. resolvedStatement := prev;
  6865. RETURN resolved
  6866. END ResolveStatement;
  6867. (** check and resolve statement sequence
  6868. - check all statements, replace if necessary
  6869. **)
  6870. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6871. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6872. BEGIN
  6873. IF statementSequence # NIL THEN (* else empty *)
  6874. FOR i := 0 TO statementSequence.Length()-1 DO
  6875. statement := statementSequence.GetStatement(i);
  6876. resolved := ResolveStatement(statement);
  6877. IF (resolved # statement) THEN
  6878. statementSequence.SetStatement(i,resolved);
  6879. END;
  6880. END;
  6881. END;
  6882. END StatementSequence;
  6883. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6884. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6885. - check if procedure is callable
  6886. - check return type = NIL (otherwise must be assignment statement)
  6887. **)
  6888. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6889. VAR call: SyntaxTree.Designator;
  6890. BEGIN
  6891. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6892. call := procedureCall.call;
  6893. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6894. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6895. END;
  6896. call := ResolveDesignator(call);
  6897. IF call = SyntaxTree.invalidDesignator THEN
  6898. (* error already handled *)
  6899. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6900. (* inline call in a statement *)
  6901. ELSIF ~IsCallable(call) THEN
  6902. Error(procedureCall.position,"called object is not a procedure");
  6903. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6904. Error(procedureCall.position,"calling procedure with non-void return type");
  6905. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6906. END;
  6907. procedureCall.SetCall(call);
  6908. (*
  6909. IF call = SyntaxTree.invalidDesignator THEN
  6910. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6911. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6912. IF IsOberonInline(procedure) THEN
  6913. Warning(procedure.position,"call to inline proc");
  6914. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6915. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6916. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6917. resolvedStatement := block;
  6918. RETURN;
  6919. END;
  6920. END;
  6921. *)
  6922. END VisitProcedureCallStatement;
  6923. (** check and resolve assignment LHS := RHS
  6924. - resolve LHS and RHS
  6925. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6926. - check if assignment is compatible
  6927. - check if LHS is variable (i.e. assignable)
  6928. - convert RHS if necessary
  6929. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6930. - assignment between different ASOTs
  6931. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6932. - assignment to ASOT elements:
  6933. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6934. **)
  6935. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6936. VAR
  6937. left: SyntaxTree.Designator;
  6938. right, expression: SyntaxTree.Expression;
  6939. designator: SyntaxTree.Designator;
  6940. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6941. mathArrayType: SyntaxTree.MathArrayType;
  6942. BEGIN
  6943. right := ResolveExpression(assignment.right);
  6944. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6945. left := ResolveDesignator(assignment.left);
  6946. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6947. (* error already handled *)
  6948. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6949. (* LHS is index write operator call on ASOT *)
  6950. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6951. (* necessary ?
  6952. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6953. type := procedureType.firstParameter.type;
  6954. expression := procedureCallDesignator.parameters.GetExpression(0);
  6955. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6956. *)
  6957. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6958. ELSIF CheckVariable(left) THEN
  6959. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6960. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6961. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6962. (* conversion done by procedure call
  6963. (* try to convert to left argument *)
  6964. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6965. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6966. procedureCallDesignator.parameters.SetExpression(1, right);
  6967. END;
  6968. *)
  6969. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6970. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6971. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6972. ELSIF AssignmentCompatible(left, right) THEN
  6973. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6974. mathArrayType := MathArrayStructureOfType(left.type);
  6975. right := NewConversion(right.position, right, mathArrayType, NIL);
  6976. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6977. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6978. ELSE
  6979. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6980. assignment.SetLeft(left);
  6981. assignment.SetRight(right);
  6982. resolvedStatement := assignment
  6983. END
  6984. END
  6985. END
  6986. END VisitAssignment;
  6987. (** check and resolve assignment LHS := RHS
  6988. - resolve LHS and RHS
  6989. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6990. - check if assignment is compatible
  6991. - check if LHS is variable (i.e. assignable)
  6992. - convert RHS if necessary
  6993. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6994. - assignment between different ASOTs
  6995. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6996. - assignment to ASOT elements:
  6997. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6998. **)
  6999. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7000. VAR
  7001. left: SyntaxTree.Designator;
  7002. right: SyntaxTree.Expression;
  7003. inPort, outPort: SyntaxTree.PortType;
  7004. expression: SyntaxTree.Expression;
  7005. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7006. BEGIN
  7007. right := ResolveExpression(communication.right);
  7008. left := ResolveDesignator(communication.left);
  7009. communication.SetLeft(left);
  7010. communication.SetRight(right);
  7011. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7012. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7013. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7014. (* conversion done by procedure call
  7015. (* try to convert to left argument *)
  7016. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7017. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7018. procedureCallDesignator.parameters.SetExpression(1, right);
  7019. END;
  7020. *)
  7021. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7022. ELSE
  7023. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7024. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7025. (* error already handled *)
  7026. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7027. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7028. IF outPort.direction # SyntaxTree.OutPort THEN
  7029. Error(left.position,"not an out-port")
  7030. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7031. Error(left.position,"incompatible to port type");
  7032. ELSE
  7033. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7034. communication.SetRight(right)
  7035. END;
  7036. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7037. IF CheckVariable(left) THEN
  7038. IF inPort.direction # SyntaxTree.InPort THEN
  7039. Error(left.position,"not an in-port")
  7040. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7041. Error(right.position,"incompatible to port type");
  7042. END;
  7043. END;
  7044. ELSE
  7045. Error(communication.position,"unsupported stream operation");
  7046. END;
  7047. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7048. IF outPort.direction # SyntaxTree.OutPort THEN
  7049. Error(left.position,"not an out-port")
  7050. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7051. Error(left.position,"incompatible to port type");
  7052. ELSE
  7053. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7054. communication.SetRight(right)
  7055. END;
  7056. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7057. IF CheckVariable(right) THEN
  7058. IF inPort.direction # SyntaxTree.InPort THEN
  7059. Error(left.position,"not an in-port")
  7060. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7061. Error(right.position,"incompatible to port type");
  7062. END;
  7063. END;
  7064. ELSE
  7065. Error(communication.position, "unsupported operation");
  7066. END;
  7067. END;
  7068. END VisitCommunicationStatement;
  7069. (** check and resolve if/eslif part
  7070. - check condition
  7071. - check statement sequence
  7072. **)
  7073. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7074. VAR prevUnreachable, b: BOOLEAN;
  7075. BEGIN
  7076. prevUnreachable := currentIsUnreachable;
  7077. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7078. IF IsBooleanValue(ifPart.condition,b) THEN
  7079. IF b=FALSE THEN
  7080. currentIsUnreachable := TRUE
  7081. ELSIF b=TRUE THEN
  7082. true := TRUE
  7083. END;
  7084. END;
  7085. StatementSequence(ifPart.statements);
  7086. currentIsUnreachable := prevUnreachable;
  7087. END IfPart;
  7088. (** check and resolve if statement
  7089. - check if parts and else part statement sequence
  7090. **)
  7091. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7092. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7093. BEGIN
  7094. prevUnreachable := currentIsUnreachable;
  7095. ifPartTrue := FALSE;
  7096. IfPart(ifStatement.ifPart,ifPartTrue);
  7097. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7098. elsif := ifStatement.GetElsifPart(i);
  7099. IfPart(elsif,ifPartTrue);
  7100. END;
  7101. IF ifStatement.elsePart # NIL THEN
  7102. IF ifPartTrue THEN
  7103. currentIsUnreachable := TRUE
  7104. END;
  7105. StatementSequence(ifStatement.elsePart)
  7106. END;
  7107. currentIsUnreachable := prevUnreachable;
  7108. END VisitIfStatement;
  7109. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7110. VAR variable: SyntaxTree.Designator;
  7111. type,variableType: SyntaxTree.Type;
  7112. withEntry: WithEntry;
  7113. BEGIN
  7114. variable := ResolveDesignator(withPart.variable);
  7115. variableType := variable.type.resolved;
  7116. withPart.SetVariable(variable);
  7117. type := ResolveType(withPart.type);
  7118. withPart.SetType(type);
  7119. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7120. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7121. END;
  7122. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7123. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7124. Error(variable.position,"is not extensible designator");
  7125. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7126. Error(variable.position,"is no local variable ");
  7127. IF VerboseErrorMessage THEN
  7128. Printout.Info("variable",variable)
  7129. END;
  7130. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7131. Error(variable.position,"withguarded symbol is no type extension of ");
  7132. IF VerboseErrorMessage THEN
  7133. Printout.Info("variable",variable);
  7134. Printout.Info("type",type);
  7135. END;
  7136. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7137. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7138. Error(variable.position,"withguarded symbol is no variable ");
  7139. IF VerboseErrorMessage THEN
  7140. Printout.Info("variable",variable);
  7141. Printout.Info("type",type);
  7142. END;
  7143. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7144. Error(variable.position,"invalid change of withguarded symbol");
  7145. ELSE
  7146. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7147. NEW(withEntry);
  7148. withEntry.previous := withEntries;
  7149. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7150. withEntry.type := type;
  7151. withEntries := withEntry;
  7152. StatementSequence(withPart.statements);
  7153. withEntries := withEntries.previous;
  7154. END;
  7155. END WithPart;
  7156. (** check and resolve with statement WITH variable: type DO ... END;
  7157. - check type and variable
  7158. - check that variable type is type extension of type
  7159. - check that variable is a variable
  7160. - enter new with scope and enter guardedVariable with same name and reference to variable
  7161. - create if statement:
  7162. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7163. **)
  7164. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7165. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7166. BEGIN
  7167. prevScope := currentScope; symbol := NIL;
  7168. FOR i := 0 TO withStatement.WithParts()-1 DO
  7169. WithPart(withStatement.GetWithPart(i),symbol);
  7170. END;
  7171. IF withStatement.elsePart # NIL THEN
  7172. StatementSequence(withStatement.elsePart)
  7173. END;
  7174. currentScope := prevScope;
  7175. END VisitWithStatement;
  7176. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7177. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7178. - check 'first' < 'last' and no overlaps between different case labels
  7179. - check statement sequence
  7180. **)
  7181. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7182. VAR
  7183. i: LONGINT;
  7184. position: Position;
  7185. expression, left, right: SyntaxTree.Expression;
  7186. expressionType: SyntaxTree.Type;
  7187. l, r: LONGINT;
  7188. cl, cr: CHAR;
  7189. thiscases: SyntaxTree.CaseConstant;
  7190. BEGIN
  7191. thiscases := NIL;
  7192. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7193. expression := casePart.elements.GetExpression(i);
  7194. position := expression.position;
  7195. (* set context of range *)
  7196. IF expression IS SyntaxTree.RangeExpression THEN
  7197. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7198. END;
  7199. expression := ResolveExpression(expression);
  7200. IF expression = SyntaxTree.invalidExpression THEN
  7201. (* error already reported *)
  7202. expressionType := SyntaxTree.invalidType;
  7203. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7204. (* read out 'first' and 'last' *)
  7205. left := expression(SyntaxTree.RangeExpression).first;
  7206. right := expression(SyntaxTree.RangeExpression).last;
  7207. (* guaranteed by VisitRangeExpression: *)
  7208. ASSERT((left # NIL) & (right # NIL));
  7209. ASSERT(left.type.resolved = right.type.resolved);
  7210. left := CompatibleConversion(left.position, left, type);
  7211. right := CompatibleConversion(right.position, right, type);
  7212. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7213. expression(SyntaxTree.RangeExpression).SetLast(right);
  7214. expressionType := RegularType(position,left.type);
  7215. ELSE
  7216. expression := ConstantExpression(expression);
  7217. expression := CompatibleConversion(expression.position, expression, type);
  7218. (*
  7219. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7220. left := Global.NewCharacterValue(system,expression.position,cl);
  7221. expression := casePart.elements.GetExpression(i);
  7222. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7223. expression := left
  7224. END;
  7225. *)
  7226. casePart.elements.SetExpression(i,expression);
  7227. left := expression; right := expression;
  7228. expressionType := RegularType(position,expression.type)
  7229. END;
  7230. IF (expressionType = SyntaxTree.invalidType) THEN
  7231. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7232. Error(position, "inadmissible case label");
  7233. expression := SyntaxTree.invalidExpression;
  7234. ELSE
  7235. l := 0; r := 0;
  7236. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7237. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7238. l := ORD(cl); r := ORD(cr);
  7239. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7240. ELSE
  7241. expression := SyntaxTree.invalidExpression
  7242. END;
  7243. IF expression # SyntaxTree.invalidExpression THEN
  7244. IF l>r THEN
  7245. Error(position, "empty case label")
  7246. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7247. Error(position, "duplicate case label");
  7248. ELSE
  7249. IF l < min THEN min := l END;
  7250. IF r > max THEN max := r END;
  7251. END;
  7252. END;
  7253. END;
  7254. casePart.elements.SetExpression(i,expression);
  7255. END;
  7256. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7257. casePart.SetConstants(thiscases);
  7258. StatementSequence(casePart.statements);
  7259. END CasePart;
  7260. (** check and resolve case statement CASE variable OF ... END;
  7261. - check variable
  7262. - check case parts
  7263. **)
  7264. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7265. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7266. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7267. BEGIN
  7268. expression := ResolveExpression(caseStatement.variable);
  7269. type := RegularType(expression.position,expression.type);
  7270. IF type = SyntaxTree.invalidType THEN
  7271. expression := SyntaxTree.invalidExpression;
  7272. ELSIF IsIntegerType(type) THEN
  7273. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7274. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7275. (*
  7276. expression := Global.NewCharacterValue(system,expression.position,ch);
  7277. *)
  7278. type := expression.type;
  7279. ELSIF IsCharacterType(type) THEN
  7280. ELSIF IsEnumerationType(type) THEN
  7281. ELSE
  7282. Error(caseStatement.variable.position,"variable must be integer or character type");
  7283. expression := SyntaxTree.invalidExpression;
  7284. END;
  7285. caseStatement.SetVariable(expression);
  7286. caseList := NIL;
  7287. min := MAX(LONGINT); max := MIN(LONGINT);
  7288. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7289. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7290. END;
  7291. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7292. msg := "huge sparse case table ";
  7293. Strings.AppendInt(msg, max-min);
  7294. Strings.Append(msg,"/");
  7295. Strings.AppendInt(msg, caseStatement.CaseParts());
  7296. Warning(caseStatement.position,msg);
  7297. END;
  7298. caseStatement.SetMinMax(min,max);
  7299. StatementSequence(caseStatement.elsePart);
  7300. IF expression.resolved # NIL THEN
  7301. IF IsCharacterValue(expression,ch) THEN
  7302. l := ORD(ch)
  7303. ELSIF IsIntegerValue(expression,l) THEN
  7304. END;
  7305. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7306. END;
  7307. END VisitCaseStatement;
  7308. (** check and resolve while statement
  7309. - check condition
  7310. - check statement sequence
  7311. **)
  7312. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7313. VAR prevIsUnreachable,b: BOOLEAN;
  7314. BEGIN
  7315. prevIsUnreachable := currentIsUnreachable;
  7316. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7317. IF IsBooleanValue(whileStatement.condition,b) THEN
  7318. IF b=FALSE THEN
  7319. currentIsUnreachable := TRUE
  7320. END;
  7321. END;
  7322. StatementSequence(whileStatement.statements);
  7323. currentIsUnreachable := prevIsUnreachable
  7324. END VisitWhileStatement;
  7325. (** check and resolve repeat statement
  7326. - check condition
  7327. - check statement sequence
  7328. **)
  7329. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7330. BEGIN
  7331. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7332. StatementSequence(repeatStatement.statements);
  7333. END VisitRepeatStatement;
  7334. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7335. VAR withEntry: WithEntry;
  7336. BEGIN
  7337. withEntry := withEntries;
  7338. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7339. withEntry := withEntry.previous
  7340. END;
  7341. IF withEntry = NIL THEN RETURN FALSE
  7342. ELSE
  7343. type := withEntry.type;
  7344. RETURN TRUE
  7345. END;
  7346. END GetGuard;
  7347. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7348. - check that variable is an integer variable
  7349. - check that from is integer typed with compatible type
  7350. - check that to has compatible type
  7351. - check that by is constant integer with compatible type
  7352. **)
  7353. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7354. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7355. BEGIN
  7356. designator := ResolveDesignator(forStatement.variable);
  7357. type := SyntaxTree.invalidType;
  7358. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7359. designator := SyntaxTree.invalidDesignator;
  7360. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7361. Error(designator.position,"control variable of non-integer type");
  7362. designator := SyntaxTree.invalidDesignator;
  7363. ELSIF CheckVariable(designator) THEN
  7364. type := designator.type;
  7365. END;
  7366. forStatement.SetVariable(designator);
  7367. expression := ResolveExpression(forStatement.from);
  7368. IF expression = SyntaxTree.invalidExpression THEN
  7369. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7370. Error(expression.position,"start value of incompatible type");
  7371. expression := SyntaxTree.invalidExpression;
  7372. ELSIF type # SyntaxTree.invalidType THEN
  7373. expression := NewConversion(expression.position,expression,type,NIL)
  7374. END;
  7375. forStatement.SetFrom(expression);
  7376. expression := ResolveExpression(forStatement.to);
  7377. IF expression = SyntaxTree.invalidExpression THEN
  7378. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7379. Error(expression.position,"end value of incompatible type");
  7380. expression := SyntaxTree.invalidExpression;
  7381. ELSIF type # SyntaxTree.invalidType THEN
  7382. expression := NewConversion(expression.position,expression,type,NIL)
  7383. END;
  7384. forStatement.SetTo(expression);
  7385. IF forStatement.by # NIL THEN
  7386. expression := ConstantInteger(forStatement.by);
  7387. ELSE
  7388. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7389. END;
  7390. IF expression = SyntaxTree.invalidExpression THEN
  7391. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7392. Error(expression.position,"step value of incompatible type");
  7393. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7394. Error(expression.position,"invalid step value");
  7395. ELSIF type # SyntaxTree.invalidType THEN
  7396. expression := NewConversion(expression.position,expression,type,NIL)
  7397. END;
  7398. forStatement.SetBy(expression);
  7399. StatementSequence(forStatement.statements);
  7400. END VisitForStatement;
  7401. (** check and resolve loop statement LOOP StatementSequence END
  7402. - check statement sequence
  7403. **)
  7404. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7405. BEGIN
  7406. StatementSequence(loopStatement.statements)
  7407. END VisitLoopStatement;
  7408. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7409. BEGIN
  7410. StatementSequence(exitableBlock.statements);
  7411. END VisitExitableBlock;
  7412. (** check and resolve exit statement EXIT
  7413. - check that exit is within LOOP statement block
  7414. **)
  7415. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7416. VAR outer: SyntaxTree.Statement;
  7417. BEGIN
  7418. outer := exitStatement.outer;
  7419. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7420. outer := outer.outer;
  7421. END;
  7422. IF outer = NIL THEN
  7423. Error(exitStatement.position,"exit statement not within loop statement");
  7424. END;
  7425. END VisitExitStatement;
  7426. (** check and resolve return statement RETURN [expression]
  7427. - check expression (if any)
  7428. - check if in procedure scope
  7429. - if in procedure scope then check expression compatibility
  7430. - if not in procecdure scope then check on return without expression
  7431. **)
  7432. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7433. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7434. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7435. BEGIN
  7436. position := returnStatement.position;
  7437. expression := returnStatement.returnValue;
  7438. IF expression # NIL THEN
  7439. expression := ResolveExpression(expression);
  7440. returnStatement.SetReturnValue(expression);
  7441. END;
  7442. outer := returnStatement.outer;
  7443. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7444. outer := outer.outer
  7445. END;
  7446. IF (outer # NIL) THEN
  7447. scope := outer(SyntaxTree.Body).inScope;
  7448. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7449. IF (expression # NIL) THEN
  7450. Error(position, "return statement with parameter not in procedure scope");
  7451. END;
  7452. ELSE
  7453. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7454. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7455. Error(position, "return statement in procedure that does not return");
  7456. END;
  7457. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7458. IF returnType # NIL THEN
  7459. returnType := returnType.resolved;
  7460. IF expression = NIL THEN
  7461. Error(position, "empty return type in procedure providing a return type")
  7462. ELSIF expression.type = NIL THEN
  7463. Error(position,"returned type incompatible: expression has no type");
  7464. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7465. Error(position, "return type not compatible");
  7466. IF VerboseErrorMessage THEN
  7467. Printout.Info("returnType",returnType);
  7468. Printout.Info("expression",expression);
  7469. END;
  7470. ELSE
  7471. expression := NewConversion(expression.position,expression,returnType,NIL);
  7472. returnStatement.SetReturnValue(expression);
  7473. END;
  7474. ELSIF expression # NIL THEN
  7475. Error(position, "non-empty return type in procedure providing no return type");
  7476. END;
  7477. END;
  7478. END;
  7479. END VisitReturnStatement;
  7480. (** check and resolve await statement AWAIT(condition: Expression)
  7481. - check await condition
  7482. **)
  7483. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7484. VAR condition: SyntaxTree.Expression;
  7485. BEGIN
  7486. condition := ResolveCondition(awaitStatement.condition);
  7487. IF currentIsRealtime THEN
  7488. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7489. END;
  7490. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7491. Error(awaitStatement.position,"senseless await statement with constant condition");
  7492. END;
  7493. awaitStatement.SetCondition(condition);
  7494. END VisitAwaitStatement;
  7495. PROCEDURE CheckSystemImport(position: Position);
  7496. VAR import: SyntaxTree.Import;
  7497. BEGIN
  7498. import := currentScope.ownerModule.moduleScope.firstImport;
  7499. WHILE(import # NIL) DO
  7500. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7501. RETURN;
  7502. END;
  7503. import := import.nextImport;
  7504. END;
  7505. Error(position, "forbidden code without system import");
  7506. END CheckSystemImport;
  7507. (** check and resolve code statement: do nothing, must be done by assembler
  7508. **)
  7509. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7510. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7511. BEGIN
  7512. CheckSystemImport(code.position);
  7513. FOR i := 0 TO code.inRules.Length()-1 DO
  7514. statement := code.inRules.GetStatement(i);
  7515. IF statement IS SyntaxTree.Assignment THEN
  7516. WITH statement: SyntaxTree.Assignment DO
  7517. statement.SetRight(ResolveExpression(statement.right));
  7518. END;
  7519. ELSE
  7520. Error(statement.position, "can only be assignment")
  7521. END;
  7522. END;
  7523. FOR i := 0 TO code.outRules.Length()-1 DO
  7524. statement := code.outRules.GetStatement(i);
  7525. IF statement IS SyntaxTree.Assignment THEN
  7526. WITH statement: SyntaxTree.Assignment DO
  7527. statement.SetLeft(ResolveDesignator(statement.left));
  7528. END;
  7529. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7530. (* must be a reference to some register *)
  7531. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7532. ELSE
  7533. Printout.Info("out statement ", statement);
  7534. Error(statement.position, "(out) can only be assignment")
  7535. END;
  7536. END;
  7537. END VisitCode;
  7538. (** check and set flags of a statement block
  7539. - check for multiply occurence of a flag
  7540. - check and set priority only in bodies
  7541. - check for valid names
  7542. **)
  7543. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7544. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7545. flag: LONGINT; recordBody: SyntaxTree.Body;
  7546. PROCEDURE SetProtectedRecord;
  7547. VAR scope: SyntaxTree.Scope;
  7548. BEGIN
  7549. scope := currentScope;
  7550. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7551. scope := scope.outerScope
  7552. END;
  7553. IF scope # NIL THEN
  7554. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7555. END;
  7556. END SetProtectedRecord;
  7557. BEGIN
  7558. flags := {};
  7559. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7560. recordBody := block(SyntaxTree.Body)
  7561. ELSE
  7562. recordBody := NIL
  7563. END;
  7564. blockModifier := block.blockModifiers;
  7565. WHILE(blockModifier # NIL) DO
  7566. name := blockModifier.identifier;
  7567. expression := blockModifier.expression;
  7568. position := blockModifier.position;
  7569. flag := -1;
  7570. IF name=Global.NamePriority THEN
  7571. IF expression = NIL THEN
  7572. Error(position, "missing priority expression");
  7573. ELSIF recordBody = NIL THEN
  7574. Error(position, "priority not on record body");
  7575. ELSIF recordBody.priority # NIL THEN
  7576. Error(position, "duplicate priority expression");
  7577. ELSE
  7578. recordBody.SetPriority(expression);
  7579. END;
  7580. ELSIF expression # NIL THEN
  7581. Error(expression.position,"expression not in connection with priority")
  7582. ELSIF name=Global.NameExclusive THEN
  7583. IF block.isExclusive THEN
  7584. Error(position, "duplicate exclusive flag")
  7585. END;
  7586. block.SetExclusive(TRUE); SetProtectedRecord;
  7587. ELSIF name=Global.NameActive THEN
  7588. IF recordBody = NIL THEN
  7589. Error(position, "active not in record body");
  7590. ELSIF recordBody.isActive THEN
  7591. Error(position, "duplicate active flag")
  7592. ELSE
  7593. recordBody.SetActive(TRUE); SetProtectedRecord;
  7594. END;
  7595. ELSIF name=Global.NameSafe THEN
  7596. IF recordBody = NIL THEN
  7597. Error(position, "safe not in record body");
  7598. ELSIF recordBody.isSafe THEN
  7599. Error(position, "duplicate safe flag")
  7600. ELSE
  7601. recordBody.SetSafe(TRUE);
  7602. SetProtectedRecord;
  7603. END;
  7604. ELSIF name=Global.NameRealtime THEN
  7605. IF recordBody = NIL THEN
  7606. Error(position, "realtime not in record body");
  7607. ELSIF recordBody.isRealtime THEN
  7608. Error(position, "duplicate realtime flag")
  7609. ELSE
  7610. recordBody.SetRealtime(TRUE);
  7611. block.SetRealtime(TRUE);
  7612. END;
  7613. ELSIF name=Global.NameUnchecked THEN
  7614. IF block.isUnchecked THEN
  7615. Error(position, "duplicate unchecked flag")
  7616. ELSE
  7617. block.SetUnchecked(TRUE);
  7618. END;
  7619. ELSIF (name=Global.NameUncooperative) THEN
  7620. IF block.isUncooperative THEN
  7621. Error(position, "duplicate uncooperative flag")
  7622. ELSE
  7623. block.SetUncooperative(TRUE);
  7624. END;
  7625. ELSE
  7626. Error(position, "unknown block modifier");
  7627. END;
  7628. blockModifier := blockModifier.nextModifier;
  7629. END;
  7630. END BlockFlags;
  7631. (** check and resolve statement block
  7632. - check flags (exclusive)
  7633. - check statement sequence
  7634. **)
  7635. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7636. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7637. BEGIN
  7638. BlockFlags(statementBlock);
  7639. IF statementBlock.isExclusive THEN
  7640. (* check that not in exclusive block *)
  7641. IF currentIsExclusive THEN
  7642. Error (statementBlock.position,"forbidden recursive exclusive")
  7643. ELSIF currentIsRealtime THEN
  7644. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7645. END;
  7646. END;
  7647. recentExclusive := currentIsExclusive;
  7648. recentUnreachable := currentIsUnreachable;
  7649. recentRealtime := currentIsRealtime;
  7650. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7651. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7652. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7653. StatementSequence(statementBlock.statements);
  7654. currentIsRealtime := recentRealtime;
  7655. currentIsExclusive := recentExclusive;
  7656. currentIsUnreachable := recentUnreachable;
  7657. END VisitStatementBlock;
  7658. (** check and resolve body
  7659. - check flags (active, priority, safe)
  7660. - check body and finally part
  7661. **)
  7662. PROCEDURE Body(body: SyntaxTree.Body);
  7663. BEGIN
  7664. VisitStatementBlock(body);
  7665. IF body.isActive THEN
  7666. IF ~currentIsBodyProcedure THEN
  7667. Error(body.position,"active flag not in object body");
  7668. ELSIF body.priority # NIL THEN
  7669. body.SetPriority(ConstantInteger(body.priority));
  7670. END;
  7671. ELSIF body.isSafe THEN
  7672. Error(body.position,"safe flag not in active body");
  7673. ELSIF body.priority # NIL THEN
  7674. Error(body.position,"priority flag not in active body");
  7675. END;
  7676. IF body.code # NIL THEN
  7677. CheckSystemImport(body.position);
  7678. END;
  7679. StatementSequence(body.finally)
  7680. END Body;
  7681. (*** scopes ***)
  7682. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7683. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7684. VAR duplicateSymbol: BOOLEAN;
  7685. BEGIN
  7686. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7687. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7688. Error(symbol.position,"globally defined keyword")
  7689. END;
  7690. scope.EnterSymbol(symbol,duplicateSymbol);
  7691. IF ~allowDuplicate & duplicateSymbol THEN
  7692. Error(symbol.position,"Multiply defined identifier.");
  7693. IF VerboseErrorMessage THEN
  7694. Printout.Info("multiply defined identifier",symbol);
  7695. Printout.Info("in scope",scope);
  7696. END;
  7697. END;
  7698. END Register;
  7699. (**
  7700. implementation: check and resolve an implementation part
  7701. **)
  7702. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7703. move implementation checker to a separate object ? *)
  7704. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7705. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7706. BEGIN
  7707. prevIsRealtime := currentIsRealtime;
  7708. prevIsBodyProcedure := currentIsBodyProcedure;
  7709. prevIsCellNet := currentIsCellNet;
  7710. prevScope := currentScope;
  7711. currentScope := scope;
  7712. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7713. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7714. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7715. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7716. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7717. (*
  7718. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7719. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7720. END;
  7721. *)
  7722. END;
  7723. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7724. (* module body, record bodies are wrapped into an artifical procedure *)
  7725. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7726. Body(scope(SyntaxTree.ProcedureScope).body)
  7727. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7728. Body(scope(SyntaxTree.ProcedureScope).body)
  7729. END;
  7730. END;
  7731. currentScope := prevScope;
  7732. currentIsRealtime := prevIsRealtime;
  7733. currentIsBodyProcedure := prevIsBodyProcedure;
  7734. currentIsCellNet := prevIsCellNet;
  7735. END Implementation;
  7736. (** implementation phase:
  7737. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7738. **)
  7739. PROCEDURE Implementations(x: SyntaxTree.Module);
  7740. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7741. BEGIN
  7742. prevPhase := phase;
  7743. phase := InlinePhase;
  7744. scope := x.firstScope;
  7745. WHILE(scope # NIL) DO
  7746. Implementation(scope);
  7747. scope := scope.nextScope;
  7748. END;
  7749. phase := ImplementationPhase;
  7750. scope := x.firstScope;
  7751. WHILE(scope # NIL) DO
  7752. Implementation(scope);
  7753. scope := scope.nextScope;
  7754. END;
  7755. phase := prevPhase;
  7756. END Implementations;
  7757. (** declaration phase:
  7758. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7759. - import lists (for module scopes)
  7760. - parameter list (for procedure scopes)
  7761. - constant declarations
  7762. - type declarations
  7763. - variable declarations
  7764. - procedure declarations
  7765. preformed in two stages:
  7766. - first all symbols are entered into the symbol table (with uniqueness check),
  7767. - then all symbols are resolved
  7768. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7769. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7770. phases :
  7771. 0 = before procedures
  7772. 1 = procedures and later
  7773. **)
  7774. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7775. VAR
  7776. constant: SyntaxTree.Constant;
  7777. typeDeclaration: SyntaxTree.TypeDeclaration;
  7778. declaredType: SyntaxTree.Type;
  7779. variable: SyntaxTree.Variable;
  7780. procedure: SyntaxTree.Procedure;
  7781. procedureType : SyntaxTree.ProcedureType;
  7782. prevScope: SyntaxTree.Scope;
  7783. parameter: SyntaxTree.Parameter;
  7784. import: SyntaxTree.Import;
  7785. symbol: SyntaxTree.Symbol;
  7786. prevPhase: LONGINT;
  7787. prevError : BOOLEAN;
  7788. i: LONGINT;
  7789. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7790. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7791. BEGIN
  7792. IF type.baseType # NIL THEN
  7793. baseType := type.baseType.resolved;
  7794. IF baseType IS SyntaxTree.PointerType THEN
  7795. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7796. END;
  7797. (*
  7798. IF baseType IS SyntaxTree.CellType THEN
  7799. DeclareCell(baseType(SyntaxTree.CellType));
  7800. END;
  7801. *)
  7802. END;
  7803. parameter := type.firstParameter;
  7804. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7805. (*
  7806. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7807. variable.SetType(parameter.type);
  7808. variable.SetAccess(SyntaxTree.Hidden);
  7809. variable.SetModifiers(parameter.modifiers);
  7810. currentScope.PushVariable(variable);
  7811. *)
  7812. Register(parameter,scope, FALSE);
  7813. parameter := parameter.nextParameter;
  7814. END;
  7815. property := type.firstProperty;
  7816. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7817. (*
  7818. variable := currentScope.FindVariable(property.name);
  7819. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7820. prop := variable(SyntaxTree.Property);
  7821. ELSE (* add, duplicate symbols detection later *)
  7822. prop := SyntaxTree.NewProperty(property.position, property.name);
  7823. currentScope.PushVariable(prop);
  7824. END;
  7825. prop.SetType(property.type);
  7826. prop.SetValue(property.value);
  7827. prop.SetAccess(SyntaxTree.Hidden);
  7828. *)
  7829. Register(property, scope, FALSE);
  7830. property := property.nextProperty;
  7831. END;
  7832. END DeclareCell;
  7833. BEGIN
  7834. prevError := error;
  7835. prevPhase := phase;
  7836. phase := DeclarationPhase;
  7837. prevScope := currentScope;
  7838. currentScope := scope;
  7839. error := FALSE;
  7840. IF 0 IN phases THEN
  7841. (* first enter all symbols in scope *)
  7842. IF scope IS SyntaxTree.ModuleScope THEN
  7843. (* treat imports first for a module scope, , set default context if necessary *)
  7844. import := scope(SyntaxTree.ModuleScope).firstImport;
  7845. WHILE(import # NIL) DO
  7846. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7847. Register(import, currentScope, FALSE);
  7848. import := import.nextImport;
  7849. END;
  7850. import := scope(SyntaxTree.ModuleScope).firstImport;
  7851. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7852. ResolveSymbol(import);
  7853. import := import.nextImport;
  7854. END;
  7855. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7856. (* enter parameters for a procedure scope *)
  7857. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7858. parameter := procedureType.firstParameter;
  7859. WHILE(parameter # NIL) DO
  7860. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7861. END;
  7862. parameter := procedureType.returnParameter;
  7863. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7864. parameter := procedureType.selfParameter;
  7865. IF parameter # NIL THEN
  7866. Register(parameter, currentScope, FALSE);
  7867. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7868. END;
  7869. ELSIF scope IS SyntaxTree.CellScope THEN
  7870. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7871. IF~skipImplementation THEN
  7872. import := scope(SyntaxTree.CellScope).firstImport;
  7873. WHILE(import # NIL) DO
  7874. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7875. Register(import, currentScope, FALSE);
  7876. import := import.nextImport;
  7877. END;
  7878. import := scope(SyntaxTree.CellScope).firstImport;
  7879. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7880. ResolveSymbol(import);
  7881. import := import.nextImport;
  7882. END;
  7883. END;
  7884. END;
  7885. IF error THEN RETURN END;
  7886. IF skipImplementation THEN
  7887. scope.Clear;
  7888. END;
  7889. (* constants *)
  7890. constant := scope.firstConstant;
  7891. WHILE (constant # NIL) DO
  7892. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7893. END;
  7894. (* type declarations *)
  7895. typeDeclaration := scope.firstTypeDeclaration;
  7896. WHILE (typeDeclaration # NIL) DO
  7897. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7898. END;
  7899. (* variables *)
  7900. variable := scope.firstVariable;
  7901. WHILE (variable # NIL) DO
  7902. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7903. END;
  7904. (* procedures *)
  7905. IF scope.procedures # NIL THEN
  7906. FOR i := 0 TO scope.procedures.Length()-1 DO
  7907. procedure := scope.procedures.GetProcedure(i);
  7908. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7909. IF procedureType.selfParameter = NIL THEN
  7910. scope.AddProcedure(procedure);
  7911. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7912. ELSE
  7913. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7914. IF typeDeclaration = NIL THEN
  7915. Error(procedureType.selfParameter.position, "No such type declaration");
  7916. ELSE
  7917. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7918. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7919. declaredType := typeDeclaration.declaredType;
  7920. IF declaredType IS SyntaxTree.PointerType THEN
  7921. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7922. END;
  7923. IF declaredType IS SyntaxTree.RecordType THEN
  7924. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7925. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7926. ELSE
  7927. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7928. END;
  7929. END;
  7930. END;
  7931. END;
  7932. END;
  7933. END;
  7934. (* now process all symbols without any presumption on the order *)
  7935. symbol := scope.firstSymbol;
  7936. WHILE(symbol # NIL) DO
  7937. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7938. IF (symbol IS SyntaxTree.Procedure) THEN
  7939. IF 1 IN phases THEN
  7940. ResolveSymbol(symbol);
  7941. END;
  7942. ELSE
  7943. IF 0 IN phases THEN
  7944. ResolveSymbol(symbol);
  7945. END;
  7946. END;
  7947. END;
  7948. symbol := symbol.nextSymbol;
  7949. END;
  7950. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7951. symbol := scope.firstSymbol;
  7952. WHILE symbol # NIL DO
  7953. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7954. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7955. pointerFixes.Add(symbol, currentScope);
  7956. END;
  7957. IF ~symbol.type.resolved.isRealtime THEN
  7958. Error(symbol.position,"symbol has no realtime type");
  7959. END;
  7960. END;
  7961. symbol := symbol.nextSymbol
  7962. END;
  7963. END;
  7964. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7965. Error(Basic.invalidPosition,"problems during offset computation in module");
  7966. END;
  7967. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7968. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7969. scope.ownerModule.AddScope(scope);
  7970. END;
  7971. phase := prevPhase;
  7972. currentScope := prevScope;
  7973. error := error OR prevError;
  7974. END Declarations;
  7975. (* nopov *)
  7976. (** check if all operators from one module are compatible to the ones in the other module
  7977. - check if there are not multiple operators with the same signature
  7978. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7979. - check for all operators whose signatures are compatible, whether the return types are compatible
  7980. note that:
  7981. - the return type is not considered to be part of the signature
  7982. - two signatures are considered compatible, if all of the operands are compatible
  7983. **)
  7984. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7985. VAR
  7986. thisOperator, thatOperator: SyntaxTree.Operator;
  7987. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7988. thisParameter, thatParameter: SyntaxTree.Parameter;
  7989. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7990. i: LONGINT;
  7991. BEGIN
  7992. currentScope := thisModuleScope;
  7993. hasError := FALSE;
  7994. (* go through all operators in the other module *)
  7995. thatOperator := thatModuleScope.firstOperator;
  7996. WHILE (thatOperator # NIL) & ~hasError DO
  7997. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7998. (* the other operator is accessible *)
  7999. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8000. (* the other operator is not the conversion operator *)
  8001. (* go through all operators in this module *)
  8002. thisOperator := thisModuleScope.firstOperator;
  8003. WHILE (thisOperator # NIL) & ~hasError DO
  8004. IF thisOperator # thatOperator THEN
  8005. (* the operators are not the same *)
  8006. IF thisOperator.name = thatOperator.name THEN
  8007. (* the operators share the same identifier *)
  8008. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8009. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8010. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8011. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8012. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8013. (* both operators have the same paramter count *)
  8014. thisParameter := thisProcedureType.firstParameter;
  8015. thatParameter := thatProcedureType.firstParameter;
  8016. operandsAreEqual := TRUE;
  8017. operandsAreCompatible := TRUE;
  8018. (* go through all parameters *)
  8019. FOR i := 1 TO thisProcedureType.numberParameters DO
  8020. ASSERT(thatParameter # NIL);
  8021. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8022. operandsAreEqual := FALSE;
  8023. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8024. operandsAreCompatible := FALSE
  8025. END
  8026. END;
  8027. thisParameter := thisParameter.nextParameter;
  8028. thatParameter := thatParameter.nextParameter
  8029. END;
  8030. IF operandsAreEqual THEN
  8031. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8032. hasError := TRUE
  8033. ELSIF operandsAreCompatible THEN
  8034. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8035. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8036. hasError := TRUE
  8037. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8038. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8039. hasError := TRUE
  8040. END
  8041. END
  8042. END
  8043. END
  8044. END;
  8045. thisOperator := thisOperator.nextOperator
  8046. END
  8047. END
  8048. END;
  8049. thatOperator := thatOperator.nextOperator
  8050. END
  8051. END CheckInterOperatorConformity;
  8052. (** check module:
  8053. - check module declaration
  8054. - add context, if necessary
  8055. - remove module from import cache, if necessary
  8056. - check declarations
  8057. - resolve all type fixes
  8058. - check implementation (bodies)
  8059. **)
  8060. PROCEDURE Module*(x: SyntaxTree.Module);
  8061. VAR (* nopov *)
  8062. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8063. BEGIN
  8064. prevScope := currentScope;
  8065. prevIsCellNet := currentIsCellNet;
  8066. module := x;
  8067. ASSERT(x # NIL);
  8068. global := system.globalScope[x.case];
  8069. x.moduleScope.SetGlobalScope(global);
  8070. currentScope := global;
  8071. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8072. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8073. RemoveModuleFromCache(importCache,x);
  8074. Declarations(x.moduleScope, FALSE, {0,1});
  8075. FixTypes();
  8076. IF module.isCellNet THEN
  8077. currentIsCellNet := TRUE;
  8078. modifier := x.modifiers;
  8079. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8080. CheckModifiers(modifier, FALSE);
  8081. END;
  8082. (* nopov *)
  8083. IF ~error THEN
  8084. (* check if operators conform to each other within this module *)
  8085. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8086. (* go through all imports *)
  8087. import := x.moduleScope.firstImport;
  8088. WHILE import # NIL DO
  8089. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8090. (* check if all operators in this module conform to the ones of the imported module *)
  8091. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8092. END;
  8093. import := import.nextImport
  8094. END;
  8095. END;
  8096. Implementations(x);
  8097. module := NIL;
  8098. currentIsCellNet := prevIsCellNet;
  8099. currentScope := prevScope;
  8100. END Module;
  8101. END Checker;
  8102. Warnings*=OBJECT (SyntaxTree.Visitor)
  8103. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8104. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8105. BEGIN
  8106. SELF.diagnostics := diagnostics
  8107. END InitWarnings;
  8108. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8109. BEGIN END VisitPortType;
  8110. (** types *)
  8111. PROCEDURE Type(x: SyntaxTree.Type);
  8112. BEGIN x.Accept(SELF)
  8113. END Type;
  8114. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8115. BEGIN END VisitType;
  8116. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8117. BEGIN END VisitBasicType;
  8118. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8119. BEGIN END VisitCharacterType;
  8120. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8121. BEGIN END VisitIntegerType;
  8122. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8123. BEGIN END VisitFloatType;
  8124. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8125. BEGIN END VisitQualifiedType;
  8126. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8127. BEGIN END VisitStringType;
  8128. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8129. BEGIN END VisitEnumerationType;
  8130. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8131. BEGIN END VisitRangeType;
  8132. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8133. BEGIN
  8134. IF ~(SyntaxTree.Warned IN x.state) THEN
  8135. x.SetState(SyntaxTree.Warned);
  8136. Type(x.arrayBase);
  8137. END;
  8138. END VisitArrayType;
  8139. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8140. BEGIN
  8141. IF ~(SyntaxTree.Warned IN x.state) THEN
  8142. x.SetState(SyntaxTree.Warned);
  8143. Type(x.arrayBase);
  8144. END;
  8145. END VisitMathArrayType;
  8146. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8147. BEGIN
  8148. IF ~(SyntaxTree.Warned IN x.state) THEN
  8149. x.SetState(SyntaxTree.Warned);
  8150. Type(x.pointerBase);
  8151. END;
  8152. END VisitPointerType;
  8153. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8154. BEGIN Scope(x.recordScope) END VisitRecordType;
  8155. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8156. BEGIN Scope(x.cellScope) END VisitCellType;
  8157. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8158. BEGIN END VisitProcedureType;
  8159. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8160. VAR msg: ARRAY 256 OF CHAR;
  8161. BEGIN
  8162. Global.GetSymbolName(x,msg);
  8163. Strings.Append(msg," ");
  8164. Strings.Append(msg,text);
  8165. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8166. END Warning;
  8167. (** symbols *)
  8168. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8169. BEGIN
  8170. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8171. IF ~(x IS SyntaxTree.Parameter) THEN
  8172. Warning(x,"never used");
  8173. END;
  8174. END;
  8175. x.Accept(SELF);
  8176. END Symbol;
  8177. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8178. BEGIN END VisitSymbol;
  8179. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8180. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8181. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8182. BEGIN END VisitConstant;
  8183. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8184. BEGIN END VisitVariable;
  8185. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8186. BEGIN END VisitProperty;
  8187. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8188. BEGIN END VisitParameter;
  8189. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8190. BEGIN
  8191. Scope(x.procedureScope)
  8192. END VisitProcedure;
  8193. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8194. BEGIN END VisitOperator;
  8195. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8196. BEGIN END VisitImport;
  8197. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8198. VAR
  8199. symbol: SyntaxTree.Symbol;
  8200. BEGIN
  8201. symbol := scope.firstSymbol;
  8202. WHILE(symbol # NIL) DO
  8203. Symbol(symbol);
  8204. symbol := symbol.nextSymbol;
  8205. END;
  8206. END Scope;
  8207. PROCEDURE Module*(x: SyntaxTree.Module);
  8208. BEGIN
  8209. SELF.module := x;
  8210. Scope(x.moduleScope);
  8211. END Module;
  8212. END Warnings;
  8213. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8214. BEGIN
  8215. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8216. END IsOberonInline;
  8217. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8218. BEGIN
  8219. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8220. END Resolved;
  8221. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8222. VAR i: LONGINT;
  8223. BEGIN
  8224. i := 1;
  8225. WHILE i < x DO
  8226. i := i *2
  8227. END;
  8228. RETURN i=x
  8229. END PowerOf2;
  8230. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8231. BEGIN
  8232. RETURN
  8233. (scope # NIL) &
  8234. (scope IS SyntaxTree.ModuleScope)
  8235. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8236. OR
  8237. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8238. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8239. END IsCellNetScope;
  8240. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8241. BEGIN
  8242. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8243. END IsCellScope;
  8244. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8245. BEGIN
  8246. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8247. RETURN (scope # NIL) & IsCellNetScope(scope)
  8248. END InCellNetScope;
  8249. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8250. BEGIN
  8251. ASSERT(size MOD system.dataUnit = 0);
  8252. RETURN size DIV system.dataUnit
  8253. END ToMemoryUnits;
  8254. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8255. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8256. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8257. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8258. BEGIN
  8259. IF t = NIL THEN
  8260. RETURN TRUE
  8261. ELSE
  8262. t := t.resolved;
  8263. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8264. END;
  8265. END TypeAllowed;
  8266. BEGIN
  8267. type := type.resolved;
  8268. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8269. RETURN FALSE
  8270. ELSE
  8271. procedureType := type(SyntaxTree.ProcedureType);
  8272. numberParameters := procedureType.numberParameters;
  8273. RETURN
  8274. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8275. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8276. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8277. END;
  8278. END GetProcedureAllowed;
  8279. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8280. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8281. VAR import: SyntaxTree.Import;
  8282. BEGIN
  8283. import := importCache.ImportByModuleName(x.name,x.context);
  8284. IF import # NIL THEN
  8285. importCache.RemoveImporters(x.name,x.context);
  8286. END;
  8287. END RemoveModuleFromCache;
  8288. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8289. (* to <- this assignment compatibility *)
  8290. VAR result: BOOLEAN;
  8291. BEGIN
  8292. IF this= NIL THEN result := (to=NIL)
  8293. ELSIF to=NIL THEN result := FALSE
  8294. ELSE
  8295. (*! will be replaced by this:
  8296. ELSE result := this.CompatibleTo(to.resolved);
  8297. *)
  8298. this := this.resolved; to := to.resolved;
  8299. IF to=SyntaxTree.invalidType THEN result := FALSE
  8300. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8301. ELSIF to = this THEN
  8302. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8303. ELSIF to IS SyntaxTree.BasicType THEN
  8304. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8305. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8306. result := this.CompatibleTo(to.resolved)
  8307. ELSE
  8308. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8309. END
  8310. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8311. result := to.sizeInBits = this.sizeInBits;
  8312. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8313. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8314. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8315. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8316. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8317. result := TRUE;
  8318. ELSIF to IS SyntaxTree.AnyType THEN
  8319. 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);
  8320. ELSIF to IS SyntaxTree.ObjectType THEN
  8321. 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 *) ;
  8322. ELSIF to IS SyntaxTree.ByteType THEN
  8323. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8324. ELSIF to IS SyntaxTree.CharacterType THEN
  8325. result := IsCharacterType(this)
  8326. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8327. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8328. 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
  8329. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8330. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8331. result := TRUE;
  8332. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8333. result := TRUE;
  8334. ELSE
  8335. result := FALSE
  8336. END;
  8337. ELSIF to IS SyntaxTree.PointerType THEN
  8338. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8339. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8340. & (~to.isRealtime OR this.isRealtime);
  8341. ELSIF to IS SyntaxTree.ProcedureType THEN
  8342. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8343. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8344. & (~to.isRealtime OR this.isRealtime)
  8345. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8346. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8347. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8348. ELSIF to IS SyntaxTree.RecordType THEN
  8349. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8350. ELSIF to IS SyntaxTree.ArrayType THEN
  8351. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8352. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8353. ELSIF StaticArrayCompatible(to, this) THEN
  8354. result := TRUE
  8355. ELSE
  8356. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8357. END;
  8358. ELSIF to IS SyntaxTree.MathArrayType THEN
  8359. IF this IS SyntaxTree.MathArrayType THEN
  8360. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8361. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8362. result := TRUE;
  8363. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8364. result := TRUE;
  8365. ELSE
  8366. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8367. END;
  8368. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8369. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8370. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8371. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8372. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8373. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8374. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8375. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8376. ELSE
  8377. result := FALSE
  8378. END;
  8379. (* an array-structured object type is compatible to the type of its array structure *)
  8380. ELSIF IsArrayStructuredObjectType(this) THEN
  8381. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8382. ELSE
  8383. result := FALSE;
  8384. END;
  8385. ELSIF to IS SyntaxTree.StringType THEN
  8386. result := FALSE;
  8387. ELSIF to IS SyntaxTree.EnumerationType THEN
  8388. result := IsEnumerationExtension(this,to);
  8389. ELSIF to IS SyntaxTree.PortType THEN
  8390. result := SameType(to, this)
  8391. ELSE
  8392. Printout.Info("CompatibleTo",to);
  8393. HALT(100); (* implement missing type check *)
  8394. END;
  8395. END;
  8396. RETURN result
  8397. END CompatibleTo;
  8398. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8399. VAR actualBase, formalBase: SyntaxTree.Type;
  8400. BEGIN
  8401. IF SameType(formal,actual) THEN
  8402. RETURN TRUE
  8403. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8404. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8405. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8406. RETURN
  8407. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8408. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8409. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8410. & StaticArrayCompatible(formalBase,actualBase)
  8411. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8412. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8413. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8414. RETURN
  8415. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8416. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8417. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8418. & StaticArrayCompatible(formalBase,actualBase)
  8419. ELSE RETURN FALSE
  8420. END;
  8421. END StaticArrayCompatible;
  8422. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8423. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8424. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8425. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8426. BEGIN
  8427. result := SameType(formal,actual);
  8428. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8429. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8430. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8431. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8432. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8433. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8434. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8435. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8436. & TC(formalBase, actualBase);
  8437. END;
  8438. RETURN result
  8439. END TC;
  8440. BEGIN
  8441. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8442. ELSE
  8443. arrayBase := formalType.arrayBase.resolved;
  8444. IF (actualType IS SyntaxTree.StringType) THEN
  8445. result := arrayBase IS SyntaxTree.CharacterType
  8446. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8447. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8448. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8449. result := TC(formalType, actualType);
  8450. ELSE
  8451. result := (arrayBase IS SyntaxTree.ByteType)
  8452. END;
  8453. END;
  8454. RETURN result
  8455. END OpenArrayCompatible;
  8456. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8457. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8458. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8459. BEGIN
  8460. IF actualType IS SyntaxTree.MathArrayType THEN
  8461. actualArray := actualType(SyntaxTree.MathArrayType);
  8462. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8463. (*
  8464. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8465. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8466. *)
  8467. actualBase := ArrayBase(actualType,Infinity);
  8468. formalBase := ArrayBase(formalType,Infinity);
  8469. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8470. ELSE
  8471. (*
  8472. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8473. *)
  8474. formalBase := Resolved(formalType.arrayBase);
  8475. actualBase := Resolved(actualArray.arrayBase);
  8476. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8477. (*
  8478. ARRAY [k] -> ARRAY [n]
  8479. *)
  8480. result := (formalType.staticLength = actualArray.staticLength)
  8481. ELSE
  8482. result := TRUE
  8483. END;
  8484. IF ~result THEN
  8485. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8486. ELSIF actualBase = NIL THEN result := FALSE
  8487. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8488. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8489. ELSE
  8490. result := SameType(formalBase,actualBase)
  8491. END;
  8492. END;
  8493. ELSE
  8494. result := FALSE
  8495. END;
  8496. RETURN result
  8497. END MathArrayCompatible;
  8498. (**
  8499. Math Array Type distance for assignments / parameter passings of the form
  8500. from -> to
  8501. variants:
  8502. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8503. allowed:
  8504. static -> static (& size match)
  8505. static -> open
  8506. static -> tensor
  8507. open -> open
  8508. open -> tensor
  8509. open -> static
  8510. tensor -> tensor
  8511. tensor -> open
  8512. tensor -> static
  8513. **)
  8514. (*! think about the metric here: is form matching more important than element type matching? *)
  8515. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8516. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8517. BEGIN
  8518. fromBase := Resolved(from.arrayBase);
  8519. toBase := Resolved(to.arrayBase);
  8520. i := Infinity;
  8521. IF from = to THEN
  8522. i := 0;
  8523. ELSIF (from.form = to.form) THEN
  8524. (* static -> static, open -> open, tensor -> tensor *)
  8525. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8526. IF fromBase = toBase THEN i := 0
  8527. ELSIF toBase = NIL THEN i := 1
  8528. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8529. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8530. ELSE
  8531. i := TypeDistance(system,fromBase, toBase, varpar);
  8532. END;
  8533. END;
  8534. ELSIF (to.form = SyntaxTree.Static) THEN
  8535. (* forbidden *)
  8536. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8537. (* static -> tensor, open -> tensor, tensor -> open *)
  8538. IF toBase=fromBase THEN i := 0;
  8539. ELSIF toBase = NIL THEN i := 1;
  8540. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8541. toBase := ArrayBase(toBase,Infinity);
  8542. IF (fromBase=toBase) THEN i := 0
  8543. ELSIF (toBase = NIL) THEN i:= 1
  8544. ELSIF (fromBase = NIL) THEN i := Infinity;
  8545. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8546. END;
  8547. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8548. fromBase := ArrayBase(fromBase,Infinity);
  8549. IF (fromBase=toBase) THEN i := 0
  8550. ELSIF (toBase = NIL) THEN i := 1
  8551. ELSIF (fromBase = NIL) THEN i := Infinity;
  8552. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8553. END;
  8554. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8555. END;
  8556. IF i # Infinity THEN INC(i,2) END;
  8557. ELSIF (from.form = SyntaxTree.Static) THEN
  8558. (* static -> open *)
  8559. IF toBase=fromBase THEN i := 0
  8560. ELSIF toBase = NIL THEN i := 1
  8561. ELSIF fromBase = NIL THEN i := Infinity
  8562. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8563. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8564. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8565. END;
  8566. IF i # Infinity THEN INC(i,1) END;
  8567. ELSE HALT(100); (* unknown case *)
  8568. END;
  8569. RETURN i;
  8570. END MathArrayTypeDistance;
  8571. (** compute and return the distance of two array types
  8572. - return the distance of the base types
  8573. **)
  8574. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8575. VAR i: LONGINT;
  8576. BEGIN
  8577. i := Infinity;
  8578. IF from = to THEN
  8579. i := 0
  8580. ELSE
  8581. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8582. (*
  8583. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8584. i := TypeDistance(from.base, to.base);
  8585. IF i >= 0 THEN INC(i) END
  8586. ELSIF (from.mode = open) & (to.mode = open) THEN
  8587. i := TypeDistance(from.base, to.base);
  8588. *)
  8589. END;
  8590. RETURN i
  8591. END ArrayTypeDistance;
  8592. (** compute the signature distance of a procedure and an actual parameter list
  8593. - if any of the parameters are not compatible, the result is infinite
  8594. - add up and return the distance over all parameters
  8595. **)
  8596. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8597. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8598. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8599. BEGIN
  8600. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8601. result := Infinity
  8602. ELSE
  8603. formalParameter := procedureType.firstParameter;
  8604. i := 0;
  8605. result := 0;
  8606. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8607. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8608. actualParameter := actualParameters.GetExpression(i);
  8609. ASSERT(formalParameter.type # NIL);
  8610. IF (actualParameter.type = NIL) THEN distance := Infinity
  8611. ELSE
  8612. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8613. END;
  8614. IF distance = Infinity THEN
  8615. result := Infinity;
  8616. ELSE
  8617. to := formalParameter.type.resolved;
  8618. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8619. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8620. (* already handled varpar *)
  8621. (*
  8622. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8623. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8624. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8625. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8626. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8627. END;
  8628. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8629. result := Infinity
  8630. END;
  8631. *)
  8632. INC(result, distance);
  8633. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8634. INC(result, distance);
  8635. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8636. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8637. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8638. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8639. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8640. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8641. END;
  8642. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8643. result := Infinity
  8644. END;
  8645. ELSE
  8646. result := Infinity
  8647. END;
  8648. ELSE
  8649. INC(result,distance);
  8650. END;
  8651. END;
  8652. (*
  8653. Printout.Info("actual=", actualParameter);
  8654. Printout.Info("formal=", formalParameter);
  8655. TRACE(result);
  8656. *)
  8657. formalParameter := formalParameter.nextParameter; INC(i);
  8658. END;
  8659. END;
  8660. ASSERT(result >= 0);
  8661. RETURN result
  8662. END Distance;
  8663. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8664. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8665. BEGIN
  8666. IF right.numberParameters # (procedureType.numberParameters) THEN
  8667. result := Infinity
  8668. ELSE
  8669. formalParameter := procedureType.firstParameter;
  8670. rightParameter := right.firstParameter;
  8671. i := 0;
  8672. result := 0;
  8673. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8674. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8675. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8676. IF distance = Infinity THEN
  8677. result := Infinity;
  8678. ELSE
  8679. INC(result,distance);
  8680. END;
  8681. formalParameter := formalParameter.nextParameter;
  8682. rightParameter := rightParameter.nextParameter;
  8683. END;
  8684. END;
  8685. ASSERT(result >= 0);
  8686. RETURN result
  8687. END ProcedureTypeDistance;
  8688. (** compute and return the distance between two types, used for computation of signature distance
  8689. from -> to
  8690. **)
  8691. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8692. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8693. BEGIN
  8694. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8695. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8696. END;
  8697. i := Infinity;
  8698. IF from = to THEN
  8699. i := 0
  8700. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8701. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8702. i := Infinity;
  8703. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8704. i := 10;
  8705. ELSIF (from IS SyntaxTree.StringType) THEN
  8706. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8707. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8708. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8709. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8710. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8711. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8712. i := 1
  8713. ELSIF (from IS SyntaxTree.NilType) THEN
  8714. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8715. (*
  8716. ELSIF (from = NoType) THEN
  8717. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8718. *)
  8719. ELSIF (from IS SyntaxTree.BasicType) THEN
  8720. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8721. IF varpar & (i # 0) THEN i := Infinity END;
  8722. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8723. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8724. ELSIF (from IS SyntaxTree.RecordType) THEN
  8725. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8726. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8727. IF to IS SyntaxTree.MathArrayType THEN
  8728. (*
  8729. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8730. i := Infinity;
  8731. ELSE
  8732. *)
  8733. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8734. (*
  8735. END;
  8736. *)
  8737. END
  8738. ELSIF (from IS SyntaxTree.PointerType) THEN
  8739. ptr := from(SyntaxTree.PointerType);
  8740. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8741. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8742. (* ELSE i := TypeDistance(ptr.base, to); *)
  8743. END
  8744. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8745. IF (to IS SyntaxTree.ProcedureType) THEN
  8746. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8747. END;
  8748. ELSIF (from IS SyntaxTree.PortType) THEN
  8749. IF (to IS SyntaxTree.PortType) THEN
  8750. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8751. i := 0;
  8752. END;
  8753. END;
  8754. (*no procedure test, procedure must be the same*)
  8755. END;
  8756. RETURN i
  8757. END TypeDistance;
  8758. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8759. BEGIN
  8760. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8761. END IsIntegerType;
  8762. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8763. BEGIN
  8764. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8765. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8766. OR IsPointerType(type)
  8767. )
  8768. END IsAddressType;
  8769. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8770. BEGIN
  8771. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8772. END IsSizeType;
  8773. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8774. BEGIN
  8775. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8776. END IsSignedIntegerType;
  8777. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8778. BEGIN
  8779. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8780. END IsUnsignedIntegerType;
  8781. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8782. VAR result: BOOLEAN;
  8783. BEGIN
  8784. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8785. value := x.resolved(SyntaxTree.IntegerValue).value;
  8786. result := TRUE
  8787. ELSE
  8788. result := FALSE
  8789. END;
  8790. RETURN result
  8791. END IsIntegerValue;
  8792. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8793. VAR result: BOOLEAN;
  8794. BEGIN
  8795. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8796. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8797. result := TRUE
  8798. ELSE
  8799. result := FALSE
  8800. END;
  8801. RETURN result
  8802. END IsEnumerationValue;
  8803. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8804. VAR result: BOOLEAN;
  8805. BEGIN
  8806. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8807. value := x.resolved(SyntaxTree.RealValue).value;
  8808. result := TRUE
  8809. ELSE
  8810. result := FALSE
  8811. END;
  8812. RETURN result
  8813. END IsRealValue;
  8814. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8815. VAR result: BOOLEAN;
  8816. BEGIN
  8817. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8818. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8819. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8820. result := TRUE
  8821. ELSE
  8822. result := FALSE
  8823. END;
  8824. RETURN result
  8825. END IsComplexValue;
  8826. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8827. VAR result: BOOLEAN;
  8828. BEGIN
  8829. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8830. value := x.resolved(SyntaxTree.CharacterValue).value;
  8831. result := TRUE
  8832. ELSE
  8833. result := FALSE
  8834. END;
  8835. RETURN result
  8836. END IsCharacterValue;
  8837. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8838. VAR result: BOOLEAN;
  8839. BEGIN
  8840. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8841. value := x.resolved(SyntaxTree.BooleanValue).value;
  8842. result := TRUE
  8843. ELSE
  8844. result := FALSE
  8845. END;
  8846. RETURN result
  8847. END IsBooleanValue;
  8848. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8849. VAR result: BOOLEAN;
  8850. BEGIN
  8851. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8852. value := x.resolved(SyntaxTree.SetValue).value;
  8853. result := TRUE
  8854. ELSE
  8855. result := FALSE
  8856. END;
  8857. RETURN result
  8858. END IsSetValue;
  8859. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8860. VAR result: BOOLEAN;
  8861. BEGIN
  8862. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8863. value := x.resolved(SyntaxTree.StringValue).value;
  8864. result := TRUE
  8865. ELSE
  8866. result := FALSE
  8867. END;
  8868. RETURN result
  8869. END IsStringValue;
  8870. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8871. BEGIN
  8872. x := x.resolved;
  8873. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8874. END Indexable;
  8875. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8876. BEGIN
  8877. RETURN t1.SameType(t2.resolved);
  8878. END SameType;
  8879. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8880. BEGIN
  8881. IF t IS SyntaxTree.MathArrayType THEN
  8882. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8883. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8884. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8885. DEC(max);
  8886. END;
  8887. ELSIF t IS SyntaxTree.ArrayType THEN
  8888. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8889. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8890. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8891. END;
  8892. END;
  8893. RETURN t;
  8894. END ArrayBase;
  8895. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8896. BEGIN
  8897. type := type.resolved;
  8898. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8899. base := type(SyntaxTree.ArrayType).arrayBase;
  8900. RETURN TRUE;
  8901. END;
  8902. RETURN FALSE;
  8903. END IsOpenArray;
  8904. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8905. BEGIN
  8906. type := type.resolved;
  8907. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8908. base := type(SyntaxTree.ArrayType).arrayBase;
  8909. dim := type(SyntaxTree.ArrayType).staticLength;
  8910. RETURN TRUE
  8911. ELSE
  8912. RETURN FALSE
  8913. END;
  8914. END IsStaticArray;
  8915. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8916. BEGIN
  8917. type := type.resolved;
  8918. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8919. base := type(SyntaxTree.ArrayType).arrayBase;
  8920. RETURN TRUE
  8921. ELSE
  8922. RETURN FALSE
  8923. END;
  8924. END IsDynamicArray;
  8925. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8926. VAR i: LONGINT;
  8927. BEGIN
  8928. i := 0;
  8929. t := t.resolved;
  8930. IF t IS SyntaxTree.MathArrayType THEN
  8931. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8932. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8933. END;
  8934. ELSIF t IS SyntaxTree.ArrayType THEN
  8935. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8936. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8937. END;
  8938. END;
  8939. RETURN i
  8940. END Dimension;
  8941. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8942. BEGIN
  8943. RETURN expression.assignable;
  8944. END IsVariable;
  8945. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8946. BEGIN
  8947. IF (symbol IS SyntaxTree.Parameter) THEN
  8948. WITH symbol: SyntaxTree.Parameter DO
  8949. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8950. END;
  8951. ELSE
  8952. RETURN FALSE
  8953. END;
  8954. END IsVariableParameter;
  8955. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8956. VAR result: BOOLEAN;
  8957. BEGIN
  8958. IF type = NIL THEN result := FALSE
  8959. ELSE
  8960. type := type.resolved;
  8961. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8962. END;
  8963. RETURN result
  8964. END IsPointerType;
  8965. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8966. VAR result: BOOLEAN;
  8967. BEGIN
  8968. IF type = NIL THEN result := FALSE
  8969. ELSE
  8970. type := type.resolved;
  8971. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8972. END;
  8973. RETURN result
  8974. END IsUnsafePointer;
  8975. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8976. BEGIN
  8977. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8978. END IsDisposable;
  8979. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8980. VAR result: BOOLEAN;
  8981. BEGIN
  8982. IF type = NIL THEN result := FALSE
  8983. ELSE
  8984. type := type.resolved;
  8985. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8986. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8987. result := result OR (type IS SyntaxTree.ObjectType);
  8988. END;
  8989. RETURN result
  8990. END IsPointerToRecord;
  8991. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8992. VAR result: BOOLEAN;
  8993. BEGIN
  8994. IF type = NIL THEN result := FALSE
  8995. ELSE
  8996. type := type.resolved;
  8997. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8998. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8999. ;
  9000. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9001. result := result OR (type IS SyntaxTree.ObjectType);
  9002. END;
  9003. RETURN result
  9004. END IsPointerToObject;
  9005. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9006. BEGIN
  9007. IF type # NIL THEN
  9008. RETURN type.resolved.hasPointers
  9009. ELSE
  9010. RETURN FALSE
  9011. END;
  9012. END ContainsPointer;
  9013. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9014. BEGIN
  9015. IF type = NIL THEN RETURN FALSE END;
  9016. type := type.resolved;
  9017. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9018. END IsStringType;
  9019. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9020. BEGIN
  9021. IF type = NIL THEN RETURN FALSE END;
  9022. type := type.resolved;
  9023. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9024. END IsCharacterType;
  9025. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9026. BEGIN
  9027. IF type = NIL THEN RETURN FALSE END;
  9028. type := type.resolved;
  9029. RETURN (type IS SyntaxTree.EnumerationType)
  9030. END IsEnumerationType;
  9031. (** cf. section "Type extension (base type)" in the language report **)
  9032. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9033. VAR result: BOOLEAN;
  9034. BEGIN
  9035. ASSERT(base # NIL); ASSERT(extension # NIL);
  9036. base := base.resolved; extension := extension.resolved;
  9037. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9038. result := TRUE;
  9039. ELSE
  9040. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9041. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9042. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9043. END;
  9044. WHILE (extension # NIL) & (extension # base) DO
  9045. IF extension IS SyntaxTree.RecordType THEN
  9046. extension := extension(SyntaxTree.RecordType).baseType;
  9047. IF (extension # NIL) THEN extension := extension.resolved END;
  9048. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9049. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9050. END;
  9051. ELSE extension := NIL;
  9052. END;
  9053. END;
  9054. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9055. END;
  9056. RETURN result
  9057. END IsTypeExtension;
  9058. (** check if base is the base enumeration type of extension **)
  9059. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9060. BEGIN
  9061. base := base.resolved; extension := extension.resolved;
  9062. WHILE (extension # NIL) & (extension # base) DO
  9063. IF extension IS SyntaxTree.EnumerationType THEN
  9064. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9065. IF extension # NIL THEN extension := extension.resolved END;
  9066. ELSE
  9067. extension := NIL
  9068. END;
  9069. END;
  9070. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9071. END IsEnumerationExtension;
  9072. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9073. BEGIN
  9074. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9075. RETURN TRUE
  9076. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9077. RETURN TRUE
  9078. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9079. RETURN TRUE
  9080. ELSE
  9081. RETURN FALSE
  9082. END
  9083. END IsCallable;
  9084. (** compute and return the distance of two record types
  9085. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9086. **)
  9087. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9088. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9089. BEGIN
  9090. i := 0;
  9091. WHILE (from # NIL) & (from # to) DO
  9092. baseType := from.baseType;
  9093. IF (baseType # NIL) THEN
  9094. baseType := baseType.resolved;
  9095. IF baseType IS SyntaxTree.PointerType THEN
  9096. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9097. END;
  9098. IF baseType IS SyntaxTree.RecordType THEN
  9099. from := baseType(SyntaxTree.RecordType);
  9100. ELSE
  9101. from := NIL;
  9102. END;
  9103. ELSE
  9104. from := NIL
  9105. END;
  9106. INC(i)
  9107. END;
  9108. IF from = NIL THEN i := Infinity END;
  9109. RETURN i
  9110. END RecordTypeDistance;
  9111. (** compute and return the distance of two pointer types **)
  9112. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9113. BEGIN
  9114. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9115. RETURN Infinity;
  9116. ELSE
  9117. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9118. END;
  9119. END PointerTypeDistance;
  9120. (** check if expression contains a symbol designator pointing to a type declaration.
  9121. - if so then enter type declaration into typeDeclaration and return true else return false
  9122. **)
  9123. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9124. VAR result: BOOLEAN;
  9125. BEGIN
  9126. result := FALSE;
  9127. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9128. result := TRUE;
  9129. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9130. END;
  9131. RETURN result
  9132. END IsTypeDesignator;
  9133. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9134. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9135. VAR result: BOOLEAN;
  9136. BEGIN
  9137. type := type.resolved;
  9138. IF type IS SyntaxTree.PointerType THEN
  9139. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9140. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9141. result := TRUE
  9142. ELSE
  9143. result := type IS SyntaxTree.RecordType
  9144. END;
  9145. RETURN result
  9146. END IsExtensibleType;
  9147. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9148. BEGIN
  9149. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9150. (d IS SyntaxTree.SymbolDesignator) &
  9151. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9152. OR
  9153. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9154. END IsUnextensibleRecord;
  9155. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9156. BEGIN
  9157. IF IsUnextensibleRecord(d) THEN
  9158. RETURN FALSE
  9159. ELSE RETURN IsExtensibleType(d.type.resolved)
  9160. END;
  9161. END IsExtensibleDesignator;
  9162. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9163. BEGIN
  9164. type := type.resolved;
  9165. IF (type IS SyntaxTree.PointerType) THEN
  9166. RETURN TRUE
  9167. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9168. RETURN TRUE
  9169. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9170. RETURN TRUE
  9171. ELSIF (type IS SyntaxTree.BasicType) THEN
  9172. RETURN TRUE
  9173. END;
  9174. RETURN FALSE
  9175. END IsBasicType;
  9176. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9177. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9178. BEGIN
  9179. baseType := record.baseType;
  9180. IF (baseType # NIL) THEN
  9181. baseType := baseType.resolved;
  9182. IF (baseType IS SyntaxTree.PointerType) THEN
  9183. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9184. END;
  9185. END;
  9186. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9187. recordType := baseType(SyntaxTree.RecordType);
  9188. ELSE
  9189. recordType := NIL;
  9190. END;
  9191. RETURN recordType
  9192. END RecordBase;
  9193. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9194. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9195. BEGIN
  9196. baseRecord := RecordBase(scope.ownerRecord);
  9197. IF baseRecord = NIL THEN RETURN NIL END;
  9198. scope := baseRecord.recordScope;
  9199. procedureType := procedure.type.resolved;
  9200. IF procedure IS SyntaxTree.Operator THEN
  9201. operator := scope.firstOperator;
  9202. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9203. (*
  9204. Printout.Info("not same ",procedureType);
  9205. Printout.Info("with ",operator.type);
  9206. *)
  9207. operator := operator.nextOperator;
  9208. END;
  9209. super := operator;
  9210. ELSE
  9211. super := scope.firstProcedure;
  9212. WHILE (super # NIL) & (super.name # procedure.name) DO
  9213. super := super.nextProcedure;
  9214. END;
  9215. END;
  9216. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9217. RETURN super
  9218. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9219. RETURN super
  9220. ELSE
  9221. RETURN FindSuperProcedure(scope,procedure);
  9222. END;
  9223. END FindSuperProcedure;
  9224. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9225. VAR procedure: SyntaxTree.Procedure;
  9226. BEGIN
  9227. procedure := record.recordScope.constructor;
  9228. IF procedure = NIL THEN
  9229. record := RecordBase(record);
  9230. IF record # NIL THEN
  9231. procedure := GetConstructor(record)
  9232. END;
  9233. END;
  9234. RETURN procedure;
  9235. END GetConstructor;
  9236. (* enter a case into a list of cases in a sorted way and check for collision *)
  9237. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9238. VAR prev,this,new: SyntaxTree.CaseConstant;
  9239. BEGIN
  9240. this := root;
  9241. prev := NIL;
  9242. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9243. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9244. RETURN FALSE
  9245. ELSE
  9246. IF (this # NIL) & (this.min = max+1) THEN
  9247. this.min := min
  9248. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9249. prev.max := min
  9250. ELSE
  9251. NEW(new); new.min := min; new.max := max;
  9252. new.next := this;
  9253. IF prev = NIL THEN
  9254. root := new;
  9255. ELSE
  9256. prev.next := new
  9257. END
  9258. END;
  9259. RETURN TRUE
  9260. END;
  9261. END EnterCase;
  9262. (** generate and return a new checker object, errors are entered into diagnostics **)
  9263. 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;
  9264. VAR checker: Checker;
  9265. BEGIN
  9266. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9267. RETURN checker
  9268. END NewChecker;
  9269. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9270. VAR warnings: Warnings;
  9271. BEGIN
  9272. NEW(warnings, diagnostics); RETURN warnings;
  9273. END NewWarnings;
  9274. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9275. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9276. END IsRangeType;
  9277. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9278. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9279. END IsMathArrayType;
  9280. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9281. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9282. END IsArrayType;
  9283. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9284. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9285. END IsComplexType;
  9286. (** if a type is an array-structured object type *)
  9287. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9288. VAR recordType: SyntaxTree.RecordType;
  9289. BEGIN
  9290. IF type = NIL THEN
  9291. RETURN FALSE
  9292. ELSE
  9293. type := type.resolved;
  9294. IF type IS SyntaxTree.PointerType THEN
  9295. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9296. IF type IS SyntaxTree.RecordType THEN
  9297. recordType := type(SyntaxTree.RecordType);
  9298. RETURN recordType.isObject & recordType.HasArrayStructure()
  9299. ELSE
  9300. RETURN FALSE
  9301. END
  9302. ELSE
  9303. RETURN FALSE
  9304. END
  9305. END
  9306. END IsArrayStructuredObjectType;
  9307. (** the math array structure of a type
  9308. - for math arrays: the array itself
  9309. - for pointers: the math array structure of the pointer base
  9310. - for array-structured object types: the underlying structure
  9311. - for non-math arrays and all other types: NIL
  9312. **)
  9313. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9314. VAR
  9315. result: SyntaxTree.MathArrayType;
  9316. BEGIN
  9317. IF type = NIL THEN
  9318. result := NIL
  9319. ELSE
  9320. type := type.resolved;
  9321. IF type IS SyntaxTree.PointerType THEN
  9322. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9323. END;
  9324. IF type IS SyntaxTree.MathArrayType THEN
  9325. result := type(SyntaxTree.MathArrayType)
  9326. ELSIF type IS SyntaxTree.RecordType THEN
  9327. result := type(SyntaxTree.RecordType).arrayStructure
  9328. ELSE
  9329. result := NIL
  9330. END
  9331. END;
  9332. RETURN result
  9333. END MathArrayStructureOfType;
  9334. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9335. VAR
  9336. result: BOOLEAN;
  9337. rangeExpression: SyntaxTree.RangeExpression;
  9338. BEGIN
  9339. IF x IS SyntaxTree.RangeExpression THEN
  9340. rangeExpression := x(SyntaxTree.RangeExpression);
  9341. result := TRUE;
  9342. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9343. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9344. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9345. ELSE
  9346. result := FALSE
  9347. END;
  9348. RETURN result
  9349. END IsStaticRange;
  9350. (** whether a type is a math array of tensor form **)
  9351. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9352. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9353. END IsTensor;
  9354. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9355. BEGIN
  9356. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9357. length := type(SyntaxTree.MathArrayType).staticLength;
  9358. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9359. RETURN TRUE
  9360. ELSE
  9361. RETURN FALSE
  9362. END;
  9363. END IsStaticMathArray;
  9364. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9365. BEGIN
  9366. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9367. END SymbolHasAddress;
  9368. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9369. BEGIN
  9370. RETURN
  9371. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9372. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9373. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9374. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9375. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9376. ;
  9377. END HasAddress;
  9378. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9379. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9380. BEGIN
  9381. IF (e IS SyntaxTree.Designator) THEN
  9382. d := e(SyntaxTree.Designator);
  9383. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9384. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9385. e := d.left;
  9386. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9387. END;
  9388. IF d # NIL THEN
  9389. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9390. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9391. END;
  9392. END;
  9393. RETURN FALSE;
  9394. END IsLocalVariable;
  9395. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9396. BEGIN
  9397. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9398. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9399. ELSE
  9400. RETURN TRUE
  9401. END;
  9402. END IsStaticProcedure;
  9403. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9404. CONST OptimizeMethodTable = FALSE;
  9405. BEGIN
  9406. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9407. END InMethodTable;
  9408. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9409. BEGIN
  9410. IF type = NIL THEN RETURN FALSE
  9411. ELSE
  9412. type := type.resolved;
  9413. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9414. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9415. END
  9416. END ReturnedAsParameter;
  9417. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9418. BEGIN
  9419. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9420. END StructuredReturnType;
  9421. END FoxSemanticChecker.
  9422. SystemTools.FreeDownTo FoxSemanticChecker ~