FoxSemanticChecker.Mod 403 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032
  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 errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.AppendPosition(errorMessage, position);
  134. diagnostics.Error(errModule.sourceName, position.start, code, errorMessage);
  135. END;
  136. error := TRUE;
  137. END Error;
  138. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  139. VAR errModule: SyntaxTree.Module;
  140. BEGIN
  141. IF diagnostics # NIL THEN
  142. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  143. diagnostics.Warning(errModule.sourceName, position.start, Diagnostics.Invalid, message);
  144. END;
  145. END Warning;
  146. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  147. VAR errorMessage: ARRAY 256 OF CHAR;
  148. BEGIN
  149. IF diagnostics # NIL THEN
  150. Basic.Concat(errorMessage,msg," ", msg2);
  151. Basic.AppendPosition(errorMessage, position);
  152. diagnostics.Error(currentScope.ownerModule.sourceName, position.start, Diagnostics.Invalid, errorMessage);
  153. END;
  154. error := TRUE;
  155. END ErrorSS;
  156. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  157. VAR msg, msg2: ARRAY 256 OF CHAR;
  158. BEGIN
  159. IF diagnostics # NIL THEN
  160. COPY(msg1, msg);
  161. Strings.Append(msg, " = ");
  162. Basic.GetString(s, msg2);
  163. Strings.Append(msg, msg2);
  164. diagnostics.Information(currentScope.ownerModule.sourceName, position.start, Diagnostics.Invalid, msg);
  165. END;
  166. END InfoSS;
  167. (*** symbol lookup ***)
  168. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  169. **)
  170. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  171. VAR
  172. scope,baseScope: SyntaxTree.Scope;
  173. symbol, s: SyntaxTree.Symbol;
  174. ownerRecord,base: SyntaxTree.RecordType;
  175. BEGIN
  176. scope := inScope;
  177. symbol := NIL;
  178. WHILE (scope # NIL) & (symbol = NIL) DO
  179. symbol := scope.FindSymbol(name);
  180. s := NIL;
  181. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  182. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  184. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  185. END;
  186. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  187. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  188. ELSE
  189. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  190. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  191. symbol.MarkUsed;
  192. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  193. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  194. base := RecordBase(ownerRecord);
  195. IF (base # NIL) THEN
  196. baseScope := base.recordScope;
  197. symbol := Find(baseScope,name,FALSE);
  198. ELSE
  199. symbol := NIL;
  200. END;
  201. ELSE
  202. symbol := NIL;
  203. END;
  204. END;
  205. END;
  206. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  207. END;
  208. IF (symbol # NIL) THEN
  209. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  210. ASSERT(phase = DeclarationPhase);
  211. ResolveSymbol(symbol)
  212. END;
  213. symbol.MarkUsed;
  214. END;
  215. RETURN symbol
  216. END Find;
  217. (*** types ***)
  218. (** find type declaration with name qualifiedIdentifier and return resolved type
  219. - check qualified identifier prefix, set scope to module scope if appropriate
  220. - check suffix in scope
  221. **)
  222. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  223. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  224. BEGIN
  225. result := NIL;
  226. prevScope := currentScope;
  227. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  228. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  229. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  230. IF symbol(SyntaxTree.Import).module = NIL THEN
  231. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  232. result := SyntaxTree.invalidType;
  233. symbol := NIL;
  234. ELSE
  235. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  236. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  237. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  238. IF VerboseErrorMessage THEN
  239. Printout.Info("scope", currentScope);
  240. Printout.Info("symbol", symbol);
  241. END;
  242. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  243. END;
  244. END;
  245. ELSE
  246. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  248. symbol := NIL;
  249. END;
  250. ELSE
  251. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  252. IF symbol = NIL THEN
  253. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  254. IF VerboseErrorMessage THEN
  255. Printout.Info("Qualident",qualifiedIdentifier);
  256. Printout.Info("in scope",currentScope) ;
  257. END;
  258. END;
  259. END;
  260. IF symbol = NIL THEN (* error already handled *)
  261. typeDeclaration := NIL;
  262. result := SyntaxTree.invalidType;
  263. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  264. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  265. typeDeclaration := NIL;
  266. result := SyntaxTree.invalidType;
  267. ELSE
  268. currentScope := symbol.scope;
  269. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  270. result := ResolveType(typeDeclaration.declaredType);
  271. symbol.MarkUsed;
  272. ASSERT(result # NIL);
  273. END;
  274. currentScope := prevScope;
  275. RETURN result
  276. END ResolveNamedType;
  277. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  278. If node is currently being resolved then emit a cyclic definition error.
  279. Return TRUE only if node is fully resolved.
  280. **)
  281. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  282. VAR result: BOOLEAN;
  283. BEGIN
  284. IF SyntaxTree.Resolved IN x.state THEN
  285. result := FALSE
  286. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  287. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  288. result := FALSE;
  289. ELSE
  290. result := TRUE;
  291. x.SetState(SyntaxTree.BeingResolved)
  292. END;
  293. RETURN result
  294. END TypeNeedsResolution;
  295. (** Return invalid type if x is currently being resolved, return x otherwise**)
  296. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  297. BEGIN
  298. IF SyntaxTree.Resolved IN x.state THEN
  299. RETURN x
  300. ELSE
  301. RETURN SyntaxTree.invalidType
  302. END;
  303. END ResolvedType;
  304. PROCEDURE VisitType(x: SyntaxTree.Type);
  305. BEGIN
  306. ASSERT(x = SyntaxTree.invalidType);
  307. END VisitType;
  308. (** resolve basic type **)
  309. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  310. BEGIN
  311. IF TypeNeedsResolution(x) THEN
  312. x.SetState(SyntaxTree.Resolved);
  313. END;
  314. resolvedType := ResolvedType(x)
  315. END VisitBasicType;
  316. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitByteType;
  320. (** resolve character type **)
  321. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  322. BEGIN
  323. VisitBasicType(x);
  324. END VisitCharacterType;
  325. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  326. BEGIN
  327. VisitBasicType(x);
  328. END VisitBooleanType;
  329. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  330. BEGIN
  331. VisitBasicType(x);
  332. END VisitSetType;
  333. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  334. BEGIN
  335. VisitBasicType(x);
  336. END VisitAddressType;
  337. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  338. BEGIN
  339. VisitBasicType(x);
  340. END VisitSizeType;
  341. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  342. BEGIN
  343. VisitBasicType(x);
  344. END VisitAnyType;
  345. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitObjectType;
  349. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  350. BEGIN
  351. VisitBasicType(x);
  352. END VisitNilType;
  353. (** resolve integer type **)
  354. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  355. BEGIN
  356. VisitBasicType(x);
  357. END VisitIntegerType;
  358. (** resolve real type **)
  359. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  360. BEGIN
  361. VisitBasicType(x);
  362. END VisitFloatType;
  363. (** resolve complex type **)
  364. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  365. BEGIN
  366. VisitBasicType(x);
  367. END VisitComplexType;
  368. (**
  369. resolve string type: nothing to be done
  370. **)
  371. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  372. BEGIN
  373. IF TypeNeedsResolution(x) THEN
  374. x.SetState(SyntaxTree.Resolved);
  375. END;
  376. resolvedType := ResolvedType(x)
  377. END VisitStringType;
  378. (**
  379. check enumeration scope: enter symbols and check for duplicate names
  380. **)
  381. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  382. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  383. BEGIN
  384. prevScope := currentScope;
  385. currentScope := x;
  386. e := x.firstConstant;
  387. WHILE (e # NIL) DO
  388. Register(e,x,FALSE);
  389. IF SymbolNeedsResolution(e) THEN
  390. IF e.value # NIL THEN
  391. value := ConstantExpression(e.value);
  392. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  393. ELSE
  394. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  395. value.SetType(x.ownerEnumeration);
  396. END;
  397. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  398. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  399. IF nextHighest > highest THEN highest := nextHighest END;
  400. END;
  401. e.SetValue(value);
  402. CheckSymbolVisibility(e);
  403. e.SetType(x.ownerEnumeration);
  404. e.SetState(SyntaxTree.Resolved);
  405. END;
  406. e := e.nextConstant;
  407. END;
  408. currentScope := prevScope;
  409. END CheckEnumerationScope;
  410. (**
  411. resolve enumeration type: check enumeration scope
  412. **)
  413. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  414. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  415. lowest, highest: LONGINT;
  416. BEGIN
  417. IF TypeNeedsResolution(x) THEN
  418. IF x.enumerationBase # NIL THEN
  419. position := x.enumerationBase.position;
  420. baseType := ResolveType(x.enumerationBase);
  421. resolved := baseType.resolved;
  422. baseScope := NIL;
  423. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  424. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  425. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  426. ELSE
  427. enumerationBase := resolved(SyntaxTree.EnumerationType);
  428. lowest := enumerationBase.rangeHighest+1;
  429. END;
  430. x.SetEnumerationBase(baseType);
  431. ELSE lowest := 0;
  432. END;
  433. highest := lowest-1;
  434. CheckEnumerationScope(x.enumerationScope, highest);
  435. x.SetRange(lowest, highest);
  436. x.SetState(SyntaxTree.Resolved);
  437. END;
  438. resolvedType := ResolvedType(x);
  439. END VisitEnumerationType;
  440. (**
  441. resolve range type: nothing to be done
  442. **)
  443. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  444. BEGIN
  445. IF TypeNeedsResolution(x) THEN
  446. x.SetState(SyntaxTree.Resolved);
  447. END;
  448. resolvedType := ResolvedType(x)
  449. END VisitRangeType;
  450. (**
  451. resolve qualified type
  452. - find and resolve named type and set resolved type
  453. **)
  454. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  455. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  456. BEGIN
  457. IF TypeNeedsResolution(x) THEN
  458. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  459. x.SetResolved(type.resolved);
  460. x.SetState(SyntaxTree.Resolved);
  461. x.SetTypeDeclaration (typeDeclaration);
  462. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  463. x.SetResolved(SyntaxTree.invalidType);
  464. END;
  465. resolvedType := x;
  466. END VisitQualifiedType;
  467. (**
  468. resolve array type
  469. - check base type
  470. - array of math array forbidden
  471. - static array of open array forbidden
  472. **)
  473. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  474. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  475. BEGIN
  476. IF TypeNeedsResolution(x) THEN
  477. x.SetArrayBase(ResolveType(x.arrayBase));
  478. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  479. arrayBase := x.arrayBase.resolved;
  480. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  481. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  482. pointerType.SetPointerBase(arrayBase);
  483. pointerType.SetHidden(TRUE);
  484. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  485. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  486. ELSE
  487. x.SetArrayBase(pointerType);
  488. END;
  489. END;
  490. IF x.length # NIL THEN
  491. variableAccessed := FALSE;
  492. e := ResolveExpression(x.length);
  493. IF (e.resolved = NIL) THEN
  494. IF variableAccessed THEN
  495. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  496. END;
  497. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  498. ELSE
  499. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  500. END;
  501. END;
  502. IF arrayBase IS SyntaxTree.ArrayType THEN
  503. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  504. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  505. END;
  506. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  507. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  508. END;
  509. x.SetHasPointers(arrayBase.hasPointers);
  510. x.SetState(SyntaxTree.Resolved);
  511. END;
  512. resolvedType := ResolvedType(x);
  513. END VisitArrayType;
  514. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  515. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  516. BEGIN
  517. module := currentScope.ownerModule;
  518. IF module.name=name THEN
  519. (* do nothing *)
  520. ELSE
  521. moduleScope := module.moduleScope;
  522. import := moduleScope.FindImport(name);
  523. IF import = NIL THEN
  524. import := SyntaxTree.NewImport(position,name,name,TRUE);
  525. moduleScope.AddImport(import);
  526. Register(import,moduleScope,FALSE);
  527. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  528. VisitImport(import);
  529. ELSIF import.direct=FALSE THEN
  530. import.SetScope(module.moduleScope);
  531. import.SetDirect(TRUE);
  532. IF moduleScope.FindSymbol(import.name) = NIL THEN
  533. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  534. duplicate.SetContext(import.context);
  535. duplicate.SetModule(import.module);
  536. Register(duplicate,moduleScope,TRUE);
  537. VisitImport(duplicate);
  538. END;
  539. END;
  540. import.MarkUsed
  541. END;
  542. END ImportModule;
  543. (**
  544. resolve math array type
  545. - check base type
  546. - open math array of array forbidden
  547. - math array of tensor forbidden
  548. - static array of open array forbidden
  549. **)
  550. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  551. VAR arrayBase: SyntaxTree.Type;
  552. BEGIN
  553. IF TypeNeedsResolution(x) THEN
  554. x.SetArrayBase(ResolveType(x.arrayBase));
  555. IF x.length # NIL THEN
  556. x.SetLength(ConstantIntegerGeq0(x.length));
  557. END;
  558. arrayBase := x.arrayBase;
  559. IF arrayBase # NIL THEN
  560. arrayBase := arrayBase.resolved;
  561. IF arrayBase = SyntaxTree.invalidType THEN
  562. (* error already handled *)
  563. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  564. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  565. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  566. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  568. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  569. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  570. END;
  571. END;
  572. IF x.form = SyntaxTree.Static THEN
  573. x.SetIncrement(system.SizeOf(arrayBase));
  574. END;
  575. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  576. END;
  577. x.SetState(SyntaxTree.Resolved);
  578. END;
  579. resolvedType := ResolvedType(x);
  580. END VisitMathArrayType;
  581. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  582. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  583. (1) Rec = RECORD ... END; Ptr <---> Rec
  584. Ptr = POINTER TO Rec; ^ |
  585. | |
  586. TypeDesc TypeDesc
  587. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  588. ^ /
  589. | /
  590. TypeDesc <-- /
  591. *)
  592. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  593. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  594. BEGIN
  595. Strings.IntToStr(x.position.start,number);
  596. COPY(prefix,name);
  597. Strings.Append(name,"@");
  598. Strings.Append(name,number);
  599. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  600. typeDeclaration.SetDeclaredType(x);
  601. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  602. x.SetTypeDeclaration(typeDeclaration);
  603. currentScope.AddTypeDeclaration(typeDeclaration);
  604. typeDeclaration.SetScope(currentScope);
  605. END AnonymousTypeDeclaration;
  606. (**
  607. deferred pointer type resolving
  608. - resolve base type
  609. - check that base type is a record or array type
  610. - if error then set base type to invalid type
  611. **)
  612. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  613. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  614. BEGIN
  615. ASSERT(type.pointerBase # NIL);
  616. position := type.pointerBase.position;
  617. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  618. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  619. (* not for pointers, a type is needed for the records only
  620. IF type.typeDeclaration = NIL THEN
  621. AnonymousTypeDeclaration(type);
  622. END;
  623. *)
  624. END;
  625. resolved := ResolveType(type.pointerBase);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  627. type.SetPointerBase(resolved);
  628. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  629. recordType := resolved.resolved(SyntaxTree.RecordType);
  630. IF recordType.isObject & (recordType.baseType # NIL) THEN
  631. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  633. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  634. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  635. END;
  636. END;
  637. END;
  638. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  639. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  640. END;
  641. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  642. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  643. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  644. ELS
  645. *)
  646. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  647. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  648. END;
  649. END;
  650. ELSE
  651. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  652. type.SetPointerBase(SyntaxTree.invalidType)
  653. END
  654. END FixPointerType;
  655. (**
  656. resolve pointer type
  657. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  658. **)
  659. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  660. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  661. modifiers: SyntaxTree.Modifier; position: Position;
  662. BEGIN
  663. IF TypeNeedsResolution(x) THEN
  664. modifiers := x.modifiers;
  665. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  666. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  667. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  668. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  669. (* inheritance cycle check
  670. example:
  671. A=POINTER TO RECORD(B) END;
  672. B=POINTER TO RECORD(A) END;
  673. *)
  674. IF x.pointerBase IS SyntaxTree.RecordType THEN
  675. recordType := x.pointerBase(SyntaxTree.RecordType);
  676. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  677. recordBaseType := ResolveType(recordType.baseType);
  678. recordType.SetBaseType(recordBaseType);
  679. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  680. END;
  681. CheckModifiers(modifiers, TRUE);
  682. typeFixes.Add(x,currentScope);
  683. x.SetState(SyntaxTree.Resolved);
  684. END;
  685. resolvedType := ResolvedType(x)
  686. END VisitPointerType;
  687. (**
  688. resolve port type
  689. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  690. **)
  691. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  692. VAR value: LONGINT;
  693. BEGIN
  694. IF TypeNeedsResolution(x) THEN
  695. x.SetCellsAreObjects(cellsAreObjects);
  696. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  697. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  698. x.SetSize(value)
  699. ELSE
  700. x.SetSize(system.SizeOf(system.longintType));
  701. END;
  702. x.SetState(SyntaxTree.Resolved);
  703. END;
  704. resolvedType := ResolvedType(x)
  705. END VisitPortType;
  706. (**
  707. deferred procedure type resolving
  708. - resolve return type
  709. - traverse and resolve parameters
  710. **)
  711. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  712. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  713. BEGIN
  714. resolved := ResolveType(procedureType.returnType);
  715. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  716. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  717. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  718. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  719. END;
  720. procedureType.SetReturnType(resolved);
  721. IF (resolved # NIL) THEN
  722. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  723. parameter.SetType(procedureType.returnType);
  724. parameter.SetAccess(SyntaxTree.Hidden);
  725. parameter.SetUntraced(procedureType.hasUntracedReturn);
  726. VisitParameter(parameter);
  727. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  728. END;
  729. (* process parameters *)
  730. parameter :=procedureType.firstParameter;
  731. WHILE (parameter # NIL) DO
  732. VisitParameter(parameter);
  733. parameter := parameter.nextParameter;
  734. END;
  735. END FixProcedureType;
  736. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  737. VAR prev,this: SyntaxTree.Modifier;
  738. BEGIN
  739. this := modifiers;prev := NIL;
  740. WHILE (this # NIL) & (this.identifier # name) DO
  741. prev := this; this := this.nextModifier;
  742. END;
  743. IF this # NIL THEN
  744. IF this.expression # NIL THEN
  745. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  746. END;
  747. this.Resolved;
  748. position := this.position;
  749. RETURN TRUE
  750. ELSE
  751. RETURN FALSE
  752. END;
  753. END HasFlag;
  754. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  755. VAR prev,this: SyntaxTree.Modifier;
  756. BEGIN
  757. this := modifiers;prev := NIL;
  758. WHILE (this # NIL) & (this.identifier # name) DO
  759. prev := this; this := this.nextModifier;
  760. END;
  761. IF this # NIL THEN
  762. IF this.expression = NIL THEN
  763. Error(this.position,Diagnostics.Invalid,"expected expression value");
  764. ELSE
  765. this.SetExpression(ConstantExpression(this.expression));
  766. IF CheckIntegerValue(this.expression,value) THEN END;
  767. END;
  768. this.Resolved;
  769. position := this.position;
  770. RETURN TRUE
  771. ELSE RETURN FALSE
  772. END;
  773. END HasValue;
  774. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  775. VAR prev,this: SyntaxTree.Modifier;
  776. BEGIN
  777. this := modifiers;prev := NIL;
  778. WHILE (this # NIL) & (this.identifier # name) DO
  779. prev := this; this := this.nextModifier;
  780. END;
  781. IF this # NIL THEN
  782. IF this.expression = NIL THEN
  783. Error(this.position,Diagnostics.Invalid,"expected expression value");
  784. ELSE
  785. this.SetExpression(ConstantExpression(this.expression));
  786. IF CheckStringValue(this.expression,value) THEN END;
  787. END;
  788. this.Resolved;
  789. position := this.position;
  790. RETURN TRUE
  791. ELSE RETURN FALSE
  792. END;
  793. END HasStringValue;
  794. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  795. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  796. BEGIN
  797. IF cellsAreObjects THEN RETURN FALSE END;
  798. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  799. IF svalue = "A2" THEN
  800. RETURN TRUE
  801. END;
  802. END;
  803. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  804. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  805. END;
  806. RETURN FALSE;
  807. (*
  808. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  809. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  810. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  811. IF svalue[0] = "~" THEN
  812. Strings.TrimLeft(svalue, "~");
  813. IF svalue = backendName THEN
  814. RETURN TRUE;
  815. END;
  816. ELSIF svalue # backendName THEN
  817. RETURN TRUE;
  818. END;
  819. END;
  820. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  821. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  822. END;
  823. RETURN FALSE;
  824. *)
  825. END SkipImplementation;
  826. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  827. VAR this: SyntaxTree.Modifier;
  828. BEGIN
  829. this := modifiers;
  830. WHILE this # NIL DO
  831. IF ~this.resolved THEN
  832. IF checkUse THEN
  833. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  834. ELSE
  835. this.SetExpression(ResolveExpression(this.expression));
  836. this.Resolved;
  837. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  838. END;
  839. END;
  840. this := this.nextModifier
  841. END;
  842. END CheckModifiers;
  843. (**
  844. resolve procedure type
  845. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  846. **)
  847. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  848. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  849. BEGIN
  850. IF TypeNeedsResolution(procedureType) THEN
  851. modifiers := procedureType.modifiers;
  852. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  853. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  854. procedureType.SetInterrupt(TRUE);
  855. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  856. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  857. IF useDarwinCCalls THEN (*fld*)
  858. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  859. ELSE
  860. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  861. END
  862. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  863. procedureType.SetNoReturn(TRUE);
  864. END;
  865. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  866. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  867. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  868. CheckModifiers(modifiers, TRUE);
  869. modifiers := procedureType.returnTypeModifiers;
  870. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  871. CheckModifiers(modifiers, TRUE);
  872. typeFixes.Add(procedureType,currentScope);
  873. procedureType.SetHasPointers(procedureType.isDelegate);
  874. procedureType.SetState(SyntaxTree.Resolved);
  875. END;
  876. resolvedType := ResolvedType(procedureType)
  877. END VisitProcedureType;
  878. (** check and resolve record type
  879. - check base type: must be record, math array or array-structured object type
  880. - check declarations
  881. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  882. **)
  883. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  884. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  885. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  886. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  887. hasPointers: BOOLEAN;
  888. modifiers: SyntaxTree.Modifier;
  889. value: LONGINT;
  890. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  891. BEGIN
  892. type := type.resolved;
  893. IF (type IS SyntaxTree.PointerType) &
  894. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  895. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  896. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  897. RETURN TRUE
  898. ELSE
  899. RETURN FALSE
  900. END;
  901. END IsPointerToRecord;
  902. BEGIN
  903. IF TypeNeedsResolution(x) THEN
  904. hasPointers := FALSE;
  905. modifiers := x.modifiers;
  906. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  907. CheckModifiers(modifiers, TRUE);
  908. IF x.baseType # NIL THEN
  909. position := x.baseType.position;
  910. baseType := ResolveType(x.baseType);
  911. resolved := baseType.resolved;
  912. hasPointers := hasPointers OR resolved.hasPointers;
  913. IF x.isObject THEN (* object *)
  914. ASSERT(x.pointerType # NIL);
  915. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  916. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  917. baseType := NIL
  918. ELSIF IsPointerToRecord(resolved,recordType) THEN
  919. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  920. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  921. ELSE
  922. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  923. END;
  924. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  925. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  926. ELSIF IsPointerToRecord(resolved,recordType) THEN
  927. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  928. ELSIF resolved IS SyntaxTree.RecordType THEN
  929. ELSE
  930. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  931. END;
  932. ELSE
  933. IF resolved IS SyntaxTree.RecordType THEN
  934. ELSE
  935. Error(position, Diagnostics.Invalid,"record does not extend record")
  936. END;
  937. END;
  938. x.SetBaseType(baseType);
  939. IF x.Level() > 15 THEN
  940. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  941. (* note:
  942. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  943. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  944. inheritance history of a type.
  945. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  946. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  947. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  948. *)
  949. END;
  950. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  951. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  952. END;
  953. END;
  954. Declarations(x.recordScope, FALSE);
  955. ResolveArrayStructure(x);
  956. (* computation of sizes and offsets skipped -> done in backend / system *)
  957. recordBase := x.GetBaseRecord();
  958. IF recordBase = NIL THEN numberMethods := 0
  959. ELSE numberMethods := recordBase.recordScope.numberMethods
  960. END;
  961. isRealtime := TRUE;
  962. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  963. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  964. END;
  965. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  966. WHILE symbol # NIL DO
  967. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  968. IF symbol IS SyntaxTree.Variable THEN
  969. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  970. END;
  971. IF symbol IS SyntaxTree.Procedure THEN
  972. procedure := symbol(SyntaxTree.Procedure);
  973. IF procedure.super # NIL THEN
  974. procedure.SetMethodNumber(procedure.super.methodNumber)
  975. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  976. procedure.SetMethodNumber(numberMethods);
  977. INC(numberMethods);
  978. END;
  979. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  980. Error(procedure.position, Diagnostics.Invalid,"realtime procedure in non-realtime object")
  981. END;
  982. END;
  983. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  984. Error(symbol.position, Diagnostics.Invalid,"non-realtime symbol in realtime object")
  985. END;
  986. symbol := symbol.nextSymbol;
  987. END;
  988. IF isRealtime THEN x.SetRealtime(TRUE) END;
  989. x.recordScope.SetNumberMethods(numberMethods);
  990. (* TODO: is this needed anymore? *)
  991. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  992. Error(x.position,Diagnostics.Invalid,"object extends a record")
  993. END;
  994. IF (x.typeDeclaration = NIL) THEN
  995. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  996. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  997. (*
  998. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  999. AnonymousTypeDeclaration(x,name);
  1000. *)
  1001. ELSE
  1002. AnonymousTypeDeclaration(x,"Anonymous");
  1003. END;
  1004. END;
  1005. x.SetHasPointers(hasPointers);
  1006. x.SetState(SyntaxTree.Resolved);
  1007. END;
  1008. resolvedType := ResolvedType(x);
  1009. END VisitRecordType;
  1010. (** check and resolve cell type
  1011. - check base type: must be cell
  1012. - check declarations
  1013. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1014. **)
  1015. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1016. VAR
  1017. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1018. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1019. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1020. recordBase: SyntaxTree.RecordType;
  1021. numberMethods, int: LONGINT;
  1022. real: LONGREAL;
  1023. bool: BOOLEAN;
  1024. set: SET;
  1025. v: SyntaxTree.Expression;
  1026. str: Scanner.StringType;
  1027. atype: SyntaxTree.ArrayType;
  1028. prev: SyntaxTree.Scope;
  1029. skip: BOOLEAN;
  1030. svalue: ARRAY 32 OF CHAR;
  1031. BEGIN
  1032. IF TypeNeedsResolution(x) THEN
  1033. recordBase := NIL;
  1034. IF cellsAreObjects THEN
  1035. IF x.baseType = NIL THEN
  1036. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1037. ImportModule(qualifiedIdentifier.prefix, x.position);
  1038. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1039. x.SetBaseType(ResolveType(x.baseType));
  1040. recordBase := x.GetBaseRecord();
  1041. IF recordBase = NIL THEN
  1042. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1043. END;
  1044. ELSE
  1045. x.SetBaseType(ResolveType(x.baseType));
  1046. END;
  1047. ELSE
  1048. x.SetBaseType(ResolveType(x.baseType));
  1049. END;
  1050. IF recordBase = NIL THEN numberMethods := 0
  1051. ELSE numberMethods := recordBase.recordScope.numberMethods
  1052. END;
  1053. modifier := x.modifiers;
  1054. (*IF ~x.isCellNet THEN*)
  1055. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1056. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1057. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1058. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1059. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1060. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1061. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1062. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1063. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1064. symbol := system.activeCellsCapabilities;
  1065. WHILE symbol # NIL DO
  1066. IF HasFlag(modifier, symbol.name, position) THEN END;
  1067. symbol := symbol.nextSymbol;
  1068. END;
  1069. modifier := x.modifiers;
  1070. WHILE (modifier # NIL) DO
  1071. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1072. IF modifier.expression # NIL THEN
  1073. v := ConstantExpression(modifier.expression);
  1074. property.SetValue(v);
  1075. IF IsIntegerValue(modifier.expression, int) THEN
  1076. (*property.SetValue(modifier.expression);*)
  1077. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1078. property.SetType(system.longintType);
  1079. ELSIF IsRealValue(modifier.expression, real) THEN
  1080. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1081. property.SetType(system.longrealType);
  1082. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1083. property.SetType(system.booleanType);
  1084. ELSIF IsSetValue(modifier.expression, set) THEN
  1085. property.SetType(system.setType);
  1086. ELSIF IsStringValue(modifier.expression, str) THEN
  1087. (*property.SetValue(modifier.expression);*)
  1088. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1089. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1090. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1091. property.SetType(atype);
  1092. ELSE
  1093. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1094. END;
  1095. ELSE (* flag property *)
  1096. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1097. property.SetType(system.booleanType);
  1098. END;
  1099. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1100. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1101. x.AddProperty(property);
  1102. modifier := modifier.nextModifier;
  1103. END;
  1104. CheckModifiers(modifier, FALSE);
  1105. Declarations(x.cellScope, SkipImplementation(x));
  1106. (* process parameters *)
  1107. prev := currentScope;
  1108. currentScope := x.cellScope;
  1109. parameter :=x.firstParameter;
  1110. WHILE (parameter # NIL) DO
  1111. VisitParameter(parameter);
  1112. type := parameter.type.resolved;
  1113. IF ~(type IS SyntaxTree.PortType) THEN
  1114. WHILE IsStaticArray(type, type, len) DO
  1115. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1116. END;
  1117. WHILE IsDynamicArray(type, type) DO
  1118. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1119. END;
  1120. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1121. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1122. END;
  1123. END;
  1124. parameter := parameter.nextParameter;
  1125. END;
  1126. currentScope := prev;
  1127. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1128. WHILE symbol # NIL DO
  1129. IF symbol IS SyntaxTree.Variable THEN
  1130. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1131. END;
  1132. symbol := symbol.nextSymbol;
  1133. END;
  1134. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1135. IF (x.typeDeclaration = NIL) THEN
  1136. AnonymousTypeDeclaration(x,"Anonymous");
  1137. END;
  1138. x.SetState(SyntaxTree.Resolved);
  1139. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1140. Warning(x.position, "Forbidden empty Body.");
  1141. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1142. Warning(x.position, "Non-empty body for an engine?");
  1143. END;
  1144. END;
  1145. resolvedType := ResolvedType(x);
  1146. END VisitCellType;
  1147. (* check if an object is an array-structured object type
  1148. - determine the array structure
  1149. - collect operators from top to bottom in the inheritance hierarchy
  1150. - check if LEN operator is declared
  1151. - determine number of possible index operators
  1152. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1153. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1154. *)
  1155. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1156. VAR
  1157. indexOperatorCount, i: LONGINT;
  1158. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1159. isTensor: BOOLEAN;
  1160. BEGIN
  1161. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1162. (* determine array structure *)
  1163. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1164. END;
  1165. IF recordType.HasArrayStructure() THEN
  1166. (* the object is an ASOT *)
  1167. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1168. (* reset array access operators *)
  1169. arrayAccessOperators.len := NIL;
  1170. arrayAccessOperators.generalRead := NIL;
  1171. arrayAccessOperators.generalWrite := NIL;
  1172. IF isTensor THEN
  1173. (* all operators of dimensionalities 1 to max *)
  1174. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1175. ELSE
  1176. (* all operators of certain dimensionality *)
  1177. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1178. END;
  1179. NEW(arrayAccessOperators.read, indexOperatorCount);
  1180. NEW(arrayAccessOperators.write, indexOperatorCount);
  1181. FOR i := 0 TO indexOperatorCount - 1 DO
  1182. arrayAccessOperators.read[i] := NIL;
  1183. arrayAccessOperators.write[i] := NIL
  1184. END;
  1185. (* collect access operators in the record scope *)
  1186. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1187. IF arrayAccessOperators.len = NIL THEN
  1188. (* TODO: think about making this operator optional for static array structures *)
  1189. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1190. END;
  1191. (* show error messages *)
  1192. IF isTensor THEN
  1193. (* require ARRAY [*] OF RANGE *)
  1194. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1195. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1196. ELSE
  1197. (* forbid ARRAY [*] OF RANGE *)
  1198. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1199. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1200. (* require RANGE, RANGE, ... RANGE *)
  1201. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1202. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1203. END;
  1204. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1205. ELSE
  1206. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1207. IF recordType.recordScope.firstOperator # NIL THEN
  1208. RETURN;
  1209. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1210. END
  1211. END
  1212. END ResolveArrayStructure;
  1213. (** collect array access operators in a record scope **)
  1214. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1215. VAR
  1216. baseType: SyntaxTree.Type;
  1217. operator: SyntaxTree.Operator;
  1218. isReadOperator, isGeneralOperator: BOOLEAN;
  1219. indexListSize, indexListKind, hashValue: LONGINT;
  1220. BEGIN
  1221. (* if a parent record scope exists, collect the operators there first *)
  1222. baseType := recordScope.ownerRecord.baseType;
  1223. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1224. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1225. END;
  1226. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1227. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1228. END;
  1229. (* go through all operators in the current record scope *)
  1230. operator := recordScope.firstOperator;
  1231. WHILE operator # NIL DO
  1232. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1233. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1234. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1235. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1236. IF isGeneralOperator THEN
  1237. IF isReadOperator THEN
  1238. arrayAccessOperators.generalRead := operator
  1239. ELSE
  1240. arrayAccessOperators.generalWrite := operator
  1241. END
  1242. ELSE
  1243. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1244. IF isReadOperator THEN
  1245. arrayAccessOperators.read[hashValue] := operator
  1246. ELSE
  1247. arrayAccessOperators.write[hashValue] := operator
  1248. END
  1249. END
  1250. END
  1251. ELSE
  1252. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1253. END;
  1254. operator := operator.nextOperator
  1255. END
  1256. END CollectArrayAccessOperators;
  1257. (** the hash value of an index operator **)
  1258. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1259. VAR result: LONGINT;
  1260. BEGIN
  1261. IF isTensor THEN
  1262. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1263. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1264. ELSE
  1265. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1266. END
  1267. ELSE
  1268. result := indexListKind
  1269. END;
  1270. RETURN result
  1271. END IndexOperatorHash;
  1272. (** 2 to the power of exponent **)
  1273. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1274. VAR result, i: LONGINT;
  1275. BEGIN
  1276. result := 1;
  1277. FOR i := 1 TO exponent DO
  1278. result := result * 2;
  1279. END;
  1280. RETURN result
  1281. END TwoToThePowerOf;
  1282. (** check if a LEN operator has a correct signature. i.e.
  1283. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1284. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1285. **)
  1286. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1287. VAR
  1288. procedureType: SyntaxTree.ProcedureType;
  1289. returnedArrayType: SyntaxTree.MathArrayType;
  1290. result: BOOLEAN;
  1291. BEGIN
  1292. result := FALSE;
  1293. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1294. IF (procedureType.numberParameters = 0) THEN
  1295. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1296. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1297. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1298. IF returnedArrayType.form = SyntaxTree.Open THEN
  1299. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1300. result := TRUE
  1301. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1302. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1303. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1304. result := TRUE
  1305. END
  1306. END
  1307. END
  1308. END
  1309. END;
  1310. IF result THEN
  1311. (* export symbol automatically *)
  1312. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1313. ELSE
  1314. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1315. END;
  1316. RETURN result
  1317. END CheckLenOperator;
  1318. (** check if an index operator has a correct signature. i.e.
  1319. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1320. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1321. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1322. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1323. - determine if it is a read or write operator (existance of return type)
  1324. - check index parameters
  1325. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1326. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1327. [LONGINT] -> binary 0 -> 0
  1328. [RANGE] -> binary 1 -> 1
  1329. [LONGINT, LONGINT] -> binary 00 -> 0
  1330. [LONGINT, RANGE] -> binary 01 -> 1
  1331. [RANGE, LONGINT] -> binary 10 -> 2
  1332. [RANGE, RANGE] -> binary 11 -> 3
  1333. etc.
  1334. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1335. - for read operators, check if return type matches the type of data that is read
  1336. - for write operators, check if last parameter type matches the type of data that is written
  1337. **)
  1338. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1339. VAR
  1340. elementType, otherElementType, dataType: SyntaxTree.Type;
  1341. procedureType: SyntaxTree.ProcedureType;
  1342. mathArrayType: SyntaxTree.MathArrayType;
  1343. parameter: SyntaxTree.Parameter;
  1344. parameterCount, rangeCount, i: LONGINT;
  1345. hasTypeError: BOOLEAN;
  1346. BEGIN
  1347. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1348. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1349. (* determine if it is a read or write operator *)
  1350. isReadOperator := (procedureType.returnType # NIL);
  1351. IF isReadOperator THEN
  1352. indexListSize := parameterCount;
  1353. ELSE
  1354. indexListSize := parameterCount - 1;
  1355. END;
  1356. IF indexListSize < 1 THEN
  1357. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1358. RETURN FALSE
  1359. END;
  1360. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1361. (* general operator *)
  1362. isGeneralOperator := TRUE;
  1363. IF indexListSize > 1 THEN
  1364. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1365. RETURN FALSE
  1366. END;
  1367. (* ARRAY [*] OF RANGE*)
  1368. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1369. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1370. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1371. RETURN FALSE
  1372. END;
  1373. parameter := procedureType.firstParameter.nextParameter
  1374. ELSE
  1375. (* fixed-dim. operator *)
  1376. isGeneralOperator := FALSE;
  1377. (* check number of index parameters *)
  1378. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1379. (* for tensors, limited to a certain size *)
  1380. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1381. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1382. RETURN FALSE
  1383. END
  1384. ELSE
  1385. (* for non-tensors, depends on dimensionality *)
  1386. IF indexListSize # arrayStructure.Dimensionality() THEN
  1387. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1388. RETURN FALSE
  1389. END
  1390. END;
  1391. (* go through all index parameters
  1392. - count the number of ranges
  1393. - determine the index list kind number
  1394. *)
  1395. indexListKind := 0;
  1396. rangeCount := 0;
  1397. parameter := procedureType.firstParameter;
  1398. FOR i := 1 TO indexListSize DO
  1399. indexListKind := indexListKind * 2;
  1400. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1401. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1402. INC(indexListKind);
  1403. INC(rangeCount)
  1404. ELSE
  1405. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1406. RETURN FALSE
  1407. END;
  1408. parameter := parameter.nextParameter
  1409. END;
  1410. END;
  1411. (*
  1412. - for read operators: check type of last parameter
  1413. - for write operators: check return type
  1414. *)
  1415. IF isReadOperator THEN
  1416. dataType := procedureType.returnType (* the return type *)
  1417. ELSE
  1418. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1419. END;
  1420. elementType := arrayStructure.ElementType();
  1421. hasTypeError := FALSE;
  1422. IF isGeneralOperator THEN
  1423. (* ARRAY [?] OF <Element> *)
  1424. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1425. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1426. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1427. hasTypeError := TRUE
  1428. END
  1429. ELSE
  1430. hasTypeError := TRUE
  1431. END
  1432. ELSE
  1433. IF rangeCount = 0 THEN
  1434. (* <Element> *)
  1435. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1436. ELSE
  1437. (* ARRAY [*, *, ..., *] OF <Element> *)
  1438. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1439. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1440. IF mathArrayType.IsFullyDynamic() THEN
  1441. IF mathArrayType.Dimensionality() = rangeCount THEN
  1442. otherElementType := mathArrayType.ElementType();
  1443. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1444. ELSE
  1445. hasTypeError := TRUE
  1446. END
  1447. ELSE
  1448. hasTypeError := TRUE
  1449. END
  1450. ELSE
  1451. hasTypeError := TRUE
  1452. END
  1453. END
  1454. END;
  1455. IF hasTypeError THEN
  1456. IF isReadOperator THEN
  1457. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1458. ELSE
  1459. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1460. END;
  1461. RETURN FALSE
  1462. END;
  1463. (* export symbol automatically *)
  1464. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1465. RETURN TRUE
  1466. END CheckIndexOperator;
  1467. (** resolve all pending types (late resolving).
  1468. - type fixes are resolved at the end of the declaration phase
  1469. - type fixes may imply new type fixes that are also entered at the end of the list
  1470. **)
  1471. PROCEDURE FixTypes;
  1472. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1473. BEGIN
  1474. prevScope := currentScope;
  1475. p := typeFixes.Get(currentScope);
  1476. WHILE p # NIL DO
  1477. ASSERT(currentScope # NIL);
  1478. ASSERT(p IS SyntaxTree.Type);
  1479. IF p IS SyntaxTree.PointerType THEN
  1480. FixPointerType(p(SyntaxTree.PointerType))
  1481. ELSIF p IS SyntaxTree.ProcedureType THEN
  1482. FixProcedureType(p(SyntaxTree.ProcedureType))
  1483. ELSE
  1484. HALT(100);
  1485. END;
  1486. p := typeFixes.Get(currentScope);
  1487. END;
  1488. currentScope :=prevScope;
  1489. END FixTypes;
  1490. (**
  1491. resolve type x
  1492. - if x is nil then return nil
  1493. - if x cannot be resolved then the result is invalidType else the result is x
  1494. - the resolved type is entered into x.resolved
  1495. **)
  1496. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1497. VAR prev,resolved: SyntaxTree.Type;
  1498. BEGIN
  1499. prev := resolvedType;
  1500. resolvedType := SyntaxTree.invalidType;
  1501. IF x = NIL THEN resolvedType := NIL
  1502. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1503. END;
  1504. resolved := resolvedType;
  1505. resolvedType := prev;
  1506. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1507. RETURN resolved
  1508. END ResolveType;
  1509. (*** compatibility rules ***)
  1510. (**
  1511. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1512. **)
  1513. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1514. VAR result: SyntaxTree.Type;
  1515. BEGIN
  1516. result := SyntaxTree.invalidType;
  1517. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1518. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1519. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1520. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1521. ELSE result := type.resolved
  1522. END;
  1523. RETURN result
  1524. END RegularType;
  1525. (** returns signature compatibility of procedure types this and to
  1526. - if not compatible then error is reported
  1527. - compatibility means type equality
  1528. **)
  1529. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1530. VAR result: BOOLEAN;
  1531. BEGIN
  1532. result := SameType(to,this);
  1533. IF ~result THEN
  1534. Error(position,Diagnostics.Invalid,"signature incompatible");
  1535. IF VerboseErrorMessage THEN
  1536. Printout.Info("this",this);
  1537. Printout.Info("to",to);
  1538. END;
  1539. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1540. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1541. END;
  1542. RETURN result
  1543. END SignatureCompatible;
  1544. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1545. - for var parameters compatibility means same type except for
  1546. - formal is of open array of system byte
  1547. - formal is of record type
  1548. - formal is of open array type
  1549. - formal is of open math array type
  1550. - for value parameters compatibllity means assignment compatibility except for
  1551. - formal is of open array type
  1552. if compatible the return true else report error and return false
  1553. **)
  1554. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1555. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1556. BEGIN
  1557. formalType := RegularType(formal.position,formal.type);
  1558. actualType := RegularType(actual.position,actual.type);
  1559. error := FALSE;
  1560. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1561. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1562. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1563. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1564. END;
  1565. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1566. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1567. ELSIF ~IsVariable(actual) THEN
  1568. result := FALSE; error := TRUE;
  1569. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1570. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1571. ELSE
  1572. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1573. END;
  1574. IF VerboseErrorMessage THEN
  1575. Printout.Info("actual",actual);
  1576. Printout.Info("formal",formal);
  1577. END;
  1578. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1579. result := CompatibleTo(system,actualType,formalType);
  1580. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1581. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1582. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1583. IF IsArrayStructuredObjectType(actualType) THEN
  1584. actualType := MathArrayStructureOfType(actualType)
  1585. END;
  1586. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1587. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1588. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1589. END;
  1590. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1591. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1592. END;
  1593. ELSE
  1594. result := SameType(actualType,formalType)
  1595. END
  1596. ELSE
  1597. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1598. actualType := system.characterType;
  1599. END;
  1600. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1601. result := TRUE; (* special rule for WINAPI parameters *)
  1602. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1603. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1604. ELSE
  1605. result := CompatibleTo(system,actualType,formalType);
  1606. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1607. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1608. END;
  1609. END;
  1610. END;
  1611. IF ~result & ~error THEN
  1612. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1613. IF VerboseErrorMessage THEN
  1614. Printout.Info("actual",actual);
  1615. Printout.Info("formal",formal);
  1616. END;
  1617. END;
  1618. RETURN result
  1619. END ParameterCompatible;
  1620. (** check compatibility for expressions of the form left := right
  1621. - if compatible then return true else error report and return false
  1622. - check if left is variable
  1623. - check compatiblity
  1624. **)
  1625. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1626. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1627. BEGIN
  1628. result := FALSE;
  1629. leftType := RegularType(left.position,left.type);
  1630. rightType := RegularType(right.position,right.type);
  1631. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1632. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1633. END;
  1634. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1635. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1636. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1637. (* error already handled *)
  1638. result := TRUE;
  1639. ELSIF ~IsVariable(left) THEN
  1640. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1641. IF VerboseErrorMessage THEN
  1642. Printout.Info("left",left);
  1643. Printout.Info("right",right);
  1644. END;
  1645. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1646. result := TRUE;
  1647. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1648. result := TRUE
  1649. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1650. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1651. IF VerboseErrorMessage THEN
  1652. Printout.Info("left",left);
  1653. Printout.Info("right",right);
  1654. END;
  1655. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1656. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1657. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1658. ELSE
  1659. result := TRUE
  1660. END;
  1661. RETURN result
  1662. END AssignmentCompatible;
  1663. (*** values ***)
  1664. (** check and resolve integer value **)
  1665. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1666. VAR hugeint: HUGEINT;
  1667. BEGIN
  1668. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1669. value.SetType(Global.GetIntegerType(system,hugeint));
  1670. resolvedExpression := value
  1671. END VisitIntegerValue;
  1672. (** check and resolve real value **)
  1673. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1674. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1675. BEGIN
  1676. subtype := value(SyntaxTree.RealValue).subtype;
  1677. IF subtype = Scanner.Real THEN
  1678. type := system.realType
  1679. ELSIF subtype = Scanner.Longreal THEN
  1680. type := system.longrealType
  1681. ELSE
  1682. HALT(100)
  1683. END;
  1684. value.SetType(type);
  1685. resolvedExpression := value
  1686. END VisitRealValue;
  1687. (** check and resolve complex value **)
  1688. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1689. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1690. BEGIN
  1691. subtype := value(SyntaxTree.ComplexValue).subtype;
  1692. IF subtype = Scanner.Real THEN
  1693. type := system.complexType
  1694. ELSIF subtype = Scanner.Longreal THEN
  1695. type := system.longcomplexType
  1696. ELSE
  1697. HALT(100)
  1698. END;
  1699. value.SetType(type);
  1700. resolvedExpression := value
  1701. END VisitComplexValue;
  1702. (** check and resolve set value **)
  1703. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1704. BEGIN
  1705. value.SetType(system.setType);
  1706. resolvedExpression := value
  1707. END VisitSetValue;
  1708. (** check and resolve set value **)
  1709. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1710. BEGIN
  1711. value.SetType(SyntaxTree.invalidType);
  1712. resolvedExpression := value
  1713. END VisitMathArrayValue;
  1714. (** check and resolve boolean value **)
  1715. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1716. BEGIN
  1717. value.SetType(system.booleanType);
  1718. resolvedExpression := value
  1719. END VisitBooleanValue;
  1720. (** check and resolve string value **)
  1721. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1722. BEGIN
  1723. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1724. resolvedExpression := value
  1725. END VisitStringValue;
  1726. (** check and resolve character value **)
  1727. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1728. BEGIN
  1729. value.SetType(system.characterType);
  1730. resolvedExpression := value
  1731. END VisitCharacterValue;
  1732. (** check and resolve nil value **)
  1733. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1734. BEGIN
  1735. value.SetType(system.nilType);
  1736. resolvedExpression := value
  1737. END VisitNilValue;
  1738. (** check and resolve enumerator value **)
  1739. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1740. BEGIN
  1741. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1742. ASSERT(value.type # NIL);
  1743. resolvedExpression := value
  1744. END VisitEnumerationValue;
  1745. (*** expressions ***)
  1746. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1747. - check all elements on integer type
  1748. - if element range is constant, then check lower and upper bound
  1749. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1750. if an error occurs then report error and return invalidExpression
  1751. **)
  1752. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1753. VAR
  1754. i: LONGINT;
  1755. element: SyntaxTree.Expression;
  1756. constant: BOOLEAN;
  1757. elements: SyntaxTree.ExpressionList;
  1758. s: SET;
  1759. result: SyntaxTree.Expression;
  1760. value: SyntaxTree.Value;
  1761. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1762. VAR
  1763. left, right: SyntaxTree.Expression;
  1764. elementResult: SyntaxTree.Expression;
  1765. leftInteger, rightInteger, temp: LONGINT;
  1766. BEGIN
  1767. (* set context of range *)
  1768. IF element IS SyntaxTree.RangeExpression THEN
  1769. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1770. END;
  1771. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1772. IF elementResult = SyntaxTree.invalidExpression THEN
  1773. (* error already reported *)
  1774. constant := FALSE
  1775. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1776. (* the element is a range expression *)
  1777. (* extract left and right hand side of range *)
  1778. left := elementResult(SyntaxTree.RangeExpression).first;
  1779. right := elementResult(SyntaxTree.RangeExpression).last;
  1780. (* guaranteed by VisitRangeExpression: *)
  1781. ASSERT((left # NIL) & (right # NIL));
  1782. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1783. ELSE
  1784. (* the element is not a range expression *)
  1785. (* check type and add conversion if needed *)
  1786. IF IsIntegerType(elementResult.type.resolved) THEN
  1787. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1788. ELSE
  1789. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1790. elementResult := SyntaxTree.invalidExpression;
  1791. constant := FALSE
  1792. END;
  1793. left := elementResult;
  1794. right := elementResult
  1795. END;
  1796. IF elementResult # SyntaxTree.invalidExpression THEN
  1797. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1798. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1799. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1800. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1801. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1802. END
  1803. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1804. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1805. ELSE
  1806. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1807. s := {};
  1808. ELSE
  1809. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1810. IF leftInteger < 0 THEN leftInteger := 0 END;
  1811. (*!!!!!!!!! this is a hack !!!!!!! *)
  1812. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1813. extends the range x..31 to x..63 !!!!!! *)
  1814. s := s + {leftInteger..rightInteger};
  1815. END;
  1816. END;
  1817. ELSE
  1818. constant := FALSE;
  1819. END
  1820. END;
  1821. RETURN elementResult
  1822. END CheckElement;
  1823. BEGIN
  1824. result := set; constant := TRUE; s := {}; elements := set.elements;
  1825. IF elements # NIL THEN
  1826. FOR i := 0 TO elements.Length()-1 DO
  1827. element := elements.GetExpression(i);
  1828. element := CheckElement(element);
  1829. IF element = SyntaxTree.invalidExpression THEN
  1830. result := SyntaxTree.invalidExpression
  1831. END;
  1832. elements.SetExpression(i,element);
  1833. END;
  1834. END;
  1835. IF constant THEN
  1836. value := SyntaxTree.NewSetValue(set.position,s);
  1837. value.SetType(system.setType);
  1838. result.SetResolved(value);
  1839. END;
  1840. (* optimization possible
  1841. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1842. left this to the programmer...
  1843. *)
  1844. result.SetType(system.setType);
  1845. resolvedExpression := result;
  1846. END VisitSet;
  1847. (*
  1848. old variant: quite generic but needs better conversion handling, do this?
  1849. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1850. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1851. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1852. BEGIN
  1853. type := NIL;
  1854. numberElements := x.elements.Length();
  1855. FOR i := 0 TO numberElements-1 DO
  1856. expression := x.elements.GetExpression(i);
  1857. position := expression.position;
  1858. expression := ResolveExpression(x.elements.GetExpression(i));
  1859. x.elements.SetExpression(i,de);
  1860. IF type = NIL THEN
  1861. type := expression.type;
  1862. ELSIF CompatibleTo(system,expression.type,type) THEN
  1863. (* ok *)
  1864. ELSIF CompatibleTo(system,type,expression.type) THEN
  1865. type := expression.type
  1866. ELSE
  1867. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1868. type := SyntaxTree.invalidType;
  1869. END;
  1870. END;
  1871. isValue := TRUE;
  1872. FOR i := 0 TO numberElements-1 DO
  1873. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1874. x.elements.SetExpression(i,expression);
  1875. isValue := isValue & (expression.resolved # NIL);
  1876. END;
  1877. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1878. arrayType.SetArrayBase(type);
  1879. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1880. IF isValue THEN
  1881. value := SyntaxTree.NewMathArrayValue(position);
  1882. value.SetElements(x.elements);
  1883. x.SetResolved(value);
  1884. END;
  1885. x.SetType(arrayType);
  1886. resolvedExpression := x;
  1887. END VisitMathArrayExpression;
  1888. *)
  1889. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1890. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1891. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1892. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1893. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1894. BEGIN
  1895. numberElements := x.elements.Length();
  1896. FOR i := 0 TO numberElements-1 DO
  1897. expression := x.elements.GetExpression(i);
  1898. IF expression IS SyntaxTree.MathArrayExpression THEN
  1899. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1900. ELSE
  1901. position := expression.position;
  1902. expression := ResolveExpression(x.elements.GetExpression(i));
  1903. x.elements.SetExpression(i,expression);
  1904. IF type = NIL THEN
  1905. type := expression.type;
  1906. ELSIF CompatibleTo(system,expression.type,type) THEN
  1907. (* ok *)
  1908. ELSIF CompatibleTo(system,type,expression.type) THEN
  1909. type := expression.type
  1910. ELSE
  1911. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1912. type := SyntaxTree.invalidType;
  1913. END;
  1914. END;
  1915. END;
  1916. END RecursivelyFindType;
  1917. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1918. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1919. BEGIN
  1920. numberElements := x.elements.Length();
  1921. FOR i := 0 TO numberElements-1 DO
  1922. expression := x.elements.GetExpression(i);
  1923. IF expression IS SyntaxTree.MathArrayExpression THEN
  1924. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1925. ELSE
  1926. position := expression.position;
  1927. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1928. x.elements.SetExpression(i,expression);
  1929. isValue := isValue & (expression.resolved # NIL);
  1930. END;
  1931. END;
  1932. END RecursivelySetExpression;
  1933. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1934. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1935. arrayType: SyntaxTree.MathArrayType;
  1936. BEGIN
  1937. numberElements := x.elements.Length();
  1938. baseType := NIL;
  1939. gsize := 0;
  1940. FOR i := 0 TO numberElements-1 DO
  1941. expression := x.elements.GetExpression(i);
  1942. IF expression IS SyntaxTree.MathArrayExpression THEN
  1943. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1944. IF i=0 THEN
  1945. gsize := size;
  1946. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1947. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1948. ELSE expression.SetType(baseType)
  1949. END;
  1950. ELSIF baseType = NIL THEN baseType := type;
  1951. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1952. END;
  1953. END;
  1954. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1955. arrayType.SetArrayBase(baseType);
  1956. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1957. RETURN ResolveType(arrayType);
  1958. END RecursivelySetType;
  1959. BEGIN
  1960. type := NIL;
  1961. RecursivelyFindType(x);
  1962. isValue := TRUE;
  1963. RecursivelySetExpression(x);
  1964. arrayType := RecursivelySetType(x);
  1965. x.SetType(arrayType);
  1966. IF isValue THEN
  1967. value := SyntaxTree.NewMathArrayValue(x.position);
  1968. value.SetArray(x);
  1969. x.SetResolved(value);
  1970. value.SetType(arrayType);
  1971. END;
  1972. x.SetType(arrayType);
  1973. resolvedExpression := x;
  1974. END VisitMathArrayExpression;
  1975. (** check and resolve unary expression **)
  1976. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1977. VAR
  1978. left: SyntaxTree.Expression;
  1979. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1980. bool: BOOLEAN;
  1981. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1982. value: SyntaxTree.Value;
  1983. BEGIN
  1984. type := SyntaxTree.invalidType;
  1985. left := ResolveExpression(unaryExpression.left);
  1986. unaryExpression.SetLeft(left);
  1987. operator := unaryExpression.operator;
  1988. result := unaryExpression;
  1989. IF ~system.operatorDefined[operator] THEN
  1990. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1991. RETURN
  1992. ELSIF left.type = NIL THEN
  1993. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1994. resolvedExpression := SyntaxTree.invalidExpression;
  1995. RETURN
  1996. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1997. RETURN
  1998. END;
  1999. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2000. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2001. END;
  2002. IF operatorCall # NIL THEN
  2003. result := operatorCall;
  2004. type := operatorCall.type;
  2005. (* admissible operators
  2006. Minus number, set
  2007. Not boolean
  2008. *)
  2009. ELSE
  2010. CASE unaryExpression.operator OF
  2011. |Scanner.Minus:
  2012. IF IsIntegerType(left.type.resolved) THEN
  2013. IF left.resolved # NIL THEN
  2014. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2015. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2016. result.SetResolved(value);
  2017. type := Global.GetIntegerType(system,int);
  2018. value.SetType(type);
  2019. ELSE
  2020. type := left.type
  2021. END
  2022. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2023. IF IsRealValue(left,real) THEN
  2024. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2025. result.SetResolved(value);
  2026. type := left.type;
  2027. value.SetType(type);
  2028. ELSE
  2029. type := left.type;
  2030. END;
  2031. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2032. IF IsSetValue(left,set) THEN
  2033. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2034. result.SetResolved(value);
  2035. type := left.type;
  2036. value.SetType(type);
  2037. ELSE
  2038. type := left.type;
  2039. END;
  2040. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2041. IF IsComplexValue(left, real, imaginary) THEN
  2042. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2043. result.SetResolved(value);
  2044. type := left.type;
  2045. value.SetType(type);
  2046. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2047. ELSE
  2048. type := left.type;
  2049. END
  2050. ELSE
  2051. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2052. END;
  2053. |Scanner.Not:
  2054. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2055. IF IsBooleanValue(left,bool) THEN
  2056. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2057. result.SetResolved(value);
  2058. type := system.booleanType;
  2059. value.SetType(type);
  2060. ELSE
  2061. type := system.booleanType;
  2062. END;
  2063. ELSE
  2064. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2065. END;
  2066. |Scanner.Plus:
  2067. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2068. result := left; type := left.type;
  2069. ELSE
  2070. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2071. END;
  2072. (* ADDRESS OF *)
  2073. |Scanner.Address:
  2074. IF HasAddress(left) THEN
  2075. type := system.addressType;
  2076. ELSE
  2077. type := SyntaxTree.invalidType;
  2078. Error(left.position,Diagnostics.Invalid,"has no address");
  2079. Printout.Info("par", left);
  2080. END;
  2081. (* SIZE OF *)
  2082. |Scanner.Size:
  2083. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2084. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2085. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2086. value := SyntaxTree.NewIntegerValue(left.position, int);
  2087. result.SetResolved(value);
  2088. type := Global.GetIntegerType(system,int);
  2089. value.SetType(type)
  2090. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2091. ELSE
  2092. (* for variables, system sizeof could represent the physically occupied size
  2093. determined via the type descriptor, implement that ? *)
  2094. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2095. END
  2096. (* ALIAS OF *)
  2097. |Scanner.Alias:
  2098. type := left.type.resolved;
  2099. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2100. type := SyntaxTree.invalidType;
  2101. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2102. END;
  2103. ELSE
  2104. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2105. END;
  2106. END;
  2107. result.SetType(type);
  2108. resolvedExpression := result
  2109. END VisitUnaryExpression;
  2110. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2111. VAR
  2112. result: SyntaxTree.Expression;
  2113. array: SyntaxTree.MathArrayExpression;
  2114. value: SyntaxTree.MathArrayValue;
  2115. isValue: BOOLEAN;
  2116. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2117. BEGIN
  2118. type := type.resolved;
  2119. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2120. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2121. END;
  2122. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2123. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2124. END;
  2125. RETURN type
  2126. END BaseType;
  2127. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2128. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2129. BEGIN
  2130. numberElements := x.elements.Length();
  2131. FOR i := 0 TO numberElements-1 DO
  2132. expression := x.elements.GetExpression(i);
  2133. IF expression IS SyntaxTree.MathArrayExpression THEN
  2134. array := SyntaxTree.NewMathArrayExpression(position);
  2135. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2136. expression := array;
  2137. ELSE
  2138. position := expression.position;
  2139. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2140. isValue := isValue & (expression.resolved # NIL);
  2141. END;
  2142. to.elements.AddExpression(expression);
  2143. END;
  2144. END RecursivelyConvert;
  2145. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2146. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2147. arrayType: SyntaxTree.MathArrayType;
  2148. BEGIN
  2149. numberElements := x.elements.Length();
  2150. baseType := NIL;
  2151. gsize := 0;
  2152. FOR i := 0 TO numberElements-1 DO
  2153. expression := x.elements.GetExpression(i);
  2154. IF expression IS SyntaxTree.MathArrayExpression THEN
  2155. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2156. IF i=0 THEN
  2157. gsize := size;
  2158. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2159. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2160. ELSE expression.SetType(baseType)
  2161. END;
  2162. ELSIF baseType = NIL THEN baseType := type;
  2163. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2164. END;
  2165. END;
  2166. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2167. arrayType.SetArrayBase(baseType);
  2168. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2169. RETURN ResolveType(arrayType);
  2170. END RecursivelySetType;
  2171. BEGIN
  2172. result := SyntaxTree.invalidExpression;
  2173. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2174. result := expression (* do not convert *)
  2175. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2176. isValue := TRUE;
  2177. type := BaseType(type);
  2178. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2179. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2180. value := SyntaxTree.NewMathArrayValue(array.position);
  2181. value.SetArray(array);
  2182. value.SetType(RecursivelySetType(array));
  2183. result := value;
  2184. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2185. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2186. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2187. IF result = NIL THEN
  2188. result := SyntaxTree.invalidExpression;
  2189. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2190. IF VerboseErrorMessage THEN
  2191. Printout.Info("expression",expression);
  2192. Printout.Info("type",type);
  2193. END;
  2194. END;
  2195. END;
  2196. RETURN result
  2197. END MathArrayConversion;
  2198. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2199. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2200. BEGIN
  2201. result := expression; type := type.resolved;
  2202. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2203. (* skip, no conversion *)
  2204. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2205. int := expression(SyntaxTree.IntegerValue).hvalue;
  2206. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2207. int := Global.ConvertSigned(int,system.SizeOf(type));
  2208. result := SyntaxTree.NewIntegerValue(position,int);
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2211. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2212. result := SyntaxTree.NewIntegerValue(position,int);
  2213. result.SetType(type);
  2214. ELSIF (type IS SyntaxTree.FloatType) THEN
  2215. result := SyntaxTree.NewRealValue(expression.position,int);
  2216. result.SetType(type);
  2217. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2218. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2219. result.SetType(type);
  2220. ELSIF (type IS SyntaxTree.SetType) THEN
  2221. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2222. result.SetType(type);
  2223. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2224. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2225. result.SetType(type);
  2226. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2227. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2228. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2229. END;
  2230. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2231. result.SetType(type);
  2232. ELSIF (type IS SyntaxTree.PortType) THEN
  2233. result := ConvertValue(position, expression, system.integerType);
  2234. ELSE
  2235. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2236. result := SyntaxTree.invalidExpression;
  2237. IF VerboseErrorMessage THEN
  2238. Printout.Info("expression",expression);
  2239. Printout.Info("type",type);
  2240. END;
  2241. END;
  2242. ELSIF IsRealValue(expression,real) THEN
  2243. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2244. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2245. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2248. int := ENTIERH(real);
  2249. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.FloatType) THEN
  2252. result := SyntaxTree.NewRealValue(position,real);
  2253. result.SetType(type);
  2254. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2255. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2256. result.SetType(type);
  2257. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2258. ELSIF (type IS SyntaxTree.PortType) THEN
  2259. result := ConvertValue(position, expression, system.integerType);
  2260. ELSE
  2261. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2262. result := SyntaxTree.invalidExpression;
  2263. END
  2264. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2265. IF (type IS SyntaxTree.ComplexType) THEN
  2266. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2267. result.SetType(type);
  2268. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2269. ELSE
  2270. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2271. result := SyntaxTree.invalidExpression;
  2272. END
  2273. ELSIF IsSetValue(expression,set) THEN
  2274. IF (type IS SyntaxTree.IntegerType) THEN
  2275. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2276. result.SetType(type);
  2277. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2278. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2279. result.SetType(type);
  2280. ELSIF (type IS SyntaxTree.PortType) THEN
  2281. result := ConvertValue(position, expression, system.integerType);
  2282. ELSE
  2283. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2284. result := SyntaxTree.invalidExpression;
  2285. END;
  2286. ELSIF IsStringValue(expression,string) THEN
  2287. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2288. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2289. result.SetType(type);
  2290. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2291. ELSE
  2292. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2293. result := SyntaxTree.invalidExpression;
  2294. END;
  2295. ELSIF IsCharacterValue(expression,char) THEN
  2296. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2297. string[0] := char; string[1] := 0X;
  2298. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2299. result := SyntaxTree.NewStringValue(expression.position,string);
  2300. result.SetType(type);
  2301. ELSIF (type IS SyntaxTree.ByteType) THEN
  2302. (* do not simply set the new type as this could invalidate types of constants *)
  2303. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2304. result.SetType(type)
  2305. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2306. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2307. result.SetType(type);
  2308. ELSIF (type IS SyntaxTree.SetType) THEN
  2309. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2310. result.SetType(type);
  2311. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2312. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2313. result.SetType(type);
  2314. ELSIF (type IS SyntaxTree.PortType) THEN
  2315. result := ConvertValue(position, expression, system.integerType);
  2316. ELSE
  2317. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2318. result := SyntaxTree.invalidExpression;
  2319. END;
  2320. ELSIF expression IS SyntaxTree.NilValue THEN
  2321. IF type IS SyntaxTree.AddressType THEN
  2322. result := SyntaxTree.NewIntegerValue(position,0);
  2323. result.SetType(type);
  2324. ELSE
  2325. result := expression;
  2326. END;
  2327. (* nothing to be done *)
  2328. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2329. result := MathArrayConversion(position, expression,type);
  2330. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2331. int := expression(SyntaxTree.EnumerationValue).value;
  2332. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2333. int := Global.ConvertSigned(int,system.SizeOf(type));
  2334. result := SyntaxTree.NewIntegerValue(position,int);
  2335. result.SetType(type);
  2336. ELSE
  2337. result := expression;
  2338. END;
  2339. (* nothing to be done *)
  2340. ELSE
  2341. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2342. IF VerboseErrorMessage THEN
  2343. Printout.Info("expression",expression);
  2344. Printout.Info("type",type);
  2345. END;
  2346. result := SyntaxTree.invalidExpression;
  2347. END;
  2348. RETURN result
  2349. END ConvertValue;
  2350. (**
  2351. return a conversion of an expression to a given type
  2352. - if expression is already of same type then return expression
  2353. - if incompatible conversion then report error and return invalidExpression
  2354. **)
  2355. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2356. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2357. BEGIN
  2358. type := type.resolved;
  2359. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2360. result := expression;
  2361. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2362. ELSIF expression = NIL THEN (* NIL expression *)
  2363. ELSIF expression.type = NIL THEN
  2364. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2365. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2366. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2367. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2368. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2369. ELSIF expression.resolved # NIL THEN (* value *)
  2370. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2371. IF value IS SyntaxTree.Value THEN
  2372. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2373. result.SetResolved(value(SyntaxTree.Value));
  2374. result.SetType(value.type);
  2375. ELSE
  2376. result := value
  2377. END;
  2378. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2379. expressionList := SyntaxTree.NewExpressionList();
  2380. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2381. typeDeclaration.SetDeclaredType(type);
  2382. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2383. typeSymbol.SetType(typeDeclaration.type);
  2384. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2385. expressionList.AddExpression(expression);
  2386. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2387. result.SetType(type);
  2388. ELSIF IsArrayStructuredObjectType(type) THEN
  2389. (* no type can be converted to an array-structured object type *)
  2390. HALT(100)
  2391. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2392. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2393. result := MathArrayConversion(position, expression,type);
  2394. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2395. expression := ConvertToMathArray(expression);
  2396. type := MathArrayStructureOfType(type);
  2397. result := MathArrayConversion(position, expression, type)
  2398. ELSE
  2399. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2400. END;
  2401. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2402. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2403. OR ~(type IS SyntaxTree.ArrayType) THEN
  2404. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2405. END;
  2406. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2407. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2408. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2409. (*skip, no conversion*)
  2410. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2411. (* skip, no conversion *)
  2412. ELSE
  2413. ASSERT(~(type IS SyntaxTree.RangeType));
  2414. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2415. ASSERT(type # NIL);
  2416. END;
  2417. RETURN result
  2418. END NewConversion;
  2419. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2420. BEGIN
  2421. IF CompatibleTo(system,expression.type, type) THEN
  2422. RETURN NewConversion(position, expression, type, NIL);
  2423. ELSE
  2424. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2425. RETURN SyntaxTree.invalidExpression
  2426. END;
  2427. END CompatibleConversion;
  2428. (**
  2429. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2430. **)
  2431. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2432. VAR leftType,rightType: SyntaxTree.Type;
  2433. BEGIN
  2434. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2435. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2436. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2437. (* error already handled *)
  2438. ELSE
  2439. leftType := left.type.resolved; rightType := right.type.resolved;
  2440. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2441. right := NewConversion(right.position, right, leftType, NIL);
  2442. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2443. left := NewConversion(left.position,left,rightType,NIL);
  2444. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2445. right := NewConversion(right.position, right, leftType, NIL);
  2446. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2447. left := NewConversion(left.position,left,rightType,NIL);
  2448. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2449. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2450. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2451. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2452. ELSIF
  2453. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2454. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2455. (* must be the case LONGREAL / COMPLEX ) *)
  2456. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2457. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2458. ELSE
  2459. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2460. END;
  2461. END;
  2462. END ConvertOperands;
  2463. (** find and return best operator matching to parameter list (nil, if none)
  2464. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2465. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2466. **)
  2467. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2468. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2469. identifier: SyntaxTree.Identifier;
  2470. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2471. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2472. BEGIN
  2473. operator := scope.firstOperator;
  2474. WHILE(operator # NIL) DO
  2475. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2476. procedureType := operator.type(SyntaxTree.ProcedureType);
  2477. distance := Distance(system, procedureType,actualParameters);
  2478. IF (distance < Infinity) THEN
  2479. IF returnType # NIL THEN
  2480. IF procedureType.returnType = NIL THEN
  2481. distance := Infinity
  2482. ELSE
  2483. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2484. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2485. END;
  2486. END;
  2487. END;
  2488. (*
  2489. IF distance < Infinity THEN
  2490. TRACE(distance, operator);
  2491. Printout.Info("potential operator",operator);
  2492. ELSE
  2493. Printout.Info("operator not possible",operator);
  2494. END;
  2495. *)
  2496. IF distance < bestDistance THEN
  2497. bestDistance := distance;
  2498. bestOperator := operator;
  2499. END;
  2500. END;
  2501. operator := operator.nextOperator;
  2502. END;
  2503. (*
  2504. Printout.Info("taken operator",bestOperator);
  2505. *)
  2506. END FindInScope;
  2507. BEGIN
  2508. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2509. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2510. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2511. import := currentScope.ownerModule.moduleScope.firstImport;
  2512. WHILE (bestDistance > 0) & (import # NIL) DO
  2513. IF import.module # NIL THEN
  2514. identifier := Global.GetIdentifier(operator,import.module.case);
  2515. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2516. END;
  2517. import := import.nextImport;
  2518. END;
  2519. RETURN bestOperator
  2520. END FindOperator;
  2521. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2522. BEGIN
  2523. currentScope := scope;
  2524. END SetCurrentScope;
  2525. (**
  2526. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2527. - handle LEN and DIM operator for array-structured object types
  2528. - find operator, if found then
  2529. - if in other module then add import designator
  2530. - create symbol designator for operator
  2531. - if error then return invalidExpression, if no operator then return NIL
  2532. **)
  2533. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2534. VAR
  2535. operator: SyntaxTree.Operator;
  2536. import: SyntaxTree.Import;
  2537. expression, result: SyntaxTree.Expression;
  2538. designator: SyntaxTree.Designator;
  2539. actualParameters, tempList: SyntaxTree.ExpressionList;
  2540. recordType: SyntaxTree.RecordType;
  2541. castReturnType : SyntaxTree.MathArrayType;
  2542. BEGIN
  2543. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2544. result := SyntaxTree.invalidExpression
  2545. ELSIF leftExpression = NIL THEN
  2546. result := NIL
  2547. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2548. (* LEN or DIM operator on array-structured object type *)
  2549. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2550. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2551. IF recordType.arrayAccessOperators.len = NIL THEN
  2552. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2553. result := SyntaxTree.invalidExpression
  2554. ELSE
  2555. ASSERT(leftExpression IS SyntaxTree.Designator);
  2556. designator := leftExpression(SyntaxTree.Designator);
  2557. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2558. ASSERT(expression IS SyntaxTree.Designator);
  2559. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2560. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2561. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2562. result := designator
  2563. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2564. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2565. tempList := SyntaxTree.NewExpressionList();
  2566. tempList.AddExpression(rightExpression);
  2567. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2568. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2569. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2570. tempList := SyntaxTree.NewExpressionList();
  2571. tempList.AddExpression(designator);
  2572. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2573. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2574. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2575. END
  2576. END;
  2577. ELSE
  2578. (* import OCArrayBase if needed *)
  2579. IF ~arrayBaseImported THEN
  2580. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2581. (* operators on complex numbers *)
  2582. ImportModule(Global.ArrayBaseName,position);
  2583. arrayBaseImported := TRUE;
  2584. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2585. IF op = Global.Dim THEN
  2586. (* not existing in OCArrayBase *)
  2587. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2588. (* not existing in OCArrayBase *)
  2589. ELSE
  2590. ImportModule(Global.ArrayBaseName,position);
  2591. arrayBaseImported := TRUE;
  2592. END
  2593. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2594. ImportModule(Global.ArrayBaseName,position);
  2595. arrayBaseImported := TRUE
  2596. END;
  2597. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2598. (* LEN(RANGE) *)
  2599. ImportModule(Global.ArrayBaseName,position);
  2600. arrayBaseImported := TRUE;
  2601. END;
  2602. END;
  2603. actualParameters := SyntaxTree.NewExpressionList();
  2604. actualParameters.AddExpression(leftExpression);
  2605. IF rightExpression # NIL THEN
  2606. actualParameters.AddExpression(rightExpression)
  2607. END;
  2608. operator := FindOperator(system,op,actualParameters,resultType);
  2609. IF operator # NIL THEN
  2610. designator := NIL;
  2611. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2612. import := currentScope.ownerModule.moduleScope.firstImport;
  2613. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2614. import := import.nextImport;
  2615. END;
  2616. expression := NewSymbolDesignator(position,NIL,import);
  2617. designator := expression(SyntaxTree.Designator);
  2618. END;
  2619. expression := NewSymbolDesignator(position,designator,operator);
  2620. designator := expression(SyntaxTree.Designator);
  2621. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2622. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2623. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2624. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2625. result.SetType(castReturnType);
  2626. END;
  2627. ELSE
  2628. result := NIL;
  2629. END;
  2630. END;
  2631. RETURN result
  2632. END NewOperatorCall;
  2633. (** check and resolve binary expression **)
  2634. (*! clean up *)
  2635. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2636. VAR left,right,result: SyntaxTree.Expression;
  2637. leftType, rightType: SyntaxTree.Type;
  2638. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2639. cl,cr: CHAR;
  2640. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2641. type: SyntaxTree.Type;
  2642. value: SyntaxTree.Value;
  2643. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2644. integerConstantFolding: BOOLEAN;
  2645. list: SyntaxTree.ExpressionList;
  2646. PROCEDURE NewBool(v: BOOLEAN);
  2647. BEGIN
  2648. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2649. value.SetType(system.booleanType);
  2650. result.SetResolved(value);
  2651. type := system.booleanType
  2652. END NewBool;
  2653. PROCEDURE NewSet(v: SET);
  2654. BEGIN
  2655. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2656. value.SetType(system.setType);
  2657. result.SetResolved(value);
  2658. type := system.setType;
  2659. END NewSet;
  2660. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2661. BEGIN
  2662. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2663. (* type cast to "larger" type only if the value is still in the range *)
  2664. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2665. value.SetType(t);
  2666. END;
  2667. result.SetResolved(value);
  2668. type := value.type;
  2669. END NewInteger;
  2670. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2671. BEGIN
  2672. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2673. value.SetType(t);
  2674. result.SetResolved(value);
  2675. type := t;
  2676. END NewReal;
  2677. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2678. BEGIN
  2679. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2680. value.SetType(t);
  2681. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2682. result.SetResolved(value);
  2683. type := t;
  2684. END NewComplex;
  2685. BEGIN
  2686. type := SyntaxTree.invalidType;
  2687. left := ResolveExpression(binaryExpression.left);
  2688. right := ResolveExpression(binaryExpression.right);
  2689. binaryExpression.SetLeft(left);
  2690. binaryExpression.SetRight(right);
  2691. result := binaryExpression;
  2692. operator := binaryExpression.operator;
  2693. IF ~system.operatorDefined[operator] THEN
  2694. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2695. result := SyntaxTree.invalidExpression;
  2696. RETURN
  2697. END;
  2698. IF left.type = NIL THEN
  2699. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2700. result := SyntaxTree.invalidExpression;
  2701. RETURN;
  2702. END;
  2703. IF right.type = NIL THEN
  2704. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2705. result := SyntaxTree.invalidExpression;
  2706. RETURN;
  2707. END;
  2708. leftType := left.type.resolved; rightType := right.type.resolved;
  2709. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2710. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2711. END;
  2712. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2713. list := SyntaxTree.NewExpressionList();
  2714. list.AddExpression(right);
  2715. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2716. END;
  2717. IF operatorCall # NIL THEN
  2718. result := operatorCall;
  2719. type := operatorCall.type;
  2720. (* admissible operators:
  2721. Times, Plus, Minus numeric numeric numeric
  2722. set set set
  2723. Slash numeric numeric real /complex
  2724. set set set
  2725. Div , Mod integer integer integer
  2726. And, Or bool bool bool
  2727. Equal, Unequal basic basic bool
  2728. pointer pointer bool
  2729. object object bool
  2730. record record bool
  2731. string string bool
  2732. enumerator enumerator bool
  2733. Less, LessEqual,
  2734. Greater, GreaterEqual integer/real integer/real bool
  2735. enumerator enumerator bool
  2736. In integer set bool
  2737. Is pointer type bool
  2738. object type bool
  2739. record type bool
  2740. Upto: special abbreviation for a..b
  2741. *)
  2742. ELSIF (left.type = NIL) THEN
  2743. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2744. D.Str("nil type in "); D.Type(left); D.Ln;
  2745. result := SyntaxTree.invalidExpression;
  2746. ELSIF (right.type = NIL) THEN
  2747. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2748. result := SyntaxTree.invalidExpression;
  2749. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2750. result := SyntaxTree.invalidExpression;
  2751. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2752. HALT(100);
  2753. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2754. type := system.booleanType;
  2755. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2756. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2757. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2758. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2759. IF VerboseErrorMessage THEN
  2760. Printout.Info("left",left);
  2761. Printout.Info("right",right);
  2762. END;
  2763. ELSIF IsUnsafePointer(left.type) THEN
  2764. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2765. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2766. NewBool(TRUE)
  2767. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2768. NewBool(TRUE);
  2769. ELSIF IsUnextensibleRecord(left) THEN
  2770. NewBool(FALSE)
  2771. END
  2772. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2773. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2774. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2775. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2776. ELSIF operator = Scanner.In THEN (* left IN right *)
  2777. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2778. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2779. NewBool(il IN sr);
  2780. ELSE
  2781. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2782. left := NewConversion(left.position, left, system.longintType,NIL);
  2783. binaryExpression.SetLeft(left)
  2784. END;
  2785. type := system.booleanType;
  2786. END
  2787. ELSE
  2788. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2789. END
  2790. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2791. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2792. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2793. END;
  2794. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2795. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2796. END
  2797. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2798. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2799. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2801. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2802. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2803. ConvertOperands(left, right);
  2804. binaryExpression.SetLeft(left);
  2805. binaryExpression.SetRight(right);
  2806. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2807. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2808. END;
  2809. type := system.booleanType;
  2810. ELSE
  2811. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2812. END
  2813. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2814. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2815. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2816. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2817. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2818. CASE operator OF
  2819. |Scanner.Equal: NewBool(strl^=strr^);
  2820. |Scanner.Unequal:NewBool(strl^#strr^);
  2821. |Scanner.Less: NewBool(strl^<strr^);
  2822. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2823. |Scanner.Greater: NewBool(strl^>strr^);
  2824. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2825. ELSE
  2826. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2827. END;
  2828. END;
  2829. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2830. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2831. type := system.booleanType
  2832. ELSE
  2833. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2834. END;
  2835. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2836. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2837. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2838. type := system.booleanType;
  2839. ELSE
  2840. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2841. END
  2842. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2843. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2844. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2845. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2846. type := system.booleanType
  2847. ELSE
  2848. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2849. END;
  2850. ELSE
  2851. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2852. END;
  2853. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2854. type := system.booleanType;
  2855. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2856. type := system.booleanType;
  2857. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2858. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2859. THEN
  2860. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2861. IF (leftType # rightType) THEN
  2862. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2863. ConvertOperands(left,right); (* operands must be of the same type here *)
  2864. END;
  2865. binaryExpression.SetLeft(left);
  2866. binaryExpression.SetRight(right);
  2867. leftType := left.type.resolved;
  2868. rightType := right.type.resolved;
  2869. END;
  2870. type := leftType;
  2871. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2872. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2873. IF VerboseErrorMessage THEN
  2874. Printout.Info("left",left);
  2875. Printout.Info("right",right);
  2876. END;
  2877. ELSIF IsIntegerType(leftType) THEN
  2878. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2879. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2880. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2881. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2882. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2883. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2884. END;
  2885. END;
  2886. (* constant folding *)
  2887. (* bootstrap64
  2888. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2889. CASE operator OF
  2890. |Scanner.Plus: NewInteger(il+ir,left.type);
  2891. |Scanner.Minus: NewInteger(il-ir,left.type);
  2892. |Scanner.Times: NewInteger(il*ir,left.type);
  2893. |Scanner.Slash:
  2894. IF ir # 0 THEN
  2895. NewReal(il/ir, system.realType);
  2896. END;
  2897. |Scanner.Mod:
  2898. IF ir > 0 THEN
  2899. NewInteger(il MOD ir,left.type);
  2900. END;
  2901. |Scanner.Div:
  2902. IF ir > 0 THEN
  2903. NewInteger(il DIV ir,left.type);
  2904. END;
  2905. |Scanner.Equal: NewBool(il=ir);
  2906. |Scanner.Unequal:NewBool(il#ir);
  2907. |Scanner.Less: NewBool(il<ir);
  2908. |Scanner.LessEqual: NewBool(il<=ir);
  2909. |Scanner.Greater: NewBool(il>ir);
  2910. |Scanner.GreaterEqual: NewBool(il>=ir);
  2911. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2912. END;
  2913. ELS*)
  2914. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2915. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2916. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2917. CASE operator OF
  2918. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2919. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2920. |Scanner.Times: NewInteger(hl*hr,left.type);
  2921. |Scanner.Slash:
  2922. IF hr = 0 THEN
  2923. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2924. ELSE
  2925. IF type.sizeInBits = 64 THEN
  2926. NewReal(hl/hr,system.longrealType);
  2927. ELSE
  2928. NewReal(hl/hr,system.realType)
  2929. END
  2930. END;
  2931. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2932. |Scanner.Mod:
  2933. IF hr = 0 THEN
  2934. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2935. ELSE
  2936. NewInteger(hl MOD hr, left.type);
  2937. (* bootstrap64
  2938. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2939. *)
  2940. END;
  2941. |Scanner.Div:
  2942. IF hr = 0 THEN
  2943. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2944. ELSE
  2945. NewInteger(hl DIV hr, left.type);
  2946. (* bootstrap64
  2947. NewInteger(Machine.DivH(hl,hr),left.type);
  2948. *)
  2949. END;
  2950. (* *)
  2951. |Scanner.Equal: NewBool(hl=hr);
  2952. |Scanner.Unequal: NewBool(hl#hr);
  2953. |Scanner.Less: NewBool(hl<hr);
  2954. |Scanner.LessEqual: NewBool(hl<=hr);
  2955. |Scanner.Greater: NewBool(hl>hr);
  2956. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2957. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2958. END;
  2959. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2960. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2961. type := left.type
  2962. ELSIF (operator = Scanner.Slash) THEN
  2963. left := NewConversion(left.position,left,system.realType,NIL);
  2964. right := NewConversion(right.position,right,system.realType,NIL);
  2965. binaryExpression.SetLeft(left);
  2966. binaryExpression.SetRight(right);
  2967. type := system.realType
  2968. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2969. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2970. type := system.booleanType
  2971. ELSE
  2972. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2973. END;
  2974. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2975. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2976. CASE operator OF
  2977. |Scanner.Plus: NewReal(rl+rr,leftType);
  2978. |Scanner.Minus: NewReal(rl-rr,leftType);
  2979. |Scanner.Times:NewReal(rl*rr,leftType);
  2980. |Scanner.Slash:
  2981. IF rr = 0 THEN
  2982. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2983. ELSE
  2984. NewReal(rl/rr,leftType);
  2985. END
  2986. |Scanner.Equal: NewBool(rl=rr);
  2987. |Scanner.Unequal: NewBool(rl#rr);
  2988. |Scanner.Less: NewBool(rl<rr);
  2989. |Scanner.LessEqual: NewBool(rl<=rr);
  2990. |Scanner.Greater: NewBool(rl>rr);
  2991. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2992. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2993. END;
  2994. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2995. type := left.type
  2996. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2997. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2998. type := system.booleanType
  2999. ELSE
  3000. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  3001. IF VerboseErrorMessage THEN
  3002. Printout.Info("left",left);
  3003. Printout.Info("right",right);
  3004. END;
  3005. END;
  3006. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3007. CASE operator OF
  3008. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3009. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3010. ELSE
  3011. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3012. IF VerboseErrorMessage THEN
  3013. Printout.Info("left", left);
  3014. Printout.Info("right", right)
  3015. END;
  3016. END;
  3017. IF ~error THEN
  3018. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3019. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3020. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3021. (* do constant folding *)
  3022. CASE operator OF
  3023. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3024. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3025. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3026. |Scanner.Slash:
  3027. divisor := c * c + d * d;
  3028. ASSERT(divisor # 0);
  3029. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3030. |Scanner.Equal: NewBool((a = c) & (b = d))
  3031. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3032. END
  3033. END
  3034. END
  3035. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3036. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3037. CASE operator OF
  3038. |Scanner.And: NewBool(bl & br);
  3039. |Scanner.Or: NewBool(bl OR br);
  3040. |Scanner.Equal: NewBool(bl = br);
  3041. |Scanner.Unequal: NewBool(bl # br);
  3042. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3043. END;
  3044. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3045. type := system.booleanType
  3046. ELSE
  3047. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3048. END;
  3049. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3050. (* constant folding *)
  3051. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3052. IF operator = Scanner.Equal THEN
  3053. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3054. ELSIF operator = Scanner.Unequal THEN
  3055. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3056. END;
  3057. END;
  3058. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3059. type := system.booleanType;
  3060. ELSE
  3061. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3062. END;
  3063. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3064. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3065. CASE operator OF
  3066. |Scanner.Plus: NewSet(sl + sr);
  3067. |Scanner.Minus: NewSet(sl - sr);
  3068. |Scanner.Times: NewSet(sl * sr);
  3069. |Scanner.Slash: NewSet(sl / sr);
  3070. |Scanner.Equal: NewBool(sl=sr);
  3071. |Scanner.Unequal: NewBool(sl#sr);
  3072. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3073. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3074. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3075. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3076. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3077. END;
  3078. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3079. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3080. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3081. THEN
  3082. type := system.booleanType
  3083. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3084. type := left.type
  3085. ELSE
  3086. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3087. END;
  3088. ELSIF IsCharacterType(left.type) THEN
  3089. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3090. CASE operator OF
  3091. |Scanner.Equal: NewBool(cl=cr);
  3092. |Scanner.Unequal: NewBool(cl#cr);
  3093. |Scanner.Less: NewBool(cl<cr);
  3094. |Scanner.LessEqual: NewBool(cl<=cr);
  3095. |Scanner.Greater: NewBool(cl>cr);
  3096. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3097. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3098. END;
  3099. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3100. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3101. type := system.booleanType
  3102. ELSE
  3103. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3104. END;
  3105. ELSE
  3106. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3107. END;
  3108. ELSE
  3109. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3110. END;
  3111. IF type = SyntaxTree.invalidType THEN
  3112. result := SyntaxTree.invalidExpression
  3113. ELSE
  3114. result.SetType(type)
  3115. END;
  3116. resolvedExpression := result
  3117. END VisitBinaryExpression;
  3118. (** resolve a range expression of the from <<first .. last BY step>>
  3119. - depending on the context different things are checked:
  3120. ArrayIndex:
  3121. - components must be integers
  3122. - replace missing lower bound with 0
  3123. - replace missing upper bound with MAX(LONGINT)
  3124. - replace missing step size with 1
  3125. SetElement:
  3126. - components must be integers
  3127. - replace missing lower bound with 0
  3128. - replace missing upper bound with MAX(SET)
  3129. - must not have step size
  3130. CaseGuard:
  3131. - components must be constant
  3132. - components must be integers or characters
  3133. - must have lower and upper bound present
  3134. - components are made compatible
  3135. - must not have step size
  3136. - if error: return invalidExpression
  3137. **)
  3138. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3139. VAR
  3140. hasError: BOOLEAN;
  3141. first, last, step: SyntaxTree.Expression;
  3142. BEGIN
  3143. hasError := FALSE;
  3144. first := x.first;
  3145. last := x.last;
  3146. step := x.step;
  3147. (* check lower bound *)
  3148. IF x.context = SyntaxTree.CaseGuard THEN
  3149. IF first = NIL THEN
  3150. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3151. hasError := TRUE
  3152. ELSE
  3153. first := ResolveExpression(first);
  3154. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3155. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3156. hasError := TRUE
  3157. ELSE
  3158. IF first IS SyntaxTree.StringValue THEN
  3159. (* add conversion from string to character *)
  3160. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3161. END
  3162. END;
  3163. (* check if expression is constant *)
  3164. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3165. (* error already reported *)
  3166. hasError := TRUE
  3167. END
  3168. END
  3169. ELSE (* ArrayIndex, SetElement *)
  3170. IF first = NIL THEN
  3171. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3172. END;
  3173. first := ResolveExpression(first);
  3174. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3175. first := NewConversion(first.position, first, system.longintType, NIL)
  3176. ELSE
  3177. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3178. hasError := TRUE
  3179. END
  3180. END;
  3181. (* check upper bound *)
  3182. IF x.context = SyntaxTree.CaseGuard THEN
  3183. IF last = NIL THEN
  3184. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3185. hasError := TRUE
  3186. ELSE
  3187. last := ResolveExpression(last);
  3188. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3189. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3190. hasError := TRUE
  3191. ELSE
  3192. IF last IS SyntaxTree.StringValue THEN
  3193. (* add conversion from string to character *)
  3194. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3195. END
  3196. END;
  3197. (* check if expression is constant *)
  3198. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3199. (* error already reported *)
  3200. hasError := TRUE
  3201. ELSE
  3202. (* try to make lower and upper bound compatible *)
  3203. ConvertOperands(first, last);
  3204. IF first.type.resolved # last.type.resolved THEN
  3205. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3206. hasError := TRUE
  3207. END
  3208. END
  3209. END
  3210. ELSE (* ArrayIndex, SetElement *)
  3211. IF last = NIL THEN
  3212. IF x.context = SyntaxTree.ArrayIndex THEN
  3213. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3214. ELSE
  3215. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3216. END
  3217. END;
  3218. last := ResolveExpression(last);
  3219. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3220. last := NewConversion(last.position, last, system.longintType, NIL)
  3221. ELSE
  3222. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3223. hasError := TRUE
  3224. END
  3225. END;
  3226. (* check step size *)
  3227. IF x.context = SyntaxTree.ArrayIndex THEN
  3228. IF step = NIL THEN
  3229. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3230. END;
  3231. step := ResolveExpression(step);
  3232. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3233. step := NewConversion(step.position, step, system.longintType, NIL)
  3234. ELSE
  3235. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3236. hasError := TRUE
  3237. END
  3238. ELSE (* SetElement, CaseGuard *)
  3239. IF step # NIL THEN
  3240. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3241. hasError := TRUE
  3242. END
  3243. END;
  3244. IF hasError THEN
  3245. resolvedExpression := SyntaxTree.invalidExpression
  3246. ELSE
  3247. x.SetFirst(first);
  3248. x.SetLast(last);
  3249. x.SetStep(step);
  3250. x.SetType(system.rangeType);
  3251. resolvedExpression := x;
  3252. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3253. END
  3254. END VisitRangeExpression;
  3255. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3256. BEGIN
  3257. x.SetType(NIL);
  3258. resolvedExpression := x;
  3259. END VisitTensorRangeExpression;
  3260. (** resolve the expression d and return result as designator
  3261. - resolve expression
  3262. - if expression is a designator then return designator else error message and return invalidDesignator
  3263. **)
  3264. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3265. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3266. BEGIN
  3267. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3268. resolved := ResolveExpression(d);
  3269. IF resolved = SyntaxTree.invalidExpression THEN
  3270. (* error should already have been reported *)
  3271. result := SyntaxTree.invalidDesignator;
  3272. ELSIF resolved IS SyntaxTree.Designator THEN
  3273. result := resolved(SyntaxTree.Designator);
  3274. ELSE
  3275. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3276. result := SyntaxTree.invalidDesignator;
  3277. END;
  3278. (* result.type might be nil. *)
  3279. RETURN result
  3280. END ResolveDesignator;
  3281. (**
  3282. symbol designator generated in this module
  3283. nothing to be resolved
  3284. **)
  3285. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3286. BEGIN
  3287. resolvedExpression := x;
  3288. END VisitSymbolDesignator;
  3289. (**
  3290. self designator generated in this module
  3291. nothing to be resolved
  3292. **)
  3293. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3294. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3295. BEGIN
  3296. (* check if in record scope *)
  3297. scope := currentScope;
  3298. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3299. scope := scope.outerScope;
  3300. END;
  3301. IF scope = NIL THEN (* in module scope *)
  3302. x.SetType(system.anyType);
  3303. ELSIF scope IS SyntaxTree.CellScope THEN
  3304. cell := scope(SyntaxTree.CellScope).ownerCell;
  3305. x.SetType(cell);
  3306. ELSE (* in record scope *)
  3307. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3308. IF (record # NIL) & (record.pointerType # NIL) THEN
  3309. type := ResolveType(record.pointerType);
  3310. x.SetType(type);
  3311. ELSE
  3312. x.SetType(record);
  3313. END;
  3314. END;
  3315. resolvedExpression := x;
  3316. END VisitSelfDesignator;
  3317. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3318. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3319. BEGIN
  3320. scope := currentScope;
  3321. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3322. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3323. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3324. returnType := procedureType.returnType;
  3325. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3326. THEN
  3327. x.SetType(returnType);
  3328. ELSE
  3329. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3330. x.SetType(SyntaxTree.invalidType);
  3331. END;
  3332. ELSE
  3333. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3334. x.SetType(SyntaxTree.invalidType);
  3335. END;
  3336. x.SetAssignable(TRUE);
  3337. resolvedExpression := x;
  3338. END VisitResultDesignator;
  3339. (**
  3340. return symbol designator as an expression
  3341. - if symbol is a constant then return the constant value expression
  3342. - else
  3343. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3344. - if symbol is a guarded variable then return a TypeGuardDesignator
  3345. - else return a symbol designator
  3346. **)
  3347. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3348. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3349. guardType: SyntaxTree.Type;
  3350. BEGIN
  3351. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3352. result := SyntaxTree.invalidExpression;
  3353. ASSERT(symbol # NIL);
  3354. (*
  3355. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3356. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3357. Error(position,Diagnostics.Invalid,"type not allowed here");
  3358. ELS *)
  3359. (* not needed any more as values are stored in the expression
  3360. IF symbol IS SyntaxTree.Constant THEN
  3361. result := symbol(SyntaxTree.Constant).value
  3362. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3363. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3364. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3365. ELSE
  3366. result := symbol(SyntaxTree.Constant).value
  3367. END;
  3368. ELSE
  3369. *)
  3370. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3371. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3372. THEN
  3373. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3374. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3375. left := NewDereferenceDesignator(position,left);
  3376. left.SetHidden(TRUE);
  3377. END;
  3378. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3379. scope := currentScope;
  3380. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3381. scope := scope.outerScope;
  3382. END;
  3383. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3384. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3385. END;
  3386. END;
  3387. 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);
  3388. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3389. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3390. ELSE
  3391. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3392. END;
  3393. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3394. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3395. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3396. result.SetType(symbol.type);
  3397. result.SetAssignable(assignable);
  3398. symbol.MarkUsed;
  3399. IF symbol IS SyntaxTree.Constant THEN
  3400. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3401. END;
  3402. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3403. variableAccessed := TRUE
  3404. END;
  3405. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3406. IF GetGuard(symbol,guardType) THEN
  3407. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3408. END;
  3409. END;
  3410. ASSERT(result.type # NIL);
  3411. RETURN result
  3412. END NewSymbolDesignator;
  3413. (** check and resolve an identifier designator "identifier"
  3414. - if identifier = self then return SelfDesignator
  3415. - else find symbol in current scope
  3416. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3417. **)
  3418. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3419. VAR symbol: SyntaxTree.Symbol;
  3420. BEGIN
  3421. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3422. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3423. IF symbol # NIL THEN
  3424. ResolveSymbol(symbol);
  3425. ASSERT(symbol.type # NIL);
  3426. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3427. ELSE
  3428. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3429. IF VerboseErrorMessage THEN
  3430. Printout.Info("undeclared identifier designator",identifierDesignator);
  3431. END;
  3432. resolvedExpression := SyntaxTree.invalidDesignator;
  3433. END;
  3434. END VisitIdentifierDesignator;
  3435. (** check and resolve a selector designator of the form left.designator
  3436. - if left is a pointer type then do auto dereferenciation
  3437. - left denotes a search scope:
  3438. - if left type is import type then set search scope to respective module
  3439. - if left type is enumeration type then set search scope to respective enumeration scope
  3440. - elsif left type is record type then set search scope to record scope
  3441. - search symbol in computed scope
  3442. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3443. **)
  3444. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3445. VAR
  3446. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3447. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3448. BEGIN
  3449. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3450. left := ResolveDesignator(selectorDesignator.left);
  3451. result := SyntaxTree.invalidDesignator;
  3452. IF left # NIL THEN
  3453. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3454. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3455. END;
  3456. scope := NIL;
  3457. IF left.type = NIL THEN
  3458. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3459. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3460. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3461. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3462. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3463. module := symbol(SyntaxTree.Import).module;
  3464. IF module # NIL THEN
  3465. scope := module.moduleScope
  3466. ELSE
  3467. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3468. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3469. END;
  3470. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3471. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3472. ASSERT(scope # NIL)
  3473. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3474. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3475. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3476. IF type IS SyntaxTree.EnumerationType THEN
  3477. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3478. ELSE
  3479. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3480. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3481. END;
  3482. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3483. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3484. ELSE
  3485. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3486. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3487. END;
  3488. symbol := NIL;
  3489. IF scope # NIL THEN
  3490. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3491. IF symbol # NIL THEN
  3492. ResolveSymbol(symbol);
  3493. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3494. symbol.MarkUsed
  3495. ELSE
  3496. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3497. IF VerboseErrorMessage THEN
  3498. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3499. Printout.Info("scope", scope);
  3500. Printout.Info("left", left);
  3501. Printout.Info("undeclared identifier",selectorDesignator);
  3502. Printout.Info("left resolved designator",left);
  3503. END
  3504. END;
  3505. END;
  3506. END;
  3507. resolvedExpression := result;
  3508. END VisitSelectorDesignator;
  3509. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3510. VAR len,idx: LONGINT;
  3511. BEGIN
  3512. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3513. IF idx < 0 THEN
  3514. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3515. ELSE
  3516. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3517. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3518. END;
  3519. END;
  3520. END;
  3521. END IndexCheck;
  3522. (*
  3523. - if index designator has not type, use newBaseType as its type
  3524. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3525. - special rule: if static array of dynamic array occurs, make it all dynamic
  3526. index designator type: new base type: new index designator type:
  3527. NIL z z
  3528. ARRAY [x, y] z ARRAY [x, y] OF z
  3529. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3530. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3531. *)
  3532. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3533. VAR
  3534. mathArrayType: SyntaxTree.MathArrayType;
  3535. makeDynamic: BOOLEAN;
  3536. BEGIN
  3537. IF indexDesignator.type = NIL THEN
  3538. indexDesignator.SetType(newBaseType)
  3539. ELSE
  3540. (* index designator must be a of math array type *)
  3541. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3542. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3543. (* determine if all arrays have to be made dynamic *)
  3544. makeDynamic :=
  3545. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3546. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3547. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3548. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3549. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3550. END;
  3551. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3552. mathArrayType.SetArrayBase(newBaseType)
  3553. END
  3554. END SetIndexBaseType;
  3555. (** check and append index list element to index designator of math array
  3556. - check validity of single index or array range
  3557. - compute new type
  3558. - if range then create new array type (calculate length of resulting array)
  3559. - otherwise take sourceArray.arrayBase as new type
  3560. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3561. **)
  3562. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3563. VAR
  3564. targetArray: SyntaxTree.MathArrayType;
  3565. first, last, step: SyntaxTree.Expression;
  3566. firstValue, lastValue, stepValue, length: LONGINT;
  3567. rangeExpression: SyntaxTree.RangeExpression;
  3568. BEGIN
  3569. IF indexListItem.type = SyntaxTree.invalidType THEN
  3570. (* error already handled *)
  3571. indexDesignator.parameters.AddExpression(indexListItem)
  3572. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3573. indexDesignator.HasRange;
  3574. indexDesignator.HasTensorRange;
  3575. indexDesignator.parameters.AddExpression(indexListItem);
  3576. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3577. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3578. IndexCheck(indexListItem, sourceArray.length);
  3579. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3580. indexDesignator.parameters.AddExpression(indexListItem)
  3581. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3582. indexDesignator.HasRange;
  3583. (* if the range is given as an array range expression, check the validity of its components *)
  3584. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3585. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3586. first := rangeExpression.first;
  3587. last := rangeExpression.last;
  3588. step := rangeExpression.step;
  3589. (* perform static checks on range components *)
  3590. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3591. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3592. END;
  3593. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3594. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3595. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3596. END
  3597. END;
  3598. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3599. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3600. END;
  3601. (* add conversions to size type *)
  3602. (* TODO: needed? *)
  3603. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3604. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3605. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3606. END;
  3607. IF indexDesignator.hasTensorRange THEN
  3608. (* the index designator's base type is a tensor: leave it as is *)
  3609. ELSE
  3610. (* append a new math array to the index designator's base type *)
  3611. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3612. IF ~error THEN
  3613. (*
  3614. (* optimization: calculate length of target array for static ranges *)
  3615. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3616. IF IsStaticallyOpenRange(rangeExpression) THEN
  3617. (* range is open ('*'): reuse source array length as target array length *)
  3618. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3619. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3620. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3621. IF lastValue = MAX(LONGINT) THEN
  3622. IF IsIntegerValue(sourceArray.length, length) THEN
  3623. lastValue := length - 1;
  3624. isStaticTargetArrayLength := TRUE
  3625. ELSE
  3626. isStaticTargetArrayLength := FALSE
  3627. END
  3628. ELSE
  3629. isStaticTargetArrayLength := TRUE
  3630. END;
  3631. IF isStaticTargetArrayLength THEN
  3632. (* calculate static target array length *)
  3633. IF firstValue > lastValue THEN
  3634. length := 0
  3635. ELSE
  3636. length := 1 + lastValue - firstValue;
  3637. IF length MOD stepValue = 0 THEN
  3638. length := length DIV stepValue
  3639. ELSE
  3640. length := length DIV stepValue + 1
  3641. END
  3642. END;
  3643. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3644. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3645. ASSERT(targetArray.form = SyntaxTree.Static)
  3646. END
  3647. END
  3648. END
  3649. *)
  3650. END;
  3651. SetIndexBaseType(indexDesignator, targetArray)
  3652. END;
  3653. indexDesignator.parameters.AddExpression(indexListItem)
  3654. ELSE
  3655. Error(position, Diagnostics.Invalid,"invalid index list item");
  3656. END;
  3657. END AppendMathIndex;
  3658. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3659. VAR parameters: SyntaxTree.ExpressionList;
  3660. BEGIN
  3661. parameters := index.parameters;
  3662. IF (expression.type = NIL) THEN
  3663. Error(position,Diagnostics.Invalid,"invalid index");
  3664. ELSIF IsIntegerType(expression.type.resolved) THEN
  3665. IF over IS SyntaxTree.ArrayType THEN
  3666. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3667. ELSIF over IS SyntaxTree.StringType THEN
  3668. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3669. END;
  3670. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3671. parameters.AddExpression(expression);
  3672. ELSE
  3673. Error(position,Diagnostics.Invalid,"invalid index");
  3674. END;
  3675. END AppendIndex;
  3676. (** convert an expression to math array type
  3677. if expression is of math array type: return expression itself
  3678. if expression is of array-structured object type: return an index operator call on it
  3679. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3680. otherwise: return invalid expression
  3681. **)
  3682. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3683. VAR
  3684. result: SyntaxTree.Expression;
  3685. mathArrayType: SyntaxTree.MathArrayType;
  3686. BEGIN
  3687. IF expression.type = NIL THEN
  3688. result := SyntaxTree.invalidExpression
  3689. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3690. (* expression of math array type *)
  3691. result := expression
  3692. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3693. (* expression of array-structured object type *)
  3694. mathArrayType := MathArrayStructureOfType(expression.type);
  3695. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3696. ELSE
  3697. result := SyntaxTree.invalidExpression
  3698. END;
  3699. RETURN result
  3700. END ConvertToMathArray;
  3701. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3702. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3703. VAR
  3704. result: SyntaxTree.ExpressionList;
  3705. i: LONGINT;
  3706. BEGIN
  3707. result := SyntaxTree.NewExpressionList();
  3708. FOR i := 1 TO itemCount DO
  3709. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3710. END;
  3711. RETURN result
  3712. END ListOfOpenRanges;
  3713. (** create a procedure call designator for an index operator call on an array-structured object type
  3714. - use given index list as actual parameters
  3715. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3716. **)
  3717. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3718. VAR
  3719. operator: SyntaxTree.Operator;
  3720. expression: SyntaxTree.Expression;
  3721. actualParameters, tempList: SyntaxTree.ExpressionList;
  3722. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3723. result, tempDesignator: SyntaxTree.Designator;
  3724. recordType: SyntaxTree.RecordType;
  3725. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3726. i, hashValue, indexListSize, indexListKind: LONGINT;
  3727. castReturnType: SyntaxTree.MathArrayType;
  3728. BEGIN
  3729. ASSERT(IsArrayStructuredObjectType(left.type));
  3730. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3731. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3732. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3733. indexListSize := indexList.Length();
  3734. indexListKind := 0;
  3735. containsNonRange := FALSE;
  3736. FOR i := 0 TO indexList.Length() - 1 DO
  3737. indexListKind := indexListKind * 2;
  3738. expression := indexList.GetExpression(i);
  3739. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3740. INC(indexListKind)
  3741. ELSE
  3742. containsNonRange := TRUE
  3743. END
  3744. END;
  3745. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3746. (* select applicable index operator
  3747. - try to look up optimal index operator
  3748. - if not present, use operator on ranges
  3749. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3750. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3751. *)
  3752. usesGeneralOperator := FALSE;
  3753. IF rhs # NIL THEN
  3754. (* write operator *)
  3755. IF hashValue = -1 THEN
  3756. operator := NIL
  3757. ELSE
  3758. operator := recordType.arrayAccessOperators.write[hashValue];
  3759. END;
  3760. IF operator = NIL THEN
  3761. usesPureRangeOperator := TRUE;
  3762. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3763. operator := recordType.arrayAccessOperators.generalWrite;
  3764. usesGeneralOperator := TRUE
  3765. ELSE
  3766. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3767. operator := recordType.arrayAccessOperators.write[hashValue];
  3768. END
  3769. END
  3770. ELSE
  3771. (* read operator *)
  3772. IF hashValue = -1 THEN
  3773. operator := NIL
  3774. ELSE
  3775. operator := recordType.arrayAccessOperators.read[hashValue];
  3776. END;
  3777. IF operator = NIL THEN
  3778. usesPureRangeOperator := TRUE;
  3779. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3780. operator := recordType.arrayAccessOperators.generalRead;
  3781. usesGeneralOperator := TRUE
  3782. ELSE
  3783. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3784. operator := recordType.arrayAccessOperators.read[hashValue];
  3785. END
  3786. END
  3787. END;
  3788. IF operator = NIL THEN
  3789. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3790. result := SyntaxTree.invalidDesignator;
  3791. ELSE
  3792. (* determine if reshaping is needed *)
  3793. needsReshaping := containsNonRange & usesPureRangeOperator;
  3794. (* import OCArrayBase if reshaping is needed *)
  3795. IF needsReshaping & ~arrayBaseImported THEN
  3796. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3797. arrayBaseImported := TRUE
  3798. END;
  3799. (* add the index list item to the list of actual parameters
  3800. - for general operators: add a single inline array containing the index list items as parameter
  3801. - otherwise: add all index list items as individual parameters
  3802. *)
  3803. actualParameters := SyntaxTree.NewExpressionList();
  3804. IF usesGeneralOperator THEN
  3805. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3806. END;
  3807. FOR i := 0 TO indexListSize - 1 DO
  3808. expression := indexList.GetExpression(i);
  3809. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3810. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3811. tempList := SyntaxTree.NewExpressionList();
  3812. tempList.AddExpression(expression);
  3813. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3814. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3815. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3816. END;
  3817. IF usesGeneralOperator THEN
  3818. tempMathArrayExpression.elements.AddExpression(expression);
  3819. ELSE
  3820. actualParameters.AddExpression(expression)
  3821. END
  3822. END;
  3823. IF usesGeneralOperator THEN
  3824. actualParameters.AddExpression(tempMathArrayExpression)
  3825. END;
  3826. IF rhs # NIL THEN
  3827. (* add actual parameter for RHS *)
  3828. IF needsReshaping THEN
  3829. (* reshape using OCArrayBase.ExpandDimensions *)
  3830. tempList := SyntaxTree.NewExpressionList();
  3831. (* source array *)
  3832. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3833. tempList.AddExpression(rhs);
  3834. ELSE
  3835. (* convert scalar to one-dimensional array *)
  3836. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3837. tempMathArrayExpression.elements.AddExpression(rhs);
  3838. tempList.AddExpression(tempMathArrayExpression)
  3839. END;
  3840. (* list of kept dimensions *)
  3841. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3842. FOR i := 0 TO indexListSize - 1 DO
  3843. expression := indexList.GetExpression(i);
  3844. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3845. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3846. ELSE
  3847. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3848. END
  3849. END;
  3850. tempList.AddExpression(tempMathArrayExpression);
  3851. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3852. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3853. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3854. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3855. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3856. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3857. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3858. expression.SetType(castReturnType);
  3859. ELSE
  3860. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3861. END;
  3862. actualParameters.AddExpression(expression)
  3863. ELSE
  3864. actualParameters.AddExpression(rhs)
  3865. END
  3866. END;
  3867. (* add dereference operator and create procedure call designator *)
  3868. ASSERT(left IS SyntaxTree.Designator);
  3869. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3870. ASSERT(expression IS SyntaxTree.Designator);
  3871. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3872. IF (rhs = NIL) & needsReshaping THEN
  3873. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3874. tempList := SyntaxTree.NewExpressionList();
  3875. FOR i := 0 TO indexList.Length() - 1 DO
  3876. expression := indexList.GetExpression(i);
  3877. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3878. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3879. ELSE
  3880. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3881. END
  3882. END;
  3883. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3884. END;
  3885. IF rhs = NIL THEN
  3886. (* special rule: index read operator calls are considered to be assignable *)
  3887. result.SetAssignable(TRUE)
  3888. END;
  3889. (* put information about this index operator call into the resulting designator *)
  3890. result.SetRelatedAsot(left);
  3891. result.SetRelatedIndexList(indexList)
  3892. END;
  3893. RETURN result
  3894. END NewIndexOperatorCall;
  3895. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3896. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3897. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3898. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3899. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3900. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3901. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3902. CONST trace = FALSE;
  3903. BEGIN
  3904. IF trace THEN
  3905. FOR i := 0 TO actualParameters.Length()-1 DO
  3906. Printout.Info("par", actualParameters.GetExpression(i));
  3907. END;
  3908. END;
  3909. operator := scope.firstOperator;
  3910. WHILE(operator # NIL) DO
  3911. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3912. procedureType := operator.type(SyntaxTree.ProcedureType);
  3913. distance := Distance(system, procedureType,actualParameters);
  3914. IF trace THEN Printout.Info("check op ",operator) END;
  3915. IF distance < bestDistance THEN
  3916. IF trace THEN Printout.Info("taken op",operator) END;
  3917. bestDistance := distance;
  3918. bestOperator := operator;
  3919. END;
  3920. END;
  3921. operator := operator.nextOperator;
  3922. END;
  3923. END FindInScope;
  3924. BEGIN
  3925. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3926. IF oper = 0 THEN (* index *)
  3927. identifier := SyntaxTree.NewIdentifier("[]");
  3928. ELSE
  3929. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3930. END;
  3931. WHILE (recordType # NIL) DO
  3932. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3933. recordType := recordType.GetBaseRecord();
  3934. END;
  3935. RETURN bestOperator
  3936. END FindOperator;
  3937. BEGIN
  3938. type := left.type.resolved;
  3939. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3940. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3941. actualParameters := SyntaxTree.NewExpressionList();
  3942. IF parameters # NIL THEN
  3943. FOR i := 0 TO parameters.Length()-1 DO
  3944. expression := ResolveExpression(parameters.GetExpression(i));
  3945. actualParameters.AddExpression(expression);
  3946. END;
  3947. END;
  3948. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3949. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3950. IF op # NIL THEN
  3951. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3952. ASSERT(expression IS SyntaxTree.Designator);
  3953. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3954. result.SetRelatedAsot(left);
  3955. result.SetRelatedIndexList(parameters);
  3956. (* check if write operator exists, for var parameters *)
  3957. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3958. actualParameters := SyntaxTree.NewExpressionList();
  3959. FOR i := 0 TO parameters.Length()-1 DO
  3960. expression := ResolveExpression(parameters.GetExpression(i));
  3961. actualParameters.AddExpression(expression);
  3962. END;
  3963. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3964. actualParameters.AddExpression(rhs);
  3965. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3966. IF op = NIL THEN rhs := NIL END;
  3967. END;
  3968. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3969. ELSE
  3970. result := NIL;
  3971. END;
  3972. RETURN result;
  3973. END NewObjectOperatorCall;
  3974. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3975. 1. convert bracket designator chains into a single one that contains separators
  3976. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3977. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3978. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3979. - if an array or math array is indexed over, create index designator
  3980. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3981. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3982. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3983. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3984. - if an array-structured object type is indexed over, create procedure call designator
  3985. e.g.: a[x, y] -> a^."[]"(x, y)
  3986. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3987. - a[i, *] = a[i][*]
  3988. - a[*, i] # a[*][i]
  3989. Because:
  3990. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3991. - 'i-th column' = a[*, i]
  3992. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3993. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3994. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3995. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3996. **)
  3997. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3998. VAR
  3999. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4000. indexDesignator: SyntaxTree.IndexDesignator;
  4001. designator: SyntaxTree.Designator;
  4002. type: SyntaxTree.Type;
  4003. recordType: SyntaxTree.RecordType;
  4004. expression, rhs: SyntaxTree.Expression;
  4005. indexList: SyntaxTree.ExpressionList;
  4006. i: LONGINT;
  4007. hasError, done: BOOLEAN;
  4008. PROCEDURE FinalizeIndexDesignator;
  4009. BEGIN
  4010. IF indexDesignator # NIL THEN
  4011. (* the end of a tensor has been reached: *)
  4012. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4013. SetIndexBaseType(indexDesignator, type);
  4014. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4015. designator := indexDesignator;
  4016. type := designator.type.resolved;
  4017. indexDesignator := NIL;
  4018. ASSERT(SyntaxTree.Resolved IN type.state)
  4019. END
  4020. END FinalizeIndexDesignator;
  4021. BEGIN
  4022. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4023. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4024. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4025. (* copy all index list entries including a separator to the left bracket designator *)
  4026. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4027. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4028. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4029. END;
  4030. (* propagate the related RHS *)
  4031. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4032. (* only resolve left bracket designator and use as final result *)
  4033. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4034. ELSE
  4035. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4036. designator := ResolveDesignator(bracketDesignator.left);
  4037. type := designator.type.resolved;
  4038. indexDesignator := NIL;
  4039. (*!!! clean up *)
  4040. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4041. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4042. IF resolvedExpression = NIL THEN
  4043. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4044. resolvedExpression := SyntaxTree.invalidDesignator
  4045. END;
  4046. RETURN;
  4047. END;
  4048. i := 0;
  4049. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4050. expression := bracketDesignator.parameters.GetExpression(i);
  4051. expression := ResolveExpression(expression);
  4052. bracketDesignator.parameters.SetExpression(i, expression);
  4053. IF expression = SyntaxTree.indexListSeparator THEN
  4054. (* finalize an existing index designator if needed *)
  4055. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4056. INC(i)
  4057. ELSE
  4058. (* do auto-dereferencing if needed *)
  4059. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4060. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4061. & (i=0)*)
  4062. THEN
  4063. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4064. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4065. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4066. designator := SyntaxTree.invalidDesignator;
  4067. type := SyntaxTree.invalidType
  4068. ELSE
  4069. FinalizeIndexDesignator;
  4070. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4071. type := designator.type.resolved
  4072. END
  4073. END;
  4074. (* create a new index designator, if needed *)
  4075. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4076. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4077. indexDesignator.SetAssignable(designator.assignable);
  4078. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4079. (* designator := indexDesignator *)
  4080. END;
  4081. IF type = SyntaxTree.invalidType THEN
  4082. (* error already handled *)
  4083. INC(i)
  4084. ELSIF type IS SyntaxTree.ArrayType THEN
  4085. (* indexing over an array *)
  4086. ASSERT(indexDesignator # NIL);
  4087. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4088. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4089. INC(i)
  4090. ELSIF type IS SyntaxTree.StringType THEN
  4091. (* indexing over an array *)
  4092. ASSERT(indexDesignator # NIL);
  4093. AppendIndex(expression.position, indexDesignator, expression, type);
  4094. type := type(SyntaxTree.StringType).baseType.resolved;
  4095. INC(i)
  4096. ELSIF type IS SyntaxTree.MathArrayType THEN
  4097. (* indexing over a math array *)
  4098. ASSERT(indexDesignator # NIL);
  4099. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4100. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4101. INC(i)
  4102. ELSIF IsArrayStructuredObjectType(type) THEN
  4103. (* indexing over ASOTs *)
  4104. FinalizeIndexDesignator;
  4105. ASSERT(type IS SyntaxTree.PointerType);
  4106. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4107. (*
  4108. - collect index list items from bracket designator that belong to ASOT
  4109. - check for errors
  4110. *)
  4111. indexList := SyntaxTree.NewExpressionList();
  4112. hasError := FALSE;
  4113. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4114. (* indexing over tensor ASOT:
  4115. - stop at index list end or separator
  4116. - dimensionality is given by number of index list items
  4117. *)
  4118. done := FALSE;
  4119. WHILE ~done DO
  4120. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4121. done := TRUE;
  4122. ELSE
  4123. expression := bracketDesignator.parameters.GetExpression(i);
  4124. IF expression = SyntaxTree.indexListSeparator THEN
  4125. done := TRUE;
  4126. ELSE
  4127. expression := ResolveExpression(expression);
  4128. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4129. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4130. hasError := TRUE
  4131. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4132. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4133. expression := SyntaxTree.invalidExpression;
  4134. hasError := TRUE
  4135. END;
  4136. indexList.AddExpression(expression)
  4137. END;
  4138. INC(i)
  4139. END
  4140. END
  4141. ELSE
  4142. (* indexing over non-tensor ASOT:
  4143. - ignore separators
  4144. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4145. *)
  4146. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4147. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4148. expression := bracketDesignator.parameters.GetExpression(i);
  4149. ELSE
  4150. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4151. END;
  4152. IF expression # SyntaxTree.indexListSeparator THEN
  4153. expression := ResolveExpression(expression);
  4154. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4155. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4156. expression := SyntaxTree.invalidExpression;
  4157. hasError := TRUE
  4158. END;
  4159. indexList.AddExpression(expression)
  4160. END;
  4161. INC(i)
  4162. END;
  4163. END;
  4164. IF hasError THEN
  4165. designator := SyntaxTree.invalidDesignator;
  4166. type := SyntaxTree.invalidType;
  4167. ELSE
  4168. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4169. and the last entry in the index list belongs to the array-structured object type in question.
  4170. E.g.: for a 2-dimensional array-structured object type:
  4171. - 'lhs := asot[1, 2]' -> read mode
  4172. - 'asot[1, 2] := rhs' -> write mode
  4173. - 'asot[1, 2, 3] := rhs' -> read mode
  4174. *)
  4175. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4176. rhs := bracketDesignator.relatedRhs
  4177. ELSE
  4178. rhs := NIL
  4179. END;
  4180. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4181. type := designator.type
  4182. END
  4183. ELSE
  4184. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4185. designator := SyntaxTree.invalidDesignator;
  4186. type := SyntaxTree.invalidType;
  4187. INC(i)
  4188. END
  4189. END
  4190. END;
  4191. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4192. resolvedExpression := designator
  4193. END
  4194. END VisitBracketDesignator;
  4195. (** check and resolve expression list
  4196. - resolve each expression in an expression list
  4197. - returns true if and only if all statements could have successfully been resolved
  4198. **)
  4199. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4200. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4201. BEGIN
  4202. result := TRUE;
  4203. FOR i := 0 TO expressionList.Length()-1 DO
  4204. expression := ResolveExpression(expressionList.GetExpression(i));
  4205. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4206. expressionList.SetExpression(i,expression);
  4207. END;
  4208. RETURN result
  4209. END ExpressionList;
  4210. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4211. BEGIN
  4212. type := type.resolved;
  4213. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4214. RETURN TRUE
  4215. ELSIF system.CanPassInRegister # NIL THEN
  4216. RETURN system.CanPassInRegister(type);
  4217. ELSE
  4218. RETURN FALSE
  4219. END;
  4220. END CanPassInRegister;
  4221. (** return procedure call designator left(actualParameters)
  4222. - check realtime procedure call in realtime procedure
  4223. - check number of parameters
  4224. - check parameter compatibility
  4225. return invalidDesignator if error
  4226. **)
  4227. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4228. VAR result: SyntaxTree.Designator;
  4229. numberFormalParameters, numberActualParameters: LONGINT;
  4230. formalType: SyntaxTree.ProcedureType;
  4231. formalParameter: SyntaxTree.Parameter;
  4232. actualParameter: SyntaxTree.Expression;
  4233. i: LONGINT;
  4234. BEGIN
  4235. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4236. result := SyntaxTree.invalidDesignator;
  4237. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4238. numberFormalParameters := formalType.numberParameters;
  4239. numberActualParameters := actualParameters.Length();
  4240. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4241. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4242. END;
  4243. IF ~ExpressionList(actualParameters) THEN
  4244. result := SyntaxTree.invalidDesignator
  4245. ELSE
  4246. IF numberActualParameters <= numberFormalParameters THEN
  4247. formalParameter := formalType.firstParameter;
  4248. FOR i := 0 TO numberActualParameters-1 DO
  4249. actualParameter := actualParameters.GetExpression(i);
  4250. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4251. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4252. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4253. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4254. ELSE
  4255. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4256. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4257. END;
  4258. actualParameters.SetExpression(i,actualParameter);
  4259. END;
  4260. formalParameter := formalParameter.nextParameter;
  4261. END;
  4262. WHILE (formalParameter # NIL) DO
  4263. IF formalParameter.defaultValue # NIL THEN
  4264. actualParameters.AddExpression(formalParameter.defaultValue);
  4265. formalParameter := formalParameter.nextParameter
  4266. ELSE
  4267. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4268. formalParameter := NIL;
  4269. END;
  4270. END;
  4271. ELSE
  4272. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4273. END;
  4274. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4275. result.SetAssignable(FALSE);
  4276. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4277. END;
  4278. RETURN result
  4279. END NewProcedureCallDesignator;
  4280. (**
  4281. builtin call designator generated in VisitParameterDesignator
  4282. -> nothing to be resolved
  4283. **)
  4284. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4285. BEGIN
  4286. resolvedExpression := x;
  4287. END VisitTypeGuardDesignator;
  4288. (**
  4289. builtin call designator generated in VisitParameterDesignator
  4290. -> nothing to be resolved
  4291. **)
  4292. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4293. BEGIN
  4294. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4295. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4296. ASSERT(resolvedExpression.type # NIL);
  4297. ELSIF ExpressionList(x.parameters) THEN
  4298. resolvedExpression := x;
  4299. END;
  4300. END VisitBuiltinCallDesignator;
  4301. (**
  4302. procedure call designator generated in VisitParameterDesignator
  4303. -> nothing to be resolved
  4304. **)
  4305. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4306. BEGIN
  4307. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4308. resolvedExpression := x;
  4309. END VisitProcedureCallDesignator;
  4310. (** return true if x is a variable else return false and report error **)
  4311. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4312. VAR result: BOOLEAN;
  4313. BEGIN
  4314. result := TRUE;
  4315. IF x = SyntaxTree.invalidExpression THEN
  4316. result := FALSE;
  4317. ELSIF ~IsVariable(x) THEN
  4318. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4319. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4320. result := FALSE;
  4321. END;
  4322. RETURN result
  4323. END CheckVariable;
  4324. (**
  4325. if expression x is of basic type then return true else report error and return false
  4326. **)
  4327. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4328. VAR result: BOOLEAN;
  4329. BEGIN
  4330. result := FALSE;
  4331. IF x = SyntaxTree.invalidExpression THEN
  4332. ELSIF ~IsBasicType(x.type) THEN
  4333. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4334. result := FALSE
  4335. ELSE result := TRUE
  4336. END;
  4337. RETURN result
  4338. END CheckBasicType;
  4339. (**
  4340. if expression x is of number type then return true else report error and return false
  4341. **)
  4342. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4343. VAR result: BOOLEAN;
  4344. BEGIN
  4345. result := FALSE;
  4346. IF x = SyntaxTree.invalidExpression THEN
  4347. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4348. Error(x.position,Diagnostics.Invalid,"is non number type");
  4349. ELSE result := TRUE
  4350. END;
  4351. RETURN result
  4352. END CheckNumberType;
  4353. (**
  4354. if expression x is of number or size type but not complex then return true else report error and return false
  4355. **)
  4356. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4357. VAR result: BOOLEAN;
  4358. BEGIN
  4359. result := FALSE;
  4360. IF x = SyntaxTree.invalidExpression THEN
  4361. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4362. Error(x.position,Diagnostics.Invalid,"is complex type");
  4363. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4364. Error(x.position,Diagnostics.Invalid,"is non number type");
  4365. ELSE result := TRUE
  4366. END;
  4367. RETURN result
  4368. END CheckNonComplexNumberSizeType;
  4369. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4370. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4371. BEGIN
  4372. result := FALSE; type := x.type.resolved;
  4373. IF x = SyntaxTree.invalidExpression THEN
  4374. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4375. TRACE(type.sizeInBits);
  4376. TRACE(system.addressType.sizeInBits);
  4377. Error(x.position,Diagnostics.Invalid,"is no address type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckAddressType;
  4382. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4384. BEGIN
  4385. result := FALSE; type := x.type.resolved;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4388. Error(x.position,Diagnostics.Invalid,"is no size type");
  4389. ELSE result := TRUE
  4390. END;
  4391. RETURN result
  4392. END CheckSizeType;
  4393. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4394. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4395. BEGIN
  4396. result := FALSE; type := x.type.resolved;
  4397. IF x = SyntaxTree.invalidExpression THEN
  4398. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4399. Error(x.position,Diagnostics.Invalid,"is no object type");
  4400. ELSE result := TRUE
  4401. END;
  4402. RETURN result
  4403. END CheckObjectType;
  4404. (**
  4405. if expression x is of integer type then return true else report error and return false
  4406. **)
  4407. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4408. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4409. BEGIN
  4410. result := FALSE; type := x.type.resolved;
  4411. IF x = SyntaxTree.invalidExpression THEN
  4412. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4413. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4414. ELSE result := TRUE
  4415. END;
  4416. RETURN result
  4417. END CheckIntegerType;
  4418. (**
  4419. if expression x is of character type then return true else report error and return false
  4420. **)
  4421. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4422. VAR result: BOOLEAN;
  4423. BEGIN
  4424. result := FALSE;
  4425. IF x = SyntaxTree.invalidExpression THEN
  4426. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4427. Error(x.position,Diagnostics.Invalid,"is no character type");
  4428. ELSE result := TRUE
  4429. END;
  4430. RETURN result
  4431. END CheckCharacterType;
  4432. (**
  4433. if expression x is of real type then return true else report error and return false
  4434. **)
  4435. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4436. VAR result: BOOLEAN;
  4437. BEGIN
  4438. result := FALSE;
  4439. IF x = SyntaxTree.invalidExpression THEN
  4440. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4441. Error(x.position,Diagnostics.Invalid,"is no float type");
  4442. ELSE result := TRUE
  4443. END;
  4444. RETURN result
  4445. END CheckRealType;
  4446. (**
  4447. if expression x is of range type then return true else report error and return false
  4448. **)
  4449. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4450. VAR result: BOOLEAN;
  4451. BEGIN
  4452. result := FALSE;
  4453. IF x = SyntaxTree.invalidExpression THEN
  4454. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4455. Error(x.position,Diagnostics.Invalid,"is no range type");
  4456. ELSE result := TRUE
  4457. END;
  4458. RETURN result
  4459. END CheckRangeType;
  4460. (**
  4461. if expression x is of boolean type then return true else report error and return false
  4462. **)
  4463. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4464. VAR result: BOOLEAN;
  4465. BEGIN
  4466. result := FALSE;
  4467. IF x = SyntaxTree.invalidExpression THEN
  4468. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4469. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4470. ELSE result := TRUE
  4471. END;
  4472. RETURN result
  4473. END CheckBooleanType;
  4474. (**
  4475. if expression x is of set type then return true else report error and return false
  4476. **)
  4477. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4478. VAR result: BOOLEAN;
  4479. BEGIN
  4480. result := FALSE;
  4481. IF x = SyntaxTree.invalidExpression THEN
  4482. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4483. Error(x.position,Diagnostics.Invalid,"is no set type");
  4484. ELSE result := TRUE
  4485. END;
  4486. RETURN result
  4487. END CheckSetType;
  4488. (**
  4489. if expression x is of string or array of character type then return true else report error and return false
  4490. **)
  4491. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4492. VAR result: BOOLEAN;
  4493. BEGIN
  4494. result := FALSE;
  4495. IF x = SyntaxTree.invalidExpression THEN
  4496. ELSIF ~IsStringType(x.type.resolved) THEN
  4497. Error(x.position,Diagnostics.Invalid,"is no string type");
  4498. ELSE result := TRUE
  4499. END;
  4500. RETURN result
  4501. END CheckStringType;
  4502. (**
  4503. if expression x is a type declaration type return true else report error and return false
  4504. **)
  4505. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4506. VAR result: BOOLEAN;
  4507. BEGIN
  4508. result := FALSE;
  4509. IF x = SyntaxTree.invalidExpression THEN
  4510. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4511. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4512. ELSE result := TRUE
  4513. END;
  4514. RETURN result
  4515. END CheckTypeDeclarationType;
  4516. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4517. VAR result: BOOLEAN;
  4518. BEGIN
  4519. result := FALSE;
  4520. IF x = SyntaxTree.invalidExpression THEN
  4521. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4522. result := TRUE;
  4523. value := x.resolved(SyntaxTree.IntegerValue).value;
  4524. ELSE
  4525. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4526. END;
  4527. RETURN result;
  4528. END CheckIntegerValue;
  4529. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4530. VAR result: BOOLEAN;
  4531. BEGIN
  4532. result := FALSE;
  4533. IF x = SyntaxTree.invalidExpression THEN
  4534. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4535. result := TRUE;
  4536. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4537. ELSE
  4538. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4539. END;
  4540. RETURN result;
  4541. END CheckStringValue;
  4542. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4543. BEGIN
  4544. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4545. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4546. ELSE
  4547. RETURN FALSE
  4548. END;
  4549. END IsUnsignedValue;
  4550. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4551. BEGIN
  4552. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4553. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4554. ELSE
  4555. RETURN FALSE
  4556. END
  4557. END IsAddressValue;
  4558. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4559. BEGIN
  4560. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4561. END IsAddressExpression;
  4562. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4563. BEGIN
  4564. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4565. END IsSizeExpression;
  4566. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4567. VAR result: BOOLEAN;
  4568. BEGIN
  4569. result := FALSE;
  4570. IF x = SyntaxTree.invalidExpression THEN
  4571. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4572. result := TRUE;
  4573. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4574. ELSE
  4575. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4576. END;
  4577. RETURN result;
  4578. END CheckEnumerationValue;
  4579. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4585. result := TRUE;
  4586. value := x.resolved(SyntaxTree.CharacterValue).value;
  4587. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4588. result := TRUE;
  4589. value := x.resolved(SyntaxTree.StringValue).value[0];
  4590. ELSE
  4591. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4592. END;
  4593. RETURN result;
  4594. END CheckCharacterValue;
  4595. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4596. VAR result: BOOLEAN;
  4597. BEGIN
  4598. result := FALSE;
  4599. IF x = SyntaxTree.invalidExpression THEN
  4600. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4601. value := x.resolved(SyntaxTree.IntegerValue).value;
  4602. IF (value > 0) OR includeZero & (value = 0) THEN
  4603. result := TRUE;
  4604. ELSE
  4605. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4606. END
  4607. ELSE
  4608. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4609. END;
  4610. RETURN result;
  4611. END CheckPositiveIntegerValue;
  4612. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4613. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4614. BEGIN
  4615. result := FALSE;
  4616. IF x = SyntaxTree.invalidExpression THEN
  4617. ELSE
  4618. type := x.type.resolved;
  4619. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4620. portType := type(SyntaxTree.PortType);
  4621. result := TRUE
  4622. ELSE
  4623. Error(x.position,Diagnostics.Invalid,"no port type");
  4624. END;
  4625. END;
  4626. RETURN result
  4627. END CheckPortType;
  4628. (* move to builtin procedure call statement ?
  4629. remove builtin procedure call designator ?
  4630. *)
  4631. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4632. VAR
  4633. numberActualParameters,numberFormalParameters: LONGINT;
  4634. formalParameter: SyntaxTree.Parameter;
  4635. actualParameter: SyntaxTree.Expression;
  4636. procedureType: SyntaxTree.ProcedureType;
  4637. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4638. inPort, outPort: SyntaxTree.PortType;
  4639. constructor: SyntaxTree.Procedure;
  4640. type0,type1,type2: SyntaxTree.Type;
  4641. type,base,parameterType: SyntaxTree.Type;
  4642. arrayType: SyntaxTree.ArrayType;
  4643. i,i0,i1: LONGINT;
  4644. r,r0,r1,im: LONGREAL;
  4645. c: CHAR;
  4646. id: LONGINT;
  4647. b: BOOLEAN;
  4648. first: LONGINT;
  4649. mathArrayType: SyntaxTree.MathArrayType;
  4650. customBuiltin: SyntaxTree.CustomBuiltin;
  4651. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4652. VAR resultB: BOOLEAN;
  4653. BEGIN
  4654. IF numberActualParameters < from THEN
  4655. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4656. result := SyntaxTree.invalidExpression;
  4657. resultB := FALSE;
  4658. ELSIF numberActualParameters > to THEN
  4659. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4660. result := SyntaxTree.invalidExpression;
  4661. resultB := FALSE;
  4662. ELSE
  4663. resultB := TRUE;
  4664. END;
  4665. RETURN resultB
  4666. END CheckArity;
  4667. BEGIN
  4668. type := NIL; result := NIL;
  4669. type0 := NIL; type1 := NIL; type2 := NIL;
  4670. numberActualParameters := actualParameters.Length();
  4671. IF numberActualParameters>0 THEN
  4672. parameter0 := actualParameters.GetExpression(0);
  4673. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4674. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4675. result := SyntaxTree.invalidExpression
  4676. END
  4677. END;
  4678. IF numberActualParameters >1 THEN
  4679. parameter1 := actualParameters.GetExpression(1);
  4680. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4681. ELSE
  4682. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4683. result := SyntaxTree.invalidExpression
  4684. END
  4685. END;
  4686. IF numberActualParameters >2 THEN
  4687. parameter2 := actualParameters.GetExpression(2);
  4688. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4689. ELSE
  4690. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4691. result := SyntaxTree.invalidExpression
  4692. END
  4693. END;
  4694. IF returnType # NIL THEN
  4695. id := Global.New;
  4696. result := NIL;
  4697. ELSE
  4698. id := builtin.id;
  4699. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4700. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4701. END;
  4702. END;
  4703. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4704. ELSIF result # NIL THEN type := result.type (* operator *)
  4705. ELSE
  4706. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4707. result(SyntaxTree.Designator).SetLeft(left);
  4708. IF returnType # NIL THEN
  4709. type := returnType;
  4710. END;
  4711. (* ---- ASSERT ----- *)
  4712. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4713. IF CheckBooleanType(parameter0) THEN
  4714. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4715. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4716. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4717. *)
  4718. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4719. Error(position,Diagnostics.Invalid,"assert failed");
  4720. END;
  4721. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4722. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4723. rules imposed by the architecture / current runtime
  4724. *)
  4725. END;
  4726. END;
  4727. (* ---- COPY ----- *)
  4728. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4729. IF~IsStringType(type0) THEN
  4730. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4731. END;
  4732. IF ~IsStringType(type1) THEN
  4733. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4734. ELSIF CheckVariable(parameter1) THEN
  4735. IF (type0 IS SyntaxTree.StringType) THEN
  4736. arrayType := type1(SyntaxTree.ArrayType);
  4737. IF arrayType.form = SyntaxTree.Static THEN
  4738. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4739. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4740. END;
  4741. END;
  4742. END;
  4743. END;
  4744. (* ---- INC, DEC----- *)
  4745. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4746. IF numberActualParameters = 1 THEN
  4747. parameter1 :=Global.NewIntegerValue(system,position,1);
  4748. actualParameters.AddExpression(parameter1);
  4749. END;
  4750. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4751. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4752. Error(position,Diagnostics.Invalid,"incompatible increment");
  4753. ELSE
  4754. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4755. actualParameters.SetExpression(1,parameter1);
  4756. END;
  4757. END;
  4758. (* ---- EXCL, INCL----- *)
  4759. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4760. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4761. IF IsIntegerValue(parameter1,i0) THEN
  4762. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4763. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4764. END;
  4765. END;
  4766. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4767. actualParameters.SetExpression(1,parameter1);
  4768. END;
  4769. (* ---- HALT, SYSTEM.HALT ----- *)
  4770. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4771. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4772. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4773. rules imposed by the architecture / current runtime
  4774. *)
  4775. END;
  4776. (* ---- WAIT ----- *)
  4777. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4778. IF CheckObjectType(parameter0) THEN
  4779. END;
  4780. (* ---- NEW ----- *)
  4781. ELSIF (id = Global.New) THEN
  4782. IF returnType # NIL THEN
  4783. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4784. ELSE
  4785. first := 1;
  4786. END;
  4787. IF CheckArity(first,Infinity) THEN
  4788. IF currentIsRealtime THEN
  4789. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4790. END;
  4791. (* check constructor *)
  4792. IF (first =0) OR CheckVariable(parameter0) THEN
  4793. IF type0 IS SyntaxTree.PointerType THEN
  4794. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4795. ELSIF type0 IS SyntaxTree.CellType THEN
  4796. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4797. ELSE
  4798. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4799. END;
  4800. IF type0 IS SyntaxTree.ArrayType THEN
  4801. arrayType := type0(SyntaxTree.ArrayType);
  4802. IF arrayType.form = SyntaxTree.Static THEN
  4803. i := first
  4804. ELSIF arrayType.form = SyntaxTree.Open THEN
  4805. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4806. ELSE HALT(100)
  4807. END;
  4808. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4809. i := first;
  4810. REPEAT
  4811. actualParameter := actualParameters.GetExpression(i);
  4812. IF CheckSizeType(actualParameter) THEN
  4813. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4814. actualParameters.SetExpression(i,actualParameter);
  4815. END;
  4816. INC(i);
  4817. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4818. END;
  4819. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4820. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4821. IF constructor = NIL THEN
  4822. IF CheckArity(first,first) THEN END;
  4823. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4824. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4825. ELSE
  4826. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4827. numberFormalParameters := procedureType.numberParameters;
  4828. IF numberActualParameters-first <= numberFormalParameters THEN
  4829. formalParameter := procedureType.firstParameter;
  4830. FOR i := first TO numberActualParameters-1 DO
  4831. actualParameter := actualParameters.GetExpression(i);
  4832. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4833. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4834. ELSE
  4835. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4836. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4837. END;
  4838. actualParameters.SetExpression(i,actualParameter);
  4839. END;
  4840. formalParameter := formalParameter.nextParameter;
  4841. END;
  4842. WHILE (formalParameter # NIL) DO
  4843. IF formalParameter.defaultValue # NIL THEN
  4844. actualParameters.AddExpression(formalParameter.defaultValue);
  4845. formalParameter := formalParameter.nextParameter
  4846. ELSE
  4847. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4848. formalParameter := NIL;
  4849. END;
  4850. END;
  4851. ELSE
  4852. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4853. END;
  4854. END;
  4855. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4856. mathArrayType := type0(SyntaxTree.MathArrayType);
  4857. IF mathArrayType.form = SyntaxTree.Static THEN
  4858. Error(position,Diagnostics.Invalid,"new on static array");
  4859. ELSE
  4860. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4861. i0 := first+1; i1 := Infinity;
  4862. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4863. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4864. i1 := i0;
  4865. ELSE HALT(100);
  4866. END;
  4867. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4868. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4869. base := ArrayBase(type0,MAX(LONGINT));
  4870. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4871. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4872. IF ~CompatibleTo(system,type0,parameterType) THEN
  4873. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4874. result := SyntaxTree.invalidExpression;
  4875. ELSE
  4876. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4877. END;
  4878. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4879. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4880. IF ~CompatibleTo(system,type1,parameterType) THEN
  4881. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4882. result := SyntaxTree.invalidExpression;
  4883. ELSE
  4884. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4885. END;
  4886. ELSE
  4887. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4888. i := first;
  4889. REPEAT
  4890. actualParameter := actualParameters.GetExpression(i);
  4891. IF CheckSizeType(actualParameter) THEN
  4892. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4893. actualParameters.SetExpression(i,actualParameter);
  4894. END;
  4895. INC(i);
  4896. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4897. END;
  4898. END;
  4899. END;
  4900. ELSIF type0 IS SyntaxTree.CellType THEN
  4901. IF ~(currentIsCellNet) THEN
  4902. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4903. ELSE
  4904. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4905. IF (constructor = NIL) & CheckArity(1,1) THEN
  4906. (* ok *)
  4907. ELSE
  4908. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4909. numberFormalParameters := procedureType.numberParameters;
  4910. DEC(numberActualParameters);
  4911. IF numberActualParameters <= numberFormalParameters THEN
  4912. formalParameter := procedureType.firstParameter;
  4913. FOR i := first TO numberActualParameters DO
  4914. actualParameter := actualParameters.GetExpression(i);
  4915. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4916. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4917. ELSE
  4918. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4919. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4920. END;
  4921. actualParameters.SetExpression(i,actualParameter);
  4922. END;
  4923. formalParameter := formalParameter.nextParameter;
  4924. END;
  4925. WHILE (formalParameter # NIL) DO
  4926. IF formalParameter.defaultValue # NIL THEN
  4927. actualParameters.AddExpression(formalParameter.defaultValue);
  4928. formalParameter := formalParameter.nextParameter
  4929. ELSE
  4930. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4931. formalParameter := NIL;
  4932. END;
  4933. END;
  4934. ELSE
  4935. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4936. END;
  4937. END;
  4938. END;
  4939. activeCellsStatement := TRUE;
  4940. ELSE
  4941. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4942. END;
  4943. END;
  4944. END;
  4945. (* ---- DISPOSE ----- *)
  4946. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4947. IF ~IsPointerType(parameter0.type) THEN
  4948. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4949. ELSIF ~IsDisposable(parameter0.type) THEN
  4950. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4951. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4952. END
  4953. (* ---- GETPROCEDURE ----- *)
  4954. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4955. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4956. IF CheckVariable(parameter2) THEN
  4957. IF ~GetProcedureAllowed(parameter2.type) THEN
  4958. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4959. END;
  4960. END;
  4961. END;
  4962. (* ---- ABS ----- *)
  4963. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4964. (* note: ABS on complex numbers is done using overloading *)
  4965. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4966. type := type0;
  4967. IF IsIntegerValue(parameter0,i0) THEN
  4968. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4969. type := Global.GetIntegerType(system,ABS(i0));
  4970. ELSIF IsRealValue(parameter0,r) THEN
  4971. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4972. END;
  4973. ELSE
  4974. type := SyntaxTree.invalidType;
  4975. END;
  4976. (* ---- ASH, ASR ----- *)
  4977. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4978. type := type0;
  4979. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4980. (*
  4981. ConvertOperands(parameter0,parameter1); (* same type *)
  4982. *)
  4983. type := parameter0.type;
  4984. IF IsIntegerValue(parameter0,i0) THEN
  4985. IF IsIntegerValue(parameter1,i1) THEN
  4986. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4987. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4988. result := ResolveExpression(result);
  4989. type := Global.GetIntegerType(system,i0);
  4990. END;
  4991. END;
  4992. IF type.resolved.sizeInBits < 32 THEN
  4993. type := system.longintType;
  4994. END;
  4995. (*!compatibility with release, remove when resolved
  4996. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4997. *)
  4998. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4999. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5000. actualParameters.SetExpression(0,parameter0);
  5001. actualParameters.SetExpression(1,parameter1);
  5002. END;
  5003. (* ---- CAP ----- *)
  5004. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5005. type := system.characterType;
  5006. IF CheckCharacterType (parameter0) THEN
  5007. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5008. actualParameters.SetExpression(0,parameter0);
  5009. IF IsCharacterValue(parameter0,c) THEN
  5010. IF (c <= "z") & (c >= "a") THEN
  5011. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5012. ELSE
  5013. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5014. END;
  5015. END;
  5016. END;
  5017. (* ---- CHR ----- *)
  5018. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5019. IF id = Global.Chr THEN
  5020. type := system.characterType
  5021. ELSE
  5022. type := system.characterType32
  5023. END;
  5024. IF CheckIntegerType(parameter0) THEN
  5025. IF IsIntegerValue(parameter0,i0) THEN
  5026. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5027. result := ResolveExpression(result);
  5028. ELSE
  5029. (*
  5030. result := NewConversion(parameter0.position,parameter0,type);
  5031. *)
  5032. END;
  5033. END
  5034. (* ---- ENTIER ----- *)
  5035. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5036. type := system.longintType;
  5037. IF CheckRealType(parameter0) THEN
  5038. IF IsRealValue(parameter0,r) THEN
  5039. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5040. type := Global.GetIntegerType(system,ENTIER(r));
  5041. END
  5042. END;
  5043. (* ---- ENTIERH ----- *)
  5044. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5045. type := system.hugeintType;
  5046. IF CheckRealType(parameter0) THEN
  5047. IF IsRealValue(parameter0,r) THEN
  5048. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5049. END
  5050. END;
  5051. (* ---- LEN ----- *)
  5052. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5053. type := system.longintType;
  5054. base := type0;
  5055. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5056. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5057. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5058. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5059. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5060. END;
  5061. type0 := base;
  5062. ELSE
  5063. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5064. type0 := parameter0.type.resolved;
  5065. actualParameters.SetExpression(0,parameter0);
  5066. base := type0;
  5067. END;
  5068. END;
  5069. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5070. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5071. IF i1 < 0 THEN
  5072. Error(position,Diagnostics.Invalid,"invalid dimension");
  5073. base := SyntaxTree.invalidType;
  5074. ELSE
  5075. base := ArrayBase(base,i1);
  5076. IF (base # NIL) & Indexable(base) THEN
  5077. ELSE
  5078. Error(position,Diagnostics.Invalid,"len on no array");
  5079. IF VerboseErrorMessage THEN
  5080. Printout.Info("base",base);
  5081. END;
  5082. base := SyntaxTree.invalidType;
  5083. END;
  5084. END;
  5085. IF numberActualParameters=2 THEN
  5086. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5087. actualParameters.SetExpression(1,parameter1);
  5088. ELSIF base IS SyntaxTree.MathArrayType THEN
  5089. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5090. END;
  5091. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5092. IF base IS SyntaxTree.ArrayType THEN
  5093. arrayType := base(SyntaxTree.ArrayType);
  5094. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5095. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5096. result := Global.NewIntegerValue(system,position,i);
  5097. type := result.type;(* arrayType.length.type;*)
  5098. ASSERT(type # NIL);
  5099. END;
  5100. ELSIF base IS SyntaxTree.MathArrayType THEN
  5101. mathArrayType := base(SyntaxTree.MathArrayType);
  5102. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5103. result := Global.NewIntegerValue(system,position,i);
  5104. type := result.type;
  5105. (*
  5106. type := mathArrayType.length.type;
  5107. *)
  5108. ASSERT(type # NIL);
  5109. END;
  5110. END;
  5111. END;
  5112. ELSE
  5113. type := system.longintType;
  5114. END;
  5115. (* ---- FIRST ---- *)
  5116. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5117. type := system.longintType;
  5118. IF CheckRangeType(parameter0) THEN END;
  5119. result.SetAssignable(parameter0.assignable)
  5120. (* ---- LAST ---- *)
  5121. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5122. type := system.longintType;
  5123. IF CheckRangeType(parameter0) THEN END;
  5124. result.SetAssignable(parameter0.assignable)
  5125. (* ---- STEP ---- *)
  5126. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5127. type := system.longintType;
  5128. IF CheckRangeType(parameter0) THEN END;
  5129. result.SetAssignable(parameter0.assignable)
  5130. (* ---- RE ---- *)
  5131. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5132. IF CheckNumberType(parameter0) THEN
  5133. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5134. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5135. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5136. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5137. type := parameter0.type
  5138. ELSE
  5139. type := system.realType
  5140. END
  5141. END;
  5142. result.SetAssignable(parameter0.assignable)
  5143. (* ---- IM ---- *)
  5144. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5145. IF CheckNumberType(parameter0) THEN
  5146. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5147. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5148. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5149. ELSE
  5150. type := system.realType;
  5151. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5152. END
  5153. END;
  5154. result.SetAssignable(parameter0.assignable)
  5155. (* ---- MAX ----- *)
  5156. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5157. IF numberActualParameters = 1 THEN
  5158. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5159. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5160. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5161. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5162. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5163. *)
  5164. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5165. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5166. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5167. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5168. ELSE Error(parameter0.position,Diagnostics.Invalid, "builtin function not applicable to this type");
  5169. END;
  5170. ELSE
  5171. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5172. END
  5173. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5174. ConvertOperands(parameter0,parameter1);
  5175. actualParameters.SetExpression(0,parameter0);
  5176. actualParameters.SetExpression(1,parameter1);
  5177. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5178. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5179. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5180. END;
  5181. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5182. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5183. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5184. END;
  5185. END;
  5186. type := parameter0.type;
  5187. ELSE type := SyntaxTree.invalidType;
  5188. END;
  5189. (* ---- MIN ----- *)
  5190. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5191. IF numberActualParameters = 1 THEN
  5192. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5193. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5194. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5195. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5196. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5197. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5198. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5199. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5200. END;
  5201. ELSE
  5202. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5203. END
  5204. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5205. ConvertOperands(parameter0,parameter1);
  5206. actualParameters.SetExpression(0,parameter0);
  5207. actualParameters.SetExpression(1,parameter1);
  5208. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5209. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5210. ELSE result.SetResolved(parameter1.resolved)
  5211. END;
  5212. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5213. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5214. ELSE result.SetResolved(parameter1.resolved)
  5215. END;
  5216. END;
  5217. type := parameter0.type;
  5218. ELSE type := SyntaxTree.invalidType;
  5219. END;
  5220. (* ---- ODD ----- *)
  5221. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5222. type := system.booleanType;
  5223. IF CheckIntegerType(parameter0) THEN
  5224. IF IsIntegerValue(parameter0,i0) THEN
  5225. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5226. type := system.booleanType;
  5227. END;
  5228. END;
  5229. (* ---- ORD ----- *)
  5230. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5231. IF id = Global.Ord THEN
  5232. type := system.integerType;
  5233. ELSE
  5234. type := system.longintType;
  5235. END;
  5236. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5237. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5238. actualParameters.SetExpression(0,parameter0);
  5239. (* IF CheckCharacterType(parameter0) THEN*)
  5240. IF IsCharacterValue(parameter0,c)THEN
  5241. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5242. type := Global.GetSignedIntegerType(system,ORD(c));
  5243. END;
  5244. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5245. END;
  5246. (* ---- SHORT ----- *)
  5247. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5248. type := type0;
  5249. IF IsSignedIntegerType(type) THEN
  5250. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5251. ELSIF type = system.integerType THEN type := system.shortintType
  5252. ELSIF type = system.longintType THEN type := system.integerType
  5253. ELSIF type = system.hugeintType THEN type:= system.longintType
  5254. ELSE
  5255. CASE type.sizeInBits OF
  5256. 16: type := Global.Integer8
  5257. |32: type := Global.Integer16
  5258. |64: type := Global.Integer32
  5259. END;
  5260. END;
  5261. ELSIF type IS SyntaxTree.FloatType THEN
  5262. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5263. ELSIF type = system.longrealType THEN type := system.realType
  5264. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5265. END;
  5266. ELSIF type IS SyntaxTree.ComplexType THEN
  5267. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5268. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5269. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5270. END;
  5271. ELSE
  5272. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5273. END;
  5274. IF (parameter0.resolved # NIL) THEN
  5275. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5276. IF parameter0 IS SyntaxTree.Value THEN
  5277. result.SetResolved(parameter0(SyntaxTree.Value));
  5278. END;
  5279. END;
  5280. (* ---- LONG ----- *)
  5281. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5282. type := type0;
  5283. IF IsSignedIntegerType(type) THEN
  5284. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5285. ELSIF type = system.longintType THEN type := system.hugeintType
  5286. ELSIF type = system.integerType THEN type := system.longintType
  5287. ELSIF type = system.shortintType THEN type := system.integerType
  5288. ELSE
  5289. CASE type.sizeInBits OF
  5290. 8: type := Global.Integer16
  5291. |16: type := Global.Integer32
  5292. |32: type := Global.Integer64
  5293. END;
  5294. END;
  5295. ELSIF type IS SyntaxTree.FloatType THEN
  5296. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5297. ELSIF type= system.realType THEN type := system.longrealType
  5298. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5299. END;
  5300. ELSIF type IS SyntaxTree.ComplexType THEN
  5301. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5302. ELSIF type = system.complexType THEN type := system.longcomplexType
  5303. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5304. END;
  5305. ELSE
  5306. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5307. END;
  5308. IF (parameter0.resolved # NIL) THEN
  5309. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5310. IF parameter0 IS SyntaxTree.Value THEN
  5311. result.SetResolved(parameter0(SyntaxTree.Value));
  5312. END;
  5313. END;
  5314. (* ---- SIZE OF ----- *)
  5315. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5316. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5317. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5318. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5319. type := system.integerType;
  5320. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5321. ELSE
  5322. (* for variables, system sizeof could represent the physically occupied size
  5323. determined via the type descriptor, implement that ? *)
  5324. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5325. END
  5326. (* ---- SYSTEM.TRACE -----*)
  5327. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5328. FOR i := 0 TO numberActualParameters-1 DO
  5329. parameter0 := actualParameters.GetExpression(i);
  5330. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5331. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5332. END;
  5333. END;
  5334. (* remaining issues can only be tested in backend *)
  5335. (* ---- ADDRESSOF----- *)
  5336. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5337. IF HasAddress(parameter0) THEN
  5338. type := system.addressType;
  5339. ELSE
  5340. type := SyntaxTree.invalidType;
  5341. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5342. END;
  5343. (* ---- BIT ----- *)
  5344. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5345. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5346. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5347. actualParameters.SetExpression(0,parameter0);
  5348. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5349. actualParameters.SetExpression(1,parameter1);
  5350. END;
  5351. type := system.booleanType;
  5352. (* ----- MSK ---- *)
  5353. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5354. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5355. ConvertOperands(parameter0,parameter1);
  5356. actualParameters.SetExpression(0,parameter0);
  5357. actualParameters.SetExpression(1,parameter1);
  5358. END;
  5359. type := parameter0.type;
  5360. (* ---- SYSTEM.GET64 ----- *)
  5361. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5362. IF CheckAddressType(parameter0) THEN
  5363. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5364. actualParameters.SetExpression(0,parameter0);
  5365. END;
  5366. type := system.hugeintType;
  5367. (* ---- SYSTEM.GET32 ----- *)
  5368. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5369. IF CheckAddressType(parameter0) THEN
  5370. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5371. actualParameters.SetExpression(0,parameter0);
  5372. END;
  5373. type := system.longintType;
  5374. (* ---- SYSTEM.GET16 ----- *)
  5375. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5376. IF CheckAddressType(parameter0) THEN
  5377. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5378. actualParameters.SetExpression(0,parameter0);
  5379. END;
  5380. type := system.integerType;
  5381. (* ---- SYSTEM.GET8 ----- *)
  5382. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5383. IF CheckAddressType(parameter0) THEN
  5384. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5385. actualParameters.SetExpression(0,parameter0);
  5386. END;
  5387. type := system.shortintType;
  5388. (* ---- SYSTEM.GetStackPointer ----- *)
  5389. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5390. type := system.addressType;
  5391. (* ---- SYSTEM.GetFramePointer ----- *)
  5392. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5393. type := system.addressType;
  5394. (* ---- SYSTEM.GetActivity ----- *)
  5395. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5396. type := system.objectType;
  5397. (* ---- SYSTEM.SetStackPointer ----- *)
  5398. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5399. IF CheckAddressType(parameter0) THEN
  5400. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5401. actualParameters.SetExpression(0,parameter0);
  5402. END;
  5403. (* ---- SYSTEM.SetFramePointer ----- *)
  5404. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5405. IF CheckAddressType(parameter0) THEN
  5406. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5407. actualParameters.SetExpression(0,parameter0);
  5408. END;
  5409. (* ---- SYSTEM.SetActivity ----- *)
  5410. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5411. IF CheckObjectType(parameter0) THEN
  5412. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5413. actualParameters.SetExpression(0,parameter0);
  5414. END;
  5415. (* ---- LSH, LSL, ROT, ROR ----- *)
  5416. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5417. type := type0;
  5418. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5419. actualParameters.SetExpression(1, parameter1);
  5420. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5421. IF id = Global.Lsh THEN
  5422. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5423. ELSIF id = Global.Rot THEN
  5424. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5425. ELSIF id = Global.Ror THEN
  5426. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5427. END;
  5428. END;
  5429. (* ---- SYSTEM.VAL ----- *)
  5430. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5431. IF CheckTypeDeclarationType(parameter0) THEN
  5432. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5433. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5434. result := SyntaxTree.invalidExpression;
  5435. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5436. ELSE
  5437. IF (parameter1.resolved # NIL) THEN
  5438. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5439. IF parameter0 IS SyntaxTree.Value THEN
  5440. result.SetResolved(parameter0(SyntaxTree.Value));
  5441. END;
  5442. END;
  5443. result.SetAssignable(parameter1.assignable);
  5444. END;
  5445. END;
  5446. (* ---- SYSTEM.GET ----- *)
  5447. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5448. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5449. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5450. actualParameters.SetExpression(0,parameter0);
  5451. END;
  5452. (* ---- SYSTEM.PUT ----- *)
  5453. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5454. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5455. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5456. actualParameters.SetExpression(0,parameter0);
  5457. END;
  5458. (* ---- SYSTEM.PUT64 ----- *)
  5459. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5460. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5461. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5462. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5463. actualParameters.SetExpression(0,parameter0);
  5464. actualParameters.SetExpression(1,parameter1);
  5465. END;
  5466. (* ---- SYSTEM.PUT32 ----- *)
  5467. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5468. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5469. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5470. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5471. actualParameters.SetExpression(0,parameter0);
  5472. actualParameters.SetExpression(1,parameter1);
  5473. END;
  5474. (* ---- SYSTEM.PUT16 ----- *)
  5475. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5476. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5477. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5478. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5479. actualParameters.SetExpression(0,parameter0);
  5480. actualParameters.SetExpression(1,parameter1);
  5481. END;
  5482. (* ---- SYSTEM.PUT8 ----- *)
  5483. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5484. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5485. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5486. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5487. actualParameters.SetExpression(0,parameter0);
  5488. actualParameters.SetExpression(1,parameter1);
  5489. END;
  5490. (* ---- SYSTEM.MOVE ----- *)
  5491. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5492. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5493. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5494. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5495. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5496. actualParameters.SetExpression(0,parameter0);
  5497. actualParameters.SetExpression(1,parameter1);
  5498. actualParameters.SetExpression(2,parameter2);
  5499. END;
  5500. (* ---- SYSTEM.NEW ----- *)
  5501. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5502. IF ~IsPointerType(parameter0.type) THEN
  5503. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5504. ELSIF CheckSizeType(parameter1) THEN
  5505. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5506. actualParameters.SetExpression(1,parameter1);
  5507. END;
  5508. (* ----SYSTEM.REF ---- *)
  5509. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5510. type := system.addressType
  5511. (* ---- INCR ----- *)
  5512. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5513. type := system.sizeType;
  5514. base := type0;
  5515. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5516. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5517. IF i1 < 0 THEN
  5518. Error(position,Diagnostics.Invalid,"invalid dimension");
  5519. base := SyntaxTree.invalidType;
  5520. ELSE
  5521. base := ArrayBase(base,i1);
  5522. IF (base # NIL) & Indexable(base) THEN
  5523. ELSE
  5524. Error(position,Diagnostics.Invalid,"len on no array");
  5525. IF VerboseErrorMessage THEN
  5526. Printout.Info("base",base);
  5527. END;
  5528. base := SyntaxTree.invalidType;
  5529. END;
  5530. END;
  5531. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5532. actualParameters.SetExpression(1,parameter1);
  5533. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5534. mathArrayType := base(SyntaxTree.MathArrayType);
  5535. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5536. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5537. type := system.longintType;
  5538. END;
  5539. END;
  5540. ELSE
  5541. type := system.longintType;
  5542. END;
  5543. (* ---- SUM ----- *)
  5544. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5545. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5546. (* ---- ALL ----- *)
  5547. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5548. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5549. (* ---- DIM ----- *)
  5550. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5551. type := system.sizeType;
  5552. IF type0 IS SyntaxTree.MathArrayType THEN
  5553. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5554. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5555. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5556. END;
  5557. ELSE
  5558. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5559. END;
  5560. (* ---- CAS ----- *)
  5561. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5562. IF type0.IsComposite () THEN
  5563. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5564. result := SyntaxTree.invalidExpression;
  5565. ELSIF ~IsVariable (parameter0) THEN
  5566. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5567. result := SyntaxTree.invalidExpression;
  5568. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5569. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5570. result := SyntaxTree.invalidExpression;
  5571. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5572. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5573. result := SyntaxTree.invalidExpression;
  5574. ELSE
  5575. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5576. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5577. type := type0;
  5578. END;
  5579. (* ---- RESHAPE ----- *)
  5580. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5581. IF type0 IS SyntaxTree.MathArrayType THEN
  5582. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5583. base := ArrayBase(type0,MAX(LONGINT));
  5584. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5585. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5586. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5587. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5588. IF ~CompatibleTo(system,type0,parameterType) THEN
  5589. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5590. result := SyntaxTree.invalidExpression;
  5591. ELSE
  5592. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5593. END;
  5594. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5595. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5596. IF ~CompatibleTo(system,type1,parameterType) THEN
  5597. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5598. result := SyntaxTree.invalidExpression;
  5599. ELSE
  5600. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5601. END;
  5602. ELSE
  5603. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5604. result := SyntaxTree.invalidExpression;
  5605. END;
  5606. (* ---- SYSTEM.TYPECODE ----- *)
  5607. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5608. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5609. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5610. type := type.resolved;
  5611. IF type IS SyntaxTree.PointerType THEN
  5612. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5613. END;
  5614. IF ~(type IS SyntaxTree.RecordType) THEN
  5615. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5616. END;
  5617. ELSE
  5618. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5619. END;
  5620. type := system.addressType;
  5621. (* -------- FLT --------- *)
  5622. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5623. type := system.realType;
  5624. IF IsRealValue(parameter0, r) THEN
  5625. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5626. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5627. i0 := i; i := ABS(i);
  5628. IF i # 0 THEN
  5629. i1 := 23;
  5630. IF i >= 2*800000H THEN
  5631. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5632. ELSIF i < 800000H THEN
  5633. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5634. END;
  5635. i := (i1 + 127)*800000H - 800000H + i;
  5636. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5637. END;
  5638. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5639. END;
  5640. (* ------- CONNECT -------*)
  5641. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5642. (*IF ~(currentIsCellNet) THEN
  5643. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5644. END;*)
  5645. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5646. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5647. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5648. END;
  5649. IF numberActualParameters = 3 THEN
  5650. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5651. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5652. END;
  5653. *)
  5654. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5655. actualParameters.SetExpression(2,parameter2);
  5656. END;
  5657. activeCellsStatement := TRUE;
  5658. (* ---------- DELEGATE --------*)
  5659. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5660. (*
  5661. IF ~(currentIsCellNet) THEN
  5662. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5663. END;
  5664. *)
  5665. IF ~CheckPortType(parameter1, inPort) THEN
  5666. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5667. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5668. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5669. ELSIF (outPort.direction # inPort.direction) THEN
  5670. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5671. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5672. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5673. END;
  5674. activeCellsStatement := TRUE;
  5675. (* --------- RECEIVE ---------*)
  5676. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5677. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5678. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5679. IF inPort.direction # SyntaxTree.InPort THEN
  5680. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5681. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5682. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5683. END;
  5684. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5685. IF ~SameType(parameter2.type, system.integerType) THEN
  5686. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5687. END;
  5688. END;
  5689. END;
  5690. (* --------- SEND ---------*)
  5691. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5692. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5693. IF CheckPortType(parameter0,outPort) THEN
  5694. IF outPort.direction # SyntaxTree.OutPort THEN
  5695. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5696. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5697. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5698. ELSE
  5699. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5700. actualParameters.SetExpression(1,parameter1);
  5701. END;
  5702. END;
  5703. (* ------- custom builtins ----- *)
  5704. ELSIF id = Global.systemSpecial THEN
  5705. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5706. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5707. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5708. type := procedureType.returnType;
  5709. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5710. (* go through all formal parameters *)
  5711. formalParameter := procedureType.firstParameter;
  5712. FOR i := 0 TO actualParameters.Length() - 1 DO
  5713. actualParameter := actualParameters.GetExpression(i);
  5714. IF actualParameter = SyntaxTree.invalidExpression THEN
  5715. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5716. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5717. ELSE
  5718. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5719. END;
  5720. actualParameters.SetExpression(i, actualParameter);
  5721. formalParameter := formalParameter.nextParameter
  5722. END
  5723. END
  5724. ELSE
  5725. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5726. result := SyntaxTree.invalidExpression;
  5727. END;
  5728. END;
  5729. IF result # SyntaxTree.invalidExpression THEN
  5730. type := ResolveType(type);
  5731. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5732. result.SetType(type);
  5733. END;
  5734. RETURN result
  5735. END NewBuiltinCallDesignator;
  5736. (** return type guard designator left(type)
  5737. - check if type can be extended (i.e. is no static record)
  5738. - check if type is a type extension of left.type
  5739. - returns new type guard designator
  5740. returns invalidDesignator = invalidExpression if error
  5741. **)
  5742. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5743. VAR result: SyntaxTree.Designator;
  5744. BEGIN
  5745. result := SyntaxTree.invalidDesignator;
  5746. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5747. Error(position,Diagnostics.Invalid,"no type extension of type");
  5748. IF VerboseErrorMessage THEN
  5749. Printout.Info("left",left);
  5750. Printout.Info("type",type);
  5751. END;
  5752. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5753. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5754. ELSIF IsUnsafePointer(left.type) THEN
  5755. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5756. ELSE
  5757. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5758. result.SetType(type);
  5759. result.SetAssignable(left.assignable);
  5760. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5761. END;
  5762. RETURN result
  5763. END NewTypeGuardDesignator;
  5764. (** check and resolve parameter designator left(expression list)
  5765. - check expression list
  5766. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5767. - elsif left is a procedure type then
  5768. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5769. - else return is a procedure call then return ProcedureCallDesignator
  5770. returns invalidDesignator = invalidExpression if error
  5771. **)
  5772. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5773. VAR
  5774. parameters: SyntaxTree.ExpressionList;
  5775. left: SyntaxTree.Designator;
  5776. result,expression: SyntaxTree.Expression;
  5777. typeDeclaration: SyntaxTree.TypeDeclaration;
  5778. type, expressionType: SyntaxTree.Type;
  5779. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5780. BEGIN
  5781. type := type.resolved;
  5782. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5783. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5784. END;
  5785. RETURN type
  5786. END BaseType;
  5787. BEGIN
  5788. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5789. result := SyntaxTree.invalidDesignator;
  5790. left := ResolveDesignator(designator.left);
  5791. IF left # SyntaxTree.invalidDesignator THEN
  5792. parameters := designator.parameters;
  5793. IF ExpressionList(parameters) THEN
  5794. IF (left.type = NIL) THEN
  5795. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5796. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5797. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5798. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5799. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5800. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5801. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5802. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5803. ELSE
  5804. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5805. END
  5806. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5807. expression := parameters.GetExpression(0);
  5808. type := typeDeclaration.declaredType.resolved;
  5809. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5810. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5811. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5812. OR (expressionType IS SyntaxTree.EnumerationType)
  5813. ) THEN
  5814. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5815. ELSE
  5816. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5817. END;
  5818. ELSE
  5819. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5820. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5821. result := SyntaxTree.invalidDesignator;
  5822. END;
  5823. ELSE
  5824. result := SyntaxTree.invalidDesignator
  5825. END;
  5826. END;
  5827. resolvedExpression := result;
  5828. END VisitParameterDesignator;
  5829. (** check dereference designator left^
  5830. - check if left is pointer type or left is object type
  5831. - return new dereference designator with type = left.baseType.type (if appropriate)
  5832. with error handling
  5833. returns invalidDesignator = invalidExpression if error
  5834. **)
  5835. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5836. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5837. BEGIN
  5838. result := SyntaxTree.invalidDesignator;
  5839. type := left.type;
  5840. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5841. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5842. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5843. result.SetAssignable(TRUE);
  5844. result.SetType(type);
  5845. result.SetHidden(left.isHidden);
  5846. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5847. type := type.resolved;
  5848. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5849. result.SetAssignable(TRUE);
  5850. result.SetType(type);
  5851. result.SetHidden(left.isHidden);
  5852. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5853. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5854. result.SetAssignable(TRUE);
  5855. result.SetType(type);
  5856. result.SetHidden(left.isHidden);
  5857. ELSE
  5858. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5859. IF VerboseErrorMessage THEN
  5860. Printout.Info("pointer", type);
  5861. Printout.Info("scope", currentScope);
  5862. END;
  5863. END;
  5864. RETURN result
  5865. END NewDereferenceDesignator;
  5866. (** check supercall designator left^
  5867. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5868. - return new supercall designator with type = left.type
  5869. with error handling
  5870. **)
  5871. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5872. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5873. objectScope: SyntaxTree.Scope;
  5874. BEGIN
  5875. result := SyntaxTree.invalidDesignator;
  5876. IF left = SyntaxTree.invalidDesignator THEN
  5877. (* error already handled *)
  5878. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5879. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5880. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5881. IF symbol IS SyntaxTree.Procedure THEN
  5882. procedure := symbol(SyntaxTree.Procedure);
  5883. objectScope := currentScope;
  5884. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5885. objectScope := objectScope.outerScope;
  5886. END;
  5887. IF (left.left = NIL) OR ~
  5888. (
  5889. (left.left IS SyntaxTree.SelfDesignator) OR
  5890. (left.left IS SyntaxTree.DereferenceDesignator)
  5891. & (left.left(SyntaxTree.Designator).left # NIL)
  5892. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5893. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5894. IF VerboseErrorMessage THEN
  5895. Printout.Info("left.left",left.left);
  5896. END;
  5897. ELSIF procedure.super # NIL THEN
  5898. result := SyntaxTree.NewSupercallDesignator(position,left);
  5899. result.SetType(left.type.resolved)
  5900. ELSE
  5901. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5902. END;
  5903. ELSE
  5904. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5905. END;
  5906. ELSE
  5907. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5908. END;
  5909. RETURN result
  5910. END NewSupercallDesignator;
  5911. (** check and semantically resolve arrow designator left^
  5912. - if left is procedure type -> result := SupercallDesignator
  5913. - else result := DereferenceDesignator
  5914. returns result via global variable resolvedExpression
  5915. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5916. **)
  5917. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5918. VAR left: SyntaxTree.Designator;
  5919. BEGIN
  5920. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5921. left := ResolveDesignator(arrowDesignator.left);
  5922. IF left # NIL THEN
  5923. IF (left.type = NIL) THEN
  5924. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5925. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5926. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5927. ELSE
  5928. IF IsPointerToObject(left.type) THEN
  5929. Warning(arrowDesignator.position, "forbidden dereference on object");
  5930. END;
  5931. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5932. END
  5933. END
  5934. END VisitArrowDesignator;
  5935. (** check and return expression
  5936. - if expression has no type then resolve expression
  5937. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5938. - return result
  5939. **)
  5940. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5941. VAR result,prev: SyntaxTree.Expression;
  5942. BEGIN
  5943. IF expression = NIL THEN result := NIL
  5944. ELSIF (expression.type = NIL) THEN
  5945. prev := resolvedExpression;
  5946. resolvedExpression := SyntaxTree.invalidExpression;
  5947. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5948. expression.SetType(SyntaxTree.invalidType);
  5949. END;
  5950. expression.Accept(SELF);
  5951. result := resolvedExpression;
  5952. IF currentIsRealtime THEN
  5953. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5954. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5955. END;
  5956. END;
  5957. (* designator modifiers for backends if they support it ...*)
  5958. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5959. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5960. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5961. END;
  5962. resolvedExpression := prev
  5963. ELSE
  5964. result := expression
  5965. END;
  5966. RETURN result
  5967. END ResolveExpression;
  5968. (**
  5969. check expression to be constant expression
  5970. - resolve expression
  5971. - if valid then check that of value type
  5972. report error and return invalidExpression if anything fails
  5973. **)
  5974. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5975. VAR position: Position;
  5976. BEGIN
  5977. position := expression.position;
  5978. expression := ResolveExpression(expression);
  5979. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5980. ELSIF (expression.resolved = NIL) THEN
  5981. Error(position,Diagnostics.Invalid,"expression is not constant");
  5982. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5983. expression := SyntaxTree.invalidExpression;
  5984. END;
  5985. RETURN expression
  5986. END ConstantExpression;
  5987. (** check expression to be constant integer
  5988. - resolve expresssion
  5989. - if valid then check that of integer value type
  5990. report error and return invalidExpression if anything fails
  5991. **)
  5992. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5993. VAR position: Position;
  5994. BEGIN
  5995. position := expression.position;
  5996. expression := ResolveExpression(expression);
  5997. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5998. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5999. expression := SyntaxTree.invalidExpression;
  6000. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  6001. END;
  6002. RETURN expression
  6003. END ConstantInteger;
  6004. (** check expression as positive (>=0) constant integer
  6005. - resolve expression
  6006. - if valid then check that integer value
  6007. - if integer value then check that value >= 0
  6008. report error and return invalidExpression if anything fails
  6009. **)
  6010. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6011. VAR position: Position;
  6012. BEGIN
  6013. position := expression.position;
  6014. expression := ConstantExpression(expression);
  6015. IF expression = SyntaxTree.invalidExpression THEN
  6016. (* error already reported *)
  6017. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6018. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  6019. expression := SyntaxTree.invalidExpression
  6020. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6021. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6022. END;
  6023. RETURN expression
  6024. END ConstantIntegerGeq0;
  6025. (** check expression as condition
  6026. - resolve expression
  6027. - if valid expression then check that result type is boolean
  6028. report error and return invalidExpression if anything fails
  6029. **)
  6030. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6031. VAR position: Position;
  6032. BEGIN
  6033. position := expression.position;
  6034. expression := ResolveExpression(expression);
  6035. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6036. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6037. expression := SyntaxTree.invalidExpression;
  6038. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6039. END;
  6040. RETURN expression
  6041. END ResolveCondition;
  6042. (*** symbols ***)
  6043. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6044. BEGIN
  6045. x.Accept(SELF);
  6046. END ResolveSymbol;
  6047. (** check a symbol
  6048. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6049. **)
  6050. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6051. VAR scope: SyntaxTree.Scope;
  6052. BEGIN
  6053. (* visibility *)
  6054. scope := symbol.scope;
  6055. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6056. scope := scope.outerScope;
  6057. END;
  6058. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6059. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6060. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6061. IF VerboseErrorMessage THEN
  6062. Printout.Info("symbol",symbol);
  6063. END;
  6064. END;
  6065. END;
  6066. END CheckSymbolVisibility;
  6067. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6068. If node is currently being resolved then emit a cyclic definition error.
  6069. Return TRUE only if node is fully resolved.
  6070. **)
  6071. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6072. VAR result: BOOLEAN;
  6073. BEGIN
  6074. IF SyntaxTree.Resolved IN x.state THEN
  6075. result := FALSE
  6076. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6077. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6078. result := FALSE;
  6079. ELSE
  6080. result := TRUE;
  6081. x.SetState(SyntaxTree.BeingResolved)
  6082. END;
  6083. RETURN result
  6084. END SymbolNeedsResolution;
  6085. (** check and resolve a type declaration symbol = Type
  6086. - set type to declaration type
  6087. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6088. This is so because the type declaration itself does not have a type but it only stands for a type.
  6089. In the implementation of the compiler this made a lot much easier.
  6090. - resolve and set declared type
  6091. - check symbol
  6092. **)
  6093. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6094. VAR prevScope: SyntaxTree.Scope;
  6095. BEGIN
  6096. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6097. IF SymbolNeedsResolution(typeDeclaration) THEN
  6098. prevScope := currentScope;
  6099. currentScope := typeDeclaration.scope;
  6100. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6101. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6102. CheckSymbolVisibility(typeDeclaration);
  6103. typeDeclaration.SetState(SyntaxTree.Resolved);
  6104. currentScope := prevScope;
  6105. END;
  6106. END VisitTypeDeclaration;
  6107. (** check and resolve a constant declaration symbol = (constant) expression
  6108. - check expression
  6109. - set type and value
  6110. - check symbol
  6111. **)
  6112. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6113. VAR
  6114. expression: SyntaxTree.Expression;
  6115. type: SyntaxTree.Type;
  6116. name: Basic.SegmentedName;
  6117. replacement: Replacement;
  6118. BEGIN
  6119. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6120. IF SymbolNeedsResolution(constant) THEN
  6121. expression := constant.value;
  6122. IF replacements # NIL THEN
  6123. Global.GetSymbolSegmentedName(constant, name);
  6124. replacement := replacements;
  6125. WHILE (replacement # NIL) & (replacement.name # name) DO
  6126. replacement := replacement.next;
  6127. END;
  6128. IF replacement # NIL THEN
  6129. InfoSS(constant.position, "replacing constant", constant.name);
  6130. (*
  6131. NEW(stringReader, Strings.Length(replacement.string^));
  6132. stringReader.Set(replacement.string^);
  6133. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6134. NEW(parser, scanner, diagnostics);
  6135. expression := parser.Expression();
  6136. *)
  6137. expression := replacement.expression;
  6138. replacement.used := TRUE;
  6139. END;
  6140. END;
  6141. constant.SetType(SyntaxTree.invalidType);
  6142. expression := ConstantExpression(expression);
  6143. ASSERT(expression.type # NIL);
  6144. type := expression.type.resolved;
  6145. constant.SetType(type);
  6146. constant.SetValue(expression);
  6147. CheckSymbolVisibility(constant);
  6148. constant.SetState(SyntaxTree.Resolved);
  6149. END;
  6150. END VisitConstant;
  6151. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6152. VAR procedureAlignment: LONGINT;
  6153. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6154. (* least common multiple *)
  6155. VAR a,b: LONGINT;
  6156. BEGIN
  6157. a := a0; b := b0;
  6158. WHILE (a # b) DO
  6159. IF a < b THEN a := a+a0
  6160. ELSE b := b + b0
  6161. END;
  6162. END;
  6163. RETURN a
  6164. END LCM;
  6165. BEGIN
  6166. IF alignment > 1 THEN
  6167. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6168. IF (procedureAlignment > 1) THEN
  6169. alignment := LCM(alignment, procedureAlignment);
  6170. END;
  6171. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6172. END;
  6173. END AdaptStackAlignment;
  6174. (** check and resolve a variable / field
  6175. - check and set type
  6176. - negative check on open array type
  6177. - check symbol
  6178. **)
  6179. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6180. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6181. BEGIN
  6182. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6183. IF SymbolNeedsResolution(variable) THEN
  6184. modifiers := variable.modifiers;
  6185. (*
  6186. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6187. variable.AddFlags(flags);
  6188. *)
  6189. variable.SetType(ResolveType(variable.type));
  6190. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6191. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6192. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6193. END;
  6194. END;
  6195. CheckSymbolVisibility(variable);
  6196. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6197. variable.SetUntraced(TRUE);
  6198. IF ~ContainsPointer(variable.type) THEN
  6199. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6200. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6201. END;
  6202. END;
  6203. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6204. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6205. IF ~PowerOf2(value) THEN
  6206. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6207. ELSE
  6208. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6209. END;
  6210. END;
  6211. variable.SetAlignment(FALSE,value);
  6212. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6213. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6214. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6215. END;
  6216. variable.SetAlignment(TRUE, value);
  6217. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6218. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6219. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6220. END;
  6221. variable.SetFictive(value);
  6222. variable.SetOffset(value*system.dataUnit);
  6223. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6224. END;
  6225. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6226. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6227. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6228. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6229. END;
  6230. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6231. IF variable.initializer # NIL THEN
  6232. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6233. END;
  6234. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6235. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6236. pointerType.SetPointerBase(variable.type);
  6237. pointerType.SetHidden(TRUE);
  6238. variable.SetType(ResolveType(pointerType));
  6239. END;
  6240. variable.SetState(SyntaxTree.Resolved);
  6241. END;
  6242. END VisitVariable;
  6243. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6244. BEGIN
  6245. VisitVariable(property)
  6246. END VisitProperty;
  6247. (** check and resolve a (procedure) parameter
  6248. - check and set type
  6249. - check symbol
  6250. - check parameter kind and set read-only flags if appropriate
  6251. **)
  6252. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6253. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6254. BEGIN
  6255. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6256. IF SymbolNeedsResolution(parameter) THEN
  6257. modifiers := parameter.modifiers;
  6258. parameter.SetType(ResolveType(parameter.type));
  6259. ASSERT(parameter.type.resolved # NIL);
  6260. CheckSymbolVisibility(parameter);
  6261. IF parameter.defaultValue # NIL THEN
  6262. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6263. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6264. ELSE
  6265. expression := ConstantExpression(parameter.defaultValue);
  6266. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6267. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6268. parameter.SetDefaultValue(expression);
  6269. END;
  6270. END;
  6271. END;
  6272. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6273. parameter.SetUntraced(TRUE);
  6274. IF ~ContainsPointer(parameter.type) THEN
  6275. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6276. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6277. END;
  6278. END;
  6279. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6280. parameter.SetMoveable(TRUE);
  6281. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6282. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6283. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6284. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6285. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6286. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6287. END;
  6288. END;
  6289. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6290. parameter.SetState(SyntaxTree.Resolved);
  6291. END;
  6292. END VisitParameter;
  6293. (** check and resolve a procedure (with declaration and implementation scope)
  6294. - check the procedure type
  6295. - check if method (i.e. in record scope), if so then
  6296. - check if (unique) constructor
  6297. - check if (unique) finalizer
  6298. - check if super method available, if so then check signature
  6299. - of not in record scope then negative check on constructor flag
  6300. - of not in record scope then negative check on finalizer flag
  6301. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6302. - check procedure symbol
  6303. **)
  6304. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6305. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6306. procedureType: SyntaxTree.ProcedureType;
  6307. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6308. qualifiedType: SyntaxTree.QualifiedType;
  6309. value: LONGINT;
  6310. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6311. position: Position;
  6312. BEGIN
  6313. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6314. IF IsOberonInline(procedure) THEN
  6315. IF SyntaxTree.Public * procedure.access # {} THEN
  6316. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6317. END;
  6318. procedure.SetInline(FALSE);
  6319. procedure.SetOberonInline(TRUE);
  6320. END;
  6321. IF SymbolNeedsResolution(procedure) THEN
  6322. recentIsRealtime := currentIsRealtime;
  6323. recentIsBodyProcedure := currentIsBodyProcedure;
  6324. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6325. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6326. modifiers := procedureType.modifiers;
  6327. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6328. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6329. IF useDarwinCCalls THEN (*fld*)
  6330. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6331. ELSE
  6332. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6333. END
  6334. END;
  6335. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6336. procedureType.SetInterrupt(TRUE);
  6337. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6338. END;
  6339. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6340. procedureType.SetNoReturn(TRUE);
  6341. END;
  6342. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6343. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6344. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6345. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6346. END;
  6347. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6348. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6349. END;
  6350. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6351. IF ~PowerOf2(value) THEN
  6352. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6353. ELSE
  6354. procedureType.SetStackAlignment(value)
  6355. END;
  6356. END;
  6357. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6358. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6359. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6360. END;
  6361. CheckModifiers(modifiers, TRUE);
  6362. modifiers := procedureType.returnTypeModifiers;
  6363. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6364. CheckModifiers(modifiers, TRUE);
  6365. FixProcedureType(procedureType);
  6366. currentIsRealtime := procedureType.isRealtime;
  6367. currentIsBodyProcedure := procedure.isBodyProcedure;
  6368. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6369. THEN
  6370. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6371. END;
  6372. CheckSymbolVisibility(procedure);
  6373. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6374. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6375. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6376. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6377. END;
  6378. END;
  6379. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6380. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6381. procedureType.SetDelegate(TRUE);
  6382. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6383. IF record.pointerType.typeDeclaration = NIL THEN
  6384. selfParameter.SetType(record.pointerType);
  6385. ELSE
  6386. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6387. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6388. qualifiedType.SetResolved(record.pointerType);
  6389. selfParameter.SetType(qualifiedType);
  6390. END;
  6391. selfParameter.SetAccess(SyntaxTree.Hidden);
  6392. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6393. IF procedure.isConstructor THEN
  6394. (*! constructor is always visible, compatibility to paco
  6395. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6396. *)
  6397. procedure.MarkUsed;
  6398. IF procedureType.returnType # NIL THEN
  6399. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6400. END;
  6401. proc := procedure.scope.firstProcedure;
  6402. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6403. proc := proc.nextProcedure;
  6404. END;
  6405. IF proc # NIL THEN
  6406. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6407. ELSE
  6408. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6409. END;
  6410. END;
  6411. IF procedure.isFinalizer THEN
  6412. procedure.MarkUsed;
  6413. IF procedureType.returnType # NIL THEN
  6414. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6415. END;
  6416. IF procedureType.numberParameters # 0 THEN
  6417. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6418. END;
  6419. proc := procedure.scope.firstProcedure;
  6420. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6421. proc := proc.nextProcedure;
  6422. END;
  6423. IF proc # NIL THEN
  6424. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6425. ELSE
  6426. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6427. END;
  6428. END;
  6429. super := FindSuperProcedure(record.recordScope, procedure);
  6430. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6431. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6432. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6433. END;
  6434. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6435. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6436. END;
  6437. IF super.isFinal THEN
  6438. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6439. END;
  6440. procedure.SetSuper(super);
  6441. super.SetOverwritten(TRUE);
  6442. procedure.SetAccess(procedure.access+super.access);
  6443. procedure.MarkUsed;
  6444. END;
  6445. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6446. THEN
  6447. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6448. END;
  6449. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6450. IF cellsAreObjects THEN
  6451. procedureType.SetDelegate(TRUE);
  6452. END;
  6453. IF procedure.isConstructor THEN
  6454. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6455. END;
  6456. ELSIF procedure.isConstructor THEN
  6457. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6458. END;
  6459. Declarations(procedure.procedureScope, FALSE);
  6460. (* body resolution part done as late fix of the procedure type *)
  6461. procedure.SetState(SyntaxTree.Resolved);
  6462. currentIsRealtime := recentIsRealtime;
  6463. currentIsBodyProcedure := recentIsBodyProcedure;
  6464. END;
  6465. END VisitProcedure;
  6466. (**
  6467. a builtin procedure is a global item that may not be modified locally
  6468. instead the resolving of builtin procedure calls are done in the esignator
  6469. **)
  6470. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6471. VAR type: SyntaxTree.Type;
  6472. BEGIN
  6473. type := ResolveType(builtinProcedure.type);
  6474. END VisitBuiltin;
  6475. (* nopov *)
  6476. (** check and resolve operator
  6477. - operators are first checked as procedures
  6478. - then additional operator-specific checks are done
  6479. - note that only module-scope operators are checked here
  6480. (operators in a record scope are only allowed in the context of
  6481. array-structured object types and checked in 'ResolveArrayStructure')
  6482. - also note that inter-operator conformity is not checked here
  6483. **)
  6484. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6485. VAR
  6486. procedureType: SyntaxTree.ProcedureType;
  6487. leftType, rightType: SyntaxTree.Type;
  6488. identifierNumber: LONGINT; position: Position;
  6489. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6490. modifiers: SyntaxTree.Modifier;
  6491. (** whether a type is locally defined in the current module scope
  6492. for arrays, the base type must be locally defined **)
  6493. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6494. BEGIN
  6495. IF type = NIL THEN
  6496. RETURN FALSE
  6497. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6498. RETURN TRUE
  6499. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6500. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6501. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6502. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6503. ELSE
  6504. RETURN FALSE
  6505. END
  6506. END IsLocallyDefined;
  6507. BEGIN
  6508. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6509. procedureType := operator.type(SyntaxTree.ProcedureType);
  6510. modifiers := procedureType.modifiers;
  6511. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6512. CheckModifiers(modifiers, TRUE);
  6513. VisitProcedure(operator);
  6514. IF operator.scope IS SyntaxTree.RecordScope THEN
  6515. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6516. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6517. IF identifierNumber = -1 THEN
  6518. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6519. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6520. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6521. ELSE
  6522. IF procedureType.numberParameters < 1 THEN
  6523. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6524. ELSIF procedureType.numberParameters > 2 THEN
  6525. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6526. ELSE
  6527. (* determine operand types *)
  6528. leftType := procedureType.firstParameter.type;
  6529. IF procedureType.numberParameters > 1 THEN
  6530. rightType := procedureType.firstParameter.nextParameter.type
  6531. ELSE
  6532. rightType := NIL
  6533. END;
  6534. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6535. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6536. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6537. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6538. END
  6539. END;
  6540. (* TODO: refine the checks, think about how restrictive the checks should be
  6541. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6542. They might be used for intersection, union, complement of custom object types *)
  6543. (* defaults *)
  6544. hasReturnType := TRUE;
  6545. mustBeUnary := FALSE;
  6546. mustBeBinary := FALSE;
  6547. mustReturnBoolean := FALSE;
  6548. mustReturnInteger := FALSE;
  6549. mustHaveEquitypedOperands := FALSE;
  6550. (* operator-specific exceptions *)
  6551. CASE identifierNumber OF
  6552. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6553. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6554. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6555. mustBeBinary := TRUE
  6556. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6557. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6558. | Scanner.Times: mustBeBinary := TRUE
  6559. | Scanner.TimesTimes: mustBeBinary := TRUE
  6560. | Scanner.DotTimes: mustBeBinary := TRUE
  6561. | Scanner.PlusTimes: mustBeBinary := TRUE
  6562. | Scanner.Slash: mustBeBinary := TRUE
  6563. | Scanner.Backslash: mustBeBinary := TRUE
  6564. | Scanner.DotSlash: mustBeBinary := TRUE
  6565. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6566. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6567. | Scanner.Not: mustBeUnary := TRUE
  6568. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6569. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6570. | Scanner.Transpose: mustBeUnary := TRUE;
  6571. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6572. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6573. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6574. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6575. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6576. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6577. | Global.Abs: mustBeUnary := TRUE;
  6578. | Global.Ash: (* TODO: arity? *)
  6579. | Global.Cap: (* TODO: arity? *)
  6580. | Global.Chr: mustBeUnary := TRUE;
  6581. | Global.Entier: (* TODO: arity? *)
  6582. | Global.EntierH: (* TODO: arity? *)
  6583. | Global.Len: (* unary and binary *)
  6584. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6585. | Global.Max, Global.Min: (* unary and binary *)
  6586. | Global.Odd: (* TODO: arity? *)
  6587. | Global.Sum: (* TODO: arity? *)
  6588. | Global.All: (* TODO: arity? *)
  6589. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6590. | Scanner.Alias:
  6591. | Scanner.GreaterGreater, Scanner.LessLess:
  6592. mustBeBinary := TRUE; hasReturnType := FALSE;
  6593. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6594. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6595. END;
  6596. (* check parameter count *)
  6597. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6598. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6599. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6600. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6601. END;
  6602. (* check parameter types *)
  6603. (* TODO: is this used at all? *)
  6604. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6605. leftType := procedureType.firstParameter.type;
  6606. rightType := procedureType.firstParameter.nextParameter.type;
  6607. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6608. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6609. END
  6610. END;
  6611. (* check return type *)
  6612. IF hasReturnType THEN
  6613. IF procedureType.returnType = NIL THEN
  6614. Error(operator.position, Diagnostics.Invalid, "return type required")
  6615. ELSIF mustReturnBoolean THEN
  6616. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6617. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6618. END
  6619. ELSIF mustReturnInteger THEN
  6620. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6621. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6622. END
  6623. END
  6624. ELSIF procedureType.returnType # NIL THEN
  6625. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6626. END
  6627. END
  6628. END
  6629. END
  6630. END VisitOperator;
  6631. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6632. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6633. BEGIN
  6634. IF error THEN RETURN FALSE END;
  6635. prevScope := currentScope;
  6636. prevDiagnostics := diagnostics;
  6637. diagnostics := NIL; (* suppress error output *)
  6638. currentScope := module.moduleScope;
  6639. VisitImport(x);
  6640. IF ~error THEN
  6641. module.moduleScope.AddImport(x);
  6642. x.SetScope(module.moduleScope);
  6643. END;
  6644. currentScope := prevScope;
  6645. diagnostics := prevDiagnostics;
  6646. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6647. END AddImport;
  6648. (** check and resolve import
  6649. - check for name = SYSTEM
  6650. - check for forbidden self import
  6651. - search through global import cache: already imported?
  6652. - check if already imported indirectly
  6653. - import if necessary -> set module and enter into import cache
  6654. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6655. - after this import this direct import and all indirect imports are stored in the current module's import list
  6656. **)
  6657. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6658. VAR
  6659. module: SyntaxTree.Module;
  6660. moduleScope: SyntaxTree.ModuleScope;
  6661. import,reimport: SyntaxTree.Import;
  6662. filename: FileName;
  6663. prevScope: SyntaxTree.Scope;
  6664. BEGIN
  6665. IF SymbolNeedsResolution(x) THEN
  6666. prevScope := currentScope;
  6667. x.SetType(SyntaxTree.importType);
  6668. moduleScope := currentScope.ownerModule.moduleScope;
  6669. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6670. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6671. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6672. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6673. ELSE
  6674. (* search through global import list: already imported ? *)
  6675. IF (x.module = NIL) & (importCache # NIL) THEN
  6676. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6677. ELSE import := NIL
  6678. END;
  6679. IF x.module # NIL THEN (* already imported indirectly *)
  6680. module := x.module;
  6681. ELSIF import # NIL THEN (* already in module list *)
  6682. module := import.module;
  6683. ASSERT(module # NIL);
  6684. x.SetModule(module);
  6685. ELSE (* must be imported *)
  6686. Global.ModuleFileName(x.moduleName,x.context,filename);
  6687. IF symbolFileFormat # NIL THEN
  6688. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6689. IF module = NIL THEN
  6690. ErrorSS(x.position,"could not import",filename);
  6691. IF VerboseErrorMessage THEN
  6692. Printout.Info("import",x)
  6693. END
  6694. ELSE
  6695. (*
  6696. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6697. (*! should rather be done by importer *)
  6698. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6699. checker.importCache := importCache;
  6700. checker.arrayBaseImported := arrayBaseImported;
  6701. checker.global := global;
  6702. checker.Module(module); (* semantic check *)
  6703. error := error OR checker.error;
  6704. END;
  6705. *)
  6706. (*
  6707. ASSERT(SyntaxTree.Resolved IN module.state);
  6708. *)
  6709. x.SetModule(module);
  6710. IF importCache # NIL THEN
  6711. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6712. import.SetContext(x.context);
  6713. import.SetModule(module);
  6714. importCache.AddImport(import);
  6715. END;
  6716. END;
  6717. ELSE
  6718. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6719. END;
  6720. END;
  6721. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6722. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6723. END;
  6724. import := module.moduleScope.firstImport;
  6725. WHILE(import # NIL) DO
  6726. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6727. ASSERT(currentScope # NIL);
  6728. ASSERT(currentScope.ownerModule # NIL);
  6729. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6730. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6731. Error(x.position,Diagnostics.Invalid,"recursive import");
  6732. ELSE
  6733. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6734. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6735. IF reimport = NIL THEN (* indirect import *)
  6736. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6737. reimport.SetContext(import.context);
  6738. reimport.SetModule(import.module);
  6739. moduleScope.AddImport(reimport);
  6740. reimport.SetScope(moduleScope);
  6741. ELSE
  6742. ASSERT(import.module # NIL);
  6743. reimport.SetModule(import.module); (* direct or indirect import *)
  6744. END;
  6745. END;
  6746. import := import.nextImport;
  6747. END;
  6748. END;
  6749. END;
  6750. currentScope := prevScope;
  6751. (* ELSE nothing to be done *)
  6752. x.SetState(SyntaxTree.Resolved);
  6753. END;
  6754. END VisitImport;
  6755. (*** statements ***)
  6756. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6757. VAR prev,resolved: SyntaxTree.Statement;
  6758. BEGIN
  6759. prev := resolvedStatement;
  6760. resolvedStatement := x;
  6761. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6762. activeCellsStatement := FALSE;
  6763. x.Accept(SELF);
  6764. (* removed this, implementation restriction should be resolved by backend
  6765. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6766. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6767. END;
  6768. *)
  6769. resolved := resolvedStatement;
  6770. resolvedStatement := prev;
  6771. RETURN resolved
  6772. END ResolveStatement;
  6773. (** check and resolve statement sequence
  6774. - check all statements, replace if necessary
  6775. **)
  6776. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6777. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6778. BEGIN
  6779. IF statementSequence # NIL THEN (* else empty *)
  6780. FOR i := 0 TO statementSequence.Length()-1 DO
  6781. statement := statementSequence.GetStatement(i);
  6782. resolved := ResolveStatement(statement);
  6783. IF (resolved # statement) THEN
  6784. statementSequence.SetStatement(i,resolved);
  6785. END;
  6786. END;
  6787. END;
  6788. END StatementSequence;
  6789. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6790. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6791. - check if procedure is callable
  6792. - check return type = NIL (otherwise must be assignment statement)
  6793. **)
  6794. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6795. VAR call: SyntaxTree.Designator;
  6796. BEGIN
  6797. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6798. call := procedureCall.call;
  6799. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6800. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6801. END;
  6802. call := ResolveDesignator(call);
  6803. IF call = SyntaxTree.invalidDesignator THEN
  6804. (* error already handled *)
  6805. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6806. (* inline call in a statement *)
  6807. ELSIF ~IsCallable(call) THEN
  6808. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6809. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6810. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6811. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6812. END;
  6813. procedureCall.SetCall(call);
  6814. (*
  6815. IF call = SyntaxTree.invalidDesignator THEN
  6816. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6817. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6818. IF IsOberonInline(procedure) THEN
  6819. Warning(procedure.position,"call to inline proc");
  6820. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6821. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6822. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6823. resolvedStatement := block;
  6824. RETURN;
  6825. END;
  6826. END;
  6827. *)
  6828. END VisitProcedureCallStatement;
  6829. (** check and resolve assignment LHS := RHS
  6830. - resolve LHS and RHS
  6831. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6832. - check if assignment is compatible
  6833. - check if LHS is variable (i.e. assignable)
  6834. - convert RHS if necessary
  6835. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6836. - assignment between different ASOTs
  6837. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6838. - assignment to ASOT elements:
  6839. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6840. **)
  6841. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6842. VAR
  6843. left: SyntaxTree.Designator;
  6844. right, expression: SyntaxTree.Expression;
  6845. designator: SyntaxTree.Designator;
  6846. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6847. mathArrayType: SyntaxTree.MathArrayType;
  6848. BEGIN
  6849. right := ResolveExpression(assignment.right);
  6850. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6851. left := ResolveDesignator(assignment.left);
  6852. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6853. (* error already handled *)
  6854. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6855. (* LHS is index write operator call on ASOT *)
  6856. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6857. (* necessary ?
  6858. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6859. type := procedureType.firstParameter.type;
  6860. expression := procedureCallDesignator.parameters.GetExpression(0);
  6861. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6862. *)
  6863. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6864. ELSIF CheckVariable(left) THEN
  6865. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6866. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6867. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6868. (* conversion done by procedure call
  6869. (* try to convert to left argument *)
  6870. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6871. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6872. procedureCallDesignator.parameters.SetExpression(1, right);
  6873. END;
  6874. *)
  6875. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6876. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6877. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6878. ELSIF AssignmentCompatible(left, right) THEN
  6879. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6880. mathArrayType := MathArrayStructureOfType(left.type);
  6881. right := NewConversion(right.position, right, mathArrayType, NIL);
  6882. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6883. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6884. ELSE
  6885. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6886. assignment.SetLeft(left);
  6887. assignment.SetRight(right);
  6888. resolvedStatement := assignment
  6889. END
  6890. END
  6891. END
  6892. END VisitAssignment;
  6893. (** check and resolve assignment LHS := RHS
  6894. - resolve LHS and RHS
  6895. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6896. - check if assignment is compatible
  6897. - check if LHS is variable (i.e. assignable)
  6898. - convert RHS if necessary
  6899. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6900. - assignment between different ASOTs
  6901. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6902. - assignment to ASOT elements:
  6903. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6904. **)
  6905. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6906. VAR
  6907. left: SyntaxTree.Designator;
  6908. right: SyntaxTree.Expression;
  6909. inPort, outPort: SyntaxTree.PortType;
  6910. expression: SyntaxTree.Expression;
  6911. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6912. BEGIN
  6913. right := ResolveExpression(communication.right);
  6914. left := ResolveDesignator(communication.left);
  6915. communication.SetLeft(left);
  6916. communication.SetRight(right);
  6917. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6918. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6919. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6920. (* conversion done by procedure call
  6921. (* try to convert to left argument *)
  6922. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6923. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6924. procedureCallDesignator.parameters.SetExpression(1, right);
  6925. END;
  6926. *)
  6927. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6928. ELSE
  6929. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6930. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6931. (* error already handled *)
  6932. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6933. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6934. IF outPort.direction # SyntaxTree.OutPort THEN
  6935. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6936. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6937. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6938. ELSE
  6939. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6940. communication.SetRight(right)
  6941. END;
  6942. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6943. IF CheckVariable(left) THEN
  6944. IF inPort.direction # SyntaxTree.InPort THEN
  6945. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6946. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6947. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6948. END;
  6949. END;
  6950. ELSE
  6951. Error(communication.position, -1, "unsupported stream operation");
  6952. END;
  6953. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6954. IF outPort.direction # SyntaxTree.OutPort THEN
  6955. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6956. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6957. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6958. ELSE
  6959. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6960. communication.SetRight(right)
  6961. END;
  6962. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6963. IF CheckVariable(right) THEN
  6964. IF inPort.direction # SyntaxTree.InPort THEN
  6965. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6966. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6967. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6968. END;
  6969. END;
  6970. ELSE
  6971. Error(communication.position, -1, "unsupported operation");
  6972. END;
  6973. END;
  6974. END VisitCommunicationStatement;
  6975. (** check and resolve if/eslif part
  6976. - check condition
  6977. - check statement sequence
  6978. **)
  6979. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6980. VAR prevUnreachable, b: BOOLEAN;
  6981. BEGIN
  6982. prevUnreachable := currentIsUnreachable;
  6983. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6984. IF IsBooleanValue(ifPart.condition,b) THEN
  6985. IF b=FALSE THEN
  6986. currentIsUnreachable := TRUE
  6987. ELSIF b=TRUE THEN
  6988. true := TRUE
  6989. END;
  6990. END;
  6991. StatementSequence(ifPart.statements);
  6992. currentIsUnreachable := prevUnreachable;
  6993. END IfPart;
  6994. (** check and resolve if statement
  6995. - check if parts and else part statement sequence
  6996. **)
  6997. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6998. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6999. BEGIN
  7000. prevUnreachable := currentIsUnreachable;
  7001. ifPartTrue := FALSE;
  7002. IfPart(ifStatement.ifPart,ifPartTrue);
  7003. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7004. elsif := ifStatement.GetElsifPart(i);
  7005. IfPart(elsif,ifPartTrue);
  7006. END;
  7007. IF ifStatement.elsePart # NIL THEN
  7008. IF ifPartTrue THEN
  7009. currentIsUnreachable := TRUE
  7010. END;
  7011. StatementSequence(ifStatement.elsePart)
  7012. END;
  7013. currentIsUnreachable := prevUnreachable;
  7014. END VisitIfStatement;
  7015. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7016. VAR variable: SyntaxTree.Designator;
  7017. type,variableType: SyntaxTree.Type;
  7018. withEntry: WithEntry;
  7019. BEGIN
  7020. variable := ResolveDesignator(withPart.variable);
  7021. variableType := variable.type.resolved;
  7022. withPart.SetVariable(variable);
  7023. type := ResolveType(withPart.type);
  7024. withPart.SetType(type);
  7025. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7026. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7027. END;
  7028. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7029. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7030. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7031. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7032. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7033. IF VerboseErrorMessage THEN
  7034. Printout.Info("variable",variable)
  7035. END;
  7036. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7037. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7038. IF VerboseErrorMessage THEN
  7039. Printout.Info("variable",variable);
  7040. Printout.Info("type",type);
  7041. END;
  7042. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7043. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7044. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7045. IF VerboseErrorMessage THEN
  7046. Printout.Info("variable",variable);
  7047. Printout.Info("type",type);
  7048. END;
  7049. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7050. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7051. ELSE
  7052. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7053. NEW(withEntry);
  7054. withEntry.previous := withEntries;
  7055. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7056. withEntry.type := type;
  7057. withEntries := withEntry;
  7058. StatementSequence(withPart.statements);
  7059. withEntries := withEntries.previous;
  7060. END;
  7061. END WithPart;
  7062. (** check and resolve with statement WITH variable: type DO ... END;
  7063. - check type and variable
  7064. - check that variable type is type extension of type
  7065. - check that variable is a variable
  7066. - enter new with scope and enter guardedVariable with same name and reference to variable
  7067. - create if statement:
  7068. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7069. **)
  7070. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7071. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7072. BEGIN
  7073. prevScope := currentScope; symbol := NIL;
  7074. FOR i := 0 TO withStatement.WithParts()-1 DO
  7075. WithPart(withStatement.GetWithPart(i),symbol);
  7076. END;
  7077. IF withStatement.elsePart # NIL THEN
  7078. StatementSequence(withStatement.elsePart)
  7079. END;
  7080. currentScope := prevScope;
  7081. END VisitWithStatement;
  7082. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7083. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7084. - check 'first' < 'last' and no overlaps between different case labels
  7085. - check statement sequence
  7086. **)
  7087. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7088. VAR
  7089. i: LONGINT;
  7090. position: Position;
  7091. expression, left, right: SyntaxTree.Expression;
  7092. expressionType: SyntaxTree.Type;
  7093. l, r: LONGINT;
  7094. cl, cr: CHAR;
  7095. thiscases: SyntaxTree.CaseConstant;
  7096. BEGIN
  7097. thiscases := NIL;
  7098. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7099. expression := casePart.elements.GetExpression(i);
  7100. position := expression.position;
  7101. (* set context of range *)
  7102. IF expression IS SyntaxTree.RangeExpression THEN
  7103. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7104. END;
  7105. expression := ResolveExpression(expression);
  7106. IF expression = SyntaxTree.invalidExpression THEN
  7107. (* error already reported *)
  7108. expressionType := SyntaxTree.invalidType;
  7109. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7110. (* read out 'first' and 'last' *)
  7111. left := expression(SyntaxTree.RangeExpression).first;
  7112. right := expression(SyntaxTree.RangeExpression).last;
  7113. (* guaranteed by VisitRangeExpression: *)
  7114. ASSERT((left # NIL) & (right # NIL));
  7115. ASSERT(left.type.resolved = right.type.resolved);
  7116. left := CompatibleConversion(left.position, left, type);
  7117. right := CompatibleConversion(right.position, right, type);
  7118. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7119. expression(SyntaxTree.RangeExpression).SetLast(right);
  7120. expressionType := RegularType(position,left.type);
  7121. ELSE
  7122. expression := ConstantExpression(expression);
  7123. expression := CompatibleConversion(expression.position, expression, type);
  7124. (*
  7125. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7126. left := Global.NewCharacterValue(system,expression.position,cl);
  7127. expression := casePart.elements.GetExpression(i);
  7128. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7129. expression := left
  7130. END;
  7131. *)
  7132. casePart.elements.SetExpression(i,expression);
  7133. left := expression; right := expression;
  7134. expressionType := RegularType(position,expression.type)
  7135. END;
  7136. IF (expressionType = SyntaxTree.invalidType) THEN
  7137. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7138. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7139. expression := SyntaxTree.invalidExpression;
  7140. ELSE
  7141. l := 0; r := 0;
  7142. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7143. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7144. l := ORD(cl); r := ORD(cr);
  7145. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7146. ELSE
  7147. expression := SyntaxTree.invalidExpression
  7148. END;
  7149. IF expression # SyntaxTree.invalidExpression THEN
  7150. IF l>r THEN
  7151. Error(position,Diagnostics.Invalid,"empty case label")
  7152. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7153. Error(position,Diagnostics.Invalid,"duplicate case label");
  7154. ELSE
  7155. IF l < min THEN min := l END;
  7156. IF r > max THEN max := r END;
  7157. END;
  7158. END;
  7159. END;
  7160. casePart.elements.SetExpression(i,expression);
  7161. END;
  7162. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7163. casePart.SetConstants(thiscases);
  7164. StatementSequence(casePart.statements);
  7165. END CasePart;
  7166. (** check and resolve case statement CASE variable OF ... END;
  7167. - check variable
  7168. - check case parts
  7169. **)
  7170. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7171. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7172. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7173. BEGIN
  7174. expression := ResolveExpression(caseStatement.variable);
  7175. type := RegularType(expression.position,expression.type);
  7176. IF type = SyntaxTree.invalidType THEN
  7177. expression := SyntaxTree.invalidExpression;
  7178. ELSIF IsIntegerType(type) THEN
  7179. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7180. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7181. (*
  7182. expression := Global.NewCharacterValue(system,expression.position,ch);
  7183. *)
  7184. type := expression.type;
  7185. ELSIF IsCharacterType(type) THEN
  7186. ELSIF IsEnumerationType(type) THEN
  7187. ELSE
  7188. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7189. expression := SyntaxTree.invalidExpression;
  7190. END;
  7191. caseStatement.SetVariable(expression);
  7192. caseList := NIL;
  7193. min := MAX(LONGINT); max := MIN(LONGINT);
  7194. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7195. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7196. END;
  7197. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7198. msg := "huge sparse case table ";
  7199. Strings.AppendInt(msg, max-min);
  7200. Strings.Append(msg,"/");
  7201. Strings.AppendInt(msg, caseStatement.CaseParts());
  7202. Warning(caseStatement.position,msg);
  7203. END;
  7204. caseStatement.SetMinMax(min,max);
  7205. StatementSequence(caseStatement.elsePart);
  7206. IF expression.resolved # NIL THEN
  7207. IF IsCharacterValue(expression,ch) THEN
  7208. l := ORD(ch)
  7209. ELSIF IsIntegerValue(expression,l) THEN
  7210. END;
  7211. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7212. END;
  7213. END VisitCaseStatement;
  7214. (** check and resolve while statement
  7215. - check condition
  7216. - check statement sequence
  7217. **)
  7218. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7219. VAR prevIsUnreachable,b: BOOLEAN;
  7220. BEGIN
  7221. prevIsUnreachable := currentIsUnreachable;
  7222. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7223. IF IsBooleanValue(whileStatement.condition,b) THEN
  7224. IF b=FALSE THEN
  7225. currentIsUnreachable := TRUE
  7226. END;
  7227. END;
  7228. StatementSequence(whileStatement.statements);
  7229. currentIsUnreachable := prevIsUnreachable
  7230. END VisitWhileStatement;
  7231. (** check and resolve repeat statement
  7232. - check condition
  7233. - check statement sequence
  7234. **)
  7235. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7236. BEGIN
  7237. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7238. StatementSequence(repeatStatement.statements);
  7239. END VisitRepeatStatement;
  7240. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7241. VAR withEntry: WithEntry;
  7242. BEGIN
  7243. withEntry := withEntries;
  7244. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7245. withEntry := withEntry.previous
  7246. END;
  7247. IF withEntry = NIL THEN RETURN FALSE
  7248. ELSE
  7249. type := withEntry.type;
  7250. RETURN TRUE
  7251. END;
  7252. END GetGuard;
  7253. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7254. - check that variable is an integer variable
  7255. - check that from is integer typed with compatible type
  7256. - check that to has compatible type
  7257. - check that by is constant integer with compatible type
  7258. **)
  7259. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7260. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7261. BEGIN
  7262. designator := ResolveDesignator(forStatement.variable);
  7263. type := SyntaxTree.invalidType;
  7264. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7265. designator := SyntaxTree.invalidDesignator;
  7266. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7267. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7268. designator := SyntaxTree.invalidDesignator;
  7269. ELSIF CheckVariable(designator) THEN
  7270. type := designator.type;
  7271. END;
  7272. forStatement.SetVariable(designator);
  7273. expression := ResolveExpression(forStatement.from);
  7274. IF expression = SyntaxTree.invalidExpression THEN
  7275. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7276. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7277. expression := SyntaxTree.invalidExpression;
  7278. ELSIF type # SyntaxTree.invalidType THEN
  7279. expression := NewConversion(expression.position,expression,type,NIL)
  7280. END;
  7281. forStatement.SetFrom(expression);
  7282. expression := ResolveExpression(forStatement.to);
  7283. IF expression = SyntaxTree.invalidExpression THEN
  7284. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7285. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7286. expression := SyntaxTree.invalidExpression;
  7287. ELSIF type # SyntaxTree.invalidType THEN
  7288. expression := NewConversion(expression.position,expression,type,NIL)
  7289. END;
  7290. forStatement.SetTo(expression);
  7291. IF forStatement.by # NIL THEN
  7292. expression := ConstantInteger(forStatement.by);
  7293. ELSE
  7294. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7295. END;
  7296. IF expression = SyntaxTree.invalidExpression THEN
  7297. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7298. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7299. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7300. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7301. ELSIF type # SyntaxTree.invalidType THEN
  7302. expression := NewConversion(expression.position,expression,type,NIL)
  7303. END;
  7304. forStatement.SetBy(expression);
  7305. StatementSequence(forStatement.statements);
  7306. END VisitForStatement;
  7307. (** check and resolve loop statement LOOP StatementSequence END
  7308. - check statement sequence
  7309. **)
  7310. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7311. BEGIN
  7312. StatementSequence(loopStatement.statements)
  7313. END VisitLoopStatement;
  7314. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7315. BEGIN
  7316. StatementSequence(exitableBlock.statements);
  7317. END VisitExitableBlock;
  7318. (** check and resolve exit statement EXIT
  7319. - check that exit is within LOOP statement block
  7320. **)
  7321. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7322. VAR outer: SyntaxTree.Statement;
  7323. BEGIN
  7324. outer := exitStatement.outer;
  7325. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7326. outer := outer.outer;
  7327. END;
  7328. IF outer = NIL THEN
  7329. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7330. END;
  7331. END VisitExitStatement;
  7332. (** check and resolve return statement RETURN [expression]
  7333. - check expression (if any)
  7334. - check if in procedure scope
  7335. - if in procedure scope then check expression compatibility
  7336. - if not in procecdure scope then check on return without expression
  7337. **)
  7338. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7339. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7340. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7341. BEGIN
  7342. position := returnStatement.position;
  7343. expression := returnStatement.returnValue;
  7344. IF expression # NIL THEN
  7345. expression := ResolveExpression(expression);
  7346. returnStatement.SetReturnValue(expression);
  7347. END;
  7348. outer := returnStatement.outer;
  7349. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7350. outer := outer.outer
  7351. END;
  7352. IF (outer # NIL) THEN
  7353. scope := outer(SyntaxTree.Body).inScope;
  7354. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7355. IF (expression # NIL) THEN
  7356. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7357. END;
  7358. ELSE
  7359. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7360. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7361. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7362. END;
  7363. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7364. IF returnType # NIL THEN
  7365. returnType := returnType.resolved;
  7366. IF expression = NIL THEN
  7367. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7368. ELSIF expression.type = NIL THEN
  7369. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7370. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7371. Error(position,Diagnostics.Invalid,"return type not compatible");
  7372. IF VerboseErrorMessage THEN
  7373. Printout.Info("returnType",returnType);
  7374. Printout.Info("expression",expression);
  7375. END;
  7376. ELSE
  7377. expression := NewConversion(expression.position,expression,returnType,NIL);
  7378. returnStatement.SetReturnValue(expression);
  7379. END;
  7380. ELSIF expression # NIL THEN
  7381. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7382. END;
  7383. END;
  7384. END;
  7385. END VisitReturnStatement;
  7386. (** check and resolve await statement AWAIT(condition: Expression)
  7387. - check await condition
  7388. **)
  7389. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7390. VAR condition: SyntaxTree.Expression;
  7391. BEGIN
  7392. condition := ResolveCondition(awaitStatement.condition);
  7393. IF currentIsRealtime THEN
  7394. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7395. END;
  7396. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7397. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7398. END;
  7399. awaitStatement.SetCondition(condition);
  7400. END VisitAwaitStatement;
  7401. PROCEDURE CheckSystemImport(position: Position);
  7402. VAR import: SyntaxTree.Import;
  7403. BEGIN
  7404. import := currentScope.ownerModule.moduleScope.firstImport;
  7405. WHILE(import # NIL) DO
  7406. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7407. RETURN;
  7408. END;
  7409. import := import.nextImport;
  7410. END;
  7411. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7412. END CheckSystemImport;
  7413. (** check and resolve code statement: do nothing, must be done by assembler
  7414. **)
  7415. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7416. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7417. BEGIN
  7418. CheckSystemImport(code.position);
  7419. FOR i := 0 TO code.inRules.Length()-1 DO
  7420. statement := code.inRules.GetStatement(i);
  7421. IF statement IS SyntaxTree.Assignment THEN
  7422. WITH statement: SyntaxTree.Assignment DO
  7423. statement.SetRight(ResolveExpression(statement.right));
  7424. END;
  7425. ELSE
  7426. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7427. END;
  7428. END;
  7429. FOR i := 0 TO code.outRules.Length()-1 DO
  7430. statement := code.outRules.GetStatement(i);
  7431. IF statement IS SyntaxTree.Assignment THEN
  7432. WITH statement: SyntaxTree.Assignment DO
  7433. statement.SetLeft(ResolveDesignator(statement.left));
  7434. END;
  7435. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7436. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7437. ELSE
  7438. Printout.Info("out statement ", statement);
  7439. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7440. END;
  7441. END;
  7442. END VisitCode;
  7443. (** check and set flags of a statement block
  7444. - check for multiply occurence of a flag
  7445. - check and set priority only in bodies
  7446. - check for valid names
  7447. **)
  7448. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7449. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7450. flag: LONGINT; recordBody: SyntaxTree.Body;
  7451. PROCEDURE SetProtectedRecord;
  7452. VAR scope: SyntaxTree.Scope;
  7453. BEGIN
  7454. scope := currentScope;
  7455. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7456. scope := scope.outerScope
  7457. END;
  7458. IF scope # NIL THEN
  7459. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7460. END;
  7461. END SetProtectedRecord;
  7462. BEGIN
  7463. flags := {};
  7464. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7465. recordBody := block(SyntaxTree.Body)
  7466. ELSE
  7467. recordBody := NIL
  7468. END;
  7469. blockModifier := block.blockModifiers;
  7470. WHILE(blockModifier # NIL) DO
  7471. name := blockModifier.identifier;
  7472. expression := blockModifier.expression;
  7473. position := blockModifier.position;
  7474. flag := -1;
  7475. IF name=Global.NamePriority THEN
  7476. IF expression = NIL THEN
  7477. Error(position,Diagnostics.Invalid,"missing priority expression");
  7478. ELSIF recordBody = NIL THEN
  7479. Error(position,Diagnostics.Invalid,"priority not on record body");
  7480. ELSIF recordBody.priority # NIL THEN
  7481. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7482. ELSE
  7483. recordBody.SetPriority(expression);
  7484. END;
  7485. ELSIF expression # NIL THEN
  7486. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7487. ELSIF name=Global.NameExclusive THEN
  7488. IF block.isExclusive THEN
  7489. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7490. END;
  7491. block.SetExclusive(TRUE); SetProtectedRecord;
  7492. ELSIF name=Global.NameActive THEN
  7493. IF recordBody = NIL THEN
  7494. Error(position,Diagnostics.Invalid,"active not in record body");
  7495. ELSIF recordBody.isActive THEN
  7496. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7497. ELSE
  7498. recordBody.SetActive(TRUE); SetProtectedRecord;
  7499. END;
  7500. ELSIF name=Global.NameSafe THEN
  7501. IF recordBody = NIL THEN
  7502. Error(position,Diagnostics.Invalid,"safe not in record body");
  7503. ELSIF recordBody.isSafe THEN
  7504. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7505. ELSE
  7506. recordBody.SetSafe(TRUE);
  7507. SetProtectedRecord;
  7508. END;
  7509. ELSIF name=Global.NameRealtime THEN
  7510. IF recordBody = NIL THEN
  7511. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7512. ELSIF recordBody.isRealtime THEN
  7513. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7514. ELSE
  7515. recordBody.SetRealtime(TRUE);
  7516. block.SetRealtime(TRUE);
  7517. END;
  7518. ELSIF name=Global.NameUnchecked THEN
  7519. IF block.isUnchecked THEN
  7520. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7521. ELSE
  7522. block.SetUnchecked(TRUE);
  7523. END;
  7524. ELSIF (name=Global.NameUncooperative) THEN
  7525. IF block.isUncooperative THEN
  7526. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7527. ELSE
  7528. block.SetUncooperative(TRUE);
  7529. END;
  7530. ELSE
  7531. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7532. END;
  7533. blockModifier := blockModifier.nextModifier;
  7534. END;
  7535. END BlockFlags;
  7536. (** check and resolve statement block
  7537. - check flags (exclusive)
  7538. - check statement sequence
  7539. **)
  7540. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7541. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7542. BEGIN
  7543. BlockFlags(statementBlock);
  7544. IF statementBlock.isExclusive THEN
  7545. (* check that not in exclusive block *)
  7546. IF currentIsExclusive THEN
  7547. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7548. ELSIF currentIsRealtime THEN
  7549. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7550. END;
  7551. END;
  7552. recentExclusive := currentIsExclusive;
  7553. recentUnreachable := currentIsUnreachable;
  7554. recentRealtime := currentIsRealtime;
  7555. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7556. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7557. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7558. StatementSequence(statementBlock.statements);
  7559. currentIsRealtime := recentRealtime;
  7560. currentIsExclusive := recentExclusive;
  7561. currentIsUnreachable := recentUnreachable;
  7562. END VisitStatementBlock;
  7563. (** check and resolve body
  7564. - check flags (active, priority, safe)
  7565. - check body and finally part
  7566. **)
  7567. PROCEDURE Body(body: SyntaxTree.Body);
  7568. BEGIN
  7569. VisitStatementBlock(body);
  7570. IF body.isActive THEN
  7571. IF ~currentIsBodyProcedure THEN
  7572. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7573. ELSIF body.priority # NIL THEN
  7574. body.SetPriority(ConstantInteger(body.priority));
  7575. END;
  7576. ELSIF body.isSafe THEN
  7577. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7578. ELSIF body.priority # NIL THEN
  7579. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7580. END;
  7581. IF body.code # NIL THEN
  7582. CheckSystemImport(body.position);
  7583. END;
  7584. StatementSequence(body.finally)
  7585. END Body;
  7586. (*** scopes ***)
  7587. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7588. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7589. VAR duplicateSymbol: BOOLEAN;
  7590. BEGIN
  7591. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7592. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7593. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7594. END;
  7595. scope.EnterSymbol(symbol,duplicateSymbol);
  7596. IF ~allowDuplicate & duplicateSymbol THEN
  7597. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7598. IF VerboseErrorMessage THEN
  7599. Printout.Info("multiply defined identifier",symbol);
  7600. Printout.Info("in scope",scope);
  7601. END;
  7602. END;
  7603. END Register;
  7604. (**
  7605. implementation: check and resolve an implementation part
  7606. **)
  7607. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7608. move implementation checker to a separate object ? *)
  7609. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7610. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7611. BEGIN
  7612. prevIsRealtime := currentIsRealtime;
  7613. prevIsBodyProcedure := currentIsBodyProcedure;
  7614. prevIsCellNet := currentIsCellNet;
  7615. prevScope := currentScope;
  7616. currentScope := scope;
  7617. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7618. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7619. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7620. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7621. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7622. (*
  7623. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7624. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7625. END;
  7626. *)
  7627. END;
  7628. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7629. (* module body, record bodies are wrapped into an artifical procedure *)
  7630. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7631. Body(scope(SyntaxTree.ProcedureScope).body)
  7632. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7633. Body(scope(SyntaxTree.ProcedureScope).body)
  7634. END;
  7635. END;
  7636. currentScope := prevScope;
  7637. currentIsRealtime := prevIsRealtime;
  7638. currentIsBodyProcedure := prevIsBodyProcedure;
  7639. currentIsCellNet := prevIsCellNet;
  7640. END Implementation;
  7641. (** implementation phase:
  7642. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7643. **)
  7644. PROCEDURE Implementations(x: SyntaxTree.Module);
  7645. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7646. BEGIN
  7647. prevPhase := phase;
  7648. phase := InlinePhase;
  7649. scope := x.firstScope;
  7650. WHILE(scope # NIL) DO
  7651. Implementation(scope);
  7652. scope := scope.nextScope;
  7653. END;
  7654. phase := ImplementationPhase;
  7655. scope := x.firstScope;
  7656. WHILE(scope # NIL) DO
  7657. Implementation(scope);
  7658. scope := scope.nextScope;
  7659. END;
  7660. phase := prevPhase;
  7661. END Implementations;
  7662. (** declaration phase:
  7663. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7664. - import lists (for module scopes)
  7665. - parameter list (for procedure scopes)
  7666. - constant declarations
  7667. - type declarations
  7668. - variable declarations
  7669. - procedure declarations
  7670. preformed in two stages:
  7671. - first all symbols are entered into the symbol table (with uniqueness check),
  7672. - then all symbols are resolved
  7673. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7674. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7675. **)
  7676. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7677. VAR
  7678. constant: SyntaxTree.Constant;
  7679. typeDeclaration: SyntaxTree.TypeDeclaration;
  7680. variable: SyntaxTree.Variable;
  7681. procedure: SyntaxTree.Procedure;
  7682. prevScope: SyntaxTree.Scope;
  7683. parameter: SyntaxTree.Parameter;
  7684. import: SyntaxTree.Import;
  7685. symbol: SyntaxTree.Symbol;
  7686. prevPhase: LONGINT;
  7687. prevError : BOOLEAN;
  7688. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7689. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7690. BEGIN
  7691. IF type.baseType # NIL THEN
  7692. baseType := type.baseType.resolved;
  7693. IF baseType IS SyntaxTree.PointerType THEN
  7694. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7695. END;
  7696. (*
  7697. IF baseType IS SyntaxTree.CellType THEN
  7698. DeclareCell(baseType(SyntaxTree.CellType));
  7699. END;
  7700. *)
  7701. END;
  7702. parameter := type.firstParameter;
  7703. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7704. (*
  7705. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7706. variable.SetType(parameter.type);
  7707. variable.SetAccess(SyntaxTree.Hidden);
  7708. variable.SetModifiers(parameter.modifiers);
  7709. currentScope.PushVariable(variable);
  7710. *)
  7711. Register(parameter,scope, FALSE);
  7712. parameter := parameter.nextParameter;
  7713. END;
  7714. property := type.firstProperty;
  7715. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7716. (*
  7717. variable := currentScope.FindVariable(property.name);
  7718. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7719. prop := variable(SyntaxTree.Property);
  7720. ELSE (* add, duplicate symbols detection later *)
  7721. prop := SyntaxTree.NewProperty(property.position, property.name);
  7722. currentScope.PushVariable(prop);
  7723. END;
  7724. prop.SetType(property.type);
  7725. prop.SetValue(property.value);
  7726. prop.SetAccess(SyntaxTree.Hidden);
  7727. *)
  7728. Register(property, scope, FALSE);
  7729. property := property.nextProperty;
  7730. END;
  7731. END DeclareCell;
  7732. BEGIN
  7733. prevError := error;
  7734. prevPhase := phase;
  7735. phase := DeclarationPhase;
  7736. prevScope := currentScope;
  7737. currentScope := scope;
  7738. error := FALSE;
  7739. (* first enter all symbols in scope *)
  7740. IF scope IS SyntaxTree.ModuleScope THEN
  7741. (* treat imports first for a module scope, , set default context if necessary *)
  7742. import := scope(SyntaxTree.ModuleScope).firstImport;
  7743. WHILE(import # NIL) DO
  7744. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7745. Register(import, currentScope, FALSE);
  7746. import := import.nextImport;
  7747. END;
  7748. import := scope(SyntaxTree.ModuleScope).firstImport;
  7749. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7750. ResolveSymbol(import);
  7751. import := import.nextImport;
  7752. END;
  7753. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7754. (* enter parameters for a procedure scope *)
  7755. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7756. WHILE(parameter # NIL) DO
  7757. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7758. END;
  7759. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7760. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7761. ELSIF scope IS SyntaxTree.CellScope THEN
  7762. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7763. IF~skipImplementation THEN
  7764. import := scope(SyntaxTree.CellScope).firstImport;
  7765. WHILE(import # NIL) DO
  7766. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7767. Register(import, currentScope, FALSE);
  7768. import := import.nextImport;
  7769. END;
  7770. import := scope(SyntaxTree.CellScope).firstImport;
  7771. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7772. ResolveSymbol(import);
  7773. import := import.nextImport;
  7774. END;
  7775. END;
  7776. END;
  7777. IF error THEN RETURN END;
  7778. IF skipImplementation THEN
  7779. scope.Clear;
  7780. END;
  7781. (* constants *)
  7782. constant := scope.firstConstant;
  7783. WHILE (constant # NIL) DO
  7784. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7785. END;
  7786. (* type declarations *)
  7787. typeDeclaration := scope.firstTypeDeclaration;
  7788. WHILE (typeDeclaration # NIL) DO
  7789. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7790. END;
  7791. (* variables *)
  7792. variable := scope.firstVariable;
  7793. WHILE (variable # NIL) DO
  7794. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7795. END;
  7796. (* procedures *)
  7797. procedure := scope.firstProcedure;
  7798. WHILE (procedure # NIL) DO
  7799. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7800. END;
  7801. (* now process all symbols without any presumption on the order *)
  7802. symbol := scope.firstSymbol;
  7803. WHILE(symbol # NIL) DO
  7804. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7805. ResolveSymbol(symbol);
  7806. END;
  7807. symbol := symbol.nextSymbol;
  7808. END;
  7809. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7810. symbol := scope.firstSymbol;
  7811. WHILE symbol # NIL DO
  7812. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7813. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7814. pointerFixes.Add(symbol, currentScope);
  7815. END;
  7816. IF ~symbol.type.resolved.isRealtime THEN
  7817. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7818. END;
  7819. END;
  7820. symbol := symbol.nextSymbol
  7821. END;
  7822. END;
  7823. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7824. Error(Basic.invalidPosition,Diagnostics.Invalid,"problems during offset computation in module");
  7825. END;
  7826. IF (scope.ownerModule # NIL) THEN
  7827. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7828. scope.ownerModule.AddScope(scope);
  7829. END;
  7830. phase := prevPhase;
  7831. currentScope := prevScope;
  7832. error := error OR prevError;
  7833. END Declarations;
  7834. (* nopov *)
  7835. (** check if all operators from one module are compatible to the ones in the other module
  7836. - check if there are not multiple operators with the same signature
  7837. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7838. - check for all operators whose signatures are compatible, whether the return types are compatible
  7839. note that:
  7840. - the return type is not considered to be part of the signature
  7841. - two signatures are considered compatible, if all of the operands are compatible
  7842. **)
  7843. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7844. VAR
  7845. thisOperator, thatOperator: SyntaxTree.Operator;
  7846. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7847. thisParameter, thatParameter: SyntaxTree.Parameter;
  7848. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7849. i: LONGINT;
  7850. BEGIN
  7851. currentScope := thisModuleScope;
  7852. hasError := FALSE;
  7853. (* go through all operators in the other module *)
  7854. thatOperator := thatModuleScope.firstOperator;
  7855. WHILE (thatOperator # NIL) & ~hasError DO
  7856. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7857. (* the other operator is accessible *)
  7858. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7859. (* the other operator is not the conversion operator *)
  7860. (* go through all operators in this module *)
  7861. thisOperator := thisModuleScope.firstOperator;
  7862. WHILE (thisOperator # NIL) & ~hasError DO
  7863. IF thisOperator # thatOperator THEN
  7864. (* the operators are not the same *)
  7865. IF thisOperator.name = thatOperator.name THEN
  7866. (* the operators share the same identifier *)
  7867. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7868. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7869. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7870. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7871. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7872. (* both operators have the same paramter count *)
  7873. thisParameter := thisProcedureType.firstParameter;
  7874. thatParameter := thatProcedureType.firstParameter;
  7875. operandsAreEqual := TRUE;
  7876. operandsAreCompatible := TRUE;
  7877. (* go through all parameters *)
  7878. FOR i := 1 TO thisProcedureType.numberParameters DO
  7879. ASSERT(thatParameter # NIL);
  7880. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7881. operandsAreEqual := FALSE;
  7882. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7883. operandsAreCompatible := FALSE
  7884. END
  7885. END;
  7886. thisParameter := thisParameter.nextParameter;
  7887. thatParameter := thatParameter.nextParameter
  7888. END;
  7889. IF operandsAreEqual THEN
  7890. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7891. hasError := TRUE
  7892. ELSIF operandsAreCompatible THEN
  7893. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7894. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7895. hasError := TRUE
  7896. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7897. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7898. hasError := TRUE
  7899. END
  7900. END
  7901. END
  7902. END
  7903. END;
  7904. thisOperator := thisOperator.nextOperator
  7905. END
  7906. END
  7907. END;
  7908. thatOperator := thatOperator.nextOperator
  7909. END
  7910. END CheckInterOperatorConformity;
  7911. (** check module:
  7912. - check module declaration
  7913. - add context, if necessary
  7914. - remove module from import cache, if necessary
  7915. - check declarations
  7916. - resolve all type fixes
  7917. - check implementation (bodies)
  7918. **)
  7919. PROCEDURE Module*(x: SyntaxTree.Module);
  7920. VAR (* nopov *)
  7921. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7922. BEGIN
  7923. prevScope := currentScope;
  7924. prevIsCellNet := currentIsCellNet;
  7925. module := x;
  7926. ASSERT(x # NIL);
  7927. global := system.globalScope[x.case];
  7928. x.moduleScope.SetGlobalScope(global);
  7929. currentScope := global;
  7930. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7931. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7932. RemoveModuleFromCache(importCache,x);
  7933. Declarations(x.moduleScope, FALSE);
  7934. FixTypes();
  7935. IF module.isCellNet THEN
  7936. currentIsCellNet := TRUE;
  7937. modifier := x.modifiers;
  7938. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7939. CheckModifiers(modifier, FALSE);
  7940. END;
  7941. (* nopov *)
  7942. IF ~error THEN
  7943. (* check if operators conform to each other within this module *)
  7944. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7945. (* go through all imports *)
  7946. import := x.moduleScope.firstImport;
  7947. WHILE import # NIL DO
  7948. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7949. (* check if all operators in this module conform to the ones of the imported module *)
  7950. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7951. END;
  7952. import := import.nextImport
  7953. END;
  7954. END;
  7955. Implementations(x);
  7956. module := NIL;
  7957. currentIsCellNet := prevIsCellNet;
  7958. currentScope := prevScope;
  7959. END Module;
  7960. END Checker;
  7961. Warnings*=OBJECT (SyntaxTree.Visitor)
  7962. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7963. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7964. BEGIN
  7965. SELF.diagnostics := diagnostics
  7966. END InitWarnings;
  7967. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  7968. BEGIN END VisitPortType;
  7969. (** types *)
  7970. PROCEDURE Type(x: SyntaxTree.Type);
  7971. BEGIN x.Accept(SELF)
  7972. END Type;
  7973. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7974. BEGIN END VisitType;
  7975. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7976. BEGIN END VisitBasicType;
  7977. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7978. BEGIN END VisitCharacterType;
  7979. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7980. BEGIN END VisitIntegerType;
  7981. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7982. BEGIN END VisitFloatType;
  7983. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7984. BEGIN END VisitQualifiedType;
  7985. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7986. BEGIN END VisitStringType;
  7987. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7988. BEGIN END VisitEnumerationType;
  7989. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7990. BEGIN END VisitRangeType;
  7991. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7992. BEGIN
  7993. IF ~(SyntaxTree.Warned IN x.state) THEN
  7994. x.SetState(SyntaxTree.Warned);
  7995. Type(x.arrayBase);
  7996. END;
  7997. END VisitArrayType;
  7998. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7999. BEGIN
  8000. IF ~(SyntaxTree.Warned IN x.state) THEN
  8001. x.SetState(SyntaxTree.Warned);
  8002. Type(x.arrayBase);
  8003. END;
  8004. END VisitMathArrayType;
  8005. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8006. BEGIN
  8007. IF ~(SyntaxTree.Warned IN x.state) THEN
  8008. x.SetState(SyntaxTree.Warned);
  8009. Type(x.pointerBase);
  8010. END;
  8011. END VisitPointerType;
  8012. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8013. BEGIN Scope(x.recordScope) END VisitRecordType;
  8014. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8015. BEGIN Scope(x.cellScope) END VisitCellType;
  8016. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8017. BEGIN END VisitProcedureType;
  8018. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8019. VAR msg: ARRAY 256 OF CHAR;
  8020. BEGIN
  8021. Global.GetSymbolName(x,msg);
  8022. Strings.Append(msg," ");
  8023. Strings.Append(msg,text);
  8024. diagnostics.Warning(module.sourceName,x.position.start,Diagnostics.Invalid,msg);
  8025. END Warning;
  8026. (** symbols *)
  8027. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8028. BEGIN
  8029. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8030. IF ~(x IS SyntaxTree.Parameter) THEN
  8031. Warning(x,"never used");
  8032. END;
  8033. END;
  8034. x.Accept(SELF);
  8035. END Symbol;
  8036. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8037. BEGIN END VisitSymbol;
  8038. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8039. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8040. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8041. BEGIN END VisitConstant;
  8042. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8043. BEGIN END VisitVariable;
  8044. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8045. BEGIN END VisitProperty;
  8046. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8047. BEGIN END VisitParameter;
  8048. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8049. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8050. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8051. BEGIN END VisitOperator;
  8052. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8053. BEGIN END VisitImport;
  8054. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8055. VAR
  8056. symbol: SyntaxTree.Symbol;
  8057. BEGIN
  8058. symbol := scope.firstSymbol;
  8059. WHILE(symbol # NIL) DO
  8060. Symbol(symbol);
  8061. symbol := symbol.nextSymbol;
  8062. END;
  8063. END Scope;
  8064. PROCEDURE Module*(x: SyntaxTree.Module);
  8065. BEGIN
  8066. SELF.module := x;
  8067. Scope(x.moduleScope);
  8068. END Module;
  8069. END Warnings;
  8070. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8071. BEGIN
  8072. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8073. END IsOberonInline;
  8074. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8075. BEGIN
  8076. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8077. END Resolved;
  8078. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8079. VAR i: LONGINT;
  8080. BEGIN
  8081. i := 1;
  8082. WHILE i < x DO
  8083. i := i *2
  8084. END;
  8085. RETURN i=x
  8086. END PowerOf2;
  8087. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8088. BEGIN
  8089. RETURN
  8090. (scope # NIL) &
  8091. (scope IS SyntaxTree.ModuleScope)
  8092. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8093. OR
  8094. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8095. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8096. END IsCellNetScope;
  8097. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8098. BEGIN
  8099. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8100. END IsCellScope;
  8101. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8102. BEGIN
  8103. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8104. RETURN (scope # NIL) & IsCellNetScope(scope)
  8105. END InCellNetScope;
  8106. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8107. BEGIN
  8108. ASSERT(size MOD system.dataUnit = 0);
  8109. RETURN size DIV system.dataUnit
  8110. END ToMemoryUnits;
  8111. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8112. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8113. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8114. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8115. BEGIN
  8116. IF t = NIL THEN
  8117. RETURN TRUE
  8118. ELSE
  8119. t := t.resolved;
  8120. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8121. END;
  8122. END TypeAllowed;
  8123. BEGIN
  8124. type := type.resolved;
  8125. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8126. RETURN FALSE
  8127. ELSE
  8128. procedureType := type(SyntaxTree.ProcedureType);
  8129. numberParameters := procedureType.numberParameters;
  8130. RETURN
  8131. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8132. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8133. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8134. END;
  8135. END GetProcedureAllowed;
  8136. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8137. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8138. VAR import: SyntaxTree.Import;
  8139. BEGIN
  8140. import := importCache.ImportByModuleName(x.name,x.context);
  8141. IF import # NIL THEN
  8142. importCache.RemoveImporters(x.name,x.context);
  8143. END;
  8144. END RemoveModuleFromCache;
  8145. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8146. (* to <- this assignment compatibility *)
  8147. VAR result: BOOLEAN;
  8148. BEGIN
  8149. IF this= NIL THEN result := (to=NIL)
  8150. ELSIF to=NIL THEN result := FALSE
  8151. ELSE
  8152. (*! will be replaced by this:
  8153. ELSE result := this.CompatibleTo(to.resolved);
  8154. *)
  8155. this := this.resolved; to := to.resolved;
  8156. IF to=SyntaxTree.invalidType THEN result := FALSE
  8157. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8158. ELSIF to = this THEN
  8159. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8160. ELSIF to IS SyntaxTree.BasicType THEN
  8161. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8162. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8163. result := this.CompatibleTo(to.resolved)
  8164. ELSE
  8165. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8166. END
  8167. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8168. result := to.sizeInBits = this.sizeInBits;
  8169. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8170. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8171. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8172. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8173. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8174. result := TRUE;
  8175. ELSIF to IS SyntaxTree.AnyType THEN
  8176. 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);
  8177. ELSIF to IS SyntaxTree.ObjectType THEN
  8178. 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 *) ;
  8179. ELSIF to IS SyntaxTree.ByteType THEN
  8180. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8181. ELSIF to IS SyntaxTree.CharacterType THEN
  8182. result := IsCharacterType(this)
  8183. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8184. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8185. 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
  8186. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8187. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8188. result := TRUE;
  8189. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8190. result := TRUE;
  8191. ELSE
  8192. result := FALSE
  8193. END;
  8194. ELSIF to IS SyntaxTree.PointerType THEN
  8195. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8196. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8197. & (~to.isRealtime OR this.isRealtime);
  8198. ELSIF to IS SyntaxTree.ProcedureType THEN
  8199. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8200. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8201. & (~to.isRealtime OR this.isRealtime)
  8202. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8203. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8204. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8205. ELSIF to IS SyntaxTree.RecordType THEN
  8206. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8207. ELSIF to IS SyntaxTree.ArrayType THEN
  8208. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8209. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8210. ELSIF StaticArrayCompatible(to, this) THEN
  8211. result := TRUE
  8212. ELSE
  8213. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8214. END;
  8215. ELSIF to IS SyntaxTree.MathArrayType THEN
  8216. IF this IS SyntaxTree.MathArrayType THEN
  8217. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8218. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8219. result := TRUE;
  8220. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8221. result := TRUE;
  8222. ELSE
  8223. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8224. END;
  8225. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8226. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8227. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8228. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8229. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8230. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8231. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8232. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8233. ELSE
  8234. result := FALSE
  8235. END;
  8236. (* an array-structured object type is compatible to the type of its array structure *)
  8237. ELSIF IsArrayStructuredObjectType(this) THEN
  8238. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8239. ELSE
  8240. result := FALSE;
  8241. END;
  8242. ELSIF to IS SyntaxTree.StringType THEN
  8243. result := FALSE;
  8244. ELSIF to IS SyntaxTree.EnumerationType THEN
  8245. result := IsEnumerationExtension(this,to);
  8246. ELSIF to IS SyntaxTree.PortType THEN
  8247. result := SameType(to, this)
  8248. ELSE
  8249. Printout.Info("CompatibleTo",to);
  8250. HALT(100); (* implement missing type check *)
  8251. END;
  8252. END;
  8253. RETURN result
  8254. END CompatibleTo;
  8255. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8256. VAR actualBase, formalBase: SyntaxTree.Type;
  8257. BEGIN
  8258. IF SameType(formal,actual) THEN
  8259. RETURN TRUE
  8260. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8261. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8262. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8263. RETURN
  8264. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8265. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8266. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8267. & StaticArrayCompatible(formalBase,actualBase)
  8268. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8269. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8270. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8271. RETURN
  8272. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8273. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8274. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8275. & StaticArrayCompatible(formalBase,actualBase)
  8276. ELSE RETURN FALSE
  8277. END;
  8278. END StaticArrayCompatible;
  8279. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8280. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8281. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8282. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8283. BEGIN
  8284. result := SameType(formal,actual);
  8285. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8286. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8287. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8288. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8289. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8290. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8291. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8292. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8293. & TC(formalBase, actualBase);
  8294. END;
  8295. RETURN result
  8296. END TC;
  8297. BEGIN
  8298. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8299. ELSE
  8300. arrayBase := formalType.arrayBase.resolved;
  8301. IF (actualType IS SyntaxTree.StringType) THEN
  8302. result := arrayBase IS SyntaxTree.CharacterType
  8303. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8304. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8305. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8306. result := TC(formalType, actualType);
  8307. ELSE
  8308. result := (arrayBase IS SyntaxTree.ByteType)
  8309. END;
  8310. END;
  8311. RETURN result
  8312. END OpenArrayCompatible;
  8313. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8314. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8315. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8316. BEGIN
  8317. IF actualType IS SyntaxTree.MathArrayType THEN
  8318. actualArray := actualType(SyntaxTree.MathArrayType);
  8319. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8320. (*
  8321. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8322. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8323. *)
  8324. actualBase := ArrayBase(actualType,Infinity);
  8325. formalBase := ArrayBase(formalType,Infinity);
  8326. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8327. ELSE
  8328. (*
  8329. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8330. *)
  8331. formalBase := Resolved(formalType.arrayBase);
  8332. actualBase := Resolved(actualArray.arrayBase);
  8333. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8334. (*
  8335. ARRAY [k] -> ARRAY [n]
  8336. *)
  8337. result := (formalType.staticLength = actualArray.staticLength)
  8338. ELSE
  8339. result := TRUE
  8340. END;
  8341. IF ~result THEN
  8342. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8343. ELSIF actualBase = NIL THEN result := FALSE
  8344. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8345. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8346. ELSE
  8347. result := SameType(formalBase,actualBase)
  8348. END;
  8349. END;
  8350. ELSE
  8351. result := FALSE
  8352. END;
  8353. RETURN result
  8354. END MathArrayCompatible;
  8355. (**
  8356. Math Array Type distance for assignments / parameter passings of the form
  8357. from -> to
  8358. variants:
  8359. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8360. allowed:
  8361. static -> static (& size match)
  8362. static -> open
  8363. static -> tensor
  8364. open -> open
  8365. open -> tensor
  8366. open -> static
  8367. tensor -> tensor
  8368. tensor -> open
  8369. tensor -> static
  8370. **)
  8371. (*! think about the metric here: is form matching more important than element type matching? *)
  8372. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8373. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8374. BEGIN
  8375. fromBase := Resolved(from.arrayBase);
  8376. toBase := Resolved(to.arrayBase);
  8377. i := Infinity;
  8378. IF from = to THEN
  8379. i := 0;
  8380. ELSIF (from.form = to.form) THEN
  8381. (* static -> static, open -> open, tensor -> tensor *)
  8382. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8383. IF fromBase = toBase THEN i := 0
  8384. ELSIF toBase = NIL THEN i := 1
  8385. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8386. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8387. ELSE
  8388. i := TypeDistance(system,fromBase, toBase, varpar);
  8389. END;
  8390. END;
  8391. ELSIF (to.form = SyntaxTree.Static) THEN
  8392. (* forbidden *)
  8393. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8394. (* static -> tensor, open -> tensor, tensor -> open *)
  8395. IF toBase=fromBase THEN i := 0;
  8396. ELSIF toBase = NIL THEN i := 1;
  8397. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8398. toBase := ArrayBase(toBase,Infinity);
  8399. IF (fromBase=toBase) THEN i := 0
  8400. ELSIF (toBase = NIL) THEN i:= 1
  8401. ELSIF (fromBase = NIL) THEN i := Infinity;
  8402. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8403. END;
  8404. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8405. fromBase := ArrayBase(fromBase,Infinity);
  8406. IF (fromBase=toBase) THEN i := 0
  8407. ELSIF (toBase = NIL) THEN i := 1
  8408. ELSIF (fromBase = NIL) THEN i := Infinity;
  8409. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8410. END;
  8411. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8412. END;
  8413. IF i # Infinity THEN INC(i,2) END;
  8414. ELSIF (from.form = SyntaxTree.Static) THEN
  8415. (* static -> open *)
  8416. IF toBase=fromBase THEN i := 0
  8417. ELSIF toBase = NIL THEN i := 1
  8418. ELSIF fromBase = NIL THEN i := Infinity
  8419. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8420. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8421. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8422. END;
  8423. IF i # Infinity THEN INC(i,1) END;
  8424. ELSE HALT(100); (* unknown case *)
  8425. END;
  8426. RETURN i;
  8427. END MathArrayTypeDistance;
  8428. (** compute and return the distance of two array types
  8429. - return the distance of the base types
  8430. **)
  8431. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8432. VAR i: LONGINT;
  8433. BEGIN
  8434. i := Infinity;
  8435. IF from = to THEN
  8436. i := 0
  8437. ELSE
  8438. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8439. (*
  8440. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8441. i := TypeDistance(from.base, to.base);
  8442. IF i >= 0 THEN INC(i) END
  8443. ELSIF (from.mode = open) & (to.mode = open) THEN
  8444. i := TypeDistance(from.base, to.base);
  8445. *)
  8446. END;
  8447. RETURN i
  8448. END ArrayTypeDistance;
  8449. (** compute the signature distance of a procedure and an actual parameter list
  8450. - if any of the parameters are not compatible, the result is infinite
  8451. - add up and return the distance over all parameters
  8452. **)
  8453. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8454. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8455. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8456. BEGIN
  8457. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8458. result := Infinity
  8459. ELSE
  8460. formalParameter := procedureType.firstParameter;
  8461. i := 0;
  8462. result := 0;
  8463. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8464. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8465. actualParameter := actualParameters.GetExpression(i);
  8466. ASSERT(formalParameter.type # NIL);
  8467. IF (actualParameter.type = NIL) THEN distance := Infinity
  8468. ELSE
  8469. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8470. END;
  8471. IF distance = Infinity THEN
  8472. result := Infinity;
  8473. ELSE
  8474. to := formalParameter.type.resolved;
  8475. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8476. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8477. (* already handled varpar *)
  8478. (*
  8479. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8480. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8481. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8482. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8483. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8484. END;
  8485. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8486. result := Infinity
  8487. END;
  8488. *)
  8489. INC(result, distance);
  8490. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8491. INC(result, distance);
  8492. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8493. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8494. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8495. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8496. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8497. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8498. END;
  8499. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8500. result := Infinity
  8501. END;
  8502. ELSE
  8503. result := Infinity
  8504. END;
  8505. ELSE
  8506. INC(result,distance);
  8507. END;
  8508. END;
  8509. (*
  8510. Printout.Info("actual=", actualParameter);
  8511. Printout.Info("formal=", formalParameter);
  8512. TRACE(result);
  8513. *)
  8514. formalParameter := formalParameter.nextParameter; INC(i);
  8515. END;
  8516. END;
  8517. ASSERT(result >= 0);
  8518. RETURN result
  8519. END Distance;
  8520. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8521. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8522. BEGIN
  8523. IF right.numberParameters # (procedureType.numberParameters) THEN
  8524. result := Infinity
  8525. ELSE
  8526. formalParameter := procedureType.firstParameter;
  8527. rightParameter := right.firstParameter;
  8528. i := 0;
  8529. result := 0;
  8530. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8531. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8532. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8533. IF distance = Infinity THEN
  8534. result := Infinity;
  8535. ELSE
  8536. INC(result,distance);
  8537. END;
  8538. formalParameter := formalParameter.nextParameter;
  8539. rightParameter := rightParameter.nextParameter;
  8540. END;
  8541. END;
  8542. ASSERT(result >= 0);
  8543. RETURN result
  8544. END ProcedureTypeDistance;
  8545. (** compute and return the distance between two types, used for computation of signature distance
  8546. from -> to
  8547. **)
  8548. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8549. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8550. BEGIN
  8551. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8552. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8553. END;
  8554. i := Infinity;
  8555. IF from = to THEN
  8556. i := 0
  8557. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8558. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8559. i := Infinity;
  8560. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8561. i := 10;
  8562. ELSIF (from IS SyntaxTree.StringType) THEN
  8563. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8564. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8565. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8566. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8567. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8568. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8569. i := 1
  8570. ELSIF (from IS SyntaxTree.NilType) THEN
  8571. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8572. (*
  8573. ELSIF (from = NoType) THEN
  8574. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8575. *)
  8576. ELSIF (from IS SyntaxTree.BasicType) THEN
  8577. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8578. IF varpar & (i # 0) THEN i := Infinity END;
  8579. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8580. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8581. ELSIF (from IS SyntaxTree.RecordType) THEN
  8582. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8583. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8584. IF to IS SyntaxTree.MathArrayType THEN
  8585. (*
  8586. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8587. i := Infinity;
  8588. ELSE
  8589. *)
  8590. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8591. (*
  8592. END;
  8593. *)
  8594. END
  8595. ELSIF (from IS SyntaxTree.PointerType) THEN
  8596. ptr := from(SyntaxTree.PointerType);
  8597. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8598. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8599. (* ELSE i := TypeDistance(ptr.base, to); *)
  8600. END
  8601. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8602. IF (to IS SyntaxTree.ProcedureType) THEN
  8603. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8604. END;
  8605. ELSIF (from IS SyntaxTree.PortType) THEN
  8606. IF (to IS SyntaxTree.PortType) THEN
  8607. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8608. i := 0;
  8609. END;
  8610. END;
  8611. (*no procedure test, procedure must be the same*)
  8612. END;
  8613. RETURN i
  8614. END TypeDistance;
  8615. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8616. BEGIN
  8617. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8618. END IsIntegerType;
  8619. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8620. BEGIN
  8621. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8622. END IsAddressType;
  8623. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8624. BEGIN
  8625. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8626. END IsSizeType;
  8627. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8628. BEGIN
  8629. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8630. END IsSignedIntegerType;
  8631. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8632. BEGIN
  8633. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8634. END IsUnsignedIntegerType;
  8635. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8636. VAR result: BOOLEAN;
  8637. BEGIN
  8638. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8639. value := x.resolved(SyntaxTree.IntegerValue).value;
  8640. result := TRUE
  8641. ELSE
  8642. result := FALSE
  8643. END;
  8644. RETURN result
  8645. END IsIntegerValue;
  8646. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8647. VAR result: BOOLEAN;
  8648. BEGIN
  8649. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8650. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8651. result := TRUE
  8652. ELSE
  8653. result := FALSE
  8654. END;
  8655. RETURN result
  8656. END IsEnumerationValue;
  8657. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8658. VAR result: BOOLEAN;
  8659. BEGIN
  8660. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8661. value := x.resolved(SyntaxTree.RealValue).value;
  8662. result := TRUE
  8663. ELSE
  8664. result := FALSE
  8665. END;
  8666. RETURN result
  8667. END IsRealValue;
  8668. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8669. VAR result: BOOLEAN;
  8670. BEGIN
  8671. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8672. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8673. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8674. result := TRUE
  8675. ELSE
  8676. result := FALSE
  8677. END;
  8678. RETURN result
  8679. END IsComplexValue;
  8680. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8681. VAR result: BOOLEAN;
  8682. BEGIN
  8683. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8684. value := x.resolved(SyntaxTree.CharacterValue).value;
  8685. result := TRUE
  8686. ELSE
  8687. result := FALSE
  8688. END;
  8689. RETURN result
  8690. END IsCharacterValue;
  8691. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8692. VAR result: BOOLEAN;
  8693. BEGIN
  8694. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8695. value := x.resolved(SyntaxTree.BooleanValue).value;
  8696. result := TRUE
  8697. ELSE
  8698. result := FALSE
  8699. END;
  8700. RETURN result
  8701. END IsBooleanValue;
  8702. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8703. VAR result: BOOLEAN;
  8704. BEGIN
  8705. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8706. value := x.resolved(SyntaxTree.SetValue).value;
  8707. result := TRUE
  8708. ELSE
  8709. result := FALSE
  8710. END;
  8711. RETURN result
  8712. END IsSetValue;
  8713. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8714. VAR result: BOOLEAN;
  8715. BEGIN
  8716. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8717. value := x.resolved(SyntaxTree.StringValue).value;
  8718. result := TRUE
  8719. ELSE
  8720. result := FALSE
  8721. END;
  8722. RETURN result
  8723. END IsStringValue;
  8724. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8725. BEGIN
  8726. x := x.resolved;
  8727. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8728. END Indexable;
  8729. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8730. BEGIN
  8731. RETURN t1.SameType(t2.resolved);
  8732. END SameType;
  8733. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8734. BEGIN
  8735. IF t IS SyntaxTree.MathArrayType THEN
  8736. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8737. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8738. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8739. DEC(max);
  8740. END;
  8741. ELSIF t IS SyntaxTree.ArrayType THEN
  8742. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8743. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8744. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8745. END;
  8746. END;
  8747. RETURN t;
  8748. END ArrayBase;
  8749. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8750. BEGIN
  8751. type := type.resolved;
  8752. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8753. base := type(SyntaxTree.ArrayType).arrayBase;
  8754. RETURN TRUE;
  8755. END;
  8756. RETURN FALSE;
  8757. END IsOpenArray;
  8758. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8759. BEGIN
  8760. type := type.resolved;
  8761. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8762. base := type(SyntaxTree.ArrayType).arrayBase;
  8763. dim := type(SyntaxTree.ArrayType).staticLength;
  8764. RETURN TRUE
  8765. ELSE
  8766. RETURN FALSE
  8767. END;
  8768. END IsStaticArray;
  8769. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8770. BEGIN
  8771. type := type.resolved;
  8772. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8773. base := type(SyntaxTree.ArrayType).arrayBase;
  8774. RETURN TRUE
  8775. ELSE
  8776. RETURN FALSE
  8777. END;
  8778. END IsDynamicArray;
  8779. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8780. VAR i: LONGINT;
  8781. BEGIN
  8782. i := 0;
  8783. t := t.resolved;
  8784. IF t IS SyntaxTree.MathArrayType THEN
  8785. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8786. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8787. END;
  8788. ELSIF t IS SyntaxTree.ArrayType THEN
  8789. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8790. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8791. END;
  8792. END;
  8793. RETURN i
  8794. END Dimension;
  8795. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8796. BEGIN
  8797. RETURN expression.assignable;
  8798. END IsVariable;
  8799. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8800. BEGIN
  8801. IF (symbol IS SyntaxTree.Parameter) THEN
  8802. WITH symbol: SyntaxTree.Parameter DO
  8803. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8804. END;
  8805. ELSE
  8806. RETURN FALSE
  8807. END;
  8808. END IsVariableParameter;
  8809. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8810. VAR result: BOOLEAN;
  8811. BEGIN
  8812. IF type = NIL THEN result := FALSE
  8813. ELSE
  8814. type := type.resolved;
  8815. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8816. END;
  8817. RETURN result
  8818. END IsPointerType;
  8819. PROCEDURE IsUnsafePointer*(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.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8826. END;
  8827. RETURN result
  8828. END IsUnsafePointer;
  8829. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8830. BEGIN
  8831. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8832. END IsDisposable;
  8833. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8834. VAR result: BOOLEAN;
  8835. BEGIN
  8836. IF type = NIL THEN result := FALSE
  8837. ELSE
  8838. type := type.resolved;
  8839. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8840. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8841. result := result OR (type IS SyntaxTree.ObjectType);
  8842. END;
  8843. RETURN result
  8844. END IsPointerToRecord;
  8845. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8846. VAR result: BOOLEAN;
  8847. BEGIN
  8848. IF type = NIL THEN result := FALSE
  8849. ELSE
  8850. type := type.resolved;
  8851. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8852. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8853. ;
  8854. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8855. result := result OR (type IS SyntaxTree.ObjectType);
  8856. END;
  8857. RETURN result
  8858. END IsPointerToObject;
  8859. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8860. BEGIN
  8861. IF type # NIL THEN
  8862. RETURN type.resolved.hasPointers
  8863. ELSE
  8864. RETURN FALSE
  8865. END;
  8866. END ContainsPointer;
  8867. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8868. BEGIN
  8869. IF type = NIL THEN RETURN FALSE END;
  8870. type := type.resolved;
  8871. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8872. END IsStringType;
  8873. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8874. BEGIN
  8875. IF type = NIL THEN RETURN FALSE END;
  8876. type := type.resolved;
  8877. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8878. END IsCharacterType;
  8879. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8880. BEGIN
  8881. IF type = NIL THEN RETURN FALSE END;
  8882. type := type.resolved;
  8883. RETURN (type IS SyntaxTree.EnumerationType)
  8884. END IsEnumerationType;
  8885. (** cf. section "Type extension (base type)" in the language report **)
  8886. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8887. VAR result: BOOLEAN;
  8888. BEGIN
  8889. ASSERT(base # NIL); ASSERT(extension # NIL);
  8890. base := base.resolved; extension := extension.resolved;
  8891. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8892. result := TRUE;
  8893. ELSE
  8894. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8895. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8896. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8897. END;
  8898. WHILE (extension # NIL) & (extension # base) DO
  8899. IF extension IS SyntaxTree.RecordType THEN
  8900. extension := extension(SyntaxTree.RecordType).baseType;
  8901. IF (extension # NIL) THEN extension := extension.resolved END;
  8902. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8903. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8904. END;
  8905. ELSE extension := NIL;
  8906. END;
  8907. END;
  8908. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8909. END;
  8910. RETURN result
  8911. END IsTypeExtension;
  8912. (** check if base is the base enumeration type of extension **)
  8913. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8914. BEGIN
  8915. base := base.resolved; extension := extension.resolved;
  8916. WHILE (extension # NIL) & (extension # base) DO
  8917. IF extension IS SyntaxTree.EnumerationType THEN
  8918. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8919. IF extension # NIL THEN extension := extension.resolved END;
  8920. ELSE
  8921. extension := NIL
  8922. END;
  8923. END;
  8924. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8925. END IsEnumerationExtension;
  8926. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8927. BEGIN
  8928. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8929. RETURN TRUE
  8930. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8931. RETURN TRUE
  8932. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8933. RETURN TRUE
  8934. ELSE
  8935. RETURN FALSE
  8936. END
  8937. END IsCallable;
  8938. (** compute and return the distance of two record types
  8939. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8940. **)
  8941. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8942. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8943. BEGIN
  8944. i := 0;
  8945. WHILE (from # NIL) & (from # to) DO
  8946. baseType := from.baseType;
  8947. IF (baseType # NIL) THEN
  8948. baseType := baseType.resolved;
  8949. IF baseType IS SyntaxTree.PointerType THEN
  8950. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8951. END;
  8952. IF baseType IS SyntaxTree.RecordType THEN
  8953. from := baseType(SyntaxTree.RecordType);
  8954. ELSE
  8955. from := NIL;
  8956. END;
  8957. ELSE
  8958. from := NIL
  8959. END;
  8960. INC(i)
  8961. END;
  8962. IF from = NIL THEN i := Infinity END;
  8963. RETURN i
  8964. END RecordTypeDistance;
  8965. (** compute and return the distance of two pointer types **)
  8966. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8967. BEGIN
  8968. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8969. RETURN Infinity;
  8970. ELSE
  8971. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8972. END;
  8973. END PointerTypeDistance;
  8974. (** check if expression contains a symbol designator pointing to a type declaration.
  8975. - if so then enter type declaration into typeDeclaration and return true else return false
  8976. **)
  8977. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8978. VAR result: BOOLEAN;
  8979. BEGIN
  8980. result := FALSE;
  8981. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8982. result := TRUE;
  8983. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8984. END;
  8985. RETURN result
  8986. END IsTypeDesignator;
  8987. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8988. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8989. VAR result: BOOLEAN;
  8990. BEGIN
  8991. type := type.resolved;
  8992. IF type IS SyntaxTree.PointerType THEN
  8993. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8994. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8995. result := TRUE
  8996. ELSE
  8997. result := type IS SyntaxTree.RecordType
  8998. END;
  8999. RETURN result
  9000. END IsExtensibleType;
  9001. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9002. BEGIN
  9003. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9004. (d IS SyntaxTree.SymbolDesignator) &
  9005. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9006. OR
  9007. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9008. END IsUnextensibleRecord;
  9009. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9010. BEGIN
  9011. IF IsUnextensibleRecord(d) THEN
  9012. RETURN FALSE
  9013. ELSE RETURN IsExtensibleType(d.type.resolved)
  9014. END;
  9015. END IsExtensibleDesignator;
  9016. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9017. BEGIN
  9018. type := type.resolved;
  9019. IF (type IS SyntaxTree.PointerType) THEN
  9020. RETURN TRUE
  9021. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9022. RETURN TRUE
  9023. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9024. RETURN TRUE
  9025. ELSIF (type IS SyntaxTree.BasicType) THEN
  9026. RETURN TRUE
  9027. END;
  9028. RETURN FALSE
  9029. END IsBasicType;
  9030. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9031. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9032. BEGIN
  9033. baseType := record.baseType;
  9034. IF (baseType # NIL) THEN
  9035. baseType := baseType.resolved;
  9036. IF (baseType IS SyntaxTree.PointerType) THEN
  9037. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9038. END;
  9039. END;
  9040. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9041. recordType := baseType(SyntaxTree.RecordType);
  9042. ELSE
  9043. recordType := NIL;
  9044. END;
  9045. RETURN recordType
  9046. END RecordBase;
  9047. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9048. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9049. BEGIN
  9050. baseRecord := RecordBase(scope.ownerRecord);
  9051. IF baseRecord = NIL THEN RETURN NIL END;
  9052. scope := baseRecord.recordScope;
  9053. procedureType := procedure.type.resolved;
  9054. IF procedure IS SyntaxTree.Operator THEN
  9055. operator := scope.firstOperator;
  9056. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9057. (*
  9058. Printout.Info("not same ",procedureType);
  9059. Printout.Info("with ",operator.type);
  9060. *)
  9061. operator := operator.nextOperator;
  9062. END;
  9063. super := operator;
  9064. ELSE
  9065. super := scope.firstProcedure;
  9066. WHILE (super # NIL) & (super.name # procedure.name) DO
  9067. super := super.nextProcedure;
  9068. END;
  9069. END;
  9070. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9071. RETURN super
  9072. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9073. RETURN super
  9074. ELSE
  9075. RETURN FindSuperProcedure(scope,procedure);
  9076. END;
  9077. END FindSuperProcedure;
  9078. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9079. VAR procedure: SyntaxTree.Procedure;
  9080. BEGIN
  9081. procedure := record.recordScope.constructor;
  9082. IF procedure = NIL THEN
  9083. record := RecordBase(record);
  9084. IF record # NIL THEN
  9085. procedure := GetConstructor(record)
  9086. END;
  9087. END;
  9088. RETURN procedure;
  9089. END GetConstructor;
  9090. (* enter a case into a list of cases in a sorted way and check for collision *)
  9091. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9092. VAR prev,this,new: SyntaxTree.CaseConstant;
  9093. BEGIN
  9094. this := root;
  9095. prev := NIL;
  9096. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9097. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9098. RETURN FALSE
  9099. ELSE
  9100. IF (this # NIL) & (this.min = max+1) THEN
  9101. this.min := min
  9102. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9103. prev.max := min
  9104. ELSE
  9105. NEW(new); new.min := min; new.max := max;
  9106. new.next := this;
  9107. IF prev = NIL THEN
  9108. root := new;
  9109. ELSE
  9110. prev.next := new
  9111. END
  9112. END;
  9113. RETURN TRUE
  9114. END;
  9115. END EnterCase;
  9116. (** generate and return a new checker object, errors are entered into diagnostics **)
  9117. 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;
  9118. VAR checker: Checker;
  9119. BEGIN
  9120. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9121. RETURN checker
  9122. END NewChecker;
  9123. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9124. VAR warnings: Warnings;
  9125. BEGIN
  9126. NEW(warnings, diagnostics); RETURN warnings;
  9127. END NewWarnings;
  9128. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9129. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9130. END IsRangeType;
  9131. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9132. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9133. END IsMathArrayType;
  9134. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9135. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9136. END IsArrayType;
  9137. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9138. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9139. END IsComplexType;
  9140. (** if a type is an array-structured object type *)
  9141. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9142. VAR recordType: SyntaxTree.RecordType;
  9143. BEGIN
  9144. IF type = NIL THEN
  9145. RETURN FALSE
  9146. ELSE
  9147. type := type.resolved;
  9148. IF type IS SyntaxTree.PointerType THEN
  9149. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9150. IF type IS SyntaxTree.RecordType THEN
  9151. recordType := type(SyntaxTree.RecordType);
  9152. RETURN recordType.isObject & recordType.HasArrayStructure()
  9153. ELSE
  9154. RETURN FALSE
  9155. END
  9156. ELSE
  9157. RETURN FALSE
  9158. END
  9159. END
  9160. END IsArrayStructuredObjectType;
  9161. (** the math array structure of a type
  9162. - for math arrays: the array itself
  9163. - for pointers: the math array structure of the pointer base
  9164. - for array-structured object types: the underlying structure
  9165. - for non-math arrays and all other types: NIL
  9166. **)
  9167. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9168. VAR
  9169. result: SyntaxTree.MathArrayType;
  9170. BEGIN
  9171. IF type = NIL THEN
  9172. result := NIL
  9173. ELSE
  9174. type := type.resolved;
  9175. IF type IS SyntaxTree.PointerType THEN
  9176. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9177. END;
  9178. IF type IS SyntaxTree.MathArrayType THEN
  9179. result := type(SyntaxTree.MathArrayType)
  9180. ELSIF type IS SyntaxTree.RecordType THEN
  9181. result := type(SyntaxTree.RecordType).arrayStructure
  9182. ELSE
  9183. result := NIL
  9184. END
  9185. END;
  9186. RETURN result
  9187. END MathArrayStructureOfType;
  9188. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9189. VAR
  9190. result: BOOLEAN;
  9191. rangeExpression: SyntaxTree.RangeExpression;
  9192. BEGIN
  9193. IF x IS SyntaxTree.RangeExpression THEN
  9194. rangeExpression := x(SyntaxTree.RangeExpression);
  9195. result := TRUE;
  9196. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9197. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9198. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9199. ELSE
  9200. result := FALSE
  9201. END;
  9202. RETURN result
  9203. END IsStaticRange;
  9204. (** whether a type is a math array of tensor form **)
  9205. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9206. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9207. END IsTensor;
  9208. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9209. BEGIN
  9210. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9211. length := type(SyntaxTree.MathArrayType).staticLength;
  9212. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9213. RETURN TRUE
  9214. ELSE
  9215. RETURN FALSE
  9216. END;
  9217. END IsStaticMathArray;
  9218. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9219. BEGIN
  9220. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9221. END SymbolHasAddress;
  9222. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9223. BEGIN
  9224. RETURN
  9225. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9226. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9227. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9228. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9229. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9230. ;
  9231. END HasAddress;
  9232. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9233. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9234. BEGIN
  9235. IF (e IS SyntaxTree.Designator) THEN
  9236. d := e(SyntaxTree.Designator);
  9237. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9238. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9239. e := d.left;
  9240. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9241. END;
  9242. IF d # NIL THEN
  9243. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9244. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9245. END;
  9246. END;
  9247. RETURN FALSE;
  9248. END IsLocalVariable;
  9249. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9250. BEGIN
  9251. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9252. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9253. ELSE
  9254. RETURN TRUE
  9255. END;
  9256. END IsStaticProcedure;
  9257. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9258. CONST OptimizeMethodTable = FALSE;
  9259. BEGIN
  9260. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9261. END InMethodTable;
  9262. END FoxSemanticChecker.
  9263. SystemTools.FreeDownTo FoxSemanticChecker ~