FoxSemanticChecker.Mod 394 KB

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