FoxSemanticChecker.Mod 404 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. inConversion: LONGINT;
  78. (* temporary variables for the visitors
  79. they replace variables on a stack during use of the visitor pattern and may only be
  80. - set in AcceptXXX procedures
  81. - set and read in ResolveXXX procedures
  82. *)
  83. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  84. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  85. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  86. currentScope-: SyntaxTree.Scope;
  87. currentIsRealtime: BOOLEAN;
  88. currentIsUnreachable: BOOLEAN;
  89. currentIsCellNet: BOOLEAN;
  90. currentIsBodyProcedure: BOOLEAN;
  91. currentIsExclusive: BOOLEAN;
  92. global: SyntaxTree.ModuleScope;
  93. withEntries: WithEntry;
  94. activeCellsStatement: BOOLEAN;
  95. replacements*: Replacement;
  96. cellsAreObjects: BOOLEAN;
  97. variableAccessed: BOOLEAN;
  98. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  99. BEGIN
  100. SELF.diagnostics := diagnostics;
  101. SELF.useDarwinCCalls := useDarwinCCalls;
  102. SELF.cooperative := cooperative;
  103. SELF.system := system;
  104. SELF.symbolFileFormat := symbolFileFormat;
  105. error := FALSE;
  106. NEW(typeFixes);
  107. NEW(pointerFixes);
  108. resolvedType := NIL;
  109. resolvedExpression := NIL;
  110. resolvedStatement := NIL;
  111. currentScope := NIL;
  112. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  113. SELF.importCache := importCache;
  114. arrayBaseImported := FALSE;
  115. complexNumbersImported := FALSE;
  116. SELF.VerboseErrorMessage := verboseErrorMessage;
  117. global := NIL;
  118. phase := UndefinedPhase;
  119. currentIsRealtime := FALSE;
  120. currentIsUnreachable := FALSE;
  121. currentIsCellNet := FALSE;
  122. currentIsBodyProcedure := FALSE;
  123. currentIsExclusive := FALSE;
  124. withEntries := NIL;
  125. SELF.cellsAreObjects := system.cellsAreObjects;
  126. COPY(backend, backendName);
  127. inConversion := 0;
  128. END InitChecker;
  129. (** report error **)
  130. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  131. VAR errModule: SyntaxTree.Module;
  132. BEGIN
  133. ASSERT(currentScope # NIL);
  134. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  135. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  136. error := TRUE;
  137. END Error;
  138. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  139. VAR errModule: SyntaxTree.Module;
  140. BEGIN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  143. END Warning;
  144. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  145. VAR errorMessage: ARRAY 256 OF CHAR;
  146. BEGIN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  149. error := TRUE;
  150. END ErrorSS;
  151. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  152. VAR msg, msg2: ARRAY 256 OF CHAR;
  153. BEGIN
  154. COPY(msg1, msg);
  155. Strings.Append(msg, " = ");
  156. Basic.GetString(s, msg2);
  157. Strings.Append(msg, msg2);
  158. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  159. END InfoSS;
  160. (*** symbol lookup ***)
  161. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  162. **)
  163. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  164. VAR
  165. scope,baseScope: SyntaxTree.Scope;
  166. symbol, s: SyntaxTree.Symbol;
  167. ownerRecord,base: SyntaxTree.RecordType;
  168. BEGIN
  169. scope := inScope;
  170. symbol := NIL;
  171. WHILE (scope # NIL) & (symbol = NIL) DO
  172. symbol := scope.FindSymbol(name);
  173. s := NIL;
  174. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  175. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  177. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  178. END;
  179. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  180. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  181. ELSE
  182. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  183. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  184. symbol.MarkUsed;
  185. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  186. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  187. base := RecordBase(ownerRecord);
  188. IF (base # NIL) THEN
  189. baseScope := base.recordScope;
  190. symbol := Find(baseScope,name,FALSE);
  191. ELSE
  192. symbol := NIL;
  193. END;
  194. ELSE
  195. symbol := NIL;
  196. END;
  197. END;
  198. END;
  199. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  200. END;
  201. IF (symbol # NIL) THEN
  202. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  203. ASSERT(phase = DeclarationPhase);
  204. ResolveSymbol(symbol)
  205. END;
  206. symbol.MarkUsed;
  207. END;
  208. RETURN symbol
  209. END Find;
  210. (*** types ***)
  211. (** find type declaration with name qualifiedIdentifier and return resolved type
  212. - check qualified identifier prefix, set scope to module scope if appropriate
  213. - check suffix in scope
  214. **)
  215. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  216. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  217. BEGIN
  218. result := NIL;
  219. prevScope := currentScope;
  220. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  221. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  222. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  223. IF symbol(SyntaxTree.Import).module = NIL THEN
  224. Error(qualifiedIdentifier.position,"module not loaded");
  225. result := SyntaxTree.invalidType;
  226. symbol := NIL;
  227. ELSE
  228. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  229. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  230. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  231. IF VerboseErrorMessage THEN
  232. Printout.Info("scope", currentScope);
  233. Printout.Info("symbol", symbol);
  234. END;
  235. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  236. END;
  237. END;
  238. ELSE
  239. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  240. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  241. symbol := NIL;
  242. END;
  243. ELSE
  244. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  245. IF symbol = NIL THEN
  246. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  247. IF VerboseErrorMessage THEN
  248. Printout.Info("Qualident",qualifiedIdentifier);
  249. Printout.Info("in scope",currentScope) ;
  250. END;
  251. END;
  252. END;
  253. IF symbol = NIL THEN (* error already handled *)
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  257. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  258. typeDeclaration := NIL;
  259. result := SyntaxTree.invalidType;
  260. ELSE
  261. currentScope := symbol.scope;
  262. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  263. result := ResolveType(typeDeclaration.declaredType);
  264. symbol.MarkUsed;
  265. ASSERT(result # NIL);
  266. END;
  267. currentScope := prevScope;
  268. RETURN result
  269. END ResolveNamedType;
  270. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  271. If node is currently being resolved then emit a cyclic definition error.
  272. Return TRUE only if node is fully resolved.
  273. **)
  274. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  275. VAR result: BOOLEAN;
  276. BEGIN
  277. IF SyntaxTree.Resolved IN x.state THEN
  278. result := FALSE
  279. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  280. Error(x.position,"cyclic definition");
  281. result := FALSE;
  282. ELSE
  283. result := TRUE;
  284. x.SetState(SyntaxTree.BeingResolved)
  285. END;
  286. RETURN result
  287. END TypeNeedsResolution;
  288. (** Return invalid type if x is currently being resolved, return x otherwise**)
  289. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  290. BEGIN
  291. IF SyntaxTree.Resolved IN x.state THEN
  292. RETURN x
  293. ELSE
  294. RETURN SyntaxTree.invalidType
  295. END;
  296. END ResolvedType;
  297. PROCEDURE VisitType*(x: SyntaxTree.Type);
  298. BEGIN
  299. ASSERT(x = SyntaxTree.invalidType);
  300. END VisitType;
  301. (** resolve basic type **)
  302. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  303. BEGIN
  304. IF TypeNeedsResolution(x) THEN
  305. x.SetState(SyntaxTree.Resolved);
  306. END;
  307. resolvedType := ResolvedType(x)
  308. END VisitBasicType;
  309. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  310. BEGIN
  311. VisitBasicType(x);
  312. END VisitByteType;
  313. (** resolve character type **)
  314. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitCharacterType;
  318. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitBooleanType;
  322. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitSetType;
  326. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitAddressType;
  330. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitSizeType;
  334. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitAnyType;
  338. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitObjectType;
  342. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  343. BEGIN
  344. VisitBasicType(x);
  345. END VisitNilType;
  346. (** resolve integer type **)
  347. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitIntegerType;
  351. (** resolve real type **)
  352. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitFloatType;
  356. (** resolve complex type **)
  357. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitComplexType;
  361. (**
  362. resolve string type: nothing to be done
  363. **)
  364. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  365. BEGIN
  366. IF TypeNeedsResolution(x) THEN
  367. x.SetState(SyntaxTree.Resolved);
  368. END;
  369. resolvedType := ResolvedType(x)
  370. END VisitStringType;
  371. (**
  372. check enumeration scope: enter symbols and check for duplicate names
  373. **)
  374. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  375. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  376. BEGIN
  377. prevScope := currentScope;
  378. currentScope := x;
  379. e := x.firstConstant;
  380. nextHighest := highest;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  408. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: Basic.Integer;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position, "base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  479. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  480. ELSE
  481. x.SetArrayBase(pointerType);
  482. END;
  483. END;
  484. IF x.length # NIL THEN
  485. variableAccessed := FALSE;
  486. e := ResolveExpression(x.length);
  487. IF (e.resolved = NIL) THEN
  488. IF variableAccessed THEN
  489. Error(e.position, "forbidden variable access");
  490. END;
  491. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. x.SetState(SyntaxTree.Resolved);
  572. END;
  573. resolvedType := ResolvedType(x);
  574. END VisitMathArrayType;
  575. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  576. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  577. (1) Rec = RECORD ... END; Ptr <---> Rec
  578. Ptr = POINTER TO Rec; ^ |
  579. | |
  580. TypeDesc TypeDesc
  581. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  582. ^ /
  583. | /
  584. TypeDesc <-- /
  585. *)
  586. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  587. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  588. BEGIN
  589. Strings.IntToStr(x.position.start,number);
  590. COPY(prefix,name);
  591. Strings.Append(name,"@");
  592. Strings.Append(name,number);
  593. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  594. typeDeclaration.SetDeclaredType(x);
  595. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  596. x.SetTypeDeclaration(typeDeclaration);
  597. currentScope.AddTypeDeclaration(typeDeclaration);
  598. typeDeclaration.SetScope(currentScope);
  599. END AnonymousTypeDeclaration;
  600. (**
  601. deferred pointer type resolving
  602. - resolve base type
  603. - check that base type is a record or array type
  604. - if error then set base type to invalid type
  605. **)
  606. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  607. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  608. BEGIN
  609. ASSERT(type.pointerBase # NIL);
  610. position := type.pointerBase.position;
  611. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  612. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  613. (* not for pointers, a type is needed for the records only
  614. IF type.typeDeclaration = NIL THEN
  615. AnonymousTypeDeclaration(type);
  616. END;
  617. *)
  618. END;
  619. resolved := ResolveType(type.pointerBase);
  620. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  621. type.SetPointerBase(resolved);
  622. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  623. recordType := resolved.resolved(SyntaxTree.RecordType);
  624. IF recordType.isObject & (recordType.baseType # NIL) THEN
  625. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  626. Error(position, "base type of object must be a realtime object");
  627. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  628. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  629. END;
  630. END;
  631. END;
  632. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  633. Error(position, "realtime object contains references to non-realtime objects");
  634. END;
  635. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  636. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  637. Error(position, "forbidden unsafe at static array");
  638. ELS
  639. *)
  640. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  641. Error(position, "forbidden unsafe at multidimensional array");
  642. END;
  643. END;
  644. ELSE
  645. Error(position, "forbidden pointer base type");
  646. type.SetPointerBase(SyntaxTree.invalidType)
  647. END
  648. END FixPointerType;
  649. (**
  650. resolve pointer type
  651. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  652. **)
  653. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  654. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  655. modifiers: SyntaxTree.Modifier; position: Position;
  656. BEGIN
  657. IF TypeNeedsResolution(x) THEN
  658. modifiers := x.modifiers;
  659. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  660. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  661. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  662. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  663. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  664. (* inheritance cycle check
  665. example:
  666. A=POINTER TO RECORD(B) END;
  667. B=POINTER TO RECORD(A) END;
  668. *)
  669. IF x.pointerBase IS SyntaxTree.RecordType THEN
  670. recordType := x.pointerBase(SyntaxTree.RecordType);
  671. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  672. recordBaseType := ResolveType(recordType.baseType);
  673. recordType.SetBaseType(recordBaseType);
  674. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  675. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  676. END;
  677. CheckModifiers(modifiers, TRUE);
  678. typeFixes.Add(x,currentScope);
  679. x.SetState(SyntaxTree.Resolved);
  680. END;
  681. resolvedType := ResolvedType(x)
  682. END VisitPointerType;
  683. (**
  684. resolve port type
  685. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  686. **)
  687. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  688. VAR value: Basic.Integer;
  689. BEGIN
  690. IF TypeNeedsResolution(x) THEN
  691. x.SetCellsAreObjects(cellsAreObjects);
  692. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  693. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  694. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  695. ELSE
  696. x.SetSize(system.SizeOf(system.longintType));
  697. END;
  698. x.SetState(SyntaxTree.Resolved);
  699. END;
  700. resolvedType := ResolvedType(x)
  701. END VisitPortType;
  702. (**
  703. deferred procedure type resolving
  704. - resolve return type
  705. - traverse and resolve parameters
  706. **)
  707. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  708. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  709. BEGIN
  710. resolved := ResolveType(procedureType.returnType);
  711. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  712. Error(procedureType.position,"forbidden open array return type");
  713. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  714. Error(procedureType.position,"procedure with return type does not return");
  715. END;
  716. procedureType.SetReturnType(resolved);
  717. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  718. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  719. parameter.SetType(procedureType.returnType);
  720. parameter.SetAccess(SyntaxTree.Hidden);
  721. parameter.SetUntraced(procedureType.hasUntracedReturn);
  722. VisitParameter(parameter);
  723. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  724. END;
  725. (* process parameters *)
  726. parameter :=procedureType.firstParameter;
  727. WHILE (parameter # NIL) DO
  728. VisitParameter(parameter);
  729. parameter := parameter.nextParameter;
  730. END;
  731. parameter := procedureType.selfParameter;
  732. IF parameter # NIL THEN
  733. VisitParameter(parameter)
  734. END;
  735. END FixProcedureType;
  736. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  737. VAR prev,this: SyntaxTree.Modifier;
  738. BEGIN
  739. this := modifiers;prev := NIL;
  740. WHILE (this # NIL) & (this.identifier # name) DO
  741. prev := this; this := this.nextModifier;
  742. END;
  743. IF this # NIL THEN
  744. IF this.expression # NIL THEN
  745. Error(this.position,"unexpected expression");
  746. END;
  747. this.Resolved;
  748. position := this.position;
  749. RETURN TRUE
  750. ELSE
  751. RETURN FALSE
  752. END;
  753. END HasFlag;
  754. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  755. VAR prev,this: SyntaxTree.Modifier;
  756. BEGIN
  757. this := modifiers;prev := NIL;
  758. WHILE (this # NIL) & (this.identifier # name) DO
  759. prev := this; this := this.nextModifier;
  760. END;
  761. IF this # NIL THEN
  762. IF this.expression = NIL THEN
  763. Error(this.position,"expected expression value");
  764. ELSE
  765. this.SetExpression(ConstantExpression(this.expression));
  766. IF CheckIntegerValue(this.expression,value) THEN END;
  767. END;
  768. this.Resolved;
  769. position := this.position;
  770. RETURN TRUE
  771. ELSE RETURN FALSE
  772. END;
  773. END HasValue;
  774. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  775. VAR prev,this: SyntaxTree.Modifier;
  776. BEGIN
  777. this := modifiers;prev := NIL;
  778. WHILE (this # NIL) & (this.identifier # name) DO
  779. prev := this; this := this.nextModifier;
  780. END;
  781. IF this # NIL THEN
  782. IF this.expression = NIL THEN
  783. Error(this.position,"expected expression value");
  784. ELSE
  785. this.SetExpression(ConstantExpression(this.expression));
  786. IF CheckStringValue(this.expression,value) THEN END;
  787. END;
  788. this.Resolved;
  789. position := this.position;
  790. RETURN TRUE
  791. ELSE RETURN FALSE
  792. END;
  793. END HasStringValue;
  794. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  795. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  796. BEGIN
  797. IF cellsAreObjects THEN RETURN FALSE END;
  798. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  799. IF svalue = "A2" THEN
  800. RETURN TRUE
  801. END;
  802. END;
  803. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  804. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  805. END;
  806. RETURN FALSE;
  807. (*
  808. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  809. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  810. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  811. IF svalue[0] = "~" THEN
  812. Strings.TrimLeft(svalue, "~");
  813. IF svalue = backendName THEN
  814. RETURN TRUE;
  815. END;
  816. ELSIF svalue # backendName THEN
  817. RETURN TRUE;
  818. END;
  819. END;
  820. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  821. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  822. END;
  823. RETURN FALSE;
  824. *)
  825. END SkipImplementation;
  826. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  827. VAR this: SyntaxTree.Modifier;
  828. BEGIN
  829. this := modifiers;
  830. WHILE this # NIL DO
  831. IF ~this.resolved THEN
  832. IF checkUse THEN
  833. Error(this.position,"unexpected modifier");
  834. ELSE
  835. this.SetExpression(ResolveExpression(this.expression));
  836. this.Resolved;
  837. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  838. END;
  839. END;
  840. this := this.nextModifier
  841. END;
  842. END CheckModifiers;
  843. (**
  844. resolve procedure type
  845. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  846. **)
  847. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  848. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  849. BEGIN
  850. IF TypeNeedsResolution(procedureType) THEN
  851. modifiers := procedureType.modifiers;
  852. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  853. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  854. procedureType.SetInterrupt(TRUE);
  855. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  856. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  857. IF useDarwinCCalls THEN (*fld*)
  858. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  859. ELSE
  860. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  861. END
  862. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  863. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  864. Error(position, "undefined platform calling convention");
  865. ELSE
  866. procedureType.SetCallingConvention(system.platformCallingConvention);
  867. END;
  868. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  869. procedureType.SetNoReturn(TRUE);
  870. END;
  871. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  872. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  873. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  874. CheckModifiers(modifiers, TRUE);
  875. modifiers := procedureType.returnTypeModifiers;
  876. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  877. CheckModifiers(modifiers, TRUE);
  878. typeFixes.Add(procedureType,currentScope);
  879. procedureType.SetHasPointers(procedureType.isDelegate);
  880. procedureType.SetState(SyntaxTree.Resolved);
  881. END;
  882. resolvedType := ResolvedType(procedureType)
  883. END VisitProcedureType;
  884. (** check and resolve record type
  885. - check base type: must be record, math array or array-structured object type
  886. - check declarations
  887. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  888. **)
  889. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  890. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  891. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  892. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  893. hasPointers: BOOLEAN;
  894. modifiers: SyntaxTree.Modifier;
  895. value: Basic.Integer;
  896. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  897. BEGIN
  898. type := type.resolved;
  899. IF (type IS SyntaxTree.PointerType) &
  900. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  901. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  902. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  903. RETURN TRUE
  904. ELSE
  905. RETURN FALSE
  906. END;
  907. END IsPointerToRecord;
  908. BEGIN
  909. IF TypeNeedsResolution(x) THEN
  910. hasPointers := FALSE;
  911. modifiers := x.modifiers;
  912. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  913. END;
  914. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  915. CheckModifiers(modifiers, TRUE);
  916. IF x.baseType # NIL THEN
  917. position := x.baseType.position;
  918. baseType := ResolveType(x.baseType);
  919. resolved := baseType.resolved;
  920. hasPointers := hasPointers OR resolved.hasPointers;
  921. IF x.isObject THEN (* object *)
  922. ASSERT(x.pointerType # NIL);
  923. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  924. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  925. baseType := NIL
  926. ELSIF IsPointerToRecord(resolved,recordType) THEN
  927. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  928. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  929. ELSE
  930. Error(position,"object does not extend pointer to record, object or math array ")
  931. END;
  932. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  933. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  934. ELSIF IsPointerToRecord(resolved,recordType) THEN
  935. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  936. ELSIF resolved IS SyntaxTree.RecordType THEN
  937. ELSE
  938. Error(position,"pointer to record does not extend pointer to record or record")
  939. END;
  940. ELSE
  941. IF resolved IS SyntaxTree.RecordType THEN
  942. ELSE
  943. Error(position,"record does not extend record")
  944. END;
  945. END;
  946. x.SetBaseType(baseType);
  947. IF x.Level() > 15 THEN
  948. Error(position, "record/object inheritance level too high");
  949. (* note:
  950. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  951. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  952. inheritance history of a type.
  953. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  954. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  955. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  956. *)
  957. END;
  958. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  959. Error(position, "invalid inheritance of disposable types");
  960. END;
  961. END;
  962. Declarations(x.recordScope, FALSE, {0});
  963. x.SetState(SyntaxTree.Resolved);
  964. Declarations(x.recordScope, FALSE, {1});
  965. ResolveArrayStructure(x);
  966. (* computation of sizes and offsets skipped -> done in backend / system *)
  967. recordBase := x.GetBaseRecord();
  968. IF recordBase = NIL THEN numberMethods := 0
  969. ELSE numberMethods := recordBase.recordScope.numberMethods
  970. END;
  971. isRealtime := TRUE;
  972. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  973. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  974. END;
  975. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  976. WHILE symbol # NIL DO
  977. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  978. IF symbol IS SyntaxTree.Variable THEN
  979. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  980. END;
  981. IF symbol IS SyntaxTree.Procedure THEN
  982. procedure := symbol(SyntaxTree.Procedure);
  983. IF procedure.super # NIL THEN
  984. procedure.SetMethodNumber(procedure.super.methodNumber);
  985. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  986. procedure.SetMethodNumber(numberMethods);
  987. INC(numberMethods);
  988. END;
  989. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  990. Error(procedure.position,"realtime procedure in non-realtime object")
  991. END;
  992. END;
  993. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  994. Error(symbol.position,"non-realtime symbol in realtime object")
  995. END;
  996. symbol := symbol.nextSymbol;
  997. END;
  998. IF isRealtime THEN x.SetRealtime(TRUE) END;
  999. x.recordScope.SetNumberMethods(numberMethods);
  1000. (* TODO: is this needed anymore? *)
  1001. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1002. Error(x.position,"object extends a record")
  1003. END;
  1004. IF ~x.isAbstract THEN
  1005. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1006. Error(x.position, "non-abstract object contains abstract procedure");
  1007. END;
  1008. ELSE
  1009. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1010. Error(x.position, "abstract object does not contain an abstract method");
  1011. END;
  1012. END;
  1013. IF (x.typeDeclaration = NIL) THEN
  1014. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1015. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1016. (*
  1017. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1018. AnonymousTypeDeclaration(x,name);
  1019. *)
  1020. ELSE
  1021. AnonymousTypeDeclaration(x,"Anonymous");
  1022. END;
  1023. END;
  1024. x.SetHasPointers(hasPointers);
  1025. x.SetState(SyntaxTree.Resolved);
  1026. END;
  1027. resolvedType := ResolvedType(x);
  1028. END VisitRecordType;
  1029. (** check and resolve cell type
  1030. - check base type: must be cell
  1031. - check declarations
  1032. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1033. **)
  1034. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1035. VAR
  1036. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1037. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1038. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1039. recordBase: SyntaxTree.RecordType;
  1040. numberMethods: LONGINT;
  1041. int: Basic.Integer;
  1042. real: LONGREAL;
  1043. bool: BOOLEAN;
  1044. set: Basic.Set;
  1045. v: SyntaxTree.Expression;
  1046. str: Scanner.StringType;
  1047. atype: SyntaxTree.ArrayType;
  1048. prev: SyntaxTree.Scope;
  1049. BEGIN
  1050. IF TypeNeedsResolution(x) THEN
  1051. recordBase := NIL;
  1052. IF cellsAreObjects THEN
  1053. IF x.baseType = NIL THEN
  1054. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1055. ImportModule(qualifiedIdentifier.prefix, x.position);
  1056. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1057. x.SetBaseType(ResolveType(x.baseType));
  1058. recordBase := x.GetBaseRecord();
  1059. IF recordBase = NIL THEN
  1060. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1061. END;
  1062. ELSE
  1063. x.SetBaseType(ResolveType(x.baseType));
  1064. END;
  1065. ELSE
  1066. x.SetBaseType(ResolveType(x.baseType));
  1067. END;
  1068. IF recordBase = NIL THEN numberMethods := 0
  1069. ELSE numberMethods := recordBase.recordScope.numberMethods
  1070. END;
  1071. modifier := x.modifiers;
  1072. (*IF ~x.isCellNet THEN*)
  1073. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1074. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1075. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1076. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1077. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1078. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1079. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1080. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1081. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1082. symbol := system.activeCellsCapabilities;
  1083. WHILE symbol # NIL DO
  1084. IF HasFlag(modifier, symbol.name, position) THEN END;
  1085. symbol := symbol.nextSymbol;
  1086. END;
  1087. modifier := x.modifiers;
  1088. WHILE (modifier # NIL) DO
  1089. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1090. IF modifier.expression # NIL THEN
  1091. v := ConstantExpression(modifier.expression);
  1092. property.SetValue(v);
  1093. IF IsIntegerValue(modifier.expression, int) THEN
  1094. (*property.SetValue(modifier.expression);*)
  1095. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1096. property.SetType(system.longintType);
  1097. ELSIF IsRealValue(modifier.expression, real) THEN
  1098. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1099. property.SetType(system.longrealType);
  1100. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1101. property.SetType(system.booleanType);
  1102. ELSIF IsSetValue(modifier.expression, set) THEN
  1103. property.SetType(system.setType);
  1104. ELSIF IsStringValue(modifier.expression, str) THEN
  1105. (*property.SetValue(modifier.expression);*)
  1106. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1107. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1108. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1109. property.SetType(atype);
  1110. ELSE
  1111. Error(modifier.position, "unsupported property type");
  1112. END;
  1113. ELSE (* flag property *)
  1114. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1115. property.SetType(system.booleanType);
  1116. END;
  1117. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1118. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1119. x.AddProperty(property);
  1120. modifier := modifier.nextModifier;
  1121. END;
  1122. CheckModifiers(modifier, FALSE);
  1123. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1124. (* process parameters *)
  1125. prev := currentScope;
  1126. currentScope := x.cellScope;
  1127. parameter :=x.firstParameter;
  1128. WHILE (parameter # NIL) DO
  1129. VisitParameter(parameter);
  1130. type := parameter.type.resolved;
  1131. IF ~(type IS SyntaxTree.PortType) THEN
  1132. WHILE IsStaticArray(type, type, len) DO
  1133. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1134. END;
  1135. WHILE IsDynamicArray(type, type) DO
  1136. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1137. END;
  1138. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1139. Error(parameter.position, "invalid type, must be port or static array of port ");
  1140. END;
  1141. END;
  1142. parameter := parameter.nextParameter;
  1143. END;
  1144. currentScope := prev;
  1145. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1146. WHILE symbol # NIL DO
  1147. IF symbol IS SyntaxTree.Variable THEN
  1148. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1149. END;
  1150. symbol := symbol.nextSymbol;
  1151. END;
  1152. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1153. IF (x.typeDeclaration = NIL) THEN
  1154. AnonymousTypeDeclaration(x,"Anonymous");
  1155. END;
  1156. x.SetState(SyntaxTree.Resolved);
  1157. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1158. Warning(x.position, "Forbidden empty Body.");
  1159. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1160. Warning(x.position, "Non-empty body for an engine?");
  1161. END;
  1162. END;
  1163. resolvedType := ResolvedType(x);
  1164. END VisitCellType;
  1165. (* check if an object is an array-structured object type
  1166. - determine the array structure
  1167. - collect operators from top to bottom in the inheritance hierarchy
  1168. - check if LEN operator is declared
  1169. - determine number of possible index operators
  1170. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1171. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1172. *)
  1173. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1174. VAR
  1175. indexOperatorCount, i: LONGINT;
  1176. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1177. isTensor: BOOLEAN;
  1178. BEGIN
  1179. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1180. (* determine array structure *)
  1181. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1182. END;
  1183. IF recordType.HasArrayStructure() THEN
  1184. (* the object is an ASOT *)
  1185. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1186. (* reset array access operators *)
  1187. arrayAccessOperators.len := NIL;
  1188. arrayAccessOperators.generalRead := NIL;
  1189. arrayAccessOperators.generalWrite := NIL;
  1190. IF isTensor THEN
  1191. (* all operators of dimensionalities 1 to max *)
  1192. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1193. ELSE
  1194. (* all operators of certain dimensionality *)
  1195. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1196. END;
  1197. NEW(arrayAccessOperators.read, indexOperatorCount);
  1198. NEW(arrayAccessOperators.write, indexOperatorCount);
  1199. FOR i := 0 TO indexOperatorCount - 1 DO
  1200. arrayAccessOperators.read[i] := NIL;
  1201. arrayAccessOperators.write[i] := NIL
  1202. END;
  1203. (* collect access operators in the record scope *)
  1204. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1205. IF arrayAccessOperators.len = NIL THEN
  1206. (* TODO: think about making this operator optional for static array structures *)
  1207. Error(recordType.position, "LEN operator missing")
  1208. END;
  1209. (* show error messages *)
  1210. IF isTensor THEN
  1211. (* require ARRAY [*] OF RANGE *)
  1212. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1213. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1214. ELSE
  1215. (* forbid ARRAY [*] OF RANGE *)
  1216. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1217. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1218. (* require RANGE, RANGE, ... RANGE *)
  1219. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1220. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1221. END;
  1222. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1223. ELSE
  1224. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1225. IF recordType.recordScope.firstOperator # NIL THEN
  1226. RETURN;
  1227. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1228. END
  1229. END
  1230. END ResolveArrayStructure;
  1231. (** collect array access operators in a record scope **)
  1232. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1233. VAR
  1234. baseType: SyntaxTree.Type;
  1235. operator: SyntaxTree.Operator;
  1236. isReadOperator, isGeneralOperator: BOOLEAN;
  1237. indexListSize, indexListKind, hashValue: LONGINT;
  1238. BEGIN
  1239. (* if a parent record scope exists, collect the operators there first *)
  1240. baseType := recordScope.ownerRecord.baseType;
  1241. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1242. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1243. END;
  1244. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1245. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1246. END;
  1247. (* go through all operators in the current record scope *)
  1248. operator := recordScope.firstOperator;
  1249. WHILE operator # NIL DO
  1250. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1251. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1252. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1253. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1254. IF isGeneralOperator THEN
  1255. IF isReadOperator THEN
  1256. arrayAccessOperators.generalRead := operator
  1257. ELSE
  1258. arrayAccessOperators.generalWrite := operator
  1259. END
  1260. ELSE
  1261. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1262. IF isReadOperator THEN
  1263. arrayAccessOperators.read[hashValue] := operator
  1264. ELSE
  1265. arrayAccessOperators.write[hashValue] := operator
  1266. END
  1267. END
  1268. END
  1269. ELSE
  1270. Error(operator.position, 'invalid operator')
  1271. END;
  1272. operator := operator.nextOperator
  1273. END
  1274. END CollectArrayAccessOperators;
  1275. (** the hash value of an index operator **)
  1276. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1277. VAR result: LONGINT;
  1278. BEGIN
  1279. IF isTensor THEN
  1280. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1281. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1282. ELSE
  1283. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1284. END
  1285. ELSE
  1286. result := indexListKind
  1287. END;
  1288. RETURN result
  1289. END IndexOperatorHash;
  1290. (** 2 to the power of exponent **)
  1291. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1292. VAR result, i: LONGINT;
  1293. BEGIN
  1294. result := 1;
  1295. FOR i := 1 TO exponent DO
  1296. result := result * 2;
  1297. END;
  1298. RETURN result
  1299. END TwoToThePowerOf;
  1300. (** check if a LEN operator has a correct signature. i.e.
  1301. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1302. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1303. **)
  1304. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1305. VAR
  1306. procedureType: SyntaxTree.ProcedureType;
  1307. returnedArrayType: SyntaxTree.MathArrayType;
  1308. result: BOOLEAN;
  1309. BEGIN
  1310. result := FALSE;
  1311. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1312. IF (procedureType.numberParameters = 0) THEN
  1313. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1314. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1315. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1316. IF returnedArrayType.form = SyntaxTree.Open THEN
  1317. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1318. result := TRUE
  1319. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1320. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1321. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1322. result := TRUE
  1323. END
  1324. END
  1325. END
  1326. END
  1327. END;
  1328. IF result THEN
  1329. (* export symbol automatically *)
  1330. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1331. ELSE
  1332. Error(operator.position, "LEN operator with invalid signature");
  1333. END;
  1334. RETURN result
  1335. END CheckLenOperator;
  1336. (** check if an index operator has a correct signature. i.e.
  1337. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1338. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1339. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1340. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1341. - determine if it is a read or write operator (existance of return type)
  1342. - check index parameters
  1343. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1344. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1345. [LONGINT] -> binary 0 -> 0
  1346. [RANGE] -> binary 1 -> 1
  1347. [LONGINT, LONGINT] -> binary 00 -> 0
  1348. [LONGINT, RANGE] -> binary 01 -> 1
  1349. [RANGE, LONGINT] -> binary 10 -> 2
  1350. [RANGE, RANGE] -> binary 11 -> 3
  1351. etc.
  1352. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1353. - for read operators, check if return type matches the type of data that is read
  1354. - for write operators, check if last parameter type matches the type of data that is written
  1355. **)
  1356. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1357. VAR
  1358. elementType, otherElementType, dataType: SyntaxTree.Type;
  1359. procedureType: SyntaxTree.ProcedureType;
  1360. mathArrayType: SyntaxTree.MathArrayType;
  1361. parameter: SyntaxTree.Parameter;
  1362. parameterCount, rangeCount, i: LONGINT;
  1363. hasTypeError: BOOLEAN;
  1364. BEGIN
  1365. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1366. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1367. (* determine if it is a read or write operator *)
  1368. isReadOperator := (procedureType.returnType # NIL);
  1369. IF isReadOperator THEN
  1370. indexListSize := parameterCount;
  1371. ELSE
  1372. indexListSize := parameterCount - 1;
  1373. END;
  1374. IF indexListSize < 1 THEN
  1375. Error(operator.position, "index operator with too few parameters");
  1376. RETURN FALSE
  1377. END;
  1378. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1379. (* general operator *)
  1380. isGeneralOperator := TRUE;
  1381. IF indexListSize > 1 THEN
  1382. Error(operator.position, "index operator with too many parameters");
  1383. RETURN FALSE
  1384. END;
  1385. (* ARRAY [*] OF RANGE*)
  1386. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1387. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1388. Error(operator.position, "index parameter not dynamic math array of range");
  1389. RETURN FALSE
  1390. END;
  1391. parameter := procedureType.firstParameter.nextParameter
  1392. ELSE
  1393. (* fixed-dim. operator *)
  1394. isGeneralOperator := FALSE;
  1395. (* check number of index parameters *)
  1396. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1397. (* for tensors, limited to a certain size *)
  1398. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1399. Error(operator.position, "too many index parameters for tensor");
  1400. RETURN FALSE
  1401. END
  1402. ELSE
  1403. (* for non-tensors, depends on dimensionality *)
  1404. IF indexListSize # arrayStructure.Dimensionality() THEN
  1405. Error(operator.position, "index parameter count does not match dimensionality");
  1406. RETURN FALSE
  1407. END
  1408. END;
  1409. (* go through all index parameters
  1410. - count the number of ranges
  1411. - determine the index list kind number
  1412. *)
  1413. indexListKind := 0;
  1414. rangeCount := 0;
  1415. parameter := procedureType.firstParameter;
  1416. FOR i := 1 TO indexListSize DO
  1417. indexListKind := indexListKind * 2;
  1418. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1419. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1420. INC(indexListKind);
  1421. INC(rangeCount)
  1422. ELSE
  1423. Error(parameter.position, "integer or range expected");
  1424. RETURN FALSE
  1425. END;
  1426. parameter := parameter.nextParameter
  1427. END;
  1428. END;
  1429. (*
  1430. - for read operators: check type of last parameter
  1431. - for write operators: check return type
  1432. *)
  1433. IF isReadOperator THEN
  1434. dataType := procedureType.returnType (* the return type *)
  1435. ELSE
  1436. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1437. END;
  1438. elementType := arrayStructure.ElementType();
  1439. hasTypeError := FALSE;
  1440. IF isGeneralOperator THEN
  1441. (* ARRAY [?] OF <Element> *)
  1442. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1443. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1444. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1445. hasTypeError := TRUE
  1446. END
  1447. ELSE
  1448. hasTypeError := TRUE
  1449. END
  1450. ELSE
  1451. IF rangeCount = 0 THEN
  1452. (* <Element> *)
  1453. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1454. ELSE
  1455. (* ARRAY [*, *, ..., *] OF <Element> *)
  1456. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1457. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1458. IF mathArrayType.IsFullyDynamic() THEN
  1459. IF mathArrayType.Dimensionality() = rangeCount THEN
  1460. otherElementType := mathArrayType.ElementType();
  1461. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1462. ELSE
  1463. hasTypeError := TRUE
  1464. END
  1465. ELSE
  1466. hasTypeError := TRUE
  1467. END
  1468. ELSE
  1469. hasTypeError := TRUE
  1470. END
  1471. END
  1472. END;
  1473. IF hasTypeError THEN
  1474. IF isReadOperator THEN
  1475. Error(operator.position, "return type does not match")
  1476. ELSE
  1477. Error(parameter.position, "type of last parameter does not match")
  1478. END;
  1479. RETURN FALSE
  1480. END;
  1481. (* export symbol automatically *)
  1482. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1483. RETURN TRUE
  1484. END CheckIndexOperator;
  1485. (** resolve all pending types (late resolving).
  1486. - type fixes are resolved at the end of the declaration phase
  1487. - type fixes may imply new type fixes that are also entered at the end of the list
  1488. **)
  1489. PROCEDURE FixTypes;
  1490. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1491. BEGIN
  1492. prevScope := currentScope;
  1493. p := typeFixes.Get(currentScope);
  1494. WHILE p # NIL DO
  1495. ASSERT(currentScope # NIL);
  1496. ASSERT(p IS SyntaxTree.Type);
  1497. IF p IS SyntaxTree.PointerType THEN
  1498. FixPointerType(p(SyntaxTree.PointerType))
  1499. ELSIF p IS SyntaxTree.ProcedureType THEN
  1500. FixProcedureType(p(SyntaxTree.ProcedureType))
  1501. ELSE
  1502. HALT(100);
  1503. END;
  1504. p := typeFixes.Get(currentScope);
  1505. END;
  1506. currentScope :=prevScope;
  1507. END FixTypes;
  1508. (**
  1509. resolve type x
  1510. - if x is nil then return nil
  1511. - if x cannot be resolved then the result is invalidType else the result is x
  1512. - the resolved type is entered into x.resolved
  1513. **)
  1514. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1515. VAR prev,resolved: SyntaxTree.Type;
  1516. BEGIN
  1517. prev := resolvedType;
  1518. resolvedType := SyntaxTree.invalidType;
  1519. IF x = NIL THEN resolvedType := NIL
  1520. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1521. END;
  1522. resolved := resolvedType;
  1523. resolvedType := prev;
  1524. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1525. RETURN resolved
  1526. END ResolveType;
  1527. (*** compatibility rules ***)
  1528. (**
  1529. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1530. **)
  1531. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1532. VAR result: SyntaxTree.Type;
  1533. BEGIN
  1534. result := SyntaxTree.invalidType;
  1535. IF type = NIL THEN Error(position, "expression of type NIL");
  1536. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1538. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1539. ELSE result := type.resolved
  1540. END;
  1541. RETURN result
  1542. END RegularType;
  1543. (** returns signature compatibility of procedure types this and to
  1544. - if not compatible then error is reported
  1545. - compatibility means type equality
  1546. **)
  1547. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1548. VAR result: BOOLEAN;
  1549. BEGIN
  1550. result := SameType(to,this);
  1551. IF ~result THEN
  1552. Error(position, "signature incompatible");
  1553. IF VerboseErrorMessage THEN
  1554. Printout.Info("this",this);
  1555. Printout.Info("to",to);
  1556. END;
  1557. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1558. Error(position, "signature incompatible: realtime flag must be inherited");
  1559. END;
  1560. RETURN result
  1561. END SignatureCompatible;
  1562. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1563. - for var parameters compatibility means same type except for
  1564. - formal is of open array of system byte
  1565. - formal is of record type
  1566. - formal is of open array type
  1567. - formal is of open math array type
  1568. - for value parameters compatibllity means assignment compatibility except for
  1569. - formal is of open array type
  1570. if compatible the return true else report error and return false
  1571. **)
  1572. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1573. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1574. BEGIN
  1575. formalType := RegularType(formal.position,formal.type);
  1576. actualType := RegularType(actual.position,actual.type);
  1577. error := FALSE;
  1578. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1579. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1580. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1581. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1582. END;
  1583. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1584. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1585. ELSIF ~IsVariable(actual) THEN
  1586. result := FALSE; error := TRUE;
  1587. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1588. Error(actual.position,"not a variable: no operator for writing");
  1589. ELSE
  1590. Error(actual.position,"is not a variable");
  1591. END;
  1592. IF VerboseErrorMessage THEN
  1593. Printout.Info("actual",actual);
  1594. Printout.Info("formal",formal);
  1595. END;
  1596. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1597. result := CompatibleTo(system,actualType,formalType);
  1598. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1599. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1600. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1601. IF IsArrayStructuredObjectType(actualType) THEN
  1602. actualType := MathArrayStructureOfType(actualType)
  1603. END;
  1604. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1605. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1606. Error(actual.position,"incompatible non-static actual type");
  1607. END;
  1608. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1609. Error(actual.position,"incompatible tensor (use a range expression)");
  1610. END;
  1611. ELSE
  1612. result := SameType(actualType,formalType)
  1613. END
  1614. ELSE
  1615. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1616. actualType := system.characterType;
  1617. END;
  1618. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1619. result := TRUE; (* special rule for WINAPI parameters *)
  1620. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1621. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1622. ELSE
  1623. result := CompatibleTo(system,actualType,formalType);
  1624. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1625. Error(actual.position,"incompatible non-static actual type");
  1626. END;
  1627. END;
  1628. END;
  1629. IF ~result & ~error THEN
  1630. Error(actual.position,"incompatible parameter");
  1631. IF VerboseErrorMessage THEN
  1632. Printout.Info("actual",actual);
  1633. Printout.Info("formal",formal);
  1634. END;
  1635. END;
  1636. RETURN result
  1637. END ParameterCompatible;
  1638. (** check compatibility for expressions of the form left := right
  1639. - if compatible then return true else error report and return false
  1640. - check if left is variable
  1641. - check compatiblity
  1642. **)
  1643. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1644. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1645. BEGIN
  1646. result := FALSE;
  1647. leftType := RegularType(left.position,left.type);
  1648. rightType := RegularType(right.position,right.type);
  1649. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1650. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1651. END;
  1652. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1653. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1654. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1655. (* error already handled *)
  1656. result := TRUE;
  1657. ELSIF ~IsVariable(left) THEN
  1658. Error(left.position,"is not a variable");
  1659. IF VerboseErrorMessage THEN
  1660. Printout.Info("left",left);
  1661. Printout.Info("right",right);
  1662. END;
  1663. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1664. result := TRUE;
  1665. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1666. result := TRUE
  1667. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1668. Error(left.position,"incompatible assignment");
  1669. IF VerboseErrorMessage THEN
  1670. Printout.Info("left",left);
  1671. Printout.Info("right",right);
  1672. END;
  1673. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1674. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1675. Error(right.position,"forbidden assignment of a nested procedure");
  1676. ELSE
  1677. result := TRUE
  1678. END;
  1679. RETURN result
  1680. END AssignmentCompatible;
  1681. (*** values ***)
  1682. (** check and resolve integer value **)
  1683. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1684. VAR integer: Basic.Integer;
  1685. BEGIN
  1686. integer := value(SyntaxTree.IntegerValue).value;
  1687. value.SetType(Global.GetIntegerType(system,integer));
  1688. resolvedExpression := value
  1689. END VisitIntegerValue;
  1690. (** check and resolve real value **)
  1691. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1692. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1693. BEGIN
  1694. subtype := value(SyntaxTree.RealValue).subtype;
  1695. IF subtype = Scanner.Real THEN
  1696. type := system.realType
  1697. ELSIF subtype = Scanner.Longreal THEN
  1698. type := system.longrealType
  1699. ELSE
  1700. HALT(100)
  1701. END;
  1702. value.SetType(type);
  1703. resolvedExpression := value
  1704. END VisitRealValue;
  1705. (** check and resolve complex value **)
  1706. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1707. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1708. BEGIN
  1709. subtype := value(SyntaxTree.ComplexValue).subtype;
  1710. IF subtype = Scanner.Real THEN
  1711. type := system.complexType
  1712. ELSIF subtype = Scanner.Longreal THEN
  1713. type := system.longcomplexType
  1714. ELSE
  1715. HALT(100)
  1716. END;
  1717. value.SetType(type);
  1718. resolvedExpression := value
  1719. END VisitComplexValue;
  1720. (** check and resolve set value **)
  1721. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1722. BEGIN
  1723. value.SetType(system.setType);
  1724. resolvedExpression := value
  1725. END VisitSetValue;
  1726. (** check and resolve set value **)
  1727. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1728. BEGIN
  1729. value.SetType(SyntaxTree.invalidType);
  1730. resolvedExpression := value
  1731. END VisitMathArrayValue;
  1732. (** check and resolve boolean value **)
  1733. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1734. BEGIN
  1735. value.SetType(system.booleanType);
  1736. resolvedExpression := value
  1737. END VisitBooleanValue;
  1738. (** check and resolve string value **)
  1739. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1740. BEGIN
  1741. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1742. resolvedExpression := value
  1743. END VisitStringValue;
  1744. (** check and resolve character value **)
  1745. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1746. BEGIN
  1747. value.SetType(system.characterType);
  1748. resolvedExpression := value
  1749. END VisitCharacterValue;
  1750. (** check and resolve nil value **)
  1751. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1752. BEGIN
  1753. value.SetType(system.nilType);
  1754. resolvedExpression := value
  1755. END VisitNilValue;
  1756. (** check and resolve enumerator value **)
  1757. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1758. BEGIN
  1759. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1760. ASSERT(value.type # NIL);
  1761. resolvedExpression := value
  1762. END VisitEnumerationValue;
  1763. (*** expressions ***)
  1764. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1765. - check all elements on integer type
  1766. - if element range is constant, then check lower and upper bound
  1767. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1768. if an error occurs then report error and return invalidExpression
  1769. **)
  1770. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1771. VAR
  1772. i: LONGINT;
  1773. element: SyntaxTree.Expression;
  1774. constant: BOOLEAN;
  1775. elements: SyntaxTree.ExpressionList;
  1776. s: Basic.Set;
  1777. result: SyntaxTree.Expression;
  1778. value: SyntaxTree.Value;
  1779. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1780. VAR
  1781. left, right: SyntaxTree.Expression;
  1782. elementResult: SyntaxTree.Expression;
  1783. leftInteger, rightInteger: Basic.Integer;
  1784. BEGIN
  1785. (* set context of range *)
  1786. IF element IS SyntaxTree.RangeExpression THEN
  1787. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1788. END;
  1789. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1790. IF elementResult = SyntaxTree.invalidExpression THEN
  1791. (* error already reported *)
  1792. constant := FALSE
  1793. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1794. (* the element is a range expression *)
  1795. (* extract left and right hand side of range *)
  1796. left := elementResult(SyntaxTree.RangeExpression).first;
  1797. right := elementResult(SyntaxTree.RangeExpression).last;
  1798. (* guaranteed by VisitRangeExpression: *)
  1799. ASSERT((left # NIL) & (right # NIL));
  1800. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1801. ELSE
  1802. (* the element is not a range expression *)
  1803. (* check type and add conversion if needed *)
  1804. IF IsIntegerType(elementResult.type.resolved) THEN
  1805. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1806. ELSE
  1807. Error(elementResult.position, "non integer element in set");
  1808. elementResult := SyntaxTree.invalidExpression;
  1809. constant := FALSE
  1810. END;
  1811. left := elementResult;
  1812. right := elementResult
  1813. END;
  1814. IF elementResult # SyntaxTree.invalidExpression THEN
  1815. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1816. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1817. Error(left.position,"not allowed set integer value");
  1818. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1819. Error(right.position,"not allowed set integer value");
  1820. END
  1821. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1822. Error(right.position,"not allowed set integer value");
  1823. ELSE
  1824. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1825. s := {};
  1826. ELSE
  1827. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1828. IF leftInteger < 0 THEN leftInteger := 0 END;
  1829. (*!!!!!!!!! this is a hack !!!!!!! *)
  1830. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1831. extends the range x..31 to x..63 !!!!!! *)
  1832. s := s + {leftInteger..rightInteger};
  1833. END;
  1834. END;
  1835. ELSE
  1836. constant := FALSE;
  1837. END
  1838. END;
  1839. RETURN elementResult
  1840. END CheckElement;
  1841. BEGIN
  1842. result := set; constant := TRUE; s := {}; elements := set.elements;
  1843. IF elements # NIL THEN
  1844. FOR i := 0 TO elements.Length()-1 DO
  1845. element := elements.GetExpression(i);
  1846. element := CheckElement(element);
  1847. IF element = SyntaxTree.invalidExpression THEN
  1848. result := SyntaxTree.invalidExpression
  1849. END;
  1850. elements.SetExpression(i,element);
  1851. END;
  1852. END;
  1853. IF constant THEN
  1854. value := Global.NewSetValue(system,set.position,s);
  1855. result.SetResolved(value);
  1856. result.SetType(value.type);
  1857. ELSE
  1858. result.SetType(system.setType);
  1859. END;
  1860. (* optimization possible
  1861. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1862. left this to the programmer...
  1863. *)
  1864. resolvedExpression := result;
  1865. END VisitSet;
  1866. (*
  1867. old variant: quite generic but needs better conversion handling, do this?
  1868. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1869. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1870. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1871. BEGIN
  1872. type := NIL;
  1873. numberElements := x.elements.Length();
  1874. FOR i := 0 TO numberElements-1 DO
  1875. expression := x.elements.GetExpression(i);
  1876. position := expression.position;
  1877. expression := ResolveExpression(x.elements.GetExpression(i));
  1878. x.elements.SetExpression(i,de);
  1879. IF type = NIL THEN
  1880. type := expression.type;
  1881. ELSIF CompatibleTo(system,expression.type,type) THEN
  1882. (* ok *)
  1883. ELSIF CompatibleTo(system,type,expression.type) THEN
  1884. type := expression.type
  1885. ELSE
  1886. Error(expression.position, "incompatible element types");
  1887. type := SyntaxTree.invalidType;
  1888. END;
  1889. END;
  1890. isValue := TRUE;
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1893. x.elements.SetExpression(i,expression);
  1894. isValue := isValue & (expression.resolved # NIL);
  1895. END;
  1896. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1897. arrayType.SetArrayBase(type);
  1898. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1899. IF isValue THEN
  1900. value := SyntaxTree.NewMathArrayValue(position);
  1901. value.SetElements(x.elements);
  1902. x.SetResolved(value);
  1903. END;
  1904. x.SetType(arrayType);
  1905. resolvedExpression := x;
  1906. END VisitMathArrayExpression;
  1907. *)
  1908. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1909. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1910. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1911. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1912. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1913. BEGIN
  1914. numberElements := x.elements.Length();
  1915. FOR i := 0 TO numberElements-1 DO
  1916. expression := x.elements.GetExpression(i);
  1917. IF expression IS SyntaxTree.MathArrayExpression THEN
  1918. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1919. ELSE
  1920. position := expression.position;
  1921. expression := ResolveExpression(x.elements.GetExpression(i));
  1922. x.elements.SetExpression(i,expression);
  1923. IF type = NIL THEN
  1924. type := expression.type;
  1925. ELSIF CompatibleTo(system,expression.type,type) THEN
  1926. (* ok *)
  1927. ELSIF CompatibleTo(system,type,expression.type) THEN
  1928. type := expression.type
  1929. ELSE
  1930. Error(expression.position, "incompatible element types");
  1931. type := SyntaxTree.invalidType;
  1932. END;
  1933. END;
  1934. END;
  1935. END RecursivelyFindType;
  1936. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1937. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1938. BEGIN
  1939. numberElements := x.elements.Length();
  1940. FOR i := 0 TO numberElements-1 DO
  1941. expression := x.elements.GetExpression(i);
  1942. IF expression IS SyntaxTree.MathArrayExpression THEN
  1943. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1944. ELSE
  1945. position := expression.position;
  1946. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1947. x.elements.SetExpression(i,expression);
  1948. isValue := isValue & (expression.resolved # NIL);
  1949. END;
  1950. END;
  1951. END RecursivelySetExpression;
  1952. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1953. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1954. arrayType: SyntaxTree.MathArrayType;
  1955. BEGIN
  1956. numberElements := x.elements.Length();
  1957. baseType := NIL;
  1958. gsize := 0;
  1959. FOR i := 0 TO numberElements-1 DO
  1960. expression := x.elements.GetExpression(i);
  1961. IF expression IS SyntaxTree.MathArrayExpression THEN
  1962. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1963. IF i=0 THEN
  1964. gsize := size;
  1965. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1966. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1967. ELSE expression.SetType(baseType)
  1968. END;
  1969. ELSIF baseType = NIL THEN baseType := type;
  1970. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1971. END;
  1972. END;
  1973. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1974. arrayType.SetArrayBase(baseType);
  1975. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1976. RETURN ResolveType(arrayType);
  1977. END RecursivelySetType;
  1978. BEGIN
  1979. type := NIL;
  1980. RecursivelyFindType(x);
  1981. isValue := TRUE;
  1982. RecursivelySetExpression(x);
  1983. arrayType := RecursivelySetType(x);
  1984. x.SetType(arrayType);
  1985. IF isValue THEN
  1986. value := SyntaxTree.NewMathArrayValue(x.position);
  1987. value.SetArray(x);
  1988. x.SetResolved(value);
  1989. value.SetType(arrayType);
  1990. END;
  1991. x.SetType(arrayType);
  1992. resolvedExpression := x;
  1993. END VisitMathArrayExpression;
  1994. (** check and resolve unary expression **)
  1995. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1996. VAR
  1997. left: SyntaxTree.Expression;
  1998. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1999. bool: BOOLEAN;
  2000. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2001. value: SyntaxTree.Value;
  2002. BEGIN
  2003. type := SyntaxTree.invalidType;
  2004. left := ResolveExpression(unaryExpression.left);
  2005. unaryExpression.SetLeft(left);
  2006. operator := unaryExpression.operator;
  2007. result := unaryExpression;
  2008. IF ~system.operatorDefined[operator] THEN
  2009. Error(left.position,"Operator Not Defined");
  2010. RETURN
  2011. ELSIF left.type = NIL THEN
  2012. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2013. resolvedExpression := SyntaxTree.invalidExpression;
  2014. RETURN
  2015. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2016. RETURN
  2017. END;
  2018. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2019. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2020. END;
  2021. IF operatorCall # NIL THEN
  2022. result := operatorCall;
  2023. type := operatorCall.type;
  2024. (* admissible operators
  2025. Minus number, set
  2026. Not boolean
  2027. *)
  2028. ELSE
  2029. CASE unaryExpression.operator OF
  2030. |Scanner.Minus:
  2031. IF IsIntegerType(left.type.resolved) THEN
  2032. IF left.resolved # NIL THEN
  2033. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2034. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2035. result.SetResolved(value);
  2036. type := Global.GetIntegerType(system,int);
  2037. value.SetType(type);
  2038. ELSE
  2039. type := left.type
  2040. END
  2041. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2042. IF IsRealValue(left,real) THEN
  2043. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2044. result.SetResolved(value);
  2045. type := left.type;
  2046. value.SetType(type);
  2047. ELSE
  2048. type := left.type;
  2049. END;
  2050. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2051. IF IsSetValue(left,set) THEN
  2052. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2053. result.SetResolved(value);
  2054. type := Global.GetSetType(system,-set);
  2055. value.SetType(type);
  2056. ELSE
  2057. type := left.type;
  2058. END;
  2059. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2060. IF IsComplexValue(left, real, imaginary) THEN
  2061. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2062. result.SetResolved(value);
  2063. type := left.type;
  2064. value.SetType(type);
  2065. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2066. ELSE
  2067. type := left.type;
  2068. END
  2069. ELSE
  2070. Error(left.position,"unary operator not applicable");
  2071. END;
  2072. |Scanner.Not:
  2073. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2074. IF IsBooleanValue(left,bool) THEN
  2075. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2076. result.SetResolved(value);
  2077. type := system.booleanType;
  2078. value.SetType(type);
  2079. ELSE
  2080. type := system.booleanType;
  2081. END;
  2082. ELSE
  2083. Error(left.position,"unary operator not applicable");
  2084. END;
  2085. |Scanner.Plus:
  2086. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2087. result := left; type := left.type;
  2088. ELSE
  2089. Error(left.position,"unary operator not applicable");
  2090. END;
  2091. (* ADDRESS OF *)
  2092. |Scanner.Address:
  2093. IF HasAddress(left) THEN
  2094. type := system.addressType;
  2095. ELSE
  2096. type := SyntaxTree.invalidType;
  2097. Error(left.position,"has no address");
  2098. Printout.Info("par", left);
  2099. END;
  2100. (* SIZE OF *)
  2101. |Scanner.Size:
  2102. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2103. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2104. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2105. value := SyntaxTree.NewIntegerValue(left.position, int);
  2106. result.SetResolved(value);
  2107. type := Global.GetIntegerType(system,int);
  2108. value.SetType(type)
  2109. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2110. ELSE
  2111. (* for variables, system sizeof could represent the physically occupied size
  2112. determined via the type descriptor, implement that ? *)
  2113. Error(left.position,"is not a type symbol");
  2114. END
  2115. (* ALIAS OF *)
  2116. |Scanner.Alias:
  2117. type := left.type.resolved;
  2118. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2119. type := SyntaxTree.invalidType;
  2120. Error(left.position,"alias on non math array type");
  2121. END;
  2122. ELSE
  2123. Error(left.position,"unary operator not defined");
  2124. END;
  2125. END;
  2126. result.SetType(type);
  2127. resolvedExpression := result
  2128. END VisitUnaryExpression;
  2129. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2130. VAR
  2131. result: SyntaxTree.Expression;
  2132. array: SyntaxTree.MathArrayExpression;
  2133. value: SyntaxTree.MathArrayValue;
  2134. isValue: BOOLEAN;
  2135. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2136. BEGIN
  2137. type := type.resolved;
  2138. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2139. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2140. END;
  2141. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2142. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2143. END;
  2144. RETURN type
  2145. END BaseType;
  2146. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2147. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2148. BEGIN
  2149. numberElements := x.elements.Length();
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. array := SyntaxTree.NewMathArrayExpression(position);
  2154. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2155. expression := array;
  2156. ELSE
  2157. position := expression.position;
  2158. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2159. isValue := isValue & (expression.resolved # NIL);
  2160. END;
  2161. to.elements.AddExpression(expression);
  2162. END;
  2163. END RecursivelyConvert;
  2164. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2165. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2166. arrayType: SyntaxTree.MathArrayType;
  2167. BEGIN
  2168. numberElements := x.elements.Length();
  2169. baseType := NIL;
  2170. gsize := 0;
  2171. FOR i := 0 TO numberElements-1 DO
  2172. expression := x.elements.GetExpression(i);
  2173. IF expression IS SyntaxTree.MathArrayExpression THEN
  2174. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2175. IF i=0 THEN
  2176. gsize := size;
  2177. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2178. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2179. ELSE expression.SetType(baseType)
  2180. END;
  2181. ELSIF baseType = NIL THEN baseType := type;
  2182. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2183. END;
  2184. END;
  2185. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2186. arrayType.SetArrayBase(baseType);
  2187. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2188. RETURN ResolveType(arrayType);
  2189. END RecursivelySetType;
  2190. BEGIN
  2191. result := SyntaxTree.invalidExpression;
  2192. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2193. result := expression (* do not convert *)
  2194. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2195. isValue := TRUE;
  2196. type := BaseType(type);
  2197. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2198. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2199. value := SyntaxTree.NewMathArrayValue(array.position);
  2200. value.SetArray(array);
  2201. value.SetType(RecursivelySetType(array));
  2202. result := value;
  2203. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2204. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2205. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2206. IF result = NIL THEN
  2207. result := SyntaxTree.invalidExpression;
  2208. Error(position, "incompatible conversion");
  2209. IF VerboseErrorMessage THEN
  2210. Printout.Info("expression",expression);
  2211. Printout.Info("type",type);
  2212. END;
  2213. END;
  2214. END;
  2215. RETURN result
  2216. END MathArrayConversion;
  2217. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2218. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2219. BEGIN
  2220. result := expression; type := type.resolved;
  2221. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2222. (* skip, no conversion *)
  2223. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2224. int := expression(SyntaxTree.IntegerValue).value;
  2225. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2226. int := Global.ConvertSigned(int,system.SizeOf(type));
  2227. result := SyntaxTree.NewIntegerValue(position,int);
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2230. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2231. result := SyntaxTree.NewIntegerValue(position,int);
  2232. result.SetType(type);
  2233. ELSIF (type IS SyntaxTree.FloatType) THEN
  2234. result := SyntaxTree.NewRealValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2237. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2238. result.SetType(type);
  2239. ELSIF (type IS SyntaxTree.SetType) THEN
  2240. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2243. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2246. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.PortType) THEN
  2249. result := ConvertValue(position, expression, system.integerType);
  2250. ELSE
  2251. Error(position, "integer value cannot be converted");
  2252. result := SyntaxTree.invalidExpression;
  2253. IF VerboseErrorMessage THEN
  2254. Printout.Info("expression",expression);
  2255. Printout.Info("type",type);
  2256. END;
  2257. END;
  2258. ELSIF IsRealValue(expression,real) THEN
  2259. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2260. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2261. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2262. result.SetType(type);
  2263. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2264. int := ENTIERH(real);
  2265. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.FloatType) THEN
  2268. result := SyntaxTree.NewRealValue(position,real);
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2271. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2272. result.SetType(type);
  2273. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2274. ELSIF (type IS SyntaxTree.PortType) THEN
  2275. result := ConvertValue(position, expression, system.integerType);
  2276. ELSE
  2277. Error(position, "real value cannot be converted");
  2278. result := SyntaxTree.invalidExpression;
  2279. END
  2280. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2281. IF (type IS SyntaxTree.ComplexType) THEN
  2282. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2283. result.SetType(type);
  2284. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2285. ELSE
  2286. Error(position, "complex value cannot be converted");
  2287. result := SyntaxTree.invalidExpression;
  2288. END
  2289. ELSIF IsSetValue(expression,set) THEN
  2290. IF (type IS SyntaxTree.IntegerType) THEN
  2291. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2292. result.SetType(type);
  2293. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2294. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.PortType) THEN
  2297. result := ConvertValue(position, expression, system.integerType);
  2298. ELSIF (type IS SyntaxTree.SetType) THEN
  2299. set := Global.ConvertSet(set,system.SizeOf(type));
  2300. result := SyntaxTree.NewSetValue(expression.position,set);
  2301. result.SetType(type);
  2302. ELSE
  2303. Error(position, "set value cannot be converted");
  2304. result := SyntaxTree.invalidExpression;
  2305. END;
  2306. ELSIF IsStringValue(expression,string) THEN
  2307. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2308. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2309. result.SetType(type);
  2310. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2311. ELSE
  2312. Error(position, "string value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF IsCharacterValue(expression,char) THEN
  2316. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2317. string[0] := char; string[1] := 0X;
  2318. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2319. result := SyntaxTree.NewStringValue(expression.position,string);
  2320. result.SetType(type);
  2321. ELSIF (type IS SyntaxTree.ByteType) THEN
  2322. (* do not simply set the new type as this could invalidate types of constants *)
  2323. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2324. result.SetType(type)
  2325. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2326. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2327. result.SetType(type);
  2328. ELSIF (type IS SyntaxTree.SetType) THEN
  2329. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2330. result.SetType(type);
  2331. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2332. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2333. result.SetType(type);
  2334. ELSIF (type IS SyntaxTree.PortType) THEN
  2335. result := ConvertValue(position, expression, system.integerType);
  2336. ELSE
  2337. Error(position, "character value cannot be converted");
  2338. result := SyntaxTree.invalidExpression;
  2339. END;
  2340. ELSIF expression IS SyntaxTree.NilValue THEN
  2341. IF type IS SyntaxTree.AddressType THEN
  2342. result := SyntaxTree.NewIntegerValue(position,0);
  2343. result.SetType(type);
  2344. ELSE
  2345. result := expression;
  2346. END;
  2347. (* nothing to be done *)
  2348. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2349. result := MathArrayConversion(position, expression,type);
  2350. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2351. int := expression(SyntaxTree.EnumerationValue).value;
  2352. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2353. int := Global.ConvertSigned(int,system.SizeOf(type));
  2354. result := SyntaxTree.NewIntegerValue(position,int);
  2355. result.SetType(type);
  2356. ELSE
  2357. result := expression;
  2358. END;
  2359. (* nothing to be done *)
  2360. ELSE
  2361. Error(position, "expression cannot be converted");
  2362. IF VerboseErrorMessage THEN
  2363. Printout.Info("expression",expression);
  2364. Printout.Info("type",type);
  2365. END;
  2366. result := SyntaxTree.invalidExpression;
  2367. END;
  2368. RETURN result
  2369. END ConvertValue;
  2370. (**
  2371. return a conversion of an expression to a given type
  2372. - if expression is already of same type then return expression
  2373. - if incompatible conversion then report error and return invalidExpression
  2374. **)
  2375. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2376. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2377. BEGIN
  2378. type := type.resolved;
  2379. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2380. result := expression;
  2381. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2382. ELSIF expression = NIL THEN (* NIL expression *)
  2383. ELSIF expression.type = NIL THEN
  2384. Error(position, "expression of type NIL cannot be converted");
  2385. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2386. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2387. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2388. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2389. ELSIF expression.resolved # NIL THEN (* value *)
  2390. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2391. IF value IS SyntaxTree.Value THEN
  2392. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2393. result.SetResolved(value(SyntaxTree.Value));
  2394. result.SetType(value.type);
  2395. ELSE
  2396. result := value
  2397. END;
  2398. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2399. expressionList := SyntaxTree.NewExpressionList();
  2400. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2401. typeDeclaration.SetDeclaredType(type);
  2402. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2403. typeSymbol.SetType(typeDeclaration.type);
  2404. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2405. expressionList.AddExpression(expression);
  2406. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2407. result.SetType(type);
  2408. ELSIF IsArrayStructuredObjectType(type) THEN
  2409. (* no type can be converted to an array-structured object type *)
  2410. HALT(100)
  2411. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2412. IF inConversion>5 THEN
  2413. Error(expression.position,"recursive Conversion");
  2414. IF VerboseErrorMessage THEN
  2415. Printout.Info("expression",expression);
  2416. Printout.Info("type",type);
  2417. END;
  2418. END;
  2419. INC(inConversion);
  2420. IF inConversion < 10 THEN
  2421. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2422. result := MathArrayConversion(position, expression,type);
  2423. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2424. expression := ConvertToMathArray(expression);
  2425. type := MathArrayStructureOfType(type);
  2426. result := MathArrayConversion(position, expression, type)
  2427. ELSE
  2428. Error(expression.position,"cannot convert non array type to array type")
  2429. END;
  2430. END;
  2431. DEC(inConversion);
  2432. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2433. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2434. OR ~(type IS SyntaxTree.ArrayType) THEN
  2435. Error(expression.position,"cannot convert array type to non-array type")
  2436. END;
  2437. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2438. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2439. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2440. (*skip, no conversion*)
  2441. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2442. (* skip, no conversion *)
  2443. ELSE
  2444. ASSERT(~(type IS SyntaxTree.RangeType));
  2445. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2446. ASSERT(type # NIL);
  2447. END;
  2448. RETURN result
  2449. END NewConversion;
  2450. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2451. BEGIN
  2452. IF CompatibleTo(system,expression.type, type) THEN
  2453. RETURN NewConversion(position, expression, type, NIL);
  2454. ELSE
  2455. Error(expression.position, "incompatible expression");
  2456. RETURN SyntaxTree.invalidExpression
  2457. END;
  2458. END CompatibleConversion;
  2459. (**
  2460. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2461. **)
  2462. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2463. VAR leftType,rightType: SyntaxTree.Type;
  2464. BEGIN
  2465. IF left.type = NIL THEN Error(left.position,"no type")
  2466. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2467. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2468. (* error already handled *)
  2469. ELSE
  2470. leftType := left.type.resolved; rightType := right.type.resolved;
  2471. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2472. right := NewConversion(right.position, right, leftType, NIL);
  2473. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2474. left := NewConversion(left.position,left,rightType,NIL);
  2475. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2476. right := NewConversion(right.position, right, leftType, NIL);
  2477. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2478. left := NewConversion(left.position,left,rightType,NIL);
  2479. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2480. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2481. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2482. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2483. ELSIF
  2484. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2485. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2486. (* must be the case LONGREAL / COMPLEX ) *)
  2487. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2488. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2489. ELSE
  2490. Error(left.position,"incompatible operands");
  2491. END;
  2492. END;
  2493. END ConvertOperands;
  2494. (** find and return best operator matching to parameter list (nil, if none)
  2495. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2496. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2497. **)
  2498. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2499. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2500. identifier: SyntaxTree.Identifier;
  2501. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2502. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2503. BEGIN
  2504. operator := scope.firstOperator;
  2505. WHILE(operator # NIL) DO
  2506. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2507. procedureType := operator.type(SyntaxTree.ProcedureType);
  2508. distance := Distance(system, procedureType,actualParameters);
  2509. IF (distance < Infinity) THEN
  2510. IF returnType # NIL THEN
  2511. IF procedureType.returnType = NIL THEN
  2512. distance := Infinity
  2513. ELSE
  2514. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2515. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2516. END;
  2517. END;
  2518. END;
  2519. (*
  2520. IF distance < Infinity THEN
  2521. TRACE(distance, operator);
  2522. Printout.Info("potential operator",operator);
  2523. ELSE
  2524. Printout.Info("operator not possible",operator);
  2525. END;
  2526. *)
  2527. IF distance < bestDistance THEN
  2528. bestDistance := distance;
  2529. bestOperator := operator;
  2530. END;
  2531. END;
  2532. operator := operator.nextOperator;
  2533. END;
  2534. (*
  2535. Printout.Info("taken operator",bestOperator);
  2536. *)
  2537. END FindInScope;
  2538. BEGIN
  2539. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2540. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2541. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2542. import := currentScope.ownerModule.moduleScope.firstImport;
  2543. WHILE (bestDistance > 0) & (import # NIL) DO
  2544. IF import.module # NIL THEN
  2545. identifier := Global.GetIdentifier(operator,import.module.case);
  2546. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2547. END;
  2548. import := import.nextImport;
  2549. END;
  2550. RETURN bestOperator
  2551. END FindOperator;
  2552. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2553. BEGIN
  2554. currentScope := scope;
  2555. END SetCurrentScope;
  2556. (**
  2557. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2558. - handle LEN and DIM operator for array-structured object types
  2559. - find operator, if found then
  2560. - if in other module then add import designator
  2561. - create symbol designator for operator
  2562. - if error then return invalidExpression, if no operator then return NIL
  2563. **)
  2564. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2565. VAR
  2566. operator: SyntaxTree.Operator;
  2567. import: SyntaxTree.Import;
  2568. expression, result: SyntaxTree.Expression;
  2569. designator: SyntaxTree.Designator;
  2570. actualParameters, tempList: SyntaxTree.ExpressionList;
  2571. recordType: SyntaxTree.RecordType;
  2572. castReturnType : SyntaxTree.MathArrayType;
  2573. BEGIN
  2574. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2575. result := SyntaxTree.invalidExpression
  2576. ELSIF leftExpression = NIL THEN
  2577. result := NIL
  2578. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2579. (* LEN or DIM operator on array-structured object type *)
  2580. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2581. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2582. IF recordType.arrayAccessOperators.len = NIL THEN
  2583. Error(position, "call of undeclared LEN operator");
  2584. result := SyntaxTree.invalidExpression
  2585. ELSE
  2586. ASSERT(leftExpression IS SyntaxTree.Designator);
  2587. designator := leftExpression(SyntaxTree.Designator);
  2588. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2589. ASSERT(expression IS SyntaxTree.Designator);
  2590. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2591. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2592. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2593. result := designator
  2594. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2595. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2596. tempList := SyntaxTree.NewExpressionList();
  2597. tempList.AddExpression(rightExpression);
  2598. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2599. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2600. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2601. tempList := SyntaxTree.NewExpressionList();
  2602. tempList.AddExpression(designator);
  2603. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2604. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2605. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2606. END
  2607. END;
  2608. ELSE
  2609. IF ~complexNumbersImported THEN
  2610. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2611. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2612. THEN
  2613. (* operators on complex numbers *)
  2614. ImportModule(Global.ComplexNumbersName,position);
  2615. complexNumbersImported := TRUE;
  2616. END;
  2617. END;
  2618. (* import OCArrayBase if needed *)
  2619. IF ~arrayBaseImported THEN
  2620. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2621. IF op = Global.Dim THEN
  2622. (* not existing in OCArrayBase *)
  2623. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2624. (* not existing in OCArrayBase *)
  2625. ELSE
  2626. ImportModule(Global.ArrayBaseName,position);
  2627. arrayBaseImported := TRUE;
  2628. END
  2629. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2630. ImportModule(Global.ArrayBaseName,position);
  2631. arrayBaseImported := TRUE
  2632. END;
  2633. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2634. (* LEN(RANGE) *)
  2635. ImportModule(Global.ArrayBaseName,position);
  2636. arrayBaseImported := TRUE;
  2637. END;
  2638. END;
  2639. actualParameters := SyntaxTree.NewExpressionList();
  2640. actualParameters.AddExpression(leftExpression);
  2641. IF rightExpression # NIL THEN
  2642. actualParameters.AddExpression(rightExpression)
  2643. END;
  2644. operator := FindOperator(system,op,actualParameters,resultType);
  2645. IF operator # NIL THEN
  2646. designator := NIL;
  2647. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2648. import := currentScope.ownerModule.moduleScope.firstImport;
  2649. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2650. import := import.nextImport;
  2651. END;
  2652. expression := NewSymbolDesignator(position,NIL,import);
  2653. designator := expression(SyntaxTree.Designator);
  2654. END;
  2655. expression := NewSymbolDesignator(position,designator,operator);
  2656. designator := expression(SyntaxTree.Designator);
  2657. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2658. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2659. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2660. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2661. result.SetType(castReturnType);
  2662. END;
  2663. ELSE
  2664. result := NIL;
  2665. END;
  2666. END;
  2667. RETURN result
  2668. END NewOperatorCall;
  2669. (** check and resolve binary expression **)
  2670. (*! clean up *)
  2671. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2672. VAR left,right,result: SyntaxTree.Expression;
  2673. leftType, rightType: SyntaxTree.Type;
  2674. il,ir: Basic.Integer; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: Basic.Integer;bl,br: BOOLEAN; sl,sr: Basic.Set; strl,strr: Scanner.StringType;
  2675. cl,cr: CHAR;
  2676. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2677. type: SyntaxTree.Type;
  2678. value: SyntaxTree.Value;
  2679. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2680. integerConstantFolding: BOOLEAN;
  2681. list: SyntaxTree.ExpressionList;
  2682. PROCEDURE NewBool(v: BOOLEAN);
  2683. BEGIN
  2684. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2685. value.SetType(system.booleanType);
  2686. result.SetResolved(value);
  2687. type := system.booleanType
  2688. END NewBool;
  2689. PROCEDURE NewSet(v: Basic.Set);
  2690. BEGIN
  2691. value := Global.NewSetValue(system,binaryExpression.position,v);
  2692. result.SetResolved(value);
  2693. type := value.type;
  2694. END NewSet;
  2695. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2696. BEGIN
  2697. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2698. (* type cast to "larger" type only if the value is still in the range *)
  2699. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2700. value.SetType(t);
  2701. END;
  2702. result.SetResolved(value);
  2703. type := value.type;
  2704. END NewInteger;
  2705. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2706. BEGIN
  2707. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2708. value.SetType(t);
  2709. result.SetResolved(value);
  2710. type := t;
  2711. END NewReal;
  2712. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2713. BEGIN
  2714. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2715. value.SetType(t);
  2716. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2717. result.SetResolved(value);
  2718. type := t;
  2719. END NewComplex;
  2720. BEGIN
  2721. type := SyntaxTree.invalidType;
  2722. left := ResolveExpression(binaryExpression.left);
  2723. right := ResolveExpression(binaryExpression.right);
  2724. binaryExpression.SetLeft(left);
  2725. binaryExpression.SetRight(right);
  2726. result := binaryExpression;
  2727. operator := binaryExpression.operator;
  2728. IF ~system.operatorDefined[operator] THEN
  2729. Error(left.position,"Operator Not Defined");
  2730. result := SyntaxTree.invalidExpression;
  2731. RETURN
  2732. END;
  2733. IF left.type = NIL THEN
  2734. Error(left.position,"Expression has no result type");
  2735. result := SyntaxTree.invalidExpression;
  2736. RETURN;
  2737. END;
  2738. IF right.type = NIL THEN
  2739. Error(right.position,"Expression has no result type");
  2740. result := SyntaxTree.invalidExpression;
  2741. RETURN;
  2742. END;
  2743. leftType := left.type.resolved; rightType := right.type.resolved;
  2744. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2745. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2746. END;
  2747. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2748. list := SyntaxTree.NewExpressionList();
  2749. list.AddExpression(right);
  2750. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2751. END;
  2752. IF operatorCall # NIL THEN
  2753. result := operatorCall;
  2754. type := operatorCall.type;
  2755. (* admissible operators:
  2756. Times, Plus, Minus numeric numeric numeric
  2757. set set set
  2758. Slash numeric numeric real /complex
  2759. set set set
  2760. Div , Mod integer integer integer
  2761. And, Or bool bool bool
  2762. Equal, Unequal basic basic bool
  2763. pointer pointer bool
  2764. object object bool
  2765. record record bool
  2766. string string bool
  2767. enumerator enumerator bool
  2768. Less, LessEqual,
  2769. Greater, GreaterEqual integer/real integer/real bool
  2770. enumerator enumerator bool
  2771. In integer set bool
  2772. Is pointer type bool
  2773. object type bool
  2774. record type bool
  2775. Upto: special abbreviation for a..b
  2776. *)
  2777. ELSIF (left.type = NIL) THEN
  2778. Error(left.position,"type (left operand) = NIL in binary expression");
  2779. D.Str("nil type in "); D.Type(left); D.Ln;
  2780. result := SyntaxTree.invalidExpression;
  2781. ELSIF (right.type = NIL) THEN
  2782. Error(right.position,"type (right operand) = NIL in binary expression");
  2783. result := SyntaxTree.invalidExpression;
  2784. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2785. result := SyntaxTree.invalidExpression;
  2786. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2787. HALT(100);
  2788. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2789. type := system.booleanType;
  2790. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2791. Error(right.position,"is not a type ");
  2792. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2793. Error(binaryExpression.position,"is not a type extension of ");
  2794. IF VerboseErrorMessage THEN
  2795. Printout.Info("left",left);
  2796. Printout.Info("right",right);
  2797. END;
  2798. ELSIF IsUnsafePointer(left.type) THEN
  2799. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2800. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2801. NewBool(TRUE)
  2802. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2803. NewBool(TRUE);
  2804. ELSIF IsUnextensibleRecord(left) THEN
  2805. NewBool(FALSE)
  2806. END
  2807. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2808. Error(right.position,"must not be a type");
  2809. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2810. Error(left.position,"must not be a type");
  2811. ELSIF operator = Scanner.In THEN (* left IN right *)
  2812. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2813. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2814. NewBool(il IN sr);
  2815. ELSE
  2816. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2817. left := NewConversion(left.position, left, system.longintType,NIL);
  2818. binaryExpression.SetLeft(left)
  2819. END;
  2820. type := system.booleanType;
  2821. END
  2822. ELSE
  2823. Error(binaryExpression.position, "incompatible operands");
  2824. END
  2825. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2826. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2827. Error(binaryExpression.position,"incompatible operands");
  2828. END;
  2829. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2830. ELSE Error(binaryExpression.position,"operator not defined 1")
  2831. END
  2832. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2833. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2834. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2835. Error(binaryExpression.position,"incompatible operands");
  2836. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2837. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2838. left := NewConversion(left.position, left, system.addressType, NIL);
  2839. right := NewConversion(right.position, right, system.addressType, NIL);
  2840. binaryExpression.SetLeft(left);
  2841. binaryExpression.SetRight(right);
  2842. type := system.addressType;
  2843. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2844. ConvertOperands(left, right);
  2845. binaryExpression.SetLeft(left);
  2846. binaryExpression.SetRight(right);
  2847. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2848. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2849. END;
  2850. type := system.booleanType;
  2851. ELSE
  2852. Error(binaryExpression.position,"operator not defined 3");
  2853. END
  2854. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2855. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2856. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2857. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2858. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2859. CASE operator OF
  2860. |Scanner.Equal: NewBool(strl^=strr^);
  2861. |Scanner.Unequal:NewBool(strl^#strr^);
  2862. |Scanner.Less: NewBool(strl^<strr^);
  2863. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2864. |Scanner.Greater: NewBool(strl^>strr^);
  2865. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2866. ELSE
  2867. Error(binaryExpression.position,"operator not defined 4");
  2868. END;
  2869. END;
  2870. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2871. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2872. type := system.booleanType
  2873. ELSE
  2874. Error(binaryExpression.position,"operator not defined 5");
  2875. END;
  2876. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2877. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2878. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2879. type := system.booleanType;
  2880. ELSE
  2881. Error(binaryExpression.position,"operator not defined 6");
  2882. END
  2883. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2884. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2885. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2886. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2887. type := system.booleanType
  2888. ELSE
  2889. Error(binaryExpression.position,"operator not defined for enumerators");
  2890. END;
  2891. ELSE
  2892. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2893. END;
  2894. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2895. type := system.booleanType;
  2896. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2897. type := system.booleanType;
  2898. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2899. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2900. THEN
  2901. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2902. IF (leftType # rightType) THEN
  2903. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2904. ConvertOperands(left,right); (* operands must be of the same type here *)
  2905. END;
  2906. binaryExpression.SetLeft(left);
  2907. binaryExpression.SetRight(right);
  2908. leftType := left.type.resolved;
  2909. rightType := right.type.resolved;
  2910. END;
  2911. type := leftType;
  2912. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2913. Error(binaryExpression.position,"conversion failed ?");
  2914. IF VerboseErrorMessage THEN
  2915. Printout.Info("left",left);
  2916. Printout.Info("right",right);
  2917. END;
  2918. ELSIF IsIntegerType(leftType) THEN
  2919. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2920. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2921. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2922. Error(binaryExpression.position,"division by zero");
  2923. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2924. Error(binaryExpression.position,"integer division by negative number");
  2925. END;
  2926. END;
  2927. (* constant folding *)
  2928. (* bootstrap64
  2929. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2930. CASE operator OF
  2931. |Scanner.Plus: NewInteger(il+ir,left.type);
  2932. |Scanner.Minus: NewInteger(il-ir,left.type);
  2933. |Scanner.Times: NewInteger(il*ir,left.type);
  2934. |Scanner.Slash:
  2935. IF ir # 0 THEN
  2936. NewReal(il/ir, system.realType);
  2937. END;
  2938. |Scanner.Mod:
  2939. IF ir > 0 THEN
  2940. NewInteger(il MOD ir,left.type);
  2941. END;
  2942. |Scanner.Div:
  2943. IF ir > 0 THEN
  2944. NewInteger(il DIV ir,left.type);
  2945. END;
  2946. |Scanner.Equal: NewBool(il=ir);
  2947. |Scanner.Unequal:NewBool(il#ir);
  2948. |Scanner.Less: NewBool(il<ir);
  2949. |Scanner.LessEqual: NewBool(il<=ir);
  2950. |Scanner.Greater: NewBool(il>ir);
  2951. |Scanner.GreaterEqual: NewBool(il>=ir);
  2952. ELSE Error(binaryExpression.position,"operator not defined 7");
  2953. END;
  2954. ELS*)
  2955. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2956. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2957. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2958. CASE operator OF
  2959. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2960. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2961. |Scanner.Times: NewInteger(hl*hr,left.type);
  2962. |Scanner.Slash:
  2963. IF hr = 0 THEN
  2964. Error(binaryExpression.position,"division by zero");
  2965. ELSE
  2966. IF type.sizeInBits = 64 THEN
  2967. NewReal(hl/hr,system.longrealType);
  2968. ELSE
  2969. NewReal(hl/hr,system.realType)
  2970. END
  2971. END;
  2972. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2973. |Scanner.Mod:
  2974. IF hr = 0 THEN
  2975. Error(binaryExpression.position,"division by zero");
  2976. ELSE
  2977. NewInteger(hl MOD hr, left.type);
  2978. (* bootstrap64
  2979. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2980. *)
  2981. END;
  2982. |Scanner.Div:
  2983. IF hr = 0 THEN
  2984. Error(binaryExpression.position,"division by zero");
  2985. ELSE
  2986. NewInteger(hl DIV hr, left.type);
  2987. (* bootstrap64
  2988. NewInteger(Machine.DivH(hl,hr),left.type);
  2989. *)
  2990. END;
  2991. (* *)
  2992. |Scanner.Equal: NewBool(hl=hr);
  2993. |Scanner.Unequal: NewBool(hl#hr);
  2994. |Scanner.Less: NewBool(hl<hr);
  2995. |Scanner.LessEqual: NewBool(hl<=hr);
  2996. |Scanner.Greater: NewBool(hl>hr);
  2997. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2998. ELSE Error(binaryExpression.position,"operator not defined 8");
  2999. END;
  3000. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3001. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3002. type := left.type
  3003. ELSIF (operator = Scanner.Slash) THEN
  3004. left := NewConversion(left.position,left,system.realType,NIL);
  3005. right := NewConversion(right.position,right,system.realType,NIL);
  3006. binaryExpression.SetLeft(left);
  3007. binaryExpression.SetRight(right);
  3008. type := system.realType
  3009. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3010. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3011. type := system.booleanType
  3012. ELSE
  3013. Error(binaryExpression.position,"operator not defined 9");
  3014. END;
  3015. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3016. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3017. CASE operator OF
  3018. |Scanner.Plus: NewReal(rl+rr,leftType);
  3019. |Scanner.Minus: NewReal(rl-rr,leftType);
  3020. |Scanner.Times:NewReal(rl*rr,leftType);
  3021. |Scanner.Slash:
  3022. IF rr = 0 THEN
  3023. Error(binaryExpression.position,"division by zero");
  3024. ELSE
  3025. NewReal(rl/rr,leftType);
  3026. END
  3027. |Scanner.Equal: NewBool(rl=rr);
  3028. |Scanner.Unequal: NewBool(rl#rr);
  3029. |Scanner.Less: NewBool(rl<rr);
  3030. |Scanner.LessEqual: NewBool(rl<=rr);
  3031. |Scanner.Greater: NewBool(rl>rr);
  3032. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3033. ELSE Error(binaryExpression.position,"operator not defined 10");
  3034. END;
  3035. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3036. type := left.type
  3037. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3038. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3039. type := system.booleanType
  3040. ELSE
  3041. Error(binaryExpression.position,"operator not defined 11");
  3042. IF VerboseErrorMessage THEN
  3043. Printout.Info("left",left);
  3044. Printout.Info("right",right);
  3045. END;
  3046. END;
  3047. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3048. CASE operator OF
  3049. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3050. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3051. ELSE
  3052. Error(binaryExpression.position,"operator not defined");
  3053. IF VerboseErrorMessage THEN
  3054. Printout.Info("left", left);
  3055. Printout.Info("right", right)
  3056. END;
  3057. END;
  3058. IF ~error THEN
  3059. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3060. Error(binaryExpression.position,"division by zero")
  3061. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3062. (* do constant folding *)
  3063. CASE operator OF
  3064. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3065. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3066. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3067. |Scanner.Slash:
  3068. divisor := c * c + d * d;
  3069. ASSERT(divisor # 0);
  3070. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3071. |Scanner.Equal: NewBool((a = c) & (b = d))
  3072. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3073. END
  3074. END
  3075. END
  3076. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3077. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3078. CASE operator OF
  3079. |Scanner.And: NewBool(bl & br);
  3080. |Scanner.Or: NewBool(bl OR br);
  3081. |Scanner.Equal: NewBool(bl = br);
  3082. |Scanner.Unequal: NewBool(bl # br);
  3083. ELSE Error(binaryExpression.position,"operator not defined 12");
  3084. END;
  3085. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3086. type := system.booleanType
  3087. ELSE
  3088. Error(binaryExpression.position,"operator not defined 13");
  3089. END;
  3090. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3091. (* constant folding *)
  3092. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3093. IF operator = Scanner.Equal THEN
  3094. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3095. ELSIF operator = Scanner.Unequal THEN
  3096. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3097. END;
  3098. END;
  3099. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3100. type := system.booleanType;
  3101. ELSE
  3102. Error(binaryExpression.position, "operator not defined");
  3103. END;
  3104. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3105. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3106. CASE operator OF
  3107. |Scanner.Plus: NewSet(sl + sr);
  3108. |Scanner.Minus: NewSet(sl - sr);
  3109. |Scanner.Times: NewSet(sl * sr);
  3110. |Scanner.Slash: NewSet(sl / sr);
  3111. |Scanner.Equal: NewBool(sl=sr);
  3112. |Scanner.Unequal: NewBool(sl#sr);
  3113. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3114. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3115. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3116. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3117. ELSE Error(binaryExpression.position,"operator not defined 14");
  3118. END;
  3119. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3120. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3121. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3122. THEN
  3123. type := system.booleanType
  3124. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3125. type := left.type
  3126. ELSE
  3127. Error(binaryExpression.position,"operator not defined 15");
  3128. END;
  3129. ELSIF IsCharacterType(left.type) THEN
  3130. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3131. CASE operator OF
  3132. |Scanner.Equal: NewBool(cl=cr);
  3133. |Scanner.Unequal: NewBool(cl#cr);
  3134. |Scanner.Less: NewBool(cl<cr);
  3135. |Scanner.LessEqual: NewBool(cl<=cr);
  3136. |Scanner.Greater: NewBool(cl>cr);
  3137. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3138. ELSE Error(binaryExpression.position,"operator not defined 16");
  3139. END;
  3140. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3141. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3142. type := system.booleanType
  3143. ELSE
  3144. Error(binaryExpression.position,"operator not defined 17");
  3145. END;
  3146. ELSE
  3147. Error(binaryExpression.position,"operator not defined 18");
  3148. END;
  3149. ELSE
  3150. Error(binaryExpression.position,"operator not defined 19");
  3151. END;
  3152. IF type = SyntaxTree.invalidType THEN
  3153. result := SyntaxTree.invalidExpression
  3154. ELSE
  3155. result.SetType(type)
  3156. END;
  3157. resolvedExpression := result
  3158. END VisitBinaryExpression;
  3159. (** resolve a range expression of the from <<first .. last BY step>>
  3160. - depending on the context different things are checked:
  3161. ArrayIndex:
  3162. - components must be integers
  3163. - replace missing lower bound with 0
  3164. - replace missing upper bound with MAX(LONGINT)
  3165. - replace missing step size with 1
  3166. SetElement:
  3167. - components must be integers
  3168. - replace missing lower bound with 0
  3169. - replace missing upper bound with MAX(SET)
  3170. - must not have step size
  3171. CaseGuard:
  3172. - components must be constant
  3173. - components must be integers or characters
  3174. - must have lower and upper bound present
  3175. - components are made compatible
  3176. - must not have step size
  3177. - if error: return invalidExpression
  3178. **)
  3179. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3180. VAR
  3181. hasError: BOOLEAN;
  3182. first, last, step: SyntaxTree.Expression;
  3183. BEGIN
  3184. hasError := FALSE;
  3185. first := x.first;
  3186. last := x.last;
  3187. step := x.step;
  3188. (* check lower bound *)
  3189. IF x.context = SyntaxTree.CaseGuard THEN
  3190. IF first = NIL THEN
  3191. Error(x.position, "missing lower bound");
  3192. hasError := TRUE
  3193. ELSE
  3194. first := ResolveExpression(first);
  3195. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3196. Error(first.position, "lower bound not integer or character");
  3197. hasError := TRUE
  3198. ELSE
  3199. IF first IS SyntaxTree.StringValue THEN
  3200. (* add conversion from string to character *)
  3201. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3202. END
  3203. END;
  3204. (* check if expression is constant *)
  3205. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3206. (* error already reported *)
  3207. hasError := TRUE
  3208. END
  3209. END
  3210. ELSE (* ArrayIndex, SetElement *)
  3211. IF first = NIL THEN
  3212. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3213. END;
  3214. first := ResolveExpression(first);
  3215. IF IsIntegerType(first.type.resolved) THEN
  3216. first := NewConversion(first.position, first, system.lenType, NIL)
  3217. ELSE
  3218. Error(first.position, "lower bound not integer");
  3219. hasError := TRUE
  3220. END
  3221. END;
  3222. (* check upper bound *)
  3223. IF x.context = SyntaxTree.CaseGuard THEN
  3224. IF last = NIL THEN
  3225. Error(x.position, "missing upper bound");
  3226. hasError := TRUE
  3227. ELSE
  3228. last := ResolveExpression(last);
  3229. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3230. Error(last.position, "lower bound not integer or character");
  3231. hasError := TRUE
  3232. ELSE
  3233. IF last IS SyntaxTree.StringValue THEN
  3234. (* add conversion from string to character *)
  3235. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3236. END
  3237. END;
  3238. (* check if expression is constant *)
  3239. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3240. (* error already reported *)
  3241. hasError := TRUE
  3242. ELSE
  3243. (* try to make lower and upper bound compatible *)
  3244. ConvertOperands(first, last);
  3245. IF first.type.resolved # last.type.resolved THEN
  3246. Error(x.position, "lower and upper bounds incompatible");
  3247. hasError := TRUE
  3248. END
  3249. END
  3250. END
  3251. ELSE (* ArrayIndex, SetElement *)
  3252. IF last = NIL THEN
  3253. IF x.context = SyntaxTree.ArrayIndex THEN
  3254. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3255. ELSE
  3256. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3257. END
  3258. END;
  3259. last := ResolveExpression(last);
  3260. IF IsIntegerType(last.type.resolved) THEN
  3261. last := NewConversion(last.position, last, system.lenType, NIL)
  3262. ELSE
  3263. Error(last.position, "upper bound not integer");
  3264. hasError := TRUE
  3265. END
  3266. END;
  3267. (* check step size *)
  3268. IF x.context = SyntaxTree.ArrayIndex THEN
  3269. IF step = NIL THEN
  3270. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3271. END;
  3272. step := ResolveExpression(step);
  3273. IF IsIntegerType(step.type.resolved) THEN
  3274. step := NewConversion(step.position, step, system.lenType, NIL)
  3275. ELSE
  3276. Error(step.position, "step size not integer");
  3277. hasError := TRUE
  3278. END
  3279. ELSE (* SetElement, CaseGuard *)
  3280. IF step # NIL THEN
  3281. Error(last.position, "step size not allowed in this context");
  3282. hasError := TRUE
  3283. END
  3284. END;
  3285. IF hasError THEN
  3286. resolvedExpression := SyntaxTree.invalidExpression
  3287. ELSE
  3288. x.SetFirst(first);
  3289. x.SetLast(last);
  3290. x.SetStep(step);
  3291. x.SetType(system.rangeType);
  3292. resolvedExpression := x;
  3293. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3294. END
  3295. END VisitRangeExpression;
  3296. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3297. BEGIN
  3298. x.SetType(NIL);
  3299. resolvedExpression := x;
  3300. END VisitTensorRangeExpression;
  3301. (** resolve the expression d and return result as designator
  3302. - resolve expression
  3303. - if expression is a designator then return designator else error message and return invalidDesignator
  3304. **)
  3305. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3306. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3307. BEGIN
  3308. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3309. resolved := ResolveExpression(d);
  3310. IF resolved = SyntaxTree.invalidExpression THEN
  3311. (* error should already have been reported *)
  3312. result := SyntaxTree.invalidDesignator;
  3313. ELSIF resolved IS SyntaxTree.Designator THEN
  3314. result := resolved(SyntaxTree.Designator);
  3315. ELSE
  3316. Error(d.position,"is no designator ! ");
  3317. result := SyntaxTree.invalidDesignator;
  3318. END;
  3319. (* result.type might be nil. *)
  3320. RETURN result
  3321. END ResolveDesignator;
  3322. (**
  3323. symbol designator generated in this module
  3324. nothing to be resolved
  3325. **)
  3326. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3327. BEGIN
  3328. resolvedExpression := x;
  3329. END VisitSymbolDesignator;
  3330. (**
  3331. self designator generated in this module
  3332. nothing to be resolved
  3333. **)
  3334. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3335. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3336. BEGIN
  3337. (* check if in record scope *)
  3338. scope := currentScope;
  3339. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3340. scope := scope.outerScope;
  3341. END;
  3342. IF scope = NIL THEN (* in module scope *)
  3343. x.SetType(system.anyType);
  3344. ELSIF scope IS SyntaxTree.CellScope THEN
  3345. cell := scope(SyntaxTree.CellScope).ownerCell;
  3346. x.SetType(cell);
  3347. ELSE (* in record scope *)
  3348. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3349. IF (record # NIL) & (record.pointerType # NIL) THEN
  3350. type := ResolveType(record.pointerType);
  3351. x.SetType(type);
  3352. ELSE
  3353. x.SetType(record);
  3354. END;
  3355. END;
  3356. resolvedExpression := x;
  3357. END VisitSelfDesignator;
  3358. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3359. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3360. BEGIN
  3361. scope := currentScope;
  3362. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3363. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3364. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3365. returnType := procedureType.returnType;
  3366. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3367. THEN
  3368. x.SetType(returnType);
  3369. ELSE
  3370. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3371. x.SetType(SyntaxTree.invalidType);
  3372. END;
  3373. ELSE
  3374. Error(x.position,"forbidden access to result designator");
  3375. x.SetType(SyntaxTree.invalidType);
  3376. END;
  3377. x.SetAssignable(TRUE);
  3378. resolvedExpression := x;
  3379. END VisitResultDesignator;
  3380. (**
  3381. return symbol designator as an expression
  3382. - if symbol is a constant then return the constant value expression
  3383. - else
  3384. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3385. - if symbol is a guarded variable then return a TypeGuardDesignator
  3386. - else return a symbol designator
  3387. **)
  3388. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3389. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3390. guardType: SyntaxTree.Type;
  3391. BEGIN
  3392. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3393. result := SyntaxTree.invalidExpression;
  3394. ASSERT(symbol # NIL);
  3395. (*
  3396. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3397. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3398. Error(position, "type not allowed here");
  3399. ELS *)
  3400. (* not needed any more as values are stored in the expression
  3401. IF symbol IS SyntaxTree.Constant THEN
  3402. result := symbol(SyntaxTree.Constant).value
  3403. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3404. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3405. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3406. ELSE
  3407. result := symbol(SyntaxTree.Constant).value
  3408. END;
  3409. ELSE
  3410. *)
  3411. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3412. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3413. THEN
  3414. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3415. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3416. left := NewDereferenceDesignator(position,left);
  3417. left.SetHidden(TRUE);
  3418. END;
  3419. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3420. scope := currentScope;
  3421. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3422. scope := scope.outerScope;
  3423. END;
  3424. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3425. Error(position, "forbidden access to symbol in parent procedure scope");
  3426. END;
  3427. END;
  3428. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3429. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3430. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3431. ELSE
  3432. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3433. END;
  3434. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3435. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3436. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3437. result.SetType(symbol.type);
  3438. result.SetAssignable(assignable);
  3439. symbol.MarkUsed;
  3440. IF symbol IS SyntaxTree.Constant THEN
  3441. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3442. END;
  3443. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3444. variableAccessed := TRUE
  3445. END;
  3446. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3447. IF GetGuard(symbol,guardType) THEN
  3448. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3449. END;
  3450. END;
  3451. ASSERT(result.type # NIL);
  3452. RETURN result
  3453. END NewSymbolDesignator;
  3454. (** check and resolve an identifier designator "identifier"
  3455. - if identifier = self then return SelfDesignator
  3456. - else find symbol in current scope
  3457. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3458. **)
  3459. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3460. VAR symbol: SyntaxTree.Symbol;
  3461. BEGIN
  3462. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3463. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3464. IF symbol # NIL THEN
  3465. ResolveSymbol(symbol);
  3466. ASSERT(symbol.type # NIL);
  3467. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3468. ELSE
  3469. Error(identifierDesignator.position,"Undeclared Identifier");
  3470. IF VerboseErrorMessage THEN
  3471. Printout.Info("undeclared identifier designator",identifierDesignator);
  3472. END;
  3473. resolvedExpression := SyntaxTree.invalidDesignator;
  3474. END;
  3475. END VisitIdentifierDesignator;
  3476. (** check and resolve a selector designator of the form left.designator
  3477. - if left is a pointer type then do auto dereferenciation
  3478. - left denotes a search scope:
  3479. - if left type is import type then set search scope to respective module
  3480. - if left type is enumeration type then set search scope to respective enumeration scope
  3481. - elsif left type is record type then set search scope to record scope
  3482. - search symbol in computed scope
  3483. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3484. **)
  3485. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3486. VAR
  3487. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3488. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3489. BEGIN
  3490. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3491. left := ResolveDesignator(selectorDesignator.left);
  3492. result := SyntaxTree.invalidDesignator;
  3493. IF left # NIL THEN
  3494. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3495. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3496. END;
  3497. scope := NIL;
  3498. IF left.type = NIL THEN
  3499. Error(selectorDesignator.position,"field on nil typed designator");
  3500. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3501. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3502. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3503. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3504. module := symbol(SyntaxTree.Import).module;
  3505. IF module # NIL THEN
  3506. scope := module.moduleScope
  3507. ELSE
  3508. Error(left.position,"module not loaded");
  3509. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3510. END;
  3511. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3512. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3513. ASSERT(scope # NIL)
  3514. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3515. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3516. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3517. IF type IS SyntaxTree.EnumerationType THEN
  3518. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3519. ELSE
  3520. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3521. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3522. END;
  3523. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3524. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3525. ELSE
  3526. Error(selectorDesignator.position,"field on non-record type designator");
  3527. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3528. END;
  3529. symbol := NIL;
  3530. IF scope # NIL THEN
  3531. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3532. IF symbol # NIL THEN
  3533. ResolveSymbol(symbol);
  3534. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3535. symbol.MarkUsed
  3536. ELSE
  3537. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3538. IF VerboseErrorMessage THEN
  3539. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3540. Printout.Info("scope", scope);
  3541. Printout.Info("left", left);
  3542. Printout.Info("undeclared identifier",selectorDesignator);
  3543. Printout.Info("left resolved designator",left);
  3544. END
  3545. END;
  3546. END;
  3547. END;
  3548. resolvedExpression := result;
  3549. END VisitSelectorDesignator;
  3550. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3551. VAR len,idx: Basic.Integer;
  3552. BEGIN
  3553. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3554. IF idx < 0 THEN
  3555. Error(index.position,"index out of bounds (too small)")
  3556. ELSE
  3557. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3558. Error(index.position,"index out of bounds (too large)");
  3559. END;
  3560. END;
  3561. END;
  3562. END IndexCheck;
  3563. (*
  3564. - if index designator has not type, use newBaseType as its type
  3565. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3566. - special rule: if static array of dynamic array occurs, make it all dynamic
  3567. index designator type: new base type: new index designator type:
  3568. NIL z z
  3569. ARRAY [x, y] z ARRAY [x, y] OF z
  3570. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3571. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3572. *)
  3573. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3574. VAR
  3575. mathArrayType: SyntaxTree.MathArrayType;
  3576. makeDynamic: BOOLEAN;
  3577. BEGIN
  3578. IF indexDesignator.type = NIL THEN
  3579. indexDesignator.SetType(newBaseType)
  3580. ELSE
  3581. (* index designator must be a of math array type *)
  3582. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3583. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3584. (* determine if all arrays have to be made dynamic *)
  3585. makeDynamic :=
  3586. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3587. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3588. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3589. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3590. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3591. END;
  3592. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3593. mathArrayType.SetArrayBase(newBaseType)
  3594. END
  3595. END SetIndexBaseType;
  3596. (** check and append index list element to index designator of math array
  3597. - check validity of single index or array range
  3598. - compute new type
  3599. - if range then create new array type (calculate length of resulting array)
  3600. - otherwise take sourceArray.arrayBase as new type
  3601. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3602. **)
  3603. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3604. VAR
  3605. targetArray: SyntaxTree.MathArrayType;
  3606. first, last, step: SyntaxTree.Expression;
  3607. firstValue, lastValue, stepValue, length: Basic.Integer;
  3608. rangeExpression: SyntaxTree.RangeExpression;
  3609. BEGIN
  3610. IF indexListItem.type = SyntaxTree.invalidType THEN
  3611. (* error already handled *)
  3612. indexDesignator.parameters.AddExpression(indexListItem)
  3613. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3614. indexDesignator.HasRange;
  3615. indexDesignator.HasTensorRange;
  3616. indexDesignator.parameters.AddExpression(indexListItem);
  3617. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3618. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3619. IndexCheck(indexListItem, sourceArray.length);
  3620. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3621. indexDesignator.parameters.AddExpression(indexListItem)
  3622. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3623. indexDesignator.HasRange;
  3624. (* if the range is given as an array range expression, check the validity of its components *)
  3625. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3626. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3627. first := rangeExpression.first;
  3628. last := rangeExpression.last;
  3629. step := rangeExpression.step;
  3630. (* perform static checks on range components *)
  3631. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3632. Error(indexListItem.position,"lower bound of array range too small")
  3633. END;
  3634. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3635. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3636. Error(indexListItem.position,"upper bound of array range too large")
  3637. END
  3638. END;
  3639. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3640. Error(indexListItem.position,"invalid step size")
  3641. END;
  3642. (* add conversions to size type *)
  3643. (* TODO: needed? *)
  3644. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3645. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3646. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3647. END;
  3648. IF indexDesignator.hasTensorRange THEN
  3649. (* the index designator's base type is a tensor: leave it as is *)
  3650. ELSE
  3651. (* append a new math array to the index designator's base type *)
  3652. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3653. IF ~error THEN
  3654. (*
  3655. (* optimization: calculate length of target array for static ranges *)
  3656. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3657. IF IsStaticallyOpenRange(rangeExpression) THEN
  3658. (* range is open ('*'): reuse source array length as target array length *)
  3659. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3660. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3661. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3662. IF lastValue = MAX(LONGINT) THEN
  3663. IF IsIntegerValue(sourceArray.length, length) THEN
  3664. lastValue := length - 1;
  3665. isStaticTargetArrayLength := TRUE
  3666. ELSE
  3667. isStaticTargetArrayLength := FALSE
  3668. END
  3669. ELSE
  3670. isStaticTargetArrayLength := TRUE
  3671. END;
  3672. IF isStaticTargetArrayLength THEN
  3673. (* calculate static target array length *)
  3674. IF firstValue > lastValue THEN
  3675. length := 0
  3676. ELSE
  3677. length := 1 + lastValue - firstValue;
  3678. IF length MOD stepValue = 0 THEN
  3679. length := length DIV stepValue
  3680. ELSE
  3681. length := length DIV stepValue + 1
  3682. END
  3683. END;
  3684. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3685. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3686. ASSERT(targetArray.form = SyntaxTree.Static)
  3687. END
  3688. END
  3689. END
  3690. *)
  3691. END;
  3692. SetIndexBaseType(indexDesignator, targetArray)
  3693. END;
  3694. indexDesignator.parameters.AddExpression(indexListItem)
  3695. ELSE
  3696. Error(position,"invalid index list item");
  3697. END;
  3698. END AppendMathIndex;
  3699. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3700. VAR parameters: SyntaxTree.ExpressionList;
  3701. BEGIN
  3702. parameters := index.parameters;
  3703. IF (expression.type = NIL) THEN
  3704. Error(position, "invalid index");
  3705. ELSIF IsIntegerType(expression.type.resolved) THEN
  3706. IF over IS SyntaxTree.ArrayType THEN
  3707. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3708. ELSIF over IS SyntaxTree.StringType THEN
  3709. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3710. END;
  3711. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3712. parameters.AddExpression(expression);
  3713. ELSE
  3714. Error(position, "invalid index");
  3715. END;
  3716. END AppendIndex;
  3717. (** convert an expression to math array type
  3718. if expression is of math array type: return expression itself
  3719. if expression is of array-structured object type: return an index operator call on it
  3720. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3721. otherwise: return invalid expression
  3722. **)
  3723. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3724. VAR
  3725. result: SyntaxTree.Expression;
  3726. mathArrayType: SyntaxTree.MathArrayType;
  3727. BEGIN
  3728. IF expression.type = NIL THEN
  3729. result := SyntaxTree.invalidExpression
  3730. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3731. (* expression of math array type *)
  3732. result := expression
  3733. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3734. (* expression of array-structured object type *)
  3735. mathArrayType := MathArrayStructureOfType(expression.type);
  3736. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3737. ELSE
  3738. result := SyntaxTree.invalidExpression
  3739. END;
  3740. RETURN result
  3741. END ConvertToMathArray;
  3742. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3743. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3744. VAR
  3745. result: SyntaxTree.ExpressionList;
  3746. i: LONGINT;
  3747. BEGIN
  3748. result := SyntaxTree.NewExpressionList();
  3749. FOR i := 1 TO itemCount DO
  3750. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3751. END;
  3752. RETURN result
  3753. END ListOfOpenRanges;
  3754. (** create a procedure call designator for an index operator call on an array-structured object type
  3755. - use given index list as actual parameters
  3756. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3757. **)
  3758. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3759. VAR
  3760. operator: SyntaxTree.Operator;
  3761. expression: SyntaxTree.Expression;
  3762. actualParameters, tempList: SyntaxTree.ExpressionList;
  3763. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3764. result, tempDesignator: SyntaxTree.Designator;
  3765. recordType: SyntaxTree.RecordType;
  3766. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3767. i, hashValue, indexListSize, indexListKind: LONGINT;
  3768. castReturnType: SyntaxTree.MathArrayType;
  3769. BEGIN
  3770. ASSERT(IsArrayStructuredObjectType(left.type));
  3771. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3772. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3773. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3774. indexListSize := indexList.Length();
  3775. indexListKind := 0;
  3776. containsNonRange := FALSE;
  3777. FOR i := 0 TO indexList.Length() - 1 DO
  3778. indexListKind := indexListKind * 2;
  3779. expression := indexList.GetExpression(i);
  3780. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3781. INC(indexListKind)
  3782. ELSE
  3783. containsNonRange := TRUE
  3784. END
  3785. END;
  3786. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3787. (* select applicable index operator
  3788. - try to look up optimal index operator
  3789. - if not present, use operator on ranges
  3790. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3791. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3792. *)
  3793. usesGeneralOperator := FALSE;
  3794. IF rhs # NIL THEN
  3795. (* write operator *)
  3796. IF hashValue = -1 THEN
  3797. operator := NIL
  3798. ELSE
  3799. operator := recordType.arrayAccessOperators.write[hashValue];
  3800. END;
  3801. IF operator = NIL THEN
  3802. usesPureRangeOperator := TRUE;
  3803. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3804. operator := recordType.arrayAccessOperators.generalWrite;
  3805. usesGeneralOperator := TRUE
  3806. ELSE
  3807. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3808. operator := recordType.arrayAccessOperators.write[hashValue];
  3809. END
  3810. END
  3811. ELSE
  3812. (* read operator *)
  3813. IF hashValue = -1 THEN
  3814. operator := NIL
  3815. ELSE
  3816. operator := recordType.arrayAccessOperators.read[hashValue];
  3817. END;
  3818. IF operator = NIL THEN
  3819. usesPureRangeOperator := TRUE;
  3820. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3821. operator := recordType.arrayAccessOperators.generalRead;
  3822. usesGeneralOperator := TRUE
  3823. ELSE
  3824. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3825. operator := recordType.arrayAccessOperators.read[hashValue];
  3826. END
  3827. END
  3828. END;
  3829. IF operator = NIL THEN
  3830. Error(position, "call of undeclared [] operator");
  3831. result := SyntaxTree.invalidDesignator;
  3832. ELSE
  3833. (* determine if reshaping is needed *)
  3834. needsReshaping := containsNonRange & usesPureRangeOperator;
  3835. (* import OCArrayBase if reshaping is needed *)
  3836. IF needsReshaping & ~arrayBaseImported THEN
  3837. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3838. arrayBaseImported := TRUE
  3839. END;
  3840. (* add the index list item to the list of actual parameters
  3841. - for general operators: add a single inline array containing the index list items as parameter
  3842. - otherwise: add all index list items as individual parameters
  3843. *)
  3844. actualParameters := SyntaxTree.NewExpressionList();
  3845. IF usesGeneralOperator THEN
  3846. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3847. END;
  3848. FOR i := 0 TO indexListSize - 1 DO
  3849. expression := indexList.GetExpression(i);
  3850. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3851. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3852. tempList := SyntaxTree.NewExpressionList();
  3853. tempList.AddExpression(expression);
  3854. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3855. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3856. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3857. END;
  3858. IF usesGeneralOperator THEN
  3859. tempMathArrayExpression.elements.AddExpression(expression);
  3860. ELSE
  3861. actualParameters.AddExpression(expression)
  3862. END
  3863. END;
  3864. IF usesGeneralOperator THEN
  3865. actualParameters.AddExpression(tempMathArrayExpression)
  3866. END;
  3867. IF rhs # NIL THEN
  3868. (* add actual parameter for RHS *)
  3869. IF needsReshaping THEN
  3870. (* reshape using OCArrayBase.ExpandDimensions *)
  3871. tempList := SyntaxTree.NewExpressionList();
  3872. (* source array *)
  3873. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3874. tempList.AddExpression(rhs);
  3875. ELSE
  3876. (* convert scalar to one-dimensional array *)
  3877. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3878. tempMathArrayExpression.elements.AddExpression(rhs);
  3879. tempList.AddExpression(tempMathArrayExpression)
  3880. END;
  3881. (* list of kept dimensions *)
  3882. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3883. FOR i := 0 TO indexListSize - 1 DO
  3884. expression := indexList.GetExpression(i);
  3885. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3886. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3887. ELSE
  3888. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3889. END
  3890. END;
  3891. tempList.AddExpression(tempMathArrayExpression);
  3892. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3893. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3894. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3895. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3896. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3897. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3898. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3899. expression.SetType(castReturnType);
  3900. ELSE
  3901. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3902. END;
  3903. actualParameters.AddExpression(expression)
  3904. ELSE
  3905. actualParameters.AddExpression(rhs)
  3906. END
  3907. END;
  3908. (* add dereference operator and create procedure call designator *)
  3909. ASSERT(left IS SyntaxTree.Designator);
  3910. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3911. ASSERT(expression IS SyntaxTree.Designator);
  3912. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3913. IF (rhs = NIL) & needsReshaping THEN
  3914. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3915. tempList := SyntaxTree.NewExpressionList();
  3916. FOR i := 0 TO indexList.Length() - 1 DO
  3917. expression := indexList.GetExpression(i);
  3918. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3919. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3920. ELSE
  3921. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3922. END
  3923. END;
  3924. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3925. END;
  3926. IF rhs = NIL THEN
  3927. (* special rule: index read operator calls are considered to be assignable *)
  3928. result.SetAssignable(TRUE)
  3929. END;
  3930. (* put information about this index operator call into the resulting designator *)
  3931. result.SetRelatedAsot(left);
  3932. result.SetRelatedIndexList(indexList)
  3933. END;
  3934. RETURN result
  3935. END NewIndexOperatorCall;
  3936. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3937. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3938. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3939. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3940. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3941. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3942. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3943. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3944. CONST trace = FALSE;
  3945. BEGIN
  3946. IF trace THEN
  3947. FOR i := 0 TO actualParameters.Length()-1 DO
  3948. Printout.Info("par", actualParameters.GetExpression(i));
  3949. END;
  3950. END;
  3951. operator := scope.firstOperator;
  3952. WHILE(operator # NIL) DO
  3953. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3954. procedureType := operator.type(SyntaxTree.ProcedureType);
  3955. distance := Distance(system, procedureType,actualParameters);
  3956. IF trace THEN Printout.Info("check op ",operator) END;
  3957. IF distance < bestDistance THEN
  3958. IF trace THEN Printout.Info("taken op",operator) END;
  3959. bestDistance := distance;
  3960. bestOperator := operator;
  3961. END;
  3962. END;
  3963. operator := operator.nextOperator;
  3964. END;
  3965. END FindInScope;
  3966. BEGIN
  3967. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3968. IF oper = 0 THEN (* index *)
  3969. identifier := SyntaxTree.NewIdentifier("[]");
  3970. ELSE
  3971. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3972. END;
  3973. WHILE (recordType # NIL) DO
  3974. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3975. recordType := recordType.GetBaseRecord();
  3976. END;
  3977. RETURN bestOperator
  3978. END FindOperator;
  3979. BEGIN
  3980. type := left.type.resolved;
  3981. IF type IS SyntaxTree.RecordType THEN
  3982. pointer := FALSE;
  3983. recordType := type(SyntaxTree.RecordType);
  3984. ELSE
  3985. pointer := TRUE;
  3986. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3987. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3988. END;
  3989. actualParameters := SyntaxTree.NewExpressionList();
  3990. IF parameters # NIL THEN
  3991. FOR i := 0 TO parameters.Length()-1 DO
  3992. expression := ResolveExpression(parameters.GetExpression(i));
  3993. actualParameters.AddExpression(expression);
  3994. END;
  3995. END;
  3996. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3997. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3998. IF op # NIL THEN
  3999. designator := left(SyntaxTree.Designator);
  4000. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  4001. expression := NewSymbolDesignator(position, designator , op);
  4002. ASSERT(expression IS SyntaxTree.Designator);
  4003. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4004. result.SetRelatedAsot(left);
  4005. result.SetRelatedIndexList(parameters);
  4006. (* check if write operator exists, for var parameters *)
  4007. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4008. actualParameters := SyntaxTree.NewExpressionList();
  4009. FOR i := 0 TO parameters.Length()-1 DO
  4010. expression := ResolveExpression(parameters.GetExpression(i));
  4011. actualParameters.AddExpression(expression);
  4012. END;
  4013. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4014. actualParameters.AddExpression(rhs);
  4015. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4016. IF op = NIL THEN rhs := NIL END;
  4017. END;
  4018. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4019. ELSE
  4020. result := NIL;
  4021. END;
  4022. RETURN result;
  4023. END NewObjectOperatorCall;
  4024. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4025. 1. convert bracket designator chains into a single one that contains separators
  4026. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4027. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4028. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4029. - if an array or math array is indexed over, create index designator
  4030. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4031. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4032. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4033. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4034. - if an array-structured object type is indexed over, create procedure call designator
  4035. e.g.: a[x, y] -> a^."[]"(x, y)
  4036. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4037. - a[i, *] = a[i][*]
  4038. - a[*, i] # a[*][i]
  4039. Because:
  4040. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4041. - 'i-th column' = a[*, i]
  4042. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4043. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4044. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4045. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4046. **)
  4047. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4048. VAR
  4049. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4050. indexDesignator: SyntaxTree.IndexDesignator;
  4051. designator: SyntaxTree.Designator;
  4052. type: SyntaxTree.Type;
  4053. recordType: SyntaxTree.RecordType;
  4054. expression, rhs: SyntaxTree.Expression;
  4055. indexList: SyntaxTree.ExpressionList;
  4056. i: LONGINT;
  4057. hasError, done: BOOLEAN;
  4058. PROCEDURE FinalizeIndexDesignator;
  4059. BEGIN
  4060. IF indexDesignator # NIL THEN
  4061. (* the end of a tensor has been reached: *)
  4062. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4063. SetIndexBaseType(indexDesignator, type);
  4064. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4065. designator := indexDesignator;
  4066. type := designator.type.resolved;
  4067. indexDesignator := NIL;
  4068. ASSERT(SyntaxTree.Resolved IN type.state)
  4069. END
  4070. END FinalizeIndexDesignator;
  4071. BEGIN
  4072. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4073. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4074. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4075. (* copy all index list entries including a separator to the left bracket designator *)
  4076. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4077. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4078. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4079. END;
  4080. (* propagate the related RHS *)
  4081. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4082. (* only resolve left bracket designator and use as final result *)
  4083. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4084. ELSE
  4085. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4086. designator := ResolveDesignator(bracketDesignator.left);
  4087. type := designator.type.resolved;
  4088. indexDesignator := NIL;
  4089. (*!!! clean up *)
  4090. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4091. OR (type IS SyntaxTree.RecordType)
  4092. THEN
  4093. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4094. IF resolvedExpression = NIL THEN
  4095. Error(bracketDesignator.position,"undefined operator");
  4096. resolvedExpression := SyntaxTree.invalidDesignator
  4097. END;
  4098. RETURN;
  4099. END;
  4100. i := 0;
  4101. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4102. expression := bracketDesignator.parameters.GetExpression(i);
  4103. expression := ResolveExpression(expression);
  4104. bracketDesignator.parameters.SetExpression(i, expression);
  4105. IF expression = SyntaxTree.indexListSeparator THEN
  4106. (* finalize an existing index designator if needed *)
  4107. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4108. INC(i)
  4109. ELSE
  4110. (* do auto-dereferencing if needed *)
  4111. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4112. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4113. & (i=0)*)
  4114. THEN
  4115. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4116. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4117. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4118. designator := SyntaxTree.invalidDesignator;
  4119. type := SyntaxTree.invalidType
  4120. ELSE
  4121. FinalizeIndexDesignator;
  4122. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4123. type := designator.type.resolved
  4124. END
  4125. END;
  4126. (* create a new index designator, if needed *)
  4127. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4128. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4129. indexDesignator.SetAssignable(designator.assignable);
  4130. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4131. (* designator := indexDesignator *)
  4132. END;
  4133. IF type = SyntaxTree.invalidType THEN
  4134. (* error already handled *)
  4135. INC(i)
  4136. ELSIF type IS SyntaxTree.ArrayType THEN
  4137. (* indexing over an array *)
  4138. ASSERT(indexDesignator # NIL);
  4139. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4140. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4141. INC(i)
  4142. ELSIF type IS SyntaxTree.StringType THEN
  4143. (* indexing over an array *)
  4144. ASSERT(indexDesignator # NIL);
  4145. AppendIndex(expression.position, indexDesignator, expression, type);
  4146. type := type(SyntaxTree.StringType).baseType.resolved;
  4147. INC(i)
  4148. ELSIF type IS SyntaxTree.MathArrayType THEN
  4149. (* indexing over a math array *)
  4150. ASSERT(indexDesignator # NIL);
  4151. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4152. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4153. INC(i)
  4154. ELSIF IsArrayStructuredObjectType(type) THEN
  4155. (* indexing over ASOTs *)
  4156. FinalizeIndexDesignator;
  4157. ASSERT(type IS SyntaxTree.PointerType);
  4158. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4159. (*
  4160. - collect index list items from bracket designator that belong to ASOT
  4161. - check for errors
  4162. *)
  4163. indexList := SyntaxTree.NewExpressionList();
  4164. hasError := FALSE;
  4165. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4166. (* indexing over tensor ASOT:
  4167. - stop at index list end or separator
  4168. - dimensionality is given by number of index list items
  4169. *)
  4170. done := FALSE;
  4171. WHILE ~done DO
  4172. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4173. done := TRUE;
  4174. ELSE
  4175. expression := bracketDesignator.parameters.GetExpression(i);
  4176. IF expression = SyntaxTree.indexListSeparator THEN
  4177. done := TRUE;
  4178. ELSE
  4179. expression := ResolveExpression(expression);
  4180. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4181. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4182. hasError := TRUE
  4183. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4184. Error(expression.position, "integer or range expected");
  4185. expression := SyntaxTree.invalidExpression;
  4186. hasError := TRUE
  4187. END;
  4188. indexList.AddExpression(expression)
  4189. END;
  4190. INC(i)
  4191. END
  4192. END
  4193. ELSE
  4194. (* indexing over non-tensor ASOT:
  4195. - ignore separators
  4196. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4197. *)
  4198. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4199. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4200. expression := bracketDesignator.parameters.GetExpression(i);
  4201. ELSE
  4202. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4203. END;
  4204. IF expression # SyntaxTree.indexListSeparator THEN
  4205. expression := ResolveExpression(expression);
  4206. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4207. Error(expression.position, "integer or range expected");
  4208. expression := SyntaxTree.invalidExpression;
  4209. hasError := TRUE
  4210. END;
  4211. indexList.AddExpression(expression)
  4212. END;
  4213. INC(i)
  4214. END;
  4215. END;
  4216. IF hasError THEN
  4217. designator := SyntaxTree.invalidDesignator;
  4218. type := SyntaxTree.invalidType;
  4219. ELSE
  4220. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4221. and the last entry in the index list belongs to the array-structured object type in question.
  4222. E.g.: for a 2-dimensional array-structured object type:
  4223. - 'lhs := asot[1, 2]' -> read mode
  4224. - 'asot[1, 2] := rhs' -> write mode
  4225. - 'asot[1, 2, 3] := rhs' -> read mode
  4226. *)
  4227. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4228. rhs := bracketDesignator.relatedRhs
  4229. ELSE
  4230. rhs := NIL
  4231. END;
  4232. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4233. type := designator.type
  4234. END
  4235. ELSE
  4236. Error(expression.position,"indexing over non-array type");
  4237. designator := SyntaxTree.invalidDesignator;
  4238. type := SyntaxTree.invalidType;
  4239. INC(i)
  4240. END
  4241. END
  4242. END;
  4243. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4244. resolvedExpression := designator
  4245. END
  4246. END VisitBracketDesignator;
  4247. (** check and resolve expression list
  4248. - resolve each expression in an expression list
  4249. - returns true if and only if all statements could have successfully been resolved
  4250. **)
  4251. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4252. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4253. BEGIN
  4254. result := TRUE;
  4255. FOR i := 0 TO expressionList.Length()-1 DO
  4256. expression := ResolveExpression(expressionList.GetExpression(i));
  4257. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4258. expressionList.SetExpression(i,expression);
  4259. END;
  4260. RETURN result
  4261. END ExpressionList;
  4262. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4263. BEGIN
  4264. type := type.resolved;
  4265. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4266. RETURN TRUE
  4267. ELSIF system.CanPassInRegister # NIL THEN
  4268. RETURN system.CanPassInRegister(type);
  4269. ELSE
  4270. RETURN FALSE
  4271. END;
  4272. END CanPassInRegister;
  4273. (** return procedure call designator left(actualParameters)
  4274. - check realtime procedure call in realtime procedure
  4275. - check number of parameters
  4276. - check parameter compatibility
  4277. return invalidDesignator if error
  4278. **)
  4279. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4280. VAR result: SyntaxTree.Designator;
  4281. numberFormalParameters, numberActualParameters: LONGINT;
  4282. formalType: SyntaxTree.ProcedureType;
  4283. formalParameter: SyntaxTree.Parameter;
  4284. actualParameter: SyntaxTree.Expression;
  4285. i: LONGINT;
  4286. self: SyntaxTree.Expression;
  4287. BEGIN
  4288. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4289. result := SyntaxTree.invalidDesignator;
  4290. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4291. numberFormalParameters := formalType.numberParameters;
  4292. numberActualParameters := actualParameters.Length();
  4293. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4294. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4295. END;
  4296. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4297. self := left.left;
  4298. IF (self # NIL) & ~IsVariable(self) THEN
  4299. Error(self.position, "Non-variable expression on variable receiver");
  4300. END;
  4301. END;
  4302. IF ~ExpressionList(actualParameters) THEN
  4303. result := SyntaxTree.invalidDesignator
  4304. ELSE
  4305. IF numberActualParameters <= numberFormalParameters THEN
  4306. formalParameter := formalType.firstParameter;
  4307. FOR i := 0 TO numberActualParameters-1 DO
  4308. actualParameter := actualParameters.GetExpression(i);
  4309. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4310. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4311. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4312. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4313. ELSE
  4314. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4315. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4316. END;
  4317. actualParameters.SetExpression(i,actualParameter);
  4318. END;
  4319. formalParameter := formalParameter.nextParameter;
  4320. END;
  4321. WHILE (formalParameter # NIL) DO
  4322. IF formalParameter.defaultValue # NIL THEN
  4323. actualParameters.AddExpression(formalParameter.defaultValue);
  4324. formalParameter := formalParameter.nextParameter
  4325. ELSE
  4326. Error(position, "less actual than formal parameters");
  4327. formalParameter := NIL;
  4328. END;
  4329. END;
  4330. ELSE
  4331. Error(position, "more actual than formal parameters")
  4332. END;
  4333. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4334. result.SetAssignable(FALSE);
  4335. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4336. END;
  4337. RETURN result
  4338. END NewProcedureCallDesignator;
  4339. (**
  4340. builtin call designator generated in VisitParameterDesignator
  4341. -> nothing to be resolved
  4342. **)
  4343. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4344. BEGIN
  4345. resolvedExpression := x;
  4346. END VisitTypeGuardDesignator;
  4347. (**
  4348. builtin call designator generated in VisitParameterDesignator
  4349. -> nothing to be resolved
  4350. **)
  4351. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4352. BEGIN
  4353. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4354. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4355. ASSERT(resolvedExpression.type # NIL);
  4356. ELSIF ExpressionList(x.parameters) THEN
  4357. resolvedExpression := x;
  4358. END;
  4359. END VisitBuiltinCallDesignator;
  4360. (**
  4361. procedure call designator generated in VisitParameterDesignator
  4362. -> nothing to be resolved
  4363. **)
  4364. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4365. BEGIN
  4366. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4367. resolvedExpression := x;
  4368. END VisitProcedureCallDesignator;
  4369. (** return true if x is a variable else return false and report error **)
  4370. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4371. VAR result: BOOLEAN;
  4372. BEGIN
  4373. result := TRUE;
  4374. IF x = SyntaxTree.invalidExpression THEN
  4375. result := FALSE;
  4376. ELSIF ~IsVariable(x) THEN
  4377. Error(x.position,"non variable expression");
  4378. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4379. result := FALSE;
  4380. END;
  4381. RETURN result
  4382. END CheckVariable;
  4383. (**
  4384. if expression x is of basic type then return true else report error and return false
  4385. **)
  4386. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4387. VAR result: BOOLEAN;
  4388. BEGIN
  4389. result := FALSE;
  4390. IF x = SyntaxTree.invalidExpression THEN
  4391. ELSIF ~IsBasicType(x.type) THEN
  4392. Error(x.position,"is no basic type");
  4393. result := FALSE
  4394. ELSE result := TRUE
  4395. END;
  4396. RETURN result
  4397. END CheckBasicType;
  4398. (**
  4399. if expression x is of number type then return true else report error and return false
  4400. **)
  4401. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4402. VAR result: BOOLEAN;
  4403. BEGIN
  4404. result := FALSE;
  4405. IF x = SyntaxTree.invalidExpression THEN
  4406. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4407. Error(x.position,"is non number type");
  4408. ELSE result := TRUE
  4409. END;
  4410. RETURN result
  4411. END CheckNumberType;
  4412. (**
  4413. if expression x is of number or size type but not complex then return true else report error and return false
  4414. **)
  4415. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4416. VAR result: BOOLEAN;
  4417. BEGIN
  4418. result := FALSE;
  4419. IF x = SyntaxTree.invalidExpression THEN
  4420. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4421. Error(x.position,"is complex type");
  4422. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4423. Error(x.position,"is non number type");
  4424. ELSE result := TRUE
  4425. END;
  4426. RETURN result
  4427. END CheckNonComplexNumberSizeType;
  4428. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4429. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4430. BEGIN
  4431. result := FALSE; type := x.type.resolved;
  4432. IF x = SyntaxTree.invalidExpression THEN
  4433. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4434. TRACE(type.sizeInBits);
  4435. TRACE(system.addressType.sizeInBits);
  4436. Error(x.position,"is no address type");
  4437. ELSE result := TRUE
  4438. END;
  4439. RETURN result
  4440. END CheckAddressType;
  4441. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4442. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4443. BEGIN
  4444. result := FALSE; type := x.type.resolved;
  4445. IF x = SyntaxTree.invalidExpression THEN
  4446. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4447. Error(x.position,"is no size type");
  4448. ELSE result := TRUE
  4449. END;
  4450. RETURN result
  4451. END CheckSizeType;
  4452. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4453. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4454. BEGIN
  4455. result := FALSE; type := x.type.resolved;
  4456. IF x = SyntaxTree.invalidExpression THEN
  4457. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4458. Error(x.position,"is no object type");
  4459. ELSE result := TRUE
  4460. END;
  4461. RETURN result
  4462. END CheckObjectType;
  4463. (**
  4464. if expression x is of integer type then return true else report error and return false
  4465. **)
  4466. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4467. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4468. BEGIN
  4469. result := FALSE; type := x.type.resolved;
  4470. IF x = SyntaxTree.invalidExpression THEN
  4471. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4472. Error(x.position,"is no integer type");
  4473. ELSE result := TRUE
  4474. END;
  4475. RETURN result
  4476. END CheckIntegerType;
  4477. (**
  4478. if expression x is of character type then return true else report error and return false
  4479. **)
  4480. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4481. VAR result: BOOLEAN;
  4482. BEGIN
  4483. result := FALSE;
  4484. IF x = SyntaxTree.invalidExpression THEN
  4485. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4486. Error(x.position,"is no character type");
  4487. ELSE result := TRUE
  4488. END;
  4489. RETURN result
  4490. END CheckCharacterType;
  4491. (**
  4492. if expression x is of real type then return true else report error and return false
  4493. **)
  4494. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4495. VAR result: BOOLEAN;
  4496. BEGIN
  4497. result := FALSE;
  4498. IF x = SyntaxTree.invalidExpression THEN
  4499. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4500. Error(x.position,"is no float type");
  4501. ELSE result := TRUE
  4502. END;
  4503. RETURN result
  4504. END CheckRealType;
  4505. (**
  4506. if expression x is of range type then return true else report error and return false
  4507. **)
  4508. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4509. VAR result: BOOLEAN;
  4510. BEGIN
  4511. result := FALSE;
  4512. IF x = SyntaxTree.invalidExpression THEN
  4513. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4514. Error(x.position,"is no range type");
  4515. ELSE result := TRUE
  4516. END;
  4517. RETURN result
  4518. END CheckRangeType;
  4519. (**
  4520. if expression x is of boolean type then return true else report error and return false
  4521. **)
  4522. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4523. VAR result: BOOLEAN;
  4524. BEGIN
  4525. result := FALSE;
  4526. IF x = SyntaxTree.invalidExpression THEN
  4527. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4528. Error(x.position,"is no boolean type");
  4529. ELSE result := TRUE
  4530. END;
  4531. RETURN result
  4532. END CheckBooleanType;
  4533. (**
  4534. if expression x is of set type then return true else report error and return false
  4535. **)
  4536. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4537. VAR result: BOOLEAN;
  4538. BEGIN
  4539. result := FALSE;
  4540. IF x = SyntaxTree.invalidExpression THEN
  4541. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4542. Error(x.position,"is no set type");
  4543. ELSE result := TRUE
  4544. END;
  4545. RETURN result
  4546. END CheckSetType;
  4547. (**
  4548. if expression x is of string or array of character type then return true else report error and return false
  4549. **)
  4550. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4551. VAR result: BOOLEAN;
  4552. BEGIN
  4553. result := FALSE;
  4554. IF x = SyntaxTree.invalidExpression THEN
  4555. ELSIF ~IsStringType(x.type.resolved) THEN
  4556. Error(x.position,"is no string type");
  4557. ELSE result := TRUE
  4558. END;
  4559. RETURN result
  4560. END CheckStringType;
  4561. (**
  4562. if expression x is a type declaration type return true else report error and return false
  4563. **)
  4564. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4565. VAR result: BOOLEAN;
  4566. BEGIN
  4567. result := FALSE;
  4568. IF x = SyntaxTree.invalidExpression THEN
  4569. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4570. Error(x.position,"is not a type declaration");
  4571. ELSE result := TRUE
  4572. END;
  4573. RETURN result
  4574. END CheckTypeDeclarationType;
  4575. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4576. VAR result: BOOLEAN;
  4577. BEGIN
  4578. result := FALSE;
  4579. IF x = SyntaxTree.invalidExpression THEN
  4580. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4581. result := TRUE;
  4582. value := x.resolved(SyntaxTree.IntegerValue).value;
  4583. ELSE
  4584. Error(x.position,"expression is not an integer constant");
  4585. END;
  4586. RETURN result;
  4587. END CheckIntegerValue;
  4588. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4589. VAR result: BOOLEAN;
  4590. BEGIN
  4591. result := FALSE;
  4592. IF x = SyntaxTree.invalidExpression THEN
  4593. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4594. result := TRUE;
  4595. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4596. ELSE
  4597. Error(x.position,"expression is not an integer constant");
  4598. END;
  4599. RETURN result;
  4600. END CheckStringValue;
  4601. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4602. BEGIN
  4603. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4604. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  4605. ELSE
  4606. RETURN FALSE
  4607. END;
  4608. END IsUnsignedValue;
  4609. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4610. BEGIN
  4611. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4612. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  4613. ELSE
  4614. RETURN FALSE
  4615. END
  4616. END IsAddressValue;
  4617. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4618. BEGIN
  4619. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4620. END IsAddressExpression;
  4621. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4622. BEGIN
  4623. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4624. END IsSizeExpression;
  4625. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4626. VAR result: BOOLEAN;
  4627. BEGIN
  4628. result := FALSE;
  4629. IF x = SyntaxTree.invalidExpression THEN
  4630. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4631. result := TRUE;
  4632. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4633. ELSE
  4634. Error(x.position,"expression is not an integer constant");
  4635. END;
  4636. RETURN result;
  4637. END CheckEnumerationValue;
  4638. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4639. VAR result: BOOLEAN;
  4640. BEGIN
  4641. result := FALSE;
  4642. IF x = SyntaxTree.invalidExpression THEN
  4643. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4644. result := TRUE;
  4645. value := x.resolved(SyntaxTree.CharacterValue).value;
  4646. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4647. result := TRUE;
  4648. value := x.resolved(SyntaxTree.StringValue).value[0];
  4649. ELSE
  4650. Error(x.position,"expression is not a character constant");
  4651. END;
  4652. RETURN result;
  4653. END CheckCharacterValue;
  4654. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4655. VAR result: BOOLEAN;
  4656. BEGIN
  4657. result := FALSE;
  4658. IF x = SyntaxTree.invalidExpression THEN
  4659. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4660. value := x.resolved(SyntaxTree.IntegerValue).value;
  4661. IF (value > 0) OR includeZero & (value = 0) THEN
  4662. result := TRUE;
  4663. ELSE
  4664. Error(x.position,"integer is not positive");
  4665. END
  4666. ELSE
  4667. Error(x.position,"expression is not an integer constant");
  4668. END;
  4669. RETURN result;
  4670. END CheckPositiveIntegerValue;
  4671. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4672. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4673. BEGIN
  4674. result := FALSE;
  4675. IF x = SyntaxTree.invalidExpression THEN
  4676. ELSE
  4677. type := x.type.resolved;
  4678. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4679. portType := type(SyntaxTree.PortType);
  4680. result := TRUE
  4681. ELSE
  4682. Error(x.position,"no port type");
  4683. END;
  4684. END;
  4685. RETURN result
  4686. END CheckPortType;
  4687. (* move to builtin procedure call statement ?
  4688. remove builtin procedure call designator ?
  4689. *)
  4690. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4691. VAR
  4692. numberActualParameters,numberFormalParameters: LONGINT;
  4693. formalParameter: SyntaxTree.Parameter;
  4694. actualParameter: SyntaxTree.Expression;
  4695. procedureType: SyntaxTree.ProcedureType;
  4696. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4697. inPort, outPort: SyntaxTree.PortType;
  4698. constructor: SyntaxTree.Procedure;
  4699. type0,type1,type2: SyntaxTree.Type;
  4700. type,base,parameterType: SyntaxTree.Type;
  4701. arrayType: SyntaxTree.ArrayType;
  4702. i: LONGINT; i0, i1, value: Basic.Integer;
  4703. r,r0,r1,im: LONGREAL;
  4704. c: CHAR;
  4705. id: LONGINT;
  4706. b: BOOLEAN;
  4707. first: LONGINT;
  4708. mathArrayType: SyntaxTree.MathArrayType;
  4709. customBuiltin: SyntaxTree.CustomBuiltin;
  4710. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4711. VAR resultB: BOOLEAN;
  4712. BEGIN
  4713. IF numberActualParameters < from THEN
  4714. Error(position, "less actual than formal parameters");
  4715. result := SyntaxTree.invalidExpression;
  4716. resultB := FALSE;
  4717. ELSIF numberActualParameters > to THEN
  4718. Error(position, "more actual than formal parameters");
  4719. result := SyntaxTree.invalidExpression;
  4720. resultB := FALSE;
  4721. ELSE
  4722. resultB := TRUE;
  4723. END;
  4724. RETURN resultB
  4725. END CheckArity;
  4726. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4727. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4728. BEGIN
  4729. WHILE modifier # NIL DO
  4730. symbol := cellType.FindProperty(modifier.identifier);
  4731. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4732. propertyType := symbol.type.resolved;
  4733. modifierType := modifier.expression.type.resolved;
  4734. IF ~CompatibleTo(system, modifierType, propertyType) &
  4735. ~(
  4736. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4737. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4738. Error(modifier.position,"incompatible to cell property");
  4739. END;
  4740. ELSE
  4741. Error(modifier.position, "undefined property");
  4742. END;
  4743. modifier := modifier.nextModifier;
  4744. END;
  4745. END CheckModifiers;
  4746. BEGIN
  4747. type := NIL; result := NIL;
  4748. type0 := NIL; type1 := NIL; type2 := NIL;
  4749. numberActualParameters := actualParameters.Length();
  4750. IF numberActualParameters>0 THEN
  4751. parameter0 := actualParameters.GetExpression(0);
  4752. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4753. Error(parameter0.position,"forbidden type-less argument");
  4754. result := SyntaxTree.invalidExpression
  4755. END
  4756. END;
  4757. IF numberActualParameters >1 THEN
  4758. parameter1 := actualParameters.GetExpression(1);
  4759. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4760. ELSE
  4761. Error(parameter1.position,"forbidden type-less argument");
  4762. result := SyntaxTree.invalidExpression
  4763. END
  4764. END;
  4765. IF numberActualParameters >2 THEN
  4766. parameter2 := actualParameters.GetExpression(2);
  4767. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4768. ELSE
  4769. Error(parameter2.position,"forbidden type-less argument");
  4770. result := SyntaxTree.invalidExpression
  4771. END
  4772. END;
  4773. IF returnType # NIL THEN
  4774. id := Global.New;
  4775. result := NIL;
  4776. ELSE
  4777. id := builtin.id;
  4778. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4779. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4780. END;
  4781. END;
  4782. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4783. ELSIF result # NIL THEN type := result.type (* operator *)
  4784. ELSE
  4785. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4786. result(SyntaxTree.Designator).SetLeft(left);
  4787. IF returnType # NIL THEN
  4788. type := returnType;
  4789. END;
  4790. (* ---- ASSERT ----- *)
  4791. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4792. IF CheckBooleanType(parameter0) THEN
  4793. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4794. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4795. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4796. *)
  4797. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4798. Error(position, "assert failed");
  4799. END;
  4800. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4801. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4802. rules imposed by the architecture / current runtime
  4803. *)
  4804. END;
  4805. END;
  4806. (* ---- COPY ----- *)
  4807. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4808. IF~IsStringType(type0) THEN
  4809. Error(parameter0.position,"no string type");
  4810. END;
  4811. IF ~IsStringType(type1) THEN
  4812. Error(parameter1.position,"no string type");
  4813. ELSIF CheckVariable(parameter1) THEN
  4814. IF (type0 IS SyntaxTree.StringType) THEN
  4815. arrayType := type1(SyntaxTree.ArrayType);
  4816. IF arrayType.form = SyntaxTree.Static THEN
  4817. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4818. Error(position, "destination length smaller than source length")
  4819. END;
  4820. END;
  4821. END;
  4822. END;
  4823. (* ---- INC, DEC----- *)
  4824. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4825. IF numberActualParameters = 1 THEN
  4826. parameter1 :=Global.NewIntegerValue(system,position,1);
  4827. actualParameters.AddExpression(parameter1);
  4828. END;
  4829. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4830. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4831. Error(position, "incompatible increment");
  4832. ELSE
  4833. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4834. actualParameters.SetExpression(1,parameter1);
  4835. END;
  4836. END;
  4837. (* ---- EXCL, INCL----- *)
  4838. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4839. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4840. IF IsIntegerValue(parameter1,i0) THEN
  4841. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4842. Error(position, "parameter out of SET range")
  4843. END;
  4844. END;
  4845. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4846. actualParameters.SetExpression(1,parameter1);
  4847. END;
  4848. (* ---- HALT, SYSTEM.HALT ----- *)
  4849. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4850. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4851. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4852. rules imposed by the architecture / current runtime
  4853. *)
  4854. END;
  4855. (* ---- WAIT ----- *)
  4856. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4857. IF CheckObjectType(parameter0) THEN
  4858. END;
  4859. (* ---- NEW ----- *)
  4860. ELSIF (id = Global.New) THEN
  4861. IF returnType # NIL THEN
  4862. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4863. ELSE
  4864. first := 1;
  4865. END;
  4866. IF CheckArity(first,Infinity) THEN
  4867. IF currentIsRealtime THEN
  4868. Error(position, "forbidden new in realtime block");
  4869. END;
  4870. IF IsUnsafePointer(type0) THEN
  4871. Error(position, "forbidden new on unsafe pointer");
  4872. END;
  4873. (* check constructor *)
  4874. IF (first =0) OR CheckVariable(parameter0) THEN
  4875. IF type0 IS SyntaxTree.PointerType THEN
  4876. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4877. ELSIF type0 IS SyntaxTree.CellType THEN
  4878. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4879. ELSE
  4880. Error(position, "forbidden new on value type");
  4881. END;
  4882. IF type0 IS SyntaxTree.ArrayType THEN
  4883. arrayType := type0(SyntaxTree.ArrayType);
  4884. IF arrayType.form = SyntaxTree.Static THEN
  4885. i := first
  4886. ELSIF arrayType.form = SyntaxTree.Open THEN
  4887. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4888. ELSE HALT(100)
  4889. END;
  4890. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4891. i := first;
  4892. REPEAT
  4893. actualParameter := actualParameters.GetExpression(i);
  4894. IF CheckSizeType(actualParameter) THEN
  4895. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4896. actualParameters.SetExpression(i,actualParameter);
  4897. END;
  4898. INC(i);
  4899. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4900. END;
  4901. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4902. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4903. Error(position, "forbidden new on abstract object");
  4904. END;
  4905. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4906. IF constructor = NIL THEN
  4907. IF CheckArity(first,first) THEN END;
  4908. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4909. Error(position, "new on object with hidden constructor");
  4910. ELSE
  4911. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4912. numberFormalParameters := procedureType.numberParameters;
  4913. IF numberActualParameters-first <= numberFormalParameters THEN
  4914. formalParameter := procedureType.firstParameter;
  4915. FOR i := first TO numberActualParameters-1 DO
  4916. actualParameter := actualParameters.GetExpression(i);
  4917. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4918. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4919. ELSE
  4920. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4921. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4922. END;
  4923. actualParameters.SetExpression(i,actualParameter);
  4924. END;
  4925. formalParameter := formalParameter.nextParameter;
  4926. END;
  4927. WHILE (formalParameter # NIL) DO
  4928. IF formalParameter.defaultValue # NIL THEN
  4929. actualParameters.AddExpression(formalParameter.defaultValue);
  4930. formalParameter := formalParameter.nextParameter
  4931. ELSE
  4932. Error(position, "less actual than formal parameters");
  4933. formalParameter := NIL;
  4934. END;
  4935. END;
  4936. ELSE
  4937. Error(position, "more actual than formal parameters")
  4938. END;
  4939. END;
  4940. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4941. mathArrayType := type0(SyntaxTree.MathArrayType);
  4942. IF mathArrayType.form = SyntaxTree.Static THEN
  4943. Error(position, "new on static array");
  4944. ELSE
  4945. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4946. i0 := first+1; i1 := Infinity;
  4947. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4948. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4949. i1 := i0;
  4950. ELSE HALT(100);
  4951. END;
  4952. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4953. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4954. base := ArrayBase(type0,MAX(LONGINT));
  4955. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4956. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4957. IF ~CompatibleTo(system,type0,parameterType) THEN
  4958. Error(parameter0.position,"incompatible parameter in new");
  4959. result := SyntaxTree.invalidExpression;
  4960. ELSE
  4961. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4962. END;
  4963. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4964. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4965. IF ~CompatibleTo(system,type1,parameterType) THEN
  4966. Error(parameter1.position,"parameter incompatible to math array len type");
  4967. result := SyntaxTree.invalidExpression;
  4968. ELSE
  4969. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4970. END;
  4971. ELSE
  4972. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4973. i := first;
  4974. REPEAT
  4975. actualParameter := actualParameters.GetExpression(i);
  4976. IF CheckSizeType(actualParameter) THEN
  4977. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4978. actualParameters.SetExpression(i,actualParameter);
  4979. END;
  4980. INC(i);
  4981. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4982. END;
  4983. END;
  4984. END;
  4985. ELSIF type0 IS SyntaxTree.CellType THEN
  4986. IF ~(currentIsCellNet) THEN
  4987. Error(position, "cell allocation outside activeCells ");
  4988. ELSE
  4989. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4990. IF (constructor = NIL) & CheckArity(1,1) THEN
  4991. (* ok *)
  4992. ELSE
  4993. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4994. numberFormalParameters := procedureType.numberParameters;
  4995. DEC(numberActualParameters);
  4996. IF numberActualParameters <= numberFormalParameters THEN
  4997. formalParameter := procedureType.firstParameter;
  4998. FOR i := first TO numberActualParameters DO
  4999. actualParameter := actualParameters.GetExpression(i);
  5000. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5001. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5002. ELSE
  5003. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5004. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5005. END;
  5006. actualParameters.SetExpression(i,actualParameter);
  5007. END;
  5008. formalParameter := formalParameter.nextParameter;
  5009. END;
  5010. WHILE (formalParameter # NIL) DO
  5011. IF formalParameter.defaultValue # NIL THEN
  5012. actualParameters.AddExpression(formalParameter.defaultValue);
  5013. formalParameter := formalParameter.nextParameter
  5014. ELSE
  5015. Error(position, "less actual than formal parameters");
  5016. formalParameter := NIL;
  5017. END;
  5018. END;
  5019. ELSE
  5020. Error(position, "more actual than formal parameters")
  5021. END;
  5022. END;
  5023. END;
  5024. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5025. activeCellsStatement := TRUE;
  5026. ELSE
  5027. Error(position, "cannot be allocated");
  5028. END;
  5029. END;
  5030. END;
  5031. (* ---- DISPOSE ----- *)
  5032. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5033. IF ~IsPointerType(parameter0.type) THEN
  5034. Error(parameter0.position,"is not a pointer")
  5035. ELSIF ~IsDisposable(parameter0.type) THEN
  5036. Error(parameter0.position,"is not disposable")
  5037. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5038. END
  5039. (* ---- GETPROCEDURE ----- *)
  5040. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5041. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5042. IF CheckVariable(parameter2) THEN
  5043. IF ~GetProcedureAllowed(parameter2.type) THEN
  5044. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5045. END;
  5046. END;
  5047. END;
  5048. (* ---- ABS ----- *)
  5049. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5050. (* note: ABS on complex numbers is done using overloading *)
  5051. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5052. type := type0;
  5053. IF IsIntegerValue(parameter0,i0) THEN
  5054. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5055. type := Global.GetIntegerType(system,ABS(i0));
  5056. ELSIF IsRealValue(parameter0,r) THEN
  5057. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5058. END;
  5059. ELSE
  5060. type := SyntaxTree.invalidType;
  5061. END;
  5062. (* ---- ASH, ASR ----- *)
  5063. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5064. type := type0;
  5065. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5066. (*
  5067. ConvertOperands(parameter0,parameter1); (* same type *)
  5068. *)
  5069. type := parameter0.type;
  5070. IF IsIntegerValue(parameter0,i0) THEN
  5071. IF IsIntegerValue(parameter1,i1) THEN
  5072. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5073. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5074. result := ResolveExpression(result);
  5075. type := Global.GetIntegerType(system,i0);
  5076. END;
  5077. END;
  5078. IF type.resolved.sizeInBits < 32 THEN
  5079. type := system.longintType;
  5080. END;
  5081. (*!compatibility with release, remove when resolved
  5082. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5083. *)
  5084. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5085. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5086. actualParameters.SetExpression(0,parameter0);
  5087. actualParameters.SetExpression(1,parameter1);
  5088. END;
  5089. (* ---- CAP ----- *)
  5090. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5091. type := system.characterType;
  5092. IF CheckCharacterType (parameter0) THEN
  5093. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5094. actualParameters.SetExpression(0,parameter0);
  5095. IF IsCharacterValue(parameter0,c) THEN
  5096. IF (c <= "z") & (c >= "a") THEN
  5097. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5098. ELSE
  5099. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5100. END;
  5101. END;
  5102. END;
  5103. (* ---- CHR ----- *)
  5104. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5105. IF id = Global.Chr THEN
  5106. type := system.characterType
  5107. ELSE
  5108. type := system.characterType32
  5109. END;
  5110. IF CheckIntegerType(parameter0) THEN
  5111. IF IsIntegerValue(parameter0,i0) THEN
  5112. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5113. result := ResolveExpression(result);
  5114. ELSE
  5115. (*
  5116. result := NewConversion(parameter0.position,parameter0,type);
  5117. *)
  5118. END;
  5119. END
  5120. (* ---- ENTIER ----- *)
  5121. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5122. type := system.longintType;
  5123. IF CheckRealType(parameter0) THEN
  5124. IF IsRealValue(parameter0,r) THEN
  5125. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5126. type := Global.GetIntegerType(system,ENTIER(r));
  5127. END
  5128. END;
  5129. (* ---- ENTIERH ----- *)
  5130. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5131. type := system.hugeintType;
  5132. IF CheckRealType(parameter0) THEN
  5133. IF IsRealValue(parameter0,r) THEN
  5134. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5135. END
  5136. END;
  5137. (* ---- LEN ----- *)
  5138. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5139. type := system.lenType;
  5140. base := type0;
  5141. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5142. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5143. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5144. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5145. Error(position, "forbidden len on unsafe pointer");
  5146. END;
  5147. type0 := base;
  5148. ELSE
  5149. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5150. type0 := parameter0.type.resolved;
  5151. actualParameters.SetExpression(0,parameter0);
  5152. base := type0;
  5153. END;
  5154. END;
  5155. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5156. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5157. IF i1 < 0 THEN
  5158. Error(position, "invalid dimension");
  5159. base := SyntaxTree.invalidType;
  5160. ELSE
  5161. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5162. IF (base # NIL) & Indexable(base) THEN
  5163. ELSE
  5164. Error(position, "len on no array");
  5165. IF VerboseErrorMessage THEN
  5166. Printout.Info("base",base);
  5167. END;
  5168. base := SyntaxTree.invalidType;
  5169. END;
  5170. END;
  5171. IF numberActualParameters=2 THEN
  5172. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5173. actualParameters.SetExpression(1,parameter1);
  5174. ELSIF base IS SyntaxTree.MathArrayType THEN
  5175. Error(position, "missing dimension specification");
  5176. END;
  5177. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5178. IF base IS SyntaxTree.ArrayType THEN
  5179. arrayType := base(SyntaxTree.ArrayType);
  5180. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5181. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5182. result := Global.NewIntegerValue(system,position,value);
  5183. type := result.type;(* arrayType.length.type;*)
  5184. ASSERT(type # NIL);
  5185. END;
  5186. ELSIF base IS SyntaxTree.MathArrayType THEN
  5187. mathArrayType := base(SyntaxTree.MathArrayType);
  5188. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5189. result := Global.NewIntegerValue(system,position,value);
  5190. type := result.type;
  5191. (*
  5192. type := mathArrayType.length.type;
  5193. *)
  5194. ASSERT(type # NIL);
  5195. END;
  5196. END;
  5197. END;
  5198. ELSE
  5199. type := system.lenType;
  5200. END;
  5201. (* ---- FIRST ---- *)
  5202. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5203. type := system.lenType;
  5204. IF CheckRangeType(parameter0) THEN END;
  5205. result.SetAssignable(parameter0.assignable)
  5206. (* ---- LAST ---- *)
  5207. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5208. type := system.lenType;
  5209. IF CheckRangeType(parameter0) THEN END;
  5210. result.SetAssignable(parameter0.assignable)
  5211. (* ---- STEP ---- *)
  5212. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5213. type := system.lenType;
  5214. IF CheckRangeType(parameter0) THEN END;
  5215. result.SetAssignable(parameter0.assignable)
  5216. (* ---- RE ---- *)
  5217. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5218. IF CheckNumberType(parameter0) THEN
  5219. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5220. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5221. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5222. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5223. type := parameter0.type
  5224. ELSE
  5225. type := system.realType
  5226. END
  5227. END;
  5228. result.SetAssignable(parameter0.assignable)
  5229. (* ---- IM ---- *)
  5230. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5231. IF CheckNumberType(parameter0) THEN
  5232. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5233. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5234. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5235. ELSE
  5236. type := system.realType;
  5237. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5238. END
  5239. END;
  5240. result.SetAssignable(parameter0.assignable)
  5241. (* ---- MAX ----- *)
  5242. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5243. IF numberActualParameters = 1 THEN
  5244. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5245. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5246. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5247. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5248. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5249. *)
  5250. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5251. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5252. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5253. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5254. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5255. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5256. END;
  5257. ELSE
  5258. Error(parameter0.position,"is not a type symbol");
  5259. END
  5260. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5261. ConvertOperands(parameter0,parameter1);
  5262. actualParameters.SetExpression(0,parameter0);
  5263. actualParameters.SetExpression(1,parameter1);
  5264. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5265. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5266. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5267. END;
  5268. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5269. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5270. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5271. END;
  5272. END;
  5273. type := parameter0.type;
  5274. ELSE type := SyntaxTree.invalidType;
  5275. END;
  5276. (* ---- MIN ----- *)
  5277. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5278. IF numberActualParameters = 1 THEN
  5279. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5280. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5281. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5282. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5283. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5284. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5285. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5286. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5287. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5288. END;
  5289. ELSE
  5290. Error(parameter0.position,"is not a type symbol");
  5291. END
  5292. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5293. ConvertOperands(parameter0,parameter1);
  5294. actualParameters.SetExpression(0,parameter0);
  5295. actualParameters.SetExpression(1,parameter1);
  5296. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5297. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5298. ELSE result.SetResolved(parameter1.resolved)
  5299. END;
  5300. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5301. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5302. ELSE result.SetResolved(parameter1.resolved)
  5303. END;
  5304. END;
  5305. type := parameter0.type;
  5306. ELSE type := SyntaxTree.invalidType;
  5307. END;
  5308. (* ---- ODD ----- *)
  5309. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5310. type := system.booleanType;
  5311. IF CheckIntegerType(parameter0) THEN
  5312. IF IsIntegerValue(parameter0,i0) THEN
  5313. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5314. type := system.booleanType;
  5315. END;
  5316. END;
  5317. (* ---- ORD ----- *)
  5318. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5319. IF id = Global.Ord THEN
  5320. type := system.integerType;
  5321. ELSE
  5322. type := system.longintType;
  5323. END;
  5324. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5325. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5326. actualParameters.SetExpression(0,parameter0);
  5327. (* IF CheckCharacterType(parameter0) THEN*)
  5328. IF IsCharacterValue(parameter0,c)THEN
  5329. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5330. type := Global.GetSignedIntegerType(system,ORD(c));
  5331. END;
  5332. ELSE Error(parameter0.position, "incompatible parameter");
  5333. END;
  5334. (* ---- SHORT ----- *)
  5335. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5336. type := type0;
  5337. IF IsSignedIntegerType(type) THEN
  5338. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5339. ELSIF type = system.integerType THEN type := system.shortintType
  5340. ELSIF type = system.longintType THEN type := system.integerType
  5341. ELSIF type = system.hugeintType THEN type:= system.longintType
  5342. ELSE
  5343. CASE type.sizeInBits OF
  5344. 16: type := Global.Integer8
  5345. |32: type := Global.Integer16
  5346. |64: type := Global.Integer32
  5347. END;
  5348. END;
  5349. ELSIF type IS SyntaxTree.FloatType THEN
  5350. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5351. ELSIF type = system.longrealType THEN type := system.realType
  5352. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5353. END;
  5354. ELSIF type IS SyntaxTree.ComplexType THEN
  5355. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5356. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5357. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5358. END;
  5359. ELSE
  5360. Error(parameter0.position,"short not applicable")
  5361. END;
  5362. IF (parameter0.resolved # NIL) THEN
  5363. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5364. IF parameter0 IS SyntaxTree.Value THEN
  5365. result.SetResolved(parameter0(SyntaxTree.Value));
  5366. END;
  5367. END;
  5368. (* ---- LONG ----- *)
  5369. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5370. type := type0;
  5371. IF IsSignedIntegerType(type) THEN
  5372. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5373. ELSIF type = system.longintType THEN type := system.hugeintType
  5374. ELSIF type = system.integerType THEN type := system.longintType
  5375. ELSIF type = system.shortintType THEN type := system.integerType
  5376. ELSE
  5377. CASE type.sizeInBits OF
  5378. 8: type := Global.Integer16
  5379. |16: type := Global.Integer32
  5380. |32: type := Global.Integer64
  5381. END;
  5382. END;
  5383. ELSIF type IS SyntaxTree.FloatType THEN
  5384. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5385. ELSIF type= system.realType THEN type := system.longrealType
  5386. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5387. END;
  5388. ELSIF type IS SyntaxTree.ComplexType THEN
  5389. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5390. ELSIF type = system.complexType THEN type := system.longcomplexType
  5391. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5392. END;
  5393. ELSE
  5394. Error(parameter0.position,"long not applicable")
  5395. END;
  5396. IF (parameter0.resolved # NIL) THEN
  5397. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5398. IF parameter0 IS SyntaxTree.Value THEN
  5399. result.SetResolved(parameter0(SyntaxTree.Value));
  5400. END;
  5401. END;
  5402. (* ---- SIZE OF ----- *)
  5403. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5404. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5405. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5406. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5407. type := system.integerType;
  5408. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5409. ELSE
  5410. (* for variables, system sizeof could represent the physically occupied size
  5411. determined via the type descriptor, implement that ? *)
  5412. Error(parameter0.position,"is not a type symbol");
  5413. END
  5414. (* ---- SYSTEM.TRACE -----*)
  5415. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5416. FOR i := 0 TO numberActualParameters-1 DO
  5417. parameter0 := actualParameters.GetExpression(i);
  5418. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5419. Error(parameter0.position,"incompatible parameter");
  5420. END;
  5421. END;
  5422. (* remaining issues can only be tested in backend *)
  5423. (* ---- ADDRESSOF----- *)
  5424. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5425. IF HasAddress(parameter0) THEN
  5426. type := system.addressType;
  5427. ELSE
  5428. type := SyntaxTree.invalidType;
  5429. Error(parameter0.position,"has no address");
  5430. END;
  5431. (* ---- BIT ----- *)
  5432. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5433. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5434. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5435. actualParameters.SetExpression(0,parameter0);
  5436. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5437. actualParameters.SetExpression(1,parameter1);
  5438. END;
  5439. type := system.booleanType;
  5440. (* ----- MSK ---- *)
  5441. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5442. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5443. ConvertOperands(parameter0,parameter1);
  5444. actualParameters.SetExpression(0,parameter0);
  5445. actualParameters.SetExpression(1,parameter1);
  5446. END;
  5447. type := parameter0.type;
  5448. (* ---- SYSTEM.GET64 ----- *)
  5449. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5450. IF CheckAddressType(parameter0) THEN
  5451. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5452. actualParameters.SetExpression(0,parameter0);
  5453. END;
  5454. type := system.hugeintType;
  5455. (* ---- SYSTEM.GET32 ----- *)
  5456. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5457. IF CheckAddressType(parameter0) THEN
  5458. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5459. actualParameters.SetExpression(0,parameter0);
  5460. END;
  5461. type := system.longintType;
  5462. (* ---- SYSTEM.GET16 ----- *)
  5463. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5464. IF CheckAddressType(parameter0) THEN
  5465. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5466. actualParameters.SetExpression(0,parameter0);
  5467. END;
  5468. type := system.integerType;
  5469. (* ---- SYSTEM.GET8 ----- *)
  5470. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5471. IF CheckAddressType(parameter0) THEN
  5472. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5473. actualParameters.SetExpression(0,parameter0);
  5474. END;
  5475. type := system.shortintType;
  5476. (* ---- SYSTEM.GetStackPointer ----- *)
  5477. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5478. type := system.addressType;
  5479. (* ---- SYSTEM.GetFramePointer ----- *)
  5480. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5481. type := system.addressType;
  5482. (* ---- SYSTEM.GetActivity ----- *)
  5483. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5484. type := system.objectType;
  5485. (* ---- SYSTEM.SetStackPointer ----- *)
  5486. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5487. IF CheckAddressType(parameter0) THEN
  5488. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5489. actualParameters.SetExpression(0,parameter0);
  5490. END;
  5491. (* ---- SYSTEM.SetFramePointer ----- *)
  5492. ELSIF (id = Global.systemSetFramePointer) & 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. (* ---- SYSTEM.SetActivity ----- *)
  5498. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5499. IF CheckObjectType(parameter0) THEN
  5500. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5501. actualParameters.SetExpression(0,parameter0);
  5502. END;
  5503. (* ---- LSH, LSL, ROT, ROR ----- *)
  5504. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5505. type := type0;
  5506. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5507. actualParameters.SetExpression(1, parameter1);
  5508. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5509. IF id = Global.Lsh THEN
  5510. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5511. ELSIF id = Global.Rot THEN
  5512. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5513. ELSIF id = Global.Ror THEN
  5514. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5515. END;
  5516. END;
  5517. (* ---- SYSTEM.VAL ----- *)
  5518. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5519. IF CheckTypeDeclarationType(parameter0) THEN
  5520. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5521. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5522. result := SyntaxTree.invalidExpression;
  5523. Error(parameter0.position,"is no basic type");
  5524. ELSE
  5525. IF (parameter1.resolved # NIL) THEN
  5526. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5527. IF parameter0 IS SyntaxTree.Value THEN
  5528. result.SetResolved(parameter0(SyntaxTree.Value));
  5529. END;
  5530. END;
  5531. result.SetAssignable(parameter1.assignable);
  5532. END;
  5533. END;
  5534. (* ---- SYSTEM.GET ----- *)
  5535. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5536. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5537. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5538. actualParameters.SetExpression(0,parameter0);
  5539. END;
  5540. (* ---- SYSTEM.PUT ----- *)
  5541. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5542. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5543. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5544. actualParameters.SetExpression(0,parameter0);
  5545. END;
  5546. (* ---- SYSTEM.PUT64 ----- *)
  5547. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5548. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5549. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5550. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5551. actualParameters.SetExpression(0,parameter0);
  5552. actualParameters.SetExpression(1,parameter1);
  5553. END;
  5554. (* ---- SYSTEM.PUT32 ----- *)
  5555. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5556. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5557. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5558. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5559. actualParameters.SetExpression(0,parameter0);
  5560. actualParameters.SetExpression(1,parameter1);
  5561. END;
  5562. (* ---- SYSTEM.PUT16 ----- *)
  5563. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5564. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5565. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5566. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5567. actualParameters.SetExpression(0,parameter0);
  5568. actualParameters.SetExpression(1,parameter1);
  5569. END;
  5570. (* ---- SYSTEM.PUT8 ----- *)
  5571. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5572. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5573. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5574. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5575. actualParameters.SetExpression(0,parameter0);
  5576. actualParameters.SetExpression(1,parameter1);
  5577. END;
  5578. (* ---- SYSTEM.MOVE ----- *)
  5579. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5580. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5581. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5582. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5583. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5584. actualParameters.SetExpression(0,parameter0);
  5585. actualParameters.SetExpression(1,parameter1);
  5586. actualParameters.SetExpression(2,parameter2);
  5587. END;
  5588. (* ---- SYSTEM.NEW ----- *)
  5589. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5590. IF ~IsPointerType(parameter0.type) THEN
  5591. Error(parameter0.position,"is not a pointer")
  5592. ELSIF CheckSizeType(parameter1) THEN
  5593. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5594. actualParameters.SetExpression(1,parameter1);
  5595. END;
  5596. (* ----SYSTEM.REF ---- *)
  5597. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5598. type := system.addressType
  5599. (* ---- INCR ----- *)
  5600. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5601. type := system.lenType;
  5602. base := type0;
  5603. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5604. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5605. IF i1 < 0 THEN
  5606. Error(position, "invalid dimension");
  5607. base := SyntaxTree.invalidType;
  5608. ELSE
  5609. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5610. IF (base # NIL) & Indexable(base) THEN
  5611. ELSE
  5612. Error(position, "len on no array");
  5613. IF VerboseErrorMessage THEN
  5614. Printout.Info("base",base);
  5615. END;
  5616. base := SyntaxTree.invalidType;
  5617. END;
  5618. END;
  5619. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5620. actualParameters.SetExpression(1,parameter1);
  5621. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5622. mathArrayType := base(SyntaxTree.MathArrayType);
  5623. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5624. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5625. type := system.lenType;
  5626. END;
  5627. END;
  5628. ELSE
  5629. type := system.lenType;
  5630. END;
  5631. (* ---- SUM ----- *)
  5632. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5633. Error(position, "sum operator not applicable");
  5634. (* ---- ALL ----- *)
  5635. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5636. Error(position, "all operator not applicable");
  5637. (* ---- DIM ----- *)
  5638. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5639. type := system.lenType;
  5640. IF type0 IS SyntaxTree.MathArrayType THEN
  5641. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5642. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5643. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5644. END;
  5645. ELSE
  5646. Error(position, "dimension on non math array type");
  5647. END;
  5648. (* ---- CAS ----- *)
  5649. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5650. IF type0.IsComposite () THEN
  5651. Error(position, "first parameter of composite type");
  5652. result := SyntaxTree.invalidExpression;
  5653. ELSIF ~IsVariable (parameter0) THEN
  5654. Error(position, "first parameter not assignable");
  5655. result := SyntaxTree.invalidExpression;
  5656. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5657. Error(position, "second parameter incompatible");
  5658. result := SyntaxTree.invalidExpression;
  5659. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5660. Error(position, "third parameter incompatible");
  5661. result := SyntaxTree.invalidExpression;
  5662. ELSE
  5663. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5664. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5665. type := type0;
  5666. END;
  5667. (* ---- RESHAPE ----- *)
  5668. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5669. IF type0 IS SyntaxTree.MathArrayType THEN
  5670. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5671. base := ArrayBase(type0,MAX(LONGINT));
  5672. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5673. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5674. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5675. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5676. IF ~CompatibleTo(system,type0,parameterType) THEN
  5677. Error(parameter0.position,"incompatible parameter in reshape");
  5678. result := SyntaxTree.invalidExpression;
  5679. ELSE
  5680. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5681. END;
  5682. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5683. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5684. IF ~CompatibleTo(system,type1,parameterType) THEN
  5685. Error(parameter1.position,"parameter incompatible to math array of longint");
  5686. result := SyntaxTree.invalidExpression;
  5687. ELSE
  5688. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5689. END;
  5690. ELSE
  5691. Error(position,"reshape on non math array type");
  5692. result := SyntaxTree.invalidExpression;
  5693. END;
  5694. (* ---- SYSTEM.TYPECODE ----- *)
  5695. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5696. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5697. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5698. type := type.resolved;
  5699. IF type IS SyntaxTree.PointerType THEN
  5700. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5701. END;
  5702. IF ~(type IS SyntaxTree.RecordType) THEN
  5703. Error(parameter0.position,"must be type with type descriptor");
  5704. END;
  5705. ELSE
  5706. Error(parameter0.position,"is not a type symbol");
  5707. END;
  5708. type := system.addressType;
  5709. (* -------- FLT --------- *)
  5710. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5711. type := system.realType;
  5712. IF IsRealValue(parameter0, r) THEN
  5713. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5714. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5715. value := ABS(i0);
  5716. IF value # 0 THEN
  5717. i1 := 23;
  5718. IF value >= 2*800000H THEN
  5719. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5720. ELSIF value < 800000H THEN
  5721. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5722. END;
  5723. value := (i1 + 127)*800000H - 800000H + value;
  5724. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5725. END;
  5726. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5727. END;
  5728. (* ------- CONNECT -------*)
  5729. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5730. (*IF ~(currentIsCellNet) THEN
  5731. Error(position, "connection outside activeCells body block");
  5732. END;*)
  5733. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5734. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5735. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5736. END;
  5737. IF numberActualParameters = 3 THEN
  5738. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5739. Error(position, "incompatible channel size parameter");
  5740. END;
  5741. *)
  5742. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5743. actualParameters.SetExpression(2,parameter2);
  5744. END;
  5745. activeCellsStatement := TRUE;
  5746. (* ---------- DELEGATE --------*)
  5747. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5748. (*
  5749. IF ~(currentIsCellNet) THEN
  5750. Error(position, "connection delegation outside activeCells body block");
  5751. END;
  5752. *)
  5753. IF ~CheckPortType(parameter1, inPort) THEN
  5754. Error(parameter0.position,"not a port")
  5755. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5756. Error(parameter1.position,"not a port")
  5757. ELSIF (outPort.direction # inPort.direction) THEN
  5758. Error(parameter0.position,"invalid port direction");
  5759. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5760. Error(position, "incompatible port sizes");
  5761. END;
  5762. activeCellsStatement := TRUE;
  5763. (* --------- RECEIVE ---------*)
  5764. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5765. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5766. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5767. IF inPort.direction # SyntaxTree.InPort THEN
  5768. Error(parameter0.position,"not an in-port")
  5769. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5770. Error(parameter1.position,"incompatible to port type");
  5771. END;
  5772. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5773. IF ~SameType(parameter2.type, system.integerType) THEN
  5774. Error(parameter2.position,"incompatible to integer type");
  5775. END;
  5776. END;
  5777. END;
  5778. (* --------- SEND ---------*)
  5779. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5780. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5781. IF CheckPortType(parameter0,outPort) THEN
  5782. IF outPort.direction # SyntaxTree.OutPort THEN
  5783. Error(parameter1.position,"not an out-port")
  5784. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5785. Error(parameter1.position,"incompatible to port type");
  5786. ELSE
  5787. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5788. actualParameters.SetExpression(1,parameter1);
  5789. END;
  5790. END;
  5791. (* ------- custom builtins ----- *)
  5792. ELSIF id = Global.systemSpecial THEN
  5793. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5794. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5795. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5796. type := procedureType.returnType;
  5797. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5798. (* go through all formal parameters *)
  5799. formalParameter := procedureType.firstParameter;
  5800. FOR i := 0 TO actualParameters.Length() - 1 DO
  5801. actualParameter := actualParameters.GetExpression(i);
  5802. IF actualParameter = SyntaxTree.invalidExpression THEN
  5803. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5804. Error(position, "incompatible parameter")
  5805. ELSE
  5806. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5807. END;
  5808. actualParameters.SetExpression(i, actualParameter);
  5809. formalParameter := formalParameter.nextParameter
  5810. END
  5811. END
  5812. ELSE
  5813. Error(position, "builtin not implemented");
  5814. result := SyntaxTree.invalidExpression;
  5815. END;
  5816. END;
  5817. IF result # SyntaxTree.invalidExpression THEN
  5818. type := ResolveType(type);
  5819. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5820. result.SetType(type);
  5821. END;
  5822. RETURN result
  5823. END NewBuiltinCallDesignator;
  5824. (** return type guard designator left(type)
  5825. - check if type can be extended (i.e. is no static record)
  5826. - check if type is a type extension of left.type
  5827. - returns new type guard designator
  5828. returns invalidDesignator = invalidExpression if error
  5829. **)
  5830. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5831. VAR result: SyntaxTree.Designator;
  5832. BEGIN
  5833. result := SyntaxTree.invalidDesignator;
  5834. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5835. Error(position, "no type extension of type");
  5836. IF VerboseErrorMessage THEN
  5837. Printout.Info("left",left);
  5838. Printout.Info("type",type);
  5839. END;
  5840. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5841. Error(position, "variable cannot be extended");
  5842. ELSIF IsUnsafePointer(left.type) THEN
  5843. Error(position, "forbidden type guard on unsafe pointer");
  5844. ELSE
  5845. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5846. result.SetType(type);
  5847. result.SetAssignable(left.assignable);
  5848. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5849. END;
  5850. RETURN result
  5851. END NewTypeGuardDesignator;
  5852. (** check and resolve parameter designator left(expression list)
  5853. - check expression list
  5854. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5855. - elsif left is a procedure type then
  5856. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5857. - else return is a procedure call then return ProcedureCallDesignator
  5858. returns invalidDesignator = invalidExpression if error
  5859. **)
  5860. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5861. VAR
  5862. parameters: SyntaxTree.ExpressionList;
  5863. left: SyntaxTree.Designator;
  5864. result,expression: SyntaxTree.Expression;
  5865. typeDeclaration: SyntaxTree.TypeDeclaration;
  5866. type, expressionType: SyntaxTree.Type;
  5867. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5868. BEGIN
  5869. type := type.resolved;
  5870. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5871. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5872. END;
  5873. RETURN type
  5874. END BaseType;
  5875. BEGIN
  5876. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5877. result := SyntaxTree.invalidDesignator;
  5878. left := ResolveDesignator(designator.left);
  5879. IF left # SyntaxTree.invalidDesignator THEN
  5880. parameters := designator.parameters;
  5881. IF ExpressionList(parameters) THEN
  5882. IF (left.type = NIL) THEN
  5883. Error(left.position,"object is not a procedure or cannot be extended");
  5884. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5885. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5886. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5887. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5888. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5889. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5890. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5891. ELSE
  5892. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5893. END
  5894. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5895. expression := parameters.GetExpression(0);
  5896. type := typeDeclaration.declaredType.resolved;
  5897. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5898. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5899. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5900. OR (expressionType IS SyntaxTree.EnumerationType)
  5901. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5902. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5903. Error(left.position,"invalid unsigned type in explicit conversion");
  5904. ELSE
  5905. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5906. END;
  5907. ELSE
  5908. Error(left.position,"invalid type in explicit conversion");
  5909. END;
  5910. ELSE
  5911. Error(left.position,"called object is not a procedure or cannot be extended");
  5912. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5913. result := SyntaxTree.invalidDesignator;
  5914. END;
  5915. ELSE
  5916. result := SyntaxTree.invalidDesignator
  5917. END;
  5918. END;
  5919. resolvedExpression := result;
  5920. END VisitParameterDesignator;
  5921. (** check dereference designator left^
  5922. - check if left is pointer type or left is object type
  5923. - return new dereference designator with type = left.baseType.type (if appropriate)
  5924. with error handling
  5925. returns invalidDesignator = invalidExpression if error
  5926. **)
  5927. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5928. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5929. BEGIN
  5930. result := SyntaxTree.invalidDesignator;
  5931. type := left.type;
  5932. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5933. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5934. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5935. result.SetAssignable(TRUE);
  5936. result.SetType(type);
  5937. result.SetHidden(left.isHidden);
  5938. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5939. type := type.resolved;
  5940. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5941. result.SetAssignable(TRUE);
  5942. result.SetType(type);
  5943. result.SetHidden(left.isHidden);
  5944. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5945. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5946. result.SetAssignable(TRUE);
  5947. result.SetType(type);
  5948. result.SetHidden(left.isHidden);
  5949. ELSE
  5950. Error(position, "dereference on no pointer");
  5951. IF VerboseErrorMessage THEN
  5952. Printout.Info("pointer", type);
  5953. Printout.Info("scope", currentScope);
  5954. END;
  5955. END;
  5956. RETURN result
  5957. END NewDereferenceDesignator;
  5958. (** check supercall designator left^
  5959. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5960. - return new supercall designator with type = left.type
  5961. with error handling
  5962. **)
  5963. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5964. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5965. objectScope: SyntaxTree.Scope;
  5966. BEGIN
  5967. result := SyntaxTree.invalidDesignator;
  5968. IF left = SyntaxTree.invalidDesignator THEN
  5969. (* error already handled *)
  5970. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5971. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5972. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5973. IF symbol IS SyntaxTree.Procedure THEN
  5974. procedure := symbol(SyntaxTree.Procedure);
  5975. objectScope := currentScope;
  5976. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5977. objectScope := objectScope.outerScope;
  5978. END;
  5979. IF (left.left = NIL) OR ~
  5980. (
  5981. (left.left IS SyntaxTree.SelfDesignator) OR
  5982. (left.left IS SyntaxTree.DereferenceDesignator)
  5983. & (left.left(SyntaxTree.Designator).left # NIL)
  5984. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5985. Error(position, "procedure not in immediate object scope");
  5986. IF VerboseErrorMessage THEN
  5987. Printout.Info("left.left",left.left);
  5988. END;
  5989. ELSIF procedure.super # NIL THEN
  5990. result := SyntaxTree.NewSupercallDesignator(position,left);
  5991. result.SetType(left.type.resolved)
  5992. ELSE
  5993. Error(position, "no supermethod for this procedure");
  5994. END;
  5995. ELSE
  5996. Error(position, "symbol is not a procedure");
  5997. END;
  5998. ELSE
  5999. Error(position, "is no symbol designator");
  6000. END;
  6001. RETURN result
  6002. END NewSupercallDesignator;
  6003. (** check and semantically resolve arrow designator left^
  6004. - if left is procedure type -> result := SupercallDesignator
  6005. - else result := DereferenceDesignator
  6006. returns result via global variable resolvedExpression
  6007. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6008. **)
  6009. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6010. VAR left: SyntaxTree.Designator;
  6011. BEGIN
  6012. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6013. left := ResolveDesignator(arrowDesignator.left);
  6014. IF left # NIL THEN
  6015. IF (left.type = NIL) THEN
  6016. Error(arrowDesignator.position,"Invalid arrow designator");
  6017. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6018. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6019. ELSE
  6020. IF IsPointerToObject(left.type) THEN
  6021. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6022. END;
  6023. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6024. END
  6025. END
  6026. END VisitArrowDesignator;
  6027. (** check and return expression
  6028. - if expression has no type then resolve expression
  6029. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6030. - return result
  6031. **)
  6032. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6033. VAR result,prev: SyntaxTree.Expression;
  6034. BEGIN
  6035. IF expression = NIL THEN result := NIL
  6036. ELSIF (expression.type = NIL) THEN
  6037. prev := resolvedExpression;
  6038. resolvedExpression := SyntaxTree.invalidExpression;
  6039. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6040. expression.SetType(SyntaxTree.invalidType);
  6041. END;
  6042. expression.Accept(SELF);
  6043. result := resolvedExpression;
  6044. IF currentIsRealtime THEN
  6045. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6046. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6047. END;
  6048. END;
  6049. (* designator modifiers for backends if they support it ...*)
  6050. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6051. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6052. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6053. END;
  6054. resolvedExpression := prev
  6055. ELSE
  6056. result := expression
  6057. END;
  6058. RETURN result
  6059. END ResolveExpression;
  6060. (**
  6061. check expression to be constant expression
  6062. - resolve expression
  6063. - if valid then check that of value type
  6064. report error and return invalidExpression if anything fails
  6065. **)
  6066. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6067. VAR position: Position;
  6068. BEGIN
  6069. position := expression.position;
  6070. expression := ResolveExpression(expression);
  6071. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6072. ELSIF (expression.resolved = NIL) THEN
  6073. Error(position, "expression is not constant");
  6074. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6075. expression := SyntaxTree.invalidExpression;
  6076. END;
  6077. RETURN expression
  6078. END ConstantExpression;
  6079. (** check expression to be constant integer
  6080. - resolve expresssion
  6081. - if valid then check that of integer value type
  6082. report error and return invalidExpression if anything fails
  6083. **)
  6084. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6085. VAR position: Position;
  6086. BEGIN
  6087. position := expression.position;
  6088. expression := ResolveExpression(expression);
  6089. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6090. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6091. expression := SyntaxTree.invalidExpression;
  6092. Error(position, "expression is not a constant integer");
  6093. END;
  6094. RETURN expression
  6095. END ConstantInteger;
  6096. (** check expression as positive (>=0) constant integer
  6097. - resolve expression
  6098. - if valid then check that integer value
  6099. - if integer value then check that value >= 0
  6100. report error and return invalidExpression if anything fails
  6101. **)
  6102. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6103. VAR position: Position;
  6104. BEGIN
  6105. position := expression.position;
  6106. expression := ConstantExpression(expression);
  6107. IF expression = SyntaxTree.invalidExpression THEN
  6108. (* error already reported *)
  6109. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6110. Error(position, "expression is not integer valued");
  6111. expression := SyntaxTree.invalidExpression
  6112. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6113. Error(position, "integer is not greater or equal zero");
  6114. END;
  6115. RETURN expression
  6116. END ConstantIntegerGeq0;
  6117. (** check expression as condition
  6118. - resolve expression
  6119. - if valid expression then check that result type is boolean
  6120. report error and return invalidExpression if anything fails
  6121. **)
  6122. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6123. VAR position: Position;
  6124. BEGIN
  6125. position := expression.position;
  6126. expression := ResolveExpression(expression);
  6127. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6128. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6129. expression := SyntaxTree.invalidExpression;
  6130. Error(position, "expression is not boolean");
  6131. END;
  6132. RETURN expression
  6133. END ResolveCondition;
  6134. (*** symbols ***)
  6135. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6136. BEGIN
  6137. x.Accept(SELF);
  6138. END ResolveSymbol;
  6139. (** check a symbol
  6140. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6141. **)
  6142. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6143. VAR scope: SyntaxTree.Scope;
  6144. BEGIN
  6145. (* visibility *)
  6146. scope := symbol.scope;
  6147. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6148. scope := scope.outerScope;
  6149. END;
  6150. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6151. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6152. Error(symbol.position,"cannot be exported");
  6153. IF VerboseErrorMessage THEN
  6154. Printout.Info("symbol",symbol);
  6155. END;
  6156. END;
  6157. END;
  6158. END CheckSymbolVisibility;
  6159. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6160. If node is currently being resolved then emit a cyclic definition error.
  6161. Return TRUE only if node is fully resolved.
  6162. **)
  6163. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6164. VAR result: BOOLEAN;
  6165. BEGIN
  6166. IF SyntaxTree.Resolved IN x.state THEN
  6167. result := FALSE
  6168. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6169. Error(x.position,"cyclic definition");
  6170. result := FALSE;
  6171. ELSE
  6172. result := TRUE;
  6173. x.SetState(SyntaxTree.BeingResolved)
  6174. END;
  6175. RETURN result
  6176. END SymbolNeedsResolution;
  6177. (** check and resolve a type declaration symbol = Type
  6178. - set type to declaration type
  6179. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6180. This is so because the type declaration itself does not have a type but it only stands for a type.
  6181. In the implementation of the compiler this made a lot much easier.
  6182. - resolve and set declared type
  6183. - check symbol
  6184. **)
  6185. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6186. VAR prevScope: SyntaxTree.Scope;
  6187. BEGIN
  6188. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6189. IF SymbolNeedsResolution(typeDeclaration) THEN
  6190. typeDeclaration.SetState(SyntaxTree.Resolved);
  6191. prevScope := currentScope;
  6192. currentScope := typeDeclaration.scope;
  6193. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6194. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6195. CheckSymbolVisibility(typeDeclaration);
  6196. typeDeclaration.SetState(SyntaxTree.Resolved);
  6197. currentScope := prevScope;
  6198. END;
  6199. END VisitTypeDeclaration;
  6200. (** check and resolve a constant declaration symbol = (constant) expression
  6201. - check expression
  6202. - set type and value
  6203. - check symbol
  6204. **)
  6205. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6206. VAR
  6207. expression: SyntaxTree.Expression;
  6208. type: SyntaxTree.Type;
  6209. name: Basic.SegmentedName;
  6210. replacement: Replacement;
  6211. BEGIN
  6212. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6213. IF SymbolNeedsResolution(constant) THEN
  6214. expression := constant.value;
  6215. IF replacements # NIL THEN
  6216. Global.GetSymbolSegmentedName(constant, name);
  6217. replacement := replacements;
  6218. WHILE (replacement # NIL) & (replacement.name # name) DO
  6219. replacement := replacement.next;
  6220. END;
  6221. IF replacement # NIL THEN
  6222. InfoSS(constant.position, "replacing constant", constant.name);
  6223. (*
  6224. NEW(stringReader, Strings.Length(replacement.string^));
  6225. stringReader.Set(replacement.string^);
  6226. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6227. NEW(parser, scanner, diagnostics);
  6228. expression := parser.Expression();
  6229. *)
  6230. expression := replacement.expression;
  6231. replacement.used := TRUE;
  6232. END;
  6233. END;
  6234. constant.SetType(SyntaxTree.invalidType);
  6235. expression := ConstantExpression(expression);
  6236. ASSERT(expression.type # NIL);
  6237. type := expression.type.resolved;
  6238. constant.SetType(type);
  6239. constant.SetValue(expression);
  6240. CheckSymbolVisibility(constant);
  6241. constant.SetState(SyntaxTree.Resolved);
  6242. END;
  6243. END VisitConstant;
  6244. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6245. VAR procedureAlignment: LONGINT;
  6246. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6247. (* least common multiple *)
  6248. VAR a,b: LONGINT;
  6249. BEGIN
  6250. a := a0; b := b0;
  6251. WHILE (a # b) DO
  6252. IF a < b THEN a := a+a0
  6253. ELSE b := b + b0
  6254. END;
  6255. END;
  6256. RETURN a
  6257. END LCM;
  6258. BEGIN
  6259. IF alignment > 1 THEN
  6260. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6261. IF (procedureAlignment > 1) THEN
  6262. alignment := LCM(alignment, procedureAlignment);
  6263. END;
  6264. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6265. END;
  6266. END AdaptStackAlignment;
  6267. (** check and resolve a variable / field
  6268. - check and set type
  6269. - negative check on open array type
  6270. - check symbol
  6271. **)
  6272. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6273. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6274. BEGIN
  6275. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6276. IF SymbolNeedsResolution(variable) THEN
  6277. modifiers := variable.modifiers;
  6278. (*
  6279. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6280. variable.AddFlags(flags);
  6281. *)
  6282. variable.SetType(ResolveType(variable.type));
  6283. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6284. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6285. Error(variable.position,"forbidden open array variable");
  6286. END;
  6287. END;
  6288. CheckSymbolVisibility(variable);
  6289. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6290. variable.SetUntraced(TRUE);
  6291. IF ~ContainsPointer(variable.type) THEN
  6292. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6293. Error(position, "untraced flag on non-pointer variable");
  6294. END;
  6295. END;
  6296. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6297. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6298. IF ~PowerOf2(value) THEN
  6299. Error(position, "forbidden alignment - must be power of two");
  6300. ELSE
  6301. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6302. END;
  6303. END;
  6304. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6305. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6306. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6307. Error(position, "fixed position not possible in procedure");
  6308. END;
  6309. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6310. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6311. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6312. Error(position,"fictive offset not possible in procedure");
  6313. END;
  6314. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6315. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6316. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6317. END;
  6318. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6319. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6320. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6321. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6322. END;
  6323. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6324. IF variable.initializer # NIL THEN
  6325. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6326. END;
  6327. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6328. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6329. pointerType.SetPointerBase(variable.type);
  6330. pointerType.SetHidden(TRUE);
  6331. variable.SetType(ResolveType(pointerType));
  6332. END;
  6333. variable.SetState(SyntaxTree.Resolved);
  6334. END;
  6335. END VisitVariable;
  6336. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6337. BEGIN
  6338. VisitVariable(property)
  6339. END VisitProperty;
  6340. (** check and resolve a (procedure) parameter
  6341. - check and set type
  6342. - check symbol
  6343. - check parameter kind and set read-only flags if appropriate
  6344. **)
  6345. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6346. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6347. BEGIN
  6348. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6349. IF SymbolNeedsResolution(parameter) THEN
  6350. modifiers := parameter.modifiers;
  6351. parameter.SetType(ResolveType(parameter.type));
  6352. ASSERT(parameter.type.resolved # NIL);
  6353. CheckSymbolVisibility(parameter);
  6354. IF parameter.defaultValue # NIL THEN
  6355. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6356. Error(parameter.position,"forbidden default value on non-value parameter");
  6357. ELSE
  6358. expression := ConstantExpression(parameter.defaultValue);
  6359. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6360. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6361. parameter.SetDefaultValue(expression);
  6362. END;
  6363. END;
  6364. END;
  6365. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6366. Error(parameter.position, "forbidden value parameter of math array type ");
  6367. END;
  6368. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6369. parameter.SetUntraced(TRUE);
  6370. IF ~ContainsPointer(parameter.type) THEN
  6371. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6372. Error(position, "untraced flag on non-pointer variable");
  6373. END;
  6374. END;
  6375. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6376. parameter.SetMoveable(TRUE);
  6377. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6378. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6379. Error(position, "illegal movable flag on non-address variable");
  6380. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6381. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6382. Error(position, "unnecessary movable flag on variable variable");
  6383. END;
  6384. END;
  6385. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6386. parameter.SetState(SyntaxTree.Resolved);
  6387. END;
  6388. END VisitParameter;
  6389. (** check and resolve a procedure (with declaration and implementation scope)
  6390. - check the procedure type
  6391. - check if method (i.e. in record scope), if so then
  6392. - check if (unique) constructor
  6393. - check if (unique) finalizer
  6394. - check if super method available, if so then check signature
  6395. - of not in record scope then negative check on constructor flag
  6396. - of not in record scope then negative check on finalizer flag
  6397. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6398. - check procedure symbol
  6399. **)
  6400. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6401. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6402. procedureType: SyntaxTree.ProcedureType;
  6403. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6404. qualifiedType: SyntaxTree.QualifiedType;
  6405. value: Basic.Integer;
  6406. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6407. position: Position;
  6408. fp: SyntaxTree.FingerPrint;
  6409. BEGIN
  6410. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6411. IF IsOberonInline(procedure) THEN
  6412. IF SyntaxTree.Public * procedure.access # {} THEN
  6413. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6414. END;
  6415. procedure.SetInline(FALSE);
  6416. procedure.SetOberonInline(TRUE);
  6417. END;
  6418. IF SymbolNeedsResolution(procedure) THEN
  6419. recentIsRealtime := currentIsRealtime;
  6420. recentIsBodyProcedure := currentIsBodyProcedure;
  6421. CheckSymbolVisibility(procedure);
  6422. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6423. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6424. modifiers := procedureType.modifiers;
  6425. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6426. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6427. IF useDarwinCCalls THEN (*fld*)
  6428. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6429. ELSE
  6430. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6431. END
  6432. END;
  6433. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6434. procedureType.SetInterrupt(TRUE);
  6435. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6436. END;
  6437. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6438. procedureType.SetNoReturn(TRUE);
  6439. END;
  6440. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6441. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6442. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6443. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6444. END;
  6445. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6446. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6447. END;
  6448. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6449. IF ~PowerOf2(value) THEN
  6450. Error(position, "forbidden stack alignment - must be power of two");
  6451. ELSE
  6452. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6453. END;
  6454. END;
  6455. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6456. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6457. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6458. END;
  6459. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6460. SyntaxTree.InitFingerPrint(fp);
  6461. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6462. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6463. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6464. fp.shallowAvailable := TRUE;
  6465. procedure.SetFingerPrint(fp);
  6466. END;
  6467. CheckModifiers(modifiers, TRUE);
  6468. modifiers := procedureType.returnTypeModifiers;
  6469. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6470. CheckModifiers(modifiers, TRUE);
  6471. procedure.SetState(SyntaxTree.Resolved);
  6472. FixProcedureType(procedureType);
  6473. currentIsRealtime := procedureType.isRealtime;
  6474. currentIsBodyProcedure := procedure.isBodyProcedure;
  6475. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6476. THEN
  6477. Error(procedure.position,"problems during parameter offset computation");
  6478. END;
  6479. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6480. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6481. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6482. Error(procedure.position,"problem during parameter offset generation");
  6483. END;
  6484. END;
  6485. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6486. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6487. procedureType.SetDelegate(TRUE);
  6488. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6489. (* add auto-self *)
  6490. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6491. IF (record.pointerType.typeDeclaration = NIL) THEN
  6492. selfParameter.SetType(record.pointerType);
  6493. ELSE
  6494. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6495. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6496. qualifiedType.SetResolved(record.pointerType);
  6497. selfParameter.SetType(qualifiedType);
  6498. END;
  6499. selfParameter.SetAccess(SyntaxTree.Hidden);
  6500. END;
  6501. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6502. IF procedure.isConstructor THEN
  6503. (*! constructor is always visible, compatibility to paco
  6504. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6505. *)
  6506. procedure.MarkUsed;
  6507. IF procedureType.returnType # NIL THEN
  6508. Error(procedure.position,"constructor with forbidden return type");
  6509. END;
  6510. proc := procedure.scope.firstProcedure;
  6511. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6512. proc := proc.nextProcedure;
  6513. END;
  6514. IF proc # NIL THEN
  6515. Error(procedure.position,"duplicate constructor")
  6516. ELSE
  6517. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6518. END;
  6519. END;
  6520. IF procedure.isFinalizer THEN
  6521. procedure.MarkUsed;
  6522. IF procedureType.returnType # NIL THEN
  6523. Error(procedure.position,"finalizer with forbidden return type");
  6524. END;
  6525. IF procedureType.numberParameters # 0 THEN
  6526. Error(procedure.position,"finalizer with formal parameters");
  6527. END;
  6528. proc := procedure.scope.firstProcedure;
  6529. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6530. proc := proc.nextProcedure;
  6531. END;
  6532. IF proc # NIL THEN
  6533. Error(procedure.position,"duplicate finalizer")
  6534. ELSE
  6535. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6536. END;
  6537. END;
  6538. super := FindSuperProcedure(record.recordScope, procedure);
  6539. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6540. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6541. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6542. END;
  6543. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6544. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6545. END;
  6546. IF super.isFinal THEN
  6547. Error(procedure.position,"forbidden method extending final method");
  6548. END;
  6549. (*
  6550. IF super.access # procedure.access THEN
  6551. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6552. END;
  6553. *)
  6554. procedure.SetSuper(super);
  6555. super.SetOverwritten(TRUE);
  6556. procedure.SetAccess(procedure.access+super.access);
  6557. procedure.MarkUsed;
  6558. END;
  6559. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6560. THEN
  6561. Error(procedure.position,"problems during parameter offset computation");
  6562. END;
  6563. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6564. IF cellsAreObjects THEN
  6565. procedureType.SetDelegate(TRUE);
  6566. END;
  6567. IF procedure.isConstructor THEN
  6568. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6569. END;
  6570. ELSIF procedure.isConstructor THEN
  6571. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6572. END;
  6573. Declarations(procedure.procedureScope, FALSE, {0,1});
  6574. (* body resolution part done as late fix of the procedure type *)
  6575. procedure.SetState(SyntaxTree.Resolved);
  6576. currentIsRealtime := recentIsRealtime;
  6577. currentIsBodyProcedure := recentIsBodyProcedure;
  6578. END;
  6579. END VisitProcedure;
  6580. (**
  6581. a builtin procedure is a global item that may not be modified locally
  6582. instead the resolving of builtin procedure calls are done in the esignator
  6583. **)
  6584. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6585. VAR type: SyntaxTree.Type;
  6586. BEGIN
  6587. type := ResolveType(builtinProcedure.type);
  6588. END VisitBuiltin;
  6589. (* nopov *)
  6590. (** check and resolve operator
  6591. - operators are first checked as procedures
  6592. - then additional operator-specific checks are done
  6593. - note that only module-scope operators are checked here
  6594. (operators in a record scope are only allowed in the context of
  6595. array-structured object types and checked in 'ResolveArrayStructure')
  6596. - also note that inter-operator conformity is not checked here
  6597. **)
  6598. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6599. VAR
  6600. procedureType: SyntaxTree.ProcedureType;
  6601. leftType, rightType: SyntaxTree.Type;
  6602. identifierNumber: LONGINT; position: Position;
  6603. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6604. modifiers: SyntaxTree.Modifier;
  6605. (** whether a type is locally defined in the current module scope
  6606. for arrays, the base type must be locally defined **)
  6607. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6608. BEGIN
  6609. IF type = NIL THEN
  6610. RETURN FALSE
  6611. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6612. RETURN TRUE
  6613. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6614. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6615. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6616. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6617. ELSE
  6618. RETURN FALSE
  6619. END
  6620. END IsLocallyDefined;
  6621. BEGIN
  6622. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6623. procedureType := operator.type(SyntaxTree.ProcedureType);
  6624. modifiers := procedureType.modifiers;
  6625. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6626. CheckModifiers(modifiers, TRUE);
  6627. VisitProcedure(operator);
  6628. IF operator.scope IS SyntaxTree.RecordScope THEN
  6629. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6630. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6631. IF identifierNumber = -1 THEN
  6632. Error(operator.position, "operator with unknown identifier")
  6633. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6634. Error(operator.position, "identifier may not be used for operator")
  6635. ELSE
  6636. IF procedureType.numberParameters < 1 THEN
  6637. Error(operator.position, "operator without operand");
  6638. ELSIF procedureType.numberParameters > 2 THEN
  6639. Error(operator.position, "operator with more than two operands");
  6640. ELSE
  6641. (* determine operand types *)
  6642. leftType := procedureType.firstParameter.type;
  6643. IF procedureType.numberParameters > 1 THEN
  6644. rightType := procedureType.firstParameter.nextParameter.type
  6645. ELSE
  6646. rightType := NIL
  6647. END;
  6648. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6649. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6650. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6651. Error(operator.position, "none of the operands is declared in the same module")
  6652. END
  6653. END;
  6654. (* TODO: refine the checks, think about how restrictive the checks should be
  6655. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6656. They might be used for intersection, union, complement of custom object types *)
  6657. (* defaults *)
  6658. hasReturnType := TRUE;
  6659. mustBeUnary := FALSE;
  6660. mustBeBinary := FALSE;
  6661. mustReturnBoolean := FALSE;
  6662. mustReturnInteger := FALSE;
  6663. mustHaveEquitypedOperands := FALSE;
  6664. (* operator-specific exceptions *)
  6665. CASE identifierNumber OF
  6666. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6667. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6668. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6669. mustBeBinary := TRUE
  6670. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6671. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6672. | Scanner.Times: mustBeBinary := TRUE
  6673. | Scanner.TimesTimes: mustBeBinary := TRUE
  6674. | Scanner.DotTimes: mustBeBinary := TRUE
  6675. | Scanner.PlusTimes: mustBeBinary := TRUE
  6676. | Scanner.Slash: mustBeBinary := TRUE
  6677. | Scanner.Backslash: mustBeBinary := TRUE
  6678. | Scanner.DotSlash: mustBeBinary := TRUE
  6679. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6680. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6681. | Scanner.Not: mustBeUnary := TRUE
  6682. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6683. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6684. | Scanner.Transpose: mustBeUnary := TRUE;
  6685. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6686. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6687. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6688. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6689. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6690. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6691. | Global.Abs: mustBeUnary := TRUE;
  6692. | Global.Ash: (* TODO: arity? *)
  6693. | Global.Cap: (* TODO: arity? *)
  6694. | Global.Chr: mustBeUnary := TRUE;
  6695. | Global.Entier: (* TODO: arity? *)
  6696. | Global.EntierH: (* TODO: arity? *)
  6697. | Global.Len: (* unary and binary *)
  6698. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6699. | Global.Max, Global.Min: (* unary and binary *)
  6700. | Global.Odd: (* TODO: arity? *)
  6701. | Global.Sum: (* TODO: arity? *)
  6702. | Global.All: (* TODO: arity? *)
  6703. | Global.Re, Global.Im:
  6704. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6705. | Scanner.Alias:
  6706. | Scanner.GreaterGreater, Scanner.LessLess:
  6707. mustBeBinary := TRUE; hasReturnType := FALSE;
  6708. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6709. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6710. END;
  6711. (* check parameter count *)
  6712. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6713. Error(operator.position,"operator is not unary")
  6714. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6715. Error(operator.position,"operator is not binary")
  6716. END;
  6717. (* check parameter types *)
  6718. (* TODO: is this used at all? *)
  6719. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6720. leftType := procedureType.firstParameter.type;
  6721. rightType := procedureType.firstParameter.nextParameter.type;
  6722. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6723. Error(operator.position, "the two operands are not of the same type")
  6724. END
  6725. END;
  6726. (* check return type *)
  6727. IF hasReturnType THEN
  6728. IF procedureType.returnType = NIL THEN
  6729. Error(operator.position, "return type required")
  6730. ELSIF mustReturnBoolean THEN
  6731. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6732. Error(operator.position,"return type is not Boolean")
  6733. END
  6734. ELSIF mustReturnInteger THEN
  6735. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6736. Error(operator.position,"return type is not integer")
  6737. END
  6738. END
  6739. ELSIF procedureType.returnType # NIL THEN
  6740. Error(operator.position, "return type not allowed")
  6741. END
  6742. END
  6743. END
  6744. END
  6745. END VisitOperator;
  6746. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6747. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6748. BEGIN
  6749. IF error THEN RETURN FALSE END;
  6750. prevScope := currentScope;
  6751. prevDiagnostics := diagnostics;
  6752. diagnostics := NIL; (* suppress error output *)
  6753. currentScope := module.moduleScope;
  6754. VisitImport(x);
  6755. IF ~error THEN
  6756. module.moduleScope.AddImport(x);
  6757. x.SetScope(module.moduleScope);
  6758. END;
  6759. currentScope := prevScope;
  6760. diagnostics := prevDiagnostics;
  6761. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6762. END AddImport;
  6763. (** check and resolve import
  6764. - check for name = SYSTEM
  6765. - check for forbidden self import
  6766. - search through global import cache: already imported?
  6767. - check if already imported indirectly
  6768. - import if necessary -> set module and enter into import cache
  6769. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6770. - after this import this direct import and all indirect imports are stored in the current module's import list
  6771. **)
  6772. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6773. VAR
  6774. module: SyntaxTree.Module;
  6775. moduleScope: SyntaxTree.ModuleScope;
  6776. import,reimport: SyntaxTree.Import;
  6777. filename: FileName;
  6778. prevScope: SyntaxTree.Scope;
  6779. BEGIN
  6780. IF SymbolNeedsResolution(x) THEN
  6781. prevScope := currentScope;
  6782. x.SetType(SyntaxTree.importType);
  6783. moduleScope := currentScope.ownerModule.moduleScope;
  6784. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6785. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6786. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6787. Error(x.position,"forbidden self import");
  6788. ELSE
  6789. (* search through global import list: already imported ? *)
  6790. IF (x.module = NIL) & (importCache # NIL) THEN
  6791. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6792. ELSE import := NIL
  6793. END;
  6794. IF x.module # NIL THEN (* already imported indirectly *)
  6795. module := x.module;
  6796. ELSIF import # NIL THEN (* already in module list *)
  6797. module := import.module;
  6798. ASSERT(module # NIL);
  6799. x.SetModule(module);
  6800. ELSE (* must be imported *)
  6801. Global.ModuleFileName(x.moduleName,x.context,filename);
  6802. IF symbolFileFormat # NIL THEN
  6803. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6804. IF module = NIL THEN
  6805. ErrorSS(x.position,"could not import",filename);
  6806. IF VerboseErrorMessage THEN
  6807. Printout.Info("import",x)
  6808. END
  6809. ELSE
  6810. (*
  6811. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6812. (*! should rather be done by importer *)
  6813. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6814. checker.importCache := importCache;
  6815. checker.arrayBaseImported := arrayBaseImported;
  6816. checker.global := global;
  6817. checker.Module(module); (* semantic check *)
  6818. error := error OR checker.error;
  6819. END;
  6820. *)
  6821. (*
  6822. ASSERT(SyntaxTree.Resolved IN module.state);
  6823. *)
  6824. x.SetModule(module);
  6825. IF importCache # NIL THEN
  6826. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6827. import.SetContext(x.context);
  6828. import.SetModule(module);
  6829. importCache.AddImport(import);
  6830. END;
  6831. END;
  6832. ELSE
  6833. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6834. END;
  6835. END;
  6836. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6837. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6838. END;
  6839. import := module.moduleScope.firstImport;
  6840. WHILE(import # NIL) DO
  6841. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6842. ASSERT(currentScope # NIL);
  6843. ASSERT(currentScope.ownerModule # NIL);
  6844. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6845. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6846. Error(x.position,"recursive import");
  6847. ELSE
  6848. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6849. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6850. IF reimport = NIL THEN (* indirect import *)
  6851. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6852. reimport.SetContext(import.context);
  6853. reimport.SetModule(import.module);
  6854. moduleScope.AddImport(reimport);
  6855. reimport.SetScope(moduleScope);
  6856. ELSE
  6857. ASSERT(import.module # NIL);
  6858. reimport.SetModule(import.module); (* direct or indirect import *)
  6859. END;
  6860. END;
  6861. import := import.nextImport;
  6862. END;
  6863. END;
  6864. END;
  6865. currentScope := prevScope;
  6866. (* ELSE nothing to be done *)
  6867. x.SetState(SyntaxTree.Resolved);
  6868. END;
  6869. END VisitImport;
  6870. (*** statements ***)
  6871. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6872. VAR prev,resolved: SyntaxTree.Statement;
  6873. BEGIN
  6874. prev := resolvedStatement;
  6875. resolvedStatement := x;
  6876. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6877. activeCellsStatement := FALSE;
  6878. x.Accept(SELF);
  6879. (* removed this, implementation restriction should be resolved by backend
  6880. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6881. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6882. END;
  6883. *)
  6884. resolved := resolvedStatement;
  6885. resolvedStatement := prev;
  6886. RETURN resolved
  6887. END ResolveStatement;
  6888. (** check and resolve statement sequence
  6889. - check all statements, replace if necessary
  6890. **)
  6891. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6892. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6893. BEGIN
  6894. IF statementSequence # NIL THEN (* else empty *)
  6895. FOR i := 0 TO statementSequence.Length()-1 DO
  6896. statement := statementSequence.GetStatement(i);
  6897. resolved := ResolveStatement(statement);
  6898. IF (resolved # statement) THEN
  6899. statementSequence.SetStatement(i,resolved);
  6900. END;
  6901. END;
  6902. END;
  6903. END StatementSequence;
  6904. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6905. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6906. - check if procedure is callable
  6907. - check return type = NIL (otherwise must be assignment statement)
  6908. **)
  6909. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6910. VAR call: SyntaxTree.Designator;
  6911. BEGIN
  6912. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6913. call := procedureCall.call;
  6914. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6915. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6916. END;
  6917. call := ResolveDesignator(call);
  6918. IF call = SyntaxTree.invalidDesignator THEN
  6919. (* error already handled *)
  6920. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6921. (* inline call in a statement *)
  6922. ELSIF ~IsCallable(call) THEN
  6923. Error(procedureCall.position,"called object is not a procedure");
  6924. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6925. Error(procedureCall.position,"calling procedure with non-void return type");
  6926. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6927. END;
  6928. procedureCall.SetCall(call);
  6929. (*
  6930. IF call = SyntaxTree.invalidDesignator THEN
  6931. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6932. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6933. IF IsOberonInline(procedure) THEN
  6934. Warning(procedure.position,"call to inline proc");
  6935. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6936. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6937. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6938. resolvedStatement := block;
  6939. RETURN;
  6940. END;
  6941. END;
  6942. *)
  6943. END VisitProcedureCallStatement;
  6944. (** check and resolve assignment LHS := RHS
  6945. - resolve LHS and RHS
  6946. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6947. - check if assignment is compatible
  6948. - check if LHS is variable (i.e. assignable)
  6949. - convert RHS if necessary
  6950. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6951. - assignment between different ASOTs
  6952. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6953. - assignment to ASOT elements:
  6954. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6955. **)
  6956. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6957. VAR
  6958. left: SyntaxTree.Designator;
  6959. right, expression: SyntaxTree.Expression;
  6960. designator: SyntaxTree.Designator;
  6961. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6962. mathArrayType: SyntaxTree.MathArrayType;
  6963. BEGIN
  6964. right := ResolveExpression(assignment.right);
  6965. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6966. left := ResolveDesignator(assignment.left);
  6967. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6968. (* error already handled *)
  6969. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6970. (* LHS is index write operator call on ASOT *)
  6971. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6972. (* necessary ?
  6973. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6974. type := procedureType.firstParameter.type;
  6975. expression := procedureCallDesignator.parameters.GetExpression(0);
  6976. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6977. *)
  6978. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6979. ELSIF CheckVariable(left) THEN
  6980. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6981. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6982. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6983. (* conversion done by procedure call
  6984. (* try to convert to left argument *)
  6985. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6986. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6987. procedureCallDesignator.parameters.SetExpression(1, right);
  6988. END;
  6989. *)
  6990. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6991. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6992. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6993. ELSIF AssignmentCompatible(left, right) THEN
  6994. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6995. mathArrayType := MathArrayStructureOfType(left.type);
  6996. right := NewConversion(right.position, right, mathArrayType, NIL);
  6997. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6998. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6999. ELSE
  7000. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7001. assignment.SetLeft(left);
  7002. assignment.SetRight(right);
  7003. resolvedStatement := assignment
  7004. END
  7005. END
  7006. END
  7007. END VisitAssignment;
  7008. (** check and resolve assignment LHS := RHS
  7009. - resolve LHS and RHS
  7010. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7011. - check if assignment is compatible
  7012. - check if LHS is variable (i.e. assignable)
  7013. - convert RHS if necessary
  7014. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7015. - assignment between different ASOTs
  7016. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7017. - assignment to ASOT elements:
  7018. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7019. **)
  7020. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7021. VAR
  7022. left: SyntaxTree.Designator;
  7023. right: SyntaxTree.Expression;
  7024. inPort, outPort: SyntaxTree.PortType;
  7025. expression: SyntaxTree.Expression;
  7026. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7027. BEGIN
  7028. right := ResolveExpression(communication.right);
  7029. left := ResolveDesignator(communication.left);
  7030. communication.SetLeft(left);
  7031. communication.SetRight(right);
  7032. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7033. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7034. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7035. (* conversion done by procedure call
  7036. (* try to convert to left argument *)
  7037. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7038. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7039. procedureCallDesignator.parameters.SetExpression(1, right);
  7040. END;
  7041. *)
  7042. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7043. ELSE
  7044. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7045. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7046. (* error already handled *)
  7047. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7048. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7049. IF outPort.direction # SyntaxTree.OutPort THEN
  7050. Error(left.position,"not an out-port")
  7051. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7052. Error(left.position,"incompatible to port type");
  7053. ELSE
  7054. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7055. communication.SetRight(right)
  7056. END;
  7057. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7058. IF CheckVariable(left) THEN
  7059. IF inPort.direction # SyntaxTree.InPort THEN
  7060. Error(left.position,"not an in-port")
  7061. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7062. Error(right.position,"incompatible to port type");
  7063. END;
  7064. END;
  7065. ELSE
  7066. Error(communication.position,"unsupported stream operation");
  7067. END;
  7068. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7069. IF outPort.direction # SyntaxTree.OutPort THEN
  7070. Error(left.position,"not an out-port")
  7071. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7072. Error(left.position,"incompatible to port type");
  7073. ELSE
  7074. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7075. communication.SetRight(right)
  7076. END;
  7077. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7078. IF CheckVariable(right) THEN
  7079. IF inPort.direction # SyntaxTree.InPort THEN
  7080. Error(left.position,"not an in-port")
  7081. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7082. Error(right.position,"incompatible to port type");
  7083. END;
  7084. END;
  7085. ELSE
  7086. Error(communication.position, "unsupported operation");
  7087. END;
  7088. END;
  7089. END VisitCommunicationStatement;
  7090. (** check and resolve if/eslif part
  7091. - check condition
  7092. - check statement sequence
  7093. **)
  7094. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7095. VAR prevUnreachable, b: BOOLEAN;
  7096. BEGIN
  7097. prevUnreachable := currentIsUnreachable;
  7098. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7099. IF IsBooleanValue(ifPart.condition,b) THEN
  7100. IF b=FALSE THEN
  7101. currentIsUnreachable := TRUE
  7102. ELSIF b=TRUE THEN
  7103. true := TRUE
  7104. END;
  7105. END;
  7106. StatementSequence(ifPart.statements);
  7107. currentIsUnreachable := prevUnreachable;
  7108. END IfPart;
  7109. (** check and resolve if statement
  7110. - check if parts and else part statement sequence
  7111. **)
  7112. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7113. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7114. BEGIN
  7115. prevUnreachable := currentIsUnreachable;
  7116. ifPartTrue := FALSE;
  7117. IfPart(ifStatement.ifPart,ifPartTrue);
  7118. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7119. elsif := ifStatement.GetElsifPart(i);
  7120. IfPart(elsif,ifPartTrue);
  7121. END;
  7122. IF ifStatement.elsePart # NIL THEN
  7123. IF ifPartTrue THEN
  7124. currentIsUnreachable := TRUE
  7125. END;
  7126. StatementSequence(ifStatement.elsePart)
  7127. END;
  7128. currentIsUnreachable := prevUnreachable;
  7129. END VisitIfStatement;
  7130. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7131. VAR variable: SyntaxTree.Designator;
  7132. type,variableType: SyntaxTree.Type;
  7133. withEntry: WithEntry;
  7134. BEGIN
  7135. variable := ResolveDesignator(withPart.variable);
  7136. variableType := variable.type.resolved;
  7137. withPart.SetVariable(variable);
  7138. type := ResolveType(withPart.type);
  7139. withPart.SetType(type);
  7140. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7141. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7142. END;
  7143. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7144. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7145. Error(variable.position,"is not extensible designator");
  7146. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7147. Error(variable.position,"is no local variable ");
  7148. IF VerboseErrorMessage THEN
  7149. Printout.Info("variable",variable)
  7150. END;
  7151. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7152. Error(variable.position,"withguarded symbol is no type extension of ");
  7153. IF VerboseErrorMessage THEN
  7154. Printout.Info("variable",variable);
  7155. Printout.Info("type",type);
  7156. END;
  7157. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7158. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7159. Error(variable.position,"withguarded symbol is no variable ");
  7160. IF VerboseErrorMessage THEN
  7161. Printout.Info("variable",variable);
  7162. Printout.Info("type",type);
  7163. END;
  7164. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7165. Error(variable.position,"invalid change of withguarded symbol");
  7166. ELSE
  7167. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7168. NEW(withEntry);
  7169. withEntry.previous := withEntries;
  7170. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7171. withEntry.type := type;
  7172. withEntries := withEntry;
  7173. StatementSequence(withPart.statements);
  7174. withEntries := withEntries.previous;
  7175. END;
  7176. END WithPart;
  7177. (** check and resolve with statement WITH variable: type DO ... END;
  7178. - check type and variable
  7179. - check that variable type is type extension of type
  7180. - check that variable is a variable
  7181. - enter new with scope and enter guardedVariable with same name and reference to variable
  7182. - create if statement:
  7183. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7184. **)
  7185. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7186. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7187. BEGIN
  7188. prevScope := currentScope; symbol := NIL;
  7189. FOR i := 0 TO withStatement.WithParts()-1 DO
  7190. WithPart(withStatement.GetWithPart(i),symbol);
  7191. END;
  7192. IF withStatement.elsePart # NIL THEN
  7193. StatementSequence(withStatement.elsePart)
  7194. END;
  7195. currentScope := prevScope;
  7196. END VisitWithStatement;
  7197. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7198. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7199. - check 'first' < 'last' and no overlaps between different case labels
  7200. - check statement sequence
  7201. **)
  7202. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7203. VAR
  7204. i: LONGINT;
  7205. position: Position;
  7206. expression, left, right: SyntaxTree.Expression;
  7207. expressionType: SyntaxTree.Type;
  7208. l, r: Basic.Integer;
  7209. cl, cr: CHAR;
  7210. thiscases: SyntaxTree.CaseConstant;
  7211. BEGIN
  7212. thiscases := NIL;
  7213. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7214. expression := casePart.elements.GetExpression(i);
  7215. position := expression.position;
  7216. (* set context of range *)
  7217. IF expression IS SyntaxTree.RangeExpression THEN
  7218. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7219. END;
  7220. expression := ResolveExpression(expression);
  7221. IF expression = SyntaxTree.invalidExpression THEN
  7222. (* error already reported *)
  7223. expressionType := SyntaxTree.invalidType;
  7224. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7225. (* read out 'first' and 'last' *)
  7226. left := expression(SyntaxTree.RangeExpression).first;
  7227. right := expression(SyntaxTree.RangeExpression).last;
  7228. (* guaranteed by VisitRangeExpression: *)
  7229. ASSERT((left # NIL) & (right # NIL));
  7230. ASSERT(left.type.resolved = right.type.resolved);
  7231. left := CompatibleConversion(left.position, left, type);
  7232. right := CompatibleConversion(right.position, right, type);
  7233. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7234. expression(SyntaxTree.RangeExpression).SetLast(right);
  7235. expressionType := RegularType(position,left.type);
  7236. ELSE
  7237. expression := ConstantExpression(expression);
  7238. expression := CompatibleConversion(expression.position, expression, type);
  7239. (*
  7240. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7241. left := Global.NewCharacterValue(system,expression.position,cl);
  7242. expression := casePart.elements.GetExpression(i);
  7243. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7244. expression := left
  7245. END;
  7246. *)
  7247. casePart.elements.SetExpression(i,expression);
  7248. left := expression; right := expression;
  7249. expressionType := RegularType(position,expression.type)
  7250. END;
  7251. IF (expressionType = SyntaxTree.invalidType) THEN
  7252. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7253. Error(position, "inadmissible case label");
  7254. expression := SyntaxTree.invalidExpression;
  7255. ELSE
  7256. l := 0; r := 0;
  7257. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7258. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7259. l := ORD(cl); r := ORD(cr);
  7260. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7261. ELSE
  7262. expression := SyntaxTree.invalidExpression
  7263. END;
  7264. IF expression # SyntaxTree.invalidExpression THEN
  7265. IF l>r THEN
  7266. Error(position, "empty case label")
  7267. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7268. Error(position, "duplicate case label");
  7269. ELSE
  7270. IF l < min THEN min := l END;
  7271. IF r > max THEN max := r END;
  7272. END;
  7273. END;
  7274. END;
  7275. casePart.elements.SetExpression(i,expression);
  7276. END;
  7277. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7278. casePart.SetConstants(thiscases);
  7279. StatementSequence(casePart.statements);
  7280. END CasePart;
  7281. (** check and resolve case statement CASE variable OF ... END;
  7282. - check variable
  7283. - check case parts
  7284. **)
  7285. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7286. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7287. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer; msg: ARRAY 64 OF CHAR;
  7288. BEGIN
  7289. expression := ResolveExpression(caseStatement.variable);
  7290. type := RegularType(expression.position,expression.type);
  7291. IF type = SyntaxTree.invalidType THEN
  7292. expression := SyntaxTree.invalidExpression;
  7293. ELSIF IsIntegerType(type) THEN
  7294. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7295. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7296. (*
  7297. expression := Global.NewCharacterValue(system,expression.position,ch);
  7298. *)
  7299. type := expression.type;
  7300. ELSIF IsCharacterType(type) THEN
  7301. ELSIF IsEnumerationType(type) THEN
  7302. ELSE
  7303. Error(caseStatement.variable.position,"variable must be integer or character type");
  7304. expression := SyntaxTree.invalidExpression;
  7305. END;
  7306. caseStatement.SetVariable(expression);
  7307. caseList := NIL;
  7308. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7309. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7310. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7311. END;
  7312. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7313. msg := "huge sparse case table ";
  7314. Strings.AppendInt(msg, max-min);
  7315. Strings.Append(msg,"/");
  7316. Strings.AppendInt(msg, caseStatement.CaseParts());
  7317. Warning(caseStatement.position,msg);
  7318. END;
  7319. caseStatement.SetMinMax(min,max);
  7320. StatementSequence(caseStatement.elsePart);
  7321. IF expression.resolved # NIL THEN
  7322. IF IsCharacterValue(expression,ch) THEN
  7323. l := ORD(ch)
  7324. ELSIF IsIntegerValue(expression,l) THEN
  7325. END;
  7326. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7327. END;
  7328. END VisitCaseStatement;
  7329. (** check and resolve while statement
  7330. - check condition
  7331. - check statement sequence
  7332. **)
  7333. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7334. VAR prevIsUnreachable,b: BOOLEAN;
  7335. BEGIN
  7336. prevIsUnreachable := currentIsUnreachable;
  7337. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7338. IF IsBooleanValue(whileStatement.condition,b) THEN
  7339. IF b=FALSE THEN
  7340. currentIsUnreachable := TRUE
  7341. END;
  7342. END;
  7343. StatementSequence(whileStatement.statements);
  7344. currentIsUnreachable := prevIsUnreachable
  7345. END VisitWhileStatement;
  7346. (** check and resolve repeat statement
  7347. - check condition
  7348. - check statement sequence
  7349. **)
  7350. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7351. BEGIN
  7352. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7353. StatementSequence(repeatStatement.statements);
  7354. END VisitRepeatStatement;
  7355. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7356. VAR withEntry: WithEntry;
  7357. BEGIN
  7358. withEntry := withEntries;
  7359. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7360. withEntry := withEntry.previous
  7361. END;
  7362. IF withEntry = NIL THEN RETURN FALSE
  7363. ELSE
  7364. type := withEntry.type;
  7365. RETURN TRUE
  7366. END;
  7367. END GetGuard;
  7368. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7369. - check that variable is an integer variable
  7370. - check that from is integer typed with compatible type
  7371. - check that to has compatible type
  7372. - check that by is constant integer with compatible type
  7373. **)
  7374. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7375. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7376. BEGIN
  7377. designator := ResolveDesignator(forStatement.variable);
  7378. type := SyntaxTree.invalidType;
  7379. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7380. designator := SyntaxTree.invalidDesignator;
  7381. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7382. Error(designator.position,"control variable of non-integer type");
  7383. designator := SyntaxTree.invalidDesignator;
  7384. ELSIF CheckVariable(designator) THEN
  7385. type := designator.type;
  7386. END;
  7387. forStatement.SetVariable(designator);
  7388. expression := ResolveExpression(forStatement.from);
  7389. IF expression = SyntaxTree.invalidExpression THEN
  7390. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7391. Error(expression.position,"start value of incompatible type");
  7392. expression := SyntaxTree.invalidExpression;
  7393. ELSIF type # SyntaxTree.invalidType THEN
  7394. expression := NewConversion(expression.position,expression,type,NIL)
  7395. END;
  7396. forStatement.SetFrom(expression);
  7397. expression := ResolveExpression(forStatement.to);
  7398. IF expression = SyntaxTree.invalidExpression THEN
  7399. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7400. Error(expression.position,"end value of incompatible type");
  7401. expression := SyntaxTree.invalidExpression;
  7402. ELSIF type # SyntaxTree.invalidType THEN
  7403. expression := NewConversion(expression.position,expression,type,NIL)
  7404. END;
  7405. forStatement.SetTo(expression);
  7406. IF forStatement.by # NIL THEN
  7407. expression := ConstantInteger(forStatement.by);
  7408. ELSE
  7409. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7410. END;
  7411. IF expression = SyntaxTree.invalidExpression THEN
  7412. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7413. Error(expression.position,"step value of incompatible type");
  7414. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7415. Error(expression.position,"invalid step value");
  7416. ELSIF type # SyntaxTree.invalidType THEN
  7417. expression := NewConversion(expression.position,expression,type,NIL)
  7418. END;
  7419. forStatement.SetBy(expression);
  7420. StatementSequence(forStatement.statements);
  7421. END VisitForStatement;
  7422. (** check and resolve loop statement LOOP StatementSequence END
  7423. - check statement sequence
  7424. **)
  7425. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7426. BEGIN
  7427. StatementSequence(loopStatement.statements)
  7428. END VisitLoopStatement;
  7429. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7430. BEGIN
  7431. StatementSequence(exitableBlock.statements);
  7432. END VisitExitableBlock;
  7433. (** check and resolve exit statement EXIT
  7434. - check that exit is within LOOP statement block
  7435. **)
  7436. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7437. VAR outer: SyntaxTree.Statement;
  7438. BEGIN
  7439. outer := exitStatement.outer;
  7440. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7441. outer := outer.outer;
  7442. END;
  7443. IF outer = NIL THEN
  7444. Error(exitStatement.position,"exit statement not within loop statement");
  7445. END;
  7446. END VisitExitStatement;
  7447. (** check and resolve return statement RETURN [expression]
  7448. - check expression (if any)
  7449. - check if in procedure scope
  7450. - if in procedure scope then check expression compatibility
  7451. - if not in procecdure scope then check on return without expression
  7452. **)
  7453. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7454. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7455. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7456. BEGIN
  7457. position := returnStatement.position;
  7458. expression := returnStatement.returnValue;
  7459. IF expression # NIL THEN
  7460. expression := ResolveExpression(expression);
  7461. returnStatement.SetReturnValue(expression);
  7462. END;
  7463. outer := returnStatement.outer;
  7464. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7465. outer := outer.outer
  7466. END;
  7467. IF (outer # NIL) THEN
  7468. scope := outer(SyntaxTree.Body).inScope;
  7469. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7470. IF (expression # NIL) THEN
  7471. Error(position, "return statement with parameter not in procedure scope");
  7472. END;
  7473. ELSE
  7474. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7475. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7476. Error(position, "return statement in procedure that does not return");
  7477. END;
  7478. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7479. IF returnType # NIL THEN
  7480. returnType := returnType.resolved;
  7481. IF expression = NIL THEN
  7482. Error(position, "empty return type in procedure providing a return type")
  7483. ELSIF expression.type = NIL THEN
  7484. Error(position,"returned type incompatible: expression has no type");
  7485. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7486. Error(position, "return type not compatible");
  7487. IF VerboseErrorMessage THEN
  7488. Printout.Info("returnType",returnType);
  7489. Printout.Info("expression",expression);
  7490. END;
  7491. ELSE
  7492. expression := NewConversion(expression.position,expression,returnType,NIL);
  7493. returnStatement.SetReturnValue(expression);
  7494. END;
  7495. ELSIF expression # NIL THEN
  7496. Error(position, "non-empty return type in procedure providing no return type");
  7497. END;
  7498. END;
  7499. END;
  7500. END VisitReturnStatement;
  7501. (** check and resolve await statement AWAIT(condition: Expression)
  7502. - check await condition
  7503. **)
  7504. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7505. VAR condition: SyntaxTree.Expression;
  7506. BEGIN
  7507. condition := ResolveCondition(awaitStatement.condition);
  7508. IF currentIsRealtime THEN
  7509. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7510. END;
  7511. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7512. Error(awaitStatement.position,"senseless await statement with constant condition");
  7513. END;
  7514. awaitStatement.SetCondition(condition);
  7515. END VisitAwaitStatement;
  7516. PROCEDURE CheckSystemImport(position: Position);
  7517. VAR import: SyntaxTree.Import;
  7518. BEGIN
  7519. import := currentScope.ownerModule.moduleScope.firstImport;
  7520. WHILE(import # NIL) DO
  7521. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7522. RETURN;
  7523. END;
  7524. import := import.nextImport;
  7525. END;
  7526. Error(position, "forbidden code without system import");
  7527. END CheckSystemImport;
  7528. (** check and resolve code statement: do nothing, must be done by assembler
  7529. **)
  7530. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7531. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7532. BEGIN
  7533. CheckSystemImport(code.position);
  7534. FOR i := 0 TO code.inRules.Length()-1 DO
  7535. statement := code.inRules.GetStatement(i);
  7536. IF statement IS SyntaxTree.Assignment THEN
  7537. WITH statement: SyntaxTree.Assignment DO
  7538. statement.SetRight(ResolveExpression(statement.right));
  7539. END;
  7540. ELSE
  7541. Error(statement.position, "can only be assignment")
  7542. END;
  7543. END;
  7544. FOR i := 0 TO code.outRules.Length()-1 DO
  7545. statement := code.outRules.GetStatement(i);
  7546. IF statement IS SyntaxTree.Assignment THEN
  7547. WITH statement: SyntaxTree.Assignment DO
  7548. statement.SetLeft(ResolveDesignator(statement.left));
  7549. END;
  7550. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7551. (* must be a reference to some register *)
  7552. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7553. ELSE
  7554. Printout.Info("out statement ", statement);
  7555. Error(statement.position, "(out) can only be assignment")
  7556. END;
  7557. END;
  7558. END VisitCode;
  7559. (** check and set flags of a statement block
  7560. - check for multiply occurence of a flag
  7561. - check and set priority only in bodies
  7562. - check for valid names
  7563. **)
  7564. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7565. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7566. flag: LONGINT; recordBody: SyntaxTree.Body;
  7567. PROCEDURE SetProtectedRecord;
  7568. VAR scope: SyntaxTree.Scope;
  7569. BEGIN
  7570. scope := currentScope;
  7571. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7572. scope := scope.outerScope
  7573. END;
  7574. IF scope # NIL THEN
  7575. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7576. END;
  7577. END SetProtectedRecord;
  7578. BEGIN
  7579. flags := {};
  7580. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7581. recordBody := block(SyntaxTree.Body)
  7582. ELSE
  7583. recordBody := NIL
  7584. END;
  7585. blockModifier := block.blockModifiers;
  7586. WHILE(blockModifier # NIL) DO
  7587. name := blockModifier.identifier;
  7588. expression := blockModifier.expression;
  7589. position := blockModifier.position;
  7590. flag := -1;
  7591. IF name=Global.NamePriority THEN
  7592. IF expression = NIL THEN
  7593. Error(position, "missing priority expression");
  7594. ELSIF recordBody = NIL THEN
  7595. Error(position, "priority not on record body");
  7596. ELSIF recordBody.priority # NIL THEN
  7597. Error(position, "duplicate priority expression");
  7598. ELSE
  7599. recordBody.SetPriority(expression);
  7600. END;
  7601. ELSIF expression # NIL THEN
  7602. Error(expression.position,"expression not in connection with priority")
  7603. ELSIF name=Global.NameExclusive THEN
  7604. IF block.isExclusive THEN
  7605. Error(position, "duplicate exclusive flag")
  7606. END;
  7607. block.SetExclusive(TRUE); SetProtectedRecord;
  7608. ELSIF name=Global.NameActive THEN
  7609. IF recordBody = NIL THEN
  7610. Error(position, "active not in record body");
  7611. ELSIF recordBody.isActive THEN
  7612. Error(position, "duplicate active flag")
  7613. ELSE
  7614. recordBody.SetActive(TRUE); SetProtectedRecord;
  7615. END;
  7616. ELSIF name=Global.NameSafe THEN
  7617. IF recordBody = NIL THEN
  7618. Error(position, "safe not in record body");
  7619. ELSIF recordBody.isSafe THEN
  7620. Error(position, "duplicate safe flag")
  7621. ELSE
  7622. recordBody.SetSafe(TRUE);
  7623. SetProtectedRecord;
  7624. END;
  7625. ELSIF name=Global.NameRealtime THEN
  7626. IF recordBody = NIL THEN
  7627. Error(position, "realtime not in record body");
  7628. ELSIF recordBody.isRealtime THEN
  7629. Error(position, "duplicate realtime flag")
  7630. ELSE
  7631. recordBody.SetRealtime(TRUE);
  7632. block.SetRealtime(TRUE);
  7633. END;
  7634. ELSIF name=Global.NameUnchecked THEN
  7635. IF block.isUnchecked THEN
  7636. Error(position, "duplicate unchecked flag")
  7637. ELSE
  7638. block.SetUnchecked(TRUE);
  7639. END;
  7640. ELSIF (name=Global.NameUncooperative) THEN
  7641. IF block.isUncooperative THEN
  7642. Error(position, "duplicate uncooperative flag")
  7643. ELSE
  7644. block.SetUncooperative(TRUE);
  7645. END;
  7646. ELSE
  7647. Error(position, "unknown block modifier");
  7648. END;
  7649. blockModifier := blockModifier.nextModifier;
  7650. END;
  7651. END BlockFlags;
  7652. (** check and resolve statement block
  7653. - check flags (exclusive)
  7654. - check statement sequence
  7655. **)
  7656. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7657. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7658. BEGIN
  7659. BlockFlags(statementBlock);
  7660. IF statementBlock.isExclusive THEN
  7661. (* check that not in exclusive block *)
  7662. IF currentIsExclusive THEN
  7663. Error (statementBlock.position,"forbidden recursive exclusive")
  7664. ELSIF currentIsRealtime THEN
  7665. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7666. END;
  7667. END;
  7668. recentExclusive := currentIsExclusive;
  7669. recentUnreachable := currentIsUnreachable;
  7670. recentRealtime := currentIsRealtime;
  7671. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7672. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7673. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7674. StatementSequence(statementBlock.statements);
  7675. currentIsRealtime := recentRealtime;
  7676. currentIsExclusive := recentExclusive;
  7677. currentIsUnreachable := recentUnreachable;
  7678. END VisitStatementBlock;
  7679. (** check and resolve body
  7680. - check flags (active, priority, safe)
  7681. - check body and finally part
  7682. **)
  7683. PROCEDURE Body(body: SyntaxTree.Body);
  7684. BEGIN
  7685. VisitStatementBlock(body);
  7686. IF body.isActive THEN
  7687. IF ~currentIsBodyProcedure THEN
  7688. Error(body.position,"active flag not in object body");
  7689. ELSIF body.priority # NIL THEN
  7690. body.SetPriority(ConstantInteger(body.priority));
  7691. END;
  7692. ELSIF body.isSafe THEN
  7693. Error(body.position,"safe flag not in active body");
  7694. ELSIF body.priority # NIL THEN
  7695. Error(body.position,"priority flag not in active body");
  7696. END;
  7697. IF body.code # NIL THEN
  7698. CheckSystemImport(body.position);
  7699. END;
  7700. StatementSequence(body.finally)
  7701. END Body;
  7702. (*** scopes ***)
  7703. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7704. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7705. VAR duplicateSymbol: BOOLEAN;
  7706. BEGIN
  7707. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7708. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7709. Error(symbol.position,"globally defined keyword")
  7710. END;
  7711. scope.EnterSymbol(symbol,duplicateSymbol);
  7712. IF ~allowDuplicate & duplicateSymbol THEN
  7713. Error(symbol.position,"Multiply defined identifier.");
  7714. IF VerboseErrorMessage THEN
  7715. Printout.Info("multiply defined identifier",symbol);
  7716. Printout.Info("in scope",scope);
  7717. END;
  7718. END;
  7719. END Register;
  7720. (**
  7721. implementation: check and resolve an implementation part
  7722. **)
  7723. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7724. move implementation checker to a separate object ? *)
  7725. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7726. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7727. BEGIN
  7728. prevIsRealtime := currentIsRealtime;
  7729. prevIsBodyProcedure := currentIsBodyProcedure;
  7730. prevIsCellNet := currentIsCellNet;
  7731. prevScope := currentScope;
  7732. currentScope := scope;
  7733. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7734. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7735. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7736. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7737. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7738. (*
  7739. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7740. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7741. END;
  7742. *)
  7743. END;
  7744. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7745. (* module body, record bodies are wrapped into an artifical procedure *)
  7746. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7747. Body(scope(SyntaxTree.ProcedureScope).body)
  7748. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7749. Body(scope(SyntaxTree.ProcedureScope).body)
  7750. END;
  7751. END;
  7752. currentScope := prevScope;
  7753. currentIsRealtime := prevIsRealtime;
  7754. currentIsBodyProcedure := prevIsBodyProcedure;
  7755. currentIsCellNet := prevIsCellNet;
  7756. END Implementation;
  7757. (** implementation phase:
  7758. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7759. **)
  7760. PROCEDURE Implementations(x: SyntaxTree.Module);
  7761. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7762. BEGIN
  7763. prevPhase := phase;
  7764. phase := InlinePhase;
  7765. scope := x.firstScope;
  7766. WHILE(scope # NIL) DO
  7767. Implementation(scope);
  7768. scope := scope.nextScope;
  7769. END;
  7770. phase := ImplementationPhase;
  7771. scope := x.firstScope;
  7772. WHILE(scope # NIL) DO
  7773. Implementation(scope);
  7774. scope := scope.nextScope;
  7775. END;
  7776. phase := prevPhase;
  7777. END Implementations;
  7778. (** declaration phase:
  7779. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7780. - import lists (for module scopes)
  7781. - parameter list (for procedure scopes)
  7782. - constant declarations
  7783. - type declarations
  7784. - variable declarations
  7785. - procedure declarations
  7786. preformed in two stages:
  7787. - first all symbols are entered into the symbol table (with uniqueness check),
  7788. - then all symbols are resolved
  7789. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7790. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7791. phases :
  7792. 0 = before procedures
  7793. 1 = procedures and later
  7794. **)
  7795. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7796. VAR
  7797. constant: SyntaxTree.Constant;
  7798. typeDeclaration: SyntaxTree.TypeDeclaration;
  7799. declaredType: SyntaxTree.Type;
  7800. variable: SyntaxTree.Variable;
  7801. procedure: SyntaxTree.Procedure;
  7802. procedureType : SyntaxTree.ProcedureType;
  7803. prevScope: SyntaxTree.Scope;
  7804. parameter: SyntaxTree.Parameter;
  7805. import: SyntaxTree.Import;
  7806. symbol: SyntaxTree.Symbol;
  7807. prevPhase: LONGINT;
  7808. prevError : BOOLEAN;
  7809. i: LONGINT;
  7810. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7811. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7812. BEGIN
  7813. IF type.baseType # NIL THEN
  7814. baseType := type.baseType.resolved;
  7815. IF baseType IS SyntaxTree.PointerType THEN
  7816. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7817. END;
  7818. (*
  7819. IF baseType IS SyntaxTree.CellType THEN
  7820. DeclareCell(baseType(SyntaxTree.CellType));
  7821. END;
  7822. *)
  7823. END;
  7824. parameter := type.firstParameter;
  7825. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7826. (*
  7827. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7828. variable.SetType(parameter.type);
  7829. variable.SetAccess(SyntaxTree.Hidden);
  7830. variable.SetModifiers(parameter.modifiers);
  7831. currentScope.PushVariable(variable);
  7832. *)
  7833. Register(parameter,scope, FALSE);
  7834. parameter := parameter.nextParameter;
  7835. END;
  7836. property := type.firstProperty;
  7837. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7838. (*
  7839. variable := currentScope.FindVariable(property.name);
  7840. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7841. prop := variable(SyntaxTree.Property);
  7842. ELSE (* add, duplicate symbols detection later *)
  7843. prop := SyntaxTree.NewProperty(property.position, property.name);
  7844. currentScope.PushVariable(prop);
  7845. END;
  7846. prop.SetType(property.type);
  7847. prop.SetValue(property.value);
  7848. prop.SetAccess(SyntaxTree.Hidden);
  7849. *)
  7850. Register(property, scope, FALSE);
  7851. property := property.nextProperty;
  7852. END;
  7853. END DeclareCell;
  7854. BEGIN
  7855. prevError := error;
  7856. prevPhase := phase;
  7857. phase := DeclarationPhase;
  7858. prevScope := currentScope;
  7859. currentScope := scope;
  7860. error := FALSE;
  7861. IF 0 IN phases THEN
  7862. (* first enter all symbols in scope *)
  7863. IF scope IS SyntaxTree.ModuleScope THEN
  7864. (* treat imports first for a module scope, , set default context if necessary *)
  7865. import := scope(SyntaxTree.ModuleScope).firstImport;
  7866. WHILE(import # NIL) DO
  7867. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7868. Register(import, currentScope, FALSE);
  7869. import := import.nextImport;
  7870. END;
  7871. import := scope(SyntaxTree.ModuleScope).firstImport;
  7872. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7873. ResolveSymbol(import);
  7874. import := import.nextImport;
  7875. END;
  7876. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7877. (* enter parameters for a procedure scope *)
  7878. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7879. parameter := procedureType.firstParameter;
  7880. WHILE(parameter # NIL) DO
  7881. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7882. END;
  7883. parameter := procedureType.returnParameter;
  7884. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7885. parameter := procedureType.selfParameter;
  7886. IF parameter # NIL THEN
  7887. Register(parameter, currentScope, FALSE);
  7888. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7889. END;
  7890. ELSIF scope IS SyntaxTree.CellScope THEN
  7891. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7892. IF~skipImplementation THEN
  7893. import := scope(SyntaxTree.CellScope).firstImport;
  7894. WHILE(import # NIL) DO
  7895. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7896. Register(import, currentScope, FALSE);
  7897. import := import.nextImport;
  7898. END;
  7899. import := scope(SyntaxTree.CellScope).firstImport;
  7900. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7901. ResolveSymbol(import);
  7902. import := import.nextImport;
  7903. END;
  7904. END;
  7905. END;
  7906. IF error THEN RETURN END;
  7907. IF skipImplementation THEN
  7908. scope.Clear;
  7909. END;
  7910. (* constants *)
  7911. constant := scope.firstConstant;
  7912. WHILE (constant # NIL) DO
  7913. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7914. END;
  7915. (* type declarations *)
  7916. typeDeclaration := scope.firstTypeDeclaration;
  7917. WHILE (typeDeclaration # NIL) DO
  7918. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7919. END;
  7920. (* variables *)
  7921. variable := scope.firstVariable;
  7922. WHILE (variable # NIL) DO
  7923. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7924. END;
  7925. (* procedures *)
  7926. IF scope.procedures # NIL THEN
  7927. FOR i := 0 TO scope.procedures.Length()-1 DO
  7928. procedure := scope.procedures.GetProcedure(i);
  7929. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7930. IF procedureType.selfParameter = NIL THEN
  7931. scope.AddProcedure(procedure);
  7932. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7933. ELSE
  7934. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7935. IF typeDeclaration = NIL THEN
  7936. Error(procedureType.selfParameter.position, "No such type declaration");
  7937. ELSE
  7938. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7939. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7940. declaredType := typeDeclaration.declaredType;
  7941. IF declaredType IS SyntaxTree.PointerType THEN
  7942. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7943. END;
  7944. IF declaredType IS SyntaxTree.RecordType THEN
  7945. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7946. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7947. ELSE
  7948. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7949. END;
  7950. END;
  7951. END;
  7952. END;
  7953. END;
  7954. END;
  7955. (* now process all symbols without any presumption on the order *)
  7956. symbol := scope.firstSymbol;
  7957. WHILE(symbol # NIL) DO
  7958. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7959. IF (symbol IS SyntaxTree.Procedure) THEN
  7960. IF 1 IN phases THEN
  7961. ResolveSymbol(symbol);
  7962. END;
  7963. ELSE
  7964. IF 0 IN phases THEN
  7965. ResolveSymbol(symbol);
  7966. END;
  7967. END;
  7968. END;
  7969. symbol := symbol.nextSymbol;
  7970. END;
  7971. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7972. symbol := scope.firstSymbol;
  7973. WHILE symbol # NIL DO
  7974. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7975. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7976. pointerFixes.Add(symbol, currentScope);
  7977. END;
  7978. IF ~symbol.type.resolved.isRealtime THEN
  7979. Error(symbol.position,"symbol has no realtime type");
  7980. END;
  7981. END;
  7982. symbol := symbol.nextSymbol
  7983. END;
  7984. END;
  7985. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7986. Error(Basic.invalidPosition,"problems during offset computation in module");
  7987. END;
  7988. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7989. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7990. scope.ownerModule.AddScope(scope);
  7991. END;
  7992. phase := prevPhase;
  7993. currentScope := prevScope;
  7994. error := error OR prevError;
  7995. END Declarations;
  7996. (* nopov *)
  7997. (** check if all operators from one module are compatible to the ones in the other module
  7998. - check if there are not multiple operators with the same signature
  7999. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8000. - check for all operators whose signatures are compatible, whether the return types are compatible
  8001. note that:
  8002. - the return type is not considered to be part of the signature
  8003. - two signatures are considered compatible, if all of the operands are compatible
  8004. **)
  8005. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8006. VAR
  8007. thisOperator, thatOperator: SyntaxTree.Operator;
  8008. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8009. thisParameter, thatParameter: SyntaxTree.Parameter;
  8010. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8011. i: LONGINT;
  8012. BEGIN
  8013. currentScope := thisModuleScope;
  8014. hasError := FALSE;
  8015. (* go through all operators in the other module *)
  8016. thatOperator := thatModuleScope.firstOperator;
  8017. WHILE (thatOperator # NIL) & ~hasError DO
  8018. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8019. (* the other operator is accessible *)
  8020. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8021. (* the other operator is not the conversion operator *)
  8022. (* go through all operators in this module *)
  8023. thisOperator := thisModuleScope.firstOperator;
  8024. WHILE (thisOperator # NIL) & ~hasError DO
  8025. IF thisOperator # thatOperator THEN
  8026. (* the operators are not the same *)
  8027. IF thisOperator.name = thatOperator.name THEN
  8028. (* the operators share the same identifier *)
  8029. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8030. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8031. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8032. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8033. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8034. (* both operators have the same paramter count *)
  8035. thisParameter := thisProcedureType.firstParameter;
  8036. thatParameter := thatProcedureType.firstParameter;
  8037. operandsAreEqual := TRUE;
  8038. operandsAreCompatible := TRUE;
  8039. (* go through all parameters *)
  8040. FOR i := 1 TO thisProcedureType.numberParameters DO
  8041. ASSERT(thatParameter # NIL);
  8042. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8043. operandsAreEqual := FALSE;
  8044. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8045. operandsAreCompatible := FALSE
  8046. END
  8047. END;
  8048. thisParameter := thisParameter.nextParameter;
  8049. thatParameter := thatParameter.nextParameter
  8050. END;
  8051. IF operandsAreEqual THEN
  8052. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8053. hasError := TRUE
  8054. ELSIF operandsAreCompatible THEN
  8055. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8056. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8057. hasError := TRUE
  8058. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8059. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8060. hasError := TRUE
  8061. END
  8062. END
  8063. END
  8064. END
  8065. END;
  8066. thisOperator := thisOperator.nextOperator
  8067. END
  8068. END
  8069. END;
  8070. thatOperator := thatOperator.nextOperator
  8071. END
  8072. END CheckInterOperatorConformity;
  8073. (** check module:
  8074. - check module declaration
  8075. - add context, if necessary
  8076. - remove module from import cache, if necessary
  8077. - check declarations
  8078. - resolve all type fixes
  8079. - check implementation (bodies)
  8080. **)
  8081. PROCEDURE Module*(x: SyntaxTree.Module);
  8082. VAR (* nopov *)
  8083. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8084. BEGIN
  8085. prevScope := currentScope;
  8086. prevIsCellNet := currentIsCellNet;
  8087. module := x;
  8088. ASSERT(x # NIL);
  8089. global := system.globalScope[x.case];
  8090. x.moduleScope.SetGlobalScope(global);
  8091. currentScope := global;
  8092. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8093. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8094. RemoveModuleFromCache(importCache,x);
  8095. Declarations(x.moduleScope, FALSE, {0,1});
  8096. FixTypes();
  8097. IF module.isCellNet THEN
  8098. currentIsCellNet := TRUE;
  8099. modifier := x.modifiers;
  8100. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8101. CheckModifiers(modifier, FALSE);
  8102. END;
  8103. (* nopov *)
  8104. IF ~error THEN
  8105. (* check if operators conform to each other within this module *)
  8106. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8107. (* go through all imports *)
  8108. import := x.moduleScope.firstImport;
  8109. WHILE import # NIL DO
  8110. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8111. (* check if all operators in this module conform to the ones of the imported module *)
  8112. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8113. END;
  8114. import := import.nextImport
  8115. END;
  8116. END;
  8117. Implementations(x);
  8118. module := NIL;
  8119. currentIsCellNet := prevIsCellNet;
  8120. currentScope := prevScope;
  8121. END Module;
  8122. END Checker;
  8123. Warnings*=OBJECT (SyntaxTree.Visitor)
  8124. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8125. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8126. BEGIN
  8127. SELF.diagnostics := diagnostics
  8128. END InitWarnings;
  8129. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8130. BEGIN END VisitPortType;
  8131. (** types *)
  8132. PROCEDURE Type(x: SyntaxTree.Type);
  8133. BEGIN x.Accept(SELF)
  8134. END Type;
  8135. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8136. BEGIN END VisitType;
  8137. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8138. BEGIN END VisitBasicType;
  8139. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8140. BEGIN END VisitCharacterType;
  8141. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8142. BEGIN END VisitIntegerType;
  8143. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8144. BEGIN END VisitFloatType;
  8145. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8146. BEGIN END VisitQualifiedType;
  8147. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8148. BEGIN END VisitStringType;
  8149. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8150. BEGIN END VisitEnumerationType;
  8151. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8152. BEGIN END VisitRangeType;
  8153. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8154. BEGIN
  8155. IF ~(SyntaxTree.Warned IN x.state) THEN
  8156. x.SetState(SyntaxTree.Warned);
  8157. Type(x.arrayBase);
  8158. END;
  8159. END VisitArrayType;
  8160. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8161. BEGIN
  8162. IF ~(SyntaxTree.Warned IN x.state) THEN
  8163. x.SetState(SyntaxTree.Warned);
  8164. Type(x.arrayBase);
  8165. END;
  8166. END VisitMathArrayType;
  8167. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8168. BEGIN
  8169. IF ~(SyntaxTree.Warned IN x.state) THEN
  8170. x.SetState(SyntaxTree.Warned);
  8171. Type(x.pointerBase);
  8172. END;
  8173. END VisitPointerType;
  8174. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8175. BEGIN Scope(x.recordScope) END VisitRecordType;
  8176. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8177. BEGIN Scope(x.cellScope) END VisitCellType;
  8178. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8179. BEGIN END VisitProcedureType;
  8180. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8181. VAR msg: ARRAY 256 OF CHAR;
  8182. BEGIN
  8183. Global.GetSymbolName(x,msg);
  8184. Strings.Append(msg," ");
  8185. Strings.Append(msg,text);
  8186. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8187. END Warning;
  8188. (** symbols *)
  8189. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8190. BEGIN
  8191. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8192. IF ~(x IS SyntaxTree.Parameter) THEN
  8193. Warning(x,"never used");
  8194. END;
  8195. END;
  8196. x.Accept(SELF);
  8197. END Symbol;
  8198. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8199. BEGIN END VisitSymbol;
  8200. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8201. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8202. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8203. BEGIN END VisitConstant;
  8204. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8205. BEGIN END VisitVariable;
  8206. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8207. BEGIN END VisitProperty;
  8208. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8209. BEGIN END VisitParameter;
  8210. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8211. BEGIN
  8212. Scope(x.procedureScope)
  8213. END VisitProcedure;
  8214. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8215. BEGIN END VisitOperator;
  8216. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8217. BEGIN END VisitImport;
  8218. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8219. VAR
  8220. symbol: SyntaxTree.Symbol;
  8221. BEGIN
  8222. symbol := scope.firstSymbol;
  8223. WHILE(symbol # NIL) DO
  8224. Symbol(symbol);
  8225. symbol := symbol.nextSymbol;
  8226. END;
  8227. END Scope;
  8228. PROCEDURE Module*(x: SyntaxTree.Module);
  8229. BEGIN
  8230. SELF.module := x;
  8231. Scope(x.moduleScope);
  8232. END Module;
  8233. END Warnings;
  8234. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8235. BEGIN
  8236. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8237. END IsOberonInline;
  8238. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8239. BEGIN
  8240. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8241. END Resolved;
  8242. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8243. VAR i: LONGINT;
  8244. BEGIN
  8245. i := 1;
  8246. WHILE i < x DO
  8247. i := i *2
  8248. END;
  8249. RETURN i=x
  8250. END PowerOf2;
  8251. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8252. BEGIN
  8253. RETURN
  8254. (scope # NIL) &
  8255. (scope IS SyntaxTree.ModuleScope)
  8256. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8257. OR
  8258. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8259. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8260. END IsCellNetScope;
  8261. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8262. BEGIN
  8263. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8264. END IsCellScope;
  8265. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8266. BEGIN
  8267. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8268. RETURN (scope # NIL) & IsCellNetScope(scope)
  8269. END InCellNetScope;
  8270. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8271. BEGIN
  8272. ASSERT(size MOD system.dataUnit = 0);
  8273. RETURN size DIV system.dataUnit
  8274. END ToMemoryUnits;
  8275. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8276. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8277. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8278. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8279. BEGIN
  8280. IF t = NIL THEN
  8281. RETURN TRUE
  8282. ELSE
  8283. t := t.resolved;
  8284. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8285. END;
  8286. END TypeAllowed;
  8287. BEGIN
  8288. type := type.resolved;
  8289. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8290. RETURN FALSE
  8291. ELSE
  8292. procedureType := type(SyntaxTree.ProcedureType);
  8293. numberParameters := procedureType.numberParameters;
  8294. RETURN
  8295. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8296. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8297. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8298. END;
  8299. END GetProcedureAllowed;
  8300. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8301. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8302. VAR import: SyntaxTree.Import;
  8303. BEGIN
  8304. import := importCache.ImportByModuleName(x.name,x.context);
  8305. IF import # NIL THEN
  8306. importCache.RemoveImporters(x.name,x.context);
  8307. END;
  8308. END RemoveModuleFromCache;
  8309. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8310. (* to <- this assignment compatibility *)
  8311. VAR result: BOOLEAN;
  8312. BEGIN
  8313. IF this= NIL THEN result := (to=NIL)
  8314. ELSIF to=NIL THEN result := FALSE
  8315. ELSE
  8316. (*! will be replaced by this:
  8317. ELSE result := this.CompatibleTo(to.resolved);
  8318. *)
  8319. this := this.resolved; to := to.resolved;
  8320. IF to=SyntaxTree.invalidType THEN result := FALSE
  8321. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8322. ELSIF (to = this) OR (to.SameType(this)) THEN
  8323. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8324. ELSIF to IS SyntaxTree.BasicType THEN
  8325. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8326. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8327. result := this.CompatibleTo(to.resolved)
  8328. ELSE
  8329. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8330. END
  8331. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8332. result := to.sizeInBits >= this.sizeInBits;
  8333. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8334. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8335. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8336. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8337. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8338. result := TRUE;
  8339. ELSIF to IS SyntaxTree.AnyType THEN
  8340. 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);
  8341. ELSIF to IS SyntaxTree.ObjectType THEN
  8342. 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 *) ;
  8343. ELSIF to IS SyntaxTree.ByteType THEN
  8344. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8345. ELSIF to IS SyntaxTree.CharacterType THEN
  8346. result := IsCharacterType(this)
  8347. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8348. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8349. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8350. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8351. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8352. result := TRUE;
  8353. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8354. result := TRUE;
  8355. ELSE
  8356. result := FALSE
  8357. END;
  8358. ELSIF to IS SyntaxTree.PointerType THEN
  8359. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8360. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8361. & (~to.isRealtime OR this.isRealtime);
  8362. ELSIF to IS SyntaxTree.ProcedureType THEN
  8363. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8364. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8365. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8366. ELSIF to IS SyntaxTree.RecordType THEN
  8367. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8368. ELSIF to IS SyntaxTree.ArrayType THEN
  8369. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8370. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8371. ELSIF StaticArrayCompatible(to, this) THEN
  8372. result := TRUE
  8373. ELSE
  8374. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8375. END;
  8376. ELSIF to IS SyntaxTree.MathArrayType THEN
  8377. IF this IS SyntaxTree.MathArrayType THEN
  8378. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8379. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8380. result := TRUE;
  8381. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8382. result := TRUE;
  8383. ELSE
  8384. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8385. END;
  8386. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8387. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8388. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8389. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8390. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8391. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8392. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8393. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8394. ELSE
  8395. result := FALSE
  8396. END;
  8397. (* an array-structured object type is compatible to the type of its array structure *)
  8398. ELSIF IsArrayStructuredObjectType(this) THEN
  8399. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8400. ELSE
  8401. result := FALSE;
  8402. END;
  8403. ELSIF to IS SyntaxTree.StringType THEN
  8404. result := FALSE;
  8405. ELSIF to IS SyntaxTree.EnumerationType THEN
  8406. result := IsEnumerationExtension(this,to);
  8407. ELSIF to IS SyntaxTree.PortType THEN
  8408. result := SameType(to, this)
  8409. ELSE
  8410. Printout.Info("CompatibleTo",to);
  8411. HALT(100); (* implement missing type check *)
  8412. END;
  8413. END;
  8414. RETURN result
  8415. END CompatibleTo;
  8416. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8417. VAR actualBase, formalBase: SyntaxTree.Type;
  8418. BEGIN
  8419. IF SameType(formal,actual) THEN
  8420. RETURN TRUE
  8421. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8422. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8423. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8424. RETURN
  8425. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8426. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8427. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8428. & StaticArrayCompatible(formalBase,actualBase)
  8429. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8430. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8431. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8432. RETURN
  8433. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8434. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8435. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8436. & StaticArrayCompatible(formalBase,actualBase)
  8437. ELSE RETURN FALSE
  8438. END;
  8439. END StaticArrayCompatible;
  8440. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8441. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8442. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8443. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8444. BEGIN
  8445. result := SameType(formal,actual);
  8446. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8447. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8448. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8449. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8450. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8451. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8452. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8453. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8454. & TC(formalBase, actualBase);
  8455. END;
  8456. RETURN result
  8457. END TC;
  8458. BEGIN
  8459. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8460. ELSE
  8461. arrayBase := formalType.arrayBase.resolved;
  8462. IF (actualType IS SyntaxTree.StringType) THEN
  8463. result := arrayBase IS SyntaxTree.CharacterType
  8464. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8465. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8466. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8467. result := TC(formalType, actualType);
  8468. ELSE
  8469. result := (arrayBase IS SyntaxTree.ByteType)
  8470. END;
  8471. END;
  8472. RETURN result
  8473. END OpenArrayCompatible;
  8474. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8475. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8476. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8477. BEGIN
  8478. IF actualType IS SyntaxTree.MathArrayType THEN
  8479. actualArray := actualType(SyntaxTree.MathArrayType);
  8480. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8481. (*
  8482. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8483. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8484. *)
  8485. actualBase := ArrayBase(actualType,Infinity);
  8486. formalBase := ArrayBase(formalType,Infinity);
  8487. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8488. ELSE
  8489. (*
  8490. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8491. *)
  8492. formalBase := Resolved(formalType.arrayBase);
  8493. actualBase := Resolved(actualArray.arrayBase);
  8494. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8495. (*
  8496. ARRAY [k] -> ARRAY [n]
  8497. *)
  8498. result := (formalType.staticLength = actualArray.staticLength)
  8499. ELSE
  8500. result := TRUE
  8501. END;
  8502. IF ~result THEN
  8503. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8504. ELSIF actualBase = NIL THEN result := FALSE
  8505. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8506. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8507. ELSE
  8508. result := SameType(formalBase,actualBase)
  8509. END;
  8510. END;
  8511. ELSE
  8512. result := FALSE
  8513. END;
  8514. RETURN result
  8515. END MathArrayCompatible;
  8516. (**
  8517. Math Array Type distance for assignments / parameter passings of the form
  8518. from -> to
  8519. variants:
  8520. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8521. allowed:
  8522. static -> static (& size match)
  8523. static -> open
  8524. static -> tensor
  8525. open -> open
  8526. open -> tensor
  8527. open -> static
  8528. tensor -> tensor
  8529. tensor -> open
  8530. tensor -> static
  8531. **)
  8532. (*! think about the metric here: is form matching more important than element type matching? *)
  8533. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8534. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8535. BEGIN
  8536. fromBase := Resolved(from.arrayBase);
  8537. toBase := Resolved(to.arrayBase);
  8538. i := Infinity;
  8539. IF (from = to) OR (from.SameType(to)) THEN
  8540. i := 0;
  8541. ELSIF (from.form = to.form) THEN
  8542. (* static -> static, open -> open, tensor -> tensor *)
  8543. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8544. IF fromBase = toBase THEN i := 0
  8545. ELSIF toBase = NIL THEN i := 1
  8546. ELSIF toBase.SameType(fromBase) THEN i := 0
  8547. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8548. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8549. ELSE
  8550. i := TypeDistance(system,fromBase, toBase, varpar);
  8551. IF i < Infinity THEN i := i * 5 END;
  8552. END;
  8553. END;
  8554. ELSIF (to.form = SyntaxTree.Static) THEN
  8555. (* forbidden *)
  8556. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8557. (* static -> tensor, open -> tensor, tensor -> open *)
  8558. IF (toBase=fromBase) THEN i := 0;
  8559. ELSIF toBase = NIL THEN i := 1;
  8560. ELSIF toBase.SameType(fromBase) THEN i := 0
  8561. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8562. toBase := ArrayBase(toBase,Infinity);
  8563. IF (fromBase=toBase) THEN i := 0
  8564. ELSIF (toBase = NIL) THEN i:= 1
  8565. ELSIF (fromBase = NIL) THEN i := Infinity;
  8566. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8567. IF i < Infinity THEN i := i * 5 END;
  8568. END;
  8569. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8570. fromBase := ArrayBase(fromBase,Infinity);
  8571. IF (fromBase=toBase) THEN i := 0
  8572. ELSIF (toBase = NIL) THEN i := 1
  8573. ELSIF (fromBase = NIL) THEN i := Infinity;
  8574. ELSIF toBase.SameType(fromBase) THEN i := 0
  8575. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8576. IF i < Infinity THEN i := i * 5 END;
  8577. END;
  8578. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8579. IF i < Infinity THEN i := i * 5 END;
  8580. END;
  8581. IF i # Infinity THEN INC(i,2) END;
  8582. ELSIF (from.form = SyntaxTree.Static) THEN
  8583. (* static -> open *)
  8584. IF (toBase=fromBase) THEN i := 0;
  8585. ELSIF toBase = NIL THEN i := 1
  8586. ELSIF fromBase = NIL THEN i := Infinity
  8587. ELSIF toBase.SameType(fromBase) THEN i := 0
  8588. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8589. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8590. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8591. IF i < Infinity THEN i := i * 5 END;
  8592. END;
  8593. IF i # Infinity THEN INC(i,1) END;
  8594. ELSE HALT(100); (* unknown case *)
  8595. END;
  8596. RETURN i;
  8597. END MathArrayTypeDistance;
  8598. (** compute and return the distance of two array types
  8599. - return the distance of the base types
  8600. **)
  8601. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8602. VAR i: LONGINT;
  8603. BEGIN
  8604. i := Infinity;
  8605. IF from = to THEN
  8606. i := 0
  8607. ELSE
  8608. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8609. (*
  8610. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8611. i := TypeDistance(from.base, to.base);
  8612. IF i >= 0 THEN INC(i) END
  8613. ELSIF (from.mode = open) & (to.mode = open) THEN
  8614. i := TypeDistance(from.base, to.base);
  8615. *)
  8616. END;
  8617. RETURN i
  8618. END ArrayTypeDistance;
  8619. (** compute the signature distance of a procedure and an actual parameter list
  8620. - if any of the parameters are not compatible, the result is infinite
  8621. - add up and return the distance over all parameters
  8622. **)
  8623. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8624. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8625. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8626. BEGIN
  8627. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8628. result := Infinity
  8629. ELSE
  8630. formalParameter := procedureType.firstParameter;
  8631. i := 0;
  8632. result := 0;
  8633. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8634. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8635. actualParameter := actualParameters.GetExpression(i);
  8636. ASSERT(formalParameter.type # NIL);
  8637. IF (actualParameter.type = NIL) THEN distance := Infinity
  8638. ELSE
  8639. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8640. END;
  8641. IF distance = Infinity THEN
  8642. result := Infinity;
  8643. ELSE
  8644. to := formalParameter.type.resolved;
  8645. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8646. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8647. (* already handled varpar *)
  8648. (*
  8649. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8650. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8651. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8652. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8653. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8654. END;
  8655. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8656. result := Infinity
  8657. END;
  8658. *)
  8659. INC(result, distance);
  8660. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8661. INC(result, distance);
  8662. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8663. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8664. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8665. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8666. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8667. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8668. END;
  8669. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8670. result := Infinity
  8671. END;
  8672. ELSE
  8673. result := Infinity
  8674. END;
  8675. ELSE
  8676. INC(result,distance);
  8677. END;
  8678. END;
  8679. (*
  8680. Printout.Info("actual=", actualParameter);
  8681. Printout.Info("formal=", formalParameter);
  8682. TRACE(result);
  8683. *)
  8684. formalParameter := formalParameter.nextParameter; INC(i);
  8685. END;
  8686. END;
  8687. ASSERT(result >= 0);
  8688. RETURN result
  8689. END Distance;
  8690. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8691. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8692. BEGIN
  8693. IF right.numberParameters # (procedureType.numberParameters) THEN
  8694. result := Infinity
  8695. ELSE
  8696. formalParameter := procedureType.firstParameter;
  8697. rightParameter := right.firstParameter;
  8698. i := 0;
  8699. result := 0;
  8700. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8701. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8702. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8703. IF distance = Infinity THEN
  8704. result := Infinity;
  8705. ELSE
  8706. INC(result,distance);
  8707. END;
  8708. formalParameter := formalParameter.nextParameter;
  8709. rightParameter := rightParameter.nextParameter;
  8710. END;
  8711. END;
  8712. ASSERT(result >= 0);
  8713. RETURN result
  8714. END ProcedureTypeDistance;
  8715. (** compute and return the distance between two types, used for computation of signature distance
  8716. from -> to
  8717. **)
  8718. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8719. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8720. BEGIN
  8721. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8722. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8723. END;
  8724. i := Infinity;
  8725. IF from = to THEN
  8726. i := 0
  8727. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8728. ELSIF to.SameType(from) THEN
  8729. i := 0;
  8730. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8731. i := Infinity;
  8732. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8733. i := 10;
  8734. ELSIF (from IS SyntaxTree.StringType) THEN
  8735. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8736. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8737. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8738. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8739. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8740. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8741. i := 1
  8742. ELSIF (from IS SyntaxTree.NilType) THEN
  8743. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8744. (*
  8745. ELSIF (from = NoType) THEN
  8746. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8747. *)
  8748. ELSIF (from IS SyntaxTree.BasicType) THEN
  8749. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8750. IF varpar & (i # 0) THEN i := Infinity END;
  8751. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8752. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8753. ELSIF (from IS SyntaxTree.RecordType) THEN
  8754. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8755. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8756. IF to IS SyntaxTree.MathArrayType THEN
  8757. (*
  8758. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8759. i := Infinity;
  8760. ELSE
  8761. *)
  8762. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8763. (*
  8764. END;
  8765. *)
  8766. END
  8767. ELSIF (from IS SyntaxTree.PointerType) THEN
  8768. ptr := from(SyntaxTree.PointerType);
  8769. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8770. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8771. (* ELSE i := TypeDistance(ptr.base, to); *)
  8772. END
  8773. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8774. IF (to IS SyntaxTree.ProcedureType) THEN
  8775. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8776. END;
  8777. ELSIF (from IS SyntaxTree.PortType) THEN
  8778. IF (to IS SyntaxTree.PortType) THEN
  8779. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8780. i := 0;
  8781. END;
  8782. END;
  8783. (*no procedure test, procedure must be the same*)
  8784. END;
  8785. RETURN i
  8786. END TypeDistance;
  8787. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8788. BEGIN
  8789. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8790. END IsIntegerType;
  8791. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8792. BEGIN
  8793. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8794. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8795. OR IsPointerType(type)
  8796. )
  8797. END IsAddressType;
  8798. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8799. BEGIN
  8800. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8801. END IsSizeType;
  8802. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8803. BEGIN
  8804. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8805. END IsSignedIntegerType;
  8806. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8807. BEGIN
  8808. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8809. END IsUnsignedIntegerType;
  8810. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8811. VAR result: BOOLEAN;
  8812. BEGIN
  8813. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8814. value := x.resolved(SyntaxTree.IntegerValue).value;
  8815. result := TRUE
  8816. ELSE
  8817. result := FALSE
  8818. END;
  8819. RETURN result
  8820. END IsIntegerValue;
  8821. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8822. VAR result: BOOLEAN;
  8823. BEGIN
  8824. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8825. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8826. result := TRUE
  8827. ELSE
  8828. result := FALSE
  8829. END;
  8830. RETURN result
  8831. END IsEnumerationValue;
  8832. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8833. VAR result: BOOLEAN;
  8834. BEGIN
  8835. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8836. value := x.resolved(SyntaxTree.RealValue).value;
  8837. result := TRUE
  8838. ELSE
  8839. result := FALSE
  8840. END;
  8841. RETURN result
  8842. END IsRealValue;
  8843. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8844. VAR result: BOOLEAN;
  8845. BEGIN
  8846. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8847. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8848. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8849. result := TRUE
  8850. ELSE
  8851. result := FALSE
  8852. END;
  8853. RETURN result
  8854. END IsComplexValue;
  8855. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8856. VAR result: BOOLEAN;
  8857. BEGIN
  8858. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8859. value := x.resolved(SyntaxTree.CharacterValue).value;
  8860. result := TRUE
  8861. ELSE
  8862. result := FALSE
  8863. END;
  8864. RETURN result
  8865. END IsCharacterValue;
  8866. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8867. VAR result: BOOLEAN;
  8868. BEGIN
  8869. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8870. value := x.resolved(SyntaxTree.BooleanValue).value;
  8871. result := TRUE
  8872. ELSE
  8873. result := FALSE
  8874. END;
  8875. RETURN result
  8876. END IsBooleanValue;
  8877. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8878. VAR result: BOOLEAN;
  8879. BEGIN
  8880. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8881. value := x.resolved(SyntaxTree.SetValue).value;
  8882. result := TRUE
  8883. ELSE
  8884. result := FALSE
  8885. END;
  8886. RETURN result
  8887. END IsSetValue;
  8888. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8889. VAR result: BOOLEAN;
  8890. BEGIN
  8891. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8892. value := x.resolved(SyntaxTree.StringValue).value;
  8893. result := TRUE
  8894. ELSE
  8895. result := FALSE
  8896. END;
  8897. RETURN result
  8898. END IsStringValue;
  8899. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8900. BEGIN
  8901. x := x.resolved;
  8902. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8903. END Indexable;
  8904. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8905. BEGIN
  8906. RETURN t1.SameType(t2.resolved);
  8907. END SameType;
  8908. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8909. BEGIN
  8910. IF t IS SyntaxTree.MathArrayType THEN
  8911. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8912. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8913. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8914. DEC(max);
  8915. END;
  8916. ELSIF t IS SyntaxTree.ArrayType THEN
  8917. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8918. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8919. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8920. END;
  8921. END;
  8922. RETURN t;
  8923. END ArrayBase;
  8924. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8925. BEGIN
  8926. type := type.resolved;
  8927. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8928. base := type(SyntaxTree.ArrayType).arrayBase;
  8929. RETURN TRUE;
  8930. END;
  8931. RETURN FALSE;
  8932. END IsOpenArray;
  8933. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8934. BEGIN
  8935. type := type.resolved;
  8936. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8937. base := type(SyntaxTree.ArrayType).arrayBase;
  8938. dim := type(SyntaxTree.ArrayType).staticLength;
  8939. RETURN TRUE
  8940. ELSE
  8941. RETURN FALSE
  8942. END;
  8943. END IsStaticArray;
  8944. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8945. BEGIN
  8946. type := type.resolved;
  8947. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8948. base := type(SyntaxTree.ArrayType).arrayBase;
  8949. RETURN TRUE
  8950. ELSE
  8951. RETURN FALSE
  8952. END;
  8953. END IsDynamicArray;
  8954. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8955. VAR i: LONGINT;
  8956. BEGIN
  8957. i := 0;
  8958. t := t.resolved;
  8959. IF t IS SyntaxTree.MathArrayType THEN
  8960. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8961. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8962. END;
  8963. ELSIF t IS SyntaxTree.ArrayType THEN
  8964. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8965. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8966. END;
  8967. END;
  8968. RETURN i
  8969. END Dimension;
  8970. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8971. BEGIN
  8972. RETURN expression.assignable;
  8973. END IsVariable;
  8974. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8975. BEGIN
  8976. IF (symbol IS SyntaxTree.Parameter) THEN
  8977. WITH symbol: SyntaxTree.Parameter DO
  8978. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8979. END;
  8980. ELSE
  8981. RETURN FALSE
  8982. END;
  8983. END IsVariableParameter;
  8984. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8985. VAR result: BOOLEAN;
  8986. BEGIN
  8987. IF type = NIL THEN result := FALSE
  8988. ELSE
  8989. type := type.resolved;
  8990. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8991. END;
  8992. RETURN result
  8993. END IsPointerType;
  8994. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8995. VAR result: BOOLEAN;
  8996. BEGIN
  8997. IF type = NIL THEN result := FALSE
  8998. ELSE
  8999. type := type.resolved;
  9000. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  9001. END;
  9002. RETURN result
  9003. END IsUnsafePointer;
  9004. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9005. BEGIN
  9006. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9007. END IsDisposable;
  9008. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9009. VAR result: BOOLEAN;
  9010. BEGIN
  9011. IF type = NIL THEN result := FALSE
  9012. ELSE
  9013. type := type.resolved;
  9014. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9015. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9016. result := result OR (type IS SyntaxTree.ObjectType);
  9017. END;
  9018. RETURN result
  9019. END IsPointerToRecord;
  9020. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9021. VAR result: BOOLEAN;
  9022. BEGIN
  9023. IF type = NIL THEN result := FALSE
  9024. ELSE
  9025. type := type.resolved;
  9026. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9027. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9028. ;
  9029. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9030. result := result OR (type IS SyntaxTree.ObjectType);
  9031. END;
  9032. RETURN result
  9033. END IsPointerToObject;
  9034. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9035. BEGIN
  9036. IF type # NIL THEN
  9037. RETURN type.resolved.hasPointers
  9038. ELSE
  9039. RETURN FALSE
  9040. END;
  9041. END ContainsPointer;
  9042. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9043. BEGIN
  9044. IF type = NIL THEN RETURN FALSE END;
  9045. type := type.resolved;
  9046. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9047. END IsStringType;
  9048. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9049. BEGIN
  9050. IF type = NIL THEN RETURN FALSE END;
  9051. type := type.resolved;
  9052. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9053. END IsCharacterType;
  9054. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9055. BEGIN
  9056. IF type = NIL THEN RETURN FALSE END;
  9057. type := type.resolved;
  9058. RETURN (type IS SyntaxTree.EnumerationType)
  9059. END IsEnumerationType;
  9060. (** cf. section "Type extension (base type)" in the language report **)
  9061. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9062. VAR result: BOOLEAN;
  9063. BEGIN
  9064. ASSERT(base # NIL); ASSERT(extension # NIL);
  9065. base := base.resolved; extension := extension.resolved;
  9066. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9067. result := TRUE;
  9068. ELSE
  9069. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9070. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9071. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9072. END;
  9073. WHILE (extension # NIL) & (extension # base) DO
  9074. IF extension IS SyntaxTree.RecordType THEN
  9075. extension := extension(SyntaxTree.RecordType).baseType;
  9076. IF (extension # NIL) THEN extension := extension.resolved END;
  9077. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9078. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9079. END;
  9080. ELSE extension := NIL;
  9081. END;
  9082. END;
  9083. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9084. END;
  9085. RETURN result
  9086. END IsTypeExtension;
  9087. (** check if base is the base enumeration type of extension **)
  9088. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9089. BEGIN
  9090. base := base.resolved; extension := extension.resolved;
  9091. WHILE (extension # NIL) & (extension # base) DO
  9092. IF extension IS SyntaxTree.EnumerationType THEN
  9093. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9094. IF extension # NIL THEN extension := extension.resolved END;
  9095. ELSE
  9096. extension := NIL
  9097. END;
  9098. END;
  9099. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9100. END IsEnumerationExtension;
  9101. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9102. BEGIN
  9103. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9104. RETURN TRUE
  9105. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9106. RETURN TRUE
  9107. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9108. RETURN TRUE
  9109. ELSE
  9110. RETURN FALSE
  9111. END
  9112. END IsCallable;
  9113. (** compute and return the distance of two record types
  9114. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9115. **)
  9116. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9117. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9118. BEGIN
  9119. i := 0;
  9120. WHILE (from # NIL) & (from # to) DO
  9121. baseType := from.baseType;
  9122. IF (baseType # NIL) THEN
  9123. baseType := baseType.resolved;
  9124. IF baseType IS SyntaxTree.PointerType THEN
  9125. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9126. END;
  9127. IF baseType IS SyntaxTree.RecordType THEN
  9128. from := baseType(SyntaxTree.RecordType);
  9129. ELSE
  9130. from := NIL;
  9131. END;
  9132. ELSE
  9133. from := NIL
  9134. END;
  9135. INC(i)
  9136. END;
  9137. IF from = NIL THEN i := Infinity END;
  9138. RETURN i
  9139. END RecordTypeDistance;
  9140. (** compute and return the distance of two pointer types **)
  9141. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9142. BEGIN
  9143. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9144. RETURN Infinity;
  9145. ELSE
  9146. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9147. END;
  9148. END PointerTypeDistance;
  9149. (** check if expression contains a symbol designator pointing to a type declaration.
  9150. - if so then enter type declaration into typeDeclaration and return true else return false
  9151. **)
  9152. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9153. VAR result: BOOLEAN;
  9154. BEGIN
  9155. result := FALSE;
  9156. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9157. result := TRUE;
  9158. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9159. END;
  9160. RETURN result
  9161. END IsTypeDesignator;
  9162. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9163. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9164. VAR result: BOOLEAN;
  9165. BEGIN
  9166. type := type.resolved;
  9167. IF type IS SyntaxTree.PointerType THEN
  9168. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9169. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9170. result := TRUE
  9171. ELSE
  9172. result := type IS SyntaxTree.RecordType
  9173. END;
  9174. RETURN result
  9175. END IsExtensibleType;
  9176. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9177. BEGIN
  9178. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9179. (d IS SyntaxTree.SymbolDesignator) &
  9180. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9181. OR
  9182. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9183. END IsUnextensibleRecord;
  9184. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9185. BEGIN
  9186. IF IsUnextensibleRecord(d) THEN
  9187. RETURN FALSE
  9188. ELSE RETURN IsExtensibleType(d.type.resolved)
  9189. END;
  9190. END IsExtensibleDesignator;
  9191. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9192. BEGIN
  9193. type := type.resolved;
  9194. IF (type IS SyntaxTree.PointerType) THEN
  9195. RETURN TRUE
  9196. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9197. RETURN TRUE
  9198. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9199. RETURN TRUE
  9200. ELSIF (type IS SyntaxTree.BasicType) THEN
  9201. RETURN TRUE
  9202. END;
  9203. RETURN FALSE
  9204. END IsBasicType;
  9205. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9206. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9207. BEGIN
  9208. baseType := record.baseType;
  9209. IF (baseType # NIL) THEN
  9210. baseType := baseType.resolved;
  9211. IF (baseType IS SyntaxTree.PointerType) THEN
  9212. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9213. END;
  9214. END;
  9215. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9216. recordType := baseType(SyntaxTree.RecordType);
  9217. ELSE
  9218. recordType := NIL;
  9219. END;
  9220. RETURN recordType
  9221. END RecordBase;
  9222. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9223. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9224. BEGIN
  9225. baseRecord := RecordBase(scope.ownerRecord);
  9226. IF baseRecord = NIL THEN RETURN NIL END;
  9227. scope := baseRecord.recordScope;
  9228. procedureType := procedure.type.resolved;
  9229. IF procedure IS SyntaxTree.Operator THEN
  9230. operator := scope.firstOperator;
  9231. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9232. (*
  9233. Printout.Info("not same ",procedureType);
  9234. Printout.Info("with ",operator.type);
  9235. *)
  9236. operator := operator.nextOperator;
  9237. END;
  9238. super := operator;
  9239. ELSE
  9240. super := scope.firstProcedure;
  9241. WHILE (super # NIL) & (super.name # procedure.name) DO
  9242. super := super.nextProcedure;
  9243. END;
  9244. END;
  9245. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9246. RETURN super
  9247. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9248. RETURN super
  9249. ELSE
  9250. RETURN FindSuperProcedure(scope,procedure);
  9251. END;
  9252. END FindSuperProcedure;
  9253. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9254. VAR procedure: SyntaxTree.Procedure;
  9255. BEGIN
  9256. procedure := record.recordScope.constructor;
  9257. IF procedure = NIL THEN
  9258. record := RecordBase(record);
  9259. IF record # NIL THEN
  9260. procedure := GetConstructor(record)
  9261. END;
  9262. END;
  9263. RETURN procedure;
  9264. END GetConstructor;
  9265. (* enter a case into a list of cases in a sorted way and check for collision *)
  9266. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9267. VAR prev,this,new: SyntaxTree.CaseConstant;
  9268. BEGIN
  9269. this := root;
  9270. prev := NIL;
  9271. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9272. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9273. RETURN FALSE
  9274. ELSE
  9275. IF (this # NIL) & (this.min = max+1) THEN
  9276. this.min := min
  9277. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9278. prev.max := min
  9279. ELSE
  9280. NEW(new); new.min := min; new.max := max;
  9281. new.next := this;
  9282. IF prev = NIL THEN
  9283. root := new;
  9284. ELSE
  9285. prev.next := new
  9286. END
  9287. END;
  9288. RETURN TRUE
  9289. END;
  9290. END EnterCase;
  9291. (** generate and return a new checker object, errors are entered into diagnostics **)
  9292. 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;
  9293. VAR checker: Checker;
  9294. BEGIN
  9295. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9296. RETURN checker
  9297. END NewChecker;
  9298. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9299. VAR warnings: Warnings;
  9300. BEGIN
  9301. NEW(warnings, diagnostics); RETURN warnings;
  9302. END NewWarnings;
  9303. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9304. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9305. END IsRangeType;
  9306. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9307. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9308. END IsMathArrayType;
  9309. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9310. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9311. END IsArrayType;
  9312. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9313. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9314. END IsComplexType;
  9315. (** if a type is an array-structured object type *)
  9316. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9317. VAR recordType: SyntaxTree.RecordType;
  9318. BEGIN
  9319. IF type = NIL THEN
  9320. RETURN FALSE
  9321. ELSE
  9322. type := type.resolved;
  9323. IF type IS SyntaxTree.PointerType THEN
  9324. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9325. IF type IS SyntaxTree.RecordType THEN
  9326. recordType := type(SyntaxTree.RecordType);
  9327. RETURN recordType.isObject & recordType.HasArrayStructure()
  9328. ELSE
  9329. RETURN FALSE
  9330. END
  9331. ELSE
  9332. RETURN FALSE
  9333. END
  9334. END
  9335. END IsArrayStructuredObjectType;
  9336. (** the math array structure of a type
  9337. - for math arrays: the array itself
  9338. - for pointers: the math array structure of the pointer base
  9339. - for array-structured object types: the underlying structure
  9340. - for non-math arrays and all other types: NIL
  9341. **)
  9342. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9343. VAR
  9344. result: SyntaxTree.MathArrayType;
  9345. BEGIN
  9346. IF type = NIL THEN
  9347. result := NIL
  9348. ELSE
  9349. type := type.resolved;
  9350. IF type IS SyntaxTree.PointerType THEN
  9351. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9352. END;
  9353. IF type IS SyntaxTree.MathArrayType THEN
  9354. result := type(SyntaxTree.MathArrayType)
  9355. ELSIF type IS SyntaxTree.RecordType THEN
  9356. result := type(SyntaxTree.RecordType).arrayStructure
  9357. ELSE
  9358. result := NIL
  9359. END
  9360. END;
  9361. RETURN result
  9362. END MathArrayStructureOfType;
  9363. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9364. VAR
  9365. result: BOOLEAN;
  9366. rangeExpression: SyntaxTree.RangeExpression;
  9367. BEGIN
  9368. IF x IS SyntaxTree.RangeExpression THEN
  9369. rangeExpression := x(SyntaxTree.RangeExpression);
  9370. result := TRUE;
  9371. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9372. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9373. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9374. ELSE
  9375. result := FALSE
  9376. END;
  9377. RETURN result
  9378. END IsStaticRange;
  9379. (** whether a type is a math array of tensor form **)
  9380. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9381. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9382. END IsTensor;
  9383. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9384. BEGIN
  9385. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9386. length := type(SyntaxTree.MathArrayType).staticLength;
  9387. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9388. RETURN TRUE
  9389. ELSE
  9390. RETURN FALSE
  9391. END;
  9392. END IsStaticMathArray;
  9393. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9394. BEGIN
  9395. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9396. END SymbolHasAddress;
  9397. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9398. BEGIN
  9399. RETURN
  9400. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9401. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9402. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9403. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9404. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9405. ;
  9406. END HasAddress;
  9407. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9408. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9409. BEGIN
  9410. IF (e IS SyntaxTree.Designator) THEN
  9411. d := e(SyntaxTree.Designator);
  9412. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9413. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9414. e := d.left;
  9415. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9416. END;
  9417. IF d # NIL THEN
  9418. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9419. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9420. END;
  9421. END;
  9422. RETURN FALSE;
  9423. END IsLocalVariable;
  9424. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9425. BEGIN
  9426. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9427. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9428. ELSE
  9429. RETURN TRUE
  9430. END;
  9431. END IsStaticProcedure;
  9432. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9433. CONST OptimizeMethodTable = FALSE;
  9434. BEGIN
  9435. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9436. END InMethodTable;
  9437. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9438. BEGIN
  9439. IF type = NIL THEN RETURN FALSE
  9440. ELSE
  9441. type := type.resolved;
  9442. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9443. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9444. END
  9445. END ReturnedAsParameter;
  9446. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9447. BEGIN
  9448. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9449. END StructuredReturnType;
  9450. END FoxSemanticChecker.
  9451. SystemTools.FreeDownTo FoxSemanticChecker ~