2
0

FoxSemanticChecker.Mod 380 KB

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