FoxSemanticChecker.Mod 400 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969
  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. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  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: LONGINT; 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. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. x.SetArrayBase(pointerType);
  483. END;
  484. IF x.length # NIL THEN
  485. variableAccessed := FALSE;
  486. e := ResolveExpression(x.length);
  487. IF (e.resolved = NIL) THEN
  488. IF variableAccessed THEN
  489. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  490. END;
  491. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. x.SetState(SyntaxTree.Resolved);
  572. END;
  573. resolvedType := ResolvedType(x);
  574. END VisitMathArrayType;
  575. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  576. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  577. (1) Rec = RECORD ... END; Ptr <---> Rec
  578. Ptr = POINTER TO Rec; ^ |
  579. | |
  580. TypeDesc TypeDesc
  581. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  582. ^ /
  583. | /
  584. TypeDesc <-- /
  585. *)
  586. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  587. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  588. BEGIN
  589. Strings.IntToStr(x.position,number);
  590. COPY(prefix,name);
  591. Strings.Append(name,"@");
  592. Strings.Append(name,number);
  593. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  594. typeDeclaration.SetDeclaredType(x);
  595. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  596. x.SetTypeDeclaration(typeDeclaration);
  597. currentScope.AddTypeDeclaration(typeDeclaration);
  598. typeDeclaration.SetScope(currentScope);
  599. END AnonymousTypeDeclaration;
  600. (**
  601. deferred pointer type resolving
  602. - resolve base type
  603. - check that base type is a record or array type
  604. - if error then set base type to invalid type
  605. **)
  606. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  607. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  608. BEGIN
  609. ASSERT(type.pointerBase # NIL);
  610. position := type.pointerBase.position;
  611. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  612. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  613. (* not for pointers, a type is needed for the records only
  614. IF type.typeDeclaration = NIL THEN
  615. AnonymousTypeDeclaration(type);
  616. END;
  617. *)
  618. END;
  619. resolved := ResolveType(type.pointerBase);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  621. type.SetPointerBase(resolved);
  622. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  623. recordType := resolved.resolved(SyntaxTree.RecordType);
  624. IF recordType.isObject & (recordType.baseType # NIL) THEN
  625. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  626. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  627. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  628. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  629. END;
  630. END;
  631. END;
  632. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  633. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  634. END;
  635. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  636. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  638. ELS
  639. *)
  640. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  642. END;
  643. END;
  644. ELSE
  645. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  646. type.SetPointerBase(SyntaxTree.invalidType)
  647. END
  648. END FixPointerType;
  649. (**
  650. resolve pointer type
  651. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  652. **)
  653. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  654. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  655. modifiers: SyntaxTree.Modifier; position: LONGINT;
  656. BEGIN
  657. IF TypeNeedsResolution(x) THEN
  658. modifiers := x.modifiers;
  659. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  660. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  661. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  662. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  663. (* inheritance cycle check
  664. example:
  665. A=POINTER TO RECORD(B) END;
  666. B=POINTER TO RECORD(A) END;
  667. *)
  668. IF x.pointerBase IS SyntaxTree.RecordType THEN
  669. recordType := x.pointerBase(SyntaxTree.RecordType);
  670. recordBaseType := ResolveType(recordType.baseType);
  671. recordType.SetBaseType(recordBaseType);
  672. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  673. END;
  674. CheckModifiers(modifiers, TRUE);
  675. typeFixes.Add(x,currentScope);
  676. x.SetState(SyntaxTree.Resolved);
  677. END;
  678. resolvedType := ResolvedType(x)
  679. END VisitPointerType;
  680. (**
  681. resolve port type
  682. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  683. **)
  684. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  685. VAR value: LONGINT;
  686. BEGIN
  687. IF TypeNeedsResolution(x) THEN
  688. x.SetCellsAreObjects(cellsAreObjects);
  689. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  690. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  691. x.SetSize(value)
  692. ELSE
  693. x.SetSize(system.SizeOf(system.longintType));
  694. END;
  695. x.SetState(SyntaxTree.Resolved);
  696. END;
  697. resolvedType := ResolvedType(x)
  698. END VisitPortType;
  699. (**
  700. deferred procedure type resolving
  701. - resolve return type
  702. - traverse and resolve parameters
  703. **)
  704. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  705. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  706. BEGIN
  707. resolved := ResolveType(procedureType.returnType);
  708. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  709. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  710. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  711. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  712. END;
  713. procedureType.SetReturnType(resolved);
  714. IF (resolved # NIL) THEN
  715. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  716. parameter.SetType(procedureType.returnType);
  717. parameter.SetAccess(SyntaxTree.Hidden);
  718. parameter.SetUntraced(procedureType.hasUntracedReturn);
  719. VisitParameter(parameter);
  720. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  721. END;
  722. (* process parameters *)
  723. parameter :=procedureType.firstParameter;
  724. WHILE (parameter # NIL) DO
  725. VisitParameter(parameter);
  726. parameter := parameter.nextParameter;
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,Diagnostics.Invalid,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,Diagnostics.Invalid,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  792. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  793. IF svalue[0] = "~" THEN
  794. Strings.TrimLeft(svalue, "~");
  795. IF svalue = backendName THEN
  796. RETURN TRUE;
  797. END;
  798. ELSIF svalue # backendName THEN
  799. RETURN TRUE;
  800. END;
  801. END;
  802. IF x.baseType # NIL THEN
  803. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  804. END;
  805. RETURN FALSE;
  806. END SkipImplementation;
  807. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  808. VAR this: SyntaxTree.Modifier;
  809. BEGIN
  810. this := modifiers;
  811. WHILE this # NIL DO
  812. IF ~this.resolved THEN
  813. IF checkUse THEN
  814. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  815. ELSE
  816. this.SetExpression(ResolveExpression(this.expression));
  817. this.Resolved;
  818. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  819. END;
  820. END;
  821. this := this.nextModifier
  822. END;
  823. END CheckModifiers;
  824. (**
  825. resolve procedure type
  826. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  827. **)
  828. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  829. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  830. BEGIN
  831. IF TypeNeedsResolution(procedureType) THEN
  832. modifiers := procedureType.modifiers;
  833. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  834. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  835. procedureType.SetInterrupt(TRUE);
  836. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  837. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  838. IF useDarwinCCalls THEN (*fld*)
  839. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  840. ELSE
  841. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  842. END
  843. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  844. procedureType.SetNoReturn(TRUE);
  845. END;
  846. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  847. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  848. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  849. CheckModifiers(modifiers, TRUE);
  850. modifiers := procedureType.returnTypeModifiers;
  851. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  852. CheckModifiers(modifiers, TRUE);
  853. typeFixes.Add(procedureType,currentScope);
  854. procedureType.SetHasPointers(procedureType.isDelegate);
  855. procedureType.SetState(SyntaxTree.Resolved);
  856. END;
  857. resolvedType := ResolvedType(procedureType)
  858. END VisitProcedureType;
  859. (** check and resolve record type
  860. - check base type: must be record, math array or array-structured object type
  861. - check declarations
  862. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  863. **)
  864. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  865. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  866. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  867. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  868. hasPointers: BOOLEAN;
  869. modifiers: SyntaxTree.Modifier;
  870. value: LONGINT;
  871. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  872. BEGIN
  873. type := type.resolved;
  874. IF (type IS SyntaxTree.PointerType) &
  875. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  876. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  877. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  878. RETURN TRUE
  879. ELSE
  880. RETURN FALSE
  881. END;
  882. END IsPointerToRecord;
  883. BEGIN
  884. IF TypeNeedsResolution(x) THEN
  885. hasPointers := FALSE;
  886. modifiers := x.modifiers;
  887. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  888. CheckModifiers(modifiers, TRUE);
  889. IF x.baseType # NIL THEN
  890. position := x.baseType.position;
  891. baseType := ResolveType(x.baseType);
  892. resolved := baseType.resolved;
  893. hasPointers := hasPointers OR resolved.hasPointers;
  894. IF x.isObject THEN (* object *)
  895. ASSERT(x.pointerType # NIL);
  896. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  897. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  898. baseType := NIL
  899. ELSIF IsPointerToRecord(resolved,recordType) THEN
  900. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  901. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  902. ELSE
  903. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  904. END;
  905. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  906. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  907. ELSIF IsPointerToRecord(resolved,recordType) THEN
  908. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  909. ELSIF resolved IS SyntaxTree.RecordType THEN
  910. ELSE
  911. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  912. END;
  913. ELSE
  914. IF resolved IS SyntaxTree.RecordType THEN
  915. ELSE
  916. Error(position, Diagnostics.Invalid,"record does not extend record")
  917. END;
  918. END;
  919. x.SetBaseType(baseType);
  920. IF x.Level() > 15 THEN
  921. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  922. (* note:
  923. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  924. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  925. inheritance history of a type.
  926. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  927. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  928. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  929. *)
  930. END;
  931. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  932. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  933. END;
  934. END;
  935. Declarations(x.recordScope);
  936. ResolveArrayStructure(x);
  937. (* computation of sizes and offsets skipped -> done in backend / system *)
  938. recordBase := x.GetBaseRecord();
  939. IF recordBase = NIL THEN numberMethods := 0
  940. ELSE numberMethods := recordBase.recordScope.numberMethods
  941. END;
  942. isRealtime := TRUE;
  943. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  944. WHILE symbol # NIL DO
  945. IF symbol IS SyntaxTree.Variable THEN
  946. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  947. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  948. END;
  949. IF symbol IS SyntaxTree.Procedure THEN
  950. procedure := symbol(SyntaxTree.Procedure);
  951. IF procedure.super # NIL THEN
  952. procedure.SetMethodNumber(procedure.super.methodNumber)
  953. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  954. procedure.SetMethodNumber(numberMethods);
  955. INC(numberMethods);
  956. END;
  957. END;
  958. symbol := symbol.nextSymbol;
  959. END;
  960. IF isRealtime THEN x.SetRealtime(TRUE) END;
  961. x.recordScope.SetNumberMethods(numberMethods);
  962. (* TODO: is this needed anymore? *)
  963. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  964. Error(x.position,Diagnostics.Invalid,"object extends a record")
  965. END;
  966. IF (x.typeDeclaration = NIL) THEN
  967. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  968. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  969. (*
  970. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  971. AnonymousTypeDeclaration(x,name);
  972. *)
  973. ELSE
  974. AnonymousTypeDeclaration(x,"Anonymous");
  975. END;
  976. END;
  977. x.SetHasPointers(hasPointers);
  978. x.SetState(SyntaxTree.Resolved);
  979. END;
  980. resolvedType := ResolvedType(x);
  981. END VisitRecordType;
  982. (** check and resolve cell type
  983. - check base type: must be cell
  984. - check declarations
  985. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  986. **)
  987. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  988. VAR
  989. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  990. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  991. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  992. recordBase: SyntaxTree.RecordType;
  993. numberMethods, int: LONGINT;
  994. real: LONGREAL;
  995. bool: BOOLEAN;
  996. set: SET;
  997. v: SyntaxTree.Expression;
  998. str: Scanner.StringType;
  999. atype: SyntaxTree.ArrayType;
  1000. prev: SyntaxTree.Scope;
  1001. skip: BOOLEAN;
  1002. svalue: ARRAY 32 OF CHAR;
  1003. BEGIN
  1004. IF TypeNeedsResolution(x) THEN
  1005. recordBase := NIL;
  1006. IF cellsAreObjects THEN
  1007. IF x.baseType = NIL THEN
  1008. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1009. ImportModule(qualifiedIdentifier.prefix, x.position);
  1010. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1011. x.SetBaseType(ResolveType(x.baseType));
  1012. recordBase := x.GetBaseRecord();
  1013. IF recordBase = NIL THEN
  1014. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1015. END;
  1016. ELSE
  1017. x.SetBaseType(ResolveType(x.baseType));
  1018. END;
  1019. ELSE
  1020. x.SetBaseType(ResolveType(x.baseType));
  1021. END;
  1022. IF recordBase = NIL THEN numberMethods := 0
  1023. ELSE numberMethods := recordBase.recordScope.numberMethods
  1024. END;
  1025. modifier := x.modifiers;
  1026. (*IF ~x.isCellNet THEN*)
  1027. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1028. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1029. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1030. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1031. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1032. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1033. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1034. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1035. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1036. symbol := system.activeCellsCapabilities;
  1037. WHILE symbol # NIL DO
  1038. IF HasFlag(modifier, symbol.name, position) THEN END;
  1039. symbol := symbol.nextSymbol;
  1040. END;
  1041. modifier := x.modifiers;
  1042. WHILE (modifier # NIL) DO
  1043. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1044. IF modifier.expression # NIL THEN
  1045. v := ConstantExpression(modifier.expression);
  1046. property.SetValue(v);
  1047. IF IsIntegerValue(modifier.expression, int) THEN
  1048. (*property.SetValue(modifier.expression);*)
  1049. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1050. property.SetType(system.longintType);
  1051. ELSIF IsRealValue(modifier.expression, real) THEN
  1052. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1053. property.SetType(system.longrealType);
  1054. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1055. property.SetType(system.booleanType);
  1056. ELSIF IsSetValue(modifier.expression, set) THEN
  1057. property.SetType(system.setType);
  1058. ELSIF IsStringValue(modifier.expression, str) THEN
  1059. (*property.SetValue(modifier.expression);*)
  1060. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1061. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1062. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1063. property.SetType(atype);
  1064. ELSE
  1065. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1066. END;
  1067. ELSE (* flag property *)
  1068. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1069. property.SetType(system.booleanType);
  1070. END;
  1071. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1072. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1073. x.AddProperty(property);
  1074. modifier := modifier.nextModifier;
  1075. END;
  1076. (*ELSE
  1077. (* no: this should not be allowed on cell net types or check against global scope ...
  1078. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1079. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1080. END;
  1081. END
  1082. *)
  1083. END;*)
  1084. CheckModifiers(modifier, FALSE);
  1085. IF ~SkipImplementation(x) THEN
  1086. Declarations(x.cellScope);
  1087. ELSE
  1088. parameter :=x.firstParameter;
  1089. WHILE(parameter # NIL) DO
  1090. parameter.SetScope(x.cellScope);
  1091. parameter := parameter.nextParameter;
  1092. END;
  1093. END;
  1094. (* process parameters *)
  1095. prev := currentScope;
  1096. currentScope := x.cellScope;
  1097. parameter :=x.firstParameter;
  1098. WHILE (parameter # NIL) DO
  1099. VisitParameter(parameter);
  1100. type := parameter.type.resolved;
  1101. IF ~(type IS SyntaxTree.PortType) THEN
  1102. WHILE IsStaticArray(type, type, len) DO
  1103. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1104. END;
  1105. WHILE IsDynamicArray(type, type) DO
  1106. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1107. END;
  1108. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1109. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1110. END;
  1111. END;
  1112. parameter := parameter.nextParameter;
  1113. END;
  1114. currentScope := prev;
  1115. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1116. WHILE symbol # NIL DO
  1117. IF symbol IS SyntaxTree.Variable THEN
  1118. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1119. END;
  1120. symbol := symbol.nextSymbol;
  1121. END;
  1122. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1123. IF (x.typeDeclaration = NIL) THEN
  1124. AnonymousTypeDeclaration(x,"Anonymous");
  1125. END;
  1126. x.SetState(SyntaxTree.Resolved);
  1127. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1128. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1129. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1130. Warning(x.position, "Non-empty body for an engine?");
  1131. END;
  1132. END;
  1133. resolvedType := ResolvedType(x);
  1134. END VisitCellType;
  1135. (* check if an object is an array-structured object type
  1136. - determine the array structure
  1137. - collect operators from top to bottom in the inheritance hierarchy
  1138. - check if LEN operator is declared
  1139. - determine number of possible index operators
  1140. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1141. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1142. *)
  1143. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1144. VAR
  1145. indexOperatorCount, i: LONGINT;
  1146. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1147. isTensor: BOOLEAN;
  1148. BEGIN
  1149. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1150. (* determine array structure *)
  1151. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1152. END;
  1153. IF recordType.HasArrayStructure() THEN
  1154. (* the object is an ASOT *)
  1155. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1156. (* reset array access operators *)
  1157. arrayAccessOperators.len := NIL;
  1158. arrayAccessOperators.generalRead := NIL;
  1159. arrayAccessOperators.generalWrite := NIL;
  1160. IF isTensor THEN
  1161. (* all operators of dimensionalities 1 to max *)
  1162. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1163. ELSE
  1164. (* all operators of certain dimensionality *)
  1165. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1166. END;
  1167. NEW(arrayAccessOperators.read, indexOperatorCount);
  1168. NEW(arrayAccessOperators.write, indexOperatorCount);
  1169. FOR i := 0 TO indexOperatorCount - 1 DO
  1170. arrayAccessOperators.read[i] := NIL;
  1171. arrayAccessOperators.write[i] := NIL
  1172. END;
  1173. (* collect access operators in the record scope *)
  1174. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1175. IF arrayAccessOperators.len = NIL THEN
  1176. (* TODO: think about making this operator optional for static array structures *)
  1177. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1178. END;
  1179. (* show error messages *)
  1180. IF isTensor THEN
  1181. (* require ARRAY [*] OF RANGE *)
  1182. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1183. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1184. ELSE
  1185. (* forbid ARRAY [*] OF RANGE *)
  1186. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1187. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1188. (* require RANGE, RANGE, ... RANGE *)
  1189. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1190. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1191. END;
  1192. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1193. ELSE
  1194. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1195. IF recordType.recordScope.firstOperator # NIL THEN
  1196. RETURN;
  1197. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1198. END
  1199. END
  1200. END ResolveArrayStructure;
  1201. (** collect array access operators in a record scope **)
  1202. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1203. VAR
  1204. baseType: SyntaxTree.Type;
  1205. operator: SyntaxTree.Operator;
  1206. isReadOperator, isGeneralOperator: BOOLEAN;
  1207. indexListSize, indexListKind, hashValue: LONGINT;
  1208. BEGIN
  1209. (* if a parent record scope exists, collect the operators there first *)
  1210. baseType := recordScope.ownerRecord.baseType;
  1211. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1212. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1213. END;
  1214. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1215. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1216. END;
  1217. (* go through all operators in the current record scope *)
  1218. operator := recordScope.firstOperator;
  1219. WHILE operator # NIL DO
  1220. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1221. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1222. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1223. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1224. IF isGeneralOperator THEN
  1225. IF isReadOperator THEN
  1226. arrayAccessOperators.generalRead := operator
  1227. ELSE
  1228. arrayAccessOperators.generalWrite := operator
  1229. END
  1230. ELSE
  1231. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1232. IF isReadOperator THEN
  1233. arrayAccessOperators.read[hashValue] := operator
  1234. ELSE
  1235. arrayAccessOperators.write[hashValue] := operator
  1236. END
  1237. END
  1238. END
  1239. ELSE
  1240. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1241. END;
  1242. operator := operator.nextOperator
  1243. END
  1244. END CollectArrayAccessOperators;
  1245. (** the hash value of an index operator **)
  1246. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1247. VAR result: LONGINT;
  1248. BEGIN
  1249. IF isTensor THEN
  1250. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1251. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1252. ELSE
  1253. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1254. END
  1255. ELSE
  1256. result := indexListKind
  1257. END;
  1258. RETURN result
  1259. END IndexOperatorHash;
  1260. (** 2 to the power of exponent **)
  1261. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1262. VAR result, i: LONGINT;
  1263. BEGIN
  1264. result := 1;
  1265. FOR i := 1 TO exponent DO
  1266. result := result * 2;
  1267. END;
  1268. RETURN result
  1269. END TwoToThePowerOf;
  1270. (** check if a LEN operator has a correct signature. i.e.
  1271. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1272. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1273. **)
  1274. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1275. VAR
  1276. procedureType: SyntaxTree.ProcedureType;
  1277. returnedArrayType: SyntaxTree.MathArrayType;
  1278. result: BOOLEAN;
  1279. BEGIN
  1280. result := FALSE;
  1281. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1282. IF (procedureType.numberParameters = 0) THEN
  1283. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1284. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1285. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1286. IF returnedArrayType.form = SyntaxTree.Open THEN
  1287. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1288. result := TRUE
  1289. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1290. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1291. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1292. result := TRUE
  1293. END
  1294. END
  1295. END
  1296. END
  1297. END;
  1298. IF result THEN
  1299. (* export symbol automatically *)
  1300. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1301. ELSE
  1302. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1303. END;
  1304. RETURN result
  1305. END CheckLenOperator;
  1306. (** check if an index operator has a correct signature. i.e.
  1307. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1308. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1309. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1310. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1311. - determine if it is a read or write operator (existance of return type)
  1312. - check index parameters
  1313. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1314. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1315. [LONGINT] -> binary 0 -> 0
  1316. [RANGE] -> binary 1 -> 1
  1317. [LONGINT, LONGINT] -> binary 00 -> 0
  1318. [LONGINT, RANGE] -> binary 01 -> 1
  1319. [RANGE, LONGINT] -> binary 10 -> 2
  1320. [RANGE, RANGE] -> binary 11 -> 3
  1321. etc.
  1322. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1323. - for read operators, check if return type matches the type of data that is read
  1324. - for write operators, check if last parameter type matches the type of data that is written
  1325. **)
  1326. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1327. VAR
  1328. elementType, otherElementType, dataType: SyntaxTree.Type;
  1329. procedureType: SyntaxTree.ProcedureType;
  1330. mathArrayType: SyntaxTree.MathArrayType;
  1331. parameter: SyntaxTree.Parameter;
  1332. parameterCount, rangeCount, i: LONGINT;
  1333. hasTypeError: BOOLEAN;
  1334. BEGIN
  1335. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1336. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1337. (* determine if it is a read or write operator *)
  1338. isReadOperator := (procedureType.returnType # NIL);
  1339. IF isReadOperator THEN
  1340. indexListSize := parameterCount;
  1341. ELSE
  1342. indexListSize := parameterCount - 1;
  1343. END;
  1344. IF indexListSize < 1 THEN
  1345. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1346. RETURN FALSE
  1347. END;
  1348. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1349. (* general operator *)
  1350. isGeneralOperator := TRUE;
  1351. IF indexListSize > 1 THEN
  1352. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1353. RETURN FALSE
  1354. END;
  1355. (* ARRAY [*] OF RANGE*)
  1356. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1357. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1358. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1359. RETURN FALSE
  1360. END;
  1361. parameter := procedureType.firstParameter.nextParameter
  1362. ELSE
  1363. (* fixed-dim. operator *)
  1364. isGeneralOperator := FALSE;
  1365. (* check number of index parameters *)
  1366. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1367. (* for tensors, limited to a certain size *)
  1368. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1369. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1370. RETURN FALSE
  1371. END
  1372. ELSE
  1373. (* for non-tensors, depends on dimensionality *)
  1374. IF indexListSize # arrayStructure.Dimensionality() THEN
  1375. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1376. RETURN FALSE
  1377. END
  1378. END;
  1379. (* go through all index parameters
  1380. - count the number of ranges
  1381. - determine the index list kind number
  1382. *)
  1383. indexListKind := 0;
  1384. rangeCount := 0;
  1385. parameter := procedureType.firstParameter;
  1386. FOR i := 1 TO indexListSize DO
  1387. indexListKind := indexListKind * 2;
  1388. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1389. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1390. INC(indexListKind);
  1391. INC(rangeCount)
  1392. ELSE
  1393. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1394. RETURN FALSE
  1395. END;
  1396. parameter := parameter.nextParameter
  1397. END;
  1398. END;
  1399. (*
  1400. - for read operators: check type of last parameter
  1401. - for write operators: check return type
  1402. *)
  1403. IF isReadOperator THEN
  1404. dataType := procedureType.returnType (* the return type *)
  1405. ELSE
  1406. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1407. END;
  1408. elementType := arrayStructure.ElementType();
  1409. hasTypeError := FALSE;
  1410. IF isGeneralOperator THEN
  1411. (* ARRAY [?] OF <Element> *)
  1412. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1413. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1414. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1415. hasTypeError := TRUE
  1416. END
  1417. ELSE
  1418. hasTypeError := TRUE
  1419. END
  1420. ELSE
  1421. IF rangeCount = 0 THEN
  1422. (* <Element> *)
  1423. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1424. ELSE
  1425. (* ARRAY [*, *, ..., *] OF <Element> *)
  1426. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1427. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1428. IF mathArrayType.IsFullyDynamic() THEN
  1429. IF mathArrayType.Dimensionality() = rangeCount THEN
  1430. otherElementType := mathArrayType.ElementType();
  1431. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1432. ELSE
  1433. hasTypeError := TRUE
  1434. END
  1435. ELSE
  1436. hasTypeError := TRUE
  1437. END
  1438. ELSE
  1439. hasTypeError := TRUE
  1440. END
  1441. END
  1442. END;
  1443. IF hasTypeError THEN
  1444. IF isReadOperator THEN
  1445. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1446. ELSE
  1447. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1448. END;
  1449. RETURN FALSE
  1450. END;
  1451. (* export symbol automatically *)
  1452. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1453. RETURN TRUE
  1454. END CheckIndexOperator;
  1455. (** resolve all pending types (late resolving).
  1456. - type fixes are resolved at the end of the declaration phase
  1457. - type fixes may imply new type fixes that are also entered at the end of the list
  1458. **)
  1459. PROCEDURE FixTypes;
  1460. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1461. BEGIN
  1462. prevScope := currentScope;
  1463. p := typeFixes.Get(currentScope);
  1464. WHILE p # NIL DO
  1465. ASSERT(currentScope # NIL);
  1466. ASSERT(p IS SyntaxTree.Type);
  1467. IF p IS SyntaxTree.PointerType THEN
  1468. FixPointerType(p(SyntaxTree.PointerType))
  1469. ELSIF p IS SyntaxTree.ProcedureType THEN
  1470. FixProcedureType(p(SyntaxTree.ProcedureType))
  1471. ELSE
  1472. HALT(100);
  1473. END;
  1474. p := typeFixes.Get(currentScope);
  1475. END;
  1476. currentScope :=prevScope;
  1477. END FixTypes;
  1478. (**
  1479. resolve type x
  1480. - if x is nil then return nil
  1481. - if x cannot be resolved then the result is invalidType else the result is x
  1482. - the resolved type is entered into x.resolved
  1483. **)
  1484. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1485. VAR prev,resolved: SyntaxTree.Type;
  1486. BEGIN
  1487. prev := resolvedType;
  1488. resolvedType := SyntaxTree.invalidType;
  1489. IF x = NIL THEN resolvedType := NIL
  1490. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1491. END;
  1492. resolved := resolvedType;
  1493. resolvedType := prev;
  1494. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1495. RETURN resolved
  1496. END ResolveType;
  1497. (*** compatibility rules ***)
  1498. (**
  1499. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1500. **)
  1501. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1502. VAR result: SyntaxTree.Type;
  1503. BEGIN
  1504. result := SyntaxTree.invalidType;
  1505. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1506. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1507. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1508. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1509. ELSE result := type.resolved
  1510. END;
  1511. RETURN result
  1512. END RegularType;
  1513. (** returns signature compatibility of procedure types this and to
  1514. - if not compatible then error is reported
  1515. - compatibility means type equality
  1516. **)
  1517. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1518. VAR result: BOOLEAN;
  1519. BEGIN
  1520. result := SameType(to,this);
  1521. IF ~result THEN
  1522. Error(position,Diagnostics.Invalid,"signature incompatible");
  1523. IF VerboseErrorMessage THEN
  1524. Printout.Info("this",this);
  1525. Printout.Info("to",to);
  1526. END;
  1527. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1528. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1529. END;
  1530. RETURN result
  1531. END SignatureCompatible;
  1532. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1533. - for var parameters compatibility means same type except for
  1534. - formal is of open array of system byte
  1535. - formal is of record type
  1536. - formal is of open array type
  1537. - formal is of open math array type
  1538. - for value parameters compatibllity means assignment compatibility except for
  1539. - formal is of open array type
  1540. if compatible the return true else report error and return false
  1541. **)
  1542. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1543. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1544. BEGIN
  1545. formalType := RegularType(formal.position,formal.type);
  1546. actualType := RegularType(actual.position,actual.type);
  1547. error := FALSE;
  1548. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1549. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1550. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1551. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1552. END;
  1553. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1554. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1555. ELSIF ~IsVariable(actual) THEN
  1556. result := FALSE; error := TRUE;
  1557. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1558. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1559. ELSE
  1560. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1561. END;
  1562. IF VerboseErrorMessage THEN
  1563. Printout.Info("actual",actual);
  1564. Printout.Info("formal",formal);
  1565. END;
  1566. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1567. result := CompatibleTo(system,actualType,formalType);
  1568. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1569. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1570. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1571. IF IsArrayStructuredObjectType(actualType) THEN
  1572. actualType := MathArrayStructureOfType(actualType)
  1573. END;
  1574. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1575. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1576. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1577. END;
  1578. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1579. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1580. END;
  1581. ELSE
  1582. result := SameType(actualType,formalType)
  1583. END
  1584. ELSE
  1585. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1586. actualType := system.characterType;
  1587. END;
  1588. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1589. result := TRUE; (* special rule for WINAPI parameters *)
  1590. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1591. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1592. ELSE
  1593. result := CompatibleTo(system,actualType,formalType);
  1594. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1595. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1596. END;
  1597. END;
  1598. END;
  1599. IF ~result & ~error THEN
  1600. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1601. IF VerboseErrorMessage THEN
  1602. Printout.Info("actual",actual);
  1603. Printout.Info("formal",formal);
  1604. END;
  1605. END;
  1606. RETURN result
  1607. END ParameterCompatible;
  1608. (** check compatibility for expressions of the form left := right
  1609. - if compatible then return true else error report and return false
  1610. - check if left is variable
  1611. - check compatiblity
  1612. **)
  1613. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1614. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1615. BEGIN
  1616. result := FALSE;
  1617. leftType := RegularType(left.position,left.type);
  1618. rightType := RegularType(right.position,right.type);
  1619. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1620. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1621. END;
  1622. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1623. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1624. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1625. (* error already handled *)
  1626. result := TRUE;
  1627. ELSIF ~IsVariable(left) THEN
  1628. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1629. IF VerboseErrorMessage THEN
  1630. Printout.Info("left",left);
  1631. Printout.Info("right",right);
  1632. END;
  1633. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1634. result := TRUE;
  1635. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1636. result := TRUE
  1637. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1638. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1639. IF VerboseErrorMessage THEN
  1640. Printout.Info("left",left);
  1641. Printout.Info("right",right);
  1642. END;
  1643. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1644. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1645. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1646. ELSE
  1647. result := TRUE
  1648. END;
  1649. RETURN result
  1650. END AssignmentCompatible;
  1651. (*** values ***)
  1652. (** check and resolve integer value **)
  1653. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1654. VAR hugeint: HUGEINT;
  1655. BEGIN
  1656. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1657. value.SetType(Global.GetIntegerType(system,hugeint));
  1658. resolvedExpression := value
  1659. END VisitIntegerValue;
  1660. (** check and resolve real value **)
  1661. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1662. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1663. BEGIN
  1664. subtype := value(SyntaxTree.RealValue).subtype;
  1665. IF subtype = Scanner.Real THEN
  1666. type := system.realType
  1667. ELSIF subtype = Scanner.Longreal THEN
  1668. type := system.longrealType
  1669. ELSE
  1670. HALT(100)
  1671. END;
  1672. value.SetType(type);
  1673. resolvedExpression := value
  1674. END VisitRealValue;
  1675. (** check and resolve complex value **)
  1676. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1677. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1678. BEGIN
  1679. subtype := value(SyntaxTree.ComplexValue).subtype;
  1680. IF subtype = Scanner.Real THEN
  1681. type := system.complexType
  1682. ELSIF subtype = Scanner.Longreal THEN
  1683. type := system.longcomplexType
  1684. ELSE
  1685. HALT(100)
  1686. END;
  1687. value.SetType(type);
  1688. resolvedExpression := value
  1689. END VisitComplexValue;
  1690. (** check and resolve set value **)
  1691. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1692. BEGIN
  1693. value.SetType(system.setType);
  1694. resolvedExpression := value
  1695. END VisitSetValue;
  1696. (** check and resolve set value **)
  1697. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1698. BEGIN
  1699. value.SetType(SyntaxTree.invalidType);
  1700. resolvedExpression := value
  1701. END VisitMathArrayValue;
  1702. (** check and resolve boolean value **)
  1703. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1704. BEGIN
  1705. value.SetType(system.booleanType);
  1706. resolvedExpression := value
  1707. END VisitBooleanValue;
  1708. (** check and resolve string value **)
  1709. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1710. BEGIN
  1711. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1712. resolvedExpression := value
  1713. END VisitStringValue;
  1714. (** check and resolve character value **)
  1715. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1716. BEGIN
  1717. value.SetType(system.characterType);
  1718. resolvedExpression := value
  1719. END VisitCharacterValue;
  1720. (** check and resolve nil value **)
  1721. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1722. BEGIN
  1723. value.SetType(system.nilType);
  1724. resolvedExpression := value
  1725. END VisitNilValue;
  1726. (** check and resolve enumerator value **)
  1727. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1728. BEGIN
  1729. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1730. ASSERT(value.type # NIL);
  1731. resolvedExpression := value
  1732. END VisitEnumerationValue;
  1733. (*** expressions ***)
  1734. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1735. - check all elements on integer type
  1736. - if element range is constant, then check lower and upper bound
  1737. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1738. if an error occurs then report error and return invalidExpression
  1739. **)
  1740. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1741. VAR
  1742. i: LONGINT;
  1743. element: SyntaxTree.Expression;
  1744. constant: BOOLEAN;
  1745. elements: SyntaxTree.ExpressionList;
  1746. s: SET;
  1747. result: SyntaxTree.Expression;
  1748. value: SyntaxTree.Value;
  1749. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1750. VAR
  1751. left, right: SyntaxTree.Expression;
  1752. elementResult: SyntaxTree.Expression;
  1753. leftInteger, rightInteger, temp: LONGINT;
  1754. BEGIN
  1755. (* set context of range *)
  1756. IF element IS SyntaxTree.RangeExpression THEN
  1757. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1758. END;
  1759. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1760. IF elementResult = SyntaxTree.invalidExpression THEN
  1761. (* error already reported *)
  1762. constant := FALSE
  1763. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1764. (* the element is a range expression *)
  1765. (* extract left and right hand side of range *)
  1766. left := elementResult(SyntaxTree.RangeExpression).first;
  1767. right := elementResult(SyntaxTree.RangeExpression).last;
  1768. (* guaranteed by VisitRangeExpression: *)
  1769. ASSERT((left # NIL) & (right # NIL));
  1770. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1771. ELSE
  1772. (* the element is not a range expression *)
  1773. (* check type and add conversion if needed *)
  1774. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1775. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1776. ELSE
  1777. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1778. elementResult := SyntaxTree.invalidExpression;
  1779. constant := FALSE
  1780. END;
  1781. left := elementResult;
  1782. right := elementResult
  1783. END;
  1784. IF elementResult # SyntaxTree.invalidExpression THEN
  1785. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1786. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1787. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1788. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1789. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1790. END
  1791. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1792. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1793. ELSE
  1794. IF leftInteger > rightInteger THEN
  1795. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1796. END;
  1797. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1798. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1799. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1800. extends the range x..31 to x..63 !!!!!! *)
  1801. s := s + {leftInteger..rightInteger};
  1802. (* same but more explicit:
  1803. WHILE (leftInteger <= rightInteger) DO
  1804. INCL(s,leftInteger);
  1805. INC(leftInteger);
  1806. END;
  1807. *)
  1808. END;
  1809. ELSE
  1810. constant := FALSE;
  1811. END
  1812. END;
  1813. RETURN elementResult
  1814. END CheckElement;
  1815. BEGIN
  1816. result := set; constant := TRUE; s := {}; elements := set.elements;
  1817. IF elements # NIL THEN
  1818. FOR i := 0 TO elements.Length()-1 DO
  1819. element := elements.GetExpression(i);
  1820. element := CheckElement(element);
  1821. IF element = SyntaxTree.invalidExpression THEN
  1822. result := SyntaxTree.invalidExpression
  1823. END;
  1824. elements.SetExpression(i,element);
  1825. END;
  1826. END;
  1827. IF constant THEN
  1828. value := SyntaxTree.NewSetValue(set.position,s);
  1829. value.SetType(system.setType);
  1830. result.SetResolved(value);
  1831. END;
  1832. (* optimization possible
  1833. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1834. left this to the programmer...
  1835. *)
  1836. result.SetType(system.setType);
  1837. resolvedExpression := result;
  1838. END VisitSet;
  1839. (*
  1840. old variant: quite generic but needs better conversion handling, do this?
  1841. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1842. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1843. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1844. BEGIN
  1845. type := NIL;
  1846. numberElements := x.elements.Length();
  1847. FOR i := 0 TO numberElements-1 DO
  1848. expression := x.elements.GetExpression(i);
  1849. position := expression.position;
  1850. expression := ResolveExpression(x.elements.GetExpression(i));
  1851. x.elements.SetExpression(i,de);
  1852. IF type = NIL THEN
  1853. type := expression.type;
  1854. ELSIF CompatibleTo(system,expression.type,type) THEN
  1855. (* ok *)
  1856. ELSIF CompatibleTo(system,type,expression.type) THEN
  1857. type := expression.type
  1858. ELSE
  1859. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1860. type := SyntaxTree.invalidType;
  1861. END;
  1862. END;
  1863. isValue := TRUE;
  1864. FOR i := 0 TO numberElements-1 DO
  1865. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1866. x.elements.SetExpression(i,expression);
  1867. isValue := isValue & (expression.resolved # NIL);
  1868. END;
  1869. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1870. arrayType.SetArrayBase(type);
  1871. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1872. IF isValue THEN
  1873. value := SyntaxTree.NewMathArrayValue(position);
  1874. value.SetElements(x.elements);
  1875. x.SetResolved(value);
  1876. END;
  1877. x.SetType(arrayType);
  1878. resolvedExpression := x;
  1879. END VisitMathArrayExpression;
  1880. *)
  1881. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1882. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1883. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1884. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1885. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1886. BEGIN
  1887. numberElements := x.elements.Length();
  1888. FOR i := 0 TO numberElements-1 DO
  1889. expression := x.elements.GetExpression(i);
  1890. IF expression IS SyntaxTree.MathArrayExpression THEN
  1891. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1892. ELSE
  1893. position := expression.position;
  1894. expression := ResolveExpression(x.elements.GetExpression(i));
  1895. x.elements.SetExpression(i,expression);
  1896. IF type = NIL THEN
  1897. type := expression.type;
  1898. ELSIF CompatibleTo(system,expression.type,type) THEN
  1899. (* ok *)
  1900. ELSIF CompatibleTo(system,type,expression.type) THEN
  1901. type := expression.type
  1902. ELSE
  1903. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1904. type := SyntaxTree.invalidType;
  1905. END;
  1906. END;
  1907. END;
  1908. END RecursivelyFindType;
  1909. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1910. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1911. BEGIN
  1912. numberElements := x.elements.Length();
  1913. FOR i := 0 TO numberElements-1 DO
  1914. expression := x.elements.GetExpression(i);
  1915. IF expression IS SyntaxTree.MathArrayExpression THEN
  1916. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1917. ELSE
  1918. position := expression.position;
  1919. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1920. x.elements.SetExpression(i,expression);
  1921. isValue := isValue & (expression.resolved # NIL);
  1922. END;
  1923. END;
  1924. END RecursivelySetExpression;
  1925. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1926. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1927. arrayType: SyntaxTree.MathArrayType;
  1928. BEGIN
  1929. numberElements := x.elements.Length();
  1930. baseType := NIL;
  1931. gsize := 0;
  1932. FOR i := 0 TO numberElements-1 DO
  1933. expression := x.elements.GetExpression(i);
  1934. IF expression IS SyntaxTree.MathArrayExpression THEN
  1935. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1936. IF i=0 THEN
  1937. gsize := size;
  1938. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1939. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1940. ELSE expression.SetType(baseType)
  1941. END;
  1942. ELSIF baseType = NIL THEN baseType := type;
  1943. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1944. END;
  1945. END;
  1946. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1947. arrayType.SetArrayBase(baseType);
  1948. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1949. RETURN ResolveType(arrayType);
  1950. END RecursivelySetType;
  1951. BEGIN
  1952. type := NIL;
  1953. RecursivelyFindType(x);
  1954. isValue := TRUE;
  1955. RecursivelySetExpression(x);
  1956. arrayType := RecursivelySetType(x);
  1957. x.SetType(arrayType);
  1958. IF isValue THEN
  1959. value := SyntaxTree.NewMathArrayValue(x.position);
  1960. value.SetArray(x);
  1961. x.SetResolved(value);
  1962. value.SetType(arrayType);
  1963. END;
  1964. x.SetType(arrayType);
  1965. resolvedExpression := x;
  1966. END VisitMathArrayExpression;
  1967. (** check and resolve unary expression **)
  1968. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1969. VAR
  1970. left: SyntaxTree.Expression;
  1971. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1972. bool: BOOLEAN;
  1973. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1974. value: SyntaxTree.Value;
  1975. BEGIN
  1976. type := SyntaxTree.invalidType;
  1977. left := ResolveExpression(unaryExpression.left);
  1978. unaryExpression.SetLeft(left);
  1979. operator := unaryExpression.operator;
  1980. result := unaryExpression;
  1981. IF ~system.operatorDefined[operator] THEN
  1982. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1983. RETURN
  1984. ELSIF left.type = NIL THEN
  1985. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1986. resolvedExpression := SyntaxTree.invalidExpression;
  1987. RETURN
  1988. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1989. RETURN
  1990. END;
  1991. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1992. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1993. END;
  1994. IF operatorCall # NIL THEN
  1995. result := operatorCall;
  1996. type := operatorCall.type;
  1997. (* admissible operators
  1998. Minus number, set
  1999. Not boolean
  2000. *)
  2001. ELSE
  2002. CASE unaryExpression.operator OF
  2003. |Scanner.Minus:
  2004. IF IsIntegerType(left.type.resolved) THEN
  2005. IF left.resolved # NIL THEN
  2006. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2007. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2008. result.SetResolved(value);
  2009. type := Global.GetIntegerType(system,int);
  2010. value.SetType(type);
  2011. ELSE
  2012. type := left.type
  2013. END
  2014. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2015. IF IsRealValue(left,real) THEN
  2016. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2017. result.SetResolved(value);
  2018. type := left.type;
  2019. value.SetType(type);
  2020. ELSE
  2021. type := left.type;
  2022. END;
  2023. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2024. IF IsSetValue(left,set) THEN
  2025. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2026. result.SetResolved(value);
  2027. type := left.type;
  2028. value.SetType(type);
  2029. ELSE
  2030. type := left.type;
  2031. END;
  2032. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2033. IF IsComplexValue(left, real, imaginary) THEN
  2034. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2035. result.SetResolved(value);
  2036. type := left.type;
  2037. value.SetType(type);
  2038. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2039. ELSE
  2040. type := left.type;
  2041. END
  2042. ELSE
  2043. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2044. END;
  2045. |Scanner.Not:
  2046. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2047. IF IsBooleanValue(left,bool) THEN
  2048. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2049. result.SetResolved(value);
  2050. type := system.booleanType;
  2051. value.SetType(type);
  2052. ELSE
  2053. type := system.booleanType;
  2054. END;
  2055. ELSE
  2056. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2057. END;
  2058. |Scanner.Plus:
  2059. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2060. result := left; type := left.type;
  2061. ELSE
  2062. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2063. END;
  2064. (* ADDRESS OF *)
  2065. |Scanner.Address:
  2066. IF HasAddress(left) THEN
  2067. type := system.addressType;
  2068. ELSE
  2069. type := SyntaxTree.invalidType;
  2070. Error(left.position,Diagnostics.Invalid,"has no address");
  2071. Printout.Info("par", left);
  2072. END;
  2073. (* SIZE OF *)
  2074. |Scanner.Size:
  2075. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2076. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2077. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2078. value := SyntaxTree.NewIntegerValue(left.position, int);
  2079. result.SetResolved(value);
  2080. type := Global.GetIntegerType(system,int);
  2081. value.SetType(type)
  2082. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2083. ELSE
  2084. (* for variables, system sizeof could represent the physically occupied size
  2085. determined via the type descriptor, implement that ? *)
  2086. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2087. END
  2088. (* ALIAS OF *)
  2089. |Scanner.Alias:
  2090. type := left.type.resolved;
  2091. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2092. type := SyntaxTree.invalidType;
  2093. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2094. END;
  2095. ELSE
  2096. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2097. END;
  2098. END;
  2099. result.SetType(type);
  2100. resolvedExpression := result
  2101. END VisitUnaryExpression;
  2102. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2103. VAR
  2104. result: SyntaxTree.Expression;
  2105. array: SyntaxTree.MathArrayExpression;
  2106. value: SyntaxTree.MathArrayValue;
  2107. isValue: BOOLEAN;
  2108. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2109. BEGIN
  2110. type := type.resolved;
  2111. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2112. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2113. END;
  2114. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2115. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2116. END;
  2117. RETURN type
  2118. END BaseType;
  2119. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2120. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2121. BEGIN
  2122. numberElements := x.elements.Length();
  2123. FOR i := 0 TO numberElements-1 DO
  2124. expression := x.elements.GetExpression(i);
  2125. IF expression IS SyntaxTree.MathArrayExpression THEN
  2126. array := SyntaxTree.NewMathArrayExpression(position);
  2127. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2128. expression := array;
  2129. ELSE
  2130. position := expression.position;
  2131. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2132. isValue := isValue & (expression.resolved # NIL);
  2133. END;
  2134. to.elements.AddExpression(expression);
  2135. END;
  2136. END RecursivelyConvert;
  2137. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2138. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2139. arrayType: SyntaxTree.MathArrayType;
  2140. BEGIN
  2141. numberElements := x.elements.Length();
  2142. baseType := NIL;
  2143. gsize := 0;
  2144. FOR i := 0 TO numberElements-1 DO
  2145. expression := x.elements.GetExpression(i);
  2146. IF expression IS SyntaxTree.MathArrayExpression THEN
  2147. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2148. IF i=0 THEN
  2149. gsize := size;
  2150. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2151. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2152. ELSE expression.SetType(baseType)
  2153. END;
  2154. ELSIF baseType = NIL THEN baseType := type;
  2155. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2156. END;
  2157. END;
  2158. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2159. arrayType.SetArrayBase(baseType);
  2160. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2161. RETURN ResolveType(arrayType);
  2162. END RecursivelySetType;
  2163. BEGIN
  2164. result := SyntaxTree.invalidExpression;
  2165. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2166. result := expression (* do not convert *)
  2167. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2168. isValue := TRUE;
  2169. type := BaseType(type);
  2170. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2171. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2172. value := SyntaxTree.NewMathArrayValue(array.position);
  2173. value.SetArray(array);
  2174. value.SetType(RecursivelySetType(array));
  2175. result := value;
  2176. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2177. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2178. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2179. IF result = NIL THEN
  2180. result := SyntaxTree.invalidExpression;
  2181. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2182. IF VerboseErrorMessage THEN
  2183. Printout.Info("expression",expression);
  2184. Printout.Info("type",type);
  2185. END;
  2186. END;
  2187. END;
  2188. RETURN result
  2189. END MathArrayConversion;
  2190. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2191. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2192. BEGIN
  2193. result := expression; type := type.resolved;
  2194. IF (expression IS SyntaxTree.IntegerValue) THEN
  2195. int := expression(SyntaxTree.IntegerValue).hvalue;
  2196. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2197. int := Global.ConvertSigned(int,system.SizeOf(type));
  2198. result := SyntaxTree.NewIntegerValue(position,int);
  2199. result.SetType(type);
  2200. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2201. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2202. result := SyntaxTree.NewIntegerValue(position,int);
  2203. result.SetType(type);
  2204. ELSIF (type IS SyntaxTree.FloatType) THEN
  2205. result := SyntaxTree.NewRealValue(expression.position,int);
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2208. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.SetType) THEN
  2211. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2214. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2217. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2218. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2219. END;
  2220. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2221. result.SetType(type);
  2222. ELSIF (type IS SyntaxTree.PortType) THEN
  2223. result := ConvertValue(position, expression, system.integerType);
  2224. ELSE
  2225. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2226. result := SyntaxTree.invalidExpression;
  2227. IF VerboseErrorMessage THEN
  2228. Printout.Info("expression",expression);
  2229. Printout.Info("type",type);
  2230. END;
  2231. END;
  2232. ELSIF IsRealValue(expression,real) THEN
  2233. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2234. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2235. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2238. int := ENTIERH(real);
  2239. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2240. result.SetType(type);
  2241. ELSIF (type IS SyntaxTree.FloatType) THEN
  2242. result := SyntaxTree.NewRealValue(position,real);
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2245. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2246. result.SetType(type);
  2247. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2248. ELSIF (type IS SyntaxTree.PortType) THEN
  2249. result := ConvertValue(position, expression, system.integerType);
  2250. ELSE
  2251. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2252. result := SyntaxTree.invalidExpression;
  2253. END
  2254. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2255. IF (type IS SyntaxTree.ComplexType) THEN
  2256. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2257. result.SetType(type);
  2258. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2259. ELSE
  2260. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2261. result := SyntaxTree.invalidExpression;
  2262. END
  2263. ELSIF IsSetValue(expression,set) THEN
  2264. IF (type IS SyntaxTree.IntegerType) THEN
  2265. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2268. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.PortType) THEN
  2271. result := ConvertValue(position, expression, system.integerType);
  2272. ELSE
  2273. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2274. result := SyntaxTree.invalidExpression;
  2275. END;
  2276. ELSIF IsStringValue(expression,string) THEN
  2277. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2278. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2279. result.SetType(type);
  2280. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2281. ELSE
  2282. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2283. result := SyntaxTree.invalidExpression;
  2284. END;
  2285. ELSIF IsCharacterValue(expression,char) THEN
  2286. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2287. string[0] := char; string[1] := 0X;
  2288. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2289. result := SyntaxTree.NewStringValue(expression.position,string);
  2290. result.SetType(type);
  2291. ELSIF (type IS SyntaxTree.ByteType) THEN
  2292. (* do not simply set the new type as this could invalidate types of constants *)
  2293. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2294. result.SetType(type)
  2295. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2296. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2297. result.SetType(type);
  2298. ELSIF (type IS SyntaxTree.SetType) THEN
  2299. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2300. result.SetType(type);
  2301. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2302. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2303. result.SetType(type);
  2304. ELSIF (type IS SyntaxTree.PortType) THEN
  2305. result := ConvertValue(position, expression, system.integerType);
  2306. ELSE
  2307. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2308. result := SyntaxTree.invalidExpression;
  2309. END;
  2310. ELSIF expression IS SyntaxTree.NilValue THEN
  2311. IF type IS SyntaxTree.AddressType THEN
  2312. result := SyntaxTree.NewIntegerValue(position,0);
  2313. result.SetType(type);
  2314. ELSE
  2315. result := expression;
  2316. END;
  2317. (* nothing to be done *)
  2318. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2319. result := MathArrayConversion(position, expression,type);
  2320. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2321. int := expression(SyntaxTree.EnumerationValue).value;
  2322. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2323. int := Global.ConvertSigned(int,system.SizeOf(type));
  2324. result := SyntaxTree.NewIntegerValue(position,int);
  2325. result.SetType(type);
  2326. ELSE
  2327. result := expression;
  2328. END;
  2329. (* nothing to be done *)
  2330. ELSE
  2331. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2332. IF VerboseErrorMessage THEN
  2333. Printout.Info("expression",expression);
  2334. Printout.Info("type",type);
  2335. END;
  2336. result := SyntaxTree.invalidExpression;
  2337. END;
  2338. RETURN result
  2339. END ConvertValue;
  2340. (**
  2341. return a conversion of an expression to a given type
  2342. - if expression is already of same type then return expression
  2343. - if incompatible conversion then report error and return invalidExpression
  2344. **)
  2345. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2346. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2347. BEGIN
  2348. type := type.resolved;
  2349. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2350. result := expression;
  2351. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2352. ELSIF expression = NIL THEN (* NIL expression *)
  2353. ELSIF expression.type = NIL THEN
  2354. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2355. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2356. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2357. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2358. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2359. ELSIF expression.resolved # NIL THEN (* value *)
  2360. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2361. IF value IS SyntaxTree.Value THEN
  2362. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2363. result.SetResolved(value(SyntaxTree.Value));
  2364. result.SetType(value.type);
  2365. ELSE
  2366. result := value
  2367. END;
  2368. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2369. expressionList := SyntaxTree.NewExpressionList();
  2370. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2371. typeDeclaration.SetDeclaredType(type);
  2372. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2373. typeSymbol.SetType(typeDeclaration.type);
  2374. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2375. expressionList.AddExpression(expression);
  2376. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2377. result.SetType(type);
  2378. ELSIF IsArrayStructuredObjectType(type) THEN
  2379. (* no type can be converted to an array-structured object type *)
  2380. HALT(100)
  2381. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2382. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2383. result := MathArrayConversion(position, expression,type);
  2384. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2385. expression := ConvertToMathArray(expression);
  2386. type := MathArrayStructureOfType(type);
  2387. result := MathArrayConversion(position, expression, type)
  2388. ELSE
  2389. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2390. END;
  2391. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2392. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2393. OR ~(type IS SyntaxTree.ArrayType) THEN
  2394. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2395. END;
  2396. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2397. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2398. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2399. (*skip, no conversion*)
  2400. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2401. (* skip, no conversion *)
  2402. ELSE
  2403. ASSERT(~(type IS SyntaxTree.RangeType));
  2404. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2405. ASSERT(type # NIL);
  2406. END;
  2407. RETURN result
  2408. END NewConversion;
  2409. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2410. BEGIN
  2411. IF CompatibleTo(system,expression.type, type) THEN
  2412. RETURN NewConversion(position, expression, type, NIL);
  2413. ELSE
  2414. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2415. RETURN SyntaxTree.invalidExpression
  2416. END;
  2417. END CompatibleConversion;
  2418. (**
  2419. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2420. **)
  2421. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2422. VAR leftType,rightType: SyntaxTree.Type;
  2423. BEGIN
  2424. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2425. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2426. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2427. (* error already handled *)
  2428. ELSE
  2429. leftType := left.type.resolved; rightType := right.type.resolved;
  2430. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2431. right := NewConversion(right.position, right, leftType, NIL);
  2432. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2433. left := NewConversion(left.position,left,rightType,NIL);
  2434. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2435. right := NewConversion(right.position, right, leftType, NIL);
  2436. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2437. left := NewConversion(left.position,left,rightType,NIL);
  2438. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2439. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2440. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2441. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2442. ELSIF
  2443. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2444. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2445. (* must be the case LONGREAL / COMPLEX ) *)
  2446. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2447. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2448. ELSE
  2449. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2450. END;
  2451. END;
  2452. END ConvertOperands;
  2453. (** find and return best operator matching to parameter list (nil, if none)
  2454. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2455. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2456. **)
  2457. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2458. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2459. identifier: SyntaxTree.Identifier;
  2460. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2461. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2462. BEGIN
  2463. operator := scope.firstOperator;
  2464. WHILE(operator # NIL) DO
  2465. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2466. procedureType := operator.type(SyntaxTree.ProcedureType);
  2467. distance := Distance(system, procedureType,actualParameters);
  2468. IF (distance < Infinity) THEN
  2469. IF returnType # NIL THEN
  2470. IF procedureType.returnType = NIL THEN
  2471. distance := Infinity
  2472. ELSE
  2473. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2474. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2475. END;
  2476. END;
  2477. END;
  2478. (*
  2479. IF distance < Infinity THEN
  2480. TRACE(distance, operator);
  2481. Printout.Info("potential operator",operator);
  2482. ELSE
  2483. Printout.Info("operator not possible",operator);
  2484. END;
  2485. *)
  2486. IF distance < bestDistance THEN
  2487. bestDistance := distance;
  2488. bestOperator := operator;
  2489. END;
  2490. END;
  2491. operator := operator.nextOperator;
  2492. END;
  2493. (*
  2494. Printout.Info("taken operator",bestOperator);
  2495. *)
  2496. END FindInScope;
  2497. BEGIN
  2498. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2499. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2500. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2501. import := currentScope.ownerModule.moduleScope.firstImport;
  2502. WHILE (bestDistance > 0) & (import # NIL) DO
  2503. IF import.module # NIL THEN
  2504. identifier := Global.GetIdentifier(operator,import.module.case);
  2505. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2506. END;
  2507. import := import.nextImport;
  2508. END;
  2509. RETURN bestOperator
  2510. END FindOperator;
  2511. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2512. BEGIN
  2513. currentScope := scope;
  2514. END SetCurrentScope;
  2515. (**
  2516. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2517. - handle LEN and DIM operator for array-structured object types
  2518. - find operator, if found then
  2519. - if in other module then add import designator
  2520. - create symbol designator for operator
  2521. - if error then return invalidExpression, if no operator then return NIL
  2522. **)
  2523. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2524. VAR
  2525. operator: SyntaxTree.Operator;
  2526. import: SyntaxTree.Import;
  2527. expression, result: SyntaxTree.Expression;
  2528. designator: SyntaxTree.Designator;
  2529. actualParameters, tempList: SyntaxTree.ExpressionList;
  2530. recordType: SyntaxTree.RecordType;
  2531. castReturnType : SyntaxTree.MathArrayType;
  2532. BEGIN
  2533. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2534. result := SyntaxTree.invalidExpression
  2535. ELSIF leftExpression = NIL THEN
  2536. result := NIL
  2537. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2538. (* LEN or DIM operator on array-structured object type *)
  2539. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2540. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2541. IF recordType.arrayAccessOperators.len = NIL THEN
  2542. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2543. result := SyntaxTree.invalidExpression
  2544. ELSE
  2545. ASSERT(leftExpression IS SyntaxTree.Designator);
  2546. designator := leftExpression(SyntaxTree.Designator);
  2547. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2548. ASSERT(expression IS SyntaxTree.Designator);
  2549. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2550. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2551. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2552. result := designator
  2553. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2554. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2555. tempList := SyntaxTree.NewExpressionList();
  2556. tempList.AddExpression(rightExpression);
  2557. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2558. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2559. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2560. tempList := SyntaxTree.NewExpressionList();
  2561. tempList.AddExpression(designator);
  2562. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2563. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2564. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2565. END
  2566. END;
  2567. ELSE
  2568. (* import OCArrayBase if needed *)
  2569. IF ~arrayBaseImported THEN
  2570. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2571. (* operators on complex numbers *)
  2572. ImportModule(Global.ArrayBaseName,position);
  2573. arrayBaseImported := TRUE;
  2574. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2575. IF op = Global.Dim THEN
  2576. (* not existing in OCArrayBase *)
  2577. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2578. (* not existing in OCArrayBase *)
  2579. ELSE
  2580. ImportModule(Global.ArrayBaseName,position);
  2581. arrayBaseImported := TRUE;
  2582. END
  2583. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2584. ImportModule(Global.ArrayBaseName,position);
  2585. arrayBaseImported := TRUE
  2586. END;
  2587. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2588. (* LEN(RANGE) *)
  2589. ImportModule(Global.ArrayBaseName,position);
  2590. arrayBaseImported := TRUE;
  2591. END;
  2592. END;
  2593. actualParameters := SyntaxTree.NewExpressionList();
  2594. actualParameters.AddExpression(leftExpression);
  2595. IF rightExpression # NIL THEN
  2596. actualParameters.AddExpression(rightExpression)
  2597. END;
  2598. operator := FindOperator(system,op,actualParameters,resultType);
  2599. IF operator # NIL THEN
  2600. designator := NIL;
  2601. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2602. import := currentScope.ownerModule.moduleScope.firstImport;
  2603. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2604. import := import.nextImport;
  2605. END;
  2606. expression := NewSymbolDesignator(position,NIL,import);
  2607. designator := expression(SyntaxTree.Designator);
  2608. END;
  2609. expression := NewSymbolDesignator(position,designator,operator);
  2610. designator := expression(SyntaxTree.Designator);
  2611. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2612. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2613. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2614. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2615. result.SetType(castReturnType);
  2616. END;
  2617. ELSE
  2618. result := NIL;
  2619. END;
  2620. END;
  2621. RETURN result
  2622. END NewOperatorCall;
  2623. (** check and resolve binary expression **)
  2624. (*! clean up *)
  2625. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2626. VAR left,right,result: SyntaxTree.Expression;
  2627. leftType, rightType: SyntaxTree.Type;
  2628. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2629. cl,cr: CHAR;
  2630. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2631. type: SyntaxTree.Type;
  2632. value: SyntaxTree.Value;
  2633. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2634. integerConstantFolding: BOOLEAN;
  2635. list: SyntaxTree.ExpressionList;
  2636. PROCEDURE NewBool(v: BOOLEAN);
  2637. BEGIN
  2638. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2639. value.SetType(system.booleanType);
  2640. result.SetResolved(value);
  2641. type := system.booleanType
  2642. END NewBool;
  2643. PROCEDURE NewSet(v: SET);
  2644. BEGIN
  2645. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2646. value.SetType(system.setType);
  2647. result.SetResolved(value);
  2648. type := system.setType;
  2649. END NewSet;
  2650. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2651. BEGIN
  2652. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2653. (* type cast to "larger" type only if the value is still in the range *)
  2654. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2655. value.SetType(t);
  2656. END;
  2657. result.SetResolved(value);
  2658. type := value.type;
  2659. END NewInteger;
  2660. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2661. BEGIN
  2662. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2663. value.SetType(t);
  2664. result.SetResolved(value);
  2665. type := t;
  2666. END NewReal;
  2667. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2668. BEGIN
  2669. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2670. value.SetType(t);
  2671. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2672. result.SetResolved(value);
  2673. type := t;
  2674. END NewComplex;
  2675. BEGIN
  2676. type := SyntaxTree.invalidType;
  2677. left := ResolveExpression(binaryExpression.left);
  2678. right := ResolveExpression(binaryExpression.right);
  2679. binaryExpression.SetLeft(left);
  2680. binaryExpression.SetRight(right);
  2681. result := binaryExpression;
  2682. operator := binaryExpression.operator;
  2683. IF ~system.operatorDefined[operator] THEN
  2684. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2685. result := SyntaxTree.invalidExpression;
  2686. RETURN
  2687. END;
  2688. IF left.type = NIL THEN
  2689. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2690. result := SyntaxTree.invalidExpression;
  2691. RETURN;
  2692. END;
  2693. IF right.type = NIL THEN
  2694. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2695. result := SyntaxTree.invalidExpression;
  2696. RETURN;
  2697. END;
  2698. leftType := left.type.resolved; rightType := right.type.resolved;
  2699. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2700. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2701. END;
  2702. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2703. list := SyntaxTree.NewExpressionList();
  2704. list.AddExpression(right);
  2705. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2706. END;
  2707. IF operatorCall # NIL THEN
  2708. result := operatorCall;
  2709. type := operatorCall.type;
  2710. (* admissible operators:
  2711. Times, Plus, Minus numeric numeric numeric
  2712. set set set
  2713. Slash numeric numeric real /complex
  2714. set set set
  2715. Div , Mod integer integer integer
  2716. And, Or bool bool bool
  2717. Equal, Unequal basic basic bool
  2718. pointer pointer bool
  2719. object object bool
  2720. record record bool
  2721. string string bool
  2722. enumerator enumerator bool
  2723. Less, LessEqual,
  2724. Greater, GreaterEqual integer/real integer/real bool
  2725. enumerator enumerator bool
  2726. In integer set bool
  2727. Is pointer type bool
  2728. object type bool
  2729. record type bool
  2730. Upto: special abbreviation for a..b
  2731. *)
  2732. ELSIF (left.type = NIL) THEN
  2733. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2734. D.Str("nil type in "); D.Type(left); D.Ln;
  2735. result := SyntaxTree.invalidExpression;
  2736. ELSIF (right.type = NIL) THEN
  2737. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2738. result := SyntaxTree.invalidExpression;
  2739. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2740. result := SyntaxTree.invalidExpression;
  2741. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2742. HALT(100);
  2743. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2744. type := system.booleanType;
  2745. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2746. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2747. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2748. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2749. IF VerboseErrorMessage THEN
  2750. Printout.Info("left",left);
  2751. Printout.Info("right",right);
  2752. END;
  2753. ELSIF IsUnsafePointer(left.type) THEN
  2754. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2755. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2756. NewBool(TRUE)
  2757. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2758. NewBool(TRUE);
  2759. ELSIF IsUnextensibleRecord(left) THEN
  2760. NewBool(FALSE)
  2761. END
  2762. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2763. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2764. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2765. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2766. ELSIF operator = Scanner.In THEN (* left IN right *)
  2767. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2768. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2769. NewBool(il IN sr);
  2770. ELSE
  2771. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2772. left := NewConversion(left.position, left, system.longintType,NIL);
  2773. binaryExpression.SetLeft(left)
  2774. END;
  2775. type := system.booleanType;
  2776. END
  2777. ELSE
  2778. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2779. END
  2780. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2781. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2782. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2783. END;
  2784. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2785. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2786. END
  2787. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2788. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2789. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2790. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2791. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2792. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2793. ConvertOperands(left, right);
  2794. binaryExpression.SetLeft(left);
  2795. binaryExpression.SetRight(right);
  2796. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2797. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2798. END;
  2799. type := system.booleanType;
  2800. ELSE
  2801. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2802. END
  2803. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2804. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2805. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2806. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2807. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2808. CASE operator OF
  2809. |Scanner.Equal: NewBool(strl^=strr^);
  2810. |Scanner.Unequal:NewBool(strl^#strr^);
  2811. |Scanner.Less: NewBool(strl^<strr^);
  2812. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2813. |Scanner.Greater: NewBool(strl^>strr^);
  2814. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2815. ELSE
  2816. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2817. END;
  2818. END;
  2819. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2820. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2821. type := system.booleanType
  2822. ELSE
  2823. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2824. END;
  2825. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2826. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2827. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2828. type := system.booleanType;
  2829. ELSE
  2830. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2831. END
  2832. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2833. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2834. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2835. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2836. type := system.booleanType
  2837. ELSE
  2838. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2839. END;
  2840. ELSE
  2841. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2842. END;
  2843. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2844. type := system.booleanType;
  2845. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2846. type := system.booleanType;
  2847. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2848. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2849. THEN
  2850. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2851. IF (leftType # rightType) THEN
  2852. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2853. ConvertOperands(left,right); (* operands must be of the same type here *)
  2854. END;
  2855. binaryExpression.SetLeft(left);
  2856. binaryExpression.SetRight(right);
  2857. leftType := left.type.resolved;
  2858. rightType := right.type.resolved;
  2859. END;
  2860. type := leftType;
  2861. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2862. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2863. IF VerboseErrorMessage THEN
  2864. Printout.Info("left",left);
  2865. Printout.Info("right",right);
  2866. END;
  2867. ELSIF IsIntegerType(leftType) THEN
  2868. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2869. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2870. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2871. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2872. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2873. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2874. END;
  2875. END;
  2876. (* constant folding *)
  2877. (* bootstrap64
  2878. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2879. CASE operator OF
  2880. |Scanner.Plus: NewInteger(il+ir,left.type);
  2881. |Scanner.Minus: NewInteger(il-ir,left.type);
  2882. |Scanner.Times: NewInteger(il*ir,left.type);
  2883. |Scanner.Slash:
  2884. IF ir # 0 THEN
  2885. NewReal(il/ir, system.realType);
  2886. END;
  2887. |Scanner.Mod:
  2888. IF ir > 0 THEN
  2889. NewInteger(il MOD ir,left.type);
  2890. END;
  2891. |Scanner.Div:
  2892. IF ir > 0 THEN
  2893. NewInteger(il DIV ir,left.type);
  2894. END;
  2895. |Scanner.Equal: NewBool(il=ir);
  2896. |Scanner.Unequal:NewBool(il#ir);
  2897. |Scanner.Less: NewBool(il<ir);
  2898. |Scanner.LessEqual: NewBool(il<=ir);
  2899. |Scanner.Greater: NewBool(il>ir);
  2900. |Scanner.GreaterEqual: NewBool(il>=ir);
  2901. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2902. END;
  2903. ELS*)
  2904. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2905. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2906. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2907. CASE operator OF
  2908. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2909. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2910. |Scanner.Times: NewInteger(hl*hr,left.type);
  2911. |Scanner.Slash:
  2912. IF hr = 0 THEN
  2913. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2914. ELSE
  2915. IF type.sizeInBits = 64 THEN
  2916. NewReal(hl/hr,system.longrealType);
  2917. ELSE
  2918. NewReal(hl/hr,system.realType)
  2919. END
  2920. END;
  2921. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2922. |Scanner.Mod:
  2923. IF hr = 0 THEN
  2924. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2925. ELSE
  2926. NewInteger(hl MOD hr, left.type);
  2927. (* bootstrap64
  2928. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2929. *)
  2930. END;
  2931. |Scanner.Div:
  2932. IF hr = 0 THEN
  2933. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2934. ELSE
  2935. NewInteger(hl DIV hr, left.type);
  2936. (* bootstrap64
  2937. NewInteger(Machine.DivH(hl,hr),left.type);
  2938. *)
  2939. END;
  2940. (* *)
  2941. |Scanner.Equal: NewBool(hl=hr);
  2942. |Scanner.Unequal: NewBool(hl#hr);
  2943. |Scanner.Less: NewBool(hl<hr);
  2944. |Scanner.LessEqual: NewBool(hl<=hr);
  2945. |Scanner.Greater: NewBool(hl>hr);
  2946. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2947. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2948. END;
  2949. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2950. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2951. type := left.type
  2952. ELSIF (operator = Scanner.Slash) THEN
  2953. left := NewConversion(left.position,left,system.realType,NIL);
  2954. right := NewConversion(right.position,right,system.realType,NIL);
  2955. binaryExpression.SetLeft(left);
  2956. binaryExpression.SetRight(right);
  2957. type := system.realType
  2958. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2959. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2960. type := system.booleanType
  2961. ELSE
  2962. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2963. END;
  2964. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2965. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2966. CASE operator OF
  2967. |Scanner.Plus: NewReal(rl+rr,leftType);
  2968. |Scanner.Minus: NewReal(rl-rr,leftType);
  2969. |Scanner.Times:NewReal(rl*rr,leftType);
  2970. |Scanner.Slash:
  2971. IF rr = 0 THEN
  2972. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2973. ELSE
  2974. NewReal(rl/rr,leftType);
  2975. END
  2976. |Scanner.Equal: NewBool(rl=rr);
  2977. |Scanner.Unequal: NewBool(rl#rr);
  2978. |Scanner.Less: NewBool(rl<rr);
  2979. |Scanner.LessEqual: NewBool(rl<=rr);
  2980. |Scanner.Greater: NewBool(rl>rr);
  2981. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2982. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2983. END;
  2984. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2985. type := left.type
  2986. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2987. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2988. type := system.booleanType
  2989. ELSE
  2990. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2991. IF VerboseErrorMessage THEN
  2992. Printout.Info("left",left);
  2993. Printout.Info("right",right);
  2994. END;
  2995. END;
  2996. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2997. CASE operator OF
  2998. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2999. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3000. ELSE
  3001. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3002. IF VerboseErrorMessage THEN
  3003. Printout.Info("left", left);
  3004. Printout.Info("right", right)
  3005. END;
  3006. END;
  3007. IF ~error THEN
  3008. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3009. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3010. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3011. (* do constant folding *)
  3012. CASE operator OF
  3013. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3014. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3015. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3016. |Scanner.Slash:
  3017. divisor := c * c + d * d;
  3018. ASSERT(divisor # 0);
  3019. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3020. |Scanner.Equal: NewBool((a = c) & (b = d))
  3021. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3022. END
  3023. END
  3024. END
  3025. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3026. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3027. CASE operator OF
  3028. |Scanner.And: NewBool(bl & br);
  3029. |Scanner.Or: NewBool(bl OR br);
  3030. |Scanner.Equal: NewBool(bl = br);
  3031. |Scanner.Unequal: NewBool(bl # br);
  3032. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3033. END;
  3034. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3035. type := system.booleanType
  3036. ELSE
  3037. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3038. END;
  3039. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3040. (* constant folding *)
  3041. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3042. IF operator = Scanner.Equal THEN
  3043. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3044. ELSIF operator = Scanner.Unequal THEN
  3045. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3046. END;
  3047. END;
  3048. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3049. type := system.booleanType;
  3050. ELSE
  3051. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3052. END;
  3053. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3054. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3055. CASE operator OF
  3056. |Scanner.Plus: NewSet(sl + sr);
  3057. |Scanner.Minus: NewSet(sl - sr);
  3058. |Scanner.Times: NewSet(sl * sr);
  3059. |Scanner.Slash: NewSet(sl / sr);
  3060. |Scanner.Equal: NewBool(sl=sr);
  3061. |Scanner.Unequal: NewBool(sl#sr);
  3062. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3063. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3064. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3065. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3066. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3067. END;
  3068. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3069. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3070. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3071. THEN
  3072. type := system.booleanType
  3073. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3074. type := left.type
  3075. ELSE
  3076. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3077. END;
  3078. ELSIF IsCharacterType(left.type) THEN
  3079. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3080. CASE operator OF
  3081. |Scanner.Equal: NewBool(cl=cr);
  3082. |Scanner.Unequal: NewBool(cl#cr);
  3083. |Scanner.Less: NewBool(cl<cr);
  3084. |Scanner.LessEqual: NewBool(cl<=cr);
  3085. |Scanner.Greater: NewBool(cl>cr);
  3086. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3087. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3088. END;
  3089. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3090. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3091. type := system.booleanType
  3092. ELSE
  3093. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3094. END;
  3095. ELSE
  3096. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3097. END;
  3098. ELSE
  3099. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3100. END;
  3101. IF type = SyntaxTree.invalidType THEN
  3102. result := SyntaxTree.invalidExpression
  3103. ELSE
  3104. result.SetType(type)
  3105. END;
  3106. resolvedExpression := result
  3107. END VisitBinaryExpression;
  3108. (** resolve a range expression of the from <<first .. last BY step>>
  3109. - depending on the context different things are checked:
  3110. ArrayIndex:
  3111. - components must be integers
  3112. - replace missing lower bound with 0
  3113. - replace missing upper bound with MAX(LONGINT)
  3114. - replace missing step size with 1
  3115. SetElement:
  3116. - components must be integers
  3117. - replace missing lower bound with 0
  3118. - replace missing upper bound with MAX(SET)
  3119. - must not have step size
  3120. CaseGuard:
  3121. - components must be constant
  3122. - components must be integers or characters
  3123. - must have lower and upper bound present
  3124. - components are made compatible
  3125. - must not have step size
  3126. - if error: return invalidExpression
  3127. **)
  3128. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3129. VAR
  3130. hasError: BOOLEAN;
  3131. first, last, step: SyntaxTree.Expression;
  3132. BEGIN
  3133. hasError := FALSE;
  3134. first := x.first;
  3135. last := x.last;
  3136. step := x.step;
  3137. (* check lower bound *)
  3138. IF x.context = SyntaxTree.CaseGuard THEN
  3139. IF first = NIL THEN
  3140. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3141. hasError := TRUE
  3142. ELSE
  3143. first := ResolveExpression(first);
  3144. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3145. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3146. hasError := TRUE
  3147. ELSE
  3148. IF first IS SyntaxTree.StringValue THEN
  3149. (* add conversion from string to character *)
  3150. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3151. END
  3152. END;
  3153. (* check if expression is constant *)
  3154. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3155. (* error already reported *)
  3156. hasError := TRUE
  3157. END
  3158. END
  3159. ELSE (* ArrayIndex, SetElement *)
  3160. IF first = NIL THEN
  3161. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3162. END;
  3163. first := ResolveExpression(first);
  3164. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3165. first := NewConversion(first.position, first, system.longintType, NIL)
  3166. ELSE
  3167. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3168. hasError := TRUE
  3169. END
  3170. END;
  3171. (* check upper bound *)
  3172. IF x.context = SyntaxTree.CaseGuard THEN
  3173. IF last = NIL THEN
  3174. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3175. hasError := TRUE
  3176. ELSE
  3177. last := ResolveExpression(last);
  3178. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3179. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3180. hasError := TRUE
  3181. ELSE
  3182. IF last IS SyntaxTree.StringValue THEN
  3183. (* add conversion from string to character *)
  3184. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3185. END
  3186. END;
  3187. (* check if expression is constant *)
  3188. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3189. (* error already reported *)
  3190. hasError := TRUE
  3191. ELSE
  3192. (* try to make lower and upper bound compatible *)
  3193. ConvertOperands(first, last);
  3194. IF first.type.resolved # last.type.resolved THEN
  3195. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3196. hasError := TRUE
  3197. END
  3198. END
  3199. END
  3200. ELSE (* ArrayIndex, SetElement *)
  3201. IF last = NIL THEN
  3202. IF x.context = SyntaxTree.ArrayIndex THEN
  3203. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3204. ELSE
  3205. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3206. END
  3207. END;
  3208. last := ResolveExpression(last);
  3209. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3210. last := NewConversion(last.position, last, system.longintType, NIL)
  3211. ELSE
  3212. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3213. hasError := TRUE
  3214. END
  3215. END;
  3216. (* check step size *)
  3217. IF x.context = SyntaxTree.ArrayIndex THEN
  3218. IF step = NIL THEN
  3219. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3220. END;
  3221. step := ResolveExpression(step);
  3222. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3223. step := NewConversion(step.position, step, system.longintType, NIL)
  3224. ELSE
  3225. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3226. hasError := TRUE
  3227. END
  3228. ELSE (* SetElement, CaseGuard *)
  3229. IF step # NIL THEN
  3230. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3231. hasError := TRUE
  3232. END
  3233. END;
  3234. IF hasError THEN
  3235. resolvedExpression := SyntaxTree.invalidExpression
  3236. ELSE
  3237. x.SetFirst(first);
  3238. x.SetLast(last);
  3239. x.SetStep(step);
  3240. x.SetType(system.rangeType);
  3241. resolvedExpression := x;
  3242. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3243. END
  3244. END VisitRangeExpression;
  3245. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3246. BEGIN
  3247. x.SetType(NIL);
  3248. resolvedExpression := x;
  3249. END VisitTensorRangeExpression;
  3250. (** resolve the expression d and return result as designator
  3251. - resolve expression
  3252. - if expression is a designator then return designator else error message and return invalidDesignator
  3253. **)
  3254. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3255. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3256. BEGIN
  3257. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3258. resolved := ResolveExpression(d);
  3259. IF resolved = SyntaxTree.invalidExpression THEN
  3260. (* error should already have been reported *)
  3261. result := SyntaxTree.invalidDesignator;
  3262. ELSIF resolved IS SyntaxTree.Designator THEN
  3263. result := resolved(SyntaxTree.Designator);
  3264. ELSE
  3265. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3266. result := SyntaxTree.invalidDesignator;
  3267. END;
  3268. (* result.type might be nil. *)
  3269. RETURN result
  3270. END ResolveDesignator;
  3271. (**
  3272. symbol designator generated in this module
  3273. nothing to be resolved
  3274. **)
  3275. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3276. BEGIN
  3277. resolvedExpression := x;
  3278. END VisitSymbolDesignator;
  3279. (**
  3280. self designator generated in this module
  3281. nothing to be resolved
  3282. **)
  3283. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3284. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3285. BEGIN
  3286. (* check if in record scope *)
  3287. scope := currentScope;
  3288. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3289. scope := scope.outerScope;
  3290. END;
  3291. IF scope = NIL THEN (* in module scope *)
  3292. x.SetType(system.anyType);
  3293. ELSIF scope IS SyntaxTree.CellScope THEN
  3294. cell := scope(SyntaxTree.CellScope).ownerCell;
  3295. x.SetType(cell);
  3296. ELSE (* in record scope *)
  3297. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3298. IF (record # NIL) & (record.pointerType # NIL) THEN
  3299. type := ResolveType(record.pointerType);
  3300. x.SetType(type);
  3301. ELSE
  3302. x.SetType(record);
  3303. END;
  3304. END;
  3305. resolvedExpression := x;
  3306. END VisitSelfDesignator;
  3307. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3308. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3309. BEGIN
  3310. scope := currentScope;
  3311. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3312. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3313. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3314. returnType := procedureType.returnType;
  3315. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3316. THEN
  3317. x.SetType(returnType);
  3318. ELSE
  3319. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3320. x.SetType(SyntaxTree.invalidType);
  3321. END;
  3322. ELSE
  3323. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3324. x.SetType(SyntaxTree.invalidType);
  3325. END;
  3326. x.SetAssignable(TRUE);
  3327. resolvedExpression := x;
  3328. END VisitResultDesignator;
  3329. (**
  3330. return symbol designator as an expression
  3331. - if symbol is a constant then return the constant value expression
  3332. - else
  3333. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3334. - if symbol is a guarded variable then return a TypeGuardDesignator
  3335. - else return a symbol designator
  3336. **)
  3337. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3338. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3339. guardType: SyntaxTree.Type;
  3340. BEGIN
  3341. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3342. result := SyntaxTree.invalidExpression;
  3343. ASSERT(symbol # NIL);
  3344. (*
  3345. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3346. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3347. Error(position,Diagnostics.Invalid,"type not allowed here");
  3348. ELS *)
  3349. (* not needed any more as values are stored in the expression
  3350. IF symbol IS SyntaxTree.Constant THEN
  3351. result := symbol(SyntaxTree.Constant).value
  3352. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3353. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3354. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3355. ELSE
  3356. result := symbol(SyntaxTree.Constant).value
  3357. END;
  3358. ELSE
  3359. *)
  3360. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3361. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3362. THEN
  3363. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3364. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3365. left := NewDereferenceDesignator(position,left);
  3366. left.SetHidden(TRUE);
  3367. END;
  3368. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3369. scope := currentScope;
  3370. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3371. scope := scope.outerScope;
  3372. END;
  3373. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3374. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3375. END;
  3376. END;
  3377. 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);
  3378. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3379. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3380. ELSE
  3381. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3382. END;
  3383. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3384. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3385. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3386. result.SetType(symbol.type);
  3387. result.SetAssignable(assignable);
  3388. symbol.MarkUsed;
  3389. IF symbol IS SyntaxTree.Constant THEN
  3390. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3391. END;
  3392. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3393. variableAccessed := TRUE
  3394. END;
  3395. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3396. IF GetGuard(symbol,guardType) THEN
  3397. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3398. END;
  3399. END;
  3400. ASSERT(result.type # NIL);
  3401. RETURN result
  3402. END NewSymbolDesignator;
  3403. (** check and resolve an identifier designator "identifier"
  3404. - if identifier = self then return SelfDesignator
  3405. - else find symbol in current scope
  3406. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3407. **)
  3408. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3409. VAR symbol: SyntaxTree.Symbol;
  3410. BEGIN
  3411. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3412. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3413. IF symbol # NIL THEN
  3414. ResolveSymbol(symbol);
  3415. ASSERT(symbol.type # NIL);
  3416. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3417. ELSE
  3418. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3419. IF VerboseErrorMessage THEN
  3420. Printout.Info("undeclared identifier designator",identifierDesignator);
  3421. END;
  3422. resolvedExpression := SyntaxTree.invalidDesignator;
  3423. END;
  3424. END VisitIdentifierDesignator;
  3425. (** check and resolve a selector designator of the form left.designator
  3426. - if left is a pointer type then do auto dereferenciation
  3427. - left denotes a search scope:
  3428. - if left type is import type then set search scope to respective module
  3429. - if left type is enumeration type then set search scope to respective enumeration scope
  3430. - elsif left type is record type then set search scope to record scope
  3431. - search symbol in computed scope
  3432. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3433. **)
  3434. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3435. VAR
  3436. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3437. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3438. BEGIN
  3439. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3440. left := ResolveDesignator(selectorDesignator.left);
  3441. result := SyntaxTree.invalidDesignator;
  3442. IF left # NIL THEN
  3443. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3444. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3445. END;
  3446. scope := NIL;
  3447. IF left.type = NIL THEN
  3448. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3449. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3450. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3451. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3452. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3453. module := symbol(SyntaxTree.Import).module;
  3454. IF module # NIL THEN
  3455. scope := module.moduleScope
  3456. ELSE
  3457. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3458. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3459. END;
  3460. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3461. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3462. ASSERT(scope # NIL)
  3463. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3464. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3465. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3466. IF type IS SyntaxTree.EnumerationType THEN
  3467. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3468. ELSE
  3469. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3470. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3471. END;
  3472. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3473. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3474. ELSE
  3475. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3476. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3477. END;
  3478. symbol := NIL;
  3479. IF scope # NIL THEN
  3480. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3481. IF symbol # NIL THEN
  3482. ResolveSymbol(symbol);
  3483. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3484. symbol.MarkUsed
  3485. ELSE
  3486. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3487. IF VerboseErrorMessage THEN
  3488. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3489. Printout.Info("scope", scope);
  3490. Printout.Info("left", left);
  3491. Printout.Info("undeclared identifier",selectorDesignator);
  3492. Printout.Info("left resolved designator",left);
  3493. END
  3494. END;
  3495. END;
  3496. END;
  3497. resolvedExpression := result;
  3498. END VisitSelectorDesignator;
  3499. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3500. VAR len,idx: LONGINT;
  3501. BEGIN
  3502. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3503. IF idx < 0 THEN
  3504. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3505. ELSE
  3506. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3507. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3508. END;
  3509. END;
  3510. END;
  3511. END IndexCheck;
  3512. (*
  3513. - if index designator has not type, use newBaseType as its type
  3514. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3515. - special rule: if static array of dynamic array occurs, make it all dynamic
  3516. index designator type: new base type: new index designator type:
  3517. NIL z z
  3518. ARRAY [x, y] z ARRAY [x, y] OF z
  3519. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3520. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3521. *)
  3522. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3523. VAR
  3524. mathArrayType: SyntaxTree.MathArrayType;
  3525. makeDynamic: BOOLEAN;
  3526. BEGIN
  3527. IF indexDesignator.type = NIL THEN
  3528. indexDesignator.SetType(newBaseType)
  3529. ELSE
  3530. (* index designator must be a of math array type *)
  3531. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3532. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3533. (* determine if all arrays have to be made dynamic *)
  3534. makeDynamic :=
  3535. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3536. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3537. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3538. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3539. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3540. END;
  3541. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3542. mathArrayType.SetArrayBase(newBaseType)
  3543. END
  3544. END SetIndexBaseType;
  3545. (** check and append index list element to index designator of math array
  3546. - check validity of single index or array range
  3547. - compute new type
  3548. - if range then create new array type (calculate length of resulting array)
  3549. - otherwise take sourceArray.arrayBase as new type
  3550. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3551. **)
  3552. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3553. VAR
  3554. targetArray: SyntaxTree.MathArrayType;
  3555. first, last, step: SyntaxTree.Expression;
  3556. firstValue, lastValue, stepValue, length: LONGINT;
  3557. rangeExpression: SyntaxTree.RangeExpression;
  3558. BEGIN
  3559. IF indexListItem.type = SyntaxTree.invalidType THEN
  3560. (* error already handled *)
  3561. indexDesignator.parameters.AddExpression(indexListItem)
  3562. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3563. indexDesignator.HasRange;
  3564. indexDesignator.HasTensorRange;
  3565. indexDesignator.parameters.AddExpression(indexListItem);
  3566. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3567. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3568. IndexCheck(indexListItem, sourceArray.length);
  3569. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3570. indexDesignator.parameters.AddExpression(indexListItem)
  3571. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3572. indexDesignator.HasRange;
  3573. (* if the range is given as an array range expression, check the validity of its components *)
  3574. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3575. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3576. first := rangeExpression.first;
  3577. last := rangeExpression.last;
  3578. step := rangeExpression.step;
  3579. (* perform static checks on range components *)
  3580. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3581. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3582. END;
  3583. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3584. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3585. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3586. END
  3587. END;
  3588. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3589. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3590. END;
  3591. (* add conversions to size type *)
  3592. (* TODO: needed? *)
  3593. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3594. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3595. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3596. END;
  3597. IF indexDesignator.hasTensorRange THEN
  3598. (* the index designator's base type is a tensor: leave it as is *)
  3599. ELSE
  3600. (* append a new math array to the index designator's base type *)
  3601. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3602. IF ~error THEN
  3603. (*
  3604. (* optimization: calculate length of target array for static ranges *)
  3605. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3606. IF IsStaticallyOpenRange(rangeExpression) THEN
  3607. (* range is open ('*'): reuse source array length as target array length *)
  3608. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3609. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3610. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3611. IF lastValue = MAX(LONGINT) THEN
  3612. IF IsIntegerValue(sourceArray.length, length) THEN
  3613. lastValue := length - 1;
  3614. isStaticTargetArrayLength := TRUE
  3615. ELSE
  3616. isStaticTargetArrayLength := FALSE
  3617. END
  3618. ELSE
  3619. isStaticTargetArrayLength := TRUE
  3620. END;
  3621. IF isStaticTargetArrayLength THEN
  3622. (* calculate static target array length *)
  3623. IF firstValue > lastValue THEN
  3624. length := 0
  3625. ELSE
  3626. length := 1 + lastValue - firstValue;
  3627. IF length MOD stepValue = 0 THEN
  3628. length := length DIV stepValue
  3629. ELSE
  3630. length := length DIV stepValue + 1
  3631. END
  3632. END;
  3633. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3634. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3635. ASSERT(targetArray.form = SyntaxTree.Static)
  3636. END
  3637. END
  3638. END
  3639. *)
  3640. END;
  3641. SetIndexBaseType(indexDesignator, targetArray)
  3642. END;
  3643. indexDesignator.parameters.AddExpression(indexListItem)
  3644. ELSE
  3645. Error(position, Diagnostics.Invalid,"invalid index list item");
  3646. END;
  3647. END AppendMathIndex;
  3648. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3649. VAR parameters: SyntaxTree.ExpressionList;
  3650. BEGIN
  3651. parameters := index.parameters;
  3652. IF (expression.type = NIL) THEN
  3653. Error(position,Diagnostics.Invalid,"invalid index");
  3654. ELSIF IsIntegerType(expression.type.resolved) THEN
  3655. IF over IS SyntaxTree.ArrayType THEN
  3656. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3657. ELSIF over IS SyntaxTree.StringType THEN
  3658. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3659. END;
  3660. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3661. parameters.AddExpression(expression);
  3662. ELSE
  3663. Error(position,Diagnostics.Invalid,"invalid index");
  3664. END;
  3665. END AppendIndex;
  3666. (** convert an expression to math array type
  3667. if expression is of math array type: return expression itself
  3668. if expression is of array-structured object type: return an index operator call on it
  3669. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3670. otherwise: return invalid expression
  3671. **)
  3672. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3673. VAR
  3674. result: SyntaxTree.Expression;
  3675. mathArrayType: SyntaxTree.MathArrayType;
  3676. BEGIN
  3677. IF expression.type = NIL THEN
  3678. result := SyntaxTree.invalidExpression
  3679. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3680. (* expression of math array type *)
  3681. result := expression
  3682. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3683. (* expression of array-structured object type *)
  3684. mathArrayType := MathArrayStructureOfType(expression.type);
  3685. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3686. ELSE
  3687. result := SyntaxTree.invalidExpression
  3688. END;
  3689. RETURN result
  3690. END ConvertToMathArray;
  3691. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3692. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3693. VAR
  3694. result: SyntaxTree.ExpressionList;
  3695. i: LONGINT;
  3696. BEGIN
  3697. result := SyntaxTree.NewExpressionList();
  3698. FOR i := 1 TO itemCount DO
  3699. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3700. END;
  3701. RETURN result
  3702. END ListOfOpenRanges;
  3703. (** create a procedure call designator for an index operator call on an array-structured object type
  3704. - use given index list as actual parameters
  3705. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3706. **)
  3707. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3708. VAR
  3709. operator: SyntaxTree.Operator;
  3710. expression: SyntaxTree.Expression;
  3711. actualParameters, tempList: SyntaxTree.ExpressionList;
  3712. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3713. result, tempDesignator: SyntaxTree.Designator;
  3714. recordType: SyntaxTree.RecordType;
  3715. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3716. i, hashValue, indexListSize, indexListKind: LONGINT;
  3717. castReturnType: SyntaxTree.MathArrayType;
  3718. BEGIN
  3719. ASSERT(IsArrayStructuredObjectType(left.type));
  3720. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3721. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3722. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3723. indexListSize := indexList.Length();
  3724. indexListKind := 0;
  3725. containsNonRange := FALSE;
  3726. FOR i := 0 TO indexList.Length() - 1 DO
  3727. indexListKind := indexListKind * 2;
  3728. expression := indexList.GetExpression(i);
  3729. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3730. INC(indexListKind)
  3731. ELSE
  3732. containsNonRange := TRUE
  3733. END
  3734. END;
  3735. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3736. (* select applicable index operator
  3737. - try to look up optimal index operator
  3738. - if not present, use operator on ranges
  3739. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3740. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3741. *)
  3742. usesGeneralOperator := FALSE;
  3743. IF rhs # NIL THEN
  3744. (* write operator *)
  3745. IF hashValue = -1 THEN
  3746. operator := NIL
  3747. ELSE
  3748. operator := recordType.arrayAccessOperators.write[hashValue];
  3749. END;
  3750. IF operator = NIL THEN
  3751. usesPureRangeOperator := TRUE;
  3752. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3753. operator := recordType.arrayAccessOperators.generalWrite;
  3754. usesGeneralOperator := TRUE
  3755. ELSE
  3756. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3757. operator := recordType.arrayAccessOperators.write[hashValue];
  3758. END
  3759. END
  3760. ELSE
  3761. (* read operator *)
  3762. IF hashValue = -1 THEN
  3763. operator := NIL
  3764. ELSE
  3765. operator := recordType.arrayAccessOperators.read[hashValue];
  3766. END;
  3767. IF operator = NIL THEN
  3768. usesPureRangeOperator := TRUE;
  3769. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3770. operator := recordType.arrayAccessOperators.generalRead;
  3771. usesGeneralOperator := TRUE
  3772. ELSE
  3773. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3774. operator := recordType.arrayAccessOperators.read[hashValue];
  3775. END
  3776. END
  3777. END;
  3778. IF operator = NIL THEN
  3779. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3780. result := SyntaxTree.invalidDesignator;
  3781. ELSE
  3782. (* determine if reshaping is needed *)
  3783. needsReshaping := containsNonRange & usesPureRangeOperator;
  3784. (* import OCArrayBase if reshaping is needed *)
  3785. IF needsReshaping & ~arrayBaseImported THEN
  3786. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3787. arrayBaseImported := TRUE
  3788. END;
  3789. (* add the index list item to the list of actual parameters
  3790. - for general operators: add a single inline array containing the index list items as parameter
  3791. - otherwise: add all index list items as individual parameters
  3792. *)
  3793. actualParameters := SyntaxTree.NewExpressionList();
  3794. IF usesGeneralOperator THEN
  3795. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3796. END;
  3797. FOR i := 0 TO indexListSize - 1 DO
  3798. expression := indexList.GetExpression(i);
  3799. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3800. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3801. tempList := SyntaxTree.NewExpressionList();
  3802. tempList.AddExpression(expression);
  3803. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3804. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3805. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3806. END;
  3807. IF usesGeneralOperator THEN
  3808. tempMathArrayExpression.elements.AddExpression(expression);
  3809. ELSE
  3810. actualParameters.AddExpression(expression)
  3811. END
  3812. END;
  3813. IF usesGeneralOperator THEN
  3814. actualParameters.AddExpression(tempMathArrayExpression)
  3815. END;
  3816. IF rhs # NIL THEN
  3817. (* add actual parameter for RHS *)
  3818. IF needsReshaping THEN
  3819. (* reshape using OCArrayBase.ExpandDimensions *)
  3820. tempList := SyntaxTree.NewExpressionList();
  3821. (* source array *)
  3822. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3823. tempList.AddExpression(rhs);
  3824. ELSE
  3825. (* convert scalar to one-dimensional array *)
  3826. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3827. tempMathArrayExpression.elements.AddExpression(rhs);
  3828. tempList.AddExpression(tempMathArrayExpression)
  3829. END;
  3830. (* list of kept dimensions *)
  3831. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3832. FOR i := 0 TO indexListSize - 1 DO
  3833. expression := indexList.GetExpression(i);
  3834. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3835. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3836. ELSE
  3837. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3838. END
  3839. END;
  3840. tempList.AddExpression(tempMathArrayExpression);
  3841. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3842. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3843. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3844. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3845. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3846. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3847. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3848. expression.SetType(castReturnType);
  3849. ELSE
  3850. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3851. END;
  3852. actualParameters.AddExpression(expression)
  3853. ELSE
  3854. actualParameters.AddExpression(rhs)
  3855. END
  3856. END;
  3857. (* add dereference operator and create procedure call designator *)
  3858. ASSERT(left IS SyntaxTree.Designator);
  3859. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3860. ASSERT(expression IS SyntaxTree.Designator);
  3861. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3862. IF (rhs = NIL) & needsReshaping THEN
  3863. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3864. tempList := SyntaxTree.NewExpressionList();
  3865. FOR i := 0 TO indexList.Length() - 1 DO
  3866. expression := indexList.GetExpression(i);
  3867. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3868. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3869. ELSE
  3870. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3871. END
  3872. END;
  3873. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3874. END;
  3875. IF rhs = NIL THEN
  3876. (* special rule: index read operator calls are considered to be assignable *)
  3877. result.SetAssignable(TRUE)
  3878. END;
  3879. (* put information about this index operator call into the resulting designator *)
  3880. result.SetRelatedAsot(left);
  3881. result.SetRelatedIndexList(indexList)
  3882. END;
  3883. RETURN result
  3884. END NewIndexOperatorCall;
  3885. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3886. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3887. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3888. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3889. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3890. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3891. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3892. CONST trace = FALSE;
  3893. BEGIN
  3894. IF trace THEN
  3895. FOR i := 0 TO actualParameters.Length()-1 DO
  3896. Printout.Info("par", actualParameters.GetExpression(i));
  3897. END;
  3898. END;
  3899. operator := scope.firstOperator;
  3900. WHILE(operator # NIL) DO
  3901. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3902. procedureType := operator.type(SyntaxTree.ProcedureType);
  3903. distance := Distance(system, procedureType,actualParameters);
  3904. IF trace THEN Printout.Info("check op ",operator) END;
  3905. IF distance < bestDistance THEN
  3906. IF trace THEN Printout.Info("taken op",operator) END;
  3907. bestDistance := distance;
  3908. bestOperator := operator;
  3909. END;
  3910. END;
  3911. operator := operator.nextOperator;
  3912. END;
  3913. END FindInScope;
  3914. BEGIN
  3915. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3916. IF oper = 0 THEN (* index *)
  3917. identifier := SyntaxTree.NewIdentifier("[]");
  3918. ELSE
  3919. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3920. END;
  3921. WHILE (recordType # NIL) DO
  3922. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3923. recordType := recordType.GetBaseRecord();
  3924. END;
  3925. RETURN bestOperator
  3926. END FindOperator;
  3927. BEGIN
  3928. type := left.type.resolved;
  3929. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3930. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3931. actualParameters := SyntaxTree.NewExpressionList();
  3932. IF parameters # NIL THEN
  3933. FOR i := 0 TO parameters.Length()-1 DO
  3934. expression := ResolveExpression(parameters.GetExpression(i));
  3935. actualParameters.AddExpression(expression);
  3936. END;
  3937. END;
  3938. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3939. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3940. IF op # NIL THEN
  3941. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3942. ASSERT(expression IS SyntaxTree.Designator);
  3943. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3944. result.SetRelatedAsot(left);
  3945. result.SetRelatedIndexList(parameters);
  3946. (* check if write operator exists, for var parameters *)
  3947. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3948. actualParameters := SyntaxTree.NewExpressionList();
  3949. FOR i := 0 TO parameters.Length()-1 DO
  3950. expression := ResolveExpression(parameters.GetExpression(i));
  3951. actualParameters.AddExpression(expression);
  3952. END;
  3953. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3954. actualParameters.AddExpression(rhs);
  3955. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3956. IF op = NIL THEN rhs := NIL END;
  3957. END;
  3958. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3959. ELSE
  3960. result := NIL;
  3961. END;
  3962. RETURN result;
  3963. END NewObjectOperatorCall;
  3964. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3965. 1. convert bracket designator chains into a single one that contains separators
  3966. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3967. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3968. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3969. - if an array or math array is indexed over, create index designator
  3970. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3971. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3972. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3973. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3974. - if an array-structured object type is indexed over, create procedure call designator
  3975. e.g.: a[x, y] -> a^."[]"(x, y)
  3976. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3977. - a[i, *] = a[i][*]
  3978. - a[*, i] # a[*][i]
  3979. Because:
  3980. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3981. - 'i-th column' = a[*, i]
  3982. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3983. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3984. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3985. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3986. **)
  3987. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3988. VAR
  3989. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3990. indexDesignator: SyntaxTree.IndexDesignator;
  3991. designator: SyntaxTree.Designator;
  3992. type: SyntaxTree.Type;
  3993. recordType: SyntaxTree.RecordType;
  3994. expression, rhs: SyntaxTree.Expression;
  3995. indexList: SyntaxTree.ExpressionList;
  3996. i: LONGINT;
  3997. hasError, done: BOOLEAN;
  3998. PROCEDURE FinalizeIndexDesignator;
  3999. BEGIN
  4000. IF indexDesignator # NIL THEN
  4001. (* the end of a tensor has been reached: *)
  4002. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4003. SetIndexBaseType(indexDesignator, type);
  4004. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4005. designator := indexDesignator;
  4006. type := designator.type.resolved;
  4007. indexDesignator := NIL;
  4008. ASSERT(SyntaxTree.Resolved IN type.state)
  4009. END
  4010. END FinalizeIndexDesignator;
  4011. BEGIN
  4012. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4013. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4014. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4015. (* copy all index list entries including a separator to the left bracket designator *)
  4016. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4017. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4018. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4019. END;
  4020. (* propagate the related RHS *)
  4021. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4022. (* only resolve left bracket designator and use as final result *)
  4023. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4024. ELSE
  4025. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4026. designator := ResolveDesignator(bracketDesignator.left);
  4027. type := designator.type.resolved;
  4028. indexDesignator := NIL;
  4029. (*!!! clean up *)
  4030. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4031. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4032. IF resolvedExpression = NIL THEN
  4033. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4034. resolvedExpression := SyntaxTree.invalidDesignator
  4035. END;
  4036. RETURN;
  4037. END;
  4038. i := 0;
  4039. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4040. expression := bracketDesignator.parameters.GetExpression(i);
  4041. expression := ResolveExpression(expression);
  4042. bracketDesignator.parameters.SetExpression(i, expression);
  4043. IF expression = SyntaxTree.indexListSeparator THEN
  4044. (* finalize an existing index designator if needed *)
  4045. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4046. INC(i)
  4047. ELSE
  4048. (* do auto-dereferencing if needed *)
  4049. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4050. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4051. & (i=0)*)
  4052. THEN
  4053. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4054. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4055. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4056. designator := SyntaxTree.invalidDesignator;
  4057. type := SyntaxTree.invalidType
  4058. ELSE
  4059. FinalizeIndexDesignator;
  4060. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4061. type := designator.type.resolved
  4062. END
  4063. END;
  4064. (* create a new index designator, if needed *)
  4065. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4066. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4067. indexDesignator.SetAssignable(designator.assignable);
  4068. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4069. (* designator := indexDesignator *)
  4070. END;
  4071. IF type = SyntaxTree.invalidType THEN
  4072. (* error already handled *)
  4073. INC(i)
  4074. ELSIF type IS SyntaxTree.ArrayType THEN
  4075. (* indexing over an array *)
  4076. ASSERT(indexDesignator # NIL);
  4077. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4078. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4079. INC(i)
  4080. ELSIF type IS SyntaxTree.StringType THEN
  4081. (* indexing over an array *)
  4082. ASSERT(indexDesignator # NIL);
  4083. AppendIndex(expression.position, indexDesignator, expression, type);
  4084. type := type(SyntaxTree.StringType).baseType.resolved;
  4085. INC(i)
  4086. ELSIF type IS SyntaxTree.MathArrayType THEN
  4087. (* indexing over a math array *)
  4088. ASSERT(indexDesignator # NIL);
  4089. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4090. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4091. INC(i)
  4092. ELSIF IsArrayStructuredObjectType(type) THEN
  4093. (* indexing over ASOTs *)
  4094. FinalizeIndexDesignator;
  4095. ASSERT(type IS SyntaxTree.PointerType);
  4096. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4097. (*
  4098. - collect index list items from bracket designator that belong to ASOT
  4099. - check for errors
  4100. *)
  4101. indexList := SyntaxTree.NewExpressionList();
  4102. hasError := FALSE;
  4103. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4104. (* indexing over tensor ASOT:
  4105. - stop at index list end or separator
  4106. - dimensionality is given by number of index list items
  4107. *)
  4108. done := FALSE;
  4109. WHILE ~done DO
  4110. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4111. done := TRUE;
  4112. ELSE
  4113. expression := bracketDesignator.parameters.GetExpression(i);
  4114. IF expression = SyntaxTree.indexListSeparator THEN
  4115. done := TRUE;
  4116. ELSE
  4117. expression := ResolveExpression(expression);
  4118. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4119. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4120. hasError := TRUE
  4121. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4122. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4123. expression := SyntaxTree.invalidExpression;
  4124. hasError := TRUE
  4125. END;
  4126. indexList.AddExpression(expression)
  4127. END;
  4128. INC(i)
  4129. END
  4130. END
  4131. ELSE
  4132. (* indexing over non-tensor ASOT:
  4133. - ignore separators
  4134. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4135. *)
  4136. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4137. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4138. expression := bracketDesignator.parameters.GetExpression(i);
  4139. ELSE
  4140. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4141. END;
  4142. IF expression # SyntaxTree.indexListSeparator THEN
  4143. expression := ResolveExpression(expression);
  4144. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4145. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4146. expression := SyntaxTree.invalidExpression;
  4147. hasError := TRUE
  4148. END;
  4149. indexList.AddExpression(expression)
  4150. END;
  4151. INC(i)
  4152. END;
  4153. END;
  4154. IF hasError THEN
  4155. designator := SyntaxTree.invalidDesignator;
  4156. type := SyntaxTree.invalidType;
  4157. ELSE
  4158. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4159. and the last entry in the index list belongs to the array-structured object type in question.
  4160. E.g.: for a 2-dimensional array-structured object type:
  4161. - 'lhs := asot[1, 2]' -> read mode
  4162. - 'asot[1, 2] := rhs' -> write mode
  4163. - 'asot[1, 2, 3] := rhs' -> read mode
  4164. *)
  4165. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4166. rhs := bracketDesignator.relatedRhs
  4167. ELSE
  4168. rhs := NIL
  4169. END;
  4170. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4171. type := designator.type
  4172. END
  4173. ELSE
  4174. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4175. designator := SyntaxTree.invalidDesignator;
  4176. type := SyntaxTree.invalidType;
  4177. INC(i)
  4178. END
  4179. END
  4180. END;
  4181. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4182. resolvedExpression := designator
  4183. END
  4184. END VisitBracketDesignator;
  4185. (** check and resolve expression list
  4186. - resolve each expression in an expression list
  4187. - returns true if and only if all statements could have successfully been resolved
  4188. **)
  4189. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4190. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4191. BEGIN
  4192. result := TRUE;
  4193. FOR i := 0 TO expressionList.Length()-1 DO
  4194. expression := ResolveExpression(expressionList.GetExpression(i));
  4195. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4196. expressionList.SetExpression(i,expression);
  4197. END;
  4198. RETURN result
  4199. END ExpressionList;
  4200. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4201. BEGIN
  4202. type := type.resolved;
  4203. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4204. RETURN TRUE
  4205. ELSIF system.CanPassInRegister # NIL THEN
  4206. RETURN system.CanPassInRegister(type);
  4207. ELSE
  4208. RETURN FALSE
  4209. END;
  4210. END CanPassInRegister;
  4211. (** return procedure call designator left(actualParameters)
  4212. - check realtime procedure call in realtime procedure
  4213. - check number of parameters
  4214. - check parameter compatibility
  4215. return invalidDesignator if error
  4216. **)
  4217. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4218. VAR result: SyntaxTree.Designator;
  4219. numberFormalParameters, numberActualParameters: LONGINT;
  4220. formalType: SyntaxTree.ProcedureType;
  4221. formalParameter: SyntaxTree.Parameter;
  4222. actualParameter: SyntaxTree.Expression;
  4223. i: LONGINT;
  4224. BEGIN
  4225. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4226. result := SyntaxTree.invalidDesignator;
  4227. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4228. numberFormalParameters := formalType.numberParameters;
  4229. numberActualParameters := actualParameters.Length();
  4230. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4231. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4232. END;
  4233. IF ~ExpressionList(actualParameters) THEN
  4234. result := SyntaxTree.invalidDesignator
  4235. ELSE
  4236. IF numberActualParameters <= numberFormalParameters THEN
  4237. formalParameter := formalType.firstParameter;
  4238. FOR i := 0 TO numberActualParameters-1 DO
  4239. actualParameter := actualParameters.GetExpression(i);
  4240. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4241. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4242. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4243. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4244. ELSE
  4245. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4246. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4247. END;
  4248. actualParameters.SetExpression(i,actualParameter);
  4249. END;
  4250. formalParameter := formalParameter.nextParameter;
  4251. END;
  4252. WHILE (formalParameter # NIL) DO
  4253. IF formalParameter.defaultValue # NIL THEN
  4254. actualParameters.AddExpression(formalParameter.defaultValue);
  4255. formalParameter := formalParameter.nextParameter
  4256. ELSE
  4257. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4258. formalParameter := NIL;
  4259. END;
  4260. END;
  4261. ELSE
  4262. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4263. END;
  4264. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4265. result.SetAssignable(FALSE);
  4266. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4267. END;
  4268. RETURN result
  4269. END NewProcedureCallDesignator;
  4270. (**
  4271. builtin call designator generated in VisitParameterDesignator
  4272. -> nothing to be resolved
  4273. **)
  4274. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4275. BEGIN
  4276. resolvedExpression := x;
  4277. END VisitTypeGuardDesignator;
  4278. (**
  4279. builtin call designator generated in VisitParameterDesignator
  4280. -> nothing to be resolved
  4281. **)
  4282. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4283. BEGIN
  4284. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4285. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4286. ASSERT(resolvedExpression.type # NIL);
  4287. ELSIF ExpressionList(x.parameters) THEN
  4288. resolvedExpression := x;
  4289. END;
  4290. END VisitBuiltinCallDesignator;
  4291. (**
  4292. procedure call designator generated in VisitParameterDesignator
  4293. -> nothing to be resolved
  4294. **)
  4295. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4296. BEGIN
  4297. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4298. resolvedExpression := x;
  4299. END VisitProcedureCallDesignator;
  4300. (** return true if x is a variable else return false and report error **)
  4301. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4302. VAR result: BOOLEAN;
  4303. BEGIN
  4304. result := TRUE;
  4305. IF x = SyntaxTree.invalidExpression THEN
  4306. result := FALSE;
  4307. ELSIF ~IsVariable(x) THEN
  4308. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4309. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4310. result := FALSE;
  4311. END;
  4312. RETURN result
  4313. END CheckVariable;
  4314. (**
  4315. if expression x is of basic type then return true else report error and return false
  4316. **)
  4317. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4318. VAR result: BOOLEAN;
  4319. BEGIN
  4320. result := FALSE;
  4321. IF x = SyntaxTree.invalidExpression THEN
  4322. ELSIF ~IsBasicType(x.type) THEN
  4323. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4324. result := FALSE
  4325. ELSE result := TRUE
  4326. END;
  4327. RETURN result
  4328. END CheckBasicType;
  4329. (**
  4330. if expression x is of number type then return true else report error and return false
  4331. **)
  4332. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4333. VAR result: BOOLEAN;
  4334. BEGIN
  4335. result := FALSE;
  4336. IF x = SyntaxTree.invalidExpression THEN
  4337. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4338. Error(x.position,Diagnostics.Invalid,"is non number type");
  4339. ELSE result := TRUE
  4340. END;
  4341. RETURN result
  4342. END CheckNumberType;
  4343. (**
  4344. if expression x is of number or size type but not complex then return true else report error and return false
  4345. **)
  4346. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4347. VAR result: BOOLEAN;
  4348. BEGIN
  4349. result := FALSE;
  4350. IF x = SyntaxTree.invalidExpression THEN
  4351. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4352. Error(x.position,Diagnostics.Invalid,"is complex type");
  4353. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4354. Error(x.position,Diagnostics.Invalid,"is non number type");
  4355. ELSE result := TRUE
  4356. END;
  4357. RETURN result
  4358. END CheckNonComplexNumberSizeType;
  4359. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4360. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4361. BEGIN
  4362. result := FALSE; type := x.type.resolved;
  4363. IF x = SyntaxTree.invalidExpression THEN
  4364. 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
  4365. TRACE(type.sizeInBits);
  4366. TRACE(system.addressType.sizeInBits);
  4367. Error(x.position,Diagnostics.Invalid,"is no address type");
  4368. ELSE result := TRUE
  4369. END;
  4370. RETURN result
  4371. END CheckAddressType;
  4372. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4373. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4374. BEGIN
  4375. result := FALSE; type := x.type.resolved;
  4376. IF x = SyntaxTree.invalidExpression THEN
  4377. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4378. Error(x.position,Diagnostics.Invalid,"is no size type");
  4379. ELSE result := TRUE
  4380. END;
  4381. RETURN result
  4382. END CheckSizeType;
  4383. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4384. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4385. BEGIN
  4386. result := FALSE; type := x.type.resolved;
  4387. IF x = SyntaxTree.invalidExpression THEN
  4388. 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
  4389. Error(x.position,Diagnostics.Invalid,"is no object type");
  4390. ELSE result := TRUE
  4391. END;
  4392. RETURN result
  4393. END CheckObjectType;
  4394. (**
  4395. if expression x is of integer type then return true else report error and return false
  4396. **)
  4397. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4398. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4399. BEGIN
  4400. result := FALSE; type := x.type.resolved;
  4401. IF x = SyntaxTree.invalidExpression THEN
  4402. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4403. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4404. ELSE result := TRUE
  4405. END;
  4406. RETURN result
  4407. END CheckIntegerType;
  4408. (**
  4409. if expression x is of character type then return true else report error and return false
  4410. **)
  4411. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4412. VAR result: BOOLEAN;
  4413. BEGIN
  4414. result := FALSE;
  4415. IF x = SyntaxTree.invalidExpression THEN
  4416. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4417. Error(x.position,Diagnostics.Invalid,"is no character type");
  4418. ELSE result := TRUE
  4419. END;
  4420. RETURN result
  4421. END CheckCharacterType;
  4422. (**
  4423. if expression x is of real type then return true else report error and return false
  4424. **)
  4425. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4426. VAR result: BOOLEAN;
  4427. BEGIN
  4428. result := FALSE;
  4429. IF x = SyntaxTree.invalidExpression THEN
  4430. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4431. Error(x.position,Diagnostics.Invalid,"is no float type");
  4432. ELSE result := TRUE
  4433. END;
  4434. RETURN result
  4435. END CheckRealType;
  4436. (**
  4437. if expression x is of range type then return true else report error and return false
  4438. **)
  4439. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4440. VAR result: BOOLEAN;
  4441. BEGIN
  4442. result := FALSE;
  4443. IF x = SyntaxTree.invalidExpression THEN
  4444. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4445. Error(x.position,Diagnostics.Invalid,"is no range type");
  4446. ELSE result := TRUE
  4447. END;
  4448. RETURN result
  4449. END CheckRangeType;
  4450. (**
  4451. if expression x is of boolean type then return true else report error and return false
  4452. **)
  4453. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4454. VAR result: BOOLEAN;
  4455. BEGIN
  4456. result := FALSE;
  4457. IF x = SyntaxTree.invalidExpression THEN
  4458. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4459. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4460. ELSE result := TRUE
  4461. END;
  4462. RETURN result
  4463. END CheckBooleanType;
  4464. (**
  4465. if expression x is of set type then return true else report error and return false
  4466. **)
  4467. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4468. VAR result: BOOLEAN;
  4469. BEGIN
  4470. result := FALSE;
  4471. IF x = SyntaxTree.invalidExpression THEN
  4472. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4473. Error(x.position,Diagnostics.Invalid,"is no set type");
  4474. ELSE result := TRUE
  4475. END;
  4476. RETURN result
  4477. END CheckSetType;
  4478. (**
  4479. if expression x is of string or array of character type then return true else report error and return false
  4480. **)
  4481. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4482. VAR result: BOOLEAN;
  4483. BEGIN
  4484. result := FALSE;
  4485. IF x = SyntaxTree.invalidExpression THEN
  4486. ELSIF ~IsStringType(x.type.resolved) THEN
  4487. Error(x.position,Diagnostics.Invalid,"is no string type");
  4488. ELSE result := TRUE
  4489. END;
  4490. RETURN result
  4491. END CheckStringType;
  4492. (**
  4493. if expression x is a type declaration type return true else report error and return false
  4494. **)
  4495. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4496. VAR result: BOOLEAN;
  4497. BEGIN
  4498. result := FALSE;
  4499. IF x = SyntaxTree.invalidExpression THEN
  4500. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4501. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4502. ELSE result := TRUE
  4503. END;
  4504. RETURN result
  4505. END CheckTypeDeclarationType;
  4506. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4507. VAR result: BOOLEAN;
  4508. BEGIN
  4509. result := FALSE;
  4510. IF x = SyntaxTree.invalidExpression THEN
  4511. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4512. result := TRUE;
  4513. value := x.resolved(SyntaxTree.IntegerValue).value;
  4514. ELSE
  4515. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4516. END;
  4517. RETURN result;
  4518. END CheckIntegerValue;
  4519. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4520. VAR result: BOOLEAN;
  4521. BEGIN
  4522. result := FALSE;
  4523. IF x = SyntaxTree.invalidExpression THEN
  4524. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4525. result := TRUE;
  4526. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4527. ELSE
  4528. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4529. END;
  4530. RETURN result;
  4531. END CheckStringValue;
  4532. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4533. BEGIN
  4534. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4535. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4536. ELSE
  4537. RETURN FALSE
  4538. END;
  4539. END IsUnsignedValue;
  4540. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4541. BEGIN
  4542. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4543. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4544. ELSE
  4545. RETURN FALSE
  4546. END
  4547. END IsAddressValue;
  4548. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4549. BEGIN
  4550. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4551. END IsAddressExpression;
  4552. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4553. BEGIN
  4554. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4555. END IsSizeExpression;
  4556. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4557. VAR result: BOOLEAN;
  4558. BEGIN
  4559. result := FALSE;
  4560. IF x = SyntaxTree.invalidExpression THEN
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4562. result := TRUE;
  4563. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4564. ELSE
  4565. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckEnumerationValue;
  4569. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4570. VAR result: BOOLEAN;
  4571. BEGIN
  4572. result := FALSE;
  4573. IF x = SyntaxTree.invalidExpression THEN
  4574. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4575. result := TRUE;
  4576. value := x.resolved(SyntaxTree.CharacterValue).value;
  4577. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4578. result := TRUE;
  4579. value := x.resolved(SyntaxTree.StringValue).value[0];
  4580. ELSE
  4581. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4582. END;
  4583. RETURN result;
  4584. END CheckCharacterValue;
  4585. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4586. VAR result: BOOLEAN;
  4587. BEGIN
  4588. result := FALSE;
  4589. IF x = SyntaxTree.invalidExpression THEN
  4590. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4591. value := x.resolved(SyntaxTree.IntegerValue).value;
  4592. IF (value > 0) OR includeZero & (value = 0) THEN
  4593. result := TRUE;
  4594. ELSE
  4595. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4596. END
  4597. ELSE
  4598. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4599. END;
  4600. RETURN result;
  4601. END CheckPositiveIntegerValue;
  4602. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4603. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4604. BEGIN
  4605. result := FALSE;
  4606. IF x = SyntaxTree.invalidExpression THEN
  4607. ELSE
  4608. type := x.type.resolved;
  4609. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4610. portType := type(SyntaxTree.PortType);
  4611. result := TRUE
  4612. ELSE
  4613. Error(x.position,Diagnostics.Invalid,"no port type");
  4614. END;
  4615. END;
  4616. RETURN result
  4617. END CheckPortType;
  4618. (* move to builtin procedure call statement ?
  4619. remove builtin procedure call designator ?
  4620. *)
  4621. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4622. VAR
  4623. numberActualParameters,numberFormalParameters: LONGINT;
  4624. formalParameter: SyntaxTree.Parameter;
  4625. actualParameter: SyntaxTree.Expression;
  4626. procedureType: SyntaxTree.ProcedureType;
  4627. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4628. inPort, outPort: SyntaxTree.PortType;
  4629. constructor: SyntaxTree.Procedure;
  4630. type0,type1,type2: SyntaxTree.Type;
  4631. type,base,parameterType: SyntaxTree.Type;
  4632. arrayType: SyntaxTree.ArrayType;
  4633. i,i0,i1: LONGINT;
  4634. r,r0,r1,im: LONGREAL;
  4635. c: CHAR;
  4636. id: LONGINT;
  4637. b: BOOLEAN;
  4638. first: LONGINT;
  4639. mathArrayType: SyntaxTree.MathArrayType;
  4640. customBuiltin: SyntaxTree.CustomBuiltin;
  4641. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4642. VAR resultB: BOOLEAN;
  4643. BEGIN
  4644. IF numberActualParameters < from THEN
  4645. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4646. result := SyntaxTree.invalidExpression;
  4647. resultB := FALSE;
  4648. ELSIF numberActualParameters > to THEN
  4649. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4650. result := SyntaxTree.invalidExpression;
  4651. resultB := FALSE;
  4652. ELSE
  4653. resultB := TRUE;
  4654. END;
  4655. RETURN resultB
  4656. END CheckArity;
  4657. BEGIN
  4658. type := NIL; result := NIL;
  4659. type0 := NIL; type1 := NIL; type2 := NIL;
  4660. numberActualParameters := actualParameters.Length();
  4661. IF numberActualParameters>0 THEN
  4662. parameter0 := actualParameters.GetExpression(0);
  4663. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4664. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4665. result := SyntaxTree.invalidExpression
  4666. END
  4667. END;
  4668. IF numberActualParameters >1 THEN
  4669. parameter1 := actualParameters.GetExpression(1);
  4670. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4671. ELSE
  4672. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4673. result := SyntaxTree.invalidExpression
  4674. END
  4675. END;
  4676. IF numberActualParameters >2 THEN
  4677. parameter2 := actualParameters.GetExpression(2);
  4678. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4679. ELSE
  4680. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4681. result := SyntaxTree.invalidExpression
  4682. END
  4683. END;
  4684. IF returnType # NIL THEN
  4685. id := Global.New;
  4686. result := NIL;
  4687. ELSE
  4688. id := builtin.id;
  4689. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4690. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4691. END;
  4692. END;
  4693. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4694. ELSIF result # NIL THEN type := result.type (* operator *)
  4695. ELSE
  4696. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4697. result(SyntaxTree.Designator).SetLeft(left);
  4698. IF returnType # NIL THEN
  4699. type := returnType;
  4700. END;
  4701. (* ---- ASSERT ----- *)
  4702. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4703. IF CheckBooleanType(parameter0) THEN
  4704. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4705. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4706. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4707. *)
  4708. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4709. Error(position,Diagnostics.Invalid,"assert failed");
  4710. END;
  4711. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4712. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4713. rules imposed by the architecture / current runtime
  4714. *)
  4715. END;
  4716. END;
  4717. (* ---- COPY ----- *)
  4718. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4719. IF~IsStringType(type0) THEN
  4720. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4721. END;
  4722. IF ~IsStringType(type1) THEN
  4723. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4724. ELSIF CheckVariable(parameter1) THEN
  4725. IF (type0 IS SyntaxTree.StringType) THEN
  4726. arrayType := type1(SyntaxTree.ArrayType);
  4727. IF arrayType.form = SyntaxTree.Static THEN
  4728. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4729. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4730. END;
  4731. END;
  4732. END;
  4733. END;
  4734. (* ---- INC, DEC----- *)
  4735. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4736. IF numberActualParameters = 1 THEN
  4737. parameter1 :=Global.NewIntegerValue(system,position,1);
  4738. actualParameters.AddExpression(parameter1);
  4739. END;
  4740. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4741. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4742. Error(position,Diagnostics.Invalid,"incompatible increment");
  4743. ELSE
  4744. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4745. actualParameters.SetExpression(1,parameter1);
  4746. END;
  4747. END;
  4748. (* ---- EXCL, INCL----- *)
  4749. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4750. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4751. IF IsIntegerValue(parameter1,i0) THEN
  4752. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4753. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4754. END;
  4755. END;
  4756. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4757. actualParameters.SetExpression(1,parameter1);
  4758. END;
  4759. (* ---- HALT, SYSTEM.HALT ----- *)
  4760. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4761. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4762. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4763. rules imposed by the architecture / current runtime
  4764. *)
  4765. END;
  4766. (* ---- WAIT ----- *)
  4767. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4768. IF CheckObjectType(parameter0) THEN
  4769. END;
  4770. (* ---- NEW ----- *)
  4771. ELSIF (id = Global.New) THEN
  4772. IF returnType # NIL THEN
  4773. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4774. ELSE
  4775. first := 1;
  4776. END;
  4777. IF CheckArity(first,Infinity) THEN
  4778. IF currentIsRealtime THEN
  4779. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4780. END;
  4781. (* check constructor *)
  4782. IF (first =0) OR CheckVariable(parameter0) THEN
  4783. IF type0 IS SyntaxTree.PointerType THEN
  4784. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4785. END;
  4786. IF type0 IS SyntaxTree.ArrayType THEN
  4787. arrayType := type0(SyntaxTree.ArrayType);
  4788. IF arrayType.form = SyntaxTree.Static THEN
  4789. i := first
  4790. ELSIF arrayType.form = SyntaxTree.Open THEN
  4791. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4792. ELSE HALT(100)
  4793. END;
  4794. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4795. i := first;
  4796. REPEAT
  4797. actualParameter := actualParameters.GetExpression(i);
  4798. IF CheckSizeType(actualParameter) THEN
  4799. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4800. actualParameters.SetExpression(i,actualParameter);
  4801. END;
  4802. INC(i);
  4803. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4804. END;
  4805. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4806. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4807. IF constructor = NIL THEN
  4808. IF CheckArity(first,first) THEN END;
  4809. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4810. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4811. ELSE
  4812. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4813. numberFormalParameters := procedureType.numberParameters;
  4814. IF numberActualParameters-first <= numberFormalParameters THEN
  4815. formalParameter := procedureType.firstParameter;
  4816. FOR i := first TO numberActualParameters-1 DO
  4817. actualParameter := actualParameters.GetExpression(i);
  4818. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4819. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4820. ELSE
  4821. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4822. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4823. END;
  4824. actualParameters.SetExpression(i,actualParameter);
  4825. END;
  4826. formalParameter := formalParameter.nextParameter;
  4827. END;
  4828. WHILE (formalParameter # NIL) DO
  4829. IF formalParameter.defaultValue # NIL THEN
  4830. actualParameters.AddExpression(formalParameter.defaultValue);
  4831. formalParameter := formalParameter.nextParameter
  4832. ELSE
  4833. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4834. formalParameter := NIL;
  4835. END;
  4836. END;
  4837. ELSE
  4838. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4839. END;
  4840. END;
  4841. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4842. mathArrayType := type0(SyntaxTree.MathArrayType);
  4843. IF mathArrayType.form = SyntaxTree.Static THEN
  4844. Error(position,Diagnostics.Invalid,"new on static array");
  4845. ELSE
  4846. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4847. i0 := first+1; i1 := Infinity;
  4848. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4849. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4850. i1 := i0;
  4851. ELSE HALT(100);
  4852. END;
  4853. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4854. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4855. base := ArrayBase(type0,MAX(LONGINT));
  4856. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4857. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4858. IF ~CompatibleTo(system,type0,parameterType) THEN
  4859. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4860. result := SyntaxTree.invalidExpression;
  4861. ELSE
  4862. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4863. END;
  4864. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4865. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4866. IF ~CompatibleTo(system,type1,parameterType) THEN
  4867. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4868. result := SyntaxTree.invalidExpression;
  4869. ELSE
  4870. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4871. END;
  4872. ELSE
  4873. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4874. i := first;
  4875. REPEAT
  4876. actualParameter := actualParameters.GetExpression(i);
  4877. IF CheckSizeType(actualParameter) THEN
  4878. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4879. actualParameters.SetExpression(i,actualParameter);
  4880. END;
  4881. INC(i);
  4882. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4883. END;
  4884. END;
  4885. END;
  4886. ELSIF type0 IS SyntaxTree.CellType THEN
  4887. IF ~(currentIsCellNet) THEN
  4888. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4889. ELSE
  4890. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4891. IF (constructor = NIL) & CheckArity(1,1) THEN
  4892. (* ok *)
  4893. ELSE
  4894. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4895. numberFormalParameters := procedureType.numberParameters;
  4896. DEC(numberActualParameters);
  4897. IF numberActualParameters <= numberFormalParameters THEN
  4898. formalParameter := procedureType.firstParameter;
  4899. FOR i := first TO numberActualParameters DO
  4900. actualParameter := actualParameters.GetExpression(i);
  4901. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4902. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4903. ELSE
  4904. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4905. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4906. END;
  4907. actualParameters.SetExpression(i,actualParameter);
  4908. END;
  4909. formalParameter := formalParameter.nextParameter;
  4910. END;
  4911. WHILE (formalParameter # NIL) DO
  4912. IF formalParameter.defaultValue # NIL THEN
  4913. actualParameters.AddExpression(formalParameter.defaultValue);
  4914. formalParameter := formalParameter.nextParameter
  4915. ELSE
  4916. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4917. formalParameter := NIL;
  4918. END;
  4919. END;
  4920. ELSE
  4921. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4922. END;
  4923. END;
  4924. END;
  4925. activeCellsStatement := TRUE;
  4926. ELSE
  4927. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4928. END;
  4929. END;
  4930. END;
  4931. (* ---- DISPOSE ----- *)
  4932. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4933. IF ~IsPointerType(parameter0.type) THEN
  4934. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4935. ELSIF ~IsDisposable(parameter0.type) THEN
  4936. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4937. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4938. END
  4939. (* ---- GETPROCEDURE ----- *)
  4940. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4941. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4942. IF CheckVariable(parameter2) THEN
  4943. IF ~GetProcedureAllowed(parameter2.type) THEN
  4944. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4945. END;
  4946. END;
  4947. END;
  4948. (* ---- ABS ----- *)
  4949. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4950. (* note: ABS on complex numbers is done using overloading *)
  4951. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4952. type := type0;
  4953. IF IsIntegerValue(parameter0,i0) THEN
  4954. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4955. type := Global.GetIntegerType(system,ABS(i0));
  4956. ELSIF IsRealValue(parameter0,r) THEN
  4957. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4958. END;
  4959. ELSE
  4960. type := SyntaxTree.invalidType;
  4961. END;
  4962. (* ---- ASH, ASR ----- *)
  4963. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4964. type := type0;
  4965. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4966. (*
  4967. ConvertOperands(parameter0,parameter1); (* same type *)
  4968. *)
  4969. type := parameter0.type;
  4970. IF IsIntegerValue(parameter0,i0) THEN
  4971. IF IsIntegerValue(parameter1,i1) THEN
  4972. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4973. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4974. result := ResolveExpression(result);
  4975. type := Global.GetIntegerType(system,i0);
  4976. END;
  4977. END;
  4978. IF type.resolved.sizeInBits < 32 THEN
  4979. type := system.longintType;
  4980. END;
  4981. (*!compatibility with release, remove when resolved
  4982. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4983. *)
  4984. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4985. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4986. actualParameters.SetExpression(0,parameter0);
  4987. actualParameters.SetExpression(1,parameter1);
  4988. END;
  4989. (* ---- CAP ----- *)
  4990. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4991. type := system.characterType;
  4992. IF CheckCharacterType (parameter0) THEN
  4993. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4994. actualParameters.SetExpression(0,parameter0);
  4995. IF IsCharacterValue(parameter0,c) THEN
  4996. IF (c <= "z") & (c >= "a") THEN
  4997. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4998. ELSE
  4999. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5000. END;
  5001. END;
  5002. END;
  5003. (* ---- CHR ----- *)
  5004. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5005. IF id = Global.Chr THEN
  5006. type := system.characterType
  5007. ELSE
  5008. type := system.characterType32
  5009. END;
  5010. IF CheckIntegerType(parameter0) THEN
  5011. IF IsIntegerValue(parameter0,i0) THEN
  5012. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5013. result := ResolveExpression(result);
  5014. ELSE
  5015. (*
  5016. result := NewConversion(parameter0.position,parameter0,type);
  5017. *)
  5018. END;
  5019. END
  5020. (* ---- ENTIER ----- *)
  5021. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5022. type := system.longintType;
  5023. IF CheckRealType(parameter0) THEN
  5024. IF IsRealValue(parameter0,r) THEN
  5025. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5026. type := Global.GetIntegerType(system,ENTIER(r));
  5027. END
  5028. END;
  5029. (* ---- ENTIERH ----- *)
  5030. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5031. type := system.hugeintType;
  5032. IF CheckRealType(parameter0) THEN
  5033. IF IsRealValue(parameter0,r) THEN
  5034. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5035. END
  5036. END;
  5037. (* ---- LEN ----- *)
  5038. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5039. type := system.longintType;
  5040. base := type0;
  5041. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5042. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5043. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5044. END;
  5045. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5046. actualParameters.SetExpression(0,parameter0);
  5047. type0 := parameter0.type.resolved;
  5048. base := type0;
  5049. END;
  5050. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5051. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5052. IF i1 < 0 THEN
  5053. Error(position,Diagnostics.Invalid,"invalid dimension");
  5054. base := SyntaxTree.invalidType;
  5055. ELSE
  5056. base := ArrayBase(base,i1);
  5057. IF (base # NIL) & Indexable(base) THEN
  5058. ELSE
  5059. Error(position,Diagnostics.Invalid,"len on no array");
  5060. IF VerboseErrorMessage THEN
  5061. Printout.Info("base",base);
  5062. END;
  5063. base := SyntaxTree.invalidType;
  5064. END;
  5065. END;
  5066. IF numberActualParameters=2 THEN
  5067. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5068. actualParameters.SetExpression(1,parameter1);
  5069. ELSIF base IS SyntaxTree.MathArrayType THEN
  5070. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5071. END;
  5072. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5073. IF base IS SyntaxTree.ArrayType THEN
  5074. arrayType := base(SyntaxTree.ArrayType);
  5075. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5076. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5077. result := Global.NewIntegerValue(system,position,i);
  5078. type := result.type;(* arrayType.length.type;*)
  5079. ASSERT(type # NIL);
  5080. END;
  5081. ELSIF base IS SyntaxTree.MathArrayType THEN
  5082. mathArrayType := base(SyntaxTree.MathArrayType);
  5083. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5084. result := Global.NewIntegerValue(system,position,i);
  5085. type := result.type;
  5086. (*
  5087. type := mathArrayType.length.type;
  5088. *)
  5089. ASSERT(type # NIL);
  5090. END;
  5091. END;
  5092. END;
  5093. ELSE
  5094. type := system.longintType;
  5095. END;
  5096. (* ---- FIRST ---- *)
  5097. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5098. type := system.longintType;
  5099. IF CheckRangeType(parameter0) THEN END;
  5100. result.SetAssignable(parameter0.assignable)
  5101. (* ---- LAST ---- *)
  5102. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5103. type := system.longintType;
  5104. IF CheckRangeType(parameter0) THEN END;
  5105. result.SetAssignable(parameter0.assignable)
  5106. (* ---- STEP ---- *)
  5107. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5108. type := system.longintType;
  5109. IF CheckRangeType(parameter0) THEN END;
  5110. result.SetAssignable(parameter0.assignable)
  5111. (* ---- RE ---- *)
  5112. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5113. IF CheckNumberType(parameter0) THEN
  5114. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5115. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5116. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5117. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5118. type := parameter0.type
  5119. ELSE
  5120. type := system.realType
  5121. END
  5122. END;
  5123. result.SetAssignable(parameter0.assignable)
  5124. (* ---- IM ---- *)
  5125. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5126. IF CheckNumberType(parameter0) THEN
  5127. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5128. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5129. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5130. ELSE
  5131. type := system.realType;
  5132. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5133. END
  5134. END;
  5135. result.SetAssignable(parameter0.assignable)
  5136. (* ---- MAX ----- *)
  5137. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5138. IF numberActualParameters = 1 THEN
  5139. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5140. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5141. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5142. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5143. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5144. *)
  5145. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5146. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5147. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5148. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5149. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5150. END;
  5151. ELSE
  5152. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5153. END
  5154. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5155. ConvertOperands(parameter0,parameter1);
  5156. actualParameters.SetExpression(0,parameter0);
  5157. actualParameters.SetExpression(1,parameter1);
  5158. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5159. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5160. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5161. END;
  5162. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5163. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5164. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5165. END;
  5166. END;
  5167. type := parameter0.type;
  5168. ELSE type := SyntaxTree.invalidType;
  5169. END;
  5170. (* ---- MIN ----- *)
  5171. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5172. IF numberActualParameters = 1 THEN
  5173. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5174. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5175. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5176. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5177. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5178. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5179. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5180. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5181. END;
  5182. ELSE
  5183. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5184. END
  5185. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5186. ConvertOperands(parameter0,parameter1);
  5187. actualParameters.SetExpression(0,parameter0);
  5188. actualParameters.SetExpression(1,parameter1);
  5189. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5190. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5191. ELSE result.SetResolved(parameter1.resolved)
  5192. END;
  5193. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5194. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5195. ELSE result.SetResolved(parameter1.resolved)
  5196. END;
  5197. END;
  5198. type := parameter0.type;
  5199. ELSE type := SyntaxTree.invalidType;
  5200. END;
  5201. (* ---- ODD ----- *)
  5202. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5203. type := system.booleanType;
  5204. IF CheckIntegerType(parameter0) THEN
  5205. IF IsIntegerValue(parameter0,i0) THEN
  5206. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5207. type := system.booleanType;
  5208. END;
  5209. END;
  5210. (* ---- ORD ----- *)
  5211. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5212. IF id = Global.Ord THEN
  5213. type := system.integerType;
  5214. ELSE
  5215. type := system.longintType;
  5216. END;
  5217. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5218. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5219. actualParameters.SetExpression(0,parameter0);
  5220. (* IF CheckCharacterType(parameter0) THEN*)
  5221. IF IsCharacterValue(parameter0,c)THEN
  5222. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5223. type := Global.GetSignedIntegerType(system,ORD(c));
  5224. END;
  5225. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5226. END;
  5227. (* ---- SHORT ----- *)
  5228. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5229. type := type0;
  5230. IF IsSignedIntegerType(type) THEN
  5231. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5232. ELSIF type = system.integerType THEN type := system.shortintType
  5233. ELSIF type = system.longintType THEN type := system.integerType
  5234. ELSIF type = system.hugeintType THEN type:= system.longintType
  5235. ELSE
  5236. CASE type.sizeInBits OF
  5237. 16: type := Global.Integer8
  5238. |32: type := Global.Integer16
  5239. |64: type := Global.Integer32
  5240. END;
  5241. END;
  5242. ELSIF type IS SyntaxTree.FloatType THEN
  5243. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5244. ELSIF type = system.longrealType THEN type := system.realType
  5245. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5246. END;
  5247. ELSIF type IS SyntaxTree.ComplexType THEN
  5248. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5249. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5250. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5251. END;
  5252. ELSE
  5253. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5254. END;
  5255. IF (parameter0.resolved # NIL) THEN
  5256. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5257. IF parameter0 IS SyntaxTree.Value THEN
  5258. result.SetResolved(parameter0(SyntaxTree.Value));
  5259. END;
  5260. END;
  5261. (* ---- LONG ----- *)
  5262. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5263. type := type0;
  5264. IF IsSignedIntegerType(type) THEN
  5265. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5266. ELSIF type = system.longintType THEN type := system.hugeintType
  5267. ELSIF type = system.integerType THEN type := system.longintType
  5268. ELSIF type = system.shortintType THEN type := system.integerType
  5269. ELSE
  5270. CASE type.sizeInBits OF
  5271. 8: type := Global.Integer16
  5272. |16: type := Global.Integer32
  5273. |32: type := Global.Integer64
  5274. END;
  5275. END;
  5276. ELSIF type IS SyntaxTree.FloatType THEN
  5277. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5278. ELSIF type= system.realType THEN type := system.longrealType
  5279. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5280. END;
  5281. ELSIF type IS SyntaxTree.ComplexType THEN
  5282. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5283. ELSIF type = system.complexType THEN type := system.longcomplexType
  5284. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5285. END;
  5286. ELSE
  5287. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5288. END;
  5289. IF (parameter0.resolved # NIL) THEN
  5290. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5291. IF parameter0 IS SyntaxTree.Value THEN
  5292. result.SetResolved(parameter0(SyntaxTree.Value));
  5293. END;
  5294. END;
  5295. (* ---- SIZE OF ----- *)
  5296. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5297. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5298. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5299. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5300. type := system.integerType;
  5301. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5302. ELSE
  5303. (* for variables, system sizeof could represent the physically occupied size
  5304. determined via the type descriptor, implement that ? *)
  5305. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5306. END
  5307. (* ---- SYSTEM.TRACE -----*)
  5308. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5309. FOR i := 0 TO numberActualParameters-1 DO
  5310. parameter0 := actualParameters.GetExpression(i);
  5311. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5312. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5313. END;
  5314. END;
  5315. (* remaining issues can only be tested in backend *)
  5316. (* ---- ADDRESSOF----- *)
  5317. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5318. IF HasAddress(parameter0) THEN
  5319. type := system.addressType;
  5320. ELSE
  5321. type := SyntaxTree.invalidType;
  5322. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5323. END;
  5324. (* ---- BIT ----- *)
  5325. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5326. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5327. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5328. actualParameters.SetExpression(0,parameter0);
  5329. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5330. actualParameters.SetExpression(1,parameter1);
  5331. END;
  5332. type := system.booleanType;
  5333. (* ----- MSK ---- *)
  5334. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5335. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5336. ConvertOperands(parameter0,parameter1);
  5337. actualParameters.SetExpression(0,parameter0);
  5338. actualParameters.SetExpression(1,parameter1);
  5339. END;
  5340. type := parameter0.type;
  5341. (* ---- SYSTEM.GET64 ----- *)
  5342. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5343. IF CheckAddressType(parameter0) THEN
  5344. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5345. actualParameters.SetExpression(0,parameter0);
  5346. END;
  5347. type := system.hugeintType;
  5348. (* ---- SYSTEM.GET32 ----- *)
  5349. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5350. IF CheckAddressType(parameter0) THEN
  5351. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5352. actualParameters.SetExpression(0,parameter0);
  5353. END;
  5354. type := system.longintType;
  5355. (* ---- SYSTEM.GET16 ----- *)
  5356. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5357. IF CheckAddressType(parameter0) THEN
  5358. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5359. actualParameters.SetExpression(0,parameter0);
  5360. END;
  5361. type := system.integerType;
  5362. (* ---- SYSTEM.GET8 ----- *)
  5363. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5364. IF CheckAddressType(parameter0) THEN
  5365. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5366. actualParameters.SetExpression(0,parameter0);
  5367. END;
  5368. type := system.shortintType;
  5369. (* ---- SYSTEM.GetStackPointer ----- *)
  5370. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5371. type := system.addressType;
  5372. (* ---- SYSTEM.GetFramePointer ----- *)
  5373. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5374. type := system.addressType;
  5375. (* ---- SYSTEM.GetActivity ----- *)
  5376. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5377. type := system.objectType;
  5378. (* ---- SYSTEM.SetStackPointer ----- *)
  5379. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5380. IF CheckAddressType(parameter0) THEN
  5381. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5382. actualParameters.SetExpression(0,parameter0);
  5383. END;
  5384. (* ---- SYSTEM.SetFramePointer ----- *)
  5385. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5386. IF CheckAddressType(parameter0) THEN
  5387. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5388. actualParameters.SetExpression(0,parameter0);
  5389. END;
  5390. (* ---- SYSTEM.SetActivity ----- *)
  5391. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5392. IF CheckObjectType(parameter0) THEN
  5393. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5394. actualParameters.SetExpression(0,parameter0);
  5395. END;
  5396. (* ---- LSH, LSL, ROT, ROR ----- *)
  5397. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5398. type := type0;
  5399. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5400. actualParameters.SetExpression(1, parameter1);
  5401. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5402. IF id = Global.Lsh THEN
  5403. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5404. ELSIF id = Global.Rot THEN
  5405. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5406. ELSIF id = Global.Ror THEN
  5407. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5408. END;
  5409. END;
  5410. (* ---- SYSTEM.VAL ----- *)
  5411. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5412. IF CheckTypeDeclarationType(parameter0) THEN
  5413. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5414. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5415. result := SyntaxTree.invalidExpression;
  5416. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5417. ELSE
  5418. IF (parameter1.resolved # NIL) THEN
  5419. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5420. IF parameter0 IS SyntaxTree.Value THEN
  5421. result.SetResolved(parameter0(SyntaxTree.Value));
  5422. END;
  5423. END;
  5424. result.SetAssignable(parameter1.assignable);
  5425. END;
  5426. END;
  5427. (* ---- SYSTEM.GET ----- *)
  5428. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5429. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5430. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5431. actualParameters.SetExpression(0,parameter0);
  5432. END;
  5433. (* ---- SYSTEM.PUT ----- *)
  5434. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5435. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5436. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5437. actualParameters.SetExpression(0,parameter0);
  5438. END;
  5439. (* ---- SYSTEM.PUT64 ----- *)
  5440. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5441. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5442. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5443. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5444. actualParameters.SetExpression(0,parameter0);
  5445. actualParameters.SetExpression(1,parameter1);
  5446. END;
  5447. (* ---- SYSTEM.PUT32 ----- *)
  5448. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5449. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5450. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5451. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5452. actualParameters.SetExpression(0,parameter0);
  5453. actualParameters.SetExpression(1,parameter1);
  5454. END;
  5455. (* ---- SYSTEM.PUT16 ----- *)
  5456. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5457. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5458. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5459. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5460. actualParameters.SetExpression(0,parameter0);
  5461. actualParameters.SetExpression(1,parameter1);
  5462. END;
  5463. (* ---- SYSTEM.PUT8 ----- *)
  5464. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5465. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5466. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5467. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5468. actualParameters.SetExpression(0,parameter0);
  5469. actualParameters.SetExpression(1,parameter1);
  5470. END;
  5471. (* ---- SYSTEM.MOVE ----- *)
  5472. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5473. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5474. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5475. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5476. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. actualParameters.SetExpression(1,parameter1);
  5479. actualParameters.SetExpression(2,parameter2);
  5480. END;
  5481. (* ---- SYSTEM.NEW ----- *)
  5482. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5483. IF ~IsPointerType(parameter0.type) THEN
  5484. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5485. ELSIF CheckSizeType(parameter1) THEN
  5486. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5487. actualParameters.SetExpression(1,parameter1);
  5488. END;
  5489. (* ----SYSTEM.REF ---- *)
  5490. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5491. type := system.addressType
  5492. (* ---- INCR ----- *)
  5493. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5494. type := system.sizeType;
  5495. base := type0;
  5496. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5497. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5498. IF i1 < 0 THEN
  5499. Error(position,Diagnostics.Invalid,"invalid dimension");
  5500. base := SyntaxTree.invalidType;
  5501. ELSE
  5502. base := ArrayBase(base,i1);
  5503. IF (base # NIL) & Indexable(base) THEN
  5504. ELSE
  5505. Error(position,Diagnostics.Invalid,"len on no array");
  5506. IF VerboseErrorMessage THEN
  5507. Printout.Info("base",base);
  5508. END;
  5509. base := SyntaxTree.invalidType;
  5510. END;
  5511. END;
  5512. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5513. actualParameters.SetExpression(1,parameter1);
  5514. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5515. mathArrayType := base(SyntaxTree.MathArrayType);
  5516. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5517. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5518. type := system.longintType;
  5519. END;
  5520. END;
  5521. ELSE
  5522. type := system.longintType;
  5523. END;
  5524. (* ---- SUM ----- *)
  5525. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5526. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5527. (* ---- ALL ----- *)
  5528. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5529. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5530. (* ---- DIM ----- *)
  5531. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5532. type := system.sizeType;
  5533. IF type0 IS SyntaxTree.MathArrayType THEN
  5534. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5535. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5536. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5537. END;
  5538. ELSE
  5539. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5540. END;
  5541. (* ---- CAS ----- *)
  5542. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5543. IF type0.IsComposite () THEN
  5544. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5545. result := SyntaxTree.invalidExpression;
  5546. ELSIF ~IsVariable (parameter0) THEN
  5547. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5548. result := SyntaxTree.invalidExpression;
  5549. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5550. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5551. result := SyntaxTree.invalidExpression;
  5552. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5553. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5554. result := SyntaxTree.invalidExpression;
  5555. ELSE
  5556. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5557. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5558. type := type0;
  5559. END;
  5560. (* ---- RESHAPE ----- *)
  5561. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5562. IF type0 IS SyntaxTree.MathArrayType THEN
  5563. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5564. base := ArrayBase(type0,MAX(LONGINT));
  5565. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5566. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5567. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5568. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5569. IF ~CompatibleTo(system,type0,parameterType) THEN
  5570. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5571. result := SyntaxTree.invalidExpression;
  5572. ELSE
  5573. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5574. END;
  5575. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5576. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5577. IF ~CompatibleTo(system,type1,parameterType) THEN
  5578. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5579. result := SyntaxTree.invalidExpression;
  5580. ELSE
  5581. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5582. END;
  5583. ELSE
  5584. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5585. result := SyntaxTree.invalidExpression;
  5586. END;
  5587. (* ---- SYSTEM.TYPECODE ----- *)
  5588. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5589. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5590. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5591. type := type.resolved;
  5592. IF type IS SyntaxTree.PointerType THEN
  5593. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5594. END;
  5595. IF ~(type IS SyntaxTree.RecordType) THEN
  5596. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5597. END;
  5598. ELSE
  5599. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5600. END;
  5601. type := system.addressType;
  5602. (* -------- FLT --------- *)
  5603. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5604. type := system.realType;
  5605. IF IsRealValue(parameter0, r) THEN
  5606. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5607. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5608. i0 := i; i := ABS(i);
  5609. IF i # 0 THEN
  5610. i1 := 23;
  5611. IF i >= 2*800000H THEN
  5612. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5613. ELSIF i < 800000H THEN
  5614. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5615. END;
  5616. i := (i1 + 127)*800000H - 800000H + i;
  5617. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5618. END;
  5619. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5620. END;
  5621. (* ------- CONNECT -------*)
  5622. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5623. IF ~(currentIsCellNet) THEN
  5624. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5625. END;
  5626. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5627. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5628. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5629. END;
  5630. IF numberActualParameters = 3 THEN
  5631. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5632. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5633. END;
  5634. *)
  5635. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5636. actualParameters.SetExpression(2,parameter2);
  5637. END;
  5638. activeCellsStatement := TRUE;
  5639. (* ---------- DELEGATE --------*)
  5640. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5641. IF ~(currentIsCellNet) THEN
  5642. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5643. END;
  5644. IF ~CheckPortType(parameter1, inPort) THEN
  5645. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5646. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5647. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5648. ELSIF (outPort.direction # inPort.direction) THEN
  5649. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5650. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5651. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5652. END;
  5653. activeCellsStatement := TRUE;
  5654. (* --------- RECEIVE ---------*)
  5655. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5656. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5657. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5658. IF inPort.direction # SyntaxTree.InPort THEN
  5659. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5660. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5661. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5662. END;
  5663. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5664. IF ~SameType(parameter2.type, system.integerType) THEN
  5665. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5666. END;
  5667. END;
  5668. END;
  5669. (* --------- SEND ---------*)
  5670. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5671. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5672. IF CheckPortType(parameter0,outPort) THEN
  5673. IF outPort.direction # SyntaxTree.OutPort THEN
  5674. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5675. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5676. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5677. ELSE
  5678. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5679. actualParameters.SetExpression(1,parameter1);
  5680. END;
  5681. END;
  5682. (* ------- custom builtins ----- *)
  5683. ELSIF id = Global.systemSpecial THEN
  5684. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5685. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5686. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5687. type := procedureType.returnType;
  5688. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5689. (* go through all formal parameters *)
  5690. formalParameter := procedureType.firstParameter;
  5691. FOR i := 0 TO actualParameters.Length() - 1 DO
  5692. actualParameter := actualParameters.GetExpression(i);
  5693. IF actualParameter = SyntaxTree.invalidExpression THEN
  5694. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5695. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5696. ELSE
  5697. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5698. END;
  5699. actualParameters.SetExpression(i, actualParameter);
  5700. formalParameter := formalParameter.nextParameter
  5701. END
  5702. END
  5703. ELSE
  5704. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5705. result := SyntaxTree.invalidExpression;
  5706. END;
  5707. END;
  5708. IF result # SyntaxTree.invalidExpression THEN
  5709. type := ResolveType(type);
  5710. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5711. result.SetType(type);
  5712. END;
  5713. RETURN result
  5714. END NewBuiltinCallDesignator;
  5715. (** return type guard designator left(type)
  5716. - check if type can be extended (i.e. is no static record)
  5717. - check if type is a type extension of left.type
  5718. - returns new type guard designator
  5719. returns invalidDesignator = invalidExpression if error
  5720. **)
  5721. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5722. VAR result: SyntaxTree.Designator;
  5723. BEGIN
  5724. result := SyntaxTree.invalidDesignator;
  5725. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5726. Error(position,Diagnostics.Invalid,"no type extension of type");
  5727. IF VerboseErrorMessage THEN
  5728. Printout.Info("left",left);
  5729. Printout.Info("type",type);
  5730. END;
  5731. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5732. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5733. ELSIF IsUnsafePointer(left.type) THEN
  5734. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5735. ELSE
  5736. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5737. result.SetType(type);
  5738. result.SetAssignable(left.assignable);
  5739. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5740. END;
  5741. RETURN result
  5742. END NewTypeGuardDesignator;
  5743. (** check and resolve parameter designator left(expression list)
  5744. - check expression list
  5745. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5746. - elsif left is a procedure type then
  5747. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5748. - else return is a procedure call then return ProcedureCallDesignator
  5749. returns invalidDesignator = invalidExpression if error
  5750. **)
  5751. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5752. VAR
  5753. parameters: SyntaxTree.ExpressionList;
  5754. left: SyntaxTree.Designator;
  5755. result,expression: SyntaxTree.Expression;
  5756. typeDeclaration: SyntaxTree.TypeDeclaration;
  5757. type, expressionType: SyntaxTree.Type;
  5758. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5759. BEGIN
  5760. type := type.resolved;
  5761. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5762. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5763. END;
  5764. RETURN type
  5765. END BaseType;
  5766. BEGIN
  5767. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5768. result := SyntaxTree.invalidDesignator;
  5769. left := ResolveDesignator(designator.left);
  5770. IF left # SyntaxTree.invalidDesignator THEN
  5771. parameters := designator.parameters;
  5772. IF ExpressionList(parameters) THEN
  5773. IF (left.type = NIL) THEN
  5774. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5775. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5776. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5777. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5778. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5779. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5780. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5781. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5782. ELSE
  5783. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5784. END
  5785. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5786. expression := parameters.GetExpression(0);
  5787. type := typeDeclaration.declaredType.resolved;
  5788. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5789. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5790. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5791. OR (expressionType IS SyntaxTree.EnumerationType)
  5792. ) THEN
  5793. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5794. ELSE
  5795. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5796. END;
  5797. ELSE
  5798. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5799. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5800. result := SyntaxTree.invalidDesignator;
  5801. END;
  5802. ELSE
  5803. result := SyntaxTree.invalidDesignator
  5804. END;
  5805. END;
  5806. resolvedExpression := result;
  5807. END VisitParameterDesignator;
  5808. (** check dereference designator left^
  5809. - check if left is pointer type or left is object type
  5810. - return new dereference designator with type = left.baseType.type (if appropriate)
  5811. with error handling
  5812. returns invalidDesignator = invalidExpression if error
  5813. **)
  5814. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5815. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5816. BEGIN
  5817. result := SyntaxTree.invalidDesignator;
  5818. type := left.type;
  5819. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5820. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5821. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5822. result.SetAssignable(TRUE);
  5823. result.SetType(type);
  5824. result.SetHidden(left.isHidden);
  5825. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5826. type := type.resolved;
  5827. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5828. result.SetAssignable(TRUE);
  5829. result.SetType(type);
  5830. result.SetHidden(left.isHidden);
  5831. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5832. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5833. result.SetAssignable(TRUE);
  5834. result.SetType(type);
  5835. result.SetHidden(left.isHidden);
  5836. ELSE
  5837. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5838. IF VerboseErrorMessage THEN
  5839. Printout.Info("pointer", type);
  5840. Printout.Info("scope", currentScope);
  5841. END;
  5842. END;
  5843. RETURN result
  5844. END NewDereferenceDesignator;
  5845. (** check supercall designator left^
  5846. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5847. - return new supercall designator with type = left.type
  5848. with error handling
  5849. **)
  5850. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5851. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5852. objectScope: SyntaxTree.Scope;
  5853. BEGIN
  5854. result := SyntaxTree.invalidDesignator;
  5855. IF left = SyntaxTree.invalidDesignator THEN
  5856. (* error already handled *)
  5857. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5858. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5859. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5860. IF symbol IS SyntaxTree.Procedure THEN
  5861. procedure := symbol(SyntaxTree.Procedure);
  5862. objectScope := currentScope;
  5863. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5864. objectScope := objectScope.outerScope;
  5865. END;
  5866. IF (left.left = NIL) OR ~
  5867. (
  5868. (left.left IS SyntaxTree.SelfDesignator) OR
  5869. (left.left IS SyntaxTree.DereferenceDesignator)
  5870. & (left.left(SyntaxTree.Designator).left # NIL)
  5871. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5872. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5873. IF VerboseErrorMessage THEN
  5874. Printout.Info("left.left",left.left);
  5875. END;
  5876. ELSIF procedure.super # NIL THEN
  5877. result := SyntaxTree.NewSupercallDesignator(position,left);
  5878. result.SetType(left.type.resolved)
  5879. ELSE
  5880. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5881. END;
  5882. ELSE
  5883. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5884. END;
  5885. ELSE
  5886. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5887. END;
  5888. RETURN result
  5889. END NewSupercallDesignator;
  5890. (** check and semantically resolve arrow designator left^
  5891. - if left is procedure type -> result := SupercallDesignator
  5892. - else result := DereferenceDesignator
  5893. returns result via global variable resolvedExpression
  5894. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5895. **)
  5896. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5897. VAR left: SyntaxTree.Designator;
  5898. BEGIN
  5899. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5900. left := ResolveDesignator(arrowDesignator.left);
  5901. IF left # NIL THEN
  5902. IF (left.type = NIL) THEN
  5903. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5904. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5905. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5906. ELSE
  5907. IF IsPointerToObject(left.type) THEN
  5908. Warning(arrowDesignator.position, "forbidden dereference on object");
  5909. END;
  5910. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5911. END
  5912. END
  5913. END VisitArrowDesignator;
  5914. (** check and return expression
  5915. - if expression has no type then resolve expression
  5916. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5917. - return result
  5918. **)
  5919. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5920. VAR result,prev: SyntaxTree.Expression;
  5921. BEGIN
  5922. IF expression = NIL THEN result := NIL
  5923. ELSIF (expression.type = NIL) THEN
  5924. prev := resolvedExpression;
  5925. resolvedExpression := SyntaxTree.invalidExpression;
  5926. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5927. expression.SetType(SyntaxTree.invalidType);
  5928. END;
  5929. expression.Accept(SELF);
  5930. result := resolvedExpression;
  5931. IF currentIsRealtime THEN
  5932. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5933. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5934. END;
  5935. END;
  5936. (* designator modifiers for backends if they support it ...*)
  5937. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5938. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5939. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5940. END;
  5941. resolvedExpression := prev
  5942. ELSE
  5943. result := expression
  5944. END;
  5945. RETURN result
  5946. END ResolveExpression;
  5947. (**
  5948. check expression to be constant expression
  5949. - resolve expression
  5950. - if valid then check that of value type
  5951. report error and return invalidExpression if anything fails
  5952. **)
  5953. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5954. VAR position: LONGINT;
  5955. BEGIN
  5956. position := expression.position;
  5957. expression := ResolveExpression(expression);
  5958. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5959. ELSIF (expression.resolved = NIL) THEN
  5960. Error(position,Diagnostics.Invalid,"expression is not constant");
  5961. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5962. expression := SyntaxTree.invalidExpression;
  5963. END;
  5964. RETURN expression
  5965. END ConstantExpression;
  5966. (** check expression to be constant integer
  5967. - resolve expresssion
  5968. - if valid then check that of integer value type
  5969. report error and return invalidExpression if anything fails
  5970. **)
  5971. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5972. VAR position: LONGINT;
  5973. BEGIN
  5974. position := expression.position;
  5975. expression := ResolveExpression(expression);
  5976. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5977. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5978. expression := SyntaxTree.invalidExpression;
  5979. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5980. END;
  5981. RETURN expression
  5982. END ConstantInteger;
  5983. (** check expression as positive (>=0) constant integer
  5984. - resolve expression
  5985. - if valid then check that integer value
  5986. - if integer value then check that value >= 0
  5987. report error and return invalidExpression if anything fails
  5988. **)
  5989. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5990. VAR position: LONGINT;
  5991. BEGIN
  5992. position := expression.position;
  5993. expression := ConstantExpression(expression);
  5994. IF expression = SyntaxTree.invalidExpression THEN
  5995. (* error already reported *)
  5996. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5997. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5998. expression := SyntaxTree.invalidExpression
  5999. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6000. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6001. END;
  6002. RETURN expression
  6003. END ConstantIntegerGeq0;
  6004. (** check expression as condition
  6005. - resolve expression
  6006. - if valid expression then check that result type is boolean
  6007. report error and return invalidExpression if anything fails
  6008. **)
  6009. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6010. VAR position: LONGINT;
  6011. BEGIN
  6012. position := expression.position;
  6013. expression := ResolveExpression(expression);
  6014. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6015. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6016. expression := SyntaxTree.invalidExpression;
  6017. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6018. END;
  6019. RETURN expression
  6020. END ResolveCondition;
  6021. (*** symbols ***)
  6022. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6023. BEGIN
  6024. x.Accept(SELF);
  6025. END ResolveSymbol;
  6026. (** check a symbol
  6027. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6028. **)
  6029. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6030. VAR scope: SyntaxTree.Scope;
  6031. BEGIN
  6032. (* visibility *)
  6033. scope := symbol.scope;
  6034. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6035. scope := scope.outerScope;
  6036. END;
  6037. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6038. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6039. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6040. IF VerboseErrorMessage THEN
  6041. Printout.Info("symbol",symbol);
  6042. END;
  6043. END;
  6044. END;
  6045. END CheckSymbolVisibility;
  6046. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6047. If node is currently being resolved then emit a cyclic definition error.
  6048. Return TRUE only if node is fully resolved.
  6049. **)
  6050. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6051. VAR result: BOOLEAN;
  6052. BEGIN
  6053. IF SyntaxTree.Resolved IN x.state THEN
  6054. result := FALSE
  6055. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6056. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6057. result := FALSE;
  6058. ELSE
  6059. result := TRUE;
  6060. x.SetState(SyntaxTree.BeingResolved)
  6061. END;
  6062. RETURN result
  6063. END SymbolNeedsResolution;
  6064. (** check and resolve a type declaration symbol = Type
  6065. - set type to declaration type
  6066. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6067. This is so because the type declaration itself does not have a type but it only stands for a type.
  6068. In the implementation of the compiler this made a lot much easier.
  6069. - resolve and set declared type
  6070. - check symbol
  6071. **)
  6072. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6073. VAR prevScope: SyntaxTree.Scope;
  6074. BEGIN
  6075. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6076. IF SymbolNeedsResolution(typeDeclaration) THEN
  6077. prevScope := currentScope;
  6078. currentScope := typeDeclaration.scope;
  6079. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6080. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6081. CheckSymbolVisibility(typeDeclaration);
  6082. typeDeclaration.SetState(SyntaxTree.Resolved);
  6083. currentScope := prevScope;
  6084. END;
  6085. END VisitTypeDeclaration;
  6086. (** check and resolve a constant declaration symbol = (constant) expression
  6087. - check expression
  6088. - set type and value
  6089. - check symbol
  6090. **)
  6091. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6092. VAR
  6093. expression: SyntaxTree.Expression;
  6094. type: SyntaxTree.Type;
  6095. name: Basic.SegmentedName;
  6096. replacement: Replacement;
  6097. BEGIN
  6098. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6099. IF SymbolNeedsResolution(constant) THEN
  6100. expression := constant.value;
  6101. IF replacements # NIL THEN
  6102. Global.GetSymbolSegmentedName(constant, name);
  6103. replacement := replacements;
  6104. WHILE (replacement # NIL) & (replacement.name # name) DO
  6105. replacement := replacement.next;
  6106. END;
  6107. IF replacement # NIL THEN
  6108. InfoSS(constant.position, "replacing constant", constant.name);
  6109. (*
  6110. NEW(stringReader, Strings.Length(replacement.string^));
  6111. stringReader.Set(replacement.string^);
  6112. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6113. NEW(parser, scanner, diagnostics);
  6114. expression := parser.Expression();
  6115. *)
  6116. expression := replacement.expression;
  6117. replacement.used := TRUE;
  6118. END;
  6119. END;
  6120. constant.SetType(SyntaxTree.invalidType);
  6121. expression := ConstantExpression(expression);
  6122. ASSERT(expression.type # NIL);
  6123. type := expression.type.resolved;
  6124. constant.SetType(type);
  6125. constant.SetValue(expression);
  6126. CheckSymbolVisibility(constant);
  6127. constant.SetState(SyntaxTree.Resolved);
  6128. END;
  6129. END VisitConstant;
  6130. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6131. VAR procedureAlignment: LONGINT;
  6132. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6133. (* least common multiple *)
  6134. VAR a,b: LONGINT;
  6135. BEGIN
  6136. a := a0; b := b0;
  6137. WHILE (a # b) DO
  6138. IF a < b THEN a := a+a0
  6139. ELSE b := b + b0
  6140. END;
  6141. END;
  6142. RETURN a
  6143. END LCM;
  6144. BEGIN
  6145. IF alignment > 1 THEN
  6146. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6147. IF (procedureAlignment > 1) THEN
  6148. alignment := LCM(alignment, procedureAlignment);
  6149. END;
  6150. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6151. END;
  6152. END AdaptStackAlignment;
  6153. (** check and resolve a variable / field
  6154. - check and set type
  6155. - negative check on open array type
  6156. - check symbol
  6157. **)
  6158. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6159. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6160. BEGIN
  6161. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6162. IF SymbolNeedsResolution(variable) THEN
  6163. modifiers := variable.modifiers;
  6164. (*
  6165. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6166. variable.AddFlags(flags);
  6167. *)
  6168. variable.SetType(ResolveType(variable.type));
  6169. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6170. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6171. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6172. END;
  6173. END;
  6174. CheckSymbolVisibility(variable);
  6175. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6176. variable.SetUntraced(TRUE);
  6177. IF ~ContainsPointer(variable.type) THEN
  6178. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6179. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6180. END;
  6181. END;
  6182. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6183. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6184. IF ~PowerOf2(value) THEN
  6185. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6186. ELSE
  6187. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6188. END;
  6189. END;
  6190. variable.SetAlignment(FALSE,value);
  6191. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6192. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6193. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6194. END;
  6195. variable.SetAlignment(TRUE, value);
  6196. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6197. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6198. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6199. END;
  6200. variable.SetFictive(value);
  6201. variable.SetOffset(value*system.dataUnit);
  6202. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6203. END;
  6204. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6205. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6206. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6207. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6208. END;
  6209. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6210. IF variable.initializer # NIL THEN
  6211. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6212. END;
  6213. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6214. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6215. pointerType.SetPointerBase(variable.type);
  6216. pointerType.SetHidden(TRUE);
  6217. variable.SetType(ResolveType(pointerType));
  6218. END;
  6219. variable.SetState(SyntaxTree.Resolved);
  6220. END;
  6221. END VisitVariable;
  6222. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6223. BEGIN
  6224. VisitVariable(property)
  6225. END VisitProperty;
  6226. (** check and resolve a (procedure) parameter
  6227. - check and set type
  6228. - check symbol
  6229. - check parameter kind and set read-only flags if appropriate
  6230. **)
  6231. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6232. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6233. BEGIN
  6234. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6235. IF SymbolNeedsResolution(parameter) THEN
  6236. modifiers := parameter.modifiers;
  6237. parameter.SetType(ResolveType(parameter.type));
  6238. ASSERT(parameter.type.resolved # NIL);
  6239. CheckSymbolVisibility(parameter);
  6240. IF parameter.defaultValue # NIL THEN
  6241. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6242. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6243. ELSE
  6244. expression := ConstantExpression(parameter.defaultValue);
  6245. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6246. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6247. parameter.SetDefaultValue(expression);
  6248. END;
  6249. END;
  6250. END;
  6251. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6252. parameter.SetUntraced(TRUE);
  6253. IF ~ContainsPointer(parameter.type) THEN
  6254. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6255. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6256. END;
  6257. END;
  6258. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6259. parameter.SetMoveable(TRUE);
  6260. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6261. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6262. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6263. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6264. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6265. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6266. END;
  6267. END;
  6268. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6269. parameter.SetState(SyntaxTree.Resolved);
  6270. END;
  6271. END VisitParameter;
  6272. (** check and resolve a procedure (with declaration and implementation scope)
  6273. - check the procedure type
  6274. - check if method (i.e. in record scope), if so then
  6275. - check if (unique) constructor
  6276. - check if (unique) finalizer
  6277. - check if super method available, if so then check signature
  6278. - of not in record scope then negative check on constructor flag
  6279. - of not in record scope then negative check on finalizer flag
  6280. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6281. - check procedure symbol
  6282. **)
  6283. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6284. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6285. procedureType: SyntaxTree.ProcedureType;
  6286. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6287. qualifiedType: SyntaxTree.QualifiedType;
  6288. value: LONGINT;
  6289. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6290. position: LONGINT;
  6291. BEGIN
  6292. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6293. IF IsOberonInline(procedure) THEN
  6294. IF SyntaxTree.Public * procedure.access # {} THEN
  6295. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6296. END;
  6297. procedure.SetInline(FALSE);
  6298. procedure.SetOberonInline(TRUE);
  6299. END;
  6300. IF SymbolNeedsResolution(procedure) THEN
  6301. recentIsRealtime := currentIsRealtime;
  6302. recentIsBodyProcedure := currentIsBodyProcedure;
  6303. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6304. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6305. modifiers := procedureType.modifiers;
  6306. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6307. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6308. IF useDarwinCCalls THEN (*fld*)
  6309. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6310. ELSE
  6311. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6312. END
  6313. END;
  6314. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6315. procedureType.SetInterrupt(TRUE);
  6316. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6317. END;
  6318. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6319. procedureType.SetNoReturn(TRUE);
  6320. END;
  6321. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6322. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6323. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6324. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6325. END;
  6326. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6327. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6328. END;
  6329. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6330. IF ~PowerOf2(value) THEN
  6331. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6332. ELSE
  6333. procedureType.SetStackAlignment(value)
  6334. END;
  6335. END;
  6336. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6337. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6338. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6339. END;
  6340. CheckModifiers(modifiers, TRUE);
  6341. modifiers := procedureType.returnTypeModifiers;
  6342. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6343. CheckModifiers(modifiers, TRUE);
  6344. FixProcedureType(procedureType);
  6345. currentIsRealtime := procedureType.isRealtime;
  6346. currentIsBodyProcedure := procedure.isBodyProcedure;
  6347. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6348. THEN
  6349. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6350. END;
  6351. CheckSymbolVisibility(procedure);
  6352. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6353. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6354. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6355. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6356. END;
  6357. END;
  6358. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6359. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6360. procedureType.SetDelegate(TRUE);
  6361. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6362. IF record.pointerType.typeDeclaration = NIL THEN
  6363. selfParameter.SetType(record.pointerType);
  6364. ELSE
  6365. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6366. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6367. qualifiedType.SetResolved(record.pointerType);
  6368. selfParameter.SetType(qualifiedType);
  6369. END;
  6370. selfParameter.SetAccess(SyntaxTree.Hidden);
  6371. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6372. IF procedure.isConstructor THEN
  6373. (*! constructor is always visible, compatibility to paco
  6374. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6375. *)
  6376. procedure.MarkUsed;
  6377. IF procedureType.returnType # NIL THEN
  6378. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6379. END;
  6380. proc := procedure.scope.firstProcedure;
  6381. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6382. proc := proc.nextProcedure;
  6383. END;
  6384. IF proc # NIL THEN
  6385. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6386. ELSE
  6387. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6388. END;
  6389. END;
  6390. IF procedure.isFinalizer THEN
  6391. procedure.MarkUsed;
  6392. IF procedureType.returnType # NIL THEN
  6393. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6394. END;
  6395. IF procedureType.numberParameters # 0 THEN
  6396. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6397. END;
  6398. proc := procedure.scope.firstProcedure;
  6399. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6400. proc := proc.nextProcedure;
  6401. END;
  6402. IF proc # NIL THEN
  6403. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6404. ELSE
  6405. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6406. END;
  6407. END;
  6408. super := FindSuperProcedure(record.recordScope, procedure);
  6409. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6410. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6411. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6412. END;
  6413. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6414. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6415. END;
  6416. IF super.isFinal THEN
  6417. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6418. END;
  6419. procedure.SetSuper(super);
  6420. super.SetOverwritten(TRUE);
  6421. procedure.SetAccess(procedure.access+super.access);
  6422. procedure.MarkUsed;
  6423. END;
  6424. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6425. THEN
  6426. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6427. END;
  6428. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6429. IF cellsAreObjects THEN
  6430. procedureType.SetDelegate(TRUE);
  6431. END;
  6432. IF procedure.isConstructor THEN
  6433. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6434. END;
  6435. ELSIF procedure.isConstructor THEN
  6436. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6437. END;
  6438. Declarations(procedure.procedureScope);
  6439. (* body resolution part done as late fix of the procedure type *)
  6440. procedure.SetState(SyntaxTree.Resolved);
  6441. currentIsRealtime := recentIsRealtime;
  6442. currentIsBodyProcedure := recentIsBodyProcedure;
  6443. END;
  6444. END VisitProcedure;
  6445. (**
  6446. a builtin procedure is a global item that may not be modified locally
  6447. instead the resolving of builtin procedure calls are done in the esignator
  6448. **)
  6449. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6450. VAR type: SyntaxTree.Type;
  6451. BEGIN
  6452. type := ResolveType(builtinProcedure.type);
  6453. END VisitBuiltin;
  6454. (* nopov *)
  6455. (** check and resolve operator
  6456. - operators are first checked as procedures
  6457. - then additional operator-specific checks are done
  6458. - note that only module-scope operators are checked here
  6459. (operators in a record scope are only allowed in the context of
  6460. array-structured object types and checked in 'ResolveArrayStructure')
  6461. - also note that inter-operator conformity is not checked here
  6462. **)
  6463. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6464. VAR
  6465. procedureType: SyntaxTree.ProcedureType;
  6466. leftType, rightType: SyntaxTree.Type;
  6467. identifierNumber, position: LONGINT;
  6468. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6469. modifiers: SyntaxTree.Modifier;
  6470. (** whether a type is locally defined in the current module scope
  6471. for arrays, the base type must be locally defined **)
  6472. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6473. BEGIN
  6474. IF type = NIL THEN
  6475. RETURN FALSE
  6476. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6477. RETURN TRUE
  6478. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6479. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6480. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6481. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6482. ELSE
  6483. RETURN FALSE
  6484. END
  6485. END IsLocallyDefined;
  6486. BEGIN
  6487. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6488. procedureType := operator.type(SyntaxTree.ProcedureType);
  6489. modifiers := procedureType.modifiers;
  6490. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6491. CheckModifiers(modifiers, TRUE);
  6492. VisitProcedure(operator);
  6493. IF operator.scope IS SyntaxTree.RecordScope THEN
  6494. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6495. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6496. IF identifierNumber = -1 THEN
  6497. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6498. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6499. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6500. ELSE
  6501. IF procedureType.numberParameters < 1 THEN
  6502. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6503. ELSIF procedureType.numberParameters > 2 THEN
  6504. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6505. ELSE
  6506. (* determine operand types *)
  6507. leftType := procedureType.firstParameter.type;
  6508. IF procedureType.numberParameters > 1 THEN
  6509. rightType := procedureType.firstParameter.nextParameter.type
  6510. ELSE
  6511. rightType := NIL
  6512. END;
  6513. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6514. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6515. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6516. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6517. END
  6518. END;
  6519. (* TODO: refine the checks, think about how restrictive the checks should be
  6520. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6521. They might be used for intersection, union, complement of custom object types *)
  6522. (* defaults *)
  6523. hasReturnType := TRUE;
  6524. mustBeUnary := FALSE;
  6525. mustBeBinary := FALSE;
  6526. mustReturnBoolean := FALSE;
  6527. mustReturnInteger := FALSE;
  6528. mustHaveEquitypedOperands := FALSE;
  6529. (* operator-specific exceptions *)
  6530. CASE identifierNumber OF
  6531. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6532. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6533. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6534. mustBeBinary := TRUE
  6535. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6536. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6537. | Scanner.Times: mustBeBinary := TRUE
  6538. | Scanner.TimesTimes: mustBeBinary := TRUE
  6539. | Scanner.DotTimes: mustBeBinary := TRUE
  6540. | Scanner.PlusTimes: mustBeBinary := TRUE
  6541. | Scanner.Slash: mustBeBinary := TRUE
  6542. | Scanner.Backslash: mustBeBinary := TRUE
  6543. | Scanner.DotSlash: mustBeBinary := TRUE
  6544. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6545. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6546. | Scanner.Not: mustBeUnary := TRUE
  6547. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6548. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6549. | Scanner.Transpose: mustBeUnary := TRUE;
  6550. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6551. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6552. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6553. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6554. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6555. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6556. | Global.Abs: mustBeUnary := TRUE;
  6557. | Global.Ash: (* TODO: arity? *)
  6558. | Global.Cap: (* TODO: arity? *)
  6559. | Global.Chr: mustBeUnary := TRUE;
  6560. | Global.Entier: (* TODO: arity? *)
  6561. | Global.EntierH: (* TODO: arity? *)
  6562. | Global.Len: (* unary and binary *)
  6563. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6564. | Global.Max, Global.Min: (* unary and binary *)
  6565. | Global.Odd: (* TODO: arity? *)
  6566. | Global.Sum: (* TODO: arity? *)
  6567. | Global.All: (* TODO: arity? *)
  6568. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6569. | Scanner.Alias:
  6570. | Scanner.GreaterGreater, Scanner.LessLess:
  6571. mustBeBinary := TRUE; hasReturnType := FALSE;
  6572. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6573. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6574. END;
  6575. (* check parameter count *)
  6576. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6577. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6578. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6579. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6580. END;
  6581. (* check parameter types *)
  6582. (* TODO: is this used at all? *)
  6583. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6584. leftType := procedureType.firstParameter.type;
  6585. rightType := procedureType.firstParameter.nextParameter.type;
  6586. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6587. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6588. END
  6589. END;
  6590. (* check return type *)
  6591. IF hasReturnType THEN
  6592. IF procedureType.returnType = NIL THEN
  6593. Error(operator.position, Diagnostics.Invalid, "return type required")
  6594. ELSIF mustReturnBoolean THEN
  6595. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6596. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6597. END
  6598. ELSIF mustReturnInteger THEN
  6599. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6600. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6601. END
  6602. END
  6603. ELSIF procedureType.returnType # NIL THEN
  6604. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6605. END
  6606. END
  6607. END
  6608. END
  6609. END VisitOperator;
  6610. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6611. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6612. BEGIN
  6613. IF error THEN RETURN FALSE END;
  6614. prevScope := currentScope;
  6615. prevDiagnostics := diagnostics;
  6616. diagnostics := NIL; (* suppress error output *)
  6617. currentScope := module.moduleScope;
  6618. VisitImport(x);
  6619. IF ~error THEN
  6620. module.moduleScope.AddImport(x);
  6621. x.SetScope(module.moduleScope);
  6622. END;
  6623. currentScope := prevScope;
  6624. diagnostics := prevDiagnostics;
  6625. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6626. END AddImport;
  6627. (** check and resolve import
  6628. - check for name = SYSTEM
  6629. - check for forbidden self import
  6630. - search through global import cache: already imported?
  6631. - check if already imported indirectly
  6632. - import if necessary -> set module and enter into import cache
  6633. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6634. - after this import this direct import and all indirect imports are stored in the current module's import list
  6635. **)
  6636. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6637. VAR
  6638. module: SyntaxTree.Module;
  6639. moduleScope: SyntaxTree.ModuleScope;
  6640. import,reimport: SyntaxTree.Import;
  6641. filename: FileName;
  6642. prevScope: SyntaxTree.Scope;
  6643. BEGIN
  6644. IF SymbolNeedsResolution(x) THEN
  6645. prevScope := currentScope;
  6646. x.SetType(SyntaxTree.importType);
  6647. moduleScope := currentScope.ownerModule.moduleScope;
  6648. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6649. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6650. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6651. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6652. ELSE
  6653. (* search through global import list: already imported ? *)
  6654. IF (x.module = NIL) & (importCache # NIL) THEN
  6655. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6656. ELSE import := NIL
  6657. END;
  6658. IF x.module # NIL THEN (* already imported indirectly *)
  6659. module := x.module;
  6660. ELSIF import # NIL THEN (* already in module list *)
  6661. module := import.module;
  6662. ASSERT(module # NIL);
  6663. x.SetModule(module);
  6664. ELSE (* must be imported *)
  6665. Global.ModuleFileName(x.moduleName,x.context,filename);
  6666. IF symbolFileFormat # NIL THEN
  6667. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6668. IF module = NIL THEN
  6669. ErrorSS(x.position,"could not import",filename);
  6670. IF VerboseErrorMessage THEN
  6671. Printout.Info("import",x)
  6672. END
  6673. ELSE
  6674. (*
  6675. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6676. (*! should rather be done by importer *)
  6677. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6678. checker.importCache := importCache;
  6679. checker.arrayBaseImported := arrayBaseImported;
  6680. checker.global := global;
  6681. checker.Module(module); (* semantic check *)
  6682. error := error OR checker.error;
  6683. END;
  6684. *)
  6685. (*
  6686. ASSERT(SyntaxTree.Resolved IN module.state);
  6687. *)
  6688. x.SetModule(module);
  6689. IF importCache # NIL THEN
  6690. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6691. import.SetContext(x.context);
  6692. import.SetModule(module);
  6693. importCache.AddImport(import);
  6694. END;
  6695. END;
  6696. ELSE
  6697. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6698. END;
  6699. END;
  6700. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6701. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6702. END;
  6703. import := module.moduleScope.firstImport;
  6704. WHILE(import # NIL) DO
  6705. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6706. ASSERT(currentScope # NIL);
  6707. ASSERT(currentScope.ownerModule # NIL);
  6708. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6709. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6710. Error(x.position,Diagnostics.Invalid,"recursive import");
  6711. ELSE
  6712. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6713. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6714. IF reimport = NIL THEN (* indirect import *)
  6715. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6716. reimport.SetContext(import.context);
  6717. reimport.SetModule(import.module);
  6718. moduleScope.AddImport(reimport);
  6719. reimport.SetScope(moduleScope);
  6720. ELSE
  6721. ASSERT(import.module # NIL);
  6722. reimport.SetModule(import.module); (* direct or indirect import *)
  6723. END;
  6724. END;
  6725. import := import.nextImport;
  6726. END;
  6727. END;
  6728. END;
  6729. currentScope := prevScope;
  6730. (* ELSE nothing to be done *)
  6731. x.SetState(SyntaxTree.Resolved);
  6732. END;
  6733. END VisitImport;
  6734. (*** statements ***)
  6735. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6736. VAR prev,resolved: SyntaxTree.Statement;
  6737. BEGIN
  6738. prev := resolvedStatement;
  6739. resolvedStatement := x;
  6740. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6741. activeCellsStatement := FALSE;
  6742. x.Accept(SELF);
  6743. (* removed this, implementation restriction should be resolved by backend
  6744. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6745. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6746. END;
  6747. *)
  6748. resolved := resolvedStatement;
  6749. resolvedStatement := prev;
  6750. RETURN resolved
  6751. END ResolveStatement;
  6752. (** check and resolve statement sequence
  6753. - check all statements, replace if necessary
  6754. **)
  6755. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6756. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6757. BEGIN
  6758. IF statementSequence # NIL THEN (* else empty *)
  6759. FOR i := 0 TO statementSequence.Length()-1 DO
  6760. statement := statementSequence.GetStatement(i);
  6761. resolved := ResolveStatement(statement);
  6762. IF (resolved # statement) THEN
  6763. statementSequence.SetStatement(i,resolved);
  6764. END;
  6765. END;
  6766. END;
  6767. END StatementSequence;
  6768. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6769. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6770. - check if procedure is callable
  6771. - check return type = NIL (otherwise must be assignment statement)
  6772. **)
  6773. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6774. VAR call: SyntaxTree.Designator;
  6775. BEGIN
  6776. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6777. call := procedureCall.call;
  6778. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6779. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6780. END;
  6781. call := ResolveDesignator(call);
  6782. IF call = SyntaxTree.invalidDesignator THEN
  6783. (* error already handled *)
  6784. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6785. (* inline call in a statement *)
  6786. ELSIF ~IsCallable(call) THEN
  6787. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6788. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6789. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6790. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6791. END;
  6792. procedureCall.SetCall(call);
  6793. (*
  6794. IF call = SyntaxTree.invalidDesignator THEN
  6795. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6796. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6797. IF IsOberonInline(procedure) THEN
  6798. Warning(procedure.position,"call to inline proc");
  6799. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6800. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6801. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6802. resolvedStatement := block;
  6803. RETURN;
  6804. END;
  6805. END;
  6806. *)
  6807. END VisitProcedureCallStatement;
  6808. (** check and resolve assignment LHS := RHS
  6809. - resolve LHS and RHS
  6810. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6811. - check if assignment is compatible
  6812. - check if LHS is variable (i.e. assignable)
  6813. - convert RHS if necessary
  6814. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6815. - assignment between different ASOTs
  6816. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6817. - assignment to ASOT elements:
  6818. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6819. **)
  6820. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6821. VAR
  6822. left: SyntaxTree.Designator;
  6823. right, expression: SyntaxTree.Expression;
  6824. designator: SyntaxTree.Designator;
  6825. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6826. mathArrayType: SyntaxTree.MathArrayType;
  6827. BEGIN
  6828. right := ResolveExpression(assignment.right);
  6829. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6830. left := ResolveDesignator(assignment.left);
  6831. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6832. (* error already handled *)
  6833. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6834. (* LHS is index write operator call on ASOT *)
  6835. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6836. (* necessary ?
  6837. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6838. type := procedureType.firstParameter.type;
  6839. expression := procedureCallDesignator.parameters.GetExpression(0);
  6840. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6841. *)
  6842. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6843. ELSIF CheckVariable(left) THEN
  6844. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6845. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6846. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6847. (* conversion done by procedure call
  6848. (* try to convert to left argument *)
  6849. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6850. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6851. procedureCallDesignator.parameters.SetExpression(1, right);
  6852. END;
  6853. *)
  6854. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6855. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6856. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6857. ELSIF AssignmentCompatible(left, right) THEN
  6858. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6859. mathArrayType := MathArrayStructureOfType(left.type);
  6860. right := NewConversion(right.position, right, mathArrayType, NIL);
  6861. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6862. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6863. ELSE
  6864. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6865. assignment.SetLeft(left);
  6866. assignment.SetRight(right);
  6867. resolvedStatement := assignment
  6868. END
  6869. END
  6870. END
  6871. END VisitAssignment;
  6872. (** check and resolve assignment LHS := RHS
  6873. - resolve LHS and RHS
  6874. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6875. - check if assignment is compatible
  6876. - check if LHS is variable (i.e. assignable)
  6877. - convert RHS if necessary
  6878. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6879. - assignment between different ASOTs
  6880. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6881. - assignment to ASOT elements:
  6882. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6883. **)
  6884. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6885. VAR
  6886. left: SyntaxTree.Designator;
  6887. right: SyntaxTree.Expression;
  6888. inPort, outPort: SyntaxTree.PortType;
  6889. expression: SyntaxTree.Expression;
  6890. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6891. BEGIN
  6892. right := ResolveExpression(communication.right);
  6893. left := ResolveDesignator(communication.left);
  6894. communication.SetLeft(left);
  6895. communication.SetRight(right);
  6896. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6897. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6898. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6899. (* conversion done by procedure call
  6900. (* try to convert to left argument *)
  6901. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6902. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6903. procedureCallDesignator.parameters.SetExpression(1, right);
  6904. END;
  6905. *)
  6906. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6907. ELSE
  6908. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6909. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6910. (* error already handled *)
  6911. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6912. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6913. IF outPort.direction # SyntaxTree.OutPort THEN
  6914. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6915. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6916. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6917. ELSE
  6918. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6919. communication.SetRight(right)
  6920. END;
  6921. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6922. IF CheckVariable(left) THEN
  6923. IF inPort.direction # SyntaxTree.InPort THEN
  6924. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6925. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6926. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6927. END;
  6928. END;
  6929. ELSE
  6930. Error(communication.position, -1, "unsupported stream operation");
  6931. END;
  6932. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6933. IF outPort.direction # SyntaxTree.OutPort THEN
  6934. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6935. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6936. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6937. ELSE
  6938. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6939. communication.SetRight(right)
  6940. END;
  6941. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6942. IF CheckVariable(right) THEN
  6943. IF inPort.direction # SyntaxTree.InPort THEN
  6944. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6945. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6946. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6947. END;
  6948. END;
  6949. ELSE
  6950. Error(communication.position, -1, "unsupported operation");
  6951. END;
  6952. END;
  6953. END VisitCommunicationStatement;
  6954. (** check and resolve if/eslif part
  6955. - check condition
  6956. - check statement sequence
  6957. **)
  6958. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6959. VAR prevUnreachable, b: BOOLEAN;
  6960. BEGIN
  6961. prevUnreachable := currentIsUnreachable;
  6962. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6963. IF IsBooleanValue(ifPart.condition,b) THEN
  6964. IF b=FALSE THEN
  6965. currentIsUnreachable := TRUE
  6966. ELSIF b=TRUE THEN
  6967. true := TRUE
  6968. END;
  6969. END;
  6970. StatementSequence(ifPart.statements);
  6971. currentIsUnreachable := prevUnreachable;
  6972. END IfPart;
  6973. (** check and resolve if statement
  6974. - check if parts and else part statement sequence
  6975. **)
  6976. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6977. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6978. BEGIN
  6979. prevUnreachable := currentIsUnreachable;
  6980. ifPartTrue := FALSE;
  6981. IfPart(ifStatement.ifPart,ifPartTrue);
  6982. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6983. elsif := ifStatement.GetElsifPart(i);
  6984. IfPart(elsif,ifPartTrue);
  6985. END;
  6986. IF ifStatement.elsePart # NIL THEN
  6987. IF ifPartTrue THEN
  6988. currentIsUnreachable := TRUE
  6989. END;
  6990. StatementSequence(ifStatement.elsePart)
  6991. END;
  6992. currentIsUnreachable := prevUnreachable;
  6993. END VisitIfStatement;
  6994. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6995. VAR variable: SyntaxTree.Designator;
  6996. type,variableType: SyntaxTree.Type;
  6997. withEntry: WithEntry;
  6998. BEGIN
  6999. variable := ResolveDesignator(withPart.variable);
  7000. variableType := variable.type.resolved;
  7001. withPart.SetVariable(variable);
  7002. type := ResolveType(withPart.type);
  7003. withPart.SetType(type);
  7004. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7005. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7006. END;
  7007. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7008. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7009. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7010. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7011. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7012. IF VerboseErrorMessage THEN
  7013. Printout.Info("variable",variable)
  7014. END;
  7015. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7016. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7017. IF VerboseErrorMessage THEN
  7018. Printout.Info("variable",variable);
  7019. Printout.Info("type",type);
  7020. END;
  7021. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7022. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7023. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7024. IF VerboseErrorMessage THEN
  7025. Printout.Info("variable",variable);
  7026. Printout.Info("type",type);
  7027. END;
  7028. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7029. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7030. ELSE
  7031. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7032. NEW(withEntry);
  7033. withEntry.previous := withEntries;
  7034. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7035. withEntry.type := type;
  7036. withEntries := withEntry;
  7037. StatementSequence(withPart.statements);
  7038. withEntries := withEntries.previous;
  7039. END;
  7040. END WithPart;
  7041. (** check and resolve with statement WITH variable: type DO ... END;
  7042. - check type and variable
  7043. - check that variable type is type extension of type
  7044. - check that variable is a variable
  7045. - enter new with scope and enter guardedVariable with same name and reference to variable
  7046. - create if statement:
  7047. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7048. **)
  7049. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7050. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7051. BEGIN
  7052. prevScope := currentScope; symbol := NIL;
  7053. FOR i := 0 TO withStatement.WithParts()-1 DO
  7054. WithPart(withStatement.GetWithPart(i),symbol);
  7055. END;
  7056. IF withStatement.elsePart # NIL THEN
  7057. StatementSequence(withStatement.elsePart)
  7058. END;
  7059. currentScope := prevScope;
  7060. END VisitWithStatement;
  7061. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7062. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7063. - check 'first' < 'last' and no overlaps between different case labels
  7064. - check statement sequence
  7065. **)
  7066. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7067. VAR
  7068. i: LONGINT;
  7069. position: LONGINT;
  7070. expression, left, right: SyntaxTree.Expression;
  7071. expressionType: SyntaxTree.Type;
  7072. l, r: LONGINT;
  7073. cl, cr: CHAR;
  7074. thiscases: SyntaxTree.CaseConstant;
  7075. BEGIN
  7076. thiscases := NIL;
  7077. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7078. expression := casePart.elements.GetExpression(i);
  7079. position := expression.position;
  7080. (* set context of range *)
  7081. IF expression IS SyntaxTree.RangeExpression THEN
  7082. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7083. END;
  7084. expression := ResolveExpression(expression);
  7085. IF expression = SyntaxTree.invalidExpression THEN
  7086. (* error already reported *)
  7087. expressionType := SyntaxTree.invalidType;
  7088. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7089. (* read out 'first' and 'last' *)
  7090. left := expression(SyntaxTree.RangeExpression).first;
  7091. right := expression(SyntaxTree.RangeExpression).last;
  7092. (* guaranteed by VisitRangeExpression: *)
  7093. ASSERT((left # NIL) & (right # NIL));
  7094. ASSERT(left.type.resolved = right.type.resolved);
  7095. left := CompatibleConversion(left.position, left, type);
  7096. right := CompatibleConversion(right.position, right, type);
  7097. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7098. expression(SyntaxTree.RangeExpression).SetLast(right);
  7099. expressionType := RegularType(position,left.type);
  7100. ELSE
  7101. expression := ConstantExpression(expression);
  7102. expression := CompatibleConversion(expression.position, expression, type);
  7103. (*
  7104. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7105. left := Global.NewCharacterValue(system,expression.position,cl);
  7106. expression := casePart.elements.GetExpression(i);
  7107. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7108. expression := left
  7109. END;
  7110. *)
  7111. casePart.elements.SetExpression(i,expression);
  7112. left := expression; right := expression;
  7113. expressionType := RegularType(position,expression.type)
  7114. END;
  7115. IF (expressionType = SyntaxTree.invalidType) THEN
  7116. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7117. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7118. expression := SyntaxTree.invalidExpression;
  7119. ELSE
  7120. l := 0; r := 0;
  7121. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7122. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7123. l := ORD(cl); r := ORD(cr);
  7124. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7125. ELSE
  7126. expression := SyntaxTree.invalidExpression
  7127. END;
  7128. IF expression # SyntaxTree.invalidExpression THEN
  7129. IF l>r THEN
  7130. Error(position,Diagnostics.Invalid,"empty case label")
  7131. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7132. Error(position,Diagnostics.Invalid,"duplicate case label");
  7133. ELSE
  7134. IF l < min THEN min := l END;
  7135. IF r > max THEN max := r END;
  7136. END;
  7137. END;
  7138. END;
  7139. casePart.elements.SetExpression(i,expression);
  7140. END;
  7141. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7142. casePart.SetConstants(thiscases);
  7143. StatementSequence(casePart.statements);
  7144. END CasePart;
  7145. (** check and resolve case statement CASE variable OF ... END;
  7146. - check variable
  7147. - check case parts
  7148. **)
  7149. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7150. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7151. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7152. BEGIN
  7153. expression := ResolveExpression(caseStatement.variable);
  7154. type := RegularType(expression.position,expression.type);
  7155. IF type = SyntaxTree.invalidType THEN
  7156. expression := SyntaxTree.invalidExpression;
  7157. ELSIF IsIntegerType(type) THEN
  7158. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7159. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7160. (*
  7161. expression := Global.NewCharacterValue(system,expression.position,ch);
  7162. *)
  7163. type := expression.type;
  7164. ELSIF IsCharacterType(type) THEN
  7165. ELSIF IsEnumerationType(type) THEN
  7166. ELSE
  7167. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7168. expression := SyntaxTree.invalidExpression;
  7169. END;
  7170. caseStatement.SetVariable(expression);
  7171. caseList := NIL;
  7172. min := MAX(LONGINT); max := MIN(LONGINT);
  7173. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7174. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7175. END;
  7176. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7177. msg := "huge sparse case table ";
  7178. Strings.AppendInt(msg, max-min);
  7179. Strings.Append(msg,"/");
  7180. Strings.AppendInt(msg, caseStatement.CaseParts());
  7181. Warning(caseStatement.position,msg);
  7182. END;
  7183. caseStatement.SetMinMax(min,max);
  7184. StatementSequence(caseStatement.elsePart);
  7185. IF expression.resolved # NIL THEN
  7186. IF IsCharacterValue(expression,ch) THEN
  7187. l := ORD(ch)
  7188. ELSIF IsIntegerValue(expression,l) THEN
  7189. END;
  7190. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7191. END;
  7192. END VisitCaseStatement;
  7193. (** check and resolve while statement
  7194. - check condition
  7195. - check statement sequence
  7196. **)
  7197. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7198. VAR prevIsUnreachable,b: BOOLEAN;
  7199. BEGIN
  7200. prevIsUnreachable := currentIsUnreachable;
  7201. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7202. IF IsBooleanValue(whileStatement.condition,b) THEN
  7203. IF b=FALSE THEN
  7204. currentIsUnreachable := TRUE
  7205. END;
  7206. END;
  7207. StatementSequence(whileStatement.statements);
  7208. currentIsUnreachable := prevIsUnreachable
  7209. END VisitWhileStatement;
  7210. (** check and resolve repeat statement
  7211. - check condition
  7212. - check statement sequence
  7213. **)
  7214. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7215. BEGIN
  7216. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7217. StatementSequence(repeatStatement.statements);
  7218. END VisitRepeatStatement;
  7219. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7220. VAR withEntry: WithEntry;
  7221. BEGIN
  7222. withEntry := withEntries;
  7223. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7224. withEntry := withEntry.previous
  7225. END;
  7226. IF withEntry = NIL THEN RETURN FALSE
  7227. ELSE
  7228. type := withEntry.type;
  7229. RETURN TRUE
  7230. END;
  7231. END GetGuard;
  7232. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7233. - check that variable is an integer variable
  7234. - check that from is integer typed with compatible type
  7235. - check that to has compatible type
  7236. - check that by is constant integer with compatible type
  7237. **)
  7238. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7239. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7240. BEGIN
  7241. designator := ResolveDesignator(forStatement.variable);
  7242. type := SyntaxTree.invalidType;
  7243. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7244. designator := SyntaxTree.invalidDesignator;
  7245. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7246. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7247. designator := SyntaxTree.invalidDesignator;
  7248. ELSIF CheckVariable(designator) THEN
  7249. type := designator.type;
  7250. END;
  7251. forStatement.SetVariable(designator);
  7252. expression := ResolveExpression(forStatement.from);
  7253. IF expression = SyntaxTree.invalidExpression THEN
  7254. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7255. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7256. expression := SyntaxTree.invalidExpression;
  7257. ELSIF type # SyntaxTree.invalidType THEN
  7258. expression := NewConversion(expression.position,expression,type,NIL)
  7259. END;
  7260. forStatement.SetFrom(expression);
  7261. expression := ResolveExpression(forStatement.to);
  7262. IF expression = SyntaxTree.invalidExpression THEN
  7263. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7264. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7265. expression := SyntaxTree.invalidExpression;
  7266. ELSIF type # SyntaxTree.invalidType THEN
  7267. expression := NewConversion(expression.position,expression,type,NIL)
  7268. END;
  7269. forStatement.SetTo(expression);
  7270. IF forStatement.by # NIL THEN
  7271. expression := ConstantInteger(forStatement.by);
  7272. ELSE
  7273. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7274. END;
  7275. IF expression = SyntaxTree.invalidExpression THEN
  7276. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7277. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7278. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7279. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7280. ELSIF type # SyntaxTree.invalidType THEN
  7281. expression := NewConversion(expression.position,expression,type,NIL)
  7282. END;
  7283. forStatement.SetBy(expression);
  7284. StatementSequence(forStatement.statements);
  7285. END VisitForStatement;
  7286. (** check and resolve loop statement LOOP StatementSequence END
  7287. - check statement sequence
  7288. **)
  7289. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7290. BEGIN
  7291. StatementSequence(loopStatement.statements)
  7292. END VisitLoopStatement;
  7293. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7294. BEGIN
  7295. StatementSequence(exitableBlock.statements);
  7296. END VisitExitableBlock;
  7297. (** check and resolve exit statement EXIT
  7298. - check that exit is within LOOP statement block
  7299. **)
  7300. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7301. VAR outer: SyntaxTree.Statement;
  7302. BEGIN
  7303. outer := exitStatement.outer;
  7304. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7305. outer := outer.outer;
  7306. END;
  7307. IF outer = NIL THEN
  7308. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7309. END;
  7310. END VisitExitStatement;
  7311. (** check and resolve return statement RETURN [expression]
  7312. - check expression (if any)
  7313. - check if in procedure scope
  7314. - if in procedure scope then check expression compatibility
  7315. - if not in procecdure scope then check on return without expression
  7316. **)
  7317. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7318. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7319. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7320. BEGIN
  7321. position := returnStatement.position;
  7322. expression := returnStatement.returnValue;
  7323. IF expression # NIL THEN
  7324. expression := ResolveExpression(expression);
  7325. returnStatement.SetReturnValue(expression);
  7326. END;
  7327. outer := returnStatement.outer;
  7328. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7329. outer := outer.outer
  7330. END;
  7331. IF (outer # NIL) THEN
  7332. scope := outer(SyntaxTree.Body).inScope;
  7333. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7334. IF (expression # NIL) THEN
  7335. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7336. END;
  7337. ELSE
  7338. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7339. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7340. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7341. END;
  7342. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7343. IF returnType # NIL THEN
  7344. returnType := returnType.resolved;
  7345. IF expression = NIL THEN
  7346. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7347. ELSIF expression.type = NIL THEN
  7348. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7349. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7350. Error(position,Diagnostics.Invalid,"return type not compatible");
  7351. IF VerboseErrorMessage THEN
  7352. Printout.Info("returnType",returnType);
  7353. Printout.Info("expression",expression);
  7354. END;
  7355. ELSE
  7356. expression := NewConversion(expression.position,expression,returnType,NIL);
  7357. returnStatement.SetReturnValue(expression);
  7358. END;
  7359. ELSIF expression # NIL THEN
  7360. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7361. END;
  7362. END;
  7363. END;
  7364. END VisitReturnStatement;
  7365. (** check and resolve await statement AWAIT(condition: Expression)
  7366. - check await condition
  7367. **)
  7368. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7369. VAR condition: SyntaxTree.Expression;
  7370. BEGIN
  7371. condition := ResolveCondition(awaitStatement.condition);
  7372. IF currentIsRealtime THEN
  7373. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7374. END;
  7375. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7376. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7377. END;
  7378. awaitStatement.SetCondition(condition);
  7379. END VisitAwaitStatement;
  7380. PROCEDURE CheckSystemImport(position: LONGINT);
  7381. VAR import: SyntaxTree.Import;
  7382. BEGIN
  7383. import := currentScope.ownerModule.moduleScope.firstImport;
  7384. WHILE(import # NIL) DO
  7385. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7386. RETURN;
  7387. END;
  7388. import := import.nextImport;
  7389. END;
  7390. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7391. END CheckSystemImport;
  7392. (** check and resolve code statement: do nothing, must be done by assembler
  7393. **)
  7394. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7395. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7396. BEGIN
  7397. CheckSystemImport(code.position);
  7398. FOR i := 0 TO code.inRules.Length()-1 DO
  7399. statement := code.inRules.GetStatement(i);
  7400. IF statement IS SyntaxTree.Assignment THEN
  7401. WITH statement: SyntaxTree.Assignment DO
  7402. statement.SetRight(ResolveExpression(statement.right));
  7403. END;
  7404. ELSE
  7405. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7406. END;
  7407. END;
  7408. FOR i := 0 TO code.outRules.Length()-1 DO
  7409. statement := code.outRules.GetStatement(i);
  7410. IF statement IS SyntaxTree.Assignment THEN
  7411. WITH statement: SyntaxTree.Assignment DO
  7412. statement.SetLeft(ResolveDesignator(statement.left));
  7413. END;
  7414. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7415. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7416. ELSE
  7417. Printout.Info("out statement ", statement);
  7418. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7419. END;
  7420. END;
  7421. END VisitCode;
  7422. (** check and set flags of a statement block
  7423. - check for multiply occurence of a flag
  7424. - check and set priority only in bodies
  7425. - check for valid names
  7426. **)
  7427. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7428. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7429. flag: LONGINT; recordBody: SyntaxTree.Body;
  7430. PROCEDURE SetProtectedRecord;
  7431. VAR scope: SyntaxTree.Scope;
  7432. BEGIN
  7433. scope := currentScope;
  7434. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7435. scope := scope.outerScope
  7436. END;
  7437. IF scope # NIL THEN
  7438. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7439. END;
  7440. END SetProtectedRecord;
  7441. BEGIN
  7442. flags := {};
  7443. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7444. recordBody := block(SyntaxTree.Body)
  7445. ELSE
  7446. recordBody := NIL
  7447. END;
  7448. blockModifier := block.blockModifiers;
  7449. WHILE(blockModifier # NIL) DO
  7450. name := blockModifier.identifier;
  7451. expression := blockModifier.expression;
  7452. position := blockModifier.position;
  7453. flag := -1;
  7454. IF name=Global.NamePriority THEN
  7455. IF expression = NIL THEN
  7456. Error(position,Diagnostics.Invalid,"missing priority expression");
  7457. ELSIF recordBody = NIL THEN
  7458. Error(position,Diagnostics.Invalid,"priority not on record body");
  7459. ELSIF recordBody.priority # NIL THEN
  7460. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7461. ELSE
  7462. recordBody.SetPriority(expression);
  7463. END;
  7464. ELSIF expression # NIL THEN
  7465. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7466. ELSIF name=Global.NameExclusive THEN
  7467. IF block.isExclusive THEN
  7468. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7469. END;
  7470. block.SetExclusive(TRUE); SetProtectedRecord;
  7471. ELSIF name=Global.NameActive THEN
  7472. IF recordBody = NIL THEN
  7473. Error(position,Diagnostics.Invalid,"active not in record body");
  7474. ELSIF recordBody.isActive THEN
  7475. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7476. ELSE
  7477. recordBody.SetActive(TRUE); SetProtectedRecord;
  7478. END;
  7479. ELSIF name=Global.NameSafe THEN
  7480. IF recordBody = NIL THEN
  7481. Error(position,Diagnostics.Invalid,"safe not in record body");
  7482. ELSIF recordBody.isSafe THEN
  7483. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7484. ELSE
  7485. recordBody.SetSafe(TRUE);
  7486. SetProtectedRecord;
  7487. END;
  7488. ELSIF name=Global.NameRealtime THEN
  7489. IF recordBody = NIL THEN
  7490. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7491. ELSIF recordBody.isRealtime THEN
  7492. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7493. ELSE
  7494. recordBody.SetRealtime(TRUE);
  7495. block.SetRealtime(TRUE);
  7496. END;
  7497. ELSIF name=Global.NameUnchecked THEN
  7498. IF block.isUnchecked THEN
  7499. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7500. ELSE
  7501. block.SetUnchecked(TRUE);
  7502. END;
  7503. ELSIF (name=Global.NameUncooperative) THEN
  7504. IF block.isUncooperative THEN
  7505. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7506. ELSE
  7507. block.SetUncooperative(TRUE);
  7508. END;
  7509. ELSE
  7510. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7511. END;
  7512. blockModifier := blockModifier.nextModifier;
  7513. END;
  7514. END BlockFlags;
  7515. (** check and resolve statement block
  7516. - check flags (exclusive)
  7517. - check statement sequence
  7518. **)
  7519. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7520. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7521. BEGIN
  7522. BlockFlags(statementBlock);
  7523. IF statementBlock.isExclusive THEN
  7524. (* check that not in exclusive block *)
  7525. IF currentIsExclusive THEN
  7526. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7527. ELSIF currentIsRealtime THEN
  7528. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7529. END;
  7530. END;
  7531. recentExclusive := currentIsExclusive;
  7532. recentUnreachable := currentIsUnreachable;
  7533. recentRealtime := currentIsRealtime;
  7534. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7535. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7536. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7537. StatementSequence(statementBlock.statements);
  7538. currentIsRealtime := recentRealtime;
  7539. currentIsExclusive := recentExclusive;
  7540. currentIsUnreachable := recentUnreachable;
  7541. END VisitStatementBlock;
  7542. (** check and resolve body
  7543. - check flags (active, priority, safe)
  7544. - check body and finally part
  7545. **)
  7546. PROCEDURE Body(body: SyntaxTree.Body);
  7547. BEGIN
  7548. VisitStatementBlock(body);
  7549. IF body.isActive THEN
  7550. IF ~currentIsBodyProcedure THEN
  7551. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7552. ELSIF body.priority # NIL THEN
  7553. body.SetPriority(ConstantInteger(body.priority));
  7554. END;
  7555. ELSIF body.isSafe THEN
  7556. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7557. ELSIF body.priority # NIL THEN
  7558. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7559. END;
  7560. IF body.code # NIL THEN
  7561. CheckSystemImport(body.position);
  7562. END;
  7563. StatementSequence(body.finally)
  7564. END Body;
  7565. (*** scopes ***)
  7566. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7567. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7568. VAR duplicateSymbol: BOOLEAN;
  7569. BEGIN
  7570. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7571. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7572. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7573. END;
  7574. scope.EnterSymbol(symbol,duplicateSymbol);
  7575. IF ~allowDuplicate & duplicateSymbol THEN
  7576. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7577. IF VerboseErrorMessage THEN
  7578. Printout.Info("multiply defined identifier",symbol);
  7579. Printout.Info("in scope",scope);
  7580. END;
  7581. END;
  7582. END Register;
  7583. (**
  7584. implementation: check and resolve an implementation part
  7585. **)
  7586. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7587. move implementation checker to a separate object ? *)
  7588. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7589. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7590. BEGIN
  7591. prevIsRealtime := currentIsRealtime;
  7592. prevIsBodyProcedure := currentIsBodyProcedure;
  7593. prevIsCellNet := currentIsCellNet;
  7594. prevScope := currentScope;
  7595. currentScope := scope;
  7596. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7597. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7598. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7599. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7600. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7601. (*
  7602. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7603. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7604. END;
  7605. *)
  7606. END;
  7607. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7608. (* module body, record bodies are wrapped into an artifical procedure *)
  7609. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7610. Body(scope(SyntaxTree.ProcedureScope).body)
  7611. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7612. Body(scope(SyntaxTree.ProcedureScope).body)
  7613. END;
  7614. END;
  7615. currentScope := prevScope;
  7616. currentIsRealtime := prevIsRealtime;
  7617. currentIsBodyProcedure := prevIsBodyProcedure;
  7618. currentIsCellNet := prevIsCellNet;
  7619. END Implementation;
  7620. (** implementation phase:
  7621. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7622. **)
  7623. PROCEDURE Implementations(x: SyntaxTree.Module);
  7624. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7625. BEGIN
  7626. prevPhase := phase;
  7627. phase := InlinePhase;
  7628. scope := x.firstScope;
  7629. WHILE(scope # NIL) DO
  7630. Implementation(scope);
  7631. scope := scope.nextScope;
  7632. END;
  7633. phase := ImplementationPhase;
  7634. scope := x.firstScope;
  7635. WHILE(scope # NIL) DO
  7636. Implementation(scope);
  7637. scope := scope.nextScope;
  7638. END;
  7639. phase := prevPhase;
  7640. END Implementations;
  7641. (** declaration phase:
  7642. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7643. - import lists (for module scopes)
  7644. - parameter list (for procedure scopes)
  7645. - constant declarations
  7646. - type declarations
  7647. - variable declarations
  7648. - procedure declarations
  7649. preformed in two stages:
  7650. - first all symbols are entered into the symbol table (with uniqueness check),
  7651. - then all symbols are resolved
  7652. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7653. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7654. **)
  7655. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7656. VAR
  7657. constant: SyntaxTree.Constant;
  7658. typeDeclaration: SyntaxTree.TypeDeclaration;
  7659. variable: SyntaxTree.Variable;
  7660. procedure: SyntaxTree.Procedure;
  7661. prevScope: SyntaxTree.Scope;
  7662. parameter: SyntaxTree.Parameter;
  7663. import: SyntaxTree.Import;
  7664. symbol: SyntaxTree.Symbol;
  7665. prevPhase: LONGINT;
  7666. prevError : BOOLEAN;
  7667. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7668. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7669. BEGIN
  7670. IF type.baseType # NIL THEN
  7671. baseType := type.baseType.resolved;
  7672. IF baseType IS SyntaxTree.PointerType THEN
  7673. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7674. END;
  7675. IF baseType IS SyntaxTree.CellType THEN
  7676. DeclareCell(baseType(SyntaxTree.CellType));
  7677. END;
  7678. END;
  7679. parameter := type.firstParameter;
  7680. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7681. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7682. variable.SetType(parameter.type);
  7683. variable.SetAccess(SyntaxTree.Hidden);
  7684. variable.SetModifiers(parameter.modifiers);
  7685. currentScope.PushVariable(variable);
  7686. (*
  7687. Register(parameter,scope, FALSE);
  7688. *)
  7689. parameter := parameter.nextParameter;
  7690. END;
  7691. property := type.firstProperty;
  7692. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7693. variable := currentScope.FindVariable(property.name);
  7694. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7695. prop := variable(SyntaxTree.Property);
  7696. ELSE (* add, duplicate symbols detection later *)
  7697. prop := SyntaxTree.NewProperty(property.position, property.name);
  7698. currentScope.PushVariable(prop);
  7699. END;
  7700. prop.SetType(property.type);
  7701. prop.SetValue(property.value);
  7702. prop.SetAccess(SyntaxTree.Hidden);
  7703. property := property.nextProperty;
  7704. END;
  7705. END DeclareCell;
  7706. BEGIN
  7707. prevError := error;
  7708. prevPhase := phase;
  7709. phase := DeclarationPhase;
  7710. prevScope := currentScope;
  7711. currentScope := scope;
  7712. error := FALSE;
  7713. (* first enter all symbols in scope *)
  7714. IF scope IS SyntaxTree.ModuleScope THEN
  7715. (* treat imports first for a module scope, , set default context if necessary *)
  7716. import := scope(SyntaxTree.ModuleScope).firstImport;
  7717. WHILE(import # NIL) DO
  7718. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7719. Register(import, currentScope, FALSE);
  7720. import := import.nextImport;
  7721. END;
  7722. import := scope(SyntaxTree.ModuleScope).firstImport;
  7723. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7724. ResolveSymbol(import);
  7725. import := import.nextImport;
  7726. END;
  7727. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7728. (* enter parameters for a procedure scope *)
  7729. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7730. WHILE(parameter # NIL) DO
  7731. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7732. END;
  7733. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7734. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7735. ELSIF scope IS SyntaxTree.CellScope THEN
  7736. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7737. END;
  7738. IF error THEN RETURN END;
  7739. (* constants *)
  7740. constant := scope.firstConstant;
  7741. WHILE (constant # NIL) DO
  7742. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7743. END;
  7744. (* type declarations *)
  7745. typeDeclaration := scope.firstTypeDeclaration;
  7746. WHILE (typeDeclaration # NIL) DO
  7747. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7748. END;
  7749. (* variables *)
  7750. variable := scope.firstVariable;
  7751. WHILE (variable # NIL) DO
  7752. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7753. END;
  7754. (* procedures *)
  7755. procedure := scope.firstProcedure;
  7756. WHILE (procedure # NIL) DO
  7757. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7758. END;
  7759. (* now process all symbols without any presumption on the order *)
  7760. symbol := scope.firstSymbol;
  7761. WHILE(symbol # NIL) DO
  7762. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7763. ResolveSymbol(symbol);
  7764. END;
  7765. symbol := symbol.nextSymbol;
  7766. END;
  7767. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7768. symbol := scope.firstSymbol;
  7769. WHILE symbol # NIL DO
  7770. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7771. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7772. pointerFixes.Add(symbol, currentScope);
  7773. END;
  7774. IF ~symbol.type.resolved.isRealtime THEN
  7775. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7776. END;
  7777. END;
  7778. symbol := symbol.nextSymbol
  7779. END;
  7780. END;
  7781. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7782. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7783. END;
  7784. IF scope.ownerModule # NIL THEN
  7785. IF ~(scope IS SyntaxTree.CellScope) OR ~SkipImplementation(scope(SyntaxTree.CellScope).ownerCell) THEN
  7786. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7787. scope.ownerModule.AddScope(scope);
  7788. END;
  7789. END;
  7790. phase := prevPhase;
  7791. currentScope := prevScope;
  7792. error := error OR prevError;
  7793. END Declarations;
  7794. (* nopov *)
  7795. (** check if all operators from one module are compatible to the ones in the other module
  7796. - check if there are not multiple operators with the same signature
  7797. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7798. - check for all operators whose signatures are compatible, whether the return types are compatible
  7799. note that:
  7800. - the return type is not considered to be part of the signature
  7801. - two signatures are considered compatible, if all of the operands are compatible
  7802. **)
  7803. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7804. VAR
  7805. thisOperator, thatOperator: SyntaxTree.Operator;
  7806. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7807. thisParameter, thatParameter: SyntaxTree.Parameter;
  7808. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7809. i: LONGINT;
  7810. BEGIN
  7811. currentScope := thisModuleScope;
  7812. hasError := FALSE;
  7813. (* go through all operators in the other module *)
  7814. thatOperator := thatModuleScope.firstOperator;
  7815. WHILE (thatOperator # NIL) & ~hasError DO
  7816. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7817. (* the other operator is accessible *)
  7818. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7819. (* the other operator is not the conversion operator *)
  7820. (* go through all operators in this module *)
  7821. thisOperator := thisModuleScope.firstOperator;
  7822. WHILE (thisOperator # NIL) & ~hasError DO
  7823. IF thisOperator # thatOperator THEN
  7824. (* the operators are not the same *)
  7825. IF thisOperator.name = thatOperator.name THEN
  7826. (* the operators share the same identifier *)
  7827. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7828. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7829. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7830. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7831. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7832. (* both operators have the same paramter count *)
  7833. thisParameter := thisProcedureType.firstParameter;
  7834. thatParameter := thatProcedureType.firstParameter;
  7835. operandsAreEqual := TRUE;
  7836. operandsAreCompatible := TRUE;
  7837. (* go through all parameters *)
  7838. FOR i := 1 TO thisProcedureType.numberParameters DO
  7839. ASSERT(thatParameter # NIL);
  7840. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7841. operandsAreEqual := FALSE;
  7842. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7843. operandsAreCompatible := FALSE
  7844. END
  7845. END;
  7846. thisParameter := thisParameter.nextParameter;
  7847. thatParameter := thatParameter.nextParameter
  7848. END;
  7849. IF operandsAreEqual THEN
  7850. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7851. hasError := TRUE
  7852. ELSIF operandsAreCompatible THEN
  7853. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7854. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7855. hasError := TRUE
  7856. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7857. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7858. hasError := TRUE
  7859. END
  7860. END
  7861. END
  7862. END
  7863. END;
  7864. thisOperator := thisOperator.nextOperator
  7865. END
  7866. END
  7867. END;
  7868. thatOperator := thatOperator.nextOperator
  7869. END
  7870. END CheckInterOperatorConformity;
  7871. (** check module:
  7872. - check module declaration
  7873. - add context, if necessary
  7874. - remove module from import cache, if necessary
  7875. - check declarations
  7876. - resolve all type fixes
  7877. - check implementation (bodies)
  7878. **)
  7879. PROCEDURE Module*(x: SyntaxTree.Module);
  7880. VAR (* nopov *)
  7881. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7882. BEGIN
  7883. prevScope := currentScope;
  7884. prevIsCellNet := currentIsCellNet;
  7885. module := x;
  7886. ASSERT(x # NIL);
  7887. global := system.globalScope[x.case];
  7888. x.moduleScope.SetGlobalScope(global);
  7889. currentScope := global;
  7890. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7891. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7892. RemoveModuleFromCache(importCache,x);
  7893. Declarations(x.moduleScope);
  7894. FixTypes();
  7895. IF module.isCellNet THEN
  7896. currentIsCellNet := TRUE;
  7897. modifier := x.modifiers;
  7898. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7899. CheckModifiers(modifier, FALSE);
  7900. END;
  7901. (* nopov *)
  7902. IF ~error THEN
  7903. (* check if operators conform to each other within this module *)
  7904. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7905. (* go through all imports *)
  7906. import := x.moduleScope.firstImport;
  7907. WHILE import # NIL DO
  7908. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7909. (* check if all operators in this module conform to the ones of the imported module *)
  7910. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7911. END;
  7912. import := import.nextImport
  7913. END;
  7914. END;
  7915. Implementations(x);
  7916. module := NIL;
  7917. currentIsCellNet := prevIsCellNet;
  7918. currentScope := prevScope;
  7919. END Module;
  7920. END Checker;
  7921. Warnings*=OBJECT (SyntaxTree.Visitor)
  7922. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7923. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7924. BEGIN
  7925. SELF.diagnostics := diagnostics
  7926. END InitWarnings;
  7927. (** types *)
  7928. PROCEDURE Type(x: SyntaxTree.Type);
  7929. BEGIN x.Accept(SELF)
  7930. END Type;
  7931. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7932. BEGIN END VisitType;
  7933. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7934. BEGIN END VisitBasicType;
  7935. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7936. BEGIN END VisitCharacterType;
  7937. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7938. BEGIN END VisitIntegerType;
  7939. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7940. BEGIN END VisitFloatType;
  7941. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7942. BEGIN END VisitQualifiedType;
  7943. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7944. BEGIN END VisitStringType;
  7945. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7946. BEGIN END VisitEnumerationType;
  7947. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7948. BEGIN END VisitRangeType;
  7949. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7950. BEGIN
  7951. IF ~(SyntaxTree.Warned IN x.state) THEN
  7952. x.SetState(SyntaxTree.Warned);
  7953. Type(x.arrayBase);
  7954. END;
  7955. END VisitArrayType;
  7956. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7957. BEGIN
  7958. IF ~(SyntaxTree.Warned IN x.state) THEN
  7959. x.SetState(SyntaxTree.Warned);
  7960. Type(x.arrayBase);
  7961. END;
  7962. END VisitMathArrayType;
  7963. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7964. BEGIN
  7965. IF ~(SyntaxTree.Warned IN x.state) THEN
  7966. x.SetState(SyntaxTree.Warned);
  7967. Type(x.pointerBase);
  7968. END;
  7969. END VisitPointerType;
  7970. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7971. BEGIN Scope(x.recordScope) END VisitRecordType;
  7972. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7973. BEGIN Scope(x.cellScope) END VisitCellType;
  7974. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7975. BEGIN END VisitProcedureType;
  7976. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7977. VAR msg: ARRAY 256 OF CHAR;
  7978. BEGIN
  7979. Global.GetSymbolName(x,msg);
  7980. Strings.Append(msg," ");
  7981. Strings.Append(msg,text);
  7982. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7983. END Warning;
  7984. (** symbols *)
  7985. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7986. BEGIN
  7987. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7988. IF ~(x IS SyntaxTree.Parameter) THEN
  7989. Warning(x,"never used");
  7990. END;
  7991. END;
  7992. x.Accept(SELF);
  7993. END Symbol;
  7994. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7995. BEGIN END VisitSymbol;
  7996. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7997. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7998. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7999. BEGIN END VisitConstant;
  8000. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8001. BEGIN END VisitVariable;
  8002. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8003. BEGIN END VisitProperty;
  8004. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8005. BEGIN END VisitParameter;
  8006. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8007. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8008. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8009. BEGIN END VisitOperator;
  8010. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8011. BEGIN END VisitImport;
  8012. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8013. VAR
  8014. symbol: SyntaxTree.Symbol;
  8015. BEGIN
  8016. symbol := scope.firstSymbol;
  8017. WHILE(symbol # NIL) DO
  8018. Symbol(symbol);
  8019. symbol := symbol.nextSymbol;
  8020. END;
  8021. END Scope;
  8022. PROCEDURE Module*(x: SyntaxTree.Module);
  8023. BEGIN
  8024. SELF.module := x;
  8025. Scope(x.moduleScope);
  8026. END Module;
  8027. END Warnings;
  8028. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8029. BEGIN
  8030. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8031. END IsOberonInline;
  8032. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8033. BEGIN
  8034. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8035. END Resolved;
  8036. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8037. VAR i: LONGINT;
  8038. BEGIN
  8039. i := 1;
  8040. WHILE i < x DO
  8041. i := i *2
  8042. END;
  8043. RETURN i=x
  8044. END PowerOf2;
  8045. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8046. BEGIN
  8047. RETURN
  8048. (scope # NIL) &
  8049. (scope IS SyntaxTree.ModuleScope)
  8050. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8051. OR
  8052. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8053. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8054. END IsCellNetScope;
  8055. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8056. BEGIN
  8057. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8058. END IsCellScope;
  8059. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8060. BEGIN
  8061. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8062. RETURN (scope # NIL) & IsCellNetScope(scope)
  8063. END InCellNetScope;
  8064. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8065. BEGIN
  8066. ASSERT(size MOD system.dataUnit = 0);
  8067. RETURN size DIV system.dataUnit
  8068. END ToMemoryUnits;
  8069. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8070. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8071. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8072. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8073. BEGIN
  8074. IF t = NIL THEN
  8075. RETURN TRUE
  8076. ELSE
  8077. t := t.resolved;
  8078. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8079. END;
  8080. END TypeAllowed;
  8081. BEGIN
  8082. type := type.resolved;
  8083. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8084. RETURN FALSE
  8085. ELSE
  8086. procedureType := type(SyntaxTree.ProcedureType);
  8087. numberParameters := procedureType.numberParameters;
  8088. RETURN
  8089. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8090. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8091. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8092. END;
  8093. END GetProcedureAllowed;
  8094. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8095. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8096. VAR import: SyntaxTree.Import;
  8097. BEGIN
  8098. import := importCache.ImportByModuleName(x.name,x.context);
  8099. IF import # NIL THEN
  8100. importCache.RemoveImporters(x.name,x.context);
  8101. END;
  8102. END RemoveModuleFromCache;
  8103. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8104. (* to <- this assignment compatibility *)
  8105. VAR result: BOOLEAN;
  8106. BEGIN
  8107. IF this= NIL THEN result := (to=NIL)
  8108. ELSIF to=NIL THEN result := FALSE
  8109. ELSE
  8110. (*! will be replaced by this:
  8111. ELSE result := this.CompatibleTo(to.resolved);
  8112. *)
  8113. this := this.resolved; to := to.resolved;
  8114. IF to=SyntaxTree.invalidType THEN result := FALSE
  8115. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8116. ELSIF to = this THEN
  8117. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8118. ELSIF to IS SyntaxTree.BasicType THEN
  8119. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8120. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8121. result := this.CompatibleTo(to.resolved)
  8122. ELSE
  8123. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8124. END
  8125. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8126. result := to.sizeInBits = this.sizeInBits;
  8127. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8128. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8129. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8130. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8131. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8132. result := TRUE;
  8133. ELSIF to IS SyntaxTree.AnyType THEN
  8134. 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);
  8135. ELSIF to IS SyntaxTree.ObjectType THEN
  8136. 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 *) ;
  8137. ELSIF to IS SyntaxTree.ByteType THEN
  8138. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8139. ELSIF to IS SyntaxTree.CharacterType THEN
  8140. result := IsCharacterType(this)
  8141. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8142. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8143. 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
  8144. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8145. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8146. result := TRUE;
  8147. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8148. result := TRUE;
  8149. ELSE
  8150. result := FALSE
  8151. END;
  8152. ELSIF to IS SyntaxTree.PointerType THEN
  8153. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8154. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8155. & (~to.isRealtime OR this.isRealtime);
  8156. ELSIF to IS SyntaxTree.ProcedureType THEN
  8157. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8158. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8159. & (~to.isRealtime OR this.isRealtime)
  8160. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8161. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8162. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8163. ELSIF to IS SyntaxTree.RecordType THEN
  8164. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8165. ELSIF to IS SyntaxTree.ArrayType THEN
  8166. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8167. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8168. ELSIF StaticArrayCompatible(to, this) THEN
  8169. result := TRUE
  8170. ELSE
  8171. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8172. END;
  8173. ELSIF to IS SyntaxTree.MathArrayType THEN
  8174. IF this IS SyntaxTree.MathArrayType THEN
  8175. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8176. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8177. result := TRUE;
  8178. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8179. result := TRUE;
  8180. ELSE
  8181. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8182. END;
  8183. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8184. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8185. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8186. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8187. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8188. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8189. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8190. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8191. ELSE
  8192. result := FALSE
  8193. END;
  8194. (* an array-structured object type is compatible to the type of its array structure *)
  8195. ELSIF IsArrayStructuredObjectType(this) THEN
  8196. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8197. ELSE
  8198. result := FALSE;
  8199. END;
  8200. ELSIF to IS SyntaxTree.StringType THEN
  8201. result := FALSE;
  8202. ELSIF to IS SyntaxTree.EnumerationType THEN
  8203. result := IsEnumerationExtension(this,to);
  8204. ELSIF to IS SyntaxTree.PortType THEN
  8205. result := SameType(to, this)
  8206. ELSE
  8207. Printout.Info("CompatibleTo",to);
  8208. HALT(100); (* implement missing type check *)
  8209. END;
  8210. END;
  8211. RETURN result
  8212. END CompatibleTo;
  8213. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8214. VAR actualBase, formalBase: SyntaxTree.Type;
  8215. BEGIN
  8216. IF SameType(formal,actual) THEN
  8217. RETURN TRUE
  8218. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8219. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8220. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8221. RETURN
  8222. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8223. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8224. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8225. & StaticArrayCompatible(formalBase,actualBase)
  8226. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8227. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8228. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8229. RETURN
  8230. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8231. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8232. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8233. & StaticArrayCompatible(formalBase,actualBase)
  8234. ELSE RETURN FALSE
  8235. END;
  8236. END StaticArrayCompatible;
  8237. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8238. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8239. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8240. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8241. BEGIN
  8242. result := SameType(formal,actual);
  8243. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8244. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8245. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8246. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8247. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8248. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8249. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8250. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8251. & TC(formalBase, actualBase);
  8252. END;
  8253. RETURN result
  8254. END TC;
  8255. BEGIN
  8256. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8257. ELSE
  8258. arrayBase := formalType.arrayBase.resolved;
  8259. IF (actualType IS SyntaxTree.StringType) THEN
  8260. result := arrayBase IS SyntaxTree.CharacterType
  8261. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8262. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8263. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8264. result := TC(formalType, actualType);
  8265. ELSE
  8266. result := (arrayBase IS SyntaxTree.ByteType)
  8267. END;
  8268. END;
  8269. RETURN result
  8270. END OpenArrayCompatible;
  8271. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8272. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8273. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8274. BEGIN
  8275. IF actualType IS SyntaxTree.MathArrayType THEN
  8276. actualArray := actualType(SyntaxTree.MathArrayType);
  8277. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8278. (*
  8279. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8280. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8281. *)
  8282. actualBase := ArrayBase(actualType,Infinity);
  8283. formalBase := ArrayBase(formalType,Infinity);
  8284. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8285. ELSE
  8286. (*
  8287. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8288. *)
  8289. formalBase := Resolved(formalType.arrayBase);
  8290. actualBase := Resolved(actualArray.arrayBase);
  8291. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8292. (*
  8293. ARRAY [k] -> ARRAY [n]
  8294. *)
  8295. result := (formalType.staticLength = actualArray.staticLength)
  8296. ELSE
  8297. result := TRUE
  8298. END;
  8299. IF ~result THEN
  8300. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8301. ELSIF actualBase = NIL THEN result := FALSE
  8302. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8303. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8304. ELSE
  8305. result := SameType(formalBase,actualBase)
  8306. END;
  8307. END;
  8308. ELSE
  8309. result := FALSE
  8310. END;
  8311. RETURN result
  8312. END MathArrayCompatible;
  8313. (**
  8314. Math Array Type distance for assignments / parameter passings of the form
  8315. from -> to
  8316. variants:
  8317. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8318. allowed:
  8319. static -> static (& size match)
  8320. static -> open
  8321. static -> tensor
  8322. open -> open
  8323. open -> tensor
  8324. open -> static
  8325. tensor -> tensor
  8326. tensor -> open
  8327. tensor -> static
  8328. **)
  8329. (*! think about the metric here: is form matching more important than element type matching? *)
  8330. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8331. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8332. BEGIN
  8333. fromBase := Resolved(from.arrayBase);
  8334. toBase := Resolved(to.arrayBase);
  8335. i := Infinity;
  8336. IF from = to THEN
  8337. i := 0;
  8338. ELSIF (from.form = to.form) THEN
  8339. (* static -> static, open -> open, tensor -> tensor *)
  8340. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8341. IF fromBase = toBase THEN i := 0
  8342. ELSIF toBase = NIL THEN i := 1
  8343. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8344. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8345. ELSE
  8346. i := TypeDistance(system,fromBase, toBase, varpar);
  8347. END;
  8348. END;
  8349. ELSIF (to.form = SyntaxTree.Static) THEN
  8350. (* forbidden *)
  8351. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8352. (* static -> tensor, open -> tensor, tensor -> open *)
  8353. IF toBase=fromBase THEN i := 0;
  8354. ELSIF toBase = NIL THEN i := 1;
  8355. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8356. toBase := ArrayBase(toBase,Infinity);
  8357. IF (fromBase=toBase) THEN i := 0
  8358. ELSIF (toBase = NIL) THEN i:= 1
  8359. ELSIF (fromBase = NIL) THEN i := Infinity;
  8360. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8361. END;
  8362. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8363. fromBase := ArrayBase(fromBase,Infinity);
  8364. IF (fromBase=toBase) THEN i := 0
  8365. ELSIF (toBase = NIL) THEN i := 1
  8366. ELSIF (fromBase = NIL) THEN i := Infinity;
  8367. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8368. END;
  8369. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8370. END;
  8371. IF i # Infinity THEN INC(i,2) END;
  8372. ELSIF (from.form = SyntaxTree.Static) THEN
  8373. (* static -> open *)
  8374. IF toBase=fromBase THEN i := 0
  8375. ELSIF toBase = NIL THEN i := 1
  8376. ELSIF fromBase = NIL THEN i := Infinity
  8377. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8378. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8379. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8380. END;
  8381. IF i # Infinity THEN INC(i,1) END;
  8382. ELSE HALT(100); (* unknown case *)
  8383. END;
  8384. RETURN i;
  8385. END MathArrayTypeDistance;
  8386. (** compute and return the distance of two array types
  8387. - return the distance of the base types
  8388. **)
  8389. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8390. VAR i: LONGINT;
  8391. BEGIN
  8392. i := Infinity;
  8393. IF from = to THEN
  8394. i := 0
  8395. ELSE
  8396. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8397. (*
  8398. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8399. i := TypeDistance(from.base, to.base);
  8400. IF i >= 0 THEN INC(i) END
  8401. ELSIF (from.mode = open) & (to.mode = open) THEN
  8402. i := TypeDistance(from.base, to.base);
  8403. *)
  8404. END;
  8405. RETURN i
  8406. END ArrayTypeDistance;
  8407. (** compute the signature distance of a procedure and an actual parameter list
  8408. - if any of the parameters are not compatible, the result is infinite
  8409. - add up and return the distance over all parameters
  8410. **)
  8411. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8412. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8413. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8414. BEGIN
  8415. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8416. result := Infinity
  8417. ELSE
  8418. formalParameter := procedureType.firstParameter;
  8419. i := 0;
  8420. result := 0;
  8421. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8422. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8423. actualParameter := actualParameters.GetExpression(i);
  8424. ASSERT(formalParameter.type # NIL);
  8425. IF (actualParameter.type = NIL) THEN distance := Infinity
  8426. ELSE
  8427. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8428. END;
  8429. IF distance = Infinity THEN
  8430. result := Infinity;
  8431. ELSE
  8432. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8433. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8434. (* already handled varpar *)
  8435. (*
  8436. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8437. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8438. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8439. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8440. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. END;
  8442. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8443. result := Infinity
  8444. END;
  8445. *)
  8446. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8447. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8448. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8449. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8450. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8451. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8452. END;
  8453. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8454. result := Infinity
  8455. END;
  8456. ELSE
  8457. result := Infinity
  8458. END;
  8459. ELSE
  8460. INC(result,distance);
  8461. END;
  8462. END;
  8463. formalParameter := formalParameter.nextParameter; INC(i);
  8464. END;
  8465. END;
  8466. ASSERT(result >= 0);
  8467. RETURN result
  8468. END Distance;
  8469. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8470. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8471. BEGIN
  8472. IF right.numberParameters # (procedureType.numberParameters) THEN
  8473. result := Infinity
  8474. ELSE
  8475. formalParameter := procedureType.firstParameter;
  8476. rightParameter := right.firstParameter;
  8477. i := 0;
  8478. result := 0;
  8479. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8480. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8481. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8482. IF distance = Infinity THEN
  8483. result := Infinity;
  8484. ELSE
  8485. INC(result,distance);
  8486. END;
  8487. formalParameter := formalParameter.nextParameter;
  8488. rightParameter := rightParameter.nextParameter;
  8489. END;
  8490. END;
  8491. ASSERT(result >= 0);
  8492. RETURN result
  8493. END ProcedureTypeDistance;
  8494. (** compute and return the distance between two types, used for computation of signature distance
  8495. from -> to
  8496. **)
  8497. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8498. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8499. BEGIN
  8500. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8501. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8502. END;
  8503. i := Infinity;
  8504. IF from = to THEN
  8505. i := 0
  8506. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8507. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8508. i := Infinity;
  8509. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8510. i := 1
  8511. ELSIF (from IS SyntaxTree.StringType) THEN
  8512. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8513. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8514. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8515. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8516. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8517. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8518. i := 1
  8519. ELSIF (from IS SyntaxTree.NilType) THEN
  8520. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8521. (*
  8522. ELSIF (from = NoType) THEN
  8523. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8524. *)
  8525. ELSIF (from IS SyntaxTree.BasicType) THEN
  8526. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8527. IF varpar & (i # 0) THEN i := Infinity END;
  8528. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8529. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8530. ELSIF (from IS SyntaxTree.RecordType) THEN
  8531. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8532. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8533. IF to IS SyntaxTree.MathArrayType THEN
  8534. (*
  8535. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8536. i := Infinity;
  8537. ELSE
  8538. *)
  8539. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8540. (*
  8541. END;
  8542. *)
  8543. END
  8544. ELSIF (from IS SyntaxTree.PointerType) THEN
  8545. ptr := from(SyntaxTree.PointerType);
  8546. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8547. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8548. (* ELSE i := TypeDistance(ptr.base, to); *)
  8549. END
  8550. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8551. IF (to IS SyntaxTree.ProcedureType) THEN
  8552. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8553. END;
  8554. ELSIF (from IS SyntaxTree.PortType) THEN
  8555. IF (to IS SyntaxTree.PortType) THEN
  8556. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8557. i := 0;
  8558. END;
  8559. END;
  8560. (*no procedure test, procedure must be the same*)
  8561. END;
  8562. RETURN i
  8563. END TypeDistance;
  8564. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8565. BEGIN
  8566. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8567. END IsIntegerType;
  8568. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8569. BEGIN
  8570. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8571. END IsAddressType;
  8572. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8573. BEGIN
  8574. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8575. END IsSizeType;
  8576. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8577. BEGIN
  8578. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8579. END IsSignedIntegerType;
  8580. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8581. BEGIN
  8582. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8583. END IsUnsignedIntegerType;
  8584. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8585. VAR result: BOOLEAN;
  8586. BEGIN
  8587. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8588. value := x.resolved(SyntaxTree.IntegerValue).value;
  8589. result := TRUE
  8590. ELSE
  8591. result := FALSE
  8592. END;
  8593. RETURN result
  8594. END IsIntegerValue;
  8595. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8596. VAR result: BOOLEAN;
  8597. BEGIN
  8598. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8599. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8600. result := TRUE
  8601. ELSE
  8602. result := FALSE
  8603. END;
  8604. RETURN result
  8605. END IsEnumerationValue;
  8606. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8607. VAR result: BOOLEAN;
  8608. BEGIN
  8609. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8610. value := x.resolved(SyntaxTree.RealValue).value;
  8611. result := TRUE
  8612. ELSE
  8613. result := FALSE
  8614. END;
  8615. RETURN result
  8616. END IsRealValue;
  8617. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8618. VAR result: BOOLEAN;
  8619. BEGIN
  8620. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8621. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8622. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8623. result := TRUE
  8624. ELSE
  8625. result := FALSE
  8626. END;
  8627. RETURN result
  8628. END IsComplexValue;
  8629. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8630. VAR result: BOOLEAN;
  8631. BEGIN
  8632. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8633. value := x.resolved(SyntaxTree.CharacterValue).value;
  8634. result := TRUE
  8635. ELSE
  8636. result := FALSE
  8637. END;
  8638. RETURN result
  8639. END IsCharacterValue;
  8640. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8641. VAR result: BOOLEAN;
  8642. BEGIN
  8643. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8644. value := x.resolved(SyntaxTree.BooleanValue).value;
  8645. result := TRUE
  8646. ELSE
  8647. result := FALSE
  8648. END;
  8649. RETURN result
  8650. END IsBooleanValue;
  8651. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8652. VAR result: BOOLEAN;
  8653. BEGIN
  8654. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8655. value := x.resolved(SyntaxTree.SetValue).value;
  8656. result := TRUE
  8657. ELSE
  8658. result := FALSE
  8659. END;
  8660. RETURN result
  8661. END IsSetValue;
  8662. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8663. VAR result: BOOLEAN;
  8664. BEGIN
  8665. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8666. value := x.resolved(SyntaxTree.StringValue).value;
  8667. result := TRUE
  8668. ELSE
  8669. result := FALSE
  8670. END;
  8671. RETURN result
  8672. END IsStringValue;
  8673. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8674. BEGIN
  8675. x := x.resolved;
  8676. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8677. END Indexable;
  8678. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8679. BEGIN
  8680. RETURN t1.SameType(t2.resolved);
  8681. END SameType;
  8682. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8683. BEGIN
  8684. IF t IS SyntaxTree.MathArrayType THEN
  8685. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8686. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8687. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8688. DEC(max);
  8689. END;
  8690. ELSIF t IS SyntaxTree.ArrayType THEN
  8691. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8692. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8693. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8694. END;
  8695. END;
  8696. RETURN t;
  8697. END ArrayBase;
  8698. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8699. BEGIN
  8700. type := type.resolved;
  8701. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8702. base := type(SyntaxTree.ArrayType).arrayBase;
  8703. RETURN TRUE;
  8704. END;
  8705. RETURN FALSE;
  8706. END IsOpenArray;
  8707. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8708. BEGIN
  8709. type := type.resolved;
  8710. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8711. base := type(SyntaxTree.ArrayType).arrayBase;
  8712. dim := type(SyntaxTree.ArrayType).staticLength;
  8713. RETURN TRUE
  8714. ELSE
  8715. RETURN FALSE
  8716. END;
  8717. END IsStaticArray;
  8718. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN
  8720. type := type.resolved;
  8721. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8722. base := type(SyntaxTree.ArrayType).arrayBase;
  8723. RETURN TRUE
  8724. ELSE
  8725. RETURN FALSE
  8726. END;
  8727. END IsDynamicArray;
  8728. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8729. VAR i: LONGINT;
  8730. BEGIN
  8731. i := 0;
  8732. t := t.resolved;
  8733. IF t IS SyntaxTree.MathArrayType THEN
  8734. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8735. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8736. END;
  8737. ELSIF t IS SyntaxTree.ArrayType THEN
  8738. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8739. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8740. END;
  8741. END;
  8742. RETURN i
  8743. END Dimension;
  8744. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8745. BEGIN
  8746. RETURN expression.assignable;
  8747. END IsVariable;
  8748. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8749. BEGIN
  8750. IF (symbol IS SyntaxTree.Parameter) THEN
  8751. WITH symbol: SyntaxTree.Parameter DO
  8752. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8753. END;
  8754. ELSE
  8755. RETURN FALSE
  8756. END;
  8757. END IsVariableParameter;
  8758. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8759. VAR result: BOOLEAN;
  8760. BEGIN
  8761. IF type = NIL THEN result := FALSE
  8762. ELSE
  8763. type := type.resolved;
  8764. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8765. END;
  8766. RETURN result
  8767. END IsPointerType;
  8768. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8769. VAR result: BOOLEAN;
  8770. BEGIN
  8771. IF type = NIL THEN result := FALSE
  8772. ELSE
  8773. type := type.resolved;
  8774. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8775. END;
  8776. RETURN result
  8777. END IsUnsafePointer;
  8778. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8779. BEGIN
  8780. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8781. END IsDisposable;
  8782. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8783. VAR result: BOOLEAN;
  8784. BEGIN
  8785. IF type = NIL THEN result := FALSE
  8786. ELSE
  8787. type := type.resolved;
  8788. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8789. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8790. result := result OR (type IS SyntaxTree.ObjectType);
  8791. END;
  8792. RETURN result
  8793. END IsPointerToRecord;
  8794. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8795. VAR result: BOOLEAN;
  8796. BEGIN
  8797. IF type = NIL THEN result := FALSE
  8798. ELSE
  8799. type := type.resolved;
  8800. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8801. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8802. ;
  8803. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8804. result := result OR (type IS SyntaxTree.ObjectType);
  8805. END;
  8806. RETURN result
  8807. END IsPointerToObject;
  8808. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8809. BEGIN
  8810. IF type # NIL THEN
  8811. RETURN type.resolved.hasPointers
  8812. ELSE
  8813. RETURN FALSE
  8814. END;
  8815. END ContainsPointer;
  8816. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8817. BEGIN
  8818. IF type = NIL THEN RETURN FALSE END;
  8819. type := type.resolved;
  8820. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8821. END IsStringType;
  8822. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8823. BEGIN
  8824. IF type = NIL THEN RETURN FALSE END;
  8825. type := type.resolved;
  8826. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8827. END IsCharacterType;
  8828. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8829. BEGIN
  8830. IF type = NIL THEN RETURN FALSE END;
  8831. type := type.resolved;
  8832. RETURN (type IS SyntaxTree.EnumerationType)
  8833. END IsEnumerationType;
  8834. (** cf. section "Type extension (base type)" in the language report **)
  8835. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8836. VAR result: BOOLEAN;
  8837. BEGIN
  8838. ASSERT(base # NIL); ASSERT(extension # NIL);
  8839. base := base.resolved; extension := extension.resolved;
  8840. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8841. result := TRUE;
  8842. ELSE
  8843. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8844. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8845. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8846. END;
  8847. WHILE (extension # NIL) & (extension # base) DO
  8848. IF extension IS SyntaxTree.RecordType THEN
  8849. extension := extension(SyntaxTree.RecordType).baseType;
  8850. IF (extension # NIL) THEN extension := extension.resolved END;
  8851. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8852. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8853. END;
  8854. ELSE extension := NIL;
  8855. END;
  8856. END;
  8857. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8858. END;
  8859. RETURN result
  8860. END IsTypeExtension;
  8861. (** check if base is the base enumeration type of extension **)
  8862. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8863. BEGIN
  8864. base := base.resolved; extension := extension.resolved;
  8865. WHILE (extension # NIL) & (extension # base) DO
  8866. IF extension IS SyntaxTree.EnumerationType THEN
  8867. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8868. IF extension # NIL THEN extension := extension.resolved END;
  8869. ELSE
  8870. extension := NIL
  8871. END;
  8872. END;
  8873. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8874. END IsEnumerationExtension;
  8875. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8876. BEGIN
  8877. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8878. RETURN TRUE
  8879. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8880. RETURN TRUE
  8881. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8882. RETURN TRUE
  8883. ELSE
  8884. RETURN FALSE
  8885. END
  8886. END IsCallable;
  8887. (** compute and return the distance of two record types
  8888. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8889. **)
  8890. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8891. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8892. BEGIN
  8893. i := 0;
  8894. WHILE (from # NIL) & (from # to) DO
  8895. baseType := from.baseType;
  8896. IF (baseType # NIL) THEN
  8897. baseType := baseType.resolved;
  8898. IF baseType IS SyntaxTree.PointerType THEN
  8899. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8900. END;
  8901. IF baseType IS SyntaxTree.RecordType THEN
  8902. from := baseType(SyntaxTree.RecordType);
  8903. ELSE
  8904. from := NIL;
  8905. END;
  8906. ELSE
  8907. from := NIL
  8908. END;
  8909. INC(i)
  8910. END;
  8911. IF from = NIL THEN i := Infinity END;
  8912. RETURN i
  8913. END RecordTypeDistance;
  8914. (** compute and return the distance of two pointer types **)
  8915. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8916. BEGIN
  8917. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8918. RETURN Infinity;
  8919. ELSE
  8920. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8921. END;
  8922. END PointerTypeDistance;
  8923. (** check if expression contains a symbol designator pointing to a type declaration.
  8924. - if so then enter type declaration into typeDeclaration and return true else return false
  8925. **)
  8926. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8927. VAR result: BOOLEAN;
  8928. BEGIN
  8929. result := FALSE;
  8930. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8931. result := TRUE;
  8932. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8933. END;
  8934. RETURN result
  8935. END IsTypeDesignator;
  8936. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8937. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8938. VAR result: BOOLEAN;
  8939. BEGIN
  8940. type := type.resolved;
  8941. IF type IS SyntaxTree.PointerType THEN
  8942. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8943. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8944. result := TRUE
  8945. ELSE
  8946. result := type IS SyntaxTree.RecordType
  8947. END;
  8948. RETURN result
  8949. END IsExtensibleType;
  8950. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8951. BEGIN
  8952. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8953. (d IS SyntaxTree.SymbolDesignator) &
  8954. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8955. OR
  8956. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8957. END IsUnextensibleRecord;
  8958. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8959. BEGIN
  8960. IF IsUnextensibleRecord(d) THEN
  8961. RETURN FALSE
  8962. ELSE RETURN IsExtensibleType(d.type.resolved)
  8963. END;
  8964. END IsExtensibleDesignator;
  8965. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8966. BEGIN
  8967. type := type.resolved;
  8968. IF (type IS SyntaxTree.PointerType) THEN
  8969. RETURN TRUE
  8970. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8971. RETURN TRUE
  8972. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8973. RETURN TRUE
  8974. ELSIF (type IS SyntaxTree.BasicType) THEN
  8975. RETURN TRUE
  8976. END;
  8977. RETURN FALSE
  8978. END IsBasicType;
  8979. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8980. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8981. BEGIN
  8982. baseType := record.baseType;
  8983. IF (baseType # NIL) THEN
  8984. baseType := baseType.resolved;
  8985. IF (baseType IS SyntaxTree.PointerType) THEN
  8986. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8987. END;
  8988. END;
  8989. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8990. recordType := baseType(SyntaxTree.RecordType);
  8991. ELSE
  8992. recordType := NIL;
  8993. END;
  8994. RETURN recordType
  8995. END RecordBase;
  8996. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8997. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8998. BEGIN
  8999. baseRecord := RecordBase(scope.ownerRecord);
  9000. IF baseRecord = NIL THEN RETURN NIL END;
  9001. scope := baseRecord.recordScope;
  9002. procedureType := procedure.type.resolved;
  9003. IF procedure IS SyntaxTree.Operator THEN
  9004. operator := scope.firstOperator;
  9005. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9006. (*
  9007. Printout.Info("not same ",procedureType);
  9008. Printout.Info("with ",operator.type);
  9009. *)
  9010. operator := operator.nextOperator;
  9011. END;
  9012. super := operator;
  9013. ELSE
  9014. super := scope.firstProcedure;
  9015. WHILE (super # NIL) & (super.name # procedure.name) DO
  9016. super := super.nextProcedure;
  9017. END;
  9018. END;
  9019. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9020. RETURN super
  9021. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9022. RETURN super
  9023. ELSE
  9024. RETURN FindSuperProcedure(scope,procedure);
  9025. END;
  9026. END FindSuperProcedure;
  9027. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9028. VAR procedure: SyntaxTree.Procedure;
  9029. BEGIN
  9030. procedure := record.recordScope.constructor;
  9031. IF procedure = NIL THEN
  9032. record := RecordBase(record);
  9033. IF record # NIL THEN
  9034. procedure := GetConstructor(record)
  9035. END;
  9036. END;
  9037. RETURN procedure;
  9038. END GetConstructor;
  9039. (* enter a case into a list of cases in a sorted way and check for collision *)
  9040. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9041. VAR prev,this,new: SyntaxTree.CaseConstant;
  9042. BEGIN
  9043. this := root;
  9044. prev := NIL;
  9045. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9046. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9047. RETURN FALSE
  9048. ELSE
  9049. IF (this # NIL) & (this.min = max+1) THEN
  9050. this.min := min
  9051. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9052. prev.max := min
  9053. ELSE
  9054. NEW(new); new.min := min; new.max := max;
  9055. new.next := this;
  9056. IF prev = NIL THEN
  9057. root := new;
  9058. ELSE
  9059. prev.next := new
  9060. END
  9061. END;
  9062. RETURN TRUE
  9063. END;
  9064. END EnterCase;
  9065. (** generate and return a new checker object, errors are entered into diagnostics **)
  9066. 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;
  9067. VAR checker: Checker;
  9068. BEGIN
  9069. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9070. RETURN checker
  9071. END NewChecker;
  9072. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9073. VAR warnings: Warnings;
  9074. BEGIN
  9075. NEW(warnings, diagnostics); RETURN warnings;
  9076. END NewWarnings;
  9077. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9078. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9079. END IsRangeType;
  9080. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9081. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9082. END IsMathArrayType;
  9083. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9084. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9085. END IsArrayType;
  9086. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9087. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9088. END IsComplexType;
  9089. (** if a type is an array-structured object type *)
  9090. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9091. VAR recordType: SyntaxTree.RecordType;
  9092. BEGIN
  9093. IF type = NIL THEN
  9094. RETURN FALSE
  9095. ELSE
  9096. type := type.resolved;
  9097. IF type IS SyntaxTree.PointerType THEN
  9098. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9099. IF type IS SyntaxTree.RecordType THEN
  9100. recordType := type(SyntaxTree.RecordType);
  9101. RETURN recordType.isObject & recordType.HasArrayStructure()
  9102. ELSE
  9103. RETURN FALSE
  9104. END
  9105. ELSE
  9106. RETURN FALSE
  9107. END
  9108. END
  9109. END IsArrayStructuredObjectType;
  9110. (** the math array structure of a type
  9111. - for math arrays: the array itself
  9112. - for pointers: the math array structure of the pointer base
  9113. - for array-structured object types: the underlying structure
  9114. - for non-math arrays and all other types: NIL
  9115. **)
  9116. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9117. VAR
  9118. result: SyntaxTree.MathArrayType;
  9119. BEGIN
  9120. IF type = NIL THEN
  9121. result := NIL
  9122. ELSE
  9123. type := type.resolved;
  9124. IF type IS SyntaxTree.PointerType THEN
  9125. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9126. END;
  9127. IF type IS SyntaxTree.MathArrayType THEN
  9128. result := type(SyntaxTree.MathArrayType)
  9129. ELSIF type IS SyntaxTree.RecordType THEN
  9130. result := type(SyntaxTree.RecordType).arrayStructure
  9131. ELSE
  9132. result := NIL
  9133. END
  9134. END;
  9135. RETURN result
  9136. END MathArrayStructureOfType;
  9137. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9138. VAR
  9139. result: BOOLEAN;
  9140. rangeExpression: SyntaxTree.RangeExpression;
  9141. BEGIN
  9142. IF x IS SyntaxTree.RangeExpression THEN
  9143. rangeExpression := x(SyntaxTree.RangeExpression);
  9144. result := TRUE;
  9145. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9146. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9147. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9148. ELSE
  9149. result := FALSE
  9150. END;
  9151. RETURN result
  9152. END IsStaticRange;
  9153. (** whether a type is a math array of tensor form **)
  9154. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9155. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9156. END IsTensor;
  9157. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9158. BEGIN
  9159. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9160. length := type(SyntaxTree.MathArrayType).staticLength;
  9161. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9162. RETURN TRUE
  9163. ELSE
  9164. RETURN FALSE
  9165. END;
  9166. END IsStaticMathArray;
  9167. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9168. BEGIN
  9169. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9170. END SymbolHasAddress;
  9171. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9172. BEGIN
  9173. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9174. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9175. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9176. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9177. END HasAddress;
  9178. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9179. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9180. BEGIN
  9181. IF (e IS SyntaxTree.Designator) THEN
  9182. d := e(SyntaxTree.Designator);
  9183. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9184. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9185. e := d.left;
  9186. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9187. END;
  9188. IF d # NIL THEN
  9189. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9190. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9191. END;
  9192. END;
  9193. RETURN FALSE;
  9194. END IsLocalVariable;
  9195. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9196. BEGIN
  9197. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9198. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9199. ELSE
  9200. RETURN TRUE
  9201. END;
  9202. END IsStaticProcedure;
  9203. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9204. CONST OptimizeMethodTable = FALSE;
  9205. BEGIN
  9206. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9207. END InMethodTable;
  9208. END FoxSemanticChecker.