FoxSemanticChecker.Mod 380 KB

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