FoxSemanticChecker.Mod 405 KB

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