FoxSemanticChecker.Mod 399 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position, "base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position, "base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position, "realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position, "forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position, "forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position, "forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. parameter := procedureType.selfParameter;
  725. IF parameter # NIL THEN
  726. VisitParameter(parameter)
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  792. IF svalue = "A2" THEN
  793. RETURN TRUE
  794. END;
  795. END;
  796. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  797. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  798. END;
  799. RETURN FALSE;
  800. (*
  801. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  802. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  803. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  804. IF svalue[0] = "~" THEN
  805. Strings.TrimLeft(svalue, "~");
  806. IF svalue = backendName THEN
  807. RETURN TRUE;
  808. END;
  809. ELSIF svalue # backendName THEN
  810. RETURN TRUE;
  811. END;
  812. END;
  813. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  814. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  815. END;
  816. RETURN FALSE;
  817. *)
  818. END SkipImplementation;
  819. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  820. VAR this: SyntaxTree.Modifier;
  821. BEGIN
  822. this := modifiers;
  823. WHILE this # NIL DO
  824. IF ~this.resolved THEN
  825. IF checkUse THEN
  826. Error(this.position,"unexpected modifier");
  827. ELSE
  828. this.SetExpression(ResolveExpression(this.expression));
  829. this.Resolved;
  830. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  831. END;
  832. END;
  833. this := this.nextModifier
  834. END;
  835. END CheckModifiers;
  836. (**
  837. resolve procedure type
  838. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  839. **)
  840. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  841. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  842. BEGIN
  843. IF TypeNeedsResolution(procedureType) THEN
  844. modifiers := procedureType.modifiers;
  845. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  846. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  847. procedureType.SetInterrupt(TRUE);
  848. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  849. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  850. IF useDarwinCCalls THEN (*fld*)
  851. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  852. ELSE
  853. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  854. END
  855. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  856. procedureType.SetNoReturn(TRUE);
  857. END;
  858. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  859. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  860. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  861. CheckModifiers(modifiers, TRUE);
  862. modifiers := procedureType.returnTypeModifiers;
  863. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  864. CheckModifiers(modifiers, TRUE);
  865. typeFixes.Add(procedureType,currentScope);
  866. procedureType.SetHasPointers(procedureType.isDelegate);
  867. procedureType.SetState(SyntaxTree.Resolved);
  868. END;
  869. resolvedType := ResolvedType(procedureType)
  870. END VisitProcedureType;
  871. (** check and resolve record type
  872. - check base type: must be record, math array or array-structured object type
  873. - check declarations
  874. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  875. **)
  876. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  877. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  878. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  879. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  880. hasPointers: BOOLEAN;
  881. modifiers: SyntaxTree.Modifier;
  882. value: LONGINT;
  883. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  884. BEGIN
  885. type := type.resolved;
  886. IF (type IS SyntaxTree.PointerType) &
  887. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  888. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  889. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  890. RETURN TRUE
  891. ELSE
  892. RETURN FALSE
  893. END;
  894. END IsPointerToRecord;
  895. BEGIN
  896. IF TypeNeedsResolution(x) THEN
  897. hasPointers := FALSE;
  898. modifiers := x.modifiers;
  899. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  900. CheckModifiers(modifiers, TRUE);
  901. IF x.baseType # NIL THEN
  902. position := x.baseType.position;
  903. baseType := ResolveType(x.baseType);
  904. resolved := baseType.resolved;
  905. hasPointers := hasPointers OR resolved.hasPointers;
  906. IF x.isObject THEN (* object *)
  907. ASSERT(x.pointerType # NIL);
  908. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  909. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  910. baseType := NIL
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  913. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  914. ELSE
  915. Error(position,"object does not extend pointer to record, object or math array ")
  916. END;
  917. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  918. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  919. ELSIF IsPointerToRecord(resolved,recordType) THEN
  920. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  921. ELSIF resolved IS SyntaxTree.RecordType THEN
  922. ELSE
  923. Error(position,"pointer to record does not extend pointer to record or record")
  924. END;
  925. ELSE
  926. IF resolved IS SyntaxTree.RecordType THEN
  927. ELSE
  928. Error(position,"record does not extend record")
  929. END;
  930. END;
  931. x.SetBaseType(baseType);
  932. IF x.Level() > 15 THEN
  933. Error(position, "record/object inheritance level too high");
  934. (* note:
  935. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  936. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  937. inheritance history of a type.
  938. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  939. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  940. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  941. *)
  942. END;
  943. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  944. Error(position, "invalid inheritance of disposable types");
  945. END;
  946. END;
  947. Declarations(x.recordScope, FALSE);
  948. ResolveArrayStructure(x);
  949. (* computation of sizes and offsets skipped -> done in backend / system *)
  950. recordBase := x.GetBaseRecord();
  951. IF recordBase = NIL THEN numberMethods := 0
  952. ELSE numberMethods := recordBase.recordScope.numberMethods
  953. END;
  954. isRealtime := TRUE;
  955. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  956. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  957. END;
  958. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  959. WHILE symbol # NIL DO
  960. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  961. IF symbol IS SyntaxTree.Variable THEN
  962. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  963. END;
  964. IF symbol IS SyntaxTree.Procedure THEN
  965. procedure := symbol(SyntaxTree.Procedure);
  966. IF procedure.super # NIL THEN
  967. procedure.SetMethodNumber(procedure.super.methodNumber)
  968. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  969. procedure.SetMethodNumber(numberMethods);
  970. INC(numberMethods);
  971. END;
  972. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  973. Error(procedure.position,"realtime procedure in non-realtime object")
  974. END;
  975. END;
  976. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  977. Error(symbol.position,"non-realtime symbol in realtime object")
  978. END;
  979. symbol := symbol.nextSymbol;
  980. END;
  981. IF isRealtime THEN x.SetRealtime(TRUE) END;
  982. x.recordScope.SetNumberMethods(numberMethods);
  983. (* TODO: is this needed anymore? *)
  984. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  985. Error(x.position,"object extends a record")
  986. END;
  987. IF (x.typeDeclaration = NIL) THEN
  988. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  989. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  990. (*
  991. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  992. AnonymousTypeDeclaration(x,name);
  993. *)
  994. ELSE
  995. AnonymousTypeDeclaration(x,"Anonymous");
  996. END;
  997. END;
  998. x.SetHasPointers(hasPointers);
  999. x.SetState(SyntaxTree.Resolved);
  1000. END;
  1001. resolvedType := ResolvedType(x);
  1002. END VisitRecordType;
  1003. (** check and resolve cell type
  1004. - check base type: must be cell
  1005. - check declarations
  1006. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1007. **)
  1008. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1009. VAR
  1010. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1011. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1012. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1013. recordBase: SyntaxTree.RecordType;
  1014. numberMethods, int: LONGINT;
  1015. real: LONGREAL;
  1016. bool: BOOLEAN;
  1017. set: SET;
  1018. v: SyntaxTree.Expression;
  1019. str: Scanner.StringType;
  1020. atype: SyntaxTree.ArrayType;
  1021. prev: SyntaxTree.Scope;
  1022. skip: BOOLEAN;
  1023. svalue: ARRAY 32 OF CHAR;
  1024. BEGIN
  1025. IF TypeNeedsResolution(x) THEN
  1026. recordBase := NIL;
  1027. IF cellsAreObjects THEN
  1028. IF x.baseType = NIL THEN
  1029. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1030. ImportModule(qualifiedIdentifier.prefix, x.position);
  1031. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1032. x.SetBaseType(ResolveType(x.baseType));
  1033. recordBase := x.GetBaseRecord();
  1034. IF recordBase = NIL THEN
  1035. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1036. END;
  1037. ELSE
  1038. x.SetBaseType(ResolveType(x.baseType));
  1039. END;
  1040. ELSE
  1041. x.SetBaseType(ResolveType(x.baseType));
  1042. END;
  1043. IF recordBase = NIL THEN numberMethods := 0
  1044. ELSE numberMethods := recordBase.recordScope.numberMethods
  1045. END;
  1046. modifier := x.modifiers;
  1047. (*IF ~x.isCellNet THEN*)
  1048. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1049. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1050. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1051. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1052. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1053. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1054. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1055. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1056. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1057. symbol := system.activeCellsCapabilities;
  1058. WHILE symbol # NIL DO
  1059. IF HasFlag(modifier, symbol.name, position) THEN END;
  1060. symbol := symbol.nextSymbol;
  1061. END;
  1062. modifier := x.modifiers;
  1063. WHILE (modifier # NIL) DO
  1064. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1065. IF modifier.expression # NIL THEN
  1066. v := ConstantExpression(modifier.expression);
  1067. property.SetValue(v);
  1068. IF IsIntegerValue(modifier.expression, int) THEN
  1069. (*property.SetValue(modifier.expression);*)
  1070. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1071. property.SetType(system.longintType);
  1072. ELSIF IsRealValue(modifier.expression, real) THEN
  1073. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1074. property.SetType(system.longrealType);
  1075. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1076. property.SetType(system.booleanType);
  1077. ELSIF IsSetValue(modifier.expression, set) THEN
  1078. property.SetType(system.setType);
  1079. ELSIF IsStringValue(modifier.expression, str) THEN
  1080. (*property.SetValue(modifier.expression);*)
  1081. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1082. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1083. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1084. property.SetType(atype);
  1085. ELSE
  1086. Error(modifier.position, "unsupported property type");
  1087. END;
  1088. ELSE (* flag property *)
  1089. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1090. property.SetType(system.booleanType);
  1091. END;
  1092. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1093. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1094. x.AddProperty(property);
  1095. modifier := modifier.nextModifier;
  1096. END;
  1097. CheckModifiers(modifier, FALSE);
  1098. Declarations(x.cellScope, SkipImplementation(x));
  1099. (* process parameters *)
  1100. prev := currentScope;
  1101. currentScope := x.cellScope;
  1102. parameter :=x.firstParameter;
  1103. WHILE (parameter # NIL) DO
  1104. VisitParameter(parameter);
  1105. type := parameter.type.resolved;
  1106. IF ~(type IS SyntaxTree.PortType) THEN
  1107. WHILE IsStaticArray(type, type, len) DO
  1108. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1109. END;
  1110. WHILE IsDynamicArray(type, type) DO
  1111. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1112. END;
  1113. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1114. Error(parameter.position, "invalid type, must be port or static array of port ");
  1115. END;
  1116. END;
  1117. parameter := parameter.nextParameter;
  1118. END;
  1119. currentScope := prev;
  1120. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1121. WHILE symbol # NIL DO
  1122. IF symbol IS SyntaxTree.Variable THEN
  1123. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1124. END;
  1125. symbol := symbol.nextSymbol;
  1126. END;
  1127. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1128. IF (x.typeDeclaration = NIL) THEN
  1129. AnonymousTypeDeclaration(x,"Anonymous");
  1130. END;
  1131. x.SetState(SyntaxTree.Resolved);
  1132. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1133. Warning(x.position, "Forbidden empty Body.");
  1134. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1135. Warning(x.position, "Non-empty body for an engine?");
  1136. END;
  1137. END;
  1138. resolvedType := ResolvedType(x);
  1139. END VisitCellType;
  1140. (* check if an object is an array-structured object type
  1141. - determine the array structure
  1142. - collect operators from top to bottom in the inheritance hierarchy
  1143. - check if LEN operator is declared
  1144. - determine number of possible index operators
  1145. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1146. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1147. *)
  1148. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1149. VAR
  1150. indexOperatorCount, i: LONGINT;
  1151. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1152. isTensor: BOOLEAN;
  1153. BEGIN
  1154. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1155. (* determine array structure *)
  1156. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1157. END;
  1158. IF recordType.HasArrayStructure() THEN
  1159. (* the object is an ASOT *)
  1160. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1161. (* reset array access operators *)
  1162. arrayAccessOperators.len := NIL;
  1163. arrayAccessOperators.generalRead := NIL;
  1164. arrayAccessOperators.generalWrite := NIL;
  1165. IF isTensor THEN
  1166. (* all operators of dimensionalities 1 to max *)
  1167. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1168. ELSE
  1169. (* all operators of certain dimensionality *)
  1170. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1171. END;
  1172. NEW(arrayAccessOperators.read, indexOperatorCount);
  1173. NEW(arrayAccessOperators.write, indexOperatorCount);
  1174. FOR i := 0 TO indexOperatorCount - 1 DO
  1175. arrayAccessOperators.read[i] := NIL;
  1176. arrayAccessOperators.write[i] := NIL
  1177. END;
  1178. (* collect access operators in the record scope *)
  1179. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1180. IF arrayAccessOperators.len = NIL THEN
  1181. (* TODO: think about making this operator optional for static array structures *)
  1182. Error(recordType.position, "LEN operator missing")
  1183. END;
  1184. (* show error messages *)
  1185. IF isTensor THEN
  1186. (* require ARRAY [*] OF RANGE *)
  1187. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1188. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1189. ELSE
  1190. (* forbid ARRAY [*] OF RANGE *)
  1191. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1192. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1193. (* require RANGE, RANGE, ... RANGE *)
  1194. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1195. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1196. END;
  1197. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1198. ELSE
  1199. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1200. IF recordType.recordScope.firstOperator # NIL THEN
  1201. RETURN;
  1202. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1203. END
  1204. END
  1205. END ResolveArrayStructure;
  1206. (** collect array access operators in a record scope **)
  1207. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1208. VAR
  1209. baseType: SyntaxTree.Type;
  1210. operator: SyntaxTree.Operator;
  1211. isReadOperator, isGeneralOperator: BOOLEAN;
  1212. indexListSize, indexListKind, hashValue: LONGINT;
  1213. BEGIN
  1214. (* if a parent record scope exists, collect the operators there first *)
  1215. baseType := recordScope.ownerRecord.baseType;
  1216. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1217. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1218. END;
  1219. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1220. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1221. END;
  1222. (* go through all operators in the current record scope *)
  1223. operator := recordScope.firstOperator;
  1224. WHILE operator # NIL DO
  1225. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1226. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1227. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1228. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1229. IF isGeneralOperator THEN
  1230. IF isReadOperator THEN
  1231. arrayAccessOperators.generalRead := operator
  1232. ELSE
  1233. arrayAccessOperators.generalWrite := operator
  1234. END
  1235. ELSE
  1236. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1237. IF isReadOperator THEN
  1238. arrayAccessOperators.read[hashValue] := operator
  1239. ELSE
  1240. arrayAccessOperators.write[hashValue] := operator
  1241. END
  1242. END
  1243. END
  1244. ELSE
  1245. Error(operator.position, 'invalid operator')
  1246. END;
  1247. operator := operator.nextOperator
  1248. END
  1249. END CollectArrayAccessOperators;
  1250. (** the hash value of an index operator **)
  1251. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1252. VAR result: LONGINT;
  1253. BEGIN
  1254. IF isTensor THEN
  1255. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1256. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1257. ELSE
  1258. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1259. END
  1260. ELSE
  1261. result := indexListKind
  1262. END;
  1263. RETURN result
  1264. END IndexOperatorHash;
  1265. (** 2 to the power of exponent **)
  1266. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1267. VAR result, i: LONGINT;
  1268. BEGIN
  1269. result := 1;
  1270. FOR i := 1 TO exponent DO
  1271. result := result * 2;
  1272. END;
  1273. RETURN result
  1274. END TwoToThePowerOf;
  1275. (** check if a LEN operator has a correct signature. i.e.
  1276. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1277. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1278. **)
  1279. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1280. VAR
  1281. procedureType: SyntaxTree.ProcedureType;
  1282. returnedArrayType: SyntaxTree.MathArrayType;
  1283. result: BOOLEAN;
  1284. BEGIN
  1285. result := FALSE;
  1286. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1287. IF (procedureType.numberParameters = 0) THEN
  1288. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1289. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1290. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1291. IF returnedArrayType.form = SyntaxTree.Open THEN
  1292. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1293. result := TRUE
  1294. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1295. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1296. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1297. result := TRUE
  1298. END
  1299. END
  1300. END
  1301. END
  1302. END;
  1303. IF result THEN
  1304. (* export symbol automatically *)
  1305. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1306. ELSE
  1307. Error(operator.position, "LEN operator with invalid signature");
  1308. END;
  1309. RETURN result
  1310. END CheckLenOperator;
  1311. (** check if an index operator has a correct signature. i.e.
  1312. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1313. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1314. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1315. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1316. - determine if it is a read or write operator (existance of return type)
  1317. - check index parameters
  1318. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1319. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1320. [LONGINT] -> binary 0 -> 0
  1321. [RANGE] -> binary 1 -> 1
  1322. [LONGINT, LONGINT] -> binary 00 -> 0
  1323. [LONGINT, RANGE] -> binary 01 -> 1
  1324. [RANGE, LONGINT] -> binary 10 -> 2
  1325. [RANGE, RANGE] -> binary 11 -> 3
  1326. etc.
  1327. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1328. - for read operators, check if return type matches the type of data that is read
  1329. - for write operators, check if last parameter type matches the type of data that is written
  1330. **)
  1331. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1332. VAR
  1333. elementType, otherElementType, dataType: SyntaxTree.Type;
  1334. procedureType: SyntaxTree.ProcedureType;
  1335. mathArrayType: SyntaxTree.MathArrayType;
  1336. parameter: SyntaxTree.Parameter;
  1337. parameterCount, rangeCount, i: LONGINT;
  1338. hasTypeError: BOOLEAN;
  1339. BEGIN
  1340. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1341. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1342. (* determine if it is a read or write operator *)
  1343. isReadOperator := (procedureType.returnType # NIL);
  1344. IF isReadOperator THEN
  1345. indexListSize := parameterCount;
  1346. ELSE
  1347. indexListSize := parameterCount - 1;
  1348. END;
  1349. IF indexListSize < 1 THEN
  1350. Error(operator.position, "index operator with too few parameters");
  1351. RETURN FALSE
  1352. END;
  1353. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1354. (* general operator *)
  1355. isGeneralOperator := TRUE;
  1356. IF indexListSize > 1 THEN
  1357. Error(operator.position, "index operator with too many parameters");
  1358. RETURN FALSE
  1359. END;
  1360. (* ARRAY [*] OF RANGE*)
  1361. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1362. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1363. Error(operator.position, "index parameter not dynamic math array of range");
  1364. RETURN FALSE
  1365. END;
  1366. parameter := procedureType.firstParameter.nextParameter
  1367. ELSE
  1368. (* fixed-dim. operator *)
  1369. isGeneralOperator := FALSE;
  1370. (* check number of index parameters *)
  1371. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1372. (* for tensors, limited to a certain size *)
  1373. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1374. Error(operator.position, "too many index parameters for tensor");
  1375. RETURN FALSE
  1376. END
  1377. ELSE
  1378. (* for non-tensors, depends on dimensionality *)
  1379. IF indexListSize # arrayStructure.Dimensionality() THEN
  1380. Error(operator.position, "index parameter count does not match dimensionality");
  1381. RETURN FALSE
  1382. END
  1383. END;
  1384. (* go through all index parameters
  1385. - count the number of ranges
  1386. - determine the index list kind number
  1387. *)
  1388. indexListKind := 0;
  1389. rangeCount := 0;
  1390. parameter := procedureType.firstParameter;
  1391. FOR i := 1 TO indexListSize DO
  1392. indexListKind := indexListKind * 2;
  1393. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1394. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1395. INC(indexListKind);
  1396. INC(rangeCount)
  1397. ELSE
  1398. Error(parameter.position, "integer or range expected");
  1399. RETURN FALSE
  1400. END;
  1401. parameter := parameter.nextParameter
  1402. END;
  1403. END;
  1404. (*
  1405. - for read operators: check type of last parameter
  1406. - for write operators: check return type
  1407. *)
  1408. IF isReadOperator THEN
  1409. dataType := procedureType.returnType (* the return type *)
  1410. ELSE
  1411. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1412. END;
  1413. elementType := arrayStructure.ElementType();
  1414. hasTypeError := FALSE;
  1415. IF isGeneralOperator THEN
  1416. (* ARRAY [?] OF <Element> *)
  1417. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1418. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1419. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1420. hasTypeError := TRUE
  1421. END
  1422. ELSE
  1423. hasTypeError := TRUE
  1424. END
  1425. ELSE
  1426. IF rangeCount = 0 THEN
  1427. (* <Element> *)
  1428. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1429. ELSE
  1430. (* ARRAY [*, *, ..., *] OF <Element> *)
  1431. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1432. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1433. IF mathArrayType.IsFullyDynamic() THEN
  1434. IF mathArrayType.Dimensionality() = rangeCount THEN
  1435. otherElementType := mathArrayType.ElementType();
  1436. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1437. ELSE
  1438. hasTypeError := TRUE
  1439. END
  1440. ELSE
  1441. hasTypeError := TRUE
  1442. END
  1443. ELSE
  1444. hasTypeError := TRUE
  1445. END
  1446. END
  1447. END;
  1448. IF hasTypeError THEN
  1449. IF isReadOperator THEN
  1450. Error(operator.position, "return type does not match")
  1451. ELSE
  1452. Error(parameter.position, "type of last parameter does not match")
  1453. END;
  1454. RETURN FALSE
  1455. END;
  1456. (* export symbol automatically *)
  1457. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1458. RETURN TRUE
  1459. END CheckIndexOperator;
  1460. (** resolve all pending types (late resolving).
  1461. - type fixes are resolved at the end of the declaration phase
  1462. - type fixes may imply new type fixes that are also entered at the end of the list
  1463. **)
  1464. PROCEDURE FixTypes;
  1465. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1466. BEGIN
  1467. prevScope := currentScope;
  1468. p := typeFixes.Get(currentScope);
  1469. WHILE p # NIL DO
  1470. ASSERT(currentScope # NIL);
  1471. ASSERT(p IS SyntaxTree.Type);
  1472. IF p IS SyntaxTree.PointerType THEN
  1473. FixPointerType(p(SyntaxTree.PointerType))
  1474. ELSIF p IS SyntaxTree.ProcedureType THEN
  1475. FixProcedureType(p(SyntaxTree.ProcedureType))
  1476. ELSE
  1477. HALT(100);
  1478. END;
  1479. p := typeFixes.Get(currentScope);
  1480. END;
  1481. currentScope :=prevScope;
  1482. END FixTypes;
  1483. (**
  1484. resolve type x
  1485. - if x is nil then return nil
  1486. - if x cannot be resolved then the result is invalidType else the result is x
  1487. - the resolved type is entered into x.resolved
  1488. **)
  1489. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR prev,resolved: SyntaxTree.Type;
  1491. BEGIN
  1492. prev := resolvedType;
  1493. resolvedType := SyntaxTree.invalidType;
  1494. IF x = NIL THEN resolvedType := NIL
  1495. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1496. END;
  1497. resolved := resolvedType;
  1498. resolvedType := prev;
  1499. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1500. RETURN resolved
  1501. END ResolveType;
  1502. (*** compatibility rules ***)
  1503. (**
  1504. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1505. **)
  1506. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1507. VAR result: SyntaxTree.Type;
  1508. BEGIN
  1509. result := SyntaxTree.invalidType;
  1510. IF type = NIL THEN Error(position, "expression of type NIL");
  1511. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1512. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1513. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1514. ELSE result := type.resolved
  1515. END;
  1516. RETURN result
  1517. END RegularType;
  1518. (** returns signature compatibility of procedure types this and to
  1519. - if not compatible then error is reported
  1520. - compatibility means type equality
  1521. **)
  1522. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1523. VAR result: BOOLEAN;
  1524. BEGIN
  1525. result := SameType(to,this);
  1526. IF ~result THEN
  1527. Error(position, "signature incompatible");
  1528. IF VerboseErrorMessage THEN
  1529. Printout.Info("this",this);
  1530. Printout.Info("to",to);
  1531. END;
  1532. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1533. Error(position, "signature incompatible: realtime flag must be inherited");
  1534. END;
  1535. RETURN result
  1536. END SignatureCompatible;
  1537. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1538. - for var parameters compatibility means same type except for
  1539. - formal is of open array of system byte
  1540. - formal is of record type
  1541. - formal is of open array type
  1542. - formal is of open math array type
  1543. - for value parameters compatibllity means assignment compatibility except for
  1544. - formal is of open array type
  1545. if compatible the return true else report error and return false
  1546. **)
  1547. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1548. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1549. BEGIN
  1550. formalType := RegularType(formal.position,formal.type);
  1551. actualType := RegularType(actual.position,actual.type);
  1552. error := FALSE;
  1553. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1554. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1555. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1556. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1557. END;
  1558. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1559. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1560. ELSIF ~IsVariable(actual) THEN
  1561. result := FALSE; error := TRUE;
  1562. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1563. Error(actual.position,"not a variable: no operator for writing");
  1564. ELSE
  1565. Error(actual.position,"is not a variable");
  1566. END;
  1567. IF VerboseErrorMessage THEN
  1568. Printout.Info("actual",actual);
  1569. Printout.Info("formal",formal);
  1570. END;
  1571. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1572. result := CompatibleTo(system,actualType,formalType);
  1573. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1574. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1575. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1576. IF IsArrayStructuredObjectType(actualType) THEN
  1577. actualType := MathArrayStructureOfType(actualType)
  1578. END;
  1579. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1580. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1581. Error(actual.position,"incompatible non-static actual type");
  1582. END;
  1583. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1584. Error(actual.position,"incompatible tensor (use a range expression)");
  1585. END;
  1586. ELSE
  1587. result := SameType(actualType,formalType)
  1588. END
  1589. ELSE
  1590. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1591. actualType := system.characterType;
  1592. END;
  1593. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1594. result := TRUE; (* special rule for WINAPI parameters *)
  1595. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1596. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1597. ELSE
  1598. result := CompatibleTo(system,actualType,formalType);
  1599. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1600. Error(actual.position,"incompatible non-static actual type");
  1601. END;
  1602. END;
  1603. END;
  1604. IF ~result & ~error THEN
  1605. Error(actual.position,"incompatible parameter");
  1606. IF VerboseErrorMessage THEN
  1607. Printout.Info("actual",actual);
  1608. Printout.Info("formal",formal);
  1609. END;
  1610. END;
  1611. RETURN result
  1612. END ParameterCompatible;
  1613. (** check compatibility for expressions of the form left := right
  1614. - if compatible then return true else error report and return false
  1615. - check if left is variable
  1616. - check compatiblity
  1617. **)
  1618. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1619. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1620. BEGIN
  1621. result := FALSE;
  1622. leftType := RegularType(left.position,left.type);
  1623. rightType := RegularType(right.position,right.type);
  1624. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1625. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1626. END;
  1627. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1628. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1629. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1630. (* error already handled *)
  1631. result := TRUE;
  1632. ELSIF ~IsVariable(left) THEN
  1633. Error(left.position,"is not a variable");
  1634. IF VerboseErrorMessage THEN
  1635. Printout.Info("left",left);
  1636. Printout.Info("right",right);
  1637. END;
  1638. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1639. result := TRUE;
  1640. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1641. result := TRUE
  1642. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1643. Error(left.position,"incompatible assignment");
  1644. IF VerboseErrorMessage THEN
  1645. Printout.Info("left",left);
  1646. Printout.Info("right",right);
  1647. END;
  1648. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1649. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1650. Error(right.position,"forbidden assignment of a nested procedure");
  1651. ELSE
  1652. result := TRUE
  1653. END;
  1654. RETURN result
  1655. END AssignmentCompatible;
  1656. (*** values ***)
  1657. (** check and resolve integer value **)
  1658. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1659. VAR hugeint: HUGEINT;
  1660. BEGIN
  1661. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1662. value.SetType(Global.GetIntegerType(system,hugeint));
  1663. resolvedExpression := value
  1664. END VisitIntegerValue;
  1665. (** check and resolve real value **)
  1666. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1667. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1668. BEGIN
  1669. subtype := value(SyntaxTree.RealValue).subtype;
  1670. IF subtype = Scanner.Real THEN
  1671. type := system.realType
  1672. ELSIF subtype = Scanner.Longreal THEN
  1673. type := system.longrealType
  1674. ELSE
  1675. HALT(100)
  1676. END;
  1677. value.SetType(type);
  1678. resolvedExpression := value
  1679. END VisitRealValue;
  1680. (** check and resolve complex value **)
  1681. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1682. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1683. BEGIN
  1684. subtype := value(SyntaxTree.ComplexValue).subtype;
  1685. IF subtype = Scanner.Real THEN
  1686. type := system.complexType
  1687. ELSIF subtype = Scanner.Longreal THEN
  1688. type := system.longcomplexType
  1689. ELSE
  1690. HALT(100)
  1691. END;
  1692. value.SetType(type);
  1693. resolvedExpression := value
  1694. END VisitComplexValue;
  1695. (** check and resolve set value **)
  1696. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1697. BEGIN
  1698. value.SetType(system.setType);
  1699. resolvedExpression := value
  1700. END VisitSetValue;
  1701. (** check and resolve set value **)
  1702. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1703. BEGIN
  1704. value.SetType(SyntaxTree.invalidType);
  1705. resolvedExpression := value
  1706. END VisitMathArrayValue;
  1707. (** check and resolve boolean value **)
  1708. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1709. BEGIN
  1710. value.SetType(system.booleanType);
  1711. resolvedExpression := value
  1712. END VisitBooleanValue;
  1713. (** check and resolve string value **)
  1714. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1715. BEGIN
  1716. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1717. resolvedExpression := value
  1718. END VisitStringValue;
  1719. (** check and resolve character value **)
  1720. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1721. BEGIN
  1722. value.SetType(system.characterType);
  1723. resolvedExpression := value
  1724. END VisitCharacterValue;
  1725. (** check and resolve nil value **)
  1726. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1727. BEGIN
  1728. value.SetType(system.nilType);
  1729. resolvedExpression := value
  1730. END VisitNilValue;
  1731. (** check and resolve enumerator value **)
  1732. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1733. BEGIN
  1734. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1735. ASSERT(value.type # NIL);
  1736. resolvedExpression := value
  1737. END VisitEnumerationValue;
  1738. (*** expressions ***)
  1739. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1740. - check all elements on integer type
  1741. - if element range is constant, then check lower and upper bound
  1742. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1743. if an error occurs then report error and return invalidExpression
  1744. **)
  1745. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1746. VAR
  1747. i: LONGINT;
  1748. element: SyntaxTree.Expression;
  1749. constant: BOOLEAN;
  1750. elements: SyntaxTree.ExpressionList;
  1751. s: SET;
  1752. result: SyntaxTree.Expression;
  1753. value: SyntaxTree.Value;
  1754. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1755. VAR
  1756. left, right: SyntaxTree.Expression;
  1757. elementResult: SyntaxTree.Expression;
  1758. leftInteger, rightInteger, temp: LONGINT;
  1759. BEGIN
  1760. (* set context of range *)
  1761. IF element IS SyntaxTree.RangeExpression THEN
  1762. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1763. END;
  1764. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1765. IF elementResult = SyntaxTree.invalidExpression THEN
  1766. (* error already reported *)
  1767. constant := FALSE
  1768. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1769. (* the element is a range expression *)
  1770. (* extract left and right hand side of range *)
  1771. left := elementResult(SyntaxTree.RangeExpression).first;
  1772. right := elementResult(SyntaxTree.RangeExpression).last;
  1773. (* guaranteed by VisitRangeExpression: *)
  1774. ASSERT((left # NIL) & (right # NIL));
  1775. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1776. ELSE
  1777. (* the element is not a range expression *)
  1778. (* check type and add conversion if needed *)
  1779. IF IsIntegerType(elementResult.type.resolved) THEN
  1780. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1781. ELSE
  1782. Error(elementResult.position, "non integer element in set");
  1783. elementResult := SyntaxTree.invalidExpression;
  1784. constant := FALSE
  1785. END;
  1786. left := elementResult;
  1787. right := elementResult
  1788. END;
  1789. IF elementResult # SyntaxTree.invalidExpression THEN
  1790. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1791. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1792. Error(left.position,"not allowed set integer value");
  1793. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1794. Error(right.position,"not allowed set integer value");
  1795. END
  1796. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1797. Error(right.position,"not allowed set integer value");
  1798. ELSE
  1799. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1800. s := {};
  1801. ELSE
  1802. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1803. IF leftInteger < 0 THEN leftInteger := 0 END;
  1804. (*!!!!!!!!! this is a hack !!!!!!! *)
  1805. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1806. extends the range x..31 to x..63 !!!!!! *)
  1807. s := s + {leftInteger..rightInteger};
  1808. END;
  1809. END;
  1810. ELSE
  1811. constant := FALSE;
  1812. END
  1813. END;
  1814. RETURN elementResult
  1815. END CheckElement;
  1816. BEGIN
  1817. result := set; constant := TRUE; s := {}; elements := set.elements;
  1818. IF elements # NIL THEN
  1819. FOR i := 0 TO elements.Length()-1 DO
  1820. element := elements.GetExpression(i);
  1821. element := CheckElement(element);
  1822. IF element = SyntaxTree.invalidExpression THEN
  1823. result := SyntaxTree.invalidExpression
  1824. END;
  1825. elements.SetExpression(i,element);
  1826. END;
  1827. END;
  1828. IF constant THEN
  1829. value := SyntaxTree.NewSetValue(set.position,s);
  1830. value.SetType(system.setType);
  1831. result.SetResolved(value);
  1832. END;
  1833. (* optimization possible
  1834. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1835. left this to the programmer...
  1836. *)
  1837. result.SetType(system.setType);
  1838. resolvedExpression := result;
  1839. END VisitSet;
  1840. (*
  1841. old variant: quite generic but needs better conversion handling, do this?
  1842. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1843. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1844. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1845. BEGIN
  1846. type := NIL;
  1847. numberElements := x.elements.Length();
  1848. FOR i := 0 TO numberElements-1 DO
  1849. expression := x.elements.GetExpression(i);
  1850. position := expression.position;
  1851. expression := ResolveExpression(x.elements.GetExpression(i));
  1852. x.elements.SetExpression(i,de);
  1853. IF type = NIL THEN
  1854. type := expression.type;
  1855. ELSIF CompatibleTo(system,expression.type,type) THEN
  1856. (* ok *)
  1857. ELSIF CompatibleTo(system,type,expression.type) THEN
  1858. type := expression.type
  1859. ELSE
  1860. Error(expression.position, "incompatible element types");
  1861. type := SyntaxTree.invalidType;
  1862. END;
  1863. END;
  1864. isValue := TRUE;
  1865. FOR i := 0 TO numberElements-1 DO
  1866. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1867. x.elements.SetExpression(i,expression);
  1868. isValue := isValue & (expression.resolved # NIL);
  1869. END;
  1870. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1871. arrayType.SetArrayBase(type);
  1872. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1873. IF isValue THEN
  1874. value := SyntaxTree.NewMathArrayValue(position);
  1875. value.SetElements(x.elements);
  1876. x.SetResolved(value);
  1877. END;
  1878. x.SetType(arrayType);
  1879. resolvedExpression := x;
  1880. END VisitMathArrayExpression;
  1881. *)
  1882. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1883. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1884. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1885. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1886. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1887. BEGIN
  1888. numberElements := x.elements.Length();
  1889. FOR i := 0 TO numberElements-1 DO
  1890. expression := x.elements.GetExpression(i);
  1891. IF expression IS SyntaxTree.MathArrayExpression THEN
  1892. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1893. ELSE
  1894. position := expression.position;
  1895. expression := ResolveExpression(x.elements.GetExpression(i));
  1896. x.elements.SetExpression(i,expression);
  1897. IF type = NIL THEN
  1898. type := expression.type;
  1899. ELSIF CompatibleTo(system,expression.type,type) THEN
  1900. (* ok *)
  1901. ELSIF CompatibleTo(system,type,expression.type) THEN
  1902. type := expression.type
  1903. ELSE
  1904. Error(expression.position, "incompatible element types");
  1905. type := SyntaxTree.invalidType;
  1906. END;
  1907. END;
  1908. END;
  1909. END RecursivelyFindType;
  1910. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1911. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. FOR i := 0 TO numberElements-1 DO
  1915. expression := x.elements.GetExpression(i);
  1916. IF expression IS SyntaxTree.MathArrayExpression THEN
  1917. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1918. ELSE
  1919. position := expression.position;
  1920. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1921. x.elements.SetExpression(i,expression);
  1922. isValue := isValue & (expression.resolved # NIL);
  1923. END;
  1924. END;
  1925. END RecursivelySetExpression;
  1926. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1927. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1928. arrayType: SyntaxTree.MathArrayType;
  1929. BEGIN
  1930. numberElements := x.elements.Length();
  1931. baseType := NIL;
  1932. gsize := 0;
  1933. FOR i := 0 TO numberElements-1 DO
  1934. expression := x.elements.GetExpression(i);
  1935. IF expression IS SyntaxTree.MathArrayExpression THEN
  1936. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1937. IF i=0 THEN
  1938. gsize := size;
  1939. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1940. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1941. ELSE expression.SetType(baseType)
  1942. END;
  1943. ELSIF baseType = NIL THEN baseType := type;
  1944. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1945. END;
  1946. END;
  1947. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1948. arrayType.SetArrayBase(baseType);
  1949. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1950. RETURN ResolveType(arrayType);
  1951. END RecursivelySetType;
  1952. BEGIN
  1953. type := NIL;
  1954. RecursivelyFindType(x);
  1955. isValue := TRUE;
  1956. RecursivelySetExpression(x);
  1957. arrayType := RecursivelySetType(x);
  1958. x.SetType(arrayType);
  1959. IF isValue THEN
  1960. value := SyntaxTree.NewMathArrayValue(x.position);
  1961. value.SetArray(x);
  1962. x.SetResolved(value);
  1963. value.SetType(arrayType);
  1964. END;
  1965. x.SetType(arrayType);
  1966. resolvedExpression := x;
  1967. END VisitMathArrayExpression;
  1968. (** check and resolve unary expression **)
  1969. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1970. VAR
  1971. left: SyntaxTree.Expression;
  1972. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1973. bool: BOOLEAN;
  1974. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1975. value: SyntaxTree.Value;
  1976. BEGIN
  1977. type := SyntaxTree.invalidType;
  1978. left := ResolveExpression(unaryExpression.left);
  1979. unaryExpression.SetLeft(left);
  1980. operator := unaryExpression.operator;
  1981. result := unaryExpression;
  1982. IF ~system.operatorDefined[operator] THEN
  1983. Error(left.position,"Operator Not Defined");
  1984. RETURN
  1985. ELSIF left.type = NIL THEN
  1986. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1987. resolvedExpression := SyntaxTree.invalidExpression;
  1988. RETURN
  1989. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1990. RETURN
  1991. END;
  1992. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1993. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1994. END;
  1995. IF operatorCall # NIL THEN
  1996. result := operatorCall;
  1997. type := operatorCall.type;
  1998. (* admissible operators
  1999. Minus number, set
  2000. Not boolean
  2001. *)
  2002. ELSE
  2003. CASE unaryExpression.operator OF
  2004. |Scanner.Minus:
  2005. IF IsIntegerType(left.type.resolved) THEN
  2006. IF left.resolved # NIL THEN
  2007. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2008. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2009. result.SetResolved(value);
  2010. type := Global.GetIntegerType(system,int);
  2011. value.SetType(type);
  2012. ELSE
  2013. type := left.type
  2014. END
  2015. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2016. IF IsRealValue(left,real) THEN
  2017. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2018. result.SetResolved(value);
  2019. type := left.type;
  2020. value.SetType(type);
  2021. ELSE
  2022. type := left.type;
  2023. END;
  2024. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2025. IF IsSetValue(left,set) THEN
  2026. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2027. result.SetResolved(value);
  2028. type := left.type;
  2029. value.SetType(type);
  2030. ELSE
  2031. type := left.type;
  2032. END;
  2033. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2034. IF IsComplexValue(left, real, imaginary) THEN
  2035. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2036. result.SetResolved(value);
  2037. type := left.type;
  2038. value.SetType(type);
  2039. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2040. ELSE
  2041. type := left.type;
  2042. END
  2043. ELSE
  2044. Error(left.position,"unary operator not applicable");
  2045. END;
  2046. |Scanner.Not:
  2047. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2048. IF IsBooleanValue(left,bool) THEN
  2049. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2050. result.SetResolved(value);
  2051. type := system.booleanType;
  2052. value.SetType(type);
  2053. ELSE
  2054. type := system.booleanType;
  2055. END;
  2056. ELSE
  2057. Error(left.position,"unary operator not applicable");
  2058. END;
  2059. |Scanner.Plus:
  2060. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2061. result := left; type := left.type;
  2062. ELSE
  2063. Error(left.position,"unary operator not applicable");
  2064. END;
  2065. (* ADDRESS OF *)
  2066. |Scanner.Address:
  2067. IF HasAddress(left) THEN
  2068. type := system.addressType;
  2069. ELSE
  2070. type := SyntaxTree.invalidType;
  2071. Error(left.position,"has no address");
  2072. Printout.Info("par", left);
  2073. END;
  2074. (* SIZE OF *)
  2075. |Scanner.Size:
  2076. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2077. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2078. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2079. value := SyntaxTree.NewIntegerValue(left.position, int);
  2080. result.SetResolved(value);
  2081. type := Global.GetIntegerType(system,int);
  2082. value.SetType(type)
  2083. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2084. ELSE
  2085. (* for variables, system sizeof could represent the physically occupied size
  2086. determined via the type descriptor, implement that ? *)
  2087. Error(left.position,"is not a type symbol");
  2088. END
  2089. (* ALIAS OF *)
  2090. |Scanner.Alias:
  2091. type := left.type.resolved;
  2092. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2093. type := SyntaxTree.invalidType;
  2094. Error(left.position,"alias on non math array type");
  2095. END;
  2096. ELSE
  2097. Error(left.position,"unary operator not defined");
  2098. END;
  2099. END;
  2100. result.SetType(type);
  2101. resolvedExpression := result
  2102. END VisitUnaryExpression;
  2103. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2104. VAR
  2105. result: SyntaxTree.Expression;
  2106. array: SyntaxTree.MathArrayExpression;
  2107. value: SyntaxTree.MathArrayValue;
  2108. isValue: BOOLEAN;
  2109. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2110. BEGIN
  2111. type := type.resolved;
  2112. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2113. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2114. END;
  2115. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2116. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2117. END;
  2118. RETURN type
  2119. END BaseType;
  2120. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2121. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2122. BEGIN
  2123. numberElements := x.elements.Length();
  2124. FOR i := 0 TO numberElements-1 DO
  2125. expression := x.elements.GetExpression(i);
  2126. IF expression IS SyntaxTree.MathArrayExpression THEN
  2127. array := SyntaxTree.NewMathArrayExpression(position);
  2128. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2129. expression := array;
  2130. ELSE
  2131. position := expression.position;
  2132. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2133. isValue := isValue & (expression.resolved # NIL);
  2134. END;
  2135. to.elements.AddExpression(expression);
  2136. END;
  2137. END RecursivelyConvert;
  2138. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2139. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2140. arrayType: SyntaxTree.MathArrayType;
  2141. BEGIN
  2142. numberElements := x.elements.Length();
  2143. baseType := NIL;
  2144. gsize := 0;
  2145. FOR i := 0 TO numberElements-1 DO
  2146. expression := x.elements.GetExpression(i);
  2147. IF expression IS SyntaxTree.MathArrayExpression THEN
  2148. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2149. IF i=0 THEN
  2150. gsize := size;
  2151. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2152. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2153. ELSE expression.SetType(baseType)
  2154. END;
  2155. ELSIF baseType = NIL THEN baseType := type;
  2156. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2157. END;
  2158. END;
  2159. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2160. arrayType.SetArrayBase(baseType);
  2161. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2162. RETURN ResolveType(arrayType);
  2163. END RecursivelySetType;
  2164. BEGIN
  2165. result := SyntaxTree.invalidExpression;
  2166. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2167. result := expression (* do not convert *)
  2168. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2169. isValue := TRUE;
  2170. type := BaseType(type);
  2171. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2172. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2173. value := SyntaxTree.NewMathArrayValue(array.position);
  2174. value.SetArray(array);
  2175. value.SetType(RecursivelySetType(array));
  2176. result := value;
  2177. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2178. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2179. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2180. IF result = NIL THEN
  2181. result := SyntaxTree.invalidExpression;
  2182. Error(position, "incompatible conversion");
  2183. IF VerboseErrorMessage THEN
  2184. Printout.Info("expression",expression);
  2185. Printout.Info("type",type);
  2186. END;
  2187. END;
  2188. END;
  2189. RETURN result
  2190. END MathArrayConversion;
  2191. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2192. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2193. BEGIN
  2194. result := expression; type := type.resolved;
  2195. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2196. (* skip, no conversion *)
  2197. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2198. int := expression(SyntaxTree.IntegerValue).hvalue;
  2199. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2200. int := Global.ConvertSigned(int,system.SizeOf(type));
  2201. result := SyntaxTree.NewIntegerValue(position,int);
  2202. result.SetType(type);
  2203. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2204. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2205. result := SyntaxTree.NewIntegerValue(position,int);
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.FloatType) THEN
  2208. result := SyntaxTree.NewRealValue(expression.position,int);
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2211. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.SetType) THEN
  2214. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2217. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2218. result.SetType(type);
  2219. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2220. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2221. Error(position, "huge integer value incompatible to enumeration");
  2222. END;
  2223. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.PortType) THEN
  2226. result := ConvertValue(position, expression, system.integerType);
  2227. ELSE
  2228. Error(position, "integer value cannot be converted");
  2229. result := SyntaxTree.invalidExpression;
  2230. IF VerboseErrorMessage THEN
  2231. Printout.Info("expression",expression);
  2232. Printout.Info("type",type);
  2233. END;
  2234. END;
  2235. ELSIF IsRealValue(expression,real) THEN
  2236. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2237. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2238. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2239. result.SetType(type);
  2240. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2241. int := ENTIERH(real);
  2242. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.FloatType) THEN
  2245. result := SyntaxTree.NewRealValue(position,real);
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2248. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2249. result.SetType(type);
  2250. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2251. ELSIF (type IS SyntaxTree.PortType) THEN
  2252. result := ConvertValue(position, expression, system.integerType);
  2253. ELSE
  2254. Error(position, "real value cannot be converted");
  2255. result := SyntaxTree.invalidExpression;
  2256. END
  2257. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2258. IF (type IS SyntaxTree.ComplexType) THEN
  2259. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2260. result.SetType(type);
  2261. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2262. ELSE
  2263. Error(position, "complex value cannot be converted");
  2264. result := SyntaxTree.invalidExpression;
  2265. END
  2266. ELSIF IsSetValue(expression,set) THEN
  2267. IF (type IS SyntaxTree.IntegerType) THEN
  2268. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2271. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2272. result.SetType(type);
  2273. ELSIF (type IS SyntaxTree.PortType) THEN
  2274. result := ConvertValue(position, expression, system.integerType);
  2275. ELSE
  2276. Error(position, "set value cannot be converted");
  2277. result := SyntaxTree.invalidExpression;
  2278. END;
  2279. ELSIF IsStringValue(expression,string) THEN
  2280. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2281. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2282. result.SetType(type);
  2283. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2284. ELSE
  2285. Error(position, "string value cannot be converted");
  2286. result := SyntaxTree.invalidExpression;
  2287. END;
  2288. ELSIF IsCharacterValue(expression,char) THEN
  2289. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2290. string[0] := char; string[1] := 0X;
  2291. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2292. result := SyntaxTree.NewStringValue(expression.position,string);
  2293. result.SetType(type);
  2294. ELSIF (type IS SyntaxTree.ByteType) THEN
  2295. (* do not simply set the new type as this could invalidate types of constants *)
  2296. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2297. result.SetType(type)
  2298. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2299. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2300. result.SetType(type);
  2301. ELSIF (type IS SyntaxTree.SetType) THEN
  2302. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2303. result.SetType(type);
  2304. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2305. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2306. result.SetType(type);
  2307. ELSIF (type IS SyntaxTree.PortType) THEN
  2308. result := ConvertValue(position, expression, system.integerType);
  2309. ELSE
  2310. Error(position, "character value cannot be converted");
  2311. result := SyntaxTree.invalidExpression;
  2312. END;
  2313. ELSIF expression IS SyntaxTree.NilValue THEN
  2314. IF type IS SyntaxTree.AddressType THEN
  2315. result := SyntaxTree.NewIntegerValue(position,0);
  2316. result.SetType(type);
  2317. ELSE
  2318. result := expression;
  2319. END;
  2320. (* nothing to be done *)
  2321. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2322. result := MathArrayConversion(position, expression,type);
  2323. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2324. int := expression(SyntaxTree.EnumerationValue).value;
  2325. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2326. int := Global.ConvertSigned(int,system.SizeOf(type));
  2327. result := SyntaxTree.NewIntegerValue(position,int);
  2328. result.SetType(type);
  2329. ELSE
  2330. result := expression;
  2331. END;
  2332. (* nothing to be done *)
  2333. ELSE
  2334. Error(position, "expression cannot be converted");
  2335. IF VerboseErrorMessage THEN
  2336. Printout.Info("expression",expression);
  2337. Printout.Info("type",type);
  2338. END;
  2339. result := SyntaxTree.invalidExpression;
  2340. END;
  2341. RETURN result
  2342. END ConvertValue;
  2343. (**
  2344. return a conversion of an expression to a given type
  2345. - if expression is already of same type then return expression
  2346. - if incompatible conversion then report error and return invalidExpression
  2347. **)
  2348. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2349. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2350. BEGIN
  2351. type := type.resolved;
  2352. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2353. result := expression;
  2354. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2355. ELSIF expression = NIL THEN (* NIL expression *)
  2356. ELSIF expression.type = NIL THEN
  2357. Error(position, "expression of type NIL cannot be converted");
  2358. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2359. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2360. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2361. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2362. ELSIF expression.resolved # NIL THEN (* value *)
  2363. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2364. IF value IS SyntaxTree.Value THEN
  2365. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2366. result.SetResolved(value(SyntaxTree.Value));
  2367. result.SetType(value.type);
  2368. ELSE
  2369. result := value
  2370. END;
  2371. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2372. expressionList := SyntaxTree.NewExpressionList();
  2373. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2374. typeDeclaration.SetDeclaredType(type);
  2375. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2376. typeSymbol.SetType(typeDeclaration.type);
  2377. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2378. expressionList.AddExpression(expression);
  2379. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2380. result.SetType(type);
  2381. ELSIF IsArrayStructuredObjectType(type) THEN
  2382. (* no type can be converted to an array-structured object type *)
  2383. HALT(100)
  2384. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2385. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2386. result := MathArrayConversion(position, expression,type);
  2387. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2388. expression := ConvertToMathArray(expression);
  2389. type := MathArrayStructureOfType(type);
  2390. result := MathArrayConversion(position, expression, type)
  2391. ELSE
  2392. Error(expression.position,"cannot convert non array type to array type")
  2393. END;
  2394. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2395. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2396. OR ~(type IS SyntaxTree.ArrayType) THEN
  2397. Error(expression.position,"cannot convert array type to non-array type")
  2398. END;
  2399. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2400. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2401. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2402. (*skip, no conversion*)
  2403. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2404. (* skip, no conversion *)
  2405. ELSE
  2406. ASSERT(~(type IS SyntaxTree.RangeType));
  2407. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2408. ASSERT(type # NIL);
  2409. END;
  2410. RETURN result
  2411. END NewConversion;
  2412. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2413. BEGIN
  2414. IF CompatibleTo(system,expression.type, type) THEN
  2415. RETURN NewConversion(position, expression, type, NIL);
  2416. ELSE
  2417. Error(expression.position, "incompatible expression");
  2418. RETURN SyntaxTree.invalidExpression
  2419. END;
  2420. END CompatibleConversion;
  2421. (**
  2422. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2423. **)
  2424. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2425. VAR leftType,rightType: SyntaxTree.Type;
  2426. BEGIN
  2427. IF left.type = NIL THEN Error(left.position,"no type")
  2428. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2429. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2430. (* error already handled *)
  2431. ELSE
  2432. leftType := left.type.resolved; rightType := right.type.resolved;
  2433. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2434. right := NewConversion(right.position, right, leftType, NIL);
  2435. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2436. left := NewConversion(left.position,left,rightType,NIL);
  2437. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2438. right := NewConversion(right.position, right, leftType, NIL);
  2439. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2440. left := NewConversion(left.position,left,rightType,NIL);
  2441. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2442. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2443. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2444. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2445. ELSIF
  2446. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2447. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2448. (* must be the case LONGREAL / COMPLEX ) *)
  2449. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2450. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2451. ELSE
  2452. Error(left.position,"incompatible operands");
  2453. END;
  2454. END;
  2455. END ConvertOperands;
  2456. (** find and return best operator matching to parameter list (nil, if none)
  2457. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2458. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2459. **)
  2460. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2461. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2462. identifier: SyntaxTree.Identifier;
  2463. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2464. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2465. BEGIN
  2466. operator := scope.firstOperator;
  2467. WHILE(operator # NIL) DO
  2468. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2469. procedureType := operator.type(SyntaxTree.ProcedureType);
  2470. distance := Distance(system, procedureType,actualParameters);
  2471. IF (distance < Infinity) THEN
  2472. IF returnType # NIL THEN
  2473. IF procedureType.returnType = NIL THEN
  2474. distance := Infinity
  2475. ELSE
  2476. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2477. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2478. END;
  2479. END;
  2480. END;
  2481. (*
  2482. IF distance < Infinity THEN
  2483. TRACE(distance, operator);
  2484. Printout.Info("potential operator",operator);
  2485. ELSE
  2486. Printout.Info("operator not possible",operator);
  2487. END;
  2488. *)
  2489. IF distance < bestDistance THEN
  2490. bestDistance := distance;
  2491. bestOperator := operator;
  2492. END;
  2493. END;
  2494. operator := operator.nextOperator;
  2495. END;
  2496. (*
  2497. Printout.Info("taken operator",bestOperator);
  2498. *)
  2499. END FindInScope;
  2500. BEGIN
  2501. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2502. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2503. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2504. import := currentScope.ownerModule.moduleScope.firstImport;
  2505. WHILE (bestDistance > 0) & (import # NIL) DO
  2506. IF import.module # NIL THEN
  2507. identifier := Global.GetIdentifier(operator,import.module.case);
  2508. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2509. END;
  2510. import := import.nextImport;
  2511. END;
  2512. RETURN bestOperator
  2513. END FindOperator;
  2514. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2515. BEGIN
  2516. currentScope := scope;
  2517. END SetCurrentScope;
  2518. (**
  2519. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2520. - handle LEN and DIM operator for array-structured object types
  2521. - find operator, if found then
  2522. - if in other module then add import designator
  2523. - create symbol designator for operator
  2524. - if error then return invalidExpression, if no operator then return NIL
  2525. **)
  2526. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2527. VAR
  2528. operator: SyntaxTree.Operator;
  2529. import: SyntaxTree.Import;
  2530. expression, result: SyntaxTree.Expression;
  2531. designator: SyntaxTree.Designator;
  2532. actualParameters, tempList: SyntaxTree.ExpressionList;
  2533. recordType: SyntaxTree.RecordType;
  2534. castReturnType : SyntaxTree.MathArrayType;
  2535. BEGIN
  2536. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2537. result := SyntaxTree.invalidExpression
  2538. ELSIF leftExpression = NIL THEN
  2539. result := NIL
  2540. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2541. (* LEN or DIM operator on array-structured object type *)
  2542. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2543. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2544. IF recordType.arrayAccessOperators.len = NIL THEN
  2545. Error(position, "call of undeclared LEN operator");
  2546. result := SyntaxTree.invalidExpression
  2547. ELSE
  2548. ASSERT(leftExpression IS SyntaxTree.Designator);
  2549. designator := leftExpression(SyntaxTree.Designator);
  2550. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2551. ASSERT(expression IS SyntaxTree.Designator);
  2552. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2553. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2554. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2555. result := designator
  2556. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2557. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2558. tempList := SyntaxTree.NewExpressionList();
  2559. tempList.AddExpression(rightExpression);
  2560. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2561. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2562. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2563. tempList := SyntaxTree.NewExpressionList();
  2564. tempList.AddExpression(designator);
  2565. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2566. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2567. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2568. END
  2569. END;
  2570. ELSE
  2571. (* import OCArrayBase if needed *)
  2572. IF ~arrayBaseImported THEN
  2573. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2574. (* operators on complex numbers *)
  2575. ImportModule(Global.ArrayBaseName,position);
  2576. arrayBaseImported := TRUE;
  2577. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2578. IF op = Global.Dim THEN
  2579. (* not existing in OCArrayBase *)
  2580. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2581. (* not existing in OCArrayBase *)
  2582. ELSE
  2583. ImportModule(Global.ArrayBaseName,position);
  2584. arrayBaseImported := TRUE;
  2585. END
  2586. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2587. ImportModule(Global.ArrayBaseName,position);
  2588. arrayBaseImported := TRUE
  2589. END;
  2590. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2591. (* LEN(RANGE) *)
  2592. ImportModule(Global.ArrayBaseName,position);
  2593. arrayBaseImported := TRUE;
  2594. END;
  2595. END;
  2596. actualParameters := SyntaxTree.NewExpressionList();
  2597. actualParameters.AddExpression(leftExpression);
  2598. IF rightExpression # NIL THEN
  2599. actualParameters.AddExpression(rightExpression)
  2600. END;
  2601. operator := FindOperator(system,op,actualParameters,resultType);
  2602. IF operator # NIL THEN
  2603. designator := NIL;
  2604. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2605. import := currentScope.ownerModule.moduleScope.firstImport;
  2606. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2607. import := import.nextImport;
  2608. END;
  2609. expression := NewSymbolDesignator(position,NIL,import);
  2610. designator := expression(SyntaxTree.Designator);
  2611. END;
  2612. expression := NewSymbolDesignator(position,designator,operator);
  2613. designator := expression(SyntaxTree.Designator);
  2614. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2615. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2616. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2617. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2618. result.SetType(castReturnType);
  2619. END;
  2620. ELSE
  2621. result := NIL;
  2622. END;
  2623. END;
  2624. RETURN result
  2625. END NewOperatorCall;
  2626. (** check and resolve binary expression **)
  2627. (*! clean up *)
  2628. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2629. VAR left,right,result: SyntaxTree.Expression;
  2630. leftType, rightType: SyntaxTree.Type;
  2631. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2632. cl,cr: CHAR;
  2633. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2634. type: SyntaxTree.Type;
  2635. value: SyntaxTree.Value;
  2636. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2637. integerConstantFolding: BOOLEAN;
  2638. list: SyntaxTree.ExpressionList;
  2639. PROCEDURE NewBool(v: BOOLEAN);
  2640. BEGIN
  2641. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2642. value.SetType(system.booleanType);
  2643. result.SetResolved(value);
  2644. type := system.booleanType
  2645. END NewBool;
  2646. PROCEDURE NewSet(v: SET);
  2647. BEGIN
  2648. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2649. value.SetType(system.setType);
  2650. result.SetResolved(value);
  2651. type := system.setType;
  2652. END NewSet;
  2653. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2654. BEGIN
  2655. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2656. (* type cast to "larger" type only if the value is still in the range *)
  2657. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2658. value.SetType(t);
  2659. END;
  2660. result.SetResolved(value);
  2661. type := value.type;
  2662. END NewInteger;
  2663. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2664. BEGIN
  2665. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2666. value.SetType(t);
  2667. result.SetResolved(value);
  2668. type := t;
  2669. END NewReal;
  2670. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2671. BEGIN
  2672. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2673. value.SetType(t);
  2674. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2675. result.SetResolved(value);
  2676. type := t;
  2677. END NewComplex;
  2678. BEGIN
  2679. type := SyntaxTree.invalidType;
  2680. left := ResolveExpression(binaryExpression.left);
  2681. right := ResolveExpression(binaryExpression.right);
  2682. binaryExpression.SetLeft(left);
  2683. binaryExpression.SetRight(right);
  2684. result := binaryExpression;
  2685. operator := binaryExpression.operator;
  2686. IF ~system.operatorDefined[operator] THEN
  2687. Error(left.position,"Operator Not Defined");
  2688. result := SyntaxTree.invalidExpression;
  2689. RETURN
  2690. END;
  2691. IF left.type = NIL THEN
  2692. Error(left.position,"Expression has no result type");
  2693. result := SyntaxTree.invalidExpression;
  2694. RETURN;
  2695. END;
  2696. IF right.type = NIL THEN
  2697. Error(right.position,"Expression has no result type");
  2698. result := SyntaxTree.invalidExpression;
  2699. RETURN;
  2700. END;
  2701. leftType := left.type.resolved; rightType := right.type.resolved;
  2702. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2703. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2704. END;
  2705. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2706. list := SyntaxTree.NewExpressionList();
  2707. list.AddExpression(right);
  2708. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2709. END;
  2710. IF operatorCall # NIL THEN
  2711. result := operatorCall;
  2712. type := operatorCall.type;
  2713. (* admissible operators:
  2714. Times, Plus, Minus numeric numeric numeric
  2715. set set set
  2716. Slash numeric numeric real /complex
  2717. set set set
  2718. Div , Mod integer integer integer
  2719. And, Or bool bool bool
  2720. Equal, Unequal basic basic bool
  2721. pointer pointer bool
  2722. object object bool
  2723. record record bool
  2724. string string bool
  2725. enumerator enumerator bool
  2726. Less, LessEqual,
  2727. Greater, GreaterEqual integer/real integer/real bool
  2728. enumerator enumerator bool
  2729. In integer set bool
  2730. Is pointer type bool
  2731. object type bool
  2732. record type bool
  2733. Upto: special abbreviation for a..b
  2734. *)
  2735. ELSIF (left.type = NIL) THEN
  2736. Error(left.position,"type (left operand) = NIL in binary expression");
  2737. D.Str("nil type in "); D.Type(left); D.Ln;
  2738. result := SyntaxTree.invalidExpression;
  2739. ELSIF (right.type = NIL) THEN
  2740. Error(right.position,"type (right operand) = NIL in binary expression");
  2741. result := SyntaxTree.invalidExpression;
  2742. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2743. result := SyntaxTree.invalidExpression;
  2744. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2745. HALT(100);
  2746. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2747. type := system.booleanType;
  2748. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2749. Error(right.position,"is not a type ");
  2750. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2751. Error(binaryExpression.position,"is not a type extension of ");
  2752. IF VerboseErrorMessage THEN
  2753. Printout.Info("left",left);
  2754. Printout.Info("right",right);
  2755. END;
  2756. ELSIF IsUnsafePointer(left.type) THEN
  2757. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2758. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2759. NewBool(TRUE)
  2760. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2761. NewBool(TRUE);
  2762. ELSIF IsUnextensibleRecord(left) THEN
  2763. NewBool(FALSE)
  2764. END
  2765. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2766. Error(right.position,"must not be a type");
  2767. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2768. Error(left.position,"must not be a type");
  2769. ELSIF operator = Scanner.In THEN (* left IN right *)
  2770. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2771. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2772. NewBool(il IN sr);
  2773. ELSE
  2774. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2775. left := NewConversion(left.position, left, system.longintType,NIL);
  2776. binaryExpression.SetLeft(left)
  2777. END;
  2778. type := system.booleanType;
  2779. END
  2780. ELSE
  2781. Error(binaryExpression.position, "incompatible operands");
  2782. END
  2783. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2784. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2785. Error(binaryExpression.position,"incompatible operands");
  2786. END;
  2787. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2788. ELSE Error(binaryExpression.position,"operator not defined 1")
  2789. END
  2790. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2791. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2792. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2793. Error(binaryExpression.position,"incompatible operands");
  2794. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2795. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2796. ConvertOperands(left, right);
  2797. binaryExpression.SetLeft(left);
  2798. binaryExpression.SetRight(right);
  2799. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2800. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2801. END;
  2802. type := system.booleanType;
  2803. ELSE
  2804. Error(binaryExpression.position,"operator not defined 3");
  2805. END
  2806. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2807. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2808. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2809. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2810. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2811. CASE operator OF
  2812. |Scanner.Equal: NewBool(strl^=strr^);
  2813. |Scanner.Unequal:NewBool(strl^#strr^);
  2814. |Scanner.Less: NewBool(strl^<strr^);
  2815. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2816. |Scanner.Greater: NewBool(strl^>strr^);
  2817. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2818. ELSE
  2819. Error(binaryExpression.position,"operator not defined 4");
  2820. END;
  2821. END;
  2822. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2823. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2824. type := system.booleanType
  2825. ELSE
  2826. Error(binaryExpression.position,"operator not defined 5");
  2827. END;
  2828. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2829. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2830. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2831. type := system.booleanType;
  2832. ELSE
  2833. Error(binaryExpression.position,"operator not defined 6");
  2834. END
  2835. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2836. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2837. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2838. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2839. type := system.booleanType
  2840. ELSE
  2841. Error(binaryExpression.position,"operator not defined for enumerators");
  2842. END;
  2843. ELSE
  2844. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2845. END;
  2846. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2847. type := system.booleanType;
  2848. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2849. type := system.booleanType;
  2850. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2851. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2852. THEN
  2853. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2854. IF (leftType # rightType) THEN
  2855. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2856. ConvertOperands(left,right); (* operands must be of the same type here *)
  2857. END;
  2858. binaryExpression.SetLeft(left);
  2859. binaryExpression.SetRight(right);
  2860. leftType := left.type.resolved;
  2861. rightType := right.type.resolved;
  2862. END;
  2863. type := leftType;
  2864. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2865. Error(binaryExpression.position,"conversion failed ?");
  2866. IF VerboseErrorMessage THEN
  2867. Printout.Info("left",left);
  2868. Printout.Info("right",right);
  2869. END;
  2870. ELSIF IsIntegerType(leftType) THEN
  2871. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2872. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2873. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2874. Error(binaryExpression.position,"division by zero");
  2875. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2876. Error(binaryExpression.position,"integer division by negative number");
  2877. END;
  2878. END;
  2879. (* constant folding *)
  2880. (* bootstrap64
  2881. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2882. CASE operator OF
  2883. |Scanner.Plus: NewInteger(il+ir,left.type);
  2884. |Scanner.Minus: NewInteger(il-ir,left.type);
  2885. |Scanner.Times: NewInteger(il*ir,left.type);
  2886. |Scanner.Slash:
  2887. IF ir # 0 THEN
  2888. NewReal(il/ir, system.realType);
  2889. END;
  2890. |Scanner.Mod:
  2891. IF ir > 0 THEN
  2892. NewInteger(il MOD ir,left.type);
  2893. END;
  2894. |Scanner.Div:
  2895. IF ir > 0 THEN
  2896. NewInteger(il DIV ir,left.type);
  2897. END;
  2898. |Scanner.Equal: NewBool(il=ir);
  2899. |Scanner.Unequal:NewBool(il#ir);
  2900. |Scanner.Less: NewBool(il<ir);
  2901. |Scanner.LessEqual: NewBool(il<=ir);
  2902. |Scanner.Greater: NewBool(il>ir);
  2903. |Scanner.GreaterEqual: NewBool(il>=ir);
  2904. ELSE Error(binaryExpression.position,"operator not defined 7");
  2905. END;
  2906. ELS*)
  2907. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2908. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2909. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2910. CASE operator OF
  2911. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2912. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2913. |Scanner.Times: NewInteger(hl*hr,left.type);
  2914. |Scanner.Slash:
  2915. IF hr = 0 THEN
  2916. Error(binaryExpression.position,"division by zero");
  2917. ELSE
  2918. IF type.sizeInBits = 64 THEN
  2919. NewReal(hl/hr,system.longrealType);
  2920. ELSE
  2921. NewReal(hl/hr,system.realType)
  2922. END
  2923. END;
  2924. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2925. |Scanner.Mod:
  2926. IF hr = 0 THEN
  2927. Error(binaryExpression.position,"division by zero");
  2928. ELSE
  2929. NewInteger(hl MOD hr, left.type);
  2930. (* bootstrap64
  2931. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2932. *)
  2933. END;
  2934. |Scanner.Div:
  2935. IF hr = 0 THEN
  2936. Error(binaryExpression.position,"division by zero");
  2937. ELSE
  2938. NewInteger(hl DIV hr, left.type);
  2939. (* bootstrap64
  2940. NewInteger(Machine.DivH(hl,hr),left.type);
  2941. *)
  2942. END;
  2943. (* *)
  2944. |Scanner.Equal: NewBool(hl=hr);
  2945. |Scanner.Unequal: NewBool(hl#hr);
  2946. |Scanner.Less: NewBool(hl<hr);
  2947. |Scanner.LessEqual: NewBool(hl<=hr);
  2948. |Scanner.Greater: NewBool(hl>hr);
  2949. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2950. ELSE Error(binaryExpression.position,"operator not defined 8");
  2951. END;
  2952. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2953. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2954. type := left.type
  2955. ELSIF (operator = Scanner.Slash) THEN
  2956. left := NewConversion(left.position,left,system.realType,NIL);
  2957. right := NewConversion(right.position,right,system.realType,NIL);
  2958. binaryExpression.SetLeft(left);
  2959. binaryExpression.SetRight(right);
  2960. type := system.realType
  2961. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2962. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2963. type := system.booleanType
  2964. ELSE
  2965. Error(binaryExpression.position,"operator not defined 9");
  2966. END;
  2967. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2968. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2969. CASE operator OF
  2970. |Scanner.Plus: NewReal(rl+rr,leftType);
  2971. |Scanner.Minus: NewReal(rl-rr,leftType);
  2972. |Scanner.Times:NewReal(rl*rr,leftType);
  2973. |Scanner.Slash:
  2974. IF rr = 0 THEN
  2975. Error(binaryExpression.position,"division by zero");
  2976. ELSE
  2977. NewReal(rl/rr,leftType);
  2978. END
  2979. |Scanner.Equal: NewBool(rl=rr);
  2980. |Scanner.Unequal: NewBool(rl#rr);
  2981. |Scanner.Less: NewBool(rl<rr);
  2982. |Scanner.LessEqual: NewBool(rl<=rr);
  2983. |Scanner.Greater: NewBool(rl>rr);
  2984. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2985. ELSE Error(binaryExpression.position,"operator not defined 10");
  2986. END;
  2987. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2988. type := left.type
  2989. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2990. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2991. type := system.booleanType
  2992. ELSE
  2993. Error(binaryExpression.position,"operator not defined 11");
  2994. IF VerboseErrorMessage THEN
  2995. Printout.Info("left",left);
  2996. Printout.Info("right",right);
  2997. END;
  2998. END;
  2999. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3000. CASE operator OF
  3001. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3002. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3003. ELSE
  3004. Error(binaryExpression.position,"operator not defined");
  3005. IF VerboseErrorMessage THEN
  3006. Printout.Info("left", left);
  3007. Printout.Info("right", right)
  3008. END;
  3009. END;
  3010. IF ~error THEN
  3011. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3012. Error(binaryExpression.position,"division by zero")
  3013. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3014. (* do constant folding *)
  3015. CASE operator OF
  3016. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3017. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3018. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3019. |Scanner.Slash:
  3020. divisor := c * c + d * d;
  3021. ASSERT(divisor # 0);
  3022. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3023. |Scanner.Equal: NewBool((a = c) & (b = d))
  3024. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3025. END
  3026. END
  3027. END
  3028. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3029. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3030. CASE operator OF
  3031. |Scanner.And: NewBool(bl & br);
  3032. |Scanner.Or: NewBool(bl OR br);
  3033. |Scanner.Equal: NewBool(bl = br);
  3034. |Scanner.Unequal: NewBool(bl # br);
  3035. ELSE Error(binaryExpression.position,"operator not defined 12");
  3036. END;
  3037. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3038. type := system.booleanType
  3039. ELSE
  3040. Error(binaryExpression.position,"operator not defined 13");
  3041. END;
  3042. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3043. (* constant folding *)
  3044. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3045. IF operator = Scanner.Equal THEN
  3046. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3047. ELSIF operator = Scanner.Unequal THEN
  3048. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3049. END;
  3050. END;
  3051. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3052. type := system.booleanType;
  3053. ELSE
  3054. Error(binaryExpression.position, "operator not defined");
  3055. END;
  3056. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3057. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3058. CASE operator OF
  3059. |Scanner.Plus: NewSet(sl + sr);
  3060. |Scanner.Minus: NewSet(sl - sr);
  3061. |Scanner.Times: NewSet(sl * sr);
  3062. |Scanner.Slash: NewSet(sl / sr);
  3063. |Scanner.Equal: NewBool(sl=sr);
  3064. |Scanner.Unequal: NewBool(sl#sr);
  3065. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3066. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3067. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3068. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3069. ELSE Error(binaryExpression.position,"operator not defined 14");
  3070. END;
  3071. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3072. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3073. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3074. THEN
  3075. type := system.booleanType
  3076. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3077. type := left.type
  3078. ELSE
  3079. Error(binaryExpression.position,"operator not defined 15");
  3080. END;
  3081. ELSIF IsCharacterType(left.type) THEN
  3082. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3083. CASE operator OF
  3084. |Scanner.Equal: NewBool(cl=cr);
  3085. |Scanner.Unequal: NewBool(cl#cr);
  3086. |Scanner.Less: NewBool(cl<cr);
  3087. |Scanner.LessEqual: NewBool(cl<=cr);
  3088. |Scanner.Greater: NewBool(cl>cr);
  3089. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3090. ELSE Error(binaryExpression.position,"operator not defined 16");
  3091. END;
  3092. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3093. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3094. type := system.booleanType
  3095. ELSE
  3096. Error(binaryExpression.position,"operator not defined 17");
  3097. END;
  3098. ELSE
  3099. Error(binaryExpression.position,"operator not defined 18");
  3100. END;
  3101. ELSE
  3102. Error(binaryExpression.position,"operator not defined 19");
  3103. END;
  3104. IF type = SyntaxTree.invalidType THEN
  3105. result := SyntaxTree.invalidExpression
  3106. ELSE
  3107. result.SetType(type)
  3108. END;
  3109. resolvedExpression := result
  3110. END VisitBinaryExpression;
  3111. (** resolve a range expression of the from <<first .. last BY step>>
  3112. - depending on the context different things are checked:
  3113. ArrayIndex:
  3114. - components must be integers
  3115. - replace missing lower bound with 0
  3116. - replace missing upper bound with MAX(LONGINT)
  3117. - replace missing step size with 1
  3118. SetElement:
  3119. - components must be integers
  3120. - replace missing lower bound with 0
  3121. - replace missing upper bound with MAX(SET)
  3122. - must not have step size
  3123. CaseGuard:
  3124. - components must be constant
  3125. - components must be integers or characters
  3126. - must have lower and upper bound present
  3127. - components are made compatible
  3128. - must not have step size
  3129. - if error: return invalidExpression
  3130. **)
  3131. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3132. VAR
  3133. hasError: BOOLEAN;
  3134. first, last, step: SyntaxTree.Expression;
  3135. BEGIN
  3136. hasError := FALSE;
  3137. first := x.first;
  3138. last := x.last;
  3139. step := x.step;
  3140. (* check lower bound *)
  3141. IF x.context = SyntaxTree.CaseGuard THEN
  3142. IF first = NIL THEN
  3143. Error(x.position, "missing lower bound");
  3144. hasError := TRUE
  3145. ELSE
  3146. first := ResolveExpression(first);
  3147. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3148. Error(first.position, "lower bound not integer or character");
  3149. hasError := TRUE
  3150. ELSE
  3151. IF first IS SyntaxTree.StringValue THEN
  3152. (* add conversion from string to character *)
  3153. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3154. END
  3155. END;
  3156. (* check if expression is constant *)
  3157. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3158. (* error already reported *)
  3159. hasError := TRUE
  3160. END
  3161. END
  3162. ELSE (* ArrayIndex, SetElement *)
  3163. IF first = NIL THEN
  3164. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3165. END;
  3166. first := ResolveExpression(first);
  3167. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3168. first := NewConversion(first.position, first, system.longintType, NIL)
  3169. ELSE
  3170. Error(first.position, "lower bound not integer");
  3171. hasError := TRUE
  3172. END
  3173. END;
  3174. (* check upper bound *)
  3175. IF x.context = SyntaxTree.CaseGuard THEN
  3176. IF last = NIL THEN
  3177. Error(x.position, "missing upper bound");
  3178. hasError := TRUE
  3179. ELSE
  3180. last := ResolveExpression(last);
  3181. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3182. Error(last.position, "lower bound not integer or character");
  3183. hasError := TRUE
  3184. ELSE
  3185. IF last IS SyntaxTree.StringValue THEN
  3186. (* add conversion from string to character *)
  3187. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3188. END
  3189. END;
  3190. (* check if expression is constant *)
  3191. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3192. (* error already reported *)
  3193. hasError := TRUE
  3194. ELSE
  3195. (* try to make lower and upper bound compatible *)
  3196. ConvertOperands(first, last);
  3197. IF first.type.resolved # last.type.resolved THEN
  3198. Error(x.position, "lower and upper bounds incompatible");
  3199. hasError := TRUE
  3200. END
  3201. END
  3202. END
  3203. ELSE (* ArrayIndex, SetElement *)
  3204. IF last = NIL THEN
  3205. IF x.context = SyntaxTree.ArrayIndex THEN
  3206. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3207. ELSE
  3208. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3209. END
  3210. END;
  3211. last := ResolveExpression(last);
  3212. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3213. last := NewConversion(last.position, last, system.longintType, NIL)
  3214. ELSE
  3215. Error(last.position, "upper bound not integer");
  3216. hasError := TRUE
  3217. END
  3218. END;
  3219. (* check step size *)
  3220. IF x.context = SyntaxTree.ArrayIndex THEN
  3221. IF step = NIL THEN
  3222. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3223. END;
  3224. step := ResolveExpression(step);
  3225. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3226. step := NewConversion(step.position, step, system.longintType, NIL)
  3227. ELSE
  3228. Error(step.position, "step size not integer");
  3229. hasError := TRUE
  3230. END
  3231. ELSE (* SetElement, CaseGuard *)
  3232. IF step # NIL THEN
  3233. Error(last.position, "step size not allowed in this context");
  3234. hasError := TRUE
  3235. END
  3236. END;
  3237. IF hasError THEN
  3238. resolvedExpression := SyntaxTree.invalidExpression
  3239. ELSE
  3240. x.SetFirst(first);
  3241. x.SetLast(last);
  3242. x.SetStep(step);
  3243. x.SetType(system.rangeType);
  3244. resolvedExpression := x;
  3245. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3246. END
  3247. END VisitRangeExpression;
  3248. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3249. BEGIN
  3250. x.SetType(NIL);
  3251. resolvedExpression := x;
  3252. END VisitTensorRangeExpression;
  3253. (** resolve the expression d and return result as designator
  3254. - resolve expression
  3255. - if expression is a designator then return designator else error message and return invalidDesignator
  3256. **)
  3257. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3258. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3259. BEGIN
  3260. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3261. resolved := ResolveExpression(d);
  3262. IF resolved = SyntaxTree.invalidExpression THEN
  3263. (* error should already have been reported *)
  3264. result := SyntaxTree.invalidDesignator;
  3265. ELSIF resolved IS SyntaxTree.Designator THEN
  3266. result := resolved(SyntaxTree.Designator);
  3267. ELSE
  3268. Error(d.position,"is no designator ! ");
  3269. result := SyntaxTree.invalidDesignator;
  3270. END;
  3271. (* result.type might be nil. *)
  3272. RETURN result
  3273. END ResolveDesignator;
  3274. (**
  3275. symbol designator generated in this module
  3276. nothing to be resolved
  3277. **)
  3278. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3279. BEGIN
  3280. resolvedExpression := x;
  3281. END VisitSymbolDesignator;
  3282. (**
  3283. self designator generated in this module
  3284. nothing to be resolved
  3285. **)
  3286. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3287. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3288. BEGIN
  3289. (* check if in record scope *)
  3290. scope := currentScope;
  3291. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3292. scope := scope.outerScope;
  3293. END;
  3294. IF scope = NIL THEN (* in module scope *)
  3295. x.SetType(system.anyType);
  3296. ELSIF scope IS SyntaxTree.CellScope THEN
  3297. cell := scope(SyntaxTree.CellScope).ownerCell;
  3298. x.SetType(cell);
  3299. ELSE (* in record scope *)
  3300. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3301. IF (record # NIL) & (record.pointerType # NIL) THEN
  3302. type := ResolveType(record.pointerType);
  3303. x.SetType(type);
  3304. ELSE
  3305. x.SetType(record);
  3306. END;
  3307. END;
  3308. resolvedExpression := x;
  3309. END VisitSelfDesignator;
  3310. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3311. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3312. BEGIN
  3313. scope := currentScope;
  3314. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3315. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3316. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3317. returnType := procedureType.returnType;
  3318. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3319. THEN
  3320. x.SetType(returnType);
  3321. ELSE
  3322. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3323. x.SetType(SyntaxTree.invalidType);
  3324. END;
  3325. ELSE
  3326. Error(x.position,"forbidden access to result designator");
  3327. x.SetType(SyntaxTree.invalidType);
  3328. END;
  3329. x.SetAssignable(TRUE);
  3330. resolvedExpression := x;
  3331. END VisitResultDesignator;
  3332. (**
  3333. return symbol designator as an expression
  3334. - if symbol is a constant then return the constant value expression
  3335. - else
  3336. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3337. - if symbol is a guarded variable then return a TypeGuardDesignator
  3338. - else return a symbol designator
  3339. **)
  3340. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3341. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3342. guardType: SyntaxTree.Type;
  3343. BEGIN
  3344. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3345. result := SyntaxTree.invalidExpression;
  3346. ASSERT(symbol # NIL);
  3347. (*
  3348. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3349. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3350. Error(position, "type not allowed here");
  3351. ELS *)
  3352. (* not needed any more as values are stored in the expression
  3353. IF symbol IS SyntaxTree.Constant THEN
  3354. result := symbol(SyntaxTree.Constant).value
  3355. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3356. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3357. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3358. ELSE
  3359. result := symbol(SyntaxTree.Constant).value
  3360. END;
  3361. ELSE
  3362. *)
  3363. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3364. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3365. THEN
  3366. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3367. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3368. left := NewDereferenceDesignator(position,left);
  3369. left.SetHidden(TRUE);
  3370. END;
  3371. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3372. scope := currentScope;
  3373. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3374. scope := scope.outerScope;
  3375. END;
  3376. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3377. Error(position, "forbidden access to symbol in parent procedure scope");
  3378. END;
  3379. END;
  3380. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3381. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3382. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3383. ELSE
  3384. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3385. END;
  3386. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3387. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3388. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3389. result.SetType(symbol.type);
  3390. result.SetAssignable(assignable);
  3391. symbol.MarkUsed;
  3392. IF symbol IS SyntaxTree.Constant THEN
  3393. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3394. END;
  3395. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3396. variableAccessed := TRUE
  3397. END;
  3398. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3399. IF GetGuard(symbol,guardType) THEN
  3400. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3401. END;
  3402. END;
  3403. ASSERT(result.type # NIL);
  3404. RETURN result
  3405. END NewSymbolDesignator;
  3406. (** check and resolve an identifier designator "identifier"
  3407. - if identifier = self then return SelfDesignator
  3408. - else find symbol in current scope
  3409. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3410. **)
  3411. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3412. VAR symbol: SyntaxTree.Symbol;
  3413. BEGIN
  3414. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3415. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3416. IF symbol # NIL THEN
  3417. ResolveSymbol(symbol);
  3418. ASSERT(symbol.type # NIL);
  3419. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3420. ELSE
  3421. Error(identifierDesignator.position,"Undeclared Identifier");
  3422. IF VerboseErrorMessage THEN
  3423. Printout.Info("undeclared identifier designator",identifierDesignator);
  3424. END;
  3425. resolvedExpression := SyntaxTree.invalidDesignator;
  3426. END;
  3427. END VisitIdentifierDesignator;
  3428. (** check and resolve a selector designator of the form left.designator
  3429. - if left is a pointer type then do auto dereferenciation
  3430. - left denotes a search scope:
  3431. - if left type is import type then set search scope to respective module
  3432. - if left type is enumeration type then set search scope to respective enumeration scope
  3433. - elsif left type is record type then set search scope to record scope
  3434. - search symbol in computed scope
  3435. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3436. **)
  3437. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3438. VAR
  3439. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3440. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3441. BEGIN
  3442. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3443. left := ResolveDesignator(selectorDesignator.left);
  3444. result := SyntaxTree.invalidDesignator;
  3445. IF left # NIL THEN
  3446. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3447. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3448. END;
  3449. scope := NIL;
  3450. IF left.type = NIL THEN
  3451. Error(selectorDesignator.position,"field on nil typed designator");
  3452. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3453. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3454. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3455. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3456. module := symbol(SyntaxTree.Import).module;
  3457. IF module # NIL THEN
  3458. scope := module.moduleScope
  3459. ELSE
  3460. Error(left.position,"module not loaded");
  3461. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3462. END;
  3463. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3464. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3465. ASSERT(scope # NIL)
  3466. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3467. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3468. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3469. IF type IS SyntaxTree.EnumerationType THEN
  3470. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3471. ELSE
  3472. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3473. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3474. END;
  3475. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3476. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3477. ELSE
  3478. Error(selectorDesignator.position,"field on non-record type designator");
  3479. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3480. END;
  3481. symbol := NIL;
  3482. IF scope # NIL THEN
  3483. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3484. IF symbol # NIL THEN
  3485. ResolveSymbol(symbol);
  3486. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3487. symbol.MarkUsed
  3488. ELSE
  3489. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3490. IF VerboseErrorMessage THEN
  3491. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3492. Printout.Info("scope", scope);
  3493. Printout.Info("left", left);
  3494. Printout.Info("undeclared identifier",selectorDesignator);
  3495. Printout.Info("left resolved designator",left);
  3496. END
  3497. END;
  3498. END;
  3499. END;
  3500. resolvedExpression := result;
  3501. END VisitSelectorDesignator;
  3502. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3503. VAR len,idx: LONGINT;
  3504. BEGIN
  3505. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3506. IF idx < 0 THEN
  3507. Error(index.position,"index out of bounds (too small)")
  3508. ELSE
  3509. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3510. Error(index.position,"index out of bounds (too large)");
  3511. END;
  3512. END;
  3513. END;
  3514. END IndexCheck;
  3515. (*
  3516. - if index designator has not type, use newBaseType as its type
  3517. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3518. - special rule: if static array of dynamic array occurs, make it all dynamic
  3519. index designator type: new base type: new index designator type:
  3520. NIL z z
  3521. ARRAY [x, y] z ARRAY [x, y] OF z
  3522. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3523. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3524. *)
  3525. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3526. VAR
  3527. mathArrayType: SyntaxTree.MathArrayType;
  3528. makeDynamic: BOOLEAN;
  3529. BEGIN
  3530. IF indexDesignator.type = NIL THEN
  3531. indexDesignator.SetType(newBaseType)
  3532. ELSE
  3533. (* index designator must be a of math array type *)
  3534. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3535. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3536. (* determine if all arrays have to be made dynamic *)
  3537. makeDynamic :=
  3538. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3539. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3540. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3541. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3542. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3543. END;
  3544. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3545. mathArrayType.SetArrayBase(newBaseType)
  3546. END
  3547. END SetIndexBaseType;
  3548. (** check and append index list element to index designator of math array
  3549. - check validity of single index or array range
  3550. - compute new type
  3551. - if range then create new array type (calculate length of resulting array)
  3552. - otherwise take sourceArray.arrayBase as new type
  3553. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3554. **)
  3555. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3556. VAR
  3557. targetArray: SyntaxTree.MathArrayType;
  3558. first, last, step: SyntaxTree.Expression;
  3559. firstValue, lastValue, stepValue, length: LONGINT;
  3560. rangeExpression: SyntaxTree.RangeExpression;
  3561. BEGIN
  3562. IF indexListItem.type = SyntaxTree.invalidType THEN
  3563. (* error already handled *)
  3564. indexDesignator.parameters.AddExpression(indexListItem)
  3565. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3566. indexDesignator.HasRange;
  3567. indexDesignator.HasTensorRange;
  3568. indexDesignator.parameters.AddExpression(indexListItem);
  3569. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3570. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3571. IndexCheck(indexListItem, sourceArray.length);
  3572. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3573. indexDesignator.parameters.AddExpression(indexListItem)
  3574. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3575. indexDesignator.HasRange;
  3576. (* if the range is given as an array range expression, check the validity of its components *)
  3577. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3578. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3579. first := rangeExpression.first;
  3580. last := rangeExpression.last;
  3581. step := rangeExpression.step;
  3582. (* perform static checks on range components *)
  3583. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3584. Error(indexListItem.position,"lower bound of array range too small")
  3585. END;
  3586. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3587. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3588. Error(indexListItem.position,"upper bound of array range too large")
  3589. END
  3590. END;
  3591. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3592. Error(indexListItem.position,"invalid step size")
  3593. END;
  3594. (* add conversions to size type *)
  3595. (* TODO: needed? *)
  3596. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3597. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3598. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3599. END;
  3600. IF indexDesignator.hasTensorRange THEN
  3601. (* the index designator's base type is a tensor: leave it as is *)
  3602. ELSE
  3603. (* append a new math array to the index designator's base type *)
  3604. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3605. IF ~error THEN
  3606. (*
  3607. (* optimization: calculate length of target array for static ranges *)
  3608. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3609. IF IsStaticallyOpenRange(rangeExpression) THEN
  3610. (* range is open ('*'): reuse source array length as target array length *)
  3611. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3612. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3613. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3614. IF lastValue = MAX(LONGINT) THEN
  3615. IF IsIntegerValue(sourceArray.length, length) THEN
  3616. lastValue := length - 1;
  3617. isStaticTargetArrayLength := TRUE
  3618. ELSE
  3619. isStaticTargetArrayLength := FALSE
  3620. END
  3621. ELSE
  3622. isStaticTargetArrayLength := TRUE
  3623. END;
  3624. IF isStaticTargetArrayLength THEN
  3625. (* calculate static target array length *)
  3626. IF firstValue > lastValue THEN
  3627. length := 0
  3628. ELSE
  3629. length := 1 + lastValue - firstValue;
  3630. IF length MOD stepValue = 0 THEN
  3631. length := length DIV stepValue
  3632. ELSE
  3633. length := length DIV stepValue + 1
  3634. END
  3635. END;
  3636. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3637. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3638. ASSERT(targetArray.form = SyntaxTree.Static)
  3639. END
  3640. END
  3641. END
  3642. *)
  3643. END;
  3644. SetIndexBaseType(indexDesignator, targetArray)
  3645. END;
  3646. indexDesignator.parameters.AddExpression(indexListItem)
  3647. ELSE
  3648. Error(position,"invalid index list item");
  3649. END;
  3650. END AppendMathIndex;
  3651. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3652. VAR parameters: SyntaxTree.ExpressionList;
  3653. BEGIN
  3654. parameters := index.parameters;
  3655. IF (expression.type = NIL) THEN
  3656. Error(position, "invalid index");
  3657. ELSIF IsIntegerType(expression.type.resolved) THEN
  3658. IF over IS SyntaxTree.ArrayType THEN
  3659. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3660. ELSIF over IS SyntaxTree.StringType THEN
  3661. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3662. END;
  3663. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3664. parameters.AddExpression(expression);
  3665. ELSE
  3666. Error(position, "invalid index");
  3667. END;
  3668. END AppendIndex;
  3669. (** convert an expression to math array type
  3670. if expression is of math array type: return expression itself
  3671. if expression is of array-structured object type: return an index operator call on it
  3672. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3673. otherwise: return invalid expression
  3674. **)
  3675. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3676. VAR
  3677. result: SyntaxTree.Expression;
  3678. mathArrayType: SyntaxTree.MathArrayType;
  3679. BEGIN
  3680. IF expression.type = NIL THEN
  3681. result := SyntaxTree.invalidExpression
  3682. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3683. (* expression of math array type *)
  3684. result := expression
  3685. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3686. (* expression of array-structured object type *)
  3687. mathArrayType := MathArrayStructureOfType(expression.type);
  3688. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3689. ELSE
  3690. result := SyntaxTree.invalidExpression
  3691. END;
  3692. RETURN result
  3693. END ConvertToMathArray;
  3694. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3695. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3696. VAR
  3697. result: SyntaxTree.ExpressionList;
  3698. i: LONGINT;
  3699. BEGIN
  3700. result := SyntaxTree.NewExpressionList();
  3701. FOR i := 1 TO itemCount DO
  3702. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3703. END;
  3704. RETURN result
  3705. END ListOfOpenRanges;
  3706. (** create a procedure call designator for an index operator call on an array-structured object type
  3707. - use given index list as actual parameters
  3708. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3709. **)
  3710. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3711. VAR
  3712. operator: SyntaxTree.Operator;
  3713. expression: SyntaxTree.Expression;
  3714. actualParameters, tempList: SyntaxTree.ExpressionList;
  3715. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3716. result, tempDesignator: SyntaxTree.Designator;
  3717. recordType: SyntaxTree.RecordType;
  3718. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3719. i, hashValue, indexListSize, indexListKind: LONGINT;
  3720. castReturnType: SyntaxTree.MathArrayType;
  3721. BEGIN
  3722. ASSERT(IsArrayStructuredObjectType(left.type));
  3723. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3724. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3725. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3726. indexListSize := indexList.Length();
  3727. indexListKind := 0;
  3728. containsNonRange := FALSE;
  3729. FOR i := 0 TO indexList.Length() - 1 DO
  3730. indexListKind := indexListKind * 2;
  3731. expression := indexList.GetExpression(i);
  3732. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3733. INC(indexListKind)
  3734. ELSE
  3735. containsNonRange := TRUE
  3736. END
  3737. END;
  3738. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3739. (* select applicable index operator
  3740. - try to look up optimal index operator
  3741. - if not present, use operator on ranges
  3742. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3743. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3744. *)
  3745. usesGeneralOperator := FALSE;
  3746. IF rhs # NIL THEN
  3747. (* write operator *)
  3748. IF hashValue = -1 THEN
  3749. operator := NIL
  3750. ELSE
  3751. operator := recordType.arrayAccessOperators.write[hashValue];
  3752. END;
  3753. IF operator = NIL THEN
  3754. usesPureRangeOperator := TRUE;
  3755. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3756. operator := recordType.arrayAccessOperators.generalWrite;
  3757. usesGeneralOperator := TRUE
  3758. ELSE
  3759. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3760. operator := recordType.arrayAccessOperators.write[hashValue];
  3761. END
  3762. END
  3763. ELSE
  3764. (* read operator *)
  3765. IF hashValue = -1 THEN
  3766. operator := NIL
  3767. ELSE
  3768. operator := recordType.arrayAccessOperators.read[hashValue];
  3769. END;
  3770. IF operator = NIL THEN
  3771. usesPureRangeOperator := TRUE;
  3772. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3773. operator := recordType.arrayAccessOperators.generalRead;
  3774. usesGeneralOperator := TRUE
  3775. ELSE
  3776. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3777. operator := recordType.arrayAccessOperators.read[hashValue];
  3778. END
  3779. END
  3780. END;
  3781. IF operator = NIL THEN
  3782. Error(position, "call of undeclared [] operator");
  3783. result := SyntaxTree.invalidDesignator;
  3784. ELSE
  3785. (* determine if reshaping is needed *)
  3786. needsReshaping := containsNonRange & usesPureRangeOperator;
  3787. (* import OCArrayBase if reshaping is needed *)
  3788. IF needsReshaping & ~arrayBaseImported THEN
  3789. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3790. arrayBaseImported := TRUE
  3791. END;
  3792. (* add the index list item to the list of actual parameters
  3793. - for general operators: add a single inline array containing the index list items as parameter
  3794. - otherwise: add all index list items as individual parameters
  3795. *)
  3796. actualParameters := SyntaxTree.NewExpressionList();
  3797. IF usesGeneralOperator THEN
  3798. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3799. END;
  3800. FOR i := 0 TO indexListSize - 1 DO
  3801. expression := indexList.GetExpression(i);
  3802. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3803. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3804. tempList := SyntaxTree.NewExpressionList();
  3805. tempList.AddExpression(expression);
  3806. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3807. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3808. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3809. END;
  3810. IF usesGeneralOperator THEN
  3811. tempMathArrayExpression.elements.AddExpression(expression);
  3812. ELSE
  3813. actualParameters.AddExpression(expression)
  3814. END
  3815. END;
  3816. IF usesGeneralOperator THEN
  3817. actualParameters.AddExpression(tempMathArrayExpression)
  3818. END;
  3819. IF rhs # NIL THEN
  3820. (* add actual parameter for RHS *)
  3821. IF needsReshaping THEN
  3822. (* reshape using OCArrayBase.ExpandDimensions *)
  3823. tempList := SyntaxTree.NewExpressionList();
  3824. (* source array *)
  3825. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3826. tempList.AddExpression(rhs);
  3827. ELSE
  3828. (* convert scalar to one-dimensional array *)
  3829. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3830. tempMathArrayExpression.elements.AddExpression(rhs);
  3831. tempList.AddExpression(tempMathArrayExpression)
  3832. END;
  3833. (* list of kept dimensions *)
  3834. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3835. FOR i := 0 TO indexListSize - 1 DO
  3836. expression := indexList.GetExpression(i);
  3837. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3838. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3839. ELSE
  3840. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3841. END
  3842. END;
  3843. tempList.AddExpression(tempMathArrayExpression);
  3844. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3845. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3846. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3847. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3848. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3849. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3850. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3851. expression.SetType(castReturnType);
  3852. ELSE
  3853. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3854. END;
  3855. actualParameters.AddExpression(expression)
  3856. ELSE
  3857. actualParameters.AddExpression(rhs)
  3858. END
  3859. END;
  3860. (* add dereference operator and create procedure call designator *)
  3861. ASSERT(left IS SyntaxTree.Designator);
  3862. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3863. ASSERT(expression IS SyntaxTree.Designator);
  3864. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3865. IF (rhs = NIL) & needsReshaping THEN
  3866. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3867. tempList := SyntaxTree.NewExpressionList();
  3868. FOR i := 0 TO indexList.Length() - 1 DO
  3869. expression := indexList.GetExpression(i);
  3870. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3871. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3872. ELSE
  3873. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3874. END
  3875. END;
  3876. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3877. END;
  3878. IF rhs = NIL THEN
  3879. (* special rule: index read operator calls are considered to be assignable *)
  3880. result.SetAssignable(TRUE)
  3881. END;
  3882. (* put information about this index operator call into the resulting designator *)
  3883. result.SetRelatedAsot(left);
  3884. result.SetRelatedIndexList(indexList)
  3885. END;
  3886. RETURN result
  3887. END NewIndexOperatorCall;
  3888. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3889. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3890. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3891. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3892. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3893. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3894. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3895. CONST trace = FALSE;
  3896. BEGIN
  3897. IF trace THEN
  3898. FOR i := 0 TO actualParameters.Length()-1 DO
  3899. Printout.Info("par", actualParameters.GetExpression(i));
  3900. END;
  3901. END;
  3902. operator := scope.firstOperator;
  3903. WHILE(operator # NIL) DO
  3904. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3905. procedureType := operator.type(SyntaxTree.ProcedureType);
  3906. distance := Distance(system, procedureType,actualParameters);
  3907. IF trace THEN Printout.Info("check op ",operator) END;
  3908. IF distance < bestDistance THEN
  3909. IF trace THEN Printout.Info("taken op",operator) END;
  3910. bestDistance := distance;
  3911. bestOperator := operator;
  3912. END;
  3913. END;
  3914. operator := operator.nextOperator;
  3915. END;
  3916. END FindInScope;
  3917. BEGIN
  3918. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3919. IF oper = 0 THEN (* index *)
  3920. identifier := SyntaxTree.NewIdentifier("[]");
  3921. ELSE
  3922. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3923. END;
  3924. WHILE (recordType # NIL) DO
  3925. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3926. recordType := recordType.GetBaseRecord();
  3927. END;
  3928. RETURN bestOperator
  3929. END FindOperator;
  3930. BEGIN
  3931. type := left.type.resolved;
  3932. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3933. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3934. actualParameters := SyntaxTree.NewExpressionList();
  3935. IF parameters # NIL THEN
  3936. FOR i := 0 TO parameters.Length()-1 DO
  3937. expression := ResolveExpression(parameters.GetExpression(i));
  3938. actualParameters.AddExpression(expression);
  3939. END;
  3940. END;
  3941. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3942. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3943. IF op # NIL THEN
  3944. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3945. ASSERT(expression IS SyntaxTree.Designator);
  3946. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3947. result.SetRelatedAsot(left);
  3948. result.SetRelatedIndexList(parameters);
  3949. (* check if write operator exists, for var parameters *)
  3950. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3951. actualParameters := SyntaxTree.NewExpressionList();
  3952. FOR i := 0 TO parameters.Length()-1 DO
  3953. expression := ResolveExpression(parameters.GetExpression(i));
  3954. actualParameters.AddExpression(expression);
  3955. END;
  3956. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3957. actualParameters.AddExpression(rhs);
  3958. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3959. IF op = NIL THEN rhs := NIL END;
  3960. END;
  3961. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3962. ELSE
  3963. result := NIL;
  3964. END;
  3965. RETURN result;
  3966. END NewObjectOperatorCall;
  3967. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3968. 1. convert bracket designator chains into a single one that contains separators
  3969. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3970. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3971. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3972. - if an array or math array is indexed over, create index designator
  3973. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3974. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3975. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3976. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3977. - if an array-structured object type is indexed over, create procedure call designator
  3978. e.g.: a[x, y] -> a^."[]"(x, y)
  3979. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3980. - a[i, *] = a[i][*]
  3981. - a[*, i] # a[*][i]
  3982. Because:
  3983. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3984. - 'i-th column' = a[*, i]
  3985. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3986. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3987. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3988. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3989. **)
  3990. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3991. VAR
  3992. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3993. indexDesignator: SyntaxTree.IndexDesignator;
  3994. designator: SyntaxTree.Designator;
  3995. type: SyntaxTree.Type;
  3996. recordType: SyntaxTree.RecordType;
  3997. expression, rhs: SyntaxTree.Expression;
  3998. indexList: SyntaxTree.ExpressionList;
  3999. i: LONGINT;
  4000. hasError, done: BOOLEAN;
  4001. PROCEDURE FinalizeIndexDesignator;
  4002. BEGIN
  4003. IF indexDesignator # NIL THEN
  4004. (* the end of a tensor has been reached: *)
  4005. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4006. SetIndexBaseType(indexDesignator, type);
  4007. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4008. designator := indexDesignator;
  4009. type := designator.type.resolved;
  4010. indexDesignator := NIL;
  4011. ASSERT(SyntaxTree.Resolved IN type.state)
  4012. END
  4013. END FinalizeIndexDesignator;
  4014. BEGIN
  4015. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4016. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4017. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4018. (* copy all index list entries including a separator to the left bracket designator *)
  4019. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4020. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4021. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4022. END;
  4023. (* propagate the related RHS *)
  4024. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4025. (* only resolve left bracket designator and use as final result *)
  4026. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4027. ELSE
  4028. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4029. designator := ResolveDesignator(bracketDesignator.left);
  4030. type := designator.type.resolved;
  4031. indexDesignator := NIL;
  4032. (*!!! clean up *)
  4033. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4034. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4035. IF resolvedExpression = NIL THEN
  4036. Error(bracketDesignator.position,"undefined operator");
  4037. resolvedExpression := SyntaxTree.invalidDesignator
  4038. END;
  4039. RETURN;
  4040. END;
  4041. i := 0;
  4042. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4043. expression := bracketDesignator.parameters.GetExpression(i);
  4044. expression := ResolveExpression(expression);
  4045. bracketDesignator.parameters.SetExpression(i, expression);
  4046. IF expression = SyntaxTree.indexListSeparator THEN
  4047. (* finalize an existing index designator if needed *)
  4048. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4049. INC(i)
  4050. ELSE
  4051. (* do auto-dereferencing if needed *)
  4052. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4053. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4054. & (i=0)*)
  4055. THEN
  4056. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4057. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4058. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4059. designator := SyntaxTree.invalidDesignator;
  4060. type := SyntaxTree.invalidType
  4061. ELSE
  4062. FinalizeIndexDesignator;
  4063. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4064. type := designator.type.resolved
  4065. END
  4066. END;
  4067. (* create a new index designator, if needed *)
  4068. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4069. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4070. indexDesignator.SetAssignable(designator.assignable);
  4071. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4072. (* designator := indexDesignator *)
  4073. END;
  4074. IF type = SyntaxTree.invalidType THEN
  4075. (* error already handled *)
  4076. INC(i)
  4077. ELSIF type IS SyntaxTree.ArrayType THEN
  4078. (* indexing over an array *)
  4079. ASSERT(indexDesignator # NIL);
  4080. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4081. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4082. INC(i)
  4083. ELSIF type IS SyntaxTree.StringType THEN
  4084. (* indexing over an array *)
  4085. ASSERT(indexDesignator # NIL);
  4086. AppendIndex(expression.position, indexDesignator, expression, type);
  4087. type := type(SyntaxTree.StringType).baseType.resolved;
  4088. INC(i)
  4089. ELSIF type IS SyntaxTree.MathArrayType THEN
  4090. (* indexing over a math array *)
  4091. ASSERT(indexDesignator # NIL);
  4092. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4093. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4094. INC(i)
  4095. ELSIF IsArrayStructuredObjectType(type) THEN
  4096. (* indexing over ASOTs *)
  4097. FinalizeIndexDesignator;
  4098. ASSERT(type IS SyntaxTree.PointerType);
  4099. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4100. (*
  4101. - collect index list items from bracket designator that belong to ASOT
  4102. - check for errors
  4103. *)
  4104. indexList := SyntaxTree.NewExpressionList();
  4105. hasError := FALSE;
  4106. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4107. (* indexing over tensor ASOT:
  4108. - stop at index list end or separator
  4109. - dimensionality is given by number of index list items
  4110. *)
  4111. done := FALSE;
  4112. WHILE ~done DO
  4113. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4114. done := TRUE;
  4115. ELSE
  4116. expression := bracketDesignator.parameters.GetExpression(i);
  4117. IF expression = SyntaxTree.indexListSeparator THEN
  4118. done := TRUE;
  4119. ELSE
  4120. expression := ResolveExpression(expression);
  4121. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4122. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4123. hasError := TRUE
  4124. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4125. Error(expression.position, "integer or range expected");
  4126. expression := SyntaxTree.invalidExpression;
  4127. hasError := TRUE
  4128. END;
  4129. indexList.AddExpression(expression)
  4130. END;
  4131. INC(i)
  4132. END
  4133. END
  4134. ELSE
  4135. (* indexing over non-tensor ASOT:
  4136. - ignore separators
  4137. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4138. *)
  4139. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4140. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4141. expression := bracketDesignator.parameters.GetExpression(i);
  4142. ELSE
  4143. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4144. END;
  4145. IF expression # SyntaxTree.indexListSeparator THEN
  4146. expression := ResolveExpression(expression);
  4147. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4148. Error(expression.position, "integer or range expected");
  4149. expression := SyntaxTree.invalidExpression;
  4150. hasError := TRUE
  4151. END;
  4152. indexList.AddExpression(expression)
  4153. END;
  4154. INC(i)
  4155. END;
  4156. END;
  4157. IF hasError THEN
  4158. designator := SyntaxTree.invalidDesignator;
  4159. type := SyntaxTree.invalidType;
  4160. ELSE
  4161. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4162. and the last entry in the index list belongs to the array-structured object type in question.
  4163. E.g.: for a 2-dimensional array-structured object type:
  4164. - 'lhs := asot[1, 2]' -> read mode
  4165. - 'asot[1, 2] := rhs' -> write mode
  4166. - 'asot[1, 2, 3] := rhs' -> read mode
  4167. *)
  4168. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4169. rhs := bracketDesignator.relatedRhs
  4170. ELSE
  4171. rhs := NIL
  4172. END;
  4173. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4174. type := designator.type
  4175. END
  4176. ELSE
  4177. Error(expression.position,"indexing over non-array type");
  4178. designator := SyntaxTree.invalidDesignator;
  4179. type := SyntaxTree.invalidType;
  4180. INC(i)
  4181. END
  4182. END
  4183. END;
  4184. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4185. resolvedExpression := designator
  4186. END
  4187. END VisitBracketDesignator;
  4188. (** check and resolve expression list
  4189. - resolve each expression in an expression list
  4190. - returns true if and only if all statements could have successfully been resolved
  4191. **)
  4192. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4193. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4194. BEGIN
  4195. result := TRUE;
  4196. FOR i := 0 TO expressionList.Length()-1 DO
  4197. expression := ResolveExpression(expressionList.GetExpression(i));
  4198. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4199. expressionList.SetExpression(i,expression);
  4200. END;
  4201. RETURN result
  4202. END ExpressionList;
  4203. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4204. BEGIN
  4205. type := type.resolved;
  4206. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4207. RETURN TRUE
  4208. ELSIF system.CanPassInRegister # NIL THEN
  4209. RETURN system.CanPassInRegister(type);
  4210. ELSE
  4211. RETURN FALSE
  4212. END;
  4213. END CanPassInRegister;
  4214. (** return procedure call designator left(actualParameters)
  4215. - check realtime procedure call in realtime procedure
  4216. - check number of parameters
  4217. - check parameter compatibility
  4218. return invalidDesignator if error
  4219. **)
  4220. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4221. VAR result: SyntaxTree.Designator;
  4222. numberFormalParameters, numberActualParameters: LONGINT;
  4223. formalType: SyntaxTree.ProcedureType;
  4224. formalParameter: SyntaxTree.Parameter;
  4225. actualParameter: SyntaxTree.Expression;
  4226. i: LONGINT;
  4227. self: SyntaxTree.Expression;
  4228. BEGIN
  4229. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4230. result := SyntaxTree.invalidDesignator;
  4231. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4232. numberFormalParameters := formalType.numberParameters;
  4233. numberActualParameters := actualParameters.Length();
  4234. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4235. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4236. END;
  4237. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4238. self := left.left;
  4239. IF (self # NIL) & ~IsVariable(self) THEN
  4240. Error(self.position, "Non-variable expression on variable receiver");
  4241. END;
  4242. END;
  4243. IF ~ExpressionList(actualParameters) THEN
  4244. result := SyntaxTree.invalidDesignator
  4245. ELSE
  4246. IF numberActualParameters <= numberFormalParameters THEN
  4247. formalParameter := formalType.firstParameter;
  4248. FOR i := 0 TO numberActualParameters-1 DO
  4249. actualParameter := actualParameters.GetExpression(i);
  4250. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4251. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4252. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4253. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4254. ELSE
  4255. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4256. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4257. END;
  4258. actualParameters.SetExpression(i,actualParameter);
  4259. END;
  4260. formalParameter := formalParameter.nextParameter;
  4261. END;
  4262. WHILE (formalParameter # NIL) DO
  4263. IF formalParameter.defaultValue # NIL THEN
  4264. actualParameters.AddExpression(formalParameter.defaultValue);
  4265. formalParameter := formalParameter.nextParameter
  4266. ELSE
  4267. Error(position, "less actual than formal parameters");
  4268. formalParameter := NIL;
  4269. END;
  4270. END;
  4271. ELSE
  4272. Error(position, "more actual than formal parameters")
  4273. END;
  4274. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4275. result.SetAssignable(FALSE);
  4276. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4277. END;
  4278. RETURN result
  4279. END NewProcedureCallDesignator;
  4280. (**
  4281. builtin call designator generated in VisitParameterDesignator
  4282. -> nothing to be resolved
  4283. **)
  4284. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4285. BEGIN
  4286. resolvedExpression := x;
  4287. END VisitTypeGuardDesignator;
  4288. (**
  4289. builtin call designator generated in VisitParameterDesignator
  4290. -> nothing to be resolved
  4291. **)
  4292. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4293. BEGIN
  4294. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4295. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4296. ASSERT(resolvedExpression.type # NIL);
  4297. ELSIF ExpressionList(x.parameters) THEN
  4298. resolvedExpression := x;
  4299. END;
  4300. END VisitBuiltinCallDesignator;
  4301. (**
  4302. procedure call designator generated in VisitParameterDesignator
  4303. -> nothing to be resolved
  4304. **)
  4305. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4306. BEGIN
  4307. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4308. resolvedExpression := x;
  4309. END VisitProcedureCallDesignator;
  4310. (** return true if x is a variable else return false and report error **)
  4311. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4312. VAR result: BOOLEAN;
  4313. BEGIN
  4314. result := TRUE;
  4315. IF x = SyntaxTree.invalidExpression THEN
  4316. result := FALSE;
  4317. ELSIF ~IsVariable(x) THEN
  4318. Error(x.position,"non variable expression");
  4319. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4320. result := FALSE;
  4321. END;
  4322. RETURN result
  4323. END CheckVariable;
  4324. (**
  4325. if expression x is of basic type then return true else report error and return false
  4326. **)
  4327. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4328. VAR result: BOOLEAN;
  4329. BEGIN
  4330. result := FALSE;
  4331. IF x = SyntaxTree.invalidExpression THEN
  4332. ELSIF ~IsBasicType(x.type) THEN
  4333. Error(x.position,"is no basic type");
  4334. result := FALSE
  4335. ELSE result := TRUE
  4336. END;
  4337. RETURN result
  4338. END CheckBasicType;
  4339. (**
  4340. if expression x is of number type then return true else report error and return false
  4341. **)
  4342. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4343. VAR result: BOOLEAN;
  4344. BEGIN
  4345. result := FALSE;
  4346. IF x = SyntaxTree.invalidExpression THEN
  4347. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4348. Error(x.position,"is non number type");
  4349. ELSE result := TRUE
  4350. END;
  4351. RETURN result
  4352. END CheckNumberType;
  4353. (**
  4354. if expression x is of number or size type but not complex then return true else report error and return false
  4355. **)
  4356. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4357. VAR result: BOOLEAN;
  4358. BEGIN
  4359. result := FALSE;
  4360. IF x = SyntaxTree.invalidExpression THEN
  4361. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4362. Error(x.position,"is complex type");
  4363. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4364. Error(x.position,"is non number type");
  4365. ELSE result := TRUE
  4366. END;
  4367. RETURN result
  4368. END CheckNonComplexNumberSizeType;
  4369. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4370. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4371. BEGIN
  4372. result := FALSE; type := x.type.resolved;
  4373. IF x = SyntaxTree.invalidExpression THEN
  4374. 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
  4375. TRACE(type.sizeInBits);
  4376. TRACE(system.addressType.sizeInBits);
  4377. Error(x.position,"is no address type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckAddressType;
  4382. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4384. BEGIN
  4385. result := FALSE; type := x.type.resolved;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4388. Error(x.position,"is no size type");
  4389. ELSE result := TRUE
  4390. END;
  4391. RETURN result
  4392. END CheckSizeType;
  4393. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4394. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4395. BEGIN
  4396. result := FALSE; type := x.type.resolved;
  4397. IF x = SyntaxTree.invalidExpression THEN
  4398. ELSIF ~(type IS SyntaxTree.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
  4399. Error(x.position,"is no object type");
  4400. ELSE result := TRUE
  4401. END;
  4402. RETURN result
  4403. END CheckObjectType;
  4404. (**
  4405. if expression x is of integer type then return true else report error and return false
  4406. **)
  4407. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4408. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4409. BEGIN
  4410. result := FALSE; type := x.type.resolved;
  4411. IF x = SyntaxTree.invalidExpression THEN
  4412. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4413. Error(x.position,"is no integer type");
  4414. ELSE result := TRUE
  4415. END;
  4416. RETURN result
  4417. END CheckIntegerType;
  4418. (**
  4419. if expression x is of character type then return true else report error and return false
  4420. **)
  4421. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4422. VAR result: BOOLEAN;
  4423. BEGIN
  4424. result := FALSE;
  4425. IF x = SyntaxTree.invalidExpression THEN
  4426. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4427. Error(x.position,"is no character type");
  4428. ELSE result := TRUE
  4429. END;
  4430. RETURN result
  4431. END CheckCharacterType;
  4432. (**
  4433. if expression x is of real type then return true else report error and return false
  4434. **)
  4435. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4436. VAR result: BOOLEAN;
  4437. BEGIN
  4438. result := FALSE;
  4439. IF x = SyntaxTree.invalidExpression THEN
  4440. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4441. Error(x.position,"is no float type");
  4442. ELSE result := TRUE
  4443. END;
  4444. RETURN result
  4445. END CheckRealType;
  4446. (**
  4447. if expression x is of range type then return true else report error and return false
  4448. **)
  4449. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4450. VAR result: BOOLEAN;
  4451. BEGIN
  4452. result := FALSE;
  4453. IF x = SyntaxTree.invalidExpression THEN
  4454. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4455. Error(x.position,"is no range type");
  4456. ELSE result := TRUE
  4457. END;
  4458. RETURN result
  4459. END CheckRangeType;
  4460. (**
  4461. if expression x is of boolean type then return true else report error and return false
  4462. **)
  4463. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4464. VAR result: BOOLEAN;
  4465. BEGIN
  4466. result := FALSE;
  4467. IF x = SyntaxTree.invalidExpression THEN
  4468. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4469. Error(x.position,"is no boolean type");
  4470. ELSE result := TRUE
  4471. END;
  4472. RETURN result
  4473. END CheckBooleanType;
  4474. (**
  4475. if expression x is of set type then return true else report error and return false
  4476. **)
  4477. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4478. VAR result: BOOLEAN;
  4479. BEGIN
  4480. result := FALSE;
  4481. IF x = SyntaxTree.invalidExpression THEN
  4482. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4483. Error(x.position,"is no set type");
  4484. ELSE result := TRUE
  4485. END;
  4486. RETURN result
  4487. END CheckSetType;
  4488. (**
  4489. if expression x is of string or array of character type then return true else report error and return false
  4490. **)
  4491. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4492. VAR result: BOOLEAN;
  4493. BEGIN
  4494. result := FALSE;
  4495. IF x = SyntaxTree.invalidExpression THEN
  4496. ELSIF ~IsStringType(x.type.resolved) THEN
  4497. Error(x.position,"is no string type");
  4498. ELSE result := TRUE
  4499. END;
  4500. RETURN result
  4501. END CheckStringType;
  4502. (**
  4503. if expression x is a type declaration type return true else report error and return false
  4504. **)
  4505. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4506. VAR result: BOOLEAN;
  4507. BEGIN
  4508. result := FALSE;
  4509. IF x = SyntaxTree.invalidExpression THEN
  4510. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4511. Error(x.position,"is not a type declaration");
  4512. ELSE result := TRUE
  4513. END;
  4514. RETURN result
  4515. END CheckTypeDeclarationType;
  4516. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4517. VAR result: BOOLEAN;
  4518. BEGIN
  4519. result := FALSE;
  4520. IF x = SyntaxTree.invalidExpression THEN
  4521. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4522. result := TRUE;
  4523. value := x.resolved(SyntaxTree.IntegerValue).value;
  4524. ELSE
  4525. Error(x.position,"expression is not an integer constant");
  4526. END;
  4527. RETURN result;
  4528. END CheckIntegerValue;
  4529. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4530. VAR result: BOOLEAN;
  4531. BEGIN
  4532. result := FALSE;
  4533. IF x = SyntaxTree.invalidExpression THEN
  4534. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4535. result := TRUE;
  4536. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4537. ELSE
  4538. Error(x.position,"expression is not an integer constant");
  4539. END;
  4540. RETURN result;
  4541. END CheckStringValue;
  4542. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4543. BEGIN
  4544. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4545. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4546. ELSE
  4547. RETURN FALSE
  4548. END;
  4549. END IsUnsignedValue;
  4550. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4551. BEGIN
  4552. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4553. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4554. ELSE
  4555. RETURN FALSE
  4556. END
  4557. END IsAddressValue;
  4558. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4559. BEGIN
  4560. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4561. END IsAddressExpression;
  4562. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4563. BEGIN
  4564. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4565. END IsSizeExpression;
  4566. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4567. VAR result: BOOLEAN;
  4568. BEGIN
  4569. result := FALSE;
  4570. IF x = SyntaxTree.invalidExpression THEN
  4571. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4572. result := TRUE;
  4573. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4574. ELSE
  4575. Error(x.position,"expression is not an integer constant");
  4576. END;
  4577. RETURN result;
  4578. END CheckEnumerationValue;
  4579. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4585. result := TRUE;
  4586. value := x.resolved(SyntaxTree.CharacterValue).value;
  4587. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4588. result := TRUE;
  4589. value := x.resolved(SyntaxTree.StringValue).value[0];
  4590. ELSE
  4591. Error(x.position,"expression is not a character constant");
  4592. END;
  4593. RETURN result;
  4594. END CheckCharacterValue;
  4595. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4596. VAR result: BOOLEAN;
  4597. BEGIN
  4598. result := FALSE;
  4599. IF x = SyntaxTree.invalidExpression THEN
  4600. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4601. value := x.resolved(SyntaxTree.IntegerValue).value;
  4602. IF (value > 0) OR includeZero & (value = 0) THEN
  4603. result := TRUE;
  4604. ELSE
  4605. Error(x.position,"integer is not positive");
  4606. END
  4607. ELSE
  4608. Error(x.position,"expression is not an integer constant");
  4609. END;
  4610. RETURN result;
  4611. END CheckPositiveIntegerValue;
  4612. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4613. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4614. BEGIN
  4615. result := FALSE;
  4616. IF x = SyntaxTree.invalidExpression THEN
  4617. ELSE
  4618. type := x.type.resolved;
  4619. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4620. portType := type(SyntaxTree.PortType);
  4621. result := TRUE
  4622. ELSE
  4623. Error(x.position,"no port type");
  4624. END;
  4625. END;
  4626. RETURN result
  4627. END CheckPortType;
  4628. (* move to builtin procedure call statement ?
  4629. remove builtin procedure call designator ?
  4630. *)
  4631. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4632. VAR
  4633. numberActualParameters,numberFormalParameters: LONGINT;
  4634. formalParameter: SyntaxTree.Parameter;
  4635. actualParameter: SyntaxTree.Expression;
  4636. procedureType: SyntaxTree.ProcedureType;
  4637. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4638. inPort, outPort: SyntaxTree.PortType;
  4639. constructor: SyntaxTree.Procedure;
  4640. type0,type1,type2: SyntaxTree.Type;
  4641. type,base,parameterType: SyntaxTree.Type;
  4642. arrayType: SyntaxTree.ArrayType;
  4643. i,i0,i1: LONGINT;
  4644. r,r0,r1,im: LONGREAL;
  4645. c: CHAR;
  4646. id: LONGINT;
  4647. b: BOOLEAN;
  4648. first: LONGINT;
  4649. mathArrayType: SyntaxTree.MathArrayType;
  4650. customBuiltin: SyntaxTree.CustomBuiltin;
  4651. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4652. VAR resultB: BOOLEAN;
  4653. BEGIN
  4654. IF numberActualParameters < from THEN
  4655. Error(position, "less actual than formal parameters");
  4656. result := SyntaxTree.invalidExpression;
  4657. resultB := FALSE;
  4658. ELSIF numberActualParameters > to THEN
  4659. Error(position, "more actual than formal parameters");
  4660. result := SyntaxTree.invalidExpression;
  4661. resultB := FALSE;
  4662. ELSE
  4663. resultB := TRUE;
  4664. END;
  4665. RETURN resultB
  4666. END CheckArity;
  4667. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4668. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4669. BEGIN
  4670. WHILE modifier # NIL DO
  4671. symbol := cellType.FindProperty(modifier.identifier);
  4672. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4673. propertyType := symbol.type.resolved;
  4674. modifierType := modifier.expression.type.resolved;
  4675. IF ~CompatibleTo(system, modifierType, propertyType) &
  4676. ~(
  4677. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4678. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4679. Error(modifier.position,"incompatible to cell property");
  4680. END;
  4681. ELSE
  4682. Error(modifier.position, "undefined property");
  4683. END;
  4684. modifier := modifier.nextModifier;
  4685. END;
  4686. END CheckModifiers;
  4687. BEGIN
  4688. type := NIL; result := NIL;
  4689. type0 := NIL; type1 := NIL; type2 := NIL;
  4690. numberActualParameters := actualParameters.Length();
  4691. IF numberActualParameters>0 THEN
  4692. parameter0 := actualParameters.GetExpression(0);
  4693. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4694. Error(parameter0.position,"forbidden type-less argument");
  4695. result := SyntaxTree.invalidExpression
  4696. END
  4697. END;
  4698. IF numberActualParameters >1 THEN
  4699. parameter1 := actualParameters.GetExpression(1);
  4700. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4701. ELSE
  4702. Error(parameter1.position,"forbidden type-less argument");
  4703. result := SyntaxTree.invalidExpression
  4704. END
  4705. END;
  4706. IF numberActualParameters >2 THEN
  4707. parameter2 := actualParameters.GetExpression(2);
  4708. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4709. ELSE
  4710. Error(parameter2.position,"forbidden type-less argument");
  4711. result := SyntaxTree.invalidExpression
  4712. END
  4713. END;
  4714. IF returnType # NIL THEN
  4715. id := Global.New;
  4716. result := NIL;
  4717. ELSE
  4718. id := builtin.id;
  4719. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4720. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4721. END;
  4722. END;
  4723. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4724. ELSIF result # NIL THEN type := result.type (* operator *)
  4725. ELSE
  4726. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4727. result(SyntaxTree.Designator).SetLeft(left);
  4728. IF returnType # NIL THEN
  4729. type := returnType;
  4730. END;
  4731. (* ---- ASSERT ----- *)
  4732. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4733. IF CheckBooleanType(parameter0) THEN
  4734. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4735. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4736. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4737. *)
  4738. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4739. Error(position, "assert failed");
  4740. END;
  4741. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4742. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4743. rules imposed by the architecture / current runtime
  4744. *)
  4745. END;
  4746. END;
  4747. (* ---- COPY ----- *)
  4748. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4749. IF~IsStringType(type0) THEN
  4750. Error(parameter0.position,"no string type");
  4751. END;
  4752. IF ~IsStringType(type1) THEN
  4753. Error(parameter1.position,"no string type");
  4754. ELSIF CheckVariable(parameter1) THEN
  4755. IF (type0 IS SyntaxTree.StringType) THEN
  4756. arrayType := type1(SyntaxTree.ArrayType);
  4757. IF arrayType.form = SyntaxTree.Static THEN
  4758. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4759. Error(position, "destination length smaller than source length")
  4760. END;
  4761. END;
  4762. END;
  4763. END;
  4764. (* ---- INC, DEC----- *)
  4765. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4766. IF numberActualParameters = 1 THEN
  4767. parameter1 :=Global.NewIntegerValue(system,position,1);
  4768. actualParameters.AddExpression(parameter1);
  4769. END;
  4770. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4771. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4772. Error(position, "incompatible increment");
  4773. ELSE
  4774. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4775. actualParameters.SetExpression(1,parameter1);
  4776. END;
  4777. END;
  4778. (* ---- EXCL, INCL----- *)
  4779. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4780. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4781. IF IsIntegerValue(parameter1,i0) THEN
  4782. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4783. Error(position, "parameter out of SET range")
  4784. END;
  4785. END;
  4786. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4787. actualParameters.SetExpression(1,parameter1);
  4788. END;
  4789. (* ---- HALT, SYSTEM.HALT ----- *)
  4790. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4791. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4792. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4793. rules imposed by the architecture / current runtime
  4794. *)
  4795. END;
  4796. (* ---- WAIT ----- *)
  4797. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4798. IF CheckObjectType(parameter0) THEN
  4799. END;
  4800. (* ---- NEW ----- *)
  4801. ELSIF (id = Global.New) THEN
  4802. IF returnType # NIL THEN
  4803. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4804. ELSE
  4805. first := 1;
  4806. END;
  4807. IF CheckArity(first,Infinity) THEN
  4808. IF currentIsRealtime THEN
  4809. Error(position, "forbidden new in realtime block");
  4810. END;
  4811. (* check constructor *)
  4812. IF (first =0) OR CheckVariable(parameter0) THEN
  4813. IF type0 IS SyntaxTree.PointerType THEN
  4814. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4815. ELSIF type0 IS SyntaxTree.CellType THEN
  4816. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4817. ELSE
  4818. Error(position, "forbidden new on value type");
  4819. END;
  4820. IF type0 IS SyntaxTree.ArrayType THEN
  4821. arrayType := type0(SyntaxTree.ArrayType);
  4822. IF arrayType.form = SyntaxTree.Static THEN
  4823. i := first
  4824. ELSIF arrayType.form = SyntaxTree.Open THEN
  4825. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4826. ELSE HALT(100)
  4827. END;
  4828. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4829. i := first;
  4830. REPEAT
  4831. actualParameter := actualParameters.GetExpression(i);
  4832. IF CheckSizeType(actualParameter) THEN
  4833. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4834. actualParameters.SetExpression(i,actualParameter);
  4835. END;
  4836. INC(i);
  4837. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4838. END;
  4839. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4840. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4841. IF constructor = NIL THEN
  4842. IF CheckArity(first,first) THEN END;
  4843. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4844. Error(position, "new on object with hidden constructor");
  4845. ELSE
  4846. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4847. numberFormalParameters := procedureType.numberParameters;
  4848. IF numberActualParameters-first <= numberFormalParameters THEN
  4849. formalParameter := procedureType.firstParameter;
  4850. FOR i := first TO numberActualParameters-1 DO
  4851. actualParameter := actualParameters.GetExpression(i);
  4852. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4853. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4854. ELSE
  4855. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4856. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4857. END;
  4858. actualParameters.SetExpression(i,actualParameter);
  4859. END;
  4860. formalParameter := formalParameter.nextParameter;
  4861. END;
  4862. WHILE (formalParameter # NIL) DO
  4863. IF formalParameter.defaultValue # NIL THEN
  4864. actualParameters.AddExpression(formalParameter.defaultValue);
  4865. formalParameter := formalParameter.nextParameter
  4866. ELSE
  4867. Error(position, "less actual than formal parameters");
  4868. formalParameter := NIL;
  4869. END;
  4870. END;
  4871. ELSE
  4872. Error(position, "more actual than formal parameters")
  4873. END;
  4874. END;
  4875. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4876. mathArrayType := type0(SyntaxTree.MathArrayType);
  4877. IF mathArrayType.form = SyntaxTree.Static THEN
  4878. Error(position, "new on static array");
  4879. ELSE
  4880. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4881. i0 := first+1; i1 := Infinity;
  4882. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4883. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4884. i1 := i0;
  4885. ELSE HALT(100);
  4886. END;
  4887. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4888. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4889. base := ArrayBase(type0,MAX(LONGINT));
  4890. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4891. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4892. IF ~CompatibleTo(system,type0,parameterType) THEN
  4893. Error(parameter0.position,"incompatible parameter in new");
  4894. result := SyntaxTree.invalidExpression;
  4895. ELSE
  4896. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4897. END;
  4898. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4899. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4900. IF ~CompatibleTo(system,type1,parameterType) THEN
  4901. Error(parameter1.position,"parameter incompatible to math array of longint");
  4902. result := SyntaxTree.invalidExpression;
  4903. ELSE
  4904. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4905. END;
  4906. ELSE
  4907. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4908. i := first;
  4909. REPEAT
  4910. actualParameter := actualParameters.GetExpression(i);
  4911. IF CheckSizeType(actualParameter) THEN
  4912. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4913. actualParameters.SetExpression(i,actualParameter);
  4914. END;
  4915. INC(i);
  4916. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4917. END;
  4918. END;
  4919. END;
  4920. ELSIF type0 IS SyntaxTree.CellType THEN
  4921. IF ~(currentIsCellNet) THEN
  4922. Error(position, "cell allocation outside activeCells ");
  4923. ELSE
  4924. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4925. IF (constructor = NIL) & CheckArity(1,1) THEN
  4926. (* ok *)
  4927. ELSE
  4928. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4929. numberFormalParameters := procedureType.numberParameters;
  4930. DEC(numberActualParameters);
  4931. IF numberActualParameters <= numberFormalParameters THEN
  4932. formalParameter := procedureType.firstParameter;
  4933. FOR i := first TO numberActualParameters DO
  4934. actualParameter := actualParameters.GetExpression(i);
  4935. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4936. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4937. ELSE
  4938. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4939. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4940. END;
  4941. actualParameters.SetExpression(i,actualParameter);
  4942. END;
  4943. formalParameter := formalParameter.nextParameter;
  4944. END;
  4945. WHILE (formalParameter # NIL) DO
  4946. IF formalParameter.defaultValue # NIL THEN
  4947. actualParameters.AddExpression(formalParameter.defaultValue);
  4948. formalParameter := formalParameter.nextParameter
  4949. ELSE
  4950. Error(position, "less actual than formal parameters");
  4951. formalParameter := NIL;
  4952. END;
  4953. END;
  4954. ELSE
  4955. Error(position, "more actual than formal parameters")
  4956. END;
  4957. END;
  4958. END;
  4959. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4960. activeCellsStatement := TRUE;
  4961. ELSE
  4962. Error(position, "cannot be allocated");
  4963. END;
  4964. END;
  4965. END;
  4966. (* ---- DISPOSE ----- *)
  4967. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4968. IF ~IsPointerType(parameter0.type) THEN
  4969. Error(parameter0.position,"is not a pointer")
  4970. ELSIF ~IsDisposable(parameter0.type) THEN
  4971. Error(parameter0.position,"is not disposable")
  4972. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4973. END
  4974. (* ---- GETPROCEDURE ----- *)
  4975. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4976. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4977. IF CheckVariable(parameter2) THEN
  4978. IF ~GetProcedureAllowed(parameter2.type) THEN
  4979. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4980. END;
  4981. END;
  4982. END;
  4983. (* ---- ABS ----- *)
  4984. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4985. (* note: ABS on complex numbers is done using overloading *)
  4986. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4987. type := type0;
  4988. IF IsIntegerValue(parameter0,i0) THEN
  4989. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4990. type := Global.GetIntegerType(system,ABS(i0));
  4991. ELSIF IsRealValue(parameter0,r) THEN
  4992. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4993. END;
  4994. ELSE
  4995. type := SyntaxTree.invalidType;
  4996. END;
  4997. (* ---- ASH, ASR ----- *)
  4998. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4999. type := type0;
  5000. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5001. (*
  5002. ConvertOperands(parameter0,parameter1); (* same type *)
  5003. *)
  5004. type := parameter0.type;
  5005. IF IsIntegerValue(parameter0,i0) THEN
  5006. IF IsIntegerValue(parameter1,i1) THEN
  5007. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5008. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5009. result := ResolveExpression(result);
  5010. type := Global.GetIntegerType(system,i0);
  5011. END;
  5012. END;
  5013. IF type.resolved.sizeInBits < 32 THEN
  5014. type := system.longintType;
  5015. END;
  5016. (*!compatibility with release, remove when resolved
  5017. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5018. *)
  5019. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5020. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5021. actualParameters.SetExpression(0,parameter0);
  5022. actualParameters.SetExpression(1,parameter1);
  5023. END;
  5024. (* ---- CAP ----- *)
  5025. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5026. type := system.characterType;
  5027. IF CheckCharacterType (parameter0) THEN
  5028. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5029. actualParameters.SetExpression(0,parameter0);
  5030. IF IsCharacterValue(parameter0,c) THEN
  5031. IF (c <= "z") & (c >= "a") THEN
  5032. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5033. ELSE
  5034. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5035. END;
  5036. END;
  5037. END;
  5038. (* ---- CHR ----- *)
  5039. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5040. IF id = Global.Chr THEN
  5041. type := system.characterType
  5042. ELSE
  5043. type := system.characterType32
  5044. END;
  5045. IF CheckIntegerType(parameter0) THEN
  5046. IF IsIntegerValue(parameter0,i0) THEN
  5047. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5048. result := ResolveExpression(result);
  5049. ELSE
  5050. (*
  5051. result := NewConversion(parameter0.position,parameter0,type);
  5052. *)
  5053. END;
  5054. END
  5055. (* ---- ENTIER ----- *)
  5056. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5057. type := system.longintType;
  5058. IF CheckRealType(parameter0) THEN
  5059. IF IsRealValue(parameter0,r) THEN
  5060. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5061. type := Global.GetIntegerType(system,ENTIER(r));
  5062. END
  5063. END;
  5064. (* ---- ENTIERH ----- *)
  5065. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5066. type := system.hugeintType;
  5067. IF CheckRealType(parameter0) THEN
  5068. IF IsRealValue(parameter0,r) THEN
  5069. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5070. END
  5071. END;
  5072. (* ---- LEN ----- *)
  5073. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5074. type := system.longintType;
  5075. base := type0;
  5076. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5077. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5078. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5079. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5080. Error(position, "forbidden len on unsafe pointer");
  5081. END;
  5082. type0 := base;
  5083. ELSE
  5084. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5085. type0 := parameter0.type.resolved;
  5086. actualParameters.SetExpression(0,parameter0);
  5087. base := type0;
  5088. END;
  5089. END;
  5090. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5091. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5092. IF i1 < 0 THEN
  5093. Error(position, "invalid dimension");
  5094. base := SyntaxTree.invalidType;
  5095. ELSE
  5096. base := ArrayBase(base,i1);
  5097. IF (base # NIL) & Indexable(base) THEN
  5098. ELSE
  5099. Error(position, "len on no array");
  5100. IF VerboseErrorMessage THEN
  5101. Printout.Info("base",base);
  5102. END;
  5103. base := SyntaxTree.invalidType;
  5104. END;
  5105. END;
  5106. IF numberActualParameters=2 THEN
  5107. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5108. actualParameters.SetExpression(1,parameter1);
  5109. ELSIF base IS SyntaxTree.MathArrayType THEN
  5110. Error(position, "missing dimension specification");
  5111. END;
  5112. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5113. IF base IS SyntaxTree.ArrayType THEN
  5114. arrayType := base(SyntaxTree.ArrayType);
  5115. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5116. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5117. result := Global.NewIntegerValue(system,position,i);
  5118. type := result.type;(* arrayType.length.type;*)
  5119. ASSERT(type # NIL);
  5120. END;
  5121. ELSIF base IS SyntaxTree.MathArrayType THEN
  5122. mathArrayType := base(SyntaxTree.MathArrayType);
  5123. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5124. result := Global.NewIntegerValue(system,position,i);
  5125. type := result.type;
  5126. (*
  5127. type := mathArrayType.length.type;
  5128. *)
  5129. ASSERT(type # NIL);
  5130. END;
  5131. END;
  5132. END;
  5133. ELSE
  5134. type := system.longintType;
  5135. END;
  5136. (* ---- FIRST ---- *)
  5137. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5138. type := system.longintType;
  5139. IF CheckRangeType(parameter0) THEN END;
  5140. result.SetAssignable(parameter0.assignable)
  5141. (* ---- LAST ---- *)
  5142. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5143. type := system.longintType;
  5144. IF CheckRangeType(parameter0) THEN END;
  5145. result.SetAssignable(parameter0.assignable)
  5146. (* ---- STEP ---- *)
  5147. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5148. type := system.longintType;
  5149. IF CheckRangeType(parameter0) THEN END;
  5150. result.SetAssignable(parameter0.assignable)
  5151. (* ---- RE ---- *)
  5152. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5153. IF CheckNumberType(parameter0) THEN
  5154. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5155. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5156. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5157. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5158. type := parameter0.type
  5159. ELSE
  5160. type := system.realType
  5161. END
  5162. END;
  5163. result.SetAssignable(parameter0.assignable)
  5164. (* ---- IM ---- *)
  5165. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5166. IF CheckNumberType(parameter0) THEN
  5167. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5168. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5169. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5170. ELSE
  5171. type := system.realType;
  5172. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5173. END
  5174. END;
  5175. result.SetAssignable(parameter0.assignable)
  5176. (* ---- MAX ----- *)
  5177. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5178. IF numberActualParameters = 1 THEN
  5179. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5180. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5181. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5182. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5183. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5184. *)
  5185. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5186. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5187. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5188. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5189. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5190. END;
  5191. ELSE
  5192. Error(parameter0.position,"is not a type symbol");
  5193. END
  5194. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5195. ConvertOperands(parameter0,parameter1);
  5196. actualParameters.SetExpression(0,parameter0);
  5197. actualParameters.SetExpression(1,parameter1);
  5198. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5199. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5200. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5201. END;
  5202. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5203. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5204. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5205. END;
  5206. END;
  5207. type := parameter0.type;
  5208. ELSE type := SyntaxTree.invalidType;
  5209. END;
  5210. (* ---- MIN ----- *)
  5211. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5212. IF numberActualParameters = 1 THEN
  5213. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5214. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5215. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5216. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5217. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5218. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5219. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5220. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5221. END;
  5222. ELSE
  5223. Error(parameter0.position,"is not a type symbol");
  5224. END
  5225. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5226. ConvertOperands(parameter0,parameter1);
  5227. actualParameters.SetExpression(0,parameter0);
  5228. actualParameters.SetExpression(1,parameter1);
  5229. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5230. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5231. ELSE result.SetResolved(parameter1.resolved)
  5232. END;
  5233. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5234. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5235. ELSE result.SetResolved(parameter1.resolved)
  5236. END;
  5237. END;
  5238. type := parameter0.type;
  5239. ELSE type := SyntaxTree.invalidType;
  5240. END;
  5241. (* ---- ODD ----- *)
  5242. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5243. type := system.booleanType;
  5244. IF CheckIntegerType(parameter0) THEN
  5245. IF IsIntegerValue(parameter0,i0) THEN
  5246. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5247. type := system.booleanType;
  5248. END;
  5249. END;
  5250. (* ---- ORD ----- *)
  5251. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5252. IF id = Global.Ord THEN
  5253. type := system.integerType;
  5254. ELSE
  5255. type := system.longintType;
  5256. END;
  5257. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5258. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5259. actualParameters.SetExpression(0,parameter0);
  5260. (* IF CheckCharacterType(parameter0) THEN*)
  5261. IF IsCharacterValue(parameter0,c)THEN
  5262. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5263. type := Global.GetSignedIntegerType(system,ORD(c));
  5264. END;
  5265. ELSE Error(parameter0.position, "incompatible parameter");
  5266. END;
  5267. (* ---- SHORT ----- *)
  5268. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5269. type := type0;
  5270. IF IsSignedIntegerType(type) THEN
  5271. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5272. ELSIF type = system.integerType THEN type := system.shortintType
  5273. ELSIF type = system.longintType THEN type := system.integerType
  5274. ELSIF type = system.hugeintType THEN type:= system.longintType
  5275. ELSE
  5276. CASE type.sizeInBits OF
  5277. 16: type := Global.Integer8
  5278. |32: type := Global.Integer16
  5279. |64: type := Global.Integer32
  5280. END;
  5281. END;
  5282. ELSIF type IS SyntaxTree.FloatType THEN
  5283. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5284. ELSIF type = system.longrealType THEN type := system.realType
  5285. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5286. END;
  5287. ELSIF type IS SyntaxTree.ComplexType THEN
  5288. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5289. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5290. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5291. END;
  5292. ELSE
  5293. Error(parameter0.position,"short not applicable")
  5294. END;
  5295. IF (parameter0.resolved # NIL) THEN
  5296. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5297. IF parameter0 IS SyntaxTree.Value THEN
  5298. result.SetResolved(parameter0(SyntaxTree.Value));
  5299. END;
  5300. END;
  5301. (* ---- LONG ----- *)
  5302. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5303. type := type0;
  5304. IF IsSignedIntegerType(type) THEN
  5305. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5306. ELSIF type = system.longintType THEN type := system.hugeintType
  5307. ELSIF type = system.integerType THEN type := system.longintType
  5308. ELSIF type = system.shortintType THEN type := system.integerType
  5309. ELSE
  5310. CASE type.sizeInBits OF
  5311. 8: type := Global.Integer16
  5312. |16: type := Global.Integer32
  5313. |32: type := Global.Integer64
  5314. END;
  5315. END;
  5316. ELSIF type IS SyntaxTree.FloatType THEN
  5317. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5318. ELSIF type= system.realType THEN type := system.longrealType
  5319. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5320. END;
  5321. ELSIF type IS SyntaxTree.ComplexType THEN
  5322. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5323. ELSIF type = system.complexType THEN type := system.longcomplexType
  5324. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5325. END;
  5326. ELSE
  5327. Error(parameter0.position,"long not applicable")
  5328. END;
  5329. IF (parameter0.resolved # NIL) THEN
  5330. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5331. IF parameter0 IS SyntaxTree.Value THEN
  5332. result.SetResolved(parameter0(SyntaxTree.Value));
  5333. END;
  5334. END;
  5335. (* ---- SIZE OF ----- *)
  5336. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5337. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5338. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5339. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5340. type := system.integerType;
  5341. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5342. ELSE
  5343. (* for variables, system sizeof could represent the physically occupied size
  5344. determined via the type descriptor, implement that ? *)
  5345. Error(parameter0.position,"is not a type symbol");
  5346. END
  5347. (* ---- SYSTEM.TRACE -----*)
  5348. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5349. FOR i := 0 TO numberActualParameters-1 DO
  5350. parameter0 := actualParameters.GetExpression(i);
  5351. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5352. Error(parameter0.position,"incompatible parameter");
  5353. END;
  5354. END;
  5355. (* remaining issues can only be tested in backend *)
  5356. (* ---- ADDRESSOF----- *)
  5357. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5358. IF HasAddress(parameter0) THEN
  5359. type := system.addressType;
  5360. ELSE
  5361. type := SyntaxTree.invalidType;
  5362. Error(parameter0.position,"has no address");
  5363. END;
  5364. (* ---- BIT ----- *)
  5365. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5366. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5367. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5368. actualParameters.SetExpression(0,parameter0);
  5369. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5370. actualParameters.SetExpression(1,parameter1);
  5371. END;
  5372. type := system.booleanType;
  5373. (* ----- MSK ---- *)
  5374. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5375. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5376. ConvertOperands(parameter0,parameter1);
  5377. actualParameters.SetExpression(0,parameter0);
  5378. actualParameters.SetExpression(1,parameter1);
  5379. END;
  5380. type := parameter0.type;
  5381. (* ---- SYSTEM.GET64 ----- *)
  5382. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5383. IF CheckAddressType(parameter0) THEN
  5384. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5385. actualParameters.SetExpression(0,parameter0);
  5386. END;
  5387. type := system.hugeintType;
  5388. (* ---- SYSTEM.GET32 ----- *)
  5389. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5390. IF CheckAddressType(parameter0) THEN
  5391. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5392. actualParameters.SetExpression(0,parameter0);
  5393. END;
  5394. type := system.longintType;
  5395. (* ---- SYSTEM.GET16 ----- *)
  5396. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5397. IF CheckAddressType(parameter0) THEN
  5398. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5399. actualParameters.SetExpression(0,parameter0);
  5400. END;
  5401. type := system.integerType;
  5402. (* ---- SYSTEM.GET8 ----- *)
  5403. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5404. IF CheckAddressType(parameter0) THEN
  5405. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5406. actualParameters.SetExpression(0,parameter0);
  5407. END;
  5408. type := system.shortintType;
  5409. (* ---- SYSTEM.GetStackPointer ----- *)
  5410. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5411. type := system.addressType;
  5412. (* ---- SYSTEM.GetFramePointer ----- *)
  5413. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5414. type := system.addressType;
  5415. (* ---- SYSTEM.GetActivity ----- *)
  5416. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5417. type := system.objectType;
  5418. (* ---- SYSTEM.SetStackPointer ----- *)
  5419. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5420. IF CheckAddressType(parameter0) THEN
  5421. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5422. actualParameters.SetExpression(0,parameter0);
  5423. END;
  5424. (* ---- SYSTEM.SetFramePointer ----- *)
  5425. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5426. IF CheckAddressType(parameter0) THEN
  5427. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5428. actualParameters.SetExpression(0,parameter0);
  5429. END;
  5430. (* ---- SYSTEM.SetActivity ----- *)
  5431. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5432. IF CheckObjectType(parameter0) THEN
  5433. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5434. actualParameters.SetExpression(0,parameter0);
  5435. END;
  5436. (* ---- LSH, LSL, ROT, ROR ----- *)
  5437. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5438. type := type0;
  5439. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5440. actualParameters.SetExpression(1, parameter1);
  5441. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5442. IF id = Global.Lsh THEN
  5443. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5444. ELSIF id = Global.Rot THEN
  5445. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5446. ELSIF id = Global.Ror THEN
  5447. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5448. END;
  5449. END;
  5450. (* ---- SYSTEM.VAL ----- *)
  5451. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5452. IF CheckTypeDeclarationType(parameter0) THEN
  5453. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5454. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5455. result := SyntaxTree.invalidExpression;
  5456. Error(parameter0.position,"is no basic type");
  5457. ELSE
  5458. IF (parameter1.resolved # NIL) THEN
  5459. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5460. IF parameter0 IS SyntaxTree.Value THEN
  5461. result.SetResolved(parameter0(SyntaxTree.Value));
  5462. END;
  5463. END;
  5464. result.SetAssignable(parameter1.assignable);
  5465. END;
  5466. END;
  5467. (* ---- SYSTEM.GET ----- *)
  5468. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5469. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5470. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5471. actualParameters.SetExpression(0,parameter0);
  5472. END;
  5473. (* ---- SYSTEM.PUT ----- *)
  5474. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5475. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5476. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. END;
  5479. (* ---- SYSTEM.PUT64 ----- *)
  5480. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5481. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5482. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5483. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5484. actualParameters.SetExpression(0,parameter0);
  5485. actualParameters.SetExpression(1,parameter1);
  5486. END;
  5487. (* ---- SYSTEM.PUT32 ----- *)
  5488. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5489. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5490. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5491. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5492. actualParameters.SetExpression(0,parameter0);
  5493. actualParameters.SetExpression(1,parameter1);
  5494. END;
  5495. (* ---- SYSTEM.PUT16 ----- *)
  5496. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5497. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5498. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5499. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5500. actualParameters.SetExpression(0,parameter0);
  5501. actualParameters.SetExpression(1,parameter1);
  5502. END;
  5503. (* ---- SYSTEM.PUT8 ----- *)
  5504. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5505. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5506. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5507. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5508. actualParameters.SetExpression(0,parameter0);
  5509. actualParameters.SetExpression(1,parameter1);
  5510. END;
  5511. (* ---- SYSTEM.MOVE ----- *)
  5512. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5513. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5514. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5515. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5516. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5517. actualParameters.SetExpression(0,parameter0);
  5518. actualParameters.SetExpression(1,parameter1);
  5519. actualParameters.SetExpression(2,parameter2);
  5520. END;
  5521. (* ---- SYSTEM.NEW ----- *)
  5522. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5523. IF ~IsPointerType(parameter0.type) THEN
  5524. Error(parameter0.position,"is not a pointer")
  5525. ELSIF CheckSizeType(parameter1) THEN
  5526. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5527. actualParameters.SetExpression(1,parameter1);
  5528. END;
  5529. (* ----SYSTEM.REF ---- *)
  5530. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5531. type := system.addressType
  5532. (* ---- INCR ----- *)
  5533. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5534. type := system.sizeType;
  5535. base := type0;
  5536. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5537. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5538. IF i1 < 0 THEN
  5539. Error(position, "invalid dimension");
  5540. base := SyntaxTree.invalidType;
  5541. ELSE
  5542. base := ArrayBase(base,i1);
  5543. IF (base # NIL) & Indexable(base) THEN
  5544. ELSE
  5545. Error(position, "len on no array");
  5546. IF VerboseErrorMessage THEN
  5547. Printout.Info("base",base);
  5548. END;
  5549. base := SyntaxTree.invalidType;
  5550. END;
  5551. END;
  5552. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5553. actualParameters.SetExpression(1,parameter1);
  5554. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5555. mathArrayType := base(SyntaxTree.MathArrayType);
  5556. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5557. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5558. type := system.longintType;
  5559. END;
  5560. END;
  5561. ELSE
  5562. type := system.longintType;
  5563. END;
  5564. (* ---- SUM ----- *)
  5565. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5566. Error(position, "sum operator not applicable");
  5567. (* ---- ALL ----- *)
  5568. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5569. Error(position, "all operator not applicable");
  5570. (* ---- DIM ----- *)
  5571. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5572. type := system.sizeType;
  5573. IF type0 IS SyntaxTree.MathArrayType THEN
  5574. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5575. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5576. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5577. END;
  5578. ELSE
  5579. Error(position, "dimension on non math array type");
  5580. END;
  5581. (* ---- CAS ----- *)
  5582. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5583. IF type0.IsComposite () THEN
  5584. Error(position, "first parameter of composite type");
  5585. result := SyntaxTree.invalidExpression;
  5586. ELSIF ~IsVariable (parameter0) THEN
  5587. Error(position, "first parameter not assignable");
  5588. result := SyntaxTree.invalidExpression;
  5589. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5590. Error(position, "second parameter incompatible");
  5591. result := SyntaxTree.invalidExpression;
  5592. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5593. Error(position, "third parameter incompatible");
  5594. result := SyntaxTree.invalidExpression;
  5595. ELSE
  5596. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5597. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5598. type := type0;
  5599. END;
  5600. (* ---- RESHAPE ----- *)
  5601. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5602. IF type0 IS SyntaxTree.MathArrayType THEN
  5603. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5604. base := ArrayBase(type0,MAX(LONGINT));
  5605. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5606. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5607. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5608. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5609. IF ~CompatibleTo(system,type0,parameterType) THEN
  5610. Error(parameter0.position,"incompatible parameter in reshape");
  5611. result := SyntaxTree.invalidExpression;
  5612. ELSE
  5613. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5614. END;
  5615. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5616. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5617. IF ~CompatibleTo(system,type1,parameterType) THEN
  5618. Error(parameter1.position,"parameter incompatible to math array of longint");
  5619. result := SyntaxTree.invalidExpression;
  5620. ELSE
  5621. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5622. END;
  5623. ELSE
  5624. Error(position,"reshape on non math array type");
  5625. result := SyntaxTree.invalidExpression;
  5626. END;
  5627. (* ---- SYSTEM.TYPECODE ----- *)
  5628. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5629. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5630. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5631. type := type.resolved;
  5632. IF type IS SyntaxTree.PointerType THEN
  5633. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5634. END;
  5635. IF ~(type IS SyntaxTree.RecordType) THEN
  5636. Error(parameter0.position,"must be type with type descriptor");
  5637. END;
  5638. ELSE
  5639. Error(parameter0.position,"is not a type symbol");
  5640. END;
  5641. type := system.addressType;
  5642. (* -------- FLT --------- *)
  5643. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5644. type := system.realType;
  5645. IF IsRealValue(parameter0, r) THEN
  5646. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5647. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5648. i0 := i; i := ABS(i);
  5649. IF i # 0 THEN
  5650. i1 := 23;
  5651. IF i >= 2*800000H THEN
  5652. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5653. ELSIF i < 800000H THEN
  5654. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5655. END;
  5656. i := (i1 + 127)*800000H - 800000H + i;
  5657. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5658. END;
  5659. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5660. END;
  5661. (* ------- CONNECT -------*)
  5662. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5663. (*IF ~(currentIsCellNet) THEN
  5664. Error(position, "connection outside activeCells body block");
  5665. END;*)
  5666. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5667. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5668. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5669. END;
  5670. IF numberActualParameters = 3 THEN
  5671. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5672. Error(position, "incompatible channel size parameter");
  5673. END;
  5674. *)
  5675. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5676. actualParameters.SetExpression(2,parameter2);
  5677. END;
  5678. activeCellsStatement := TRUE;
  5679. (* ---------- DELEGATE --------*)
  5680. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5681. (*
  5682. IF ~(currentIsCellNet) THEN
  5683. Error(position, "connection delegation outside activeCells body block");
  5684. END;
  5685. *)
  5686. IF ~CheckPortType(parameter1, inPort) THEN
  5687. Error(parameter0.position,"not a port")
  5688. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5689. Error(parameter1.position,"not a port")
  5690. ELSIF (outPort.direction # inPort.direction) THEN
  5691. Error(parameter0.position,"invalid port direction");
  5692. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5693. Error(position, "incompatible port sizes");
  5694. END;
  5695. activeCellsStatement := TRUE;
  5696. (* --------- RECEIVE ---------*)
  5697. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5698. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5699. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5700. IF inPort.direction # SyntaxTree.InPort THEN
  5701. Error(parameter0.position,"not an in-port")
  5702. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5703. Error(parameter1.position,"incompatible to port type");
  5704. END;
  5705. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5706. IF ~SameType(parameter2.type, system.integerType) THEN
  5707. Error(parameter2.position,"incompatible to integer type");
  5708. END;
  5709. END;
  5710. END;
  5711. (* --------- SEND ---------*)
  5712. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5713. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5714. IF CheckPortType(parameter0,outPort) THEN
  5715. IF outPort.direction # SyntaxTree.OutPort THEN
  5716. Error(parameter1.position,"not an out-port")
  5717. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5718. Error(parameter1.position,"incompatible to port type");
  5719. ELSE
  5720. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5721. actualParameters.SetExpression(1,parameter1);
  5722. END;
  5723. END;
  5724. (* ------- custom builtins ----- *)
  5725. ELSIF id = Global.systemSpecial THEN
  5726. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5727. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5728. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5729. type := procedureType.returnType;
  5730. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5731. (* go through all formal parameters *)
  5732. formalParameter := procedureType.firstParameter;
  5733. FOR i := 0 TO actualParameters.Length() - 1 DO
  5734. actualParameter := actualParameters.GetExpression(i);
  5735. IF actualParameter = SyntaxTree.invalidExpression THEN
  5736. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5737. Error(position, "incompatible parameter")
  5738. ELSE
  5739. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5740. END;
  5741. actualParameters.SetExpression(i, actualParameter);
  5742. formalParameter := formalParameter.nextParameter
  5743. END
  5744. END
  5745. ELSE
  5746. Error(position, "builtin not implemented");
  5747. result := SyntaxTree.invalidExpression;
  5748. END;
  5749. END;
  5750. IF result # SyntaxTree.invalidExpression THEN
  5751. type := ResolveType(type);
  5752. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5753. result.SetType(type);
  5754. END;
  5755. RETURN result
  5756. END NewBuiltinCallDesignator;
  5757. (** return type guard designator left(type)
  5758. - check if type can be extended (i.e. is no static record)
  5759. - check if type is a type extension of left.type
  5760. - returns new type guard designator
  5761. returns invalidDesignator = invalidExpression if error
  5762. **)
  5763. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5764. VAR result: SyntaxTree.Designator;
  5765. BEGIN
  5766. result := SyntaxTree.invalidDesignator;
  5767. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5768. Error(position, "no type extension of type");
  5769. IF VerboseErrorMessage THEN
  5770. Printout.Info("left",left);
  5771. Printout.Info("type",type);
  5772. END;
  5773. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5774. Error(position, "variable cannot be extended");
  5775. ELSIF IsUnsafePointer(left.type) THEN
  5776. Error(position, "forbidden type guard on unsafe pointer");
  5777. ELSE
  5778. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5779. result.SetType(type);
  5780. result.SetAssignable(left.assignable);
  5781. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5782. END;
  5783. RETURN result
  5784. END NewTypeGuardDesignator;
  5785. (** check and resolve parameter designator left(expression list)
  5786. - check expression list
  5787. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5788. - elsif left is a procedure type then
  5789. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5790. - else return is a procedure call then return ProcedureCallDesignator
  5791. returns invalidDesignator = invalidExpression if error
  5792. **)
  5793. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5794. VAR
  5795. parameters: SyntaxTree.ExpressionList;
  5796. left: SyntaxTree.Designator;
  5797. result,expression: SyntaxTree.Expression;
  5798. typeDeclaration: SyntaxTree.TypeDeclaration;
  5799. type, expressionType: SyntaxTree.Type;
  5800. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5801. BEGIN
  5802. type := type.resolved;
  5803. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5804. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5805. END;
  5806. RETURN type
  5807. END BaseType;
  5808. BEGIN
  5809. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5810. result := SyntaxTree.invalidDesignator;
  5811. left := ResolveDesignator(designator.left);
  5812. IF left # SyntaxTree.invalidDesignator THEN
  5813. parameters := designator.parameters;
  5814. IF ExpressionList(parameters) THEN
  5815. IF (left.type = NIL) THEN
  5816. Error(left.position,"object is not a procedure or cannot be extended");
  5817. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5818. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5819. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5820. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5821. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5822. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5823. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5824. ELSE
  5825. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5826. END
  5827. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5828. expression := parameters.GetExpression(0);
  5829. type := typeDeclaration.declaredType.resolved;
  5830. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5831. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5832. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5833. OR (expressionType IS SyntaxTree.EnumerationType)
  5834. ) THEN
  5835. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5836. ELSE
  5837. Error(left.position,"invalid type in explicit conversion");
  5838. END;
  5839. ELSE
  5840. Error(left.position,"called object is not a procedure or cannot be extended");
  5841. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5842. result := SyntaxTree.invalidDesignator;
  5843. END;
  5844. ELSE
  5845. result := SyntaxTree.invalidDesignator
  5846. END;
  5847. END;
  5848. resolvedExpression := result;
  5849. END VisitParameterDesignator;
  5850. (** check dereference designator left^
  5851. - check if left is pointer type or left is object type
  5852. - return new dereference designator with type = left.baseType.type (if appropriate)
  5853. with error handling
  5854. returns invalidDesignator = invalidExpression if error
  5855. **)
  5856. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5857. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5858. BEGIN
  5859. result := SyntaxTree.invalidDesignator;
  5860. type := left.type;
  5861. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5862. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5863. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5864. result.SetAssignable(TRUE);
  5865. result.SetType(type);
  5866. result.SetHidden(left.isHidden);
  5867. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5868. type := type.resolved;
  5869. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5870. result.SetAssignable(TRUE);
  5871. result.SetType(type);
  5872. result.SetHidden(left.isHidden);
  5873. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5874. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5875. result.SetAssignable(TRUE);
  5876. result.SetType(type);
  5877. result.SetHidden(left.isHidden);
  5878. ELSE
  5879. Error(position, "dereference on no pointer");
  5880. IF VerboseErrorMessage THEN
  5881. Printout.Info("pointer", type);
  5882. Printout.Info("scope", currentScope);
  5883. END;
  5884. END;
  5885. RETURN result
  5886. END NewDereferenceDesignator;
  5887. (** check supercall designator left^
  5888. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5889. - return new supercall designator with type = left.type
  5890. with error handling
  5891. **)
  5892. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5893. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5894. objectScope: SyntaxTree.Scope;
  5895. BEGIN
  5896. result := SyntaxTree.invalidDesignator;
  5897. IF left = SyntaxTree.invalidDesignator THEN
  5898. (* error already handled *)
  5899. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5900. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5901. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5902. IF symbol IS SyntaxTree.Procedure THEN
  5903. procedure := symbol(SyntaxTree.Procedure);
  5904. objectScope := currentScope;
  5905. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5906. objectScope := objectScope.outerScope;
  5907. END;
  5908. IF (left.left = NIL) OR ~
  5909. (
  5910. (left.left IS SyntaxTree.SelfDesignator) OR
  5911. (left.left IS SyntaxTree.DereferenceDesignator)
  5912. & (left.left(SyntaxTree.Designator).left # NIL)
  5913. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5914. Error(position, "procedure not in immediate object scope");
  5915. IF VerboseErrorMessage THEN
  5916. Printout.Info("left.left",left.left);
  5917. END;
  5918. ELSIF procedure.super # NIL THEN
  5919. result := SyntaxTree.NewSupercallDesignator(position,left);
  5920. result.SetType(left.type.resolved)
  5921. ELSE
  5922. Error(position, "no supermethod for this procedure");
  5923. END;
  5924. ELSE
  5925. Error(position, "symbol is not a procedure");
  5926. END;
  5927. ELSE
  5928. Error(position, "is no symbol designator");
  5929. END;
  5930. RETURN result
  5931. END NewSupercallDesignator;
  5932. (** check and semantically resolve arrow designator left^
  5933. - if left is procedure type -> result := SupercallDesignator
  5934. - else result := DereferenceDesignator
  5935. returns result via global variable resolvedExpression
  5936. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5937. **)
  5938. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5939. VAR left: SyntaxTree.Designator;
  5940. BEGIN
  5941. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5942. left := ResolveDesignator(arrowDesignator.left);
  5943. IF left # NIL THEN
  5944. IF (left.type = NIL) THEN
  5945. Error(arrowDesignator.position,"Invalid arrow designator");
  5946. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5947. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5948. ELSE
  5949. IF IsPointerToObject(left.type) THEN
  5950. Warning(arrowDesignator.position, "forbidden dereference on object");
  5951. END;
  5952. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5953. END
  5954. END
  5955. END VisitArrowDesignator;
  5956. (** check and return expression
  5957. - if expression has no type then resolve expression
  5958. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5959. - return result
  5960. **)
  5961. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5962. VAR result,prev: SyntaxTree.Expression;
  5963. BEGIN
  5964. IF expression = NIL THEN result := NIL
  5965. ELSIF (expression.type = NIL) THEN
  5966. prev := resolvedExpression;
  5967. resolvedExpression := SyntaxTree.invalidExpression;
  5968. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5969. expression.SetType(SyntaxTree.invalidType);
  5970. END;
  5971. expression.Accept(SELF);
  5972. result := resolvedExpression;
  5973. IF currentIsRealtime THEN
  5974. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5975. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  5976. END;
  5977. END;
  5978. (* designator modifiers for backends if they support it ...*)
  5979. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5980. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5981. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5982. END;
  5983. resolvedExpression := prev
  5984. ELSE
  5985. result := expression
  5986. END;
  5987. RETURN result
  5988. END ResolveExpression;
  5989. (**
  5990. check expression to be constant expression
  5991. - resolve expression
  5992. - if valid then check that of value type
  5993. report error and return invalidExpression if anything fails
  5994. **)
  5995. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5996. VAR position: Position;
  5997. BEGIN
  5998. position := expression.position;
  5999. expression := ResolveExpression(expression);
  6000. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6001. ELSIF (expression.resolved = NIL) THEN
  6002. Error(position, "expression is not constant");
  6003. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6004. expression := SyntaxTree.invalidExpression;
  6005. END;
  6006. RETURN expression
  6007. END ConstantExpression;
  6008. (** check expression to be constant integer
  6009. - resolve expresssion
  6010. - if valid then check that of integer value type
  6011. report error and return invalidExpression if anything fails
  6012. **)
  6013. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6014. VAR position: Position;
  6015. BEGIN
  6016. position := expression.position;
  6017. expression := ResolveExpression(expression);
  6018. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6019. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6020. expression := SyntaxTree.invalidExpression;
  6021. Error(position, "expression is not a constant integer");
  6022. END;
  6023. RETURN expression
  6024. END ConstantInteger;
  6025. (** check expression as positive (>=0) constant integer
  6026. - resolve expression
  6027. - if valid then check that integer value
  6028. - if integer value then check that value >= 0
  6029. report error and return invalidExpression if anything fails
  6030. **)
  6031. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6032. VAR position: Position;
  6033. BEGIN
  6034. position := expression.position;
  6035. expression := ConstantExpression(expression);
  6036. IF expression = SyntaxTree.invalidExpression THEN
  6037. (* error already reported *)
  6038. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6039. Error(position, "expression is not integer valued");
  6040. expression := SyntaxTree.invalidExpression
  6041. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6042. Error(position, "integer is not greater or equal zero");
  6043. END;
  6044. RETURN expression
  6045. END ConstantIntegerGeq0;
  6046. (** check expression as condition
  6047. - resolve expression
  6048. - if valid expression then check that result type is boolean
  6049. report error and return invalidExpression if anything fails
  6050. **)
  6051. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6052. VAR position: Position;
  6053. BEGIN
  6054. position := expression.position;
  6055. expression := ResolveExpression(expression);
  6056. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6057. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6058. expression := SyntaxTree.invalidExpression;
  6059. Error(position, "expression is not boolean");
  6060. END;
  6061. RETURN expression
  6062. END ResolveCondition;
  6063. (*** symbols ***)
  6064. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6065. BEGIN
  6066. x.Accept(SELF);
  6067. END ResolveSymbol;
  6068. (** check a symbol
  6069. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6070. **)
  6071. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6072. VAR scope: SyntaxTree.Scope;
  6073. BEGIN
  6074. (* visibility *)
  6075. scope := symbol.scope;
  6076. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6077. scope := scope.outerScope;
  6078. END;
  6079. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6080. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6081. Error(symbol.position,"cannot be exported");
  6082. IF VerboseErrorMessage THEN
  6083. Printout.Info("symbol",symbol);
  6084. END;
  6085. END;
  6086. END;
  6087. END CheckSymbolVisibility;
  6088. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6089. If node is currently being resolved then emit a cyclic definition error.
  6090. Return TRUE only if node is fully resolved.
  6091. **)
  6092. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6093. VAR result: BOOLEAN;
  6094. BEGIN
  6095. IF SyntaxTree.Resolved IN x.state THEN
  6096. result := FALSE
  6097. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6098. Error(x.position,"cyclic definition");
  6099. result := FALSE;
  6100. ELSE
  6101. result := TRUE;
  6102. x.SetState(SyntaxTree.BeingResolved)
  6103. END;
  6104. RETURN result
  6105. END SymbolNeedsResolution;
  6106. (** check and resolve a type declaration symbol = Type
  6107. - set type to declaration type
  6108. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6109. This is so because the type declaration itself does not have a type but it only stands for a type.
  6110. In the implementation of the compiler this made a lot much easier.
  6111. - resolve and set declared type
  6112. - check symbol
  6113. **)
  6114. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6115. VAR prevScope: SyntaxTree.Scope;
  6116. BEGIN
  6117. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6118. IF SymbolNeedsResolution(typeDeclaration) THEN
  6119. prevScope := currentScope;
  6120. currentScope := typeDeclaration.scope;
  6121. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6122. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6123. CheckSymbolVisibility(typeDeclaration);
  6124. typeDeclaration.SetState(SyntaxTree.Resolved);
  6125. currentScope := prevScope;
  6126. END;
  6127. END VisitTypeDeclaration;
  6128. (** check and resolve a constant declaration symbol = (constant) expression
  6129. - check expression
  6130. - set type and value
  6131. - check symbol
  6132. **)
  6133. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6134. VAR
  6135. expression: SyntaxTree.Expression;
  6136. type: SyntaxTree.Type;
  6137. name: Basic.SegmentedName;
  6138. replacement: Replacement;
  6139. BEGIN
  6140. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6141. IF SymbolNeedsResolution(constant) THEN
  6142. expression := constant.value;
  6143. IF replacements # NIL THEN
  6144. Global.GetSymbolSegmentedName(constant, name);
  6145. replacement := replacements;
  6146. WHILE (replacement # NIL) & (replacement.name # name) DO
  6147. replacement := replacement.next;
  6148. END;
  6149. IF replacement # NIL THEN
  6150. InfoSS(constant.position, "replacing constant", constant.name);
  6151. (*
  6152. NEW(stringReader, Strings.Length(replacement.string^));
  6153. stringReader.Set(replacement.string^);
  6154. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6155. NEW(parser, scanner, diagnostics);
  6156. expression := parser.Expression();
  6157. *)
  6158. expression := replacement.expression;
  6159. replacement.used := TRUE;
  6160. END;
  6161. END;
  6162. constant.SetType(SyntaxTree.invalidType);
  6163. expression := ConstantExpression(expression);
  6164. ASSERT(expression.type # NIL);
  6165. type := expression.type.resolved;
  6166. constant.SetType(type);
  6167. constant.SetValue(expression);
  6168. CheckSymbolVisibility(constant);
  6169. constant.SetState(SyntaxTree.Resolved);
  6170. END;
  6171. END VisitConstant;
  6172. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6173. VAR procedureAlignment: LONGINT;
  6174. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6175. (* least common multiple *)
  6176. VAR a,b: LONGINT;
  6177. BEGIN
  6178. a := a0; b := b0;
  6179. WHILE (a # b) DO
  6180. IF a < b THEN a := a+a0
  6181. ELSE b := b + b0
  6182. END;
  6183. END;
  6184. RETURN a
  6185. END LCM;
  6186. BEGIN
  6187. IF alignment > 1 THEN
  6188. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6189. IF (procedureAlignment > 1) THEN
  6190. alignment := LCM(alignment, procedureAlignment);
  6191. END;
  6192. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6193. END;
  6194. END AdaptStackAlignment;
  6195. (** check and resolve a variable / field
  6196. - check and set type
  6197. - negative check on open array type
  6198. - check symbol
  6199. **)
  6200. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6201. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6202. BEGIN
  6203. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6204. IF SymbolNeedsResolution(variable) THEN
  6205. modifiers := variable.modifiers;
  6206. (*
  6207. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6208. variable.AddFlags(flags);
  6209. *)
  6210. variable.SetType(ResolveType(variable.type));
  6211. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6212. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6213. Error(variable.position,"forbidden open array variable");
  6214. END;
  6215. END;
  6216. CheckSymbolVisibility(variable);
  6217. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6218. variable.SetUntraced(TRUE);
  6219. IF ~ContainsPointer(variable.type) THEN
  6220. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6221. Error(position, "untraced flag on non-pointer variable");
  6222. END;
  6223. END;
  6224. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6225. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6226. IF ~PowerOf2(value) THEN
  6227. Error(position, "forbidden alignment - must be power of two");
  6228. ELSE
  6229. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6230. END;
  6231. END;
  6232. variable.SetAlignment(FALSE,value);
  6233. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6234. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6235. Error(position, "fixed position not possible in procedure");
  6236. END;
  6237. variable.SetAlignment(TRUE, value);
  6238. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6239. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6240. Error(position,"fictive offset not possible in procedure");
  6241. END;
  6242. variable.SetFictive(value);
  6243. variable.SetOffset(value*system.dataUnit);
  6244. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6245. END;
  6246. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6247. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6248. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6249. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6250. END;
  6251. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6252. IF variable.initializer # NIL THEN
  6253. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6254. END;
  6255. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6256. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6257. pointerType.SetPointerBase(variable.type);
  6258. pointerType.SetHidden(TRUE);
  6259. variable.SetType(ResolveType(pointerType));
  6260. END;
  6261. variable.SetState(SyntaxTree.Resolved);
  6262. END;
  6263. END VisitVariable;
  6264. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6265. BEGIN
  6266. VisitVariable(property)
  6267. END VisitProperty;
  6268. (** check and resolve a (procedure) parameter
  6269. - check and set type
  6270. - check symbol
  6271. - check parameter kind and set read-only flags if appropriate
  6272. **)
  6273. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6274. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6275. BEGIN
  6276. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6277. IF SymbolNeedsResolution(parameter) THEN
  6278. modifiers := parameter.modifiers;
  6279. parameter.SetType(ResolveType(parameter.type));
  6280. ASSERT(parameter.type.resolved # NIL);
  6281. CheckSymbolVisibility(parameter);
  6282. IF parameter.defaultValue # NIL THEN
  6283. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6284. Error(parameter.position,"forbidden default value on non-value parameter");
  6285. ELSE
  6286. expression := ConstantExpression(parameter.defaultValue);
  6287. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6288. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6289. parameter.SetDefaultValue(expression);
  6290. END;
  6291. END;
  6292. END;
  6293. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6294. parameter.SetUntraced(TRUE);
  6295. IF ~ContainsPointer(parameter.type) THEN
  6296. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6297. Error(position, "untraced flag on non-pointer variable");
  6298. END;
  6299. END;
  6300. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6301. parameter.SetMoveable(TRUE);
  6302. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6303. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6304. Error(position, "illegal movable flag on non-address variable");
  6305. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6306. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6307. Error(position, "unnecessary movable flag on variable variable");
  6308. END;
  6309. END;
  6310. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6311. parameter.SetState(SyntaxTree.Resolved);
  6312. END;
  6313. END VisitParameter;
  6314. (** check and resolve a procedure (with declaration and implementation scope)
  6315. - check the procedure type
  6316. - check if method (i.e. in record scope), if so then
  6317. - check if (unique) constructor
  6318. - check if (unique) finalizer
  6319. - check if super method available, if so then check signature
  6320. - of not in record scope then negative check on constructor flag
  6321. - of not in record scope then negative check on finalizer flag
  6322. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6323. - check procedure symbol
  6324. **)
  6325. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6326. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6327. procedureType: SyntaxTree.ProcedureType;
  6328. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6329. qualifiedType: SyntaxTree.QualifiedType;
  6330. value: LONGINT;
  6331. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6332. position: Position;
  6333. BEGIN
  6334. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6335. IF IsOberonInline(procedure) THEN
  6336. IF SyntaxTree.Public * procedure.access # {} THEN
  6337. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6338. END;
  6339. procedure.SetInline(FALSE);
  6340. procedure.SetOberonInline(TRUE);
  6341. END;
  6342. IF SymbolNeedsResolution(procedure) THEN
  6343. recentIsRealtime := currentIsRealtime;
  6344. recentIsBodyProcedure := currentIsBodyProcedure;
  6345. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6346. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6347. modifiers := procedureType.modifiers;
  6348. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6349. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6350. IF useDarwinCCalls THEN (*fld*)
  6351. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6352. ELSE
  6353. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6354. END
  6355. END;
  6356. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6357. procedureType.SetInterrupt(TRUE);
  6358. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6359. END;
  6360. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6361. procedureType.SetNoReturn(TRUE);
  6362. END;
  6363. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6364. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6365. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6366. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6367. END;
  6368. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6369. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6370. END;
  6371. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6372. IF ~PowerOf2(value) THEN
  6373. Error(position, "forbidden stack alignment - must be power of two");
  6374. ELSE
  6375. procedureType.SetStackAlignment(value)
  6376. END;
  6377. END;
  6378. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6379. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6380. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6381. END;
  6382. CheckModifiers(modifiers, TRUE);
  6383. modifiers := procedureType.returnTypeModifiers;
  6384. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6385. CheckModifiers(modifiers, TRUE);
  6386. FixProcedureType(procedureType);
  6387. currentIsRealtime := procedureType.isRealtime;
  6388. currentIsBodyProcedure := procedure.isBodyProcedure;
  6389. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6390. THEN
  6391. Error(procedure.position,"problems during parameter offset computation");
  6392. END;
  6393. CheckSymbolVisibility(procedure);
  6394. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6395. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6396. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6397. Error(procedure.position,"problem during parameter offset generation");
  6398. END;
  6399. END;
  6400. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6401. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6402. procedureType.SetDelegate(TRUE);
  6403. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6404. (* add auto-self *)
  6405. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6406. IF (record.pointerType.typeDeclaration = NIL) THEN
  6407. selfParameter.SetType(record.pointerType);
  6408. ELSE
  6409. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6410. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6411. qualifiedType.SetResolved(record.pointerType);
  6412. selfParameter.SetType(qualifiedType);
  6413. END;
  6414. selfParameter.SetAccess(SyntaxTree.Hidden);
  6415. END;
  6416. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6417. IF procedure.isConstructor THEN
  6418. (*! constructor is always visible, compatibility to paco
  6419. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6420. *)
  6421. procedure.MarkUsed;
  6422. IF procedureType.returnType # NIL THEN
  6423. Error(procedure.position,"constructor with forbidden return type");
  6424. END;
  6425. proc := procedure.scope.firstProcedure;
  6426. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6427. proc := proc.nextProcedure;
  6428. END;
  6429. IF proc # NIL THEN
  6430. Error(procedure.position,"duplicate constructor")
  6431. ELSE
  6432. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6433. END;
  6434. END;
  6435. IF procedure.isFinalizer THEN
  6436. procedure.MarkUsed;
  6437. IF procedureType.returnType # NIL THEN
  6438. Error(procedure.position,"finalizer with forbidden return type");
  6439. END;
  6440. IF procedureType.numberParameters # 0 THEN
  6441. Error(procedure.position,"finalizer with formal parameters");
  6442. END;
  6443. proc := procedure.scope.firstProcedure;
  6444. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6445. proc := proc.nextProcedure;
  6446. END;
  6447. IF proc # NIL THEN
  6448. Error(procedure.position,"duplicate finalizer")
  6449. ELSE
  6450. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6451. END;
  6452. END;
  6453. super := FindSuperProcedure(record.recordScope, procedure);
  6454. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6455. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6456. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6457. END;
  6458. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6459. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6460. END;
  6461. IF super.isFinal THEN
  6462. Error(procedure.position,"forbidden method extending final method");
  6463. END;
  6464. procedure.SetSuper(super);
  6465. super.SetOverwritten(TRUE);
  6466. procedure.SetAccess(procedure.access+super.access);
  6467. procedure.MarkUsed;
  6468. END;
  6469. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6470. THEN
  6471. Error(procedure.position,"problems during parameter offset computation");
  6472. END;
  6473. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6474. IF cellsAreObjects THEN
  6475. procedureType.SetDelegate(TRUE);
  6476. END;
  6477. IF procedure.isConstructor THEN
  6478. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6479. END;
  6480. ELSIF procedure.isConstructor THEN
  6481. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6482. END;
  6483. Declarations(procedure.procedureScope, FALSE);
  6484. (* body resolution part done as late fix of the procedure type *)
  6485. procedure.SetState(SyntaxTree.Resolved);
  6486. currentIsRealtime := recentIsRealtime;
  6487. currentIsBodyProcedure := recentIsBodyProcedure;
  6488. END;
  6489. END VisitProcedure;
  6490. (**
  6491. a builtin procedure is a global item that may not be modified locally
  6492. instead the resolving of builtin procedure calls are done in the esignator
  6493. **)
  6494. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6495. VAR type: SyntaxTree.Type;
  6496. BEGIN
  6497. type := ResolveType(builtinProcedure.type);
  6498. END VisitBuiltin;
  6499. (* nopov *)
  6500. (** check and resolve operator
  6501. - operators are first checked as procedures
  6502. - then additional operator-specific checks are done
  6503. - note that only module-scope operators are checked here
  6504. (operators in a record scope are only allowed in the context of
  6505. array-structured object types and checked in 'ResolveArrayStructure')
  6506. - also note that inter-operator conformity is not checked here
  6507. **)
  6508. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6509. VAR
  6510. procedureType: SyntaxTree.ProcedureType;
  6511. leftType, rightType: SyntaxTree.Type;
  6512. identifierNumber: LONGINT; position: Position;
  6513. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6514. modifiers: SyntaxTree.Modifier;
  6515. (** whether a type is locally defined in the current module scope
  6516. for arrays, the base type must be locally defined **)
  6517. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6518. BEGIN
  6519. IF type = NIL THEN
  6520. RETURN FALSE
  6521. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6522. RETURN TRUE
  6523. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6524. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6525. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6526. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6527. ELSE
  6528. RETURN FALSE
  6529. END
  6530. END IsLocallyDefined;
  6531. BEGIN
  6532. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6533. procedureType := operator.type(SyntaxTree.ProcedureType);
  6534. modifiers := procedureType.modifiers;
  6535. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6536. CheckModifiers(modifiers, TRUE);
  6537. VisitProcedure(operator);
  6538. IF operator.scope IS SyntaxTree.RecordScope THEN
  6539. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6540. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6541. IF identifierNumber = -1 THEN
  6542. Error(operator.position, "operator with unknown identifier")
  6543. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6544. Error(operator.position, "identifier may not be used for operator")
  6545. ELSE
  6546. IF procedureType.numberParameters < 1 THEN
  6547. Error(operator.position, "operator without operand");
  6548. ELSIF procedureType.numberParameters > 2 THEN
  6549. Error(operator.position, "operator with more than two operands");
  6550. ELSE
  6551. (* determine operand types *)
  6552. leftType := procedureType.firstParameter.type;
  6553. IF procedureType.numberParameters > 1 THEN
  6554. rightType := procedureType.firstParameter.nextParameter.type
  6555. ELSE
  6556. rightType := NIL
  6557. END;
  6558. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6559. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6560. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6561. Error(operator.position, "none of the operands is declared in the same module")
  6562. END
  6563. END;
  6564. (* TODO: refine the checks, think about how restrictive the checks should be
  6565. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6566. They might be used for intersection, union, complement of custom object types *)
  6567. (* defaults *)
  6568. hasReturnType := TRUE;
  6569. mustBeUnary := FALSE;
  6570. mustBeBinary := FALSE;
  6571. mustReturnBoolean := FALSE;
  6572. mustReturnInteger := FALSE;
  6573. mustHaveEquitypedOperands := FALSE;
  6574. (* operator-specific exceptions *)
  6575. CASE identifierNumber OF
  6576. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6577. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6578. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6579. mustBeBinary := TRUE
  6580. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6581. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6582. | Scanner.Times: mustBeBinary := TRUE
  6583. | Scanner.TimesTimes: mustBeBinary := TRUE
  6584. | Scanner.DotTimes: mustBeBinary := TRUE
  6585. | Scanner.PlusTimes: mustBeBinary := TRUE
  6586. | Scanner.Slash: mustBeBinary := TRUE
  6587. | Scanner.Backslash: mustBeBinary := TRUE
  6588. | Scanner.DotSlash: mustBeBinary := TRUE
  6589. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6590. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6591. | Scanner.Not: mustBeUnary := TRUE
  6592. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6593. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6594. | Scanner.Transpose: mustBeUnary := TRUE;
  6595. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6596. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6597. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6598. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6599. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6600. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6601. | Global.Abs: mustBeUnary := TRUE;
  6602. | Global.Ash: (* TODO: arity? *)
  6603. | Global.Cap: (* TODO: arity? *)
  6604. | Global.Chr: mustBeUnary := TRUE;
  6605. | Global.Entier: (* TODO: arity? *)
  6606. | Global.EntierH: (* TODO: arity? *)
  6607. | Global.Len: (* unary and binary *)
  6608. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6609. | Global.Max, Global.Min: (* unary and binary *)
  6610. | Global.Odd: (* TODO: arity? *)
  6611. | Global.Sum: (* TODO: arity? *)
  6612. | Global.All: (* TODO: arity? *)
  6613. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6614. | Scanner.Alias:
  6615. | Scanner.GreaterGreater, Scanner.LessLess:
  6616. mustBeBinary := TRUE; hasReturnType := FALSE;
  6617. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6618. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6619. END;
  6620. (* check parameter count *)
  6621. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6622. Error(operator.position,"operator is not unary")
  6623. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6624. Error(operator.position,"operator is not binary")
  6625. END;
  6626. (* check parameter types *)
  6627. (* TODO: is this used at all? *)
  6628. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6629. leftType := procedureType.firstParameter.type;
  6630. rightType := procedureType.firstParameter.nextParameter.type;
  6631. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6632. Error(operator.position, "the two operands are not of the same type")
  6633. END
  6634. END;
  6635. (* check return type *)
  6636. IF hasReturnType THEN
  6637. IF procedureType.returnType = NIL THEN
  6638. Error(operator.position, "return type required")
  6639. ELSIF mustReturnBoolean THEN
  6640. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6641. Error(operator.position,"return type is not Boolean")
  6642. END
  6643. ELSIF mustReturnInteger THEN
  6644. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6645. Error(operator.position,"return type is not integer")
  6646. END
  6647. END
  6648. ELSIF procedureType.returnType # NIL THEN
  6649. Error(operator.position, "return type not allowed")
  6650. END
  6651. END
  6652. END
  6653. END
  6654. END VisitOperator;
  6655. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6656. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6657. BEGIN
  6658. IF error THEN RETURN FALSE END;
  6659. prevScope := currentScope;
  6660. prevDiagnostics := diagnostics;
  6661. diagnostics := NIL; (* suppress error output *)
  6662. currentScope := module.moduleScope;
  6663. VisitImport(x);
  6664. IF ~error THEN
  6665. module.moduleScope.AddImport(x);
  6666. x.SetScope(module.moduleScope);
  6667. END;
  6668. currentScope := prevScope;
  6669. diagnostics := prevDiagnostics;
  6670. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6671. END AddImport;
  6672. (** check and resolve import
  6673. - check for name = SYSTEM
  6674. - check for forbidden self import
  6675. - search through global import cache: already imported?
  6676. - check if already imported indirectly
  6677. - import if necessary -> set module and enter into import cache
  6678. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6679. - after this import this direct import and all indirect imports are stored in the current module's import list
  6680. **)
  6681. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6682. VAR
  6683. module: SyntaxTree.Module;
  6684. moduleScope: SyntaxTree.ModuleScope;
  6685. import,reimport: SyntaxTree.Import;
  6686. filename: FileName;
  6687. prevScope: SyntaxTree.Scope;
  6688. BEGIN
  6689. IF SymbolNeedsResolution(x) THEN
  6690. prevScope := currentScope;
  6691. x.SetType(SyntaxTree.importType);
  6692. moduleScope := currentScope.ownerModule.moduleScope;
  6693. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6694. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6695. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6696. Error(x.position,"forbidden self import");
  6697. ELSE
  6698. (* search through global import list: already imported ? *)
  6699. IF (x.module = NIL) & (importCache # NIL) THEN
  6700. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6701. ELSE import := NIL
  6702. END;
  6703. IF x.module # NIL THEN (* already imported indirectly *)
  6704. module := x.module;
  6705. ELSIF import # NIL THEN (* already in module list *)
  6706. module := import.module;
  6707. ASSERT(module # NIL);
  6708. x.SetModule(module);
  6709. ELSE (* must be imported *)
  6710. Global.ModuleFileName(x.moduleName,x.context,filename);
  6711. IF symbolFileFormat # NIL THEN
  6712. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6713. IF module = NIL THEN
  6714. ErrorSS(x.position,"could not import",filename);
  6715. IF VerboseErrorMessage THEN
  6716. Printout.Info("import",x)
  6717. END
  6718. ELSE
  6719. (*
  6720. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6721. (*! should rather be done by importer *)
  6722. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6723. checker.importCache := importCache;
  6724. checker.arrayBaseImported := arrayBaseImported;
  6725. checker.global := global;
  6726. checker.Module(module); (* semantic check *)
  6727. error := error OR checker.error;
  6728. END;
  6729. *)
  6730. (*
  6731. ASSERT(SyntaxTree.Resolved IN module.state);
  6732. *)
  6733. x.SetModule(module);
  6734. IF importCache # NIL THEN
  6735. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6736. import.SetContext(x.context);
  6737. import.SetModule(module);
  6738. importCache.AddImport(import);
  6739. END;
  6740. END;
  6741. ELSE
  6742. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6743. END;
  6744. END;
  6745. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6746. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6747. END;
  6748. import := module.moduleScope.firstImport;
  6749. WHILE(import # NIL) DO
  6750. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6751. ASSERT(currentScope # NIL);
  6752. ASSERT(currentScope.ownerModule # NIL);
  6753. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6754. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6755. Error(x.position,"recursive import");
  6756. ELSE
  6757. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6758. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6759. IF reimport = NIL THEN (* indirect import *)
  6760. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6761. reimport.SetContext(import.context);
  6762. reimport.SetModule(import.module);
  6763. moduleScope.AddImport(reimport);
  6764. reimport.SetScope(moduleScope);
  6765. ELSE
  6766. ASSERT(import.module # NIL);
  6767. reimport.SetModule(import.module); (* direct or indirect import *)
  6768. END;
  6769. END;
  6770. import := import.nextImport;
  6771. END;
  6772. END;
  6773. END;
  6774. currentScope := prevScope;
  6775. (* ELSE nothing to be done *)
  6776. x.SetState(SyntaxTree.Resolved);
  6777. END;
  6778. END VisitImport;
  6779. (*** statements ***)
  6780. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6781. VAR prev,resolved: SyntaxTree.Statement;
  6782. BEGIN
  6783. prev := resolvedStatement;
  6784. resolvedStatement := x;
  6785. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6786. activeCellsStatement := FALSE;
  6787. x.Accept(SELF);
  6788. (* removed this, implementation restriction should be resolved by backend
  6789. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6790. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6791. END;
  6792. *)
  6793. resolved := resolvedStatement;
  6794. resolvedStatement := prev;
  6795. RETURN resolved
  6796. END ResolveStatement;
  6797. (** check and resolve statement sequence
  6798. - check all statements, replace if necessary
  6799. **)
  6800. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6801. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6802. BEGIN
  6803. IF statementSequence # NIL THEN (* else empty *)
  6804. FOR i := 0 TO statementSequence.Length()-1 DO
  6805. statement := statementSequence.GetStatement(i);
  6806. resolved := ResolveStatement(statement);
  6807. IF (resolved # statement) THEN
  6808. statementSequence.SetStatement(i,resolved);
  6809. END;
  6810. END;
  6811. END;
  6812. END StatementSequence;
  6813. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6814. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6815. - check if procedure is callable
  6816. - check return type = NIL (otherwise must be assignment statement)
  6817. **)
  6818. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6819. VAR call: SyntaxTree.Designator;
  6820. BEGIN
  6821. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6822. call := procedureCall.call;
  6823. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6824. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6825. END;
  6826. call := ResolveDesignator(call);
  6827. IF call = SyntaxTree.invalidDesignator THEN
  6828. (* error already handled *)
  6829. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6830. (* inline call in a statement *)
  6831. ELSIF ~IsCallable(call) THEN
  6832. Error(procedureCall.position,"called object is not a procedure");
  6833. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6834. Error(procedureCall.position,"calling procedure with non-void return type");
  6835. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6836. END;
  6837. procedureCall.SetCall(call);
  6838. (*
  6839. IF call = SyntaxTree.invalidDesignator THEN
  6840. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6841. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6842. IF IsOberonInline(procedure) THEN
  6843. Warning(procedure.position,"call to inline proc");
  6844. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6845. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6846. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6847. resolvedStatement := block;
  6848. RETURN;
  6849. END;
  6850. END;
  6851. *)
  6852. END VisitProcedureCallStatement;
  6853. (** check and resolve assignment LHS := RHS
  6854. - resolve LHS and RHS
  6855. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6856. - check if assignment is compatible
  6857. - check if LHS is variable (i.e. assignable)
  6858. - convert RHS if necessary
  6859. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6860. - assignment between different ASOTs
  6861. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6862. - assignment to ASOT elements:
  6863. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6864. **)
  6865. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6866. VAR
  6867. left: SyntaxTree.Designator;
  6868. right, expression: SyntaxTree.Expression;
  6869. designator: SyntaxTree.Designator;
  6870. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6871. mathArrayType: SyntaxTree.MathArrayType;
  6872. BEGIN
  6873. right := ResolveExpression(assignment.right);
  6874. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6875. left := ResolveDesignator(assignment.left);
  6876. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6877. (* error already handled *)
  6878. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6879. (* LHS is index write operator call on ASOT *)
  6880. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6881. (* necessary ?
  6882. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6883. type := procedureType.firstParameter.type;
  6884. expression := procedureCallDesignator.parameters.GetExpression(0);
  6885. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6886. *)
  6887. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6888. ELSIF CheckVariable(left) THEN
  6889. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6890. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6891. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6892. (* conversion done by procedure call
  6893. (* try to convert to left argument *)
  6894. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6895. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6896. procedureCallDesignator.parameters.SetExpression(1, right);
  6897. END;
  6898. *)
  6899. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6900. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6901. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6902. ELSIF AssignmentCompatible(left, right) THEN
  6903. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6904. mathArrayType := MathArrayStructureOfType(left.type);
  6905. right := NewConversion(right.position, right, mathArrayType, NIL);
  6906. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6907. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6908. ELSE
  6909. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6910. assignment.SetLeft(left);
  6911. assignment.SetRight(right);
  6912. resolvedStatement := assignment
  6913. END
  6914. END
  6915. END
  6916. END VisitAssignment;
  6917. (** check and resolve assignment LHS := RHS
  6918. - resolve LHS and RHS
  6919. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6920. - check if assignment is compatible
  6921. - check if LHS is variable (i.e. assignable)
  6922. - convert RHS if necessary
  6923. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6924. - assignment between different ASOTs
  6925. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6926. - assignment to ASOT elements:
  6927. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6928. **)
  6929. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6930. VAR
  6931. left: SyntaxTree.Designator;
  6932. right: SyntaxTree.Expression;
  6933. inPort, outPort: SyntaxTree.PortType;
  6934. expression: SyntaxTree.Expression;
  6935. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6936. BEGIN
  6937. right := ResolveExpression(communication.right);
  6938. left := ResolveDesignator(communication.left);
  6939. communication.SetLeft(left);
  6940. communication.SetRight(right);
  6941. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6942. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6943. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6944. (* conversion done by procedure call
  6945. (* try to convert to left argument *)
  6946. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6947. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6948. procedureCallDesignator.parameters.SetExpression(1, right);
  6949. END;
  6950. *)
  6951. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6952. ELSE
  6953. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6954. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6955. (* error already handled *)
  6956. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6957. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6958. IF outPort.direction # SyntaxTree.OutPort THEN
  6959. Error(left.position,"not an out-port")
  6960. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6961. Error(left.position,"incompatible to port type");
  6962. ELSE
  6963. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6964. communication.SetRight(right)
  6965. END;
  6966. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6967. IF CheckVariable(left) THEN
  6968. IF inPort.direction # SyntaxTree.InPort THEN
  6969. Error(left.position,"not an in-port")
  6970. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6971. Error(right.position,"incompatible to port type");
  6972. END;
  6973. END;
  6974. ELSE
  6975. Error(communication.position,"unsupported stream operation");
  6976. END;
  6977. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6978. IF outPort.direction # SyntaxTree.OutPort THEN
  6979. Error(left.position,"not an out-port")
  6980. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6981. Error(left.position,"incompatible to port type");
  6982. ELSE
  6983. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6984. communication.SetRight(right)
  6985. END;
  6986. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6987. IF CheckVariable(right) THEN
  6988. IF inPort.direction # SyntaxTree.InPort THEN
  6989. Error(left.position,"not an in-port")
  6990. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6991. Error(right.position,"incompatible to port type");
  6992. END;
  6993. END;
  6994. ELSE
  6995. Error(communication.position, "unsupported operation");
  6996. END;
  6997. END;
  6998. END VisitCommunicationStatement;
  6999. (** check and resolve if/eslif part
  7000. - check condition
  7001. - check statement sequence
  7002. **)
  7003. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7004. VAR prevUnreachable, b: BOOLEAN;
  7005. BEGIN
  7006. prevUnreachable := currentIsUnreachable;
  7007. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7008. IF IsBooleanValue(ifPart.condition,b) THEN
  7009. IF b=FALSE THEN
  7010. currentIsUnreachable := TRUE
  7011. ELSIF b=TRUE THEN
  7012. true := TRUE
  7013. END;
  7014. END;
  7015. StatementSequence(ifPart.statements);
  7016. currentIsUnreachable := prevUnreachable;
  7017. END IfPart;
  7018. (** check and resolve if statement
  7019. - check if parts and else part statement sequence
  7020. **)
  7021. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7022. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7023. BEGIN
  7024. prevUnreachable := currentIsUnreachable;
  7025. ifPartTrue := FALSE;
  7026. IfPart(ifStatement.ifPart,ifPartTrue);
  7027. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7028. elsif := ifStatement.GetElsifPart(i);
  7029. IfPart(elsif,ifPartTrue);
  7030. END;
  7031. IF ifStatement.elsePart # NIL THEN
  7032. IF ifPartTrue THEN
  7033. currentIsUnreachable := TRUE
  7034. END;
  7035. StatementSequence(ifStatement.elsePart)
  7036. END;
  7037. currentIsUnreachable := prevUnreachable;
  7038. END VisitIfStatement;
  7039. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7040. VAR variable: SyntaxTree.Designator;
  7041. type,variableType: SyntaxTree.Type;
  7042. withEntry: WithEntry;
  7043. BEGIN
  7044. variable := ResolveDesignator(withPart.variable);
  7045. variableType := variable.type.resolved;
  7046. withPart.SetVariable(variable);
  7047. type := ResolveType(withPart.type);
  7048. withPart.SetType(type);
  7049. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7050. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7051. END;
  7052. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7053. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7054. Error(variable.position,"is not extensible designator");
  7055. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7056. Error(variable.position,"is no local variable ");
  7057. IF VerboseErrorMessage THEN
  7058. Printout.Info("variable",variable)
  7059. END;
  7060. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7061. Error(variable.position,"withguarded symbol is no type extension of ");
  7062. IF VerboseErrorMessage THEN
  7063. Printout.Info("variable",variable);
  7064. Printout.Info("type",type);
  7065. END;
  7066. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7067. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7068. Error(variable.position,"withguarded symbol is no variable ");
  7069. IF VerboseErrorMessage THEN
  7070. Printout.Info("variable",variable);
  7071. Printout.Info("type",type);
  7072. END;
  7073. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7074. Error(variable.position,"invalid change of withguarded symbol");
  7075. ELSE
  7076. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7077. NEW(withEntry);
  7078. withEntry.previous := withEntries;
  7079. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7080. withEntry.type := type;
  7081. withEntries := withEntry;
  7082. StatementSequence(withPart.statements);
  7083. withEntries := withEntries.previous;
  7084. END;
  7085. END WithPart;
  7086. (** check and resolve with statement WITH variable: type DO ... END;
  7087. - check type and variable
  7088. - check that variable type is type extension of type
  7089. - check that variable is a variable
  7090. - enter new with scope and enter guardedVariable with same name and reference to variable
  7091. - create if statement:
  7092. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7093. **)
  7094. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7095. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7096. BEGIN
  7097. prevScope := currentScope; symbol := NIL;
  7098. FOR i := 0 TO withStatement.WithParts()-1 DO
  7099. WithPart(withStatement.GetWithPart(i),symbol);
  7100. END;
  7101. IF withStatement.elsePart # NIL THEN
  7102. StatementSequence(withStatement.elsePart)
  7103. END;
  7104. currentScope := prevScope;
  7105. END VisitWithStatement;
  7106. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7107. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7108. - check 'first' < 'last' and no overlaps between different case labels
  7109. - check statement sequence
  7110. **)
  7111. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7112. VAR
  7113. i: LONGINT;
  7114. position: Position;
  7115. expression, left, right: SyntaxTree.Expression;
  7116. expressionType: SyntaxTree.Type;
  7117. l, r: LONGINT;
  7118. cl, cr: CHAR;
  7119. thiscases: SyntaxTree.CaseConstant;
  7120. BEGIN
  7121. thiscases := NIL;
  7122. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7123. expression := casePart.elements.GetExpression(i);
  7124. position := expression.position;
  7125. (* set context of range *)
  7126. IF expression IS SyntaxTree.RangeExpression THEN
  7127. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7128. END;
  7129. expression := ResolveExpression(expression);
  7130. IF expression = SyntaxTree.invalidExpression THEN
  7131. (* error already reported *)
  7132. expressionType := SyntaxTree.invalidType;
  7133. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7134. (* read out 'first' and 'last' *)
  7135. left := expression(SyntaxTree.RangeExpression).first;
  7136. right := expression(SyntaxTree.RangeExpression).last;
  7137. (* guaranteed by VisitRangeExpression: *)
  7138. ASSERT((left # NIL) & (right # NIL));
  7139. ASSERT(left.type.resolved = right.type.resolved);
  7140. left := CompatibleConversion(left.position, left, type);
  7141. right := CompatibleConversion(right.position, right, type);
  7142. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7143. expression(SyntaxTree.RangeExpression).SetLast(right);
  7144. expressionType := RegularType(position,left.type);
  7145. ELSE
  7146. expression := ConstantExpression(expression);
  7147. expression := CompatibleConversion(expression.position, expression, type);
  7148. (*
  7149. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7150. left := Global.NewCharacterValue(system,expression.position,cl);
  7151. expression := casePart.elements.GetExpression(i);
  7152. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7153. expression := left
  7154. END;
  7155. *)
  7156. casePart.elements.SetExpression(i,expression);
  7157. left := expression; right := expression;
  7158. expressionType := RegularType(position,expression.type)
  7159. END;
  7160. IF (expressionType = SyntaxTree.invalidType) THEN
  7161. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7162. Error(position, "inadmissible case label");
  7163. expression := SyntaxTree.invalidExpression;
  7164. ELSE
  7165. l := 0; r := 0;
  7166. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7167. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7168. l := ORD(cl); r := ORD(cr);
  7169. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7170. ELSE
  7171. expression := SyntaxTree.invalidExpression
  7172. END;
  7173. IF expression # SyntaxTree.invalidExpression THEN
  7174. IF l>r THEN
  7175. Error(position, "empty case label")
  7176. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7177. Error(position, "duplicate case label");
  7178. ELSE
  7179. IF l < min THEN min := l END;
  7180. IF r > max THEN max := r END;
  7181. END;
  7182. END;
  7183. END;
  7184. casePart.elements.SetExpression(i,expression);
  7185. END;
  7186. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7187. casePart.SetConstants(thiscases);
  7188. StatementSequence(casePart.statements);
  7189. END CasePart;
  7190. (** check and resolve case statement CASE variable OF ... END;
  7191. - check variable
  7192. - check case parts
  7193. **)
  7194. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7195. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7196. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7197. BEGIN
  7198. expression := ResolveExpression(caseStatement.variable);
  7199. type := RegularType(expression.position,expression.type);
  7200. IF type = SyntaxTree.invalidType THEN
  7201. expression := SyntaxTree.invalidExpression;
  7202. ELSIF IsIntegerType(type) THEN
  7203. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7204. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7205. (*
  7206. expression := Global.NewCharacterValue(system,expression.position,ch);
  7207. *)
  7208. type := expression.type;
  7209. ELSIF IsCharacterType(type) THEN
  7210. ELSIF IsEnumerationType(type) THEN
  7211. ELSE
  7212. Error(caseStatement.variable.position,"variable must be integer or character type");
  7213. expression := SyntaxTree.invalidExpression;
  7214. END;
  7215. caseStatement.SetVariable(expression);
  7216. caseList := NIL;
  7217. min := MAX(LONGINT); max := MIN(LONGINT);
  7218. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7219. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7220. END;
  7221. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7222. msg := "huge sparse case table ";
  7223. Strings.AppendInt(msg, max-min);
  7224. Strings.Append(msg,"/");
  7225. Strings.AppendInt(msg, caseStatement.CaseParts());
  7226. Warning(caseStatement.position,msg);
  7227. END;
  7228. caseStatement.SetMinMax(min,max);
  7229. StatementSequence(caseStatement.elsePart);
  7230. IF expression.resolved # NIL THEN
  7231. IF IsCharacterValue(expression,ch) THEN
  7232. l := ORD(ch)
  7233. ELSIF IsIntegerValue(expression,l) THEN
  7234. END;
  7235. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7236. END;
  7237. END VisitCaseStatement;
  7238. (** check and resolve while statement
  7239. - check condition
  7240. - check statement sequence
  7241. **)
  7242. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7243. VAR prevIsUnreachable,b: BOOLEAN;
  7244. BEGIN
  7245. prevIsUnreachable := currentIsUnreachable;
  7246. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7247. IF IsBooleanValue(whileStatement.condition,b) THEN
  7248. IF b=FALSE THEN
  7249. currentIsUnreachable := TRUE
  7250. END;
  7251. END;
  7252. StatementSequence(whileStatement.statements);
  7253. currentIsUnreachable := prevIsUnreachable
  7254. END VisitWhileStatement;
  7255. (** check and resolve repeat statement
  7256. - check condition
  7257. - check statement sequence
  7258. **)
  7259. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7260. BEGIN
  7261. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7262. StatementSequence(repeatStatement.statements);
  7263. END VisitRepeatStatement;
  7264. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7265. VAR withEntry: WithEntry;
  7266. BEGIN
  7267. withEntry := withEntries;
  7268. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7269. withEntry := withEntry.previous
  7270. END;
  7271. IF withEntry = NIL THEN RETURN FALSE
  7272. ELSE
  7273. type := withEntry.type;
  7274. RETURN TRUE
  7275. END;
  7276. END GetGuard;
  7277. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7278. - check that variable is an integer variable
  7279. - check that from is integer typed with compatible type
  7280. - check that to has compatible type
  7281. - check that by is constant integer with compatible type
  7282. **)
  7283. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7284. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7285. BEGIN
  7286. designator := ResolveDesignator(forStatement.variable);
  7287. type := SyntaxTree.invalidType;
  7288. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7289. designator := SyntaxTree.invalidDesignator;
  7290. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7291. Error(designator.position,"control variable of non-integer type");
  7292. designator := SyntaxTree.invalidDesignator;
  7293. ELSIF CheckVariable(designator) THEN
  7294. type := designator.type;
  7295. END;
  7296. forStatement.SetVariable(designator);
  7297. expression := ResolveExpression(forStatement.from);
  7298. IF expression = SyntaxTree.invalidExpression THEN
  7299. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7300. Error(expression.position,"start value of incompatible type");
  7301. expression := SyntaxTree.invalidExpression;
  7302. ELSIF type # SyntaxTree.invalidType THEN
  7303. expression := NewConversion(expression.position,expression,type,NIL)
  7304. END;
  7305. forStatement.SetFrom(expression);
  7306. expression := ResolveExpression(forStatement.to);
  7307. IF expression = SyntaxTree.invalidExpression THEN
  7308. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7309. Error(expression.position,"end value of incompatible type");
  7310. expression := SyntaxTree.invalidExpression;
  7311. ELSIF type # SyntaxTree.invalidType THEN
  7312. expression := NewConversion(expression.position,expression,type,NIL)
  7313. END;
  7314. forStatement.SetTo(expression);
  7315. IF forStatement.by # NIL THEN
  7316. expression := ConstantInteger(forStatement.by);
  7317. ELSE
  7318. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7319. END;
  7320. IF expression = SyntaxTree.invalidExpression THEN
  7321. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7322. Error(expression.position,"step value of incompatible type");
  7323. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7324. Error(expression.position,"invalid step value");
  7325. ELSIF type # SyntaxTree.invalidType THEN
  7326. expression := NewConversion(expression.position,expression,type,NIL)
  7327. END;
  7328. forStatement.SetBy(expression);
  7329. StatementSequence(forStatement.statements);
  7330. END VisitForStatement;
  7331. (** check and resolve loop statement LOOP StatementSequence END
  7332. - check statement sequence
  7333. **)
  7334. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7335. BEGIN
  7336. StatementSequence(loopStatement.statements)
  7337. END VisitLoopStatement;
  7338. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7339. BEGIN
  7340. StatementSequence(exitableBlock.statements);
  7341. END VisitExitableBlock;
  7342. (** check and resolve exit statement EXIT
  7343. - check that exit is within LOOP statement block
  7344. **)
  7345. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7346. VAR outer: SyntaxTree.Statement;
  7347. BEGIN
  7348. outer := exitStatement.outer;
  7349. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7350. outer := outer.outer;
  7351. END;
  7352. IF outer = NIL THEN
  7353. Error(exitStatement.position,"exit statement not within loop statement");
  7354. END;
  7355. END VisitExitStatement;
  7356. (** check and resolve return statement RETURN [expression]
  7357. - check expression (if any)
  7358. - check if in procedure scope
  7359. - if in procedure scope then check expression compatibility
  7360. - if not in procecdure scope then check on return without expression
  7361. **)
  7362. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7363. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7364. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7365. BEGIN
  7366. position := returnStatement.position;
  7367. expression := returnStatement.returnValue;
  7368. IF expression # NIL THEN
  7369. expression := ResolveExpression(expression);
  7370. returnStatement.SetReturnValue(expression);
  7371. END;
  7372. outer := returnStatement.outer;
  7373. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7374. outer := outer.outer
  7375. END;
  7376. IF (outer # NIL) THEN
  7377. scope := outer(SyntaxTree.Body).inScope;
  7378. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7379. IF (expression # NIL) THEN
  7380. Error(position, "return statement with parameter not in procedure scope");
  7381. END;
  7382. ELSE
  7383. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7384. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7385. Error(position, "return statement in procedure that does not return");
  7386. END;
  7387. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7388. IF returnType # NIL THEN
  7389. returnType := returnType.resolved;
  7390. IF expression = NIL THEN
  7391. Error(position, "empty return type in procedure providing a return type")
  7392. ELSIF expression.type = NIL THEN
  7393. Error(position,"returned type incompatible: expression has no type");
  7394. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7395. Error(position, "return type not compatible");
  7396. IF VerboseErrorMessage THEN
  7397. Printout.Info("returnType",returnType);
  7398. Printout.Info("expression",expression);
  7399. END;
  7400. ELSE
  7401. expression := NewConversion(expression.position,expression,returnType,NIL);
  7402. returnStatement.SetReturnValue(expression);
  7403. END;
  7404. ELSIF expression # NIL THEN
  7405. Error(position, "non-empty return type in procedure providing no return type");
  7406. END;
  7407. END;
  7408. END;
  7409. END VisitReturnStatement;
  7410. (** check and resolve await statement AWAIT(condition: Expression)
  7411. - check await condition
  7412. **)
  7413. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7414. VAR condition: SyntaxTree.Expression;
  7415. BEGIN
  7416. condition := ResolveCondition(awaitStatement.condition);
  7417. IF currentIsRealtime THEN
  7418. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7419. END;
  7420. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7421. Error(awaitStatement.position,"senseless await statement with constant condition");
  7422. END;
  7423. awaitStatement.SetCondition(condition);
  7424. END VisitAwaitStatement;
  7425. PROCEDURE CheckSystemImport(position: Position);
  7426. VAR import: SyntaxTree.Import;
  7427. BEGIN
  7428. import := currentScope.ownerModule.moduleScope.firstImport;
  7429. WHILE(import # NIL) DO
  7430. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7431. RETURN;
  7432. END;
  7433. import := import.nextImport;
  7434. END;
  7435. Error(position, "forbidden code without system import");
  7436. END CheckSystemImport;
  7437. (** check and resolve code statement: do nothing, must be done by assembler
  7438. **)
  7439. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7440. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7441. BEGIN
  7442. CheckSystemImport(code.position);
  7443. FOR i := 0 TO code.inRules.Length()-1 DO
  7444. statement := code.inRules.GetStatement(i);
  7445. IF statement IS SyntaxTree.Assignment THEN
  7446. WITH statement: SyntaxTree.Assignment DO
  7447. statement.SetRight(ResolveExpression(statement.right));
  7448. END;
  7449. ELSE
  7450. Error(statement.position, "can only be assignment")
  7451. END;
  7452. END;
  7453. FOR i := 0 TO code.outRules.Length()-1 DO
  7454. statement := code.outRules.GetStatement(i);
  7455. IF statement IS SyntaxTree.Assignment THEN
  7456. WITH statement: SyntaxTree.Assignment DO
  7457. statement.SetLeft(ResolveDesignator(statement.left));
  7458. END;
  7459. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7460. (* must be a reference to some register *)
  7461. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7462. ELSE
  7463. Printout.Info("out statement ", statement);
  7464. Error(statement.position, "(out) can only be assignment")
  7465. END;
  7466. END;
  7467. END VisitCode;
  7468. (** check and set flags of a statement block
  7469. - check for multiply occurence of a flag
  7470. - check and set priority only in bodies
  7471. - check for valid names
  7472. **)
  7473. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7474. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7475. flag: LONGINT; recordBody: SyntaxTree.Body;
  7476. PROCEDURE SetProtectedRecord;
  7477. VAR scope: SyntaxTree.Scope;
  7478. BEGIN
  7479. scope := currentScope;
  7480. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7481. scope := scope.outerScope
  7482. END;
  7483. IF scope # NIL THEN
  7484. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7485. END;
  7486. END SetProtectedRecord;
  7487. BEGIN
  7488. flags := {};
  7489. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7490. recordBody := block(SyntaxTree.Body)
  7491. ELSE
  7492. recordBody := NIL
  7493. END;
  7494. blockModifier := block.blockModifiers;
  7495. WHILE(blockModifier # NIL) DO
  7496. name := blockModifier.identifier;
  7497. expression := blockModifier.expression;
  7498. position := blockModifier.position;
  7499. flag := -1;
  7500. IF name=Global.NamePriority THEN
  7501. IF expression = NIL THEN
  7502. Error(position, "missing priority expression");
  7503. ELSIF recordBody = NIL THEN
  7504. Error(position, "priority not on record body");
  7505. ELSIF recordBody.priority # NIL THEN
  7506. Error(position, "duplicate priority expression");
  7507. ELSE
  7508. recordBody.SetPriority(expression);
  7509. END;
  7510. ELSIF expression # NIL THEN
  7511. Error(expression.position,"expression not in connection with priority")
  7512. ELSIF name=Global.NameExclusive THEN
  7513. IF block.isExclusive THEN
  7514. Error(position, "duplicate exclusive flag")
  7515. END;
  7516. block.SetExclusive(TRUE); SetProtectedRecord;
  7517. ELSIF name=Global.NameActive THEN
  7518. IF recordBody = NIL THEN
  7519. Error(position, "active not in record body");
  7520. ELSIF recordBody.isActive THEN
  7521. Error(position, "duplicate active flag")
  7522. ELSE
  7523. recordBody.SetActive(TRUE); SetProtectedRecord;
  7524. END;
  7525. ELSIF name=Global.NameSafe THEN
  7526. IF recordBody = NIL THEN
  7527. Error(position, "safe not in record body");
  7528. ELSIF recordBody.isSafe THEN
  7529. Error(position, "duplicate safe flag")
  7530. ELSE
  7531. recordBody.SetSafe(TRUE);
  7532. SetProtectedRecord;
  7533. END;
  7534. ELSIF name=Global.NameRealtime THEN
  7535. IF recordBody = NIL THEN
  7536. Error(position, "realtime not in record body");
  7537. ELSIF recordBody.isRealtime THEN
  7538. Error(position, "duplicate realtime flag")
  7539. ELSE
  7540. recordBody.SetRealtime(TRUE);
  7541. block.SetRealtime(TRUE);
  7542. END;
  7543. ELSIF name=Global.NameUnchecked THEN
  7544. IF block.isUnchecked THEN
  7545. Error(position, "duplicate unchecked flag")
  7546. ELSE
  7547. block.SetUnchecked(TRUE);
  7548. END;
  7549. ELSIF (name=Global.NameUncooperative) THEN
  7550. IF block.isUncooperative THEN
  7551. Error(position, "duplicate uncooperative flag")
  7552. ELSE
  7553. block.SetUncooperative(TRUE);
  7554. END;
  7555. ELSE
  7556. Error(position, "unknown block modifier");
  7557. END;
  7558. blockModifier := blockModifier.nextModifier;
  7559. END;
  7560. END BlockFlags;
  7561. (** check and resolve statement block
  7562. - check flags (exclusive)
  7563. - check statement sequence
  7564. **)
  7565. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7566. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7567. BEGIN
  7568. BlockFlags(statementBlock);
  7569. IF statementBlock.isExclusive THEN
  7570. (* check that not in exclusive block *)
  7571. IF currentIsExclusive THEN
  7572. Error (statementBlock.position,"forbidden recursive exclusive")
  7573. ELSIF currentIsRealtime THEN
  7574. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7575. END;
  7576. END;
  7577. recentExclusive := currentIsExclusive;
  7578. recentUnreachable := currentIsUnreachable;
  7579. recentRealtime := currentIsRealtime;
  7580. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7581. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7582. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7583. StatementSequence(statementBlock.statements);
  7584. currentIsRealtime := recentRealtime;
  7585. currentIsExclusive := recentExclusive;
  7586. currentIsUnreachable := recentUnreachable;
  7587. END VisitStatementBlock;
  7588. (** check and resolve body
  7589. - check flags (active, priority, safe)
  7590. - check body and finally part
  7591. **)
  7592. PROCEDURE Body(body: SyntaxTree.Body);
  7593. BEGIN
  7594. VisitStatementBlock(body);
  7595. IF body.isActive THEN
  7596. IF ~currentIsBodyProcedure THEN
  7597. Error(body.position,"active flag not in object body");
  7598. ELSIF body.priority # NIL THEN
  7599. body.SetPriority(ConstantInteger(body.priority));
  7600. END;
  7601. ELSIF body.isSafe THEN
  7602. Error(body.position,"safe flag not in active body");
  7603. ELSIF body.priority # NIL THEN
  7604. Error(body.position,"priority flag not in active body");
  7605. END;
  7606. IF body.code # NIL THEN
  7607. CheckSystemImport(body.position);
  7608. END;
  7609. StatementSequence(body.finally)
  7610. END Body;
  7611. (*** scopes ***)
  7612. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7613. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7614. VAR duplicateSymbol: BOOLEAN;
  7615. BEGIN
  7616. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7617. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7618. Error(symbol.position,"globally defined keyword")
  7619. END;
  7620. scope.EnterSymbol(symbol,duplicateSymbol);
  7621. IF ~allowDuplicate & duplicateSymbol THEN
  7622. Error(symbol.position,"Multiply defined identifier.");
  7623. IF VerboseErrorMessage THEN
  7624. Printout.Info("multiply defined identifier",symbol);
  7625. Printout.Info("in scope",scope);
  7626. END;
  7627. END;
  7628. END Register;
  7629. (**
  7630. implementation: check and resolve an implementation part
  7631. **)
  7632. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7633. move implementation checker to a separate object ? *)
  7634. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7635. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7636. BEGIN
  7637. prevIsRealtime := currentIsRealtime;
  7638. prevIsBodyProcedure := currentIsBodyProcedure;
  7639. prevIsCellNet := currentIsCellNet;
  7640. prevScope := currentScope;
  7641. currentScope := scope;
  7642. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7643. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7644. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7645. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7646. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7647. (*
  7648. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7649. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7650. END;
  7651. *)
  7652. END;
  7653. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7654. (* module body, record bodies are wrapped into an artifical procedure *)
  7655. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7656. Body(scope(SyntaxTree.ProcedureScope).body)
  7657. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7658. Body(scope(SyntaxTree.ProcedureScope).body)
  7659. END;
  7660. END;
  7661. currentScope := prevScope;
  7662. currentIsRealtime := prevIsRealtime;
  7663. currentIsBodyProcedure := prevIsBodyProcedure;
  7664. currentIsCellNet := prevIsCellNet;
  7665. END Implementation;
  7666. (** implementation phase:
  7667. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7668. **)
  7669. PROCEDURE Implementations(x: SyntaxTree.Module);
  7670. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7671. BEGIN
  7672. prevPhase := phase;
  7673. phase := InlinePhase;
  7674. scope := x.firstScope;
  7675. WHILE(scope # NIL) DO
  7676. Implementation(scope);
  7677. scope := scope.nextScope;
  7678. END;
  7679. phase := ImplementationPhase;
  7680. scope := x.firstScope;
  7681. WHILE(scope # NIL) DO
  7682. Implementation(scope);
  7683. scope := scope.nextScope;
  7684. END;
  7685. phase := prevPhase;
  7686. END Implementations;
  7687. (** declaration phase:
  7688. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7689. - import lists (for module scopes)
  7690. - parameter list (for procedure scopes)
  7691. - constant declarations
  7692. - type declarations
  7693. - variable declarations
  7694. - procedure declarations
  7695. preformed in two stages:
  7696. - first all symbols are entered into the symbol table (with uniqueness check),
  7697. - then all symbols are resolved
  7698. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7699. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7700. **)
  7701. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7702. VAR
  7703. constant: SyntaxTree.Constant;
  7704. typeDeclaration: SyntaxTree.TypeDeclaration;
  7705. variable: SyntaxTree.Variable;
  7706. procedure: SyntaxTree.Procedure;
  7707. procedureType : SyntaxTree.ProcedureType;
  7708. prevScope: SyntaxTree.Scope;
  7709. parameter: SyntaxTree.Parameter;
  7710. import: SyntaxTree.Import;
  7711. symbol: SyntaxTree.Symbol;
  7712. prevPhase: LONGINT;
  7713. prevError : BOOLEAN;
  7714. i: LONGINT;
  7715. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7716. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7717. BEGIN
  7718. IF type.baseType # NIL THEN
  7719. baseType := type.baseType.resolved;
  7720. IF baseType IS SyntaxTree.PointerType THEN
  7721. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7722. END;
  7723. (*
  7724. IF baseType IS SyntaxTree.CellType THEN
  7725. DeclareCell(baseType(SyntaxTree.CellType));
  7726. END;
  7727. *)
  7728. END;
  7729. parameter := type.firstParameter;
  7730. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7731. (*
  7732. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7733. variable.SetType(parameter.type);
  7734. variable.SetAccess(SyntaxTree.Hidden);
  7735. variable.SetModifiers(parameter.modifiers);
  7736. currentScope.PushVariable(variable);
  7737. *)
  7738. Register(parameter,scope, FALSE);
  7739. parameter := parameter.nextParameter;
  7740. END;
  7741. property := type.firstProperty;
  7742. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7743. (*
  7744. variable := currentScope.FindVariable(property.name);
  7745. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7746. prop := variable(SyntaxTree.Property);
  7747. ELSE (* add, duplicate symbols detection later *)
  7748. prop := SyntaxTree.NewProperty(property.position, property.name);
  7749. currentScope.PushVariable(prop);
  7750. END;
  7751. prop.SetType(property.type);
  7752. prop.SetValue(property.value);
  7753. prop.SetAccess(SyntaxTree.Hidden);
  7754. *)
  7755. Register(property, scope, FALSE);
  7756. property := property.nextProperty;
  7757. END;
  7758. END DeclareCell;
  7759. BEGIN
  7760. prevError := error;
  7761. prevPhase := phase;
  7762. phase := DeclarationPhase;
  7763. prevScope := currentScope;
  7764. currentScope := scope;
  7765. error := FALSE;
  7766. (* first enter all symbols in scope *)
  7767. IF scope IS SyntaxTree.ModuleScope THEN
  7768. (* treat imports first for a module scope, , set default context if necessary *)
  7769. import := scope(SyntaxTree.ModuleScope).firstImport;
  7770. WHILE(import # NIL) DO
  7771. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7772. Register(import, currentScope, FALSE);
  7773. import := import.nextImport;
  7774. END;
  7775. import := scope(SyntaxTree.ModuleScope).firstImport;
  7776. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7777. ResolveSymbol(import);
  7778. import := import.nextImport;
  7779. END;
  7780. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7781. (* enter parameters for a procedure scope *)
  7782. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7783. parameter := procedureType.firstParameter;
  7784. WHILE(parameter # NIL) DO
  7785. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7786. END;
  7787. parameter := procedureType.returnParameter;
  7788. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7789. parameter := procedureType.selfParameter;
  7790. IF parameter # NIL THEN
  7791. Register(parameter, currentScope, FALSE);
  7792. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7793. END;
  7794. ELSIF scope IS SyntaxTree.CellScope THEN
  7795. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7796. IF~skipImplementation THEN
  7797. import := scope(SyntaxTree.CellScope).firstImport;
  7798. WHILE(import # NIL) DO
  7799. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7800. Register(import, currentScope, FALSE);
  7801. import := import.nextImport;
  7802. END;
  7803. import := scope(SyntaxTree.CellScope).firstImport;
  7804. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7805. ResolveSymbol(import);
  7806. import := import.nextImport;
  7807. END;
  7808. END;
  7809. END;
  7810. IF error THEN RETURN END;
  7811. IF skipImplementation THEN
  7812. scope.Clear;
  7813. END;
  7814. (* constants *)
  7815. constant := scope.firstConstant;
  7816. WHILE (constant # NIL) DO
  7817. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7818. END;
  7819. (* type declarations *)
  7820. typeDeclaration := scope.firstTypeDeclaration;
  7821. WHILE (typeDeclaration # NIL) DO
  7822. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7823. END;
  7824. (* variables *)
  7825. variable := scope.firstVariable;
  7826. WHILE (variable # NIL) DO
  7827. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7828. END;
  7829. (* procedures *)
  7830. IF scope.procedures # NIL THEN
  7831. FOR i := 0 TO scope.procedures.Length()-1 DO
  7832. procedure := scope.procedures.GetProcedure(i);
  7833. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7834. IF procedureType.selfParameter = NIL THEN
  7835. scope.AddProcedure(procedure);
  7836. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7837. ELSE
  7838. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7839. IF typeDeclaration = NIL THEN
  7840. Error(procedureType.selfParameter.position, "No such type declaration");
  7841. ELSE
  7842. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7843. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7844. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7845. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7846. END;
  7847. END;
  7848. END;
  7849. END;
  7850. (*
  7851. procedure := scope.firstProcedure;
  7852. WHILE (procedure # NIL) DO
  7853. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7854. IF procedureType.selfParameter = NIL THEN
  7855. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7856. END;
  7857. procedure := procedure.nextProcedure;
  7858. END;
  7859. *)
  7860. (* type bound procedures *)
  7861. (*
  7862. procedure := scope.firstProcedure;
  7863. WHILE (procedure # NIL) DO
  7864. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7865. IF procedureType.selfParameter # NIL THEN
  7866. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7867. IF typeDeclaration = NIL THEN
  7868. Error(procedureType.selfParameter.position, "No such type declaration");
  7869. ELSE
  7870. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7871. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7872. END;
  7873. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7874. END;
  7875. procedure := procedure.nextProcedure;
  7876. END;
  7877. *)
  7878. (* now process all symbols without any presumption on the order *)
  7879. symbol := scope.firstSymbol;
  7880. WHILE(symbol # NIL) DO
  7881. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7882. ResolveSymbol(symbol);
  7883. END;
  7884. symbol := symbol.nextSymbol;
  7885. END;
  7886. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7887. symbol := scope.firstSymbol;
  7888. WHILE symbol # NIL DO
  7889. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7890. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7891. pointerFixes.Add(symbol, currentScope);
  7892. END;
  7893. IF ~symbol.type.resolved.isRealtime THEN
  7894. Error(symbol.position,"symbol has no realtime type");
  7895. END;
  7896. END;
  7897. symbol := symbol.nextSymbol
  7898. END;
  7899. END;
  7900. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7901. Error(Basic.invalidPosition,"problems during offset computation in module");
  7902. END;
  7903. IF (scope.ownerModule # NIL) THEN
  7904. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7905. scope.ownerModule.AddScope(scope);
  7906. END;
  7907. phase := prevPhase;
  7908. currentScope := prevScope;
  7909. error := error OR prevError;
  7910. END Declarations;
  7911. (* nopov *)
  7912. (** check if all operators from one module are compatible to the ones in the other module
  7913. - check if there are not multiple operators with the same signature
  7914. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7915. - check for all operators whose signatures are compatible, whether the return types are compatible
  7916. note that:
  7917. - the return type is not considered to be part of the signature
  7918. - two signatures are considered compatible, if all of the operands are compatible
  7919. **)
  7920. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7921. VAR
  7922. thisOperator, thatOperator: SyntaxTree.Operator;
  7923. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7924. thisParameter, thatParameter: SyntaxTree.Parameter;
  7925. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7926. i: LONGINT;
  7927. BEGIN
  7928. currentScope := thisModuleScope;
  7929. hasError := FALSE;
  7930. (* go through all operators in the other module *)
  7931. thatOperator := thatModuleScope.firstOperator;
  7932. WHILE (thatOperator # NIL) & ~hasError DO
  7933. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7934. (* the other operator is accessible *)
  7935. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7936. (* the other operator is not the conversion operator *)
  7937. (* go through all operators in this module *)
  7938. thisOperator := thisModuleScope.firstOperator;
  7939. WHILE (thisOperator # NIL) & ~hasError DO
  7940. IF thisOperator # thatOperator THEN
  7941. (* the operators are not the same *)
  7942. IF thisOperator.name = thatOperator.name THEN
  7943. (* the operators share the same identifier *)
  7944. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7945. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7946. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7947. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7948. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7949. (* both operators have the same paramter count *)
  7950. thisParameter := thisProcedureType.firstParameter;
  7951. thatParameter := thatProcedureType.firstParameter;
  7952. operandsAreEqual := TRUE;
  7953. operandsAreCompatible := TRUE;
  7954. (* go through all parameters *)
  7955. FOR i := 1 TO thisProcedureType.numberParameters DO
  7956. ASSERT(thatParameter # NIL);
  7957. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7958. operandsAreEqual := FALSE;
  7959. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7960. operandsAreCompatible := FALSE
  7961. END
  7962. END;
  7963. thisParameter := thisParameter.nextParameter;
  7964. thatParameter := thatParameter.nextParameter
  7965. END;
  7966. IF operandsAreEqual THEN
  7967. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7968. hasError := TRUE
  7969. ELSIF operandsAreCompatible THEN
  7970. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7971. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7972. hasError := TRUE
  7973. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7974. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7975. hasError := TRUE
  7976. END
  7977. END
  7978. END
  7979. END
  7980. END;
  7981. thisOperator := thisOperator.nextOperator
  7982. END
  7983. END
  7984. END;
  7985. thatOperator := thatOperator.nextOperator
  7986. END
  7987. END CheckInterOperatorConformity;
  7988. (** check module:
  7989. - check module declaration
  7990. - add context, if necessary
  7991. - remove module from import cache, if necessary
  7992. - check declarations
  7993. - resolve all type fixes
  7994. - check implementation (bodies)
  7995. **)
  7996. PROCEDURE Module*(x: SyntaxTree.Module);
  7997. VAR (* nopov *)
  7998. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7999. BEGIN
  8000. prevScope := currentScope;
  8001. prevIsCellNet := currentIsCellNet;
  8002. module := x;
  8003. ASSERT(x # NIL);
  8004. global := system.globalScope[x.case];
  8005. x.moduleScope.SetGlobalScope(global);
  8006. currentScope := global;
  8007. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8008. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8009. RemoveModuleFromCache(importCache,x);
  8010. Declarations(x.moduleScope, FALSE);
  8011. FixTypes();
  8012. IF module.isCellNet THEN
  8013. currentIsCellNet := TRUE;
  8014. modifier := x.modifiers;
  8015. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8016. CheckModifiers(modifier, FALSE);
  8017. END;
  8018. (* nopov *)
  8019. IF ~error THEN
  8020. (* check if operators conform to each other within this module *)
  8021. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8022. (* go through all imports *)
  8023. import := x.moduleScope.firstImport;
  8024. WHILE import # NIL DO
  8025. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8026. (* check if all operators in this module conform to the ones of the imported module *)
  8027. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8028. END;
  8029. import := import.nextImport
  8030. END;
  8031. END;
  8032. Implementations(x);
  8033. module := NIL;
  8034. currentIsCellNet := prevIsCellNet;
  8035. currentScope := prevScope;
  8036. END Module;
  8037. END Checker;
  8038. Warnings*=OBJECT (SyntaxTree.Visitor)
  8039. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8040. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8041. BEGIN
  8042. SELF.diagnostics := diagnostics
  8043. END InitWarnings;
  8044. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8045. BEGIN END VisitPortType;
  8046. (** types *)
  8047. PROCEDURE Type(x: SyntaxTree.Type);
  8048. BEGIN x.Accept(SELF)
  8049. END Type;
  8050. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8051. BEGIN END VisitType;
  8052. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8053. BEGIN END VisitBasicType;
  8054. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8055. BEGIN END VisitCharacterType;
  8056. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8057. BEGIN END VisitIntegerType;
  8058. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8059. BEGIN END VisitFloatType;
  8060. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8061. BEGIN END VisitQualifiedType;
  8062. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8063. BEGIN END VisitStringType;
  8064. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8065. BEGIN END VisitEnumerationType;
  8066. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8067. BEGIN END VisitRangeType;
  8068. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8069. BEGIN
  8070. IF ~(SyntaxTree.Warned IN x.state) THEN
  8071. x.SetState(SyntaxTree.Warned);
  8072. Type(x.arrayBase);
  8073. END;
  8074. END VisitArrayType;
  8075. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8076. BEGIN
  8077. IF ~(SyntaxTree.Warned IN x.state) THEN
  8078. x.SetState(SyntaxTree.Warned);
  8079. Type(x.arrayBase);
  8080. END;
  8081. END VisitMathArrayType;
  8082. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8083. BEGIN
  8084. IF ~(SyntaxTree.Warned IN x.state) THEN
  8085. x.SetState(SyntaxTree.Warned);
  8086. Type(x.pointerBase);
  8087. END;
  8088. END VisitPointerType;
  8089. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8090. BEGIN Scope(x.recordScope) END VisitRecordType;
  8091. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8092. BEGIN Scope(x.cellScope) END VisitCellType;
  8093. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8094. BEGIN END VisitProcedureType;
  8095. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8096. VAR msg: ARRAY 256 OF CHAR;
  8097. BEGIN
  8098. Global.GetSymbolName(x,msg);
  8099. Strings.Append(msg," ");
  8100. Strings.Append(msg,text);
  8101. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8102. END Warning;
  8103. (** symbols *)
  8104. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8105. BEGIN
  8106. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8107. IF ~(x IS SyntaxTree.Parameter) THEN
  8108. Warning(x,"never used");
  8109. END;
  8110. END;
  8111. x.Accept(SELF);
  8112. END Symbol;
  8113. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8114. BEGIN END VisitSymbol;
  8115. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8116. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8117. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8118. BEGIN END VisitConstant;
  8119. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8120. BEGIN END VisitVariable;
  8121. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8122. BEGIN END VisitProperty;
  8123. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8124. BEGIN END VisitParameter;
  8125. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8126. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8127. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8128. BEGIN END VisitOperator;
  8129. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8130. BEGIN END VisitImport;
  8131. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8132. VAR
  8133. symbol: SyntaxTree.Symbol;
  8134. BEGIN
  8135. symbol := scope.firstSymbol;
  8136. WHILE(symbol # NIL) DO
  8137. Symbol(symbol);
  8138. symbol := symbol.nextSymbol;
  8139. END;
  8140. END Scope;
  8141. PROCEDURE Module*(x: SyntaxTree.Module);
  8142. BEGIN
  8143. SELF.module := x;
  8144. Scope(x.moduleScope);
  8145. END Module;
  8146. END Warnings;
  8147. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8148. BEGIN
  8149. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8150. END IsOberonInline;
  8151. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8152. BEGIN
  8153. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8154. END Resolved;
  8155. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8156. VAR i: LONGINT;
  8157. BEGIN
  8158. i := 1;
  8159. WHILE i < x DO
  8160. i := i *2
  8161. END;
  8162. RETURN i=x
  8163. END PowerOf2;
  8164. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8165. BEGIN
  8166. RETURN
  8167. (scope # NIL) &
  8168. (scope IS SyntaxTree.ModuleScope)
  8169. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8170. OR
  8171. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8172. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8173. END IsCellNetScope;
  8174. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8175. BEGIN
  8176. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8177. END IsCellScope;
  8178. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8179. BEGIN
  8180. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8181. RETURN (scope # NIL) & IsCellNetScope(scope)
  8182. END InCellNetScope;
  8183. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8184. BEGIN
  8185. ASSERT(size MOD system.dataUnit = 0);
  8186. RETURN size DIV system.dataUnit
  8187. END ToMemoryUnits;
  8188. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8189. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8190. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8191. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8192. BEGIN
  8193. IF t = NIL THEN
  8194. RETURN TRUE
  8195. ELSE
  8196. t := t.resolved;
  8197. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8198. END;
  8199. END TypeAllowed;
  8200. BEGIN
  8201. type := type.resolved;
  8202. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8203. RETURN FALSE
  8204. ELSE
  8205. procedureType := type(SyntaxTree.ProcedureType);
  8206. numberParameters := procedureType.numberParameters;
  8207. RETURN
  8208. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8209. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8210. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8211. END;
  8212. END GetProcedureAllowed;
  8213. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8214. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8215. VAR import: SyntaxTree.Import;
  8216. BEGIN
  8217. import := importCache.ImportByModuleName(x.name,x.context);
  8218. IF import # NIL THEN
  8219. importCache.RemoveImporters(x.name,x.context);
  8220. END;
  8221. END RemoveModuleFromCache;
  8222. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8223. (* to <- this assignment compatibility *)
  8224. VAR result: BOOLEAN;
  8225. BEGIN
  8226. IF this= NIL THEN result := (to=NIL)
  8227. ELSIF to=NIL THEN result := FALSE
  8228. ELSE
  8229. (*! will be replaced by this:
  8230. ELSE result := this.CompatibleTo(to.resolved);
  8231. *)
  8232. this := this.resolved; to := to.resolved;
  8233. IF to=SyntaxTree.invalidType THEN result := FALSE
  8234. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8235. ELSIF to = this THEN
  8236. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8237. ELSIF to IS SyntaxTree.BasicType THEN
  8238. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8239. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8240. result := this.CompatibleTo(to.resolved)
  8241. ELSE
  8242. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8243. END
  8244. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8245. result := to.sizeInBits = this.sizeInBits;
  8246. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8247. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8248. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8249. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8250. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8251. result := TRUE;
  8252. ELSIF to IS SyntaxTree.AnyType THEN
  8253. 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);
  8254. ELSIF to IS SyntaxTree.ObjectType THEN
  8255. 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 *) ;
  8256. ELSIF to IS SyntaxTree.ByteType THEN
  8257. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8258. ELSIF to IS SyntaxTree.CharacterType THEN
  8259. result := IsCharacterType(this)
  8260. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8261. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8262. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8263. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8264. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8265. result := TRUE;
  8266. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8267. result := TRUE;
  8268. ELSE
  8269. result := FALSE
  8270. END;
  8271. ELSIF to IS SyntaxTree.PointerType THEN
  8272. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8273. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8274. & (~to.isRealtime OR this.isRealtime);
  8275. ELSIF to IS SyntaxTree.ProcedureType THEN
  8276. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8277. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8278. & (~to.isRealtime OR this.isRealtime)
  8279. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8280. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8281. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8282. ELSIF to IS SyntaxTree.RecordType THEN
  8283. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8284. ELSIF to IS SyntaxTree.ArrayType THEN
  8285. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8286. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8287. ELSIF StaticArrayCompatible(to, this) THEN
  8288. result := TRUE
  8289. ELSE
  8290. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8291. END;
  8292. ELSIF to IS SyntaxTree.MathArrayType THEN
  8293. IF this IS SyntaxTree.MathArrayType THEN
  8294. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8295. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8296. result := TRUE;
  8297. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8298. result := TRUE;
  8299. ELSE
  8300. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8301. END;
  8302. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8303. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8304. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8305. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8306. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8307. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8308. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8309. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8310. ELSE
  8311. result := FALSE
  8312. END;
  8313. (* an array-structured object type is compatible to the type of its array structure *)
  8314. ELSIF IsArrayStructuredObjectType(this) THEN
  8315. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8316. ELSE
  8317. result := FALSE;
  8318. END;
  8319. ELSIF to IS SyntaxTree.StringType THEN
  8320. result := FALSE;
  8321. ELSIF to IS SyntaxTree.EnumerationType THEN
  8322. result := IsEnumerationExtension(this,to);
  8323. ELSIF to IS SyntaxTree.PortType THEN
  8324. result := SameType(to, this)
  8325. ELSE
  8326. Printout.Info("CompatibleTo",to);
  8327. HALT(100); (* implement missing type check *)
  8328. END;
  8329. END;
  8330. RETURN result
  8331. END CompatibleTo;
  8332. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8333. VAR actualBase, formalBase: SyntaxTree.Type;
  8334. BEGIN
  8335. IF SameType(formal,actual) THEN
  8336. RETURN TRUE
  8337. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8338. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8339. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8340. RETURN
  8341. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8342. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8343. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8344. & StaticArrayCompatible(formalBase,actualBase)
  8345. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8346. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8347. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8348. RETURN
  8349. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8350. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8351. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8352. & StaticArrayCompatible(formalBase,actualBase)
  8353. ELSE RETURN FALSE
  8354. END;
  8355. END StaticArrayCompatible;
  8356. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8357. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8358. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8359. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8360. BEGIN
  8361. result := SameType(formal,actual);
  8362. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8363. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8364. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8365. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8366. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8367. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8368. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8369. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8370. & TC(formalBase, actualBase);
  8371. END;
  8372. RETURN result
  8373. END TC;
  8374. BEGIN
  8375. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8376. ELSE
  8377. arrayBase := formalType.arrayBase.resolved;
  8378. IF (actualType IS SyntaxTree.StringType) THEN
  8379. result := arrayBase IS SyntaxTree.CharacterType
  8380. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8381. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8382. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8383. result := TC(formalType, actualType);
  8384. ELSE
  8385. result := (arrayBase IS SyntaxTree.ByteType)
  8386. END;
  8387. END;
  8388. RETURN result
  8389. END OpenArrayCompatible;
  8390. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8391. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8392. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8393. BEGIN
  8394. IF actualType IS SyntaxTree.MathArrayType THEN
  8395. actualArray := actualType(SyntaxTree.MathArrayType);
  8396. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8397. (*
  8398. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8399. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8400. *)
  8401. actualBase := ArrayBase(actualType,Infinity);
  8402. formalBase := ArrayBase(formalType,Infinity);
  8403. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8404. ELSE
  8405. (*
  8406. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8407. *)
  8408. formalBase := Resolved(formalType.arrayBase);
  8409. actualBase := Resolved(actualArray.arrayBase);
  8410. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8411. (*
  8412. ARRAY [k] -> ARRAY [n]
  8413. *)
  8414. result := (formalType.staticLength = actualArray.staticLength)
  8415. ELSE
  8416. result := TRUE
  8417. END;
  8418. IF ~result THEN
  8419. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8420. ELSIF actualBase = NIL THEN result := FALSE
  8421. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8422. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8423. ELSE
  8424. result := SameType(formalBase,actualBase)
  8425. END;
  8426. END;
  8427. ELSE
  8428. result := FALSE
  8429. END;
  8430. RETURN result
  8431. END MathArrayCompatible;
  8432. (**
  8433. Math Array Type distance for assignments / parameter passings of the form
  8434. from -> to
  8435. variants:
  8436. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8437. allowed:
  8438. static -> static (& size match)
  8439. static -> open
  8440. static -> tensor
  8441. open -> open
  8442. open -> tensor
  8443. open -> static
  8444. tensor -> tensor
  8445. tensor -> open
  8446. tensor -> static
  8447. **)
  8448. (*! think about the metric here: is form matching more important than element type matching? *)
  8449. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8450. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8451. BEGIN
  8452. fromBase := Resolved(from.arrayBase);
  8453. toBase := Resolved(to.arrayBase);
  8454. i := Infinity;
  8455. IF from = to THEN
  8456. i := 0;
  8457. ELSIF (from.form = to.form) THEN
  8458. (* static -> static, open -> open, tensor -> tensor *)
  8459. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8460. IF fromBase = toBase THEN i := 0
  8461. ELSIF toBase = NIL THEN i := 1
  8462. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8463. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8464. ELSE
  8465. i := TypeDistance(system,fromBase, toBase, varpar);
  8466. END;
  8467. END;
  8468. ELSIF (to.form = SyntaxTree.Static) THEN
  8469. (* forbidden *)
  8470. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8471. (* static -> tensor, open -> tensor, tensor -> open *)
  8472. IF toBase=fromBase THEN i := 0;
  8473. ELSIF toBase = NIL THEN i := 1;
  8474. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8475. toBase := ArrayBase(toBase,Infinity);
  8476. IF (fromBase=toBase) THEN i := 0
  8477. ELSIF (toBase = NIL) THEN i:= 1
  8478. ELSIF (fromBase = NIL) THEN i := Infinity;
  8479. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8480. END;
  8481. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8482. fromBase := ArrayBase(fromBase,Infinity);
  8483. IF (fromBase=toBase) THEN i := 0
  8484. ELSIF (toBase = NIL) THEN i := 1
  8485. ELSIF (fromBase = NIL) THEN i := Infinity;
  8486. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8487. END;
  8488. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8489. END;
  8490. IF i # Infinity THEN INC(i,2) END;
  8491. ELSIF (from.form = SyntaxTree.Static) THEN
  8492. (* static -> open *)
  8493. IF toBase=fromBase THEN i := 0
  8494. ELSIF toBase = NIL THEN i := 1
  8495. ELSIF fromBase = NIL THEN i := Infinity
  8496. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8497. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8498. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8499. END;
  8500. IF i # Infinity THEN INC(i,1) END;
  8501. ELSE HALT(100); (* unknown case *)
  8502. END;
  8503. RETURN i;
  8504. END MathArrayTypeDistance;
  8505. (** compute and return the distance of two array types
  8506. - return the distance of the base types
  8507. **)
  8508. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8509. VAR i: LONGINT;
  8510. BEGIN
  8511. i := Infinity;
  8512. IF from = to THEN
  8513. i := 0
  8514. ELSE
  8515. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8516. (*
  8517. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8518. i := TypeDistance(from.base, to.base);
  8519. IF i >= 0 THEN INC(i) END
  8520. ELSIF (from.mode = open) & (to.mode = open) THEN
  8521. i := TypeDistance(from.base, to.base);
  8522. *)
  8523. END;
  8524. RETURN i
  8525. END ArrayTypeDistance;
  8526. (** compute the signature distance of a procedure and an actual parameter list
  8527. - if any of the parameters are not compatible, the result is infinite
  8528. - add up and return the distance over all parameters
  8529. **)
  8530. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8531. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8532. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8533. BEGIN
  8534. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8535. result := Infinity
  8536. ELSE
  8537. formalParameter := procedureType.firstParameter;
  8538. i := 0;
  8539. result := 0;
  8540. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8541. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8542. actualParameter := actualParameters.GetExpression(i);
  8543. ASSERT(formalParameter.type # NIL);
  8544. IF (actualParameter.type = NIL) THEN distance := Infinity
  8545. ELSE
  8546. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8547. END;
  8548. IF distance = Infinity THEN
  8549. result := Infinity;
  8550. ELSE
  8551. to := formalParameter.type.resolved;
  8552. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8553. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8554. (* already handled varpar *)
  8555. (*
  8556. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8557. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8558. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8559. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8560. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8561. END;
  8562. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8563. result := Infinity
  8564. END;
  8565. *)
  8566. INC(result, distance);
  8567. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8568. INC(result, distance);
  8569. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8570. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8571. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8572. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8573. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8574. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8575. END;
  8576. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8577. result := Infinity
  8578. END;
  8579. ELSE
  8580. result := Infinity
  8581. END;
  8582. ELSE
  8583. INC(result,distance);
  8584. END;
  8585. END;
  8586. (*
  8587. Printout.Info("actual=", actualParameter);
  8588. Printout.Info("formal=", formalParameter);
  8589. TRACE(result);
  8590. *)
  8591. formalParameter := formalParameter.nextParameter; INC(i);
  8592. END;
  8593. END;
  8594. ASSERT(result >= 0);
  8595. RETURN result
  8596. END Distance;
  8597. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8598. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8599. BEGIN
  8600. IF right.numberParameters # (procedureType.numberParameters) THEN
  8601. result := Infinity
  8602. ELSE
  8603. formalParameter := procedureType.firstParameter;
  8604. rightParameter := right.firstParameter;
  8605. i := 0;
  8606. result := 0;
  8607. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8608. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8609. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8610. IF distance = Infinity THEN
  8611. result := Infinity;
  8612. ELSE
  8613. INC(result,distance);
  8614. END;
  8615. formalParameter := formalParameter.nextParameter;
  8616. rightParameter := rightParameter.nextParameter;
  8617. END;
  8618. END;
  8619. ASSERT(result >= 0);
  8620. RETURN result
  8621. END ProcedureTypeDistance;
  8622. (** compute and return the distance between two types, used for computation of signature distance
  8623. from -> to
  8624. **)
  8625. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8626. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8627. BEGIN
  8628. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8629. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8630. END;
  8631. i := Infinity;
  8632. IF from = to THEN
  8633. i := 0
  8634. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8635. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8636. i := Infinity;
  8637. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8638. i := 10;
  8639. ELSIF (from IS SyntaxTree.StringType) THEN
  8640. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8641. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8642. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8643. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8644. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8645. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8646. i := 1
  8647. ELSIF (from IS SyntaxTree.NilType) THEN
  8648. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8649. (*
  8650. ELSIF (from = NoType) THEN
  8651. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8652. *)
  8653. ELSIF (from IS SyntaxTree.BasicType) THEN
  8654. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8655. IF varpar & (i # 0) THEN i := Infinity END;
  8656. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8657. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8658. ELSIF (from IS SyntaxTree.RecordType) THEN
  8659. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8660. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8661. IF to IS SyntaxTree.MathArrayType THEN
  8662. (*
  8663. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8664. i := Infinity;
  8665. ELSE
  8666. *)
  8667. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8668. (*
  8669. END;
  8670. *)
  8671. END
  8672. ELSIF (from IS SyntaxTree.PointerType) THEN
  8673. ptr := from(SyntaxTree.PointerType);
  8674. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8675. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8676. (* ELSE i := TypeDistance(ptr.base, to); *)
  8677. END
  8678. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8679. IF (to IS SyntaxTree.ProcedureType) THEN
  8680. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8681. END;
  8682. ELSIF (from IS SyntaxTree.PortType) THEN
  8683. IF (to IS SyntaxTree.PortType) THEN
  8684. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8685. i := 0;
  8686. END;
  8687. END;
  8688. (*no procedure test, procedure must be the same*)
  8689. END;
  8690. RETURN i
  8691. END TypeDistance;
  8692. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8693. BEGIN
  8694. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8695. END IsIntegerType;
  8696. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8697. BEGIN
  8698. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8699. END IsAddressType;
  8700. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8701. BEGIN
  8702. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8703. END IsSizeType;
  8704. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8705. BEGIN
  8706. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8707. END IsSignedIntegerType;
  8708. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8709. BEGIN
  8710. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8711. END IsUnsignedIntegerType;
  8712. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8713. VAR result: BOOLEAN;
  8714. BEGIN
  8715. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8716. value := x.resolved(SyntaxTree.IntegerValue).value;
  8717. result := TRUE
  8718. ELSE
  8719. result := FALSE
  8720. END;
  8721. RETURN result
  8722. END IsIntegerValue;
  8723. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8724. VAR result: BOOLEAN;
  8725. BEGIN
  8726. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8727. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8728. result := TRUE
  8729. ELSE
  8730. result := FALSE
  8731. END;
  8732. RETURN result
  8733. END IsEnumerationValue;
  8734. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8735. VAR result: BOOLEAN;
  8736. BEGIN
  8737. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8738. value := x.resolved(SyntaxTree.RealValue).value;
  8739. result := TRUE
  8740. ELSE
  8741. result := FALSE
  8742. END;
  8743. RETURN result
  8744. END IsRealValue;
  8745. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8746. VAR result: BOOLEAN;
  8747. BEGIN
  8748. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8749. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8750. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8751. result := TRUE
  8752. ELSE
  8753. result := FALSE
  8754. END;
  8755. RETURN result
  8756. END IsComplexValue;
  8757. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8758. VAR result: BOOLEAN;
  8759. BEGIN
  8760. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8761. value := x.resolved(SyntaxTree.CharacterValue).value;
  8762. result := TRUE
  8763. ELSE
  8764. result := FALSE
  8765. END;
  8766. RETURN result
  8767. END IsCharacterValue;
  8768. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8769. VAR result: BOOLEAN;
  8770. BEGIN
  8771. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8772. value := x.resolved(SyntaxTree.BooleanValue).value;
  8773. result := TRUE
  8774. ELSE
  8775. result := FALSE
  8776. END;
  8777. RETURN result
  8778. END IsBooleanValue;
  8779. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8780. VAR result: BOOLEAN;
  8781. BEGIN
  8782. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8783. value := x.resolved(SyntaxTree.SetValue).value;
  8784. result := TRUE
  8785. ELSE
  8786. result := FALSE
  8787. END;
  8788. RETURN result
  8789. END IsSetValue;
  8790. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8791. VAR result: BOOLEAN;
  8792. BEGIN
  8793. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8794. value := x.resolved(SyntaxTree.StringValue).value;
  8795. result := TRUE
  8796. ELSE
  8797. result := FALSE
  8798. END;
  8799. RETURN result
  8800. END IsStringValue;
  8801. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8802. BEGIN
  8803. x := x.resolved;
  8804. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8805. END Indexable;
  8806. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8807. BEGIN
  8808. RETURN t1.SameType(t2.resolved);
  8809. END SameType;
  8810. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8811. BEGIN
  8812. IF t IS SyntaxTree.MathArrayType THEN
  8813. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8814. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8815. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8816. DEC(max);
  8817. END;
  8818. ELSIF t IS SyntaxTree.ArrayType THEN
  8819. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8820. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8821. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8822. END;
  8823. END;
  8824. RETURN t;
  8825. END ArrayBase;
  8826. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8827. BEGIN
  8828. type := type.resolved;
  8829. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8830. base := type(SyntaxTree.ArrayType).arrayBase;
  8831. RETURN TRUE;
  8832. END;
  8833. RETURN FALSE;
  8834. END IsOpenArray;
  8835. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8836. BEGIN
  8837. type := type.resolved;
  8838. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8839. base := type(SyntaxTree.ArrayType).arrayBase;
  8840. dim := type(SyntaxTree.ArrayType).staticLength;
  8841. RETURN TRUE
  8842. ELSE
  8843. RETURN FALSE
  8844. END;
  8845. END IsStaticArray;
  8846. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8847. BEGIN
  8848. type := type.resolved;
  8849. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8850. base := type(SyntaxTree.ArrayType).arrayBase;
  8851. RETURN TRUE
  8852. ELSE
  8853. RETURN FALSE
  8854. END;
  8855. END IsDynamicArray;
  8856. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8857. VAR i: LONGINT;
  8858. BEGIN
  8859. i := 0;
  8860. t := t.resolved;
  8861. IF t IS SyntaxTree.MathArrayType THEN
  8862. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8863. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8864. END;
  8865. ELSIF t IS SyntaxTree.ArrayType THEN
  8866. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8867. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8868. END;
  8869. END;
  8870. RETURN i
  8871. END Dimension;
  8872. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8873. BEGIN
  8874. RETURN expression.assignable;
  8875. END IsVariable;
  8876. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8877. BEGIN
  8878. IF (symbol IS SyntaxTree.Parameter) THEN
  8879. WITH symbol: SyntaxTree.Parameter DO
  8880. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8881. END;
  8882. ELSE
  8883. RETURN FALSE
  8884. END;
  8885. END IsVariableParameter;
  8886. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8887. VAR result: BOOLEAN;
  8888. BEGIN
  8889. IF type = NIL THEN result := FALSE
  8890. ELSE
  8891. type := type.resolved;
  8892. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8893. END;
  8894. RETURN result
  8895. END IsPointerType;
  8896. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8897. VAR result: BOOLEAN;
  8898. BEGIN
  8899. IF type = NIL THEN result := FALSE
  8900. ELSE
  8901. type := type.resolved;
  8902. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8903. END;
  8904. RETURN result
  8905. END IsUnsafePointer;
  8906. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8907. BEGIN
  8908. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8909. END IsDisposable;
  8910. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8911. VAR result: BOOLEAN;
  8912. BEGIN
  8913. IF type = NIL THEN result := FALSE
  8914. ELSE
  8915. type := type.resolved;
  8916. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8917. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8918. result := result OR (type IS SyntaxTree.ObjectType);
  8919. END;
  8920. RETURN result
  8921. END IsPointerToRecord;
  8922. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8923. VAR result: BOOLEAN;
  8924. BEGIN
  8925. IF type = NIL THEN result := FALSE
  8926. ELSE
  8927. type := type.resolved;
  8928. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8929. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8930. ;
  8931. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8932. result := result OR (type IS SyntaxTree.ObjectType);
  8933. END;
  8934. RETURN result
  8935. END IsPointerToObject;
  8936. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8937. BEGIN
  8938. IF type # NIL THEN
  8939. RETURN type.resolved.hasPointers
  8940. ELSE
  8941. RETURN FALSE
  8942. END;
  8943. END ContainsPointer;
  8944. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8945. BEGIN
  8946. IF type = NIL THEN RETURN FALSE END;
  8947. type := type.resolved;
  8948. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8949. END IsStringType;
  8950. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8951. BEGIN
  8952. IF type = NIL THEN RETURN FALSE END;
  8953. type := type.resolved;
  8954. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8955. END IsCharacterType;
  8956. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8957. BEGIN
  8958. IF type = NIL THEN RETURN FALSE END;
  8959. type := type.resolved;
  8960. RETURN (type IS SyntaxTree.EnumerationType)
  8961. END IsEnumerationType;
  8962. (** cf. section "Type extension (base type)" in the language report **)
  8963. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8964. VAR result: BOOLEAN;
  8965. BEGIN
  8966. ASSERT(base # NIL); ASSERT(extension # NIL);
  8967. base := base.resolved; extension := extension.resolved;
  8968. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8969. result := TRUE;
  8970. ELSE
  8971. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8972. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8973. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8974. END;
  8975. WHILE (extension # NIL) & (extension # base) DO
  8976. IF extension IS SyntaxTree.RecordType THEN
  8977. extension := extension(SyntaxTree.RecordType).baseType;
  8978. IF (extension # NIL) THEN extension := extension.resolved END;
  8979. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8980. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8981. END;
  8982. ELSE extension := NIL;
  8983. END;
  8984. END;
  8985. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8986. END;
  8987. RETURN result
  8988. END IsTypeExtension;
  8989. (** check if base is the base enumeration type of extension **)
  8990. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8991. BEGIN
  8992. base := base.resolved; extension := extension.resolved;
  8993. WHILE (extension # NIL) & (extension # base) DO
  8994. IF extension IS SyntaxTree.EnumerationType THEN
  8995. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8996. IF extension # NIL THEN extension := extension.resolved END;
  8997. ELSE
  8998. extension := NIL
  8999. END;
  9000. END;
  9001. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9002. END IsEnumerationExtension;
  9003. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9004. BEGIN
  9005. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9006. RETURN TRUE
  9007. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9008. RETURN TRUE
  9009. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9010. RETURN TRUE
  9011. ELSE
  9012. RETURN FALSE
  9013. END
  9014. END IsCallable;
  9015. (** compute and return the distance of two record types
  9016. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9017. **)
  9018. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9019. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9020. BEGIN
  9021. i := 0;
  9022. WHILE (from # NIL) & (from # to) DO
  9023. baseType := from.baseType;
  9024. IF (baseType # NIL) THEN
  9025. baseType := baseType.resolved;
  9026. IF baseType IS SyntaxTree.PointerType THEN
  9027. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9028. END;
  9029. IF baseType IS SyntaxTree.RecordType THEN
  9030. from := baseType(SyntaxTree.RecordType);
  9031. ELSE
  9032. from := NIL;
  9033. END;
  9034. ELSE
  9035. from := NIL
  9036. END;
  9037. INC(i)
  9038. END;
  9039. IF from = NIL THEN i := Infinity END;
  9040. RETURN i
  9041. END RecordTypeDistance;
  9042. (** compute and return the distance of two pointer types **)
  9043. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9044. BEGIN
  9045. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9046. RETURN Infinity;
  9047. ELSE
  9048. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9049. END;
  9050. END PointerTypeDistance;
  9051. (** check if expression contains a symbol designator pointing to a type declaration.
  9052. - if so then enter type declaration into typeDeclaration and return true else return false
  9053. **)
  9054. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9055. VAR result: BOOLEAN;
  9056. BEGIN
  9057. result := FALSE;
  9058. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9059. result := TRUE;
  9060. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9061. END;
  9062. RETURN result
  9063. END IsTypeDesignator;
  9064. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9065. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9066. VAR result: BOOLEAN;
  9067. BEGIN
  9068. type := type.resolved;
  9069. IF type IS SyntaxTree.PointerType THEN
  9070. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9071. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9072. result := TRUE
  9073. ELSE
  9074. result := type IS SyntaxTree.RecordType
  9075. END;
  9076. RETURN result
  9077. END IsExtensibleType;
  9078. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9079. BEGIN
  9080. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9081. (d IS SyntaxTree.SymbolDesignator) &
  9082. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9083. OR
  9084. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9085. END IsUnextensibleRecord;
  9086. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9087. BEGIN
  9088. IF IsUnextensibleRecord(d) THEN
  9089. RETURN FALSE
  9090. ELSE RETURN IsExtensibleType(d.type.resolved)
  9091. END;
  9092. END IsExtensibleDesignator;
  9093. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9094. BEGIN
  9095. type := type.resolved;
  9096. IF (type IS SyntaxTree.PointerType) THEN
  9097. RETURN TRUE
  9098. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9099. RETURN TRUE
  9100. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9101. RETURN TRUE
  9102. ELSIF (type IS SyntaxTree.BasicType) THEN
  9103. RETURN TRUE
  9104. END;
  9105. RETURN FALSE
  9106. END IsBasicType;
  9107. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9108. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9109. BEGIN
  9110. baseType := record.baseType;
  9111. IF (baseType # NIL) THEN
  9112. baseType := baseType.resolved;
  9113. IF (baseType IS SyntaxTree.PointerType) THEN
  9114. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9115. END;
  9116. END;
  9117. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9118. recordType := baseType(SyntaxTree.RecordType);
  9119. ELSE
  9120. recordType := NIL;
  9121. END;
  9122. RETURN recordType
  9123. END RecordBase;
  9124. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9125. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9126. BEGIN
  9127. baseRecord := RecordBase(scope.ownerRecord);
  9128. IF baseRecord = NIL THEN RETURN NIL END;
  9129. scope := baseRecord.recordScope;
  9130. procedureType := procedure.type.resolved;
  9131. IF procedure IS SyntaxTree.Operator THEN
  9132. operator := scope.firstOperator;
  9133. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9134. (*
  9135. Printout.Info("not same ",procedureType);
  9136. Printout.Info("with ",operator.type);
  9137. *)
  9138. operator := operator.nextOperator;
  9139. END;
  9140. super := operator;
  9141. ELSE
  9142. super := scope.firstProcedure;
  9143. WHILE (super # NIL) & (super.name # procedure.name) DO
  9144. super := super.nextProcedure;
  9145. END;
  9146. END;
  9147. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9148. RETURN super
  9149. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9150. RETURN super
  9151. ELSE
  9152. RETURN FindSuperProcedure(scope,procedure);
  9153. END;
  9154. END FindSuperProcedure;
  9155. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9156. VAR procedure: SyntaxTree.Procedure;
  9157. BEGIN
  9158. procedure := record.recordScope.constructor;
  9159. IF procedure = NIL THEN
  9160. record := RecordBase(record);
  9161. IF record # NIL THEN
  9162. procedure := GetConstructor(record)
  9163. END;
  9164. END;
  9165. RETURN procedure;
  9166. END GetConstructor;
  9167. (* enter a case into a list of cases in a sorted way and check for collision *)
  9168. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9169. VAR prev,this,new: SyntaxTree.CaseConstant;
  9170. BEGIN
  9171. this := root;
  9172. prev := NIL;
  9173. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9174. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9175. RETURN FALSE
  9176. ELSE
  9177. IF (this # NIL) & (this.min = max+1) THEN
  9178. this.min := min
  9179. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9180. prev.max := min
  9181. ELSE
  9182. NEW(new); new.min := min; new.max := max;
  9183. new.next := this;
  9184. IF prev = NIL THEN
  9185. root := new;
  9186. ELSE
  9187. prev.next := new
  9188. END
  9189. END;
  9190. RETURN TRUE
  9191. END;
  9192. END EnterCase;
  9193. (** generate and return a new checker object, errors are entered into diagnostics **)
  9194. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9195. VAR checker: Checker;
  9196. BEGIN
  9197. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9198. RETURN checker
  9199. END NewChecker;
  9200. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9201. VAR warnings: Warnings;
  9202. BEGIN
  9203. NEW(warnings, diagnostics); RETURN warnings;
  9204. END NewWarnings;
  9205. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9206. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9207. END IsRangeType;
  9208. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9209. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9210. END IsMathArrayType;
  9211. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9212. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9213. END IsArrayType;
  9214. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9215. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9216. END IsComplexType;
  9217. (** if a type is an array-structured object type *)
  9218. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9219. VAR recordType: SyntaxTree.RecordType;
  9220. BEGIN
  9221. IF type = NIL THEN
  9222. RETURN FALSE
  9223. ELSE
  9224. type := type.resolved;
  9225. IF type IS SyntaxTree.PointerType THEN
  9226. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9227. IF type IS SyntaxTree.RecordType THEN
  9228. recordType := type(SyntaxTree.RecordType);
  9229. RETURN recordType.isObject & recordType.HasArrayStructure()
  9230. ELSE
  9231. RETURN FALSE
  9232. END
  9233. ELSE
  9234. RETURN FALSE
  9235. END
  9236. END
  9237. END IsArrayStructuredObjectType;
  9238. (** the math array structure of a type
  9239. - for math arrays: the array itself
  9240. - for pointers: the math array structure of the pointer base
  9241. - for array-structured object types: the underlying structure
  9242. - for non-math arrays and all other types: NIL
  9243. **)
  9244. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9245. VAR
  9246. result: SyntaxTree.MathArrayType;
  9247. BEGIN
  9248. IF type = NIL THEN
  9249. result := NIL
  9250. ELSE
  9251. type := type.resolved;
  9252. IF type IS SyntaxTree.PointerType THEN
  9253. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9254. END;
  9255. IF type IS SyntaxTree.MathArrayType THEN
  9256. result := type(SyntaxTree.MathArrayType)
  9257. ELSIF type IS SyntaxTree.RecordType THEN
  9258. result := type(SyntaxTree.RecordType).arrayStructure
  9259. ELSE
  9260. result := NIL
  9261. END
  9262. END;
  9263. RETURN result
  9264. END MathArrayStructureOfType;
  9265. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9266. VAR
  9267. result: BOOLEAN;
  9268. rangeExpression: SyntaxTree.RangeExpression;
  9269. BEGIN
  9270. IF x IS SyntaxTree.RangeExpression THEN
  9271. rangeExpression := x(SyntaxTree.RangeExpression);
  9272. result := TRUE;
  9273. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9274. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9275. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9276. ELSE
  9277. result := FALSE
  9278. END;
  9279. RETURN result
  9280. END IsStaticRange;
  9281. (** whether a type is a math array of tensor form **)
  9282. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9283. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9284. END IsTensor;
  9285. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9286. BEGIN
  9287. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9288. length := type(SyntaxTree.MathArrayType).staticLength;
  9289. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9290. RETURN TRUE
  9291. ELSE
  9292. RETURN FALSE
  9293. END;
  9294. END IsStaticMathArray;
  9295. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9296. BEGIN
  9297. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9298. END SymbolHasAddress;
  9299. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9300. BEGIN
  9301. RETURN
  9302. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9303. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9304. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9305. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9306. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9307. ;
  9308. END HasAddress;
  9309. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9310. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9311. BEGIN
  9312. IF (e IS SyntaxTree.Designator) THEN
  9313. d := e(SyntaxTree.Designator);
  9314. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9315. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9316. e := d.left;
  9317. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9318. END;
  9319. IF d # NIL THEN
  9320. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9321. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9322. END;
  9323. END;
  9324. RETURN FALSE;
  9325. END IsLocalVariable;
  9326. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9327. BEGIN
  9328. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9329. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9330. ELSE
  9331. RETURN TRUE
  9332. END;
  9333. END IsStaticProcedure;
  9334. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9335. CONST OptimizeMethodTable = FALSE;
  9336. BEGIN
  9337. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9338. END InMethodTable;
  9339. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9340. BEGIN
  9341. IF type = NIL THEN RETURN FALSE
  9342. ELSE
  9343. type := type.resolved;
  9344. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9345. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9346. END
  9347. END ReturnedAsParameter;
  9348. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9349. BEGIN
  9350. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9351. END StructuredReturnType;
  9352. END FoxSemanticChecker.
  9353. SystemTools.FreeDownTo FoxSemanticChecker ~