FoxSemanticChecker.Mod 366 KB

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