FoxSemanticChecker.Mod 388 KB

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