FoxSemanticChecker.Mod 403 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: Position; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, code, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. END FixProcedureType;
  725. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  726. VAR prev,this: SyntaxTree.Modifier;
  727. BEGIN
  728. this := modifiers;prev := NIL;
  729. WHILE (this # NIL) & (this.identifier # name) DO
  730. prev := this; this := this.nextModifier;
  731. END;
  732. IF this # NIL THEN
  733. IF this.expression # NIL THEN
  734. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  735. END;
  736. this.Resolved;
  737. position := this.position;
  738. RETURN TRUE
  739. ELSE
  740. RETURN FALSE
  741. END;
  742. END HasFlag;
  743. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  744. VAR prev,this: SyntaxTree.Modifier;
  745. BEGIN
  746. this := modifiers;prev := NIL;
  747. WHILE (this # NIL) & (this.identifier # name) DO
  748. prev := this; this := this.nextModifier;
  749. END;
  750. IF this # NIL THEN
  751. IF this.expression = NIL THEN
  752. Error(this.position,Diagnostics.Invalid,"expected expression value");
  753. ELSE
  754. this.SetExpression(ConstantExpression(this.expression));
  755. IF CheckIntegerValue(this.expression,value) THEN END;
  756. END;
  757. this.Resolved;
  758. position := this.position;
  759. RETURN TRUE
  760. ELSE RETURN FALSE
  761. END;
  762. END HasValue;
  763. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  764. VAR prev,this: SyntaxTree.Modifier;
  765. BEGIN
  766. this := modifiers;prev := NIL;
  767. WHILE (this # NIL) & (this.identifier # name) DO
  768. prev := this; this := this.nextModifier;
  769. END;
  770. IF this # NIL THEN
  771. IF this.expression = NIL THEN
  772. Error(this.position,Diagnostics.Invalid,"expected expression value");
  773. ELSE
  774. this.SetExpression(ConstantExpression(this.expression));
  775. IF CheckStringValue(this.expression,value) THEN END;
  776. END;
  777. this.Resolved;
  778. position := this.position;
  779. RETURN TRUE
  780. ELSE RETURN FALSE
  781. END;
  782. END HasStringValue;
  783. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  784. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  785. BEGIN
  786. IF cellsAreObjects THEN RETURN FALSE END;
  787. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  788. IF svalue = "A2" THEN
  789. RETURN TRUE
  790. END;
  791. END;
  792. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  793. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  794. END;
  795. RETURN FALSE;
  796. (*
  797. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  798. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  799. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  800. IF svalue[0] = "~" THEN
  801. Strings.TrimLeft(svalue, "~");
  802. IF svalue = backendName THEN
  803. RETURN TRUE;
  804. END;
  805. ELSIF svalue # backendName THEN
  806. RETURN TRUE;
  807. END;
  808. END;
  809. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  810. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  811. END;
  812. RETURN FALSE;
  813. *)
  814. END SkipImplementation;
  815. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  816. VAR this: SyntaxTree.Modifier;
  817. BEGIN
  818. this := modifiers;
  819. WHILE this # NIL DO
  820. IF ~this.resolved THEN
  821. IF checkUse THEN
  822. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  823. ELSE
  824. this.SetExpression(ResolveExpression(this.expression));
  825. this.Resolved;
  826. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  827. END;
  828. END;
  829. this := this.nextModifier
  830. END;
  831. END CheckModifiers;
  832. (**
  833. resolve procedure type
  834. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  835. **)
  836. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  837. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  838. BEGIN
  839. IF TypeNeedsResolution(procedureType) THEN
  840. modifiers := procedureType.modifiers;
  841. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  842. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  843. procedureType.SetInterrupt(TRUE);
  844. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  845. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  846. IF useDarwinCCalls THEN (*fld*)
  847. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  848. ELSE
  849. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  850. END
  851. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  852. procedureType.SetNoReturn(TRUE);
  853. END;
  854. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  855. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  856. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  857. CheckModifiers(modifiers, TRUE);
  858. modifiers := procedureType.returnTypeModifiers;
  859. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  860. CheckModifiers(modifiers, TRUE);
  861. typeFixes.Add(procedureType,currentScope);
  862. procedureType.SetHasPointers(procedureType.isDelegate);
  863. procedureType.SetState(SyntaxTree.Resolved);
  864. END;
  865. resolvedType := ResolvedType(procedureType)
  866. END VisitProcedureType;
  867. (** check and resolve record type
  868. - check base type: must be record, math array or array-structured object type
  869. - check declarations
  870. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  871. **)
  872. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  873. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  874. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  875. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  876. hasPointers: BOOLEAN;
  877. modifiers: SyntaxTree.Modifier;
  878. value: LONGINT;
  879. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  880. BEGIN
  881. type := type.resolved;
  882. IF (type IS SyntaxTree.PointerType) &
  883. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  884. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  885. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  886. RETURN TRUE
  887. ELSE
  888. RETURN FALSE
  889. END;
  890. END IsPointerToRecord;
  891. BEGIN
  892. IF TypeNeedsResolution(x) THEN
  893. hasPointers := FALSE;
  894. modifiers := x.modifiers;
  895. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  896. CheckModifiers(modifiers, TRUE);
  897. IF x.baseType # NIL THEN
  898. position := x.baseType.position;
  899. baseType := ResolveType(x.baseType);
  900. resolved := baseType.resolved;
  901. hasPointers := hasPointers OR resolved.hasPointers;
  902. IF x.isObject THEN (* object *)
  903. ASSERT(x.pointerType # NIL);
  904. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  905. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  906. baseType := NIL
  907. ELSIF IsPointerToRecord(resolved,recordType) THEN
  908. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  909. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  910. ELSE
  911. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  912. END;
  913. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  914. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  915. ELSIF IsPointerToRecord(resolved,recordType) THEN
  916. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  917. ELSIF resolved IS SyntaxTree.RecordType THEN
  918. ELSE
  919. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  920. END;
  921. ELSE
  922. IF resolved IS SyntaxTree.RecordType THEN
  923. ELSE
  924. Error(position, Diagnostics.Invalid,"record does not extend record")
  925. END;
  926. END;
  927. x.SetBaseType(baseType);
  928. IF x.Level() > 15 THEN
  929. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  930. (* note:
  931. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  932. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  933. inheritance history of a type.
  934. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  935. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  936. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  937. *)
  938. END;
  939. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  940. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  941. END;
  942. END;
  943. Declarations(x.recordScope, FALSE);
  944. ResolveArrayStructure(x);
  945. (* computation of sizes and offsets skipped -> done in backend / system *)
  946. recordBase := x.GetBaseRecord();
  947. IF recordBase = NIL THEN numberMethods := 0
  948. ELSE numberMethods := recordBase.recordScope.numberMethods
  949. END;
  950. isRealtime := TRUE;
  951. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  952. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  953. END;
  954. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  955. WHILE symbol # NIL DO
  956. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  957. IF symbol IS SyntaxTree.Variable THEN
  958. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  959. END;
  960. IF symbol IS SyntaxTree.Procedure THEN
  961. procedure := symbol(SyntaxTree.Procedure);
  962. IF procedure.super # NIL THEN
  963. procedure.SetMethodNumber(procedure.super.methodNumber)
  964. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  965. procedure.SetMethodNumber(numberMethods);
  966. INC(numberMethods);
  967. END;
  968. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  969. Error(procedure.position, Diagnostics.Invalid,"realtime procedure in non-realtime object")
  970. END;
  971. END;
  972. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  973. Error(symbol.position, Diagnostics.Invalid,"non-realtime symbol in realtime object")
  974. END;
  975. symbol := symbol.nextSymbol;
  976. END;
  977. IF isRealtime THEN x.SetRealtime(TRUE) END;
  978. x.recordScope.SetNumberMethods(numberMethods);
  979. (* TODO: is this needed anymore? *)
  980. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  981. Error(x.position,Diagnostics.Invalid,"object extends a record")
  982. END;
  983. IF (x.typeDeclaration = NIL) THEN
  984. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  985. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  986. (*
  987. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  988. AnonymousTypeDeclaration(x,name);
  989. *)
  990. ELSE
  991. AnonymousTypeDeclaration(x,"Anonymous");
  992. END;
  993. END;
  994. x.SetHasPointers(hasPointers);
  995. x.SetState(SyntaxTree.Resolved);
  996. END;
  997. resolvedType := ResolvedType(x);
  998. END VisitRecordType;
  999. (** check and resolve cell type
  1000. - check base type: must be cell
  1001. - check declarations
  1002. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1003. **)
  1004. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1005. VAR
  1006. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1007. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1008. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1009. recordBase: SyntaxTree.RecordType;
  1010. numberMethods, int: LONGINT;
  1011. real: LONGREAL;
  1012. bool: BOOLEAN;
  1013. set: SET;
  1014. v: SyntaxTree.Expression;
  1015. str: Scanner.StringType;
  1016. atype: SyntaxTree.ArrayType;
  1017. prev: SyntaxTree.Scope;
  1018. skip: BOOLEAN;
  1019. svalue: ARRAY 32 OF CHAR;
  1020. BEGIN
  1021. IF TypeNeedsResolution(x) THEN
  1022. recordBase := NIL;
  1023. IF cellsAreObjects THEN
  1024. IF x.baseType = NIL THEN
  1025. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1026. ImportModule(qualifiedIdentifier.prefix, x.position);
  1027. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1028. x.SetBaseType(ResolveType(x.baseType));
  1029. recordBase := x.GetBaseRecord();
  1030. IF recordBase = NIL THEN
  1031. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1032. END;
  1033. ELSE
  1034. x.SetBaseType(ResolveType(x.baseType));
  1035. END;
  1036. ELSE
  1037. x.SetBaseType(ResolveType(x.baseType));
  1038. END;
  1039. IF recordBase = NIL THEN numberMethods := 0
  1040. ELSE numberMethods := recordBase.recordScope.numberMethods
  1041. END;
  1042. modifier := x.modifiers;
  1043. (*IF ~x.isCellNet THEN*)
  1044. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1045. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1046. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1047. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1048. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1049. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1050. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1051. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1052. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1053. symbol := system.activeCellsCapabilities;
  1054. WHILE symbol # NIL DO
  1055. IF HasFlag(modifier, symbol.name, position) THEN END;
  1056. symbol := symbol.nextSymbol;
  1057. END;
  1058. modifier := x.modifiers;
  1059. WHILE (modifier # NIL) DO
  1060. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1061. IF modifier.expression # NIL THEN
  1062. v := ConstantExpression(modifier.expression);
  1063. property.SetValue(v);
  1064. IF IsIntegerValue(modifier.expression, int) THEN
  1065. (*property.SetValue(modifier.expression);*)
  1066. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1067. property.SetType(system.longintType);
  1068. ELSIF IsRealValue(modifier.expression, real) THEN
  1069. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1070. property.SetType(system.longrealType);
  1071. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1072. property.SetType(system.booleanType);
  1073. ELSIF IsSetValue(modifier.expression, set) THEN
  1074. property.SetType(system.setType);
  1075. ELSIF IsStringValue(modifier.expression, str) THEN
  1076. (*property.SetValue(modifier.expression);*)
  1077. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1078. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1079. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1080. property.SetType(atype);
  1081. ELSE
  1082. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1083. END;
  1084. ELSE (* flag property *)
  1085. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1086. property.SetType(system.booleanType);
  1087. END;
  1088. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1089. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1090. x.AddProperty(property);
  1091. modifier := modifier.nextModifier;
  1092. END;
  1093. CheckModifiers(modifier, FALSE);
  1094. Declarations(x.cellScope, SkipImplementation(x));
  1095. (* process parameters *)
  1096. prev := currentScope;
  1097. currentScope := x.cellScope;
  1098. parameter :=x.firstParameter;
  1099. WHILE (parameter # NIL) DO
  1100. VisitParameter(parameter);
  1101. type := parameter.type.resolved;
  1102. IF ~(type IS SyntaxTree.PortType) THEN
  1103. WHILE IsStaticArray(type, type, len) DO
  1104. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1105. END;
  1106. WHILE IsDynamicArray(type, type) DO
  1107. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1108. END;
  1109. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1110. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1111. END;
  1112. END;
  1113. parameter := parameter.nextParameter;
  1114. END;
  1115. currentScope := prev;
  1116. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1117. WHILE symbol # NIL DO
  1118. IF symbol IS SyntaxTree.Variable THEN
  1119. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1120. END;
  1121. symbol := symbol.nextSymbol;
  1122. END;
  1123. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1124. IF (x.typeDeclaration = NIL) THEN
  1125. AnonymousTypeDeclaration(x,"Anonymous");
  1126. END;
  1127. x.SetState(SyntaxTree.Resolved);
  1128. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1129. Warning(x.position, "Forbidden empty Body.");
  1130. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1131. Warning(x.position, "Non-empty body for an engine?");
  1132. END;
  1133. END;
  1134. resolvedType := ResolvedType(x);
  1135. END VisitCellType;
  1136. (* check if an object is an array-structured object type
  1137. - determine the array structure
  1138. - collect operators from top to bottom in the inheritance hierarchy
  1139. - check if LEN operator is declared
  1140. - determine number of possible index operators
  1141. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1142. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1143. *)
  1144. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1145. VAR
  1146. indexOperatorCount, i: LONGINT;
  1147. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1148. isTensor: BOOLEAN;
  1149. BEGIN
  1150. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1151. (* determine array structure *)
  1152. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1153. END;
  1154. IF recordType.HasArrayStructure() THEN
  1155. (* the object is an ASOT *)
  1156. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1157. (* reset array access operators *)
  1158. arrayAccessOperators.len := NIL;
  1159. arrayAccessOperators.generalRead := NIL;
  1160. arrayAccessOperators.generalWrite := NIL;
  1161. IF isTensor THEN
  1162. (* all operators of dimensionalities 1 to max *)
  1163. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1164. ELSE
  1165. (* all operators of certain dimensionality *)
  1166. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1167. END;
  1168. NEW(arrayAccessOperators.read, indexOperatorCount);
  1169. NEW(arrayAccessOperators.write, indexOperatorCount);
  1170. FOR i := 0 TO indexOperatorCount - 1 DO
  1171. arrayAccessOperators.read[i] := NIL;
  1172. arrayAccessOperators.write[i] := NIL
  1173. END;
  1174. (* collect access operators in the record scope *)
  1175. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1176. IF arrayAccessOperators.len = NIL THEN
  1177. (* TODO: think about making this operator optional for static array structures *)
  1178. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1179. END;
  1180. (* show error messages *)
  1181. IF isTensor THEN
  1182. (* require ARRAY [*] OF RANGE *)
  1183. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1184. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1185. ELSE
  1186. (* forbid ARRAY [*] OF RANGE *)
  1187. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1188. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1189. (* require RANGE, RANGE, ... RANGE *)
  1190. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1191. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1192. END;
  1193. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1194. ELSE
  1195. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1196. IF recordType.recordScope.firstOperator # NIL THEN
  1197. RETURN;
  1198. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1199. END
  1200. END
  1201. END ResolveArrayStructure;
  1202. (** collect array access operators in a record scope **)
  1203. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1204. VAR
  1205. baseType: SyntaxTree.Type;
  1206. operator: SyntaxTree.Operator;
  1207. isReadOperator, isGeneralOperator: BOOLEAN;
  1208. indexListSize, indexListKind, hashValue: LONGINT;
  1209. BEGIN
  1210. (* if a parent record scope exists, collect the operators there first *)
  1211. baseType := recordScope.ownerRecord.baseType;
  1212. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1213. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1214. END;
  1215. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1216. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1217. END;
  1218. (* go through all operators in the current record scope *)
  1219. operator := recordScope.firstOperator;
  1220. WHILE operator # NIL DO
  1221. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1222. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1223. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1224. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1225. IF isGeneralOperator THEN
  1226. IF isReadOperator THEN
  1227. arrayAccessOperators.generalRead := operator
  1228. ELSE
  1229. arrayAccessOperators.generalWrite := operator
  1230. END
  1231. ELSE
  1232. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1233. IF isReadOperator THEN
  1234. arrayAccessOperators.read[hashValue] := operator
  1235. ELSE
  1236. arrayAccessOperators.write[hashValue] := operator
  1237. END
  1238. END
  1239. END
  1240. ELSE
  1241. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1242. END;
  1243. operator := operator.nextOperator
  1244. END
  1245. END CollectArrayAccessOperators;
  1246. (** the hash value of an index operator **)
  1247. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1248. VAR result: LONGINT;
  1249. BEGIN
  1250. IF isTensor THEN
  1251. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1252. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1253. ELSE
  1254. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1255. END
  1256. ELSE
  1257. result := indexListKind
  1258. END;
  1259. RETURN result
  1260. END IndexOperatorHash;
  1261. (** 2 to the power of exponent **)
  1262. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1263. VAR result, i: LONGINT;
  1264. BEGIN
  1265. result := 1;
  1266. FOR i := 1 TO exponent DO
  1267. result := result * 2;
  1268. END;
  1269. RETURN result
  1270. END TwoToThePowerOf;
  1271. (** check if a LEN operator has a correct signature. i.e.
  1272. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1273. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1274. **)
  1275. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1276. VAR
  1277. procedureType: SyntaxTree.ProcedureType;
  1278. returnedArrayType: SyntaxTree.MathArrayType;
  1279. result: BOOLEAN;
  1280. BEGIN
  1281. result := FALSE;
  1282. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1283. IF (procedureType.numberParameters = 0) THEN
  1284. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1285. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1286. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1287. IF returnedArrayType.form = SyntaxTree.Open THEN
  1288. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1289. result := TRUE
  1290. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1291. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1292. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1293. result := TRUE
  1294. END
  1295. END
  1296. END
  1297. END
  1298. END;
  1299. IF result THEN
  1300. (* export symbol automatically *)
  1301. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1302. ELSE
  1303. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1304. END;
  1305. RETURN result
  1306. END CheckLenOperator;
  1307. (** check if an index operator has a correct signature. i.e.
  1308. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1309. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1310. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1311. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1312. - determine if it is a read or write operator (existance of return type)
  1313. - check index parameters
  1314. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1315. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1316. [LONGINT] -> binary 0 -> 0
  1317. [RANGE] -> binary 1 -> 1
  1318. [LONGINT, LONGINT] -> binary 00 -> 0
  1319. [LONGINT, RANGE] -> binary 01 -> 1
  1320. [RANGE, LONGINT] -> binary 10 -> 2
  1321. [RANGE, RANGE] -> binary 11 -> 3
  1322. etc.
  1323. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1324. - for read operators, check if return type matches the type of data that is read
  1325. - for write operators, check if last parameter type matches the type of data that is written
  1326. **)
  1327. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1328. VAR
  1329. elementType, otherElementType, dataType: SyntaxTree.Type;
  1330. procedureType: SyntaxTree.ProcedureType;
  1331. mathArrayType: SyntaxTree.MathArrayType;
  1332. parameter: SyntaxTree.Parameter;
  1333. parameterCount, rangeCount, i: LONGINT;
  1334. hasTypeError: BOOLEAN;
  1335. BEGIN
  1336. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1337. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1338. (* determine if it is a read or write operator *)
  1339. isReadOperator := (procedureType.returnType # NIL);
  1340. IF isReadOperator THEN
  1341. indexListSize := parameterCount;
  1342. ELSE
  1343. indexListSize := parameterCount - 1;
  1344. END;
  1345. IF indexListSize < 1 THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1347. RETURN FALSE
  1348. END;
  1349. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1350. (* general operator *)
  1351. isGeneralOperator := TRUE;
  1352. IF indexListSize > 1 THEN
  1353. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1354. RETURN FALSE
  1355. END;
  1356. (* ARRAY [*] OF RANGE*)
  1357. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1358. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1359. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1360. RETURN FALSE
  1361. END;
  1362. parameter := procedureType.firstParameter.nextParameter
  1363. ELSE
  1364. (* fixed-dim. operator *)
  1365. isGeneralOperator := FALSE;
  1366. (* check number of index parameters *)
  1367. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1368. (* for tensors, limited to a certain size *)
  1369. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1370. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1371. RETURN FALSE
  1372. END
  1373. ELSE
  1374. (* for non-tensors, depends on dimensionality *)
  1375. IF indexListSize # arrayStructure.Dimensionality() THEN
  1376. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1377. RETURN FALSE
  1378. END
  1379. END;
  1380. (* go through all index parameters
  1381. - count the number of ranges
  1382. - determine the index list kind number
  1383. *)
  1384. indexListKind := 0;
  1385. rangeCount := 0;
  1386. parameter := procedureType.firstParameter;
  1387. FOR i := 1 TO indexListSize DO
  1388. indexListKind := indexListKind * 2;
  1389. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1390. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1391. INC(indexListKind);
  1392. INC(rangeCount)
  1393. ELSE
  1394. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1395. RETURN FALSE
  1396. END;
  1397. parameter := parameter.nextParameter
  1398. END;
  1399. END;
  1400. (*
  1401. - for read operators: check type of last parameter
  1402. - for write operators: check return type
  1403. *)
  1404. IF isReadOperator THEN
  1405. dataType := procedureType.returnType (* the return type *)
  1406. ELSE
  1407. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1408. END;
  1409. elementType := arrayStructure.ElementType();
  1410. hasTypeError := FALSE;
  1411. IF isGeneralOperator THEN
  1412. (* ARRAY [?] OF <Element> *)
  1413. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1414. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1415. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1416. hasTypeError := TRUE
  1417. END
  1418. ELSE
  1419. hasTypeError := TRUE
  1420. END
  1421. ELSE
  1422. IF rangeCount = 0 THEN
  1423. (* <Element> *)
  1424. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1425. ELSE
  1426. (* ARRAY [*, *, ..., *] OF <Element> *)
  1427. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1428. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1429. IF mathArrayType.IsFullyDynamic() THEN
  1430. IF mathArrayType.Dimensionality() = rangeCount THEN
  1431. otherElementType := mathArrayType.ElementType();
  1432. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1433. ELSE
  1434. hasTypeError := TRUE
  1435. END
  1436. ELSE
  1437. hasTypeError := TRUE
  1438. END
  1439. ELSE
  1440. hasTypeError := TRUE
  1441. END
  1442. END
  1443. END;
  1444. IF hasTypeError THEN
  1445. IF isReadOperator THEN
  1446. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1447. ELSE
  1448. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1449. END;
  1450. RETURN FALSE
  1451. END;
  1452. (* export symbol automatically *)
  1453. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1454. RETURN TRUE
  1455. END CheckIndexOperator;
  1456. (** resolve all pending types (late resolving).
  1457. - type fixes are resolved at the end of the declaration phase
  1458. - type fixes may imply new type fixes that are also entered at the end of the list
  1459. **)
  1460. PROCEDURE FixTypes;
  1461. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1462. BEGIN
  1463. prevScope := currentScope;
  1464. p := typeFixes.Get(currentScope);
  1465. WHILE p # NIL DO
  1466. ASSERT(currentScope # NIL);
  1467. ASSERT(p IS SyntaxTree.Type);
  1468. IF p IS SyntaxTree.PointerType THEN
  1469. FixPointerType(p(SyntaxTree.PointerType))
  1470. ELSIF p IS SyntaxTree.ProcedureType THEN
  1471. FixProcedureType(p(SyntaxTree.ProcedureType))
  1472. ELSE
  1473. HALT(100);
  1474. END;
  1475. p := typeFixes.Get(currentScope);
  1476. END;
  1477. currentScope :=prevScope;
  1478. END FixTypes;
  1479. (**
  1480. resolve type x
  1481. - if x is nil then return nil
  1482. - if x cannot be resolved then the result is invalidType else the result is x
  1483. - the resolved type is entered into x.resolved
  1484. **)
  1485. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1486. VAR prev,resolved: SyntaxTree.Type;
  1487. BEGIN
  1488. prev := resolvedType;
  1489. resolvedType := SyntaxTree.invalidType;
  1490. IF x = NIL THEN resolvedType := NIL
  1491. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1492. END;
  1493. resolved := resolvedType;
  1494. resolvedType := prev;
  1495. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1496. RETURN resolved
  1497. END ResolveType;
  1498. (*** compatibility rules ***)
  1499. (**
  1500. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1501. **)
  1502. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1503. VAR result: SyntaxTree.Type;
  1504. BEGIN
  1505. result := SyntaxTree.invalidType;
  1506. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1507. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1508. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1509. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1510. ELSE result := type.resolved
  1511. END;
  1512. RETURN result
  1513. END RegularType;
  1514. (** returns signature compatibility of procedure types this and to
  1515. - if not compatible then error is reported
  1516. - compatibility means type equality
  1517. **)
  1518. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1519. VAR result: BOOLEAN;
  1520. BEGIN
  1521. result := SameType(to,this);
  1522. IF ~result THEN
  1523. Error(position,Diagnostics.Invalid,"signature incompatible");
  1524. IF VerboseErrorMessage THEN
  1525. Printout.Info("this",this);
  1526. Printout.Info("to",to);
  1527. END;
  1528. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1529. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1530. END;
  1531. RETURN result
  1532. END SignatureCompatible;
  1533. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1534. - for var parameters compatibility means same type except for
  1535. - formal is of open array of system byte
  1536. - formal is of record type
  1537. - formal is of open array type
  1538. - formal is of open math array type
  1539. - for value parameters compatibllity means assignment compatibility except for
  1540. - formal is of open array type
  1541. if compatible the return true else report error and return false
  1542. **)
  1543. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1544. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1545. BEGIN
  1546. formalType := RegularType(formal.position,formal.type);
  1547. actualType := RegularType(actual.position,actual.type);
  1548. error := FALSE;
  1549. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1550. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1551. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1552. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1553. END;
  1554. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1555. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1556. ELSIF ~IsVariable(actual) THEN
  1557. result := FALSE; error := TRUE;
  1558. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1559. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1560. ELSE
  1561. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1562. END;
  1563. IF VerboseErrorMessage THEN
  1564. Printout.Info("actual",actual);
  1565. Printout.Info("formal",formal);
  1566. END;
  1567. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1568. result := CompatibleTo(system,actualType,formalType);
  1569. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1570. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1571. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1572. IF IsArrayStructuredObjectType(actualType) THEN
  1573. actualType := MathArrayStructureOfType(actualType)
  1574. END;
  1575. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1576. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1577. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1578. END;
  1579. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1580. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1581. END;
  1582. ELSE
  1583. result := SameType(actualType,formalType)
  1584. END
  1585. ELSE
  1586. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1587. actualType := system.characterType;
  1588. END;
  1589. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1590. result := TRUE; (* special rule for WINAPI parameters *)
  1591. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1592. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1593. ELSE
  1594. result := CompatibleTo(system,actualType,formalType);
  1595. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1596. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1597. END;
  1598. END;
  1599. END;
  1600. IF ~result & ~error THEN
  1601. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1602. IF VerboseErrorMessage THEN
  1603. Printout.Info("actual",actual);
  1604. Printout.Info("formal",formal);
  1605. END;
  1606. END;
  1607. RETURN result
  1608. END ParameterCompatible;
  1609. (** check compatibility for expressions of the form left := right
  1610. - if compatible then return true else error report and return false
  1611. - check if left is variable
  1612. - check compatiblity
  1613. **)
  1614. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1615. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1616. BEGIN
  1617. result := FALSE;
  1618. leftType := RegularType(left.position,left.type);
  1619. rightType := RegularType(right.position,right.type);
  1620. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1621. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1622. END;
  1623. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1624. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1625. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1626. (* error already handled *)
  1627. result := TRUE;
  1628. ELSIF ~IsVariable(left) THEN
  1629. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1630. IF VerboseErrorMessage THEN
  1631. Printout.Info("left",left);
  1632. Printout.Info("right",right);
  1633. END;
  1634. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1635. result := TRUE;
  1636. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1637. result := TRUE
  1638. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1639. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1640. IF VerboseErrorMessage THEN
  1641. Printout.Info("left",left);
  1642. Printout.Info("right",right);
  1643. END;
  1644. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1645. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1646. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1647. ELSE
  1648. result := TRUE
  1649. END;
  1650. RETURN result
  1651. END AssignmentCompatible;
  1652. (*** values ***)
  1653. (** check and resolve integer value **)
  1654. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1655. VAR hugeint: HUGEINT;
  1656. BEGIN
  1657. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1658. value.SetType(Global.GetIntegerType(system,hugeint));
  1659. resolvedExpression := value
  1660. END VisitIntegerValue;
  1661. (** check and resolve real value **)
  1662. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1663. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1664. BEGIN
  1665. subtype := value(SyntaxTree.RealValue).subtype;
  1666. IF subtype = Scanner.Real THEN
  1667. type := system.realType
  1668. ELSIF subtype = Scanner.Longreal THEN
  1669. type := system.longrealType
  1670. ELSE
  1671. HALT(100)
  1672. END;
  1673. value.SetType(type);
  1674. resolvedExpression := value
  1675. END VisitRealValue;
  1676. (** check and resolve complex value **)
  1677. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1678. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1679. BEGIN
  1680. subtype := value(SyntaxTree.ComplexValue).subtype;
  1681. IF subtype = Scanner.Real THEN
  1682. type := system.complexType
  1683. ELSIF subtype = Scanner.Longreal THEN
  1684. type := system.longcomplexType
  1685. ELSE
  1686. HALT(100)
  1687. END;
  1688. value.SetType(type);
  1689. resolvedExpression := value
  1690. END VisitComplexValue;
  1691. (** check and resolve set value **)
  1692. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1693. BEGIN
  1694. value.SetType(system.setType);
  1695. resolvedExpression := value
  1696. END VisitSetValue;
  1697. (** check and resolve set value **)
  1698. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1699. BEGIN
  1700. value.SetType(SyntaxTree.invalidType);
  1701. resolvedExpression := value
  1702. END VisitMathArrayValue;
  1703. (** check and resolve boolean value **)
  1704. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1705. BEGIN
  1706. value.SetType(system.booleanType);
  1707. resolvedExpression := value
  1708. END VisitBooleanValue;
  1709. (** check and resolve string value **)
  1710. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1711. BEGIN
  1712. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1713. resolvedExpression := value
  1714. END VisitStringValue;
  1715. (** check and resolve character value **)
  1716. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1717. BEGIN
  1718. value.SetType(system.characterType);
  1719. resolvedExpression := value
  1720. END VisitCharacterValue;
  1721. (** check and resolve nil value **)
  1722. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1723. BEGIN
  1724. value.SetType(system.nilType);
  1725. resolvedExpression := value
  1726. END VisitNilValue;
  1727. (** check and resolve enumerator value **)
  1728. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1729. BEGIN
  1730. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1731. ASSERT(value.type # NIL);
  1732. resolvedExpression := value
  1733. END VisitEnumerationValue;
  1734. (*** expressions ***)
  1735. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1736. - check all elements on integer type
  1737. - if element range is constant, then check lower and upper bound
  1738. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1739. if an error occurs then report error and return invalidExpression
  1740. **)
  1741. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1742. VAR
  1743. i: LONGINT;
  1744. element: SyntaxTree.Expression;
  1745. constant: BOOLEAN;
  1746. elements: SyntaxTree.ExpressionList;
  1747. s: SET;
  1748. result: SyntaxTree.Expression;
  1749. value: SyntaxTree.Value;
  1750. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1751. VAR
  1752. left, right: SyntaxTree.Expression;
  1753. elementResult: SyntaxTree.Expression;
  1754. leftInteger, rightInteger, temp: LONGINT;
  1755. BEGIN
  1756. (* set context of range *)
  1757. IF element IS SyntaxTree.RangeExpression THEN
  1758. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1759. END;
  1760. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1761. IF elementResult = SyntaxTree.invalidExpression THEN
  1762. (* error already reported *)
  1763. constant := FALSE
  1764. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1765. (* the element is a range expression *)
  1766. (* extract left and right hand side of range *)
  1767. left := elementResult(SyntaxTree.RangeExpression).first;
  1768. right := elementResult(SyntaxTree.RangeExpression).last;
  1769. (* guaranteed by VisitRangeExpression: *)
  1770. ASSERT((left # NIL) & (right # NIL));
  1771. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1772. ELSE
  1773. (* the element is not a range expression *)
  1774. (* check type and add conversion if needed *)
  1775. IF IsIntegerType(elementResult.type.resolved) THEN
  1776. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1777. ELSE
  1778. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1779. elementResult := SyntaxTree.invalidExpression;
  1780. constant := FALSE
  1781. END;
  1782. left := elementResult;
  1783. right := elementResult
  1784. END;
  1785. IF elementResult # SyntaxTree.invalidExpression THEN
  1786. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1787. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1788. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1789. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1790. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1791. END
  1792. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1793. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1794. ELSE
  1795. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1796. s := {};
  1797. ELSE
  1798. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1799. IF leftInteger < 0 THEN leftInteger := 0 END;
  1800. (*!!!!!!!!! this is a hack !!!!!!! *)
  1801. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1802. extends the range x..31 to x..63 !!!!!! *)
  1803. s := s + {leftInteger..rightInteger};
  1804. END;
  1805. END;
  1806. ELSE
  1807. constant := FALSE;
  1808. END
  1809. END;
  1810. RETURN elementResult
  1811. END CheckElement;
  1812. BEGIN
  1813. result := set; constant := TRUE; s := {}; elements := set.elements;
  1814. IF elements # NIL THEN
  1815. FOR i := 0 TO elements.Length()-1 DO
  1816. element := elements.GetExpression(i);
  1817. element := CheckElement(element);
  1818. IF element = SyntaxTree.invalidExpression THEN
  1819. result := SyntaxTree.invalidExpression
  1820. END;
  1821. elements.SetExpression(i,element);
  1822. END;
  1823. END;
  1824. IF constant THEN
  1825. value := SyntaxTree.NewSetValue(set.position,s);
  1826. value.SetType(system.setType);
  1827. result.SetResolved(value);
  1828. END;
  1829. (* optimization possible
  1830. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1831. left this to the programmer...
  1832. *)
  1833. result.SetType(system.setType);
  1834. resolvedExpression := result;
  1835. END VisitSet;
  1836. (*
  1837. old variant: quite generic but needs better conversion handling, do this?
  1838. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1839. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1840. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1841. BEGIN
  1842. type := NIL;
  1843. numberElements := x.elements.Length();
  1844. FOR i := 0 TO numberElements-1 DO
  1845. expression := x.elements.GetExpression(i);
  1846. position := expression.position;
  1847. expression := ResolveExpression(x.elements.GetExpression(i));
  1848. x.elements.SetExpression(i,de);
  1849. IF type = NIL THEN
  1850. type := expression.type;
  1851. ELSIF CompatibleTo(system,expression.type,type) THEN
  1852. (* ok *)
  1853. ELSIF CompatibleTo(system,type,expression.type) THEN
  1854. type := expression.type
  1855. ELSE
  1856. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1857. type := SyntaxTree.invalidType;
  1858. END;
  1859. END;
  1860. isValue := TRUE;
  1861. FOR i := 0 TO numberElements-1 DO
  1862. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1863. x.elements.SetExpression(i,expression);
  1864. isValue := isValue & (expression.resolved # NIL);
  1865. END;
  1866. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1867. arrayType.SetArrayBase(type);
  1868. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1869. IF isValue THEN
  1870. value := SyntaxTree.NewMathArrayValue(position);
  1871. value.SetElements(x.elements);
  1872. x.SetResolved(value);
  1873. END;
  1874. x.SetType(arrayType);
  1875. resolvedExpression := x;
  1876. END VisitMathArrayExpression;
  1877. *)
  1878. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1879. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1880. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1881. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1882. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1883. BEGIN
  1884. numberElements := x.elements.Length();
  1885. FOR i := 0 TO numberElements-1 DO
  1886. expression := x.elements.GetExpression(i);
  1887. IF expression IS SyntaxTree.MathArrayExpression THEN
  1888. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1889. ELSE
  1890. position := expression.position;
  1891. expression := ResolveExpression(x.elements.GetExpression(i));
  1892. x.elements.SetExpression(i,expression);
  1893. IF type = NIL THEN
  1894. type := expression.type;
  1895. ELSIF CompatibleTo(system,expression.type,type) THEN
  1896. (* ok *)
  1897. ELSIF CompatibleTo(system,type,expression.type) THEN
  1898. type := expression.type
  1899. ELSE
  1900. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1901. type := SyntaxTree.invalidType;
  1902. END;
  1903. END;
  1904. END;
  1905. END RecursivelyFindType;
  1906. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1907. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1908. BEGIN
  1909. numberElements := x.elements.Length();
  1910. FOR i := 0 TO numberElements-1 DO
  1911. expression := x.elements.GetExpression(i);
  1912. IF expression IS SyntaxTree.MathArrayExpression THEN
  1913. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1914. ELSE
  1915. position := expression.position;
  1916. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1917. x.elements.SetExpression(i,expression);
  1918. isValue := isValue & (expression.resolved # NIL);
  1919. END;
  1920. END;
  1921. END RecursivelySetExpression;
  1922. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1923. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1924. arrayType: SyntaxTree.MathArrayType;
  1925. BEGIN
  1926. numberElements := x.elements.Length();
  1927. baseType := NIL;
  1928. gsize := 0;
  1929. FOR i := 0 TO numberElements-1 DO
  1930. expression := x.elements.GetExpression(i);
  1931. IF expression IS SyntaxTree.MathArrayExpression THEN
  1932. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1933. IF i=0 THEN
  1934. gsize := size;
  1935. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1936. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1937. ELSE expression.SetType(baseType)
  1938. END;
  1939. ELSIF baseType = NIL THEN baseType := type;
  1940. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1941. END;
  1942. END;
  1943. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1944. arrayType.SetArrayBase(baseType);
  1945. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1946. RETURN ResolveType(arrayType);
  1947. END RecursivelySetType;
  1948. BEGIN
  1949. type := NIL;
  1950. RecursivelyFindType(x);
  1951. isValue := TRUE;
  1952. RecursivelySetExpression(x);
  1953. arrayType := RecursivelySetType(x);
  1954. x.SetType(arrayType);
  1955. IF isValue THEN
  1956. value := SyntaxTree.NewMathArrayValue(x.position);
  1957. value.SetArray(x);
  1958. x.SetResolved(value);
  1959. value.SetType(arrayType);
  1960. END;
  1961. x.SetType(arrayType);
  1962. resolvedExpression := x;
  1963. END VisitMathArrayExpression;
  1964. (** check and resolve unary expression **)
  1965. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1966. VAR
  1967. left: SyntaxTree.Expression;
  1968. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1969. bool: BOOLEAN;
  1970. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1971. value: SyntaxTree.Value;
  1972. BEGIN
  1973. type := SyntaxTree.invalidType;
  1974. left := ResolveExpression(unaryExpression.left);
  1975. unaryExpression.SetLeft(left);
  1976. operator := unaryExpression.operator;
  1977. result := unaryExpression;
  1978. IF ~system.operatorDefined[operator] THEN
  1979. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1980. RETURN
  1981. ELSIF left.type = NIL THEN
  1982. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1983. resolvedExpression := SyntaxTree.invalidExpression;
  1984. RETURN
  1985. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1986. RETURN
  1987. END;
  1988. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1989. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1990. END;
  1991. IF operatorCall # NIL THEN
  1992. result := operatorCall;
  1993. type := operatorCall.type;
  1994. (* admissible operators
  1995. Minus number, set
  1996. Not boolean
  1997. *)
  1998. ELSE
  1999. CASE unaryExpression.operator OF
  2000. |Scanner.Minus:
  2001. IF IsIntegerType(left.type.resolved) THEN
  2002. IF left.resolved # NIL THEN
  2003. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2004. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2005. result.SetResolved(value);
  2006. type := Global.GetIntegerType(system,int);
  2007. value.SetType(type);
  2008. ELSE
  2009. type := left.type
  2010. END
  2011. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2012. IF IsRealValue(left,real) THEN
  2013. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2014. result.SetResolved(value);
  2015. type := left.type;
  2016. value.SetType(type);
  2017. ELSE
  2018. type := left.type;
  2019. END;
  2020. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2021. IF IsSetValue(left,set) THEN
  2022. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2023. result.SetResolved(value);
  2024. type := left.type;
  2025. value.SetType(type);
  2026. ELSE
  2027. type := left.type;
  2028. END;
  2029. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2030. IF IsComplexValue(left, real, imaginary) THEN
  2031. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2032. result.SetResolved(value);
  2033. type := left.type;
  2034. value.SetType(type);
  2035. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2036. ELSE
  2037. type := left.type;
  2038. END
  2039. ELSE
  2040. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2041. END;
  2042. |Scanner.Not:
  2043. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2044. IF IsBooleanValue(left,bool) THEN
  2045. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2046. result.SetResolved(value);
  2047. type := system.booleanType;
  2048. value.SetType(type);
  2049. ELSE
  2050. type := system.booleanType;
  2051. END;
  2052. ELSE
  2053. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2054. END;
  2055. |Scanner.Plus:
  2056. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2057. result := left; type := left.type;
  2058. ELSE
  2059. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2060. END;
  2061. (* ADDRESS OF *)
  2062. |Scanner.Address:
  2063. IF HasAddress(left) THEN
  2064. type := system.addressType;
  2065. ELSE
  2066. type := SyntaxTree.invalidType;
  2067. Error(left.position,Diagnostics.Invalid,"has no address");
  2068. Printout.Info("par", left);
  2069. END;
  2070. (* SIZE OF *)
  2071. |Scanner.Size:
  2072. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2073. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2074. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2075. value := SyntaxTree.NewIntegerValue(left.position, int);
  2076. result.SetResolved(value);
  2077. type := Global.GetIntegerType(system,int);
  2078. value.SetType(type)
  2079. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2080. ELSE
  2081. (* for variables, system sizeof could represent the physically occupied size
  2082. determined via the type descriptor, implement that ? *)
  2083. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2084. END
  2085. (* ALIAS OF *)
  2086. |Scanner.Alias:
  2087. type := left.type.resolved;
  2088. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2089. type := SyntaxTree.invalidType;
  2090. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2091. END;
  2092. ELSE
  2093. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2094. END;
  2095. END;
  2096. result.SetType(type);
  2097. resolvedExpression := result
  2098. END VisitUnaryExpression;
  2099. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2100. VAR
  2101. result: SyntaxTree.Expression;
  2102. array: SyntaxTree.MathArrayExpression;
  2103. value: SyntaxTree.MathArrayValue;
  2104. isValue: BOOLEAN;
  2105. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2106. BEGIN
  2107. type := type.resolved;
  2108. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2109. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2110. END;
  2111. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2112. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2113. END;
  2114. RETURN type
  2115. END BaseType;
  2116. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2117. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2118. BEGIN
  2119. numberElements := x.elements.Length();
  2120. FOR i := 0 TO numberElements-1 DO
  2121. expression := x.elements.GetExpression(i);
  2122. IF expression IS SyntaxTree.MathArrayExpression THEN
  2123. array := SyntaxTree.NewMathArrayExpression(position);
  2124. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2125. expression := array;
  2126. ELSE
  2127. position := expression.position;
  2128. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2129. isValue := isValue & (expression.resolved # NIL);
  2130. END;
  2131. to.elements.AddExpression(expression);
  2132. END;
  2133. END RecursivelyConvert;
  2134. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2135. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2136. arrayType: SyntaxTree.MathArrayType;
  2137. BEGIN
  2138. numberElements := x.elements.Length();
  2139. baseType := NIL;
  2140. gsize := 0;
  2141. FOR i := 0 TO numberElements-1 DO
  2142. expression := x.elements.GetExpression(i);
  2143. IF expression IS SyntaxTree.MathArrayExpression THEN
  2144. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2145. IF i=0 THEN
  2146. gsize := size;
  2147. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2148. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2149. ELSE expression.SetType(baseType)
  2150. END;
  2151. ELSIF baseType = NIL THEN baseType := type;
  2152. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2153. END;
  2154. END;
  2155. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2156. arrayType.SetArrayBase(baseType);
  2157. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2158. RETURN ResolveType(arrayType);
  2159. END RecursivelySetType;
  2160. BEGIN
  2161. result := SyntaxTree.invalidExpression;
  2162. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2163. result := expression (* do not convert *)
  2164. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2165. isValue := TRUE;
  2166. type := BaseType(type);
  2167. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2168. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2169. value := SyntaxTree.NewMathArrayValue(array.position);
  2170. value.SetArray(array);
  2171. value.SetType(RecursivelySetType(array));
  2172. result := value;
  2173. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2174. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2175. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2176. IF result = NIL THEN
  2177. result := SyntaxTree.invalidExpression;
  2178. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2179. IF VerboseErrorMessage THEN
  2180. Printout.Info("expression",expression);
  2181. Printout.Info("type",type);
  2182. END;
  2183. END;
  2184. END;
  2185. RETURN result
  2186. END MathArrayConversion;
  2187. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2188. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2189. BEGIN
  2190. result := expression; type := type.resolved;
  2191. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2192. (* skip, no conversion *)
  2193. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2194. int := expression(SyntaxTree.IntegerValue).hvalue;
  2195. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2196. int := Global.ConvertSigned(int,system.SizeOf(type));
  2197. result := SyntaxTree.NewIntegerValue(position,int);
  2198. result.SetType(type);
  2199. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2200. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2201. result := SyntaxTree.NewIntegerValue(position,int);
  2202. result.SetType(type);
  2203. ELSIF (type IS SyntaxTree.FloatType) THEN
  2204. result := SyntaxTree.NewRealValue(expression.position,int);
  2205. result.SetType(type);
  2206. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2207. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2208. result.SetType(type);
  2209. ELSIF (type IS SyntaxTree.SetType) THEN
  2210. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2213. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2216. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2217. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2218. END;
  2219. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.PortType) THEN
  2222. result := ConvertValue(position, expression, system.integerType);
  2223. ELSE
  2224. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2225. result := SyntaxTree.invalidExpression;
  2226. IF VerboseErrorMessage THEN
  2227. Printout.Info("expression",expression);
  2228. Printout.Info("type",type);
  2229. END;
  2230. END;
  2231. ELSIF IsRealValue(expression,real) THEN
  2232. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2233. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2234. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2237. int := ENTIERH(real);
  2238. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2239. result.SetType(type);
  2240. ELSIF (type IS SyntaxTree.FloatType) THEN
  2241. result := SyntaxTree.NewRealValue(position,real);
  2242. result.SetType(type);
  2243. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2244. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2245. result.SetType(type);
  2246. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2247. ELSIF (type IS SyntaxTree.PortType) THEN
  2248. result := ConvertValue(position, expression, system.integerType);
  2249. ELSE
  2250. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2251. result := SyntaxTree.invalidExpression;
  2252. END
  2253. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2254. IF (type IS SyntaxTree.ComplexType) THEN
  2255. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2256. result.SetType(type);
  2257. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2258. ELSE
  2259. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2260. result := SyntaxTree.invalidExpression;
  2261. END
  2262. ELSIF IsSetValue(expression,set) THEN
  2263. IF (type IS SyntaxTree.IntegerType) THEN
  2264. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2265. result.SetType(type);
  2266. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2267. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2268. result.SetType(type);
  2269. ELSIF (type IS SyntaxTree.PortType) THEN
  2270. result := ConvertValue(position, expression, system.integerType);
  2271. ELSE
  2272. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2273. result := SyntaxTree.invalidExpression;
  2274. END;
  2275. ELSIF IsStringValue(expression,string) THEN
  2276. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2277. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2278. result.SetType(type);
  2279. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2280. ELSE
  2281. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. ELSIF IsCharacterValue(expression,char) THEN
  2285. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2286. string[0] := char; string[1] := 0X;
  2287. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2288. result := SyntaxTree.NewStringValue(expression.position,string);
  2289. result.SetType(type);
  2290. ELSIF (type IS SyntaxTree.ByteType) THEN
  2291. (* do not simply set the new type as this could invalidate types of constants *)
  2292. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2293. result.SetType(type)
  2294. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2295. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2296. result.SetType(type);
  2297. ELSIF (type IS SyntaxTree.SetType) THEN
  2298. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2299. result.SetType(type);
  2300. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2301. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2302. result.SetType(type);
  2303. ELSIF (type IS SyntaxTree.PortType) THEN
  2304. result := ConvertValue(position, expression, system.integerType);
  2305. ELSE
  2306. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2307. result := SyntaxTree.invalidExpression;
  2308. END;
  2309. ELSIF expression IS SyntaxTree.NilValue THEN
  2310. IF type IS SyntaxTree.AddressType THEN
  2311. result := SyntaxTree.NewIntegerValue(position,0);
  2312. result.SetType(type);
  2313. ELSE
  2314. result := expression;
  2315. END;
  2316. (* nothing to be done *)
  2317. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2318. result := MathArrayConversion(position, expression,type);
  2319. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2320. int := expression(SyntaxTree.EnumerationValue).value;
  2321. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2322. int := Global.ConvertSigned(int,system.SizeOf(type));
  2323. result := SyntaxTree.NewIntegerValue(position,int);
  2324. result.SetType(type);
  2325. ELSE
  2326. result := expression;
  2327. END;
  2328. (* nothing to be done *)
  2329. ELSE
  2330. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2331. IF VerboseErrorMessage THEN
  2332. Printout.Info("expression",expression);
  2333. Printout.Info("type",type);
  2334. END;
  2335. result := SyntaxTree.invalidExpression;
  2336. END;
  2337. RETURN result
  2338. END ConvertValue;
  2339. (**
  2340. return a conversion of an expression to a given type
  2341. - if expression is already of same type then return expression
  2342. - if incompatible conversion then report error and return invalidExpression
  2343. **)
  2344. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2345. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2346. BEGIN
  2347. type := type.resolved;
  2348. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2349. result := expression;
  2350. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2351. ELSIF expression = NIL THEN (* NIL expression *)
  2352. ELSIF expression.type = NIL THEN
  2353. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2354. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2355. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2356. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2357. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2358. ELSIF expression.resolved # NIL THEN (* value *)
  2359. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2360. IF value IS SyntaxTree.Value THEN
  2361. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2362. result.SetResolved(value(SyntaxTree.Value));
  2363. result.SetType(value.type);
  2364. ELSE
  2365. result := value
  2366. END;
  2367. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2368. expressionList := SyntaxTree.NewExpressionList();
  2369. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2370. typeDeclaration.SetDeclaredType(type);
  2371. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2372. typeSymbol.SetType(typeDeclaration.type);
  2373. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2374. expressionList.AddExpression(expression);
  2375. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2376. result.SetType(type);
  2377. ELSIF IsArrayStructuredObjectType(type) THEN
  2378. (* no type can be converted to an array-structured object type *)
  2379. HALT(100)
  2380. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2381. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2382. result := MathArrayConversion(position, expression,type);
  2383. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2384. expression := ConvertToMathArray(expression);
  2385. type := MathArrayStructureOfType(type);
  2386. result := MathArrayConversion(position, expression, type)
  2387. ELSE
  2388. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2389. END;
  2390. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2391. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2392. OR ~(type IS SyntaxTree.ArrayType) THEN
  2393. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2394. END;
  2395. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2396. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2397. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2398. (*skip, no conversion*)
  2399. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2400. (* skip, no conversion *)
  2401. ELSE
  2402. ASSERT(~(type IS SyntaxTree.RangeType));
  2403. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2404. ASSERT(type # NIL);
  2405. END;
  2406. RETURN result
  2407. END NewConversion;
  2408. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2409. BEGIN
  2410. IF CompatibleTo(system,expression.type, type) THEN
  2411. RETURN NewConversion(position, expression, type, NIL);
  2412. ELSE
  2413. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2414. RETURN SyntaxTree.invalidExpression
  2415. END;
  2416. END CompatibleConversion;
  2417. (**
  2418. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2419. **)
  2420. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2421. VAR leftType,rightType: SyntaxTree.Type;
  2422. BEGIN
  2423. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2424. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2425. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2426. (* error already handled *)
  2427. ELSE
  2428. leftType := left.type.resolved; rightType := right.type.resolved;
  2429. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2430. right := NewConversion(right.position, right, leftType, NIL);
  2431. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2432. left := NewConversion(left.position,left,rightType,NIL);
  2433. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2434. right := NewConversion(right.position, right, leftType, NIL);
  2435. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2436. left := NewConversion(left.position,left,rightType,NIL);
  2437. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2438. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2439. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2440. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2441. ELSIF
  2442. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2443. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2444. (* must be the case LONGREAL / COMPLEX ) *)
  2445. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2446. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2447. ELSE
  2448. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2449. END;
  2450. END;
  2451. END ConvertOperands;
  2452. (** find and return best operator matching to parameter list (nil, if none)
  2453. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2454. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2455. **)
  2456. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2457. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2458. identifier: SyntaxTree.Identifier;
  2459. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2460. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2461. BEGIN
  2462. operator := scope.firstOperator;
  2463. WHILE(operator # NIL) DO
  2464. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2465. procedureType := operator.type(SyntaxTree.ProcedureType);
  2466. distance := Distance(system, procedureType,actualParameters);
  2467. IF (distance < Infinity) THEN
  2468. IF returnType # NIL THEN
  2469. IF procedureType.returnType = NIL THEN
  2470. distance := Infinity
  2471. ELSE
  2472. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2473. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2474. END;
  2475. END;
  2476. END;
  2477. (*
  2478. IF distance < Infinity THEN
  2479. TRACE(distance, operator);
  2480. Printout.Info("potential operator",operator);
  2481. ELSE
  2482. Printout.Info("operator not possible",operator);
  2483. END;
  2484. *)
  2485. IF distance < bestDistance THEN
  2486. bestDistance := distance;
  2487. bestOperator := operator;
  2488. END;
  2489. END;
  2490. operator := operator.nextOperator;
  2491. END;
  2492. (*
  2493. Printout.Info("taken operator",bestOperator);
  2494. *)
  2495. END FindInScope;
  2496. BEGIN
  2497. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2498. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2499. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2500. import := currentScope.ownerModule.moduleScope.firstImport;
  2501. WHILE (bestDistance > 0) & (import # NIL) DO
  2502. IF import.module # NIL THEN
  2503. identifier := Global.GetIdentifier(operator,import.module.case);
  2504. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2505. END;
  2506. import := import.nextImport;
  2507. END;
  2508. RETURN bestOperator
  2509. END FindOperator;
  2510. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2511. BEGIN
  2512. currentScope := scope;
  2513. END SetCurrentScope;
  2514. (**
  2515. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2516. - handle LEN and DIM operator for array-structured object types
  2517. - find operator, if found then
  2518. - if in other module then add import designator
  2519. - create symbol designator for operator
  2520. - if error then return invalidExpression, if no operator then return NIL
  2521. **)
  2522. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2523. VAR
  2524. operator: SyntaxTree.Operator;
  2525. import: SyntaxTree.Import;
  2526. expression, result: SyntaxTree.Expression;
  2527. designator: SyntaxTree.Designator;
  2528. actualParameters, tempList: SyntaxTree.ExpressionList;
  2529. recordType: SyntaxTree.RecordType;
  2530. castReturnType : SyntaxTree.MathArrayType;
  2531. BEGIN
  2532. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2533. result := SyntaxTree.invalidExpression
  2534. ELSIF leftExpression = NIL THEN
  2535. result := NIL
  2536. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2537. (* LEN or DIM operator on array-structured object type *)
  2538. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2539. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2540. IF recordType.arrayAccessOperators.len = NIL THEN
  2541. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2542. result := SyntaxTree.invalidExpression
  2543. ELSE
  2544. ASSERT(leftExpression IS SyntaxTree.Designator);
  2545. designator := leftExpression(SyntaxTree.Designator);
  2546. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2547. ASSERT(expression IS SyntaxTree.Designator);
  2548. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2549. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2550. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2551. result := designator
  2552. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2553. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2554. tempList := SyntaxTree.NewExpressionList();
  2555. tempList.AddExpression(rightExpression);
  2556. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2557. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2558. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2559. tempList := SyntaxTree.NewExpressionList();
  2560. tempList.AddExpression(designator);
  2561. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2562. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2563. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2564. END
  2565. END;
  2566. ELSE
  2567. (* import OCArrayBase if needed *)
  2568. IF ~arrayBaseImported THEN
  2569. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2570. (* operators on complex numbers *)
  2571. ImportModule(Global.ArrayBaseName,position);
  2572. arrayBaseImported := TRUE;
  2573. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2574. IF op = Global.Dim THEN
  2575. (* not existing in OCArrayBase *)
  2576. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2577. (* not existing in OCArrayBase *)
  2578. ELSE
  2579. ImportModule(Global.ArrayBaseName,position);
  2580. arrayBaseImported := TRUE;
  2581. END
  2582. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2583. ImportModule(Global.ArrayBaseName,position);
  2584. arrayBaseImported := TRUE
  2585. END;
  2586. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2587. (* LEN(RANGE) *)
  2588. ImportModule(Global.ArrayBaseName,position);
  2589. arrayBaseImported := TRUE;
  2590. END;
  2591. END;
  2592. actualParameters := SyntaxTree.NewExpressionList();
  2593. actualParameters.AddExpression(leftExpression);
  2594. IF rightExpression # NIL THEN
  2595. actualParameters.AddExpression(rightExpression)
  2596. END;
  2597. operator := FindOperator(system,op,actualParameters,resultType);
  2598. IF operator # NIL THEN
  2599. designator := NIL;
  2600. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2601. import := currentScope.ownerModule.moduleScope.firstImport;
  2602. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2603. import := import.nextImport;
  2604. END;
  2605. expression := NewSymbolDesignator(position,NIL,import);
  2606. designator := expression(SyntaxTree.Designator);
  2607. END;
  2608. expression := NewSymbolDesignator(position,designator,operator);
  2609. designator := expression(SyntaxTree.Designator);
  2610. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2611. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2612. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2613. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2614. result.SetType(castReturnType);
  2615. END;
  2616. ELSE
  2617. result := NIL;
  2618. END;
  2619. END;
  2620. RETURN result
  2621. END NewOperatorCall;
  2622. (** check and resolve binary expression **)
  2623. (*! clean up *)
  2624. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2625. VAR left,right,result: SyntaxTree.Expression;
  2626. leftType, rightType: SyntaxTree.Type;
  2627. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2628. cl,cr: CHAR;
  2629. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2630. type: SyntaxTree.Type;
  2631. value: SyntaxTree.Value;
  2632. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2633. integerConstantFolding: BOOLEAN;
  2634. list: SyntaxTree.ExpressionList;
  2635. PROCEDURE NewBool(v: BOOLEAN);
  2636. BEGIN
  2637. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2638. value.SetType(system.booleanType);
  2639. result.SetResolved(value);
  2640. type := system.booleanType
  2641. END NewBool;
  2642. PROCEDURE NewSet(v: SET);
  2643. BEGIN
  2644. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2645. value.SetType(system.setType);
  2646. result.SetResolved(value);
  2647. type := system.setType;
  2648. END NewSet;
  2649. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2650. BEGIN
  2651. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2652. (* type cast to "larger" type only if the value is still in the range *)
  2653. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2654. value.SetType(t);
  2655. END;
  2656. result.SetResolved(value);
  2657. type := value.type;
  2658. END NewInteger;
  2659. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2660. BEGIN
  2661. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2662. value.SetType(t);
  2663. result.SetResolved(value);
  2664. type := t;
  2665. END NewReal;
  2666. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2667. BEGIN
  2668. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2669. value.SetType(t);
  2670. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2671. result.SetResolved(value);
  2672. type := t;
  2673. END NewComplex;
  2674. BEGIN
  2675. type := SyntaxTree.invalidType;
  2676. left := ResolveExpression(binaryExpression.left);
  2677. right := ResolveExpression(binaryExpression.right);
  2678. binaryExpression.SetLeft(left);
  2679. binaryExpression.SetRight(right);
  2680. result := binaryExpression;
  2681. operator := binaryExpression.operator;
  2682. IF ~system.operatorDefined[operator] THEN
  2683. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2684. result := SyntaxTree.invalidExpression;
  2685. RETURN
  2686. END;
  2687. IF left.type = NIL THEN
  2688. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2689. result := SyntaxTree.invalidExpression;
  2690. RETURN;
  2691. END;
  2692. IF right.type = NIL THEN
  2693. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2694. result := SyntaxTree.invalidExpression;
  2695. RETURN;
  2696. END;
  2697. leftType := left.type.resolved; rightType := right.type.resolved;
  2698. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2699. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2700. END;
  2701. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2702. list := SyntaxTree.NewExpressionList();
  2703. list.AddExpression(right);
  2704. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2705. END;
  2706. IF operatorCall # NIL THEN
  2707. result := operatorCall;
  2708. type := operatorCall.type;
  2709. (* admissible operators:
  2710. Times, Plus, Minus numeric numeric numeric
  2711. set set set
  2712. Slash numeric numeric real /complex
  2713. set set set
  2714. Div , Mod integer integer integer
  2715. And, Or bool bool bool
  2716. Equal, Unequal basic basic bool
  2717. pointer pointer bool
  2718. object object bool
  2719. record record bool
  2720. string string bool
  2721. enumerator enumerator bool
  2722. Less, LessEqual,
  2723. Greater, GreaterEqual integer/real integer/real bool
  2724. enumerator enumerator bool
  2725. In integer set bool
  2726. Is pointer type bool
  2727. object type bool
  2728. record type bool
  2729. Upto: special abbreviation for a..b
  2730. *)
  2731. ELSIF (left.type = NIL) THEN
  2732. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2733. D.Str("nil type in "); D.Type(left); D.Ln;
  2734. result := SyntaxTree.invalidExpression;
  2735. ELSIF (right.type = NIL) THEN
  2736. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2737. result := SyntaxTree.invalidExpression;
  2738. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2739. result := SyntaxTree.invalidExpression;
  2740. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2741. HALT(100);
  2742. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2743. type := system.booleanType;
  2744. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2745. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2746. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2747. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2748. IF VerboseErrorMessage THEN
  2749. Printout.Info("left",left);
  2750. Printout.Info("right",right);
  2751. END;
  2752. ELSIF IsUnsafePointer(left.type) THEN
  2753. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2754. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2755. NewBool(TRUE)
  2756. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2757. NewBool(TRUE);
  2758. ELSIF IsUnextensibleRecord(left) THEN
  2759. NewBool(FALSE)
  2760. END
  2761. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2762. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2763. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2764. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2765. ELSIF operator = Scanner.In THEN (* left IN right *)
  2766. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2767. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2768. NewBool(il IN sr);
  2769. ELSE
  2770. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2771. left := NewConversion(left.position, left, system.longintType,NIL);
  2772. binaryExpression.SetLeft(left)
  2773. END;
  2774. type := system.booleanType;
  2775. END
  2776. ELSE
  2777. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2778. END
  2779. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2780. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2781. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2782. END;
  2783. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2784. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2785. END
  2786. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2787. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2788. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2789. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2790. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2791. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2792. ConvertOperands(left, right);
  2793. binaryExpression.SetLeft(left);
  2794. binaryExpression.SetRight(right);
  2795. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2796. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2797. END;
  2798. type := system.booleanType;
  2799. ELSE
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2801. END
  2802. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2803. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2804. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2805. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2806. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2807. CASE operator OF
  2808. |Scanner.Equal: NewBool(strl^=strr^);
  2809. |Scanner.Unequal:NewBool(strl^#strr^);
  2810. |Scanner.Less: NewBool(strl^<strr^);
  2811. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2812. |Scanner.Greater: NewBool(strl^>strr^);
  2813. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2814. ELSE
  2815. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2816. END;
  2817. END;
  2818. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2819. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2820. type := system.booleanType
  2821. ELSE
  2822. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2823. END;
  2824. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2825. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2826. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2827. type := system.booleanType;
  2828. ELSE
  2829. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2830. END
  2831. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2832. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2833. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2834. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2835. type := system.booleanType
  2836. ELSE
  2837. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2838. END;
  2839. ELSE
  2840. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2841. END;
  2842. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2843. type := system.booleanType;
  2844. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2845. type := system.booleanType;
  2846. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2847. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2848. THEN
  2849. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2850. IF (leftType # rightType) THEN
  2851. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2852. ConvertOperands(left,right); (* operands must be of the same type here *)
  2853. END;
  2854. binaryExpression.SetLeft(left);
  2855. binaryExpression.SetRight(right);
  2856. leftType := left.type.resolved;
  2857. rightType := right.type.resolved;
  2858. END;
  2859. type := leftType;
  2860. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2861. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2862. IF VerboseErrorMessage THEN
  2863. Printout.Info("left",left);
  2864. Printout.Info("right",right);
  2865. END;
  2866. ELSIF IsIntegerType(leftType) THEN
  2867. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2868. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2869. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2870. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2871. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2872. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2873. END;
  2874. END;
  2875. (* constant folding *)
  2876. (* bootstrap64
  2877. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2878. CASE operator OF
  2879. |Scanner.Plus: NewInteger(il+ir,left.type);
  2880. |Scanner.Minus: NewInteger(il-ir,left.type);
  2881. |Scanner.Times: NewInteger(il*ir,left.type);
  2882. |Scanner.Slash:
  2883. IF ir # 0 THEN
  2884. NewReal(il/ir, system.realType);
  2885. END;
  2886. |Scanner.Mod:
  2887. IF ir > 0 THEN
  2888. NewInteger(il MOD ir,left.type);
  2889. END;
  2890. |Scanner.Div:
  2891. IF ir > 0 THEN
  2892. NewInteger(il DIV ir,left.type);
  2893. END;
  2894. |Scanner.Equal: NewBool(il=ir);
  2895. |Scanner.Unequal:NewBool(il#ir);
  2896. |Scanner.Less: NewBool(il<ir);
  2897. |Scanner.LessEqual: NewBool(il<=ir);
  2898. |Scanner.Greater: NewBool(il>ir);
  2899. |Scanner.GreaterEqual: NewBool(il>=ir);
  2900. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2901. END;
  2902. ELS*)
  2903. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2904. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2905. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2906. CASE operator OF
  2907. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2908. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2909. |Scanner.Times: NewInteger(hl*hr,left.type);
  2910. |Scanner.Slash:
  2911. IF hr = 0 THEN
  2912. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2913. ELSE
  2914. IF type.sizeInBits = 64 THEN
  2915. NewReal(hl/hr,system.longrealType);
  2916. ELSE
  2917. NewReal(hl/hr,system.realType)
  2918. END
  2919. END;
  2920. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2921. |Scanner.Mod:
  2922. IF hr = 0 THEN
  2923. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2924. ELSE
  2925. NewInteger(hl MOD hr, left.type);
  2926. (* bootstrap64
  2927. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2928. *)
  2929. END;
  2930. |Scanner.Div:
  2931. IF hr = 0 THEN
  2932. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2933. ELSE
  2934. NewInteger(hl DIV hr, left.type);
  2935. (* bootstrap64
  2936. NewInteger(Machine.DivH(hl,hr),left.type);
  2937. *)
  2938. END;
  2939. (* *)
  2940. |Scanner.Equal: NewBool(hl=hr);
  2941. |Scanner.Unequal: NewBool(hl#hr);
  2942. |Scanner.Less: NewBool(hl<hr);
  2943. |Scanner.LessEqual: NewBool(hl<=hr);
  2944. |Scanner.Greater: NewBool(hl>hr);
  2945. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2946. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2947. END;
  2948. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2949. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2950. type := left.type
  2951. ELSIF (operator = Scanner.Slash) THEN
  2952. left := NewConversion(left.position,left,system.realType,NIL);
  2953. right := NewConversion(right.position,right,system.realType,NIL);
  2954. binaryExpression.SetLeft(left);
  2955. binaryExpression.SetRight(right);
  2956. type := system.realType
  2957. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2958. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2959. type := system.booleanType
  2960. ELSE
  2961. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2962. END;
  2963. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2964. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2965. CASE operator OF
  2966. |Scanner.Plus: NewReal(rl+rr,leftType);
  2967. |Scanner.Minus: NewReal(rl-rr,leftType);
  2968. |Scanner.Times:NewReal(rl*rr,leftType);
  2969. |Scanner.Slash:
  2970. IF rr = 0 THEN
  2971. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2972. ELSE
  2973. NewReal(rl/rr,leftType);
  2974. END
  2975. |Scanner.Equal: NewBool(rl=rr);
  2976. |Scanner.Unequal: NewBool(rl#rr);
  2977. |Scanner.Less: NewBool(rl<rr);
  2978. |Scanner.LessEqual: NewBool(rl<=rr);
  2979. |Scanner.Greater: NewBool(rl>rr);
  2980. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2981. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2982. END;
  2983. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2984. type := left.type
  2985. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2986. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2987. type := system.booleanType
  2988. ELSE
  2989. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2990. IF VerboseErrorMessage THEN
  2991. Printout.Info("left",left);
  2992. Printout.Info("right",right);
  2993. END;
  2994. END;
  2995. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2996. CASE operator OF
  2997. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2998. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2999. ELSE
  3000. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3001. IF VerboseErrorMessage THEN
  3002. Printout.Info("left", left);
  3003. Printout.Info("right", right)
  3004. END;
  3005. END;
  3006. IF ~error THEN
  3007. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3008. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3009. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3010. (* do constant folding *)
  3011. CASE operator OF
  3012. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3013. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3014. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3015. |Scanner.Slash:
  3016. divisor := c * c + d * d;
  3017. ASSERT(divisor # 0);
  3018. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3019. |Scanner.Equal: NewBool((a = c) & (b = d))
  3020. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3021. END
  3022. END
  3023. END
  3024. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3025. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3026. CASE operator OF
  3027. |Scanner.And: NewBool(bl & br);
  3028. |Scanner.Or: NewBool(bl OR br);
  3029. |Scanner.Equal: NewBool(bl = br);
  3030. |Scanner.Unequal: NewBool(bl # br);
  3031. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3032. END;
  3033. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3034. type := system.booleanType
  3035. ELSE
  3036. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3037. END;
  3038. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3039. (* constant folding *)
  3040. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3041. IF operator = Scanner.Equal THEN
  3042. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3043. ELSIF operator = Scanner.Unequal THEN
  3044. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3045. END;
  3046. END;
  3047. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3048. type := system.booleanType;
  3049. ELSE
  3050. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3051. END;
  3052. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3053. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3054. CASE operator OF
  3055. |Scanner.Plus: NewSet(sl + sr);
  3056. |Scanner.Minus: NewSet(sl - sr);
  3057. |Scanner.Times: NewSet(sl * sr);
  3058. |Scanner.Slash: NewSet(sl / sr);
  3059. |Scanner.Equal: NewBool(sl=sr);
  3060. |Scanner.Unequal: NewBool(sl#sr);
  3061. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3062. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3063. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3064. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3065. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3066. END;
  3067. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3068. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3069. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3070. THEN
  3071. type := system.booleanType
  3072. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3073. type := left.type
  3074. ELSE
  3075. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3076. END;
  3077. ELSIF IsCharacterType(left.type) THEN
  3078. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3079. CASE operator OF
  3080. |Scanner.Equal: NewBool(cl=cr);
  3081. |Scanner.Unequal: NewBool(cl#cr);
  3082. |Scanner.Less: NewBool(cl<cr);
  3083. |Scanner.LessEqual: NewBool(cl<=cr);
  3084. |Scanner.Greater: NewBool(cl>cr);
  3085. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3086. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3087. END;
  3088. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3089. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3090. type := system.booleanType
  3091. ELSE
  3092. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3093. END;
  3094. ELSE
  3095. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3096. END;
  3097. ELSE
  3098. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3099. END;
  3100. IF type = SyntaxTree.invalidType THEN
  3101. result := SyntaxTree.invalidExpression
  3102. ELSE
  3103. result.SetType(type)
  3104. END;
  3105. resolvedExpression := result
  3106. END VisitBinaryExpression;
  3107. (** resolve a range expression of the from <<first .. last BY step>>
  3108. - depending on the context different things are checked:
  3109. ArrayIndex:
  3110. - components must be integers
  3111. - replace missing lower bound with 0
  3112. - replace missing upper bound with MAX(LONGINT)
  3113. - replace missing step size with 1
  3114. SetElement:
  3115. - components must be integers
  3116. - replace missing lower bound with 0
  3117. - replace missing upper bound with MAX(SET)
  3118. - must not have step size
  3119. CaseGuard:
  3120. - components must be constant
  3121. - components must be integers or characters
  3122. - must have lower and upper bound present
  3123. - components are made compatible
  3124. - must not have step size
  3125. - if error: return invalidExpression
  3126. **)
  3127. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3128. VAR
  3129. hasError: BOOLEAN;
  3130. first, last, step: SyntaxTree.Expression;
  3131. BEGIN
  3132. hasError := FALSE;
  3133. first := x.first;
  3134. last := x.last;
  3135. step := x.step;
  3136. (* check lower bound *)
  3137. IF x.context = SyntaxTree.CaseGuard THEN
  3138. IF first = NIL THEN
  3139. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3140. hasError := TRUE
  3141. ELSE
  3142. first := ResolveExpression(first);
  3143. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3144. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3145. hasError := TRUE
  3146. ELSE
  3147. IF first IS SyntaxTree.StringValue THEN
  3148. (* add conversion from string to character *)
  3149. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3150. END
  3151. END;
  3152. (* check if expression is constant *)
  3153. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3154. (* error already reported *)
  3155. hasError := TRUE
  3156. END
  3157. END
  3158. ELSE (* ArrayIndex, SetElement *)
  3159. IF first = NIL THEN
  3160. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3161. END;
  3162. first := ResolveExpression(first);
  3163. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3164. first := NewConversion(first.position, first, system.longintType, NIL)
  3165. ELSE
  3166. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3167. hasError := TRUE
  3168. END
  3169. END;
  3170. (* check upper bound *)
  3171. IF x.context = SyntaxTree.CaseGuard THEN
  3172. IF last = NIL THEN
  3173. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3174. hasError := TRUE
  3175. ELSE
  3176. last := ResolveExpression(last);
  3177. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3178. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3179. hasError := TRUE
  3180. ELSE
  3181. IF last IS SyntaxTree.StringValue THEN
  3182. (* add conversion from string to character *)
  3183. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3184. END
  3185. END;
  3186. (* check if expression is constant *)
  3187. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3188. (* error already reported *)
  3189. hasError := TRUE
  3190. ELSE
  3191. (* try to make lower and upper bound compatible *)
  3192. ConvertOperands(first, last);
  3193. IF first.type.resolved # last.type.resolved THEN
  3194. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3195. hasError := TRUE
  3196. END
  3197. END
  3198. END
  3199. ELSE (* ArrayIndex, SetElement *)
  3200. IF last = NIL THEN
  3201. IF x.context = SyntaxTree.ArrayIndex THEN
  3202. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3203. ELSE
  3204. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3205. END
  3206. END;
  3207. last := ResolveExpression(last);
  3208. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3209. last := NewConversion(last.position, last, system.longintType, NIL)
  3210. ELSE
  3211. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3212. hasError := TRUE
  3213. END
  3214. END;
  3215. (* check step size *)
  3216. IF x.context = SyntaxTree.ArrayIndex THEN
  3217. IF step = NIL THEN
  3218. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3219. END;
  3220. step := ResolveExpression(step);
  3221. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3222. step := NewConversion(step.position, step, system.longintType, NIL)
  3223. ELSE
  3224. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3225. hasError := TRUE
  3226. END
  3227. ELSE (* SetElement, CaseGuard *)
  3228. IF step # NIL THEN
  3229. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3230. hasError := TRUE
  3231. END
  3232. END;
  3233. IF hasError THEN
  3234. resolvedExpression := SyntaxTree.invalidExpression
  3235. ELSE
  3236. x.SetFirst(first);
  3237. x.SetLast(last);
  3238. x.SetStep(step);
  3239. x.SetType(system.rangeType);
  3240. resolvedExpression := x;
  3241. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3242. END
  3243. END VisitRangeExpression;
  3244. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3245. BEGIN
  3246. x.SetType(NIL);
  3247. resolvedExpression := x;
  3248. END VisitTensorRangeExpression;
  3249. (** resolve the expression d and return result as designator
  3250. - resolve expression
  3251. - if expression is a designator then return designator else error message and return invalidDesignator
  3252. **)
  3253. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3254. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3255. BEGIN
  3256. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3257. resolved := ResolveExpression(d);
  3258. IF resolved = SyntaxTree.invalidExpression THEN
  3259. (* error should already have been reported *)
  3260. result := SyntaxTree.invalidDesignator;
  3261. ELSIF resolved IS SyntaxTree.Designator THEN
  3262. result := resolved(SyntaxTree.Designator);
  3263. ELSE
  3264. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3265. result := SyntaxTree.invalidDesignator;
  3266. END;
  3267. (* result.type might be nil. *)
  3268. RETURN result
  3269. END ResolveDesignator;
  3270. (**
  3271. symbol designator generated in this module
  3272. nothing to be resolved
  3273. **)
  3274. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3275. BEGIN
  3276. resolvedExpression := x;
  3277. END VisitSymbolDesignator;
  3278. (**
  3279. self designator generated in this module
  3280. nothing to be resolved
  3281. **)
  3282. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3283. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3284. BEGIN
  3285. (* check if in record scope *)
  3286. scope := currentScope;
  3287. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3288. scope := scope.outerScope;
  3289. END;
  3290. IF scope = NIL THEN (* in module scope *)
  3291. x.SetType(system.anyType);
  3292. ELSIF scope IS SyntaxTree.CellScope THEN
  3293. cell := scope(SyntaxTree.CellScope).ownerCell;
  3294. x.SetType(cell);
  3295. ELSE (* in record scope *)
  3296. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3297. IF (record # NIL) & (record.pointerType # NIL) THEN
  3298. type := ResolveType(record.pointerType);
  3299. x.SetType(type);
  3300. ELSE
  3301. x.SetType(record);
  3302. END;
  3303. END;
  3304. resolvedExpression := x;
  3305. END VisitSelfDesignator;
  3306. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3307. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3308. BEGIN
  3309. scope := currentScope;
  3310. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3311. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3312. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3313. returnType := procedureType.returnType;
  3314. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3315. THEN
  3316. x.SetType(returnType);
  3317. ELSE
  3318. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3319. x.SetType(SyntaxTree.invalidType);
  3320. END;
  3321. ELSE
  3322. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3323. x.SetType(SyntaxTree.invalidType);
  3324. END;
  3325. x.SetAssignable(TRUE);
  3326. resolvedExpression := x;
  3327. END VisitResultDesignator;
  3328. (**
  3329. return symbol designator as an expression
  3330. - if symbol is a constant then return the constant value expression
  3331. - else
  3332. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3333. - if symbol is a guarded variable then return a TypeGuardDesignator
  3334. - else return a symbol designator
  3335. **)
  3336. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3337. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3338. guardType: SyntaxTree.Type;
  3339. BEGIN
  3340. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3341. result := SyntaxTree.invalidExpression;
  3342. ASSERT(symbol # NIL);
  3343. (*
  3344. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3345. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3346. Error(position,Diagnostics.Invalid,"type not allowed here");
  3347. ELS *)
  3348. (* not needed any more as values are stored in the expression
  3349. IF symbol IS SyntaxTree.Constant THEN
  3350. result := symbol(SyntaxTree.Constant).value
  3351. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3352. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3353. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3354. ELSE
  3355. result := symbol(SyntaxTree.Constant).value
  3356. END;
  3357. ELSE
  3358. *)
  3359. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3360. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3361. THEN
  3362. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3363. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3364. left := NewDereferenceDesignator(position,left);
  3365. left.SetHidden(TRUE);
  3366. END;
  3367. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3368. scope := currentScope;
  3369. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3370. scope := scope.outerScope;
  3371. END;
  3372. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3373. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3374. END;
  3375. END;
  3376. 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);
  3377. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3378. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3379. ELSE
  3380. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3381. END;
  3382. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3383. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3384. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3385. result.SetType(symbol.type);
  3386. result.SetAssignable(assignable);
  3387. symbol.MarkUsed;
  3388. IF symbol IS SyntaxTree.Constant THEN
  3389. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3390. END;
  3391. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3392. variableAccessed := TRUE
  3393. END;
  3394. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3395. IF GetGuard(symbol,guardType) THEN
  3396. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3397. END;
  3398. END;
  3399. ASSERT(result.type # NIL);
  3400. RETURN result
  3401. END NewSymbolDesignator;
  3402. (** check and resolve an identifier designator "identifier"
  3403. - if identifier = self then return SelfDesignator
  3404. - else find symbol in current scope
  3405. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3406. **)
  3407. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3408. VAR symbol: SyntaxTree.Symbol;
  3409. BEGIN
  3410. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3411. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3412. IF symbol # NIL THEN
  3413. ResolveSymbol(symbol);
  3414. ASSERT(symbol.type # NIL);
  3415. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3416. ELSE
  3417. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3418. IF VerboseErrorMessage THEN
  3419. Printout.Info("undeclared identifier designator",identifierDesignator);
  3420. END;
  3421. resolvedExpression := SyntaxTree.invalidDesignator;
  3422. END;
  3423. END VisitIdentifierDesignator;
  3424. (** check and resolve a selector designator of the form left.designator
  3425. - if left is a pointer type then do auto dereferenciation
  3426. - left denotes a search scope:
  3427. - if left type is import type then set search scope to respective module
  3428. - if left type is enumeration type then set search scope to respective enumeration scope
  3429. - elsif left type is record type then set search scope to record scope
  3430. - search symbol in computed scope
  3431. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3432. **)
  3433. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3434. VAR
  3435. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3436. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3437. BEGIN
  3438. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3439. left := ResolveDesignator(selectorDesignator.left);
  3440. result := SyntaxTree.invalidDesignator;
  3441. IF left # NIL THEN
  3442. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3443. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3444. END;
  3445. scope := NIL;
  3446. IF left.type = NIL THEN
  3447. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3448. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3449. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3450. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3451. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3452. module := symbol(SyntaxTree.Import).module;
  3453. IF module # NIL THEN
  3454. scope := module.moduleScope
  3455. ELSE
  3456. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3457. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3458. END;
  3459. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3460. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3461. ASSERT(scope # NIL)
  3462. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3463. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3464. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3465. IF type IS SyntaxTree.EnumerationType THEN
  3466. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3467. ELSE
  3468. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3469. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3470. END;
  3471. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3472. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3473. ELSE
  3474. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3475. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3476. END;
  3477. symbol := NIL;
  3478. IF scope # NIL THEN
  3479. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3480. IF symbol # NIL THEN
  3481. ResolveSymbol(symbol);
  3482. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3483. symbol.MarkUsed
  3484. ELSE
  3485. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3486. IF VerboseErrorMessage THEN
  3487. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3488. Printout.Info("scope", scope);
  3489. Printout.Info("left", left);
  3490. Printout.Info("undeclared identifier",selectorDesignator);
  3491. Printout.Info("left resolved designator",left);
  3492. END
  3493. END;
  3494. END;
  3495. END;
  3496. resolvedExpression := result;
  3497. END VisitSelectorDesignator;
  3498. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3499. VAR len,idx: LONGINT;
  3500. BEGIN
  3501. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3502. IF idx < 0 THEN
  3503. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3504. ELSE
  3505. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3506. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3507. END;
  3508. END;
  3509. END;
  3510. END IndexCheck;
  3511. (*
  3512. - if index designator has not type, use newBaseType as its type
  3513. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3514. - special rule: if static array of dynamic array occurs, make it all dynamic
  3515. index designator type: new base type: new index designator type:
  3516. NIL z z
  3517. ARRAY [x, y] z ARRAY [x, y] OF z
  3518. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3519. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3520. *)
  3521. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3522. VAR
  3523. mathArrayType: SyntaxTree.MathArrayType;
  3524. makeDynamic: BOOLEAN;
  3525. BEGIN
  3526. IF indexDesignator.type = NIL THEN
  3527. indexDesignator.SetType(newBaseType)
  3528. ELSE
  3529. (* index designator must be a of math array type *)
  3530. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3531. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3532. (* determine if all arrays have to be made dynamic *)
  3533. makeDynamic :=
  3534. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3535. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3536. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3537. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3538. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3539. END;
  3540. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3541. mathArrayType.SetArrayBase(newBaseType)
  3542. END
  3543. END SetIndexBaseType;
  3544. (** check and append index list element to index designator of math array
  3545. - check validity of single index or array range
  3546. - compute new type
  3547. - if range then create new array type (calculate length of resulting array)
  3548. - otherwise take sourceArray.arrayBase as new type
  3549. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3550. **)
  3551. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3552. VAR
  3553. targetArray: SyntaxTree.MathArrayType;
  3554. first, last, step: SyntaxTree.Expression;
  3555. firstValue, lastValue, stepValue, length: LONGINT;
  3556. rangeExpression: SyntaxTree.RangeExpression;
  3557. BEGIN
  3558. IF indexListItem.type = SyntaxTree.invalidType THEN
  3559. (* error already handled *)
  3560. indexDesignator.parameters.AddExpression(indexListItem)
  3561. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3562. indexDesignator.HasRange;
  3563. indexDesignator.HasTensorRange;
  3564. indexDesignator.parameters.AddExpression(indexListItem);
  3565. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3566. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3567. IndexCheck(indexListItem, sourceArray.length);
  3568. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3569. indexDesignator.parameters.AddExpression(indexListItem)
  3570. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3571. indexDesignator.HasRange;
  3572. (* if the range is given as an array range expression, check the validity of its components *)
  3573. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3574. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3575. first := rangeExpression.first;
  3576. last := rangeExpression.last;
  3577. step := rangeExpression.step;
  3578. (* perform static checks on range components *)
  3579. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3580. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3581. END;
  3582. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3583. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3584. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3585. END
  3586. END;
  3587. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3588. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3589. END;
  3590. (* add conversions to size type *)
  3591. (* TODO: needed? *)
  3592. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3593. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3594. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3595. END;
  3596. IF indexDesignator.hasTensorRange THEN
  3597. (* the index designator's base type is a tensor: leave it as is *)
  3598. ELSE
  3599. (* append a new math array to the index designator's base type *)
  3600. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3601. IF ~error THEN
  3602. (*
  3603. (* optimization: calculate length of target array for static ranges *)
  3604. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3605. IF IsStaticallyOpenRange(rangeExpression) THEN
  3606. (* range is open ('*'): reuse source array length as target array length *)
  3607. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3608. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3609. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3610. IF lastValue = MAX(LONGINT) THEN
  3611. IF IsIntegerValue(sourceArray.length, length) THEN
  3612. lastValue := length - 1;
  3613. isStaticTargetArrayLength := TRUE
  3614. ELSE
  3615. isStaticTargetArrayLength := FALSE
  3616. END
  3617. ELSE
  3618. isStaticTargetArrayLength := TRUE
  3619. END;
  3620. IF isStaticTargetArrayLength THEN
  3621. (* calculate static target array length *)
  3622. IF firstValue > lastValue THEN
  3623. length := 0
  3624. ELSE
  3625. length := 1 + lastValue - firstValue;
  3626. IF length MOD stepValue = 0 THEN
  3627. length := length DIV stepValue
  3628. ELSE
  3629. length := length DIV stepValue + 1
  3630. END
  3631. END;
  3632. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3633. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3634. ASSERT(targetArray.form = SyntaxTree.Static)
  3635. END
  3636. END
  3637. END
  3638. *)
  3639. END;
  3640. SetIndexBaseType(indexDesignator, targetArray)
  3641. END;
  3642. indexDesignator.parameters.AddExpression(indexListItem)
  3643. ELSE
  3644. Error(position, Diagnostics.Invalid,"invalid index list item");
  3645. END;
  3646. END AppendMathIndex;
  3647. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3648. VAR parameters: SyntaxTree.ExpressionList;
  3649. BEGIN
  3650. parameters := index.parameters;
  3651. IF (expression.type = NIL) THEN
  3652. Error(position,Diagnostics.Invalid,"invalid index");
  3653. ELSIF IsIntegerType(expression.type.resolved) THEN
  3654. IF over IS SyntaxTree.ArrayType THEN
  3655. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3656. ELSIF over IS SyntaxTree.StringType THEN
  3657. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3658. END;
  3659. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3660. parameters.AddExpression(expression);
  3661. ELSE
  3662. Error(position,Diagnostics.Invalid,"invalid index");
  3663. END;
  3664. END AppendIndex;
  3665. (** convert an expression to math array type
  3666. if expression is of math array type: return expression itself
  3667. if expression is of array-structured object type: return an index operator call on it
  3668. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3669. otherwise: return invalid expression
  3670. **)
  3671. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3672. VAR
  3673. result: SyntaxTree.Expression;
  3674. mathArrayType: SyntaxTree.MathArrayType;
  3675. BEGIN
  3676. IF expression.type = NIL THEN
  3677. result := SyntaxTree.invalidExpression
  3678. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3679. (* expression of math array type *)
  3680. result := expression
  3681. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3682. (* expression of array-structured object type *)
  3683. mathArrayType := MathArrayStructureOfType(expression.type);
  3684. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3685. ELSE
  3686. result := SyntaxTree.invalidExpression
  3687. END;
  3688. RETURN result
  3689. END ConvertToMathArray;
  3690. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3691. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3692. VAR
  3693. result: SyntaxTree.ExpressionList;
  3694. i: LONGINT;
  3695. BEGIN
  3696. result := SyntaxTree.NewExpressionList();
  3697. FOR i := 1 TO itemCount DO
  3698. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3699. END;
  3700. RETURN result
  3701. END ListOfOpenRanges;
  3702. (** create a procedure call designator for an index operator call on an array-structured object type
  3703. - use given index list as actual parameters
  3704. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3705. **)
  3706. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3707. VAR
  3708. operator: SyntaxTree.Operator;
  3709. expression: SyntaxTree.Expression;
  3710. actualParameters, tempList: SyntaxTree.ExpressionList;
  3711. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3712. result, tempDesignator: SyntaxTree.Designator;
  3713. recordType: SyntaxTree.RecordType;
  3714. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3715. i, hashValue, indexListSize, indexListKind: LONGINT;
  3716. castReturnType: SyntaxTree.MathArrayType;
  3717. BEGIN
  3718. ASSERT(IsArrayStructuredObjectType(left.type));
  3719. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3720. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3721. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3722. indexListSize := indexList.Length();
  3723. indexListKind := 0;
  3724. containsNonRange := FALSE;
  3725. FOR i := 0 TO indexList.Length() - 1 DO
  3726. indexListKind := indexListKind * 2;
  3727. expression := indexList.GetExpression(i);
  3728. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3729. INC(indexListKind)
  3730. ELSE
  3731. containsNonRange := TRUE
  3732. END
  3733. END;
  3734. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3735. (* select applicable index operator
  3736. - try to look up optimal index operator
  3737. - if not present, use operator on ranges
  3738. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3739. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3740. *)
  3741. usesGeneralOperator := FALSE;
  3742. IF rhs # NIL THEN
  3743. (* write operator *)
  3744. IF hashValue = -1 THEN
  3745. operator := NIL
  3746. ELSE
  3747. operator := recordType.arrayAccessOperators.write[hashValue];
  3748. END;
  3749. IF operator = NIL THEN
  3750. usesPureRangeOperator := TRUE;
  3751. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3752. operator := recordType.arrayAccessOperators.generalWrite;
  3753. usesGeneralOperator := TRUE
  3754. ELSE
  3755. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3756. operator := recordType.arrayAccessOperators.write[hashValue];
  3757. END
  3758. END
  3759. ELSE
  3760. (* read operator *)
  3761. IF hashValue = -1 THEN
  3762. operator := NIL
  3763. ELSE
  3764. operator := recordType.arrayAccessOperators.read[hashValue];
  3765. END;
  3766. IF operator = NIL THEN
  3767. usesPureRangeOperator := TRUE;
  3768. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3769. operator := recordType.arrayAccessOperators.generalRead;
  3770. usesGeneralOperator := TRUE
  3771. ELSE
  3772. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3773. operator := recordType.arrayAccessOperators.read[hashValue];
  3774. END
  3775. END
  3776. END;
  3777. IF operator = NIL THEN
  3778. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3779. result := SyntaxTree.invalidDesignator;
  3780. ELSE
  3781. (* determine if reshaping is needed *)
  3782. needsReshaping := containsNonRange & usesPureRangeOperator;
  3783. (* import OCArrayBase if reshaping is needed *)
  3784. IF needsReshaping & ~arrayBaseImported THEN
  3785. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3786. arrayBaseImported := TRUE
  3787. END;
  3788. (* add the index list item to the list of actual parameters
  3789. - for general operators: add a single inline array containing the index list items as parameter
  3790. - otherwise: add all index list items as individual parameters
  3791. *)
  3792. actualParameters := SyntaxTree.NewExpressionList();
  3793. IF usesGeneralOperator THEN
  3794. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3795. END;
  3796. FOR i := 0 TO indexListSize - 1 DO
  3797. expression := indexList.GetExpression(i);
  3798. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3799. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3800. tempList := SyntaxTree.NewExpressionList();
  3801. tempList.AddExpression(expression);
  3802. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3803. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3804. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3805. END;
  3806. IF usesGeneralOperator THEN
  3807. tempMathArrayExpression.elements.AddExpression(expression);
  3808. ELSE
  3809. actualParameters.AddExpression(expression)
  3810. END
  3811. END;
  3812. IF usesGeneralOperator THEN
  3813. actualParameters.AddExpression(tempMathArrayExpression)
  3814. END;
  3815. IF rhs # NIL THEN
  3816. (* add actual parameter for RHS *)
  3817. IF needsReshaping THEN
  3818. (* reshape using OCArrayBase.ExpandDimensions *)
  3819. tempList := SyntaxTree.NewExpressionList();
  3820. (* source array *)
  3821. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3822. tempList.AddExpression(rhs);
  3823. ELSE
  3824. (* convert scalar to one-dimensional array *)
  3825. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3826. tempMathArrayExpression.elements.AddExpression(rhs);
  3827. tempList.AddExpression(tempMathArrayExpression)
  3828. END;
  3829. (* list of kept dimensions *)
  3830. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3831. FOR i := 0 TO indexListSize - 1 DO
  3832. expression := indexList.GetExpression(i);
  3833. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3834. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3835. ELSE
  3836. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3837. END
  3838. END;
  3839. tempList.AddExpression(tempMathArrayExpression);
  3840. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3841. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3842. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3843. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3844. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3845. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3846. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3847. expression.SetType(castReturnType);
  3848. ELSE
  3849. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3850. END;
  3851. actualParameters.AddExpression(expression)
  3852. ELSE
  3853. actualParameters.AddExpression(rhs)
  3854. END
  3855. END;
  3856. (* add dereference operator and create procedure call designator *)
  3857. ASSERT(left IS SyntaxTree.Designator);
  3858. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3859. ASSERT(expression IS SyntaxTree.Designator);
  3860. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3861. IF (rhs = NIL) & needsReshaping THEN
  3862. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3863. tempList := SyntaxTree.NewExpressionList();
  3864. FOR i := 0 TO indexList.Length() - 1 DO
  3865. expression := indexList.GetExpression(i);
  3866. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3867. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3868. ELSE
  3869. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3870. END
  3871. END;
  3872. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3873. END;
  3874. IF rhs = NIL THEN
  3875. (* special rule: index read operator calls are considered to be assignable *)
  3876. result.SetAssignable(TRUE)
  3877. END;
  3878. (* put information about this index operator call into the resulting designator *)
  3879. result.SetRelatedAsot(left);
  3880. result.SetRelatedIndexList(indexList)
  3881. END;
  3882. RETURN result
  3883. END NewIndexOperatorCall;
  3884. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3885. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3886. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3887. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3888. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3889. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3890. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3891. CONST trace = FALSE;
  3892. BEGIN
  3893. IF trace THEN
  3894. FOR i := 0 TO actualParameters.Length()-1 DO
  3895. Printout.Info("par", actualParameters.GetExpression(i));
  3896. END;
  3897. END;
  3898. operator := scope.firstOperator;
  3899. WHILE(operator # NIL) DO
  3900. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3901. procedureType := operator.type(SyntaxTree.ProcedureType);
  3902. distance := Distance(system, procedureType,actualParameters);
  3903. IF trace THEN Printout.Info("check op ",operator) END;
  3904. IF distance < bestDistance THEN
  3905. IF trace THEN Printout.Info("taken op",operator) END;
  3906. bestDistance := distance;
  3907. bestOperator := operator;
  3908. END;
  3909. END;
  3910. operator := operator.nextOperator;
  3911. END;
  3912. END FindInScope;
  3913. BEGIN
  3914. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3915. IF oper = 0 THEN (* index *)
  3916. identifier := SyntaxTree.NewIdentifier("[]");
  3917. ELSE
  3918. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3919. END;
  3920. WHILE (recordType # NIL) DO
  3921. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3922. recordType := recordType.GetBaseRecord();
  3923. END;
  3924. RETURN bestOperator
  3925. END FindOperator;
  3926. BEGIN
  3927. type := left.type.resolved;
  3928. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3929. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3930. actualParameters := SyntaxTree.NewExpressionList();
  3931. IF parameters # NIL THEN
  3932. FOR i := 0 TO parameters.Length()-1 DO
  3933. expression := ResolveExpression(parameters.GetExpression(i));
  3934. actualParameters.AddExpression(expression);
  3935. END;
  3936. END;
  3937. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3938. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3939. IF op # NIL THEN
  3940. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3941. ASSERT(expression IS SyntaxTree.Designator);
  3942. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3943. result.SetRelatedAsot(left);
  3944. result.SetRelatedIndexList(parameters);
  3945. (* check if write operator exists, for var parameters *)
  3946. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3947. actualParameters := SyntaxTree.NewExpressionList();
  3948. FOR i := 0 TO parameters.Length()-1 DO
  3949. expression := ResolveExpression(parameters.GetExpression(i));
  3950. actualParameters.AddExpression(expression);
  3951. END;
  3952. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3953. actualParameters.AddExpression(rhs);
  3954. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3955. IF op = NIL THEN rhs := NIL END;
  3956. END;
  3957. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3958. ELSE
  3959. result := NIL;
  3960. END;
  3961. RETURN result;
  3962. END NewObjectOperatorCall;
  3963. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3964. 1. convert bracket designator chains into a single one that contains separators
  3965. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3966. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3967. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3968. - if an array or math array is indexed over, create index designator
  3969. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3970. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3971. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3972. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3973. - if an array-structured object type is indexed over, create procedure call designator
  3974. e.g.: a[x, y] -> a^."[]"(x, y)
  3975. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3976. - a[i, *] = a[i][*]
  3977. - a[*, i] # a[*][i]
  3978. Because:
  3979. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3980. - 'i-th column' = a[*, i]
  3981. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3982. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3983. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3984. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3985. **)
  3986. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3987. VAR
  3988. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3989. indexDesignator: SyntaxTree.IndexDesignator;
  3990. designator: SyntaxTree.Designator;
  3991. type: SyntaxTree.Type;
  3992. recordType: SyntaxTree.RecordType;
  3993. expression, rhs: SyntaxTree.Expression;
  3994. indexList: SyntaxTree.ExpressionList;
  3995. i: LONGINT;
  3996. hasError, done: BOOLEAN;
  3997. PROCEDURE FinalizeIndexDesignator;
  3998. BEGIN
  3999. IF indexDesignator # NIL THEN
  4000. (* the end of a tensor has been reached: *)
  4001. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4002. SetIndexBaseType(indexDesignator, type);
  4003. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4004. designator := indexDesignator;
  4005. type := designator.type.resolved;
  4006. indexDesignator := NIL;
  4007. ASSERT(SyntaxTree.Resolved IN type.state)
  4008. END
  4009. END FinalizeIndexDesignator;
  4010. BEGIN
  4011. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4012. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4013. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4014. (* copy all index list entries including a separator to the left bracket designator *)
  4015. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4016. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4017. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4018. END;
  4019. (* propagate the related RHS *)
  4020. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4021. (* only resolve left bracket designator and use as final result *)
  4022. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4023. ELSE
  4024. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4025. designator := ResolveDesignator(bracketDesignator.left);
  4026. type := designator.type.resolved;
  4027. indexDesignator := NIL;
  4028. (*!!! clean up *)
  4029. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4030. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4031. IF resolvedExpression = NIL THEN
  4032. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4033. resolvedExpression := SyntaxTree.invalidDesignator
  4034. END;
  4035. RETURN;
  4036. END;
  4037. i := 0;
  4038. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4039. expression := bracketDesignator.parameters.GetExpression(i);
  4040. expression := ResolveExpression(expression);
  4041. bracketDesignator.parameters.SetExpression(i, expression);
  4042. IF expression = SyntaxTree.indexListSeparator THEN
  4043. (* finalize an existing index designator if needed *)
  4044. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4045. INC(i)
  4046. ELSE
  4047. (* do auto-dereferencing if needed *)
  4048. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4049. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4050. & (i=0)*)
  4051. THEN
  4052. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4053. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4054. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4055. designator := SyntaxTree.invalidDesignator;
  4056. type := SyntaxTree.invalidType
  4057. ELSE
  4058. FinalizeIndexDesignator;
  4059. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4060. type := designator.type.resolved
  4061. END
  4062. END;
  4063. (* create a new index designator, if needed *)
  4064. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4065. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4066. indexDesignator.SetAssignable(designator.assignable);
  4067. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4068. (* designator := indexDesignator *)
  4069. END;
  4070. IF type = SyntaxTree.invalidType THEN
  4071. (* error already handled *)
  4072. INC(i)
  4073. ELSIF type IS SyntaxTree.ArrayType THEN
  4074. (* indexing over an array *)
  4075. ASSERT(indexDesignator # NIL);
  4076. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4077. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4078. INC(i)
  4079. ELSIF type IS SyntaxTree.StringType THEN
  4080. (* indexing over an array *)
  4081. ASSERT(indexDesignator # NIL);
  4082. AppendIndex(expression.position, indexDesignator, expression, type);
  4083. type := type(SyntaxTree.StringType).baseType.resolved;
  4084. INC(i)
  4085. ELSIF type IS SyntaxTree.MathArrayType THEN
  4086. (* indexing over a math array *)
  4087. ASSERT(indexDesignator # NIL);
  4088. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4089. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4090. INC(i)
  4091. ELSIF IsArrayStructuredObjectType(type) THEN
  4092. (* indexing over ASOTs *)
  4093. FinalizeIndexDesignator;
  4094. ASSERT(type IS SyntaxTree.PointerType);
  4095. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4096. (*
  4097. - collect index list items from bracket designator that belong to ASOT
  4098. - check for errors
  4099. *)
  4100. indexList := SyntaxTree.NewExpressionList();
  4101. hasError := FALSE;
  4102. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4103. (* indexing over tensor ASOT:
  4104. - stop at index list end or separator
  4105. - dimensionality is given by number of index list items
  4106. *)
  4107. done := FALSE;
  4108. WHILE ~done DO
  4109. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4110. done := TRUE;
  4111. ELSE
  4112. expression := bracketDesignator.parameters.GetExpression(i);
  4113. IF expression = SyntaxTree.indexListSeparator THEN
  4114. done := TRUE;
  4115. ELSE
  4116. expression := ResolveExpression(expression);
  4117. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4118. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4119. hasError := TRUE
  4120. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4121. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4122. expression := SyntaxTree.invalidExpression;
  4123. hasError := TRUE
  4124. END;
  4125. indexList.AddExpression(expression)
  4126. END;
  4127. INC(i)
  4128. END
  4129. END
  4130. ELSE
  4131. (* indexing over non-tensor ASOT:
  4132. - ignore separators
  4133. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4134. *)
  4135. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4136. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4137. expression := bracketDesignator.parameters.GetExpression(i);
  4138. ELSE
  4139. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4140. END;
  4141. IF expression # SyntaxTree.indexListSeparator THEN
  4142. expression := ResolveExpression(expression);
  4143. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4144. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4145. expression := SyntaxTree.invalidExpression;
  4146. hasError := TRUE
  4147. END;
  4148. indexList.AddExpression(expression)
  4149. END;
  4150. INC(i)
  4151. END;
  4152. END;
  4153. IF hasError THEN
  4154. designator := SyntaxTree.invalidDesignator;
  4155. type := SyntaxTree.invalidType;
  4156. ELSE
  4157. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4158. and the last entry in the index list belongs to the array-structured object type in question.
  4159. E.g.: for a 2-dimensional array-structured object type:
  4160. - 'lhs := asot[1, 2]' -> read mode
  4161. - 'asot[1, 2] := rhs' -> write mode
  4162. - 'asot[1, 2, 3] := rhs' -> read mode
  4163. *)
  4164. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4165. rhs := bracketDesignator.relatedRhs
  4166. ELSE
  4167. rhs := NIL
  4168. END;
  4169. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4170. type := designator.type
  4171. END
  4172. ELSE
  4173. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4174. designator := SyntaxTree.invalidDesignator;
  4175. type := SyntaxTree.invalidType;
  4176. INC(i)
  4177. END
  4178. END
  4179. END;
  4180. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4181. resolvedExpression := designator
  4182. END
  4183. END VisitBracketDesignator;
  4184. (** check and resolve expression list
  4185. - resolve each expression in an expression list
  4186. - returns true if and only if all statements could have successfully been resolved
  4187. **)
  4188. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4189. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4190. BEGIN
  4191. result := TRUE;
  4192. FOR i := 0 TO expressionList.Length()-1 DO
  4193. expression := ResolveExpression(expressionList.GetExpression(i));
  4194. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4195. expressionList.SetExpression(i,expression);
  4196. END;
  4197. RETURN result
  4198. END ExpressionList;
  4199. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4200. BEGIN
  4201. type := type.resolved;
  4202. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4203. RETURN TRUE
  4204. ELSIF system.CanPassInRegister # NIL THEN
  4205. RETURN system.CanPassInRegister(type);
  4206. ELSE
  4207. RETURN FALSE
  4208. END;
  4209. END CanPassInRegister;
  4210. (** return procedure call designator left(actualParameters)
  4211. - check realtime procedure call in realtime procedure
  4212. - check number of parameters
  4213. - check parameter compatibility
  4214. return invalidDesignator if error
  4215. **)
  4216. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4217. VAR result: SyntaxTree.Designator;
  4218. numberFormalParameters, numberActualParameters: LONGINT;
  4219. formalType: SyntaxTree.ProcedureType;
  4220. formalParameter: SyntaxTree.Parameter;
  4221. actualParameter: SyntaxTree.Expression;
  4222. i: LONGINT;
  4223. BEGIN
  4224. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4225. result := SyntaxTree.invalidDesignator;
  4226. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4227. numberFormalParameters := formalType.numberParameters;
  4228. numberActualParameters := actualParameters.Length();
  4229. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4230. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4231. END;
  4232. IF ~ExpressionList(actualParameters) THEN
  4233. result := SyntaxTree.invalidDesignator
  4234. ELSE
  4235. IF numberActualParameters <= numberFormalParameters THEN
  4236. formalParameter := formalType.firstParameter;
  4237. FOR i := 0 TO numberActualParameters-1 DO
  4238. actualParameter := actualParameters.GetExpression(i);
  4239. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4240. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4241. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4242. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4243. ELSE
  4244. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4245. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4246. END;
  4247. actualParameters.SetExpression(i,actualParameter);
  4248. END;
  4249. formalParameter := formalParameter.nextParameter;
  4250. END;
  4251. WHILE (formalParameter # NIL) DO
  4252. IF formalParameter.defaultValue # NIL THEN
  4253. actualParameters.AddExpression(formalParameter.defaultValue);
  4254. formalParameter := formalParameter.nextParameter
  4255. ELSE
  4256. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4257. formalParameter := NIL;
  4258. END;
  4259. END;
  4260. ELSE
  4261. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4262. END;
  4263. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4264. result.SetAssignable(FALSE);
  4265. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4266. END;
  4267. RETURN result
  4268. END NewProcedureCallDesignator;
  4269. (**
  4270. builtin call designator generated in VisitParameterDesignator
  4271. -> nothing to be resolved
  4272. **)
  4273. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4274. BEGIN
  4275. resolvedExpression := x;
  4276. END VisitTypeGuardDesignator;
  4277. (**
  4278. builtin call designator generated in VisitParameterDesignator
  4279. -> nothing to be resolved
  4280. **)
  4281. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4282. BEGIN
  4283. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4284. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4285. ASSERT(resolvedExpression.type # NIL);
  4286. ELSIF ExpressionList(x.parameters) THEN
  4287. resolvedExpression := x;
  4288. END;
  4289. END VisitBuiltinCallDesignator;
  4290. (**
  4291. procedure call designator generated in VisitParameterDesignator
  4292. -> nothing to be resolved
  4293. **)
  4294. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4295. BEGIN
  4296. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4297. resolvedExpression := x;
  4298. END VisitProcedureCallDesignator;
  4299. (** return true if x is a variable else return false and report error **)
  4300. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4301. VAR result: BOOLEAN;
  4302. BEGIN
  4303. result := TRUE;
  4304. IF x = SyntaxTree.invalidExpression THEN
  4305. result := FALSE;
  4306. ELSIF ~IsVariable(x) THEN
  4307. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4308. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4309. result := FALSE;
  4310. END;
  4311. RETURN result
  4312. END CheckVariable;
  4313. (**
  4314. if expression x is of basic type then return true else report error and return false
  4315. **)
  4316. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN;
  4318. BEGIN
  4319. result := FALSE;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~IsBasicType(x.type) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4323. result := FALSE
  4324. ELSE result := TRUE
  4325. END;
  4326. RETURN result
  4327. END CheckBasicType;
  4328. (**
  4329. if expression x is of number type then return true else report error and return false
  4330. **)
  4331. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4332. VAR result: BOOLEAN;
  4333. BEGIN
  4334. result := FALSE;
  4335. IF x = SyntaxTree.invalidExpression THEN
  4336. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4337. Error(x.position,Diagnostics.Invalid,"is non number type");
  4338. ELSE result := TRUE
  4339. END;
  4340. RETURN result
  4341. END CheckNumberType;
  4342. (**
  4343. if expression x is of number or size type but not complex then return true else report error and return false
  4344. **)
  4345. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4346. VAR result: BOOLEAN;
  4347. BEGIN
  4348. result := FALSE;
  4349. IF x = SyntaxTree.invalidExpression THEN
  4350. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4351. Error(x.position,Diagnostics.Invalid,"is complex type");
  4352. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4353. Error(x.position,Diagnostics.Invalid,"is non number type");
  4354. ELSE result := TRUE
  4355. END;
  4356. RETURN result
  4357. END CheckNonComplexNumberSizeType;
  4358. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4359. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4360. BEGIN
  4361. result := FALSE; type := x.type.resolved;
  4362. IF x = SyntaxTree.invalidExpression THEN
  4363. 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
  4364. TRACE(type.sizeInBits);
  4365. TRACE(system.addressType.sizeInBits);
  4366. Error(x.position,Diagnostics.Invalid,"is no address type");
  4367. ELSE result := TRUE
  4368. END;
  4369. RETURN result
  4370. END CheckAddressType;
  4371. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4372. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4373. BEGIN
  4374. result := FALSE; type := x.type.resolved;
  4375. IF x = SyntaxTree.invalidExpression THEN
  4376. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4377. Error(x.position,Diagnostics.Invalid,"is no size type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckSizeType;
  4382. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4384. BEGIN
  4385. result := FALSE; type := x.type.resolved;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. ELSIF ~(type IS SyntaxTree.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
  4388. Error(x.position,Diagnostics.Invalid,"is no object type");
  4389. ELSE result := TRUE
  4390. END;
  4391. RETURN result
  4392. END CheckObjectType;
  4393. (**
  4394. if expression x is of integer type then return true else report error and return false
  4395. **)
  4396. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4397. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4398. BEGIN
  4399. result := FALSE; type := x.type.resolved;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4402. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4403. ELSE result := TRUE
  4404. END;
  4405. RETURN result
  4406. END CheckIntegerType;
  4407. (**
  4408. if expression x is of character type then return true else report error and return false
  4409. **)
  4410. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4411. VAR result: BOOLEAN;
  4412. BEGIN
  4413. result := FALSE;
  4414. IF x = SyntaxTree.invalidExpression THEN
  4415. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4416. Error(x.position,Diagnostics.Invalid,"is no character type");
  4417. ELSE result := TRUE
  4418. END;
  4419. RETURN result
  4420. END CheckCharacterType;
  4421. (**
  4422. if expression x is of real type then return true else report error and return false
  4423. **)
  4424. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4425. VAR result: BOOLEAN;
  4426. BEGIN
  4427. result := FALSE;
  4428. IF x = SyntaxTree.invalidExpression THEN
  4429. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4430. Error(x.position,Diagnostics.Invalid,"is no float type");
  4431. ELSE result := TRUE
  4432. END;
  4433. RETURN result
  4434. END CheckRealType;
  4435. (**
  4436. if expression x is of range type then return true else report error and return false
  4437. **)
  4438. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4439. VAR result: BOOLEAN;
  4440. BEGIN
  4441. result := FALSE;
  4442. IF x = SyntaxTree.invalidExpression THEN
  4443. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4444. Error(x.position,Diagnostics.Invalid,"is no range type");
  4445. ELSE result := TRUE
  4446. END;
  4447. RETURN result
  4448. END CheckRangeType;
  4449. (**
  4450. if expression x is of boolean type then return true else report error and return false
  4451. **)
  4452. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4453. VAR result: BOOLEAN;
  4454. BEGIN
  4455. result := FALSE;
  4456. IF x = SyntaxTree.invalidExpression THEN
  4457. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4458. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4459. ELSE result := TRUE
  4460. END;
  4461. RETURN result
  4462. END CheckBooleanType;
  4463. (**
  4464. if expression x is of set type then return true else report error and return false
  4465. **)
  4466. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4467. VAR result: BOOLEAN;
  4468. BEGIN
  4469. result := FALSE;
  4470. IF x = SyntaxTree.invalidExpression THEN
  4471. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4472. Error(x.position,Diagnostics.Invalid,"is no set type");
  4473. ELSE result := TRUE
  4474. END;
  4475. RETURN result
  4476. END CheckSetType;
  4477. (**
  4478. if expression x is of string or array of character type then return true else report error and return false
  4479. **)
  4480. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4481. VAR result: BOOLEAN;
  4482. BEGIN
  4483. result := FALSE;
  4484. IF x = SyntaxTree.invalidExpression THEN
  4485. ELSIF ~IsStringType(x.type.resolved) THEN
  4486. Error(x.position,Diagnostics.Invalid,"is no string type");
  4487. ELSE result := TRUE
  4488. END;
  4489. RETURN result
  4490. END CheckStringType;
  4491. (**
  4492. if expression x is a type declaration type return true else report error and return false
  4493. **)
  4494. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4495. VAR result: BOOLEAN;
  4496. BEGIN
  4497. result := FALSE;
  4498. IF x = SyntaxTree.invalidExpression THEN
  4499. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4500. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4501. ELSE result := TRUE
  4502. END;
  4503. RETURN result
  4504. END CheckTypeDeclarationType;
  4505. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4506. VAR result: BOOLEAN;
  4507. BEGIN
  4508. result := FALSE;
  4509. IF x = SyntaxTree.invalidExpression THEN
  4510. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4511. result := TRUE;
  4512. value := x.resolved(SyntaxTree.IntegerValue).value;
  4513. ELSE
  4514. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4515. END;
  4516. RETURN result;
  4517. END CheckIntegerValue;
  4518. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4524. result := TRUE;
  4525. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4526. ELSE
  4527. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4528. END;
  4529. RETURN result;
  4530. END CheckStringValue;
  4531. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4532. BEGIN
  4533. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4534. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4535. ELSE
  4536. RETURN FALSE
  4537. END;
  4538. END IsUnsignedValue;
  4539. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4540. BEGIN
  4541. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4542. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4543. ELSE
  4544. RETURN FALSE
  4545. END
  4546. END IsAddressValue;
  4547. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4548. BEGIN
  4549. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4550. END IsAddressExpression;
  4551. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4552. BEGIN
  4553. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4554. END IsSizeExpression;
  4555. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4556. VAR result: BOOLEAN;
  4557. BEGIN
  4558. result := FALSE;
  4559. IF x = SyntaxTree.invalidExpression THEN
  4560. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4561. result := TRUE;
  4562. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4563. ELSE
  4564. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4565. END;
  4566. RETURN result;
  4567. END CheckEnumerationValue;
  4568. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4569. VAR result: BOOLEAN;
  4570. BEGIN
  4571. result := FALSE;
  4572. IF x = SyntaxTree.invalidExpression THEN
  4573. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4574. result := TRUE;
  4575. value := x.resolved(SyntaxTree.CharacterValue).value;
  4576. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4577. result := TRUE;
  4578. value := x.resolved(SyntaxTree.StringValue).value[0];
  4579. ELSE
  4580. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4581. END;
  4582. RETURN result;
  4583. END CheckCharacterValue;
  4584. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4585. VAR result: BOOLEAN;
  4586. BEGIN
  4587. result := FALSE;
  4588. IF x = SyntaxTree.invalidExpression THEN
  4589. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4590. value := x.resolved(SyntaxTree.IntegerValue).value;
  4591. IF (value > 0) OR includeZero & (value = 0) THEN
  4592. result := TRUE;
  4593. ELSE
  4594. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4595. END
  4596. ELSE
  4597. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4598. END;
  4599. RETURN result;
  4600. END CheckPositiveIntegerValue;
  4601. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4602. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4603. BEGIN
  4604. result := FALSE;
  4605. IF x = SyntaxTree.invalidExpression THEN
  4606. ELSE
  4607. type := x.type.resolved;
  4608. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4609. portType := type(SyntaxTree.PortType);
  4610. result := TRUE
  4611. ELSE
  4612. Error(x.position,Diagnostics.Invalid,"no port type");
  4613. END;
  4614. END;
  4615. RETURN result
  4616. END CheckPortType;
  4617. (* move to builtin procedure call statement ?
  4618. remove builtin procedure call designator ?
  4619. *)
  4620. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4621. VAR
  4622. numberActualParameters,numberFormalParameters: LONGINT;
  4623. formalParameter: SyntaxTree.Parameter;
  4624. actualParameter: SyntaxTree.Expression;
  4625. procedureType: SyntaxTree.ProcedureType;
  4626. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4627. inPort, outPort: SyntaxTree.PortType;
  4628. constructor: SyntaxTree.Procedure;
  4629. type0,type1,type2: SyntaxTree.Type;
  4630. type,base,parameterType: SyntaxTree.Type;
  4631. arrayType: SyntaxTree.ArrayType;
  4632. i,i0,i1: LONGINT;
  4633. r,r0,r1,im: LONGREAL;
  4634. c: CHAR;
  4635. id: LONGINT;
  4636. b: BOOLEAN;
  4637. first: LONGINT;
  4638. mathArrayType: SyntaxTree.MathArrayType;
  4639. customBuiltin: SyntaxTree.CustomBuiltin;
  4640. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4641. VAR resultB: BOOLEAN;
  4642. BEGIN
  4643. IF numberActualParameters < from THEN
  4644. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4645. result := SyntaxTree.invalidExpression;
  4646. resultB := FALSE;
  4647. ELSIF numberActualParameters > to THEN
  4648. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4649. result := SyntaxTree.invalidExpression;
  4650. resultB := FALSE;
  4651. ELSE
  4652. resultB := TRUE;
  4653. END;
  4654. RETURN resultB
  4655. END CheckArity;
  4656. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4657. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4658. BEGIN
  4659. WHILE modifier # NIL DO
  4660. symbol := cellType.FindProperty(modifier.identifier);
  4661. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4662. propertyType := symbol.type.resolved;
  4663. modifierType := modifier.expression.type.resolved;
  4664. IF ~CompatibleTo(system, modifierType, propertyType) &
  4665. ~(
  4666. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4667. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4668. Error(modifier.position,Diagnostics.Invalid,"incompatible to cell property");
  4669. END;
  4670. ELSE
  4671. Error(modifier.position, Diagnostics.Invalid, "undefined property");
  4672. END;
  4673. modifier := modifier.nextModifier;
  4674. END;
  4675. END CheckModifiers;
  4676. BEGIN
  4677. type := NIL; result := NIL;
  4678. type0 := NIL; type1 := NIL; type2 := NIL;
  4679. numberActualParameters := actualParameters.Length();
  4680. IF numberActualParameters>0 THEN
  4681. parameter0 := actualParameters.GetExpression(0);
  4682. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4683. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4684. result := SyntaxTree.invalidExpression
  4685. END
  4686. END;
  4687. IF numberActualParameters >1 THEN
  4688. parameter1 := actualParameters.GetExpression(1);
  4689. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4690. ELSE
  4691. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4692. result := SyntaxTree.invalidExpression
  4693. END
  4694. END;
  4695. IF numberActualParameters >2 THEN
  4696. parameter2 := actualParameters.GetExpression(2);
  4697. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4698. ELSE
  4699. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4700. result := SyntaxTree.invalidExpression
  4701. END
  4702. END;
  4703. IF returnType # NIL THEN
  4704. id := Global.New;
  4705. result := NIL;
  4706. ELSE
  4707. id := builtin.id;
  4708. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4709. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4710. END;
  4711. END;
  4712. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4713. ELSIF result # NIL THEN type := result.type (* operator *)
  4714. ELSE
  4715. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4716. result(SyntaxTree.Designator).SetLeft(left);
  4717. IF returnType # NIL THEN
  4718. type := returnType;
  4719. END;
  4720. (* ---- ASSERT ----- *)
  4721. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4722. IF CheckBooleanType(parameter0) THEN
  4723. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4724. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4725. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4726. *)
  4727. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4728. Error(position,Diagnostics.Invalid,"assert failed");
  4729. END;
  4730. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4731. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4732. rules imposed by the architecture / current runtime
  4733. *)
  4734. END;
  4735. END;
  4736. (* ---- COPY ----- *)
  4737. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4738. IF~IsStringType(type0) THEN
  4739. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4740. END;
  4741. IF ~IsStringType(type1) THEN
  4742. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4743. ELSIF CheckVariable(parameter1) THEN
  4744. IF (type0 IS SyntaxTree.StringType) THEN
  4745. arrayType := type1(SyntaxTree.ArrayType);
  4746. IF arrayType.form = SyntaxTree.Static THEN
  4747. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4748. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4749. END;
  4750. END;
  4751. END;
  4752. END;
  4753. (* ---- INC, DEC----- *)
  4754. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4755. IF numberActualParameters = 1 THEN
  4756. parameter1 :=Global.NewIntegerValue(system,position,1);
  4757. actualParameters.AddExpression(parameter1);
  4758. END;
  4759. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4760. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4761. Error(position,Diagnostics.Invalid,"incompatible increment");
  4762. ELSE
  4763. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4764. actualParameters.SetExpression(1,parameter1);
  4765. END;
  4766. END;
  4767. (* ---- EXCL, INCL----- *)
  4768. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4769. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4770. IF IsIntegerValue(parameter1,i0) THEN
  4771. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4772. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4773. END;
  4774. END;
  4775. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4776. actualParameters.SetExpression(1,parameter1);
  4777. END;
  4778. (* ---- HALT, SYSTEM.HALT ----- *)
  4779. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4780. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4781. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4782. rules imposed by the architecture / current runtime
  4783. *)
  4784. END;
  4785. (* ---- WAIT ----- *)
  4786. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4787. IF CheckObjectType(parameter0) THEN
  4788. END;
  4789. (* ---- NEW ----- *)
  4790. ELSIF (id = Global.New) THEN
  4791. IF returnType # NIL THEN
  4792. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4793. ELSE
  4794. first := 1;
  4795. END;
  4796. IF CheckArity(first,Infinity) THEN
  4797. IF currentIsRealtime THEN
  4798. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4799. END;
  4800. (* check constructor *)
  4801. IF (first =0) OR CheckVariable(parameter0) THEN
  4802. IF type0 IS SyntaxTree.PointerType THEN
  4803. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4804. ELSIF type0 IS SyntaxTree.CellType THEN
  4805. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4806. ELSE
  4807. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4808. END;
  4809. IF type0 IS SyntaxTree.ArrayType THEN
  4810. arrayType := type0(SyntaxTree.ArrayType);
  4811. IF arrayType.form = SyntaxTree.Static THEN
  4812. i := first
  4813. ELSIF arrayType.form = SyntaxTree.Open THEN
  4814. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4815. ELSE HALT(100)
  4816. END;
  4817. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4818. i := first;
  4819. REPEAT
  4820. actualParameter := actualParameters.GetExpression(i);
  4821. IF CheckSizeType(actualParameter) THEN
  4822. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4823. actualParameters.SetExpression(i,actualParameter);
  4824. END;
  4825. INC(i);
  4826. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4827. END;
  4828. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4829. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4830. IF constructor = NIL THEN
  4831. IF CheckArity(first,first) THEN END;
  4832. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4833. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4834. ELSE
  4835. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4836. numberFormalParameters := procedureType.numberParameters;
  4837. IF numberActualParameters-first <= numberFormalParameters THEN
  4838. formalParameter := procedureType.firstParameter;
  4839. FOR i := first TO numberActualParameters-1 DO
  4840. actualParameter := actualParameters.GetExpression(i);
  4841. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4842. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4843. ELSE
  4844. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4845. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4846. END;
  4847. actualParameters.SetExpression(i,actualParameter);
  4848. END;
  4849. formalParameter := formalParameter.nextParameter;
  4850. END;
  4851. WHILE (formalParameter # NIL) DO
  4852. IF formalParameter.defaultValue # NIL THEN
  4853. actualParameters.AddExpression(formalParameter.defaultValue);
  4854. formalParameter := formalParameter.nextParameter
  4855. ELSE
  4856. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4857. formalParameter := NIL;
  4858. END;
  4859. END;
  4860. ELSE
  4861. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4862. END;
  4863. END;
  4864. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4865. mathArrayType := type0(SyntaxTree.MathArrayType);
  4866. IF mathArrayType.form = SyntaxTree.Static THEN
  4867. Error(position,Diagnostics.Invalid,"new on static array");
  4868. ELSE
  4869. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4870. i0 := first+1; i1 := Infinity;
  4871. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4872. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4873. i1 := i0;
  4874. ELSE HALT(100);
  4875. END;
  4876. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4877. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4878. base := ArrayBase(type0,MAX(LONGINT));
  4879. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4880. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4881. IF ~CompatibleTo(system,type0,parameterType) THEN
  4882. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4883. result := SyntaxTree.invalidExpression;
  4884. ELSE
  4885. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4886. END;
  4887. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4888. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4889. IF ~CompatibleTo(system,type1,parameterType) THEN
  4890. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4891. result := SyntaxTree.invalidExpression;
  4892. ELSE
  4893. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4894. END;
  4895. ELSE
  4896. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4897. i := first;
  4898. REPEAT
  4899. actualParameter := actualParameters.GetExpression(i);
  4900. IF CheckSizeType(actualParameter) THEN
  4901. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4902. actualParameters.SetExpression(i,actualParameter);
  4903. END;
  4904. INC(i);
  4905. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4906. END;
  4907. END;
  4908. END;
  4909. ELSIF type0 IS SyntaxTree.CellType THEN
  4910. IF ~(currentIsCellNet) THEN
  4911. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4912. ELSE
  4913. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4914. IF (constructor = NIL) & CheckArity(1,1) THEN
  4915. (* ok *)
  4916. ELSE
  4917. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4918. numberFormalParameters := procedureType.numberParameters;
  4919. DEC(numberActualParameters);
  4920. IF numberActualParameters <= numberFormalParameters THEN
  4921. formalParameter := procedureType.firstParameter;
  4922. FOR i := first TO numberActualParameters DO
  4923. actualParameter := actualParameters.GetExpression(i);
  4924. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4925. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4926. ELSE
  4927. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4928. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4929. END;
  4930. actualParameters.SetExpression(i,actualParameter);
  4931. END;
  4932. formalParameter := formalParameter.nextParameter;
  4933. END;
  4934. WHILE (formalParameter # NIL) DO
  4935. IF formalParameter.defaultValue # NIL THEN
  4936. actualParameters.AddExpression(formalParameter.defaultValue);
  4937. formalParameter := formalParameter.nextParameter
  4938. ELSE
  4939. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4940. formalParameter := NIL;
  4941. END;
  4942. END;
  4943. ELSE
  4944. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4945. END;
  4946. END;
  4947. END;
  4948. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4949. activeCellsStatement := TRUE;
  4950. ELSE
  4951. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4952. END;
  4953. END;
  4954. END;
  4955. (* ---- DISPOSE ----- *)
  4956. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4957. IF ~IsPointerType(parameter0.type) THEN
  4958. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4959. ELSIF ~IsDisposable(parameter0.type) THEN
  4960. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4961. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4962. END
  4963. (* ---- GETPROCEDURE ----- *)
  4964. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4965. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4966. IF CheckVariable(parameter2) THEN
  4967. IF ~GetProcedureAllowed(parameter2.type) THEN
  4968. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4969. END;
  4970. END;
  4971. END;
  4972. (* ---- ABS ----- *)
  4973. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4974. (* note: ABS on complex numbers is done using overloading *)
  4975. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4976. type := type0;
  4977. IF IsIntegerValue(parameter0,i0) THEN
  4978. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4979. type := Global.GetIntegerType(system,ABS(i0));
  4980. ELSIF IsRealValue(parameter0,r) THEN
  4981. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4982. END;
  4983. ELSE
  4984. type := SyntaxTree.invalidType;
  4985. END;
  4986. (* ---- ASH, ASR ----- *)
  4987. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4988. type := type0;
  4989. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4990. (*
  4991. ConvertOperands(parameter0,parameter1); (* same type *)
  4992. *)
  4993. type := parameter0.type;
  4994. IF IsIntegerValue(parameter0,i0) THEN
  4995. IF IsIntegerValue(parameter1,i1) THEN
  4996. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4997. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4998. result := ResolveExpression(result);
  4999. type := Global.GetIntegerType(system,i0);
  5000. END;
  5001. END;
  5002. IF type.resolved.sizeInBits < 32 THEN
  5003. type := system.longintType;
  5004. END;
  5005. (*!compatibility with release, remove when resolved
  5006. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5007. *)
  5008. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5009. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5010. actualParameters.SetExpression(0,parameter0);
  5011. actualParameters.SetExpression(1,parameter1);
  5012. END;
  5013. (* ---- CAP ----- *)
  5014. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5015. type := system.characterType;
  5016. IF CheckCharacterType (parameter0) THEN
  5017. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5018. actualParameters.SetExpression(0,parameter0);
  5019. IF IsCharacterValue(parameter0,c) THEN
  5020. IF (c <= "z") & (c >= "a") THEN
  5021. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5022. ELSE
  5023. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5024. END;
  5025. END;
  5026. END;
  5027. (* ---- CHR ----- *)
  5028. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5029. IF id = Global.Chr THEN
  5030. type := system.characterType
  5031. ELSE
  5032. type := system.characterType32
  5033. END;
  5034. IF CheckIntegerType(parameter0) THEN
  5035. IF IsIntegerValue(parameter0,i0) THEN
  5036. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5037. result := ResolveExpression(result);
  5038. ELSE
  5039. (*
  5040. result := NewConversion(parameter0.position,parameter0,type);
  5041. *)
  5042. END;
  5043. END
  5044. (* ---- ENTIER ----- *)
  5045. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5046. type := system.longintType;
  5047. IF CheckRealType(parameter0) THEN
  5048. IF IsRealValue(parameter0,r) THEN
  5049. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5050. type := Global.GetIntegerType(system,ENTIER(r));
  5051. END
  5052. END;
  5053. (* ---- ENTIERH ----- *)
  5054. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5055. type := system.hugeintType;
  5056. IF CheckRealType(parameter0) THEN
  5057. IF IsRealValue(parameter0,r) THEN
  5058. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5059. END
  5060. END;
  5061. (* ---- LEN ----- *)
  5062. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5063. type := system.longintType;
  5064. base := type0;
  5065. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5066. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5067. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5068. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5069. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5070. END;
  5071. type0 := base;
  5072. ELSE
  5073. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5074. type0 := parameter0.type.resolved;
  5075. actualParameters.SetExpression(0,parameter0);
  5076. base := type0;
  5077. END;
  5078. END;
  5079. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5080. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5081. IF i1 < 0 THEN
  5082. Error(position,Diagnostics.Invalid,"invalid dimension");
  5083. base := SyntaxTree.invalidType;
  5084. ELSE
  5085. base := ArrayBase(base,i1);
  5086. IF (base # NIL) & Indexable(base) THEN
  5087. ELSE
  5088. Error(position,Diagnostics.Invalid,"len on no array");
  5089. IF VerboseErrorMessage THEN
  5090. Printout.Info("base",base);
  5091. END;
  5092. base := SyntaxTree.invalidType;
  5093. END;
  5094. END;
  5095. IF numberActualParameters=2 THEN
  5096. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5097. actualParameters.SetExpression(1,parameter1);
  5098. ELSIF base IS SyntaxTree.MathArrayType THEN
  5099. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5100. END;
  5101. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5102. IF base IS SyntaxTree.ArrayType THEN
  5103. arrayType := base(SyntaxTree.ArrayType);
  5104. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5105. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5106. result := Global.NewIntegerValue(system,position,i);
  5107. type := result.type;(* arrayType.length.type;*)
  5108. ASSERT(type # NIL);
  5109. END;
  5110. ELSIF base IS SyntaxTree.MathArrayType THEN
  5111. mathArrayType := base(SyntaxTree.MathArrayType);
  5112. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5113. result := Global.NewIntegerValue(system,position,i);
  5114. type := result.type;
  5115. (*
  5116. type := mathArrayType.length.type;
  5117. *)
  5118. ASSERT(type # NIL);
  5119. END;
  5120. END;
  5121. END;
  5122. ELSE
  5123. type := system.longintType;
  5124. END;
  5125. (* ---- FIRST ---- *)
  5126. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5127. type := system.longintType;
  5128. IF CheckRangeType(parameter0) THEN END;
  5129. result.SetAssignable(parameter0.assignable)
  5130. (* ---- LAST ---- *)
  5131. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5132. type := system.longintType;
  5133. IF CheckRangeType(parameter0) THEN END;
  5134. result.SetAssignable(parameter0.assignable)
  5135. (* ---- STEP ---- *)
  5136. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5137. type := system.longintType;
  5138. IF CheckRangeType(parameter0) THEN END;
  5139. result.SetAssignable(parameter0.assignable)
  5140. (* ---- RE ---- *)
  5141. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5142. IF CheckNumberType(parameter0) THEN
  5143. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5144. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5145. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5146. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5147. type := parameter0.type
  5148. ELSE
  5149. type := system.realType
  5150. END
  5151. END;
  5152. result.SetAssignable(parameter0.assignable)
  5153. (* ---- IM ---- *)
  5154. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5155. IF CheckNumberType(parameter0) THEN
  5156. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5157. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5158. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5159. ELSE
  5160. type := system.realType;
  5161. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5162. END
  5163. END;
  5164. result.SetAssignable(parameter0.assignable)
  5165. (* ---- MAX ----- *)
  5166. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5167. IF numberActualParameters = 1 THEN
  5168. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5169. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5170. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5171. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5172. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5173. *)
  5174. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5175. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5176. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5177. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5178. ELSE Error(parameter0.position,Diagnostics.Invalid, "builtin function not applicable to this type");
  5179. END;
  5180. ELSE
  5181. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5182. END
  5183. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5184. ConvertOperands(parameter0,parameter1);
  5185. actualParameters.SetExpression(0,parameter0);
  5186. actualParameters.SetExpression(1,parameter1);
  5187. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5188. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5189. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5190. END;
  5191. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5192. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5193. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5194. END;
  5195. END;
  5196. type := parameter0.type;
  5197. ELSE type := SyntaxTree.invalidType;
  5198. END;
  5199. (* ---- MIN ----- *)
  5200. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5201. IF numberActualParameters = 1 THEN
  5202. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5203. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5204. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5205. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5206. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5207. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5208. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5209. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5210. END;
  5211. ELSE
  5212. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5213. END
  5214. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5215. ConvertOperands(parameter0,parameter1);
  5216. actualParameters.SetExpression(0,parameter0);
  5217. actualParameters.SetExpression(1,parameter1);
  5218. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5219. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5220. ELSE result.SetResolved(parameter1.resolved)
  5221. END;
  5222. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5223. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5224. ELSE result.SetResolved(parameter1.resolved)
  5225. END;
  5226. END;
  5227. type := parameter0.type;
  5228. ELSE type := SyntaxTree.invalidType;
  5229. END;
  5230. (* ---- ODD ----- *)
  5231. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5232. type := system.booleanType;
  5233. IF CheckIntegerType(parameter0) THEN
  5234. IF IsIntegerValue(parameter0,i0) THEN
  5235. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5236. type := system.booleanType;
  5237. END;
  5238. END;
  5239. (* ---- ORD ----- *)
  5240. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5241. IF id = Global.Ord THEN
  5242. type := system.integerType;
  5243. ELSE
  5244. type := system.longintType;
  5245. END;
  5246. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5247. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5248. actualParameters.SetExpression(0,parameter0);
  5249. (* IF CheckCharacterType(parameter0) THEN*)
  5250. IF IsCharacterValue(parameter0,c)THEN
  5251. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5252. type := Global.GetSignedIntegerType(system,ORD(c));
  5253. END;
  5254. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5255. END;
  5256. (* ---- SHORT ----- *)
  5257. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5258. type := type0;
  5259. IF IsSignedIntegerType(type) THEN
  5260. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5261. ELSIF type = system.integerType THEN type := system.shortintType
  5262. ELSIF type = system.longintType THEN type := system.integerType
  5263. ELSIF type = system.hugeintType THEN type:= system.longintType
  5264. ELSE
  5265. CASE type.sizeInBits OF
  5266. 16: type := Global.Integer8
  5267. |32: type := Global.Integer16
  5268. |64: type := Global.Integer32
  5269. END;
  5270. END;
  5271. ELSIF type IS SyntaxTree.FloatType THEN
  5272. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5273. ELSIF type = system.longrealType THEN type := system.realType
  5274. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5275. END;
  5276. ELSIF type IS SyntaxTree.ComplexType THEN
  5277. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5278. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5279. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5280. END;
  5281. ELSE
  5282. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5283. END;
  5284. IF (parameter0.resolved # NIL) THEN
  5285. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5286. IF parameter0 IS SyntaxTree.Value THEN
  5287. result.SetResolved(parameter0(SyntaxTree.Value));
  5288. END;
  5289. END;
  5290. (* ---- LONG ----- *)
  5291. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5292. type := type0;
  5293. IF IsSignedIntegerType(type) THEN
  5294. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5295. ELSIF type = system.longintType THEN type := system.hugeintType
  5296. ELSIF type = system.integerType THEN type := system.longintType
  5297. ELSIF type = system.shortintType THEN type := system.integerType
  5298. ELSE
  5299. CASE type.sizeInBits OF
  5300. 8: type := Global.Integer16
  5301. |16: type := Global.Integer32
  5302. |32: type := Global.Integer64
  5303. END;
  5304. END;
  5305. ELSIF type IS SyntaxTree.FloatType THEN
  5306. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5307. ELSIF type= system.realType THEN type := system.longrealType
  5308. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5309. END;
  5310. ELSIF type IS SyntaxTree.ComplexType THEN
  5311. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5312. ELSIF type = system.complexType THEN type := system.longcomplexType
  5313. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5314. END;
  5315. ELSE
  5316. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5317. END;
  5318. IF (parameter0.resolved # NIL) THEN
  5319. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5320. IF parameter0 IS SyntaxTree.Value THEN
  5321. result.SetResolved(parameter0(SyntaxTree.Value));
  5322. END;
  5323. END;
  5324. (* ---- SIZE OF ----- *)
  5325. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5326. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5327. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5328. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5329. type := system.integerType;
  5330. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5331. ELSE
  5332. (* for variables, system sizeof could represent the physically occupied size
  5333. determined via the type descriptor, implement that ? *)
  5334. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5335. END
  5336. (* ---- SYSTEM.TRACE -----*)
  5337. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5338. FOR i := 0 TO numberActualParameters-1 DO
  5339. parameter0 := actualParameters.GetExpression(i);
  5340. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5341. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5342. END;
  5343. END;
  5344. (* remaining issues can only be tested in backend *)
  5345. (* ---- ADDRESSOF----- *)
  5346. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5347. IF HasAddress(parameter0) THEN
  5348. type := system.addressType;
  5349. ELSE
  5350. type := SyntaxTree.invalidType;
  5351. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5352. END;
  5353. (* ---- BIT ----- *)
  5354. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5355. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5356. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5357. actualParameters.SetExpression(0,parameter0);
  5358. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5359. actualParameters.SetExpression(1,parameter1);
  5360. END;
  5361. type := system.booleanType;
  5362. (* ----- MSK ---- *)
  5363. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5364. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5365. ConvertOperands(parameter0,parameter1);
  5366. actualParameters.SetExpression(0,parameter0);
  5367. actualParameters.SetExpression(1,parameter1);
  5368. END;
  5369. type := parameter0.type;
  5370. (* ---- SYSTEM.GET64 ----- *)
  5371. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5372. IF CheckAddressType(parameter0) THEN
  5373. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5374. actualParameters.SetExpression(0,parameter0);
  5375. END;
  5376. type := system.hugeintType;
  5377. (* ---- SYSTEM.GET32 ----- *)
  5378. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5379. IF CheckAddressType(parameter0) THEN
  5380. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5381. actualParameters.SetExpression(0,parameter0);
  5382. END;
  5383. type := system.longintType;
  5384. (* ---- SYSTEM.GET16 ----- *)
  5385. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5386. IF CheckAddressType(parameter0) THEN
  5387. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5388. actualParameters.SetExpression(0,parameter0);
  5389. END;
  5390. type := system.integerType;
  5391. (* ---- SYSTEM.GET8 ----- *)
  5392. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5393. IF CheckAddressType(parameter0) THEN
  5394. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5395. actualParameters.SetExpression(0,parameter0);
  5396. END;
  5397. type := system.shortintType;
  5398. (* ---- SYSTEM.GetStackPointer ----- *)
  5399. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5400. type := system.addressType;
  5401. (* ---- SYSTEM.GetFramePointer ----- *)
  5402. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5403. type := system.addressType;
  5404. (* ---- SYSTEM.GetActivity ----- *)
  5405. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5406. type := system.objectType;
  5407. (* ---- SYSTEM.SetStackPointer ----- *)
  5408. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5409. IF CheckAddressType(parameter0) THEN
  5410. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5411. actualParameters.SetExpression(0,parameter0);
  5412. END;
  5413. (* ---- SYSTEM.SetFramePointer ----- *)
  5414. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5415. IF CheckAddressType(parameter0) THEN
  5416. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5417. actualParameters.SetExpression(0,parameter0);
  5418. END;
  5419. (* ---- SYSTEM.SetActivity ----- *)
  5420. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5421. IF CheckObjectType(parameter0) THEN
  5422. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5423. actualParameters.SetExpression(0,parameter0);
  5424. END;
  5425. (* ---- LSH, LSL, ROT, ROR ----- *)
  5426. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5427. type := type0;
  5428. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5429. actualParameters.SetExpression(1, parameter1);
  5430. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5431. IF id = Global.Lsh THEN
  5432. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5433. ELSIF id = Global.Rot THEN
  5434. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5435. ELSIF id = Global.Ror THEN
  5436. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5437. END;
  5438. END;
  5439. (* ---- SYSTEM.VAL ----- *)
  5440. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5441. IF CheckTypeDeclarationType(parameter0) THEN
  5442. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5443. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5444. result := SyntaxTree.invalidExpression;
  5445. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5446. ELSE
  5447. IF (parameter1.resolved # NIL) THEN
  5448. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5449. IF parameter0 IS SyntaxTree.Value THEN
  5450. result.SetResolved(parameter0(SyntaxTree.Value));
  5451. END;
  5452. END;
  5453. result.SetAssignable(parameter1.assignable);
  5454. END;
  5455. END;
  5456. (* ---- SYSTEM.GET ----- *)
  5457. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5458. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5459. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5460. actualParameters.SetExpression(0,parameter0);
  5461. END;
  5462. (* ---- SYSTEM.PUT ----- *)
  5463. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5464. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5465. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5466. actualParameters.SetExpression(0,parameter0);
  5467. END;
  5468. (* ---- SYSTEM.PUT64 ----- *)
  5469. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5470. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5471. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5472. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5473. actualParameters.SetExpression(0,parameter0);
  5474. actualParameters.SetExpression(1,parameter1);
  5475. END;
  5476. (* ---- SYSTEM.PUT32 ----- *)
  5477. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5478. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5479. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5480. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5481. actualParameters.SetExpression(0,parameter0);
  5482. actualParameters.SetExpression(1,parameter1);
  5483. END;
  5484. (* ---- SYSTEM.PUT16 ----- *)
  5485. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5486. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5487. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5488. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5489. actualParameters.SetExpression(0,parameter0);
  5490. actualParameters.SetExpression(1,parameter1);
  5491. END;
  5492. (* ---- SYSTEM.PUT8 ----- *)
  5493. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5494. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5495. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5496. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5497. actualParameters.SetExpression(0,parameter0);
  5498. actualParameters.SetExpression(1,parameter1);
  5499. END;
  5500. (* ---- SYSTEM.MOVE ----- *)
  5501. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5502. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5503. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5504. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5505. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5506. actualParameters.SetExpression(0,parameter0);
  5507. actualParameters.SetExpression(1,parameter1);
  5508. actualParameters.SetExpression(2,parameter2);
  5509. END;
  5510. (* ---- SYSTEM.NEW ----- *)
  5511. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5512. IF ~IsPointerType(parameter0.type) THEN
  5513. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5514. ELSIF CheckSizeType(parameter1) THEN
  5515. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5516. actualParameters.SetExpression(1,parameter1);
  5517. END;
  5518. (* ----SYSTEM.REF ---- *)
  5519. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5520. type := system.addressType
  5521. (* ---- INCR ----- *)
  5522. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5523. type := system.sizeType;
  5524. base := type0;
  5525. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5526. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5527. IF i1 < 0 THEN
  5528. Error(position,Diagnostics.Invalid,"invalid dimension");
  5529. base := SyntaxTree.invalidType;
  5530. ELSE
  5531. base := ArrayBase(base,i1);
  5532. IF (base # NIL) & Indexable(base) THEN
  5533. ELSE
  5534. Error(position,Diagnostics.Invalid,"len on no array");
  5535. IF VerboseErrorMessage THEN
  5536. Printout.Info("base",base);
  5537. END;
  5538. base := SyntaxTree.invalidType;
  5539. END;
  5540. END;
  5541. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5542. actualParameters.SetExpression(1,parameter1);
  5543. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5544. mathArrayType := base(SyntaxTree.MathArrayType);
  5545. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5546. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5547. type := system.longintType;
  5548. END;
  5549. END;
  5550. ELSE
  5551. type := system.longintType;
  5552. END;
  5553. (* ---- SUM ----- *)
  5554. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5555. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5556. (* ---- ALL ----- *)
  5557. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5558. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5559. (* ---- DIM ----- *)
  5560. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5561. type := system.sizeType;
  5562. IF type0 IS SyntaxTree.MathArrayType THEN
  5563. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5564. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5565. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5566. END;
  5567. ELSE
  5568. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5569. END;
  5570. (* ---- CAS ----- *)
  5571. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5572. IF type0.IsComposite () THEN
  5573. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5574. result := SyntaxTree.invalidExpression;
  5575. ELSIF ~IsVariable (parameter0) THEN
  5576. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5577. result := SyntaxTree.invalidExpression;
  5578. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5579. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5580. result := SyntaxTree.invalidExpression;
  5581. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5582. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5583. result := SyntaxTree.invalidExpression;
  5584. ELSE
  5585. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5586. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5587. type := type0;
  5588. END;
  5589. (* ---- RESHAPE ----- *)
  5590. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5591. IF type0 IS SyntaxTree.MathArrayType THEN
  5592. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5593. base := ArrayBase(type0,MAX(LONGINT));
  5594. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5595. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5596. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5597. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5598. IF ~CompatibleTo(system,type0,parameterType) THEN
  5599. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5600. result := SyntaxTree.invalidExpression;
  5601. ELSE
  5602. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5603. END;
  5604. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5605. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5606. IF ~CompatibleTo(system,type1,parameterType) THEN
  5607. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5608. result := SyntaxTree.invalidExpression;
  5609. ELSE
  5610. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5611. END;
  5612. ELSE
  5613. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5614. result := SyntaxTree.invalidExpression;
  5615. END;
  5616. (* ---- SYSTEM.TYPECODE ----- *)
  5617. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5618. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5619. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5620. type := type.resolved;
  5621. IF type IS SyntaxTree.PointerType THEN
  5622. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5623. END;
  5624. IF ~(type IS SyntaxTree.RecordType) THEN
  5625. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5626. END;
  5627. ELSE
  5628. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5629. END;
  5630. type := system.addressType;
  5631. (* -------- FLT --------- *)
  5632. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5633. type := system.realType;
  5634. IF IsRealValue(parameter0, r) THEN
  5635. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5636. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5637. i0 := i; i := ABS(i);
  5638. IF i # 0 THEN
  5639. i1 := 23;
  5640. IF i >= 2*800000H THEN
  5641. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5642. ELSIF i < 800000H THEN
  5643. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5644. END;
  5645. i := (i1 + 127)*800000H - 800000H + i;
  5646. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5647. END;
  5648. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5649. END;
  5650. (* ------- CONNECT -------*)
  5651. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5652. (*IF ~(currentIsCellNet) THEN
  5653. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5654. END;*)
  5655. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5656. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5657. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5658. END;
  5659. IF numberActualParameters = 3 THEN
  5660. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5661. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5662. END;
  5663. *)
  5664. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5665. actualParameters.SetExpression(2,parameter2);
  5666. END;
  5667. activeCellsStatement := TRUE;
  5668. (* ---------- DELEGATE --------*)
  5669. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5670. (*
  5671. IF ~(currentIsCellNet) THEN
  5672. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5673. END;
  5674. *)
  5675. IF ~CheckPortType(parameter1, inPort) THEN
  5676. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5677. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5678. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5679. ELSIF (outPort.direction # inPort.direction) THEN
  5680. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5681. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5682. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5683. END;
  5684. activeCellsStatement := TRUE;
  5685. (* --------- RECEIVE ---------*)
  5686. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5687. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5688. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5689. IF inPort.direction # SyntaxTree.InPort THEN
  5690. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5691. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5692. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5693. END;
  5694. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5695. IF ~SameType(parameter2.type, system.integerType) THEN
  5696. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5697. END;
  5698. END;
  5699. END;
  5700. (* --------- SEND ---------*)
  5701. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5702. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5703. IF CheckPortType(parameter0,outPort) THEN
  5704. IF outPort.direction # SyntaxTree.OutPort THEN
  5705. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5706. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5707. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5708. ELSE
  5709. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5710. actualParameters.SetExpression(1,parameter1);
  5711. END;
  5712. END;
  5713. (* ------- custom builtins ----- *)
  5714. ELSIF id = Global.systemSpecial THEN
  5715. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5716. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5717. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5718. type := procedureType.returnType;
  5719. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5720. (* go through all formal parameters *)
  5721. formalParameter := procedureType.firstParameter;
  5722. FOR i := 0 TO actualParameters.Length() - 1 DO
  5723. actualParameter := actualParameters.GetExpression(i);
  5724. IF actualParameter = SyntaxTree.invalidExpression THEN
  5725. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5726. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5727. ELSE
  5728. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5729. END;
  5730. actualParameters.SetExpression(i, actualParameter);
  5731. formalParameter := formalParameter.nextParameter
  5732. END
  5733. END
  5734. ELSE
  5735. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5736. result := SyntaxTree.invalidExpression;
  5737. END;
  5738. END;
  5739. IF result # SyntaxTree.invalidExpression THEN
  5740. type := ResolveType(type);
  5741. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5742. result.SetType(type);
  5743. END;
  5744. RETURN result
  5745. END NewBuiltinCallDesignator;
  5746. (** return type guard designator left(type)
  5747. - check if type can be extended (i.e. is no static record)
  5748. - check if type is a type extension of left.type
  5749. - returns new type guard designator
  5750. returns invalidDesignator = invalidExpression if error
  5751. **)
  5752. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5753. VAR result: SyntaxTree.Designator;
  5754. BEGIN
  5755. result := SyntaxTree.invalidDesignator;
  5756. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5757. Error(position,Diagnostics.Invalid,"no type extension of type");
  5758. IF VerboseErrorMessage THEN
  5759. Printout.Info("left",left);
  5760. Printout.Info("type",type);
  5761. END;
  5762. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5763. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5764. ELSIF IsUnsafePointer(left.type) THEN
  5765. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5766. ELSE
  5767. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5768. result.SetType(type);
  5769. result.SetAssignable(left.assignable);
  5770. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5771. END;
  5772. RETURN result
  5773. END NewTypeGuardDesignator;
  5774. (** check and resolve parameter designator left(expression list)
  5775. - check expression list
  5776. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5777. - elsif left is a procedure type then
  5778. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5779. - else return is a procedure call then return ProcedureCallDesignator
  5780. returns invalidDesignator = invalidExpression if error
  5781. **)
  5782. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5783. VAR
  5784. parameters: SyntaxTree.ExpressionList;
  5785. left: SyntaxTree.Designator;
  5786. result,expression: SyntaxTree.Expression;
  5787. typeDeclaration: SyntaxTree.TypeDeclaration;
  5788. type, expressionType: SyntaxTree.Type;
  5789. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5790. BEGIN
  5791. type := type.resolved;
  5792. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5793. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5794. END;
  5795. RETURN type
  5796. END BaseType;
  5797. BEGIN
  5798. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5799. result := SyntaxTree.invalidDesignator;
  5800. left := ResolveDesignator(designator.left);
  5801. IF left # SyntaxTree.invalidDesignator THEN
  5802. parameters := designator.parameters;
  5803. IF ExpressionList(parameters) THEN
  5804. IF (left.type = NIL) THEN
  5805. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5806. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5807. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5808. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5809. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5810. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5811. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5812. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5813. ELSE
  5814. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5815. END
  5816. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5817. expression := parameters.GetExpression(0);
  5818. type := typeDeclaration.declaredType.resolved;
  5819. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5820. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5821. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5822. OR (expressionType IS SyntaxTree.EnumerationType)
  5823. ) THEN
  5824. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5825. ELSE
  5826. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5827. END;
  5828. ELSE
  5829. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5830. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5831. result := SyntaxTree.invalidDesignator;
  5832. END;
  5833. ELSE
  5834. result := SyntaxTree.invalidDesignator
  5835. END;
  5836. END;
  5837. resolvedExpression := result;
  5838. END VisitParameterDesignator;
  5839. (** check dereference designator left^
  5840. - check if left is pointer type or left is object type
  5841. - return new dereference designator with type = left.baseType.type (if appropriate)
  5842. with error handling
  5843. returns invalidDesignator = invalidExpression if error
  5844. **)
  5845. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5846. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5847. BEGIN
  5848. result := SyntaxTree.invalidDesignator;
  5849. type := left.type;
  5850. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5851. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5852. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5853. result.SetAssignable(TRUE);
  5854. result.SetType(type);
  5855. result.SetHidden(left.isHidden);
  5856. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5857. type := type.resolved;
  5858. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5859. result.SetAssignable(TRUE);
  5860. result.SetType(type);
  5861. result.SetHidden(left.isHidden);
  5862. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5863. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5864. result.SetAssignable(TRUE);
  5865. result.SetType(type);
  5866. result.SetHidden(left.isHidden);
  5867. ELSE
  5868. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5869. IF VerboseErrorMessage THEN
  5870. Printout.Info("pointer", type);
  5871. Printout.Info("scope", currentScope);
  5872. END;
  5873. END;
  5874. RETURN result
  5875. END NewDereferenceDesignator;
  5876. (** check supercall designator left^
  5877. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5878. - return new supercall designator with type = left.type
  5879. with error handling
  5880. **)
  5881. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5882. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5883. objectScope: SyntaxTree.Scope;
  5884. BEGIN
  5885. result := SyntaxTree.invalidDesignator;
  5886. IF left = SyntaxTree.invalidDesignator THEN
  5887. (* error already handled *)
  5888. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5889. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5890. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5891. IF symbol IS SyntaxTree.Procedure THEN
  5892. procedure := symbol(SyntaxTree.Procedure);
  5893. objectScope := currentScope;
  5894. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5895. objectScope := objectScope.outerScope;
  5896. END;
  5897. IF (left.left = NIL) OR ~
  5898. (
  5899. (left.left IS SyntaxTree.SelfDesignator) OR
  5900. (left.left IS SyntaxTree.DereferenceDesignator)
  5901. & (left.left(SyntaxTree.Designator).left # NIL)
  5902. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5903. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5904. IF VerboseErrorMessage THEN
  5905. Printout.Info("left.left",left.left);
  5906. END;
  5907. ELSIF procedure.super # NIL THEN
  5908. result := SyntaxTree.NewSupercallDesignator(position,left);
  5909. result.SetType(left.type.resolved)
  5910. ELSE
  5911. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5912. END;
  5913. ELSE
  5914. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5915. END;
  5916. ELSE
  5917. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5918. END;
  5919. RETURN result
  5920. END NewSupercallDesignator;
  5921. (** check and semantically resolve arrow designator left^
  5922. - if left is procedure type -> result := SupercallDesignator
  5923. - else result := DereferenceDesignator
  5924. returns result via global variable resolvedExpression
  5925. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5926. **)
  5927. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5928. VAR left: SyntaxTree.Designator;
  5929. BEGIN
  5930. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5931. left := ResolveDesignator(arrowDesignator.left);
  5932. IF left # NIL THEN
  5933. IF (left.type = NIL) THEN
  5934. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5935. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5936. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5937. ELSE
  5938. IF IsPointerToObject(left.type) THEN
  5939. Warning(arrowDesignator.position, "forbidden dereference on object");
  5940. END;
  5941. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5942. END
  5943. END
  5944. END VisitArrowDesignator;
  5945. (** check and return expression
  5946. - if expression has no type then resolve expression
  5947. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5948. - return result
  5949. **)
  5950. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5951. VAR result,prev: SyntaxTree.Expression;
  5952. BEGIN
  5953. IF expression = NIL THEN result := NIL
  5954. ELSIF (expression.type = NIL) THEN
  5955. prev := resolvedExpression;
  5956. resolvedExpression := SyntaxTree.invalidExpression;
  5957. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5958. expression.SetType(SyntaxTree.invalidType);
  5959. END;
  5960. expression.Accept(SELF);
  5961. result := resolvedExpression;
  5962. IF currentIsRealtime THEN
  5963. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5964. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5965. END;
  5966. END;
  5967. (* designator modifiers for backends if they support it ...*)
  5968. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5969. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5970. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5971. END;
  5972. resolvedExpression := prev
  5973. ELSE
  5974. result := expression
  5975. END;
  5976. RETURN result
  5977. END ResolveExpression;
  5978. (**
  5979. check expression to be constant expression
  5980. - resolve expression
  5981. - if valid then check that of value type
  5982. report error and return invalidExpression if anything fails
  5983. **)
  5984. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5985. VAR position: Position;
  5986. BEGIN
  5987. position := expression.position;
  5988. expression := ResolveExpression(expression);
  5989. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5990. ELSIF (expression.resolved = NIL) THEN
  5991. Error(position,Diagnostics.Invalid,"expression is not constant");
  5992. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5993. expression := SyntaxTree.invalidExpression;
  5994. END;
  5995. RETURN expression
  5996. END ConstantExpression;
  5997. (** check expression to be constant integer
  5998. - resolve expresssion
  5999. - if valid then check that of integer value type
  6000. report error and return invalidExpression if anything fails
  6001. **)
  6002. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6003. VAR position: Position;
  6004. BEGIN
  6005. position := expression.position;
  6006. expression := ResolveExpression(expression);
  6007. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6008. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6009. expression := SyntaxTree.invalidExpression;
  6010. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  6011. END;
  6012. RETURN expression
  6013. END ConstantInteger;
  6014. (** check expression as positive (>=0) constant integer
  6015. - resolve expression
  6016. - if valid then check that integer value
  6017. - if integer value then check that value >= 0
  6018. report error and return invalidExpression if anything fails
  6019. **)
  6020. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6021. VAR position: Position;
  6022. BEGIN
  6023. position := expression.position;
  6024. expression := ConstantExpression(expression);
  6025. IF expression = SyntaxTree.invalidExpression THEN
  6026. (* error already reported *)
  6027. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6028. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  6029. expression := SyntaxTree.invalidExpression
  6030. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6031. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6032. END;
  6033. RETURN expression
  6034. END ConstantIntegerGeq0;
  6035. (** check expression as condition
  6036. - resolve expression
  6037. - if valid expression then check that result type is boolean
  6038. report error and return invalidExpression if anything fails
  6039. **)
  6040. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6041. VAR position: Position;
  6042. BEGIN
  6043. position := expression.position;
  6044. expression := ResolveExpression(expression);
  6045. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6046. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6047. expression := SyntaxTree.invalidExpression;
  6048. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6049. END;
  6050. RETURN expression
  6051. END ResolveCondition;
  6052. (*** symbols ***)
  6053. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6054. BEGIN
  6055. x.Accept(SELF);
  6056. END ResolveSymbol;
  6057. (** check a symbol
  6058. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6059. **)
  6060. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6061. VAR scope: SyntaxTree.Scope;
  6062. BEGIN
  6063. (* visibility *)
  6064. scope := symbol.scope;
  6065. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6066. scope := scope.outerScope;
  6067. END;
  6068. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6069. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6070. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6071. IF VerboseErrorMessage THEN
  6072. Printout.Info("symbol",symbol);
  6073. END;
  6074. END;
  6075. END;
  6076. END CheckSymbolVisibility;
  6077. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6078. If node is currently being resolved then emit a cyclic definition error.
  6079. Return TRUE only if node is fully resolved.
  6080. **)
  6081. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6082. VAR result: BOOLEAN;
  6083. BEGIN
  6084. IF SyntaxTree.Resolved IN x.state THEN
  6085. result := FALSE
  6086. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6087. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6088. result := FALSE;
  6089. ELSE
  6090. result := TRUE;
  6091. x.SetState(SyntaxTree.BeingResolved)
  6092. END;
  6093. RETURN result
  6094. END SymbolNeedsResolution;
  6095. (** check and resolve a type declaration symbol = Type
  6096. - set type to declaration type
  6097. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6098. This is so because the type declaration itself does not have a type but it only stands for a type.
  6099. In the implementation of the compiler this made a lot much easier.
  6100. - resolve and set declared type
  6101. - check symbol
  6102. **)
  6103. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6104. VAR prevScope: SyntaxTree.Scope;
  6105. BEGIN
  6106. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6107. IF SymbolNeedsResolution(typeDeclaration) THEN
  6108. prevScope := currentScope;
  6109. currentScope := typeDeclaration.scope;
  6110. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6111. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6112. CheckSymbolVisibility(typeDeclaration);
  6113. typeDeclaration.SetState(SyntaxTree.Resolved);
  6114. currentScope := prevScope;
  6115. END;
  6116. END VisitTypeDeclaration;
  6117. (** check and resolve a constant declaration symbol = (constant) expression
  6118. - check expression
  6119. - set type and value
  6120. - check symbol
  6121. **)
  6122. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6123. VAR
  6124. expression: SyntaxTree.Expression;
  6125. type: SyntaxTree.Type;
  6126. name: Basic.SegmentedName;
  6127. replacement: Replacement;
  6128. BEGIN
  6129. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6130. IF SymbolNeedsResolution(constant) THEN
  6131. expression := constant.value;
  6132. IF replacements # NIL THEN
  6133. Global.GetSymbolSegmentedName(constant, name);
  6134. replacement := replacements;
  6135. WHILE (replacement # NIL) & (replacement.name # name) DO
  6136. replacement := replacement.next;
  6137. END;
  6138. IF replacement # NIL THEN
  6139. InfoSS(constant.position, "replacing constant", constant.name);
  6140. (*
  6141. NEW(stringReader, Strings.Length(replacement.string^));
  6142. stringReader.Set(replacement.string^);
  6143. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6144. NEW(parser, scanner, diagnostics);
  6145. expression := parser.Expression();
  6146. *)
  6147. expression := replacement.expression;
  6148. replacement.used := TRUE;
  6149. END;
  6150. END;
  6151. constant.SetType(SyntaxTree.invalidType);
  6152. expression := ConstantExpression(expression);
  6153. ASSERT(expression.type # NIL);
  6154. type := expression.type.resolved;
  6155. constant.SetType(type);
  6156. constant.SetValue(expression);
  6157. CheckSymbolVisibility(constant);
  6158. constant.SetState(SyntaxTree.Resolved);
  6159. END;
  6160. END VisitConstant;
  6161. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6162. VAR procedureAlignment: LONGINT;
  6163. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6164. (* least common multiple *)
  6165. VAR a,b: LONGINT;
  6166. BEGIN
  6167. a := a0; b := b0;
  6168. WHILE (a # b) DO
  6169. IF a < b THEN a := a+a0
  6170. ELSE b := b + b0
  6171. END;
  6172. END;
  6173. RETURN a
  6174. END LCM;
  6175. BEGIN
  6176. IF alignment > 1 THEN
  6177. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6178. IF (procedureAlignment > 1) THEN
  6179. alignment := LCM(alignment, procedureAlignment);
  6180. END;
  6181. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6182. END;
  6183. END AdaptStackAlignment;
  6184. (** check and resolve a variable / field
  6185. - check and set type
  6186. - negative check on open array type
  6187. - check symbol
  6188. **)
  6189. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6190. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6191. BEGIN
  6192. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6193. IF SymbolNeedsResolution(variable) THEN
  6194. modifiers := variable.modifiers;
  6195. (*
  6196. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6197. variable.AddFlags(flags);
  6198. *)
  6199. variable.SetType(ResolveType(variable.type));
  6200. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6201. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6202. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6203. END;
  6204. END;
  6205. CheckSymbolVisibility(variable);
  6206. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6207. variable.SetUntraced(TRUE);
  6208. IF ~ContainsPointer(variable.type) THEN
  6209. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6210. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6211. END;
  6212. END;
  6213. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6214. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6215. IF ~PowerOf2(value) THEN
  6216. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6217. ELSE
  6218. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6219. END;
  6220. END;
  6221. variable.SetAlignment(FALSE,value);
  6222. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6223. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6224. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6225. END;
  6226. variable.SetAlignment(TRUE, value);
  6227. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6228. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6229. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6230. END;
  6231. variable.SetFictive(value);
  6232. variable.SetOffset(value*system.dataUnit);
  6233. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6234. END;
  6235. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6236. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6237. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6238. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6239. END;
  6240. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6241. IF variable.initializer # NIL THEN
  6242. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6243. END;
  6244. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6245. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6246. pointerType.SetPointerBase(variable.type);
  6247. pointerType.SetHidden(TRUE);
  6248. variable.SetType(ResolveType(pointerType));
  6249. END;
  6250. variable.SetState(SyntaxTree.Resolved);
  6251. END;
  6252. END VisitVariable;
  6253. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6254. BEGIN
  6255. VisitVariable(property)
  6256. END VisitProperty;
  6257. (** check and resolve a (procedure) parameter
  6258. - check and set type
  6259. - check symbol
  6260. - check parameter kind and set read-only flags if appropriate
  6261. **)
  6262. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6263. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6264. BEGIN
  6265. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6266. IF SymbolNeedsResolution(parameter) THEN
  6267. modifiers := parameter.modifiers;
  6268. parameter.SetType(ResolveType(parameter.type));
  6269. ASSERT(parameter.type.resolved # NIL);
  6270. CheckSymbolVisibility(parameter);
  6271. IF parameter.defaultValue # NIL THEN
  6272. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6273. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6274. ELSE
  6275. expression := ConstantExpression(parameter.defaultValue);
  6276. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6277. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6278. parameter.SetDefaultValue(expression);
  6279. END;
  6280. END;
  6281. END;
  6282. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6283. parameter.SetUntraced(TRUE);
  6284. IF ~ContainsPointer(parameter.type) THEN
  6285. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6286. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6287. END;
  6288. END;
  6289. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6290. parameter.SetMoveable(TRUE);
  6291. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6292. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6293. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6294. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6295. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6296. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6297. END;
  6298. END;
  6299. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6300. parameter.SetState(SyntaxTree.Resolved);
  6301. END;
  6302. END VisitParameter;
  6303. (** check and resolve a procedure (with declaration and implementation scope)
  6304. - check the procedure type
  6305. - check if method (i.e. in record scope), if so then
  6306. - check if (unique) constructor
  6307. - check if (unique) finalizer
  6308. - check if super method available, if so then check signature
  6309. - of not in record scope then negative check on constructor flag
  6310. - of not in record scope then negative check on finalizer flag
  6311. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6312. - check procedure symbol
  6313. **)
  6314. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6315. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6316. procedureType: SyntaxTree.ProcedureType;
  6317. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6318. qualifiedType: SyntaxTree.QualifiedType;
  6319. value: LONGINT;
  6320. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6321. position: Position;
  6322. BEGIN
  6323. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6324. IF IsOberonInline(procedure) THEN
  6325. IF SyntaxTree.Public * procedure.access # {} THEN
  6326. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6327. END;
  6328. procedure.SetInline(FALSE);
  6329. procedure.SetOberonInline(TRUE);
  6330. END;
  6331. IF SymbolNeedsResolution(procedure) THEN
  6332. recentIsRealtime := currentIsRealtime;
  6333. recentIsBodyProcedure := currentIsBodyProcedure;
  6334. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6335. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6336. modifiers := procedureType.modifiers;
  6337. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6338. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6339. IF useDarwinCCalls THEN (*fld*)
  6340. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6341. ELSE
  6342. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6343. END
  6344. END;
  6345. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6346. procedureType.SetInterrupt(TRUE);
  6347. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6348. END;
  6349. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6350. procedureType.SetNoReturn(TRUE);
  6351. END;
  6352. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6353. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6354. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6355. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6356. END;
  6357. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6358. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6359. END;
  6360. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6361. IF ~PowerOf2(value) THEN
  6362. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6363. ELSE
  6364. procedureType.SetStackAlignment(value)
  6365. END;
  6366. END;
  6367. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6368. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6369. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6370. END;
  6371. CheckModifiers(modifiers, TRUE);
  6372. modifiers := procedureType.returnTypeModifiers;
  6373. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6374. CheckModifiers(modifiers, TRUE);
  6375. FixProcedureType(procedureType);
  6376. currentIsRealtime := procedureType.isRealtime;
  6377. currentIsBodyProcedure := procedure.isBodyProcedure;
  6378. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6379. THEN
  6380. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6381. END;
  6382. CheckSymbolVisibility(procedure);
  6383. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6384. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6385. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6386. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6387. END;
  6388. END;
  6389. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6390. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6391. procedureType.SetDelegate(TRUE);
  6392. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6393. IF record.pointerType.typeDeclaration = NIL THEN
  6394. selfParameter.SetType(record.pointerType);
  6395. ELSE
  6396. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6397. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6398. qualifiedType.SetResolved(record.pointerType);
  6399. selfParameter.SetType(qualifiedType);
  6400. END;
  6401. selfParameter.SetAccess(SyntaxTree.Hidden);
  6402. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6403. IF procedure.isConstructor THEN
  6404. (*! constructor is always visible, compatibility to paco
  6405. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6406. *)
  6407. procedure.MarkUsed;
  6408. IF procedureType.returnType # NIL THEN
  6409. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6410. END;
  6411. proc := procedure.scope.firstProcedure;
  6412. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6413. proc := proc.nextProcedure;
  6414. END;
  6415. IF proc # NIL THEN
  6416. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6417. ELSE
  6418. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6419. END;
  6420. END;
  6421. IF procedure.isFinalizer THEN
  6422. procedure.MarkUsed;
  6423. IF procedureType.returnType # NIL THEN
  6424. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6425. END;
  6426. IF procedureType.numberParameters # 0 THEN
  6427. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6428. END;
  6429. proc := procedure.scope.firstProcedure;
  6430. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6431. proc := proc.nextProcedure;
  6432. END;
  6433. IF proc # NIL THEN
  6434. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6435. ELSE
  6436. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6437. END;
  6438. END;
  6439. super := FindSuperProcedure(record.recordScope, procedure);
  6440. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6441. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6442. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6443. END;
  6444. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6445. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6446. END;
  6447. IF super.isFinal THEN
  6448. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6449. END;
  6450. procedure.SetSuper(super);
  6451. super.SetOverwritten(TRUE);
  6452. procedure.SetAccess(procedure.access+super.access);
  6453. procedure.MarkUsed;
  6454. END;
  6455. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6456. THEN
  6457. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6458. END;
  6459. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6460. IF cellsAreObjects THEN
  6461. procedureType.SetDelegate(TRUE);
  6462. END;
  6463. IF procedure.isConstructor THEN
  6464. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6465. END;
  6466. ELSIF procedure.isConstructor THEN
  6467. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6468. END;
  6469. Declarations(procedure.procedureScope, FALSE);
  6470. (* body resolution part done as late fix of the procedure type *)
  6471. procedure.SetState(SyntaxTree.Resolved);
  6472. currentIsRealtime := recentIsRealtime;
  6473. currentIsBodyProcedure := recentIsBodyProcedure;
  6474. END;
  6475. END VisitProcedure;
  6476. (**
  6477. a builtin procedure is a global item that may not be modified locally
  6478. instead the resolving of builtin procedure calls are done in the esignator
  6479. **)
  6480. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6481. VAR type: SyntaxTree.Type;
  6482. BEGIN
  6483. type := ResolveType(builtinProcedure.type);
  6484. END VisitBuiltin;
  6485. (* nopov *)
  6486. (** check and resolve operator
  6487. - operators are first checked as procedures
  6488. - then additional operator-specific checks are done
  6489. - note that only module-scope operators are checked here
  6490. (operators in a record scope are only allowed in the context of
  6491. array-structured object types and checked in 'ResolveArrayStructure')
  6492. - also note that inter-operator conformity is not checked here
  6493. **)
  6494. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6495. VAR
  6496. procedureType: SyntaxTree.ProcedureType;
  6497. leftType, rightType: SyntaxTree.Type;
  6498. identifierNumber: LONGINT; position: Position;
  6499. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6500. modifiers: SyntaxTree.Modifier;
  6501. (** whether a type is locally defined in the current module scope
  6502. for arrays, the base type must be locally defined **)
  6503. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6504. BEGIN
  6505. IF type = NIL THEN
  6506. RETURN FALSE
  6507. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6508. RETURN TRUE
  6509. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6510. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6511. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6512. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6513. ELSE
  6514. RETURN FALSE
  6515. END
  6516. END IsLocallyDefined;
  6517. BEGIN
  6518. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6519. procedureType := operator.type(SyntaxTree.ProcedureType);
  6520. modifiers := procedureType.modifiers;
  6521. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6522. CheckModifiers(modifiers, TRUE);
  6523. VisitProcedure(operator);
  6524. IF operator.scope IS SyntaxTree.RecordScope THEN
  6525. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6526. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6527. IF identifierNumber = -1 THEN
  6528. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6529. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6530. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6531. ELSE
  6532. IF procedureType.numberParameters < 1 THEN
  6533. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6534. ELSIF procedureType.numberParameters > 2 THEN
  6535. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6536. ELSE
  6537. (* determine operand types *)
  6538. leftType := procedureType.firstParameter.type;
  6539. IF procedureType.numberParameters > 1 THEN
  6540. rightType := procedureType.firstParameter.nextParameter.type
  6541. ELSE
  6542. rightType := NIL
  6543. END;
  6544. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6545. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6546. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6547. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6548. END
  6549. END;
  6550. (* TODO: refine the checks, think about how restrictive the checks should be
  6551. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6552. They might be used for intersection, union, complement of custom object types *)
  6553. (* defaults *)
  6554. hasReturnType := TRUE;
  6555. mustBeUnary := FALSE;
  6556. mustBeBinary := FALSE;
  6557. mustReturnBoolean := FALSE;
  6558. mustReturnInteger := FALSE;
  6559. mustHaveEquitypedOperands := FALSE;
  6560. (* operator-specific exceptions *)
  6561. CASE identifierNumber OF
  6562. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6563. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6564. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6565. mustBeBinary := TRUE
  6566. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6567. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6568. | Scanner.Times: mustBeBinary := TRUE
  6569. | Scanner.TimesTimes: mustBeBinary := TRUE
  6570. | Scanner.DotTimes: mustBeBinary := TRUE
  6571. | Scanner.PlusTimes: mustBeBinary := TRUE
  6572. | Scanner.Slash: mustBeBinary := TRUE
  6573. | Scanner.Backslash: mustBeBinary := TRUE
  6574. | Scanner.DotSlash: mustBeBinary := TRUE
  6575. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6576. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6577. | Scanner.Not: mustBeUnary := TRUE
  6578. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6579. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6580. | Scanner.Transpose: mustBeUnary := TRUE;
  6581. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6582. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6583. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6584. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6585. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6586. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6587. | Global.Abs: mustBeUnary := TRUE;
  6588. | Global.Ash: (* TODO: arity? *)
  6589. | Global.Cap: (* TODO: arity? *)
  6590. | Global.Chr: mustBeUnary := TRUE;
  6591. | Global.Entier: (* TODO: arity? *)
  6592. | Global.EntierH: (* TODO: arity? *)
  6593. | Global.Len: (* unary and binary *)
  6594. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6595. | Global.Max, Global.Min: (* unary and binary *)
  6596. | Global.Odd: (* TODO: arity? *)
  6597. | Global.Sum: (* TODO: arity? *)
  6598. | Global.All: (* TODO: arity? *)
  6599. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6600. | Scanner.Alias:
  6601. | Scanner.GreaterGreater, Scanner.LessLess:
  6602. mustBeBinary := TRUE; hasReturnType := FALSE;
  6603. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6604. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6605. END;
  6606. (* check parameter count *)
  6607. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6608. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6609. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6610. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6611. END;
  6612. (* check parameter types *)
  6613. (* TODO: is this used at all? *)
  6614. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6615. leftType := procedureType.firstParameter.type;
  6616. rightType := procedureType.firstParameter.nextParameter.type;
  6617. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6618. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6619. END
  6620. END;
  6621. (* check return type *)
  6622. IF hasReturnType THEN
  6623. IF procedureType.returnType = NIL THEN
  6624. Error(operator.position, Diagnostics.Invalid, "return type required")
  6625. ELSIF mustReturnBoolean THEN
  6626. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6627. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6628. END
  6629. ELSIF mustReturnInteger THEN
  6630. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6631. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6632. END
  6633. END
  6634. ELSIF procedureType.returnType # NIL THEN
  6635. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6636. END
  6637. END
  6638. END
  6639. END
  6640. END VisitOperator;
  6641. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6642. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6643. BEGIN
  6644. IF error THEN RETURN FALSE END;
  6645. prevScope := currentScope;
  6646. prevDiagnostics := diagnostics;
  6647. diagnostics := NIL; (* suppress error output *)
  6648. currentScope := module.moduleScope;
  6649. VisitImport(x);
  6650. IF ~error THEN
  6651. module.moduleScope.AddImport(x);
  6652. x.SetScope(module.moduleScope);
  6653. END;
  6654. currentScope := prevScope;
  6655. diagnostics := prevDiagnostics;
  6656. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6657. END AddImport;
  6658. (** check and resolve import
  6659. - check for name = SYSTEM
  6660. - check for forbidden self import
  6661. - search through global import cache: already imported?
  6662. - check if already imported indirectly
  6663. - import if necessary -> set module and enter into import cache
  6664. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6665. - after this import this direct import and all indirect imports are stored in the current module's import list
  6666. **)
  6667. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6668. VAR
  6669. module: SyntaxTree.Module;
  6670. moduleScope: SyntaxTree.ModuleScope;
  6671. import,reimport: SyntaxTree.Import;
  6672. filename: FileName;
  6673. prevScope: SyntaxTree.Scope;
  6674. BEGIN
  6675. IF SymbolNeedsResolution(x) THEN
  6676. prevScope := currentScope;
  6677. x.SetType(SyntaxTree.importType);
  6678. moduleScope := currentScope.ownerModule.moduleScope;
  6679. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6680. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6681. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6682. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6683. ELSE
  6684. (* search through global import list: already imported ? *)
  6685. IF (x.module = NIL) & (importCache # NIL) THEN
  6686. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6687. ELSE import := NIL
  6688. END;
  6689. IF x.module # NIL THEN (* already imported indirectly *)
  6690. module := x.module;
  6691. ELSIF import # NIL THEN (* already in module list *)
  6692. module := import.module;
  6693. ASSERT(module # NIL);
  6694. x.SetModule(module);
  6695. ELSE (* must be imported *)
  6696. Global.ModuleFileName(x.moduleName,x.context,filename);
  6697. IF symbolFileFormat # NIL THEN
  6698. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6699. IF module = NIL THEN
  6700. ErrorSS(x.position,"could not import",filename);
  6701. IF VerboseErrorMessage THEN
  6702. Printout.Info("import",x)
  6703. END
  6704. ELSE
  6705. (*
  6706. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6707. (*! should rather be done by importer *)
  6708. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6709. checker.importCache := importCache;
  6710. checker.arrayBaseImported := arrayBaseImported;
  6711. checker.global := global;
  6712. checker.Module(module); (* semantic check *)
  6713. error := error OR checker.error;
  6714. END;
  6715. *)
  6716. (*
  6717. ASSERT(SyntaxTree.Resolved IN module.state);
  6718. *)
  6719. x.SetModule(module);
  6720. IF importCache # NIL THEN
  6721. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6722. import.SetContext(x.context);
  6723. import.SetModule(module);
  6724. importCache.AddImport(import);
  6725. END;
  6726. END;
  6727. ELSE
  6728. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6729. END;
  6730. END;
  6731. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6732. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6733. END;
  6734. import := module.moduleScope.firstImport;
  6735. WHILE(import # NIL) DO
  6736. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6737. ASSERT(currentScope # NIL);
  6738. ASSERT(currentScope.ownerModule # NIL);
  6739. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6740. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6741. Error(x.position,Diagnostics.Invalid,"recursive import");
  6742. ELSE
  6743. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6744. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6745. IF reimport = NIL THEN (* indirect import *)
  6746. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6747. reimport.SetContext(import.context);
  6748. reimport.SetModule(import.module);
  6749. moduleScope.AddImport(reimport);
  6750. reimport.SetScope(moduleScope);
  6751. ELSE
  6752. ASSERT(import.module # NIL);
  6753. reimport.SetModule(import.module); (* direct or indirect import *)
  6754. END;
  6755. END;
  6756. import := import.nextImport;
  6757. END;
  6758. END;
  6759. END;
  6760. currentScope := prevScope;
  6761. (* ELSE nothing to be done *)
  6762. x.SetState(SyntaxTree.Resolved);
  6763. END;
  6764. END VisitImport;
  6765. (*** statements ***)
  6766. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6767. VAR prev,resolved: SyntaxTree.Statement;
  6768. BEGIN
  6769. prev := resolvedStatement;
  6770. resolvedStatement := x;
  6771. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6772. activeCellsStatement := FALSE;
  6773. x.Accept(SELF);
  6774. (* removed this, implementation restriction should be resolved by backend
  6775. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6776. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6777. END;
  6778. *)
  6779. resolved := resolvedStatement;
  6780. resolvedStatement := prev;
  6781. RETURN resolved
  6782. END ResolveStatement;
  6783. (** check and resolve statement sequence
  6784. - check all statements, replace if necessary
  6785. **)
  6786. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6787. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6788. BEGIN
  6789. IF statementSequence # NIL THEN (* else empty *)
  6790. FOR i := 0 TO statementSequence.Length()-1 DO
  6791. statement := statementSequence.GetStatement(i);
  6792. resolved := ResolveStatement(statement);
  6793. IF (resolved # statement) THEN
  6794. statementSequence.SetStatement(i,resolved);
  6795. END;
  6796. END;
  6797. END;
  6798. END StatementSequence;
  6799. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6800. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6801. - check if procedure is callable
  6802. - check return type = NIL (otherwise must be assignment statement)
  6803. **)
  6804. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6805. VAR call: SyntaxTree.Designator;
  6806. BEGIN
  6807. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6808. call := procedureCall.call;
  6809. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6810. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6811. END;
  6812. call := ResolveDesignator(call);
  6813. IF call = SyntaxTree.invalidDesignator THEN
  6814. (* error already handled *)
  6815. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6816. (* inline call in a statement *)
  6817. ELSIF ~IsCallable(call) THEN
  6818. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6819. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6820. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6821. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6822. END;
  6823. procedureCall.SetCall(call);
  6824. (*
  6825. IF call = SyntaxTree.invalidDesignator THEN
  6826. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6827. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6828. IF IsOberonInline(procedure) THEN
  6829. Warning(procedure.position,"call to inline proc");
  6830. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6831. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6832. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6833. resolvedStatement := block;
  6834. RETURN;
  6835. END;
  6836. END;
  6837. *)
  6838. END VisitProcedureCallStatement;
  6839. (** check and resolve assignment LHS := RHS
  6840. - resolve LHS and RHS
  6841. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6842. - check if assignment is compatible
  6843. - check if LHS is variable (i.e. assignable)
  6844. - convert RHS if necessary
  6845. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6846. - assignment between different ASOTs
  6847. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6848. - assignment to ASOT elements:
  6849. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6850. **)
  6851. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6852. VAR
  6853. left: SyntaxTree.Designator;
  6854. right, expression: SyntaxTree.Expression;
  6855. designator: SyntaxTree.Designator;
  6856. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6857. mathArrayType: SyntaxTree.MathArrayType;
  6858. BEGIN
  6859. right := ResolveExpression(assignment.right);
  6860. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6861. left := ResolveDesignator(assignment.left);
  6862. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6863. (* error already handled *)
  6864. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6865. (* LHS is index write operator call on ASOT *)
  6866. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6867. (* necessary ?
  6868. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6869. type := procedureType.firstParameter.type;
  6870. expression := procedureCallDesignator.parameters.GetExpression(0);
  6871. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6872. *)
  6873. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6874. ELSIF CheckVariable(left) THEN
  6875. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6876. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6877. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6878. (* conversion done by procedure call
  6879. (* try to convert to left argument *)
  6880. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6881. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6882. procedureCallDesignator.parameters.SetExpression(1, right);
  6883. END;
  6884. *)
  6885. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6886. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6887. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6888. ELSIF AssignmentCompatible(left, right) THEN
  6889. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6890. mathArrayType := MathArrayStructureOfType(left.type);
  6891. right := NewConversion(right.position, right, mathArrayType, NIL);
  6892. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6893. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6894. ELSE
  6895. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6896. assignment.SetLeft(left);
  6897. assignment.SetRight(right);
  6898. resolvedStatement := assignment
  6899. END
  6900. END
  6901. END
  6902. END VisitAssignment;
  6903. (** check and resolve assignment LHS := RHS
  6904. - resolve LHS and RHS
  6905. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6906. - check if assignment is compatible
  6907. - check if LHS is variable (i.e. assignable)
  6908. - convert RHS if necessary
  6909. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6910. - assignment between different ASOTs
  6911. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6912. - assignment to ASOT elements:
  6913. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6914. **)
  6915. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6916. VAR
  6917. left: SyntaxTree.Designator;
  6918. right: SyntaxTree.Expression;
  6919. inPort, outPort: SyntaxTree.PortType;
  6920. expression: SyntaxTree.Expression;
  6921. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6922. BEGIN
  6923. right := ResolveExpression(communication.right);
  6924. left := ResolveDesignator(communication.left);
  6925. communication.SetLeft(left);
  6926. communication.SetRight(right);
  6927. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6928. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6929. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6930. (* conversion done by procedure call
  6931. (* try to convert to left argument *)
  6932. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6933. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6934. procedureCallDesignator.parameters.SetExpression(1, right);
  6935. END;
  6936. *)
  6937. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6938. ELSE
  6939. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6940. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6941. (* error already handled *)
  6942. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6943. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6944. IF outPort.direction # SyntaxTree.OutPort THEN
  6945. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6946. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6947. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6948. ELSE
  6949. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6950. communication.SetRight(right)
  6951. END;
  6952. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6953. IF CheckVariable(left) THEN
  6954. IF inPort.direction # SyntaxTree.InPort THEN
  6955. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6956. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6957. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6958. END;
  6959. END;
  6960. ELSE
  6961. Error(communication.position, -1, "unsupported stream operation");
  6962. END;
  6963. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6964. IF outPort.direction # SyntaxTree.OutPort THEN
  6965. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6966. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6967. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6968. ELSE
  6969. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6970. communication.SetRight(right)
  6971. END;
  6972. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6973. IF CheckVariable(right) THEN
  6974. IF inPort.direction # SyntaxTree.InPort THEN
  6975. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6976. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6977. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6978. END;
  6979. END;
  6980. ELSE
  6981. Error(communication.position, -1, "unsupported operation");
  6982. END;
  6983. END;
  6984. END VisitCommunicationStatement;
  6985. (** check and resolve if/eslif part
  6986. - check condition
  6987. - check statement sequence
  6988. **)
  6989. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6990. VAR prevUnreachable, b: BOOLEAN;
  6991. BEGIN
  6992. prevUnreachable := currentIsUnreachable;
  6993. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6994. IF IsBooleanValue(ifPart.condition,b) THEN
  6995. IF b=FALSE THEN
  6996. currentIsUnreachable := TRUE
  6997. ELSIF b=TRUE THEN
  6998. true := TRUE
  6999. END;
  7000. END;
  7001. StatementSequence(ifPart.statements);
  7002. currentIsUnreachable := prevUnreachable;
  7003. END IfPart;
  7004. (** check and resolve if statement
  7005. - check if parts and else part statement sequence
  7006. **)
  7007. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7008. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7009. BEGIN
  7010. prevUnreachable := currentIsUnreachable;
  7011. ifPartTrue := FALSE;
  7012. IfPart(ifStatement.ifPart,ifPartTrue);
  7013. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7014. elsif := ifStatement.GetElsifPart(i);
  7015. IfPart(elsif,ifPartTrue);
  7016. END;
  7017. IF ifStatement.elsePart # NIL THEN
  7018. IF ifPartTrue THEN
  7019. currentIsUnreachable := TRUE
  7020. END;
  7021. StatementSequence(ifStatement.elsePart)
  7022. END;
  7023. currentIsUnreachable := prevUnreachable;
  7024. END VisitIfStatement;
  7025. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7026. VAR variable: SyntaxTree.Designator;
  7027. type,variableType: SyntaxTree.Type;
  7028. withEntry: WithEntry;
  7029. BEGIN
  7030. variable := ResolveDesignator(withPart.variable);
  7031. variableType := variable.type.resolved;
  7032. withPart.SetVariable(variable);
  7033. type := ResolveType(withPart.type);
  7034. withPart.SetType(type);
  7035. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7036. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7037. END;
  7038. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7039. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7040. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7041. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7042. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7043. IF VerboseErrorMessage THEN
  7044. Printout.Info("variable",variable)
  7045. END;
  7046. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7047. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7048. IF VerboseErrorMessage THEN
  7049. Printout.Info("variable",variable);
  7050. Printout.Info("type",type);
  7051. END;
  7052. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7053. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7054. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7055. IF VerboseErrorMessage THEN
  7056. Printout.Info("variable",variable);
  7057. Printout.Info("type",type);
  7058. END;
  7059. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7060. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7061. ELSE
  7062. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7063. NEW(withEntry);
  7064. withEntry.previous := withEntries;
  7065. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7066. withEntry.type := type;
  7067. withEntries := withEntry;
  7068. StatementSequence(withPart.statements);
  7069. withEntries := withEntries.previous;
  7070. END;
  7071. END WithPart;
  7072. (** check and resolve with statement WITH variable: type DO ... END;
  7073. - check type and variable
  7074. - check that variable type is type extension of type
  7075. - check that variable is a variable
  7076. - enter new with scope and enter guardedVariable with same name and reference to variable
  7077. - create if statement:
  7078. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7079. **)
  7080. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7081. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7082. BEGIN
  7083. prevScope := currentScope; symbol := NIL;
  7084. FOR i := 0 TO withStatement.WithParts()-1 DO
  7085. WithPart(withStatement.GetWithPart(i),symbol);
  7086. END;
  7087. IF withStatement.elsePart # NIL THEN
  7088. StatementSequence(withStatement.elsePart)
  7089. END;
  7090. currentScope := prevScope;
  7091. END VisitWithStatement;
  7092. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7093. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7094. - check 'first' < 'last' and no overlaps between different case labels
  7095. - check statement sequence
  7096. **)
  7097. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7098. VAR
  7099. i: LONGINT;
  7100. position: Position;
  7101. expression, left, right: SyntaxTree.Expression;
  7102. expressionType: SyntaxTree.Type;
  7103. l, r: LONGINT;
  7104. cl, cr: CHAR;
  7105. thiscases: SyntaxTree.CaseConstant;
  7106. BEGIN
  7107. thiscases := NIL;
  7108. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7109. expression := casePart.elements.GetExpression(i);
  7110. position := expression.position;
  7111. (* set context of range *)
  7112. IF expression IS SyntaxTree.RangeExpression THEN
  7113. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7114. END;
  7115. expression := ResolveExpression(expression);
  7116. IF expression = SyntaxTree.invalidExpression THEN
  7117. (* error already reported *)
  7118. expressionType := SyntaxTree.invalidType;
  7119. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7120. (* read out 'first' and 'last' *)
  7121. left := expression(SyntaxTree.RangeExpression).first;
  7122. right := expression(SyntaxTree.RangeExpression).last;
  7123. (* guaranteed by VisitRangeExpression: *)
  7124. ASSERT((left # NIL) & (right # NIL));
  7125. ASSERT(left.type.resolved = right.type.resolved);
  7126. left := CompatibleConversion(left.position, left, type);
  7127. right := CompatibleConversion(right.position, right, type);
  7128. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7129. expression(SyntaxTree.RangeExpression).SetLast(right);
  7130. expressionType := RegularType(position,left.type);
  7131. ELSE
  7132. expression := ConstantExpression(expression);
  7133. expression := CompatibleConversion(expression.position, expression, type);
  7134. (*
  7135. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7136. left := Global.NewCharacterValue(system,expression.position,cl);
  7137. expression := casePart.elements.GetExpression(i);
  7138. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7139. expression := left
  7140. END;
  7141. *)
  7142. casePart.elements.SetExpression(i,expression);
  7143. left := expression; right := expression;
  7144. expressionType := RegularType(position,expression.type)
  7145. END;
  7146. IF (expressionType = SyntaxTree.invalidType) THEN
  7147. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7148. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7149. expression := SyntaxTree.invalidExpression;
  7150. ELSE
  7151. l := 0; r := 0;
  7152. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7153. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7154. l := ORD(cl); r := ORD(cr);
  7155. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7156. ELSE
  7157. expression := SyntaxTree.invalidExpression
  7158. END;
  7159. IF expression # SyntaxTree.invalidExpression THEN
  7160. IF l>r THEN
  7161. Error(position,Diagnostics.Invalid,"empty case label")
  7162. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7163. Error(position,Diagnostics.Invalid,"duplicate case label");
  7164. ELSE
  7165. IF l < min THEN min := l END;
  7166. IF r > max THEN max := r END;
  7167. END;
  7168. END;
  7169. END;
  7170. casePart.elements.SetExpression(i,expression);
  7171. END;
  7172. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7173. casePart.SetConstants(thiscases);
  7174. StatementSequence(casePart.statements);
  7175. END CasePart;
  7176. (** check and resolve case statement CASE variable OF ... END;
  7177. - check variable
  7178. - check case parts
  7179. **)
  7180. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7181. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7182. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7183. BEGIN
  7184. expression := ResolveExpression(caseStatement.variable);
  7185. type := RegularType(expression.position,expression.type);
  7186. IF type = SyntaxTree.invalidType THEN
  7187. expression := SyntaxTree.invalidExpression;
  7188. ELSIF IsIntegerType(type) THEN
  7189. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7190. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7191. (*
  7192. expression := Global.NewCharacterValue(system,expression.position,ch);
  7193. *)
  7194. type := expression.type;
  7195. ELSIF IsCharacterType(type) THEN
  7196. ELSIF IsEnumerationType(type) THEN
  7197. ELSE
  7198. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7199. expression := SyntaxTree.invalidExpression;
  7200. END;
  7201. caseStatement.SetVariable(expression);
  7202. caseList := NIL;
  7203. min := MAX(LONGINT); max := MIN(LONGINT);
  7204. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7205. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7206. END;
  7207. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7208. msg := "huge sparse case table ";
  7209. Strings.AppendInt(msg, max-min);
  7210. Strings.Append(msg,"/");
  7211. Strings.AppendInt(msg, caseStatement.CaseParts());
  7212. Warning(caseStatement.position,msg);
  7213. END;
  7214. caseStatement.SetMinMax(min,max);
  7215. StatementSequence(caseStatement.elsePart);
  7216. IF expression.resolved # NIL THEN
  7217. IF IsCharacterValue(expression,ch) THEN
  7218. l := ORD(ch)
  7219. ELSIF IsIntegerValue(expression,l) THEN
  7220. END;
  7221. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7222. END;
  7223. END VisitCaseStatement;
  7224. (** check and resolve while statement
  7225. - check condition
  7226. - check statement sequence
  7227. **)
  7228. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7229. VAR prevIsUnreachable,b: BOOLEAN;
  7230. BEGIN
  7231. prevIsUnreachable := currentIsUnreachable;
  7232. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7233. IF IsBooleanValue(whileStatement.condition,b) THEN
  7234. IF b=FALSE THEN
  7235. currentIsUnreachable := TRUE
  7236. END;
  7237. END;
  7238. StatementSequence(whileStatement.statements);
  7239. currentIsUnreachable := prevIsUnreachable
  7240. END VisitWhileStatement;
  7241. (** check and resolve repeat statement
  7242. - check condition
  7243. - check statement sequence
  7244. **)
  7245. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7246. BEGIN
  7247. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7248. StatementSequence(repeatStatement.statements);
  7249. END VisitRepeatStatement;
  7250. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7251. VAR withEntry: WithEntry;
  7252. BEGIN
  7253. withEntry := withEntries;
  7254. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7255. withEntry := withEntry.previous
  7256. END;
  7257. IF withEntry = NIL THEN RETURN FALSE
  7258. ELSE
  7259. type := withEntry.type;
  7260. RETURN TRUE
  7261. END;
  7262. END GetGuard;
  7263. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7264. - check that variable is an integer variable
  7265. - check that from is integer typed with compatible type
  7266. - check that to has compatible type
  7267. - check that by is constant integer with compatible type
  7268. **)
  7269. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7270. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7271. BEGIN
  7272. designator := ResolveDesignator(forStatement.variable);
  7273. type := SyntaxTree.invalidType;
  7274. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7275. designator := SyntaxTree.invalidDesignator;
  7276. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7277. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7278. designator := SyntaxTree.invalidDesignator;
  7279. ELSIF CheckVariable(designator) THEN
  7280. type := designator.type;
  7281. END;
  7282. forStatement.SetVariable(designator);
  7283. expression := ResolveExpression(forStatement.from);
  7284. IF expression = SyntaxTree.invalidExpression THEN
  7285. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7286. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7287. expression := SyntaxTree.invalidExpression;
  7288. ELSIF type # SyntaxTree.invalidType THEN
  7289. expression := NewConversion(expression.position,expression,type,NIL)
  7290. END;
  7291. forStatement.SetFrom(expression);
  7292. expression := ResolveExpression(forStatement.to);
  7293. IF expression = SyntaxTree.invalidExpression THEN
  7294. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7295. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7296. expression := SyntaxTree.invalidExpression;
  7297. ELSIF type # SyntaxTree.invalidType THEN
  7298. expression := NewConversion(expression.position,expression,type,NIL)
  7299. END;
  7300. forStatement.SetTo(expression);
  7301. IF forStatement.by # NIL THEN
  7302. expression := ConstantInteger(forStatement.by);
  7303. ELSE
  7304. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7305. END;
  7306. IF expression = SyntaxTree.invalidExpression THEN
  7307. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7308. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7309. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7310. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7311. ELSIF type # SyntaxTree.invalidType THEN
  7312. expression := NewConversion(expression.position,expression,type,NIL)
  7313. END;
  7314. forStatement.SetBy(expression);
  7315. StatementSequence(forStatement.statements);
  7316. END VisitForStatement;
  7317. (** check and resolve loop statement LOOP StatementSequence END
  7318. - check statement sequence
  7319. **)
  7320. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7321. BEGIN
  7322. StatementSequence(loopStatement.statements)
  7323. END VisitLoopStatement;
  7324. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7325. BEGIN
  7326. StatementSequence(exitableBlock.statements);
  7327. END VisitExitableBlock;
  7328. (** check and resolve exit statement EXIT
  7329. - check that exit is within LOOP statement block
  7330. **)
  7331. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7332. VAR outer: SyntaxTree.Statement;
  7333. BEGIN
  7334. outer := exitStatement.outer;
  7335. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7336. outer := outer.outer;
  7337. END;
  7338. IF outer = NIL THEN
  7339. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7340. END;
  7341. END VisitExitStatement;
  7342. (** check and resolve return statement RETURN [expression]
  7343. - check expression (if any)
  7344. - check if in procedure scope
  7345. - if in procedure scope then check expression compatibility
  7346. - if not in procecdure scope then check on return without expression
  7347. **)
  7348. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7349. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7350. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7351. BEGIN
  7352. position := returnStatement.position;
  7353. expression := returnStatement.returnValue;
  7354. IF expression # NIL THEN
  7355. expression := ResolveExpression(expression);
  7356. returnStatement.SetReturnValue(expression);
  7357. END;
  7358. outer := returnStatement.outer;
  7359. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7360. outer := outer.outer
  7361. END;
  7362. IF (outer # NIL) THEN
  7363. scope := outer(SyntaxTree.Body).inScope;
  7364. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7365. IF (expression # NIL) THEN
  7366. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7367. END;
  7368. ELSE
  7369. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7370. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7371. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7372. END;
  7373. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7374. IF returnType # NIL THEN
  7375. returnType := returnType.resolved;
  7376. IF expression = NIL THEN
  7377. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7378. ELSIF expression.type = NIL THEN
  7379. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7380. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7381. Error(position,Diagnostics.Invalid,"return type not compatible");
  7382. IF VerboseErrorMessage THEN
  7383. Printout.Info("returnType",returnType);
  7384. Printout.Info("expression",expression);
  7385. END;
  7386. ELSE
  7387. expression := NewConversion(expression.position,expression,returnType,NIL);
  7388. returnStatement.SetReturnValue(expression);
  7389. END;
  7390. ELSIF expression # NIL THEN
  7391. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7392. END;
  7393. END;
  7394. END;
  7395. END VisitReturnStatement;
  7396. (** check and resolve await statement AWAIT(condition: Expression)
  7397. - check await condition
  7398. **)
  7399. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7400. VAR condition: SyntaxTree.Expression;
  7401. BEGIN
  7402. condition := ResolveCondition(awaitStatement.condition);
  7403. IF currentIsRealtime THEN
  7404. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7405. END;
  7406. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7407. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7408. END;
  7409. awaitStatement.SetCondition(condition);
  7410. END VisitAwaitStatement;
  7411. PROCEDURE CheckSystemImport(position: Position);
  7412. VAR import: SyntaxTree.Import;
  7413. BEGIN
  7414. import := currentScope.ownerModule.moduleScope.firstImport;
  7415. WHILE(import # NIL) DO
  7416. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7417. RETURN;
  7418. END;
  7419. import := import.nextImport;
  7420. END;
  7421. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7422. END CheckSystemImport;
  7423. (** check and resolve code statement: do nothing, must be done by assembler
  7424. **)
  7425. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7426. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7427. BEGIN
  7428. CheckSystemImport(code.position);
  7429. FOR i := 0 TO code.inRules.Length()-1 DO
  7430. statement := code.inRules.GetStatement(i);
  7431. IF statement IS SyntaxTree.Assignment THEN
  7432. WITH statement: SyntaxTree.Assignment DO
  7433. statement.SetRight(ResolveExpression(statement.right));
  7434. END;
  7435. ELSE
  7436. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7437. END;
  7438. END;
  7439. FOR i := 0 TO code.outRules.Length()-1 DO
  7440. statement := code.outRules.GetStatement(i);
  7441. IF statement IS SyntaxTree.Assignment THEN
  7442. WITH statement: SyntaxTree.Assignment DO
  7443. statement.SetLeft(ResolveDesignator(statement.left));
  7444. END;
  7445. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7446. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7447. ELSE
  7448. Printout.Info("out statement ", statement);
  7449. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7450. END;
  7451. END;
  7452. END VisitCode;
  7453. (** check and set flags of a statement block
  7454. - check for multiply occurence of a flag
  7455. - check and set priority only in bodies
  7456. - check for valid names
  7457. **)
  7458. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7459. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7460. flag: LONGINT; recordBody: SyntaxTree.Body;
  7461. PROCEDURE SetProtectedRecord;
  7462. VAR scope: SyntaxTree.Scope;
  7463. BEGIN
  7464. scope := currentScope;
  7465. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7466. scope := scope.outerScope
  7467. END;
  7468. IF scope # NIL THEN
  7469. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7470. END;
  7471. END SetProtectedRecord;
  7472. BEGIN
  7473. flags := {};
  7474. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7475. recordBody := block(SyntaxTree.Body)
  7476. ELSE
  7477. recordBody := NIL
  7478. END;
  7479. blockModifier := block.blockModifiers;
  7480. WHILE(blockModifier # NIL) DO
  7481. name := blockModifier.identifier;
  7482. expression := blockModifier.expression;
  7483. position := blockModifier.position;
  7484. flag := -1;
  7485. IF name=Global.NamePriority THEN
  7486. IF expression = NIL THEN
  7487. Error(position,Diagnostics.Invalid,"missing priority expression");
  7488. ELSIF recordBody = NIL THEN
  7489. Error(position,Diagnostics.Invalid,"priority not on record body");
  7490. ELSIF recordBody.priority # NIL THEN
  7491. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7492. ELSE
  7493. recordBody.SetPriority(expression);
  7494. END;
  7495. ELSIF expression # NIL THEN
  7496. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7497. ELSIF name=Global.NameExclusive THEN
  7498. IF block.isExclusive THEN
  7499. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7500. END;
  7501. block.SetExclusive(TRUE); SetProtectedRecord;
  7502. ELSIF name=Global.NameActive THEN
  7503. IF recordBody = NIL THEN
  7504. Error(position,Diagnostics.Invalid,"active not in record body");
  7505. ELSIF recordBody.isActive THEN
  7506. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7507. ELSE
  7508. recordBody.SetActive(TRUE); SetProtectedRecord;
  7509. END;
  7510. ELSIF name=Global.NameSafe THEN
  7511. IF recordBody = NIL THEN
  7512. Error(position,Diagnostics.Invalid,"safe not in record body");
  7513. ELSIF recordBody.isSafe THEN
  7514. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7515. ELSE
  7516. recordBody.SetSafe(TRUE);
  7517. SetProtectedRecord;
  7518. END;
  7519. ELSIF name=Global.NameRealtime THEN
  7520. IF recordBody = NIL THEN
  7521. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7522. ELSIF recordBody.isRealtime THEN
  7523. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7524. ELSE
  7525. recordBody.SetRealtime(TRUE);
  7526. block.SetRealtime(TRUE);
  7527. END;
  7528. ELSIF name=Global.NameUnchecked THEN
  7529. IF block.isUnchecked THEN
  7530. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7531. ELSE
  7532. block.SetUnchecked(TRUE);
  7533. END;
  7534. ELSIF (name=Global.NameUncooperative) THEN
  7535. IF block.isUncooperative THEN
  7536. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7537. ELSE
  7538. block.SetUncooperative(TRUE);
  7539. END;
  7540. ELSE
  7541. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7542. END;
  7543. blockModifier := blockModifier.nextModifier;
  7544. END;
  7545. END BlockFlags;
  7546. (** check and resolve statement block
  7547. - check flags (exclusive)
  7548. - check statement sequence
  7549. **)
  7550. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7551. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7552. BEGIN
  7553. BlockFlags(statementBlock);
  7554. IF statementBlock.isExclusive THEN
  7555. (* check that not in exclusive block *)
  7556. IF currentIsExclusive THEN
  7557. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7558. ELSIF currentIsRealtime THEN
  7559. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7560. END;
  7561. END;
  7562. recentExclusive := currentIsExclusive;
  7563. recentUnreachable := currentIsUnreachable;
  7564. recentRealtime := currentIsRealtime;
  7565. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7566. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7567. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7568. StatementSequence(statementBlock.statements);
  7569. currentIsRealtime := recentRealtime;
  7570. currentIsExclusive := recentExclusive;
  7571. currentIsUnreachable := recentUnreachable;
  7572. END VisitStatementBlock;
  7573. (** check and resolve body
  7574. - check flags (active, priority, safe)
  7575. - check body and finally part
  7576. **)
  7577. PROCEDURE Body(body: SyntaxTree.Body);
  7578. BEGIN
  7579. VisitStatementBlock(body);
  7580. IF body.isActive THEN
  7581. IF ~currentIsBodyProcedure THEN
  7582. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7583. ELSIF body.priority # NIL THEN
  7584. body.SetPriority(ConstantInteger(body.priority));
  7585. END;
  7586. ELSIF body.isSafe THEN
  7587. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7588. ELSIF body.priority # NIL THEN
  7589. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7590. END;
  7591. IF body.code # NIL THEN
  7592. CheckSystemImport(body.position);
  7593. END;
  7594. StatementSequence(body.finally)
  7595. END Body;
  7596. (*** scopes ***)
  7597. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7598. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7599. VAR duplicateSymbol: BOOLEAN;
  7600. BEGIN
  7601. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7602. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7603. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7604. END;
  7605. scope.EnterSymbol(symbol,duplicateSymbol);
  7606. IF ~allowDuplicate & duplicateSymbol THEN
  7607. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7608. IF VerboseErrorMessage THEN
  7609. Printout.Info("multiply defined identifier",symbol);
  7610. Printout.Info("in scope",scope);
  7611. END;
  7612. END;
  7613. END Register;
  7614. (**
  7615. implementation: check and resolve an implementation part
  7616. **)
  7617. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7618. move implementation checker to a separate object ? *)
  7619. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7620. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7621. BEGIN
  7622. prevIsRealtime := currentIsRealtime;
  7623. prevIsBodyProcedure := currentIsBodyProcedure;
  7624. prevIsCellNet := currentIsCellNet;
  7625. prevScope := currentScope;
  7626. currentScope := scope;
  7627. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7628. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7629. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7630. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7631. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7632. (*
  7633. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7634. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7635. END;
  7636. *)
  7637. END;
  7638. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7639. (* module body, record bodies are wrapped into an artifical procedure *)
  7640. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7641. Body(scope(SyntaxTree.ProcedureScope).body)
  7642. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7643. Body(scope(SyntaxTree.ProcedureScope).body)
  7644. END;
  7645. END;
  7646. currentScope := prevScope;
  7647. currentIsRealtime := prevIsRealtime;
  7648. currentIsBodyProcedure := prevIsBodyProcedure;
  7649. currentIsCellNet := prevIsCellNet;
  7650. END Implementation;
  7651. (** implementation phase:
  7652. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7653. **)
  7654. PROCEDURE Implementations(x: SyntaxTree.Module);
  7655. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7656. BEGIN
  7657. prevPhase := phase;
  7658. phase := InlinePhase;
  7659. scope := x.firstScope;
  7660. WHILE(scope # NIL) DO
  7661. Implementation(scope);
  7662. scope := scope.nextScope;
  7663. END;
  7664. phase := ImplementationPhase;
  7665. scope := x.firstScope;
  7666. WHILE(scope # NIL) DO
  7667. Implementation(scope);
  7668. scope := scope.nextScope;
  7669. END;
  7670. phase := prevPhase;
  7671. END Implementations;
  7672. (** declaration phase:
  7673. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7674. - import lists (for module scopes)
  7675. - parameter list (for procedure scopes)
  7676. - constant declarations
  7677. - type declarations
  7678. - variable declarations
  7679. - procedure declarations
  7680. preformed in two stages:
  7681. - first all symbols are entered into the symbol table (with uniqueness check),
  7682. - then all symbols are resolved
  7683. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7684. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7685. **)
  7686. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7687. VAR
  7688. constant: SyntaxTree.Constant;
  7689. typeDeclaration: SyntaxTree.TypeDeclaration;
  7690. variable: SyntaxTree.Variable;
  7691. procedure: SyntaxTree.Procedure;
  7692. prevScope: SyntaxTree.Scope;
  7693. parameter: SyntaxTree.Parameter;
  7694. import: SyntaxTree.Import;
  7695. symbol: SyntaxTree.Symbol;
  7696. prevPhase: LONGINT;
  7697. prevError : BOOLEAN;
  7698. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7699. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7700. BEGIN
  7701. IF type.baseType # NIL THEN
  7702. baseType := type.baseType.resolved;
  7703. IF baseType IS SyntaxTree.PointerType THEN
  7704. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7705. END;
  7706. (*
  7707. IF baseType IS SyntaxTree.CellType THEN
  7708. DeclareCell(baseType(SyntaxTree.CellType));
  7709. END;
  7710. *)
  7711. END;
  7712. parameter := type.firstParameter;
  7713. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7714. (*
  7715. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7716. variable.SetType(parameter.type);
  7717. variable.SetAccess(SyntaxTree.Hidden);
  7718. variable.SetModifiers(parameter.modifiers);
  7719. currentScope.PushVariable(variable);
  7720. *)
  7721. Register(parameter,scope, FALSE);
  7722. parameter := parameter.nextParameter;
  7723. END;
  7724. property := type.firstProperty;
  7725. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7726. (*
  7727. variable := currentScope.FindVariable(property.name);
  7728. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7729. prop := variable(SyntaxTree.Property);
  7730. ELSE (* add, duplicate symbols detection later *)
  7731. prop := SyntaxTree.NewProperty(property.position, property.name);
  7732. currentScope.PushVariable(prop);
  7733. END;
  7734. prop.SetType(property.type);
  7735. prop.SetValue(property.value);
  7736. prop.SetAccess(SyntaxTree.Hidden);
  7737. *)
  7738. Register(property, scope, FALSE);
  7739. property := property.nextProperty;
  7740. END;
  7741. END DeclareCell;
  7742. BEGIN
  7743. prevError := error;
  7744. prevPhase := phase;
  7745. phase := DeclarationPhase;
  7746. prevScope := currentScope;
  7747. currentScope := scope;
  7748. error := FALSE;
  7749. (* first enter all symbols in scope *)
  7750. IF scope IS SyntaxTree.ModuleScope THEN
  7751. (* treat imports first for a module scope, , set default context if necessary *)
  7752. import := scope(SyntaxTree.ModuleScope).firstImport;
  7753. WHILE(import # NIL) DO
  7754. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7755. Register(import, currentScope, FALSE);
  7756. import := import.nextImport;
  7757. END;
  7758. import := scope(SyntaxTree.ModuleScope).firstImport;
  7759. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7760. ResolveSymbol(import);
  7761. import := import.nextImport;
  7762. END;
  7763. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7764. (* enter parameters for a procedure scope *)
  7765. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7766. WHILE(parameter # NIL) DO
  7767. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7768. END;
  7769. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7770. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7771. ELSIF scope IS SyntaxTree.CellScope THEN
  7772. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7773. IF~skipImplementation THEN
  7774. import := scope(SyntaxTree.CellScope).firstImport;
  7775. WHILE(import # NIL) DO
  7776. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7777. Register(import, currentScope, FALSE);
  7778. import := import.nextImport;
  7779. END;
  7780. import := scope(SyntaxTree.CellScope).firstImport;
  7781. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7782. ResolveSymbol(import);
  7783. import := import.nextImport;
  7784. END;
  7785. END;
  7786. END;
  7787. IF error THEN RETURN END;
  7788. IF skipImplementation THEN
  7789. scope.Clear;
  7790. END;
  7791. (* constants *)
  7792. constant := scope.firstConstant;
  7793. WHILE (constant # NIL) DO
  7794. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7795. END;
  7796. (* type declarations *)
  7797. typeDeclaration := scope.firstTypeDeclaration;
  7798. WHILE (typeDeclaration # NIL) DO
  7799. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7800. END;
  7801. (* variables *)
  7802. variable := scope.firstVariable;
  7803. WHILE (variable # NIL) DO
  7804. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7805. END;
  7806. (* procedures *)
  7807. procedure := scope.firstProcedure;
  7808. WHILE (procedure # NIL) DO
  7809. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7810. END;
  7811. (* now process all symbols without any presumption on the order *)
  7812. symbol := scope.firstSymbol;
  7813. WHILE(symbol # NIL) DO
  7814. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7815. ResolveSymbol(symbol);
  7816. END;
  7817. symbol := symbol.nextSymbol;
  7818. END;
  7819. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7820. symbol := scope.firstSymbol;
  7821. WHILE symbol # NIL DO
  7822. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7823. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7824. pointerFixes.Add(symbol, currentScope);
  7825. END;
  7826. IF ~symbol.type.resolved.isRealtime THEN
  7827. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7828. END;
  7829. END;
  7830. symbol := symbol.nextSymbol
  7831. END;
  7832. END;
  7833. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7834. Error(Basic.invalidPosition,Diagnostics.Invalid,"problems during offset computation in module");
  7835. END;
  7836. IF (scope.ownerModule # NIL) THEN
  7837. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7838. scope.ownerModule.AddScope(scope);
  7839. END;
  7840. phase := prevPhase;
  7841. currentScope := prevScope;
  7842. error := error OR prevError;
  7843. END Declarations;
  7844. (* nopov *)
  7845. (** check if all operators from one module are compatible to the ones in the other module
  7846. - check if there are not multiple operators with the same signature
  7847. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7848. - check for all operators whose signatures are compatible, whether the return types are compatible
  7849. note that:
  7850. - the return type is not considered to be part of the signature
  7851. - two signatures are considered compatible, if all of the operands are compatible
  7852. **)
  7853. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7854. VAR
  7855. thisOperator, thatOperator: SyntaxTree.Operator;
  7856. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7857. thisParameter, thatParameter: SyntaxTree.Parameter;
  7858. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7859. i: LONGINT;
  7860. BEGIN
  7861. currentScope := thisModuleScope;
  7862. hasError := FALSE;
  7863. (* go through all operators in the other module *)
  7864. thatOperator := thatModuleScope.firstOperator;
  7865. WHILE (thatOperator # NIL) & ~hasError DO
  7866. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7867. (* the other operator is accessible *)
  7868. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7869. (* the other operator is not the conversion operator *)
  7870. (* go through all operators in this module *)
  7871. thisOperator := thisModuleScope.firstOperator;
  7872. WHILE (thisOperator # NIL) & ~hasError DO
  7873. IF thisOperator # thatOperator THEN
  7874. (* the operators are not the same *)
  7875. IF thisOperator.name = thatOperator.name THEN
  7876. (* the operators share the same identifier *)
  7877. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7878. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7879. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7880. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7881. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7882. (* both operators have the same paramter count *)
  7883. thisParameter := thisProcedureType.firstParameter;
  7884. thatParameter := thatProcedureType.firstParameter;
  7885. operandsAreEqual := TRUE;
  7886. operandsAreCompatible := TRUE;
  7887. (* go through all parameters *)
  7888. FOR i := 1 TO thisProcedureType.numberParameters DO
  7889. ASSERT(thatParameter # NIL);
  7890. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7891. operandsAreEqual := FALSE;
  7892. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7893. operandsAreCompatible := FALSE
  7894. END
  7895. END;
  7896. thisParameter := thisParameter.nextParameter;
  7897. thatParameter := thatParameter.nextParameter
  7898. END;
  7899. IF operandsAreEqual THEN
  7900. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7901. hasError := TRUE
  7902. ELSIF operandsAreCompatible THEN
  7903. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7904. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7905. hasError := TRUE
  7906. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7907. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7908. hasError := TRUE
  7909. END
  7910. END
  7911. END
  7912. END
  7913. END;
  7914. thisOperator := thisOperator.nextOperator
  7915. END
  7916. END
  7917. END;
  7918. thatOperator := thatOperator.nextOperator
  7919. END
  7920. END CheckInterOperatorConformity;
  7921. (** check module:
  7922. - check module declaration
  7923. - add context, if necessary
  7924. - remove module from import cache, if necessary
  7925. - check declarations
  7926. - resolve all type fixes
  7927. - check implementation (bodies)
  7928. **)
  7929. PROCEDURE Module*(x: SyntaxTree.Module);
  7930. VAR (* nopov *)
  7931. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7932. BEGIN
  7933. prevScope := currentScope;
  7934. prevIsCellNet := currentIsCellNet;
  7935. module := x;
  7936. ASSERT(x # NIL);
  7937. global := system.globalScope[x.case];
  7938. x.moduleScope.SetGlobalScope(global);
  7939. currentScope := global;
  7940. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7941. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7942. RemoveModuleFromCache(importCache,x);
  7943. Declarations(x.moduleScope, FALSE);
  7944. FixTypes();
  7945. IF module.isCellNet THEN
  7946. currentIsCellNet := TRUE;
  7947. modifier := x.modifiers;
  7948. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7949. CheckModifiers(modifier, FALSE);
  7950. END;
  7951. (* nopov *)
  7952. IF ~error THEN
  7953. (* check if operators conform to each other within this module *)
  7954. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7955. (* go through all imports *)
  7956. import := x.moduleScope.firstImport;
  7957. WHILE import # NIL DO
  7958. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7959. (* check if all operators in this module conform to the ones of the imported module *)
  7960. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7961. END;
  7962. import := import.nextImport
  7963. END;
  7964. END;
  7965. Implementations(x);
  7966. module := NIL;
  7967. currentIsCellNet := prevIsCellNet;
  7968. currentScope := prevScope;
  7969. END Module;
  7970. END Checker;
  7971. Warnings*=OBJECT (SyntaxTree.Visitor)
  7972. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7973. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7974. BEGIN
  7975. SELF.diagnostics := diagnostics
  7976. END InitWarnings;
  7977. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  7978. BEGIN END VisitPortType;
  7979. (** types *)
  7980. PROCEDURE Type(x: SyntaxTree.Type);
  7981. BEGIN x.Accept(SELF)
  7982. END Type;
  7983. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7984. BEGIN END VisitType;
  7985. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7986. BEGIN END VisitBasicType;
  7987. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7988. BEGIN END VisitCharacterType;
  7989. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7990. BEGIN END VisitIntegerType;
  7991. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7992. BEGIN END VisitFloatType;
  7993. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7994. BEGIN END VisitQualifiedType;
  7995. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7996. BEGIN END VisitStringType;
  7997. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7998. BEGIN END VisitEnumerationType;
  7999. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8000. BEGIN END VisitRangeType;
  8001. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8002. BEGIN
  8003. IF ~(SyntaxTree.Warned IN x.state) THEN
  8004. x.SetState(SyntaxTree.Warned);
  8005. Type(x.arrayBase);
  8006. END;
  8007. END VisitArrayType;
  8008. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8009. BEGIN
  8010. IF ~(SyntaxTree.Warned IN x.state) THEN
  8011. x.SetState(SyntaxTree.Warned);
  8012. Type(x.arrayBase);
  8013. END;
  8014. END VisitMathArrayType;
  8015. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8016. BEGIN
  8017. IF ~(SyntaxTree.Warned IN x.state) THEN
  8018. x.SetState(SyntaxTree.Warned);
  8019. Type(x.pointerBase);
  8020. END;
  8021. END VisitPointerType;
  8022. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8023. BEGIN Scope(x.recordScope) END VisitRecordType;
  8024. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8025. BEGIN Scope(x.cellScope) END VisitCellType;
  8026. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8027. BEGIN END VisitProcedureType;
  8028. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8029. VAR msg: ARRAY 256 OF CHAR;
  8030. BEGIN
  8031. Global.GetSymbolName(x,msg);
  8032. Strings.Append(msg," ");
  8033. Strings.Append(msg,text);
  8034. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8035. END Warning;
  8036. (** symbols *)
  8037. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8038. BEGIN
  8039. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8040. IF ~(x IS SyntaxTree.Parameter) THEN
  8041. Warning(x,"never used");
  8042. END;
  8043. END;
  8044. x.Accept(SELF);
  8045. END Symbol;
  8046. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8047. BEGIN END VisitSymbol;
  8048. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8049. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8050. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8051. BEGIN END VisitConstant;
  8052. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8053. BEGIN END VisitVariable;
  8054. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8055. BEGIN END VisitProperty;
  8056. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8057. BEGIN END VisitParameter;
  8058. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8059. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8060. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8061. BEGIN END VisitOperator;
  8062. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8063. BEGIN END VisitImport;
  8064. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8065. VAR
  8066. symbol: SyntaxTree.Symbol;
  8067. BEGIN
  8068. symbol := scope.firstSymbol;
  8069. WHILE(symbol # NIL) DO
  8070. Symbol(symbol);
  8071. symbol := symbol.nextSymbol;
  8072. END;
  8073. END Scope;
  8074. PROCEDURE Module*(x: SyntaxTree.Module);
  8075. BEGIN
  8076. SELF.module := x;
  8077. Scope(x.moduleScope);
  8078. END Module;
  8079. END Warnings;
  8080. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8081. BEGIN
  8082. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8083. END IsOberonInline;
  8084. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8085. BEGIN
  8086. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8087. END Resolved;
  8088. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8089. VAR i: LONGINT;
  8090. BEGIN
  8091. i := 1;
  8092. WHILE i < x DO
  8093. i := i *2
  8094. END;
  8095. RETURN i=x
  8096. END PowerOf2;
  8097. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8098. BEGIN
  8099. RETURN
  8100. (scope # NIL) &
  8101. (scope IS SyntaxTree.ModuleScope)
  8102. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8103. OR
  8104. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8105. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8106. END IsCellNetScope;
  8107. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8108. BEGIN
  8109. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8110. END IsCellScope;
  8111. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8112. BEGIN
  8113. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8114. RETURN (scope # NIL) & IsCellNetScope(scope)
  8115. END InCellNetScope;
  8116. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8117. BEGIN
  8118. ASSERT(size MOD system.dataUnit = 0);
  8119. RETURN size DIV system.dataUnit
  8120. END ToMemoryUnits;
  8121. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8122. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8123. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8124. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8125. BEGIN
  8126. IF t = NIL THEN
  8127. RETURN TRUE
  8128. ELSE
  8129. t := t.resolved;
  8130. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8131. END;
  8132. END TypeAllowed;
  8133. BEGIN
  8134. type := type.resolved;
  8135. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8136. RETURN FALSE
  8137. ELSE
  8138. procedureType := type(SyntaxTree.ProcedureType);
  8139. numberParameters := procedureType.numberParameters;
  8140. RETURN
  8141. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8142. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8143. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8144. END;
  8145. END GetProcedureAllowed;
  8146. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8147. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8148. VAR import: SyntaxTree.Import;
  8149. BEGIN
  8150. import := importCache.ImportByModuleName(x.name,x.context);
  8151. IF import # NIL THEN
  8152. importCache.RemoveImporters(x.name,x.context);
  8153. END;
  8154. END RemoveModuleFromCache;
  8155. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8156. (* to <- this assignment compatibility *)
  8157. VAR result: BOOLEAN;
  8158. BEGIN
  8159. IF this= NIL THEN result := (to=NIL)
  8160. ELSIF to=NIL THEN result := FALSE
  8161. ELSE
  8162. (*! will be replaced by this:
  8163. ELSE result := this.CompatibleTo(to.resolved);
  8164. *)
  8165. this := this.resolved; to := to.resolved;
  8166. IF to=SyntaxTree.invalidType THEN result := FALSE
  8167. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8168. ELSIF to = this THEN
  8169. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8170. ELSIF to IS SyntaxTree.BasicType THEN
  8171. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8172. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8173. result := this.CompatibleTo(to.resolved)
  8174. ELSE
  8175. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8176. END
  8177. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8178. result := to.sizeInBits = this.sizeInBits;
  8179. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8180. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8181. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8182. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8183. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8184. result := TRUE;
  8185. ELSIF to IS SyntaxTree.AnyType THEN
  8186. 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);
  8187. ELSIF to IS SyntaxTree.ObjectType THEN
  8188. 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 *) ;
  8189. ELSIF to IS SyntaxTree.ByteType THEN
  8190. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8191. ELSIF to IS SyntaxTree.CharacterType THEN
  8192. result := IsCharacterType(this)
  8193. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8194. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8195. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8196. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8197. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8198. result := TRUE;
  8199. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8200. result := TRUE;
  8201. ELSE
  8202. result := FALSE
  8203. END;
  8204. ELSIF to IS SyntaxTree.PointerType THEN
  8205. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8206. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8207. & (~to.isRealtime OR this.isRealtime);
  8208. ELSIF to IS SyntaxTree.ProcedureType THEN
  8209. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8210. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8211. & (~to.isRealtime OR this.isRealtime)
  8212. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8213. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8214. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8215. ELSIF to IS SyntaxTree.RecordType THEN
  8216. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8217. ELSIF to IS SyntaxTree.ArrayType THEN
  8218. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8219. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8220. ELSIF StaticArrayCompatible(to, this) THEN
  8221. result := TRUE
  8222. ELSE
  8223. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8224. END;
  8225. ELSIF to IS SyntaxTree.MathArrayType THEN
  8226. IF this IS SyntaxTree.MathArrayType THEN
  8227. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8228. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8229. result := TRUE;
  8230. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8231. result := TRUE;
  8232. ELSE
  8233. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8234. END;
  8235. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8236. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8237. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8238. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8239. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8240. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8241. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8242. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8243. ELSE
  8244. result := FALSE
  8245. END;
  8246. (* an array-structured object type is compatible to the type of its array structure *)
  8247. ELSIF IsArrayStructuredObjectType(this) THEN
  8248. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8249. ELSE
  8250. result := FALSE;
  8251. END;
  8252. ELSIF to IS SyntaxTree.StringType THEN
  8253. result := FALSE;
  8254. ELSIF to IS SyntaxTree.EnumerationType THEN
  8255. result := IsEnumerationExtension(this,to);
  8256. ELSIF to IS SyntaxTree.PortType THEN
  8257. result := SameType(to, this)
  8258. ELSE
  8259. Printout.Info("CompatibleTo",to);
  8260. HALT(100); (* implement missing type check *)
  8261. END;
  8262. END;
  8263. RETURN result
  8264. END CompatibleTo;
  8265. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8266. VAR actualBase, formalBase: SyntaxTree.Type;
  8267. BEGIN
  8268. IF SameType(formal,actual) THEN
  8269. RETURN TRUE
  8270. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8271. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8272. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8273. RETURN
  8274. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8275. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8276. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8277. & StaticArrayCompatible(formalBase,actualBase)
  8278. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8279. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8280. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8281. RETURN
  8282. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8283. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8284. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8285. & StaticArrayCompatible(formalBase,actualBase)
  8286. ELSE RETURN FALSE
  8287. END;
  8288. END StaticArrayCompatible;
  8289. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8290. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8291. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8292. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8293. BEGIN
  8294. result := SameType(formal,actual);
  8295. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8296. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8297. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8298. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8299. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8300. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8301. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8302. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8303. & TC(formalBase, actualBase);
  8304. END;
  8305. RETURN result
  8306. END TC;
  8307. BEGIN
  8308. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8309. ELSE
  8310. arrayBase := formalType.arrayBase.resolved;
  8311. IF (actualType IS SyntaxTree.StringType) THEN
  8312. result := arrayBase IS SyntaxTree.CharacterType
  8313. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8314. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8315. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8316. result := TC(formalType, actualType);
  8317. ELSE
  8318. result := (arrayBase IS SyntaxTree.ByteType)
  8319. END;
  8320. END;
  8321. RETURN result
  8322. END OpenArrayCompatible;
  8323. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8324. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8325. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8326. BEGIN
  8327. IF actualType IS SyntaxTree.MathArrayType THEN
  8328. actualArray := actualType(SyntaxTree.MathArrayType);
  8329. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8330. (*
  8331. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8332. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8333. *)
  8334. actualBase := ArrayBase(actualType,Infinity);
  8335. formalBase := ArrayBase(formalType,Infinity);
  8336. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8337. ELSE
  8338. (*
  8339. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8340. *)
  8341. formalBase := Resolved(formalType.arrayBase);
  8342. actualBase := Resolved(actualArray.arrayBase);
  8343. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8344. (*
  8345. ARRAY [k] -> ARRAY [n]
  8346. *)
  8347. result := (formalType.staticLength = actualArray.staticLength)
  8348. ELSE
  8349. result := TRUE
  8350. END;
  8351. IF ~result THEN
  8352. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8353. ELSIF actualBase = NIL THEN result := FALSE
  8354. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8355. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8356. ELSE
  8357. result := SameType(formalBase,actualBase)
  8358. END;
  8359. END;
  8360. ELSE
  8361. result := FALSE
  8362. END;
  8363. RETURN result
  8364. END MathArrayCompatible;
  8365. (**
  8366. Math Array Type distance for assignments / parameter passings of the form
  8367. from -> to
  8368. variants:
  8369. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8370. allowed:
  8371. static -> static (& size match)
  8372. static -> open
  8373. static -> tensor
  8374. open -> open
  8375. open -> tensor
  8376. open -> static
  8377. tensor -> tensor
  8378. tensor -> open
  8379. tensor -> static
  8380. **)
  8381. (*! think about the metric here: is form matching more important than element type matching? *)
  8382. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8383. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8384. BEGIN
  8385. fromBase := Resolved(from.arrayBase);
  8386. toBase := Resolved(to.arrayBase);
  8387. i := Infinity;
  8388. IF from = to THEN
  8389. i := 0;
  8390. ELSIF (from.form = to.form) THEN
  8391. (* static -> static, open -> open, tensor -> tensor *)
  8392. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8393. IF fromBase = toBase THEN i := 0
  8394. ELSIF toBase = NIL THEN i := 1
  8395. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8396. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8397. ELSE
  8398. i := TypeDistance(system,fromBase, toBase, varpar);
  8399. END;
  8400. END;
  8401. ELSIF (to.form = SyntaxTree.Static) THEN
  8402. (* forbidden *)
  8403. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8404. (* static -> tensor, open -> tensor, tensor -> open *)
  8405. IF toBase=fromBase THEN i := 0;
  8406. ELSIF toBase = NIL THEN i := 1;
  8407. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8408. toBase := ArrayBase(toBase,Infinity);
  8409. IF (fromBase=toBase) THEN i := 0
  8410. ELSIF (toBase = NIL) THEN i:= 1
  8411. ELSIF (fromBase = NIL) THEN i := Infinity;
  8412. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8413. END;
  8414. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8415. fromBase := ArrayBase(fromBase,Infinity);
  8416. IF (fromBase=toBase) THEN i := 0
  8417. ELSIF (toBase = NIL) THEN i := 1
  8418. ELSIF (fromBase = NIL) THEN i := Infinity;
  8419. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8420. END;
  8421. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8422. END;
  8423. IF i # Infinity THEN INC(i,2) END;
  8424. ELSIF (from.form = SyntaxTree.Static) THEN
  8425. (* static -> open *)
  8426. IF toBase=fromBase THEN i := 0
  8427. ELSIF toBase = NIL THEN i := 1
  8428. ELSIF fromBase = NIL THEN i := Infinity
  8429. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8430. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8431. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8432. END;
  8433. IF i # Infinity THEN INC(i,1) END;
  8434. ELSE HALT(100); (* unknown case *)
  8435. END;
  8436. RETURN i;
  8437. END MathArrayTypeDistance;
  8438. (** compute and return the distance of two array types
  8439. - return the distance of the base types
  8440. **)
  8441. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8442. VAR i: LONGINT;
  8443. BEGIN
  8444. i := Infinity;
  8445. IF from = to THEN
  8446. i := 0
  8447. ELSE
  8448. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8449. (*
  8450. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8451. i := TypeDistance(from.base, to.base);
  8452. IF i >= 0 THEN INC(i) END
  8453. ELSIF (from.mode = open) & (to.mode = open) THEN
  8454. i := TypeDistance(from.base, to.base);
  8455. *)
  8456. END;
  8457. RETURN i
  8458. END ArrayTypeDistance;
  8459. (** compute the signature distance of a procedure and an actual parameter list
  8460. - if any of the parameters are not compatible, the result is infinite
  8461. - add up and return the distance over all parameters
  8462. **)
  8463. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8464. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8465. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8466. BEGIN
  8467. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8468. result := Infinity
  8469. ELSE
  8470. formalParameter := procedureType.firstParameter;
  8471. i := 0;
  8472. result := 0;
  8473. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8474. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8475. actualParameter := actualParameters.GetExpression(i);
  8476. ASSERT(formalParameter.type # NIL);
  8477. IF (actualParameter.type = NIL) THEN distance := Infinity
  8478. ELSE
  8479. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8480. END;
  8481. IF distance = Infinity THEN
  8482. result := Infinity;
  8483. ELSE
  8484. to := formalParameter.type.resolved;
  8485. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8486. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8487. (* already handled varpar *)
  8488. (*
  8489. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8490. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8491. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8492. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8493. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8494. END;
  8495. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8496. result := Infinity
  8497. END;
  8498. *)
  8499. INC(result, distance);
  8500. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8501. INC(result, distance);
  8502. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8503. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8504. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8505. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8506. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8507. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8508. END;
  8509. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8510. result := Infinity
  8511. END;
  8512. ELSE
  8513. result := Infinity
  8514. END;
  8515. ELSE
  8516. INC(result,distance);
  8517. END;
  8518. END;
  8519. (*
  8520. Printout.Info("actual=", actualParameter);
  8521. Printout.Info("formal=", formalParameter);
  8522. TRACE(result);
  8523. *)
  8524. formalParameter := formalParameter.nextParameter; INC(i);
  8525. END;
  8526. END;
  8527. ASSERT(result >= 0);
  8528. RETURN result
  8529. END Distance;
  8530. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8531. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8532. BEGIN
  8533. IF right.numberParameters # (procedureType.numberParameters) THEN
  8534. result := Infinity
  8535. ELSE
  8536. formalParameter := procedureType.firstParameter;
  8537. rightParameter := right.firstParameter;
  8538. i := 0;
  8539. result := 0;
  8540. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8541. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8542. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8543. IF distance = Infinity THEN
  8544. result := Infinity;
  8545. ELSE
  8546. INC(result,distance);
  8547. END;
  8548. formalParameter := formalParameter.nextParameter;
  8549. rightParameter := rightParameter.nextParameter;
  8550. END;
  8551. END;
  8552. ASSERT(result >= 0);
  8553. RETURN result
  8554. END ProcedureTypeDistance;
  8555. (** compute and return the distance between two types, used for computation of signature distance
  8556. from -> to
  8557. **)
  8558. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8559. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8560. BEGIN
  8561. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8562. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8563. END;
  8564. i := Infinity;
  8565. IF from = to THEN
  8566. i := 0
  8567. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8568. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8569. i := Infinity;
  8570. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8571. i := 10;
  8572. ELSIF (from IS SyntaxTree.StringType) THEN
  8573. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8574. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8575. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8576. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8577. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8578. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8579. i := 1
  8580. ELSIF (from IS SyntaxTree.NilType) THEN
  8581. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8582. (*
  8583. ELSIF (from = NoType) THEN
  8584. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8585. *)
  8586. ELSIF (from IS SyntaxTree.BasicType) THEN
  8587. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8588. IF varpar & (i # 0) THEN i := Infinity END;
  8589. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8590. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8591. ELSIF (from IS SyntaxTree.RecordType) THEN
  8592. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8593. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8594. IF to IS SyntaxTree.MathArrayType THEN
  8595. (*
  8596. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8597. i := Infinity;
  8598. ELSE
  8599. *)
  8600. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8601. (*
  8602. END;
  8603. *)
  8604. END
  8605. ELSIF (from IS SyntaxTree.PointerType) THEN
  8606. ptr := from(SyntaxTree.PointerType);
  8607. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8608. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8609. (* ELSE i := TypeDistance(ptr.base, to); *)
  8610. END
  8611. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8612. IF (to IS SyntaxTree.ProcedureType) THEN
  8613. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8614. END;
  8615. ELSIF (from IS SyntaxTree.PortType) THEN
  8616. IF (to IS SyntaxTree.PortType) THEN
  8617. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8618. i := 0;
  8619. END;
  8620. END;
  8621. (*no procedure test, procedure must be the same*)
  8622. END;
  8623. RETURN i
  8624. END TypeDistance;
  8625. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8626. BEGIN
  8627. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8628. END IsIntegerType;
  8629. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8630. BEGIN
  8631. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8632. END IsAddressType;
  8633. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8634. BEGIN
  8635. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8636. END IsSizeType;
  8637. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8638. BEGIN
  8639. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8640. END IsSignedIntegerType;
  8641. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8642. BEGIN
  8643. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8644. END IsUnsignedIntegerType;
  8645. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8646. VAR result: BOOLEAN;
  8647. BEGIN
  8648. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8649. value := x.resolved(SyntaxTree.IntegerValue).value;
  8650. result := TRUE
  8651. ELSE
  8652. result := FALSE
  8653. END;
  8654. RETURN result
  8655. END IsIntegerValue;
  8656. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8657. VAR result: BOOLEAN;
  8658. BEGIN
  8659. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8660. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8661. result := TRUE
  8662. ELSE
  8663. result := FALSE
  8664. END;
  8665. RETURN result
  8666. END IsEnumerationValue;
  8667. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8668. VAR result: BOOLEAN;
  8669. BEGIN
  8670. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8671. value := x.resolved(SyntaxTree.RealValue).value;
  8672. result := TRUE
  8673. ELSE
  8674. result := FALSE
  8675. END;
  8676. RETURN result
  8677. END IsRealValue;
  8678. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8679. VAR result: BOOLEAN;
  8680. BEGIN
  8681. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8682. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8683. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8684. result := TRUE
  8685. ELSE
  8686. result := FALSE
  8687. END;
  8688. RETURN result
  8689. END IsComplexValue;
  8690. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8691. VAR result: BOOLEAN;
  8692. BEGIN
  8693. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8694. value := x.resolved(SyntaxTree.CharacterValue).value;
  8695. result := TRUE
  8696. ELSE
  8697. result := FALSE
  8698. END;
  8699. RETURN result
  8700. END IsCharacterValue;
  8701. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8702. VAR result: BOOLEAN;
  8703. BEGIN
  8704. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8705. value := x.resolved(SyntaxTree.BooleanValue).value;
  8706. result := TRUE
  8707. ELSE
  8708. result := FALSE
  8709. END;
  8710. RETURN result
  8711. END IsBooleanValue;
  8712. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8713. VAR result: BOOLEAN;
  8714. BEGIN
  8715. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8716. value := x.resolved(SyntaxTree.SetValue).value;
  8717. result := TRUE
  8718. ELSE
  8719. result := FALSE
  8720. END;
  8721. RETURN result
  8722. END IsSetValue;
  8723. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8724. VAR result: BOOLEAN;
  8725. BEGIN
  8726. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8727. value := x.resolved(SyntaxTree.StringValue).value;
  8728. result := TRUE
  8729. ELSE
  8730. result := FALSE
  8731. END;
  8732. RETURN result
  8733. END IsStringValue;
  8734. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8735. BEGIN
  8736. x := x.resolved;
  8737. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8738. END Indexable;
  8739. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8740. BEGIN
  8741. RETURN t1.SameType(t2.resolved);
  8742. END SameType;
  8743. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8744. BEGIN
  8745. IF t IS SyntaxTree.MathArrayType THEN
  8746. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8747. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8748. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8749. DEC(max);
  8750. END;
  8751. ELSIF t IS SyntaxTree.ArrayType THEN
  8752. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8753. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8754. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8755. END;
  8756. END;
  8757. RETURN t;
  8758. END ArrayBase;
  8759. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8760. BEGIN
  8761. type := type.resolved;
  8762. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8763. base := type(SyntaxTree.ArrayType).arrayBase;
  8764. RETURN TRUE;
  8765. END;
  8766. RETURN FALSE;
  8767. END IsOpenArray;
  8768. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8769. BEGIN
  8770. type := type.resolved;
  8771. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8772. base := type(SyntaxTree.ArrayType).arrayBase;
  8773. dim := type(SyntaxTree.ArrayType).staticLength;
  8774. RETURN TRUE
  8775. ELSE
  8776. RETURN FALSE
  8777. END;
  8778. END IsStaticArray;
  8779. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8780. BEGIN
  8781. type := type.resolved;
  8782. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8783. base := type(SyntaxTree.ArrayType).arrayBase;
  8784. RETURN TRUE
  8785. ELSE
  8786. RETURN FALSE
  8787. END;
  8788. END IsDynamicArray;
  8789. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8790. VAR i: LONGINT;
  8791. BEGIN
  8792. i := 0;
  8793. t := t.resolved;
  8794. IF t IS SyntaxTree.MathArrayType THEN
  8795. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8796. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8797. END;
  8798. ELSIF t IS SyntaxTree.ArrayType THEN
  8799. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8800. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8801. END;
  8802. END;
  8803. RETURN i
  8804. END Dimension;
  8805. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8806. BEGIN
  8807. RETURN expression.assignable;
  8808. END IsVariable;
  8809. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8810. BEGIN
  8811. IF (symbol IS SyntaxTree.Parameter) THEN
  8812. WITH symbol: SyntaxTree.Parameter DO
  8813. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8814. END;
  8815. ELSE
  8816. RETURN FALSE
  8817. END;
  8818. END IsVariableParameter;
  8819. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8820. VAR result: BOOLEAN;
  8821. BEGIN
  8822. IF type = NIL THEN result := FALSE
  8823. ELSE
  8824. type := type.resolved;
  8825. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8826. END;
  8827. RETURN result
  8828. END IsPointerType;
  8829. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8830. VAR result: BOOLEAN;
  8831. BEGIN
  8832. IF type = NIL THEN result := FALSE
  8833. ELSE
  8834. type := type.resolved;
  8835. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8836. END;
  8837. RETURN result
  8838. END IsUnsafePointer;
  8839. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8840. BEGIN
  8841. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8842. END IsDisposable;
  8843. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8844. VAR result: BOOLEAN;
  8845. BEGIN
  8846. IF type = NIL THEN result := FALSE
  8847. ELSE
  8848. type := type.resolved;
  8849. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8850. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8851. result := result OR (type IS SyntaxTree.ObjectType);
  8852. END;
  8853. RETURN result
  8854. END IsPointerToRecord;
  8855. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8856. VAR result: BOOLEAN;
  8857. BEGIN
  8858. IF type = NIL THEN result := FALSE
  8859. ELSE
  8860. type := type.resolved;
  8861. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8862. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8863. ;
  8864. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8865. result := result OR (type IS SyntaxTree.ObjectType);
  8866. END;
  8867. RETURN result
  8868. END IsPointerToObject;
  8869. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8870. BEGIN
  8871. IF type # NIL THEN
  8872. RETURN type.resolved.hasPointers
  8873. ELSE
  8874. RETURN FALSE
  8875. END;
  8876. END ContainsPointer;
  8877. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8878. BEGIN
  8879. IF type = NIL THEN RETURN FALSE END;
  8880. type := type.resolved;
  8881. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8882. END IsStringType;
  8883. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8884. BEGIN
  8885. IF type = NIL THEN RETURN FALSE END;
  8886. type := type.resolved;
  8887. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8888. END IsCharacterType;
  8889. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8890. BEGIN
  8891. IF type = NIL THEN RETURN FALSE END;
  8892. type := type.resolved;
  8893. RETURN (type IS SyntaxTree.EnumerationType)
  8894. END IsEnumerationType;
  8895. (** cf. section "Type extension (base type)" in the language report **)
  8896. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8897. VAR result: BOOLEAN;
  8898. BEGIN
  8899. ASSERT(base # NIL); ASSERT(extension # NIL);
  8900. base := base.resolved; extension := extension.resolved;
  8901. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8902. result := TRUE;
  8903. ELSE
  8904. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8905. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8906. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8907. END;
  8908. WHILE (extension # NIL) & (extension # base) DO
  8909. IF extension IS SyntaxTree.RecordType THEN
  8910. extension := extension(SyntaxTree.RecordType).baseType;
  8911. IF (extension # NIL) THEN extension := extension.resolved END;
  8912. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8913. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8914. END;
  8915. ELSE extension := NIL;
  8916. END;
  8917. END;
  8918. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8919. END;
  8920. RETURN result
  8921. END IsTypeExtension;
  8922. (** check if base is the base enumeration type of extension **)
  8923. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8924. BEGIN
  8925. base := base.resolved; extension := extension.resolved;
  8926. WHILE (extension # NIL) & (extension # base) DO
  8927. IF extension IS SyntaxTree.EnumerationType THEN
  8928. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8929. IF extension # NIL THEN extension := extension.resolved END;
  8930. ELSE
  8931. extension := NIL
  8932. END;
  8933. END;
  8934. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8935. END IsEnumerationExtension;
  8936. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8937. BEGIN
  8938. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8939. RETURN TRUE
  8940. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8941. RETURN TRUE
  8942. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8943. RETURN TRUE
  8944. ELSE
  8945. RETURN FALSE
  8946. END
  8947. END IsCallable;
  8948. (** compute and return the distance of two record types
  8949. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8950. **)
  8951. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8952. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8953. BEGIN
  8954. i := 0;
  8955. WHILE (from # NIL) & (from # to) DO
  8956. baseType := from.baseType;
  8957. IF (baseType # NIL) THEN
  8958. baseType := baseType.resolved;
  8959. IF baseType IS SyntaxTree.PointerType THEN
  8960. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8961. END;
  8962. IF baseType IS SyntaxTree.RecordType THEN
  8963. from := baseType(SyntaxTree.RecordType);
  8964. ELSE
  8965. from := NIL;
  8966. END;
  8967. ELSE
  8968. from := NIL
  8969. END;
  8970. INC(i)
  8971. END;
  8972. IF from = NIL THEN i := Infinity END;
  8973. RETURN i
  8974. END RecordTypeDistance;
  8975. (** compute and return the distance of two pointer types **)
  8976. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8977. BEGIN
  8978. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8979. RETURN Infinity;
  8980. ELSE
  8981. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8982. END;
  8983. END PointerTypeDistance;
  8984. (** check if expression contains a symbol designator pointing to a type declaration.
  8985. - if so then enter type declaration into typeDeclaration and return true else return false
  8986. **)
  8987. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8988. VAR result: BOOLEAN;
  8989. BEGIN
  8990. result := FALSE;
  8991. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8992. result := TRUE;
  8993. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8994. END;
  8995. RETURN result
  8996. END IsTypeDesignator;
  8997. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8998. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8999. VAR result: BOOLEAN;
  9000. BEGIN
  9001. type := type.resolved;
  9002. IF type IS SyntaxTree.PointerType THEN
  9003. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9004. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9005. result := TRUE
  9006. ELSE
  9007. result := type IS SyntaxTree.RecordType
  9008. END;
  9009. RETURN result
  9010. END IsExtensibleType;
  9011. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9012. BEGIN
  9013. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9014. (d IS SyntaxTree.SymbolDesignator) &
  9015. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9016. OR
  9017. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9018. END IsUnextensibleRecord;
  9019. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9020. BEGIN
  9021. IF IsUnextensibleRecord(d) THEN
  9022. RETURN FALSE
  9023. ELSE RETURN IsExtensibleType(d.type.resolved)
  9024. END;
  9025. END IsExtensibleDesignator;
  9026. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9027. BEGIN
  9028. type := type.resolved;
  9029. IF (type IS SyntaxTree.PointerType) THEN
  9030. RETURN TRUE
  9031. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9032. RETURN TRUE
  9033. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9034. RETURN TRUE
  9035. ELSIF (type IS SyntaxTree.BasicType) THEN
  9036. RETURN TRUE
  9037. END;
  9038. RETURN FALSE
  9039. END IsBasicType;
  9040. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9041. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9042. BEGIN
  9043. baseType := record.baseType;
  9044. IF (baseType # NIL) THEN
  9045. baseType := baseType.resolved;
  9046. IF (baseType IS SyntaxTree.PointerType) THEN
  9047. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9048. END;
  9049. END;
  9050. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9051. recordType := baseType(SyntaxTree.RecordType);
  9052. ELSE
  9053. recordType := NIL;
  9054. END;
  9055. RETURN recordType
  9056. END RecordBase;
  9057. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9058. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9059. BEGIN
  9060. baseRecord := RecordBase(scope.ownerRecord);
  9061. IF baseRecord = NIL THEN RETURN NIL END;
  9062. scope := baseRecord.recordScope;
  9063. procedureType := procedure.type.resolved;
  9064. IF procedure IS SyntaxTree.Operator THEN
  9065. operator := scope.firstOperator;
  9066. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9067. (*
  9068. Printout.Info("not same ",procedureType);
  9069. Printout.Info("with ",operator.type);
  9070. *)
  9071. operator := operator.nextOperator;
  9072. END;
  9073. super := operator;
  9074. ELSE
  9075. super := scope.firstProcedure;
  9076. WHILE (super # NIL) & (super.name # procedure.name) DO
  9077. super := super.nextProcedure;
  9078. END;
  9079. END;
  9080. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9081. RETURN super
  9082. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9083. RETURN super
  9084. ELSE
  9085. RETURN FindSuperProcedure(scope,procedure);
  9086. END;
  9087. END FindSuperProcedure;
  9088. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9089. VAR procedure: SyntaxTree.Procedure;
  9090. BEGIN
  9091. procedure := record.recordScope.constructor;
  9092. IF procedure = NIL THEN
  9093. record := RecordBase(record);
  9094. IF record # NIL THEN
  9095. procedure := GetConstructor(record)
  9096. END;
  9097. END;
  9098. RETURN procedure;
  9099. END GetConstructor;
  9100. (* enter a case into a list of cases in a sorted way and check for collision *)
  9101. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9102. VAR prev,this,new: SyntaxTree.CaseConstant;
  9103. BEGIN
  9104. this := root;
  9105. prev := NIL;
  9106. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9107. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9108. RETURN FALSE
  9109. ELSE
  9110. IF (this # NIL) & (this.min = max+1) THEN
  9111. this.min := min
  9112. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9113. prev.max := min
  9114. ELSE
  9115. NEW(new); new.min := min; new.max := max;
  9116. new.next := this;
  9117. IF prev = NIL THEN
  9118. root := new;
  9119. ELSE
  9120. prev.next := new
  9121. END
  9122. END;
  9123. RETURN TRUE
  9124. END;
  9125. END EnterCase;
  9126. (** generate and return a new checker object, errors are entered into diagnostics **)
  9127. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9128. VAR checker: Checker;
  9129. BEGIN
  9130. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9131. RETURN checker
  9132. END NewChecker;
  9133. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9134. VAR warnings: Warnings;
  9135. BEGIN
  9136. NEW(warnings, diagnostics); RETURN warnings;
  9137. END NewWarnings;
  9138. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9139. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9140. END IsRangeType;
  9141. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9142. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9143. END IsMathArrayType;
  9144. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9145. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9146. END IsArrayType;
  9147. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9148. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9149. END IsComplexType;
  9150. (** if a type is an array-structured object type *)
  9151. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9152. VAR recordType: SyntaxTree.RecordType;
  9153. BEGIN
  9154. IF type = NIL THEN
  9155. RETURN FALSE
  9156. ELSE
  9157. type := type.resolved;
  9158. IF type IS SyntaxTree.PointerType THEN
  9159. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9160. IF type IS SyntaxTree.RecordType THEN
  9161. recordType := type(SyntaxTree.RecordType);
  9162. RETURN recordType.isObject & recordType.HasArrayStructure()
  9163. ELSE
  9164. RETURN FALSE
  9165. END
  9166. ELSE
  9167. RETURN FALSE
  9168. END
  9169. END
  9170. END IsArrayStructuredObjectType;
  9171. (** the math array structure of a type
  9172. - for math arrays: the array itself
  9173. - for pointers: the math array structure of the pointer base
  9174. - for array-structured object types: the underlying structure
  9175. - for non-math arrays and all other types: NIL
  9176. **)
  9177. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9178. VAR
  9179. result: SyntaxTree.MathArrayType;
  9180. BEGIN
  9181. IF type = NIL THEN
  9182. result := NIL
  9183. ELSE
  9184. type := type.resolved;
  9185. IF type IS SyntaxTree.PointerType THEN
  9186. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9187. END;
  9188. IF type IS SyntaxTree.MathArrayType THEN
  9189. result := type(SyntaxTree.MathArrayType)
  9190. ELSIF type IS SyntaxTree.RecordType THEN
  9191. result := type(SyntaxTree.RecordType).arrayStructure
  9192. ELSE
  9193. result := NIL
  9194. END
  9195. END;
  9196. RETURN result
  9197. END MathArrayStructureOfType;
  9198. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9199. VAR
  9200. result: BOOLEAN;
  9201. rangeExpression: SyntaxTree.RangeExpression;
  9202. BEGIN
  9203. IF x IS SyntaxTree.RangeExpression THEN
  9204. rangeExpression := x(SyntaxTree.RangeExpression);
  9205. result := TRUE;
  9206. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9207. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9208. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9209. ELSE
  9210. result := FALSE
  9211. END;
  9212. RETURN result
  9213. END IsStaticRange;
  9214. (** whether a type is a math array of tensor form **)
  9215. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9216. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9217. END IsTensor;
  9218. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9219. BEGIN
  9220. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9221. length := type(SyntaxTree.MathArrayType).staticLength;
  9222. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9223. RETURN TRUE
  9224. ELSE
  9225. RETURN FALSE
  9226. END;
  9227. END IsStaticMathArray;
  9228. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9229. BEGIN
  9230. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9231. END SymbolHasAddress;
  9232. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9233. BEGIN
  9234. RETURN
  9235. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9236. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9237. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9238. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9239. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9240. ;
  9241. END HasAddress;
  9242. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9243. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9244. BEGIN
  9245. IF (e IS SyntaxTree.Designator) THEN
  9246. d := e(SyntaxTree.Designator);
  9247. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9248. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9249. e := d.left;
  9250. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9251. END;
  9252. IF d # NIL THEN
  9253. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9254. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9255. END;
  9256. END;
  9257. RETURN FALSE;
  9258. END IsLocalVariable;
  9259. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9260. BEGIN
  9261. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9262. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9263. ELSE
  9264. RETURN TRUE
  9265. END;
  9266. END IsStaticProcedure;
  9267. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9268. CONST OptimizeMethodTable = FALSE;
  9269. BEGIN
  9270. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9271. END InMethodTable;
  9272. END FoxSemanticChecker.
  9273. SystemTools.FreeDownTo FoxSemanticChecker ~