FoxSemanticChecker.Mod 404 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232
  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. (* check constructor *)
  4871. IF (first =0) OR CheckVariable(parameter0) THEN
  4872. IF type0 IS SyntaxTree.PointerType THEN
  4873. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4874. ELSIF type0 IS SyntaxTree.CellType THEN
  4875. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4876. ELSE
  4877. Error(position, "forbidden new on value type");
  4878. END;
  4879. IF type0 IS SyntaxTree.ArrayType THEN
  4880. arrayType := type0(SyntaxTree.ArrayType);
  4881. IF arrayType.form = SyntaxTree.Static THEN
  4882. i := first
  4883. ELSIF arrayType.form = SyntaxTree.Open THEN
  4884. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4885. ELSE HALT(100)
  4886. END;
  4887. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4888. i := first;
  4889. REPEAT
  4890. actualParameter := actualParameters.GetExpression(i);
  4891. IF CheckSizeType(actualParameter) THEN
  4892. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4893. actualParameters.SetExpression(i,actualParameter);
  4894. END;
  4895. INC(i);
  4896. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4897. END;
  4898. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4899. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4900. Error(position, "forbidden new on abstract object");
  4901. END;
  4902. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4903. IF constructor = NIL THEN
  4904. IF CheckArity(first,first) THEN END;
  4905. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4906. Error(position, "new on object with hidden constructor");
  4907. ELSE
  4908. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4909. numberFormalParameters := procedureType.numberParameters;
  4910. IF numberActualParameters-first <= numberFormalParameters THEN
  4911. formalParameter := procedureType.firstParameter;
  4912. FOR i := first TO numberActualParameters-1 DO
  4913. actualParameter := actualParameters.GetExpression(i);
  4914. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4915. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4916. ELSE
  4917. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4918. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4919. END;
  4920. actualParameters.SetExpression(i,actualParameter);
  4921. END;
  4922. formalParameter := formalParameter.nextParameter;
  4923. END;
  4924. WHILE (formalParameter # NIL) DO
  4925. IF formalParameter.defaultValue # NIL THEN
  4926. actualParameters.AddExpression(formalParameter.defaultValue);
  4927. formalParameter := formalParameter.nextParameter
  4928. ELSE
  4929. Error(position, "less actual than formal parameters");
  4930. formalParameter := NIL;
  4931. END;
  4932. END;
  4933. ELSE
  4934. Error(position, "more actual than formal parameters")
  4935. END;
  4936. END;
  4937. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4938. mathArrayType := type0(SyntaxTree.MathArrayType);
  4939. IF mathArrayType.form = SyntaxTree.Static THEN
  4940. Error(position, "new on static array");
  4941. ELSE
  4942. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4943. i0 := first+1; i1 := Infinity;
  4944. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4945. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4946. i1 := i0;
  4947. ELSE HALT(100);
  4948. END;
  4949. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4950. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4951. base := ArrayBase(type0,MAX(LONGINT));
  4952. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4953. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4954. IF ~CompatibleTo(system,type0,parameterType) THEN
  4955. Error(parameter0.position,"incompatible parameter in new");
  4956. result := SyntaxTree.invalidExpression;
  4957. ELSE
  4958. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4959. END;
  4960. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4961. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4962. IF ~CompatibleTo(system,type1,parameterType) THEN
  4963. Error(parameter1.position,"parameter incompatible to math array len type");
  4964. result := SyntaxTree.invalidExpression;
  4965. ELSE
  4966. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4967. END;
  4968. ELSE
  4969. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4970. i := first;
  4971. REPEAT
  4972. actualParameter := actualParameters.GetExpression(i);
  4973. IF CheckSizeType(actualParameter) THEN
  4974. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4975. actualParameters.SetExpression(i,actualParameter);
  4976. END;
  4977. INC(i);
  4978. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4979. END;
  4980. END;
  4981. END;
  4982. ELSIF type0 IS SyntaxTree.CellType THEN
  4983. IF ~(currentIsCellNet) THEN
  4984. Error(position, "cell allocation outside activeCells ");
  4985. ELSE
  4986. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4987. IF (constructor = NIL) & CheckArity(1,1) THEN
  4988. (* ok *)
  4989. ELSE
  4990. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4991. numberFormalParameters := procedureType.numberParameters;
  4992. DEC(numberActualParameters);
  4993. IF numberActualParameters <= numberFormalParameters THEN
  4994. formalParameter := procedureType.firstParameter;
  4995. FOR i := first TO numberActualParameters DO
  4996. actualParameter := actualParameters.GetExpression(i);
  4997. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4998. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4999. ELSE
  5000. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5001. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5002. END;
  5003. actualParameters.SetExpression(i,actualParameter);
  5004. END;
  5005. formalParameter := formalParameter.nextParameter;
  5006. END;
  5007. WHILE (formalParameter # NIL) DO
  5008. IF formalParameter.defaultValue # NIL THEN
  5009. actualParameters.AddExpression(formalParameter.defaultValue);
  5010. formalParameter := formalParameter.nextParameter
  5011. ELSE
  5012. Error(position, "less actual than formal parameters");
  5013. formalParameter := NIL;
  5014. END;
  5015. END;
  5016. ELSE
  5017. Error(position, "more actual than formal parameters")
  5018. END;
  5019. END;
  5020. END;
  5021. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5022. activeCellsStatement := TRUE;
  5023. ELSE
  5024. Error(position, "cannot be allocated");
  5025. END;
  5026. END;
  5027. END;
  5028. (* ---- DISPOSE ----- *)
  5029. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5030. IF ~IsPointerType(parameter0.type) THEN
  5031. Error(parameter0.position,"is not a pointer")
  5032. ELSIF ~IsDisposable(parameter0.type) THEN
  5033. Error(parameter0.position,"is not disposable")
  5034. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5035. END
  5036. (* ---- GETPROCEDURE ----- *)
  5037. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5038. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5039. IF CheckVariable(parameter2) THEN
  5040. IF ~GetProcedureAllowed(parameter2.type) THEN
  5041. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5042. END;
  5043. END;
  5044. END;
  5045. (* ---- ABS ----- *)
  5046. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5047. (* note: ABS on complex numbers is done using overloading *)
  5048. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5049. type := type0;
  5050. IF IsIntegerValue(parameter0,i0) THEN
  5051. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5052. type := Global.GetIntegerType(system,ABS(i0));
  5053. ELSIF IsRealValue(parameter0,r) THEN
  5054. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5055. END;
  5056. ELSE
  5057. type := SyntaxTree.invalidType;
  5058. END;
  5059. (* ---- ASH, ASR ----- *)
  5060. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5061. type := type0;
  5062. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5063. (*
  5064. ConvertOperands(parameter0,parameter1); (* same type *)
  5065. *)
  5066. type := parameter0.type;
  5067. IF IsIntegerValue(parameter0,i0) THEN
  5068. IF IsIntegerValue(parameter1,i1) THEN
  5069. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5070. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5071. result := ResolveExpression(result);
  5072. type := Global.GetIntegerType(system,i0);
  5073. END;
  5074. END;
  5075. IF type.resolved.sizeInBits < 32 THEN
  5076. type := system.longintType;
  5077. END;
  5078. (*!compatibility with release, remove when resolved
  5079. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5080. *)
  5081. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5082. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5083. actualParameters.SetExpression(0,parameter0);
  5084. actualParameters.SetExpression(1,parameter1);
  5085. END;
  5086. (* ---- CAP ----- *)
  5087. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5088. type := system.characterType;
  5089. IF CheckCharacterType (parameter0) THEN
  5090. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5091. actualParameters.SetExpression(0,parameter0);
  5092. IF IsCharacterValue(parameter0,c) THEN
  5093. IF (c <= "z") & (c >= "a") THEN
  5094. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5095. ELSE
  5096. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5097. END;
  5098. END;
  5099. END;
  5100. (* ---- CHR ----- *)
  5101. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5102. IF id = Global.Chr THEN
  5103. type := system.characterType
  5104. ELSE
  5105. type := system.characterType32
  5106. END;
  5107. IF CheckIntegerType(parameter0) THEN
  5108. IF IsIntegerValue(parameter0,i0) THEN
  5109. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5110. result := ResolveExpression(result);
  5111. ELSE
  5112. (*
  5113. result := NewConversion(parameter0.position,parameter0,type);
  5114. *)
  5115. END;
  5116. END
  5117. (* ---- ENTIER ----- *)
  5118. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5119. type := system.longintType;
  5120. IF CheckRealType(parameter0) THEN
  5121. IF IsRealValue(parameter0,r) THEN
  5122. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5123. type := Global.GetIntegerType(system,ENTIER(r));
  5124. END
  5125. END;
  5126. (* ---- ENTIERH ----- *)
  5127. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5128. type := system.hugeintType;
  5129. IF CheckRealType(parameter0) THEN
  5130. IF IsRealValue(parameter0,r) THEN
  5131. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5132. END
  5133. END;
  5134. (* ---- LEN ----- *)
  5135. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5136. type := system.lenType;
  5137. base := type0;
  5138. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5139. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5140. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5141. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5142. Error(position, "forbidden len on unsafe pointer");
  5143. END;
  5144. type0 := base;
  5145. ELSE
  5146. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5147. type0 := parameter0.type.resolved;
  5148. actualParameters.SetExpression(0,parameter0);
  5149. base := type0;
  5150. END;
  5151. END;
  5152. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5153. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5154. IF i1 < 0 THEN
  5155. Error(position, "invalid dimension");
  5156. base := SyntaxTree.invalidType;
  5157. ELSE
  5158. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5159. IF (base # NIL) & Indexable(base) THEN
  5160. ELSE
  5161. Error(position, "len on no array");
  5162. IF VerboseErrorMessage THEN
  5163. Printout.Info("base",base);
  5164. END;
  5165. base := SyntaxTree.invalidType;
  5166. END;
  5167. END;
  5168. IF numberActualParameters=2 THEN
  5169. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5170. actualParameters.SetExpression(1,parameter1);
  5171. ELSIF base IS SyntaxTree.MathArrayType THEN
  5172. Error(position, "missing dimension specification");
  5173. END;
  5174. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5175. IF base IS SyntaxTree.ArrayType THEN
  5176. arrayType := base(SyntaxTree.ArrayType);
  5177. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5178. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5179. result := Global.NewIntegerValue(system,position,value);
  5180. type := result.type;(* arrayType.length.type;*)
  5181. ASSERT(type # NIL);
  5182. END;
  5183. ELSIF base IS SyntaxTree.MathArrayType THEN
  5184. mathArrayType := base(SyntaxTree.MathArrayType);
  5185. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5186. result := Global.NewIntegerValue(system,position,value);
  5187. type := result.type;
  5188. (*
  5189. type := mathArrayType.length.type;
  5190. *)
  5191. ASSERT(type # NIL);
  5192. END;
  5193. END;
  5194. END;
  5195. ELSE
  5196. type := system.lenType;
  5197. END;
  5198. (* ---- FIRST ---- *)
  5199. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5200. type := system.lenType;
  5201. IF CheckRangeType(parameter0) THEN END;
  5202. result.SetAssignable(parameter0.assignable)
  5203. (* ---- LAST ---- *)
  5204. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5205. type := system.lenType;
  5206. IF CheckRangeType(parameter0) THEN END;
  5207. result.SetAssignable(parameter0.assignable)
  5208. (* ---- STEP ---- *)
  5209. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5210. type := system.lenType;
  5211. IF CheckRangeType(parameter0) THEN END;
  5212. result.SetAssignable(parameter0.assignable)
  5213. (* ---- RE ---- *)
  5214. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5215. IF CheckNumberType(parameter0) THEN
  5216. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5217. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5218. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5219. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5220. type := parameter0.type
  5221. ELSE
  5222. type := system.realType
  5223. END
  5224. END;
  5225. result.SetAssignable(parameter0.assignable)
  5226. (* ---- IM ---- *)
  5227. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5228. IF CheckNumberType(parameter0) THEN
  5229. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5230. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5231. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5232. ELSE
  5233. type := system.realType;
  5234. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5235. END
  5236. END;
  5237. result.SetAssignable(parameter0.assignable)
  5238. (* ---- MAX ----- *)
  5239. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5240. IF numberActualParameters = 1 THEN
  5241. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5242. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5243. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5244. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5245. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5246. *)
  5247. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5248. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5249. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5250. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5251. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5252. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5253. END;
  5254. ELSE
  5255. Error(parameter0.position,"is not a type symbol");
  5256. END
  5257. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5258. ConvertOperands(parameter0,parameter1);
  5259. actualParameters.SetExpression(0,parameter0);
  5260. actualParameters.SetExpression(1,parameter1);
  5261. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5262. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5263. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5264. END;
  5265. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5266. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5267. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5268. END;
  5269. END;
  5270. type := parameter0.type;
  5271. ELSE type := SyntaxTree.invalidType;
  5272. END;
  5273. (* ---- MIN ----- *)
  5274. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5275. IF numberActualParameters = 1 THEN
  5276. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5277. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5278. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5279. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5280. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5281. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5282. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5283. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5284. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5285. END;
  5286. ELSE
  5287. Error(parameter0.position,"is not a type symbol");
  5288. END
  5289. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5290. ConvertOperands(parameter0,parameter1);
  5291. actualParameters.SetExpression(0,parameter0);
  5292. actualParameters.SetExpression(1,parameter1);
  5293. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5294. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5295. ELSE result.SetResolved(parameter1.resolved)
  5296. END;
  5297. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5298. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5299. ELSE result.SetResolved(parameter1.resolved)
  5300. END;
  5301. END;
  5302. type := parameter0.type;
  5303. ELSE type := SyntaxTree.invalidType;
  5304. END;
  5305. (* ---- ODD ----- *)
  5306. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5307. type := system.booleanType;
  5308. IF CheckIntegerType(parameter0) THEN
  5309. IF IsIntegerValue(parameter0,i0) THEN
  5310. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5311. type := system.booleanType;
  5312. END;
  5313. END;
  5314. (* ---- ORD ----- *)
  5315. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5316. IF id = Global.Ord THEN
  5317. type := system.integerType;
  5318. ELSE
  5319. type := system.longintType;
  5320. END;
  5321. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5322. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5323. actualParameters.SetExpression(0,parameter0);
  5324. (* IF CheckCharacterType(parameter0) THEN*)
  5325. IF IsCharacterValue(parameter0,c)THEN
  5326. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5327. type := Global.GetSignedIntegerType(system,ORD(c));
  5328. END;
  5329. ELSE Error(parameter0.position, "incompatible parameter");
  5330. END;
  5331. (* ---- SHORT ----- *)
  5332. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5333. type := type0;
  5334. IF IsSignedIntegerType(type) THEN
  5335. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5336. ELSIF type = system.integerType THEN type := system.shortintType
  5337. ELSIF type = system.longintType THEN type := system.integerType
  5338. ELSIF type = system.hugeintType THEN type:= system.longintType
  5339. ELSE
  5340. CASE type.sizeInBits OF
  5341. 16: type := Global.Integer8
  5342. |32: type := Global.Integer16
  5343. |64: type := Global.Integer32
  5344. END;
  5345. END;
  5346. ELSIF type IS SyntaxTree.FloatType THEN
  5347. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5348. ELSIF type = system.longrealType THEN type := system.realType
  5349. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5350. END;
  5351. ELSIF type IS SyntaxTree.ComplexType THEN
  5352. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5353. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5354. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5355. END;
  5356. ELSE
  5357. Error(parameter0.position,"short not applicable")
  5358. END;
  5359. IF (parameter0.resolved # NIL) THEN
  5360. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5361. IF parameter0 IS SyntaxTree.Value THEN
  5362. result.SetResolved(parameter0(SyntaxTree.Value));
  5363. END;
  5364. END;
  5365. (* ---- LONG ----- *)
  5366. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5367. type := type0;
  5368. IF IsSignedIntegerType(type) THEN
  5369. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5370. ELSIF type = system.longintType THEN type := system.hugeintType
  5371. ELSIF type = system.integerType THEN type := system.longintType
  5372. ELSIF type = system.shortintType THEN type := system.integerType
  5373. ELSE
  5374. CASE type.sizeInBits OF
  5375. 8: type := Global.Integer16
  5376. |16: type := Global.Integer32
  5377. |32: type := Global.Integer64
  5378. END;
  5379. END;
  5380. ELSIF type IS SyntaxTree.FloatType THEN
  5381. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5382. ELSIF type= system.realType THEN type := system.longrealType
  5383. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5384. END;
  5385. ELSIF type IS SyntaxTree.ComplexType THEN
  5386. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5387. ELSIF type = system.complexType THEN type := system.longcomplexType
  5388. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5389. END;
  5390. ELSE
  5391. Error(parameter0.position,"long not applicable")
  5392. END;
  5393. IF (parameter0.resolved # NIL) THEN
  5394. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5395. IF parameter0 IS SyntaxTree.Value THEN
  5396. result.SetResolved(parameter0(SyntaxTree.Value));
  5397. END;
  5398. END;
  5399. (* ---- SIZE OF ----- *)
  5400. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5401. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5402. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5403. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5404. type := system.integerType;
  5405. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5406. ELSE
  5407. (* for variables, system sizeof could represent the physically occupied size
  5408. determined via the type descriptor, implement that ? *)
  5409. Error(parameter0.position,"is not a type symbol");
  5410. END
  5411. (* ---- SYSTEM.TRACE -----*)
  5412. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5413. FOR i := 0 TO numberActualParameters-1 DO
  5414. parameter0 := actualParameters.GetExpression(i);
  5415. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5416. Error(parameter0.position,"incompatible parameter");
  5417. END;
  5418. END;
  5419. (* remaining issues can only be tested in backend *)
  5420. (* ---- ADDRESSOF----- *)
  5421. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5422. IF HasAddress(parameter0) THEN
  5423. type := system.addressType;
  5424. ELSE
  5425. type := SyntaxTree.invalidType;
  5426. Error(parameter0.position,"has no address");
  5427. END;
  5428. (* ---- BIT ----- *)
  5429. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5430. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5431. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5432. actualParameters.SetExpression(0,parameter0);
  5433. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5434. actualParameters.SetExpression(1,parameter1);
  5435. END;
  5436. type := system.booleanType;
  5437. (* ----- MSK ---- *)
  5438. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5439. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5440. ConvertOperands(parameter0,parameter1);
  5441. actualParameters.SetExpression(0,parameter0);
  5442. actualParameters.SetExpression(1,parameter1);
  5443. END;
  5444. type := parameter0.type;
  5445. (* ---- SYSTEM.GET64 ----- *)
  5446. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5447. IF CheckAddressType(parameter0) THEN
  5448. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5449. actualParameters.SetExpression(0,parameter0);
  5450. END;
  5451. type := system.hugeintType;
  5452. (* ---- SYSTEM.GET32 ----- *)
  5453. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5454. IF CheckAddressType(parameter0) THEN
  5455. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5456. actualParameters.SetExpression(0,parameter0);
  5457. END;
  5458. type := system.longintType;
  5459. (* ---- SYSTEM.GET16 ----- *)
  5460. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5461. IF CheckAddressType(parameter0) THEN
  5462. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5463. actualParameters.SetExpression(0,parameter0);
  5464. END;
  5465. type := system.integerType;
  5466. (* ---- SYSTEM.GET8 ----- *)
  5467. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5468. IF CheckAddressType(parameter0) THEN
  5469. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5470. actualParameters.SetExpression(0,parameter0);
  5471. END;
  5472. type := system.shortintType;
  5473. (* ---- SYSTEM.GetStackPointer ----- *)
  5474. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5475. type := system.addressType;
  5476. (* ---- SYSTEM.GetFramePointer ----- *)
  5477. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5478. type := system.addressType;
  5479. (* ---- SYSTEM.GetActivity ----- *)
  5480. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5481. type := system.objectType;
  5482. (* ---- SYSTEM.SetStackPointer ----- *)
  5483. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5484. IF CheckAddressType(parameter0) THEN
  5485. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5486. actualParameters.SetExpression(0,parameter0);
  5487. END;
  5488. (* ---- SYSTEM.SetFramePointer ----- *)
  5489. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5490. IF CheckAddressType(parameter0) THEN
  5491. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5492. actualParameters.SetExpression(0,parameter0);
  5493. END;
  5494. (* ---- SYSTEM.SetActivity ----- *)
  5495. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5496. IF CheckObjectType(parameter0) THEN
  5497. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5498. actualParameters.SetExpression(0,parameter0);
  5499. END;
  5500. (* ---- LSH, LSL, ROT, ROR ----- *)
  5501. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5502. type := type0;
  5503. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5504. actualParameters.SetExpression(1, parameter1);
  5505. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5506. IF id = Global.Lsh THEN
  5507. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5508. ELSIF id = Global.Rot THEN
  5509. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5510. ELSIF id = Global.Ror THEN
  5511. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5512. END;
  5513. END;
  5514. (* ---- SYSTEM.VAL ----- *)
  5515. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5516. IF CheckTypeDeclarationType(parameter0) THEN
  5517. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5518. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5519. result := SyntaxTree.invalidExpression;
  5520. Error(parameter0.position,"is no basic type");
  5521. ELSE
  5522. IF (parameter1.resolved # NIL) THEN
  5523. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5524. IF parameter0 IS SyntaxTree.Value THEN
  5525. result.SetResolved(parameter0(SyntaxTree.Value));
  5526. END;
  5527. END;
  5528. result.SetAssignable(parameter1.assignable);
  5529. END;
  5530. END;
  5531. (* ---- SYSTEM.GET ----- *)
  5532. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5533. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5534. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5535. actualParameters.SetExpression(0,parameter0);
  5536. END;
  5537. (* ---- SYSTEM.PUT ----- *)
  5538. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5539. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5540. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5541. actualParameters.SetExpression(0,parameter0);
  5542. END;
  5543. (* ---- SYSTEM.PUT64 ----- *)
  5544. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5545. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5546. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5547. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5548. actualParameters.SetExpression(0,parameter0);
  5549. actualParameters.SetExpression(1,parameter1);
  5550. END;
  5551. (* ---- SYSTEM.PUT32 ----- *)
  5552. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5553. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5554. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5555. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5556. actualParameters.SetExpression(0,parameter0);
  5557. actualParameters.SetExpression(1,parameter1);
  5558. END;
  5559. (* ---- SYSTEM.PUT16 ----- *)
  5560. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5561. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5562. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5563. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5564. actualParameters.SetExpression(0,parameter0);
  5565. actualParameters.SetExpression(1,parameter1);
  5566. END;
  5567. (* ---- SYSTEM.PUT8 ----- *)
  5568. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5569. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5570. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5571. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5572. actualParameters.SetExpression(0,parameter0);
  5573. actualParameters.SetExpression(1,parameter1);
  5574. END;
  5575. (* ---- SYSTEM.MOVE ----- *)
  5576. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5577. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5578. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5579. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5580. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5581. actualParameters.SetExpression(0,parameter0);
  5582. actualParameters.SetExpression(1,parameter1);
  5583. actualParameters.SetExpression(2,parameter2);
  5584. END;
  5585. (* ---- SYSTEM.NEW ----- *)
  5586. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5587. IF ~IsPointerType(parameter0.type) THEN
  5588. Error(parameter0.position,"is not a pointer")
  5589. ELSIF CheckSizeType(parameter1) THEN
  5590. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5591. actualParameters.SetExpression(1,parameter1);
  5592. END;
  5593. (* ----SYSTEM.REF ---- *)
  5594. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5595. type := system.addressType
  5596. (* ---- INCR ----- *)
  5597. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5598. type := system.lenType;
  5599. base := type0;
  5600. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5601. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5602. IF i1 < 0 THEN
  5603. Error(position, "invalid dimension");
  5604. base := SyntaxTree.invalidType;
  5605. ELSE
  5606. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5607. IF (base # NIL) & Indexable(base) THEN
  5608. ELSE
  5609. Error(position, "len on no array");
  5610. IF VerboseErrorMessage THEN
  5611. Printout.Info("base",base);
  5612. END;
  5613. base := SyntaxTree.invalidType;
  5614. END;
  5615. END;
  5616. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5617. actualParameters.SetExpression(1,parameter1);
  5618. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5619. mathArrayType := base(SyntaxTree.MathArrayType);
  5620. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5621. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5622. type := system.lenType;
  5623. END;
  5624. END;
  5625. ELSE
  5626. type := system.lenType;
  5627. END;
  5628. (* ---- SUM ----- *)
  5629. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5630. Error(position, "sum operator not applicable");
  5631. (* ---- ALL ----- *)
  5632. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5633. Error(position, "all operator not applicable");
  5634. (* ---- DIM ----- *)
  5635. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5636. type := system.lenType;
  5637. IF type0 IS SyntaxTree.MathArrayType THEN
  5638. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5639. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5640. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5641. END;
  5642. ELSE
  5643. Error(position, "dimension on non math array type");
  5644. END;
  5645. (* ---- CAS ----- *)
  5646. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5647. IF type0.IsComposite () THEN
  5648. Error(position, "first parameter of composite type");
  5649. result := SyntaxTree.invalidExpression;
  5650. ELSIF ~IsVariable (parameter0) THEN
  5651. Error(position, "first parameter not assignable");
  5652. result := SyntaxTree.invalidExpression;
  5653. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5654. Error(position, "second parameter incompatible");
  5655. result := SyntaxTree.invalidExpression;
  5656. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5657. Error(position, "third parameter incompatible");
  5658. result := SyntaxTree.invalidExpression;
  5659. ELSE
  5660. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5661. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5662. type := type0;
  5663. END;
  5664. (* ---- RESHAPE ----- *)
  5665. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5666. IF type0 IS SyntaxTree.MathArrayType THEN
  5667. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5668. base := ArrayBase(type0,MAX(LONGINT));
  5669. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5670. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5671. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5672. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5673. IF ~CompatibleTo(system,type0,parameterType) THEN
  5674. Error(parameter0.position,"incompatible parameter in reshape");
  5675. result := SyntaxTree.invalidExpression;
  5676. ELSE
  5677. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5678. END;
  5679. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5680. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5681. IF ~CompatibleTo(system,type1,parameterType) THEN
  5682. Error(parameter1.position,"parameter incompatible to math array of longint");
  5683. result := SyntaxTree.invalidExpression;
  5684. ELSE
  5685. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5686. END;
  5687. ELSE
  5688. Error(position,"reshape on non math array type");
  5689. result := SyntaxTree.invalidExpression;
  5690. END;
  5691. (* ---- SYSTEM.TYPECODE ----- *)
  5692. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5693. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5694. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5695. type := type.resolved;
  5696. IF type IS SyntaxTree.PointerType THEN
  5697. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5698. END;
  5699. IF ~(type IS SyntaxTree.RecordType) THEN
  5700. Error(parameter0.position,"must be type with type descriptor");
  5701. END;
  5702. ELSE
  5703. Error(parameter0.position,"is not a type symbol");
  5704. END;
  5705. type := system.addressType;
  5706. (* -------- FLT --------- *)
  5707. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5708. type := system.realType;
  5709. IF IsRealValue(parameter0, r) THEN
  5710. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5711. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5712. value := ABS(i0);
  5713. IF value # 0 THEN
  5714. i1 := 23;
  5715. IF value >= 2*800000H THEN
  5716. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5717. ELSIF value < 800000H THEN
  5718. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5719. END;
  5720. value := (i1 + 127)*800000H - 800000H + value;
  5721. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5722. END;
  5723. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5724. END;
  5725. (* ------- CONNECT -------*)
  5726. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5727. (*IF ~(currentIsCellNet) THEN
  5728. Error(position, "connection outside activeCells body block");
  5729. END;*)
  5730. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5731. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5732. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5733. END;
  5734. IF numberActualParameters = 3 THEN
  5735. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5736. Error(position, "incompatible channel size parameter");
  5737. END;
  5738. *)
  5739. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5740. actualParameters.SetExpression(2,parameter2);
  5741. END;
  5742. activeCellsStatement := TRUE;
  5743. (* ---------- DELEGATE --------*)
  5744. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5745. (*
  5746. IF ~(currentIsCellNet) THEN
  5747. Error(position, "connection delegation outside activeCells body block");
  5748. END;
  5749. *)
  5750. IF ~CheckPortType(parameter1, inPort) THEN
  5751. Error(parameter0.position,"not a port")
  5752. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5753. Error(parameter1.position,"not a port")
  5754. ELSIF (outPort.direction # inPort.direction) THEN
  5755. Error(parameter0.position,"invalid port direction");
  5756. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5757. Error(position, "incompatible port sizes");
  5758. END;
  5759. activeCellsStatement := TRUE;
  5760. (* --------- RECEIVE ---------*)
  5761. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5762. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5763. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5764. IF inPort.direction # SyntaxTree.InPort THEN
  5765. Error(parameter0.position,"not an in-port")
  5766. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5767. Error(parameter1.position,"incompatible to port type");
  5768. END;
  5769. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5770. IF ~SameType(parameter2.type, system.integerType) THEN
  5771. Error(parameter2.position,"incompatible to integer type");
  5772. END;
  5773. END;
  5774. END;
  5775. (* --------- SEND ---------*)
  5776. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5777. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5778. IF CheckPortType(parameter0,outPort) THEN
  5779. IF outPort.direction # SyntaxTree.OutPort THEN
  5780. Error(parameter1.position,"not an out-port")
  5781. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5782. Error(parameter1.position,"incompatible to port type");
  5783. ELSE
  5784. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5785. actualParameters.SetExpression(1,parameter1);
  5786. END;
  5787. END;
  5788. (* ------- custom builtins ----- *)
  5789. ELSIF id = Global.systemSpecial THEN
  5790. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5791. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5792. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5793. type := procedureType.returnType;
  5794. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5795. (* go through all formal parameters *)
  5796. formalParameter := procedureType.firstParameter;
  5797. FOR i := 0 TO actualParameters.Length() - 1 DO
  5798. actualParameter := actualParameters.GetExpression(i);
  5799. IF actualParameter = SyntaxTree.invalidExpression THEN
  5800. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5801. Error(position, "incompatible parameter")
  5802. ELSE
  5803. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5804. END;
  5805. actualParameters.SetExpression(i, actualParameter);
  5806. formalParameter := formalParameter.nextParameter
  5807. END
  5808. END
  5809. ELSE
  5810. Error(position, "builtin not implemented");
  5811. result := SyntaxTree.invalidExpression;
  5812. END;
  5813. END;
  5814. IF result # SyntaxTree.invalidExpression THEN
  5815. type := ResolveType(type);
  5816. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5817. result.SetType(type);
  5818. END;
  5819. RETURN result
  5820. END NewBuiltinCallDesignator;
  5821. (** return type guard designator left(type)
  5822. - check if type can be extended (i.e. is no static record)
  5823. - check if type is a type extension of left.type
  5824. - returns new type guard designator
  5825. returns invalidDesignator = invalidExpression if error
  5826. **)
  5827. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5828. VAR result: SyntaxTree.Designator;
  5829. BEGIN
  5830. result := SyntaxTree.invalidDesignator;
  5831. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5832. Error(position, "no type extension of type");
  5833. IF VerboseErrorMessage THEN
  5834. Printout.Info("left",left);
  5835. Printout.Info("type",type);
  5836. END;
  5837. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5838. Error(position, "variable cannot be extended");
  5839. ELSIF IsUnsafePointer(left.type) THEN
  5840. Error(position, "forbidden type guard on unsafe pointer");
  5841. ELSE
  5842. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5843. result.SetType(type);
  5844. result.SetAssignable(left.assignable);
  5845. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5846. END;
  5847. RETURN result
  5848. END NewTypeGuardDesignator;
  5849. (** check and resolve parameter designator left(expression list)
  5850. - check expression list
  5851. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5852. - elsif left is a procedure type then
  5853. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5854. - else return is a procedure call then return ProcedureCallDesignator
  5855. returns invalidDesignator = invalidExpression if error
  5856. **)
  5857. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5858. VAR
  5859. parameters: SyntaxTree.ExpressionList;
  5860. left: SyntaxTree.Designator;
  5861. result,expression: SyntaxTree.Expression;
  5862. typeDeclaration: SyntaxTree.TypeDeclaration;
  5863. type, expressionType: SyntaxTree.Type;
  5864. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5865. BEGIN
  5866. type := type.resolved;
  5867. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5868. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5869. END;
  5870. RETURN type
  5871. END BaseType;
  5872. BEGIN
  5873. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5874. result := SyntaxTree.invalidDesignator;
  5875. left := ResolveDesignator(designator.left);
  5876. IF left # SyntaxTree.invalidDesignator THEN
  5877. parameters := designator.parameters;
  5878. IF ExpressionList(parameters) THEN
  5879. IF (left.type = NIL) THEN
  5880. Error(left.position,"object is not a procedure or cannot be extended");
  5881. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5882. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5883. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5884. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5885. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5886. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5887. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5888. ELSE
  5889. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5890. END
  5891. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5892. expression := parameters.GetExpression(0);
  5893. type := typeDeclaration.declaredType.resolved;
  5894. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5895. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5896. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5897. OR (expressionType IS SyntaxTree.EnumerationType)
  5898. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5899. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5900. Error(left.position,"invalid unsigned type in explicit conversion");
  5901. ELSE
  5902. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5903. END;
  5904. ELSE
  5905. Error(left.position,"invalid type in explicit conversion");
  5906. END;
  5907. ELSE
  5908. Error(left.position,"called object is not a procedure or cannot be extended");
  5909. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5910. result := SyntaxTree.invalidDesignator;
  5911. END;
  5912. ELSE
  5913. result := SyntaxTree.invalidDesignator
  5914. END;
  5915. END;
  5916. resolvedExpression := result;
  5917. END VisitParameterDesignator;
  5918. (** check dereference designator left^
  5919. - check if left is pointer type or left is object type
  5920. - return new dereference designator with type = left.baseType.type (if appropriate)
  5921. with error handling
  5922. returns invalidDesignator = invalidExpression if error
  5923. **)
  5924. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5925. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5926. BEGIN
  5927. result := SyntaxTree.invalidDesignator;
  5928. type := left.type;
  5929. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5930. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5931. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5932. result.SetAssignable(TRUE);
  5933. result.SetType(type);
  5934. result.SetHidden(left.isHidden);
  5935. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5936. type := type.resolved;
  5937. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5938. result.SetAssignable(TRUE);
  5939. result.SetType(type);
  5940. result.SetHidden(left.isHidden);
  5941. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5942. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5943. result.SetAssignable(TRUE);
  5944. result.SetType(type);
  5945. result.SetHidden(left.isHidden);
  5946. ELSE
  5947. Error(position, "dereference on no pointer");
  5948. IF VerboseErrorMessage THEN
  5949. Printout.Info("pointer", type);
  5950. Printout.Info("scope", currentScope);
  5951. END;
  5952. END;
  5953. RETURN result
  5954. END NewDereferenceDesignator;
  5955. (** check supercall designator left^
  5956. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5957. - return new supercall designator with type = left.type
  5958. with error handling
  5959. **)
  5960. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5961. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5962. objectScope: SyntaxTree.Scope;
  5963. BEGIN
  5964. result := SyntaxTree.invalidDesignator;
  5965. IF left = SyntaxTree.invalidDesignator THEN
  5966. (* error already handled *)
  5967. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5968. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5969. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5970. IF symbol IS SyntaxTree.Procedure THEN
  5971. procedure := symbol(SyntaxTree.Procedure);
  5972. objectScope := currentScope;
  5973. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5974. objectScope := objectScope.outerScope;
  5975. END;
  5976. IF (left.left = NIL) OR ~
  5977. (
  5978. (left.left IS SyntaxTree.SelfDesignator) OR
  5979. (left.left IS SyntaxTree.DereferenceDesignator)
  5980. & (left.left(SyntaxTree.Designator).left # NIL)
  5981. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5982. Error(position, "procedure not in immediate object scope");
  5983. IF VerboseErrorMessage THEN
  5984. Printout.Info("left.left",left.left);
  5985. END;
  5986. ELSIF procedure.super # NIL THEN
  5987. result := SyntaxTree.NewSupercallDesignator(position,left);
  5988. result.SetType(left.type.resolved)
  5989. ELSE
  5990. Error(position, "no supermethod for this procedure");
  5991. END;
  5992. ELSE
  5993. Error(position, "symbol is not a procedure");
  5994. END;
  5995. ELSE
  5996. Error(position, "is no symbol designator");
  5997. END;
  5998. RETURN result
  5999. END NewSupercallDesignator;
  6000. (** check and semantically resolve arrow designator left^
  6001. - if left is procedure type -> result := SupercallDesignator
  6002. - else result := DereferenceDesignator
  6003. returns result via global variable resolvedExpression
  6004. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6005. **)
  6006. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6007. VAR left: SyntaxTree.Designator;
  6008. BEGIN
  6009. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6010. left := ResolveDesignator(arrowDesignator.left);
  6011. IF left # NIL THEN
  6012. IF (left.type = NIL) THEN
  6013. Error(arrowDesignator.position,"Invalid arrow designator");
  6014. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6015. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6016. ELSE
  6017. IF IsPointerToObject(left.type) THEN
  6018. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6019. END;
  6020. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6021. END
  6022. END
  6023. END VisitArrowDesignator;
  6024. (** check and return expression
  6025. - if expression has no type then resolve expression
  6026. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6027. - return result
  6028. **)
  6029. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6030. VAR result,prev: SyntaxTree.Expression;
  6031. BEGIN
  6032. IF expression = NIL THEN result := NIL
  6033. ELSIF (expression.type = NIL) THEN
  6034. prev := resolvedExpression;
  6035. resolvedExpression := SyntaxTree.invalidExpression;
  6036. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6037. expression.SetType(SyntaxTree.invalidType);
  6038. END;
  6039. expression.Accept(SELF);
  6040. result := resolvedExpression;
  6041. IF currentIsRealtime THEN
  6042. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6043. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6044. END;
  6045. END;
  6046. (* designator modifiers for backends if they support it ...*)
  6047. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6048. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6049. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6050. END;
  6051. resolvedExpression := prev
  6052. ELSE
  6053. result := expression
  6054. END;
  6055. RETURN result
  6056. END ResolveExpression;
  6057. (**
  6058. check expression to be constant expression
  6059. - resolve expression
  6060. - if valid then check that of value type
  6061. report error and return invalidExpression if anything fails
  6062. **)
  6063. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6064. VAR position: Position;
  6065. BEGIN
  6066. position := expression.position;
  6067. expression := ResolveExpression(expression);
  6068. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6069. ELSIF (expression.resolved = NIL) THEN
  6070. Error(position, "expression is not constant");
  6071. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6072. expression := SyntaxTree.invalidExpression;
  6073. END;
  6074. RETURN expression
  6075. END ConstantExpression;
  6076. (** check expression to be constant integer
  6077. - resolve expresssion
  6078. - if valid then check that of integer value type
  6079. report error and return invalidExpression if anything fails
  6080. **)
  6081. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6082. VAR position: Position;
  6083. BEGIN
  6084. position := expression.position;
  6085. expression := ResolveExpression(expression);
  6086. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6087. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6088. expression := SyntaxTree.invalidExpression;
  6089. Error(position, "expression is not a constant integer");
  6090. END;
  6091. RETURN expression
  6092. END ConstantInteger;
  6093. (** check expression as positive (>=0) constant integer
  6094. - resolve expression
  6095. - if valid then check that integer value
  6096. - if integer value then check that value >= 0
  6097. report error and return invalidExpression if anything fails
  6098. **)
  6099. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6100. VAR position: Position;
  6101. BEGIN
  6102. position := expression.position;
  6103. expression := ConstantExpression(expression);
  6104. IF expression = SyntaxTree.invalidExpression THEN
  6105. (* error already reported *)
  6106. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6107. Error(position, "expression is not integer valued");
  6108. expression := SyntaxTree.invalidExpression
  6109. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6110. Error(position, "integer is not greater or equal zero");
  6111. END;
  6112. RETURN expression
  6113. END ConstantIntegerGeq0;
  6114. (** check expression as condition
  6115. - resolve expression
  6116. - if valid expression then check that result type is boolean
  6117. report error and return invalidExpression if anything fails
  6118. **)
  6119. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6120. VAR position: Position;
  6121. BEGIN
  6122. position := expression.position;
  6123. expression := ResolveExpression(expression);
  6124. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6125. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6126. expression := SyntaxTree.invalidExpression;
  6127. Error(position, "expression is not boolean");
  6128. END;
  6129. RETURN expression
  6130. END ResolveCondition;
  6131. (*** symbols ***)
  6132. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6133. BEGIN
  6134. x.Accept(SELF);
  6135. END ResolveSymbol;
  6136. (** check a symbol
  6137. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6138. **)
  6139. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6140. VAR scope: SyntaxTree.Scope;
  6141. BEGIN
  6142. (* visibility *)
  6143. scope := symbol.scope;
  6144. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6145. scope := scope.outerScope;
  6146. END;
  6147. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6148. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6149. Error(symbol.position,"cannot be exported");
  6150. IF VerboseErrorMessage THEN
  6151. Printout.Info("symbol",symbol);
  6152. END;
  6153. END;
  6154. END;
  6155. END CheckSymbolVisibility;
  6156. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6157. If node is currently being resolved then emit a cyclic definition error.
  6158. Return TRUE only if node is fully resolved.
  6159. **)
  6160. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6161. VAR result: BOOLEAN;
  6162. BEGIN
  6163. IF SyntaxTree.Resolved IN x.state THEN
  6164. result := FALSE
  6165. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6166. Error(x.position,"cyclic definition");
  6167. result := FALSE;
  6168. ELSE
  6169. result := TRUE;
  6170. x.SetState(SyntaxTree.BeingResolved)
  6171. END;
  6172. RETURN result
  6173. END SymbolNeedsResolution;
  6174. (** check and resolve a type declaration symbol = Type
  6175. - set type to declaration type
  6176. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6177. This is so because the type declaration itself does not have a type but it only stands for a type.
  6178. In the implementation of the compiler this made a lot much easier.
  6179. - resolve and set declared type
  6180. - check symbol
  6181. **)
  6182. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6183. VAR prevScope: SyntaxTree.Scope;
  6184. BEGIN
  6185. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6186. IF SymbolNeedsResolution(typeDeclaration) THEN
  6187. typeDeclaration.SetState(SyntaxTree.Resolved);
  6188. prevScope := currentScope;
  6189. currentScope := typeDeclaration.scope;
  6190. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6191. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6192. CheckSymbolVisibility(typeDeclaration);
  6193. typeDeclaration.SetState(SyntaxTree.Resolved);
  6194. currentScope := prevScope;
  6195. END;
  6196. END VisitTypeDeclaration;
  6197. (** check and resolve a constant declaration symbol = (constant) expression
  6198. - check expression
  6199. - set type and value
  6200. - check symbol
  6201. **)
  6202. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6203. VAR
  6204. expression: SyntaxTree.Expression;
  6205. type: SyntaxTree.Type;
  6206. name: Basic.SegmentedName;
  6207. replacement: Replacement;
  6208. BEGIN
  6209. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6210. IF SymbolNeedsResolution(constant) THEN
  6211. expression := constant.value;
  6212. IF replacements # NIL THEN
  6213. Global.GetSymbolSegmentedName(constant, name);
  6214. replacement := replacements;
  6215. WHILE (replacement # NIL) & (replacement.name # name) DO
  6216. replacement := replacement.next;
  6217. END;
  6218. IF replacement # NIL THEN
  6219. InfoSS(constant.position, "replacing constant", constant.name);
  6220. (*
  6221. NEW(stringReader, Strings.Length(replacement.string^));
  6222. stringReader.Set(replacement.string^);
  6223. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6224. NEW(parser, scanner, diagnostics);
  6225. expression := parser.Expression();
  6226. *)
  6227. expression := replacement.expression;
  6228. replacement.used := TRUE;
  6229. END;
  6230. END;
  6231. constant.SetType(SyntaxTree.invalidType);
  6232. expression := ConstantExpression(expression);
  6233. ASSERT(expression.type # NIL);
  6234. type := expression.type.resolved;
  6235. constant.SetType(type);
  6236. constant.SetValue(expression);
  6237. CheckSymbolVisibility(constant);
  6238. constant.SetState(SyntaxTree.Resolved);
  6239. END;
  6240. END VisitConstant;
  6241. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6242. VAR procedureAlignment: LONGINT;
  6243. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6244. (* least common multiple *)
  6245. VAR a,b: LONGINT;
  6246. BEGIN
  6247. a := a0; b := b0;
  6248. WHILE (a # b) DO
  6249. IF a < b THEN a := a+a0
  6250. ELSE b := b + b0
  6251. END;
  6252. END;
  6253. RETURN a
  6254. END LCM;
  6255. BEGIN
  6256. IF alignment > 1 THEN
  6257. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6258. IF (procedureAlignment > 1) THEN
  6259. alignment := LCM(alignment, procedureAlignment);
  6260. END;
  6261. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6262. END;
  6263. END AdaptStackAlignment;
  6264. (** check and resolve a variable / field
  6265. - check and set type
  6266. - negative check on open array type
  6267. - check symbol
  6268. **)
  6269. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6270. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6271. BEGIN
  6272. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6273. IF SymbolNeedsResolution(variable) THEN
  6274. modifiers := variable.modifiers;
  6275. (*
  6276. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6277. variable.AddFlags(flags);
  6278. *)
  6279. variable.SetType(ResolveType(variable.type));
  6280. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6281. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6282. Error(variable.position,"forbidden open array variable");
  6283. END;
  6284. END;
  6285. CheckSymbolVisibility(variable);
  6286. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6287. variable.SetUntraced(TRUE);
  6288. IF ~ContainsPointer(variable.type) THEN
  6289. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6290. Error(position, "untraced flag on non-pointer variable");
  6291. END;
  6292. END;
  6293. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6294. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6295. IF ~PowerOf2(value) THEN
  6296. Error(position, "forbidden alignment - must be power of two");
  6297. ELSE
  6298. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6299. END;
  6300. END;
  6301. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6302. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6303. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6304. Error(position, "fixed position not possible in procedure");
  6305. END;
  6306. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6307. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6308. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6309. Error(position,"fictive offset not possible in procedure");
  6310. END;
  6311. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6312. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6313. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6314. END;
  6315. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6316. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6317. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6318. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6319. END;
  6320. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6321. IF variable.initializer # NIL THEN
  6322. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6323. END;
  6324. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6325. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6326. pointerType.SetPointerBase(variable.type);
  6327. pointerType.SetHidden(TRUE);
  6328. variable.SetType(ResolveType(pointerType));
  6329. END;
  6330. variable.SetState(SyntaxTree.Resolved);
  6331. END;
  6332. END VisitVariable;
  6333. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6334. BEGIN
  6335. VisitVariable(property)
  6336. END VisitProperty;
  6337. (** check and resolve a (procedure) parameter
  6338. - check and set type
  6339. - check symbol
  6340. - check parameter kind and set read-only flags if appropriate
  6341. **)
  6342. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6343. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6344. BEGIN
  6345. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6346. IF SymbolNeedsResolution(parameter) THEN
  6347. modifiers := parameter.modifiers;
  6348. parameter.SetType(ResolveType(parameter.type));
  6349. ASSERT(parameter.type.resolved # NIL);
  6350. CheckSymbolVisibility(parameter);
  6351. IF parameter.defaultValue # NIL THEN
  6352. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6353. Error(parameter.position,"forbidden default value on non-value parameter");
  6354. ELSE
  6355. expression := ConstantExpression(parameter.defaultValue);
  6356. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6357. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6358. parameter.SetDefaultValue(expression);
  6359. END;
  6360. END;
  6361. END;
  6362. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6363. Error(parameter.position, "forbidden value parameter of math array type ");
  6364. END;
  6365. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6366. parameter.SetUntraced(TRUE);
  6367. IF ~ContainsPointer(parameter.type) THEN
  6368. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6369. Error(position, "untraced flag on non-pointer variable");
  6370. END;
  6371. END;
  6372. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6373. parameter.SetMoveable(TRUE);
  6374. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6375. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6376. Error(position, "illegal movable flag on non-address variable");
  6377. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6378. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6379. Error(position, "unnecessary movable flag on variable variable");
  6380. END;
  6381. END;
  6382. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6383. parameter.SetState(SyntaxTree.Resolved);
  6384. END;
  6385. END VisitParameter;
  6386. (** check and resolve a procedure (with declaration and implementation scope)
  6387. - check the procedure type
  6388. - check if method (i.e. in record scope), if so then
  6389. - check if (unique) constructor
  6390. - check if (unique) finalizer
  6391. - check if super method available, if so then check signature
  6392. - of not in record scope then negative check on constructor flag
  6393. - of not in record scope then negative check on finalizer flag
  6394. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6395. - check procedure symbol
  6396. **)
  6397. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6398. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6399. procedureType: SyntaxTree.ProcedureType;
  6400. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6401. qualifiedType: SyntaxTree.QualifiedType;
  6402. value: Basic.Integer;
  6403. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6404. position: Position;
  6405. fp: SyntaxTree.FingerPrint;
  6406. BEGIN
  6407. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6408. IF IsOberonInline(procedure) THEN
  6409. IF SyntaxTree.Public * procedure.access # {} THEN
  6410. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6411. END;
  6412. procedure.SetInline(FALSE);
  6413. procedure.SetOberonInline(TRUE);
  6414. END;
  6415. IF SymbolNeedsResolution(procedure) THEN
  6416. recentIsRealtime := currentIsRealtime;
  6417. recentIsBodyProcedure := currentIsBodyProcedure;
  6418. CheckSymbolVisibility(procedure);
  6419. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6420. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6421. modifiers := procedureType.modifiers;
  6422. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6423. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6424. IF useDarwinCCalls THEN (*fld*)
  6425. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6426. ELSE
  6427. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6428. END
  6429. END;
  6430. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6431. procedureType.SetInterrupt(TRUE);
  6432. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6433. END;
  6434. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6435. procedureType.SetNoReturn(TRUE);
  6436. END;
  6437. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6438. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6439. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6440. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6441. END;
  6442. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6443. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6444. END;
  6445. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6446. IF ~PowerOf2(value) THEN
  6447. Error(position, "forbidden stack alignment - must be power of two");
  6448. ELSE
  6449. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6450. END;
  6451. END;
  6452. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6453. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6454. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6455. END;
  6456. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6457. SyntaxTree.InitFingerPrint(fp);
  6458. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6459. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6460. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6461. fp.shallowAvailable := TRUE;
  6462. procedure.SetFingerPrint(fp);
  6463. END;
  6464. CheckModifiers(modifiers, TRUE);
  6465. modifiers := procedureType.returnTypeModifiers;
  6466. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6467. CheckModifiers(modifiers, TRUE);
  6468. procedure.SetState(SyntaxTree.Resolved);
  6469. FixProcedureType(procedureType);
  6470. currentIsRealtime := procedureType.isRealtime;
  6471. currentIsBodyProcedure := procedure.isBodyProcedure;
  6472. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6473. THEN
  6474. Error(procedure.position,"problems during parameter offset computation");
  6475. END;
  6476. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6477. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6478. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6479. Error(procedure.position,"problem during parameter offset generation");
  6480. END;
  6481. END;
  6482. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6483. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6484. procedureType.SetDelegate(TRUE);
  6485. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6486. (* add auto-self *)
  6487. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6488. IF (record.pointerType.typeDeclaration = NIL) THEN
  6489. selfParameter.SetType(record.pointerType);
  6490. ELSE
  6491. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6492. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6493. qualifiedType.SetResolved(record.pointerType);
  6494. selfParameter.SetType(qualifiedType);
  6495. END;
  6496. selfParameter.SetAccess(SyntaxTree.Hidden);
  6497. END;
  6498. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6499. IF procedure.isConstructor THEN
  6500. (*! constructor is always visible, compatibility to paco
  6501. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6502. *)
  6503. procedure.MarkUsed;
  6504. IF procedureType.returnType # NIL THEN
  6505. Error(procedure.position,"constructor with forbidden return type");
  6506. END;
  6507. proc := procedure.scope.firstProcedure;
  6508. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6509. proc := proc.nextProcedure;
  6510. END;
  6511. IF proc # NIL THEN
  6512. Error(procedure.position,"duplicate constructor")
  6513. ELSE
  6514. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6515. END;
  6516. END;
  6517. IF procedure.isFinalizer THEN
  6518. procedure.MarkUsed;
  6519. IF procedureType.returnType # NIL THEN
  6520. Error(procedure.position,"finalizer with forbidden return type");
  6521. END;
  6522. IF procedureType.numberParameters # 0 THEN
  6523. Error(procedure.position,"finalizer with formal parameters");
  6524. END;
  6525. proc := procedure.scope.firstProcedure;
  6526. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6527. proc := proc.nextProcedure;
  6528. END;
  6529. IF proc # NIL THEN
  6530. Error(procedure.position,"duplicate finalizer")
  6531. ELSE
  6532. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6533. END;
  6534. END;
  6535. super := FindSuperProcedure(record.recordScope, procedure);
  6536. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6537. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6538. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6539. END;
  6540. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6541. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6542. END;
  6543. IF super.isFinal THEN
  6544. Error(procedure.position,"forbidden method extending final method");
  6545. END;
  6546. (*
  6547. IF super.access # procedure.access THEN
  6548. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6549. END;
  6550. *)
  6551. procedure.SetSuper(super);
  6552. super.SetOverwritten(TRUE);
  6553. procedure.SetAccess(procedure.access+super.access);
  6554. procedure.MarkUsed;
  6555. END;
  6556. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6557. THEN
  6558. Error(procedure.position,"problems during parameter offset computation");
  6559. END;
  6560. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6561. IF cellsAreObjects THEN
  6562. procedureType.SetDelegate(TRUE);
  6563. END;
  6564. IF procedure.isConstructor THEN
  6565. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6566. END;
  6567. ELSIF procedure.isConstructor THEN
  6568. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6569. END;
  6570. Declarations(procedure.procedureScope, FALSE, {0,1});
  6571. (* body resolution part done as late fix of the procedure type *)
  6572. procedure.SetState(SyntaxTree.Resolved);
  6573. currentIsRealtime := recentIsRealtime;
  6574. currentIsBodyProcedure := recentIsBodyProcedure;
  6575. END;
  6576. END VisitProcedure;
  6577. (**
  6578. a builtin procedure is a global item that may not be modified locally
  6579. instead the resolving of builtin procedure calls are done in the esignator
  6580. **)
  6581. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6582. VAR type: SyntaxTree.Type;
  6583. BEGIN
  6584. type := ResolveType(builtinProcedure.type);
  6585. END VisitBuiltin;
  6586. (* nopov *)
  6587. (** check and resolve operator
  6588. - operators are first checked as procedures
  6589. - then additional operator-specific checks are done
  6590. - note that only module-scope operators are checked here
  6591. (operators in a record scope are only allowed in the context of
  6592. array-structured object types and checked in 'ResolveArrayStructure')
  6593. - also note that inter-operator conformity is not checked here
  6594. **)
  6595. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6596. VAR
  6597. procedureType: SyntaxTree.ProcedureType;
  6598. leftType, rightType: SyntaxTree.Type;
  6599. identifierNumber: LONGINT; position: Position;
  6600. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6601. modifiers: SyntaxTree.Modifier;
  6602. (** whether a type is locally defined in the current module scope
  6603. for arrays, the base type must be locally defined **)
  6604. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6605. BEGIN
  6606. IF type = NIL THEN
  6607. RETURN FALSE
  6608. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6609. RETURN TRUE
  6610. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6611. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6612. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6613. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6614. ELSE
  6615. RETURN FALSE
  6616. END
  6617. END IsLocallyDefined;
  6618. BEGIN
  6619. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6620. procedureType := operator.type(SyntaxTree.ProcedureType);
  6621. modifiers := procedureType.modifiers;
  6622. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6623. CheckModifiers(modifiers, TRUE);
  6624. VisitProcedure(operator);
  6625. IF operator.scope IS SyntaxTree.RecordScope THEN
  6626. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6627. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6628. IF identifierNumber = -1 THEN
  6629. Error(operator.position, "operator with unknown identifier")
  6630. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6631. Error(operator.position, "identifier may not be used for operator")
  6632. ELSE
  6633. IF procedureType.numberParameters < 1 THEN
  6634. Error(operator.position, "operator without operand");
  6635. ELSIF procedureType.numberParameters > 2 THEN
  6636. Error(operator.position, "operator with more than two operands");
  6637. ELSE
  6638. (* determine operand types *)
  6639. leftType := procedureType.firstParameter.type;
  6640. IF procedureType.numberParameters > 1 THEN
  6641. rightType := procedureType.firstParameter.nextParameter.type
  6642. ELSE
  6643. rightType := NIL
  6644. END;
  6645. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6646. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6647. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6648. Error(operator.position, "none of the operands is declared in the same module")
  6649. END
  6650. END;
  6651. (* TODO: refine the checks, think about how restrictive the checks should be
  6652. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6653. They might be used for intersection, union, complement of custom object types *)
  6654. (* defaults *)
  6655. hasReturnType := TRUE;
  6656. mustBeUnary := FALSE;
  6657. mustBeBinary := FALSE;
  6658. mustReturnBoolean := FALSE;
  6659. mustReturnInteger := FALSE;
  6660. mustHaveEquitypedOperands := FALSE;
  6661. (* operator-specific exceptions *)
  6662. CASE identifierNumber OF
  6663. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6664. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6665. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6666. mustBeBinary := TRUE
  6667. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6668. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6669. | Scanner.Times: mustBeBinary := TRUE
  6670. | Scanner.TimesTimes: mustBeBinary := TRUE
  6671. | Scanner.DotTimes: mustBeBinary := TRUE
  6672. | Scanner.PlusTimes: mustBeBinary := TRUE
  6673. | Scanner.Slash: mustBeBinary := TRUE
  6674. | Scanner.Backslash: mustBeBinary := TRUE
  6675. | Scanner.DotSlash: mustBeBinary := TRUE
  6676. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6677. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6678. | Scanner.Not: mustBeUnary := TRUE
  6679. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6680. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6681. | Scanner.Transpose: mustBeUnary := TRUE;
  6682. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6683. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6684. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6685. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6686. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6687. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6688. | Global.Abs: mustBeUnary := TRUE;
  6689. | Global.Ash: (* TODO: arity? *)
  6690. | Global.Cap: (* TODO: arity? *)
  6691. | Global.Chr: mustBeUnary := TRUE;
  6692. | Global.Entier: (* TODO: arity? *)
  6693. | Global.EntierH: (* TODO: arity? *)
  6694. | Global.Len: (* unary and binary *)
  6695. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6696. | Global.Max, Global.Min: (* unary and binary *)
  6697. | Global.Odd: (* TODO: arity? *)
  6698. | Global.Sum: (* TODO: arity? *)
  6699. | Global.All: (* TODO: arity? *)
  6700. | Global.Re, Global.Im:
  6701. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6702. | Scanner.Alias:
  6703. | Scanner.GreaterGreater, Scanner.LessLess:
  6704. mustBeBinary := TRUE; hasReturnType := FALSE;
  6705. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6706. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6707. END;
  6708. (* check parameter count *)
  6709. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6710. Error(operator.position,"operator is not unary")
  6711. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6712. Error(operator.position,"operator is not binary")
  6713. END;
  6714. (* check parameter types *)
  6715. (* TODO: is this used at all? *)
  6716. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6717. leftType := procedureType.firstParameter.type;
  6718. rightType := procedureType.firstParameter.nextParameter.type;
  6719. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6720. Error(operator.position, "the two operands are not of the same type")
  6721. END
  6722. END;
  6723. (* check return type *)
  6724. IF hasReturnType THEN
  6725. IF procedureType.returnType = NIL THEN
  6726. Error(operator.position, "return type required")
  6727. ELSIF mustReturnBoolean THEN
  6728. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6729. Error(operator.position,"return type is not Boolean")
  6730. END
  6731. ELSIF mustReturnInteger THEN
  6732. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6733. Error(operator.position,"return type is not integer")
  6734. END
  6735. END
  6736. ELSIF procedureType.returnType # NIL THEN
  6737. Error(operator.position, "return type not allowed")
  6738. END
  6739. END
  6740. END
  6741. END
  6742. END VisitOperator;
  6743. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6744. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6745. BEGIN
  6746. IF error THEN RETURN FALSE END;
  6747. prevScope := currentScope;
  6748. prevDiagnostics := diagnostics;
  6749. diagnostics := NIL; (* suppress error output *)
  6750. currentScope := module.moduleScope;
  6751. VisitImport(x);
  6752. IF ~error THEN
  6753. module.moduleScope.AddImport(x);
  6754. x.SetScope(module.moduleScope);
  6755. END;
  6756. currentScope := prevScope;
  6757. diagnostics := prevDiagnostics;
  6758. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6759. END AddImport;
  6760. (** check and resolve import
  6761. - check for name = SYSTEM
  6762. - check for forbidden self import
  6763. - search through global import cache: already imported?
  6764. - check if already imported indirectly
  6765. - import if necessary -> set module and enter into import cache
  6766. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6767. - after this import this direct import and all indirect imports are stored in the current module's import list
  6768. **)
  6769. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6770. VAR
  6771. module: SyntaxTree.Module;
  6772. moduleScope: SyntaxTree.ModuleScope;
  6773. import,reimport: SyntaxTree.Import;
  6774. filename: FileName;
  6775. prevScope: SyntaxTree.Scope;
  6776. BEGIN
  6777. IF SymbolNeedsResolution(x) THEN
  6778. prevScope := currentScope;
  6779. x.SetType(SyntaxTree.importType);
  6780. moduleScope := currentScope.ownerModule.moduleScope;
  6781. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6782. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6783. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6784. Error(x.position,"forbidden self import");
  6785. ELSE
  6786. (* search through global import list: already imported ? *)
  6787. IF (x.module = NIL) & (importCache # NIL) THEN
  6788. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6789. ELSE import := NIL
  6790. END;
  6791. IF x.module # NIL THEN (* already imported indirectly *)
  6792. module := x.module;
  6793. ELSIF import # NIL THEN (* already in module list *)
  6794. module := import.module;
  6795. ASSERT(module # NIL);
  6796. x.SetModule(module);
  6797. ELSE (* must be imported *)
  6798. Global.ModuleFileName(x.moduleName,x.context,filename);
  6799. IF symbolFileFormat # NIL THEN
  6800. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6801. IF module = NIL THEN
  6802. ErrorSS(x.position,"could not import",filename);
  6803. IF VerboseErrorMessage THEN
  6804. Printout.Info("import",x)
  6805. END
  6806. ELSE
  6807. (*
  6808. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6809. (*! should rather be done by importer *)
  6810. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6811. checker.importCache := importCache;
  6812. checker.arrayBaseImported := arrayBaseImported;
  6813. checker.global := global;
  6814. checker.Module(module); (* semantic check *)
  6815. error := error OR checker.error;
  6816. END;
  6817. *)
  6818. (*
  6819. ASSERT(SyntaxTree.Resolved IN module.state);
  6820. *)
  6821. x.SetModule(module);
  6822. IF importCache # NIL THEN
  6823. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6824. import.SetContext(x.context);
  6825. import.SetModule(module);
  6826. importCache.AddImport(import);
  6827. END;
  6828. END;
  6829. ELSE
  6830. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6831. END;
  6832. END;
  6833. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6834. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6835. END;
  6836. import := module.moduleScope.firstImport;
  6837. WHILE(import # NIL) DO
  6838. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6839. ASSERT(currentScope # NIL);
  6840. ASSERT(currentScope.ownerModule # NIL);
  6841. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6842. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6843. Error(x.position,"recursive import");
  6844. ELSE
  6845. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6846. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6847. IF reimport = NIL THEN (* indirect import *)
  6848. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6849. reimport.SetContext(import.context);
  6850. reimport.SetModule(import.module);
  6851. moduleScope.AddImport(reimport);
  6852. reimport.SetScope(moduleScope);
  6853. ELSE
  6854. ASSERT(import.module # NIL);
  6855. reimport.SetModule(import.module); (* direct or indirect import *)
  6856. END;
  6857. END;
  6858. import := import.nextImport;
  6859. END;
  6860. END;
  6861. END;
  6862. currentScope := prevScope;
  6863. (* ELSE nothing to be done *)
  6864. x.SetState(SyntaxTree.Resolved);
  6865. END;
  6866. END VisitImport;
  6867. (*** statements ***)
  6868. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6869. VAR prev,resolved: SyntaxTree.Statement;
  6870. BEGIN
  6871. prev := resolvedStatement;
  6872. resolvedStatement := x;
  6873. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6874. activeCellsStatement := FALSE;
  6875. x.Accept(SELF);
  6876. (* removed this, implementation restriction should be resolved by backend
  6877. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6878. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6879. END;
  6880. *)
  6881. resolved := resolvedStatement;
  6882. resolvedStatement := prev;
  6883. RETURN resolved
  6884. END ResolveStatement;
  6885. (** check and resolve statement sequence
  6886. - check all statements, replace if necessary
  6887. **)
  6888. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6889. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6890. BEGIN
  6891. IF statementSequence # NIL THEN (* else empty *)
  6892. FOR i := 0 TO statementSequence.Length()-1 DO
  6893. statement := statementSequence.GetStatement(i);
  6894. resolved := ResolveStatement(statement);
  6895. IF (resolved # statement) THEN
  6896. statementSequence.SetStatement(i,resolved);
  6897. END;
  6898. END;
  6899. END;
  6900. END StatementSequence;
  6901. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6902. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6903. - check if procedure is callable
  6904. - check return type = NIL (otherwise must be assignment statement)
  6905. **)
  6906. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6907. VAR call: SyntaxTree.Designator;
  6908. BEGIN
  6909. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6910. call := procedureCall.call;
  6911. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6912. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6913. END;
  6914. call := ResolveDesignator(call);
  6915. IF call = SyntaxTree.invalidDesignator THEN
  6916. (* error already handled *)
  6917. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6918. (* inline call in a statement *)
  6919. ELSIF ~IsCallable(call) THEN
  6920. Error(procedureCall.position,"called object is not a procedure");
  6921. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6922. Error(procedureCall.position,"calling procedure with non-void return type");
  6923. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6924. END;
  6925. procedureCall.SetCall(call);
  6926. (*
  6927. IF call = SyntaxTree.invalidDesignator THEN
  6928. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6929. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6930. IF IsOberonInline(procedure) THEN
  6931. Warning(procedure.position,"call to inline proc");
  6932. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6933. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6934. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6935. resolvedStatement := block;
  6936. RETURN;
  6937. END;
  6938. END;
  6939. *)
  6940. END VisitProcedureCallStatement;
  6941. (** check and resolve assignment LHS := RHS
  6942. - resolve LHS and RHS
  6943. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6944. - check if assignment is compatible
  6945. - check if LHS is variable (i.e. assignable)
  6946. - convert RHS if necessary
  6947. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6948. - assignment between different ASOTs
  6949. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6950. - assignment to ASOT elements:
  6951. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6952. **)
  6953. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6954. VAR
  6955. left: SyntaxTree.Designator;
  6956. right, expression: SyntaxTree.Expression;
  6957. designator: SyntaxTree.Designator;
  6958. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6959. mathArrayType: SyntaxTree.MathArrayType;
  6960. BEGIN
  6961. right := ResolveExpression(assignment.right);
  6962. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6963. left := ResolveDesignator(assignment.left);
  6964. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6965. (* error already handled *)
  6966. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6967. (* LHS is index write operator call on ASOT *)
  6968. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6969. (* necessary ?
  6970. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6971. type := procedureType.firstParameter.type;
  6972. expression := procedureCallDesignator.parameters.GetExpression(0);
  6973. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6974. *)
  6975. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6976. ELSIF CheckVariable(left) THEN
  6977. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6978. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6979. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6980. (* conversion done by procedure call
  6981. (* try to convert to left argument *)
  6982. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6983. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6984. procedureCallDesignator.parameters.SetExpression(1, right);
  6985. END;
  6986. *)
  6987. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6988. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6989. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6990. ELSIF AssignmentCompatible(left, right) THEN
  6991. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6992. mathArrayType := MathArrayStructureOfType(left.type);
  6993. right := NewConversion(right.position, right, mathArrayType, NIL);
  6994. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6995. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6996. ELSE
  6997. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6998. assignment.SetLeft(left);
  6999. assignment.SetRight(right);
  7000. resolvedStatement := assignment
  7001. END
  7002. END
  7003. END
  7004. END VisitAssignment;
  7005. (** check and resolve assignment LHS := RHS
  7006. - resolve LHS and RHS
  7007. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7008. - check if assignment is compatible
  7009. - check if LHS is variable (i.e. assignable)
  7010. - convert RHS if necessary
  7011. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7012. - assignment between different ASOTs
  7013. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7014. - assignment to ASOT elements:
  7015. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7016. **)
  7017. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7018. VAR
  7019. left: SyntaxTree.Designator;
  7020. right: SyntaxTree.Expression;
  7021. inPort, outPort: SyntaxTree.PortType;
  7022. expression: SyntaxTree.Expression;
  7023. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7024. BEGIN
  7025. right := ResolveExpression(communication.right);
  7026. left := ResolveDesignator(communication.left);
  7027. communication.SetLeft(left);
  7028. communication.SetRight(right);
  7029. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7030. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7031. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7032. (* conversion done by procedure call
  7033. (* try to convert to left argument *)
  7034. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7035. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7036. procedureCallDesignator.parameters.SetExpression(1, right);
  7037. END;
  7038. *)
  7039. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7040. ELSE
  7041. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7042. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7043. (* error already handled *)
  7044. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7045. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7046. IF outPort.direction # SyntaxTree.OutPort THEN
  7047. Error(left.position,"not an out-port")
  7048. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7049. Error(left.position,"incompatible to port type");
  7050. ELSE
  7051. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7052. communication.SetRight(right)
  7053. END;
  7054. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7055. IF CheckVariable(left) THEN
  7056. IF inPort.direction # SyntaxTree.InPort THEN
  7057. Error(left.position,"not an in-port")
  7058. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7059. Error(right.position,"incompatible to port type");
  7060. END;
  7061. END;
  7062. ELSE
  7063. Error(communication.position,"unsupported stream operation");
  7064. END;
  7065. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7066. IF outPort.direction # SyntaxTree.OutPort THEN
  7067. Error(left.position,"not an out-port")
  7068. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7069. Error(left.position,"incompatible to port type");
  7070. ELSE
  7071. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7072. communication.SetRight(right)
  7073. END;
  7074. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7075. IF CheckVariable(right) THEN
  7076. IF inPort.direction # SyntaxTree.InPort THEN
  7077. Error(left.position,"not an in-port")
  7078. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7079. Error(right.position,"incompatible to port type");
  7080. END;
  7081. END;
  7082. ELSE
  7083. Error(communication.position, "unsupported operation");
  7084. END;
  7085. END;
  7086. END VisitCommunicationStatement;
  7087. (** check and resolve if/eslif part
  7088. - check condition
  7089. - check statement sequence
  7090. **)
  7091. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7092. VAR prevUnreachable, b: BOOLEAN;
  7093. BEGIN
  7094. prevUnreachable := currentIsUnreachable;
  7095. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7096. IF IsBooleanValue(ifPart.condition,b) THEN
  7097. IF b=FALSE THEN
  7098. currentIsUnreachable := TRUE
  7099. ELSIF b=TRUE THEN
  7100. true := TRUE
  7101. END;
  7102. END;
  7103. StatementSequence(ifPart.statements);
  7104. currentIsUnreachable := prevUnreachable;
  7105. END IfPart;
  7106. (** check and resolve if statement
  7107. - check if parts and else part statement sequence
  7108. **)
  7109. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7110. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7111. BEGIN
  7112. prevUnreachable := currentIsUnreachable;
  7113. ifPartTrue := FALSE;
  7114. IfPart(ifStatement.ifPart,ifPartTrue);
  7115. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7116. elsif := ifStatement.GetElsifPart(i);
  7117. IfPart(elsif,ifPartTrue);
  7118. END;
  7119. IF ifStatement.elsePart # NIL THEN
  7120. IF ifPartTrue THEN
  7121. currentIsUnreachable := TRUE
  7122. END;
  7123. StatementSequence(ifStatement.elsePart)
  7124. END;
  7125. currentIsUnreachable := prevUnreachable;
  7126. END VisitIfStatement;
  7127. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7128. VAR variable: SyntaxTree.Designator;
  7129. type,variableType: SyntaxTree.Type;
  7130. withEntry: WithEntry;
  7131. BEGIN
  7132. variable := ResolveDesignator(withPart.variable);
  7133. variableType := variable.type.resolved;
  7134. withPart.SetVariable(variable);
  7135. type := ResolveType(withPart.type);
  7136. withPart.SetType(type);
  7137. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7138. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7139. END;
  7140. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7141. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7142. Error(variable.position,"is not extensible designator");
  7143. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7144. Error(variable.position,"is no local variable ");
  7145. IF VerboseErrorMessage THEN
  7146. Printout.Info("variable",variable)
  7147. END;
  7148. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7149. Error(variable.position,"withguarded symbol is no type extension of ");
  7150. IF VerboseErrorMessage THEN
  7151. Printout.Info("variable",variable);
  7152. Printout.Info("type",type);
  7153. END;
  7154. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7155. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7156. Error(variable.position,"withguarded symbol is no variable ");
  7157. IF VerboseErrorMessage THEN
  7158. Printout.Info("variable",variable);
  7159. Printout.Info("type",type);
  7160. END;
  7161. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7162. Error(variable.position,"invalid change of withguarded symbol");
  7163. ELSE
  7164. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7165. NEW(withEntry);
  7166. withEntry.previous := withEntries;
  7167. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7168. withEntry.type := type;
  7169. withEntries := withEntry;
  7170. StatementSequence(withPart.statements);
  7171. withEntries := withEntries.previous;
  7172. END;
  7173. END WithPart;
  7174. (** check and resolve with statement WITH variable: type DO ... END;
  7175. - check type and variable
  7176. - check that variable type is type extension of type
  7177. - check that variable is a variable
  7178. - enter new with scope and enter guardedVariable with same name and reference to variable
  7179. - create if statement:
  7180. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7181. **)
  7182. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7183. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7184. BEGIN
  7185. prevScope := currentScope; symbol := NIL;
  7186. FOR i := 0 TO withStatement.WithParts()-1 DO
  7187. WithPart(withStatement.GetWithPart(i),symbol);
  7188. END;
  7189. IF withStatement.elsePart # NIL THEN
  7190. StatementSequence(withStatement.elsePart)
  7191. END;
  7192. currentScope := prevScope;
  7193. END VisitWithStatement;
  7194. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7195. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7196. - check 'first' < 'last' and no overlaps between different case labels
  7197. - check statement sequence
  7198. **)
  7199. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7200. VAR
  7201. i: LONGINT;
  7202. position: Position;
  7203. expression, left, right: SyntaxTree.Expression;
  7204. expressionType: SyntaxTree.Type;
  7205. l, r: Basic.Integer;
  7206. cl, cr: CHAR;
  7207. thiscases: SyntaxTree.CaseConstant;
  7208. BEGIN
  7209. thiscases := NIL;
  7210. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7211. expression := casePart.elements.GetExpression(i);
  7212. position := expression.position;
  7213. (* set context of range *)
  7214. IF expression IS SyntaxTree.RangeExpression THEN
  7215. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7216. END;
  7217. expression := ResolveExpression(expression);
  7218. IF expression = SyntaxTree.invalidExpression THEN
  7219. (* error already reported *)
  7220. expressionType := SyntaxTree.invalidType;
  7221. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7222. (* read out 'first' and 'last' *)
  7223. left := expression(SyntaxTree.RangeExpression).first;
  7224. right := expression(SyntaxTree.RangeExpression).last;
  7225. (* guaranteed by VisitRangeExpression: *)
  7226. ASSERT((left # NIL) & (right # NIL));
  7227. ASSERT(left.type.resolved = right.type.resolved);
  7228. left := CompatibleConversion(left.position, left, type);
  7229. right := CompatibleConversion(right.position, right, type);
  7230. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7231. expression(SyntaxTree.RangeExpression).SetLast(right);
  7232. expressionType := RegularType(position,left.type);
  7233. ELSE
  7234. expression := ConstantExpression(expression);
  7235. expression := CompatibleConversion(expression.position, expression, type);
  7236. (*
  7237. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7238. left := Global.NewCharacterValue(system,expression.position,cl);
  7239. expression := casePart.elements.GetExpression(i);
  7240. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7241. expression := left
  7242. END;
  7243. *)
  7244. casePart.elements.SetExpression(i,expression);
  7245. left := expression; right := expression;
  7246. expressionType := RegularType(position,expression.type)
  7247. END;
  7248. IF (expressionType = SyntaxTree.invalidType) THEN
  7249. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7250. Error(position, "inadmissible case label");
  7251. expression := SyntaxTree.invalidExpression;
  7252. ELSE
  7253. l := 0; r := 0;
  7254. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7255. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7256. l := ORD(cl); r := ORD(cr);
  7257. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7258. ELSE
  7259. expression := SyntaxTree.invalidExpression
  7260. END;
  7261. IF expression # SyntaxTree.invalidExpression THEN
  7262. IF l>r THEN
  7263. Error(position, "empty case label")
  7264. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7265. Error(position, "duplicate case label");
  7266. ELSE
  7267. IF l < min THEN min := l END;
  7268. IF r > max THEN max := r END;
  7269. END;
  7270. END;
  7271. END;
  7272. casePart.elements.SetExpression(i,expression);
  7273. END;
  7274. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7275. casePart.SetConstants(thiscases);
  7276. StatementSequence(casePart.statements);
  7277. END CasePart;
  7278. (** check and resolve case statement CASE variable OF ... END;
  7279. - check variable
  7280. - check case parts
  7281. **)
  7282. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7283. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7284. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer; msg: ARRAY 64 OF CHAR;
  7285. BEGIN
  7286. expression := ResolveExpression(caseStatement.variable);
  7287. type := RegularType(expression.position,expression.type);
  7288. IF type = SyntaxTree.invalidType THEN
  7289. expression := SyntaxTree.invalidExpression;
  7290. ELSIF IsIntegerType(type) THEN
  7291. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7292. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7293. (*
  7294. expression := Global.NewCharacterValue(system,expression.position,ch);
  7295. *)
  7296. type := expression.type;
  7297. ELSIF IsCharacterType(type) THEN
  7298. ELSIF IsEnumerationType(type) THEN
  7299. ELSE
  7300. Error(caseStatement.variable.position,"variable must be integer or character type");
  7301. expression := SyntaxTree.invalidExpression;
  7302. END;
  7303. caseStatement.SetVariable(expression);
  7304. caseList := NIL;
  7305. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7306. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7307. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7308. END;
  7309. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7310. msg := "huge sparse case table ";
  7311. Strings.AppendInt(msg, max-min);
  7312. Strings.Append(msg,"/");
  7313. Strings.AppendInt(msg, caseStatement.CaseParts());
  7314. Warning(caseStatement.position,msg);
  7315. END;
  7316. caseStatement.SetMinMax(min,max);
  7317. StatementSequence(caseStatement.elsePart);
  7318. IF expression.resolved # NIL THEN
  7319. IF IsCharacterValue(expression,ch) THEN
  7320. l := ORD(ch)
  7321. ELSIF IsIntegerValue(expression,l) THEN
  7322. END;
  7323. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7324. END;
  7325. END VisitCaseStatement;
  7326. (** check and resolve while statement
  7327. - check condition
  7328. - check statement sequence
  7329. **)
  7330. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7331. VAR prevIsUnreachable,b: BOOLEAN;
  7332. BEGIN
  7333. prevIsUnreachable := currentIsUnreachable;
  7334. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7335. IF IsBooleanValue(whileStatement.condition,b) THEN
  7336. IF b=FALSE THEN
  7337. currentIsUnreachable := TRUE
  7338. END;
  7339. END;
  7340. StatementSequence(whileStatement.statements);
  7341. currentIsUnreachable := prevIsUnreachable
  7342. END VisitWhileStatement;
  7343. (** check and resolve repeat statement
  7344. - check condition
  7345. - check statement sequence
  7346. **)
  7347. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7348. BEGIN
  7349. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7350. StatementSequence(repeatStatement.statements);
  7351. END VisitRepeatStatement;
  7352. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7353. VAR withEntry: WithEntry;
  7354. BEGIN
  7355. withEntry := withEntries;
  7356. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7357. withEntry := withEntry.previous
  7358. END;
  7359. IF withEntry = NIL THEN RETURN FALSE
  7360. ELSE
  7361. type := withEntry.type;
  7362. RETURN TRUE
  7363. END;
  7364. END GetGuard;
  7365. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7366. - check that variable is an integer variable
  7367. - check that from is integer typed with compatible type
  7368. - check that to has compatible type
  7369. - check that by is constant integer with compatible type
  7370. **)
  7371. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7372. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7373. BEGIN
  7374. designator := ResolveDesignator(forStatement.variable);
  7375. type := SyntaxTree.invalidType;
  7376. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7377. designator := SyntaxTree.invalidDesignator;
  7378. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7379. Error(designator.position,"control variable of non-integer type");
  7380. designator := SyntaxTree.invalidDesignator;
  7381. ELSIF CheckVariable(designator) THEN
  7382. type := designator.type;
  7383. END;
  7384. forStatement.SetVariable(designator);
  7385. expression := ResolveExpression(forStatement.from);
  7386. IF expression = SyntaxTree.invalidExpression THEN
  7387. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7388. Error(expression.position,"start value of incompatible type");
  7389. expression := SyntaxTree.invalidExpression;
  7390. ELSIF type # SyntaxTree.invalidType THEN
  7391. expression := NewConversion(expression.position,expression,type,NIL)
  7392. END;
  7393. forStatement.SetFrom(expression);
  7394. expression := ResolveExpression(forStatement.to);
  7395. IF expression = SyntaxTree.invalidExpression THEN
  7396. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7397. Error(expression.position,"end value of incompatible type");
  7398. expression := SyntaxTree.invalidExpression;
  7399. ELSIF type # SyntaxTree.invalidType THEN
  7400. expression := NewConversion(expression.position,expression,type,NIL)
  7401. END;
  7402. forStatement.SetTo(expression);
  7403. IF forStatement.by # NIL THEN
  7404. expression := ConstantInteger(forStatement.by);
  7405. ELSE
  7406. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7407. END;
  7408. IF expression = SyntaxTree.invalidExpression THEN
  7409. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7410. Error(expression.position,"step value of incompatible type");
  7411. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7412. Error(expression.position,"invalid step value");
  7413. ELSIF type # SyntaxTree.invalidType THEN
  7414. expression := NewConversion(expression.position,expression,type,NIL)
  7415. END;
  7416. forStatement.SetBy(expression);
  7417. StatementSequence(forStatement.statements);
  7418. END VisitForStatement;
  7419. (** check and resolve loop statement LOOP StatementSequence END
  7420. - check statement sequence
  7421. **)
  7422. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7423. BEGIN
  7424. StatementSequence(loopStatement.statements)
  7425. END VisitLoopStatement;
  7426. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7427. BEGIN
  7428. StatementSequence(exitableBlock.statements);
  7429. END VisitExitableBlock;
  7430. (** check and resolve exit statement EXIT
  7431. - check that exit is within LOOP statement block
  7432. **)
  7433. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7434. VAR outer: SyntaxTree.Statement;
  7435. BEGIN
  7436. outer := exitStatement.outer;
  7437. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7438. outer := outer.outer;
  7439. END;
  7440. IF outer = NIL THEN
  7441. Error(exitStatement.position,"exit statement not within loop statement");
  7442. END;
  7443. END VisitExitStatement;
  7444. (** check and resolve return statement RETURN [expression]
  7445. - check expression (if any)
  7446. - check if in procedure scope
  7447. - if in procedure scope then check expression compatibility
  7448. - if not in procecdure scope then check on return without expression
  7449. **)
  7450. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7451. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7452. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7453. BEGIN
  7454. position := returnStatement.position;
  7455. expression := returnStatement.returnValue;
  7456. IF expression # NIL THEN
  7457. expression := ResolveExpression(expression);
  7458. returnStatement.SetReturnValue(expression);
  7459. END;
  7460. outer := returnStatement.outer;
  7461. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7462. outer := outer.outer
  7463. END;
  7464. IF (outer # NIL) THEN
  7465. scope := outer(SyntaxTree.Body).inScope;
  7466. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7467. IF (expression # NIL) THEN
  7468. Error(position, "return statement with parameter not in procedure scope");
  7469. END;
  7470. ELSE
  7471. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7472. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7473. Error(position, "return statement in procedure that does not return");
  7474. END;
  7475. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7476. IF returnType # NIL THEN
  7477. returnType := returnType.resolved;
  7478. IF expression = NIL THEN
  7479. Error(position, "empty return type in procedure providing a return type")
  7480. ELSIF expression.type = NIL THEN
  7481. Error(position,"returned type incompatible: expression has no type");
  7482. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7483. Error(position, "return type not compatible");
  7484. IF VerboseErrorMessage THEN
  7485. Printout.Info("returnType",returnType);
  7486. Printout.Info("expression",expression);
  7487. END;
  7488. ELSE
  7489. expression := NewConversion(expression.position,expression,returnType,NIL);
  7490. returnStatement.SetReturnValue(expression);
  7491. END;
  7492. ELSIF expression # NIL THEN
  7493. Error(position, "non-empty return type in procedure providing no return type");
  7494. END;
  7495. END;
  7496. END;
  7497. END VisitReturnStatement;
  7498. (** check and resolve await statement AWAIT(condition: Expression)
  7499. - check await condition
  7500. **)
  7501. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7502. VAR condition: SyntaxTree.Expression;
  7503. BEGIN
  7504. condition := ResolveCondition(awaitStatement.condition);
  7505. IF currentIsRealtime THEN
  7506. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7507. END;
  7508. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7509. Error(awaitStatement.position,"senseless await statement with constant condition");
  7510. END;
  7511. awaitStatement.SetCondition(condition);
  7512. END VisitAwaitStatement;
  7513. PROCEDURE CheckSystemImport(position: Position);
  7514. VAR import: SyntaxTree.Import;
  7515. BEGIN
  7516. import := currentScope.ownerModule.moduleScope.firstImport;
  7517. WHILE(import # NIL) DO
  7518. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7519. RETURN;
  7520. END;
  7521. import := import.nextImport;
  7522. END;
  7523. Error(position, "forbidden code without system import");
  7524. END CheckSystemImport;
  7525. (** check and resolve code statement: do nothing, must be done by assembler
  7526. **)
  7527. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7528. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7529. BEGIN
  7530. CheckSystemImport(code.position);
  7531. FOR i := 0 TO code.inRules.Length()-1 DO
  7532. statement := code.inRules.GetStatement(i);
  7533. IF statement IS SyntaxTree.Assignment THEN
  7534. WITH statement: SyntaxTree.Assignment DO
  7535. statement.SetRight(ResolveExpression(statement.right));
  7536. END;
  7537. ELSE
  7538. Error(statement.position, "can only be assignment")
  7539. END;
  7540. END;
  7541. FOR i := 0 TO code.outRules.Length()-1 DO
  7542. statement := code.outRules.GetStatement(i);
  7543. IF statement IS SyntaxTree.Assignment THEN
  7544. WITH statement: SyntaxTree.Assignment DO
  7545. statement.SetLeft(ResolveDesignator(statement.left));
  7546. END;
  7547. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7548. (* must be a reference to some register *)
  7549. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7550. ELSE
  7551. Printout.Info("out statement ", statement);
  7552. Error(statement.position, "(out) can only be assignment")
  7553. END;
  7554. END;
  7555. END VisitCode;
  7556. (** check and set flags of a statement block
  7557. - check for multiply occurence of a flag
  7558. - check and set priority only in bodies
  7559. - check for valid names
  7560. **)
  7561. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7562. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7563. flag: LONGINT; recordBody: SyntaxTree.Body;
  7564. PROCEDURE SetProtectedRecord;
  7565. VAR scope: SyntaxTree.Scope;
  7566. BEGIN
  7567. scope := currentScope;
  7568. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7569. scope := scope.outerScope
  7570. END;
  7571. IF scope # NIL THEN
  7572. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7573. END;
  7574. END SetProtectedRecord;
  7575. BEGIN
  7576. flags := {};
  7577. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7578. recordBody := block(SyntaxTree.Body)
  7579. ELSE
  7580. recordBody := NIL
  7581. END;
  7582. blockModifier := block.blockModifiers;
  7583. WHILE(blockModifier # NIL) DO
  7584. name := blockModifier.identifier;
  7585. expression := blockModifier.expression;
  7586. position := blockModifier.position;
  7587. flag := -1;
  7588. IF name=Global.NamePriority THEN
  7589. IF expression = NIL THEN
  7590. Error(position, "missing priority expression");
  7591. ELSIF recordBody = NIL THEN
  7592. Error(position, "priority not on record body");
  7593. ELSIF recordBody.priority # NIL THEN
  7594. Error(position, "duplicate priority expression");
  7595. ELSE
  7596. recordBody.SetPriority(expression);
  7597. END;
  7598. ELSIF expression # NIL THEN
  7599. Error(expression.position,"expression not in connection with priority")
  7600. ELSIF name=Global.NameExclusive THEN
  7601. IF block.isExclusive THEN
  7602. Error(position, "duplicate exclusive flag")
  7603. END;
  7604. block.SetExclusive(TRUE); SetProtectedRecord;
  7605. ELSIF name=Global.NameActive THEN
  7606. IF recordBody = NIL THEN
  7607. Error(position, "active not in record body");
  7608. ELSIF recordBody.isActive THEN
  7609. Error(position, "duplicate active flag")
  7610. ELSE
  7611. recordBody.SetActive(TRUE); SetProtectedRecord;
  7612. END;
  7613. ELSIF name=Global.NameSafe THEN
  7614. IF recordBody = NIL THEN
  7615. Error(position, "safe not in record body");
  7616. ELSIF recordBody.isSafe THEN
  7617. Error(position, "duplicate safe flag")
  7618. ELSE
  7619. recordBody.SetSafe(TRUE);
  7620. SetProtectedRecord;
  7621. END;
  7622. ELSIF name=Global.NameRealtime THEN
  7623. IF recordBody = NIL THEN
  7624. Error(position, "realtime not in record body");
  7625. ELSIF recordBody.isRealtime THEN
  7626. Error(position, "duplicate realtime flag")
  7627. ELSE
  7628. recordBody.SetRealtime(TRUE);
  7629. block.SetRealtime(TRUE);
  7630. END;
  7631. ELSIF name=Global.NameUnchecked THEN
  7632. IF block.isUnchecked THEN
  7633. Error(position, "duplicate unchecked flag")
  7634. ELSE
  7635. block.SetUnchecked(TRUE);
  7636. END;
  7637. ELSIF (name=Global.NameUncooperative) THEN
  7638. IF block.isUncooperative THEN
  7639. Error(position, "duplicate uncooperative flag")
  7640. ELSE
  7641. block.SetUncooperative(TRUE);
  7642. END;
  7643. ELSE
  7644. Error(position, "unknown block modifier");
  7645. END;
  7646. blockModifier := blockModifier.nextModifier;
  7647. END;
  7648. END BlockFlags;
  7649. (** check and resolve statement block
  7650. - check flags (exclusive)
  7651. - check statement sequence
  7652. **)
  7653. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7654. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7655. BEGIN
  7656. BlockFlags(statementBlock);
  7657. IF statementBlock.isExclusive THEN
  7658. (* check that not in exclusive block *)
  7659. IF currentIsExclusive THEN
  7660. Error (statementBlock.position,"forbidden recursive exclusive")
  7661. ELSIF currentIsRealtime THEN
  7662. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7663. END;
  7664. END;
  7665. recentExclusive := currentIsExclusive;
  7666. recentUnreachable := currentIsUnreachable;
  7667. recentRealtime := currentIsRealtime;
  7668. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7669. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7670. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7671. StatementSequence(statementBlock.statements);
  7672. currentIsRealtime := recentRealtime;
  7673. currentIsExclusive := recentExclusive;
  7674. currentIsUnreachable := recentUnreachable;
  7675. END VisitStatementBlock;
  7676. (** check and resolve body
  7677. - check flags (active, priority, safe)
  7678. - check body and finally part
  7679. **)
  7680. PROCEDURE Body(body: SyntaxTree.Body);
  7681. BEGIN
  7682. VisitStatementBlock(body);
  7683. IF body.isActive THEN
  7684. IF ~currentIsBodyProcedure THEN
  7685. Error(body.position,"active flag not in object body");
  7686. ELSIF body.priority # NIL THEN
  7687. body.SetPriority(ConstantInteger(body.priority));
  7688. END;
  7689. ELSIF body.isSafe THEN
  7690. Error(body.position,"safe flag not in active body");
  7691. ELSIF body.priority # NIL THEN
  7692. Error(body.position,"priority flag not in active body");
  7693. END;
  7694. IF body.code # NIL THEN
  7695. CheckSystemImport(body.position);
  7696. END;
  7697. StatementSequence(body.finally)
  7698. END Body;
  7699. (*** scopes ***)
  7700. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7701. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7702. VAR duplicateSymbol: BOOLEAN;
  7703. BEGIN
  7704. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7705. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7706. Error(symbol.position,"globally defined keyword")
  7707. END;
  7708. scope.EnterSymbol(symbol,duplicateSymbol);
  7709. IF ~allowDuplicate & duplicateSymbol THEN
  7710. Error(symbol.position,"Multiply defined identifier.");
  7711. IF VerboseErrorMessage THEN
  7712. Printout.Info("multiply defined identifier",symbol);
  7713. Printout.Info("in scope",scope);
  7714. END;
  7715. END;
  7716. END Register;
  7717. (**
  7718. implementation: check and resolve an implementation part
  7719. **)
  7720. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7721. move implementation checker to a separate object ? *)
  7722. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7723. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7724. BEGIN
  7725. prevIsRealtime := currentIsRealtime;
  7726. prevIsBodyProcedure := currentIsBodyProcedure;
  7727. prevIsCellNet := currentIsCellNet;
  7728. prevScope := currentScope;
  7729. currentScope := scope;
  7730. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7731. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7732. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7733. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7734. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7735. (*
  7736. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7737. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7738. END;
  7739. *)
  7740. END;
  7741. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7742. (* module body, record bodies are wrapped into an artifical procedure *)
  7743. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7744. Body(scope(SyntaxTree.ProcedureScope).body)
  7745. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7746. Body(scope(SyntaxTree.ProcedureScope).body)
  7747. END;
  7748. END;
  7749. currentScope := prevScope;
  7750. currentIsRealtime := prevIsRealtime;
  7751. currentIsBodyProcedure := prevIsBodyProcedure;
  7752. currentIsCellNet := prevIsCellNet;
  7753. END Implementation;
  7754. (** implementation phase:
  7755. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7756. **)
  7757. PROCEDURE Implementations(x: SyntaxTree.Module);
  7758. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7759. BEGIN
  7760. prevPhase := phase;
  7761. phase := InlinePhase;
  7762. scope := x.firstScope;
  7763. WHILE(scope # NIL) DO
  7764. Implementation(scope);
  7765. scope := scope.nextScope;
  7766. END;
  7767. phase := ImplementationPhase;
  7768. scope := x.firstScope;
  7769. WHILE(scope # NIL) DO
  7770. Implementation(scope);
  7771. scope := scope.nextScope;
  7772. END;
  7773. phase := prevPhase;
  7774. END Implementations;
  7775. (** declaration phase:
  7776. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7777. - import lists (for module scopes)
  7778. - parameter list (for procedure scopes)
  7779. - constant declarations
  7780. - type declarations
  7781. - variable declarations
  7782. - procedure declarations
  7783. preformed in two stages:
  7784. - first all symbols are entered into the symbol table (with uniqueness check),
  7785. - then all symbols are resolved
  7786. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7787. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7788. phases :
  7789. 0 = before procedures
  7790. 1 = procedures and later
  7791. **)
  7792. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7793. VAR
  7794. constant: SyntaxTree.Constant;
  7795. typeDeclaration: SyntaxTree.TypeDeclaration;
  7796. declaredType: SyntaxTree.Type;
  7797. variable: SyntaxTree.Variable;
  7798. procedure: SyntaxTree.Procedure;
  7799. procedureType : SyntaxTree.ProcedureType;
  7800. prevScope: SyntaxTree.Scope;
  7801. parameter: SyntaxTree.Parameter;
  7802. import: SyntaxTree.Import;
  7803. symbol: SyntaxTree.Symbol;
  7804. prevPhase: LONGINT;
  7805. prevError : BOOLEAN;
  7806. i: LONGINT;
  7807. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7808. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7809. BEGIN
  7810. IF type.baseType # NIL THEN
  7811. baseType := type.baseType.resolved;
  7812. IF baseType IS SyntaxTree.PointerType THEN
  7813. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7814. END;
  7815. (*
  7816. IF baseType IS SyntaxTree.CellType THEN
  7817. DeclareCell(baseType(SyntaxTree.CellType));
  7818. END;
  7819. *)
  7820. END;
  7821. parameter := type.firstParameter;
  7822. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7823. (*
  7824. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7825. variable.SetType(parameter.type);
  7826. variable.SetAccess(SyntaxTree.Hidden);
  7827. variable.SetModifiers(parameter.modifiers);
  7828. currentScope.PushVariable(variable);
  7829. *)
  7830. Register(parameter,scope, FALSE);
  7831. parameter := parameter.nextParameter;
  7832. END;
  7833. property := type.firstProperty;
  7834. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7835. (*
  7836. variable := currentScope.FindVariable(property.name);
  7837. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7838. prop := variable(SyntaxTree.Property);
  7839. ELSE (* add, duplicate symbols detection later *)
  7840. prop := SyntaxTree.NewProperty(property.position, property.name);
  7841. currentScope.PushVariable(prop);
  7842. END;
  7843. prop.SetType(property.type);
  7844. prop.SetValue(property.value);
  7845. prop.SetAccess(SyntaxTree.Hidden);
  7846. *)
  7847. Register(property, scope, FALSE);
  7848. property := property.nextProperty;
  7849. END;
  7850. END DeclareCell;
  7851. BEGIN
  7852. prevError := error;
  7853. prevPhase := phase;
  7854. phase := DeclarationPhase;
  7855. prevScope := currentScope;
  7856. currentScope := scope;
  7857. error := FALSE;
  7858. IF 0 IN phases THEN
  7859. (* first enter all symbols in scope *)
  7860. IF scope IS SyntaxTree.ModuleScope THEN
  7861. (* treat imports first for a module scope, , set default context if necessary *)
  7862. import := scope(SyntaxTree.ModuleScope).firstImport;
  7863. WHILE(import # NIL) DO
  7864. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7865. Register(import, currentScope, FALSE);
  7866. import := import.nextImport;
  7867. END;
  7868. import := scope(SyntaxTree.ModuleScope).firstImport;
  7869. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7870. ResolveSymbol(import);
  7871. import := import.nextImport;
  7872. END;
  7873. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7874. (* enter parameters for a procedure scope *)
  7875. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7876. parameter := procedureType.firstParameter;
  7877. WHILE(parameter # NIL) DO
  7878. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7879. END;
  7880. parameter := procedureType.returnParameter;
  7881. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7882. parameter := procedureType.selfParameter;
  7883. IF parameter # NIL THEN
  7884. Register(parameter, currentScope, FALSE);
  7885. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7886. END;
  7887. ELSIF scope IS SyntaxTree.CellScope THEN
  7888. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7889. IF~skipImplementation THEN
  7890. import := scope(SyntaxTree.CellScope).firstImport;
  7891. WHILE(import # NIL) DO
  7892. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7893. Register(import, currentScope, FALSE);
  7894. import := import.nextImport;
  7895. END;
  7896. import := scope(SyntaxTree.CellScope).firstImport;
  7897. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7898. ResolveSymbol(import);
  7899. import := import.nextImport;
  7900. END;
  7901. END;
  7902. END;
  7903. IF error THEN RETURN END;
  7904. IF skipImplementation THEN
  7905. scope.Clear;
  7906. END;
  7907. (* constants *)
  7908. constant := scope.firstConstant;
  7909. WHILE (constant # NIL) DO
  7910. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7911. END;
  7912. (* type declarations *)
  7913. typeDeclaration := scope.firstTypeDeclaration;
  7914. WHILE (typeDeclaration # NIL) DO
  7915. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7916. END;
  7917. (* variables *)
  7918. variable := scope.firstVariable;
  7919. WHILE (variable # NIL) DO
  7920. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7921. END;
  7922. (* procedures *)
  7923. IF scope.procedures # NIL THEN
  7924. FOR i := 0 TO scope.procedures.Length()-1 DO
  7925. procedure := scope.procedures.GetProcedure(i);
  7926. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7927. IF procedureType.selfParameter = NIL THEN
  7928. scope.AddProcedure(procedure);
  7929. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7930. ELSE
  7931. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7932. IF typeDeclaration = NIL THEN
  7933. Error(procedureType.selfParameter.position, "No such type declaration");
  7934. ELSE
  7935. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7936. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7937. declaredType := typeDeclaration.declaredType;
  7938. IF declaredType IS SyntaxTree.PointerType THEN
  7939. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7940. END;
  7941. IF declaredType IS SyntaxTree.RecordType THEN
  7942. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7943. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7944. ELSE
  7945. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7946. END;
  7947. END;
  7948. END;
  7949. END;
  7950. END;
  7951. END;
  7952. (* now process all symbols without any presumption on the order *)
  7953. symbol := scope.firstSymbol;
  7954. WHILE(symbol # NIL) DO
  7955. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7956. IF (symbol IS SyntaxTree.Procedure) THEN
  7957. IF 1 IN phases THEN
  7958. ResolveSymbol(symbol);
  7959. END;
  7960. ELSE
  7961. IF 0 IN phases THEN
  7962. ResolveSymbol(symbol);
  7963. END;
  7964. END;
  7965. END;
  7966. symbol := symbol.nextSymbol;
  7967. END;
  7968. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7969. symbol := scope.firstSymbol;
  7970. WHILE symbol # NIL DO
  7971. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7972. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7973. pointerFixes.Add(symbol, currentScope);
  7974. END;
  7975. IF ~symbol.type.resolved.isRealtime THEN
  7976. Error(symbol.position,"symbol has no realtime type");
  7977. END;
  7978. END;
  7979. symbol := symbol.nextSymbol
  7980. END;
  7981. END;
  7982. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7983. Error(Basic.invalidPosition,"problems during offset computation in module");
  7984. END;
  7985. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7986. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7987. scope.ownerModule.AddScope(scope);
  7988. END;
  7989. phase := prevPhase;
  7990. currentScope := prevScope;
  7991. error := error OR prevError;
  7992. END Declarations;
  7993. (* nopov *)
  7994. (** check if all operators from one module are compatible to the ones in the other module
  7995. - check if there are not multiple operators with the same signature
  7996. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7997. - check for all operators whose signatures are compatible, whether the return types are compatible
  7998. note that:
  7999. - the return type is not considered to be part of the signature
  8000. - two signatures are considered compatible, if all of the operands are compatible
  8001. **)
  8002. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8003. VAR
  8004. thisOperator, thatOperator: SyntaxTree.Operator;
  8005. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8006. thisParameter, thatParameter: SyntaxTree.Parameter;
  8007. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8008. i: LONGINT;
  8009. BEGIN
  8010. currentScope := thisModuleScope;
  8011. hasError := FALSE;
  8012. (* go through all operators in the other module *)
  8013. thatOperator := thatModuleScope.firstOperator;
  8014. WHILE (thatOperator # NIL) & ~hasError DO
  8015. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8016. (* the other operator is accessible *)
  8017. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8018. (* the other operator is not the conversion operator *)
  8019. (* go through all operators in this module *)
  8020. thisOperator := thisModuleScope.firstOperator;
  8021. WHILE (thisOperator # NIL) & ~hasError DO
  8022. IF thisOperator # thatOperator THEN
  8023. (* the operators are not the same *)
  8024. IF thisOperator.name = thatOperator.name THEN
  8025. (* the operators share the same identifier *)
  8026. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8027. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8028. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8029. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8030. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8031. (* both operators have the same paramter count *)
  8032. thisParameter := thisProcedureType.firstParameter;
  8033. thatParameter := thatProcedureType.firstParameter;
  8034. operandsAreEqual := TRUE;
  8035. operandsAreCompatible := TRUE;
  8036. (* go through all parameters *)
  8037. FOR i := 1 TO thisProcedureType.numberParameters DO
  8038. ASSERT(thatParameter # NIL);
  8039. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8040. operandsAreEqual := FALSE;
  8041. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  8042. operandsAreCompatible := FALSE
  8043. END
  8044. END;
  8045. thisParameter := thisParameter.nextParameter;
  8046. thatParameter := thatParameter.nextParameter
  8047. END;
  8048. IF operandsAreEqual THEN
  8049. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8050. hasError := TRUE
  8051. ELSIF operandsAreCompatible THEN
  8052. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8053. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8054. hasError := TRUE
  8055. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8056. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8057. hasError := TRUE
  8058. END
  8059. END
  8060. END
  8061. END
  8062. END;
  8063. thisOperator := thisOperator.nextOperator
  8064. END
  8065. END
  8066. END;
  8067. thatOperator := thatOperator.nextOperator
  8068. END
  8069. END CheckInterOperatorConformity;
  8070. (** check module:
  8071. - check module declaration
  8072. - add context, if necessary
  8073. - remove module from import cache, if necessary
  8074. - check declarations
  8075. - resolve all type fixes
  8076. - check implementation (bodies)
  8077. **)
  8078. PROCEDURE Module*(x: SyntaxTree.Module);
  8079. VAR (* nopov *)
  8080. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8081. BEGIN
  8082. prevScope := currentScope;
  8083. prevIsCellNet := currentIsCellNet;
  8084. module := x;
  8085. ASSERT(x # NIL);
  8086. global := system.globalScope[x.case];
  8087. x.moduleScope.SetGlobalScope(global);
  8088. currentScope := global;
  8089. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8090. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8091. RemoveModuleFromCache(importCache,x);
  8092. Declarations(x.moduleScope, FALSE, {0,1});
  8093. FixTypes();
  8094. IF module.isCellNet THEN
  8095. currentIsCellNet := TRUE;
  8096. modifier := x.modifiers;
  8097. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8098. CheckModifiers(modifier, FALSE);
  8099. END;
  8100. (* nopov *)
  8101. IF ~error THEN
  8102. (* check if operators conform to each other within this module *)
  8103. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8104. (* go through all imports *)
  8105. import := x.moduleScope.firstImport;
  8106. WHILE import # NIL DO
  8107. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8108. (* check if all operators in this module conform to the ones of the imported module *)
  8109. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8110. END;
  8111. import := import.nextImport
  8112. END;
  8113. END;
  8114. Implementations(x);
  8115. module := NIL;
  8116. currentIsCellNet := prevIsCellNet;
  8117. currentScope := prevScope;
  8118. END Module;
  8119. END Checker;
  8120. Warnings*=OBJECT (SyntaxTree.Visitor)
  8121. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8122. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8123. BEGIN
  8124. SELF.diagnostics := diagnostics
  8125. END InitWarnings;
  8126. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8127. BEGIN END VisitPortType;
  8128. (** types *)
  8129. PROCEDURE Type(x: SyntaxTree.Type);
  8130. BEGIN x.Accept(SELF)
  8131. END Type;
  8132. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8133. BEGIN END VisitType;
  8134. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8135. BEGIN END VisitBasicType;
  8136. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8137. BEGIN END VisitCharacterType;
  8138. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8139. BEGIN END VisitIntegerType;
  8140. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8141. BEGIN END VisitFloatType;
  8142. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8143. BEGIN END VisitQualifiedType;
  8144. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8145. BEGIN END VisitStringType;
  8146. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8147. BEGIN END VisitEnumerationType;
  8148. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8149. BEGIN END VisitRangeType;
  8150. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8151. BEGIN
  8152. IF ~(SyntaxTree.Warned IN x.state) THEN
  8153. x.SetState(SyntaxTree.Warned);
  8154. Type(x.arrayBase);
  8155. END;
  8156. END VisitArrayType;
  8157. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8158. BEGIN
  8159. IF ~(SyntaxTree.Warned IN x.state) THEN
  8160. x.SetState(SyntaxTree.Warned);
  8161. Type(x.arrayBase);
  8162. END;
  8163. END VisitMathArrayType;
  8164. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8165. BEGIN
  8166. IF ~(SyntaxTree.Warned IN x.state) THEN
  8167. x.SetState(SyntaxTree.Warned);
  8168. Type(x.pointerBase);
  8169. END;
  8170. END VisitPointerType;
  8171. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8172. BEGIN Scope(x.recordScope) END VisitRecordType;
  8173. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8174. BEGIN Scope(x.cellScope) END VisitCellType;
  8175. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8176. BEGIN END VisitProcedureType;
  8177. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8178. VAR msg: ARRAY 256 OF CHAR;
  8179. BEGIN
  8180. Global.GetSymbolName(x,msg);
  8181. Strings.Append(msg," ");
  8182. Strings.Append(msg,text);
  8183. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8184. END Warning;
  8185. (** symbols *)
  8186. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8187. BEGIN
  8188. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8189. IF ~(x IS SyntaxTree.Parameter) THEN
  8190. Warning(x,"never used");
  8191. END;
  8192. END;
  8193. x.Accept(SELF);
  8194. END Symbol;
  8195. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8196. BEGIN END VisitSymbol;
  8197. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8198. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8199. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8200. BEGIN END VisitConstant;
  8201. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8202. BEGIN END VisitVariable;
  8203. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8204. BEGIN END VisitProperty;
  8205. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8206. BEGIN END VisitParameter;
  8207. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8208. BEGIN
  8209. Scope(x.procedureScope)
  8210. END VisitProcedure;
  8211. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8212. BEGIN END VisitOperator;
  8213. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8214. BEGIN END VisitImport;
  8215. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8216. VAR
  8217. symbol: SyntaxTree.Symbol;
  8218. BEGIN
  8219. symbol := scope.firstSymbol;
  8220. WHILE(symbol # NIL) DO
  8221. Symbol(symbol);
  8222. symbol := symbol.nextSymbol;
  8223. END;
  8224. END Scope;
  8225. PROCEDURE Module*(x: SyntaxTree.Module);
  8226. BEGIN
  8227. SELF.module := x;
  8228. Scope(x.moduleScope);
  8229. END Module;
  8230. END Warnings;
  8231. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8232. BEGIN
  8233. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8234. END IsOberonInline;
  8235. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8236. BEGIN
  8237. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8238. END Resolved;
  8239. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8240. VAR i: LONGINT;
  8241. BEGIN
  8242. i := 1;
  8243. WHILE i < x DO
  8244. i := i *2
  8245. END;
  8246. RETURN i=x
  8247. END PowerOf2;
  8248. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8249. BEGIN
  8250. RETURN
  8251. (scope # NIL) &
  8252. (scope IS SyntaxTree.ModuleScope)
  8253. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8254. OR
  8255. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8256. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8257. END IsCellNetScope;
  8258. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8259. BEGIN
  8260. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8261. END IsCellScope;
  8262. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8263. BEGIN
  8264. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8265. RETURN (scope # NIL) & IsCellNetScope(scope)
  8266. END InCellNetScope;
  8267. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8268. BEGIN
  8269. ASSERT(size MOD system.dataUnit = 0);
  8270. RETURN size DIV system.dataUnit
  8271. END ToMemoryUnits;
  8272. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8273. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8274. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8275. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8276. BEGIN
  8277. IF t = NIL THEN
  8278. RETURN TRUE
  8279. ELSE
  8280. t := t.resolved;
  8281. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8282. END;
  8283. END TypeAllowed;
  8284. BEGIN
  8285. type := type.resolved;
  8286. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8287. RETURN FALSE
  8288. ELSE
  8289. procedureType := type(SyntaxTree.ProcedureType);
  8290. numberParameters := procedureType.numberParameters;
  8291. RETURN
  8292. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8293. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8294. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8295. END;
  8296. END GetProcedureAllowed;
  8297. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8298. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8299. VAR import: SyntaxTree.Import;
  8300. BEGIN
  8301. import := importCache.ImportByModuleName(x.name,x.context);
  8302. IF import # NIL THEN
  8303. importCache.RemoveImporters(x.name,x.context);
  8304. END;
  8305. END RemoveModuleFromCache;
  8306. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8307. (* to <- this assignment compatibility *)
  8308. VAR result: BOOLEAN;
  8309. BEGIN
  8310. IF this= NIL THEN result := (to=NIL)
  8311. ELSIF to=NIL THEN result := FALSE
  8312. ELSE
  8313. (*! will be replaced by this:
  8314. ELSE result := this.CompatibleTo(to.resolved);
  8315. *)
  8316. this := this.resolved; to := to.resolved;
  8317. IF to=SyntaxTree.invalidType THEN result := FALSE
  8318. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8319. ELSIF (to = this) OR (to.SameType(this)) THEN
  8320. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8321. ELSIF to IS SyntaxTree.BasicType THEN
  8322. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8323. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8324. result := this.CompatibleTo(to.resolved)
  8325. ELSE
  8326. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8327. END
  8328. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8329. result := to.sizeInBits >= this.sizeInBits;
  8330. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8331. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8332. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8333. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8334. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8335. result := TRUE;
  8336. ELSIF to IS SyntaxTree.AnyType THEN
  8337. 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);
  8338. ELSIF to IS SyntaxTree.ObjectType THEN
  8339. 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 *) ;
  8340. ELSIF to IS SyntaxTree.ByteType THEN
  8341. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8342. ELSIF to IS SyntaxTree.CharacterType THEN
  8343. result := IsCharacterType(this)
  8344. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8345. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8346. 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
  8347. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8348. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8349. result := TRUE;
  8350. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8351. result := TRUE;
  8352. ELSE
  8353. result := FALSE
  8354. END;
  8355. ELSIF to IS SyntaxTree.PointerType THEN
  8356. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8357. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8358. & (~to.isRealtime OR this.isRealtime);
  8359. ELSIF to IS SyntaxTree.ProcedureType THEN
  8360. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8361. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8362. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8363. ELSIF to IS SyntaxTree.RecordType THEN
  8364. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8365. ELSIF to IS SyntaxTree.ArrayType THEN
  8366. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8367. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8368. ELSIF StaticArrayCompatible(to, this) THEN
  8369. result := TRUE
  8370. ELSE
  8371. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8372. END;
  8373. ELSIF to IS SyntaxTree.MathArrayType THEN
  8374. IF this IS SyntaxTree.MathArrayType THEN
  8375. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8376. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8377. result := TRUE;
  8378. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8379. result := TRUE;
  8380. ELSE
  8381. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8382. END;
  8383. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8384. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8385. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8386. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8387. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8388. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8389. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8390. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8391. ELSE
  8392. result := FALSE
  8393. END;
  8394. (* an array-structured object type is compatible to the type of its array structure *)
  8395. ELSIF IsArrayStructuredObjectType(this) THEN
  8396. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8397. ELSE
  8398. result := FALSE;
  8399. END;
  8400. ELSIF to IS SyntaxTree.StringType THEN
  8401. result := FALSE;
  8402. ELSIF to IS SyntaxTree.EnumerationType THEN
  8403. result := IsEnumerationExtension(this,to);
  8404. ELSIF to IS SyntaxTree.PortType THEN
  8405. result := SameType(to, this)
  8406. ELSE
  8407. Printout.Info("CompatibleTo",to);
  8408. HALT(100); (* implement missing type check *)
  8409. END;
  8410. END;
  8411. RETURN result
  8412. END CompatibleTo;
  8413. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8414. VAR actualBase, formalBase: SyntaxTree.Type;
  8415. BEGIN
  8416. IF SameType(formal,actual) THEN
  8417. RETURN TRUE
  8418. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8419. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8420. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8421. RETURN
  8422. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8423. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8424. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8425. & StaticArrayCompatible(formalBase,actualBase)
  8426. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8427. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8428. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8429. RETURN
  8430. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8431. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8432. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8433. & StaticArrayCompatible(formalBase,actualBase)
  8434. ELSE RETURN FALSE
  8435. END;
  8436. END StaticArrayCompatible;
  8437. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8438. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8439. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8440. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8441. BEGIN
  8442. result := SameType(formal,actual);
  8443. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8444. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8445. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8446. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8447. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8448. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8449. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8450. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8451. & TC(formalBase, actualBase);
  8452. END;
  8453. RETURN result
  8454. END TC;
  8455. BEGIN
  8456. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8457. ELSE
  8458. arrayBase := formalType.arrayBase.resolved;
  8459. IF (actualType IS SyntaxTree.StringType) THEN
  8460. result := arrayBase IS SyntaxTree.CharacterType
  8461. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8462. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8463. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8464. result := TC(formalType, actualType);
  8465. ELSE
  8466. result := (arrayBase IS SyntaxTree.ByteType)
  8467. END;
  8468. END;
  8469. RETURN result
  8470. END OpenArrayCompatible;
  8471. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8472. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8473. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8474. BEGIN
  8475. IF actualType IS SyntaxTree.MathArrayType THEN
  8476. actualArray := actualType(SyntaxTree.MathArrayType);
  8477. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8478. (*
  8479. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8480. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8481. *)
  8482. actualBase := ArrayBase(actualType,Infinity);
  8483. formalBase := ArrayBase(formalType,Infinity);
  8484. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8485. ELSE
  8486. (*
  8487. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8488. *)
  8489. formalBase := Resolved(formalType.arrayBase);
  8490. actualBase := Resolved(actualArray.arrayBase);
  8491. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8492. (*
  8493. ARRAY [k] -> ARRAY [n]
  8494. *)
  8495. result := (formalType.staticLength = actualArray.staticLength)
  8496. ELSE
  8497. result := TRUE
  8498. END;
  8499. IF ~result THEN
  8500. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8501. ELSIF actualBase = NIL THEN result := FALSE
  8502. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8503. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8504. ELSE
  8505. result := SameType(formalBase,actualBase)
  8506. END;
  8507. END;
  8508. ELSE
  8509. result := FALSE
  8510. END;
  8511. RETURN result
  8512. END MathArrayCompatible;
  8513. (**
  8514. Math Array Type distance for assignments / parameter passings of the form
  8515. from -> to
  8516. variants:
  8517. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8518. allowed:
  8519. static -> static (& size match)
  8520. static -> open
  8521. static -> tensor
  8522. open -> open
  8523. open -> tensor
  8524. open -> static
  8525. tensor -> tensor
  8526. tensor -> open
  8527. tensor -> static
  8528. **)
  8529. (*! think about the metric here: is form matching more important than element type matching? *)
  8530. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8531. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8532. BEGIN
  8533. fromBase := Resolved(from.arrayBase);
  8534. toBase := Resolved(to.arrayBase);
  8535. i := Infinity;
  8536. IF (from = to) OR (from.SameType(to)) THEN
  8537. i := 0;
  8538. ELSIF (from.form = to.form) THEN
  8539. (* static -> static, open -> open, tensor -> tensor *)
  8540. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8541. IF fromBase = toBase THEN i := 0
  8542. ELSIF toBase = NIL THEN i := 1
  8543. ELSIF toBase.SameType(fromBase) THEN i := 0
  8544. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8545. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8546. ELSE
  8547. i := TypeDistance(system,fromBase, toBase, varpar);
  8548. IF i < Infinity THEN i := i * 5 END;
  8549. END;
  8550. END;
  8551. ELSIF (to.form = SyntaxTree.Static) THEN
  8552. (* forbidden *)
  8553. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8554. (* static -> tensor, open -> tensor, tensor -> open *)
  8555. IF (toBase=fromBase) THEN i := 0;
  8556. ELSIF toBase = NIL THEN i := 1;
  8557. ELSIF toBase.SameType(fromBase) THEN i := 0
  8558. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8559. toBase := ArrayBase(toBase,Infinity);
  8560. IF (fromBase=toBase) THEN i := 0
  8561. ELSIF (toBase = NIL) THEN i:= 1
  8562. ELSIF (fromBase = NIL) THEN i := Infinity;
  8563. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8564. IF i < Infinity THEN i := i * 5 END;
  8565. END;
  8566. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8567. fromBase := ArrayBase(fromBase,Infinity);
  8568. IF (fromBase=toBase) THEN i := 0
  8569. ELSIF (toBase = NIL) THEN i := 1
  8570. ELSIF (fromBase = NIL) THEN i := Infinity;
  8571. ELSIF toBase.SameType(fromBase) THEN i := 0
  8572. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8573. IF i < Infinity THEN i := i * 5 END;
  8574. END;
  8575. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8576. IF i < Infinity THEN i := i * 5 END;
  8577. END;
  8578. IF i # Infinity THEN INC(i,2) END;
  8579. ELSIF (from.form = SyntaxTree.Static) THEN
  8580. (* static -> open *)
  8581. IF (toBase=fromBase) THEN i := 0;
  8582. ELSIF toBase = NIL THEN i := 1
  8583. ELSIF fromBase = NIL THEN i := Infinity
  8584. ELSIF toBase.SameType(fromBase) THEN i := 0
  8585. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8586. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8587. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8588. IF i < Infinity THEN i := i * 5 END;
  8589. END;
  8590. IF i # Infinity THEN INC(i,1) END;
  8591. ELSE HALT(100); (* unknown case *)
  8592. END;
  8593. RETURN i;
  8594. END MathArrayTypeDistance;
  8595. (** compute and return the distance of two array types
  8596. - return the distance of the base types
  8597. **)
  8598. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8599. VAR i: LONGINT;
  8600. BEGIN
  8601. i := Infinity;
  8602. IF from = to THEN
  8603. i := 0
  8604. ELSE
  8605. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8606. (*
  8607. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8608. i := TypeDistance(from.base, to.base);
  8609. IF i >= 0 THEN INC(i) END
  8610. ELSIF (from.mode = open) & (to.mode = open) THEN
  8611. i := TypeDistance(from.base, to.base);
  8612. *)
  8613. END;
  8614. RETURN i
  8615. END ArrayTypeDistance;
  8616. (** compute the signature distance of a procedure and an actual parameter list
  8617. - if any of the parameters are not compatible, the result is infinite
  8618. - add up and return the distance over all parameters
  8619. **)
  8620. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8621. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8622. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8623. BEGIN
  8624. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8625. result := Infinity
  8626. ELSE
  8627. formalParameter := procedureType.firstParameter;
  8628. i := 0;
  8629. result := 0;
  8630. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8631. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8632. actualParameter := actualParameters.GetExpression(i);
  8633. ASSERT(formalParameter.type # NIL);
  8634. IF (actualParameter.type = NIL) THEN distance := Infinity
  8635. ELSE
  8636. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8637. END;
  8638. IF distance = Infinity THEN
  8639. result := Infinity;
  8640. ELSE
  8641. to := formalParameter.type.resolved;
  8642. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8643. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8644. (* already handled varpar *)
  8645. (*
  8646. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8647. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8648. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8649. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8650. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8651. END;
  8652. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8653. result := Infinity
  8654. END;
  8655. *)
  8656. INC(result, distance);
  8657. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8658. INC(result, distance);
  8659. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8660. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8661. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8662. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8663. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8664. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8665. END;
  8666. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8667. result := Infinity
  8668. END;
  8669. ELSE
  8670. result := Infinity
  8671. END;
  8672. ELSE
  8673. INC(result,distance);
  8674. END;
  8675. END;
  8676. (*
  8677. Printout.Info("actual=", actualParameter);
  8678. Printout.Info("formal=", formalParameter);
  8679. TRACE(result);
  8680. *)
  8681. formalParameter := formalParameter.nextParameter; INC(i);
  8682. END;
  8683. END;
  8684. ASSERT(result >= 0);
  8685. RETURN result
  8686. END Distance;
  8687. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8688. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8689. BEGIN
  8690. IF right.numberParameters # (procedureType.numberParameters) THEN
  8691. result := Infinity
  8692. ELSE
  8693. formalParameter := procedureType.firstParameter;
  8694. rightParameter := right.firstParameter;
  8695. i := 0;
  8696. result := 0;
  8697. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8698. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8699. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8700. IF distance = Infinity THEN
  8701. result := Infinity;
  8702. ELSE
  8703. INC(result,distance);
  8704. END;
  8705. formalParameter := formalParameter.nextParameter;
  8706. rightParameter := rightParameter.nextParameter;
  8707. END;
  8708. END;
  8709. ASSERT(result >= 0);
  8710. RETURN result
  8711. END ProcedureTypeDistance;
  8712. (** compute and return the distance between two types, used for computation of signature distance
  8713. from -> to
  8714. **)
  8715. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8716. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8717. BEGIN
  8718. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8719. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8720. END;
  8721. i := Infinity;
  8722. IF from = to THEN
  8723. i := 0
  8724. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8725. ELSIF to.SameType(from) THEN
  8726. i := 0;
  8727. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8728. i := Infinity;
  8729. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8730. i := 10;
  8731. ELSIF (from IS SyntaxTree.StringType) THEN
  8732. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8733. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8734. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8735. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8736. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8737. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8738. i := 1
  8739. ELSIF (from IS SyntaxTree.NilType) THEN
  8740. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8741. (*
  8742. ELSIF (from = NoType) THEN
  8743. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8744. *)
  8745. ELSIF (from IS SyntaxTree.BasicType) THEN
  8746. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8747. IF varpar & (i # 0) THEN i := Infinity END;
  8748. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8749. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8750. ELSIF (from IS SyntaxTree.RecordType) THEN
  8751. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8752. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8753. IF to IS SyntaxTree.MathArrayType THEN
  8754. (*
  8755. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8756. i := Infinity;
  8757. ELSE
  8758. *)
  8759. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8760. (*
  8761. END;
  8762. *)
  8763. END
  8764. ELSIF (from IS SyntaxTree.PointerType) THEN
  8765. ptr := from(SyntaxTree.PointerType);
  8766. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8767. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8768. (* ELSE i := TypeDistance(ptr.base, to); *)
  8769. END
  8770. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8771. IF (to IS SyntaxTree.ProcedureType) THEN
  8772. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8773. END;
  8774. ELSIF (from IS SyntaxTree.PortType) THEN
  8775. IF (to IS SyntaxTree.PortType) THEN
  8776. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8777. i := 0;
  8778. END;
  8779. END;
  8780. (*no procedure test, procedure must be the same*)
  8781. END;
  8782. RETURN i
  8783. END TypeDistance;
  8784. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8785. BEGIN
  8786. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8787. END IsIntegerType;
  8788. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8789. BEGIN
  8790. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8791. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8792. OR IsPointerType(type)
  8793. )
  8794. END IsAddressType;
  8795. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8796. BEGIN
  8797. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8798. END IsSizeType;
  8799. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8800. BEGIN
  8801. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8802. END IsSignedIntegerType;
  8803. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8804. BEGIN
  8805. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8806. END IsUnsignedIntegerType;
  8807. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8808. VAR result: BOOLEAN;
  8809. BEGIN
  8810. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8811. value := x.resolved(SyntaxTree.IntegerValue).value;
  8812. result := TRUE
  8813. ELSE
  8814. result := FALSE
  8815. END;
  8816. RETURN result
  8817. END IsIntegerValue;
  8818. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8819. VAR result: BOOLEAN;
  8820. BEGIN
  8821. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8822. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8823. result := TRUE
  8824. ELSE
  8825. result := FALSE
  8826. END;
  8827. RETURN result
  8828. END IsEnumerationValue;
  8829. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8830. VAR result: BOOLEAN;
  8831. BEGIN
  8832. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8833. value := x.resolved(SyntaxTree.RealValue).value;
  8834. result := TRUE
  8835. ELSE
  8836. result := FALSE
  8837. END;
  8838. RETURN result
  8839. END IsRealValue;
  8840. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8841. VAR result: BOOLEAN;
  8842. BEGIN
  8843. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8844. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8845. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8846. result := TRUE
  8847. ELSE
  8848. result := FALSE
  8849. END;
  8850. RETURN result
  8851. END IsComplexValue;
  8852. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8853. VAR result: BOOLEAN;
  8854. BEGIN
  8855. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8856. value := x.resolved(SyntaxTree.CharacterValue).value;
  8857. result := TRUE
  8858. ELSE
  8859. result := FALSE
  8860. END;
  8861. RETURN result
  8862. END IsCharacterValue;
  8863. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8864. VAR result: BOOLEAN;
  8865. BEGIN
  8866. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8867. value := x.resolved(SyntaxTree.BooleanValue).value;
  8868. result := TRUE
  8869. ELSE
  8870. result := FALSE
  8871. END;
  8872. RETURN result
  8873. END IsBooleanValue;
  8874. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8875. VAR result: BOOLEAN;
  8876. BEGIN
  8877. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8878. value := x.resolved(SyntaxTree.SetValue).value;
  8879. result := TRUE
  8880. ELSE
  8881. result := FALSE
  8882. END;
  8883. RETURN result
  8884. END IsSetValue;
  8885. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8886. VAR result: BOOLEAN;
  8887. BEGIN
  8888. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8889. value := x.resolved(SyntaxTree.StringValue).value;
  8890. result := TRUE
  8891. ELSE
  8892. result := FALSE
  8893. END;
  8894. RETURN result
  8895. END IsStringValue;
  8896. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8897. BEGIN
  8898. x := x.resolved;
  8899. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8900. END Indexable;
  8901. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8902. BEGIN
  8903. RETURN t1.SameType(t2.resolved);
  8904. END SameType;
  8905. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8906. BEGIN
  8907. IF t IS SyntaxTree.MathArrayType THEN
  8908. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8909. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8910. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8911. DEC(max);
  8912. END;
  8913. ELSIF t IS SyntaxTree.ArrayType THEN
  8914. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8915. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8916. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8917. END;
  8918. END;
  8919. RETURN t;
  8920. END ArrayBase;
  8921. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8922. BEGIN
  8923. type := type.resolved;
  8924. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8925. base := type(SyntaxTree.ArrayType).arrayBase;
  8926. RETURN TRUE;
  8927. END;
  8928. RETURN FALSE;
  8929. END IsOpenArray;
  8930. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8931. BEGIN
  8932. type := type.resolved;
  8933. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8934. base := type(SyntaxTree.ArrayType).arrayBase;
  8935. dim := type(SyntaxTree.ArrayType).staticLength;
  8936. RETURN TRUE
  8937. ELSE
  8938. RETURN FALSE
  8939. END;
  8940. END IsStaticArray;
  8941. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8942. BEGIN
  8943. type := type.resolved;
  8944. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8945. base := type(SyntaxTree.ArrayType).arrayBase;
  8946. RETURN TRUE
  8947. ELSE
  8948. RETURN FALSE
  8949. END;
  8950. END IsDynamicArray;
  8951. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8952. VAR i: LONGINT;
  8953. BEGIN
  8954. i := 0;
  8955. t := t.resolved;
  8956. IF t IS SyntaxTree.MathArrayType THEN
  8957. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8958. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8959. END;
  8960. ELSIF t IS SyntaxTree.ArrayType THEN
  8961. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8962. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8963. END;
  8964. END;
  8965. RETURN i
  8966. END Dimension;
  8967. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8968. BEGIN
  8969. RETURN expression.assignable;
  8970. END IsVariable;
  8971. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8972. BEGIN
  8973. IF (symbol IS SyntaxTree.Parameter) THEN
  8974. WITH symbol: SyntaxTree.Parameter DO
  8975. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8976. END;
  8977. ELSE
  8978. RETURN FALSE
  8979. END;
  8980. END IsVariableParameter;
  8981. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8982. VAR result: BOOLEAN;
  8983. BEGIN
  8984. IF type = NIL THEN result := FALSE
  8985. ELSE
  8986. type := type.resolved;
  8987. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8988. END;
  8989. RETURN result
  8990. END IsPointerType;
  8991. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8992. VAR result: BOOLEAN;
  8993. BEGIN
  8994. IF type = NIL THEN result := FALSE
  8995. ELSE
  8996. type := type.resolved;
  8997. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8998. END;
  8999. RETURN result
  9000. END IsUnsafePointer;
  9001. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9002. BEGIN
  9003. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9004. END IsDisposable;
  9005. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9006. VAR result: BOOLEAN;
  9007. BEGIN
  9008. IF type = NIL THEN result := FALSE
  9009. ELSE
  9010. type := type.resolved;
  9011. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9012. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9013. result := result OR (type IS SyntaxTree.ObjectType);
  9014. END;
  9015. RETURN result
  9016. END IsPointerToRecord;
  9017. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9018. VAR result: BOOLEAN;
  9019. BEGIN
  9020. IF type = NIL THEN result := FALSE
  9021. ELSE
  9022. type := type.resolved;
  9023. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9024. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9025. ;
  9026. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9027. result := result OR (type IS SyntaxTree.ObjectType);
  9028. END;
  9029. RETURN result
  9030. END IsPointerToObject;
  9031. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9032. BEGIN
  9033. IF type # NIL THEN
  9034. RETURN type.resolved.hasPointers
  9035. ELSE
  9036. RETURN FALSE
  9037. END;
  9038. END ContainsPointer;
  9039. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9040. BEGIN
  9041. IF type = NIL THEN RETURN FALSE END;
  9042. type := type.resolved;
  9043. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9044. END IsStringType;
  9045. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9046. BEGIN
  9047. IF type = NIL THEN RETURN FALSE END;
  9048. type := type.resolved;
  9049. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9050. END IsCharacterType;
  9051. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9052. BEGIN
  9053. IF type = NIL THEN RETURN FALSE END;
  9054. type := type.resolved;
  9055. RETURN (type IS SyntaxTree.EnumerationType)
  9056. END IsEnumerationType;
  9057. (** cf. section "Type extension (base type)" in the language report **)
  9058. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9059. VAR result: BOOLEAN;
  9060. BEGIN
  9061. ASSERT(base # NIL); ASSERT(extension # NIL);
  9062. base := base.resolved; extension := extension.resolved;
  9063. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9064. result := TRUE;
  9065. ELSE
  9066. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9067. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9068. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9069. END;
  9070. WHILE (extension # NIL) & (extension # base) DO
  9071. IF extension IS SyntaxTree.RecordType THEN
  9072. extension := extension(SyntaxTree.RecordType).baseType;
  9073. IF (extension # NIL) THEN extension := extension.resolved END;
  9074. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9075. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9076. END;
  9077. ELSE extension := NIL;
  9078. END;
  9079. END;
  9080. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9081. END;
  9082. RETURN result
  9083. END IsTypeExtension;
  9084. (** check if base is the base enumeration type of extension **)
  9085. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9086. BEGIN
  9087. base := base.resolved; extension := extension.resolved;
  9088. WHILE (extension # NIL) & (extension # base) DO
  9089. IF extension IS SyntaxTree.EnumerationType THEN
  9090. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9091. IF extension # NIL THEN extension := extension.resolved END;
  9092. ELSE
  9093. extension := NIL
  9094. END;
  9095. END;
  9096. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9097. END IsEnumerationExtension;
  9098. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9099. BEGIN
  9100. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9101. RETURN TRUE
  9102. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9103. RETURN TRUE
  9104. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9105. RETURN TRUE
  9106. ELSE
  9107. RETURN FALSE
  9108. END
  9109. END IsCallable;
  9110. (** compute and return the distance of two record types
  9111. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9112. **)
  9113. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9114. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9115. BEGIN
  9116. i := 0;
  9117. WHILE (from # NIL) & (from # to) DO
  9118. baseType := from.baseType;
  9119. IF (baseType # NIL) THEN
  9120. baseType := baseType.resolved;
  9121. IF baseType IS SyntaxTree.PointerType THEN
  9122. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9123. END;
  9124. IF baseType IS SyntaxTree.RecordType THEN
  9125. from := baseType(SyntaxTree.RecordType);
  9126. ELSE
  9127. from := NIL;
  9128. END;
  9129. ELSE
  9130. from := NIL
  9131. END;
  9132. INC(i)
  9133. END;
  9134. IF from = NIL THEN i := Infinity END;
  9135. RETURN i
  9136. END RecordTypeDistance;
  9137. (** compute and return the distance of two pointer types **)
  9138. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9139. BEGIN
  9140. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9141. RETURN Infinity;
  9142. ELSE
  9143. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9144. END;
  9145. END PointerTypeDistance;
  9146. (** check if expression contains a symbol designator pointing to a type declaration.
  9147. - if so then enter type declaration into typeDeclaration and return true else return false
  9148. **)
  9149. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9150. VAR result: BOOLEAN;
  9151. BEGIN
  9152. result := FALSE;
  9153. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9154. result := TRUE;
  9155. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9156. END;
  9157. RETURN result
  9158. END IsTypeDesignator;
  9159. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9160. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9161. VAR result: BOOLEAN;
  9162. BEGIN
  9163. type := type.resolved;
  9164. IF type IS SyntaxTree.PointerType THEN
  9165. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9166. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9167. result := TRUE
  9168. ELSE
  9169. result := type IS SyntaxTree.RecordType
  9170. END;
  9171. RETURN result
  9172. END IsExtensibleType;
  9173. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9174. BEGIN
  9175. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9176. (d IS SyntaxTree.SymbolDesignator) &
  9177. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9178. OR
  9179. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9180. END IsUnextensibleRecord;
  9181. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9182. BEGIN
  9183. IF IsUnextensibleRecord(d) THEN
  9184. RETURN FALSE
  9185. ELSE RETURN IsExtensibleType(d.type.resolved)
  9186. END;
  9187. END IsExtensibleDesignator;
  9188. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9189. BEGIN
  9190. type := type.resolved;
  9191. IF (type IS SyntaxTree.PointerType) THEN
  9192. RETURN TRUE
  9193. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9194. RETURN TRUE
  9195. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9196. RETURN TRUE
  9197. ELSIF (type IS SyntaxTree.BasicType) THEN
  9198. RETURN TRUE
  9199. END;
  9200. RETURN FALSE
  9201. END IsBasicType;
  9202. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9203. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9204. BEGIN
  9205. baseType := record.baseType;
  9206. IF (baseType # NIL) THEN
  9207. baseType := baseType.resolved;
  9208. IF (baseType IS SyntaxTree.PointerType) THEN
  9209. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9210. END;
  9211. END;
  9212. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9213. recordType := baseType(SyntaxTree.RecordType);
  9214. ELSE
  9215. recordType := NIL;
  9216. END;
  9217. RETURN recordType
  9218. END RecordBase;
  9219. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9220. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9221. BEGIN
  9222. baseRecord := RecordBase(scope.ownerRecord);
  9223. IF baseRecord = NIL THEN RETURN NIL END;
  9224. scope := baseRecord.recordScope;
  9225. procedureType := procedure.type.resolved;
  9226. IF procedure IS SyntaxTree.Operator THEN
  9227. operator := scope.firstOperator;
  9228. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9229. (*
  9230. Printout.Info("not same ",procedureType);
  9231. Printout.Info("with ",operator.type);
  9232. *)
  9233. operator := operator.nextOperator;
  9234. END;
  9235. super := operator;
  9236. ELSE
  9237. super := scope.firstProcedure;
  9238. WHILE (super # NIL) & (super.name # procedure.name) DO
  9239. super := super.nextProcedure;
  9240. END;
  9241. END;
  9242. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9243. RETURN super
  9244. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9245. RETURN super
  9246. ELSE
  9247. RETURN FindSuperProcedure(scope,procedure);
  9248. END;
  9249. END FindSuperProcedure;
  9250. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9251. VAR procedure: SyntaxTree.Procedure;
  9252. BEGIN
  9253. procedure := record.recordScope.constructor;
  9254. IF procedure = NIL THEN
  9255. record := RecordBase(record);
  9256. IF record # NIL THEN
  9257. procedure := GetConstructor(record)
  9258. END;
  9259. END;
  9260. RETURN procedure;
  9261. END GetConstructor;
  9262. (* enter a case into a list of cases in a sorted way and check for collision *)
  9263. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9264. VAR prev,this,new: SyntaxTree.CaseConstant;
  9265. BEGIN
  9266. this := root;
  9267. prev := NIL;
  9268. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9269. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9270. RETURN FALSE
  9271. ELSE
  9272. IF (this # NIL) & (this.min = max+1) THEN
  9273. this.min := min
  9274. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9275. prev.max := min
  9276. ELSE
  9277. NEW(new); new.min := min; new.max := max;
  9278. new.next := this;
  9279. IF prev = NIL THEN
  9280. root := new;
  9281. ELSE
  9282. prev.next := new
  9283. END
  9284. END;
  9285. RETURN TRUE
  9286. END;
  9287. END EnterCase;
  9288. (** generate and return a new checker object, errors are entered into diagnostics **)
  9289. 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;
  9290. VAR checker: Checker;
  9291. BEGIN
  9292. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9293. RETURN checker
  9294. END NewChecker;
  9295. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9296. VAR warnings: Warnings;
  9297. BEGIN
  9298. NEW(warnings, diagnostics); RETURN warnings;
  9299. END NewWarnings;
  9300. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9301. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9302. END IsRangeType;
  9303. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9304. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9305. END IsMathArrayType;
  9306. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9307. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9308. END IsArrayType;
  9309. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9310. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9311. END IsComplexType;
  9312. (** if a type is an array-structured object type *)
  9313. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9314. VAR recordType: SyntaxTree.RecordType;
  9315. BEGIN
  9316. IF type = NIL THEN
  9317. RETURN FALSE
  9318. ELSE
  9319. type := type.resolved;
  9320. IF type IS SyntaxTree.PointerType THEN
  9321. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9322. IF type IS SyntaxTree.RecordType THEN
  9323. recordType := type(SyntaxTree.RecordType);
  9324. RETURN recordType.isObject & recordType.HasArrayStructure()
  9325. ELSE
  9326. RETURN FALSE
  9327. END
  9328. ELSE
  9329. RETURN FALSE
  9330. END
  9331. END
  9332. END IsArrayStructuredObjectType;
  9333. (** the math array structure of a type
  9334. - for math arrays: the array itself
  9335. - for pointers: the math array structure of the pointer base
  9336. - for array-structured object types: the underlying structure
  9337. - for non-math arrays and all other types: NIL
  9338. **)
  9339. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9340. VAR
  9341. result: SyntaxTree.MathArrayType;
  9342. BEGIN
  9343. IF type = NIL THEN
  9344. result := NIL
  9345. ELSE
  9346. type := type.resolved;
  9347. IF type IS SyntaxTree.PointerType THEN
  9348. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9349. END;
  9350. IF type IS SyntaxTree.MathArrayType THEN
  9351. result := type(SyntaxTree.MathArrayType)
  9352. ELSIF type IS SyntaxTree.RecordType THEN
  9353. result := type(SyntaxTree.RecordType).arrayStructure
  9354. ELSE
  9355. result := NIL
  9356. END
  9357. END;
  9358. RETURN result
  9359. END MathArrayStructureOfType;
  9360. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9361. VAR
  9362. result: BOOLEAN;
  9363. rangeExpression: SyntaxTree.RangeExpression;
  9364. BEGIN
  9365. IF x IS SyntaxTree.RangeExpression THEN
  9366. rangeExpression := x(SyntaxTree.RangeExpression);
  9367. result := TRUE;
  9368. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9369. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9370. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9371. ELSE
  9372. result := FALSE
  9373. END;
  9374. RETURN result
  9375. END IsStaticRange;
  9376. (** whether a type is a math array of tensor form **)
  9377. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9378. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9379. END IsTensor;
  9380. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9381. BEGIN
  9382. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9383. length := type(SyntaxTree.MathArrayType).staticLength;
  9384. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9385. RETURN TRUE
  9386. ELSE
  9387. RETURN FALSE
  9388. END;
  9389. END IsStaticMathArray;
  9390. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9391. BEGIN
  9392. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9393. END SymbolHasAddress;
  9394. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9395. BEGIN
  9396. RETURN
  9397. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9398. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9399. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9400. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9401. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9402. ;
  9403. END HasAddress;
  9404. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9405. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9406. BEGIN
  9407. IF (e IS SyntaxTree.Designator) THEN
  9408. d := e(SyntaxTree.Designator);
  9409. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9410. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9411. e := d.left;
  9412. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9413. END;
  9414. IF d # NIL THEN
  9415. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9416. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9417. END;
  9418. END;
  9419. RETURN FALSE;
  9420. END IsLocalVariable;
  9421. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9422. BEGIN
  9423. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9424. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9425. ELSE
  9426. RETURN TRUE
  9427. END;
  9428. END IsStaticProcedure;
  9429. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9430. CONST OptimizeMethodTable = FALSE;
  9431. BEGIN
  9432. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9433. END InMethodTable;
  9434. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9435. BEGIN
  9436. IF type = NIL THEN RETURN FALSE
  9437. ELSE
  9438. type := type.resolved;
  9439. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9440. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9441. END
  9442. END ReturnedAsParameter;
  9443. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9444. BEGIN
  9445. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9446. END StructuredReturnType;
  9447. END FoxSemanticChecker.
  9448. SystemTools.FreeDownTo FoxSemanticChecker ~