FoxSemanticChecker.Mod 404 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253
  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. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. inConversion: LONGINT;
  78. (* temporary variables for the visitors
  79. they replace variables on a stack during use of the visitor pattern and may only be
  80. - set in AcceptXXX procedures
  81. - set and read in ResolveXXX procedures
  82. *)
  83. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  84. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  85. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  86. currentScope-: SyntaxTree.Scope;
  87. currentIsRealtime: BOOLEAN;
  88. currentIsUnreachable: BOOLEAN;
  89. currentIsCellNet: BOOLEAN;
  90. currentIsBodyProcedure: BOOLEAN;
  91. currentIsExclusive: BOOLEAN;
  92. global: SyntaxTree.ModuleScope;
  93. withEntries: WithEntry;
  94. activeCellsStatement: BOOLEAN;
  95. replacements*: Replacement;
  96. cellsAreObjects: BOOLEAN;
  97. variableAccessed: BOOLEAN;
  98. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  99. BEGIN
  100. SELF.diagnostics := diagnostics;
  101. SELF.useDarwinCCalls := useDarwinCCalls;
  102. SELF.cooperative := cooperative;
  103. SELF.system := system;
  104. SELF.symbolFileFormat := symbolFileFormat;
  105. error := FALSE;
  106. NEW(typeFixes);
  107. NEW(pointerFixes);
  108. resolvedType := NIL;
  109. resolvedExpression := NIL;
  110. resolvedStatement := NIL;
  111. currentScope := NIL;
  112. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  113. SELF.importCache := importCache;
  114. arrayBaseImported := FALSE;
  115. complexNumbersImported := FALSE;
  116. SELF.VerboseErrorMessage := verboseErrorMessage;
  117. global := NIL;
  118. phase := UndefinedPhase;
  119. currentIsRealtime := FALSE;
  120. currentIsUnreachable := FALSE;
  121. currentIsCellNet := FALSE;
  122. currentIsBodyProcedure := FALSE;
  123. currentIsExclusive := FALSE;
  124. withEntries := NIL;
  125. SELF.cellsAreObjects := system.cellsAreObjects;
  126. COPY(backend, backendName);
  127. inConversion := 0;
  128. END InitChecker;
  129. (** report error **)
  130. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  131. VAR errModule: SyntaxTree.Module;
  132. BEGIN
  133. ASSERT(currentScope # NIL);
  134. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  135. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  136. error := TRUE;
  137. END Error;
  138. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  139. VAR errModule: SyntaxTree.Module;
  140. BEGIN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  143. END Warning;
  144. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  145. VAR errorMessage: ARRAY 256 OF CHAR;
  146. BEGIN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  149. error := TRUE;
  150. END ErrorSS;
  151. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  152. VAR msg, msg2: ARRAY 256 OF CHAR;
  153. BEGIN
  154. COPY(msg1, msg);
  155. Strings.Append(msg, " = ");
  156. Basic.GetString(s, msg2);
  157. Strings.Append(msg, msg2);
  158. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  159. END InfoSS;
  160. (*** symbol lookup ***)
  161. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  162. **)
  163. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  164. VAR
  165. scope,baseScope: SyntaxTree.Scope;
  166. symbol, s: SyntaxTree.Symbol;
  167. ownerRecord,base: SyntaxTree.RecordType;
  168. BEGIN
  169. scope := inScope;
  170. symbol := NIL;
  171. WHILE (scope # NIL) & (symbol = NIL) DO
  172. symbol := scope.FindSymbol(name);
  173. s := NIL;
  174. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  175. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  177. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  178. END;
  179. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  180. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  181. ELSE
  182. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  183. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  184. symbol.MarkUsed;
  185. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  186. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  187. base := RecordBase(ownerRecord);
  188. IF (base # NIL) THEN
  189. baseScope := base.recordScope;
  190. symbol := Find(baseScope,name,FALSE);
  191. ELSE
  192. symbol := NIL;
  193. END;
  194. ELSE
  195. symbol := NIL;
  196. END;
  197. END;
  198. END;
  199. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  200. END;
  201. IF (symbol # NIL) THEN
  202. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  203. ASSERT(phase = DeclarationPhase);
  204. ResolveSymbol(symbol)
  205. END;
  206. symbol.MarkUsed;
  207. END;
  208. RETURN symbol
  209. END Find;
  210. (*** types ***)
  211. (** find type declaration with name qualifiedIdentifier and return resolved type
  212. - check qualified identifier prefix, set scope to module scope if appropriate
  213. - check suffix in scope
  214. **)
  215. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  216. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  217. BEGIN
  218. result := NIL;
  219. prevScope := currentScope;
  220. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  221. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  222. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  223. IF symbol(SyntaxTree.Import).module = NIL THEN
  224. Error(qualifiedIdentifier.position,"module not loaded");
  225. result := SyntaxTree.invalidType;
  226. symbol := NIL;
  227. ELSE
  228. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  229. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  230. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  231. IF VerboseErrorMessage THEN
  232. Printout.Info("scope", currentScope);
  233. Printout.Info("symbol", symbol);
  234. END;
  235. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  236. END;
  237. END;
  238. ELSE
  239. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  240. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  241. symbol := NIL;
  242. END;
  243. ELSE
  244. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  245. IF symbol = NIL THEN
  246. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  247. IF VerboseErrorMessage THEN
  248. Printout.Info("Qualident",qualifiedIdentifier);
  249. Printout.Info("in scope",currentScope) ;
  250. END;
  251. END;
  252. END;
  253. IF symbol = NIL THEN (* error already handled *)
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  257. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  258. typeDeclaration := NIL;
  259. result := SyntaxTree.invalidType;
  260. ELSE
  261. currentScope := symbol.scope;
  262. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  263. result := ResolveType(typeDeclaration.declaredType);
  264. symbol.MarkUsed;
  265. ASSERT(result # NIL);
  266. END;
  267. currentScope := prevScope;
  268. RETURN result
  269. END ResolveNamedType;
  270. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  271. If node is currently being resolved then emit a cyclic definition error.
  272. Return TRUE only if node is fully resolved.
  273. **)
  274. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  275. VAR result: BOOLEAN;
  276. BEGIN
  277. IF SyntaxTree.Resolved IN x.state THEN
  278. result := FALSE
  279. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  280. Error(x.position,"cyclic definition");
  281. result := FALSE;
  282. ELSE
  283. result := TRUE;
  284. x.SetState(SyntaxTree.BeingResolved)
  285. END;
  286. RETURN result
  287. END TypeNeedsResolution;
  288. (** Return invalid type if x is currently being resolved, return x otherwise**)
  289. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  290. BEGIN
  291. IF SyntaxTree.Resolved IN x.state THEN
  292. RETURN x
  293. ELSE
  294. RETURN SyntaxTree.invalidType
  295. END;
  296. END ResolvedType;
  297. PROCEDURE VisitType*(x: SyntaxTree.Type);
  298. BEGIN
  299. ASSERT(x = SyntaxTree.invalidType);
  300. END VisitType;
  301. (** resolve basic type **)
  302. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  303. BEGIN
  304. IF TypeNeedsResolution(x) THEN
  305. x.SetState(SyntaxTree.Resolved);
  306. END;
  307. resolvedType := ResolvedType(x)
  308. END VisitBasicType;
  309. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  310. BEGIN
  311. VisitBasicType(x);
  312. END VisitByteType;
  313. (** resolve character type **)
  314. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitCharacterType;
  318. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitBooleanType;
  322. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitSetType;
  326. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitAddressType;
  330. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitSizeType;
  334. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitAnyType;
  338. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitObjectType;
  342. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  343. BEGIN
  344. VisitBasicType(x);
  345. END VisitNilType;
  346. (** resolve integer type **)
  347. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitIntegerType;
  351. (** resolve real type **)
  352. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitFloatType;
  356. (** resolve complex type **)
  357. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitComplexType;
  361. (**
  362. resolve string type: nothing to be done
  363. **)
  364. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  365. BEGIN
  366. IF TypeNeedsResolution(x) THEN
  367. x.SetState(SyntaxTree.Resolved);
  368. END;
  369. resolvedType := ResolvedType(x)
  370. END VisitStringType;
  371. (**
  372. check enumeration scope: enter symbols and check for duplicate names
  373. **)
  374. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  375. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  376. BEGIN
  377. prevScope := currentScope;
  378. currentScope := x;
  379. e := x.firstConstant;
  380. nextHighest := highest;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  408. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: Basic.Integer;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position, "base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  479. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  480. ELSE
  481. x.SetArrayBase(pointerType);
  482. END;
  483. END;
  484. IF x.length # NIL THEN
  485. variableAccessed := FALSE;
  486. e := ResolveExpression(x.length);
  487. IF (e.resolved = NIL) THEN
  488. IF variableAccessed THEN
  489. Error(e.position, "forbidden variable access");
  490. END;
  491. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. x.SetState(SyntaxTree.Resolved);
  572. END;
  573. resolvedType := ResolvedType(x);
  574. END VisitMathArrayType;
  575. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  576. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  577. (1) Rec = RECORD ... END; Ptr <---> Rec
  578. Ptr = POINTER TO Rec; ^ |
  579. | |
  580. TypeDesc TypeDesc
  581. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  582. ^ /
  583. | /
  584. TypeDesc <-- /
  585. *)
  586. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  587. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  588. BEGIN
  589. Strings.IntToStr(x.position.start,number);
  590. COPY(prefix,name);
  591. Strings.Append(name,"@");
  592. Strings.Append(name,number);
  593. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  594. typeDeclaration.SetDeclaredType(x);
  595. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  596. x.SetTypeDeclaration(typeDeclaration);
  597. currentScope.AddTypeDeclaration(typeDeclaration);
  598. typeDeclaration.SetScope(currentScope);
  599. END AnonymousTypeDeclaration;
  600. (**
  601. deferred pointer type resolving
  602. - resolve base type
  603. - check that base type is a record or array type
  604. - if error then set base type to invalid type
  605. **)
  606. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  607. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  608. BEGIN
  609. ASSERT(type.pointerBase # NIL);
  610. position := type.pointerBase.position;
  611. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  612. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  613. (* not for pointers, a type is needed for the records only
  614. IF type.typeDeclaration = NIL THEN
  615. AnonymousTypeDeclaration(type);
  616. END;
  617. *)
  618. END;
  619. resolved := ResolveType(type.pointerBase);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  621. type.SetPointerBase(resolved);
  622. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  623. recordType := resolved.resolved(SyntaxTree.RecordType);
  624. IF recordType.isObject & (recordType.baseType # NIL) THEN
  625. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  626. Error(position, "base type of object must be a realtime object");
  627. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  628. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  629. END;
  630. END;
  631. END;
  632. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  633. Error(position, "realtime object contains references to non-realtime objects");
  634. END;
  635. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  636. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  637. Error(position, "forbidden unsafe at static array");
  638. ELS
  639. *)
  640. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  641. Error(position, "forbidden unsafe at multidimensional array");
  642. END;
  643. END;
  644. ELSE
  645. Error(position, "forbidden pointer base type");
  646. type.SetPointerBase(SyntaxTree.invalidType)
  647. END
  648. END FixPointerType;
  649. (**
  650. resolve pointer type
  651. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  652. **)
  653. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  654. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  655. modifiers: SyntaxTree.Modifier; position: Position;
  656. BEGIN
  657. IF TypeNeedsResolution(x) THEN
  658. modifiers := x.modifiers;
  659. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  660. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  661. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  662. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  663. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  664. (* inheritance cycle check
  665. example:
  666. A=POINTER TO RECORD(B) END;
  667. B=POINTER TO RECORD(A) END;
  668. *)
  669. IF x.pointerBase IS SyntaxTree.RecordType THEN
  670. recordType := x.pointerBase(SyntaxTree.RecordType);
  671. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  672. recordBaseType := ResolveType(recordType.baseType);
  673. recordType.SetBaseType(recordBaseType);
  674. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  675. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  676. END;
  677. CheckModifiers(modifiers, TRUE);
  678. typeFixes.Add(x,currentScope);
  679. x.SetState(SyntaxTree.Resolved);
  680. END;
  681. resolvedType := ResolvedType(x)
  682. END VisitPointerType;
  683. (**
  684. resolve port type
  685. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  686. **)
  687. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  688. VAR value: Basic.Integer;
  689. BEGIN
  690. IF TypeNeedsResolution(x) THEN
  691. x.SetCellsAreObjects(cellsAreObjects);
  692. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  693. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  694. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  695. ELSE
  696. x.SetSize(system.SizeOf(system.longintType));
  697. END;
  698. x.SetState(SyntaxTree.Resolved);
  699. END;
  700. resolvedType := ResolvedType(x)
  701. END VisitPortType;
  702. (**
  703. deferred procedure type resolving
  704. - resolve return type
  705. - traverse and resolve parameters
  706. **)
  707. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  708. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  709. BEGIN
  710. resolved := ResolveType(procedureType.returnType);
  711. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  712. Error(procedureType.position,"forbidden open array return type");
  713. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  714. Error(procedureType.position,"procedure with return type does not return");
  715. END;
  716. procedureType.SetReturnType(resolved);
  717. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  718. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  719. parameter.SetType(procedureType.returnType);
  720. parameter.SetAccess(SyntaxTree.Hidden);
  721. parameter.SetUntraced(procedureType.hasUntracedReturn);
  722. VisitParameter(parameter);
  723. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  724. END;
  725. (* process parameters *)
  726. parameter :=procedureType.firstParameter;
  727. WHILE (parameter # NIL) DO
  728. VisitParameter(parameter);
  729. parameter := parameter.nextParameter;
  730. END;
  731. parameter := procedureType.selfParameter;
  732. IF parameter # NIL THEN
  733. VisitParameter(parameter)
  734. END;
  735. END FixProcedureType;
  736. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  737. VAR prev,this: SyntaxTree.Modifier;
  738. BEGIN
  739. this := modifiers;prev := NIL;
  740. WHILE (this # NIL) & (this.identifier # name) DO
  741. prev := this; this := this.nextModifier;
  742. END;
  743. IF this # NIL THEN
  744. IF this.expression # NIL THEN
  745. Error(this.position,"unexpected expression");
  746. END;
  747. this.Resolved;
  748. position := this.position;
  749. RETURN TRUE
  750. ELSE
  751. RETURN FALSE
  752. END;
  753. END HasFlag;
  754. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  755. VAR prev,this: SyntaxTree.Modifier;
  756. BEGIN
  757. this := modifiers;prev := NIL;
  758. WHILE (this # NIL) & (this.identifier # name) DO
  759. prev := this; this := this.nextModifier;
  760. END;
  761. IF this # NIL THEN
  762. IF this.expression = NIL THEN
  763. Error(this.position,"expected expression value");
  764. ELSE
  765. this.SetExpression(ConstantExpression(this.expression));
  766. IF CheckIntegerValue(this.expression,value) THEN END;
  767. END;
  768. this.Resolved;
  769. position := this.position;
  770. RETURN TRUE
  771. ELSE RETURN FALSE
  772. END;
  773. END HasValue;
  774. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  775. VAR prev,this: SyntaxTree.Modifier;
  776. BEGIN
  777. this := modifiers;prev := NIL;
  778. WHILE (this # NIL) & (this.identifier # name) DO
  779. prev := this; this := this.nextModifier;
  780. END;
  781. IF this # NIL THEN
  782. IF this.expression = NIL THEN
  783. Error(this.position,"expected expression value");
  784. ELSE
  785. this.SetExpression(ConstantExpression(this.expression));
  786. IF CheckStringValue(this.expression,value) THEN END;
  787. END;
  788. this.Resolved;
  789. position := this.position;
  790. RETURN TRUE
  791. ELSE RETURN FALSE
  792. END;
  793. END HasStringValue;
  794. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  795. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  796. BEGIN
  797. IF cellsAreObjects THEN RETURN FALSE END;
  798. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  799. IF svalue = "A2" THEN
  800. RETURN TRUE
  801. END;
  802. END;
  803. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  804. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  805. END;
  806. RETURN FALSE;
  807. (*
  808. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  809. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  810. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  811. IF svalue[0] = "~" THEN
  812. Strings.TrimLeft(svalue, "~");
  813. IF svalue = backendName THEN
  814. RETURN TRUE;
  815. END;
  816. ELSIF svalue # backendName THEN
  817. RETURN TRUE;
  818. END;
  819. END;
  820. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  821. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  822. END;
  823. RETURN FALSE;
  824. *)
  825. END SkipImplementation;
  826. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  827. VAR this: SyntaxTree.Modifier;
  828. BEGIN
  829. this := modifiers;
  830. WHILE this # NIL DO
  831. IF ~this.resolved THEN
  832. IF checkUse THEN
  833. Error(this.position,"unexpected modifier");
  834. ELSE
  835. this.SetExpression(ResolveExpression(this.expression));
  836. this.Resolved;
  837. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  838. END;
  839. END;
  840. this := this.nextModifier
  841. END;
  842. END CheckModifiers;
  843. (**
  844. resolve procedure type
  845. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  846. **)
  847. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  848. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  849. BEGIN
  850. IF TypeNeedsResolution(procedureType) THEN
  851. modifiers := procedureType.modifiers;
  852. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  853. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  854. procedureType.SetInterrupt(TRUE);
  855. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  856. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  857. IF useDarwinCCalls THEN (*fld*)
  858. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  859. ELSE
  860. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  861. END
  862. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  863. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  864. Error(position, "undefined platform calling convention");
  865. ELSE
  866. procedureType.SetCallingConvention(system.platformCallingConvention);
  867. END;
  868. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  869. procedureType.SetNoReturn(TRUE);
  870. END;
  871. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  872. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  873. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  874. CheckModifiers(modifiers, TRUE);
  875. modifiers := procedureType.returnTypeModifiers;
  876. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  877. CheckModifiers(modifiers, TRUE);
  878. typeFixes.Add(procedureType,currentScope);
  879. procedureType.SetHasPointers(procedureType.isDelegate);
  880. procedureType.SetState(SyntaxTree.Resolved);
  881. END;
  882. resolvedType := ResolvedType(procedureType)
  883. END VisitProcedureType;
  884. (** check and resolve record type
  885. - check base type: must be record, math array or array-structured object type
  886. - check declarations
  887. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  888. **)
  889. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  890. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  891. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  892. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  893. hasPointers: BOOLEAN;
  894. modifiers: SyntaxTree.Modifier;
  895. value: Basic.Integer;
  896. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  897. BEGIN
  898. type := type.resolved;
  899. IF (type IS SyntaxTree.PointerType) &
  900. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  901. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  902. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  903. RETURN TRUE
  904. ELSE
  905. RETURN FALSE
  906. END;
  907. END IsPointerToRecord;
  908. BEGIN
  909. IF TypeNeedsResolution(x) THEN
  910. hasPointers := FALSE;
  911. modifiers := x.modifiers;
  912. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  913. END;
  914. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  915. CheckModifiers(modifiers, TRUE);
  916. IF x.baseType # NIL THEN
  917. position := x.baseType.position;
  918. baseType := ResolveType(x.baseType);
  919. resolved := baseType.resolved;
  920. hasPointers := hasPointers OR resolved.hasPointers;
  921. IF x.isObject THEN (* object *)
  922. ASSERT(x.pointerType # NIL);
  923. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  924. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  925. baseType := NIL
  926. ELSIF IsPointerToRecord(resolved,recordType) THEN
  927. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  928. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  929. ELSE
  930. Error(position,"object does not extend pointer to record, object or math array ")
  931. END;
  932. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  933. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  934. ELSIF IsPointerToRecord(resolved,recordType) THEN
  935. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  936. ELSIF resolved IS SyntaxTree.RecordType THEN
  937. ELSE
  938. Error(position,"pointer to record does not extend pointer to record or record")
  939. END;
  940. ELSE
  941. IF resolved IS SyntaxTree.RecordType THEN
  942. ELSE
  943. Error(position,"record does not extend record")
  944. END;
  945. END;
  946. x.SetBaseType(baseType);
  947. IF x.Level() > 15 THEN
  948. Error(position, "record/object inheritance level too high");
  949. (* note:
  950. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  951. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  952. inheritance history of a type.
  953. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  954. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  955. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  956. *)
  957. END;
  958. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  959. Error(position, "invalid inheritance of disposable types");
  960. END;
  961. END;
  962. Declarations(x.recordScope, FALSE, {0});
  963. x.SetState(SyntaxTree.Resolved);
  964. Declarations(x.recordScope, FALSE, {1});
  965. ResolveArrayStructure(x);
  966. (* computation of sizes and offsets skipped -> done in backend / system *)
  967. recordBase := x.GetBaseRecord();
  968. IF recordBase = NIL THEN numberMethods := 0
  969. ELSE numberMethods := recordBase.recordScope.numberMethods
  970. END;
  971. isRealtime := TRUE;
  972. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  973. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  974. END;
  975. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  976. WHILE symbol # NIL DO
  977. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  978. IF symbol IS SyntaxTree.Variable THEN
  979. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  980. END;
  981. IF symbol IS SyntaxTree.Procedure THEN
  982. procedure := symbol(SyntaxTree.Procedure);
  983. IF procedure.super # NIL THEN
  984. procedure.SetMethodNumber(procedure.super.methodNumber);
  985. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  986. procedure.SetMethodNumber(numberMethods);
  987. INC(numberMethods);
  988. END;
  989. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  990. Error(procedure.position,"realtime procedure in non-realtime object")
  991. END;
  992. END;
  993. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  994. Error(symbol.position,"non-realtime symbol in realtime object")
  995. END;
  996. symbol := symbol.nextSymbol;
  997. END;
  998. IF isRealtime THEN x.SetRealtime(TRUE) END;
  999. x.recordScope.SetNumberMethods(numberMethods);
  1000. (* TODO: is this needed anymore? *)
  1001. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1002. Error(x.position,"object extends a record")
  1003. END;
  1004. IF ~x.isAbstract THEN
  1005. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1006. Error(x.position, "non-abstract object contains abstract procedure");
  1007. END;
  1008. ELSE
  1009. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1010. Error(x.position, "abstract object does not contain an abstract method");
  1011. END;
  1012. END;
  1013. IF (x.typeDeclaration = NIL) THEN
  1014. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1015. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1016. (*
  1017. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1018. AnonymousTypeDeclaration(x,name);
  1019. *)
  1020. ELSE
  1021. AnonymousTypeDeclaration(x,"Anonymous");
  1022. END;
  1023. END;
  1024. x.SetHasPointers(hasPointers);
  1025. x.SetState(SyntaxTree.Resolved);
  1026. END;
  1027. resolvedType := ResolvedType(x);
  1028. END VisitRecordType;
  1029. (** check and resolve cell type
  1030. - check base type: must be cell
  1031. - check declarations
  1032. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1033. **)
  1034. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1035. VAR
  1036. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1037. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1038. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1039. recordBase: SyntaxTree.RecordType;
  1040. numberMethods: LONGINT;
  1041. int: Basic.Integer;
  1042. real: LONGREAL;
  1043. bool: BOOLEAN;
  1044. set: Basic.Set;
  1045. v: SyntaxTree.Expression;
  1046. str: Scanner.StringType;
  1047. atype: SyntaxTree.ArrayType;
  1048. prev: SyntaxTree.Scope;
  1049. BEGIN
  1050. IF TypeNeedsResolution(x) THEN
  1051. recordBase := NIL;
  1052. IF cellsAreObjects THEN
  1053. IF x.baseType = NIL THEN
  1054. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1055. ImportModule(qualifiedIdentifier.prefix, x.position);
  1056. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1057. x.SetBaseType(ResolveType(x.baseType));
  1058. recordBase := x.GetBaseRecord();
  1059. IF recordBase = NIL THEN
  1060. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1061. END;
  1062. ELSE
  1063. x.SetBaseType(ResolveType(x.baseType));
  1064. END;
  1065. ELSE
  1066. x.SetBaseType(ResolveType(x.baseType));
  1067. END;
  1068. IF recordBase = NIL THEN numberMethods := 0
  1069. ELSE numberMethods := recordBase.recordScope.numberMethods
  1070. END;
  1071. modifier := x.modifiers;
  1072. (*IF ~x.isCellNet THEN*)
  1073. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1074. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1075. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1076. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1077. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1078. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1079. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1080. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1081. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1082. symbol := system.activeCellsCapabilities;
  1083. WHILE symbol # NIL DO
  1084. IF HasFlag(modifier, symbol.name, position) THEN END;
  1085. symbol := symbol.nextSymbol;
  1086. END;
  1087. modifier := x.modifiers;
  1088. WHILE (modifier # NIL) DO
  1089. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1090. IF modifier.expression # NIL THEN
  1091. v := ConstantExpression(modifier.expression);
  1092. property.SetValue(v);
  1093. IF IsIntegerValue(modifier.expression, int) THEN
  1094. (*property.SetValue(modifier.expression);*)
  1095. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1096. property.SetType(system.longintType);
  1097. ELSIF IsRealValue(modifier.expression, real) THEN
  1098. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1099. property.SetType(system.longrealType);
  1100. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1101. property.SetType(system.booleanType);
  1102. ELSIF IsSetValue(modifier.expression, set) THEN
  1103. property.SetType(system.setType);
  1104. ELSIF IsStringValue(modifier.expression, str) THEN
  1105. (*property.SetValue(modifier.expression);*)
  1106. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1107. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1108. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1109. property.SetType(atype);
  1110. ELSE
  1111. Error(modifier.position, "unsupported property type");
  1112. END;
  1113. ELSE (* flag property *)
  1114. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1115. property.SetType(system.booleanType);
  1116. END;
  1117. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1118. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1119. x.AddProperty(property);
  1120. modifier := modifier.nextModifier;
  1121. END;
  1122. CheckModifiers(modifier, FALSE);
  1123. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1124. (* process parameters *)
  1125. prev := currentScope;
  1126. currentScope := x.cellScope;
  1127. parameter :=x.firstParameter;
  1128. WHILE (parameter # NIL) DO
  1129. VisitParameter(parameter);
  1130. type := parameter.type.resolved;
  1131. IF ~(type IS SyntaxTree.PortType) THEN
  1132. WHILE IsStaticArray(type, type, len) DO
  1133. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1134. END;
  1135. WHILE IsDynamicArray(type, type) DO
  1136. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1137. END;
  1138. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1139. Error(parameter.position, "invalid type, must be port or static array of port ");
  1140. END;
  1141. END;
  1142. parameter := parameter.nextParameter;
  1143. END;
  1144. currentScope := prev;
  1145. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1146. WHILE symbol # NIL DO
  1147. IF symbol IS SyntaxTree.Variable THEN
  1148. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1149. END;
  1150. symbol := symbol.nextSymbol;
  1151. END;
  1152. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1153. IF (x.typeDeclaration = NIL) THEN
  1154. AnonymousTypeDeclaration(x,"Anonymous");
  1155. END;
  1156. x.SetState(SyntaxTree.Resolved);
  1157. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1158. Warning(x.position, "Forbidden empty Body.");
  1159. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1160. Warning(x.position, "Non-empty body for an engine?");
  1161. END;
  1162. END;
  1163. resolvedType := ResolvedType(x);
  1164. END VisitCellType;
  1165. (* check if an object is an array-structured object type
  1166. - determine the array structure
  1167. - collect operators from top to bottom in the inheritance hierarchy
  1168. - check if LEN operator is declared
  1169. - determine number of possible index operators
  1170. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1171. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1172. *)
  1173. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1174. VAR
  1175. indexOperatorCount, i: LONGINT;
  1176. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1177. isTensor: BOOLEAN;
  1178. BEGIN
  1179. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1180. (* determine array structure *)
  1181. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1182. END;
  1183. IF recordType.HasArrayStructure() THEN
  1184. (* the object is an ASOT *)
  1185. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1186. (* reset array access operators *)
  1187. arrayAccessOperators.len := NIL;
  1188. arrayAccessOperators.generalRead := NIL;
  1189. arrayAccessOperators.generalWrite := NIL;
  1190. IF isTensor THEN
  1191. (* all operators of dimensionalities 1 to max *)
  1192. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1193. ELSE
  1194. (* all operators of certain dimensionality *)
  1195. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1196. END;
  1197. NEW(arrayAccessOperators.read, indexOperatorCount);
  1198. NEW(arrayAccessOperators.write, indexOperatorCount);
  1199. FOR i := 0 TO indexOperatorCount - 1 DO
  1200. arrayAccessOperators.read[i] := NIL;
  1201. arrayAccessOperators.write[i] := NIL
  1202. END;
  1203. (* collect access operators in the record scope *)
  1204. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1205. IF arrayAccessOperators.len = NIL THEN
  1206. (* TODO: think about making this operator optional for static array structures *)
  1207. Error(recordType.position, "LEN operator missing")
  1208. END;
  1209. (* show error messages *)
  1210. IF isTensor THEN
  1211. (* require ARRAY [*] OF RANGE *)
  1212. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1213. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1214. ELSE
  1215. (* forbid ARRAY [*] OF RANGE *)
  1216. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1217. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1218. (* require RANGE, RANGE, ... RANGE *)
  1219. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1220. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1221. END;
  1222. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1223. ELSE
  1224. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1225. IF recordType.recordScope.firstOperator # NIL THEN
  1226. RETURN;
  1227. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1228. END
  1229. END
  1230. END ResolveArrayStructure;
  1231. (** collect array access operators in a record scope **)
  1232. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1233. VAR
  1234. baseType: SyntaxTree.Type;
  1235. operator: SyntaxTree.Operator;
  1236. isReadOperator, isGeneralOperator: BOOLEAN;
  1237. indexListSize, indexListKind, hashValue: LONGINT;
  1238. BEGIN
  1239. (* if a parent record scope exists, collect the operators there first *)
  1240. baseType := recordScope.ownerRecord.baseType;
  1241. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1242. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1243. END;
  1244. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1245. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1246. END;
  1247. (* go through all operators in the current record scope *)
  1248. operator := recordScope.firstOperator;
  1249. WHILE operator # NIL DO
  1250. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1251. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1252. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1253. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1254. IF isGeneralOperator THEN
  1255. IF isReadOperator THEN
  1256. arrayAccessOperators.generalRead := operator
  1257. ELSE
  1258. arrayAccessOperators.generalWrite := operator
  1259. END
  1260. ELSE
  1261. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1262. IF isReadOperator THEN
  1263. arrayAccessOperators.read[hashValue] := operator
  1264. ELSE
  1265. arrayAccessOperators.write[hashValue] := operator
  1266. END
  1267. END
  1268. END
  1269. ELSE
  1270. Error(operator.position, 'invalid operator')
  1271. END;
  1272. operator := operator.nextOperator
  1273. END
  1274. END CollectArrayAccessOperators;
  1275. (** the hash value of an index operator **)
  1276. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1277. VAR result: LONGINT;
  1278. BEGIN
  1279. IF isTensor THEN
  1280. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1281. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1282. ELSE
  1283. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1284. END
  1285. ELSE
  1286. result := indexListKind
  1287. END;
  1288. RETURN result
  1289. END IndexOperatorHash;
  1290. (** 2 to the power of exponent **)
  1291. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1292. VAR result, i: LONGINT;
  1293. BEGIN
  1294. result := 1;
  1295. FOR i := 1 TO exponent DO
  1296. result := result * 2;
  1297. END;
  1298. RETURN result
  1299. END TwoToThePowerOf;
  1300. (** check if a LEN operator has a correct signature. i.e.
  1301. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1302. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1303. **)
  1304. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1305. VAR
  1306. procedureType: SyntaxTree.ProcedureType;
  1307. returnedArrayType: SyntaxTree.MathArrayType;
  1308. result: BOOLEAN;
  1309. BEGIN
  1310. result := FALSE;
  1311. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1312. IF (procedureType.numberParameters = 0) THEN
  1313. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1314. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1315. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1316. IF returnedArrayType.form = SyntaxTree.Open THEN
  1317. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1318. result := TRUE
  1319. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1320. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1321. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1322. result := TRUE
  1323. END
  1324. END
  1325. END
  1326. END
  1327. END;
  1328. IF result THEN
  1329. (* export symbol automatically *)
  1330. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1331. ELSE
  1332. Error(operator.position, "LEN operator with invalid signature");
  1333. END;
  1334. RETURN result
  1335. END CheckLenOperator;
  1336. (** check if an index operator has a correct signature. i.e.
  1337. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1338. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1339. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1340. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1341. - determine if it is a read or write operator (existance of return type)
  1342. - check index parameters
  1343. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1344. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1345. [LONGINT] -> binary 0 -> 0
  1346. [RANGE] -> binary 1 -> 1
  1347. [LONGINT, LONGINT] -> binary 00 -> 0
  1348. [LONGINT, RANGE] -> binary 01 -> 1
  1349. [RANGE, LONGINT] -> binary 10 -> 2
  1350. [RANGE, RANGE] -> binary 11 -> 3
  1351. etc.
  1352. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1353. - for read operators, check if return type matches the type of data that is read
  1354. - for write operators, check if last parameter type matches the type of data that is written
  1355. **)
  1356. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1357. VAR
  1358. elementType, otherElementType, dataType: SyntaxTree.Type;
  1359. procedureType: SyntaxTree.ProcedureType;
  1360. mathArrayType: SyntaxTree.MathArrayType;
  1361. parameter: SyntaxTree.Parameter;
  1362. parameterCount, rangeCount, i: LONGINT;
  1363. hasTypeError: BOOLEAN;
  1364. BEGIN
  1365. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1366. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1367. (* determine if it is a read or write operator *)
  1368. isReadOperator := (procedureType.returnType # NIL);
  1369. IF isReadOperator THEN
  1370. indexListSize := parameterCount;
  1371. ELSE
  1372. indexListSize := parameterCount - 1;
  1373. END;
  1374. IF indexListSize < 1 THEN
  1375. Error(operator.position, "index operator with too few parameters");
  1376. RETURN FALSE
  1377. END;
  1378. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1379. (* general operator *)
  1380. isGeneralOperator := TRUE;
  1381. IF indexListSize > 1 THEN
  1382. Error(operator.position, "index operator with too many parameters");
  1383. RETURN FALSE
  1384. END;
  1385. (* ARRAY [*] OF RANGE*)
  1386. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1387. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1388. Error(operator.position, "index parameter not dynamic math array of range");
  1389. RETURN FALSE
  1390. END;
  1391. parameter := procedureType.firstParameter.nextParameter
  1392. ELSE
  1393. (* fixed-dim. operator *)
  1394. isGeneralOperator := FALSE;
  1395. (* check number of index parameters *)
  1396. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1397. (* for tensors, limited to a certain size *)
  1398. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1399. Error(operator.position, "too many index parameters for tensor");
  1400. RETURN FALSE
  1401. END
  1402. ELSE
  1403. (* for non-tensors, depends on dimensionality *)
  1404. IF indexListSize # arrayStructure.Dimensionality() THEN
  1405. Error(operator.position, "index parameter count does not match dimensionality");
  1406. RETURN FALSE
  1407. END
  1408. END;
  1409. (* go through all index parameters
  1410. - count the number of ranges
  1411. - determine the index list kind number
  1412. *)
  1413. indexListKind := 0;
  1414. rangeCount := 0;
  1415. parameter := procedureType.firstParameter;
  1416. FOR i := 1 TO indexListSize DO
  1417. indexListKind := indexListKind * 2;
  1418. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1419. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1420. INC(indexListKind);
  1421. INC(rangeCount)
  1422. ELSE
  1423. Error(parameter.position, "integer or range expected");
  1424. RETURN FALSE
  1425. END;
  1426. parameter := parameter.nextParameter
  1427. END;
  1428. END;
  1429. (*
  1430. - for read operators: check type of last parameter
  1431. - for write operators: check return type
  1432. *)
  1433. IF isReadOperator THEN
  1434. dataType := procedureType.returnType (* the return type *)
  1435. ELSE
  1436. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1437. END;
  1438. elementType := arrayStructure.ElementType();
  1439. hasTypeError := FALSE;
  1440. IF isGeneralOperator THEN
  1441. (* ARRAY [?] OF <Element> *)
  1442. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1443. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1444. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1445. hasTypeError := TRUE
  1446. END
  1447. ELSE
  1448. hasTypeError := TRUE
  1449. END
  1450. ELSE
  1451. IF rangeCount = 0 THEN
  1452. (* <Element> *)
  1453. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1454. ELSE
  1455. (* ARRAY [*, *, ..., *] OF <Element> *)
  1456. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1457. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1458. IF mathArrayType.IsFullyDynamic() THEN
  1459. IF mathArrayType.Dimensionality() = rangeCount THEN
  1460. otherElementType := mathArrayType.ElementType();
  1461. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1462. ELSE
  1463. hasTypeError := TRUE
  1464. END
  1465. ELSE
  1466. hasTypeError := TRUE
  1467. END
  1468. ELSE
  1469. hasTypeError := TRUE
  1470. END
  1471. END
  1472. END;
  1473. IF hasTypeError THEN
  1474. IF isReadOperator THEN
  1475. Error(operator.position, "return type does not match")
  1476. ELSE
  1477. Error(parameter.position, "type of last parameter does not match")
  1478. END;
  1479. RETURN FALSE
  1480. END;
  1481. (* export symbol automatically *)
  1482. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1483. RETURN TRUE
  1484. END CheckIndexOperator;
  1485. (** resolve all pending types (late resolving).
  1486. - type fixes are resolved at the end of the declaration phase
  1487. - type fixes may imply new type fixes that are also entered at the end of the list
  1488. **)
  1489. PROCEDURE FixTypes;
  1490. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1491. BEGIN
  1492. prevScope := currentScope;
  1493. p := typeFixes.Get(currentScope);
  1494. WHILE p # NIL DO
  1495. ASSERT(currentScope # NIL);
  1496. ASSERT(p IS SyntaxTree.Type);
  1497. IF p IS SyntaxTree.PointerType THEN
  1498. FixPointerType(p(SyntaxTree.PointerType))
  1499. ELSIF p IS SyntaxTree.ProcedureType THEN
  1500. FixProcedureType(p(SyntaxTree.ProcedureType))
  1501. ELSE
  1502. HALT(100);
  1503. END;
  1504. p := typeFixes.Get(currentScope);
  1505. END;
  1506. currentScope :=prevScope;
  1507. END FixTypes;
  1508. (**
  1509. resolve type x
  1510. - if x is nil then return nil
  1511. - if x cannot be resolved then the result is invalidType else the result is x
  1512. - the resolved type is entered into x.resolved
  1513. **)
  1514. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1515. VAR prev,resolved: SyntaxTree.Type;
  1516. BEGIN
  1517. prev := resolvedType;
  1518. resolvedType := SyntaxTree.invalidType;
  1519. IF x = NIL THEN resolvedType := NIL
  1520. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1521. END;
  1522. resolved := resolvedType;
  1523. resolvedType := prev;
  1524. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1525. RETURN resolved
  1526. END ResolveType;
  1527. (*** compatibility rules ***)
  1528. (**
  1529. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1530. **)
  1531. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1532. VAR result: SyntaxTree.Type;
  1533. BEGIN
  1534. result := SyntaxTree.invalidType;
  1535. IF type = NIL THEN Error(position, "expression of type NIL");
  1536. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1538. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1539. ELSE result := type.resolved
  1540. END;
  1541. RETURN result
  1542. END RegularType;
  1543. (** returns signature compatibility of procedure types this and to
  1544. - if not compatible then error is reported
  1545. - compatibility means type equality
  1546. **)
  1547. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1548. VAR result: BOOLEAN;
  1549. BEGIN
  1550. result := SameType(to,this);
  1551. IF ~result THEN
  1552. Error(position, "signature incompatible");
  1553. IF VerboseErrorMessage THEN
  1554. Printout.Info("this",this);
  1555. Printout.Info("to",to);
  1556. END;
  1557. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1558. Error(position, "signature incompatible: realtime flag must be inherited");
  1559. END;
  1560. RETURN result
  1561. END SignatureCompatible;
  1562. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1563. - for var parameters compatibility means same type except for
  1564. - formal is of open array of system byte
  1565. - formal is of record type
  1566. - formal is of open array type
  1567. - formal is of open math array type
  1568. - for value parameters compatibllity means assignment compatibility except for
  1569. - formal is of open array type
  1570. if compatible the return true else report error and return false
  1571. **)
  1572. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1573. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1574. BEGIN
  1575. formalType := RegularType(formal.position,formal.type);
  1576. actualType := RegularType(actual.position,actual.type);
  1577. error := FALSE;
  1578. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1579. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1580. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1581. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1582. END;
  1583. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1584. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1585. ELSIF ~IsVariable(actual) THEN
  1586. result := FALSE; error := TRUE;
  1587. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1588. Error(actual.position,"not a variable: no operator for writing");
  1589. ELSE
  1590. Error(actual.position,"is not a variable");
  1591. END;
  1592. IF VerboseErrorMessage THEN
  1593. Printout.Info("actual",actual);
  1594. Printout.Info("formal",formal);
  1595. END;
  1596. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1597. result := CompatibleTo(system,actualType,formalType);
  1598. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1599. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1600. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1601. IF IsArrayStructuredObjectType(actualType) THEN
  1602. actualType := MathArrayStructureOfType(actualType)
  1603. END;
  1604. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1605. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1606. Error(actual.position,"incompatible non-static actual type");
  1607. END;
  1608. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1609. Error(actual.position,"incompatible tensor (use a range expression)");
  1610. END;
  1611. ELSE
  1612. result := SameType(actualType,formalType)
  1613. END
  1614. ELSE
  1615. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1616. actualType := system.characterType;
  1617. END;
  1618. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1619. result := TRUE; (* special rule for WINAPI parameters *)
  1620. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1621. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1622. ELSE
  1623. result := CompatibleTo(system,actualType,formalType);
  1624. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1625. Error(actual.position,"incompatible non-static actual type");
  1626. END;
  1627. END;
  1628. END;
  1629. IF ~result & ~error THEN
  1630. Error(actual.position,"incompatible parameter");
  1631. IF VerboseErrorMessage THEN
  1632. Printout.Info("actual",actual);
  1633. Printout.Info("formal",formal);
  1634. END;
  1635. END;
  1636. RETURN result
  1637. END ParameterCompatible;
  1638. (** check compatibility for expressions of the form left := right
  1639. - if compatible then return true else error report and return false
  1640. - check if left is variable
  1641. - check compatiblity
  1642. **)
  1643. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1644. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1645. BEGIN
  1646. result := FALSE;
  1647. leftType := RegularType(left.position,left.type);
  1648. rightType := RegularType(right.position,right.type);
  1649. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1650. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1651. END;
  1652. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1653. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1654. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1655. (* error already handled *)
  1656. result := TRUE;
  1657. ELSIF ~IsVariable(left) THEN
  1658. Error(left.position,"is not a variable");
  1659. IF VerboseErrorMessage THEN
  1660. Printout.Info("left",left);
  1661. Printout.Info("right",right);
  1662. END;
  1663. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1664. result := TRUE;
  1665. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1666. result := TRUE
  1667. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1668. Error(left.position,"incompatible assignment");
  1669. IF VerboseErrorMessage THEN
  1670. Printout.Info("left",left);
  1671. Printout.Info("right",right);
  1672. END;
  1673. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1674. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1675. Error(right.position,"forbidden assignment of a nested procedure");
  1676. ELSE
  1677. result := TRUE
  1678. END;
  1679. RETURN result
  1680. END AssignmentCompatible;
  1681. (*** values ***)
  1682. (** check and resolve integer value **)
  1683. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1684. VAR integer: Basic.Integer;
  1685. BEGIN
  1686. integer := value(SyntaxTree.IntegerValue).value;
  1687. value.SetType(Global.GetIntegerType(system,integer));
  1688. resolvedExpression := value
  1689. END VisitIntegerValue;
  1690. (** check and resolve real value **)
  1691. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1692. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1693. BEGIN
  1694. subtype := value(SyntaxTree.RealValue).subtype;
  1695. IF subtype = Scanner.Real THEN
  1696. type := system.realType
  1697. ELSIF subtype = Scanner.Longreal THEN
  1698. type := system.longrealType
  1699. ELSE
  1700. HALT(100)
  1701. END;
  1702. value.SetType(type);
  1703. resolvedExpression := value
  1704. END VisitRealValue;
  1705. (** check and resolve complex value **)
  1706. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1707. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1708. BEGIN
  1709. subtype := value(SyntaxTree.ComplexValue).subtype;
  1710. IF subtype = Scanner.Real THEN
  1711. type := system.complexType
  1712. ELSIF subtype = Scanner.Longreal THEN
  1713. type := system.longcomplexType
  1714. ELSE
  1715. HALT(100)
  1716. END;
  1717. value.SetType(type);
  1718. resolvedExpression := value
  1719. END VisitComplexValue;
  1720. (** check and resolve set value **)
  1721. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1722. BEGIN
  1723. value.SetType(system.setType);
  1724. resolvedExpression := value
  1725. END VisitSetValue;
  1726. (** check and resolve set value **)
  1727. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1728. BEGIN
  1729. value.SetType(SyntaxTree.invalidType);
  1730. resolvedExpression := value
  1731. END VisitMathArrayValue;
  1732. (** check and resolve boolean value **)
  1733. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1734. BEGIN
  1735. value.SetType(system.booleanType);
  1736. resolvedExpression := value
  1737. END VisitBooleanValue;
  1738. (** check and resolve string value **)
  1739. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1740. BEGIN
  1741. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1742. resolvedExpression := value
  1743. END VisitStringValue;
  1744. (** check and resolve character value **)
  1745. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1746. BEGIN
  1747. value.SetType(system.characterType);
  1748. resolvedExpression := value
  1749. END VisitCharacterValue;
  1750. (** check and resolve nil value **)
  1751. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1752. BEGIN
  1753. value.SetType(system.nilType);
  1754. resolvedExpression := value
  1755. END VisitNilValue;
  1756. (** check and resolve enumerator value **)
  1757. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1758. BEGIN
  1759. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1760. ASSERT(value.type # NIL);
  1761. resolvedExpression := value
  1762. END VisitEnumerationValue;
  1763. (*** expressions ***)
  1764. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1765. - check all elements on integer type
  1766. - if element range is constant, then check lower and upper bound
  1767. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1768. if an error occurs then report error and return invalidExpression
  1769. **)
  1770. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1771. VAR
  1772. i: LONGINT;
  1773. element: SyntaxTree.Expression;
  1774. constant: BOOLEAN;
  1775. elements: SyntaxTree.ExpressionList;
  1776. s: Basic.Set;
  1777. result: SyntaxTree.Expression;
  1778. value: SyntaxTree.Value;
  1779. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1780. VAR
  1781. left, right: SyntaxTree.Expression;
  1782. elementResult: SyntaxTree.Expression;
  1783. leftInteger, rightInteger: Basic.Integer;
  1784. BEGIN
  1785. (* set context of range *)
  1786. IF element IS SyntaxTree.RangeExpression THEN
  1787. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1788. END;
  1789. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1790. IF elementResult = SyntaxTree.invalidExpression THEN
  1791. (* error already reported *)
  1792. constant := FALSE
  1793. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1794. (* the element is a range expression *)
  1795. (* extract left and right hand side of range *)
  1796. left := elementResult(SyntaxTree.RangeExpression).first;
  1797. right := elementResult(SyntaxTree.RangeExpression).last;
  1798. (* guaranteed by VisitRangeExpression: *)
  1799. ASSERT((left # NIL) & (right # NIL));
  1800. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1801. ELSE
  1802. (* the element is not a range expression *)
  1803. (* check type and add conversion if needed *)
  1804. IF IsIntegerType(elementResult.type.resolved) THEN
  1805. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1806. ELSE
  1807. Error(elementResult.position, "non integer element in set");
  1808. elementResult := SyntaxTree.invalidExpression;
  1809. constant := FALSE
  1810. END;
  1811. left := elementResult;
  1812. right := elementResult
  1813. END;
  1814. IF elementResult # SyntaxTree.invalidExpression THEN
  1815. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1816. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1817. Error(left.position,"not allowed set integer value");
  1818. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1819. Error(right.position,"not allowed set integer value");
  1820. END
  1821. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1822. Error(right.position,"not allowed set integer value");
  1823. ELSE
  1824. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1825. s := {};
  1826. ELSE
  1827. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1828. IF leftInteger < 0 THEN leftInteger := 0 END;
  1829. (*!!!!!!!!! this is a hack !!!!!!! *)
  1830. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1831. extends the range x..31 to x..63 !!!!!! *)
  1832. s := s + {leftInteger..rightInteger};
  1833. END;
  1834. END;
  1835. ELSE
  1836. constant := FALSE;
  1837. END
  1838. END;
  1839. RETURN elementResult
  1840. END CheckElement;
  1841. BEGIN
  1842. result := set; constant := TRUE; s := {}; elements := set.elements;
  1843. IF elements # NIL THEN
  1844. FOR i := 0 TO elements.Length()-1 DO
  1845. element := elements.GetExpression(i);
  1846. element := CheckElement(element);
  1847. IF element = SyntaxTree.invalidExpression THEN
  1848. result := SyntaxTree.invalidExpression
  1849. END;
  1850. elements.SetExpression(i,element);
  1851. END;
  1852. END;
  1853. IF constant THEN
  1854. value := Global.NewSetValue(system,set.position,s);
  1855. result.SetResolved(value);
  1856. result.SetType(value.type);
  1857. ELSE
  1858. result.SetType(system.setType);
  1859. END;
  1860. (* optimization possible
  1861. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1862. left this to the programmer...
  1863. *)
  1864. resolvedExpression := result;
  1865. END VisitSet;
  1866. (*
  1867. old variant: quite generic but needs better conversion handling, do this?
  1868. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1869. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1870. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1871. BEGIN
  1872. type := NIL;
  1873. numberElements := x.elements.Length();
  1874. FOR i := 0 TO numberElements-1 DO
  1875. expression := x.elements.GetExpression(i);
  1876. position := expression.position;
  1877. expression := ResolveExpression(x.elements.GetExpression(i));
  1878. x.elements.SetExpression(i,de);
  1879. IF type = NIL THEN
  1880. type := expression.type;
  1881. ELSIF CompatibleTo(system,expression.type,type) THEN
  1882. (* ok *)
  1883. ELSIF CompatibleTo(system,type,expression.type) THEN
  1884. type := expression.type
  1885. ELSE
  1886. Error(expression.position, "incompatible element types");
  1887. type := SyntaxTree.invalidType;
  1888. END;
  1889. END;
  1890. isValue := TRUE;
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1893. x.elements.SetExpression(i,expression);
  1894. isValue := isValue & (expression.resolved # NIL);
  1895. END;
  1896. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1897. arrayType.SetArrayBase(type);
  1898. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1899. IF isValue THEN
  1900. value := SyntaxTree.NewMathArrayValue(position);
  1901. value.SetElements(x.elements);
  1902. x.SetResolved(value);
  1903. END;
  1904. x.SetType(arrayType);
  1905. resolvedExpression := x;
  1906. END VisitMathArrayExpression;
  1907. *)
  1908. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1909. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1910. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1911. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1912. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1913. BEGIN
  1914. numberElements := x.elements.Length();
  1915. FOR i := 0 TO numberElements-1 DO
  1916. expression := x.elements.GetExpression(i);
  1917. IF expression IS SyntaxTree.MathArrayExpression THEN
  1918. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1919. ELSE
  1920. position := expression.position;
  1921. expression := ResolveExpression(x.elements.GetExpression(i));
  1922. x.elements.SetExpression(i,expression);
  1923. IF type = NIL THEN
  1924. type := expression.type;
  1925. ELSIF CompatibleTo(system,expression.type,type) THEN
  1926. (* ok *)
  1927. ELSIF CompatibleTo(system,type,expression.type) THEN
  1928. type := expression.type
  1929. ELSE
  1930. Error(expression.position, "incompatible element types");
  1931. type := SyntaxTree.invalidType;
  1932. END;
  1933. END;
  1934. END;
  1935. END RecursivelyFindType;
  1936. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1937. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1938. BEGIN
  1939. numberElements := x.elements.Length();
  1940. FOR i := 0 TO numberElements-1 DO
  1941. expression := x.elements.GetExpression(i);
  1942. IF expression IS SyntaxTree.MathArrayExpression THEN
  1943. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1944. ELSE
  1945. position := expression.position;
  1946. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1947. x.elements.SetExpression(i,expression);
  1948. isValue := isValue & (expression.resolved # NIL);
  1949. END;
  1950. END;
  1951. END RecursivelySetExpression;
  1952. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1953. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1954. arrayType: SyntaxTree.MathArrayType;
  1955. BEGIN
  1956. numberElements := x.elements.Length();
  1957. baseType := NIL;
  1958. gsize := 0;
  1959. FOR i := 0 TO numberElements-1 DO
  1960. expression := x.elements.GetExpression(i);
  1961. IF expression IS SyntaxTree.MathArrayExpression THEN
  1962. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1963. IF i=0 THEN
  1964. gsize := size;
  1965. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1966. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1967. ELSE expression.SetType(baseType)
  1968. END;
  1969. ELSIF baseType = NIL THEN baseType := type;
  1970. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1971. END;
  1972. END;
  1973. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1974. arrayType.SetArrayBase(baseType);
  1975. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1976. RETURN ResolveType(arrayType);
  1977. END RecursivelySetType;
  1978. BEGIN
  1979. type := NIL;
  1980. RecursivelyFindType(x);
  1981. isValue := TRUE;
  1982. RecursivelySetExpression(x);
  1983. arrayType := RecursivelySetType(x);
  1984. x.SetType(arrayType);
  1985. IF isValue THEN
  1986. value := SyntaxTree.NewMathArrayValue(x.position);
  1987. value.SetArray(x);
  1988. x.SetResolved(value);
  1989. value.SetType(arrayType);
  1990. END;
  1991. x.SetType(arrayType);
  1992. resolvedExpression := x;
  1993. END VisitMathArrayExpression;
  1994. (** check and resolve unary expression **)
  1995. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1996. VAR
  1997. left: SyntaxTree.Expression;
  1998. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1999. bool: BOOLEAN;
  2000. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2001. value: SyntaxTree.Value;
  2002. BEGIN
  2003. type := SyntaxTree.invalidType;
  2004. left := ResolveExpression(unaryExpression.left);
  2005. unaryExpression.SetLeft(left);
  2006. operator := unaryExpression.operator;
  2007. result := unaryExpression;
  2008. IF ~system.operatorDefined[operator] THEN
  2009. Error(left.position,"Operator Not Defined");
  2010. RETURN
  2011. ELSIF left.type = NIL THEN
  2012. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2013. resolvedExpression := SyntaxTree.invalidExpression;
  2014. RETURN
  2015. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2016. RETURN
  2017. END;
  2018. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2019. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2020. END;
  2021. IF operatorCall # NIL THEN
  2022. result := operatorCall;
  2023. type := operatorCall.type;
  2024. (* admissible operators
  2025. Minus number, set
  2026. Not boolean
  2027. *)
  2028. ELSE
  2029. CASE unaryExpression.operator OF
  2030. |Scanner.Minus:
  2031. IF IsIntegerType(left.type.resolved) THEN
  2032. IF left.resolved # NIL THEN
  2033. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2034. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2035. result.SetResolved(value);
  2036. type := Global.GetIntegerType(system,int);
  2037. value.SetType(type);
  2038. ELSE
  2039. type := left.type
  2040. END
  2041. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2042. IF IsRealValue(left,real) THEN
  2043. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2044. result.SetResolved(value);
  2045. type := left.type;
  2046. value.SetType(type);
  2047. ELSE
  2048. type := left.type;
  2049. END;
  2050. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2051. IF IsSetValue(left,set) THEN
  2052. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2053. result.SetResolved(value);
  2054. type := Global.GetSetType(system,-set);
  2055. value.SetType(type);
  2056. ELSE
  2057. type := left.type;
  2058. END;
  2059. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2060. IF IsComplexValue(left, real, imaginary) THEN
  2061. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2062. result.SetResolved(value);
  2063. type := left.type;
  2064. value.SetType(type);
  2065. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2066. ELSE
  2067. type := left.type;
  2068. END
  2069. ELSE
  2070. Error(left.position,"unary operator not applicable");
  2071. END;
  2072. |Scanner.Not:
  2073. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2074. IF IsBooleanValue(left,bool) THEN
  2075. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2076. result.SetResolved(value);
  2077. type := system.booleanType;
  2078. value.SetType(type);
  2079. ELSE
  2080. type := system.booleanType;
  2081. END;
  2082. ELSE
  2083. Error(left.position,"unary operator not applicable");
  2084. END;
  2085. |Scanner.Plus:
  2086. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2087. result := left; type := left.type;
  2088. ELSE
  2089. Error(left.position,"unary operator not applicable");
  2090. END;
  2091. (* ADDRESS OF *)
  2092. |Scanner.Address:
  2093. IF HasAddress(left) THEN
  2094. type := system.addressType;
  2095. ELSE
  2096. type := SyntaxTree.invalidType;
  2097. Error(left.position,"has no address");
  2098. Printout.Info("par", left);
  2099. END;
  2100. (* SIZE OF *)
  2101. |Scanner.Size:
  2102. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2103. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2104. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2105. value := SyntaxTree.NewIntegerValue(left.position, int);
  2106. result.SetResolved(value);
  2107. type := Global.GetIntegerType(system,int);
  2108. value.SetType(type)
  2109. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2110. ELSE
  2111. (* for variables, system sizeof could represent the physically occupied size
  2112. determined via the type descriptor, implement that ? *)
  2113. Error(left.position,"is not a type symbol");
  2114. END
  2115. (* ALIAS OF *)
  2116. |Scanner.Alias:
  2117. type := left.type.resolved;
  2118. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2119. type := SyntaxTree.invalidType;
  2120. Error(left.position,"alias on non math array type");
  2121. END;
  2122. ELSE
  2123. Error(left.position,"unary operator not defined");
  2124. END;
  2125. END;
  2126. result.SetType(type);
  2127. resolvedExpression := result
  2128. END VisitUnaryExpression;
  2129. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2130. VAR
  2131. result: SyntaxTree.Expression;
  2132. array: SyntaxTree.MathArrayExpression;
  2133. value: SyntaxTree.MathArrayValue;
  2134. isValue: BOOLEAN;
  2135. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2136. BEGIN
  2137. type := type.resolved;
  2138. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2139. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2140. END;
  2141. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2142. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2143. END;
  2144. RETURN type
  2145. END BaseType;
  2146. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2147. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2148. BEGIN
  2149. numberElements := x.elements.Length();
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. array := SyntaxTree.NewMathArrayExpression(position);
  2154. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2155. expression := array;
  2156. ELSE
  2157. position := expression.position;
  2158. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2159. isValue := isValue & (expression.resolved # NIL);
  2160. END;
  2161. to.elements.AddExpression(expression);
  2162. END;
  2163. END RecursivelyConvert;
  2164. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2165. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2166. arrayType: SyntaxTree.MathArrayType;
  2167. BEGIN
  2168. numberElements := x.elements.Length();
  2169. baseType := NIL;
  2170. gsize := 0;
  2171. FOR i := 0 TO numberElements-1 DO
  2172. expression := x.elements.GetExpression(i);
  2173. IF expression IS SyntaxTree.MathArrayExpression THEN
  2174. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2175. IF i=0 THEN
  2176. gsize := size;
  2177. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2178. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2179. ELSE expression.SetType(baseType)
  2180. END;
  2181. ELSIF baseType = NIL THEN baseType := type;
  2182. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2183. END;
  2184. END;
  2185. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2186. arrayType.SetArrayBase(baseType);
  2187. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2188. RETURN ResolveType(arrayType);
  2189. END RecursivelySetType;
  2190. BEGIN
  2191. result := SyntaxTree.invalidExpression;
  2192. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2193. result := expression (* do not convert *)
  2194. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2195. isValue := TRUE;
  2196. type := BaseType(type);
  2197. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2198. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2199. value := SyntaxTree.NewMathArrayValue(array.position);
  2200. value.SetArray(array);
  2201. value.SetType(RecursivelySetType(array));
  2202. result := value;
  2203. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2204. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2205. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2206. IF result = NIL THEN
  2207. result := SyntaxTree.invalidExpression;
  2208. Error(position, "incompatible conversion");
  2209. IF VerboseErrorMessage THEN
  2210. Printout.Info("expression",expression);
  2211. Printout.Info("type",type);
  2212. END;
  2213. END;
  2214. END;
  2215. RETURN result
  2216. END MathArrayConversion;
  2217. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2218. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2219. BEGIN
  2220. result := expression; type := type.resolved;
  2221. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2222. (* skip, no conversion *)
  2223. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2224. int := expression(SyntaxTree.IntegerValue).value;
  2225. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2226. int := Global.ConvertSigned(int,system.SizeOf(type));
  2227. result := SyntaxTree.NewIntegerValue(position,int);
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2230. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2231. result := SyntaxTree.NewIntegerValue(position,int);
  2232. result.SetType(type);
  2233. ELSIF (type IS SyntaxTree.FloatType) THEN
  2234. result := SyntaxTree.NewRealValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2237. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2238. result.SetType(type);
  2239. ELSIF (type IS SyntaxTree.SetType) THEN
  2240. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2243. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2246. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.PortType) THEN
  2249. result := ConvertValue(position, expression, system.integerType);
  2250. ELSE
  2251. Error(position, "integer value cannot be converted");
  2252. result := SyntaxTree.invalidExpression;
  2253. IF VerboseErrorMessage THEN
  2254. Printout.Info("expression",expression);
  2255. Printout.Info("type",type);
  2256. END;
  2257. END;
  2258. ELSIF IsRealValue(expression,real) THEN
  2259. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2260. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2261. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2262. result.SetType(type);
  2263. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2264. int := ENTIERH(real);
  2265. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.FloatType) THEN
  2268. result := SyntaxTree.NewRealValue(position,real);
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2271. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2272. result.SetType(type);
  2273. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2274. ELSIF (type IS SyntaxTree.PortType) THEN
  2275. result := ConvertValue(position, expression, system.integerType);
  2276. ELSE
  2277. Error(position, "real value cannot be converted");
  2278. result := SyntaxTree.invalidExpression;
  2279. END
  2280. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2281. IF (type IS SyntaxTree.ComplexType) THEN
  2282. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2283. result.SetType(type);
  2284. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2285. ELSE
  2286. Error(position, "complex value cannot be converted");
  2287. result := SyntaxTree.invalidExpression;
  2288. END
  2289. ELSIF IsSetValue(expression,set) THEN
  2290. IF (type IS SyntaxTree.IntegerType) THEN
  2291. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2292. result.SetType(type);
  2293. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2294. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.PortType) THEN
  2297. result := ConvertValue(position, expression, system.integerType);
  2298. ELSIF (type IS SyntaxTree.SetType) THEN
  2299. set := Global.ConvertSet(set,system.SizeOf(type));
  2300. result := SyntaxTree.NewSetValue(expression.position,set);
  2301. result.SetType(type);
  2302. ELSE
  2303. Error(position, "set value cannot be converted");
  2304. result := SyntaxTree.invalidExpression;
  2305. END;
  2306. ELSIF IsStringValue(expression,string) THEN
  2307. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2308. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2309. result.SetType(type);
  2310. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2311. ELSE
  2312. Error(position, "string value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF IsCharacterValue(expression,char) THEN
  2316. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2317. string[0] := char; string[1] := 0X;
  2318. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2319. result := SyntaxTree.NewStringValue(expression.position,string);
  2320. result.SetType(type);
  2321. ELSIF (type IS SyntaxTree.ByteType) THEN
  2322. (* do not simply set the new type as this could invalidate types of constants *)
  2323. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2324. result.SetType(type)
  2325. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2326. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2327. result.SetType(type);
  2328. ELSIF (type IS SyntaxTree.SetType) THEN
  2329. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2330. result.SetType(type);
  2331. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2332. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2333. result.SetType(type);
  2334. ELSIF (type IS SyntaxTree.PortType) THEN
  2335. result := ConvertValue(position, expression, system.integerType);
  2336. ELSE
  2337. Error(position, "character value cannot be converted");
  2338. result := SyntaxTree.invalidExpression;
  2339. END;
  2340. ELSIF expression IS SyntaxTree.NilValue THEN
  2341. IF type IS SyntaxTree.AddressType THEN
  2342. result := SyntaxTree.NewIntegerValue(position,0);
  2343. result.SetType(type);
  2344. ELSE
  2345. result := expression;
  2346. END;
  2347. (* nothing to be done *)
  2348. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2349. result := MathArrayConversion(position, expression,type);
  2350. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2351. int := expression(SyntaxTree.EnumerationValue).value;
  2352. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2353. int := Global.ConvertSigned(int,system.SizeOf(type));
  2354. result := SyntaxTree.NewIntegerValue(position,int);
  2355. result.SetType(type);
  2356. ELSE
  2357. result := expression;
  2358. END;
  2359. (* nothing to be done *)
  2360. ELSE
  2361. Error(position, "expression cannot be converted");
  2362. IF VerboseErrorMessage THEN
  2363. Printout.Info("expression",expression);
  2364. Printout.Info("type",type);
  2365. END;
  2366. result := SyntaxTree.invalidExpression;
  2367. END;
  2368. RETURN result
  2369. END ConvertValue;
  2370. (**
  2371. return a conversion of an expression to a given type
  2372. - if expression is already of same type then return expression
  2373. - if incompatible conversion then report error and return invalidExpression
  2374. **)
  2375. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2376. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2377. BEGIN
  2378. type := type.resolved;
  2379. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2380. result := expression;
  2381. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2382. ELSIF expression = NIL THEN (* NIL expression *)
  2383. ELSIF expression.type = NIL THEN
  2384. Error(position, "expression of type NIL cannot be converted");
  2385. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2386. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2387. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2388. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2389. ELSIF expression.resolved # NIL THEN (* value *)
  2390. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2391. IF value IS SyntaxTree.Value THEN
  2392. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2393. result.SetResolved(value(SyntaxTree.Value));
  2394. result.SetType(value.type);
  2395. ELSE
  2396. result := value
  2397. END;
  2398. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2399. expressionList := SyntaxTree.NewExpressionList();
  2400. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2401. typeDeclaration.SetDeclaredType(type);
  2402. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2403. typeSymbol.SetType(typeDeclaration.type);
  2404. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2405. expressionList.AddExpression(expression);
  2406. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2407. result.SetType(type);
  2408. ELSIF IsArrayStructuredObjectType(type) THEN
  2409. (* no type can be converted to an array-structured object type *)
  2410. HALT(100)
  2411. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2412. IF inConversion>5 THEN
  2413. Error(expression.position,"recursive Conversion");
  2414. IF VerboseErrorMessage THEN
  2415. Printout.Info("expression",expression);
  2416. Printout.Info("type",type);
  2417. END;
  2418. END;
  2419. INC(inConversion);
  2420. IF inConversion < 10 THEN
  2421. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2422. result := MathArrayConversion(position, expression,type);
  2423. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2424. expression := ConvertToMathArray(expression);
  2425. type := MathArrayStructureOfType(type);
  2426. result := MathArrayConversion(position, expression, type)
  2427. ELSE
  2428. Error(expression.position,"cannot convert non array type to array type")
  2429. END;
  2430. END;
  2431. DEC(inConversion);
  2432. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2433. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2434. OR ~(type IS SyntaxTree.ArrayType) THEN
  2435. Error(expression.position,"cannot convert array type to non-array type")
  2436. END;
  2437. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2438. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2439. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2440. (*skip, no conversion*)
  2441. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2442. (* skip, no conversion *)
  2443. ELSE
  2444. ASSERT(~(type IS SyntaxTree.RangeType));
  2445. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2446. ASSERT(type # NIL);
  2447. END;
  2448. RETURN result
  2449. END NewConversion;
  2450. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2451. BEGIN
  2452. IF CompatibleTo(system,expression.type, type) THEN
  2453. RETURN NewConversion(position, expression, type, NIL);
  2454. ELSE
  2455. Error(expression.position, "incompatible expression");
  2456. RETURN SyntaxTree.invalidExpression
  2457. END;
  2458. END CompatibleConversion;
  2459. (**
  2460. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2461. **)
  2462. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2463. VAR leftType,rightType: SyntaxTree.Type;
  2464. BEGIN
  2465. IF left.type = NIL THEN Error(left.position,"no type")
  2466. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2467. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2468. (* error already handled *)
  2469. ELSE
  2470. leftType := left.type.resolved; rightType := right.type.resolved;
  2471. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2472. right := NewConversion(right.position, right, leftType, NIL);
  2473. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2474. left := NewConversion(left.position,left,rightType,NIL);
  2475. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2476. right := NewConversion(right.position, right, leftType, NIL);
  2477. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2478. left := NewConversion(left.position,left,rightType,NIL);
  2479. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2480. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2481. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2482. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2483. ELSIF
  2484. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2485. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2486. (* must be the case LONGREAL / COMPLEX ) *)
  2487. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2488. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2489. ELSE
  2490. Error(left.position,"incompatible operands");
  2491. END;
  2492. END;
  2493. END ConvertOperands;
  2494. (** find and return best operator matching to parameter list (nil, if none)
  2495. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2496. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2497. **)
  2498. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2499. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2500. identifier: SyntaxTree.Identifier;
  2501. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2502. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2503. BEGIN
  2504. operator := scope.firstOperator;
  2505. WHILE(operator # NIL) DO
  2506. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2507. procedureType := operator.type(SyntaxTree.ProcedureType);
  2508. distance := Distance(system, procedureType,actualParameters);
  2509. IF (distance < Infinity) THEN
  2510. IF returnType # NIL THEN
  2511. IF procedureType.returnType = NIL THEN
  2512. distance := Infinity
  2513. ELSE
  2514. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2515. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2516. END;
  2517. END;
  2518. END;
  2519. (*
  2520. IF distance < Infinity THEN
  2521. TRACE(distance, operator);
  2522. Printout.Info("potential operator",operator);
  2523. ELSE
  2524. Printout.Info("operator not possible",operator);
  2525. END;
  2526. *)
  2527. IF distance < bestDistance THEN
  2528. bestDistance := distance;
  2529. bestOperator := operator;
  2530. END;
  2531. END;
  2532. operator := operator.nextOperator;
  2533. END;
  2534. (*
  2535. Printout.Info("taken operator",bestOperator);
  2536. *)
  2537. END FindInScope;
  2538. BEGIN
  2539. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2540. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2541. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2542. import := currentScope.ownerModule.moduleScope.firstImport;
  2543. WHILE (bestDistance > 0) & (import # NIL) DO
  2544. IF import.module # NIL THEN
  2545. identifier := Global.GetIdentifier(operator,import.module.case);
  2546. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2547. END;
  2548. import := import.nextImport;
  2549. END;
  2550. RETURN bestOperator
  2551. END FindOperator;
  2552. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2553. BEGIN
  2554. currentScope := scope;
  2555. END SetCurrentScope;
  2556. (**
  2557. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2558. - handle LEN and DIM operator for array-structured object types
  2559. - find operator, if found then
  2560. - if in other module then add import designator
  2561. - create symbol designator for operator
  2562. - if error then return invalidExpression, if no operator then return NIL
  2563. **)
  2564. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2565. VAR
  2566. operator: SyntaxTree.Operator;
  2567. import: SyntaxTree.Import;
  2568. expression, result: SyntaxTree.Expression;
  2569. designator: SyntaxTree.Designator;
  2570. actualParameters, tempList: SyntaxTree.ExpressionList;
  2571. recordType: SyntaxTree.RecordType;
  2572. castReturnType : SyntaxTree.MathArrayType;
  2573. BEGIN
  2574. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2575. result := SyntaxTree.invalidExpression
  2576. ELSIF leftExpression = NIL THEN
  2577. result := NIL
  2578. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2579. (* LEN or DIM operator on array-structured object type *)
  2580. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2581. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2582. IF recordType.arrayAccessOperators.len = NIL THEN
  2583. Error(position, "call of undeclared LEN operator");
  2584. result := SyntaxTree.invalidExpression
  2585. ELSE
  2586. ASSERT(leftExpression IS SyntaxTree.Designator);
  2587. designator := leftExpression(SyntaxTree.Designator);
  2588. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2589. ASSERT(expression IS SyntaxTree.Designator);
  2590. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2591. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2592. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2593. result := designator
  2594. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2595. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2596. tempList := SyntaxTree.NewExpressionList();
  2597. tempList.AddExpression(rightExpression);
  2598. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2599. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2600. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2601. tempList := SyntaxTree.NewExpressionList();
  2602. tempList.AddExpression(designator);
  2603. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2604. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2605. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2606. END
  2607. END;
  2608. ELSE
  2609. IF ~complexNumbersImported THEN
  2610. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2611. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2612. THEN
  2613. (* operators on complex numbers *)
  2614. ImportModule(Global.ComplexNumbersName,position);
  2615. complexNumbersImported := TRUE;
  2616. END;
  2617. END;
  2618. (* import OCArrayBase if needed *)
  2619. IF ~arrayBaseImported THEN
  2620. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2621. IF op = Global.Dim THEN
  2622. (* not existing in OCArrayBase *)
  2623. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2624. (* not existing in OCArrayBase *)
  2625. ELSE
  2626. ImportModule(Global.ArrayBaseName,position);
  2627. arrayBaseImported := TRUE;
  2628. END
  2629. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2630. ImportModule(Global.ArrayBaseName,position);
  2631. arrayBaseImported := TRUE
  2632. END;
  2633. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2634. (* LEN(RANGE) *)
  2635. ImportModule(Global.ArrayBaseName,position);
  2636. arrayBaseImported := TRUE;
  2637. END;
  2638. END;
  2639. actualParameters := SyntaxTree.NewExpressionList();
  2640. actualParameters.AddExpression(leftExpression);
  2641. IF rightExpression # NIL THEN
  2642. actualParameters.AddExpression(rightExpression)
  2643. END;
  2644. operator := FindOperator(system,op,actualParameters,resultType);
  2645. IF operator # NIL THEN
  2646. designator := NIL;
  2647. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2648. import := currentScope.ownerModule.moduleScope.firstImport;
  2649. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2650. import := import.nextImport;
  2651. END;
  2652. expression := NewSymbolDesignator(position,NIL,import);
  2653. designator := expression(SyntaxTree.Designator);
  2654. END;
  2655. expression := NewSymbolDesignator(position,designator,operator);
  2656. designator := expression(SyntaxTree.Designator);
  2657. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2658. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2659. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2660. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2661. result.SetType(castReturnType);
  2662. END;
  2663. ELSE
  2664. result := NIL;
  2665. END;
  2666. END;
  2667. RETURN result
  2668. END NewOperatorCall;
  2669. (** check and resolve binary expression **)
  2670. (*! clean up *)
  2671. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2672. VAR left,right,result: SyntaxTree.Expression;
  2673. leftType, rightType: SyntaxTree.Type;
  2674. 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;
  2675. cl,cr: CHAR;
  2676. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2677. type: SyntaxTree.Type;
  2678. value: SyntaxTree.Value;
  2679. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2680. integerConstantFolding: BOOLEAN;
  2681. list: SyntaxTree.ExpressionList;
  2682. PROCEDURE NewBool(v: BOOLEAN);
  2683. BEGIN
  2684. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2685. value.SetType(system.booleanType);
  2686. result.SetResolved(value);
  2687. type := system.booleanType
  2688. END NewBool;
  2689. PROCEDURE NewSet(v: Basic.Set);
  2690. BEGIN
  2691. value := Global.NewSetValue(system,binaryExpression.position,v);
  2692. result.SetResolved(value);
  2693. type := value.type;
  2694. END NewSet;
  2695. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2696. BEGIN
  2697. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2698. (* type cast to "larger" type only if the value is still in the range *)
  2699. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2700. value.SetType(t);
  2701. END;
  2702. result.SetResolved(value);
  2703. type := value.type;
  2704. END NewInteger;
  2705. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2706. BEGIN
  2707. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2708. value.SetType(t);
  2709. result.SetResolved(value);
  2710. type := t;
  2711. END NewReal;
  2712. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2713. BEGIN
  2714. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2715. value.SetType(t);
  2716. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2717. result.SetResolved(value);
  2718. type := t;
  2719. END NewComplex;
  2720. BEGIN
  2721. type := SyntaxTree.invalidType;
  2722. left := ResolveExpression(binaryExpression.left);
  2723. right := ResolveExpression(binaryExpression.right);
  2724. binaryExpression.SetLeft(left);
  2725. binaryExpression.SetRight(right);
  2726. result := binaryExpression;
  2727. operator := binaryExpression.operator;
  2728. IF ~system.operatorDefined[operator] THEN
  2729. Error(left.position,"Operator Not Defined");
  2730. result := SyntaxTree.invalidExpression;
  2731. RETURN
  2732. END;
  2733. IF left.type = NIL THEN
  2734. Error(left.position,"Expression has no result type");
  2735. result := SyntaxTree.invalidExpression;
  2736. RETURN;
  2737. END;
  2738. IF right.type = NIL THEN
  2739. Error(right.position,"Expression has no result type");
  2740. result := SyntaxTree.invalidExpression;
  2741. RETURN;
  2742. END;
  2743. leftType := left.type.resolved; rightType := right.type.resolved;
  2744. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2745. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2746. END;
  2747. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2748. list := SyntaxTree.NewExpressionList();
  2749. list.AddExpression(right);
  2750. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2751. END;
  2752. IF operatorCall # NIL THEN
  2753. result := operatorCall;
  2754. type := operatorCall.type;
  2755. (* admissible operators:
  2756. Times, Plus, Minus numeric numeric numeric
  2757. set set set
  2758. Slash numeric numeric real /complex
  2759. set set set
  2760. Div , Mod integer integer integer
  2761. And, Or bool bool bool
  2762. Equal, Unequal basic basic bool
  2763. pointer pointer bool
  2764. object object bool
  2765. record record bool
  2766. string string bool
  2767. enumerator enumerator bool
  2768. Less, LessEqual,
  2769. Greater, GreaterEqual integer/real integer/real bool
  2770. enumerator enumerator bool
  2771. In integer set bool
  2772. Is pointer type bool
  2773. object type bool
  2774. record type bool
  2775. Upto: special abbreviation for a..b
  2776. *)
  2777. ELSIF (left.type = NIL) THEN
  2778. Error(left.position,"type (left operand) = NIL in binary expression");
  2779. D.Str("nil type in "); D.Type(left); D.Ln;
  2780. result := SyntaxTree.invalidExpression;
  2781. ELSIF (right.type = NIL) THEN
  2782. Error(right.position,"type (right operand) = NIL in binary expression");
  2783. result := SyntaxTree.invalidExpression;
  2784. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2785. result := SyntaxTree.invalidExpression;
  2786. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2787. HALT(100);
  2788. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2789. type := system.booleanType;
  2790. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2791. Error(right.position,"is not a type ");
  2792. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2793. Error(binaryExpression.position,"is not a type extension of ");
  2794. IF VerboseErrorMessage THEN
  2795. Printout.Info("left",left);
  2796. Printout.Info("right",right);
  2797. END;
  2798. ELSIF IsUnsafePointer(left.type) THEN
  2799. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2800. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2801. NewBool(TRUE)
  2802. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2803. NewBool(TRUE);
  2804. ELSIF IsUnextensibleRecord(left) THEN
  2805. NewBool(FALSE)
  2806. END
  2807. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2808. Error(right.position,"must not be a type");
  2809. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2810. Error(left.position,"must not be a type");
  2811. ELSIF operator = Scanner.In THEN (* left IN right *)
  2812. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2813. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2814. NewBool(il IN sr);
  2815. ELSE
  2816. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2817. left := NewConversion(left.position, left, system.longintType,NIL);
  2818. binaryExpression.SetLeft(left)
  2819. END;
  2820. type := system.booleanType;
  2821. END
  2822. ELSE
  2823. Error(binaryExpression.position, "incompatible operands");
  2824. END
  2825. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2826. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2827. Error(binaryExpression.position,"incompatible operands");
  2828. END;
  2829. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2830. ELSE Error(binaryExpression.position,"operator not defined 1")
  2831. END
  2832. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2833. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2834. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2835. Error(binaryExpression.position,"incompatible operands");
  2836. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2837. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2838. left := NewConversion(left.position, left, system.addressType, NIL);
  2839. right := NewConversion(right.position, right, system.addressType, NIL);
  2840. binaryExpression.SetLeft(left);
  2841. binaryExpression.SetRight(right);
  2842. type := system.addressType;
  2843. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2844. ConvertOperands(left, right);
  2845. binaryExpression.SetLeft(left);
  2846. binaryExpression.SetRight(right);
  2847. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2848. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2849. END;
  2850. type := system.booleanType;
  2851. ELSE
  2852. Error(binaryExpression.position,"operator not defined 3");
  2853. END
  2854. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2855. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2856. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2857. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2858. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2859. CASE operator OF
  2860. |Scanner.Equal: NewBool(strl^=strr^);
  2861. |Scanner.Unequal:NewBool(strl^#strr^);
  2862. |Scanner.Less: NewBool(strl^<strr^);
  2863. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2864. |Scanner.Greater: NewBool(strl^>strr^);
  2865. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2866. ELSE
  2867. Error(binaryExpression.position,"operator not defined 4");
  2868. END;
  2869. END;
  2870. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2871. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2872. type := system.booleanType
  2873. ELSE
  2874. Error(binaryExpression.position,"operator not defined 5");
  2875. END;
  2876. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2877. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2878. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2879. type := system.booleanType;
  2880. ELSE
  2881. Error(binaryExpression.position,"operator not defined 6");
  2882. END
  2883. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2884. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2885. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2886. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2887. type := system.booleanType
  2888. ELSE
  2889. Error(binaryExpression.position,"operator not defined for enumerators");
  2890. END;
  2891. ELSE
  2892. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2893. END;
  2894. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2895. type := system.booleanType;
  2896. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2897. type := system.booleanType;
  2898. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2899. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2900. THEN
  2901. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2902. IF (leftType # rightType) THEN
  2903. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2904. ConvertOperands(left,right); (* operands must be of the same type here *)
  2905. END;
  2906. binaryExpression.SetLeft(left);
  2907. binaryExpression.SetRight(right);
  2908. leftType := left.type.resolved;
  2909. rightType := right.type.resolved;
  2910. END;
  2911. type := leftType;
  2912. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2913. Error(binaryExpression.position,"conversion failed ?");
  2914. IF VerboseErrorMessage THEN
  2915. Printout.Info("left",left);
  2916. Printout.Info("right",right);
  2917. END;
  2918. ELSIF IsIntegerType(leftType) THEN
  2919. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2920. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2921. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2922. Error(binaryExpression.position,"division by zero");
  2923. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2924. Error(binaryExpression.position,"integer division by negative number");
  2925. END;
  2926. END;
  2927. (* constant folding *)
  2928. (* bootstrap64
  2929. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2930. CASE operator OF
  2931. |Scanner.Plus: NewInteger(il+ir,left.type);
  2932. |Scanner.Minus: NewInteger(il-ir,left.type);
  2933. |Scanner.Times: NewInteger(il*ir,left.type);
  2934. |Scanner.Slash:
  2935. IF ir # 0 THEN
  2936. NewReal(il/ir, system.realType);
  2937. END;
  2938. |Scanner.Mod:
  2939. IF ir > 0 THEN
  2940. NewInteger(il MOD ir,left.type);
  2941. END;
  2942. |Scanner.Div:
  2943. IF ir > 0 THEN
  2944. NewInteger(il DIV ir,left.type);
  2945. END;
  2946. |Scanner.Equal: NewBool(il=ir);
  2947. |Scanner.Unequal:NewBool(il#ir);
  2948. |Scanner.Less: NewBool(il<ir);
  2949. |Scanner.LessEqual: NewBool(il<=ir);
  2950. |Scanner.Greater: NewBool(il>ir);
  2951. |Scanner.GreaterEqual: NewBool(il>=ir);
  2952. ELSE Error(binaryExpression.position,"operator not defined 7");
  2953. END;
  2954. ELS*)
  2955. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2956. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2957. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2958. CASE operator OF
  2959. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2960. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2961. |Scanner.Times: NewInteger(hl*hr,left.type);
  2962. |Scanner.Slash:
  2963. IF hr = 0 THEN
  2964. Error(binaryExpression.position,"division by zero");
  2965. ELSE
  2966. IF type.sizeInBits = 64 THEN
  2967. NewReal(hl/hr,system.longrealType);
  2968. ELSE
  2969. NewReal(hl/hr,system.realType)
  2970. END
  2971. END;
  2972. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2973. |Scanner.Mod:
  2974. IF hr = 0 THEN
  2975. Error(binaryExpression.position,"division by zero");
  2976. ELSE
  2977. NewInteger(hl MOD hr, left.type);
  2978. (* bootstrap64
  2979. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2980. *)
  2981. END;
  2982. |Scanner.Div:
  2983. IF hr = 0 THEN
  2984. Error(binaryExpression.position,"division by zero");
  2985. ELSE
  2986. NewInteger(hl DIV hr, left.type);
  2987. (* bootstrap64
  2988. NewInteger(Machine.DivH(hl,hr),left.type);
  2989. *)
  2990. END;
  2991. (* *)
  2992. |Scanner.Equal: NewBool(hl=hr);
  2993. |Scanner.Unequal: NewBool(hl#hr);
  2994. |Scanner.Less: NewBool(hl<hr);
  2995. |Scanner.LessEqual: NewBool(hl<=hr);
  2996. |Scanner.Greater: NewBool(hl>hr);
  2997. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2998. ELSE Error(binaryExpression.position,"operator not defined 8");
  2999. END;
  3000. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3001. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3002. type := left.type
  3003. ELSIF (operator = Scanner.Slash) THEN
  3004. left := NewConversion(left.position,left,system.realType,NIL);
  3005. right := NewConversion(right.position,right,system.realType,NIL);
  3006. binaryExpression.SetLeft(left);
  3007. binaryExpression.SetRight(right);
  3008. type := system.realType
  3009. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3010. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3011. type := system.booleanType
  3012. ELSE
  3013. Error(binaryExpression.position,"operator not defined 9");
  3014. END;
  3015. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3016. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3017. CASE operator OF
  3018. |Scanner.Plus: NewReal(rl+rr,leftType);
  3019. |Scanner.Minus: NewReal(rl-rr,leftType);
  3020. |Scanner.Times:NewReal(rl*rr,leftType);
  3021. |Scanner.Slash:
  3022. IF rr = 0 THEN
  3023. Error(binaryExpression.position,"division by zero");
  3024. ELSE
  3025. NewReal(rl/rr,leftType);
  3026. END
  3027. |Scanner.Equal: NewBool(rl=rr);
  3028. |Scanner.Unequal: NewBool(rl#rr);
  3029. |Scanner.Less: NewBool(rl<rr);
  3030. |Scanner.LessEqual: NewBool(rl<=rr);
  3031. |Scanner.Greater: NewBool(rl>rr);
  3032. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3033. ELSE Error(binaryExpression.position,"operator not defined 10");
  3034. END;
  3035. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3036. type := left.type
  3037. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3038. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3039. type := system.booleanType
  3040. ELSE
  3041. Error(binaryExpression.position,"operator not defined 11");
  3042. IF VerboseErrorMessage THEN
  3043. Printout.Info("left",left);
  3044. Printout.Info("right",right);
  3045. END;
  3046. END;
  3047. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3048. CASE operator OF
  3049. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3050. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3051. ELSE
  3052. Error(binaryExpression.position,"operator not defined");
  3053. IF VerboseErrorMessage THEN
  3054. Printout.Info("left", left);
  3055. Printout.Info("right", right)
  3056. END;
  3057. END;
  3058. IF ~error THEN
  3059. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3060. Error(binaryExpression.position,"division by zero")
  3061. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3062. (* do constant folding *)
  3063. CASE operator OF
  3064. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3065. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3066. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3067. |Scanner.Slash:
  3068. divisor := c * c + d * d;
  3069. ASSERT(divisor # 0);
  3070. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3071. |Scanner.Equal: NewBool((a = c) & (b = d))
  3072. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3073. END
  3074. END
  3075. END
  3076. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3077. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3078. CASE operator OF
  3079. |Scanner.And: NewBool(bl & br);
  3080. |Scanner.Or: NewBool(bl OR br);
  3081. |Scanner.Equal: NewBool(bl = br);
  3082. |Scanner.Unequal: NewBool(bl # br);
  3083. ELSE Error(binaryExpression.position,"operator not defined 12");
  3084. END;
  3085. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3086. type := system.booleanType
  3087. ELSE
  3088. Error(binaryExpression.position,"operator not defined 13");
  3089. END;
  3090. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3091. (* constant folding *)
  3092. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3093. IF operator = Scanner.Equal THEN
  3094. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3095. ELSIF operator = Scanner.Unequal THEN
  3096. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3097. END;
  3098. END;
  3099. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3100. type := system.booleanType;
  3101. ELSE
  3102. Error(binaryExpression.position, "operator not defined");
  3103. END;
  3104. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3105. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3106. CASE operator OF
  3107. |Scanner.Plus: NewSet(sl + sr);
  3108. |Scanner.Minus: NewSet(sl - sr);
  3109. |Scanner.Times: NewSet(sl * sr);
  3110. |Scanner.Slash: NewSet(sl / sr);
  3111. |Scanner.Equal: NewBool(sl=sr);
  3112. |Scanner.Unequal: NewBool(sl#sr);
  3113. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3114. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3115. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3116. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3117. ELSE Error(binaryExpression.position,"operator not defined 14");
  3118. END;
  3119. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3120. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3121. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3122. THEN
  3123. type := system.booleanType
  3124. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3125. type := left.type
  3126. ELSE
  3127. Error(binaryExpression.position,"operator not defined 15");
  3128. END;
  3129. ELSIF IsCharacterType(left.type) THEN
  3130. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3131. CASE operator OF
  3132. |Scanner.Equal: NewBool(cl=cr);
  3133. |Scanner.Unequal: NewBool(cl#cr);
  3134. |Scanner.Less: NewBool(cl<cr);
  3135. |Scanner.LessEqual: NewBool(cl<=cr);
  3136. |Scanner.Greater: NewBool(cl>cr);
  3137. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3138. ELSE Error(binaryExpression.position,"operator not defined 16");
  3139. END;
  3140. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3141. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3142. type := system.booleanType
  3143. ELSE
  3144. Error(binaryExpression.position,"operator not defined 17");
  3145. END;
  3146. ELSE
  3147. Error(binaryExpression.position,"operator not defined 18");
  3148. END;
  3149. ELSE
  3150. Error(binaryExpression.position,"operator not defined 19");
  3151. END;
  3152. IF type = SyntaxTree.invalidType THEN
  3153. result := SyntaxTree.invalidExpression
  3154. ELSE
  3155. result.SetType(type)
  3156. END;
  3157. resolvedExpression := result
  3158. END VisitBinaryExpression;
  3159. (** resolve a range expression of the from <<first .. last BY step>>
  3160. - depending on the context different things are checked:
  3161. ArrayIndex:
  3162. - components must be integers
  3163. - replace missing lower bound with 0
  3164. - replace missing upper bound with MAX(LONGINT)
  3165. - replace missing step size with 1
  3166. SetElement:
  3167. - components must be integers
  3168. - replace missing lower bound with 0
  3169. - replace missing upper bound with MAX(SET)
  3170. - must not have step size
  3171. CaseGuard:
  3172. - components must be constant
  3173. - components must be integers or characters
  3174. - must have lower and upper bound present
  3175. - components are made compatible
  3176. - must not have step size
  3177. - if error: return invalidExpression
  3178. **)
  3179. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3180. VAR
  3181. hasError: BOOLEAN;
  3182. first, last, step: SyntaxTree.Expression;
  3183. BEGIN
  3184. hasError := FALSE;
  3185. first := x.first;
  3186. last := x.last;
  3187. step := x.step;
  3188. (* check lower bound *)
  3189. IF x.context = SyntaxTree.CaseGuard THEN
  3190. IF first = NIL THEN
  3191. Error(x.position, "missing lower bound");
  3192. hasError := TRUE
  3193. ELSE
  3194. first := ResolveExpression(first);
  3195. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3196. Error(first.position, "lower bound not integer or character");
  3197. hasError := TRUE
  3198. ELSE
  3199. IF first IS SyntaxTree.StringValue THEN
  3200. (* add conversion from string to character *)
  3201. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3202. END
  3203. END;
  3204. (* check if expression is constant *)
  3205. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3206. (* error already reported *)
  3207. hasError := TRUE
  3208. END
  3209. END
  3210. ELSE (* ArrayIndex, SetElement *)
  3211. IF first = NIL THEN
  3212. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3213. END;
  3214. first := ResolveExpression(first);
  3215. IF IsIntegerType(first.type.resolved) THEN
  3216. first := NewConversion(first.position, first, system.lenType, NIL)
  3217. ELSE
  3218. Error(first.position, "lower bound not integer");
  3219. hasError := TRUE
  3220. END
  3221. END;
  3222. (* check upper bound *)
  3223. IF x.context = SyntaxTree.CaseGuard THEN
  3224. IF last = NIL THEN
  3225. Error(x.position, "missing upper bound");
  3226. hasError := TRUE
  3227. ELSE
  3228. last := ResolveExpression(last);
  3229. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3230. Error(last.position, "lower bound not integer or character");
  3231. hasError := TRUE
  3232. ELSE
  3233. IF last IS SyntaxTree.StringValue THEN
  3234. (* add conversion from string to character *)
  3235. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3236. END
  3237. END;
  3238. (* check if expression is constant *)
  3239. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3240. (* error already reported *)
  3241. hasError := TRUE
  3242. ELSE
  3243. (* try to make lower and upper bound compatible *)
  3244. ConvertOperands(first, last);
  3245. IF first.type.resolved # last.type.resolved THEN
  3246. Error(x.position, "lower and upper bounds incompatible");
  3247. hasError := TRUE
  3248. END
  3249. END
  3250. END
  3251. ELSE (* ArrayIndex, SetElement *)
  3252. IF last = NIL THEN
  3253. IF x.context = SyntaxTree.ArrayIndex THEN
  3254. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3255. ELSE
  3256. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3257. END
  3258. END;
  3259. last := ResolveExpression(last);
  3260. IF IsIntegerType(last.type.resolved) THEN
  3261. last := NewConversion(last.position, last, system.lenType, NIL)
  3262. ELSE
  3263. Error(last.position, "upper bound not integer");
  3264. hasError := TRUE
  3265. END
  3266. END;
  3267. (* check step size *)
  3268. IF x.context = SyntaxTree.ArrayIndex THEN
  3269. IF step = NIL THEN
  3270. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3271. END;
  3272. step := ResolveExpression(step);
  3273. IF IsIntegerType(step.type.resolved) THEN
  3274. step := NewConversion(step.position, step, system.lenType, NIL)
  3275. ELSE
  3276. Error(step.position, "step size not integer");
  3277. hasError := TRUE
  3278. END
  3279. ELSE (* SetElement, CaseGuard *)
  3280. IF step # NIL THEN
  3281. Error(last.position, "step size not allowed in this context");
  3282. hasError := TRUE
  3283. END
  3284. END;
  3285. IF hasError THEN
  3286. resolvedExpression := SyntaxTree.invalidExpression
  3287. ELSE
  3288. x.SetFirst(first);
  3289. x.SetLast(last);
  3290. x.SetStep(step);
  3291. x.SetType(system.rangeType);
  3292. resolvedExpression := x;
  3293. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3294. END
  3295. END VisitRangeExpression;
  3296. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3297. BEGIN
  3298. x.SetType(NIL);
  3299. resolvedExpression := x;
  3300. END VisitTensorRangeExpression;
  3301. (** resolve the expression d and return result as designator
  3302. - resolve expression
  3303. - if expression is a designator then return designator else error message and return invalidDesignator
  3304. **)
  3305. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3306. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3307. BEGIN
  3308. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3309. resolved := ResolveExpression(d);
  3310. IF resolved = SyntaxTree.invalidExpression THEN
  3311. (* error should already have been reported *)
  3312. result := SyntaxTree.invalidDesignator;
  3313. ELSIF resolved IS SyntaxTree.Designator THEN
  3314. result := resolved(SyntaxTree.Designator);
  3315. ELSE
  3316. Error(d.position,"is no designator ! ");
  3317. result := SyntaxTree.invalidDesignator;
  3318. END;
  3319. (* result.type might be nil. *)
  3320. RETURN result
  3321. END ResolveDesignator;
  3322. (**
  3323. symbol designator generated in this module
  3324. nothing to be resolved
  3325. **)
  3326. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3327. BEGIN
  3328. resolvedExpression := x;
  3329. END VisitSymbolDesignator;
  3330. (**
  3331. self designator generated in this module
  3332. nothing to be resolved
  3333. **)
  3334. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3335. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3336. BEGIN
  3337. (* check if in record scope *)
  3338. scope := currentScope;
  3339. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3340. scope := scope.outerScope;
  3341. END;
  3342. IF scope = NIL THEN (* in module scope *)
  3343. x.SetType(system.anyType);
  3344. ELSIF scope IS SyntaxTree.CellScope THEN
  3345. cell := scope(SyntaxTree.CellScope).ownerCell;
  3346. x.SetType(cell);
  3347. ELSE (* in record scope *)
  3348. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3349. IF (record # NIL) & (record.pointerType # NIL) THEN
  3350. type := ResolveType(record.pointerType);
  3351. x.SetType(type);
  3352. ELSE
  3353. x.SetType(record);
  3354. END;
  3355. END;
  3356. resolvedExpression := x;
  3357. END VisitSelfDesignator;
  3358. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3359. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3360. BEGIN
  3361. scope := currentScope;
  3362. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3363. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3364. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3365. returnType := procedureType.returnType;
  3366. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3367. THEN
  3368. x.SetType(returnType);
  3369. ELSE
  3370. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3371. x.SetType(SyntaxTree.invalidType);
  3372. END;
  3373. ELSE
  3374. Error(x.position,"forbidden access to result designator");
  3375. x.SetType(SyntaxTree.invalidType);
  3376. END;
  3377. x.SetAssignable(TRUE);
  3378. resolvedExpression := x;
  3379. END VisitResultDesignator;
  3380. (**
  3381. return symbol designator as an expression
  3382. - if symbol is a constant then return the constant value expression
  3383. - else
  3384. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3385. - if symbol is a guarded variable then return a TypeGuardDesignator
  3386. - else return a symbol designator
  3387. **)
  3388. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3389. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3390. guardType: SyntaxTree.Type;
  3391. BEGIN
  3392. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3393. result := SyntaxTree.invalidExpression;
  3394. ASSERT(symbol # NIL);
  3395. (*
  3396. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3397. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3398. Error(position, "type not allowed here");
  3399. ELS *)
  3400. (* not needed any more as values are stored in the expression
  3401. IF symbol IS SyntaxTree.Constant THEN
  3402. result := symbol(SyntaxTree.Constant).value
  3403. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3404. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3405. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3406. ELSE
  3407. result := symbol(SyntaxTree.Constant).value
  3408. END;
  3409. ELSE
  3410. *)
  3411. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3412. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3413. THEN
  3414. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3415. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3416. left := NewDereferenceDesignator(position,left);
  3417. left.SetHidden(TRUE);
  3418. END;
  3419. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3420. scope := currentScope;
  3421. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3422. scope := scope.outerScope;
  3423. END;
  3424. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3425. Error(position, "forbidden access to symbol in parent procedure scope");
  3426. END;
  3427. END;
  3428. 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);
  3429. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3430. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3431. ELSE
  3432. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3433. END;
  3434. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3435. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3436. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3437. result.SetType(symbol.type);
  3438. result.SetAssignable(assignable);
  3439. symbol.MarkUsed;
  3440. IF symbol IS SyntaxTree.Constant THEN
  3441. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3442. END;
  3443. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3444. variableAccessed := TRUE
  3445. END;
  3446. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3447. IF GetGuard(symbol,guardType) THEN
  3448. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3449. END;
  3450. END;
  3451. ASSERT(result.type # NIL);
  3452. RETURN result
  3453. END NewSymbolDesignator;
  3454. (** check and resolve an identifier designator "identifier"
  3455. - if identifier = self then return SelfDesignator
  3456. - else find symbol in current scope
  3457. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3458. **)
  3459. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3460. VAR symbol: SyntaxTree.Symbol;
  3461. BEGIN
  3462. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3463. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3464. IF symbol # NIL THEN
  3465. ResolveSymbol(symbol);
  3466. ASSERT(symbol.type # NIL);
  3467. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3468. ELSE
  3469. Error(identifierDesignator.position,"Undeclared Identifier");
  3470. IF VerboseErrorMessage THEN
  3471. Printout.Info("undeclared identifier designator",identifierDesignator);
  3472. END;
  3473. resolvedExpression := SyntaxTree.invalidDesignator;
  3474. END;
  3475. END VisitIdentifierDesignator;
  3476. (** check and resolve a selector designator of the form left.designator
  3477. - if left is a pointer type then do auto dereferenciation
  3478. - left denotes a search scope:
  3479. - if left type is import type then set search scope to respective module
  3480. - if left type is enumeration type then set search scope to respective enumeration scope
  3481. - elsif left type is record type then set search scope to record scope
  3482. - search symbol in computed scope
  3483. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3484. **)
  3485. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3486. VAR
  3487. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3488. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3489. BEGIN
  3490. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3491. left := ResolveDesignator(selectorDesignator.left);
  3492. result := SyntaxTree.invalidDesignator;
  3493. IF left # NIL THEN
  3494. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3495. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3496. END;
  3497. scope := NIL;
  3498. IF left.type = NIL THEN
  3499. Error(selectorDesignator.position,"field on nil typed designator");
  3500. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3501. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3502. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3503. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3504. module := symbol(SyntaxTree.Import).module;
  3505. IF module # NIL THEN
  3506. scope := module.moduleScope
  3507. ELSE
  3508. Error(left.position,"module not loaded");
  3509. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3510. END;
  3511. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3512. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3513. ASSERT(scope # NIL)
  3514. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3515. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3516. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3517. IF type IS SyntaxTree.EnumerationType THEN
  3518. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3519. ELSE
  3520. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3521. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3522. END;
  3523. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3524. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3525. ELSE
  3526. Error(selectorDesignator.position,"field on non-record type designator");
  3527. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3528. END;
  3529. symbol := NIL;
  3530. IF scope # NIL THEN
  3531. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3532. IF symbol # NIL THEN
  3533. ResolveSymbol(symbol);
  3534. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3535. symbol.MarkUsed
  3536. ELSE
  3537. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3538. IF VerboseErrorMessage THEN
  3539. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3540. Printout.Info("scope", scope);
  3541. Printout.Info("left", left);
  3542. Printout.Info("undeclared identifier",selectorDesignator);
  3543. Printout.Info("left resolved designator",left);
  3544. END
  3545. END;
  3546. END;
  3547. END;
  3548. resolvedExpression := result;
  3549. END VisitSelectorDesignator;
  3550. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3551. VAR len,idx: Basic.Integer;
  3552. BEGIN
  3553. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3554. IF idx < 0 THEN
  3555. Error(index.position,"index out of bounds (too small)")
  3556. ELSE
  3557. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3558. Error(index.position,"index out of bounds (too large)");
  3559. END;
  3560. END;
  3561. END;
  3562. END IndexCheck;
  3563. (*
  3564. - if index designator has not type, use newBaseType as its type
  3565. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3566. - special rule: if static array of dynamic array occurs, make it all dynamic
  3567. index designator type: new base type: new index designator type:
  3568. NIL z z
  3569. ARRAY [x, y] z ARRAY [x, y] OF z
  3570. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3571. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3572. *)
  3573. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3574. VAR
  3575. mathArrayType: SyntaxTree.MathArrayType;
  3576. makeDynamic: BOOLEAN;
  3577. BEGIN
  3578. IF indexDesignator.type = NIL THEN
  3579. indexDesignator.SetType(newBaseType)
  3580. ELSE
  3581. (* index designator must be a of math array type *)
  3582. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3583. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3584. (* determine if all arrays have to be made dynamic *)
  3585. makeDynamic :=
  3586. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3587. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3588. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3589. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3590. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3591. END;
  3592. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3593. mathArrayType.SetArrayBase(newBaseType)
  3594. END
  3595. END SetIndexBaseType;
  3596. (** check and append index list element to index designator of math array
  3597. - check validity of single index or array range
  3598. - compute new type
  3599. - if range then create new array type (calculate length of resulting array)
  3600. - otherwise take sourceArray.arrayBase as new type
  3601. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3602. **)
  3603. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3604. VAR
  3605. targetArray: SyntaxTree.MathArrayType;
  3606. first, last, step: SyntaxTree.Expression;
  3607. firstValue, lastValue, stepValue, length: Basic.Integer;
  3608. rangeExpression: SyntaxTree.RangeExpression;
  3609. BEGIN
  3610. IF indexListItem.type = SyntaxTree.invalidType THEN
  3611. (* error already handled *)
  3612. indexDesignator.parameters.AddExpression(indexListItem)
  3613. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3614. indexDesignator.HasRange;
  3615. indexDesignator.HasTensorRange;
  3616. indexDesignator.parameters.AddExpression(indexListItem);
  3617. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3618. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3619. IndexCheck(indexListItem, sourceArray.length);
  3620. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3621. indexDesignator.parameters.AddExpression(indexListItem)
  3622. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3623. indexDesignator.HasRange;
  3624. (* if the range is given as an array range expression, check the validity of its components *)
  3625. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3626. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3627. first := rangeExpression.first;
  3628. last := rangeExpression.last;
  3629. step := rangeExpression.step;
  3630. (* perform static checks on range components *)
  3631. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3632. Error(indexListItem.position,"lower bound of array range too small")
  3633. END;
  3634. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3635. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3636. Error(indexListItem.position,"upper bound of array range too large")
  3637. END
  3638. END;
  3639. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3640. Error(indexListItem.position,"invalid step size")
  3641. END;
  3642. (* add conversions to size type *)
  3643. (* TODO: needed? *)
  3644. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3645. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3646. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3647. END;
  3648. IF indexDesignator.hasTensorRange THEN
  3649. (* the index designator's base type is a tensor: leave it as is *)
  3650. ELSE
  3651. (* append a new math array to the index designator's base type *)
  3652. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3653. IF ~error THEN
  3654. (*
  3655. (* optimization: calculate length of target array for static ranges *)
  3656. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3657. IF IsStaticallyOpenRange(rangeExpression) THEN
  3658. (* range is open ('*'): reuse source array length as target array length *)
  3659. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3660. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3661. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3662. IF lastValue = MAX(LONGINT) THEN
  3663. IF IsIntegerValue(sourceArray.length, length) THEN
  3664. lastValue := length - 1;
  3665. isStaticTargetArrayLength := TRUE
  3666. ELSE
  3667. isStaticTargetArrayLength := FALSE
  3668. END
  3669. ELSE
  3670. isStaticTargetArrayLength := TRUE
  3671. END;
  3672. IF isStaticTargetArrayLength THEN
  3673. (* calculate static target array length *)
  3674. IF firstValue > lastValue THEN
  3675. length := 0
  3676. ELSE
  3677. length := 1 + lastValue - firstValue;
  3678. IF length MOD stepValue = 0 THEN
  3679. length := length DIV stepValue
  3680. ELSE
  3681. length := length DIV stepValue + 1
  3682. END
  3683. END;
  3684. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3685. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3686. ASSERT(targetArray.form = SyntaxTree.Static)
  3687. END
  3688. END
  3689. END
  3690. *)
  3691. END;
  3692. SetIndexBaseType(indexDesignator, targetArray)
  3693. END;
  3694. indexDesignator.parameters.AddExpression(indexListItem)
  3695. ELSE
  3696. Error(position,"invalid index list item");
  3697. END;
  3698. END AppendMathIndex;
  3699. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3700. VAR parameters: SyntaxTree.ExpressionList;
  3701. BEGIN
  3702. parameters := index.parameters;
  3703. IF (expression.type = NIL) THEN
  3704. Error(position, "invalid index");
  3705. ELSIF IsIntegerType(expression.type.resolved) THEN
  3706. IF over IS SyntaxTree.ArrayType THEN
  3707. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3708. ELSIF over IS SyntaxTree.StringType THEN
  3709. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3710. END;
  3711. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3712. parameters.AddExpression(expression);
  3713. ELSE
  3714. Error(position, "invalid index");
  3715. END;
  3716. END AppendIndex;
  3717. (** convert an expression to math array type
  3718. if expression is of math array type: return expression itself
  3719. if expression is of array-structured object type: return an index operator call on it
  3720. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3721. otherwise: return invalid expression
  3722. **)
  3723. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3724. VAR
  3725. result: SyntaxTree.Expression;
  3726. mathArrayType: SyntaxTree.MathArrayType;
  3727. BEGIN
  3728. IF expression.type = NIL THEN
  3729. result := SyntaxTree.invalidExpression
  3730. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3731. (* expression of math array type *)
  3732. result := expression
  3733. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3734. (* expression of array-structured object type *)
  3735. mathArrayType := MathArrayStructureOfType(expression.type);
  3736. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3737. ELSE
  3738. result := SyntaxTree.invalidExpression
  3739. END;
  3740. RETURN result
  3741. END ConvertToMathArray;
  3742. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3743. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3744. VAR
  3745. result: SyntaxTree.ExpressionList;
  3746. i: LONGINT;
  3747. BEGIN
  3748. result := SyntaxTree.NewExpressionList();
  3749. FOR i := 1 TO itemCount DO
  3750. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3751. END;
  3752. RETURN result
  3753. END ListOfOpenRanges;
  3754. (** create a procedure call designator for an index operator call on an array-structured object type
  3755. - use given index list as actual parameters
  3756. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3757. **)
  3758. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3759. VAR
  3760. operator: SyntaxTree.Operator;
  3761. expression: SyntaxTree.Expression;
  3762. actualParameters, tempList: SyntaxTree.ExpressionList;
  3763. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3764. result, tempDesignator: SyntaxTree.Designator;
  3765. recordType: SyntaxTree.RecordType;
  3766. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3767. i, hashValue, indexListSize, indexListKind: LONGINT;
  3768. castReturnType: SyntaxTree.MathArrayType;
  3769. BEGIN
  3770. ASSERT(IsArrayStructuredObjectType(left.type));
  3771. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3772. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3773. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3774. indexListSize := indexList.Length();
  3775. indexListKind := 0;
  3776. containsNonRange := FALSE;
  3777. FOR i := 0 TO indexList.Length() - 1 DO
  3778. indexListKind := indexListKind * 2;
  3779. expression := indexList.GetExpression(i);
  3780. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3781. INC(indexListKind)
  3782. ELSE
  3783. containsNonRange := TRUE
  3784. END
  3785. END;
  3786. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3787. (* select applicable index operator
  3788. - try to look up optimal index operator
  3789. - if not present, use operator on ranges
  3790. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3791. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3792. *)
  3793. usesGeneralOperator := FALSE;
  3794. IF rhs # NIL THEN
  3795. (* write operator *)
  3796. IF hashValue = -1 THEN
  3797. operator := NIL
  3798. ELSE
  3799. operator := recordType.arrayAccessOperators.write[hashValue];
  3800. END;
  3801. IF operator = NIL THEN
  3802. usesPureRangeOperator := TRUE;
  3803. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3804. operator := recordType.arrayAccessOperators.generalWrite;
  3805. usesGeneralOperator := TRUE
  3806. ELSE
  3807. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3808. operator := recordType.arrayAccessOperators.write[hashValue];
  3809. END
  3810. END
  3811. ELSE
  3812. (* read operator *)
  3813. IF hashValue = -1 THEN
  3814. operator := NIL
  3815. ELSE
  3816. operator := recordType.arrayAccessOperators.read[hashValue];
  3817. END;
  3818. IF operator = NIL THEN
  3819. usesPureRangeOperator := TRUE;
  3820. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3821. operator := recordType.arrayAccessOperators.generalRead;
  3822. usesGeneralOperator := TRUE
  3823. ELSE
  3824. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3825. operator := recordType.arrayAccessOperators.read[hashValue];
  3826. END
  3827. END
  3828. END;
  3829. IF operator = NIL THEN
  3830. Error(position, "call of undeclared [] operator");
  3831. result := SyntaxTree.invalidDesignator;
  3832. ELSE
  3833. (* determine if reshaping is needed *)
  3834. needsReshaping := containsNonRange & usesPureRangeOperator;
  3835. (* import OCArrayBase if reshaping is needed *)
  3836. IF needsReshaping & ~arrayBaseImported THEN
  3837. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3838. arrayBaseImported := TRUE
  3839. END;
  3840. (* add the index list item to the list of actual parameters
  3841. - for general operators: add a single inline array containing the index list items as parameter
  3842. - otherwise: add all index list items as individual parameters
  3843. *)
  3844. actualParameters := SyntaxTree.NewExpressionList();
  3845. IF usesGeneralOperator THEN
  3846. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3847. END;
  3848. FOR i := 0 TO indexListSize - 1 DO
  3849. expression := indexList.GetExpression(i);
  3850. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3851. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3852. tempList := SyntaxTree.NewExpressionList();
  3853. tempList.AddExpression(expression);
  3854. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3855. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3856. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3857. END;
  3858. IF usesGeneralOperator THEN
  3859. tempMathArrayExpression.elements.AddExpression(expression);
  3860. ELSE
  3861. actualParameters.AddExpression(expression)
  3862. END
  3863. END;
  3864. IF usesGeneralOperator THEN
  3865. actualParameters.AddExpression(tempMathArrayExpression)
  3866. END;
  3867. IF rhs # NIL THEN
  3868. (* add actual parameter for RHS *)
  3869. IF needsReshaping THEN
  3870. (* reshape using OCArrayBase.ExpandDimensions *)
  3871. tempList := SyntaxTree.NewExpressionList();
  3872. (* source array *)
  3873. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3874. tempList.AddExpression(rhs);
  3875. ELSE
  3876. (* convert scalar to one-dimensional array *)
  3877. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3878. tempMathArrayExpression.elements.AddExpression(rhs);
  3879. tempList.AddExpression(tempMathArrayExpression)
  3880. END;
  3881. (* list of kept dimensions *)
  3882. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3883. FOR i := 0 TO indexListSize - 1 DO
  3884. expression := indexList.GetExpression(i);
  3885. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3886. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3887. ELSE
  3888. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3889. END
  3890. END;
  3891. tempList.AddExpression(tempMathArrayExpression);
  3892. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3893. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3894. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3895. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3896. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3897. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3898. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3899. expression.SetType(castReturnType);
  3900. ELSE
  3901. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3902. END;
  3903. actualParameters.AddExpression(expression)
  3904. ELSE
  3905. actualParameters.AddExpression(rhs)
  3906. END
  3907. END;
  3908. (* add dereference operator and create procedure call designator *)
  3909. ASSERT(left IS SyntaxTree.Designator);
  3910. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3911. ASSERT(expression IS SyntaxTree.Designator);
  3912. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3913. IF (rhs = NIL) & needsReshaping THEN
  3914. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3915. tempList := SyntaxTree.NewExpressionList();
  3916. FOR i := 0 TO indexList.Length() - 1 DO
  3917. expression := indexList.GetExpression(i);
  3918. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3919. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3920. ELSE
  3921. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3922. END
  3923. END;
  3924. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3925. END;
  3926. IF rhs = NIL THEN
  3927. (* special rule: index read operator calls are considered to be assignable *)
  3928. result.SetAssignable(TRUE)
  3929. END;
  3930. (* put information about this index operator call into the resulting designator *)
  3931. result.SetRelatedAsot(left);
  3932. result.SetRelatedIndexList(indexList)
  3933. END;
  3934. RETURN result
  3935. END NewIndexOperatorCall;
  3936. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3937. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3938. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3939. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3940. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3941. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3942. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3943. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3944. CONST trace = FALSE;
  3945. BEGIN
  3946. IF trace THEN
  3947. FOR i := 0 TO actualParameters.Length()-1 DO
  3948. Printout.Info("par", actualParameters.GetExpression(i));
  3949. END;
  3950. END;
  3951. operator := scope.firstOperator;
  3952. WHILE(operator # NIL) DO
  3953. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3954. procedureType := operator.type(SyntaxTree.ProcedureType);
  3955. distance := Distance(system, procedureType,actualParameters);
  3956. IF trace THEN Printout.Info("check op ",operator) END;
  3957. IF distance < bestDistance THEN
  3958. IF trace THEN Printout.Info("taken op",operator) END;
  3959. bestDistance := distance;
  3960. bestOperator := operator;
  3961. END;
  3962. END;
  3963. operator := operator.nextOperator;
  3964. END;
  3965. END FindInScope;
  3966. BEGIN
  3967. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3968. IF oper = 0 THEN (* index *)
  3969. identifier := SyntaxTree.NewIdentifier("[]");
  3970. ELSE
  3971. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3972. END;
  3973. WHILE (recordType # NIL) DO
  3974. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3975. recordType := recordType.GetBaseRecord();
  3976. END;
  3977. RETURN bestOperator
  3978. END FindOperator;
  3979. BEGIN
  3980. type := left.type.resolved;
  3981. IF type IS SyntaxTree.RecordType THEN
  3982. pointer := FALSE;
  3983. recordType := type(SyntaxTree.RecordType);
  3984. ELSE
  3985. pointer := TRUE;
  3986. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3987. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3988. END;
  3989. actualParameters := SyntaxTree.NewExpressionList();
  3990. IF parameters # NIL THEN
  3991. FOR i := 0 TO parameters.Length()-1 DO
  3992. expression := ResolveExpression(parameters.GetExpression(i));
  3993. actualParameters.AddExpression(expression);
  3994. END;
  3995. END;
  3996. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3997. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3998. IF op # NIL THEN
  3999. designator := left(SyntaxTree.Designator);
  4000. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  4001. expression := NewSymbolDesignator(position, designator , op);
  4002. ASSERT(expression IS SyntaxTree.Designator);
  4003. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4004. result.SetRelatedAsot(left);
  4005. result.SetRelatedIndexList(parameters);
  4006. (* check if write operator exists, for var parameters *)
  4007. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4008. actualParameters := SyntaxTree.NewExpressionList();
  4009. FOR i := 0 TO parameters.Length()-1 DO
  4010. expression := ResolveExpression(parameters.GetExpression(i));
  4011. actualParameters.AddExpression(expression);
  4012. END;
  4013. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4014. actualParameters.AddExpression(rhs);
  4015. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4016. IF op = NIL THEN rhs := NIL END;
  4017. END;
  4018. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4019. ELSE
  4020. result := NIL;
  4021. END;
  4022. RETURN result;
  4023. END NewObjectOperatorCall;
  4024. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4025. 1. convert bracket designator chains into a single one that contains separators
  4026. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4027. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4028. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4029. - if an array or math array is indexed over, create index designator
  4030. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4031. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4032. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4033. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4034. - if an array-structured object type is indexed over, create procedure call designator
  4035. e.g.: a[x, y] -> a^."[]"(x, y)
  4036. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4037. - a[i, *] = a[i][*]
  4038. - a[*, i] # a[*][i]
  4039. Because:
  4040. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4041. - 'i-th column' = a[*, i]
  4042. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4043. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4044. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4045. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4046. **)
  4047. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4048. VAR
  4049. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4050. indexDesignator: SyntaxTree.IndexDesignator;
  4051. designator: SyntaxTree.Designator;
  4052. type: SyntaxTree.Type;
  4053. recordType: SyntaxTree.RecordType;
  4054. expression, rhs: SyntaxTree.Expression;
  4055. indexList: SyntaxTree.ExpressionList;
  4056. i: LONGINT;
  4057. hasError, done: BOOLEAN;
  4058. PROCEDURE FinalizeIndexDesignator;
  4059. BEGIN
  4060. IF indexDesignator # NIL THEN
  4061. (* the end of a tensor has been reached: *)
  4062. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4063. SetIndexBaseType(indexDesignator, type);
  4064. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4065. designator := indexDesignator;
  4066. type := designator.type.resolved;
  4067. indexDesignator := NIL;
  4068. ASSERT(SyntaxTree.Resolved IN type.state)
  4069. END
  4070. END FinalizeIndexDesignator;
  4071. BEGIN
  4072. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4073. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4074. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4075. (* copy all index list entries including a separator to the left bracket designator *)
  4076. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4077. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4078. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4079. END;
  4080. (* propagate the related RHS *)
  4081. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4082. (* only resolve left bracket designator and use as final result *)
  4083. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4084. ELSE
  4085. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4086. designator := ResolveDesignator(bracketDesignator.left);
  4087. type := designator.type.resolved;
  4088. indexDesignator := NIL;
  4089. (*!!! clean up *)
  4090. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4091. OR (type IS SyntaxTree.RecordType)
  4092. THEN
  4093. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4094. IF resolvedExpression = NIL THEN
  4095. Error(bracketDesignator.position,"undefined operator");
  4096. resolvedExpression := SyntaxTree.invalidDesignator
  4097. END;
  4098. RETURN;
  4099. END;
  4100. i := 0;
  4101. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4102. expression := bracketDesignator.parameters.GetExpression(i);
  4103. expression := ResolveExpression(expression);
  4104. bracketDesignator.parameters.SetExpression(i, expression);
  4105. IF expression = SyntaxTree.indexListSeparator THEN
  4106. (* finalize an existing index designator if needed *)
  4107. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4108. INC(i)
  4109. ELSE
  4110. (* do auto-dereferencing if needed *)
  4111. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4112. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4113. & (i=0)*)
  4114. THEN
  4115. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4116. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4117. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4118. designator := SyntaxTree.invalidDesignator;
  4119. type := SyntaxTree.invalidType
  4120. ELSE
  4121. FinalizeIndexDesignator;
  4122. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4123. type := designator.type.resolved
  4124. END
  4125. END;
  4126. (* create a new index designator, if needed *)
  4127. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4128. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4129. indexDesignator.SetAssignable(designator.assignable);
  4130. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4131. (* designator := indexDesignator *)
  4132. END;
  4133. IF type = SyntaxTree.invalidType THEN
  4134. (* error already handled *)
  4135. INC(i)
  4136. ELSIF type IS SyntaxTree.ArrayType THEN
  4137. (* indexing over an array *)
  4138. ASSERT(indexDesignator # NIL);
  4139. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4140. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4141. INC(i)
  4142. ELSIF type IS SyntaxTree.StringType THEN
  4143. (* indexing over an array *)
  4144. ASSERT(indexDesignator # NIL);
  4145. AppendIndex(expression.position, indexDesignator, expression, type);
  4146. type := type(SyntaxTree.StringType).baseType.resolved;
  4147. INC(i)
  4148. ELSIF type IS SyntaxTree.MathArrayType THEN
  4149. (* indexing over a math array *)
  4150. ASSERT(indexDesignator # NIL);
  4151. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4152. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4153. INC(i)
  4154. ELSIF IsArrayStructuredObjectType(type) THEN
  4155. (* indexing over ASOTs *)
  4156. FinalizeIndexDesignator;
  4157. ASSERT(type IS SyntaxTree.PointerType);
  4158. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4159. (*
  4160. - collect index list items from bracket designator that belong to ASOT
  4161. - check for errors
  4162. *)
  4163. indexList := SyntaxTree.NewExpressionList();
  4164. hasError := FALSE;
  4165. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4166. (* indexing over tensor ASOT:
  4167. - stop at index list end or separator
  4168. - dimensionality is given by number of index list items
  4169. *)
  4170. done := FALSE;
  4171. WHILE ~done DO
  4172. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4173. done := TRUE;
  4174. ELSE
  4175. expression := bracketDesignator.parameters.GetExpression(i);
  4176. IF expression = SyntaxTree.indexListSeparator THEN
  4177. done := TRUE;
  4178. ELSE
  4179. expression := ResolveExpression(expression);
  4180. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4181. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4182. hasError := TRUE
  4183. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4184. Error(expression.position, "integer or range expected");
  4185. expression := SyntaxTree.invalidExpression;
  4186. hasError := TRUE
  4187. END;
  4188. indexList.AddExpression(expression)
  4189. END;
  4190. INC(i)
  4191. END
  4192. END
  4193. ELSE
  4194. (* indexing over non-tensor ASOT:
  4195. - ignore separators
  4196. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4197. *)
  4198. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4199. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4200. expression := bracketDesignator.parameters.GetExpression(i);
  4201. ELSE
  4202. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4203. END;
  4204. IF expression # SyntaxTree.indexListSeparator THEN
  4205. expression := ResolveExpression(expression);
  4206. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4207. Error(expression.position, "integer or range expected");
  4208. expression := SyntaxTree.invalidExpression;
  4209. hasError := TRUE
  4210. END;
  4211. indexList.AddExpression(expression)
  4212. END;
  4213. INC(i)
  4214. END;
  4215. END;
  4216. IF hasError THEN
  4217. designator := SyntaxTree.invalidDesignator;
  4218. type := SyntaxTree.invalidType;
  4219. ELSE
  4220. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4221. and the last entry in the index list belongs to the array-structured object type in question.
  4222. E.g.: for a 2-dimensional array-structured object type:
  4223. - 'lhs := asot[1, 2]' -> read mode
  4224. - 'asot[1, 2] := rhs' -> write mode
  4225. - 'asot[1, 2, 3] := rhs' -> read mode
  4226. *)
  4227. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4228. rhs := bracketDesignator.relatedRhs
  4229. ELSE
  4230. rhs := NIL
  4231. END;
  4232. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4233. type := designator.type
  4234. END
  4235. ELSE
  4236. Error(expression.position,"indexing over non-array type");
  4237. designator := SyntaxTree.invalidDesignator;
  4238. type := SyntaxTree.invalidType;
  4239. INC(i)
  4240. END
  4241. END
  4242. END;
  4243. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4244. resolvedExpression := designator
  4245. END
  4246. END VisitBracketDesignator;
  4247. (** check and resolve expression list
  4248. - resolve each expression in an expression list
  4249. - returns true if and only if all statements could have successfully been resolved
  4250. **)
  4251. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4252. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4253. BEGIN
  4254. result := TRUE;
  4255. FOR i := 0 TO expressionList.Length()-1 DO
  4256. expression := ResolveExpression(expressionList.GetExpression(i));
  4257. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4258. expressionList.SetExpression(i,expression);
  4259. END;
  4260. RETURN result
  4261. END ExpressionList;
  4262. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4263. BEGIN
  4264. type := type.resolved;
  4265. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4266. RETURN TRUE
  4267. ELSIF system.CanPassInRegister # NIL THEN
  4268. RETURN system.CanPassInRegister(type);
  4269. ELSE
  4270. RETURN FALSE
  4271. END;
  4272. END CanPassInRegister;
  4273. (** return procedure call designator left(actualParameters)
  4274. - check realtime procedure call in realtime procedure
  4275. - check number of parameters
  4276. - check parameter compatibility
  4277. return invalidDesignator if error
  4278. **)
  4279. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4280. VAR result: SyntaxTree.Designator;
  4281. numberFormalParameters, numberActualParameters: LONGINT;
  4282. formalType: SyntaxTree.ProcedureType;
  4283. formalParameter: SyntaxTree.Parameter;
  4284. actualParameter: SyntaxTree.Expression;
  4285. i: LONGINT;
  4286. self: SyntaxTree.Expression;
  4287. BEGIN
  4288. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4289. result := SyntaxTree.invalidDesignator;
  4290. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4291. numberFormalParameters := formalType.numberParameters;
  4292. numberActualParameters := actualParameters.Length();
  4293. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4294. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4295. END;
  4296. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4297. self := left.left;
  4298. IF (self # NIL) & ~IsVariable(self) THEN
  4299. Error(self.position, "Non-variable expression on variable receiver");
  4300. END;
  4301. END;
  4302. IF ~ExpressionList(actualParameters) THEN
  4303. result := SyntaxTree.invalidDesignator
  4304. ELSE
  4305. IF numberActualParameters <= numberFormalParameters THEN
  4306. formalParameter := formalType.firstParameter;
  4307. FOR i := 0 TO numberActualParameters-1 DO
  4308. actualParameter := actualParameters.GetExpression(i);
  4309. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4310. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4311. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4312. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4313. ELSE
  4314. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4315. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4316. END;
  4317. actualParameters.SetExpression(i,actualParameter);
  4318. END;
  4319. formalParameter := formalParameter.nextParameter;
  4320. END;
  4321. WHILE (formalParameter # NIL) DO
  4322. IF formalParameter.defaultValue # NIL THEN
  4323. actualParameters.AddExpression(formalParameter.defaultValue);
  4324. formalParameter := formalParameter.nextParameter
  4325. ELSE
  4326. Error(position, "less actual than formal parameters");
  4327. formalParameter := NIL;
  4328. END;
  4329. END;
  4330. ELSE
  4331. Error(position, "more actual than formal parameters")
  4332. END;
  4333. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4334. result.SetAssignable(FALSE);
  4335. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4336. END;
  4337. RETURN result
  4338. END NewProcedureCallDesignator;
  4339. (**
  4340. builtin call designator generated in VisitParameterDesignator
  4341. -> nothing to be resolved
  4342. **)
  4343. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4344. BEGIN
  4345. resolvedExpression := x;
  4346. END VisitTypeGuardDesignator;
  4347. (**
  4348. builtin call designator generated in VisitParameterDesignator
  4349. -> nothing to be resolved
  4350. **)
  4351. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4352. BEGIN
  4353. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4354. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4355. ASSERT(resolvedExpression.type # NIL);
  4356. ELSIF ExpressionList(x.parameters) THEN
  4357. resolvedExpression := x;
  4358. END;
  4359. END VisitBuiltinCallDesignator;
  4360. (**
  4361. procedure call designator generated in VisitParameterDesignator
  4362. -> nothing to be resolved
  4363. **)
  4364. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4365. BEGIN
  4366. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4367. resolvedExpression := x;
  4368. END VisitProcedureCallDesignator;
  4369. (** return true if x is a variable else return false and report error **)
  4370. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4371. VAR result: BOOLEAN;
  4372. BEGIN
  4373. result := TRUE;
  4374. IF x = SyntaxTree.invalidExpression THEN
  4375. result := FALSE;
  4376. ELSIF ~IsVariable(x) THEN
  4377. Error(x.position,"non variable expression");
  4378. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4379. result := FALSE;
  4380. END;
  4381. RETURN result
  4382. END CheckVariable;
  4383. (**
  4384. if expression x is of basic type then return true else report error and return false
  4385. **)
  4386. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4387. VAR result: BOOLEAN;
  4388. BEGIN
  4389. result := FALSE;
  4390. IF x = SyntaxTree.invalidExpression THEN
  4391. ELSIF ~IsBasicType(x.type) THEN
  4392. Error(x.position,"is no basic type");
  4393. result := FALSE
  4394. ELSE result := TRUE
  4395. END;
  4396. RETURN result
  4397. END CheckBasicType;
  4398. (**
  4399. if expression x is of number type then return true else report error and return false
  4400. **)
  4401. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4402. VAR result: BOOLEAN;
  4403. BEGIN
  4404. result := FALSE;
  4405. IF x = SyntaxTree.invalidExpression THEN
  4406. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4407. Error(x.position,"is non number type");
  4408. ELSE result := TRUE
  4409. END;
  4410. RETURN result
  4411. END CheckNumberType;
  4412. (**
  4413. if expression x is of number or size type but not complex then return true else report error and return false
  4414. **)
  4415. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4416. VAR result: BOOLEAN;
  4417. BEGIN
  4418. result := FALSE;
  4419. IF x = SyntaxTree.invalidExpression THEN
  4420. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4421. Error(x.position,"is complex type");
  4422. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4423. Error(x.position,"is non number type");
  4424. ELSE result := TRUE
  4425. END;
  4426. RETURN result
  4427. END CheckNonComplexNumberSizeType;
  4428. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4429. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4430. BEGIN
  4431. result := FALSE; type := x.type.resolved;
  4432. IF x = SyntaxTree.invalidExpression THEN
  4433. 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
  4434. TRACE(type.sizeInBits);
  4435. TRACE(system.addressType.sizeInBits);
  4436. Error(x.position,"is no address type");
  4437. ELSE result := TRUE
  4438. END;
  4439. RETURN result
  4440. END CheckAddressType;
  4441. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4442. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4443. BEGIN
  4444. result := FALSE; type := x.type.resolved;
  4445. IF x = SyntaxTree.invalidExpression THEN
  4446. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4447. Error(x.position,"is no size type");
  4448. ELSE result := TRUE
  4449. END;
  4450. RETURN result
  4451. END CheckSizeType;
  4452. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4453. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4454. BEGIN
  4455. result := FALSE; type := x.type.resolved;
  4456. IF x = SyntaxTree.invalidExpression THEN
  4457. 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
  4458. Error(x.position,"is no object type");
  4459. ELSE result := TRUE
  4460. END;
  4461. RETURN result
  4462. END CheckObjectType;
  4463. (**
  4464. if expression x is of integer type then return true else report error and return false
  4465. **)
  4466. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4467. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4468. BEGIN
  4469. result := FALSE; type := x.type.resolved;
  4470. IF x = SyntaxTree.invalidExpression THEN
  4471. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4472. Error(x.position,"is no integer type");
  4473. ELSE result := TRUE
  4474. END;
  4475. RETURN result
  4476. END CheckIntegerType;
  4477. (**
  4478. if expression x is of character type then return true else report error and return false
  4479. **)
  4480. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4481. VAR result: BOOLEAN;
  4482. BEGIN
  4483. result := FALSE;
  4484. IF x = SyntaxTree.invalidExpression THEN
  4485. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4486. Error(x.position,"is no character type");
  4487. ELSE result := TRUE
  4488. END;
  4489. RETURN result
  4490. END CheckCharacterType;
  4491. (**
  4492. if expression x is of real type then return true else report error and return false
  4493. **)
  4494. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4495. VAR result: BOOLEAN;
  4496. BEGIN
  4497. result := FALSE;
  4498. IF x = SyntaxTree.invalidExpression THEN
  4499. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4500. Error(x.position,"is no float type");
  4501. ELSE result := TRUE
  4502. END;
  4503. RETURN result
  4504. END CheckRealType;
  4505. (**
  4506. if expression x is of range type then return true else report error and return false
  4507. **)
  4508. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4509. VAR result: BOOLEAN;
  4510. BEGIN
  4511. result := FALSE;
  4512. IF x = SyntaxTree.invalidExpression THEN
  4513. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4514. Error(x.position,"is no range type");
  4515. ELSE result := TRUE
  4516. END;
  4517. RETURN result
  4518. END CheckRangeType;
  4519. (**
  4520. if expression x is of boolean type then return true else report error and return false
  4521. **)
  4522. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4523. VAR result: BOOLEAN;
  4524. BEGIN
  4525. result := FALSE;
  4526. IF x = SyntaxTree.invalidExpression THEN
  4527. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4528. Error(x.position,"is no boolean type");
  4529. ELSE result := TRUE
  4530. END;
  4531. RETURN result
  4532. END CheckBooleanType;
  4533. (**
  4534. if expression x is of set type then return true else report error and return false
  4535. **)
  4536. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4537. VAR result: BOOLEAN;
  4538. BEGIN
  4539. result := FALSE;
  4540. IF x = SyntaxTree.invalidExpression THEN
  4541. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4542. Error(x.position,"is no set type");
  4543. ELSE result := TRUE
  4544. END;
  4545. RETURN result
  4546. END CheckSetType;
  4547. (**
  4548. if expression x is of string or array of character type then return true else report error and return false
  4549. **)
  4550. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4551. VAR result: BOOLEAN;
  4552. BEGIN
  4553. result := FALSE;
  4554. IF x = SyntaxTree.invalidExpression THEN
  4555. ELSIF ~IsStringType(x.type.resolved) THEN
  4556. Error(x.position,"is no string type");
  4557. ELSE result := TRUE
  4558. END;
  4559. RETURN result
  4560. END CheckStringType;
  4561. (**
  4562. if expression x is a type declaration type return true else report error and return false
  4563. **)
  4564. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4565. VAR result: BOOLEAN;
  4566. BEGIN
  4567. result := FALSE;
  4568. IF x = SyntaxTree.invalidExpression THEN
  4569. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4570. Error(x.position,"is not a type declaration");
  4571. ELSE result := TRUE
  4572. END;
  4573. RETURN result
  4574. END CheckTypeDeclarationType;
  4575. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4576. VAR result: BOOLEAN;
  4577. BEGIN
  4578. result := FALSE;
  4579. IF x = SyntaxTree.invalidExpression THEN
  4580. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4581. result := TRUE;
  4582. value := x.resolved(SyntaxTree.IntegerValue).value;
  4583. ELSE
  4584. Error(x.position,"expression is not an integer constant");
  4585. END;
  4586. RETURN result;
  4587. END CheckIntegerValue;
  4588. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4589. VAR result: BOOLEAN;
  4590. BEGIN
  4591. result := FALSE;
  4592. IF x = SyntaxTree.invalidExpression THEN
  4593. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4594. result := TRUE;
  4595. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4596. ELSE
  4597. Error(x.position,"expression is not an integer constant");
  4598. END;
  4599. RETURN result;
  4600. END CheckStringValue;
  4601. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4602. BEGIN
  4603. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4604. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  4605. ELSE
  4606. RETURN FALSE
  4607. END;
  4608. END IsUnsignedValue;
  4609. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4610. BEGIN
  4611. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4612. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  4613. ELSE
  4614. RETURN FALSE
  4615. END
  4616. END IsAddressValue;
  4617. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4618. BEGIN
  4619. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4620. END IsAddressExpression;
  4621. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4622. BEGIN
  4623. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4624. END IsSizeExpression;
  4625. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4626. VAR result: BOOLEAN;
  4627. BEGIN
  4628. result := FALSE;
  4629. IF x = SyntaxTree.invalidExpression THEN
  4630. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4631. result := TRUE;
  4632. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4633. ELSE
  4634. Error(x.position,"expression is not an integer constant");
  4635. END;
  4636. RETURN result;
  4637. END CheckEnumerationValue;
  4638. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4639. VAR result: BOOLEAN;
  4640. BEGIN
  4641. result := FALSE;
  4642. IF x = SyntaxTree.invalidExpression THEN
  4643. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4644. result := TRUE;
  4645. value := x.resolved(SyntaxTree.CharacterValue).value;
  4646. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4647. result := TRUE;
  4648. value := x.resolved(SyntaxTree.StringValue).value[0];
  4649. ELSE
  4650. Error(x.position,"expression is not a character constant");
  4651. END;
  4652. RETURN result;
  4653. END CheckCharacterValue;
  4654. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4655. VAR result: BOOLEAN;
  4656. BEGIN
  4657. result := FALSE;
  4658. IF x = SyntaxTree.invalidExpression THEN
  4659. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4660. value := x.resolved(SyntaxTree.IntegerValue).value;
  4661. IF (value > 0) OR includeZero & (value = 0) THEN
  4662. result := TRUE;
  4663. ELSE
  4664. Error(x.position,"integer is not positive");
  4665. END
  4666. ELSE
  4667. Error(x.position,"expression is not an integer constant");
  4668. END;
  4669. RETURN result;
  4670. END CheckPositiveIntegerValue;
  4671. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4672. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4673. BEGIN
  4674. result := FALSE;
  4675. IF x = SyntaxTree.invalidExpression THEN
  4676. ELSE
  4677. type := x.type.resolved;
  4678. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4679. portType := type(SyntaxTree.PortType);
  4680. result := TRUE
  4681. ELSE
  4682. Error(x.position,"no port type");
  4683. END;
  4684. END;
  4685. RETURN result
  4686. END CheckPortType;
  4687. (* move to builtin procedure call statement ?
  4688. remove builtin procedure call designator ?
  4689. *)
  4690. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4691. VAR
  4692. numberActualParameters,numberFormalParameters: LONGINT;
  4693. formalParameter: SyntaxTree.Parameter;
  4694. actualParameter: SyntaxTree.Expression;
  4695. procedureType: SyntaxTree.ProcedureType;
  4696. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4697. inPort, outPort: SyntaxTree.PortType;
  4698. constructor: SyntaxTree.Procedure;
  4699. type0,type1,type2: SyntaxTree.Type;
  4700. type,base,parameterType: SyntaxTree.Type;
  4701. arrayType: SyntaxTree.ArrayType;
  4702. i: LONGINT; i0, i1, value: Basic.Integer;
  4703. r,r0,r1,im: LONGREAL;
  4704. c: CHAR;
  4705. id: LONGINT;
  4706. b: BOOLEAN;
  4707. first: LONGINT;
  4708. mathArrayType: SyntaxTree.MathArrayType;
  4709. customBuiltin: SyntaxTree.CustomBuiltin;
  4710. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4711. VAR resultB: BOOLEAN;
  4712. BEGIN
  4713. IF numberActualParameters < from THEN
  4714. Error(position, "less actual than formal parameters");
  4715. result := SyntaxTree.invalidExpression;
  4716. resultB := FALSE;
  4717. ELSIF numberActualParameters > to THEN
  4718. Error(position, "more actual than formal parameters");
  4719. result := SyntaxTree.invalidExpression;
  4720. resultB := FALSE;
  4721. ELSE
  4722. resultB := TRUE;
  4723. END;
  4724. RETURN resultB
  4725. END CheckArity;
  4726. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4727. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4728. BEGIN
  4729. WHILE modifier # NIL DO
  4730. symbol := cellType.FindProperty(modifier.identifier);
  4731. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4732. propertyType := symbol.type.resolved;
  4733. modifierType := modifier.expression.type.resolved;
  4734. IF ~CompatibleTo(system, modifierType, propertyType) &
  4735. ~(
  4736. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4737. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4738. Error(modifier.position,"incompatible to cell property");
  4739. END;
  4740. ELSE
  4741. Error(modifier.position, "undefined property");
  4742. END;
  4743. modifier := modifier.nextModifier;
  4744. END;
  4745. END CheckModifiers;
  4746. BEGIN
  4747. type := NIL; result := NIL;
  4748. type0 := NIL; type1 := NIL; type2 := NIL;
  4749. numberActualParameters := actualParameters.Length();
  4750. IF numberActualParameters>0 THEN
  4751. parameter0 := actualParameters.GetExpression(0);
  4752. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4753. Error(parameter0.position,"forbidden type-less argument");
  4754. result := SyntaxTree.invalidExpression
  4755. END
  4756. END;
  4757. IF numberActualParameters >1 THEN
  4758. parameter1 := actualParameters.GetExpression(1);
  4759. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4760. ELSE
  4761. Error(parameter1.position,"forbidden type-less argument");
  4762. result := SyntaxTree.invalidExpression
  4763. END
  4764. END;
  4765. IF numberActualParameters >2 THEN
  4766. parameter2 := actualParameters.GetExpression(2);
  4767. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4768. ELSE
  4769. Error(parameter2.position,"forbidden type-less argument");
  4770. result := SyntaxTree.invalidExpression
  4771. END
  4772. END;
  4773. IF returnType # NIL THEN
  4774. id := Global.New;
  4775. result := NIL;
  4776. ELSE
  4777. id := builtin.id;
  4778. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4779. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4780. END;
  4781. END;
  4782. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4783. ELSIF result # NIL THEN type := result.type (* operator *)
  4784. ELSE
  4785. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4786. result(SyntaxTree.Designator).SetLeft(left);
  4787. IF returnType # NIL THEN
  4788. type := returnType;
  4789. END;
  4790. (* ---- ASSERT ----- *)
  4791. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4792. IF CheckBooleanType(parameter0) THEN
  4793. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4794. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4795. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4796. *)
  4797. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4798. Error(position, "assert failed");
  4799. END;
  4800. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4801. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4802. rules imposed by the architecture / current runtime
  4803. *)
  4804. END;
  4805. END;
  4806. (* ---- COPY ----- *)
  4807. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4808. IF~IsStringType(type0) THEN
  4809. Error(parameter0.position,"no string type");
  4810. END;
  4811. IF ~IsStringType(type1) THEN
  4812. Error(parameter1.position,"no string type");
  4813. ELSIF CheckVariable(parameter1) THEN
  4814. IF (type0 IS SyntaxTree.StringType) THEN
  4815. arrayType := type1(SyntaxTree.ArrayType);
  4816. IF arrayType.form = SyntaxTree.Static THEN
  4817. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4818. Error(position, "destination length smaller than source length")
  4819. END;
  4820. END;
  4821. END;
  4822. END;
  4823. (* ---- INC, DEC----- *)
  4824. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4825. IF numberActualParameters = 1 THEN
  4826. parameter1 :=Global.NewIntegerValue(system,position,1);
  4827. actualParameters.AddExpression(parameter1);
  4828. END;
  4829. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4830. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4831. Error(position, "incompatible increment");
  4832. ELSE
  4833. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4834. actualParameters.SetExpression(1,parameter1);
  4835. END;
  4836. END;
  4837. (* ---- EXCL, INCL----- *)
  4838. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4839. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4840. IF IsIntegerValue(parameter1,i0) THEN
  4841. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4842. Error(position, "parameter out of SET range")
  4843. END;
  4844. END;
  4845. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4846. actualParameters.SetExpression(1,parameter1);
  4847. END;
  4848. (* ---- HALT, SYSTEM.HALT ----- *)
  4849. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4850. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4851. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4852. rules imposed by the architecture / current runtime
  4853. *)
  4854. END;
  4855. (* ---- WAIT ----- *)
  4856. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4857. IF CheckObjectType(parameter0) THEN
  4858. END;
  4859. (* ---- NEW ----- *)
  4860. ELSIF (id = Global.New) THEN
  4861. IF returnType # NIL THEN
  4862. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4863. ELSE
  4864. first := 1;
  4865. END;
  4866. IF CheckArity(first,Infinity) THEN
  4867. IF currentIsRealtime THEN
  4868. Error(position, "forbidden new in realtime block");
  4869. END;
  4870. IF IsUnsafePointer(type0) THEN
  4871. Error(position, "forbidden new on unsafe pointer");
  4872. END;
  4873. (* check constructor *)
  4874. IF (first =0) OR CheckVariable(parameter0) THEN
  4875. IF type0 IS SyntaxTree.PointerType THEN
  4876. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4877. ELSIF type0 IS SyntaxTree.CellType THEN
  4878. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4879. ELSE
  4880. Error(position, "forbidden new on value type");
  4881. END;
  4882. IF type0 IS SyntaxTree.ArrayType THEN
  4883. arrayType := type0(SyntaxTree.ArrayType);
  4884. IF arrayType.form = SyntaxTree.Static THEN
  4885. i := first
  4886. ELSIF arrayType.form = SyntaxTree.Open THEN
  4887. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4888. ELSE HALT(100)
  4889. END;
  4890. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4891. i := first;
  4892. REPEAT
  4893. actualParameter := actualParameters.GetExpression(i);
  4894. IF CheckSizeType(actualParameter) THEN
  4895. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4896. actualParameters.SetExpression(i,actualParameter);
  4897. END;
  4898. INC(i);
  4899. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4900. END;
  4901. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4902. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4903. Error(position, "forbidden new on abstract object");
  4904. END;
  4905. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4906. IF constructor = NIL THEN
  4907. IF CheckArity(first,first) THEN END;
  4908. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4909. Error(position, "new on object with hidden constructor");
  4910. ELSE
  4911. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4912. numberFormalParameters := procedureType.numberParameters;
  4913. IF numberActualParameters-first <= numberFormalParameters THEN
  4914. formalParameter := procedureType.firstParameter;
  4915. FOR i := first TO numberActualParameters-1 DO
  4916. actualParameter := actualParameters.GetExpression(i);
  4917. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4918. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4919. ELSE
  4920. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4921. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4922. END;
  4923. actualParameters.SetExpression(i,actualParameter);
  4924. END;
  4925. formalParameter := formalParameter.nextParameter;
  4926. END;
  4927. WHILE (formalParameter # NIL) DO
  4928. IF formalParameter.defaultValue # NIL THEN
  4929. actualParameters.AddExpression(formalParameter.defaultValue);
  4930. formalParameter := formalParameter.nextParameter
  4931. ELSE
  4932. Error(position, "less actual than formal parameters");
  4933. formalParameter := NIL;
  4934. END;
  4935. END;
  4936. ELSE
  4937. Error(position, "more actual than formal parameters")
  4938. END;
  4939. END;
  4940. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4941. mathArrayType := type0(SyntaxTree.MathArrayType);
  4942. IF mathArrayType.form = SyntaxTree.Static THEN
  4943. Error(position, "new on static array");
  4944. ELSE
  4945. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4946. i0 := first+1; i1 := Infinity;
  4947. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4948. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4949. i1 := i0;
  4950. ELSE HALT(100);
  4951. END;
  4952. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4953. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4954. base := ArrayBase(type0,MAX(LONGINT));
  4955. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4956. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4957. IF ~CompatibleTo(system,type0,parameterType) THEN
  4958. Error(parameter0.position,"incompatible parameter in new");
  4959. result := SyntaxTree.invalidExpression;
  4960. ELSE
  4961. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4962. END;
  4963. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4964. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4965. IF ~CompatibleTo(system,type1,parameterType) THEN
  4966. Error(parameter1.position,"parameter incompatible to math array len type");
  4967. result := SyntaxTree.invalidExpression;
  4968. ELSE
  4969. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4970. END;
  4971. ELSE
  4972. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4973. i := first;
  4974. REPEAT
  4975. actualParameter := actualParameters.GetExpression(i);
  4976. IF CheckSizeType(actualParameter) THEN
  4977. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4978. actualParameters.SetExpression(i,actualParameter);
  4979. END;
  4980. INC(i);
  4981. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4982. END;
  4983. END;
  4984. END;
  4985. ELSIF type0 IS SyntaxTree.CellType THEN
  4986. IF ~(currentIsCellNet) THEN
  4987. Error(position, "cell allocation outside activeCells ");
  4988. ELSE
  4989. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4990. IF (constructor = NIL) & CheckArity(1,1) THEN
  4991. (* ok *)
  4992. ELSE
  4993. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4994. numberFormalParameters := procedureType.numberParameters;
  4995. DEC(numberActualParameters);
  4996. IF numberActualParameters <= numberFormalParameters THEN
  4997. formalParameter := procedureType.firstParameter;
  4998. FOR i := first TO numberActualParameters DO
  4999. actualParameter := actualParameters.GetExpression(i);
  5000. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5001. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5002. ELSE
  5003. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5004. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5005. END;
  5006. actualParameters.SetExpression(i,actualParameter);
  5007. END;
  5008. formalParameter := formalParameter.nextParameter;
  5009. END;
  5010. WHILE (formalParameter # NIL) DO
  5011. IF formalParameter.defaultValue # NIL THEN
  5012. actualParameters.AddExpression(formalParameter.defaultValue);
  5013. formalParameter := formalParameter.nextParameter
  5014. ELSE
  5015. Error(position, "less actual than formal parameters");
  5016. formalParameter := NIL;
  5017. END;
  5018. END;
  5019. ELSE
  5020. Error(position, "more actual than formal parameters")
  5021. END;
  5022. END;
  5023. END;
  5024. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5025. activeCellsStatement := TRUE;
  5026. ELSE
  5027. Error(position, "cannot be allocated");
  5028. END;
  5029. END;
  5030. END;
  5031. (* ---- DISPOSE ----- *)
  5032. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5033. IF ~IsPointerType(parameter0.type) THEN
  5034. Error(parameter0.position,"is not a pointer")
  5035. ELSIF ~IsDisposable(parameter0.type) THEN
  5036. Error(parameter0.position,"is not disposable")
  5037. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5038. END
  5039. (* ---- GETPROCEDURE ----- *)
  5040. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5041. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5042. IF CheckVariable(parameter2) THEN
  5043. IF ~GetProcedureAllowed(parameter2.type) THEN
  5044. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5045. END;
  5046. END;
  5047. END;
  5048. (* ---- ABS ----- *)
  5049. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5050. (* note: ABS on complex numbers is done using overloading *)
  5051. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5052. type := type0;
  5053. IF IsIntegerValue(parameter0,i0) THEN
  5054. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5055. type := Global.GetIntegerType(system,ABS(i0));
  5056. ELSIF IsRealValue(parameter0,r) THEN
  5057. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5058. END;
  5059. ELSE
  5060. type := SyntaxTree.invalidType;
  5061. END;
  5062. (* ---- ASH, ASR ----- *)
  5063. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5064. type := type0;
  5065. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5066. (*
  5067. ConvertOperands(parameter0,parameter1); (* same type *)
  5068. *)
  5069. type := parameter0.type;
  5070. IF IsIntegerValue(parameter0,i0) THEN
  5071. IF IsIntegerValue(parameter1,i1) THEN
  5072. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5073. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5074. result := ResolveExpression(result);
  5075. type := Global.GetIntegerType(system,i0);
  5076. END;
  5077. END;
  5078. IF type.resolved.sizeInBits < 32 THEN
  5079. type := system.longintType;
  5080. END;
  5081. (*!compatibility with release, remove when resolved
  5082. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5083. *)
  5084. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5085. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5086. actualParameters.SetExpression(0,parameter0);
  5087. actualParameters.SetExpression(1,parameter1);
  5088. END;
  5089. (* ---- CAP ----- *)
  5090. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5091. type := system.characterType;
  5092. IF CheckCharacterType (parameter0) THEN
  5093. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5094. actualParameters.SetExpression(0,parameter0);
  5095. IF IsCharacterValue(parameter0,c) THEN
  5096. IF (c <= "z") & (c >= "a") THEN
  5097. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5098. ELSE
  5099. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5100. END;
  5101. END;
  5102. END;
  5103. (* ---- CHR ----- *)
  5104. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5105. IF id = Global.Chr THEN
  5106. type := system.characterType
  5107. ELSE
  5108. type := system.characterType32
  5109. END;
  5110. IF CheckIntegerType(parameter0) THEN
  5111. IF IsIntegerValue(parameter0,i0) THEN
  5112. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5113. result := ResolveExpression(result);
  5114. ELSE
  5115. (*
  5116. result := NewConversion(parameter0.position,parameter0,type);
  5117. *)
  5118. END;
  5119. END
  5120. (* ---- ENTIER ----- *)
  5121. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5122. type := system.longintType;
  5123. IF CheckRealType(parameter0) THEN
  5124. IF IsRealValue(parameter0,r) THEN
  5125. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5126. type := Global.GetIntegerType(system,ENTIER(r));
  5127. END
  5128. END;
  5129. (* ---- ENTIERH ----- *)
  5130. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5131. type := system.hugeintType;
  5132. IF CheckRealType(parameter0) THEN
  5133. IF IsRealValue(parameter0,r) THEN
  5134. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5135. END
  5136. END;
  5137. (* ---- LEN ----- *)
  5138. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5139. type := system.lenType;
  5140. base := type0;
  5141. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5142. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5143. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5144. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5145. Error(position, "forbidden len on unsafe pointer");
  5146. END;
  5147. type0 := base;
  5148. ELSE
  5149. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5150. type0 := parameter0.type.resolved;
  5151. actualParameters.SetExpression(0,parameter0);
  5152. base := type0;
  5153. END;
  5154. END;
  5155. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5156. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5157. IF i1 < 0 THEN
  5158. Error(position, "invalid dimension");
  5159. base := SyntaxTree.invalidType;
  5160. ELSE
  5161. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5162. IF (base # NIL) & Indexable(base) THEN
  5163. ELSE
  5164. Error(position, "len on no array");
  5165. IF VerboseErrorMessage THEN
  5166. Printout.Info("base",base);
  5167. END;
  5168. base := SyntaxTree.invalidType;
  5169. END;
  5170. END;
  5171. IF numberActualParameters=2 THEN
  5172. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5173. actualParameters.SetExpression(1,parameter1);
  5174. ELSIF base IS SyntaxTree.MathArrayType THEN
  5175. Error(position, "missing dimension specification");
  5176. END;
  5177. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5178. IF base IS SyntaxTree.ArrayType THEN
  5179. arrayType := base(SyntaxTree.ArrayType);
  5180. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5181. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5182. result := Global.NewIntegerValue(system,position,value);
  5183. type := result.type;(* arrayType.length.type;*)
  5184. ASSERT(type # NIL);
  5185. END;
  5186. ELSIF base IS SyntaxTree.MathArrayType THEN
  5187. mathArrayType := base(SyntaxTree.MathArrayType);
  5188. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5189. result := Global.NewIntegerValue(system,position,value);
  5190. type := result.type;
  5191. (*
  5192. type := mathArrayType.length.type;
  5193. *)
  5194. ASSERT(type # NIL);
  5195. END;
  5196. END;
  5197. END;
  5198. ELSE
  5199. type := system.lenType;
  5200. END;
  5201. (* ---- FIRST ---- *)
  5202. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5203. type := system.lenType;
  5204. IF CheckRangeType(parameter0) THEN END;
  5205. result.SetAssignable(parameter0.assignable)
  5206. (* ---- LAST ---- *)
  5207. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5208. type := system.lenType;
  5209. IF CheckRangeType(parameter0) THEN END;
  5210. result.SetAssignable(parameter0.assignable)
  5211. (* ---- STEP ---- *)
  5212. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5213. type := system.lenType;
  5214. IF CheckRangeType(parameter0) THEN END;
  5215. result.SetAssignable(parameter0.assignable)
  5216. (* ---- RE ---- *)
  5217. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5218. IF CheckNumberType(parameter0) THEN
  5219. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5220. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5221. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5222. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5223. type := parameter0.type
  5224. ELSE
  5225. type := system.realType
  5226. END
  5227. END;
  5228. result.SetAssignable(parameter0.assignable)
  5229. (* ---- IM ---- *)
  5230. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5231. IF CheckNumberType(parameter0) THEN
  5232. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5233. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5234. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5235. ELSE
  5236. type := system.realType;
  5237. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5238. END
  5239. END;
  5240. result.SetAssignable(parameter0.assignable)
  5241. (* ---- MAX ----- *)
  5242. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5243. IF numberActualParameters = 1 THEN
  5244. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5245. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5246. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5247. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5248. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5249. *)
  5250. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5251. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5252. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5253. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5254. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5255. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5256. END;
  5257. ELSE
  5258. Error(parameter0.position,"is not a type symbol");
  5259. END
  5260. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5261. ConvertOperands(parameter0,parameter1);
  5262. actualParameters.SetExpression(0,parameter0);
  5263. actualParameters.SetExpression(1,parameter1);
  5264. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5265. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5266. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5267. END;
  5268. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5269. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5270. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5271. END;
  5272. END;
  5273. type := parameter0.type;
  5274. ELSE type := SyntaxTree.invalidType;
  5275. END;
  5276. (* ---- MIN ----- *)
  5277. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5278. IF numberActualParameters = 1 THEN
  5279. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5280. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5281. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5282. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5283. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5284. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5285. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5286. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5287. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5288. END;
  5289. ELSE
  5290. Error(parameter0.position,"is not a type symbol");
  5291. END
  5292. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5293. ConvertOperands(parameter0,parameter1);
  5294. actualParameters.SetExpression(0,parameter0);
  5295. actualParameters.SetExpression(1,parameter1);
  5296. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5297. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5298. ELSE result.SetResolved(parameter1.resolved)
  5299. END;
  5300. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5301. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5302. ELSE result.SetResolved(parameter1.resolved)
  5303. END;
  5304. END;
  5305. type := parameter0.type;
  5306. ELSE type := SyntaxTree.invalidType;
  5307. END;
  5308. (* ---- ODD ----- *)
  5309. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5310. type := system.booleanType;
  5311. IF CheckIntegerType(parameter0) THEN
  5312. IF IsIntegerValue(parameter0,i0) THEN
  5313. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5314. type := system.booleanType;
  5315. END;
  5316. END;
  5317. (* ---- ORD ----- *)
  5318. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5319. IF id = Global.Ord THEN
  5320. type := system.integerType;
  5321. ELSE
  5322. type := system.longintType;
  5323. END;
  5324. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5325. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5326. actualParameters.SetExpression(0,parameter0);
  5327. (* IF CheckCharacterType(parameter0) THEN*)
  5328. IF IsCharacterValue(parameter0,c)THEN
  5329. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5330. type := Global.GetSignedIntegerType(system,ORD(c));
  5331. END;
  5332. ELSE Error(parameter0.position, "incompatible parameter");
  5333. END;
  5334. (* ---- SHORT ----- *)
  5335. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5336. type := type0;
  5337. IF IsSignedIntegerType(type) THEN
  5338. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5339. ELSIF type = system.integerType THEN type := system.shortintType
  5340. ELSIF type = system.longintType THEN type := system.integerType
  5341. ELSIF type = system.hugeintType THEN type:= system.longintType
  5342. ELSE
  5343. CASE type.sizeInBits OF
  5344. 16: type := Global.Integer8
  5345. |32: type := Global.Integer16
  5346. |64: type := Global.Integer32
  5347. END;
  5348. END;
  5349. ELSIF IsUnsignedIntegerType(type) THEN
  5350. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  5351. ELSE
  5352. CASE type.sizeInBits OF
  5353. 16: type := Global.Unsigned8
  5354. |32: type := Global.Unsigned16
  5355. |64: type := Global.Unsigned32
  5356. END;
  5357. END;
  5358. ELSIF type IS SyntaxTree.FloatType THEN
  5359. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5360. ELSIF type = system.longrealType THEN type := system.realType
  5361. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5362. END;
  5363. ELSIF type IS SyntaxTree.ComplexType THEN
  5364. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5365. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5366. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5367. END;
  5368. ELSE
  5369. Error(parameter0.position,"short not applicable")
  5370. END;
  5371. IF (parameter0.resolved # NIL) THEN
  5372. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5373. IF parameter0 IS SyntaxTree.Value THEN
  5374. result.SetResolved(parameter0(SyntaxTree.Value));
  5375. END;
  5376. END;
  5377. (* ---- LONG ----- *)
  5378. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5379. type := type0;
  5380. IF IsSignedIntegerType(type) THEN
  5381. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5382. ELSIF type = system.longintType THEN type := system.hugeintType
  5383. ELSIF type = system.integerType THEN type := system.longintType
  5384. ELSIF type = system.shortintType THEN type := system.integerType
  5385. ELSE
  5386. CASE type.sizeInBits OF
  5387. 8: type := Global.Integer16
  5388. |16: type := Global.Integer32
  5389. |32: type := Global.Integer64
  5390. END;
  5391. END;
  5392. ELSIF IsUnsignedIntegerType(type) THEN
  5393. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  5394. ELSE
  5395. CASE type.sizeInBits OF
  5396. 8: type := Global.Unsigned16
  5397. |16: type := Global.Unsigned32
  5398. |32: type := Global.Unsigned64
  5399. END;
  5400. END;
  5401. ELSIF type IS SyntaxTree.FloatType THEN
  5402. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5403. ELSIF type= system.realType THEN type := system.longrealType
  5404. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5405. END;
  5406. ELSIF type IS SyntaxTree.ComplexType THEN
  5407. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5408. ELSIF type = system.complexType THEN type := system.longcomplexType
  5409. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5410. END;
  5411. ELSE
  5412. Error(parameter0.position,"long not applicable")
  5413. END;
  5414. IF (parameter0.resolved # NIL) THEN
  5415. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5416. IF parameter0 IS SyntaxTree.Value THEN
  5417. result.SetResolved(parameter0(SyntaxTree.Value));
  5418. END;
  5419. END;
  5420. (* ---- SIZE OF ----- *)
  5421. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5422. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5423. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5424. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5425. type := system.integerType;
  5426. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5427. ELSE
  5428. (* for variables, system sizeof could represent the physically occupied size
  5429. determined via the type descriptor, implement that ? *)
  5430. Error(parameter0.position,"is not a type symbol");
  5431. END
  5432. (* ---- SYSTEM.TRACE -----*)
  5433. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5434. FOR i := 0 TO numberActualParameters-1 DO
  5435. parameter0 := actualParameters.GetExpression(i);
  5436. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5437. Error(parameter0.position,"incompatible parameter");
  5438. END;
  5439. END;
  5440. (* remaining issues can only be tested in backend *)
  5441. (* ---- ADDRESSOF----- *)
  5442. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5443. IF HasAddress(parameter0) THEN
  5444. type := system.addressType;
  5445. ELSE
  5446. type := SyntaxTree.invalidType;
  5447. Error(parameter0.position,"has no address");
  5448. END;
  5449. (* ---- BIT ----- *)
  5450. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5451. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5452. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5453. actualParameters.SetExpression(0,parameter0);
  5454. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5455. actualParameters.SetExpression(1,parameter1);
  5456. END;
  5457. type := system.booleanType;
  5458. (* ----- MSK ---- *)
  5459. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5460. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5461. ConvertOperands(parameter0,parameter1);
  5462. actualParameters.SetExpression(0,parameter0);
  5463. actualParameters.SetExpression(1,parameter1);
  5464. END;
  5465. type := parameter0.type;
  5466. (* ---- SYSTEM.GET64 ----- *)
  5467. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5468. IF CheckAddressType(parameter0) THEN
  5469. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5470. actualParameters.SetExpression(0,parameter0);
  5471. END;
  5472. type := system.hugeintType;
  5473. (* ---- SYSTEM.GET32 ----- *)
  5474. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5475. IF CheckAddressType(parameter0) THEN
  5476. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. END;
  5479. type := system.longintType;
  5480. (* ---- SYSTEM.GET16 ----- *)
  5481. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5482. IF CheckAddressType(parameter0) THEN
  5483. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5484. actualParameters.SetExpression(0,parameter0);
  5485. END;
  5486. type := system.integerType;
  5487. (* ---- SYSTEM.GET8 ----- *)
  5488. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5489. IF CheckAddressType(parameter0) THEN
  5490. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5491. actualParameters.SetExpression(0,parameter0);
  5492. END;
  5493. type := system.shortintType;
  5494. (* ---- SYSTEM.GetStackPointer ----- *)
  5495. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5496. type := system.addressType;
  5497. (* ---- SYSTEM.GetFramePointer ----- *)
  5498. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5499. type := system.addressType;
  5500. (* ---- SYSTEM.GetActivity ----- *)
  5501. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5502. type := system.objectType;
  5503. (* ---- SYSTEM.SetStackPointer ----- *)
  5504. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5505. IF CheckAddressType(parameter0) THEN
  5506. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5507. actualParameters.SetExpression(0,parameter0);
  5508. END;
  5509. (* ---- SYSTEM.SetFramePointer ----- *)
  5510. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5511. IF CheckAddressType(parameter0) THEN
  5512. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5513. actualParameters.SetExpression(0,parameter0);
  5514. END;
  5515. (* ---- SYSTEM.SetActivity ----- *)
  5516. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5517. IF CheckObjectType(parameter0) THEN
  5518. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5519. actualParameters.SetExpression(0,parameter0);
  5520. END;
  5521. (* ---- LSH, LSL, ROT, ROR ----- *)
  5522. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5523. type := type0;
  5524. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5525. actualParameters.SetExpression(1, parameter1);
  5526. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5527. IF id = Global.Lsh THEN
  5528. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5529. ELSIF id = Global.Rot THEN
  5530. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5531. ELSIF id = Global.Ror THEN
  5532. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5533. END;
  5534. END;
  5535. (* ---- SYSTEM.VAL ----- *)
  5536. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5537. IF CheckTypeDeclarationType(parameter0) THEN
  5538. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5539. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5540. result := SyntaxTree.invalidExpression;
  5541. Error(parameter0.position,"is no basic type");
  5542. ELSE
  5543. IF (parameter1.resolved # NIL) THEN
  5544. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5545. IF parameter0 IS SyntaxTree.Value THEN
  5546. result.SetResolved(parameter0(SyntaxTree.Value));
  5547. END;
  5548. END;
  5549. result.SetAssignable(parameter1.assignable);
  5550. END;
  5551. END;
  5552. (* ---- SYSTEM.GET ----- *)
  5553. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5554. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5555. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5556. actualParameters.SetExpression(0,parameter0);
  5557. END;
  5558. (* ---- SYSTEM.PUT ----- *)
  5559. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5560. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5561. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5562. actualParameters.SetExpression(0,parameter0);
  5563. END;
  5564. (* ---- SYSTEM.PUT64 ----- *)
  5565. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5566. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5567. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5568. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5569. actualParameters.SetExpression(0,parameter0);
  5570. actualParameters.SetExpression(1,parameter1);
  5571. END;
  5572. (* ---- SYSTEM.PUT32 ----- *)
  5573. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5574. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5575. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5576. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5577. actualParameters.SetExpression(0,parameter0);
  5578. actualParameters.SetExpression(1,parameter1);
  5579. END;
  5580. (* ---- SYSTEM.PUT16 ----- *)
  5581. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5582. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5583. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5584. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5585. actualParameters.SetExpression(0,parameter0);
  5586. actualParameters.SetExpression(1,parameter1);
  5587. END;
  5588. (* ---- SYSTEM.PUT8 ----- *)
  5589. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5590. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5591. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5592. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5593. actualParameters.SetExpression(0,parameter0);
  5594. actualParameters.SetExpression(1,parameter1);
  5595. END;
  5596. (* ---- SYSTEM.MOVE ----- *)
  5597. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5598. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5599. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5600. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5601. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5602. actualParameters.SetExpression(0,parameter0);
  5603. actualParameters.SetExpression(1,parameter1);
  5604. actualParameters.SetExpression(2,parameter2);
  5605. END;
  5606. (* ---- SYSTEM.NEW ----- *)
  5607. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5608. IF ~IsPointerType(parameter0.type) THEN
  5609. Error(parameter0.position,"is not a pointer")
  5610. ELSIF CheckSizeType(parameter1) THEN
  5611. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5612. actualParameters.SetExpression(1,parameter1);
  5613. END;
  5614. (* ----SYSTEM.REF ---- *)
  5615. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5616. type := system.addressType
  5617. (* ---- INCR ----- *)
  5618. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5619. type := system.lenType;
  5620. base := type0;
  5621. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5622. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5623. IF i1 < 0 THEN
  5624. Error(position, "invalid dimension");
  5625. base := SyntaxTree.invalidType;
  5626. ELSE
  5627. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5628. IF (base # NIL) & Indexable(base) THEN
  5629. ELSE
  5630. Error(position, "len on no array");
  5631. IF VerboseErrorMessage THEN
  5632. Printout.Info("base",base);
  5633. END;
  5634. base := SyntaxTree.invalidType;
  5635. END;
  5636. END;
  5637. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5638. actualParameters.SetExpression(1,parameter1);
  5639. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5640. mathArrayType := base(SyntaxTree.MathArrayType);
  5641. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5642. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5643. type := system.lenType;
  5644. END;
  5645. END;
  5646. ELSE
  5647. type := system.lenType;
  5648. END;
  5649. (* ---- SUM ----- *)
  5650. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5651. Error(position, "sum operator not applicable");
  5652. (* ---- ALL ----- *)
  5653. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5654. Error(position, "all operator not applicable");
  5655. (* ---- DIM ----- *)
  5656. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5657. type := system.lenType;
  5658. IF type0 IS SyntaxTree.MathArrayType THEN
  5659. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5660. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5661. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5662. END;
  5663. ELSE
  5664. Error(position, "dimension on non math array type");
  5665. END;
  5666. (* ---- CAS ----- *)
  5667. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5668. IF type0.IsComposite () THEN
  5669. Error(position, "first parameter of composite type");
  5670. result := SyntaxTree.invalidExpression;
  5671. ELSIF ~IsVariable (parameter0) THEN
  5672. Error(position, "first parameter not assignable");
  5673. result := SyntaxTree.invalidExpression;
  5674. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5675. Error(position, "second parameter incompatible");
  5676. result := SyntaxTree.invalidExpression;
  5677. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5678. Error(position, "third parameter incompatible");
  5679. result := SyntaxTree.invalidExpression;
  5680. ELSE
  5681. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5682. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5683. type := type0;
  5684. END;
  5685. (* ---- RESHAPE ----- *)
  5686. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5687. IF type0 IS SyntaxTree.MathArrayType THEN
  5688. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5689. base := ArrayBase(type0,MAX(LONGINT));
  5690. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5691. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5692. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5693. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5694. IF ~CompatibleTo(system,type0,parameterType) THEN
  5695. Error(parameter0.position,"incompatible parameter in reshape");
  5696. result := SyntaxTree.invalidExpression;
  5697. ELSE
  5698. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5699. END;
  5700. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5701. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5702. IF ~CompatibleTo(system,type1,parameterType) THEN
  5703. Error(parameter1.position,"parameter incompatible to math array of longint");
  5704. result := SyntaxTree.invalidExpression;
  5705. ELSE
  5706. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5707. END;
  5708. ELSE
  5709. Error(position,"reshape on non math array type");
  5710. result := SyntaxTree.invalidExpression;
  5711. END;
  5712. (* ---- SYSTEM.TYPECODE ----- *)
  5713. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5714. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5715. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5716. type := type.resolved;
  5717. IF type IS SyntaxTree.PointerType THEN
  5718. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5719. END;
  5720. IF ~(type IS SyntaxTree.RecordType) THEN
  5721. Error(parameter0.position,"must be type with type descriptor");
  5722. END;
  5723. ELSE
  5724. Error(parameter0.position,"is not a type symbol");
  5725. END;
  5726. type := system.addressType;
  5727. (* -------- FLT --------- *)
  5728. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5729. type := system.realType;
  5730. IF IsRealValue(parameter0, r) THEN
  5731. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5732. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5733. value := ABS(i0);
  5734. IF value # 0 THEN
  5735. i1 := 23;
  5736. IF value >= 2*800000H THEN
  5737. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5738. ELSIF value < 800000H THEN
  5739. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5740. END;
  5741. value := (i1 + 127)*800000H - 800000H + value;
  5742. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5743. END;
  5744. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5745. END;
  5746. (* ------- CONNECT -------*)
  5747. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5748. (*IF ~(currentIsCellNet) THEN
  5749. Error(position, "connection outside activeCells body block");
  5750. END;*)
  5751. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5752. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5753. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5754. END;
  5755. IF numberActualParameters = 3 THEN
  5756. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5757. Error(position, "incompatible channel size parameter");
  5758. END;
  5759. *)
  5760. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5761. actualParameters.SetExpression(2,parameter2);
  5762. END;
  5763. activeCellsStatement := TRUE;
  5764. (* ---------- DELEGATE --------*)
  5765. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5766. (*
  5767. IF ~(currentIsCellNet) THEN
  5768. Error(position, "connection delegation outside activeCells body block");
  5769. END;
  5770. *)
  5771. IF ~CheckPortType(parameter1, inPort) THEN
  5772. Error(parameter0.position,"not a port")
  5773. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5774. Error(parameter1.position,"not a port")
  5775. ELSIF (outPort.direction # inPort.direction) THEN
  5776. Error(parameter0.position,"invalid port direction");
  5777. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5778. Error(position, "incompatible port sizes");
  5779. END;
  5780. activeCellsStatement := TRUE;
  5781. (* --------- RECEIVE ---------*)
  5782. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5783. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5784. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5785. IF inPort.direction # SyntaxTree.InPort THEN
  5786. Error(parameter0.position,"not an in-port")
  5787. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5788. Error(parameter1.position,"incompatible to port type");
  5789. END;
  5790. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5791. IF ~SameType(parameter2.type, system.integerType) THEN
  5792. Error(parameter2.position,"incompatible to integer type");
  5793. END;
  5794. END;
  5795. END;
  5796. (* --------- SEND ---------*)
  5797. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5798. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5799. IF CheckPortType(parameter0,outPort) THEN
  5800. IF outPort.direction # SyntaxTree.OutPort THEN
  5801. Error(parameter1.position,"not an out-port")
  5802. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5803. Error(parameter1.position,"incompatible to port type");
  5804. ELSE
  5805. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5806. actualParameters.SetExpression(1,parameter1);
  5807. END;
  5808. END;
  5809. (* ------- custom builtins ----- *)
  5810. ELSIF id = Global.systemSpecial THEN
  5811. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5812. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5813. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5814. type := procedureType.returnType;
  5815. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5816. (* go through all formal parameters *)
  5817. formalParameter := procedureType.firstParameter;
  5818. FOR i := 0 TO actualParameters.Length() - 1 DO
  5819. actualParameter := actualParameters.GetExpression(i);
  5820. IF actualParameter = SyntaxTree.invalidExpression THEN
  5821. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5822. Error(position, "incompatible parameter")
  5823. ELSE
  5824. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5825. END;
  5826. actualParameters.SetExpression(i, actualParameter);
  5827. formalParameter := formalParameter.nextParameter
  5828. END
  5829. END
  5830. ELSE
  5831. Error(position, "builtin not implemented");
  5832. result := SyntaxTree.invalidExpression;
  5833. END;
  5834. END;
  5835. IF result # SyntaxTree.invalidExpression THEN
  5836. type := ResolveType(type);
  5837. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5838. result.SetType(type);
  5839. END;
  5840. RETURN result
  5841. END NewBuiltinCallDesignator;
  5842. (** return type guard designator left(type)
  5843. - check if type can be extended (i.e. is no static record)
  5844. - check if type is a type extension of left.type
  5845. - returns new type guard designator
  5846. returns invalidDesignator = invalidExpression if error
  5847. **)
  5848. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5849. VAR result: SyntaxTree.Designator;
  5850. BEGIN
  5851. result := SyntaxTree.invalidDesignator;
  5852. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5853. Error(position, "no type extension of type");
  5854. IF VerboseErrorMessage THEN
  5855. Printout.Info("left",left);
  5856. Printout.Info("type",type);
  5857. END;
  5858. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5859. Error(position, "variable cannot be extended");
  5860. ELSIF IsUnsafePointer(left.type) THEN
  5861. Error(position, "forbidden type guard on unsafe pointer");
  5862. ELSE
  5863. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5864. result.SetType(type);
  5865. result.SetAssignable(left.assignable);
  5866. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5867. END;
  5868. RETURN result
  5869. END NewTypeGuardDesignator;
  5870. (** check and resolve parameter designator left(expression list)
  5871. - check expression list
  5872. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5873. - elsif left is a procedure type then
  5874. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5875. - else return is a procedure call then return ProcedureCallDesignator
  5876. returns invalidDesignator = invalidExpression if error
  5877. **)
  5878. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5879. VAR
  5880. parameters: SyntaxTree.ExpressionList;
  5881. left: SyntaxTree.Designator;
  5882. result,expression: SyntaxTree.Expression;
  5883. typeDeclaration: SyntaxTree.TypeDeclaration;
  5884. type, expressionType: SyntaxTree.Type;
  5885. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5886. BEGIN
  5887. type := type.resolved;
  5888. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5889. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5890. END;
  5891. RETURN type
  5892. END BaseType;
  5893. BEGIN
  5894. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5895. result := SyntaxTree.invalidDesignator;
  5896. left := ResolveDesignator(designator.left);
  5897. IF left # SyntaxTree.invalidDesignator THEN
  5898. parameters := designator.parameters;
  5899. IF ExpressionList(parameters) THEN
  5900. IF (left.type = NIL) THEN
  5901. Error(left.position,"object is not a procedure or cannot be extended");
  5902. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5903. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5904. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5905. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5906. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5907. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5908. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5909. ELSE
  5910. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5911. END
  5912. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5913. expression := parameters.GetExpression(0);
  5914. type := typeDeclaration.declaredType.resolved;
  5915. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5916. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5917. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5918. OR (expressionType IS SyntaxTree.EnumerationType)
  5919. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5920. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5921. Error(left.position,"invalid unsigned type in explicit conversion");
  5922. ELSE
  5923. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5924. END;
  5925. ELSE
  5926. Error(left.position,"invalid type in explicit conversion");
  5927. END;
  5928. ELSE
  5929. Error(left.position,"called object is not a procedure or cannot be extended");
  5930. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5931. result := SyntaxTree.invalidDesignator;
  5932. END;
  5933. ELSE
  5934. result := SyntaxTree.invalidDesignator
  5935. END;
  5936. END;
  5937. resolvedExpression := result;
  5938. END VisitParameterDesignator;
  5939. (** check dereference designator left^
  5940. - check if left is pointer type or left is object type
  5941. - return new dereference designator with type = left.baseType.type (if appropriate)
  5942. with error handling
  5943. returns invalidDesignator = invalidExpression if error
  5944. **)
  5945. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5946. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5947. BEGIN
  5948. result := SyntaxTree.invalidDesignator;
  5949. type := left.type;
  5950. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5951. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5952. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5953. result.SetAssignable(TRUE);
  5954. result.SetType(type);
  5955. result.SetHidden(left.isHidden);
  5956. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5957. type := type.resolved;
  5958. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5959. result.SetAssignable(TRUE);
  5960. result.SetType(type);
  5961. result.SetHidden(left.isHidden);
  5962. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5963. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5964. result.SetAssignable(TRUE);
  5965. result.SetType(type);
  5966. result.SetHidden(left.isHidden);
  5967. ELSE
  5968. Error(position, "dereference on no pointer");
  5969. IF VerboseErrorMessage THEN
  5970. Printout.Info("pointer", type);
  5971. Printout.Info("scope", currentScope);
  5972. END;
  5973. END;
  5974. RETURN result
  5975. END NewDereferenceDesignator;
  5976. (** check supercall designator left^
  5977. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5978. - return new supercall designator with type = left.type
  5979. with error handling
  5980. **)
  5981. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5982. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5983. objectScope: SyntaxTree.Scope;
  5984. BEGIN
  5985. result := SyntaxTree.invalidDesignator;
  5986. IF left = SyntaxTree.invalidDesignator THEN
  5987. (* error already handled *)
  5988. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5989. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5990. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5991. IF symbol IS SyntaxTree.Procedure THEN
  5992. procedure := symbol(SyntaxTree.Procedure);
  5993. objectScope := currentScope;
  5994. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5995. objectScope := objectScope.outerScope;
  5996. END;
  5997. IF (left.left = NIL) OR ~
  5998. (
  5999. (left.left IS SyntaxTree.SelfDesignator) OR
  6000. (left.left IS SyntaxTree.DereferenceDesignator)
  6001. & (left.left(SyntaxTree.Designator).left # NIL)
  6002. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  6003. Error(position, "procedure not in immediate object scope");
  6004. IF VerboseErrorMessage THEN
  6005. Printout.Info("left.left",left.left);
  6006. END;
  6007. ELSIF procedure.super # NIL THEN
  6008. result := SyntaxTree.NewSupercallDesignator(position,left);
  6009. result.SetType(left.type.resolved)
  6010. ELSE
  6011. Error(position, "no supermethod for this procedure");
  6012. END;
  6013. ELSE
  6014. Error(position, "symbol is not a procedure");
  6015. END;
  6016. ELSE
  6017. Error(position, "is no symbol designator");
  6018. END;
  6019. RETURN result
  6020. END NewSupercallDesignator;
  6021. (** check and semantically resolve arrow designator left^
  6022. - if left is procedure type -> result := SupercallDesignator
  6023. - else result := DereferenceDesignator
  6024. returns result via global variable resolvedExpression
  6025. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6026. **)
  6027. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6028. VAR left: SyntaxTree.Designator;
  6029. BEGIN
  6030. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6031. left := ResolveDesignator(arrowDesignator.left);
  6032. IF left # NIL THEN
  6033. IF (left.type = NIL) THEN
  6034. Error(arrowDesignator.position,"Invalid arrow designator");
  6035. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6036. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6037. ELSE
  6038. IF IsPointerToObject(left.type) THEN
  6039. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6040. END;
  6041. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6042. END
  6043. END
  6044. END VisitArrowDesignator;
  6045. (** check and return expression
  6046. - if expression has no type then resolve expression
  6047. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6048. - return result
  6049. **)
  6050. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6051. VAR result,prev: SyntaxTree.Expression;
  6052. BEGIN
  6053. IF expression = NIL THEN result := NIL
  6054. ELSIF (expression.type = NIL) THEN
  6055. prev := resolvedExpression;
  6056. resolvedExpression := SyntaxTree.invalidExpression;
  6057. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6058. expression.SetType(SyntaxTree.invalidType);
  6059. END;
  6060. expression.Accept(SELF);
  6061. result := resolvedExpression;
  6062. IF currentIsRealtime THEN
  6063. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6064. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6065. END;
  6066. END;
  6067. (* designator modifiers for backends if they support it ...*)
  6068. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6069. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6070. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6071. END;
  6072. resolvedExpression := prev
  6073. ELSE
  6074. result := expression
  6075. END;
  6076. RETURN result
  6077. END ResolveExpression;
  6078. (**
  6079. check expression to be constant expression
  6080. - resolve expression
  6081. - if valid then check that of value type
  6082. report error and return invalidExpression if anything fails
  6083. **)
  6084. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6085. VAR position: Position;
  6086. BEGIN
  6087. position := expression.position;
  6088. expression := ResolveExpression(expression);
  6089. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6090. ELSIF (expression.resolved = NIL) THEN
  6091. Error(position, "expression is not constant");
  6092. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6093. expression := SyntaxTree.invalidExpression;
  6094. END;
  6095. RETURN expression
  6096. END ConstantExpression;
  6097. (** check expression to be constant integer
  6098. - resolve expresssion
  6099. - if valid then check that of integer value type
  6100. report error and return invalidExpression if anything fails
  6101. **)
  6102. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6103. VAR position: Position;
  6104. BEGIN
  6105. position := expression.position;
  6106. expression := ResolveExpression(expression);
  6107. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6108. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6109. expression := SyntaxTree.invalidExpression;
  6110. Error(position, "expression is not a constant integer");
  6111. END;
  6112. RETURN expression
  6113. END ConstantInteger;
  6114. (** check expression as positive (>=0) constant integer
  6115. - resolve expression
  6116. - if valid then check that integer value
  6117. - if integer value then check that value >= 0
  6118. report error and return invalidExpression if anything fails
  6119. **)
  6120. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6121. VAR position: Position;
  6122. BEGIN
  6123. position := expression.position;
  6124. expression := ConstantExpression(expression);
  6125. IF expression = SyntaxTree.invalidExpression THEN
  6126. (* error already reported *)
  6127. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6128. Error(position, "expression is not integer valued");
  6129. expression := SyntaxTree.invalidExpression
  6130. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6131. Error(position, "integer is not greater or equal zero");
  6132. END;
  6133. RETURN expression
  6134. END ConstantIntegerGeq0;
  6135. (** check expression as condition
  6136. - resolve expression
  6137. - if valid expression then check that result type is boolean
  6138. report error and return invalidExpression if anything fails
  6139. **)
  6140. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6141. VAR position: Position;
  6142. BEGIN
  6143. position := expression.position;
  6144. expression := ResolveExpression(expression);
  6145. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6146. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6147. expression := SyntaxTree.invalidExpression;
  6148. Error(position, "expression is not boolean");
  6149. END;
  6150. RETURN expression
  6151. END ResolveCondition;
  6152. (*** symbols ***)
  6153. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6154. BEGIN
  6155. x.Accept(SELF);
  6156. END ResolveSymbol;
  6157. (** check a symbol
  6158. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6159. **)
  6160. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6161. VAR scope: SyntaxTree.Scope;
  6162. BEGIN
  6163. (* visibility *)
  6164. scope := symbol.scope;
  6165. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6166. scope := scope.outerScope;
  6167. END;
  6168. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6169. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6170. Error(symbol.position,"cannot be exported");
  6171. IF VerboseErrorMessage THEN
  6172. Printout.Info("symbol",symbol);
  6173. END;
  6174. END;
  6175. END;
  6176. END CheckSymbolVisibility;
  6177. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6178. If node is currently being resolved then emit a cyclic definition error.
  6179. Return TRUE only if node is fully resolved.
  6180. **)
  6181. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6182. VAR result: BOOLEAN;
  6183. BEGIN
  6184. IF SyntaxTree.Resolved IN x.state THEN
  6185. result := FALSE
  6186. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6187. Error(x.position,"cyclic definition");
  6188. result := FALSE;
  6189. ELSE
  6190. result := TRUE;
  6191. x.SetState(SyntaxTree.BeingResolved)
  6192. END;
  6193. RETURN result
  6194. END SymbolNeedsResolution;
  6195. (** check and resolve a type declaration symbol = Type
  6196. - set type to declaration type
  6197. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6198. This is so because the type declaration itself does not have a type but it only stands for a type.
  6199. In the implementation of the compiler this made a lot much easier.
  6200. - resolve and set declared type
  6201. - check symbol
  6202. **)
  6203. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6204. VAR prevScope: SyntaxTree.Scope;
  6205. BEGIN
  6206. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6207. IF SymbolNeedsResolution(typeDeclaration) THEN
  6208. typeDeclaration.SetState(SyntaxTree.Resolved);
  6209. prevScope := currentScope;
  6210. currentScope := typeDeclaration.scope;
  6211. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6212. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6213. CheckSymbolVisibility(typeDeclaration);
  6214. typeDeclaration.SetState(SyntaxTree.Resolved);
  6215. currentScope := prevScope;
  6216. END;
  6217. END VisitTypeDeclaration;
  6218. (** check and resolve a constant declaration symbol = (constant) expression
  6219. - check expression
  6220. - set type and value
  6221. - check symbol
  6222. **)
  6223. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6224. VAR
  6225. expression: SyntaxTree.Expression;
  6226. type: SyntaxTree.Type;
  6227. name: Basic.SegmentedName;
  6228. replacement: Replacement;
  6229. BEGIN
  6230. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6231. IF SymbolNeedsResolution(constant) THEN
  6232. expression := constant.value;
  6233. IF replacements # NIL THEN
  6234. Global.GetSymbolSegmentedName(constant, name);
  6235. replacement := replacements;
  6236. WHILE (replacement # NIL) & (replacement.name # name) DO
  6237. replacement := replacement.next;
  6238. END;
  6239. IF replacement # NIL THEN
  6240. InfoSS(constant.position, "replacing constant", constant.name);
  6241. (*
  6242. NEW(stringReader, Strings.Length(replacement.string^));
  6243. stringReader.Set(replacement.string^);
  6244. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6245. NEW(parser, scanner, diagnostics);
  6246. expression := parser.Expression();
  6247. *)
  6248. expression := replacement.expression;
  6249. replacement.used := TRUE;
  6250. END;
  6251. END;
  6252. constant.SetType(SyntaxTree.invalidType);
  6253. expression := ConstantExpression(expression);
  6254. ASSERT(expression.type # NIL);
  6255. type := expression.type.resolved;
  6256. constant.SetType(type);
  6257. constant.SetValue(expression);
  6258. CheckSymbolVisibility(constant);
  6259. constant.SetState(SyntaxTree.Resolved);
  6260. END;
  6261. END VisitConstant;
  6262. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6263. VAR procedureAlignment: LONGINT;
  6264. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6265. (* least common multiple *)
  6266. VAR a,b: LONGINT;
  6267. BEGIN
  6268. a := a0; b := b0;
  6269. WHILE (a # b) DO
  6270. IF a < b THEN a := a+a0
  6271. ELSE b := b + b0
  6272. END;
  6273. END;
  6274. RETURN a
  6275. END LCM;
  6276. BEGIN
  6277. IF alignment > 1 THEN
  6278. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6279. IF (procedureAlignment > 1) THEN
  6280. alignment := LCM(alignment, procedureAlignment);
  6281. END;
  6282. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6283. END;
  6284. END AdaptStackAlignment;
  6285. (** check and resolve a variable / field
  6286. - check and set type
  6287. - negative check on open array type
  6288. - check symbol
  6289. **)
  6290. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6291. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6292. BEGIN
  6293. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6294. IF SymbolNeedsResolution(variable) THEN
  6295. modifiers := variable.modifiers;
  6296. (*
  6297. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6298. variable.AddFlags(flags);
  6299. *)
  6300. variable.SetType(ResolveType(variable.type));
  6301. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6302. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6303. Error(variable.position,"forbidden open array variable");
  6304. END;
  6305. END;
  6306. CheckSymbolVisibility(variable);
  6307. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6308. variable.SetUntraced(TRUE);
  6309. IF ~ContainsPointer(variable.type) THEN
  6310. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6311. Error(position, "untraced flag on non-pointer variable");
  6312. END;
  6313. END;
  6314. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6315. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6316. IF ~PowerOf2(value) THEN
  6317. Error(position, "forbidden alignment - must be power of two");
  6318. ELSE
  6319. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6320. END;
  6321. END;
  6322. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6323. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6324. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6325. Error(position, "fixed position not possible in procedure");
  6326. END;
  6327. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6328. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6329. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6330. Error(position,"fictive offset not possible in procedure");
  6331. END;
  6332. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6333. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6334. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6335. END;
  6336. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6337. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6338. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6339. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6340. END;
  6341. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6342. IF variable.initializer # NIL THEN
  6343. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6344. END;
  6345. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6346. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6347. pointerType.SetPointerBase(variable.type);
  6348. pointerType.SetHidden(TRUE);
  6349. variable.SetType(ResolveType(pointerType));
  6350. END;
  6351. variable.SetState(SyntaxTree.Resolved);
  6352. END;
  6353. END VisitVariable;
  6354. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6355. BEGIN
  6356. VisitVariable(property)
  6357. END VisitProperty;
  6358. (** check and resolve a (procedure) parameter
  6359. - check and set type
  6360. - check symbol
  6361. - check parameter kind and set read-only flags if appropriate
  6362. **)
  6363. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6364. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6365. BEGIN
  6366. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6367. IF SymbolNeedsResolution(parameter) THEN
  6368. modifiers := parameter.modifiers;
  6369. parameter.SetType(ResolveType(parameter.type));
  6370. ASSERT(parameter.type.resolved # NIL);
  6371. CheckSymbolVisibility(parameter);
  6372. IF parameter.defaultValue # NIL THEN
  6373. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6374. Error(parameter.position,"forbidden default value on non-value parameter");
  6375. ELSE
  6376. expression := ConstantExpression(parameter.defaultValue);
  6377. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6378. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6379. parameter.SetDefaultValue(expression);
  6380. END;
  6381. END;
  6382. END;
  6383. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6384. Error(parameter.position, "forbidden value parameter of math array type ");
  6385. END;
  6386. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6387. parameter.SetUntraced(TRUE);
  6388. IF ~ContainsPointer(parameter.type) THEN
  6389. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6390. Error(position, "untraced flag on non-pointer variable");
  6391. END;
  6392. END;
  6393. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6394. parameter.SetMoveable(TRUE);
  6395. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6396. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6397. Error(position, "illegal movable flag on non-address variable");
  6398. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6399. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6400. Error(position, "unnecessary movable flag on variable variable");
  6401. END;
  6402. END;
  6403. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6404. parameter.SetState(SyntaxTree.Resolved);
  6405. END;
  6406. END VisitParameter;
  6407. (** check and resolve a procedure (with declaration and implementation scope)
  6408. - check the procedure type
  6409. - check if method (i.e. in record scope), if so then
  6410. - check if (unique) constructor
  6411. - check if (unique) finalizer
  6412. - check if super method available, if so then check signature
  6413. - of not in record scope then negative check on constructor flag
  6414. - of not in record scope then negative check on finalizer flag
  6415. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6416. - check procedure symbol
  6417. **)
  6418. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6419. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6420. procedureType: SyntaxTree.ProcedureType;
  6421. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6422. qualifiedType: SyntaxTree.QualifiedType;
  6423. value: Basic.Integer;
  6424. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6425. position: Position;
  6426. fp: SyntaxTree.FingerPrint;
  6427. BEGIN
  6428. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6429. IF IsOberonInline(procedure) THEN
  6430. IF SyntaxTree.Public * procedure.access # {} THEN
  6431. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6432. END;
  6433. procedure.SetInline(FALSE);
  6434. procedure.SetOberonInline(TRUE);
  6435. END;
  6436. IF SymbolNeedsResolution(procedure) THEN
  6437. recentIsRealtime := currentIsRealtime;
  6438. recentIsBodyProcedure := currentIsBodyProcedure;
  6439. CheckSymbolVisibility(procedure);
  6440. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6441. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6442. modifiers := procedureType.modifiers;
  6443. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6444. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6445. IF useDarwinCCalls THEN (*fld*)
  6446. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6447. ELSE
  6448. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6449. END
  6450. END;
  6451. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6452. procedureType.SetInterrupt(TRUE);
  6453. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6454. END;
  6455. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6456. procedureType.SetNoReturn(TRUE);
  6457. END;
  6458. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6459. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6460. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6461. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6462. END;
  6463. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6464. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6465. END;
  6466. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6467. IF ~PowerOf2(value) THEN
  6468. Error(position, "forbidden stack alignment - must be power of two");
  6469. ELSE
  6470. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6471. END;
  6472. END;
  6473. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6474. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6475. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6476. END;
  6477. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6478. SyntaxTree.InitFingerPrint(fp);
  6479. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6480. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6481. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6482. fp.shallowAvailable := TRUE;
  6483. procedure.SetFingerPrint(fp);
  6484. END;
  6485. CheckModifiers(modifiers, TRUE);
  6486. modifiers := procedureType.returnTypeModifiers;
  6487. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6488. CheckModifiers(modifiers, TRUE);
  6489. procedure.SetState(SyntaxTree.Resolved);
  6490. FixProcedureType(procedureType);
  6491. currentIsRealtime := procedureType.isRealtime;
  6492. currentIsBodyProcedure := procedure.isBodyProcedure;
  6493. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6494. THEN
  6495. Error(procedure.position,"problems during parameter offset computation");
  6496. END;
  6497. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6498. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6499. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6500. Error(procedure.position,"problem during parameter offset generation");
  6501. END;
  6502. END;
  6503. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6504. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6505. procedureType.SetDelegate(TRUE);
  6506. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6507. (* add auto-self *)
  6508. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6509. IF (record.pointerType.typeDeclaration = NIL) THEN
  6510. selfParameter.SetType(record.pointerType);
  6511. ELSE
  6512. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6513. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6514. qualifiedType.SetResolved(record.pointerType);
  6515. selfParameter.SetType(qualifiedType);
  6516. END;
  6517. selfParameter.SetAccess(SyntaxTree.Hidden);
  6518. END;
  6519. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6520. IF procedure.isConstructor THEN
  6521. (*! constructor is always visible, compatibility to paco
  6522. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6523. *)
  6524. procedure.MarkUsed;
  6525. IF procedureType.returnType # NIL THEN
  6526. Error(procedure.position,"constructor with forbidden return type");
  6527. END;
  6528. proc := procedure.scope.firstProcedure;
  6529. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6530. proc := proc.nextProcedure;
  6531. END;
  6532. IF proc # NIL THEN
  6533. Error(procedure.position,"duplicate constructor")
  6534. ELSE
  6535. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6536. END;
  6537. END;
  6538. IF procedure.isFinalizer THEN
  6539. procedure.MarkUsed;
  6540. IF procedureType.returnType # NIL THEN
  6541. Error(procedure.position,"finalizer with forbidden return type");
  6542. END;
  6543. IF procedureType.numberParameters # 0 THEN
  6544. Error(procedure.position,"finalizer with formal parameters");
  6545. END;
  6546. proc := procedure.scope.firstProcedure;
  6547. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6548. proc := proc.nextProcedure;
  6549. END;
  6550. IF proc # NIL THEN
  6551. Error(procedure.position,"duplicate finalizer")
  6552. ELSE
  6553. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6554. END;
  6555. END;
  6556. super := FindSuperProcedure(record.recordScope, procedure);
  6557. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6558. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6559. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6560. END;
  6561. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6562. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6563. END;
  6564. IF super.isFinal THEN
  6565. Error(procedure.position,"forbidden method extending final method");
  6566. END;
  6567. (*
  6568. IF super.access # procedure.access THEN
  6569. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6570. END;
  6571. *)
  6572. procedure.SetSuper(super);
  6573. super.SetOverwritten(TRUE);
  6574. procedure.SetAccess(procedure.access+super.access);
  6575. procedure.MarkUsed;
  6576. END;
  6577. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6578. THEN
  6579. Error(procedure.position,"problems during parameter offset computation");
  6580. END;
  6581. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6582. IF cellsAreObjects THEN
  6583. procedureType.SetDelegate(TRUE);
  6584. END;
  6585. IF procedure.isConstructor THEN
  6586. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6587. END;
  6588. ELSIF procedure.isConstructor THEN
  6589. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6590. END;
  6591. Declarations(procedure.procedureScope, FALSE, {0,1});
  6592. (* body resolution part done as late fix of the procedure type *)
  6593. procedure.SetState(SyntaxTree.Resolved);
  6594. currentIsRealtime := recentIsRealtime;
  6595. currentIsBodyProcedure := recentIsBodyProcedure;
  6596. END;
  6597. END VisitProcedure;
  6598. (**
  6599. a builtin procedure is a global item that may not be modified locally
  6600. instead the resolving of builtin procedure calls are done in the esignator
  6601. **)
  6602. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6603. VAR type: SyntaxTree.Type;
  6604. BEGIN
  6605. type := ResolveType(builtinProcedure.type);
  6606. END VisitBuiltin;
  6607. (* nopov *)
  6608. (** check and resolve operator
  6609. - operators are first checked as procedures
  6610. - then additional operator-specific checks are done
  6611. - note that only module-scope operators are checked here
  6612. (operators in a record scope are only allowed in the context of
  6613. array-structured object types and checked in 'ResolveArrayStructure')
  6614. - also note that inter-operator conformity is not checked here
  6615. **)
  6616. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6617. VAR
  6618. procedureType: SyntaxTree.ProcedureType;
  6619. leftType, rightType: SyntaxTree.Type;
  6620. identifierNumber: LONGINT; position: Position;
  6621. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6622. modifiers: SyntaxTree.Modifier;
  6623. (** whether a type is locally defined in the current module scope
  6624. for arrays, the base type must be locally defined **)
  6625. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6626. BEGIN
  6627. IF type = NIL THEN
  6628. RETURN FALSE
  6629. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6630. RETURN TRUE
  6631. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6632. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6633. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6634. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6635. ELSE
  6636. RETURN FALSE
  6637. END
  6638. END IsLocallyDefined;
  6639. BEGIN
  6640. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6641. procedureType := operator.type(SyntaxTree.ProcedureType);
  6642. modifiers := procedureType.modifiers;
  6643. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6644. CheckModifiers(modifiers, TRUE);
  6645. VisitProcedure(operator);
  6646. IF operator.scope IS SyntaxTree.RecordScope THEN
  6647. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6648. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6649. IF identifierNumber = -1 THEN
  6650. Error(operator.position, "operator with unknown identifier")
  6651. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6652. Error(operator.position, "identifier may not be used for operator")
  6653. ELSE
  6654. IF procedureType.numberParameters < 1 THEN
  6655. Error(operator.position, "operator without operand");
  6656. ELSIF procedureType.numberParameters > 2 THEN
  6657. Error(operator.position, "operator with more than two operands");
  6658. ELSE
  6659. (* determine operand types *)
  6660. leftType := procedureType.firstParameter.type;
  6661. IF procedureType.numberParameters > 1 THEN
  6662. rightType := procedureType.firstParameter.nextParameter.type
  6663. ELSE
  6664. rightType := NIL
  6665. END;
  6666. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6667. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6668. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6669. Error(operator.position, "none of the operands is declared in the same module")
  6670. END
  6671. END;
  6672. (* TODO: refine the checks, think about how restrictive the checks should be
  6673. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6674. They might be used for intersection, union, complement of custom object types *)
  6675. (* defaults *)
  6676. hasReturnType := TRUE;
  6677. mustBeUnary := FALSE;
  6678. mustBeBinary := FALSE;
  6679. mustReturnBoolean := FALSE;
  6680. mustReturnInteger := FALSE;
  6681. mustHaveEquitypedOperands := FALSE;
  6682. (* operator-specific exceptions *)
  6683. CASE identifierNumber OF
  6684. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6685. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6686. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6687. mustBeBinary := TRUE
  6688. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6689. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6690. | Scanner.Times: mustBeBinary := TRUE
  6691. | Scanner.TimesTimes: mustBeBinary := TRUE
  6692. | Scanner.DotTimes: mustBeBinary := TRUE
  6693. | Scanner.PlusTimes: mustBeBinary := TRUE
  6694. | Scanner.Slash: mustBeBinary := TRUE
  6695. | Scanner.Backslash: mustBeBinary := TRUE
  6696. | Scanner.DotSlash: mustBeBinary := TRUE
  6697. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6698. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6699. | Scanner.Not: mustBeUnary := TRUE
  6700. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6701. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6702. | Scanner.Transpose: mustBeUnary := TRUE;
  6703. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6704. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6705. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6706. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6707. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6708. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6709. | Global.Abs: mustBeUnary := TRUE;
  6710. | Global.Ash: (* TODO: arity? *)
  6711. | Global.Cap: (* TODO: arity? *)
  6712. | Global.Chr: mustBeUnary := TRUE;
  6713. | Global.Entier: (* TODO: arity? *)
  6714. | Global.EntierH: (* TODO: arity? *)
  6715. | Global.Len: (* unary and binary *)
  6716. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6717. | Global.Max, Global.Min: (* unary and binary *)
  6718. | Global.Odd: (* TODO: arity? *)
  6719. | Global.Sum: (* TODO: arity? *)
  6720. | Global.All: (* TODO: arity? *)
  6721. | Global.Re, Global.Im:
  6722. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6723. | Scanner.Alias:
  6724. | Scanner.GreaterGreater, Scanner.LessLess:
  6725. mustBeBinary := TRUE; hasReturnType := FALSE;
  6726. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6727. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6728. END;
  6729. (* check parameter count *)
  6730. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6731. Error(operator.position,"operator is not unary")
  6732. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6733. Error(operator.position,"operator is not binary")
  6734. END;
  6735. (* check parameter types *)
  6736. (* TODO: is this used at all? *)
  6737. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6738. leftType := procedureType.firstParameter.type;
  6739. rightType := procedureType.firstParameter.nextParameter.type;
  6740. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6741. Error(operator.position, "the two operands are not of the same type")
  6742. END
  6743. END;
  6744. (* check return type *)
  6745. IF hasReturnType THEN
  6746. IF procedureType.returnType = NIL THEN
  6747. Error(operator.position, "return type required")
  6748. ELSIF mustReturnBoolean THEN
  6749. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6750. Error(operator.position,"return type is not Boolean")
  6751. END
  6752. ELSIF mustReturnInteger THEN
  6753. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6754. Error(operator.position,"return type is not integer")
  6755. END
  6756. END
  6757. ELSIF procedureType.returnType # NIL THEN
  6758. Error(operator.position, "return type not allowed")
  6759. END
  6760. END
  6761. END
  6762. END
  6763. END VisitOperator;
  6764. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6765. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6766. BEGIN
  6767. IF error THEN RETURN FALSE END;
  6768. prevScope := currentScope;
  6769. prevDiagnostics := diagnostics;
  6770. diagnostics := NIL; (* suppress error output *)
  6771. currentScope := module.moduleScope;
  6772. VisitImport(x);
  6773. IF ~error THEN
  6774. module.moduleScope.AddImport(x);
  6775. x.SetScope(module.moduleScope);
  6776. END;
  6777. currentScope := prevScope;
  6778. diagnostics := prevDiagnostics;
  6779. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6780. END AddImport;
  6781. (** check and resolve import
  6782. - check for name = SYSTEM
  6783. - check for forbidden self import
  6784. - search through global import cache: already imported?
  6785. - check if already imported indirectly
  6786. - import if necessary -> set module and enter into import cache
  6787. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6788. - after this import this direct import and all indirect imports are stored in the current module's import list
  6789. **)
  6790. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6791. VAR
  6792. module: SyntaxTree.Module;
  6793. moduleScope: SyntaxTree.ModuleScope;
  6794. import,reimport: SyntaxTree.Import;
  6795. filename: FileName;
  6796. prevScope: SyntaxTree.Scope;
  6797. BEGIN
  6798. IF SymbolNeedsResolution(x) THEN
  6799. prevScope := currentScope;
  6800. x.SetType(SyntaxTree.importType);
  6801. moduleScope := currentScope.ownerModule.moduleScope;
  6802. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6803. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6804. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6805. Error(x.position,"forbidden self import");
  6806. ELSE
  6807. (* search through global import list: already imported ? *)
  6808. IF (x.module = NIL) & (importCache # NIL) THEN
  6809. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6810. ELSE import := NIL
  6811. END;
  6812. IF x.module # NIL THEN (* already imported indirectly *)
  6813. module := x.module;
  6814. ELSIF import # NIL THEN (* already in module list *)
  6815. module := import.module;
  6816. ASSERT(module # NIL);
  6817. x.SetModule(module);
  6818. ELSE (* must be imported *)
  6819. Global.ModuleFileName(x.moduleName,x.context,filename);
  6820. IF symbolFileFormat # NIL THEN
  6821. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6822. IF module = NIL THEN
  6823. ErrorSS(x.position,"could not import",filename);
  6824. IF VerboseErrorMessage THEN
  6825. Printout.Info("import",x)
  6826. END
  6827. ELSE
  6828. (*
  6829. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6830. (*! should rather be done by importer *)
  6831. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6832. checker.importCache := importCache;
  6833. checker.arrayBaseImported := arrayBaseImported;
  6834. checker.global := global;
  6835. checker.Module(module); (* semantic check *)
  6836. error := error OR checker.error;
  6837. END;
  6838. *)
  6839. (*
  6840. ASSERT(SyntaxTree.Resolved IN module.state);
  6841. *)
  6842. x.SetModule(module);
  6843. IF importCache # NIL THEN
  6844. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6845. import.SetContext(x.context);
  6846. import.SetModule(module);
  6847. importCache.AddImport(import);
  6848. END;
  6849. END;
  6850. ELSE
  6851. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6852. END;
  6853. END;
  6854. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6855. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6856. END;
  6857. import := module.moduleScope.firstImport;
  6858. WHILE(import # NIL) DO
  6859. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6860. ASSERT(currentScope # NIL);
  6861. ASSERT(currentScope.ownerModule # NIL);
  6862. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6863. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6864. Error(x.position,"recursive import");
  6865. ELSE
  6866. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6867. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6868. IF reimport = NIL THEN (* indirect import *)
  6869. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6870. reimport.SetContext(import.context);
  6871. reimport.SetModule(import.module);
  6872. moduleScope.AddImport(reimport);
  6873. reimport.SetScope(moduleScope);
  6874. ELSE
  6875. ASSERT(import.module # NIL);
  6876. reimport.SetModule(import.module); (* direct or indirect import *)
  6877. END;
  6878. END;
  6879. import := import.nextImport;
  6880. END;
  6881. END;
  6882. END;
  6883. currentScope := prevScope;
  6884. (* ELSE nothing to be done *)
  6885. x.SetState(SyntaxTree.Resolved);
  6886. END;
  6887. END VisitImport;
  6888. (*** statements ***)
  6889. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6890. VAR prev,resolved: SyntaxTree.Statement;
  6891. BEGIN
  6892. prev := resolvedStatement;
  6893. resolvedStatement := x;
  6894. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6895. activeCellsStatement := FALSE;
  6896. x.Accept(SELF);
  6897. (* removed this, implementation restriction should be resolved by backend
  6898. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6899. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6900. END;
  6901. *)
  6902. resolved := resolvedStatement;
  6903. resolvedStatement := prev;
  6904. RETURN resolved
  6905. END ResolveStatement;
  6906. (** check and resolve statement sequence
  6907. - check all statements, replace if necessary
  6908. **)
  6909. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6910. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6911. BEGIN
  6912. IF statementSequence # NIL THEN (* else empty *)
  6913. FOR i := 0 TO statementSequence.Length()-1 DO
  6914. statement := statementSequence.GetStatement(i);
  6915. resolved := ResolveStatement(statement);
  6916. IF (resolved # statement) THEN
  6917. statementSequence.SetStatement(i,resolved);
  6918. END;
  6919. END;
  6920. END;
  6921. END StatementSequence;
  6922. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6923. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6924. - check if procedure is callable
  6925. - check return type = NIL (otherwise must be assignment statement)
  6926. **)
  6927. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6928. VAR call: SyntaxTree.Designator;
  6929. BEGIN
  6930. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6931. call := procedureCall.call;
  6932. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6933. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6934. END;
  6935. call := ResolveDesignator(call);
  6936. IF call = SyntaxTree.invalidDesignator THEN
  6937. (* error already handled *)
  6938. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6939. (* inline call in a statement *)
  6940. ELSIF ~IsCallable(call) THEN
  6941. Error(procedureCall.position,"called object is not a procedure");
  6942. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6943. Error(procedureCall.position,"calling procedure with non-void return type");
  6944. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6945. END;
  6946. procedureCall.SetCall(call);
  6947. (*
  6948. IF call = SyntaxTree.invalidDesignator THEN
  6949. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6950. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6951. IF IsOberonInline(procedure) THEN
  6952. Warning(procedure.position,"call to inline proc");
  6953. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6954. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6955. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6956. resolvedStatement := block;
  6957. RETURN;
  6958. END;
  6959. END;
  6960. *)
  6961. END VisitProcedureCallStatement;
  6962. (** check and resolve assignment LHS := RHS
  6963. - resolve LHS and RHS
  6964. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6965. - check if assignment is compatible
  6966. - check if LHS is variable (i.e. assignable)
  6967. - convert RHS if necessary
  6968. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6969. - assignment between different ASOTs
  6970. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6971. - assignment to ASOT elements:
  6972. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6973. **)
  6974. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6975. VAR
  6976. left: SyntaxTree.Designator;
  6977. right, expression: SyntaxTree.Expression;
  6978. designator: SyntaxTree.Designator;
  6979. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6980. mathArrayType: SyntaxTree.MathArrayType;
  6981. BEGIN
  6982. right := ResolveExpression(assignment.right);
  6983. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6984. left := ResolveDesignator(assignment.left);
  6985. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6986. (* error already handled *)
  6987. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6988. (* LHS is index write operator call on ASOT *)
  6989. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6990. (* necessary ?
  6991. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6992. type := procedureType.firstParameter.type;
  6993. expression := procedureCallDesignator.parameters.GetExpression(0);
  6994. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6995. *)
  6996. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6997. ELSIF CheckVariable(left) THEN
  6998. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6999. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7000. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7001. (* conversion done by procedure call
  7002. (* try to convert to left argument *)
  7003. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7004. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7005. procedureCallDesignator.parameters.SetExpression(1, right);
  7006. END;
  7007. *)
  7008. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7009. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7010. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7011. ELSIF AssignmentCompatible(left, right) THEN
  7012. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7013. mathArrayType := MathArrayStructureOfType(left.type);
  7014. right := NewConversion(right.position, right, mathArrayType, NIL);
  7015. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7016. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7017. ELSE
  7018. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7019. assignment.SetLeft(left);
  7020. assignment.SetRight(right);
  7021. resolvedStatement := assignment
  7022. END
  7023. END
  7024. END
  7025. END VisitAssignment;
  7026. (** check and resolve assignment LHS := RHS
  7027. - resolve LHS and RHS
  7028. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7029. - check if assignment is compatible
  7030. - check if LHS is variable (i.e. assignable)
  7031. - convert RHS if necessary
  7032. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7033. - assignment between different ASOTs
  7034. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7035. - assignment to ASOT elements:
  7036. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7037. **)
  7038. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7039. VAR
  7040. left: SyntaxTree.Designator;
  7041. right: SyntaxTree.Expression;
  7042. inPort, outPort: SyntaxTree.PortType;
  7043. expression: SyntaxTree.Expression;
  7044. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7045. BEGIN
  7046. right := ResolveExpression(communication.right);
  7047. left := ResolveDesignator(communication.left);
  7048. communication.SetLeft(left);
  7049. communication.SetRight(right);
  7050. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7051. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7052. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7053. (* conversion done by procedure call
  7054. (* try to convert to left argument *)
  7055. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7056. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7057. procedureCallDesignator.parameters.SetExpression(1, right);
  7058. END;
  7059. *)
  7060. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7061. ELSE
  7062. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7063. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7064. (* error already handled *)
  7065. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7066. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7067. IF outPort.direction # SyntaxTree.OutPort THEN
  7068. Error(left.position,"not an out-port")
  7069. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7070. Error(left.position,"incompatible to port type");
  7071. ELSE
  7072. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7073. communication.SetRight(right)
  7074. END;
  7075. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7076. IF CheckVariable(left) THEN
  7077. IF inPort.direction # SyntaxTree.InPort THEN
  7078. Error(left.position,"not an in-port")
  7079. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7080. Error(right.position,"incompatible to port type");
  7081. END;
  7082. END;
  7083. ELSE
  7084. Error(communication.position,"unsupported stream operation");
  7085. END;
  7086. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7087. IF outPort.direction # SyntaxTree.OutPort THEN
  7088. Error(left.position,"not an out-port")
  7089. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7090. Error(left.position,"incompatible to port type");
  7091. ELSE
  7092. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7093. communication.SetRight(right)
  7094. END;
  7095. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7096. IF CheckVariable(right) THEN
  7097. IF inPort.direction # SyntaxTree.InPort THEN
  7098. Error(left.position,"not an in-port")
  7099. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7100. Error(right.position,"incompatible to port type");
  7101. END;
  7102. END;
  7103. ELSE
  7104. Error(communication.position, "unsupported operation");
  7105. END;
  7106. END;
  7107. END VisitCommunicationStatement;
  7108. (** check and resolve if/eslif part
  7109. - check condition
  7110. - check statement sequence
  7111. **)
  7112. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7113. VAR prevUnreachable, b: BOOLEAN;
  7114. BEGIN
  7115. prevUnreachable := currentIsUnreachable;
  7116. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7117. IF IsBooleanValue(ifPart.condition,b) THEN
  7118. IF b=FALSE THEN
  7119. currentIsUnreachable := TRUE
  7120. ELSIF b=TRUE THEN
  7121. true := TRUE
  7122. END;
  7123. END;
  7124. StatementSequence(ifPart.statements);
  7125. currentIsUnreachable := prevUnreachable;
  7126. END IfPart;
  7127. (** check and resolve if statement
  7128. - check if parts and else part statement sequence
  7129. **)
  7130. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7131. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7132. BEGIN
  7133. prevUnreachable := currentIsUnreachable;
  7134. ifPartTrue := FALSE;
  7135. IfPart(ifStatement.ifPart,ifPartTrue);
  7136. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7137. elsif := ifStatement.GetElsifPart(i);
  7138. IfPart(elsif,ifPartTrue);
  7139. END;
  7140. IF ifStatement.elsePart # NIL THEN
  7141. IF ifPartTrue THEN
  7142. currentIsUnreachable := TRUE
  7143. END;
  7144. StatementSequence(ifStatement.elsePart)
  7145. END;
  7146. currentIsUnreachable := prevUnreachable;
  7147. END VisitIfStatement;
  7148. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7149. VAR variable: SyntaxTree.Designator;
  7150. type,variableType: SyntaxTree.Type;
  7151. withEntry: WithEntry;
  7152. BEGIN
  7153. variable := ResolveDesignator(withPart.variable);
  7154. variableType := variable.type.resolved;
  7155. withPart.SetVariable(variable);
  7156. type := ResolveType(withPart.type);
  7157. withPart.SetType(type);
  7158. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7159. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7160. END;
  7161. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7162. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7163. Error(variable.position,"is not extensible designator");
  7164. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7165. Error(variable.position,"is no local variable ");
  7166. IF VerboseErrorMessage THEN
  7167. Printout.Info("variable",variable)
  7168. END;
  7169. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7170. Error(variable.position,"withguarded symbol is no type extension of ");
  7171. IF VerboseErrorMessage THEN
  7172. Printout.Info("variable",variable);
  7173. Printout.Info("type",type);
  7174. END;
  7175. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7176. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7177. Error(variable.position,"withguarded symbol is no variable ");
  7178. IF VerboseErrorMessage THEN
  7179. Printout.Info("variable",variable);
  7180. Printout.Info("type",type);
  7181. END;
  7182. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7183. Error(variable.position,"invalid change of withguarded symbol");
  7184. ELSE
  7185. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7186. NEW(withEntry);
  7187. withEntry.previous := withEntries;
  7188. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7189. withEntry.type := type;
  7190. withEntries := withEntry;
  7191. StatementSequence(withPart.statements);
  7192. withEntries := withEntries.previous;
  7193. END;
  7194. END WithPart;
  7195. (** check and resolve with statement WITH variable: type DO ... END;
  7196. - check type and variable
  7197. - check that variable type is type extension of type
  7198. - check that variable is a variable
  7199. - enter new with scope and enter guardedVariable with same name and reference to variable
  7200. - create if statement:
  7201. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7202. **)
  7203. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7204. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7205. BEGIN
  7206. prevScope := currentScope; symbol := NIL;
  7207. FOR i := 0 TO withStatement.WithParts()-1 DO
  7208. WithPart(withStatement.GetWithPart(i),symbol);
  7209. END;
  7210. IF withStatement.elsePart # NIL THEN
  7211. StatementSequence(withStatement.elsePart)
  7212. END;
  7213. currentScope := prevScope;
  7214. END VisitWithStatement;
  7215. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7216. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7217. - check 'first' < 'last' and no overlaps between different case labels
  7218. - check statement sequence
  7219. **)
  7220. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7221. VAR
  7222. i: LONGINT;
  7223. position: Position;
  7224. expression, left, right: SyntaxTree.Expression;
  7225. expressionType: SyntaxTree.Type;
  7226. l, r: Basic.Integer;
  7227. cl, cr: CHAR;
  7228. thiscases: SyntaxTree.CaseConstant;
  7229. BEGIN
  7230. thiscases := NIL;
  7231. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7232. expression := casePart.elements.GetExpression(i);
  7233. position := expression.position;
  7234. (* set context of range *)
  7235. IF expression IS SyntaxTree.RangeExpression THEN
  7236. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7237. END;
  7238. expression := ResolveExpression(expression);
  7239. IF expression = SyntaxTree.invalidExpression THEN
  7240. (* error already reported *)
  7241. expressionType := SyntaxTree.invalidType;
  7242. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7243. (* read out 'first' and 'last' *)
  7244. left := expression(SyntaxTree.RangeExpression).first;
  7245. right := expression(SyntaxTree.RangeExpression).last;
  7246. (* guaranteed by VisitRangeExpression: *)
  7247. ASSERT((left # NIL) & (right # NIL));
  7248. ASSERT(left.type.resolved = right.type.resolved);
  7249. left := CompatibleConversion(left.position, left, type);
  7250. right := CompatibleConversion(right.position, right, type);
  7251. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7252. expression(SyntaxTree.RangeExpression).SetLast(right);
  7253. expressionType := RegularType(position,left.type);
  7254. ELSE
  7255. expression := ConstantExpression(expression);
  7256. expression := CompatibleConversion(expression.position, expression, type);
  7257. (*
  7258. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7259. left := Global.NewCharacterValue(system,expression.position,cl);
  7260. expression := casePart.elements.GetExpression(i);
  7261. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7262. expression := left
  7263. END;
  7264. *)
  7265. casePart.elements.SetExpression(i,expression);
  7266. left := expression; right := expression;
  7267. expressionType := RegularType(position,expression.type)
  7268. END;
  7269. IF (expressionType = SyntaxTree.invalidType) THEN
  7270. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7271. Error(position, "inadmissible case label");
  7272. expression := SyntaxTree.invalidExpression;
  7273. ELSE
  7274. l := 0; r := 0;
  7275. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7276. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7277. l := ORD(cl); r := ORD(cr);
  7278. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7279. ELSE
  7280. expression := SyntaxTree.invalidExpression
  7281. END;
  7282. IF expression # SyntaxTree.invalidExpression THEN
  7283. IF l>r THEN
  7284. Error(position, "empty case label")
  7285. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7286. Error(position, "duplicate case label");
  7287. ELSE
  7288. IF l < min THEN min := l END;
  7289. IF r > max THEN max := r END;
  7290. END;
  7291. END;
  7292. END;
  7293. casePart.elements.SetExpression(i,expression);
  7294. END;
  7295. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7296. casePart.SetConstants(thiscases);
  7297. StatementSequence(casePart.statements);
  7298. END CasePart;
  7299. (** check and resolve case statement CASE variable OF ... END;
  7300. - check variable
  7301. - check case parts
  7302. **)
  7303. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7304. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7305. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer; msg: ARRAY 64 OF CHAR;
  7306. BEGIN
  7307. expression := ResolveExpression(caseStatement.variable);
  7308. type := RegularType(expression.position,expression.type);
  7309. IF type = SyntaxTree.invalidType THEN
  7310. expression := SyntaxTree.invalidExpression;
  7311. ELSIF IsIntegerType(type) THEN
  7312. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7313. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7314. (*
  7315. expression := Global.NewCharacterValue(system,expression.position,ch);
  7316. *)
  7317. type := expression.type;
  7318. ELSIF IsCharacterType(type) THEN
  7319. ELSIF IsEnumerationType(type) THEN
  7320. ELSE
  7321. Error(caseStatement.variable.position,"variable must be integer or character type");
  7322. expression := SyntaxTree.invalidExpression;
  7323. END;
  7324. caseStatement.SetVariable(expression);
  7325. caseList := NIL;
  7326. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7327. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7328. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7329. END;
  7330. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7331. msg := "huge sparse case table ";
  7332. Strings.AppendInt(msg, max-min);
  7333. Strings.Append(msg,"/");
  7334. Strings.AppendInt(msg, caseStatement.CaseParts());
  7335. Warning(caseStatement.position,msg);
  7336. END;
  7337. caseStatement.SetMinMax(min,max);
  7338. StatementSequence(caseStatement.elsePart);
  7339. IF expression.resolved # NIL THEN
  7340. IF IsCharacterValue(expression,ch) THEN
  7341. l := ORD(ch)
  7342. ELSIF IsIntegerValue(expression,l) THEN
  7343. END;
  7344. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7345. END;
  7346. END VisitCaseStatement;
  7347. (** check and resolve while statement
  7348. - check condition
  7349. - check statement sequence
  7350. **)
  7351. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7352. VAR prevIsUnreachable,b: BOOLEAN;
  7353. BEGIN
  7354. prevIsUnreachable := currentIsUnreachable;
  7355. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7356. IF IsBooleanValue(whileStatement.condition,b) THEN
  7357. IF b=FALSE THEN
  7358. currentIsUnreachable := TRUE
  7359. END;
  7360. END;
  7361. StatementSequence(whileStatement.statements);
  7362. currentIsUnreachable := prevIsUnreachable
  7363. END VisitWhileStatement;
  7364. (** check and resolve repeat statement
  7365. - check condition
  7366. - check statement sequence
  7367. **)
  7368. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7369. BEGIN
  7370. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7371. StatementSequence(repeatStatement.statements);
  7372. END VisitRepeatStatement;
  7373. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7374. VAR withEntry: WithEntry;
  7375. BEGIN
  7376. withEntry := withEntries;
  7377. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7378. withEntry := withEntry.previous
  7379. END;
  7380. IF withEntry = NIL THEN RETURN FALSE
  7381. ELSE
  7382. type := withEntry.type;
  7383. RETURN TRUE
  7384. END;
  7385. END GetGuard;
  7386. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7387. - check that variable is an integer variable
  7388. - check that from is integer typed with compatible type
  7389. - check that to has compatible type
  7390. - check that by is constant integer with compatible type
  7391. **)
  7392. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7393. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7394. BEGIN
  7395. designator := ResolveDesignator(forStatement.variable);
  7396. type := SyntaxTree.invalidType;
  7397. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7398. designator := SyntaxTree.invalidDesignator;
  7399. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7400. Error(designator.position,"control variable of non-integer type");
  7401. designator := SyntaxTree.invalidDesignator;
  7402. ELSIF CheckVariable(designator) THEN
  7403. type := designator.type;
  7404. END;
  7405. forStatement.SetVariable(designator);
  7406. expression := ResolveExpression(forStatement.from);
  7407. IF expression = SyntaxTree.invalidExpression THEN
  7408. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7409. Error(expression.position,"start value of incompatible type");
  7410. expression := SyntaxTree.invalidExpression;
  7411. ELSIF type # SyntaxTree.invalidType THEN
  7412. expression := NewConversion(expression.position,expression,type,NIL)
  7413. END;
  7414. forStatement.SetFrom(expression);
  7415. expression := ResolveExpression(forStatement.to);
  7416. IF expression = SyntaxTree.invalidExpression THEN
  7417. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7418. Error(expression.position,"end value of incompatible type");
  7419. expression := SyntaxTree.invalidExpression;
  7420. ELSIF type # SyntaxTree.invalidType THEN
  7421. expression := NewConversion(expression.position,expression,type,NIL)
  7422. END;
  7423. forStatement.SetTo(expression);
  7424. IF forStatement.by # NIL THEN
  7425. expression := ConstantInteger(forStatement.by);
  7426. ELSE
  7427. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7428. END;
  7429. IF expression = SyntaxTree.invalidExpression THEN
  7430. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7431. Error(expression.position,"step value of incompatible type");
  7432. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7433. Error(expression.position,"invalid step value");
  7434. ELSIF type # SyntaxTree.invalidType THEN
  7435. expression := NewConversion(expression.position,expression,type,NIL)
  7436. END;
  7437. forStatement.SetBy(expression);
  7438. StatementSequence(forStatement.statements);
  7439. END VisitForStatement;
  7440. (** check and resolve loop statement LOOP StatementSequence END
  7441. - check statement sequence
  7442. **)
  7443. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7444. BEGIN
  7445. StatementSequence(loopStatement.statements)
  7446. END VisitLoopStatement;
  7447. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7448. BEGIN
  7449. StatementSequence(exitableBlock.statements);
  7450. END VisitExitableBlock;
  7451. (** check and resolve exit statement EXIT
  7452. - check that exit is within LOOP statement block
  7453. **)
  7454. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7455. VAR outer: SyntaxTree.Statement;
  7456. BEGIN
  7457. outer := exitStatement.outer;
  7458. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7459. outer := outer.outer;
  7460. END;
  7461. IF outer = NIL THEN
  7462. Error(exitStatement.position,"exit statement not within loop statement");
  7463. END;
  7464. END VisitExitStatement;
  7465. (** check and resolve return statement RETURN [expression]
  7466. - check expression (if any)
  7467. - check if in procedure scope
  7468. - if in procedure scope then check expression compatibility
  7469. - if not in procecdure scope then check on return without expression
  7470. **)
  7471. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7472. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7473. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7474. BEGIN
  7475. position := returnStatement.position;
  7476. expression := returnStatement.returnValue;
  7477. IF expression # NIL THEN
  7478. expression := ResolveExpression(expression);
  7479. returnStatement.SetReturnValue(expression);
  7480. END;
  7481. outer := returnStatement.outer;
  7482. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7483. outer := outer.outer
  7484. END;
  7485. IF (outer # NIL) THEN
  7486. scope := outer(SyntaxTree.Body).inScope;
  7487. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7488. IF (expression # NIL) THEN
  7489. Error(position, "return statement with parameter not in procedure scope");
  7490. END;
  7491. ELSE
  7492. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7493. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7494. Error(position, "return statement in procedure that does not return");
  7495. END;
  7496. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7497. IF returnType # NIL THEN
  7498. returnType := returnType.resolved;
  7499. IF expression = NIL THEN
  7500. Error(position, "empty return type in procedure providing a return type")
  7501. ELSIF expression.type = NIL THEN
  7502. Error(position,"returned type incompatible: expression has no type");
  7503. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7504. Error(position, "return type not compatible");
  7505. IF VerboseErrorMessage THEN
  7506. Printout.Info("returnType",returnType);
  7507. Printout.Info("expression",expression);
  7508. END;
  7509. ELSE
  7510. expression := NewConversion(expression.position,expression,returnType,NIL);
  7511. returnStatement.SetReturnValue(expression);
  7512. END;
  7513. ELSIF expression # NIL THEN
  7514. Error(position, "non-empty return type in procedure providing no return type");
  7515. END;
  7516. END;
  7517. END;
  7518. END VisitReturnStatement;
  7519. (** check and resolve await statement AWAIT(condition: Expression)
  7520. - check await condition
  7521. **)
  7522. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7523. VAR condition: SyntaxTree.Expression;
  7524. BEGIN
  7525. condition := ResolveCondition(awaitStatement.condition);
  7526. IF currentIsRealtime THEN
  7527. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7528. END;
  7529. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7530. Error(awaitStatement.position,"senseless await statement with constant condition");
  7531. END;
  7532. awaitStatement.SetCondition(condition);
  7533. END VisitAwaitStatement;
  7534. PROCEDURE CheckSystemImport(position: Position);
  7535. VAR import: SyntaxTree.Import;
  7536. BEGIN
  7537. import := currentScope.ownerModule.moduleScope.firstImport;
  7538. WHILE(import # NIL) DO
  7539. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7540. RETURN;
  7541. END;
  7542. import := import.nextImport;
  7543. END;
  7544. Error(position, "forbidden code without system import");
  7545. END CheckSystemImport;
  7546. (** check and resolve code statement: do nothing, must be done by assembler
  7547. **)
  7548. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7549. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7550. BEGIN
  7551. CheckSystemImport(code.position);
  7552. FOR i := 0 TO code.inRules.Length()-1 DO
  7553. statement := code.inRules.GetStatement(i);
  7554. IF statement IS SyntaxTree.Assignment THEN
  7555. WITH statement: SyntaxTree.Assignment DO
  7556. statement.SetRight(ResolveExpression(statement.right));
  7557. END;
  7558. ELSE
  7559. Error(statement.position, "can only be assignment")
  7560. END;
  7561. END;
  7562. FOR i := 0 TO code.outRules.Length()-1 DO
  7563. statement := code.outRules.GetStatement(i);
  7564. IF statement IS SyntaxTree.Assignment THEN
  7565. WITH statement: SyntaxTree.Assignment DO
  7566. statement.SetLeft(ResolveDesignator(statement.left));
  7567. END;
  7568. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7569. (* must be a reference to some register *)
  7570. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7571. ELSE
  7572. Printout.Info("out statement ", statement);
  7573. Error(statement.position, "(out) can only be assignment")
  7574. END;
  7575. END;
  7576. END VisitCode;
  7577. (** check and set flags of a statement block
  7578. - check for multiply occurence of a flag
  7579. - check and set priority only in bodies
  7580. - check for valid names
  7581. **)
  7582. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7583. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7584. flag: LONGINT; recordBody: SyntaxTree.Body;
  7585. PROCEDURE SetProtectedRecord;
  7586. VAR scope: SyntaxTree.Scope;
  7587. BEGIN
  7588. scope := currentScope;
  7589. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7590. scope := scope.outerScope
  7591. END;
  7592. IF scope # NIL THEN
  7593. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7594. END;
  7595. END SetProtectedRecord;
  7596. BEGIN
  7597. flags := {};
  7598. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7599. recordBody := block(SyntaxTree.Body)
  7600. ELSE
  7601. recordBody := NIL
  7602. END;
  7603. blockModifier := block.blockModifiers;
  7604. WHILE(blockModifier # NIL) DO
  7605. name := blockModifier.identifier;
  7606. expression := blockModifier.expression;
  7607. position := blockModifier.position;
  7608. flag := -1;
  7609. IF name=Global.NamePriority THEN
  7610. IF expression = NIL THEN
  7611. Error(position, "missing priority expression");
  7612. ELSIF recordBody = NIL THEN
  7613. Error(position, "priority not on record body");
  7614. ELSIF recordBody.priority # NIL THEN
  7615. Error(position, "duplicate priority expression");
  7616. ELSE
  7617. recordBody.SetPriority(expression);
  7618. END;
  7619. ELSIF expression # NIL THEN
  7620. Error(expression.position,"expression not in connection with priority")
  7621. ELSIF name=Global.NameExclusive THEN
  7622. IF block.isExclusive THEN
  7623. Error(position, "duplicate exclusive flag")
  7624. END;
  7625. block.SetExclusive(TRUE); SetProtectedRecord;
  7626. ELSIF name=Global.NameActive THEN
  7627. IF recordBody = NIL THEN
  7628. Error(position, "active not in record body");
  7629. ELSIF recordBody.isActive THEN
  7630. Error(position, "duplicate active flag")
  7631. ELSE
  7632. recordBody.SetActive(TRUE); SetProtectedRecord;
  7633. END;
  7634. ELSIF name=Global.NameSafe THEN
  7635. IF recordBody = NIL THEN
  7636. Error(position, "safe not in record body");
  7637. ELSIF recordBody.isSafe THEN
  7638. Error(position, "duplicate safe flag")
  7639. ELSE
  7640. recordBody.SetSafe(TRUE);
  7641. SetProtectedRecord;
  7642. END;
  7643. ELSIF name=Global.NameRealtime THEN
  7644. IF recordBody = NIL THEN
  7645. Error(position, "realtime not in record body");
  7646. ELSIF recordBody.isRealtime THEN
  7647. Error(position, "duplicate realtime flag")
  7648. ELSE
  7649. recordBody.SetRealtime(TRUE);
  7650. block.SetRealtime(TRUE);
  7651. END;
  7652. ELSIF name=Global.NameUnchecked THEN
  7653. IF block.isUnchecked THEN
  7654. Error(position, "duplicate unchecked flag")
  7655. ELSE
  7656. block.SetUnchecked(TRUE);
  7657. END;
  7658. ELSIF (name=Global.NameUncooperative) THEN
  7659. IF block.isUncooperative THEN
  7660. Error(position, "duplicate uncooperative flag")
  7661. ELSE
  7662. block.SetUncooperative(TRUE);
  7663. END;
  7664. ELSE
  7665. Error(position, "unknown block modifier");
  7666. END;
  7667. blockModifier := blockModifier.nextModifier;
  7668. END;
  7669. END BlockFlags;
  7670. (** check and resolve statement block
  7671. - check flags (exclusive)
  7672. - check statement sequence
  7673. **)
  7674. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7675. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7676. BEGIN
  7677. BlockFlags(statementBlock);
  7678. IF statementBlock.isExclusive THEN
  7679. (* check that not in exclusive block *)
  7680. IF currentIsExclusive THEN
  7681. Error (statementBlock.position,"forbidden recursive exclusive")
  7682. ELSIF currentIsRealtime THEN
  7683. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7684. END;
  7685. END;
  7686. recentExclusive := currentIsExclusive;
  7687. recentUnreachable := currentIsUnreachable;
  7688. recentRealtime := currentIsRealtime;
  7689. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7690. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7691. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7692. StatementSequence(statementBlock.statements);
  7693. currentIsRealtime := recentRealtime;
  7694. currentIsExclusive := recentExclusive;
  7695. currentIsUnreachable := recentUnreachable;
  7696. END VisitStatementBlock;
  7697. (** check and resolve body
  7698. - check flags (active, priority, safe)
  7699. - check body and finally part
  7700. **)
  7701. PROCEDURE Body(body: SyntaxTree.Body);
  7702. BEGIN
  7703. VisitStatementBlock(body);
  7704. IF body.isActive THEN
  7705. IF ~currentIsBodyProcedure THEN
  7706. Error(body.position,"active flag not in object body");
  7707. ELSIF body.priority # NIL THEN
  7708. body.SetPriority(ConstantInteger(body.priority));
  7709. END;
  7710. ELSIF body.isSafe THEN
  7711. Error(body.position,"safe flag not in active body");
  7712. ELSIF body.priority # NIL THEN
  7713. Error(body.position,"priority flag not in active body");
  7714. END;
  7715. IF body.code # NIL THEN
  7716. CheckSystemImport(body.position);
  7717. END;
  7718. StatementSequence(body.finally)
  7719. END Body;
  7720. (*** scopes ***)
  7721. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7722. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7723. VAR duplicateSymbol: BOOLEAN;
  7724. BEGIN
  7725. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7726. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7727. Error(symbol.position,"globally defined keyword")
  7728. END;
  7729. scope.EnterSymbol(symbol,duplicateSymbol);
  7730. IF ~allowDuplicate & duplicateSymbol THEN
  7731. Error(symbol.position,"Multiply defined identifier.");
  7732. IF VerboseErrorMessage THEN
  7733. Printout.Info("multiply defined identifier",symbol);
  7734. Printout.Info("in scope",scope);
  7735. END;
  7736. END;
  7737. END Register;
  7738. (**
  7739. implementation: check and resolve an implementation part
  7740. **)
  7741. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7742. move implementation checker to a separate object ? *)
  7743. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7744. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7745. BEGIN
  7746. prevIsRealtime := currentIsRealtime;
  7747. prevIsBodyProcedure := currentIsBodyProcedure;
  7748. prevIsCellNet := currentIsCellNet;
  7749. prevScope := currentScope;
  7750. currentScope := scope;
  7751. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7752. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7753. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7754. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7755. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7756. (*
  7757. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7758. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7759. END;
  7760. *)
  7761. END;
  7762. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7763. (* module body, record bodies are wrapped into an artifical procedure *)
  7764. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7765. Body(scope(SyntaxTree.ProcedureScope).body)
  7766. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7767. Body(scope(SyntaxTree.ProcedureScope).body)
  7768. END;
  7769. END;
  7770. currentScope := prevScope;
  7771. currentIsRealtime := prevIsRealtime;
  7772. currentIsBodyProcedure := prevIsBodyProcedure;
  7773. currentIsCellNet := prevIsCellNet;
  7774. END Implementation;
  7775. (** implementation phase:
  7776. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7777. **)
  7778. PROCEDURE Implementations(x: SyntaxTree.Module);
  7779. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7780. BEGIN
  7781. prevPhase := phase;
  7782. phase := InlinePhase;
  7783. scope := x.firstScope;
  7784. WHILE(scope # NIL) DO
  7785. Implementation(scope);
  7786. scope := scope.nextScope;
  7787. END;
  7788. phase := ImplementationPhase;
  7789. scope := x.firstScope;
  7790. WHILE(scope # NIL) DO
  7791. Implementation(scope);
  7792. scope := scope.nextScope;
  7793. END;
  7794. phase := prevPhase;
  7795. END Implementations;
  7796. (** declaration phase:
  7797. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7798. - import lists (for module scopes)
  7799. - parameter list (for procedure scopes)
  7800. - constant declarations
  7801. - type declarations
  7802. - variable declarations
  7803. - procedure declarations
  7804. preformed in two stages:
  7805. - first all symbols are entered into the symbol table (with uniqueness check),
  7806. - then all symbols are resolved
  7807. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7808. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7809. phases :
  7810. 0 = before procedures
  7811. 1 = procedures and later
  7812. **)
  7813. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7814. VAR
  7815. constant: SyntaxTree.Constant;
  7816. typeDeclaration: SyntaxTree.TypeDeclaration;
  7817. declaredType: SyntaxTree.Type;
  7818. variable: SyntaxTree.Variable;
  7819. procedure: SyntaxTree.Procedure;
  7820. procedureType : SyntaxTree.ProcedureType;
  7821. prevScope: SyntaxTree.Scope;
  7822. parameter: SyntaxTree.Parameter;
  7823. import: SyntaxTree.Import;
  7824. symbol: SyntaxTree.Symbol;
  7825. prevPhase: LONGINT;
  7826. prevError : BOOLEAN;
  7827. i: LONGINT;
  7828. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7829. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7830. BEGIN
  7831. IF type.baseType # NIL THEN
  7832. baseType := type.baseType.resolved;
  7833. IF baseType IS SyntaxTree.PointerType THEN
  7834. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7835. END;
  7836. (*
  7837. IF baseType IS SyntaxTree.CellType THEN
  7838. DeclareCell(baseType(SyntaxTree.CellType));
  7839. END;
  7840. *)
  7841. END;
  7842. parameter := type.firstParameter;
  7843. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7844. (*
  7845. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7846. variable.SetType(parameter.type);
  7847. variable.SetAccess(SyntaxTree.Hidden);
  7848. variable.SetModifiers(parameter.modifiers);
  7849. currentScope.PushVariable(variable);
  7850. *)
  7851. Register(parameter,scope, FALSE);
  7852. parameter := parameter.nextParameter;
  7853. END;
  7854. property := type.firstProperty;
  7855. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7856. (*
  7857. variable := currentScope.FindVariable(property.name);
  7858. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7859. prop := variable(SyntaxTree.Property);
  7860. ELSE (* add, duplicate symbols detection later *)
  7861. prop := SyntaxTree.NewProperty(property.position, property.name);
  7862. currentScope.PushVariable(prop);
  7863. END;
  7864. prop.SetType(property.type);
  7865. prop.SetValue(property.value);
  7866. prop.SetAccess(SyntaxTree.Hidden);
  7867. *)
  7868. Register(property, scope, FALSE);
  7869. property := property.nextProperty;
  7870. END;
  7871. END DeclareCell;
  7872. BEGIN
  7873. prevError := error;
  7874. prevPhase := phase;
  7875. phase := DeclarationPhase;
  7876. prevScope := currentScope;
  7877. currentScope := scope;
  7878. error := FALSE;
  7879. IF 0 IN phases THEN
  7880. (* first enter all symbols in scope *)
  7881. IF scope IS SyntaxTree.ModuleScope THEN
  7882. (* treat imports first for a module scope, , set default context if necessary *)
  7883. import := scope(SyntaxTree.ModuleScope).firstImport;
  7884. WHILE(import # NIL) DO
  7885. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7886. Register(import, currentScope, FALSE);
  7887. import := import.nextImport;
  7888. END;
  7889. import := scope(SyntaxTree.ModuleScope).firstImport;
  7890. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7891. ResolveSymbol(import);
  7892. import := import.nextImport;
  7893. END;
  7894. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7895. (* enter parameters for a procedure scope *)
  7896. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7897. parameter := procedureType.firstParameter;
  7898. WHILE(parameter # NIL) DO
  7899. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7900. END;
  7901. parameter := procedureType.returnParameter;
  7902. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7903. parameter := procedureType.selfParameter;
  7904. IF parameter # NIL THEN
  7905. Register(parameter, currentScope, FALSE);
  7906. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7907. END;
  7908. ELSIF scope IS SyntaxTree.CellScope THEN
  7909. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7910. IF~skipImplementation THEN
  7911. import := scope(SyntaxTree.CellScope).firstImport;
  7912. WHILE(import # NIL) DO
  7913. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7914. Register(import, currentScope, FALSE);
  7915. import := import.nextImport;
  7916. END;
  7917. import := scope(SyntaxTree.CellScope).firstImport;
  7918. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7919. ResolveSymbol(import);
  7920. import := import.nextImport;
  7921. END;
  7922. END;
  7923. END;
  7924. IF error THEN RETURN END;
  7925. IF skipImplementation THEN
  7926. scope.Clear;
  7927. END;
  7928. (* constants *)
  7929. constant := scope.firstConstant;
  7930. WHILE (constant # NIL) DO
  7931. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7932. END;
  7933. (* type declarations *)
  7934. typeDeclaration := scope.firstTypeDeclaration;
  7935. WHILE (typeDeclaration # NIL) DO
  7936. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7937. END;
  7938. (* variables *)
  7939. variable := scope.firstVariable;
  7940. WHILE (variable # NIL) DO
  7941. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7942. END;
  7943. (* procedures *)
  7944. IF scope.procedures # NIL THEN
  7945. FOR i := 0 TO scope.procedures.Length()-1 DO
  7946. procedure := scope.procedures.GetProcedure(i);
  7947. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7948. IF procedureType.selfParameter = NIL THEN
  7949. scope.AddProcedure(procedure);
  7950. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7951. ELSE
  7952. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7953. IF typeDeclaration = NIL THEN
  7954. Error(procedureType.selfParameter.position, "No such type declaration");
  7955. ELSE
  7956. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7957. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7958. declaredType := typeDeclaration.declaredType;
  7959. IF declaredType IS SyntaxTree.PointerType THEN
  7960. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7961. END;
  7962. IF declaredType IS SyntaxTree.RecordType THEN
  7963. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7964. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7965. ELSE
  7966. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7967. END;
  7968. END;
  7969. END;
  7970. END;
  7971. END;
  7972. END;
  7973. (* now process all symbols without any presumption on the order *)
  7974. symbol := scope.firstSymbol;
  7975. WHILE(symbol # NIL) DO
  7976. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7977. IF (symbol IS SyntaxTree.Procedure) THEN
  7978. IF 1 IN phases THEN
  7979. ResolveSymbol(symbol);
  7980. END;
  7981. ELSE
  7982. IF 0 IN phases THEN
  7983. ResolveSymbol(symbol);
  7984. END;
  7985. END;
  7986. END;
  7987. symbol := symbol.nextSymbol;
  7988. END;
  7989. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7990. symbol := scope.firstSymbol;
  7991. WHILE symbol # NIL DO
  7992. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7993. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7994. pointerFixes.Add(symbol, currentScope);
  7995. END;
  7996. IF ~symbol.type.resolved.isRealtime THEN
  7997. Error(symbol.position,"symbol has no realtime type");
  7998. END;
  7999. END;
  8000. symbol := symbol.nextSymbol
  8001. END;
  8002. END;
  8003. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  8004. Error(Basic.invalidPosition,"problems during offset computation in module");
  8005. END;
  8006. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  8007. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  8008. scope.ownerModule.AddScope(scope);
  8009. END;
  8010. phase := prevPhase;
  8011. currentScope := prevScope;
  8012. error := error OR prevError;
  8013. END Declarations;
  8014. (* nopov *)
  8015. (** check if all operators from one module are compatible to the ones in the other module
  8016. - check if there are not multiple operators with the same signature
  8017. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8018. - check for all operators whose signatures are compatible, whether the return types are compatible
  8019. note that:
  8020. - the return type is not considered to be part of the signature
  8021. - two signatures are considered compatible, if all of the operands are compatible
  8022. **)
  8023. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8024. VAR
  8025. thisOperator, thatOperator: SyntaxTree.Operator;
  8026. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8027. thisParameter, thatParameter: SyntaxTree.Parameter;
  8028. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8029. i: LONGINT;
  8030. BEGIN
  8031. currentScope := thisModuleScope;
  8032. hasError := FALSE;
  8033. (* go through all operators in the other module *)
  8034. thatOperator := thatModuleScope.firstOperator;
  8035. WHILE (thatOperator # NIL) & ~hasError DO
  8036. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8037. (* the other operator is accessible *)
  8038. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8039. (* the other operator is not the conversion operator *)
  8040. (* go through all operators in this module *)
  8041. thisOperator := thisModuleScope.firstOperator;
  8042. WHILE (thisOperator # NIL) & ~hasError DO
  8043. IF thisOperator # thatOperator THEN
  8044. (* the operators are not the same *)
  8045. IF thisOperator.name = thatOperator.name THEN
  8046. (* the operators share the same identifier *)
  8047. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8048. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8049. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8050. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8051. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8052. (* both operators have the same paramter count *)
  8053. thisParameter := thisProcedureType.firstParameter;
  8054. thatParameter := thatProcedureType.firstParameter;
  8055. operandsAreEqual := TRUE;
  8056. operandsAreCompatible := TRUE;
  8057. (* go through all parameters *)
  8058. FOR i := 1 TO thisProcedureType.numberParameters DO
  8059. ASSERT(thatParameter # NIL);
  8060. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8061. operandsAreEqual := FALSE;
  8062. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8063. operandsAreCompatible := FALSE
  8064. END
  8065. END;
  8066. thisParameter := thisParameter.nextParameter;
  8067. thatParameter := thatParameter.nextParameter
  8068. END;
  8069. IF operandsAreEqual THEN
  8070. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8071. hasError := TRUE
  8072. ELSIF operandsAreCompatible THEN
  8073. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8074. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8075. hasError := TRUE
  8076. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8077. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8078. hasError := TRUE
  8079. END
  8080. END
  8081. END
  8082. END
  8083. END;
  8084. thisOperator := thisOperator.nextOperator
  8085. END
  8086. END
  8087. END;
  8088. thatOperator := thatOperator.nextOperator
  8089. END
  8090. END CheckInterOperatorConformity;
  8091. (** check module:
  8092. - check module declaration
  8093. - add context, if necessary
  8094. - remove module from import cache, if necessary
  8095. - check declarations
  8096. - resolve all type fixes
  8097. - check implementation (bodies)
  8098. **)
  8099. PROCEDURE Module*(x: SyntaxTree.Module);
  8100. VAR (* nopov *)
  8101. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8102. BEGIN
  8103. prevScope := currentScope;
  8104. prevIsCellNet := currentIsCellNet;
  8105. module := x;
  8106. ASSERT(x # NIL);
  8107. global := system.globalScope[x.case];
  8108. x.moduleScope.SetGlobalScope(global);
  8109. currentScope := global;
  8110. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8111. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8112. RemoveModuleFromCache(importCache,x);
  8113. Declarations(x.moduleScope, FALSE, {0,1});
  8114. FixTypes();
  8115. IF module.isCellNet THEN
  8116. currentIsCellNet := TRUE;
  8117. modifier := x.modifiers;
  8118. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8119. CheckModifiers(modifier, FALSE);
  8120. END;
  8121. (* nopov *)
  8122. IF ~error THEN
  8123. (* check if operators conform to each other within this module *)
  8124. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8125. (* go through all imports *)
  8126. import := x.moduleScope.firstImport;
  8127. WHILE import # NIL DO
  8128. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8129. (* check if all operators in this module conform to the ones of the imported module *)
  8130. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8131. END;
  8132. import := import.nextImport
  8133. END;
  8134. END;
  8135. Implementations(x);
  8136. module := NIL;
  8137. currentIsCellNet := prevIsCellNet;
  8138. currentScope := prevScope;
  8139. END Module;
  8140. END Checker;
  8141. Warnings*=OBJECT (SyntaxTree.Visitor)
  8142. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8143. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8144. BEGIN
  8145. SELF.diagnostics := diagnostics
  8146. END InitWarnings;
  8147. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8148. BEGIN END VisitPortType;
  8149. (** types *)
  8150. PROCEDURE Type(x: SyntaxTree.Type);
  8151. BEGIN x.Accept(SELF)
  8152. END Type;
  8153. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8154. BEGIN END VisitType;
  8155. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8156. BEGIN END VisitBasicType;
  8157. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8158. BEGIN END VisitCharacterType;
  8159. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8160. BEGIN END VisitIntegerType;
  8161. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8162. BEGIN END VisitFloatType;
  8163. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8164. BEGIN END VisitQualifiedType;
  8165. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8166. BEGIN END VisitStringType;
  8167. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8168. BEGIN END VisitEnumerationType;
  8169. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8170. BEGIN END VisitRangeType;
  8171. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8172. BEGIN
  8173. IF ~(SyntaxTree.Warned IN x.state) THEN
  8174. x.SetState(SyntaxTree.Warned);
  8175. Type(x.arrayBase);
  8176. END;
  8177. END VisitArrayType;
  8178. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8179. BEGIN
  8180. IF ~(SyntaxTree.Warned IN x.state) THEN
  8181. x.SetState(SyntaxTree.Warned);
  8182. Type(x.arrayBase);
  8183. END;
  8184. END VisitMathArrayType;
  8185. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8186. BEGIN
  8187. IF ~(SyntaxTree.Warned IN x.state) THEN
  8188. x.SetState(SyntaxTree.Warned);
  8189. Type(x.pointerBase);
  8190. END;
  8191. END VisitPointerType;
  8192. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8193. BEGIN Scope(x.recordScope) END VisitRecordType;
  8194. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8195. BEGIN Scope(x.cellScope) END VisitCellType;
  8196. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8197. BEGIN END VisitProcedureType;
  8198. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8199. VAR msg: ARRAY 256 OF CHAR;
  8200. BEGIN
  8201. Global.GetSymbolName(x,msg);
  8202. Strings.Append(msg," ");
  8203. Strings.Append(msg,text);
  8204. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8205. END Warning;
  8206. (** symbols *)
  8207. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8208. BEGIN
  8209. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8210. IF ~(x IS SyntaxTree.Parameter) THEN
  8211. Warning(x,"never used");
  8212. END;
  8213. END;
  8214. x.Accept(SELF);
  8215. END Symbol;
  8216. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8217. BEGIN END VisitSymbol;
  8218. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8219. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8220. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8221. BEGIN END VisitConstant;
  8222. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8223. BEGIN END VisitVariable;
  8224. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8225. BEGIN END VisitProperty;
  8226. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8227. BEGIN END VisitParameter;
  8228. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8229. BEGIN
  8230. Scope(x.procedureScope)
  8231. END VisitProcedure;
  8232. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8233. BEGIN END VisitOperator;
  8234. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8235. BEGIN END VisitImport;
  8236. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8237. VAR
  8238. symbol: SyntaxTree.Symbol;
  8239. BEGIN
  8240. symbol := scope.firstSymbol;
  8241. WHILE(symbol # NIL) DO
  8242. Symbol(symbol);
  8243. symbol := symbol.nextSymbol;
  8244. END;
  8245. END Scope;
  8246. PROCEDURE Module*(x: SyntaxTree.Module);
  8247. BEGIN
  8248. SELF.module := x;
  8249. Scope(x.moduleScope);
  8250. END Module;
  8251. END Warnings;
  8252. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8253. BEGIN
  8254. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8255. END IsOberonInline;
  8256. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8257. BEGIN
  8258. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8259. END Resolved;
  8260. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8261. VAR i: LONGINT;
  8262. BEGIN
  8263. i := 1;
  8264. WHILE i < x DO
  8265. i := i *2
  8266. END;
  8267. RETURN i=x
  8268. END PowerOf2;
  8269. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8270. BEGIN
  8271. RETURN
  8272. (scope # NIL) &
  8273. (scope IS SyntaxTree.ModuleScope)
  8274. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8275. OR
  8276. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8277. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8278. END IsCellNetScope;
  8279. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8280. BEGIN
  8281. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8282. END IsCellScope;
  8283. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8284. BEGIN
  8285. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8286. RETURN (scope # NIL) & IsCellNetScope(scope)
  8287. END InCellNetScope;
  8288. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8289. BEGIN
  8290. ASSERT(size MOD system.dataUnit = 0);
  8291. RETURN size DIV system.dataUnit
  8292. END ToMemoryUnits;
  8293. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8294. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8295. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8296. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8297. BEGIN
  8298. IF t = NIL THEN
  8299. RETURN TRUE
  8300. ELSE
  8301. t := t.resolved;
  8302. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8303. END;
  8304. END TypeAllowed;
  8305. BEGIN
  8306. type := type.resolved;
  8307. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8308. RETURN FALSE
  8309. ELSE
  8310. procedureType := type(SyntaxTree.ProcedureType);
  8311. numberParameters := procedureType.numberParameters;
  8312. RETURN
  8313. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8314. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8315. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8316. END;
  8317. END GetProcedureAllowed;
  8318. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8319. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8320. VAR import: SyntaxTree.Import;
  8321. BEGIN
  8322. import := importCache.ImportByModuleName(x.name,x.context);
  8323. IF import # NIL THEN
  8324. importCache.RemoveImporters(x.name,x.context);
  8325. END;
  8326. END RemoveModuleFromCache;
  8327. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8328. (* to <- this assignment compatibility *)
  8329. VAR result: BOOLEAN;
  8330. BEGIN
  8331. IF this= NIL THEN result := (to=NIL)
  8332. ELSIF to=NIL THEN result := FALSE
  8333. ELSE
  8334. (*! will be replaced by this:
  8335. ELSE result := this.CompatibleTo(to.resolved);
  8336. *)
  8337. this := this.resolved; to := to.resolved;
  8338. IF to=SyntaxTree.invalidType THEN result := FALSE
  8339. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8340. ELSIF (to = this) OR (to.SameType(this)) THEN
  8341. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8342. ELSIF to IS SyntaxTree.BasicType THEN
  8343. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8344. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8345. result := this.CompatibleTo(to.resolved)
  8346. ELSE
  8347. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8348. END
  8349. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8350. result := to.sizeInBits >= this.sizeInBits;
  8351. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8352. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8353. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8354. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8355. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8356. result := TRUE;
  8357. ELSIF to IS SyntaxTree.AnyType THEN
  8358. 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);
  8359. ELSIF to IS SyntaxTree.ObjectType THEN
  8360. 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 *) ;
  8361. ELSIF to IS SyntaxTree.ByteType THEN
  8362. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8363. ELSIF to IS SyntaxTree.CharacterType THEN
  8364. result := IsCharacterType(this)
  8365. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8366. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8367. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8368. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8369. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8370. result := TRUE;
  8371. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8372. result := TRUE;
  8373. ELSE
  8374. result := FALSE
  8375. END;
  8376. ELSIF to IS SyntaxTree.PointerType THEN
  8377. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8378. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8379. & (~to.isRealtime OR this.isRealtime);
  8380. ELSIF to IS SyntaxTree.ProcedureType THEN
  8381. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8382. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8383. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8384. ELSIF to IS SyntaxTree.RecordType THEN
  8385. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8386. ELSIF to IS SyntaxTree.ArrayType THEN
  8387. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8388. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8389. ELSIF StaticArrayCompatible(to, this) THEN
  8390. result := TRUE
  8391. ELSE
  8392. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8393. END;
  8394. ELSIF to IS SyntaxTree.MathArrayType THEN
  8395. IF this IS SyntaxTree.MathArrayType THEN
  8396. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8397. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8398. result := TRUE;
  8399. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8400. result := TRUE;
  8401. ELSE
  8402. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8403. END;
  8404. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8405. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8406. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8407. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8408. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8409. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8410. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8411. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8412. ELSE
  8413. result := FALSE
  8414. END;
  8415. (* an array-structured object type is compatible to the type of its array structure *)
  8416. ELSIF IsArrayStructuredObjectType(this) THEN
  8417. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8418. ELSE
  8419. result := FALSE;
  8420. END;
  8421. ELSIF to IS SyntaxTree.StringType THEN
  8422. result := FALSE;
  8423. ELSIF to IS SyntaxTree.EnumerationType THEN
  8424. result := IsEnumerationExtension(this,to);
  8425. ELSIF to IS SyntaxTree.PortType THEN
  8426. result := SameType(to, this)
  8427. ELSE
  8428. Printout.Info("CompatibleTo",to);
  8429. HALT(100); (* implement missing type check *)
  8430. END;
  8431. END;
  8432. RETURN result
  8433. END CompatibleTo;
  8434. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8435. VAR actualBase, formalBase: SyntaxTree.Type;
  8436. BEGIN
  8437. IF SameType(formal,actual) THEN
  8438. RETURN TRUE
  8439. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8440. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8441. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8442. RETURN
  8443. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8444. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8445. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8446. & StaticArrayCompatible(formalBase,actualBase)
  8447. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8448. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8449. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8450. RETURN
  8451. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8452. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8453. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8454. & StaticArrayCompatible(formalBase,actualBase)
  8455. ELSE RETURN FALSE
  8456. END;
  8457. END StaticArrayCompatible;
  8458. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8459. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8460. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8461. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8462. BEGIN
  8463. result := SameType(formal,actual);
  8464. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8465. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8466. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8467. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8468. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8469. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8470. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8471. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8472. & TC(formalBase, actualBase);
  8473. END;
  8474. RETURN result
  8475. END TC;
  8476. BEGIN
  8477. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8478. ELSE
  8479. arrayBase := formalType.arrayBase.resolved;
  8480. IF (actualType IS SyntaxTree.StringType) THEN
  8481. result := arrayBase IS SyntaxTree.CharacterType
  8482. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8483. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8484. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8485. result := TC(formalType, actualType);
  8486. ELSE
  8487. result := (arrayBase IS SyntaxTree.ByteType)
  8488. END;
  8489. END;
  8490. RETURN result
  8491. END OpenArrayCompatible;
  8492. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8493. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8494. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8495. BEGIN
  8496. IF actualType IS SyntaxTree.MathArrayType THEN
  8497. actualArray := actualType(SyntaxTree.MathArrayType);
  8498. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8499. (*
  8500. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8501. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8502. *)
  8503. actualBase := ArrayBase(actualType,Infinity);
  8504. formalBase := ArrayBase(formalType,Infinity);
  8505. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8506. ELSE
  8507. (*
  8508. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8509. *)
  8510. formalBase := Resolved(formalType.arrayBase);
  8511. actualBase := Resolved(actualArray.arrayBase);
  8512. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8513. (*
  8514. ARRAY [k] -> ARRAY [n]
  8515. *)
  8516. result := (formalType.staticLength = actualArray.staticLength)
  8517. ELSE
  8518. result := TRUE
  8519. END;
  8520. IF ~result THEN
  8521. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8522. ELSIF actualBase = NIL THEN result := FALSE
  8523. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8524. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8525. ELSE
  8526. result := SameType(formalBase,actualBase)
  8527. END;
  8528. END;
  8529. ELSE
  8530. result := FALSE
  8531. END;
  8532. RETURN result
  8533. END MathArrayCompatible;
  8534. (**
  8535. Math Array Type distance for assignments / parameter passings of the form
  8536. from -> to
  8537. variants:
  8538. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8539. allowed:
  8540. static -> static (& size match)
  8541. static -> open
  8542. static -> tensor
  8543. open -> open
  8544. open -> tensor
  8545. open -> static
  8546. tensor -> tensor
  8547. tensor -> open
  8548. tensor -> static
  8549. **)
  8550. (*! think about the metric here: is form matching more important than element type matching? *)
  8551. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8552. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8553. BEGIN
  8554. fromBase := Resolved(from.arrayBase);
  8555. toBase := Resolved(to.arrayBase);
  8556. i := Infinity;
  8557. IF (from = to) OR (from.SameType(to)) THEN
  8558. i := 0;
  8559. ELSIF (from.form = to.form) THEN
  8560. (* static -> static, open -> open, tensor -> tensor *)
  8561. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8562. IF fromBase = toBase THEN i := 0
  8563. ELSIF toBase = NIL THEN i := 1
  8564. ELSIF toBase.SameType(fromBase) THEN i := 0
  8565. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8566. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8567. ELSE
  8568. i := TypeDistance(system,fromBase, toBase, varpar);
  8569. IF i < Infinity THEN i := i * 5 END;
  8570. END;
  8571. END;
  8572. ELSIF (to.form = SyntaxTree.Static) THEN
  8573. (* forbidden *)
  8574. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8575. (* static -> tensor, open -> tensor, tensor -> open *)
  8576. IF (toBase=fromBase) THEN i := 0;
  8577. ELSIF toBase = NIL THEN i := 1;
  8578. ELSIF toBase.SameType(fromBase) THEN i := 0
  8579. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8580. toBase := ArrayBase(toBase,Infinity);
  8581. IF (fromBase=toBase) THEN i := 0
  8582. ELSIF (toBase = NIL) THEN i:= 1
  8583. ELSIF (fromBase = NIL) THEN i := Infinity;
  8584. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8585. IF i < Infinity THEN i := i * 5 END;
  8586. END;
  8587. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8588. fromBase := ArrayBase(fromBase,Infinity);
  8589. IF (fromBase=toBase) THEN i := 0
  8590. ELSIF (toBase = NIL) THEN i := 1
  8591. ELSIF (fromBase = NIL) THEN i := Infinity;
  8592. ELSIF toBase.SameType(fromBase) THEN i := 0
  8593. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8594. IF i < Infinity THEN i := i * 5 END;
  8595. END;
  8596. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8597. IF i < Infinity THEN i := i * 5 END;
  8598. END;
  8599. IF i # Infinity THEN INC(i,2) END;
  8600. ELSIF (from.form = SyntaxTree.Static) THEN
  8601. (* static -> open *)
  8602. IF (toBase=fromBase) THEN i := 0;
  8603. ELSIF toBase = NIL THEN i := 1
  8604. ELSIF fromBase = NIL THEN i := Infinity
  8605. ELSIF toBase.SameType(fromBase) THEN i := 0
  8606. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8607. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8608. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8609. IF i < Infinity THEN i := i * 5 END;
  8610. END;
  8611. IF i # Infinity THEN INC(i,1) END;
  8612. ELSE HALT(100); (* unknown case *)
  8613. END;
  8614. RETURN i;
  8615. END MathArrayTypeDistance;
  8616. (** compute and return the distance of two array types
  8617. - return the distance of the base types
  8618. **)
  8619. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8620. VAR i: LONGINT;
  8621. BEGIN
  8622. i := Infinity;
  8623. IF from = to THEN
  8624. i := 0
  8625. ELSE
  8626. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8627. (*
  8628. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8629. i := TypeDistance(from.base, to.base);
  8630. IF i >= 0 THEN INC(i) END
  8631. ELSIF (from.mode = open) & (to.mode = open) THEN
  8632. i := TypeDistance(from.base, to.base);
  8633. *)
  8634. END;
  8635. RETURN i
  8636. END ArrayTypeDistance;
  8637. (** compute the signature distance of a procedure and an actual parameter list
  8638. - if any of the parameters are not compatible, the result is infinite
  8639. - add up and return the distance over all parameters
  8640. **)
  8641. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8642. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8643. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8644. BEGIN
  8645. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8646. result := Infinity
  8647. ELSE
  8648. formalParameter := procedureType.firstParameter;
  8649. i := 0;
  8650. result := 0;
  8651. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8652. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8653. actualParameter := actualParameters.GetExpression(i);
  8654. ASSERT(formalParameter.type # NIL);
  8655. IF (actualParameter.type = NIL) THEN distance := Infinity
  8656. ELSE
  8657. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8658. END;
  8659. IF distance = Infinity THEN
  8660. result := Infinity;
  8661. ELSE
  8662. to := formalParameter.type.resolved;
  8663. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8664. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8665. (* already handled varpar *)
  8666. (*
  8667. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8668. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8669. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8670. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8671. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8672. END;
  8673. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8674. result := Infinity
  8675. END;
  8676. *)
  8677. INC(result, distance);
  8678. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8679. INC(result, distance);
  8680. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8681. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8682. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8683. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8684. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8685. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8686. END;
  8687. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8688. result := Infinity
  8689. END;
  8690. ELSE
  8691. result := Infinity
  8692. END;
  8693. ELSE
  8694. INC(result,distance);
  8695. END;
  8696. END;
  8697. (*
  8698. Printout.Info("actual=", actualParameter);
  8699. Printout.Info("formal=", formalParameter);
  8700. TRACE(result);
  8701. *)
  8702. formalParameter := formalParameter.nextParameter; INC(i);
  8703. END;
  8704. END;
  8705. ASSERT(result >= 0);
  8706. RETURN result
  8707. END Distance;
  8708. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8709. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8710. BEGIN
  8711. IF right.numberParameters # (procedureType.numberParameters) THEN
  8712. result := Infinity
  8713. ELSE
  8714. formalParameter := procedureType.firstParameter;
  8715. rightParameter := right.firstParameter;
  8716. i := 0;
  8717. result := 0;
  8718. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8719. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8720. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8721. IF distance = Infinity THEN
  8722. result := Infinity;
  8723. ELSE
  8724. INC(result,distance);
  8725. END;
  8726. formalParameter := formalParameter.nextParameter;
  8727. rightParameter := rightParameter.nextParameter;
  8728. END;
  8729. END;
  8730. ASSERT(result >= 0);
  8731. RETURN result
  8732. END ProcedureTypeDistance;
  8733. (** compute and return the distance between two types, used for computation of signature distance
  8734. from -> to
  8735. **)
  8736. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8737. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8738. BEGIN
  8739. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8740. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8741. END;
  8742. i := Infinity;
  8743. IF from = to THEN
  8744. i := 0
  8745. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8746. ELSIF to.SameType(from) THEN
  8747. i := 0;
  8748. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8749. i := Infinity;
  8750. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8751. i := 10;
  8752. ELSIF (from IS SyntaxTree.StringType) THEN
  8753. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8754. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8755. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8756. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8757. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8758. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8759. i := 1
  8760. ELSIF (from IS SyntaxTree.NilType) THEN
  8761. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8762. (*
  8763. ELSIF (from = NoType) THEN
  8764. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8765. *)
  8766. ELSIF (from IS SyntaxTree.BasicType) THEN
  8767. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8768. IF varpar & (i # 0) THEN i := Infinity END;
  8769. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8770. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8771. ELSIF (from IS SyntaxTree.RecordType) THEN
  8772. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8773. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8774. IF to IS SyntaxTree.MathArrayType THEN
  8775. (*
  8776. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8777. i := Infinity;
  8778. ELSE
  8779. *)
  8780. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8781. (*
  8782. END;
  8783. *)
  8784. END
  8785. ELSIF (from IS SyntaxTree.PointerType) THEN
  8786. ptr := from(SyntaxTree.PointerType);
  8787. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8788. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8789. (* ELSE i := TypeDistance(ptr.base, to); *)
  8790. END
  8791. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8792. IF (to IS SyntaxTree.ProcedureType) THEN
  8793. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8794. END;
  8795. ELSIF (from IS SyntaxTree.PortType) THEN
  8796. IF (to IS SyntaxTree.PortType) THEN
  8797. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8798. i := 0;
  8799. END;
  8800. END;
  8801. (*no procedure test, procedure must be the same*)
  8802. END;
  8803. RETURN i
  8804. END TypeDistance;
  8805. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8806. BEGIN
  8807. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8808. END IsIntegerType;
  8809. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8810. BEGIN
  8811. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8812. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8813. OR IsPointerType(type)
  8814. )
  8815. END IsAddressType;
  8816. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8817. BEGIN
  8818. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8819. END IsSizeType;
  8820. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8821. BEGIN
  8822. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8823. END IsSignedIntegerType;
  8824. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8825. BEGIN
  8826. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8827. END IsUnsignedIntegerType;
  8828. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8829. VAR result: BOOLEAN;
  8830. BEGIN
  8831. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8832. value := x.resolved(SyntaxTree.IntegerValue).value;
  8833. result := TRUE
  8834. ELSE
  8835. result := FALSE
  8836. END;
  8837. RETURN result
  8838. END IsIntegerValue;
  8839. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8840. VAR result: BOOLEAN;
  8841. BEGIN
  8842. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8843. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8844. result := TRUE
  8845. ELSE
  8846. result := FALSE
  8847. END;
  8848. RETURN result
  8849. END IsEnumerationValue;
  8850. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8851. VAR result: BOOLEAN;
  8852. BEGIN
  8853. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8854. value := x.resolved(SyntaxTree.RealValue).value;
  8855. result := TRUE
  8856. ELSE
  8857. result := FALSE
  8858. END;
  8859. RETURN result
  8860. END IsRealValue;
  8861. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8862. VAR result: BOOLEAN;
  8863. BEGIN
  8864. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8865. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8866. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8867. result := TRUE
  8868. ELSE
  8869. result := FALSE
  8870. END;
  8871. RETURN result
  8872. END IsComplexValue;
  8873. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8874. VAR result: BOOLEAN;
  8875. BEGIN
  8876. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8877. value := x.resolved(SyntaxTree.CharacterValue).value;
  8878. result := TRUE
  8879. ELSE
  8880. result := FALSE
  8881. END;
  8882. RETURN result
  8883. END IsCharacterValue;
  8884. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8885. VAR result: BOOLEAN;
  8886. BEGIN
  8887. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8888. value := x.resolved(SyntaxTree.BooleanValue).value;
  8889. result := TRUE
  8890. ELSE
  8891. result := FALSE
  8892. END;
  8893. RETURN result
  8894. END IsBooleanValue;
  8895. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8896. VAR result: BOOLEAN;
  8897. BEGIN
  8898. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8899. value := x.resolved(SyntaxTree.SetValue).value;
  8900. result := TRUE
  8901. ELSE
  8902. result := FALSE
  8903. END;
  8904. RETURN result
  8905. END IsSetValue;
  8906. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8907. VAR result: BOOLEAN;
  8908. BEGIN
  8909. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8910. value := x.resolved(SyntaxTree.StringValue).value;
  8911. result := TRUE
  8912. ELSE
  8913. result := FALSE
  8914. END;
  8915. RETURN result
  8916. END IsStringValue;
  8917. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8918. BEGIN
  8919. x := x.resolved;
  8920. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8921. END Indexable;
  8922. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8923. BEGIN
  8924. RETURN t1.SameType(t2.resolved);
  8925. END SameType;
  8926. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8927. BEGIN
  8928. IF t IS SyntaxTree.MathArrayType THEN
  8929. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8930. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8931. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8932. DEC(max);
  8933. END;
  8934. ELSIF t IS SyntaxTree.ArrayType THEN
  8935. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8936. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8937. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8938. END;
  8939. END;
  8940. RETURN t;
  8941. END ArrayBase;
  8942. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8943. BEGIN
  8944. type := type.resolved;
  8945. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8946. base := type(SyntaxTree.ArrayType).arrayBase;
  8947. RETURN TRUE;
  8948. END;
  8949. RETURN FALSE;
  8950. END IsOpenArray;
  8951. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8952. BEGIN
  8953. type := type.resolved;
  8954. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8955. base := type(SyntaxTree.ArrayType).arrayBase;
  8956. dim := type(SyntaxTree.ArrayType).staticLength;
  8957. RETURN TRUE
  8958. ELSE
  8959. RETURN FALSE
  8960. END;
  8961. END IsStaticArray;
  8962. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8963. BEGIN
  8964. type := type.resolved;
  8965. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8966. base := type(SyntaxTree.ArrayType).arrayBase;
  8967. RETURN TRUE
  8968. ELSE
  8969. RETURN FALSE
  8970. END;
  8971. END IsDynamicArray;
  8972. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8973. VAR i: LONGINT;
  8974. BEGIN
  8975. i := 0;
  8976. t := t.resolved;
  8977. IF t IS SyntaxTree.MathArrayType THEN
  8978. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8979. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8980. END;
  8981. ELSIF t IS SyntaxTree.ArrayType THEN
  8982. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8983. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8984. END;
  8985. END;
  8986. RETURN i
  8987. END Dimension;
  8988. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8989. BEGIN
  8990. RETURN expression.assignable;
  8991. END IsVariable;
  8992. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8993. BEGIN
  8994. IF (symbol IS SyntaxTree.Parameter) THEN
  8995. WITH symbol: SyntaxTree.Parameter DO
  8996. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8997. END;
  8998. ELSE
  8999. RETURN FALSE
  9000. END;
  9001. END IsVariableParameter;
  9002. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  9003. VAR result: BOOLEAN;
  9004. BEGIN
  9005. IF type = NIL THEN result := FALSE
  9006. ELSE
  9007. type := type.resolved;
  9008. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  9009. END;
  9010. RETURN result
  9011. END IsPointerType;
  9012. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  9013. VAR result: BOOLEAN;
  9014. BEGIN
  9015. IF type = NIL THEN result := FALSE
  9016. ELSE
  9017. type := type.resolved;
  9018. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  9019. END;
  9020. RETURN result
  9021. END IsUnsafePointer;
  9022. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9023. BEGIN
  9024. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9025. END IsDisposable;
  9026. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9027. VAR result: BOOLEAN;
  9028. BEGIN
  9029. IF type = NIL THEN result := FALSE
  9030. ELSE
  9031. type := type.resolved;
  9032. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9033. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9034. result := result OR (type IS SyntaxTree.ObjectType);
  9035. END;
  9036. RETURN result
  9037. END IsPointerToRecord;
  9038. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9039. VAR result: BOOLEAN;
  9040. BEGIN
  9041. IF type = NIL THEN result := FALSE
  9042. ELSE
  9043. type := type.resolved;
  9044. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9045. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9046. ;
  9047. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9048. result := result OR (type IS SyntaxTree.ObjectType);
  9049. END;
  9050. RETURN result
  9051. END IsPointerToObject;
  9052. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9053. BEGIN
  9054. IF type # NIL THEN
  9055. RETURN type.resolved.hasPointers
  9056. ELSE
  9057. RETURN FALSE
  9058. END;
  9059. END ContainsPointer;
  9060. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9061. BEGIN
  9062. IF type = NIL THEN RETURN FALSE END;
  9063. type := type.resolved;
  9064. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9065. END IsStringType;
  9066. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9067. BEGIN
  9068. IF type = NIL THEN RETURN FALSE END;
  9069. type := type.resolved;
  9070. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9071. END IsCharacterType;
  9072. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9073. BEGIN
  9074. IF type = NIL THEN RETURN FALSE END;
  9075. type := type.resolved;
  9076. RETURN (type IS SyntaxTree.EnumerationType)
  9077. END IsEnumerationType;
  9078. (** cf. section "Type extension (base type)" in the language report **)
  9079. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9080. VAR result: BOOLEAN;
  9081. BEGIN
  9082. ASSERT(base # NIL); ASSERT(extension # NIL);
  9083. base := base.resolved; extension := extension.resolved;
  9084. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9085. result := TRUE;
  9086. ELSE
  9087. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9088. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9089. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9090. END;
  9091. WHILE (extension # NIL) & (extension # base) DO
  9092. IF extension IS SyntaxTree.RecordType THEN
  9093. extension := extension(SyntaxTree.RecordType).baseType;
  9094. IF (extension # NIL) THEN extension := extension.resolved END;
  9095. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9096. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9097. END;
  9098. ELSE extension := NIL;
  9099. END;
  9100. END;
  9101. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9102. END;
  9103. RETURN result
  9104. END IsTypeExtension;
  9105. (** check if base is the base enumeration type of extension **)
  9106. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9107. BEGIN
  9108. base := base.resolved; extension := extension.resolved;
  9109. WHILE (extension # NIL) & (extension # base) DO
  9110. IF extension IS SyntaxTree.EnumerationType THEN
  9111. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9112. IF extension # NIL THEN extension := extension.resolved END;
  9113. ELSE
  9114. extension := NIL
  9115. END;
  9116. END;
  9117. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9118. END IsEnumerationExtension;
  9119. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9120. BEGIN
  9121. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9122. RETURN TRUE
  9123. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9124. RETURN TRUE
  9125. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9126. RETURN TRUE
  9127. ELSE
  9128. RETURN FALSE
  9129. END
  9130. END IsCallable;
  9131. (** compute and return the distance of two record types
  9132. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9133. **)
  9134. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9135. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9136. BEGIN
  9137. i := 0;
  9138. WHILE (from # NIL) & (from # to) DO
  9139. baseType := from.baseType;
  9140. IF (baseType # NIL) THEN
  9141. baseType := baseType.resolved;
  9142. IF baseType IS SyntaxTree.PointerType THEN
  9143. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9144. END;
  9145. IF baseType IS SyntaxTree.RecordType THEN
  9146. from := baseType(SyntaxTree.RecordType);
  9147. ELSE
  9148. from := NIL;
  9149. END;
  9150. ELSE
  9151. from := NIL
  9152. END;
  9153. INC(i)
  9154. END;
  9155. IF from = NIL THEN i := Infinity END;
  9156. RETURN i
  9157. END RecordTypeDistance;
  9158. (** compute and return the distance of two pointer types **)
  9159. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9160. BEGIN
  9161. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9162. RETURN Infinity;
  9163. ELSE
  9164. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9165. END;
  9166. END PointerTypeDistance;
  9167. (** check if expression contains a symbol designator pointing to a type declaration.
  9168. - if so then enter type declaration into typeDeclaration and return true else return false
  9169. **)
  9170. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9171. VAR result: BOOLEAN;
  9172. BEGIN
  9173. result := FALSE;
  9174. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9175. result := TRUE;
  9176. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9177. END;
  9178. RETURN result
  9179. END IsTypeDesignator;
  9180. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9181. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9182. VAR result: BOOLEAN;
  9183. BEGIN
  9184. type := type.resolved;
  9185. IF type IS SyntaxTree.PointerType THEN
  9186. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9187. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9188. result := TRUE
  9189. ELSE
  9190. result := type IS SyntaxTree.RecordType
  9191. END;
  9192. RETURN result
  9193. END IsExtensibleType;
  9194. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9195. BEGIN
  9196. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9197. (d IS SyntaxTree.SymbolDesignator) &
  9198. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9199. OR
  9200. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9201. END IsUnextensibleRecord;
  9202. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9203. BEGIN
  9204. IF IsUnextensibleRecord(d) THEN
  9205. RETURN FALSE
  9206. ELSE RETURN IsExtensibleType(d.type.resolved)
  9207. END;
  9208. END IsExtensibleDesignator;
  9209. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9210. BEGIN
  9211. type := type.resolved;
  9212. IF (type IS SyntaxTree.PointerType) THEN
  9213. RETURN TRUE
  9214. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9215. RETURN TRUE
  9216. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9217. RETURN TRUE
  9218. ELSIF (type IS SyntaxTree.BasicType) THEN
  9219. RETURN TRUE
  9220. END;
  9221. RETURN FALSE
  9222. END IsBasicType;
  9223. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9224. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9225. BEGIN
  9226. baseType := record.baseType;
  9227. IF (baseType # NIL) THEN
  9228. baseType := baseType.resolved;
  9229. IF (baseType IS SyntaxTree.PointerType) THEN
  9230. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9231. END;
  9232. END;
  9233. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9234. recordType := baseType(SyntaxTree.RecordType);
  9235. ELSE
  9236. recordType := NIL;
  9237. END;
  9238. RETURN recordType
  9239. END RecordBase;
  9240. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9241. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9242. BEGIN
  9243. baseRecord := RecordBase(scope.ownerRecord);
  9244. IF baseRecord = NIL THEN RETURN NIL END;
  9245. scope := baseRecord.recordScope;
  9246. procedureType := procedure.type.resolved;
  9247. IF procedure IS SyntaxTree.Operator THEN
  9248. operator := scope.firstOperator;
  9249. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9250. (*
  9251. Printout.Info("not same ",procedureType);
  9252. Printout.Info("with ",operator.type);
  9253. *)
  9254. operator := operator.nextOperator;
  9255. END;
  9256. super := operator;
  9257. ELSE
  9258. super := scope.firstProcedure;
  9259. WHILE (super # NIL) & (super.name # procedure.name) DO
  9260. super := super.nextProcedure;
  9261. END;
  9262. END;
  9263. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9264. RETURN super
  9265. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9266. RETURN super
  9267. ELSE
  9268. RETURN FindSuperProcedure(scope,procedure);
  9269. END;
  9270. END FindSuperProcedure;
  9271. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9272. VAR procedure: SyntaxTree.Procedure;
  9273. BEGIN
  9274. procedure := record.recordScope.constructor;
  9275. IF procedure = NIL THEN
  9276. record := RecordBase(record);
  9277. IF record # NIL THEN
  9278. procedure := GetConstructor(record)
  9279. END;
  9280. END;
  9281. RETURN procedure;
  9282. END GetConstructor;
  9283. (* enter a case into a list of cases in a sorted way and check for collision *)
  9284. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9285. VAR prev,this,new: SyntaxTree.CaseConstant;
  9286. BEGIN
  9287. this := root;
  9288. prev := NIL;
  9289. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9290. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9291. RETURN FALSE
  9292. ELSE
  9293. IF (this # NIL) & (this.min = max+1) THEN
  9294. this.min := min
  9295. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9296. prev.max := min
  9297. ELSE
  9298. NEW(new); new.min := min; new.max := max;
  9299. new.next := this;
  9300. IF prev = NIL THEN
  9301. root := new;
  9302. ELSE
  9303. prev.next := new
  9304. END
  9305. END;
  9306. RETURN TRUE
  9307. END;
  9308. END EnterCase;
  9309. (** generate and return a new checker object, errors are entered into diagnostics **)
  9310. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9311. VAR checker: Checker;
  9312. BEGIN
  9313. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9314. RETURN checker
  9315. END NewChecker;
  9316. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9317. VAR warnings: Warnings;
  9318. BEGIN
  9319. NEW(warnings, diagnostics); RETURN warnings;
  9320. END NewWarnings;
  9321. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9322. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9323. END IsRangeType;
  9324. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9325. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9326. END IsMathArrayType;
  9327. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9328. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9329. END IsArrayType;
  9330. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9331. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9332. END IsComplexType;
  9333. (** if a type is an array-structured object type *)
  9334. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9335. VAR recordType: SyntaxTree.RecordType;
  9336. BEGIN
  9337. IF type = NIL THEN
  9338. RETURN FALSE
  9339. ELSE
  9340. type := type.resolved;
  9341. IF type IS SyntaxTree.PointerType THEN
  9342. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9343. IF type IS SyntaxTree.RecordType THEN
  9344. recordType := type(SyntaxTree.RecordType);
  9345. RETURN recordType.isObject & recordType.HasArrayStructure()
  9346. ELSE
  9347. RETURN FALSE
  9348. END
  9349. ELSE
  9350. RETURN FALSE
  9351. END
  9352. END
  9353. END IsArrayStructuredObjectType;
  9354. (** the math array structure of a type
  9355. - for math arrays: the array itself
  9356. - for pointers: the math array structure of the pointer base
  9357. - for array-structured object types: the underlying structure
  9358. - for non-math arrays and all other types: NIL
  9359. **)
  9360. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9361. VAR
  9362. result: SyntaxTree.MathArrayType;
  9363. BEGIN
  9364. IF type = NIL THEN
  9365. result := NIL
  9366. ELSE
  9367. type := type.resolved;
  9368. IF type IS SyntaxTree.PointerType THEN
  9369. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9370. END;
  9371. IF type IS SyntaxTree.MathArrayType THEN
  9372. result := type(SyntaxTree.MathArrayType)
  9373. ELSIF type IS SyntaxTree.RecordType THEN
  9374. result := type(SyntaxTree.RecordType).arrayStructure
  9375. ELSE
  9376. result := NIL
  9377. END
  9378. END;
  9379. RETURN result
  9380. END MathArrayStructureOfType;
  9381. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9382. VAR
  9383. result: BOOLEAN;
  9384. rangeExpression: SyntaxTree.RangeExpression;
  9385. BEGIN
  9386. IF x IS SyntaxTree.RangeExpression THEN
  9387. rangeExpression := x(SyntaxTree.RangeExpression);
  9388. result := TRUE;
  9389. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9390. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9391. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9392. ELSE
  9393. result := FALSE
  9394. END;
  9395. RETURN result
  9396. END IsStaticRange;
  9397. (** whether a type is a math array of tensor form **)
  9398. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9399. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9400. END IsTensor;
  9401. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9402. BEGIN
  9403. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9404. length := type(SyntaxTree.MathArrayType).staticLength;
  9405. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9406. RETURN TRUE
  9407. ELSE
  9408. RETURN FALSE
  9409. END;
  9410. END IsStaticMathArray;
  9411. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9412. BEGIN
  9413. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9414. END SymbolHasAddress;
  9415. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9416. BEGIN
  9417. RETURN
  9418. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9419. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9420. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9421. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9422. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9423. ;
  9424. END HasAddress;
  9425. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9426. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9427. BEGIN
  9428. IF (e IS SyntaxTree.Designator) THEN
  9429. d := e(SyntaxTree.Designator);
  9430. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9431. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9432. e := d.left;
  9433. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9434. END;
  9435. IF d # NIL THEN
  9436. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9437. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9438. END;
  9439. END;
  9440. RETURN FALSE;
  9441. END IsLocalVariable;
  9442. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9443. BEGIN
  9444. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9445. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9446. ELSE
  9447. RETURN TRUE
  9448. END;
  9449. END IsStaticProcedure;
  9450. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9451. CONST OptimizeMethodTable = FALSE;
  9452. BEGIN
  9453. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9454. END InMethodTable;
  9455. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9456. BEGIN
  9457. IF type = NIL THEN RETURN FALSE
  9458. ELSE
  9459. type := type.resolved;
  9460. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9461. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9462. END
  9463. END ReturnedAsParameter;
  9464. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9465. BEGIN
  9466. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9467. END StructuredReturnType;
  9468. END FoxSemanticChecker.
  9469. SystemTools.FreeDownTo FoxSemanticChecker ~