FoxSemanticChecker.Mod 391 KB

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