FoxSemanticChecker.Mod 393 KB

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