FoxSemanticChecker.Mod 400 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. 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. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  483. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  484. ELSE
  485. x.SetArrayBase(pointerType);
  486. END;
  487. END;
  488. IF x.length # NIL THEN
  489. variableAccessed := FALSE;
  490. e := ResolveExpression(x.length);
  491. IF (e.resolved = NIL) THEN
  492. IF variableAccessed THEN
  493. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  494. END;
  495. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  496. ELSE
  497. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  498. END;
  499. END;
  500. IF arrayBase IS SyntaxTree.ArrayType THEN
  501. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  502. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  503. END;
  504. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  505. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  506. END;
  507. x.SetHasPointers(arrayBase.hasPointers);
  508. x.SetState(SyntaxTree.Resolved);
  509. END;
  510. resolvedType := ResolvedType(x);
  511. END VisitArrayType;
  512. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  513. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  514. BEGIN
  515. module := currentScope.ownerModule;
  516. IF module.name=name THEN
  517. (* do nothing *)
  518. ELSE
  519. moduleScope := module.moduleScope;
  520. import := moduleScope.FindImport(name);
  521. IF import = NIL THEN
  522. import := SyntaxTree.NewImport(position,name,name,TRUE);
  523. moduleScope.AddImport(import);
  524. Register(import,moduleScope,FALSE);
  525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  526. VisitImport(import);
  527. ELSIF import.direct=FALSE THEN
  528. import.SetScope(module.moduleScope);
  529. import.SetDirect(TRUE);
  530. IF moduleScope.FindSymbol(import.name) = NIL THEN
  531. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  532. duplicate.SetContext(import.context);
  533. duplicate.SetModule(import.module);
  534. Register(duplicate,moduleScope,TRUE);
  535. VisitImport(duplicate);
  536. END;
  537. END;
  538. import.MarkUsed
  539. END;
  540. END ImportModule;
  541. (**
  542. resolve math array type
  543. - check base type
  544. - open math array of array forbidden
  545. - math array of tensor forbidden
  546. - static array of open array forbidden
  547. **)
  548. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  549. VAR arrayBase: SyntaxTree.Type;
  550. BEGIN
  551. IF TypeNeedsResolution(x) THEN
  552. x.SetArrayBase(ResolveType(x.arrayBase));
  553. IF x.length # NIL THEN
  554. x.SetLength(ConstantIntegerGeq0(x.length));
  555. END;
  556. arrayBase := x.arrayBase;
  557. IF arrayBase # NIL THEN
  558. arrayBase := arrayBase.resolved;
  559. IF arrayBase = SyntaxTree.invalidType THEN
  560. (* error already handled *)
  561. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  562. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  563. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  564. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  565. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  566. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  568. END;
  569. END;
  570. IF x.form = SyntaxTree.Static THEN
  571. x.SetIncrement(system.SizeOf(arrayBase));
  572. END;
  573. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  574. END;
  575. x.SetState(SyntaxTree.Resolved);
  576. END;
  577. resolvedType := ResolvedType(x);
  578. END VisitMathArrayType;
  579. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  580. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  581. (1) Rec = RECORD ... END; Ptr <---> Rec
  582. Ptr = POINTER TO Rec; ^ |
  583. | |
  584. TypeDesc TypeDesc
  585. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  586. ^ /
  587. | /
  588. TypeDesc <-- /
  589. *)
  590. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  591. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  592. BEGIN
  593. Strings.IntToStr(x.position,number);
  594. COPY(prefix,name);
  595. Strings.Append(name,"@");
  596. Strings.Append(name,number);
  597. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  598. typeDeclaration.SetDeclaredType(x);
  599. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  600. x.SetTypeDeclaration(typeDeclaration);
  601. currentScope.AddTypeDeclaration(typeDeclaration);
  602. typeDeclaration.SetScope(currentScope);
  603. END AnonymousTypeDeclaration;
  604. (**
  605. deferred pointer type resolving
  606. - resolve base type
  607. - check that base type is a record or array type
  608. - if error then set base type to invalid type
  609. **)
  610. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  611. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  612. BEGIN
  613. ASSERT(type.pointerBase # NIL);
  614. position := type.pointerBase.position;
  615. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  616. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  617. (* not for pointers, a type is needed for the records only
  618. IF type.typeDeclaration = NIL THEN
  619. AnonymousTypeDeclaration(type);
  620. END;
  621. *)
  622. END;
  623. resolved := ResolveType(type.pointerBase);
  624. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  625. type.SetPointerBase(resolved);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  627. recordType := resolved.resolved(SyntaxTree.RecordType);
  628. IF recordType.isObject & (recordType.baseType # NIL) THEN
  629. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  630. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  631. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  633. END;
  634. END;
  635. END;
  636. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  637. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  638. END;
  639. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  640. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  642. ELS
  643. *)
  644. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  645. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  646. END;
  647. END;
  648. ELSE
  649. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  650. type.SetPointerBase(SyntaxTree.invalidType)
  651. END
  652. END FixPointerType;
  653. (**
  654. resolve pointer type
  655. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  656. **)
  657. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  658. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  659. modifiers: SyntaxTree.Modifier; position: LONGINT;
  660. BEGIN
  661. IF TypeNeedsResolution(x) THEN
  662. modifiers := x.modifiers;
  663. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  664. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  665. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  666. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. recordBaseType := ResolveType(recordType.baseType);
  675. recordType.SetBaseType(recordBaseType);
  676. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  677. END;
  678. CheckModifiers(modifiers, TRUE);
  679. typeFixes.Add(x,currentScope);
  680. x.SetState(SyntaxTree.Resolved);
  681. END;
  682. resolvedType := ResolvedType(x)
  683. END VisitPointerType;
  684. (**
  685. resolve port type
  686. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  687. **)
  688. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  689. VAR value: LONGINT;
  690. BEGIN
  691. IF TypeNeedsResolution(x) THEN
  692. x.SetCellsAreObjects(cellsAreObjects);
  693. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  694. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  695. x.SetSize(value)
  696. ELSE
  697. x.SetSize(system.SizeOf(system.longintType));
  698. END;
  699. x.SetState(SyntaxTree.Resolved);
  700. END;
  701. resolvedType := ResolvedType(x)
  702. END VisitPortType;
  703. (**
  704. deferred procedure type resolving
  705. - resolve return type
  706. - traverse and resolve parameters
  707. **)
  708. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  709. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  710. BEGIN
  711. resolved := ResolveType(procedureType.returnType);
  712. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  713. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  714. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  715. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  716. END;
  717. procedureType.SetReturnType(resolved);
  718. IF (resolved # NIL) THEN
  719. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  720. parameter.SetType(procedureType.returnType);
  721. parameter.SetAccess(SyntaxTree.Hidden);
  722. parameter.SetUntraced(procedureType.hasUntracedReturn);
  723. VisitParameter(parameter);
  724. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  725. END;
  726. (* process parameters *)
  727. parameter :=procedureType.firstParameter;
  728. WHILE (parameter # NIL) DO
  729. VisitParameter(parameter);
  730. parameter := parameter.nextParameter;
  731. END;
  732. END FixProcedureType;
  733. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  734. VAR prev,this: SyntaxTree.Modifier;
  735. BEGIN
  736. this := modifiers;prev := NIL;
  737. WHILE (this # NIL) & (this.identifier # name) DO
  738. prev := this; this := this.nextModifier;
  739. END;
  740. IF this # NIL THEN
  741. IF this.expression # NIL THEN
  742. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  743. END;
  744. this.Resolved;
  745. position := this.position;
  746. RETURN TRUE
  747. ELSE
  748. RETURN FALSE
  749. END;
  750. END HasFlag;
  751. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  752. VAR prev,this: SyntaxTree.Modifier;
  753. BEGIN
  754. this := modifiers;prev := NIL;
  755. WHILE (this # NIL) & (this.identifier # name) DO
  756. prev := this; this := this.nextModifier;
  757. END;
  758. IF this # NIL THEN
  759. IF this.expression = NIL THEN
  760. Error(this.position,Diagnostics.Invalid,"expected expression value");
  761. ELSE
  762. this.SetExpression(ConstantExpression(this.expression));
  763. IF CheckIntegerValue(this.expression,value) THEN END;
  764. END;
  765. this.Resolved;
  766. position := this.position;
  767. RETURN TRUE
  768. ELSE RETURN FALSE
  769. END;
  770. END HasValue;
  771. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  772. VAR prev,this: SyntaxTree.Modifier;
  773. BEGIN
  774. this := modifiers;prev := NIL;
  775. WHILE (this # NIL) & (this.identifier # name) DO
  776. prev := this; this := this.nextModifier;
  777. END;
  778. IF this # NIL THEN
  779. IF this.expression = NIL THEN
  780. Error(this.position,Diagnostics.Invalid,"expected expression value");
  781. ELSE
  782. this.SetExpression(ConstantExpression(this.expression));
  783. IF CheckStringValue(this.expression,value) THEN END;
  784. END;
  785. this.Resolved;
  786. position := this.position;
  787. RETURN TRUE
  788. ELSE RETURN FALSE
  789. END;
  790. END HasStringValue;
  791. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  792. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  793. BEGIN
  794. IF cellsAreObjects THEN RETURN FALSE END;
  795. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  796. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  797. IF svalue[0] = "~" THEN
  798. Strings.TrimLeft(svalue, "~");
  799. IF svalue = backendName THEN
  800. RETURN TRUE;
  801. END;
  802. ELSIF svalue # backendName THEN
  803. RETURN TRUE;
  804. END;
  805. END;
  806. IF x.baseType # NIL THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. END SkipImplementation;
  811. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  812. VAR this: SyntaxTree.Modifier;
  813. BEGIN
  814. this := modifiers;
  815. WHILE this # NIL DO
  816. IF ~this.resolved THEN
  817. IF checkUse THEN
  818. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  819. ELSE
  820. this.SetExpression(ResolveExpression(this.expression));
  821. this.Resolved;
  822. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  823. END;
  824. END;
  825. this := this.nextModifier
  826. END;
  827. END CheckModifiers;
  828. (**
  829. resolve procedure type
  830. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  831. **)
  832. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  833. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  834. BEGIN
  835. IF TypeNeedsResolution(procedureType) THEN
  836. modifiers := procedureType.modifiers;
  837. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  838. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  839. procedureType.SetInterrupt(TRUE);
  840. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  841. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  842. IF useDarwinCCalls THEN (*fld*)
  843. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  844. ELSE
  845. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  846. END
  847. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  848. procedureType.SetNoReturn(TRUE);
  849. END;
  850. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  851. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  852. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  853. CheckModifiers(modifiers, TRUE);
  854. modifiers := procedureType.returnTypeModifiers;
  855. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  856. CheckModifiers(modifiers, TRUE);
  857. typeFixes.Add(procedureType,currentScope);
  858. procedureType.SetHasPointers(procedureType.isDelegate);
  859. procedureType.SetState(SyntaxTree.Resolved);
  860. END;
  861. resolvedType := ResolvedType(procedureType)
  862. END VisitProcedureType;
  863. (** check and resolve record type
  864. - check base type: must be record, math array or array-structured object type
  865. - check declarations
  866. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  867. **)
  868. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  869. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  870. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  871. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  872. hasPointers: BOOLEAN;
  873. modifiers: SyntaxTree.Modifier;
  874. value: LONGINT;
  875. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  876. BEGIN
  877. type := type.resolved;
  878. IF (type IS SyntaxTree.PointerType) &
  879. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  880. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  881. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  882. RETURN TRUE
  883. ELSE
  884. RETURN FALSE
  885. END;
  886. END IsPointerToRecord;
  887. BEGIN
  888. IF TypeNeedsResolution(x) THEN
  889. hasPointers := FALSE;
  890. modifiers := x.modifiers;
  891. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  892. CheckModifiers(modifiers, TRUE);
  893. IF x.baseType # NIL THEN
  894. position := x.baseType.position;
  895. baseType := ResolveType(x.baseType);
  896. resolved := baseType.resolved;
  897. hasPointers := hasPointers OR resolved.hasPointers;
  898. IF x.isObject THEN (* object *)
  899. ASSERT(x.pointerType # NIL);
  900. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  901. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  902. baseType := NIL
  903. ELSIF IsPointerToRecord(resolved,recordType) THEN
  904. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  905. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  906. ELSE
  907. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  908. END;
  909. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  910. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  913. ELSIF resolved IS SyntaxTree.RecordType THEN
  914. ELSE
  915. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  916. END;
  917. ELSE
  918. IF resolved IS SyntaxTree.RecordType THEN
  919. ELSE
  920. Error(position, Diagnostics.Invalid,"record does not extend record")
  921. END;
  922. END;
  923. x.SetBaseType(baseType);
  924. IF x.Level() > 15 THEN
  925. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  926. (* note:
  927. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  928. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  929. inheritance history of a type.
  930. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  931. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  932. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  933. *)
  934. END;
  935. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  936. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  937. END;
  938. END;
  939. Declarations(x.recordScope, FALSE);
  940. ResolveArrayStructure(x);
  941. (* computation of sizes and offsets skipped -> done in backend / system *)
  942. recordBase := x.GetBaseRecord();
  943. IF recordBase = NIL THEN numberMethods := 0
  944. ELSE numberMethods := recordBase.recordScope.numberMethods
  945. END;
  946. isRealtime := TRUE;
  947. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  948. WHILE symbol # NIL DO
  949. IF symbol IS SyntaxTree.Variable THEN
  950. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  951. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  952. END;
  953. IF symbol IS SyntaxTree.Procedure THEN
  954. procedure := symbol(SyntaxTree.Procedure);
  955. IF procedure.super # NIL THEN
  956. procedure.SetMethodNumber(procedure.super.methodNumber)
  957. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  958. procedure.SetMethodNumber(numberMethods);
  959. INC(numberMethods);
  960. END;
  961. END;
  962. symbol := symbol.nextSymbol;
  963. END;
  964. IF isRealtime THEN x.SetRealtime(TRUE) END;
  965. x.recordScope.SetNumberMethods(numberMethods);
  966. (* TODO: is this needed anymore? *)
  967. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  968. Error(x.position,Diagnostics.Invalid,"object extends a record")
  969. END;
  970. IF (x.typeDeclaration = NIL) THEN
  971. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  972. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  973. (*
  974. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  975. AnonymousTypeDeclaration(x,name);
  976. *)
  977. ELSE
  978. AnonymousTypeDeclaration(x,"Anonymous");
  979. END;
  980. END;
  981. x.SetHasPointers(hasPointers);
  982. x.SetState(SyntaxTree.Resolved);
  983. END;
  984. resolvedType := ResolvedType(x);
  985. END VisitRecordType;
  986. (** check and resolve cell type
  987. - check base type: must be cell
  988. - check declarations
  989. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  990. **)
  991. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  992. VAR
  993. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  994. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  995. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  996. recordBase: SyntaxTree.RecordType;
  997. numberMethods, int: LONGINT;
  998. real: LONGREAL;
  999. bool: BOOLEAN;
  1000. set: SET;
  1001. v: SyntaxTree.Expression;
  1002. str: Scanner.StringType;
  1003. atype: SyntaxTree.ArrayType;
  1004. prev: SyntaxTree.Scope;
  1005. skip: BOOLEAN;
  1006. svalue: ARRAY 32 OF CHAR;
  1007. BEGIN
  1008. IF TypeNeedsResolution(x) THEN
  1009. recordBase := NIL;
  1010. IF cellsAreObjects THEN
  1011. IF x.baseType = NIL THEN
  1012. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1013. ImportModule(qualifiedIdentifier.prefix, x.position);
  1014. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1015. x.SetBaseType(ResolveType(x.baseType));
  1016. recordBase := x.GetBaseRecord();
  1017. IF recordBase = NIL THEN
  1018. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1019. END;
  1020. ELSE
  1021. x.SetBaseType(ResolveType(x.baseType));
  1022. END;
  1023. ELSE
  1024. x.SetBaseType(ResolveType(x.baseType));
  1025. END;
  1026. IF recordBase = NIL THEN numberMethods := 0
  1027. ELSE numberMethods := recordBase.recordScope.numberMethods
  1028. END;
  1029. modifier := x.modifiers;
  1030. (*IF ~x.isCellNet THEN*)
  1031. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1032. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1033. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1034. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1035. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1036. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1037. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1038. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1039. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1040. symbol := system.activeCellsCapabilities;
  1041. WHILE symbol # NIL DO
  1042. IF HasFlag(modifier, symbol.name, position) THEN END;
  1043. symbol := symbol.nextSymbol;
  1044. END;
  1045. modifier := x.modifiers;
  1046. WHILE (modifier # NIL) DO
  1047. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1048. IF modifier.expression # NIL THEN
  1049. v := ConstantExpression(modifier.expression);
  1050. property.SetValue(v);
  1051. IF IsIntegerValue(modifier.expression, int) THEN
  1052. (*property.SetValue(modifier.expression);*)
  1053. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1054. property.SetType(system.longintType);
  1055. ELSIF IsRealValue(modifier.expression, real) THEN
  1056. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1057. property.SetType(system.longrealType);
  1058. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1059. property.SetType(system.booleanType);
  1060. ELSIF IsSetValue(modifier.expression, set) THEN
  1061. property.SetType(system.setType);
  1062. ELSIF IsStringValue(modifier.expression, str) THEN
  1063. (*property.SetValue(modifier.expression);*)
  1064. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1065. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1066. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1067. property.SetType(atype);
  1068. ELSE
  1069. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1070. END;
  1071. ELSE (* flag property *)
  1072. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1073. property.SetType(system.booleanType);
  1074. END;
  1075. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1076. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1077. x.AddProperty(property);
  1078. modifier := modifier.nextModifier;
  1079. END;
  1080. CheckModifiers(modifier, FALSE);
  1081. Declarations(x.cellScope, SkipImplementation(x));
  1082. (* process parameters *)
  1083. prev := currentScope;
  1084. currentScope := x.cellScope;
  1085. parameter :=x.firstParameter;
  1086. WHILE (parameter # NIL) DO
  1087. VisitParameter(parameter);
  1088. type := parameter.type.resolved;
  1089. IF ~(type IS SyntaxTree.PortType) THEN
  1090. WHILE IsStaticArray(type, type, len) DO
  1091. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1092. END;
  1093. WHILE IsDynamicArray(type, type) DO
  1094. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1095. END;
  1096. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1097. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1098. END;
  1099. END;
  1100. parameter := parameter.nextParameter;
  1101. END;
  1102. currentScope := prev;
  1103. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1104. WHILE symbol # NIL DO
  1105. IF symbol IS SyntaxTree.Variable THEN
  1106. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1107. END;
  1108. symbol := symbol.nextSymbol;
  1109. END;
  1110. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1111. IF (x.typeDeclaration = NIL) THEN
  1112. AnonymousTypeDeclaration(x,"Anonymous");
  1113. END;
  1114. x.SetState(SyntaxTree.Resolved);
  1115. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1116. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1117. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1118. Warning(x.position, "Non-empty body for an engine?");
  1119. END;
  1120. END;
  1121. resolvedType := ResolvedType(x);
  1122. END VisitCellType;
  1123. (* check if an object is an array-structured object type
  1124. - determine the array structure
  1125. - collect operators from top to bottom in the inheritance hierarchy
  1126. - check if LEN operator is declared
  1127. - determine number of possible index operators
  1128. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1129. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1130. *)
  1131. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1132. VAR
  1133. indexOperatorCount, i: LONGINT;
  1134. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1135. isTensor: BOOLEAN;
  1136. BEGIN
  1137. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1138. (* determine array structure *)
  1139. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1140. END;
  1141. IF recordType.HasArrayStructure() THEN
  1142. (* the object is an ASOT *)
  1143. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1144. (* reset array access operators *)
  1145. arrayAccessOperators.len := NIL;
  1146. arrayAccessOperators.generalRead := NIL;
  1147. arrayAccessOperators.generalWrite := NIL;
  1148. IF isTensor THEN
  1149. (* all operators of dimensionalities 1 to max *)
  1150. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1151. ELSE
  1152. (* all operators of certain dimensionality *)
  1153. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1154. END;
  1155. NEW(arrayAccessOperators.read, indexOperatorCount);
  1156. NEW(arrayAccessOperators.write, indexOperatorCount);
  1157. FOR i := 0 TO indexOperatorCount - 1 DO
  1158. arrayAccessOperators.read[i] := NIL;
  1159. arrayAccessOperators.write[i] := NIL
  1160. END;
  1161. (* collect access operators in the record scope *)
  1162. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1163. IF arrayAccessOperators.len = NIL THEN
  1164. (* TODO: think about making this operator optional for static array structures *)
  1165. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1166. END;
  1167. (* show error messages *)
  1168. IF isTensor THEN
  1169. (* require ARRAY [*] OF RANGE *)
  1170. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1171. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1172. ELSE
  1173. (* forbid ARRAY [*] OF RANGE *)
  1174. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1175. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1176. (* require RANGE, RANGE, ... RANGE *)
  1177. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1178. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1179. END;
  1180. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1181. ELSE
  1182. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1183. IF recordType.recordScope.firstOperator # NIL THEN
  1184. RETURN;
  1185. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1186. END
  1187. END
  1188. END ResolveArrayStructure;
  1189. (** collect array access operators in a record scope **)
  1190. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1191. VAR
  1192. baseType: SyntaxTree.Type;
  1193. operator: SyntaxTree.Operator;
  1194. isReadOperator, isGeneralOperator: BOOLEAN;
  1195. indexListSize, indexListKind, hashValue: LONGINT;
  1196. BEGIN
  1197. (* if a parent record scope exists, collect the operators there first *)
  1198. baseType := recordScope.ownerRecord.baseType;
  1199. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1200. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1201. END;
  1202. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1203. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1204. END;
  1205. (* go through all operators in the current record scope *)
  1206. operator := recordScope.firstOperator;
  1207. WHILE operator # NIL DO
  1208. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1209. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1210. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1211. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1212. IF isGeneralOperator THEN
  1213. IF isReadOperator THEN
  1214. arrayAccessOperators.generalRead := operator
  1215. ELSE
  1216. arrayAccessOperators.generalWrite := operator
  1217. END
  1218. ELSE
  1219. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1220. IF isReadOperator THEN
  1221. arrayAccessOperators.read[hashValue] := operator
  1222. ELSE
  1223. arrayAccessOperators.write[hashValue] := operator
  1224. END
  1225. END
  1226. END
  1227. ELSE
  1228. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1229. END;
  1230. operator := operator.nextOperator
  1231. END
  1232. END CollectArrayAccessOperators;
  1233. (** the hash value of an index operator **)
  1234. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1235. VAR result: LONGINT;
  1236. BEGIN
  1237. IF isTensor THEN
  1238. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1239. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1240. ELSE
  1241. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1242. END
  1243. ELSE
  1244. result := indexListKind
  1245. END;
  1246. RETURN result
  1247. END IndexOperatorHash;
  1248. (** 2 to the power of exponent **)
  1249. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1250. VAR result, i: LONGINT;
  1251. BEGIN
  1252. result := 1;
  1253. FOR i := 1 TO exponent DO
  1254. result := result * 2;
  1255. END;
  1256. RETURN result
  1257. END TwoToThePowerOf;
  1258. (** check if a LEN operator has a correct signature. i.e.
  1259. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1260. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1261. **)
  1262. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1263. VAR
  1264. procedureType: SyntaxTree.ProcedureType;
  1265. returnedArrayType: SyntaxTree.MathArrayType;
  1266. result: BOOLEAN;
  1267. BEGIN
  1268. result := FALSE;
  1269. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1270. IF (procedureType.numberParameters = 0) THEN
  1271. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1272. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1273. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1274. IF returnedArrayType.form = SyntaxTree.Open THEN
  1275. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1276. result := TRUE
  1277. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1278. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1279. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1280. result := TRUE
  1281. END
  1282. END
  1283. END
  1284. END
  1285. END;
  1286. IF result THEN
  1287. (* export symbol automatically *)
  1288. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1289. ELSE
  1290. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1291. END;
  1292. RETURN result
  1293. END CheckLenOperator;
  1294. (** check if an index operator has a correct signature. i.e.
  1295. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1296. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1297. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1298. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1299. - determine if it is a read or write operator (existance of return type)
  1300. - check index parameters
  1301. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1302. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1303. [LONGINT] -> binary 0 -> 0
  1304. [RANGE] -> binary 1 -> 1
  1305. [LONGINT, LONGINT] -> binary 00 -> 0
  1306. [LONGINT, RANGE] -> binary 01 -> 1
  1307. [RANGE, LONGINT] -> binary 10 -> 2
  1308. [RANGE, RANGE] -> binary 11 -> 3
  1309. etc.
  1310. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1311. - for read operators, check if return type matches the type of data that is read
  1312. - for write operators, check if last parameter type matches the type of data that is written
  1313. **)
  1314. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1315. VAR
  1316. elementType, otherElementType, dataType: SyntaxTree.Type;
  1317. procedureType: SyntaxTree.ProcedureType;
  1318. mathArrayType: SyntaxTree.MathArrayType;
  1319. parameter: SyntaxTree.Parameter;
  1320. parameterCount, rangeCount, i: LONGINT;
  1321. hasTypeError: BOOLEAN;
  1322. BEGIN
  1323. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1324. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1325. (* determine if it is a read or write operator *)
  1326. isReadOperator := (procedureType.returnType # NIL);
  1327. IF isReadOperator THEN
  1328. indexListSize := parameterCount;
  1329. ELSE
  1330. indexListSize := parameterCount - 1;
  1331. END;
  1332. IF indexListSize < 1 THEN
  1333. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1334. RETURN FALSE
  1335. END;
  1336. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1337. (* general operator *)
  1338. isGeneralOperator := TRUE;
  1339. IF indexListSize > 1 THEN
  1340. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1341. RETURN FALSE
  1342. END;
  1343. (* ARRAY [*] OF RANGE*)
  1344. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1345. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1347. RETURN FALSE
  1348. END;
  1349. parameter := procedureType.firstParameter.nextParameter
  1350. ELSE
  1351. (* fixed-dim. operator *)
  1352. isGeneralOperator := FALSE;
  1353. (* check number of index parameters *)
  1354. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1355. (* for tensors, limited to a certain size *)
  1356. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1357. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1358. RETURN FALSE
  1359. END
  1360. ELSE
  1361. (* for non-tensors, depends on dimensionality *)
  1362. IF indexListSize # arrayStructure.Dimensionality() THEN
  1363. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1364. RETURN FALSE
  1365. END
  1366. END;
  1367. (* go through all index parameters
  1368. - count the number of ranges
  1369. - determine the index list kind number
  1370. *)
  1371. indexListKind := 0;
  1372. rangeCount := 0;
  1373. parameter := procedureType.firstParameter;
  1374. FOR i := 1 TO indexListSize DO
  1375. indexListKind := indexListKind * 2;
  1376. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1377. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1378. INC(indexListKind);
  1379. INC(rangeCount)
  1380. ELSE
  1381. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1382. RETURN FALSE
  1383. END;
  1384. parameter := parameter.nextParameter
  1385. END;
  1386. END;
  1387. (*
  1388. - for read operators: check type of last parameter
  1389. - for write operators: check return type
  1390. *)
  1391. IF isReadOperator THEN
  1392. dataType := procedureType.returnType (* the return type *)
  1393. ELSE
  1394. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1395. END;
  1396. elementType := arrayStructure.ElementType();
  1397. hasTypeError := FALSE;
  1398. IF isGeneralOperator THEN
  1399. (* ARRAY [?] OF <Element> *)
  1400. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1401. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1402. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1403. hasTypeError := TRUE
  1404. END
  1405. ELSE
  1406. hasTypeError := TRUE
  1407. END
  1408. ELSE
  1409. IF rangeCount = 0 THEN
  1410. (* <Element> *)
  1411. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1412. ELSE
  1413. (* ARRAY [*, *, ..., *] OF <Element> *)
  1414. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1415. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1416. IF mathArrayType.IsFullyDynamic() THEN
  1417. IF mathArrayType.Dimensionality() = rangeCount THEN
  1418. otherElementType := mathArrayType.ElementType();
  1419. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1420. ELSE
  1421. hasTypeError := TRUE
  1422. END
  1423. ELSE
  1424. hasTypeError := TRUE
  1425. END
  1426. ELSE
  1427. hasTypeError := TRUE
  1428. END
  1429. END
  1430. END;
  1431. IF hasTypeError THEN
  1432. IF isReadOperator THEN
  1433. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1434. ELSE
  1435. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1436. END;
  1437. RETURN FALSE
  1438. END;
  1439. (* export symbol automatically *)
  1440. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1441. RETURN TRUE
  1442. END CheckIndexOperator;
  1443. (** resolve all pending types (late resolving).
  1444. - type fixes are resolved at the end of the declaration phase
  1445. - type fixes may imply new type fixes that are also entered at the end of the list
  1446. **)
  1447. PROCEDURE FixTypes;
  1448. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1449. BEGIN
  1450. prevScope := currentScope;
  1451. p := typeFixes.Get(currentScope);
  1452. WHILE p # NIL DO
  1453. ASSERT(currentScope # NIL);
  1454. ASSERT(p IS SyntaxTree.Type);
  1455. IF p IS SyntaxTree.PointerType THEN
  1456. FixPointerType(p(SyntaxTree.PointerType))
  1457. ELSIF p IS SyntaxTree.ProcedureType THEN
  1458. FixProcedureType(p(SyntaxTree.ProcedureType))
  1459. ELSE
  1460. HALT(100);
  1461. END;
  1462. p := typeFixes.Get(currentScope);
  1463. END;
  1464. currentScope :=prevScope;
  1465. END FixTypes;
  1466. (**
  1467. resolve type x
  1468. - if x is nil then return nil
  1469. - if x cannot be resolved then the result is invalidType else the result is x
  1470. - the resolved type is entered into x.resolved
  1471. **)
  1472. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1473. VAR prev,resolved: SyntaxTree.Type;
  1474. BEGIN
  1475. prev := resolvedType;
  1476. resolvedType := SyntaxTree.invalidType;
  1477. IF x = NIL THEN resolvedType := NIL
  1478. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1479. END;
  1480. resolved := resolvedType;
  1481. resolvedType := prev;
  1482. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1483. RETURN resolved
  1484. END ResolveType;
  1485. (*** compatibility rules ***)
  1486. (**
  1487. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1488. **)
  1489. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR result: SyntaxTree.Type;
  1491. BEGIN
  1492. result := SyntaxTree.invalidType;
  1493. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1494. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1496. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1497. ELSE result := type.resolved
  1498. END;
  1499. RETURN result
  1500. END RegularType;
  1501. (** returns signature compatibility of procedure types this and to
  1502. - if not compatible then error is reported
  1503. - compatibility means type equality
  1504. **)
  1505. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1506. VAR result: BOOLEAN;
  1507. BEGIN
  1508. result := SameType(to,this);
  1509. IF ~result THEN
  1510. Error(position,Diagnostics.Invalid,"signature incompatible");
  1511. IF VerboseErrorMessage THEN
  1512. Printout.Info("this",this);
  1513. Printout.Info("to",to);
  1514. END;
  1515. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1516. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1517. END;
  1518. RETURN result
  1519. END SignatureCompatible;
  1520. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1521. - for var parameters compatibility means same type except for
  1522. - formal is of open array of system byte
  1523. - formal is of record type
  1524. - formal is of open array type
  1525. - formal is of open math array type
  1526. - for value parameters compatibllity means assignment compatibility except for
  1527. - formal is of open array type
  1528. if compatible the return true else report error and return false
  1529. **)
  1530. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1531. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1532. BEGIN
  1533. formalType := RegularType(formal.position,formal.type);
  1534. actualType := RegularType(actual.position,actual.type);
  1535. error := FALSE;
  1536. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1538. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1539. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1540. END;
  1541. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1542. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1543. ELSIF ~IsVariable(actual) THEN
  1544. result := FALSE; error := TRUE;
  1545. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1546. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1547. ELSE
  1548. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1549. END;
  1550. IF VerboseErrorMessage THEN
  1551. Printout.Info("actual",actual);
  1552. Printout.Info("formal",formal);
  1553. END;
  1554. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1555. result := CompatibleTo(system,actualType,formalType);
  1556. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1557. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1558. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1559. IF IsArrayStructuredObjectType(actualType) THEN
  1560. actualType := MathArrayStructureOfType(actualType)
  1561. END;
  1562. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1563. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1564. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1565. END;
  1566. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1567. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1568. END;
  1569. ELSE
  1570. result := SameType(actualType,formalType)
  1571. END
  1572. ELSE
  1573. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1574. actualType := system.characterType;
  1575. END;
  1576. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1577. result := TRUE; (* special rule for WINAPI parameters *)
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSE
  1581. result := CompatibleTo(system,actualType,formalType);
  1582. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1584. END;
  1585. END;
  1586. END;
  1587. IF ~result & ~error THEN
  1588. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1589. IF VerboseErrorMessage THEN
  1590. Printout.Info("actual",actual);
  1591. Printout.Info("formal",formal);
  1592. END;
  1593. END;
  1594. RETURN result
  1595. END ParameterCompatible;
  1596. (** check compatibility for expressions of the form left := right
  1597. - if compatible then return true else error report and return false
  1598. - check if left is variable
  1599. - check compatiblity
  1600. **)
  1601. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1602. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1603. BEGIN
  1604. result := FALSE;
  1605. leftType := RegularType(left.position,left.type);
  1606. rightType := RegularType(right.position,right.type);
  1607. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1608. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1609. END;
  1610. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1611. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1612. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1613. (* error already handled *)
  1614. result := TRUE;
  1615. ELSIF ~IsVariable(left) THEN
  1616. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1617. IF VerboseErrorMessage THEN
  1618. Printout.Info("left",left);
  1619. Printout.Info("right",right);
  1620. END;
  1621. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1622. result := TRUE;
  1623. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1624. result := TRUE
  1625. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1626. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1627. IF VerboseErrorMessage THEN
  1628. Printout.Info("left",left);
  1629. Printout.Info("right",right);
  1630. END;
  1631. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1632. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1633. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1634. ELSE
  1635. result := TRUE
  1636. END;
  1637. RETURN result
  1638. END AssignmentCompatible;
  1639. (*** values ***)
  1640. (** check and resolve integer value **)
  1641. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1642. VAR hugeint: HUGEINT;
  1643. BEGIN
  1644. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1645. value.SetType(Global.GetIntegerType(system,hugeint));
  1646. resolvedExpression := value
  1647. END VisitIntegerValue;
  1648. (** check and resolve real value **)
  1649. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1650. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1651. BEGIN
  1652. subtype := value(SyntaxTree.RealValue).subtype;
  1653. IF subtype = Scanner.Real THEN
  1654. type := system.realType
  1655. ELSIF subtype = Scanner.Longreal THEN
  1656. type := system.longrealType
  1657. ELSE
  1658. HALT(100)
  1659. END;
  1660. value.SetType(type);
  1661. resolvedExpression := value
  1662. END VisitRealValue;
  1663. (** check and resolve complex value **)
  1664. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1665. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1666. BEGIN
  1667. subtype := value(SyntaxTree.ComplexValue).subtype;
  1668. IF subtype = Scanner.Real THEN
  1669. type := system.complexType
  1670. ELSIF subtype = Scanner.Longreal THEN
  1671. type := system.longcomplexType
  1672. ELSE
  1673. HALT(100)
  1674. END;
  1675. value.SetType(type);
  1676. resolvedExpression := value
  1677. END VisitComplexValue;
  1678. (** check and resolve set value **)
  1679. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1680. BEGIN
  1681. value.SetType(system.setType);
  1682. resolvedExpression := value
  1683. END VisitSetValue;
  1684. (** check and resolve set value **)
  1685. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1686. BEGIN
  1687. value.SetType(SyntaxTree.invalidType);
  1688. resolvedExpression := value
  1689. END VisitMathArrayValue;
  1690. (** check and resolve boolean value **)
  1691. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1692. BEGIN
  1693. value.SetType(system.booleanType);
  1694. resolvedExpression := value
  1695. END VisitBooleanValue;
  1696. (** check and resolve string value **)
  1697. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1698. BEGIN
  1699. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1700. resolvedExpression := value
  1701. END VisitStringValue;
  1702. (** check and resolve character value **)
  1703. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1704. BEGIN
  1705. value.SetType(system.characterType);
  1706. resolvedExpression := value
  1707. END VisitCharacterValue;
  1708. (** check and resolve nil value **)
  1709. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1710. BEGIN
  1711. value.SetType(system.nilType);
  1712. resolvedExpression := value
  1713. END VisitNilValue;
  1714. (** check and resolve enumerator value **)
  1715. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1716. BEGIN
  1717. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1718. ASSERT(value.type # NIL);
  1719. resolvedExpression := value
  1720. END VisitEnumerationValue;
  1721. (*** expressions ***)
  1722. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1723. - check all elements on integer type
  1724. - if element range is constant, then check lower and upper bound
  1725. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1726. if an error occurs then report error and return invalidExpression
  1727. **)
  1728. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1729. VAR
  1730. i: LONGINT;
  1731. element: SyntaxTree.Expression;
  1732. constant: BOOLEAN;
  1733. elements: SyntaxTree.ExpressionList;
  1734. s: SET;
  1735. result: SyntaxTree.Expression;
  1736. value: SyntaxTree.Value;
  1737. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1738. VAR
  1739. left, right: SyntaxTree.Expression;
  1740. elementResult: SyntaxTree.Expression;
  1741. leftInteger, rightInteger, temp: LONGINT;
  1742. BEGIN
  1743. (* set context of range *)
  1744. IF element IS SyntaxTree.RangeExpression THEN
  1745. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1746. END;
  1747. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1748. IF elementResult = SyntaxTree.invalidExpression THEN
  1749. (* error already reported *)
  1750. constant := FALSE
  1751. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1752. (* the element is a range expression *)
  1753. (* extract left and right hand side of range *)
  1754. left := elementResult(SyntaxTree.RangeExpression).first;
  1755. right := elementResult(SyntaxTree.RangeExpression).last;
  1756. (* guaranteed by VisitRangeExpression: *)
  1757. ASSERT((left # NIL) & (right # NIL));
  1758. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1759. ELSE
  1760. (* the element is not a range expression *)
  1761. (* check type and add conversion if needed *)
  1762. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1763. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1764. ELSE
  1765. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1766. elementResult := SyntaxTree.invalidExpression;
  1767. constant := FALSE
  1768. END;
  1769. left := elementResult;
  1770. right := elementResult
  1771. END;
  1772. IF elementResult # SyntaxTree.invalidExpression THEN
  1773. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1774. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1775. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1776. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1777. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1778. END
  1779. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1780. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1781. ELSE
  1782. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1783. s := {};
  1784. ELSE
  1785. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1786. IF leftInteger < 0 THEN leftInteger := 0 END;
  1787. (*!!!!!!!!! this is a hack !!!!!!! *)
  1788. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1789. extends the range x..31 to x..63 !!!!!! *)
  1790. s := s + {leftInteger..rightInteger};
  1791. END;
  1792. END;
  1793. ELSE
  1794. constant := FALSE;
  1795. END
  1796. END;
  1797. RETURN elementResult
  1798. END CheckElement;
  1799. BEGIN
  1800. result := set; constant := TRUE; s := {}; elements := set.elements;
  1801. IF elements # NIL THEN
  1802. FOR i := 0 TO elements.Length()-1 DO
  1803. element := elements.GetExpression(i);
  1804. element := CheckElement(element);
  1805. IF element = SyntaxTree.invalidExpression THEN
  1806. result := SyntaxTree.invalidExpression
  1807. END;
  1808. elements.SetExpression(i,element);
  1809. END;
  1810. END;
  1811. IF constant THEN
  1812. value := SyntaxTree.NewSetValue(set.position,s);
  1813. value.SetType(system.setType);
  1814. result.SetResolved(value);
  1815. END;
  1816. (* optimization possible
  1817. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1818. left this to the programmer...
  1819. *)
  1820. result.SetType(system.setType);
  1821. resolvedExpression := result;
  1822. END VisitSet;
  1823. (*
  1824. old variant: quite generic but needs better conversion handling, do this?
  1825. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1826. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1827. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1828. BEGIN
  1829. type := NIL;
  1830. numberElements := x.elements.Length();
  1831. FOR i := 0 TO numberElements-1 DO
  1832. expression := x.elements.GetExpression(i);
  1833. position := expression.position;
  1834. expression := ResolveExpression(x.elements.GetExpression(i));
  1835. x.elements.SetExpression(i,de);
  1836. IF type = NIL THEN
  1837. type := expression.type;
  1838. ELSIF CompatibleTo(system,expression.type,type) THEN
  1839. (* ok *)
  1840. ELSIF CompatibleTo(system,type,expression.type) THEN
  1841. type := expression.type
  1842. ELSE
  1843. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1844. type := SyntaxTree.invalidType;
  1845. END;
  1846. END;
  1847. isValue := TRUE;
  1848. FOR i := 0 TO numberElements-1 DO
  1849. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1850. x.elements.SetExpression(i,expression);
  1851. isValue := isValue & (expression.resolved # NIL);
  1852. END;
  1853. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1854. arrayType.SetArrayBase(type);
  1855. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1856. IF isValue THEN
  1857. value := SyntaxTree.NewMathArrayValue(position);
  1858. value.SetElements(x.elements);
  1859. x.SetResolved(value);
  1860. END;
  1861. x.SetType(arrayType);
  1862. resolvedExpression := x;
  1863. END VisitMathArrayExpression;
  1864. *)
  1865. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1866. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1867. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1868. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1869. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1870. BEGIN
  1871. numberElements := x.elements.Length();
  1872. FOR i := 0 TO numberElements-1 DO
  1873. expression := x.elements.GetExpression(i);
  1874. IF expression IS SyntaxTree.MathArrayExpression THEN
  1875. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1876. ELSE
  1877. position := expression.position;
  1878. expression := ResolveExpression(x.elements.GetExpression(i));
  1879. x.elements.SetExpression(i,expression);
  1880. IF type = NIL THEN
  1881. type := expression.type;
  1882. ELSIF CompatibleTo(system,expression.type,type) THEN
  1883. (* ok *)
  1884. ELSIF CompatibleTo(system,type,expression.type) THEN
  1885. type := expression.type
  1886. ELSE
  1887. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1888. type := SyntaxTree.invalidType;
  1889. END;
  1890. END;
  1891. END;
  1892. END RecursivelyFindType;
  1893. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1894. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1895. BEGIN
  1896. numberElements := x.elements.Length();
  1897. FOR i := 0 TO numberElements-1 DO
  1898. expression := x.elements.GetExpression(i);
  1899. IF expression IS SyntaxTree.MathArrayExpression THEN
  1900. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1901. ELSE
  1902. position := expression.position;
  1903. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1904. x.elements.SetExpression(i,expression);
  1905. isValue := isValue & (expression.resolved # NIL);
  1906. END;
  1907. END;
  1908. END RecursivelySetExpression;
  1909. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1910. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1911. arrayType: SyntaxTree.MathArrayType;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. baseType := NIL;
  1915. gsize := 0;
  1916. FOR i := 0 TO numberElements-1 DO
  1917. expression := x.elements.GetExpression(i);
  1918. IF expression IS SyntaxTree.MathArrayExpression THEN
  1919. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1920. IF i=0 THEN
  1921. gsize := size;
  1922. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1923. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1924. ELSE expression.SetType(baseType)
  1925. END;
  1926. ELSIF baseType = NIL THEN baseType := type;
  1927. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1928. END;
  1929. END;
  1930. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1931. arrayType.SetArrayBase(baseType);
  1932. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1933. RETURN ResolveType(arrayType);
  1934. END RecursivelySetType;
  1935. BEGIN
  1936. type := NIL;
  1937. RecursivelyFindType(x);
  1938. isValue := TRUE;
  1939. RecursivelySetExpression(x);
  1940. arrayType := RecursivelySetType(x);
  1941. x.SetType(arrayType);
  1942. IF isValue THEN
  1943. value := SyntaxTree.NewMathArrayValue(x.position);
  1944. value.SetArray(x);
  1945. x.SetResolved(value);
  1946. value.SetType(arrayType);
  1947. END;
  1948. x.SetType(arrayType);
  1949. resolvedExpression := x;
  1950. END VisitMathArrayExpression;
  1951. (** check and resolve unary expression **)
  1952. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1953. VAR
  1954. left: SyntaxTree.Expression;
  1955. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1956. bool: BOOLEAN;
  1957. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1958. value: SyntaxTree.Value;
  1959. BEGIN
  1960. type := SyntaxTree.invalidType;
  1961. left := ResolveExpression(unaryExpression.left);
  1962. unaryExpression.SetLeft(left);
  1963. operator := unaryExpression.operator;
  1964. result := unaryExpression;
  1965. IF ~system.operatorDefined[operator] THEN
  1966. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1967. RETURN
  1968. ELSIF left.type = NIL THEN
  1969. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1970. resolvedExpression := SyntaxTree.invalidExpression;
  1971. RETURN
  1972. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1973. RETURN
  1974. END;
  1975. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1976. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1977. END;
  1978. IF operatorCall # NIL THEN
  1979. result := operatorCall;
  1980. type := operatorCall.type;
  1981. (* admissible operators
  1982. Minus number, set
  1983. Not boolean
  1984. *)
  1985. ELSE
  1986. CASE unaryExpression.operator OF
  1987. |Scanner.Minus:
  1988. IF IsIntegerType(left.type.resolved) THEN
  1989. IF left.resolved # NIL THEN
  1990. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1991. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1992. result.SetResolved(value);
  1993. type := Global.GetIntegerType(system,int);
  1994. value.SetType(type);
  1995. ELSE
  1996. type := left.type
  1997. END
  1998. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1999. IF IsRealValue(left,real) THEN
  2000. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2001. result.SetResolved(value);
  2002. type := left.type;
  2003. value.SetType(type);
  2004. ELSE
  2005. type := left.type;
  2006. END;
  2007. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2008. IF IsSetValue(left,set) THEN
  2009. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2010. result.SetResolved(value);
  2011. type := left.type;
  2012. value.SetType(type);
  2013. ELSE
  2014. type := left.type;
  2015. END;
  2016. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2017. IF IsComplexValue(left, real, imaginary) THEN
  2018. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2019. result.SetResolved(value);
  2020. type := left.type;
  2021. value.SetType(type);
  2022. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2023. ELSE
  2024. type := left.type;
  2025. END
  2026. ELSE
  2027. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2028. END;
  2029. |Scanner.Not:
  2030. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2031. IF IsBooleanValue(left,bool) THEN
  2032. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2033. result.SetResolved(value);
  2034. type := system.booleanType;
  2035. value.SetType(type);
  2036. ELSE
  2037. type := system.booleanType;
  2038. END;
  2039. ELSE
  2040. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2041. END;
  2042. |Scanner.Plus:
  2043. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2044. result := left; type := left.type;
  2045. ELSE
  2046. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2047. END;
  2048. (* ADDRESS OF *)
  2049. |Scanner.Address:
  2050. IF HasAddress(left) THEN
  2051. type := system.addressType;
  2052. ELSE
  2053. type := SyntaxTree.invalidType;
  2054. Error(left.position,Diagnostics.Invalid,"has no address");
  2055. Printout.Info("par", left);
  2056. END;
  2057. (* SIZE OF *)
  2058. |Scanner.Size:
  2059. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2060. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2061. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2062. value := SyntaxTree.NewIntegerValue(left.position, int);
  2063. result.SetResolved(value);
  2064. type := Global.GetIntegerType(system,int);
  2065. value.SetType(type)
  2066. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2067. ELSE
  2068. (* for variables, system sizeof could represent the physically occupied size
  2069. determined via the type descriptor, implement that ? *)
  2070. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2071. END
  2072. (* ALIAS OF *)
  2073. |Scanner.Alias:
  2074. type := left.type.resolved;
  2075. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2076. type := SyntaxTree.invalidType;
  2077. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2078. END;
  2079. ELSE
  2080. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2081. END;
  2082. END;
  2083. result.SetType(type);
  2084. resolvedExpression := result
  2085. END VisitUnaryExpression;
  2086. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2087. VAR
  2088. result: SyntaxTree.Expression;
  2089. array: SyntaxTree.MathArrayExpression;
  2090. value: SyntaxTree.MathArrayValue;
  2091. isValue: BOOLEAN;
  2092. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2093. BEGIN
  2094. type := type.resolved;
  2095. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2096. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2097. END;
  2098. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2099. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2100. END;
  2101. RETURN type
  2102. END BaseType;
  2103. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2104. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2105. BEGIN
  2106. numberElements := x.elements.Length();
  2107. FOR i := 0 TO numberElements-1 DO
  2108. expression := x.elements.GetExpression(i);
  2109. IF expression IS SyntaxTree.MathArrayExpression THEN
  2110. array := SyntaxTree.NewMathArrayExpression(position);
  2111. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2112. expression := array;
  2113. ELSE
  2114. position := expression.position;
  2115. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2116. isValue := isValue & (expression.resolved # NIL);
  2117. END;
  2118. to.elements.AddExpression(expression);
  2119. END;
  2120. END RecursivelyConvert;
  2121. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2122. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2123. arrayType: SyntaxTree.MathArrayType;
  2124. BEGIN
  2125. numberElements := x.elements.Length();
  2126. baseType := NIL;
  2127. gsize := 0;
  2128. FOR i := 0 TO numberElements-1 DO
  2129. expression := x.elements.GetExpression(i);
  2130. IF expression IS SyntaxTree.MathArrayExpression THEN
  2131. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2132. IF i=0 THEN
  2133. gsize := size;
  2134. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2135. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2136. ELSE expression.SetType(baseType)
  2137. END;
  2138. ELSIF baseType = NIL THEN baseType := type;
  2139. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2140. END;
  2141. END;
  2142. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2143. arrayType.SetArrayBase(baseType);
  2144. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2145. RETURN ResolveType(arrayType);
  2146. END RecursivelySetType;
  2147. BEGIN
  2148. result := SyntaxTree.invalidExpression;
  2149. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2150. result := expression (* do not convert *)
  2151. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2152. isValue := TRUE;
  2153. type := BaseType(type);
  2154. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2155. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2156. value := SyntaxTree.NewMathArrayValue(array.position);
  2157. value.SetArray(array);
  2158. value.SetType(RecursivelySetType(array));
  2159. result := value;
  2160. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2161. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2162. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2163. IF result = NIL THEN
  2164. result := SyntaxTree.invalidExpression;
  2165. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2166. IF VerboseErrorMessage THEN
  2167. Printout.Info("expression",expression);
  2168. Printout.Info("type",type);
  2169. END;
  2170. END;
  2171. END;
  2172. RETURN result
  2173. END MathArrayConversion;
  2174. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2175. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2176. BEGIN
  2177. result := expression; type := type.resolved;
  2178. IF (expression IS SyntaxTree.IntegerValue) THEN
  2179. int := expression(SyntaxTree.IntegerValue).hvalue;
  2180. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2181. int := Global.ConvertSigned(int,system.SizeOf(type));
  2182. result := SyntaxTree.NewIntegerValue(position,int);
  2183. result.SetType(type);
  2184. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2185. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2186. result := SyntaxTree.NewIntegerValue(position,int);
  2187. result.SetType(type);
  2188. ELSIF (type IS SyntaxTree.FloatType) THEN
  2189. result := SyntaxTree.NewRealValue(expression.position,int);
  2190. result.SetType(type);
  2191. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2192. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2193. result.SetType(type);
  2194. ELSIF (type IS SyntaxTree.SetType) THEN
  2195. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2196. result.SetType(type);
  2197. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2198. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2199. result.SetType(type);
  2200. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2201. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2202. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2203. END;
  2204. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2205. result.SetType(type);
  2206. ELSIF (type IS SyntaxTree.PortType) THEN
  2207. result := ConvertValue(position, expression, system.integerType);
  2208. ELSE
  2209. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2210. result := SyntaxTree.invalidExpression;
  2211. IF VerboseErrorMessage THEN
  2212. Printout.Info("expression",expression);
  2213. Printout.Info("type",type);
  2214. END;
  2215. END;
  2216. ELSIF IsRealValue(expression,real) THEN
  2217. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2218. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2219. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2222. int := ENTIERH(real);
  2223. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.FloatType) THEN
  2226. result := SyntaxTree.NewRealValue(position,real);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2229. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2230. result.SetType(type);
  2231. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2232. ELSIF (type IS SyntaxTree.PortType) THEN
  2233. result := ConvertValue(position, expression, system.integerType);
  2234. ELSE
  2235. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2236. result := SyntaxTree.invalidExpression;
  2237. END
  2238. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2239. IF (type IS SyntaxTree.ComplexType) THEN
  2240. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2241. result.SetType(type);
  2242. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2243. ELSE
  2244. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2245. result := SyntaxTree.invalidExpression;
  2246. END
  2247. ELSIF IsSetValue(expression,set) THEN
  2248. IF (type IS SyntaxTree.IntegerType) THEN
  2249. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2252. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2253. result.SetType(type);
  2254. ELSIF (type IS SyntaxTree.PortType) THEN
  2255. result := ConvertValue(position, expression, system.integerType);
  2256. ELSE
  2257. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2258. result := SyntaxTree.invalidExpression;
  2259. END;
  2260. ELSIF IsStringValue(expression,string) THEN
  2261. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2262. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2263. result.SetType(type);
  2264. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2265. ELSE
  2266. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2267. result := SyntaxTree.invalidExpression;
  2268. END;
  2269. ELSIF IsCharacterValue(expression,char) THEN
  2270. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2271. string[0] := char; string[1] := 0X;
  2272. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2273. result := SyntaxTree.NewStringValue(expression.position,string);
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.ByteType) THEN
  2276. (* do not simply set the new type as this could invalidate types of constants *)
  2277. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2278. result.SetType(type)
  2279. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2280. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2281. result.SetType(type);
  2282. ELSIF (type IS SyntaxTree.SetType) THEN
  2283. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2286. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.PortType) THEN
  2289. result := ConvertValue(position, expression, system.integerType);
  2290. ELSE
  2291. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2292. result := SyntaxTree.invalidExpression;
  2293. END;
  2294. ELSIF expression IS SyntaxTree.NilValue THEN
  2295. IF type IS SyntaxTree.AddressType THEN
  2296. result := SyntaxTree.NewIntegerValue(position,0);
  2297. result.SetType(type);
  2298. ELSE
  2299. result := expression;
  2300. END;
  2301. (* nothing to be done *)
  2302. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2303. result := MathArrayConversion(position, expression,type);
  2304. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2305. int := expression(SyntaxTree.EnumerationValue).value;
  2306. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2307. int := Global.ConvertSigned(int,system.SizeOf(type));
  2308. result := SyntaxTree.NewIntegerValue(position,int);
  2309. result.SetType(type);
  2310. ELSE
  2311. result := expression;
  2312. END;
  2313. (* nothing to be done *)
  2314. ELSE
  2315. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2316. IF VerboseErrorMessage THEN
  2317. Printout.Info("expression",expression);
  2318. Printout.Info("type",type);
  2319. END;
  2320. result := SyntaxTree.invalidExpression;
  2321. END;
  2322. RETURN result
  2323. END ConvertValue;
  2324. (**
  2325. return a conversion of an expression to a given type
  2326. - if expression is already of same type then return expression
  2327. - if incompatible conversion then report error and return invalidExpression
  2328. **)
  2329. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2330. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2331. BEGIN
  2332. type := type.resolved;
  2333. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2334. result := expression;
  2335. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2336. ELSIF expression = NIL THEN (* NIL expression *)
  2337. ELSIF expression.type = NIL THEN
  2338. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2339. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2340. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2341. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2342. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2343. ELSIF expression.resolved # NIL THEN (* value *)
  2344. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2345. IF value IS SyntaxTree.Value THEN
  2346. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2347. result.SetResolved(value(SyntaxTree.Value));
  2348. result.SetType(value.type);
  2349. ELSE
  2350. result := value
  2351. END;
  2352. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2353. expressionList := SyntaxTree.NewExpressionList();
  2354. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2355. typeDeclaration.SetDeclaredType(type);
  2356. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2357. typeSymbol.SetType(typeDeclaration.type);
  2358. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2359. expressionList.AddExpression(expression);
  2360. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2361. result.SetType(type);
  2362. ELSIF IsArrayStructuredObjectType(type) THEN
  2363. (* no type can be converted to an array-structured object type *)
  2364. HALT(100)
  2365. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2366. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2367. result := MathArrayConversion(position, expression,type);
  2368. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2369. expression := ConvertToMathArray(expression);
  2370. type := MathArrayStructureOfType(type);
  2371. result := MathArrayConversion(position, expression, type)
  2372. ELSE
  2373. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2374. END;
  2375. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2376. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2377. OR ~(type IS SyntaxTree.ArrayType) THEN
  2378. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2379. END;
  2380. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2381. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2382. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2383. (*skip, no conversion*)
  2384. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2385. (* skip, no conversion *)
  2386. ELSE
  2387. ASSERT(~(type IS SyntaxTree.RangeType));
  2388. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2389. ASSERT(type # NIL);
  2390. END;
  2391. RETURN result
  2392. END NewConversion;
  2393. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2394. BEGIN
  2395. IF CompatibleTo(system,expression.type, type) THEN
  2396. RETURN NewConversion(position, expression, type, NIL);
  2397. ELSE
  2398. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2399. RETURN SyntaxTree.invalidExpression
  2400. END;
  2401. END CompatibleConversion;
  2402. (**
  2403. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2404. **)
  2405. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2406. VAR leftType,rightType: SyntaxTree.Type;
  2407. BEGIN
  2408. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2409. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2410. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2411. (* error already handled *)
  2412. ELSE
  2413. leftType := left.type.resolved; rightType := right.type.resolved;
  2414. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2415. right := NewConversion(right.position, right, leftType, NIL);
  2416. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2417. left := NewConversion(left.position,left,rightType,NIL);
  2418. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2419. right := NewConversion(right.position, right, leftType, NIL);
  2420. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2421. left := NewConversion(left.position,left,rightType,NIL);
  2422. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2423. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2424. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2425. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2426. ELSIF
  2427. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2428. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2429. (* must be the case LONGREAL / COMPLEX ) *)
  2430. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2431. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2432. ELSE
  2433. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2434. END;
  2435. END;
  2436. END ConvertOperands;
  2437. (** find and return best operator matching to parameter list (nil, if none)
  2438. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2439. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2440. **)
  2441. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2442. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2443. identifier: SyntaxTree.Identifier;
  2444. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2445. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2446. BEGIN
  2447. operator := scope.firstOperator;
  2448. WHILE(operator # NIL) DO
  2449. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2450. procedureType := operator.type(SyntaxTree.ProcedureType);
  2451. distance := Distance(system, procedureType,actualParameters);
  2452. IF (distance < Infinity) THEN
  2453. IF returnType # NIL THEN
  2454. IF procedureType.returnType = NIL THEN
  2455. distance := Infinity
  2456. ELSE
  2457. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2458. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2459. END;
  2460. END;
  2461. END;
  2462. (*
  2463. IF distance < Infinity THEN
  2464. TRACE(distance, operator);
  2465. Printout.Info("potential operator",operator);
  2466. ELSE
  2467. Printout.Info("operator not possible",operator);
  2468. END;
  2469. *)
  2470. IF distance < bestDistance THEN
  2471. bestDistance := distance;
  2472. bestOperator := operator;
  2473. END;
  2474. END;
  2475. operator := operator.nextOperator;
  2476. END;
  2477. (*
  2478. Printout.Info("taken operator",bestOperator);
  2479. *)
  2480. END FindInScope;
  2481. BEGIN
  2482. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2483. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2484. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2485. import := currentScope.ownerModule.moduleScope.firstImport;
  2486. WHILE (bestDistance > 0) & (import # NIL) DO
  2487. IF import.module # NIL THEN
  2488. identifier := Global.GetIdentifier(operator,import.module.case);
  2489. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2490. END;
  2491. import := import.nextImport;
  2492. END;
  2493. RETURN bestOperator
  2494. END FindOperator;
  2495. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2496. BEGIN
  2497. currentScope := scope;
  2498. END SetCurrentScope;
  2499. (**
  2500. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2501. - handle LEN and DIM operator for array-structured object types
  2502. - find operator, if found then
  2503. - if in other module then add import designator
  2504. - create symbol designator for operator
  2505. - if error then return invalidExpression, if no operator then return NIL
  2506. **)
  2507. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2508. VAR
  2509. operator: SyntaxTree.Operator;
  2510. import: SyntaxTree.Import;
  2511. expression, result: SyntaxTree.Expression;
  2512. designator: SyntaxTree.Designator;
  2513. actualParameters, tempList: SyntaxTree.ExpressionList;
  2514. recordType: SyntaxTree.RecordType;
  2515. castReturnType : SyntaxTree.MathArrayType;
  2516. BEGIN
  2517. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2518. result := SyntaxTree.invalidExpression
  2519. ELSIF leftExpression = NIL THEN
  2520. result := NIL
  2521. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2522. (* LEN or DIM operator on array-structured object type *)
  2523. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2524. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2525. IF recordType.arrayAccessOperators.len = NIL THEN
  2526. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2527. result := SyntaxTree.invalidExpression
  2528. ELSE
  2529. ASSERT(leftExpression IS SyntaxTree.Designator);
  2530. designator := leftExpression(SyntaxTree.Designator);
  2531. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2532. ASSERT(expression IS SyntaxTree.Designator);
  2533. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2534. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2535. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2536. result := designator
  2537. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2538. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2539. tempList := SyntaxTree.NewExpressionList();
  2540. tempList.AddExpression(rightExpression);
  2541. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2542. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2543. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2544. tempList := SyntaxTree.NewExpressionList();
  2545. tempList.AddExpression(designator);
  2546. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2547. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2548. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2549. END
  2550. END;
  2551. ELSE
  2552. (* import OCArrayBase if needed *)
  2553. IF ~arrayBaseImported THEN
  2554. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2555. (* operators on complex numbers *)
  2556. ImportModule(Global.ArrayBaseName,position);
  2557. arrayBaseImported := TRUE;
  2558. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2559. IF op = Global.Dim THEN
  2560. (* not existing in OCArrayBase *)
  2561. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2562. (* not existing in OCArrayBase *)
  2563. ELSE
  2564. ImportModule(Global.ArrayBaseName,position);
  2565. arrayBaseImported := TRUE;
  2566. END
  2567. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2568. ImportModule(Global.ArrayBaseName,position);
  2569. arrayBaseImported := TRUE
  2570. END;
  2571. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2572. (* LEN(RANGE) *)
  2573. ImportModule(Global.ArrayBaseName,position);
  2574. arrayBaseImported := TRUE;
  2575. END;
  2576. END;
  2577. actualParameters := SyntaxTree.NewExpressionList();
  2578. actualParameters.AddExpression(leftExpression);
  2579. IF rightExpression # NIL THEN
  2580. actualParameters.AddExpression(rightExpression)
  2581. END;
  2582. operator := FindOperator(system,op,actualParameters,resultType);
  2583. IF operator # NIL THEN
  2584. designator := NIL;
  2585. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2586. import := currentScope.ownerModule.moduleScope.firstImport;
  2587. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2588. import := import.nextImport;
  2589. END;
  2590. expression := NewSymbolDesignator(position,NIL,import);
  2591. designator := expression(SyntaxTree.Designator);
  2592. END;
  2593. expression := NewSymbolDesignator(position,designator,operator);
  2594. designator := expression(SyntaxTree.Designator);
  2595. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2596. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2597. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2598. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2599. result.SetType(castReturnType);
  2600. END;
  2601. ELSE
  2602. result := NIL;
  2603. END;
  2604. END;
  2605. RETURN result
  2606. END NewOperatorCall;
  2607. (** check and resolve binary expression **)
  2608. (*! clean up *)
  2609. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2610. VAR left,right,result: SyntaxTree.Expression;
  2611. leftType, rightType: SyntaxTree.Type;
  2612. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2613. cl,cr: CHAR;
  2614. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2615. type: SyntaxTree.Type;
  2616. value: SyntaxTree.Value;
  2617. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2618. integerConstantFolding: BOOLEAN;
  2619. list: SyntaxTree.ExpressionList;
  2620. PROCEDURE NewBool(v: BOOLEAN);
  2621. BEGIN
  2622. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2623. value.SetType(system.booleanType);
  2624. result.SetResolved(value);
  2625. type := system.booleanType
  2626. END NewBool;
  2627. PROCEDURE NewSet(v: SET);
  2628. BEGIN
  2629. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2630. value.SetType(system.setType);
  2631. result.SetResolved(value);
  2632. type := system.setType;
  2633. END NewSet;
  2634. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2635. BEGIN
  2636. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2637. (* type cast to "larger" type only if the value is still in the range *)
  2638. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2639. value.SetType(t);
  2640. END;
  2641. result.SetResolved(value);
  2642. type := value.type;
  2643. END NewInteger;
  2644. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2645. BEGIN
  2646. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2647. value.SetType(t);
  2648. result.SetResolved(value);
  2649. type := t;
  2650. END NewReal;
  2651. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2652. BEGIN
  2653. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2654. value.SetType(t);
  2655. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2656. result.SetResolved(value);
  2657. type := t;
  2658. END NewComplex;
  2659. BEGIN
  2660. type := SyntaxTree.invalidType;
  2661. left := ResolveExpression(binaryExpression.left);
  2662. right := ResolveExpression(binaryExpression.right);
  2663. binaryExpression.SetLeft(left);
  2664. binaryExpression.SetRight(right);
  2665. result := binaryExpression;
  2666. operator := binaryExpression.operator;
  2667. IF ~system.operatorDefined[operator] THEN
  2668. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2669. result := SyntaxTree.invalidExpression;
  2670. RETURN
  2671. END;
  2672. IF left.type = NIL THEN
  2673. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2674. result := SyntaxTree.invalidExpression;
  2675. RETURN;
  2676. END;
  2677. IF right.type = NIL THEN
  2678. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2679. result := SyntaxTree.invalidExpression;
  2680. RETURN;
  2681. END;
  2682. leftType := left.type.resolved; rightType := right.type.resolved;
  2683. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2684. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2685. END;
  2686. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2687. list := SyntaxTree.NewExpressionList();
  2688. list.AddExpression(right);
  2689. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2690. END;
  2691. IF operatorCall # NIL THEN
  2692. result := operatorCall;
  2693. type := operatorCall.type;
  2694. (* admissible operators:
  2695. Times, Plus, Minus numeric numeric numeric
  2696. set set set
  2697. Slash numeric numeric real /complex
  2698. set set set
  2699. Div , Mod integer integer integer
  2700. And, Or bool bool bool
  2701. Equal, Unequal basic basic bool
  2702. pointer pointer bool
  2703. object object bool
  2704. record record bool
  2705. string string bool
  2706. enumerator enumerator bool
  2707. Less, LessEqual,
  2708. Greater, GreaterEqual integer/real integer/real bool
  2709. enumerator enumerator bool
  2710. In integer set bool
  2711. Is pointer type bool
  2712. object type bool
  2713. record type bool
  2714. Upto: special abbreviation for a..b
  2715. *)
  2716. ELSIF (left.type = NIL) THEN
  2717. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2718. D.Str("nil type in "); D.Type(left); D.Ln;
  2719. result := SyntaxTree.invalidExpression;
  2720. ELSIF (right.type = NIL) THEN
  2721. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2722. result := SyntaxTree.invalidExpression;
  2723. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2724. result := SyntaxTree.invalidExpression;
  2725. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2726. HALT(100);
  2727. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2728. type := system.booleanType;
  2729. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2730. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2731. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2732. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2733. IF VerboseErrorMessage THEN
  2734. Printout.Info("left",left);
  2735. Printout.Info("right",right);
  2736. END;
  2737. ELSIF IsUnsafePointer(left.type) THEN
  2738. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2739. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2740. NewBool(TRUE)
  2741. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2742. NewBool(TRUE);
  2743. ELSIF IsUnextensibleRecord(left) THEN
  2744. NewBool(FALSE)
  2745. END
  2746. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2747. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2748. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2749. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2750. ELSIF operator = Scanner.In THEN (* left IN right *)
  2751. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2752. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2753. NewBool(il IN sr);
  2754. ELSE
  2755. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2756. left := NewConversion(left.position, left, system.longintType,NIL);
  2757. binaryExpression.SetLeft(left)
  2758. END;
  2759. type := system.booleanType;
  2760. END
  2761. ELSE
  2762. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2763. END
  2764. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2765. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2766. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2767. END;
  2768. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2769. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2770. END
  2771. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2772. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2773. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2774. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2775. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2776. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2777. ConvertOperands(left, right);
  2778. binaryExpression.SetLeft(left);
  2779. binaryExpression.SetRight(right);
  2780. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2781. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2782. END;
  2783. type := system.booleanType;
  2784. ELSE
  2785. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2786. END
  2787. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2788. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2789. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2790. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2791. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2792. CASE operator OF
  2793. |Scanner.Equal: NewBool(strl^=strr^);
  2794. |Scanner.Unequal:NewBool(strl^#strr^);
  2795. |Scanner.Less: NewBool(strl^<strr^);
  2796. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2797. |Scanner.Greater: NewBool(strl^>strr^);
  2798. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2799. ELSE
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2801. END;
  2802. END;
  2803. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2804. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2805. type := system.booleanType
  2806. ELSE
  2807. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2808. END;
  2809. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2810. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2811. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2812. type := system.booleanType;
  2813. ELSE
  2814. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2815. END
  2816. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2817. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2818. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2819. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2820. type := system.booleanType
  2821. ELSE
  2822. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2823. END;
  2824. ELSE
  2825. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2826. END;
  2827. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2828. type := system.booleanType;
  2829. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2830. type := system.booleanType;
  2831. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2832. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2833. THEN
  2834. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2835. IF (leftType # rightType) THEN
  2836. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2837. ConvertOperands(left,right); (* operands must be of the same type here *)
  2838. END;
  2839. binaryExpression.SetLeft(left);
  2840. binaryExpression.SetRight(right);
  2841. leftType := left.type.resolved;
  2842. rightType := right.type.resolved;
  2843. END;
  2844. type := leftType;
  2845. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2846. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2847. IF VerboseErrorMessage THEN
  2848. Printout.Info("left",left);
  2849. Printout.Info("right",right);
  2850. END;
  2851. ELSIF IsIntegerType(leftType) THEN
  2852. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2853. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2854. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2855. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2856. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2857. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2858. END;
  2859. END;
  2860. (* constant folding *)
  2861. (* bootstrap64
  2862. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2863. CASE operator OF
  2864. |Scanner.Plus: NewInteger(il+ir,left.type);
  2865. |Scanner.Minus: NewInteger(il-ir,left.type);
  2866. |Scanner.Times: NewInteger(il*ir,left.type);
  2867. |Scanner.Slash:
  2868. IF ir # 0 THEN
  2869. NewReal(il/ir, system.realType);
  2870. END;
  2871. |Scanner.Mod:
  2872. IF ir > 0 THEN
  2873. NewInteger(il MOD ir,left.type);
  2874. END;
  2875. |Scanner.Div:
  2876. IF ir > 0 THEN
  2877. NewInteger(il DIV ir,left.type);
  2878. END;
  2879. |Scanner.Equal: NewBool(il=ir);
  2880. |Scanner.Unequal:NewBool(il#ir);
  2881. |Scanner.Less: NewBool(il<ir);
  2882. |Scanner.LessEqual: NewBool(il<=ir);
  2883. |Scanner.Greater: NewBool(il>ir);
  2884. |Scanner.GreaterEqual: NewBool(il>=ir);
  2885. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2886. END;
  2887. ELS*)
  2888. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2889. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2890. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2891. CASE operator OF
  2892. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2893. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2894. |Scanner.Times: NewInteger(hl*hr,left.type);
  2895. |Scanner.Slash:
  2896. IF hr = 0 THEN
  2897. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2898. ELSE
  2899. IF type.sizeInBits = 64 THEN
  2900. NewReal(hl/hr,system.longrealType);
  2901. ELSE
  2902. NewReal(hl/hr,system.realType)
  2903. END
  2904. END;
  2905. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2906. |Scanner.Mod:
  2907. IF hr = 0 THEN
  2908. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2909. ELSE
  2910. NewInteger(hl MOD hr, left.type);
  2911. (* bootstrap64
  2912. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2913. *)
  2914. END;
  2915. |Scanner.Div:
  2916. IF hr = 0 THEN
  2917. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2918. ELSE
  2919. NewInteger(hl DIV hr, left.type);
  2920. (* bootstrap64
  2921. NewInteger(Machine.DivH(hl,hr),left.type);
  2922. *)
  2923. END;
  2924. (* *)
  2925. |Scanner.Equal: NewBool(hl=hr);
  2926. |Scanner.Unequal: NewBool(hl#hr);
  2927. |Scanner.Less: NewBool(hl<hr);
  2928. |Scanner.LessEqual: NewBool(hl<=hr);
  2929. |Scanner.Greater: NewBool(hl>hr);
  2930. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2931. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2932. END;
  2933. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2934. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2935. type := left.type
  2936. ELSIF (operator = Scanner.Slash) THEN
  2937. left := NewConversion(left.position,left,system.realType,NIL);
  2938. right := NewConversion(right.position,right,system.realType,NIL);
  2939. binaryExpression.SetLeft(left);
  2940. binaryExpression.SetRight(right);
  2941. type := system.realType
  2942. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2943. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2944. type := system.booleanType
  2945. ELSE
  2946. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2947. END;
  2948. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2949. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2950. CASE operator OF
  2951. |Scanner.Plus: NewReal(rl+rr,leftType);
  2952. |Scanner.Minus: NewReal(rl-rr,leftType);
  2953. |Scanner.Times:NewReal(rl*rr,leftType);
  2954. |Scanner.Slash:
  2955. IF rr = 0 THEN
  2956. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2957. ELSE
  2958. NewReal(rl/rr,leftType);
  2959. END
  2960. |Scanner.Equal: NewBool(rl=rr);
  2961. |Scanner.Unequal: NewBool(rl#rr);
  2962. |Scanner.Less: NewBool(rl<rr);
  2963. |Scanner.LessEqual: NewBool(rl<=rr);
  2964. |Scanner.Greater: NewBool(rl>rr);
  2965. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2966. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2967. END;
  2968. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2969. type := left.type
  2970. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2971. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2972. type := system.booleanType
  2973. ELSE
  2974. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2975. IF VerboseErrorMessage THEN
  2976. Printout.Info("left",left);
  2977. Printout.Info("right",right);
  2978. END;
  2979. END;
  2980. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2981. CASE operator OF
  2982. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2983. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2984. ELSE
  2985. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2986. IF VerboseErrorMessage THEN
  2987. Printout.Info("left", left);
  2988. Printout.Info("right", right)
  2989. END;
  2990. END;
  2991. IF ~error THEN
  2992. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2993. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2994. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2995. (* do constant folding *)
  2996. CASE operator OF
  2997. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2998. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2999. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3000. |Scanner.Slash:
  3001. divisor := c * c + d * d;
  3002. ASSERT(divisor # 0);
  3003. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3004. |Scanner.Equal: NewBool((a = c) & (b = d))
  3005. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3006. END
  3007. END
  3008. END
  3009. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3010. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3011. CASE operator OF
  3012. |Scanner.And: NewBool(bl & br);
  3013. |Scanner.Or: NewBool(bl OR br);
  3014. |Scanner.Equal: NewBool(bl = br);
  3015. |Scanner.Unequal: NewBool(bl # br);
  3016. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3017. END;
  3018. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3019. type := system.booleanType
  3020. ELSE
  3021. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3022. END;
  3023. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3024. (* constant folding *)
  3025. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3026. IF operator = Scanner.Equal THEN
  3027. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3028. ELSIF operator = Scanner.Unequal THEN
  3029. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3030. END;
  3031. END;
  3032. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3033. type := system.booleanType;
  3034. ELSE
  3035. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3036. END;
  3037. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3038. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3039. CASE operator OF
  3040. |Scanner.Plus: NewSet(sl + sr);
  3041. |Scanner.Minus: NewSet(sl - sr);
  3042. |Scanner.Times: NewSet(sl * sr);
  3043. |Scanner.Slash: NewSet(sl / sr);
  3044. |Scanner.Equal: NewBool(sl=sr);
  3045. |Scanner.Unequal: NewBool(sl#sr);
  3046. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3047. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3048. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3049. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3050. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3051. END;
  3052. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3053. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3054. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3055. THEN
  3056. type := system.booleanType
  3057. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3058. type := left.type
  3059. ELSE
  3060. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3061. END;
  3062. ELSIF IsCharacterType(left.type) THEN
  3063. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3064. CASE operator OF
  3065. |Scanner.Equal: NewBool(cl=cr);
  3066. |Scanner.Unequal: NewBool(cl#cr);
  3067. |Scanner.Less: NewBool(cl<cr);
  3068. |Scanner.LessEqual: NewBool(cl<=cr);
  3069. |Scanner.Greater: NewBool(cl>cr);
  3070. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3071. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3072. END;
  3073. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3074. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3075. type := system.booleanType
  3076. ELSE
  3077. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3078. END;
  3079. ELSE
  3080. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3081. END;
  3082. ELSE
  3083. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3084. END;
  3085. IF type = SyntaxTree.invalidType THEN
  3086. result := SyntaxTree.invalidExpression
  3087. ELSE
  3088. result.SetType(type)
  3089. END;
  3090. resolvedExpression := result
  3091. END VisitBinaryExpression;
  3092. (** resolve a range expression of the from <<first .. last BY step>>
  3093. - depending on the context different things are checked:
  3094. ArrayIndex:
  3095. - components must be integers
  3096. - replace missing lower bound with 0
  3097. - replace missing upper bound with MAX(LONGINT)
  3098. - replace missing step size with 1
  3099. SetElement:
  3100. - components must be integers
  3101. - replace missing lower bound with 0
  3102. - replace missing upper bound with MAX(SET)
  3103. - must not have step size
  3104. CaseGuard:
  3105. - components must be constant
  3106. - components must be integers or characters
  3107. - must have lower and upper bound present
  3108. - components are made compatible
  3109. - must not have step size
  3110. - if error: return invalidExpression
  3111. **)
  3112. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3113. VAR
  3114. hasError: BOOLEAN;
  3115. first, last, step: SyntaxTree.Expression;
  3116. BEGIN
  3117. hasError := FALSE;
  3118. first := x.first;
  3119. last := x.last;
  3120. step := x.step;
  3121. (* check lower bound *)
  3122. IF x.context = SyntaxTree.CaseGuard THEN
  3123. IF first = NIL THEN
  3124. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3125. hasError := TRUE
  3126. ELSE
  3127. first := ResolveExpression(first);
  3128. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3129. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3130. hasError := TRUE
  3131. ELSE
  3132. IF first IS SyntaxTree.StringValue THEN
  3133. (* add conversion from string to character *)
  3134. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3135. END
  3136. END;
  3137. (* check if expression is constant *)
  3138. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3139. (* error already reported *)
  3140. hasError := TRUE
  3141. END
  3142. END
  3143. ELSE (* ArrayIndex, SetElement *)
  3144. IF first = NIL THEN
  3145. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3146. END;
  3147. first := ResolveExpression(first);
  3148. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3149. first := NewConversion(first.position, first, system.longintType, NIL)
  3150. ELSE
  3151. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3152. hasError := TRUE
  3153. END
  3154. END;
  3155. (* check upper bound *)
  3156. IF x.context = SyntaxTree.CaseGuard THEN
  3157. IF last = NIL THEN
  3158. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3159. hasError := TRUE
  3160. ELSE
  3161. last := ResolveExpression(last);
  3162. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3163. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3164. hasError := TRUE
  3165. ELSE
  3166. IF last IS SyntaxTree.StringValue THEN
  3167. (* add conversion from string to character *)
  3168. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3169. END
  3170. END;
  3171. (* check if expression is constant *)
  3172. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3173. (* error already reported *)
  3174. hasError := TRUE
  3175. ELSE
  3176. (* try to make lower and upper bound compatible *)
  3177. ConvertOperands(first, last);
  3178. IF first.type.resolved # last.type.resolved THEN
  3179. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3180. hasError := TRUE
  3181. END
  3182. END
  3183. END
  3184. ELSE (* ArrayIndex, SetElement *)
  3185. IF last = NIL THEN
  3186. IF x.context = SyntaxTree.ArrayIndex THEN
  3187. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3188. ELSE
  3189. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3190. END
  3191. END;
  3192. last := ResolveExpression(last);
  3193. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3194. last := NewConversion(last.position, last, system.longintType, NIL)
  3195. ELSE
  3196. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3197. hasError := TRUE
  3198. END
  3199. END;
  3200. (* check step size *)
  3201. IF x.context = SyntaxTree.ArrayIndex THEN
  3202. IF step = NIL THEN
  3203. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3204. END;
  3205. step := ResolveExpression(step);
  3206. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3207. step := NewConversion(step.position, step, system.longintType, NIL)
  3208. ELSE
  3209. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3210. hasError := TRUE
  3211. END
  3212. ELSE (* SetElement, CaseGuard *)
  3213. IF step # NIL THEN
  3214. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3215. hasError := TRUE
  3216. END
  3217. END;
  3218. IF hasError THEN
  3219. resolvedExpression := SyntaxTree.invalidExpression
  3220. ELSE
  3221. x.SetFirst(first);
  3222. x.SetLast(last);
  3223. x.SetStep(step);
  3224. x.SetType(system.rangeType);
  3225. resolvedExpression := x;
  3226. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3227. END
  3228. END VisitRangeExpression;
  3229. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3230. BEGIN
  3231. x.SetType(NIL);
  3232. resolvedExpression := x;
  3233. END VisitTensorRangeExpression;
  3234. (** resolve the expression d and return result as designator
  3235. - resolve expression
  3236. - if expression is a designator then return designator else error message and return invalidDesignator
  3237. **)
  3238. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3239. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3240. BEGIN
  3241. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3242. resolved := ResolveExpression(d);
  3243. IF resolved = SyntaxTree.invalidExpression THEN
  3244. (* error should already have been reported *)
  3245. result := SyntaxTree.invalidDesignator;
  3246. ELSIF resolved IS SyntaxTree.Designator THEN
  3247. result := resolved(SyntaxTree.Designator);
  3248. ELSE
  3249. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3250. result := SyntaxTree.invalidDesignator;
  3251. END;
  3252. (* result.type might be nil. *)
  3253. RETURN result
  3254. END ResolveDesignator;
  3255. (**
  3256. symbol designator generated in this module
  3257. nothing to be resolved
  3258. **)
  3259. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3260. BEGIN
  3261. resolvedExpression := x;
  3262. END VisitSymbolDesignator;
  3263. (**
  3264. self designator generated in this module
  3265. nothing to be resolved
  3266. **)
  3267. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3268. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3269. BEGIN
  3270. (* check if in record scope *)
  3271. scope := currentScope;
  3272. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3273. scope := scope.outerScope;
  3274. END;
  3275. IF scope = NIL THEN (* in module scope *)
  3276. x.SetType(system.anyType);
  3277. ELSIF scope IS SyntaxTree.CellScope THEN
  3278. cell := scope(SyntaxTree.CellScope).ownerCell;
  3279. x.SetType(cell);
  3280. ELSE (* in record scope *)
  3281. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3282. IF (record # NIL) & (record.pointerType # NIL) THEN
  3283. type := ResolveType(record.pointerType);
  3284. x.SetType(type);
  3285. ELSE
  3286. x.SetType(record);
  3287. END;
  3288. END;
  3289. resolvedExpression := x;
  3290. END VisitSelfDesignator;
  3291. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3292. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3293. BEGIN
  3294. scope := currentScope;
  3295. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3296. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3297. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3298. returnType := procedureType.returnType;
  3299. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3300. THEN
  3301. x.SetType(returnType);
  3302. ELSE
  3303. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3304. x.SetType(SyntaxTree.invalidType);
  3305. END;
  3306. ELSE
  3307. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3308. x.SetType(SyntaxTree.invalidType);
  3309. END;
  3310. x.SetAssignable(TRUE);
  3311. resolvedExpression := x;
  3312. END VisitResultDesignator;
  3313. (**
  3314. return symbol designator as an expression
  3315. - if symbol is a constant then return the constant value expression
  3316. - else
  3317. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3318. - if symbol is a guarded variable then return a TypeGuardDesignator
  3319. - else return a symbol designator
  3320. **)
  3321. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3322. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3323. guardType: SyntaxTree.Type;
  3324. BEGIN
  3325. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3326. result := SyntaxTree.invalidExpression;
  3327. ASSERT(symbol # NIL);
  3328. (*
  3329. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3330. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3331. Error(position,Diagnostics.Invalid,"type not allowed here");
  3332. ELS *)
  3333. (* not needed any more as values are stored in the expression
  3334. IF symbol IS SyntaxTree.Constant THEN
  3335. result := symbol(SyntaxTree.Constant).value
  3336. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3337. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3338. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3339. ELSE
  3340. result := symbol(SyntaxTree.Constant).value
  3341. END;
  3342. ELSE
  3343. *)
  3344. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3345. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3346. THEN
  3347. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3348. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3349. left := NewDereferenceDesignator(position,left);
  3350. left.SetHidden(TRUE);
  3351. END;
  3352. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3353. scope := currentScope;
  3354. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3355. scope := scope.outerScope;
  3356. END;
  3357. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3358. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3359. END;
  3360. END;
  3361. 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);
  3362. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3363. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3364. ELSE
  3365. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3366. END;
  3367. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3368. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3369. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3370. result.SetType(symbol.type);
  3371. result.SetAssignable(assignable);
  3372. symbol.MarkUsed;
  3373. IF symbol IS SyntaxTree.Constant THEN
  3374. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3375. END;
  3376. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3377. variableAccessed := TRUE
  3378. END;
  3379. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3380. IF GetGuard(symbol,guardType) THEN
  3381. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3382. END;
  3383. END;
  3384. ASSERT(result.type # NIL);
  3385. RETURN result
  3386. END NewSymbolDesignator;
  3387. (** check and resolve an identifier designator "identifier"
  3388. - if identifier = self then return SelfDesignator
  3389. - else find symbol in current scope
  3390. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3391. **)
  3392. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3393. VAR symbol: SyntaxTree.Symbol;
  3394. BEGIN
  3395. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3396. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3397. IF symbol # NIL THEN
  3398. ResolveSymbol(symbol);
  3399. ASSERT(symbol.type # NIL);
  3400. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3401. ELSE
  3402. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3403. IF VerboseErrorMessage THEN
  3404. Printout.Info("undeclared identifier designator",identifierDesignator);
  3405. END;
  3406. resolvedExpression := SyntaxTree.invalidDesignator;
  3407. END;
  3408. END VisitIdentifierDesignator;
  3409. (** check and resolve a selector designator of the form left.designator
  3410. - if left is a pointer type then do auto dereferenciation
  3411. - left denotes a search scope:
  3412. - if left type is import type then set search scope to respective module
  3413. - if left type is enumeration type then set search scope to respective enumeration scope
  3414. - elsif left type is record type then set search scope to record scope
  3415. - search symbol in computed scope
  3416. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3417. **)
  3418. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3419. VAR
  3420. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3421. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3422. BEGIN
  3423. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3424. left := ResolveDesignator(selectorDesignator.left);
  3425. result := SyntaxTree.invalidDesignator;
  3426. IF left # NIL THEN
  3427. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3428. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3429. END;
  3430. scope := NIL;
  3431. IF left.type = NIL THEN
  3432. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3433. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3434. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3435. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3436. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3437. module := symbol(SyntaxTree.Import).module;
  3438. IF module # NIL THEN
  3439. scope := module.moduleScope
  3440. ELSE
  3441. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3442. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3443. END;
  3444. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3445. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3446. ASSERT(scope # NIL)
  3447. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3448. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3449. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3450. IF type IS SyntaxTree.EnumerationType THEN
  3451. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3452. ELSE
  3453. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3454. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3455. END;
  3456. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3457. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3458. ELSE
  3459. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3460. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3461. END;
  3462. symbol := NIL;
  3463. IF scope # NIL THEN
  3464. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3465. IF symbol # NIL THEN
  3466. ResolveSymbol(symbol);
  3467. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3468. symbol.MarkUsed
  3469. ELSE
  3470. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3471. IF VerboseErrorMessage THEN
  3472. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3473. Printout.Info("scope", scope);
  3474. Printout.Info("left", left);
  3475. Printout.Info("undeclared identifier",selectorDesignator);
  3476. Printout.Info("left resolved designator",left);
  3477. END
  3478. END;
  3479. END;
  3480. END;
  3481. resolvedExpression := result;
  3482. END VisitSelectorDesignator;
  3483. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3484. VAR len,idx: LONGINT;
  3485. BEGIN
  3486. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3487. IF idx < 0 THEN
  3488. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3489. ELSE
  3490. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3491. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3492. END;
  3493. END;
  3494. END;
  3495. END IndexCheck;
  3496. (*
  3497. - if index designator has not type, use newBaseType as its type
  3498. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3499. - special rule: if static array of dynamic array occurs, make it all dynamic
  3500. index designator type: new base type: new index designator type:
  3501. NIL z z
  3502. ARRAY [x, y] z ARRAY [x, y] OF z
  3503. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3504. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3505. *)
  3506. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3507. VAR
  3508. mathArrayType: SyntaxTree.MathArrayType;
  3509. makeDynamic: BOOLEAN;
  3510. BEGIN
  3511. IF indexDesignator.type = NIL THEN
  3512. indexDesignator.SetType(newBaseType)
  3513. ELSE
  3514. (* index designator must be a of math array type *)
  3515. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3516. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3517. (* determine if all arrays have to be made dynamic *)
  3518. makeDynamic :=
  3519. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3520. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3521. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3522. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3523. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3524. END;
  3525. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3526. mathArrayType.SetArrayBase(newBaseType)
  3527. END
  3528. END SetIndexBaseType;
  3529. (** check and append index list element to index designator of math array
  3530. - check validity of single index or array range
  3531. - compute new type
  3532. - if range then create new array type (calculate length of resulting array)
  3533. - otherwise take sourceArray.arrayBase as new type
  3534. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3535. **)
  3536. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3537. VAR
  3538. targetArray: SyntaxTree.MathArrayType;
  3539. first, last, step: SyntaxTree.Expression;
  3540. firstValue, lastValue, stepValue, length: LONGINT;
  3541. rangeExpression: SyntaxTree.RangeExpression;
  3542. BEGIN
  3543. IF indexListItem.type = SyntaxTree.invalidType THEN
  3544. (* error already handled *)
  3545. indexDesignator.parameters.AddExpression(indexListItem)
  3546. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3547. indexDesignator.HasRange;
  3548. indexDesignator.HasTensorRange;
  3549. indexDesignator.parameters.AddExpression(indexListItem);
  3550. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3551. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3552. IndexCheck(indexListItem, sourceArray.length);
  3553. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3554. indexDesignator.parameters.AddExpression(indexListItem)
  3555. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3556. indexDesignator.HasRange;
  3557. (* if the range is given as an array range expression, check the validity of its components *)
  3558. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3559. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3560. first := rangeExpression.first;
  3561. last := rangeExpression.last;
  3562. step := rangeExpression.step;
  3563. (* perform static checks on range components *)
  3564. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3565. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3566. END;
  3567. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3568. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3569. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3570. END
  3571. END;
  3572. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3573. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3574. END;
  3575. (* add conversions to size type *)
  3576. (* TODO: needed? *)
  3577. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3578. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3579. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3580. END;
  3581. IF indexDesignator.hasTensorRange THEN
  3582. (* the index designator's base type is a tensor: leave it as is *)
  3583. ELSE
  3584. (* append a new math array to the index designator's base type *)
  3585. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3586. IF ~error THEN
  3587. (*
  3588. (* optimization: calculate length of target array for static ranges *)
  3589. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3590. IF IsStaticallyOpenRange(rangeExpression) THEN
  3591. (* range is open ('*'): reuse source array length as target array length *)
  3592. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3593. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3594. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3595. IF lastValue = MAX(LONGINT) THEN
  3596. IF IsIntegerValue(sourceArray.length, length) THEN
  3597. lastValue := length - 1;
  3598. isStaticTargetArrayLength := TRUE
  3599. ELSE
  3600. isStaticTargetArrayLength := FALSE
  3601. END
  3602. ELSE
  3603. isStaticTargetArrayLength := TRUE
  3604. END;
  3605. IF isStaticTargetArrayLength THEN
  3606. (* calculate static target array length *)
  3607. IF firstValue > lastValue THEN
  3608. length := 0
  3609. ELSE
  3610. length := 1 + lastValue - firstValue;
  3611. IF length MOD stepValue = 0 THEN
  3612. length := length DIV stepValue
  3613. ELSE
  3614. length := length DIV stepValue + 1
  3615. END
  3616. END;
  3617. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3618. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3619. ASSERT(targetArray.form = SyntaxTree.Static)
  3620. END
  3621. END
  3622. END
  3623. *)
  3624. END;
  3625. SetIndexBaseType(indexDesignator, targetArray)
  3626. END;
  3627. indexDesignator.parameters.AddExpression(indexListItem)
  3628. ELSE
  3629. Error(position, Diagnostics.Invalid,"invalid index list item");
  3630. END;
  3631. END AppendMathIndex;
  3632. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3633. VAR parameters: SyntaxTree.ExpressionList;
  3634. BEGIN
  3635. parameters := index.parameters;
  3636. IF (expression.type = NIL) THEN
  3637. Error(position,Diagnostics.Invalid,"invalid index");
  3638. ELSIF IsIntegerType(expression.type.resolved) THEN
  3639. IF over IS SyntaxTree.ArrayType THEN
  3640. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3641. ELSIF over IS SyntaxTree.StringType THEN
  3642. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3643. END;
  3644. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3645. parameters.AddExpression(expression);
  3646. ELSE
  3647. Error(position,Diagnostics.Invalid,"invalid index");
  3648. END;
  3649. END AppendIndex;
  3650. (** convert an expression to math array type
  3651. if expression is of math array type: return expression itself
  3652. if expression is of array-structured object type: return an index operator call on it
  3653. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3654. otherwise: return invalid expression
  3655. **)
  3656. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3657. VAR
  3658. result: SyntaxTree.Expression;
  3659. mathArrayType: SyntaxTree.MathArrayType;
  3660. BEGIN
  3661. IF expression.type = NIL THEN
  3662. result := SyntaxTree.invalidExpression
  3663. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3664. (* expression of math array type *)
  3665. result := expression
  3666. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3667. (* expression of array-structured object type *)
  3668. mathArrayType := MathArrayStructureOfType(expression.type);
  3669. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3670. ELSE
  3671. result := SyntaxTree.invalidExpression
  3672. END;
  3673. RETURN result
  3674. END ConvertToMathArray;
  3675. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3676. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3677. VAR
  3678. result: SyntaxTree.ExpressionList;
  3679. i: LONGINT;
  3680. BEGIN
  3681. result := SyntaxTree.NewExpressionList();
  3682. FOR i := 1 TO itemCount DO
  3683. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3684. END;
  3685. RETURN result
  3686. END ListOfOpenRanges;
  3687. (** create a procedure call designator for an index operator call on an array-structured object type
  3688. - use given index list as actual parameters
  3689. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3690. **)
  3691. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3692. VAR
  3693. operator: SyntaxTree.Operator;
  3694. expression: SyntaxTree.Expression;
  3695. actualParameters, tempList: SyntaxTree.ExpressionList;
  3696. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3697. result, tempDesignator: SyntaxTree.Designator;
  3698. recordType: SyntaxTree.RecordType;
  3699. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3700. i, hashValue, indexListSize, indexListKind: LONGINT;
  3701. castReturnType: SyntaxTree.MathArrayType;
  3702. BEGIN
  3703. ASSERT(IsArrayStructuredObjectType(left.type));
  3704. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3705. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3706. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3707. indexListSize := indexList.Length();
  3708. indexListKind := 0;
  3709. containsNonRange := FALSE;
  3710. FOR i := 0 TO indexList.Length() - 1 DO
  3711. indexListKind := indexListKind * 2;
  3712. expression := indexList.GetExpression(i);
  3713. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3714. INC(indexListKind)
  3715. ELSE
  3716. containsNonRange := TRUE
  3717. END
  3718. END;
  3719. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3720. (* select applicable index operator
  3721. - try to look up optimal index operator
  3722. - if not present, use operator on ranges
  3723. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3724. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3725. *)
  3726. usesGeneralOperator := FALSE;
  3727. IF rhs # NIL THEN
  3728. (* write operator *)
  3729. IF hashValue = -1 THEN
  3730. operator := NIL
  3731. ELSE
  3732. operator := recordType.arrayAccessOperators.write[hashValue];
  3733. END;
  3734. IF operator = NIL THEN
  3735. usesPureRangeOperator := TRUE;
  3736. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3737. operator := recordType.arrayAccessOperators.generalWrite;
  3738. usesGeneralOperator := TRUE
  3739. ELSE
  3740. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3741. operator := recordType.arrayAccessOperators.write[hashValue];
  3742. END
  3743. END
  3744. ELSE
  3745. (* read operator *)
  3746. IF hashValue = -1 THEN
  3747. operator := NIL
  3748. ELSE
  3749. operator := recordType.arrayAccessOperators.read[hashValue];
  3750. END;
  3751. IF operator = NIL THEN
  3752. usesPureRangeOperator := TRUE;
  3753. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3754. operator := recordType.arrayAccessOperators.generalRead;
  3755. usesGeneralOperator := TRUE
  3756. ELSE
  3757. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3758. operator := recordType.arrayAccessOperators.read[hashValue];
  3759. END
  3760. END
  3761. END;
  3762. IF operator = NIL THEN
  3763. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3764. result := SyntaxTree.invalidDesignator;
  3765. ELSE
  3766. (* determine if reshaping is needed *)
  3767. needsReshaping := containsNonRange & usesPureRangeOperator;
  3768. (* import OCArrayBase if reshaping is needed *)
  3769. IF needsReshaping & ~arrayBaseImported THEN
  3770. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3771. arrayBaseImported := TRUE
  3772. END;
  3773. (* add the index list item to the list of actual parameters
  3774. - for general operators: add a single inline array containing the index list items as parameter
  3775. - otherwise: add all index list items as individual parameters
  3776. *)
  3777. actualParameters := SyntaxTree.NewExpressionList();
  3778. IF usesGeneralOperator THEN
  3779. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3780. END;
  3781. FOR i := 0 TO indexListSize - 1 DO
  3782. expression := indexList.GetExpression(i);
  3783. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3784. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3785. tempList := SyntaxTree.NewExpressionList();
  3786. tempList.AddExpression(expression);
  3787. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3788. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3789. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3790. END;
  3791. IF usesGeneralOperator THEN
  3792. tempMathArrayExpression.elements.AddExpression(expression);
  3793. ELSE
  3794. actualParameters.AddExpression(expression)
  3795. END
  3796. END;
  3797. IF usesGeneralOperator THEN
  3798. actualParameters.AddExpression(tempMathArrayExpression)
  3799. END;
  3800. IF rhs # NIL THEN
  3801. (* add actual parameter for RHS *)
  3802. IF needsReshaping THEN
  3803. (* reshape using OCArrayBase.ExpandDimensions *)
  3804. tempList := SyntaxTree.NewExpressionList();
  3805. (* source array *)
  3806. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3807. tempList.AddExpression(rhs);
  3808. ELSE
  3809. (* convert scalar to one-dimensional array *)
  3810. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3811. tempMathArrayExpression.elements.AddExpression(rhs);
  3812. tempList.AddExpression(tempMathArrayExpression)
  3813. END;
  3814. (* list of kept dimensions *)
  3815. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3816. FOR i := 0 TO indexListSize - 1 DO
  3817. expression := indexList.GetExpression(i);
  3818. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3819. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3820. ELSE
  3821. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3822. END
  3823. END;
  3824. tempList.AddExpression(tempMathArrayExpression);
  3825. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3826. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3827. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3828. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3829. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3830. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3831. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3832. expression.SetType(castReturnType);
  3833. ELSE
  3834. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3835. END;
  3836. actualParameters.AddExpression(expression)
  3837. ELSE
  3838. actualParameters.AddExpression(rhs)
  3839. END
  3840. END;
  3841. (* add dereference operator and create procedure call designator *)
  3842. ASSERT(left IS SyntaxTree.Designator);
  3843. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3844. ASSERT(expression IS SyntaxTree.Designator);
  3845. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3846. IF (rhs = NIL) & needsReshaping THEN
  3847. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3848. tempList := SyntaxTree.NewExpressionList();
  3849. FOR i := 0 TO indexList.Length() - 1 DO
  3850. expression := indexList.GetExpression(i);
  3851. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3852. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3853. ELSE
  3854. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3855. END
  3856. END;
  3857. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3858. END;
  3859. IF rhs = NIL THEN
  3860. (* special rule: index read operator calls are considered to be assignable *)
  3861. result.SetAssignable(TRUE)
  3862. END;
  3863. (* put information about this index operator call into the resulting designator *)
  3864. result.SetRelatedAsot(left);
  3865. result.SetRelatedIndexList(indexList)
  3866. END;
  3867. RETURN result
  3868. END NewIndexOperatorCall;
  3869. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3870. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3871. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3872. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3873. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3874. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3875. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3876. CONST trace = FALSE;
  3877. BEGIN
  3878. IF trace THEN
  3879. FOR i := 0 TO actualParameters.Length()-1 DO
  3880. Printout.Info("par", actualParameters.GetExpression(i));
  3881. END;
  3882. END;
  3883. operator := scope.firstOperator;
  3884. WHILE(operator # NIL) DO
  3885. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3886. procedureType := operator.type(SyntaxTree.ProcedureType);
  3887. distance := Distance(system, procedureType,actualParameters);
  3888. IF trace THEN Printout.Info("check op ",operator) END;
  3889. IF distance < bestDistance THEN
  3890. IF trace THEN Printout.Info("taken op",operator) END;
  3891. bestDistance := distance;
  3892. bestOperator := operator;
  3893. END;
  3894. END;
  3895. operator := operator.nextOperator;
  3896. END;
  3897. END FindInScope;
  3898. BEGIN
  3899. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3900. IF oper = 0 THEN (* index *)
  3901. identifier := SyntaxTree.NewIdentifier("[]");
  3902. ELSE
  3903. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3904. END;
  3905. WHILE (recordType # NIL) DO
  3906. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3907. recordType := recordType.GetBaseRecord();
  3908. END;
  3909. RETURN bestOperator
  3910. END FindOperator;
  3911. BEGIN
  3912. type := left.type.resolved;
  3913. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3914. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3915. actualParameters := SyntaxTree.NewExpressionList();
  3916. IF parameters # NIL THEN
  3917. FOR i := 0 TO parameters.Length()-1 DO
  3918. expression := ResolveExpression(parameters.GetExpression(i));
  3919. actualParameters.AddExpression(expression);
  3920. END;
  3921. END;
  3922. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3923. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3924. IF op # NIL THEN
  3925. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3926. ASSERT(expression IS SyntaxTree.Designator);
  3927. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3928. result.SetRelatedAsot(left);
  3929. result.SetRelatedIndexList(parameters);
  3930. (* check if write operator exists, for var parameters *)
  3931. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3932. actualParameters := SyntaxTree.NewExpressionList();
  3933. FOR i := 0 TO parameters.Length()-1 DO
  3934. expression := ResolveExpression(parameters.GetExpression(i));
  3935. actualParameters.AddExpression(expression);
  3936. END;
  3937. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3938. actualParameters.AddExpression(rhs);
  3939. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3940. IF op = NIL THEN rhs := NIL END;
  3941. END;
  3942. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3943. ELSE
  3944. result := NIL;
  3945. END;
  3946. RETURN result;
  3947. END NewObjectOperatorCall;
  3948. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3949. 1. convert bracket designator chains into a single one that contains separators
  3950. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3951. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3952. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3953. - if an array or math array is indexed over, create index designator
  3954. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3955. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3956. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3957. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3958. - if an array-structured object type is indexed over, create procedure call designator
  3959. e.g.: a[x, y] -> a^."[]"(x, y)
  3960. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3961. - a[i, *] = a[i][*]
  3962. - a[*, i] # a[*][i]
  3963. Because:
  3964. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3965. - 'i-th column' = a[*, i]
  3966. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3967. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3968. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3969. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3970. **)
  3971. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3972. VAR
  3973. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3974. indexDesignator: SyntaxTree.IndexDesignator;
  3975. designator: SyntaxTree.Designator;
  3976. type: SyntaxTree.Type;
  3977. recordType: SyntaxTree.RecordType;
  3978. expression, rhs: SyntaxTree.Expression;
  3979. indexList: SyntaxTree.ExpressionList;
  3980. i: LONGINT;
  3981. hasError, done: BOOLEAN;
  3982. PROCEDURE FinalizeIndexDesignator;
  3983. BEGIN
  3984. IF indexDesignator # NIL THEN
  3985. (* the end of a tensor has been reached: *)
  3986. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3987. SetIndexBaseType(indexDesignator, type);
  3988. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3989. designator := indexDesignator;
  3990. type := designator.type.resolved;
  3991. indexDesignator := NIL;
  3992. ASSERT(SyntaxTree.Resolved IN type.state)
  3993. END
  3994. END FinalizeIndexDesignator;
  3995. BEGIN
  3996. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3997. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3998. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3999. (* copy all index list entries including a separator to the left bracket designator *)
  4000. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4001. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4002. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4003. END;
  4004. (* propagate the related RHS *)
  4005. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4006. (* only resolve left bracket designator and use as final result *)
  4007. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4008. ELSE
  4009. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4010. designator := ResolveDesignator(bracketDesignator.left);
  4011. type := designator.type.resolved;
  4012. indexDesignator := NIL;
  4013. (*!!! clean up *)
  4014. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4015. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4016. IF resolvedExpression = NIL THEN
  4017. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4018. resolvedExpression := SyntaxTree.invalidDesignator
  4019. END;
  4020. RETURN;
  4021. END;
  4022. i := 0;
  4023. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4024. expression := bracketDesignator.parameters.GetExpression(i);
  4025. expression := ResolveExpression(expression);
  4026. bracketDesignator.parameters.SetExpression(i, expression);
  4027. IF expression = SyntaxTree.indexListSeparator THEN
  4028. (* finalize an existing index designator if needed *)
  4029. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4030. INC(i)
  4031. ELSE
  4032. (* do auto-dereferencing if needed *)
  4033. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4034. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4035. & (i=0)*)
  4036. THEN
  4037. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4038. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4039. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4040. designator := SyntaxTree.invalidDesignator;
  4041. type := SyntaxTree.invalidType
  4042. ELSE
  4043. FinalizeIndexDesignator;
  4044. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4045. type := designator.type.resolved
  4046. END
  4047. END;
  4048. (* create a new index designator, if needed *)
  4049. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4050. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4051. indexDesignator.SetAssignable(designator.assignable);
  4052. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4053. (* designator := indexDesignator *)
  4054. END;
  4055. IF type = SyntaxTree.invalidType THEN
  4056. (* error already handled *)
  4057. INC(i)
  4058. ELSIF type IS SyntaxTree.ArrayType THEN
  4059. (* indexing over an array *)
  4060. ASSERT(indexDesignator # NIL);
  4061. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4062. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4063. INC(i)
  4064. ELSIF type IS SyntaxTree.StringType THEN
  4065. (* indexing over an array *)
  4066. ASSERT(indexDesignator # NIL);
  4067. AppendIndex(expression.position, indexDesignator, expression, type);
  4068. type := type(SyntaxTree.StringType).baseType.resolved;
  4069. INC(i)
  4070. ELSIF type IS SyntaxTree.MathArrayType THEN
  4071. (* indexing over a math array *)
  4072. ASSERT(indexDesignator # NIL);
  4073. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4074. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4075. INC(i)
  4076. ELSIF IsArrayStructuredObjectType(type) THEN
  4077. (* indexing over ASOTs *)
  4078. FinalizeIndexDesignator;
  4079. ASSERT(type IS SyntaxTree.PointerType);
  4080. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4081. (*
  4082. - collect index list items from bracket designator that belong to ASOT
  4083. - check for errors
  4084. *)
  4085. indexList := SyntaxTree.NewExpressionList();
  4086. hasError := FALSE;
  4087. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4088. (* indexing over tensor ASOT:
  4089. - stop at index list end or separator
  4090. - dimensionality is given by number of index list items
  4091. *)
  4092. done := FALSE;
  4093. WHILE ~done DO
  4094. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4095. done := TRUE;
  4096. ELSE
  4097. expression := bracketDesignator.parameters.GetExpression(i);
  4098. IF expression = SyntaxTree.indexListSeparator THEN
  4099. done := TRUE;
  4100. ELSE
  4101. expression := ResolveExpression(expression);
  4102. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4103. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4104. hasError := TRUE
  4105. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4106. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4107. expression := SyntaxTree.invalidExpression;
  4108. hasError := TRUE
  4109. END;
  4110. indexList.AddExpression(expression)
  4111. END;
  4112. INC(i)
  4113. END
  4114. END
  4115. ELSE
  4116. (* indexing over non-tensor ASOT:
  4117. - ignore separators
  4118. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4119. *)
  4120. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4121. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4122. expression := bracketDesignator.parameters.GetExpression(i);
  4123. ELSE
  4124. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4125. END;
  4126. IF expression # SyntaxTree.indexListSeparator THEN
  4127. expression := ResolveExpression(expression);
  4128. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4129. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4130. expression := SyntaxTree.invalidExpression;
  4131. hasError := TRUE
  4132. END;
  4133. indexList.AddExpression(expression)
  4134. END;
  4135. INC(i)
  4136. END;
  4137. END;
  4138. IF hasError THEN
  4139. designator := SyntaxTree.invalidDesignator;
  4140. type := SyntaxTree.invalidType;
  4141. ELSE
  4142. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4143. and the last entry in the index list belongs to the array-structured object type in question.
  4144. E.g.: for a 2-dimensional array-structured object type:
  4145. - 'lhs := asot[1, 2]' -> read mode
  4146. - 'asot[1, 2] := rhs' -> write mode
  4147. - 'asot[1, 2, 3] := rhs' -> read mode
  4148. *)
  4149. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4150. rhs := bracketDesignator.relatedRhs
  4151. ELSE
  4152. rhs := NIL
  4153. END;
  4154. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4155. type := designator.type
  4156. END
  4157. ELSE
  4158. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4159. designator := SyntaxTree.invalidDesignator;
  4160. type := SyntaxTree.invalidType;
  4161. INC(i)
  4162. END
  4163. END
  4164. END;
  4165. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4166. resolvedExpression := designator
  4167. END
  4168. END VisitBracketDesignator;
  4169. (** check and resolve expression list
  4170. - resolve each expression in an expression list
  4171. - returns true if and only if all statements could have successfully been resolved
  4172. **)
  4173. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4174. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4175. BEGIN
  4176. result := TRUE;
  4177. FOR i := 0 TO expressionList.Length()-1 DO
  4178. expression := ResolveExpression(expressionList.GetExpression(i));
  4179. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4180. expressionList.SetExpression(i,expression);
  4181. END;
  4182. RETURN result
  4183. END ExpressionList;
  4184. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4185. BEGIN
  4186. type := type.resolved;
  4187. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4188. RETURN TRUE
  4189. ELSIF system.CanPassInRegister # NIL THEN
  4190. RETURN system.CanPassInRegister(type);
  4191. ELSE
  4192. RETURN FALSE
  4193. END;
  4194. END CanPassInRegister;
  4195. (** return procedure call designator left(actualParameters)
  4196. - check realtime procedure call in realtime procedure
  4197. - check number of parameters
  4198. - check parameter compatibility
  4199. return invalidDesignator if error
  4200. **)
  4201. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4202. VAR result: SyntaxTree.Designator;
  4203. numberFormalParameters, numberActualParameters: LONGINT;
  4204. formalType: SyntaxTree.ProcedureType;
  4205. formalParameter: SyntaxTree.Parameter;
  4206. actualParameter: SyntaxTree.Expression;
  4207. i: LONGINT;
  4208. BEGIN
  4209. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4210. result := SyntaxTree.invalidDesignator;
  4211. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4212. numberFormalParameters := formalType.numberParameters;
  4213. numberActualParameters := actualParameters.Length();
  4214. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4215. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4216. END;
  4217. IF ~ExpressionList(actualParameters) THEN
  4218. result := SyntaxTree.invalidDesignator
  4219. ELSE
  4220. IF numberActualParameters <= numberFormalParameters THEN
  4221. formalParameter := formalType.firstParameter;
  4222. FOR i := 0 TO numberActualParameters-1 DO
  4223. actualParameter := actualParameters.GetExpression(i);
  4224. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4225. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4226. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4227. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4228. ELSE
  4229. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4230. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4231. END;
  4232. actualParameters.SetExpression(i,actualParameter);
  4233. END;
  4234. formalParameter := formalParameter.nextParameter;
  4235. END;
  4236. WHILE (formalParameter # NIL) DO
  4237. IF formalParameter.defaultValue # NIL THEN
  4238. actualParameters.AddExpression(formalParameter.defaultValue);
  4239. formalParameter := formalParameter.nextParameter
  4240. ELSE
  4241. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4242. formalParameter := NIL;
  4243. END;
  4244. END;
  4245. ELSE
  4246. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4247. END;
  4248. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4249. result.SetAssignable(FALSE);
  4250. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4251. END;
  4252. RETURN result
  4253. END NewProcedureCallDesignator;
  4254. (**
  4255. builtin call designator generated in VisitParameterDesignator
  4256. -> nothing to be resolved
  4257. **)
  4258. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4259. BEGIN
  4260. resolvedExpression := x;
  4261. END VisitTypeGuardDesignator;
  4262. (**
  4263. builtin call designator generated in VisitParameterDesignator
  4264. -> nothing to be resolved
  4265. **)
  4266. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4267. BEGIN
  4268. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4269. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4270. ASSERT(resolvedExpression.type # NIL);
  4271. ELSIF ExpressionList(x.parameters) THEN
  4272. resolvedExpression := x;
  4273. END;
  4274. END VisitBuiltinCallDesignator;
  4275. (**
  4276. procedure call designator generated in VisitParameterDesignator
  4277. -> nothing to be resolved
  4278. **)
  4279. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4280. BEGIN
  4281. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4282. resolvedExpression := x;
  4283. END VisitProcedureCallDesignator;
  4284. (** return true if x is a variable else return false and report error **)
  4285. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4286. VAR result: BOOLEAN;
  4287. BEGIN
  4288. result := TRUE;
  4289. IF x = SyntaxTree.invalidExpression THEN
  4290. result := FALSE;
  4291. ELSIF ~IsVariable(x) THEN
  4292. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4293. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4294. result := FALSE;
  4295. END;
  4296. RETURN result
  4297. END CheckVariable;
  4298. (**
  4299. if expression x is of basic type then return true else report error and return false
  4300. **)
  4301. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4302. VAR result: BOOLEAN;
  4303. BEGIN
  4304. result := FALSE;
  4305. IF x = SyntaxTree.invalidExpression THEN
  4306. ELSIF ~IsBasicType(x.type) THEN
  4307. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4308. result := FALSE
  4309. ELSE result := TRUE
  4310. END;
  4311. RETURN result
  4312. END CheckBasicType;
  4313. (**
  4314. if expression x is of number type then return true else report error and return false
  4315. **)
  4316. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN;
  4318. BEGIN
  4319. result := FALSE;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is non number type");
  4323. ELSE result := TRUE
  4324. END;
  4325. RETURN result
  4326. END CheckNumberType;
  4327. (**
  4328. if expression x is of number or size type but not complex then return true else report error and return false
  4329. **)
  4330. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4331. VAR result: BOOLEAN;
  4332. BEGIN
  4333. result := FALSE;
  4334. IF x = SyntaxTree.invalidExpression THEN
  4335. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4336. Error(x.position,Diagnostics.Invalid,"is complex type");
  4337. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4338. Error(x.position,Diagnostics.Invalid,"is non number type");
  4339. ELSE result := TRUE
  4340. END;
  4341. RETURN result
  4342. END CheckNonComplexNumberSizeType;
  4343. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4344. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4345. BEGIN
  4346. result := FALSE; type := x.type.resolved;
  4347. IF x = SyntaxTree.invalidExpression THEN
  4348. 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
  4349. TRACE(type.sizeInBits);
  4350. TRACE(system.addressType.sizeInBits);
  4351. Error(x.position,Diagnostics.Invalid,"is no address type");
  4352. ELSE result := TRUE
  4353. END;
  4354. RETURN result
  4355. END CheckAddressType;
  4356. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4357. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4358. BEGIN
  4359. result := FALSE; type := x.type.resolved;
  4360. IF x = SyntaxTree.invalidExpression THEN
  4361. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4362. Error(x.position,Diagnostics.Invalid,"is no size type");
  4363. ELSE result := TRUE
  4364. END;
  4365. RETURN result
  4366. END CheckSizeType;
  4367. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4369. BEGIN
  4370. result := FALSE; type := x.type.resolved;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. 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
  4373. Error(x.position,Diagnostics.Invalid,"is no object type");
  4374. ELSE result := TRUE
  4375. END;
  4376. RETURN result
  4377. END CheckObjectType;
  4378. (**
  4379. if expression x is of integer type then return true else report error and return false
  4380. **)
  4381. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4382. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4383. BEGIN
  4384. result := FALSE; type := x.type.resolved;
  4385. IF x = SyntaxTree.invalidExpression THEN
  4386. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4387. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4388. ELSE result := TRUE
  4389. END;
  4390. RETURN result
  4391. END CheckIntegerType;
  4392. (**
  4393. if expression x is of character type then return true else report error and return false
  4394. **)
  4395. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN;
  4397. BEGIN
  4398. result := FALSE;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4401. Error(x.position,Diagnostics.Invalid,"is no character type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckCharacterType;
  4406. (**
  4407. if expression x is of real type then return true else report error and return false
  4408. **)
  4409. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN;
  4411. BEGIN
  4412. result := FALSE;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4415. Error(x.position,Diagnostics.Invalid,"is no float type");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckRealType;
  4420. (**
  4421. if expression x is of range type then return true else report error and return false
  4422. **)
  4423. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4424. VAR result: BOOLEAN;
  4425. BEGIN
  4426. result := FALSE;
  4427. IF x = SyntaxTree.invalidExpression THEN
  4428. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4429. Error(x.position,Diagnostics.Invalid,"is no range type");
  4430. ELSE result := TRUE
  4431. END;
  4432. RETURN result
  4433. END CheckRangeType;
  4434. (**
  4435. if expression x is of boolean type then return true else report error and return false
  4436. **)
  4437. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4438. VAR result: BOOLEAN;
  4439. BEGIN
  4440. result := FALSE;
  4441. IF x = SyntaxTree.invalidExpression THEN
  4442. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4443. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4444. ELSE result := TRUE
  4445. END;
  4446. RETURN result
  4447. END CheckBooleanType;
  4448. (**
  4449. if expression x is of set type then return true else report error and return false
  4450. **)
  4451. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4452. VAR result: BOOLEAN;
  4453. BEGIN
  4454. result := FALSE;
  4455. IF x = SyntaxTree.invalidExpression THEN
  4456. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4457. Error(x.position,Diagnostics.Invalid,"is no set type");
  4458. ELSE result := TRUE
  4459. END;
  4460. RETURN result
  4461. END CheckSetType;
  4462. (**
  4463. if expression x is of string or array of character type then return true else report error and return false
  4464. **)
  4465. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4466. VAR result: BOOLEAN;
  4467. BEGIN
  4468. result := FALSE;
  4469. IF x = SyntaxTree.invalidExpression THEN
  4470. ELSIF ~IsStringType(x.type.resolved) THEN
  4471. Error(x.position,Diagnostics.Invalid,"is no string type");
  4472. ELSE result := TRUE
  4473. END;
  4474. RETURN result
  4475. END CheckStringType;
  4476. (**
  4477. if expression x is a type declaration type return true else report error and return false
  4478. **)
  4479. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4480. VAR result: BOOLEAN;
  4481. BEGIN
  4482. result := FALSE;
  4483. IF x = SyntaxTree.invalidExpression THEN
  4484. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4485. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4486. ELSE result := TRUE
  4487. END;
  4488. RETURN result
  4489. END CheckTypeDeclarationType;
  4490. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4496. result := TRUE;
  4497. value := x.resolved(SyntaxTree.IntegerValue).value;
  4498. ELSE
  4499. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4500. END;
  4501. RETURN result;
  4502. END CheckIntegerValue;
  4503. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4509. result := TRUE;
  4510. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4511. ELSE
  4512. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4513. END;
  4514. RETURN result;
  4515. END CheckStringValue;
  4516. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4517. BEGIN
  4518. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4519. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4520. ELSE
  4521. RETURN FALSE
  4522. END;
  4523. END IsUnsignedValue;
  4524. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4525. BEGIN
  4526. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4527. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4528. ELSE
  4529. RETURN FALSE
  4530. END
  4531. END IsAddressValue;
  4532. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4533. BEGIN
  4534. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4535. END IsAddressExpression;
  4536. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4537. BEGIN
  4538. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4539. END IsSizeExpression;
  4540. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4541. VAR result: BOOLEAN;
  4542. BEGIN
  4543. result := FALSE;
  4544. IF x = SyntaxTree.invalidExpression THEN
  4545. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4546. result := TRUE;
  4547. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4548. ELSE
  4549. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4550. END;
  4551. RETURN result;
  4552. END CheckEnumerationValue;
  4553. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4554. VAR result: BOOLEAN;
  4555. BEGIN
  4556. result := FALSE;
  4557. IF x = SyntaxTree.invalidExpression THEN
  4558. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4559. result := TRUE;
  4560. value := x.resolved(SyntaxTree.CharacterValue).value;
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4562. result := TRUE;
  4563. value := x.resolved(SyntaxTree.StringValue).value[0];
  4564. ELSE
  4565. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckCharacterValue;
  4569. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4570. VAR result: BOOLEAN;
  4571. BEGIN
  4572. result := FALSE;
  4573. IF x = SyntaxTree.invalidExpression THEN
  4574. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4575. value := x.resolved(SyntaxTree.IntegerValue).value;
  4576. IF (value > 0) OR includeZero & (value = 0) THEN
  4577. result := TRUE;
  4578. ELSE
  4579. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4580. END
  4581. ELSE
  4582. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4583. END;
  4584. RETURN result;
  4585. END CheckPositiveIntegerValue;
  4586. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4587. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4588. BEGIN
  4589. result := FALSE;
  4590. IF x = SyntaxTree.invalidExpression THEN
  4591. ELSE
  4592. type := x.type.resolved;
  4593. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4594. portType := type(SyntaxTree.PortType);
  4595. result := TRUE
  4596. ELSE
  4597. Error(x.position,Diagnostics.Invalid,"no port type");
  4598. END;
  4599. END;
  4600. RETURN result
  4601. END CheckPortType;
  4602. (* move to builtin procedure call statement ?
  4603. remove builtin procedure call designator ?
  4604. *)
  4605. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4606. VAR
  4607. numberActualParameters,numberFormalParameters: LONGINT;
  4608. formalParameter: SyntaxTree.Parameter;
  4609. actualParameter: SyntaxTree.Expression;
  4610. procedureType: SyntaxTree.ProcedureType;
  4611. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4612. inPort, outPort: SyntaxTree.PortType;
  4613. constructor: SyntaxTree.Procedure;
  4614. type0,type1,type2: SyntaxTree.Type;
  4615. type,base,parameterType: SyntaxTree.Type;
  4616. arrayType: SyntaxTree.ArrayType;
  4617. i,i0,i1: LONGINT;
  4618. r,r0,r1,im: LONGREAL;
  4619. c: CHAR;
  4620. id: LONGINT;
  4621. b: BOOLEAN;
  4622. first: LONGINT;
  4623. mathArrayType: SyntaxTree.MathArrayType;
  4624. customBuiltin: SyntaxTree.CustomBuiltin;
  4625. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4626. VAR resultB: BOOLEAN;
  4627. BEGIN
  4628. IF numberActualParameters < from THEN
  4629. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4630. result := SyntaxTree.invalidExpression;
  4631. resultB := FALSE;
  4632. ELSIF numberActualParameters > to THEN
  4633. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4634. result := SyntaxTree.invalidExpression;
  4635. resultB := FALSE;
  4636. ELSE
  4637. resultB := TRUE;
  4638. END;
  4639. RETURN resultB
  4640. END CheckArity;
  4641. BEGIN
  4642. type := NIL; result := NIL;
  4643. type0 := NIL; type1 := NIL; type2 := NIL;
  4644. numberActualParameters := actualParameters.Length();
  4645. IF numberActualParameters>0 THEN
  4646. parameter0 := actualParameters.GetExpression(0);
  4647. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4648. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4649. result := SyntaxTree.invalidExpression
  4650. END
  4651. END;
  4652. IF numberActualParameters >1 THEN
  4653. parameter1 := actualParameters.GetExpression(1);
  4654. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4655. ELSE
  4656. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4657. result := SyntaxTree.invalidExpression
  4658. END
  4659. END;
  4660. IF numberActualParameters >2 THEN
  4661. parameter2 := actualParameters.GetExpression(2);
  4662. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4663. ELSE
  4664. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4665. result := SyntaxTree.invalidExpression
  4666. END
  4667. END;
  4668. IF returnType # NIL THEN
  4669. id := Global.New;
  4670. result := NIL;
  4671. ELSE
  4672. id := builtin.id;
  4673. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4674. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4675. END;
  4676. END;
  4677. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4678. ELSIF result # NIL THEN type := result.type (* operator *)
  4679. ELSE
  4680. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4681. result(SyntaxTree.Designator).SetLeft(left);
  4682. IF returnType # NIL THEN
  4683. type := returnType;
  4684. END;
  4685. (* ---- ASSERT ----- *)
  4686. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4687. IF CheckBooleanType(parameter0) THEN
  4688. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4689. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4690. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4691. *)
  4692. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4693. Error(position,Diagnostics.Invalid,"assert failed");
  4694. END;
  4695. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4696. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4697. rules imposed by the architecture / current runtime
  4698. *)
  4699. END;
  4700. END;
  4701. (* ---- COPY ----- *)
  4702. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4703. IF~IsStringType(type0) THEN
  4704. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4705. END;
  4706. IF ~IsStringType(type1) THEN
  4707. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4708. ELSIF CheckVariable(parameter1) THEN
  4709. IF (type0 IS SyntaxTree.StringType) THEN
  4710. arrayType := type1(SyntaxTree.ArrayType);
  4711. IF arrayType.form = SyntaxTree.Static THEN
  4712. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4713. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4714. END;
  4715. END;
  4716. END;
  4717. END;
  4718. (* ---- INC, DEC----- *)
  4719. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4720. IF numberActualParameters = 1 THEN
  4721. parameter1 :=Global.NewIntegerValue(system,position,1);
  4722. actualParameters.AddExpression(parameter1);
  4723. END;
  4724. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4725. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4726. Error(position,Diagnostics.Invalid,"incompatible increment");
  4727. ELSE
  4728. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4729. actualParameters.SetExpression(1,parameter1);
  4730. END;
  4731. END;
  4732. (* ---- EXCL, INCL----- *)
  4733. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4734. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4735. IF IsIntegerValue(parameter1,i0) THEN
  4736. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4737. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4738. END;
  4739. END;
  4740. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4741. actualParameters.SetExpression(1,parameter1);
  4742. END;
  4743. (* ---- HALT, SYSTEM.HALT ----- *)
  4744. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4745. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4746. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4747. rules imposed by the architecture / current runtime
  4748. *)
  4749. END;
  4750. (* ---- WAIT ----- *)
  4751. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4752. IF CheckObjectType(parameter0) THEN
  4753. END;
  4754. (* ---- NEW ----- *)
  4755. ELSIF (id = Global.New) THEN
  4756. IF returnType # NIL THEN
  4757. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4758. ELSE
  4759. first := 1;
  4760. END;
  4761. IF CheckArity(first,Infinity) THEN
  4762. IF currentIsRealtime THEN
  4763. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4764. END;
  4765. (* check constructor *)
  4766. IF (first =0) OR CheckVariable(parameter0) THEN
  4767. IF type0 IS SyntaxTree.PointerType THEN
  4768. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4769. ELSIF type0 IS SyntaxTree.CellType THEN
  4770. ELSE
  4771. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4772. END;
  4773. IF type0 IS SyntaxTree.ArrayType THEN
  4774. arrayType := type0(SyntaxTree.ArrayType);
  4775. IF arrayType.form = SyntaxTree.Static THEN
  4776. i := first
  4777. ELSIF arrayType.form = SyntaxTree.Open THEN
  4778. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4779. ELSE HALT(100)
  4780. END;
  4781. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4782. i := first;
  4783. REPEAT
  4784. actualParameter := actualParameters.GetExpression(i);
  4785. IF CheckSizeType(actualParameter) THEN
  4786. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4787. actualParameters.SetExpression(i,actualParameter);
  4788. END;
  4789. INC(i);
  4790. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4791. END;
  4792. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4793. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4794. IF constructor = NIL THEN
  4795. IF CheckArity(first,first) THEN END;
  4796. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4797. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4798. ELSE
  4799. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4800. numberFormalParameters := procedureType.numberParameters;
  4801. IF numberActualParameters-first <= numberFormalParameters THEN
  4802. formalParameter := procedureType.firstParameter;
  4803. FOR i := first TO numberActualParameters-1 DO
  4804. actualParameter := actualParameters.GetExpression(i);
  4805. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4806. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4807. ELSE
  4808. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4809. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4810. END;
  4811. actualParameters.SetExpression(i,actualParameter);
  4812. END;
  4813. formalParameter := formalParameter.nextParameter;
  4814. END;
  4815. WHILE (formalParameter # NIL) DO
  4816. IF formalParameter.defaultValue # NIL THEN
  4817. actualParameters.AddExpression(formalParameter.defaultValue);
  4818. formalParameter := formalParameter.nextParameter
  4819. ELSE
  4820. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4821. formalParameter := NIL;
  4822. END;
  4823. END;
  4824. ELSE
  4825. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4826. END;
  4827. END;
  4828. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4829. mathArrayType := type0(SyntaxTree.MathArrayType);
  4830. IF mathArrayType.form = SyntaxTree.Static THEN
  4831. Error(position,Diagnostics.Invalid,"new on static array");
  4832. ELSE
  4833. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4834. i0 := first+1; i1 := Infinity;
  4835. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4836. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4837. i1 := i0;
  4838. ELSE HALT(100);
  4839. END;
  4840. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4841. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4842. base := ArrayBase(type0,MAX(LONGINT));
  4843. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4844. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4845. IF ~CompatibleTo(system,type0,parameterType) THEN
  4846. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4847. result := SyntaxTree.invalidExpression;
  4848. ELSE
  4849. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4850. END;
  4851. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4852. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4853. IF ~CompatibleTo(system,type1,parameterType) THEN
  4854. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4855. result := SyntaxTree.invalidExpression;
  4856. ELSE
  4857. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4858. END;
  4859. ELSE
  4860. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4861. i := first;
  4862. REPEAT
  4863. actualParameter := actualParameters.GetExpression(i);
  4864. IF CheckSizeType(actualParameter) THEN
  4865. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4866. actualParameters.SetExpression(i,actualParameter);
  4867. END;
  4868. INC(i);
  4869. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4870. END;
  4871. END;
  4872. END;
  4873. ELSIF type0 IS SyntaxTree.CellType THEN
  4874. IF ~(currentIsCellNet) THEN
  4875. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4876. ELSE
  4877. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4878. IF (constructor = NIL) & CheckArity(1,1) THEN
  4879. (* ok *)
  4880. ELSE
  4881. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4882. numberFormalParameters := procedureType.numberParameters;
  4883. DEC(numberActualParameters);
  4884. IF numberActualParameters <= numberFormalParameters THEN
  4885. formalParameter := procedureType.firstParameter;
  4886. FOR i := first TO numberActualParameters DO
  4887. actualParameter := actualParameters.GetExpression(i);
  4888. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4889. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4890. ELSE
  4891. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4892. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4893. END;
  4894. actualParameters.SetExpression(i,actualParameter);
  4895. END;
  4896. formalParameter := formalParameter.nextParameter;
  4897. END;
  4898. WHILE (formalParameter # NIL) DO
  4899. IF formalParameter.defaultValue # NIL THEN
  4900. actualParameters.AddExpression(formalParameter.defaultValue);
  4901. formalParameter := formalParameter.nextParameter
  4902. ELSE
  4903. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4904. formalParameter := NIL;
  4905. END;
  4906. END;
  4907. ELSE
  4908. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4909. END;
  4910. END;
  4911. END;
  4912. activeCellsStatement := TRUE;
  4913. ELSE
  4914. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4915. END;
  4916. END;
  4917. END;
  4918. (* ---- DISPOSE ----- *)
  4919. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4920. IF ~IsPointerType(parameter0.type) THEN
  4921. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4922. ELSIF ~IsDisposable(parameter0.type) THEN
  4923. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4924. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4925. END
  4926. (* ---- GETPROCEDURE ----- *)
  4927. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4928. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4929. IF CheckVariable(parameter2) THEN
  4930. IF ~GetProcedureAllowed(parameter2.type) THEN
  4931. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4932. END;
  4933. END;
  4934. END;
  4935. (* ---- ABS ----- *)
  4936. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4937. (* note: ABS on complex numbers is done using overloading *)
  4938. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4939. type := type0;
  4940. IF IsIntegerValue(parameter0,i0) THEN
  4941. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4942. type := Global.GetIntegerType(system,ABS(i0));
  4943. ELSIF IsRealValue(parameter0,r) THEN
  4944. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4945. END;
  4946. ELSE
  4947. type := SyntaxTree.invalidType;
  4948. END;
  4949. (* ---- ASH, ASR ----- *)
  4950. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4951. type := type0;
  4952. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4953. (*
  4954. ConvertOperands(parameter0,parameter1); (* same type *)
  4955. *)
  4956. type := parameter0.type;
  4957. IF IsIntegerValue(parameter0,i0) THEN
  4958. IF IsIntegerValue(parameter1,i1) THEN
  4959. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4960. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4961. result := ResolveExpression(result);
  4962. type := Global.GetIntegerType(system,i0);
  4963. END;
  4964. END;
  4965. IF type.resolved.sizeInBits < 32 THEN
  4966. type := system.longintType;
  4967. END;
  4968. (*!compatibility with release, remove when resolved
  4969. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4970. *)
  4971. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4972. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4973. actualParameters.SetExpression(0,parameter0);
  4974. actualParameters.SetExpression(1,parameter1);
  4975. END;
  4976. (* ---- CAP ----- *)
  4977. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4978. type := system.characterType;
  4979. IF CheckCharacterType (parameter0) THEN
  4980. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4981. actualParameters.SetExpression(0,parameter0);
  4982. IF IsCharacterValue(parameter0,c) THEN
  4983. IF (c <= "z") & (c >= "a") THEN
  4984. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4985. ELSE
  4986. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4987. END;
  4988. END;
  4989. END;
  4990. (* ---- CHR ----- *)
  4991. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4992. IF id = Global.Chr THEN
  4993. type := system.characterType
  4994. ELSE
  4995. type := system.characterType32
  4996. END;
  4997. IF CheckIntegerType(parameter0) THEN
  4998. IF IsIntegerValue(parameter0,i0) THEN
  4999. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5000. result := ResolveExpression(result);
  5001. ELSE
  5002. (*
  5003. result := NewConversion(parameter0.position,parameter0,type);
  5004. *)
  5005. END;
  5006. END
  5007. (* ---- ENTIER ----- *)
  5008. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5009. type := system.longintType;
  5010. IF CheckRealType(parameter0) THEN
  5011. IF IsRealValue(parameter0,r) THEN
  5012. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5013. type := Global.GetIntegerType(system,ENTIER(r));
  5014. END
  5015. END;
  5016. (* ---- ENTIERH ----- *)
  5017. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5018. type := system.hugeintType;
  5019. IF CheckRealType(parameter0) THEN
  5020. IF IsRealValue(parameter0,r) THEN
  5021. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5022. END
  5023. END;
  5024. (* ---- LEN ----- *)
  5025. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5026. type := system.longintType;
  5027. base := type0;
  5028. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5029. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5030. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5031. END;
  5032. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5033. actualParameters.SetExpression(0,parameter0);
  5034. type0 := parameter0.type.resolved;
  5035. base := type0;
  5036. END;
  5037. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5038. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5039. IF i1 < 0 THEN
  5040. Error(position,Diagnostics.Invalid,"invalid dimension");
  5041. base := SyntaxTree.invalidType;
  5042. ELSE
  5043. base := ArrayBase(base,i1);
  5044. IF (base # NIL) & Indexable(base) THEN
  5045. ELSE
  5046. Error(position,Diagnostics.Invalid,"len on no array");
  5047. IF VerboseErrorMessage THEN
  5048. Printout.Info("base",base);
  5049. END;
  5050. base := SyntaxTree.invalidType;
  5051. END;
  5052. END;
  5053. IF numberActualParameters=2 THEN
  5054. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5055. actualParameters.SetExpression(1,parameter1);
  5056. ELSIF base IS SyntaxTree.MathArrayType THEN
  5057. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5058. END;
  5059. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5060. IF base IS SyntaxTree.ArrayType THEN
  5061. arrayType := base(SyntaxTree.ArrayType);
  5062. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5063. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5064. result := Global.NewIntegerValue(system,position,i);
  5065. type := result.type;(* arrayType.length.type;*)
  5066. ASSERT(type # NIL);
  5067. END;
  5068. ELSIF base IS SyntaxTree.MathArrayType THEN
  5069. mathArrayType := base(SyntaxTree.MathArrayType);
  5070. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5071. result := Global.NewIntegerValue(system,position,i);
  5072. type := result.type;
  5073. (*
  5074. type := mathArrayType.length.type;
  5075. *)
  5076. ASSERT(type # NIL);
  5077. END;
  5078. END;
  5079. END;
  5080. ELSE
  5081. type := system.longintType;
  5082. END;
  5083. (* ---- FIRST ---- *)
  5084. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5085. type := system.longintType;
  5086. IF CheckRangeType(parameter0) THEN END;
  5087. result.SetAssignable(parameter0.assignable)
  5088. (* ---- LAST ---- *)
  5089. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5090. type := system.longintType;
  5091. IF CheckRangeType(parameter0) THEN END;
  5092. result.SetAssignable(parameter0.assignable)
  5093. (* ---- STEP ---- *)
  5094. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5095. type := system.longintType;
  5096. IF CheckRangeType(parameter0) THEN END;
  5097. result.SetAssignable(parameter0.assignable)
  5098. (* ---- RE ---- *)
  5099. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5100. IF CheckNumberType(parameter0) THEN
  5101. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5102. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5103. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5104. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5105. type := parameter0.type
  5106. ELSE
  5107. type := system.realType
  5108. END
  5109. END;
  5110. result.SetAssignable(parameter0.assignable)
  5111. (* ---- IM ---- *)
  5112. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5113. IF CheckNumberType(parameter0) THEN
  5114. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5115. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5116. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5117. ELSE
  5118. type := system.realType;
  5119. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5120. END
  5121. END;
  5122. result.SetAssignable(parameter0.assignable)
  5123. (* ---- MAX ----- *)
  5124. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5125. IF numberActualParameters = 1 THEN
  5126. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5127. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5128. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5129. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5130. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5131. *)
  5132. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5133. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5134. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5135. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5136. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5137. END;
  5138. ELSE
  5139. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5140. END
  5141. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5142. ConvertOperands(parameter0,parameter1);
  5143. actualParameters.SetExpression(0,parameter0);
  5144. actualParameters.SetExpression(1,parameter1);
  5145. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5146. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5147. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5148. END;
  5149. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5150. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5151. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5152. END;
  5153. END;
  5154. type := parameter0.type;
  5155. ELSE type := SyntaxTree.invalidType;
  5156. END;
  5157. (* ---- MIN ----- *)
  5158. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5159. IF numberActualParameters = 1 THEN
  5160. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5161. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5162. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5163. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5164. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5165. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5166. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5167. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5168. END;
  5169. ELSE
  5170. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5171. END
  5172. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5173. ConvertOperands(parameter0,parameter1);
  5174. actualParameters.SetExpression(0,parameter0);
  5175. actualParameters.SetExpression(1,parameter1);
  5176. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5177. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5178. ELSE result.SetResolved(parameter1.resolved)
  5179. END;
  5180. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5181. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5182. ELSE result.SetResolved(parameter1.resolved)
  5183. END;
  5184. END;
  5185. type := parameter0.type;
  5186. ELSE type := SyntaxTree.invalidType;
  5187. END;
  5188. (* ---- ODD ----- *)
  5189. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5190. type := system.booleanType;
  5191. IF CheckIntegerType(parameter0) THEN
  5192. IF IsIntegerValue(parameter0,i0) THEN
  5193. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5194. type := system.booleanType;
  5195. END;
  5196. END;
  5197. (* ---- ORD ----- *)
  5198. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5199. IF id = Global.Ord THEN
  5200. type := system.integerType;
  5201. ELSE
  5202. type := system.longintType;
  5203. END;
  5204. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5205. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5206. actualParameters.SetExpression(0,parameter0);
  5207. (* IF CheckCharacterType(parameter0) THEN*)
  5208. IF IsCharacterValue(parameter0,c)THEN
  5209. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5210. type := Global.GetSignedIntegerType(system,ORD(c));
  5211. END;
  5212. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5213. END;
  5214. (* ---- SHORT ----- *)
  5215. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5216. type := type0;
  5217. IF IsSignedIntegerType(type) THEN
  5218. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5219. ELSIF type = system.integerType THEN type := system.shortintType
  5220. ELSIF type = system.longintType THEN type := system.integerType
  5221. ELSIF type = system.hugeintType THEN type:= system.longintType
  5222. ELSE
  5223. CASE type.sizeInBits OF
  5224. 16: type := Global.Integer8
  5225. |32: type := Global.Integer16
  5226. |64: type := Global.Integer32
  5227. END;
  5228. END;
  5229. ELSIF type IS SyntaxTree.FloatType THEN
  5230. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5231. ELSIF type = system.longrealType THEN type := system.realType
  5232. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5233. END;
  5234. ELSIF type IS SyntaxTree.ComplexType THEN
  5235. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5236. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5237. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5238. END;
  5239. ELSE
  5240. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5241. END;
  5242. IF (parameter0.resolved # NIL) THEN
  5243. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5244. IF parameter0 IS SyntaxTree.Value THEN
  5245. result.SetResolved(parameter0(SyntaxTree.Value));
  5246. END;
  5247. END;
  5248. (* ---- LONG ----- *)
  5249. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5250. type := type0;
  5251. IF IsSignedIntegerType(type) THEN
  5252. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5253. ELSIF type = system.longintType THEN type := system.hugeintType
  5254. ELSIF type = system.integerType THEN type := system.longintType
  5255. ELSIF type = system.shortintType THEN type := system.integerType
  5256. ELSE
  5257. CASE type.sizeInBits OF
  5258. 8: type := Global.Integer16
  5259. |16: type := Global.Integer32
  5260. |32: type := Global.Integer64
  5261. END;
  5262. END;
  5263. ELSIF type IS SyntaxTree.FloatType THEN
  5264. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5265. ELSIF type= system.realType THEN type := system.longrealType
  5266. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5267. END;
  5268. ELSIF type IS SyntaxTree.ComplexType THEN
  5269. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5270. ELSIF type = system.complexType THEN type := system.longcomplexType
  5271. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5272. END;
  5273. ELSE
  5274. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5275. END;
  5276. IF (parameter0.resolved # NIL) THEN
  5277. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5278. IF parameter0 IS SyntaxTree.Value THEN
  5279. result.SetResolved(parameter0(SyntaxTree.Value));
  5280. END;
  5281. END;
  5282. (* ---- SIZE OF ----- *)
  5283. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5284. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5285. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5286. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5287. type := system.integerType;
  5288. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5289. ELSE
  5290. (* for variables, system sizeof could represent the physically occupied size
  5291. determined via the type descriptor, implement that ? *)
  5292. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5293. END
  5294. (* ---- SYSTEM.TRACE -----*)
  5295. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5296. FOR i := 0 TO numberActualParameters-1 DO
  5297. parameter0 := actualParameters.GetExpression(i);
  5298. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5299. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5300. END;
  5301. END;
  5302. (* remaining issues can only be tested in backend *)
  5303. (* ---- ADDRESSOF----- *)
  5304. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5305. IF HasAddress(parameter0) THEN
  5306. type := system.addressType;
  5307. ELSE
  5308. type := SyntaxTree.invalidType;
  5309. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5310. END;
  5311. (* ---- BIT ----- *)
  5312. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5313. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5314. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5315. actualParameters.SetExpression(0,parameter0);
  5316. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5317. actualParameters.SetExpression(1,parameter1);
  5318. END;
  5319. type := system.booleanType;
  5320. (* ----- MSK ---- *)
  5321. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5322. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5323. ConvertOperands(parameter0,parameter1);
  5324. actualParameters.SetExpression(0,parameter0);
  5325. actualParameters.SetExpression(1,parameter1);
  5326. END;
  5327. type := parameter0.type;
  5328. (* ---- SYSTEM.GET64 ----- *)
  5329. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5330. IF CheckAddressType(parameter0) THEN
  5331. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5332. actualParameters.SetExpression(0,parameter0);
  5333. END;
  5334. type := system.hugeintType;
  5335. (* ---- SYSTEM.GET32 ----- *)
  5336. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5337. IF CheckAddressType(parameter0) THEN
  5338. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5339. actualParameters.SetExpression(0,parameter0);
  5340. END;
  5341. type := system.longintType;
  5342. (* ---- SYSTEM.GET16 ----- *)
  5343. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5344. IF CheckAddressType(parameter0) THEN
  5345. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5346. actualParameters.SetExpression(0,parameter0);
  5347. END;
  5348. type := system.integerType;
  5349. (* ---- SYSTEM.GET8 ----- *)
  5350. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5351. IF CheckAddressType(parameter0) THEN
  5352. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5353. actualParameters.SetExpression(0,parameter0);
  5354. END;
  5355. type := system.shortintType;
  5356. (* ---- SYSTEM.GetStackPointer ----- *)
  5357. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5358. type := system.addressType;
  5359. (* ---- SYSTEM.GetFramePointer ----- *)
  5360. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5361. type := system.addressType;
  5362. (* ---- SYSTEM.GetActivity ----- *)
  5363. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5364. type := system.objectType;
  5365. (* ---- SYSTEM.SetStackPointer ----- *)
  5366. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5367. IF CheckAddressType(parameter0) THEN
  5368. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5369. actualParameters.SetExpression(0,parameter0);
  5370. END;
  5371. (* ---- SYSTEM.SetFramePointer ----- *)
  5372. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5373. IF CheckAddressType(parameter0) THEN
  5374. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5375. actualParameters.SetExpression(0,parameter0);
  5376. END;
  5377. (* ---- SYSTEM.SetActivity ----- *)
  5378. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5379. IF CheckObjectType(parameter0) THEN
  5380. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5381. actualParameters.SetExpression(0,parameter0);
  5382. END;
  5383. (* ---- LSH, LSL, ROT, ROR ----- *)
  5384. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5385. type := type0;
  5386. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5387. actualParameters.SetExpression(1, parameter1);
  5388. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5389. IF id = Global.Lsh THEN
  5390. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5391. ELSIF id = Global.Rot THEN
  5392. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5393. ELSIF id = Global.Ror THEN
  5394. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5395. END;
  5396. END;
  5397. (* ---- SYSTEM.VAL ----- *)
  5398. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5399. IF CheckTypeDeclarationType(parameter0) THEN
  5400. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5401. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5402. result := SyntaxTree.invalidExpression;
  5403. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5404. ELSE
  5405. IF (parameter1.resolved # NIL) THEN
  5406. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5407. IF parameter0 IS SyntaxTree.Value THEN
  5408. result.SetResolved(parameter0(SyntaxTree.Value));
  5409. END;
  5410. END;
  5411. result.SetAssignable(parameter1.assignable);
  5412. END;
  5413. END;
  5414. (* ---- SYSTEM.GET ----- *)
  5415. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5416. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5417. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5418. actualParameters.SetExpression(0,parameter0);
  5419. END;
  5420. (* ---- SYSTEM.PUT ----- *)
  5421. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5422. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5423. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5424. actualParameters.SetExpression(0,parameter0);
  5425. END;
  5426. (* ---- SYSTEM.PUT64 ----- *)
  5427. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5428. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5429. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5430. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5431. actualParameters.SetExpression(0,parameter0);
  5432. actualParameters.SetExpression(1,parameter1);
  5433. END;
  5434. (* ---- SYSTEM.PUT32 ----- *)
  5435. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5436. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5437. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5438. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5439. actualParameters.SetExpression(0,parameter0);
  5440. actualParameters.SetExpression(1,parameter1);
  5441. END;
  5442. (* ---- SYSTEM.PUT16 ----- *)
  5443. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5444. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5445. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5446. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5447. actualParameters.SetExpression(0,parameter0);
  5448. actualParameters.SetExpression(1,parameter1);
  5449. END;
  5450. (* ---- SYSTEM.PUT8 ----- *)
  5451. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5452. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5453. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5454. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5455. actualParameters.SetExpression(0,parameter0);
  5456. actualParameters.SetExpression(1,parameter1);
  5457. END;
  5458. (* ---- SYSTEM.MOVE ----- *)
  5459. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5460. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5461. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5462. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5463. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5464. actualParameters.SetExpression(0,parameter0);
  5465. actualParameters.SetExpression(1,parameter1);
  5466. actualParameters.SetExpression(2,parameter2);
  5467. END;
  5468. (* ---- SYSTEM.NEW ----- *)
  5469. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5470. IF ~IsPointerType(parameter0.type) THEN
  5471. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5472. ELSIF CheckSizeType(parameter1) THEN
  5473. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5474. actualParameters.SetExpression(1,parameter1);
  5475. END;
  5476. (* ----SYSTEM.REF ---- *)
  5477. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5478. type := system.addressType
  5479. (* ---- INCR ----- *)
  5480. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5481. type := system.sizeType;
  5482. base := type0;
  5483. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5484. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5485. IF i1 < 0 THEN
  5486. Error(position,Diagnostics.Invalid,"invalid dimension");
  5487. base := SyntaxTree.invalidType;
  5488. ELSE
  5489. base := ArrayBase(base,i1);
  5490. IF (base # NIL) & Indexable(base) THEN
  5491. ELSE
  5492. Error(position,Diagnostics.Invalid,"len on no array");
  5493. IF VerboseErrorMessage THEN
  5494. Printout.Info("base",base);
  5495. END;
  5496. base := SyntaxTree.invalidType;
  5497. END;
  5498. END;
  5499. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5500. actualParameters.SetExpression(1,parameter1);
  5501. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5502. mathArrayType := base(SyntaxTree.MathArrayType);
  5503. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5504. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5505. type := system.longintType;
  5506. END;
  5507. END;
  5508. ELSE
  5509. type := system.longintType;
  5510. END;
  5511. (* ---- SUM ----- *)
  5512. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5513. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5514. (* ---- ALL ----- *)
  5515. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5516. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5517. (* ---- DIM ----- *)
  5518. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5519. type := system.sizeType;
  5520. IF type0 IS SyntaxTree.MathArrayType THEN
  5521. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5522. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5523. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5524. END;
  5525. ELSE
  5526. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5527. END;
  5528. (* ---- CAS ----- *)
  5529. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5530. IF type0.IsComposite () THEN
  5531. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5532. result := SyntaxTree.invalidExpression;
  5533. ELSIF ~IsVariable (parameter0) THEN
  5534. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5535. result := SyntaxTree.invalidExpression;
  5536. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5537. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5538. result := SyntaxTree.invalidExpression;
  5539. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5540. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5541. result := SyntaxTree.invalidExpression;
  5542. ELSE
  5543. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5544. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5545. type := type0;
  5546. END;
  5547. (* ---- RESHAPE ----- *)
  5548. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5549. IF type0 IS SyntaxTree.MathArrayType THEN
  5550. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5551. base := ArrayBase(type0,MAX(LONGINT));
  5552. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5553. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5554. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5555. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5556. IF ~CompatibleTo(system,type0,parameterType) THEN
  5557. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5558. result := SyntaxTree.invalidExpression;
  5559. ELSE
  5560. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5561. END;
  5562. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5563. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5564. IF ~CompatibleTo(system,type1,parameterType) THEN
  5565. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5566. result := SyntaxTree.invalidExpression;
  5567. ELSE
  5568. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5569. END;
  5570. ELSE
  5571. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5572. result := SyntaxTree.invalidExpression;
  5573. END;
  5574. (* ---- SYSTEM.TYPECODE ----- *)
  5575. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5576. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5577. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5578. type := type.resolved;
  5579. IF type IS SyntaxTree.PointerType THEN
  5580. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5581. END;
  5582. IF ~(type IS SyntaxTree.RecordType) THEN
  5583. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5584. END;
  5585. ELSE
  5586. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5587. END;
  5588. type := system.addressType;
  5589. (* -------- FLT --------- *)
  5590. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5591. type := system.realType;
  5592. IF IsRealValue(parameter0, r) THEN
  5593. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5594. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5595. i0 := i; i := ABS(i);
  5596. IF i # 0 THEN
  5597. i1 := 23;
  5598. IF i >= 2*800000H THEN
  5599. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5600. ELSIF i < 800000H THEN
  5601. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5602. END;
  5603. i := (i1 + 127)*800000H - 800000H + i;
  5604. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5605. END;
  5606. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5607. END;
  5608. (* ------- CONNECT -------*)
  5609. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5610. IF ~(currentIsCellNet) THEN
  5611. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5612. END;
  5613. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5614. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5615. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5616. END;
  5617. IF numberActualParameters = 3 THEN
  5618. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5619. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5620. END;
  5621. *)
  5622. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5623. actualParameters.SetExpression(2,parameter2);
  5624. END;
  5625. activeCellsStatement := TRUE;
  5626. (* ---------- DELEGATE --------*)
  5627. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5628. IF ~(currentIsCellNet) THEN
  5629. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5630. END;
  5631. IF ~CheckPortType(parameter1, inPort) THEN
  5632. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5633. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5634. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5635. ELSIF (outPort.direction # inPort.direction) THEN
  5636. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5637. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5638. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5639. END;
  5640. activeCellsStatement := TRUE;
  5641. (* --------- RECEIVE ---------*)
  5642. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5643. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5644. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5645. IF inPort.direction # SyntaxTree.InPort THEN
  5646. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5647. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5648. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5649. END;
  5650. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5651. IF ~SameType(parameter2.type, system.integerType) THEN
  5652. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5653. END;
  5654. END;
  5655. END;
  5656. (* --------- SEND ---------*)
  5657. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5658. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5659. IF CheckPortType(parameter0,outPort) THEN
  5660. IF outPort.direction # SyntaxTree.OutPort THEN
  5661. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5662. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5663. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5664. ELSE
  5665. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5666. actualParameters.SetExpression(1,parameter1);
  5667. END;
  5668. END;
  5669. (* ------- custom builtins ----- *)
  5670. ELSIF id = Global.systemSpecial THEN
  5671. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5672. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5673. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5674. type := procedureType.returnType;
  5675. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5676. (* go through all formal parameters *)
  5677. formalParameter := procedureType.firstParameter;
  5678. FOR i := 0 TO actualParameters.Length() - 1 DO
  5679. actualParameter := actualParameters.GetExpression(i);
  5680. IF actualParameter = SyntaxTree.invalidExpression THEN
  5681. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5682. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5683. ELSE
  5684. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5685. END;
  5686. actualParameters.SetExpression(i, actualParameter);
  5687. formalParameter := formalParameter.nextParameter
  5688. END
  5689. END
  5690. ELSE
  5691. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5692. result := SyntaxTree.invalidExpression;
  5693. END;
  5694. END;
  5695. IF result # SyntaxTree.invalidExpression THEN
  5696. type := ResolveType(type);
  5697. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5698. result.SetType(type);
  5699. END;
  5700. RETURN result
  5701. END NewBuiltinCallDesignator;
  5702. (** return type guard designator left(type)
  5703. - check if type can be extended (i.e. is no static record)
  5704. - check if type is a type extension of left.type
  5705. - returns new type guard designator
  5706. returns invalidDesignator = invalidExpression if error
  5707. **)
  5708. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5709. VAR result: SyntaxTree.Designator;
  5710. BEGIN
  5711. result := SyntaxTree.invalidDesignator;
  5712. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5713. Error(position,Diagnostics.Invalid,"no type extension of type");
  5714. IF VerboseErrorMessage THEN
  5715. Printout.Info("left",left);
  5716. Printout.Info("type",type);
  5717. END;
  5718. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5719. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5720. ELSIF IsUnsafePointer(left.type) THEN
  5721. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5722. ELSE
  5723. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5724. result.SetType(type);
  5725. result.SetAssignable(left.assignable);
  5726. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5727. END;
  5728. RETURN result
  5729. END NewTypeGuardDesignator;
  5730. (** check and resolve parameter designator left(expression list)
  5731. - check expression list
  5732. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5733. - elsif left is a procedure type then
  5734. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5735. - else return is a procedure call then return ProcedureCallDesignator
  5736. returns invalidDesignator = invalidExpression if error
  5737. **)
  5738. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5739. VAR
  5740. parameters: SyntaxTree.ExpressionList;
  5741. left: SyntaxTree.Designator;
  5742. result,expression: SyntaxTree.Expression;
  5743. typeDeclaration: SyntaxTree.TypeDeclaration;
  5744. type, expressionType: SyntaxTree.Type;
  5745. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5746. BEGIN
  5747. type := type.resolved;
  5748. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5749. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5750. END;
  5751. RETURN type
  5752. END BaseType;
  5753. BEGIN
  5754. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5755. result := SyntaxTree.invalidDesignator;
  5756. left := ResolveDesignator(designator.left);
  5757. IF left # SyntaxTree.invalidDesignator THEN
  5758. parameters := designator.parameters;
  5759. IF ExpressionList(parameters) THEN
  5760. IF (left.type = NIL) THEN
  5761. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5762. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5763. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5764. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5765. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5766. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5767. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5768. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5769. ELSE
  5770. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5771. END
  5772. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5773. expression := parameters.GetExpression(0);
  5774. type := typeDeclaration.declaredType.resolved;
  5775. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5776. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5777. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5778. OR (expressionType IS SyntaxTree.EnumerationType)
  5779. ) THEN
  5780. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5781. ELSE
  5782. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5783. END;
  5784. ELSE
  5785. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5786. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5787. result := SyntaxTree.invalidDesignator;
  5788. END;
  5789. ELSE
  5790. result := SyntaxTree.invalidDesignator
  5791. END;
  5792. END;
  5793. resolvedExpression := result;
  5794. END VisitParameterDesignator;
  5795. (** check dereference designator left^
  5796. - check if left is pointer type or left is object type
  5797. - return new dereference designator with type = left.baseType.type (if appropriate)
  5798. with error handling
  5799. returns invalidDesignator = invalidExpression if error
  5800. **)
  5801. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5802. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5803. BEGIN
  5804. result := SyntaxTree.invalidDesignator;
  5805. type := left.type;
  5806. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5807. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5808. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5809. result.SetAssignable(TRUE);
  5810. result.SetType(type);
  5811. result.SetHidden(left.isHidden);
  5812. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5813. type := type.resolved;
  5814. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5815. result.SetAssignable(TRUE);
  5816. result.SetType(type);
  5817. result.SetHidden(left.isHidden);
  5818. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5819. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5820. result.SetAssignable(TRUE);
  5821. result.SetType(type);
  5822. result.SetHidden(left.isHidden);
  5823. ELSE
  5824. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5825. IF VerboseErrorMessage THEN
  5826. Printout.Info("pointer", type);
  5827. Printout.Info("scope", currentScope);
  5828. END;
  5829. END;
  5830. RETURN result
  5831. END NewDereferenceDesignator;
  5832. (** check supercall designator left^
  5833. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5834. - return new supercall designator with type = left.type
  5835. with error handling
  5836. **)
  5837. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5838. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5839. objectScope: SyntaxTree.Scope;
  5840. BEGIN
  5841. result := SyntaxTree.invalidDesignator;
  5842. IF left = SyntaxTree.invalidDesignator THEN
  5843. (* error already handled *)
  5844. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5845. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5846. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5847. IF symbol IS SyntaxTree.Procedure THEN
  5848. procedure := symbol(SyntaxTree.Procedure);
  5849. objectScope := currentScope;
  5850. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5851. objectScope := objectScope.outerScope;
  5852. END;
  5853. IF (left.left = NIL) OR ~
  5854. (
  5855. (left.left IS SyntaxTree.SelfDesignator) OR
  5856. (left.left IS SyntaxTree.DereferenceDesignator)
  5857. & (left.left(SyntaxTree.Designator).left # NIL)
  5858. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5859. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5860. IF VerboseErrorMessage THEN
  5861. Printout.Info("left.left",left.left);
  5862. END;
  5863. ELSIF procedure.super # NIL THEN
  5864. result := SyntaxTree.NewSupercallDesignator(position,left);
  5865. result.SetType(left.type.resolved)
  5866. ELSE
  5867. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5868. END;
  5869. ELSE
  5870. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5871. END;
  5872. ELSE
  5873. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5874. END;
  5875. RETURN result
  5876. END NewSupercallDesignator;
  5877. (** check and semantically resolve arrow designator left^
  5878. - if left is procedure type -> result := SupercallDesignator
  5879. - else result := DereferenceDesignator
  5880. returns result via global variable resolvedExpression
  5881. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5882. **)
  5883. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5884. VAR left: SyntaxTree.Designator;
  5885. BEGIN
  5886. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5887. left := ResolveDesignator(arrowDesignator.left);
  5888. IF left # NIL THEN
  5889. IF (left.type = NIL) THEN
  5890. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5891. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5892. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5893. ELSE
  5894. IF IsPointerToObject(left.type) THEN
  5895. Warning(arrowDesignator.position, "forbidden dereference on object");
  5896. END;
  5897. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5898. END
  5899. END
  5900. END VisitArrowDesignator;
  5901. (** check and return expression
  5902. - if expression has no type then resolve expression
  5903. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5904. - return result
  5905. **)
  5906. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5907. VAR result,prev: SyntaxTree.Expression;
  5908. BEGIN
  5909. IF expression = NIL THEN result := NIL
  5910. ELSIF (expression.type = NIL) THEN
  5911. prev := resolvedExpression;
  5912. resolvedExpression := SyntaxTree.invalidExpression;
  5913. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5914. expression.SetType(SyntaxTree.invalidType);
  5915. END;
  5916. expression.Accept(SELF);
  5917. result := resolvedExpression;
  5918. IF currentIsRealtime THEN
  5919. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5920. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5921. END;
  5922. END;
  5923. (* designator modifiers for backends if they support it ...*)
  5924. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5925. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5926. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5927. END;
  5928. resolvedExpression := prev
  5929. ELSE
  5930. result := expression
  5931. END;
  5932. RETURN result
  5933. END ResolveExpression;
  5934. (**
  5935. check expression to be constant expression
  5936. - resolve expression
  5937. - if valid then check that of value type
  5938. report error and return invalidExpression if anything fails
  5939. **)
  5940. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5941. VAR position: LONGINT;
  5942. BEGIN
  5943. position := expression.position;
  5944. expression := ResolveExpression(expression);
  5945. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5946. ELSIF (expression.resolved = NIL) THEN
  5947. Error(position,Diagnostics.Invalid,"expression is not constant");
  5948. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5949. expression := SyntaxTree.invalidExpression;
  5950. END;
  5951. RETURN expression
  5952. END ConstantExpression;
  5953. (** check expression to be constant integer
  5954. - resolve expresssion
  5955. - if valid then check that of integer value type
  5956. report error and return invalidExpression if anything fails
  5957. **)
  5958. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5959. VAR position: LONGINT;
  5960. BEGIN
  5961. position := expression.position;
  5962. expression := ResolveExpression(expression);
  5963. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5964. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5965. expression := SyntaxTree.invalidExpression;
  5966. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5967. END;
  5968. RETURN expression
  5969. END ConstantInteger;
  5970. (** check expression as positive (>=0) constant integer
  5971. - resolve expression
  5972. - if valid then check that integer value
  5973. - if integer value then check that value >= 0
  5974. report error and return invalidExpression if anything fails
  5975. **)
  5976. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5977. VAR position: LONGINT;
  5978. BEGIN
  5979. position := expression.position;
  5980. expression := ConstantExpression(expression);
  5981. IF expression = SyntaxTree.invalidExpression THEN
  5982. (* error already reported *)
  5983. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5984. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5985. expression := SyntaxTree.invalidExpression
  5986. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5987. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5988. END;
  5989. RETURN expression
  5990. END ConstantIntegerGeq0;
  5991. (** check expression as condition
  5992. - resolve expression
  5993. - if valid expression then check that result type is boolean
  5994. report error and return invalidExpression if anything fails
  5995. **)
  5996. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5997. VAR position: LONGINT;
  5998. BEGIN
  5999. position := expression.position;
  6000. expression := ResolveExpression(expression);
  6001. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6002. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6003. expression := SyntaxTree.invalidExpression;
  6004. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6005. END;
  6006. RETURN expression
  6007. END ResolveCondition;
  6008. (*** symbols ***)
  6009. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6010. BEGIN
  6011. x.Accept(SELF);
  6012. END ResolveSymbol;
  6013. (** check a symbol
  6014. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6015. **)
  6016. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6017. VAR scope: SyntaxTree.Scope;
  6018. BEGIN
  6019. (* visibility *)
  6020. scope := symbol.scope;
  6021. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6022. scope := scope.outerScope;
  6023. END;
  6024. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6025. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6026. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6027. IF VerboseErrorMessage THEN
  6028. Printout.Info("symbol",symbol);
  6029. END;
  6030. END;
  6031. END;
  6032. END CheckSymbolVisibility;
  6033. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6034. If node is currently being resolved then emit a cyclic definition error.
  6035. Return TRUE only if node is fully resolved.
  6036. **)
  6037. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6038. VAR result: BOOLEAN;
  6039. BEGIN
  6040. IF SyntaxTree.Resolved IN x.state THEN
  6041. result := FALSE
  6042. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6043. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6044. result := FALSE;
  6045. ELSE
  6046. result := TRUE;
  6047. x.SetState(SyntaxTree.BeingResolved)
  6048. END;
  6049. RETURN result
  6050. END SymbolNeedsResolution;
  6051. (** check and resolve a type declaration symbol = Type
  6052. - set type to declaration type
  6053. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6054. This is so because the type declaration itself does not have a type but it only stands for a type.
  6055. In the implementation of the compiler this made a lot much easier.
  6056. - resolve and set declared type
  6057. - check symbol
  6058. **)
  6059. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6060. VAR prevScope: SyntaxTree.Scope;
  6061. BEGIN
  6062. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6063. IF SymbolNeedsResolution(typeDeclaration) THEN
  6064. prevScope := currentScope;
  6065. currentScope := typeDeclaration.scope;
  6066. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6067. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6068. CheckSymbolVisibility(typeDeclaration);
  6069. typeDeclaration.SetState(SyntaxTree.Resolved);
  6070. currentScope := prevScope;
  6071. END;
  6072. END VisitTypeDeclaration;
  6073. (** check and resolve a constant declaration symbol = (constant) expression
  6074. - check expression
  6075. - set type and value
  6076. - check symbol
  6077. **)
  6078. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6079. VAR
  6080. expression: SyntaxTree.Expression;
  6081. type: SyntaxTree.Type;
  6082. name: Basic.SegmentedName;
  6083. replacement: Replacement;
  6084. BEGIN
  6085. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6086. IF SymbolNeedsResolution(constant) THEN
  6087. expression := constant.value;
  6088. IF replacements # NIL THEN
  6089. Global.GetSymbolSegmentedName(constant, name);
  6090. replacement := replacements;
  6091. WHILE (replacement # NIL) & (replacement.name # name) DO
  6092. replacement := replacement.next;
  6093. END;
  6094. IF replacement # NIL THEN
  6095. InfoSS(constant.position, "replacing constant", constant.name);
  6096. (*
  6097. NEW(stringReader, Strings.Length(replacement.string^));
  6098. stringReader.Set(replacement.string^);
  6099. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6100. NEW(parser, scanner, diagnostics);
  6101. expression := parser.Expression();
  6102. *)
  6103. expression := replacement.expression;
  6104. replacement.used := TRUE;
  6105. END;
  6106. END;
  6107. constant.SetType(SyntaxTree.invalidType);
  6108. expression := ConstantExpression(expression);
  6109. ASSERT(expression.type # NIL);
  6110. type := expression.type.resolved;
  6111. constant.SetType(type);
  6112. constant.SetValue(expression);
  6113. CheckSymbolVisibility(constant);
  6114. constant.SetState(SyntaxTree.Resolved);
  6115. END;
  6116. END VisitConstant;
  6117. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6118. VAR procedureAlignment: LONGINT;
  6119. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6120. (* least common multiple *)
  6121. VAR a,b: LONGINT;
  6122. BEGIN
  6123. a := a0; b := b0;
  6124. WHILE (a # b) DO
  6125. IF a < b THEN a := a+a0
  6126. ELSE b := b + b0
  6127. END;
  6128. END;
  6129. RETURN a
  6130. END LCM;
  6131. BEGIN
  6132. IF alignment > 1 THEN
  6133. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6134. IF (procedureAlignment > 1) THEN
  6135. alignment := LCM(alignment, procedureAlignment);
  6136. END;
  6137. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6138. END;
  6139. END AdaptStackAlignment;
  6140. (** check and resolve a variable / field
  6141. - check and set type
  6142. - negative check on open array type
  6143. - check symbol
  6144. **)
  6145. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6146. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6147. BEGIN
  6148. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6149. IF SymbolNeedsResolution(variable) THEN
  6150. modifiers := variable.modifiers;
  6151. (*
  6152. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6153. variable.AddFlags(flags);
  6154. *)
  6155. variable.SetType(ResolveType(variable.type));
  6156. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6157. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6158. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6159. END;
  6160. END;
  6161. CheckSymbolVisibility(variable);
  6162. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6163. variable.SetUntraced(TRUE);
  6164. IF ~ContainsPointer(variable.type) THEN
  6165. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6166. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6167. END;
  6168. END;
  6169. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6170. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6171. IF ~PowerOf2(value) THEN
  6172. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6173. ELSE
  6174. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6175. END;
  6176. END;
  6177. variable.SetAlignment(FALSE,value);
  6178. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6179. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6180. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6181. END;
  6182. variable.SetAlignment(TRUE, value);
  6183. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6184. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6185. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6186. END;
  6187. variable.SetFictive(value);
  6188. variable.SetOffset(value*system.dataUnit);
  6189. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6190. END;
  6191. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6192. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6193. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6194. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6195. END;
  6196. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6197. IF variable.initializer # NIL THEN
  6198. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6199. END;
  6200. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6201. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6202. pointerType.SetPointerBase(variable.type);
  6203. pointerType.SetHidden(TRUE);
  6204. variable.SetType(ResolveType(pointerType));
  6205. END;
  6206. variable.SetState(SyntaxTree.Resolved);
  6207. END;
  6208. END VisitVariable;
  6209. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6210. BEGIN
  6211. VisitVariable(property)
  6212. END VisitProperty;
  6213. (** check and resolve a (procedure) parameter
  6214. - check and set type
  6215. - check symbol
  6216. - check parameter kind and set read-only flags if appropriate
  6217. **)
  6218. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6219. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6220. BEGIN
  6221. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6222. IF SymbolNeedsResolution(parameter) THEN
  6223. modifiers := parameter.modifiers;
  6224. parameter.SetType(ResolveType(parameter.type));
  6225. ASSERT(parameter.type.resolved # NIL);
  6226. CheckSymbolVisibility(parameter);
  6227. IF parameter.defaultValue # NIL THEN
  6228. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6229. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6230. ELSE
  6231. expression := ConstantExpression(parameter.defaultValue);
  6232. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6233. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6234. parameter.SetDefaultValue(expression);
  6235. END;
  6236. END;
  6237. END;
  6238. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6239. parameter.SetUntraced(TRUE);
  6240. IF ~ContainsPointer(parameter.type) THEN
  6241. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6242. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6243. END;
  6244. END;
  6245. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6246. parameter.SetMoveable(TRUE);
  6247. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6248. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6249. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6250. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6251. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6252. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6253. END;
  6254. END;
  6255. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6256. parameter.SetState(SyntaxTree.Resolved);
  6257. END;
  6258. END VisitParameter;
  6259. (** check and resolve a procedure (with declaration and implementation scope)
  6260. - check the procedure type
  6261. - check if method (i.e. in record scope), if so then
  6262. - check if (unique) constructor
  6263. - check if (unique) finalizer
  6264. - check if super method available, if so then check signature
  6265. - of not in record scope then negative check on constructor flag
  6266. - of not in record scope then negative check on finalizer flag
  6267. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6268. - check procedure symbol
  6269. **)
  6270. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6271. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6272. procedureType: SyntaxTree.ProcedureType;
  6273. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6274. qualifiedType: SyntaxTree.QualifiedType;
  6275. value: LONGINT;
  6276. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6277. position: LONGINT;
  6278. BEGIN
  6279. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6280. IF IsOberonInline(procedure) THEN
  6281. IF SyntaxTree.Public * procedure.access # {} THEN
  6282. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6283. END;
  6284. procedure.SetInline(FALSE);
  6285. procedure.SetOberonInline(TRUE);
  6286. END;
  6287. IF SymbolNeedsResolution(procedure) THEN
  6288. recentIsRealtime := currentIsRealtime;
  6289. recentIsBodyProcedure := currentIsBodyProcedure;
  6290. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6291. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6292. modifiers := procedureType.modifiers;
  6293. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6294. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6295. IF useDarwinCCalls THEN (*fld*)
  6296. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6297. ELSE
  6298. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6299. END
  6300. END;
  6301. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6302. procedureType.SetInterrupt(TRUE);
  6303. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6304. END;
  6305. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6306. procedureType.SetNoReturn(TRUE);
  6307. END;
  6308. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6309. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6310. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6311. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6312. END;
  6313. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6314. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6315. END;
  6316. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6317. IF ~PowerOf2(value) THEN
  6318. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6319. ELSE
  6320. procedureType.SetStackAlignment(value)
  6321. END;
  6322. END;
  6323. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6324. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6325. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6326. END;
  6327. CheckModifiers(modifiers, TRUE);
  6328. modifiers := procedureType.returnTypeModifiers;
  6329. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6330. CheckModifiers(modifiers, TRUE);
  6331. FixProcedureType(procedureType);
  6332. currentIsRealtime := procedureType.isRealtime;
  6333. currentIsBodyProcedure := procedure.isBodyProcedure;
  6334. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6335. THEN
  6336. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6337. END;
  6338. CheckSymbolVisibility(procedure);
  6339. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6340. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6341. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6342. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6343. END;
  6344. END;
  6345. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6346. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6347. procedureType.SetDelegate(TRUE);
  6348. IF procedureType.isRealtime & ~record.isRealtime THEN
  6349. Error(procedure.position, Diagnostics.Invalid, "invalid realtime procedure in non-realtime object");
  6350. END;
  6351. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6352. IF record.pointerType.typeDeclaration = NIL THEN
  6353. selfParameter.SetType(record.pointerType);
  6354. ELSE
  6355. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6356. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6357. qualifiedType.SetResolved(record.pointerType);
  6358. selfParameter.SetType(qualifiedType);
  6359. END;
  6360. selfParameter.SetAccess(SyntaxTree.Hidden);
  6361. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6362. IF procedure.isConstructor THEN
  6363. (*! constructor is always visible, compatibility to paco
  6364. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6365. *)
  6366. procedure.MarkUsed;
  6367. IF procedureType.returnType # NIL THEN
  6368. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6369. END;
  6370. proc := procedure.scope.firstProcedure;
  6371. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6372. proc := proc.nextProcedure;
  6373. END;
  6374. IF proc # NIL THEN
  6375. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6376. ELSE
  6377. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6378. END;
  6379. END;
  6380. IF procedure.isFinalizer THEN
  6381. procedure.MarkUsed;
  6382. IF procedureType.returnType # NIL THEN
  6383. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6384. END;
  6385. IF procedureType.numberParameters # 0 THEN
  6386. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6387. END;
  6388. proc := procedure.scope.firstProcedure;
  6389. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6390. proc := proc.nextProcedure;
  6391. END;
  6392. IF proc # NIL THEN
  6393. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6394. ELSE
  6395. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6396. END;
  6397. END;
  6398. super := FindSuperProcedure(record.recordScope, procedure);
  6399. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6400. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6401. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6402. END;
  6403. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6404. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6405. END;
  6406. IF super.isFinal THEN
  6407. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6408. END;
  6409. procedure.SetSuper(super);
  6410. super.SetOverwritten(TRUE);
  6411. procedure.SetAccess(procedure.access+super.access);
  6412. procedure.MarkUsed;
  6413. END;
  6414. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6415. THEN
  6416. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6417. END;
  6418. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6419. IF cellsAreObjects THEN
  6420. procedureType.SetDelegate(TRUE);
  6421. END;
  6422. IF procedure.isConstructor THEN
  6423. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6424. END;
  6425. ELSIF procedure.isConstructor THEN
  6426. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6427. END;
  6428. Declarations(procedure.procedureScope, FALSE);
  6429. (* body resolution part done as late fix of the procedure type *)
  6430. procedure.SetState(SyntaxTree.Resolved);
  6431. currentIsRealtime := recentIsRealtime;
  6432. currentIsBodyProcedure := recentIsBodyProcedure;
  6433. END;
  6434. END VisitProcedure;
  6435. (**
  6436. a builtin procedure is a global item that may not be modified locally
  6437. instead the resolving of builtin procedure calls are done in the esignator
  6438. **)
  6439. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6440. VAR type: SyntaxTree.Type;
  6441. BEGIN
  6442. type := ResolveType(builtinProcedure.type);
  6443. END VisitBuiltin;
  6444. (* nopov *)
  6445. (** check and resolve operator
  6446. - operators are first checked as procedures
  6447. - then additional operator-specific checks are done
  6448. - note that only module-scope operators are checked here
  6449. (operators in a record scope are only allowed in the context of
  6450. array-structured object types and checked in 'ResolveArrayStructure')
  6451. - also note that inter-operator conformity is not checked here
  6452. **)
  6453. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6454. VAR
  6455. procedureType: SyntaxTree.ProcedureType;
  6456. leftType, rightType: SyntaxTree.Type;
  6457. identifierNumber, position: LONGINT;
  6458. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6459. modifiers: SyntaxTree.Modifier;
  6460. (** whether a type is locally defined in the current module scope
  6461. for arrays, the base type must be locally defined **)
  6462. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6463. BEGIN
  6464. IF type = NIL THEN
  6465. RETURN FALSE
  6466. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6467. RETURN TRUE
  6468. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6469. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6470. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6471. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6472. ELSE
  6473. RETURN FALSE
  6474. END
  6475. END IsLocallyDefined;
  6476. BEGIN
  6477. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6478. procedureType := operator.type(SyntaxTree.ProcedureType);
  6479. modifiers := procedureType.modifiers;
  6480. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6481. CheckModifiers(modifiers, TRUE);
  6482. VisitProcedure(operator);
  6483. IF operator.scope IS SyntaxTree.RecordScope THEN
  6484. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6485. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6486. IF identifierNumber = -1 THEN
  6487. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6488. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6489. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6490. ELSE
  6491. IF procedureType.numberParameters < 1 THEN
  6492. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6493. ELSIF procedureType.numberParameters > 2 THEN
  6494. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6495. ELSE
  6496. (* determine operand types *)
  6497. leftType := procedureType.firstParameter.type;
  6498. IF procedureType.numberParameters > 1 THEN
  6499. rightType := procedureType.firstParameter.nextParameter.type
  6500. ELSE
  6501. rightType := NIL
  6502. END;
  6503. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6504. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6505. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6506. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6507. END
  6508. END;
  6509. (* TODO: refine the checks, think about how restrictive the checks should be
  6510. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6511. They might be used for intersection, union, complement of custom object types *)
  6512. (* defaults *)
  6513. hasReturnType := TRUE;
  6514. mustBeUnary := FALSE;
  6515. mustBeBinary := FALSE;
  6516. mustReturnBoolean := FALSE;
  6517. mustReturnInteger := FALSE;
  6518. mustHaveEquitypedOperands := FALSE;
  6519. (* operator-specific exceptions *)
  6520. CASE identifierNumber OF
  6521. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6522. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6523. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6524. mustBeBinary := TRUE
  6525. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6526. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6527. | Scanner.Times: mustBeBinary := TRUE
  6528. | Scanner.TimesTimes: mustBeBinary := TRUE
  6529. | Scanner.DotTimes: mustBeBinary := TRUE
  6530. | Scanner.PlusTimes: mustBeBinary := TRUE
  6531. | Scanner.Slash: mustBeBinary := TRUE
  6532. | Scanner.Backslash: mustBeBinary := TRUE
  6533. | Scanner.DotSlash: mustBeBinary := TRUE
  6534. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6535. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6536. | Scanner.Not: mustBeUnary := TRUE
  6537. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6538. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6539. | Scanner.Transpose: mustBeUnary := TRUE;
  6540. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6541. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6542. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6543. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6544. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6545. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6546. | Global.Abs: mustBeUnary := TRUE;
  6547. | Global.Ash: (* TODO: arity? *)
  6548. | Global.Cap: (* TODO: arity? *)
  6549. | Global.Chr: mustBeUnary := TRUE;
  6550. | Global.Entier: (* TODO: arity? *)
  6551. | Global.EntierH: (* TODO: arity? *)
  6552. | Global.Len: (* unary and binary *)
  6553. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6554. | Global.Max, Global.Min: (* unary and binary *)
  6555. | Global.Odd: (* TODO: arity? *)
  6556. | Global.Sum: (* TODO: arity? *)
  6557. | Global.All: (* TODO: arity? *)
  6558. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6559. | Scanner.Alias:
  6560. | Scanner.GreaterGreater, Scanner.LessLess:
  6561. mustBeBinary := TRUE; hasReturnType := FALSE;
  6562. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6563. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6564. END;
  6565. (* check parameter count *)
  6566. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6567. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6568. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6569. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6570. END;
  6571. (* check parameter types *)
  6572. (* TODO: is this used at all? *)
  6573. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6574. leftType := procedureType.firstParameter.type;
  6575. rightType := procedureType.firstParameter.nextParameter.type;
  6576. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6577. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6578. END
  6579. END;
  6580. (* check return type *)
  6581. IF hasReturnType THEN
  6582. IF procedureType.returnType = NIL THEN
  6583. Error(operator.position, Diagnostics.Invalid, "return type required")
  6584. ELSIF mustReturnBoolean THEN
  6585. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6586. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6587. END
  6588. ELSIF mustReturnInteger THEN
  6589. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6590. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6591. END
  6592. END
  6593. ELSIF procedureType.returnType # NIL THEN
  6594. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6595. END
  6596. END
  6597. END
  6598. END
  6599. END VisitOperator;
  6600. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6601. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6602. BEGIN
  6603. IF error THEN RETURN FALSE END;
  6604. prevScope := currentScope;
  6605. prevDiagnostics := diagnostics;
  6606. diagnostics := NIL; (* suppress error output *)
  6607. currentScope := module.moduleScope;
  6608. VisitImport(x);
  6609. IF ~error THEN
  6610. module.moduleScope.AddImport(x);
  6611. x.SetScope(module.moduleScope);
  6612. END;
  6613. currentScope := prevScope;
  6614. diagnostics := prevDiagnostics;
  6615. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6616. END AddImport;
  6617. (** check and resolve import
  6618. - check for name = SYSTEM
  6619. - check for forbidden self import
  6620. - search through global import cache: already imported?
  6621. - check if already imported indirectly
  6622. - import if necessary -> set module and enter into import cache
  6623. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6624. - after this import this direct import and all indirect imports are stored in the current module's import list
  6625. **)
  6626. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6627. VAR
  6628. module: SyntaxTree.Module;
  6629. moduleScope: SyntaxTree.ModuleScope;
  6630. import,reimport: SyntaxTree.Import;
  6631. filename: FileName;
  6632. prevScope: SyntaxTree.Scope;
  6633. BEGIN
  6634. IF SymbolNeedsResolution(x) THEN
  6635. prevScope := currentScope;
  6636. x.SetType(SyntaxTree.importType);
  6637. moduleScope := currentScope.ownerModule.moduleScope;
  6638. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6639. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6640. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6641. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6642. ELSE
  6643. (* search through global import list: already imported ? *)
  6644. IF (x.module = NIL) & (importCache # NIL) THEN
  6645. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6646. ELSE import := NIL
  6647. END;
  6648. IF x.module # NIL THEN (* already imported indirectly *)
  6649. module := x.module;
  6650. ELSIF import # NIL THEN (* already in module list *)
  6651. module := import.module;
  6652. ASSERT(module # NIL);
  6653. x.SetModule(module);
  6654. ELSE (* must be imported *)
  6655. Global.ModuleFileName(x.moduleName,x.context,filename);
  6656. IF symbolFileFormat # NIL THEN
  6657. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6658. IF module = NIL THEN
  6659. ErrorSS(x.position,"could not import",filename);
  6660. IF VerboseErrorMessage THEN
  6661. Printout.Info("import",x)
  6662. END
  6663. ELSE
  6664. (*
  6665. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6666. (*! should rather be done by importer *)
  6667. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6668. checker.importCache := importCache;
  6669. checker.arrayBaseImported := arrayBaseImported;
  6670. checker.global := global;
  6671. checker.Module(module); (* semantic check *)
  6672. error := error OR checker.error;
  6673. END;
  6674. *)
  6675. (*
  6676. ASSERT(SyntaxTree.Resolved IN module.state);
  6677. *)
  6678. x.SetModule(module);
  6679. IF importCache # NIL THEN
  6680. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6681. import.SetContext(x.context);
  6682. import.SetModule(module);
  6683. importCache.AddImport(import);
  6684. END;
  6685. END;
  6686. ELSE
  6687. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6688. END;
  6689. END;
  6690. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6691. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6692. END;
  6693. import := module.moduleScope.firstImport;
  6694. WHILE(import # NIL) DO
  6695. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6696. ASSERT(currentScope # NIL);
  6697. ASSERT(currentScope.ownerModule # NIL);
  6698. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6699. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6700. Error(x.position,Diagnostics.Invalid,"recursive import");
  6701. ELSE
  6702. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6703. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6704. IF reimport = NIL THEN (* indirect import *)
  6705. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6706. reimport.SetContext(import.context);
  6707. reimport.SetModule(import.module);
  6708. moduleScope.AddImport(reimport);
  6709. reimport.SetScope(moduleScope);
  6710. ELSE
  6711. ASSERT(import.module # NIL);
  6712. reimport.SetModule(import.module); (* direct or indirect import *)
  6713. END;
  6714. END;
  6715. import := import.nextImport;
  6716. END;
  6717. END;
  6718. END;
  6719. currentScope := prevScope;
  6720. (* ELSE nothing to be done *)
  6721. x.SetState(SyntaxTree.Resolved);
  6722. END;
  6723. END VisitImport;
  6724. (*** statements ***)
  6725. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6726. VAR prev,resolved: SyntaxTree.Statement;
  6727. BEGIN
  6728. prev := resolvedStatement;
  6729. resolvedStatement := x;
  6730. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6731. activeCellsStatement := FALSE;
  6732. x.Accept(SELF);
  6733. (* removed this, implementation restriction should be resolved by backend
  6734. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6735. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6736. END;
  6737. *)
  6738. resolved := resolvedStatement;
  6739. resolvedStatement := prev;
  6740. RETURN resolved
  6741. END ResolveStatement;
  6742. (** check and resolve statement sequence
  6743. - check all statements, replace if necessary
  6744. **)
  6745. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6746. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6747. BEGIN
  6748. IF statementSequence # NIL THEN (* else empty *)
  6749. FOR i := 0 TO statementSequence.Length()-1 DO
  6750. statement := statementSequence.GetStatement(i);
  6751. resolved := ResolveStatement(statement);
  6752. IF (resolved # statement) THEN
  6753. statementSequence.SetStatement(i,resolved);
  6754. END;
  6755. END;
  6756. END;
  6757. END StatementSequence;
  6758. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6759. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6760. - check if procedure is callable
  6761. - check return type = NIL (otherwise must be assignment statement)
  6762. **)
  6763. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6764. VAR call: SyntaxTree.Designator;
  6765. BEGIN
  6766. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6767. call := procedureCall.call;
  6768. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6769. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6770. END;
  6771. call := ResolveDesignator(call);
  6772. IF call = SyntaxTree.invalidDesignator THEN
  6773. (* error already handled *)
  6774. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6775. (* inline call in a statement *)
  6776. ELSIF ~IsCallable(call) THEN
  6777. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6778. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6779. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6780. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6781. END;
  6782. procedureCall.SetCall(call);
  6783. (*
  6784. IF call = SyntaxTree.invalidDesignator THEN
  6785. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6786. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6787. IF IsOberonInline(procedure) THEN
  6788. Warning(procedure.position,"call to inline proc");
  6789. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6790. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6791. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6792. resolvedStatement := block;
  6793. RETURN;
  6794. END;
  6795. END;
  6796. *)
  6797. END VisitProcedureCallStatement;
  6798. (** check and resolve assignment LHS := RHS
  6799. - resolve LHS and RHS
  6800. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6801. - check if assignment is compatible
  6802. - check if LHS is variable (i.e. assignable)
  6803. - convert RHS if necessary
  6804. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6805. - assignment between different ASOTs
  6806. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6807. - assignment to ASOT elements:
  6808. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6809. **)
  6810. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6811. VAR
  6812. left: SyntaxTree.Designator;
  6813. right, expression: SyntaxTree.Expression;
  6814. designator: SyntaxTree.Designator;
  6815. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6816. mathArrayType: SyntaxTree.MathArrayType;
  6817. BEGIN
  6818. right := ResolveExpression(assignment.right);
  6819. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6820. left := ResolveDesignator(assignment.left);
  6821. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6822. (* error already handled *)
  6823. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6824. (* LHS is index write operator call on ASOT *)
  6825. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6826. (* necessary ?
  6827. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6828. type := procedureType.firstParameter.type;
  6829. expression := procedureCallDesignator.parameters.GetExpression(0);
  6830. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6831. *)
  6832. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6833. ELSIF CheckVariable(left) THEN
  6834. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6835. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6836. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6837. (* conversion done by procedure call
  6838. (* try to convert to left argument *)
  6839. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6840. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6841. procedureCallDesignator.parameters.SetExpression(1, right);
  6842. END;
  6843. *)
  6844. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6845. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6846. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6847. ELSIF AssignmentCompatible(left, right) THEN
  6848. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6849. mathArrayType := MathArrayStructureOfType(left.type);
  6850. right := NewConversion(right.position, right, mathArrayType, NIL);
  6851. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6852. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6853. ELSE
  6854. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6855. assignment.SetLeft(left);
  6856. assignment.SetRight(right);
  6857. resolvedStatement := assignment
  6858. END
  6859. END
  6860. END
  6861. END VisitAssignment;
  6862. (** check and resolve assignment LHS := RHS
  6863. - resolve LHS and RHS
  6864. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6865. - check if assignment is compatible
  6866. - check if LHS is variable (i.e. assignable)
  6867. - convert RHS if necessary
  6868. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6869. - assignment between different ASOTs
  6870. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6871. - assignment to ASOT elements:
  6872. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6873. **)
  6874. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6875. VAR
  6876. left: SyntaxTree.Designator;
  6877. right: SyntaxTree.Expression;
  6878. inPort, outPort: SyntaxTree.PortType;
  6879. expression: SyntaxTree.Expression;
  6880. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6881. BEGIN
  6882. right := ResolveExpression(communication.right);
  6883. left := ResolveDesignator(communication.left);
  6884. communication.SetLeft(left);
  6885. communication.SetRight(right);
  6886. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6887. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6888. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6889. (* conversion done by procedure call
  6890. (* try to convert to left argument *)
  6891. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6892. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6893. procedureCallDesignator.parameters.SetExpression(1, right);
  6894. END;
  6895. *)
  6896. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6897. ELSE
  6898. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6899. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6900. (* error already handled *)
  6901. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6902. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6903. IF outPort.direction # SyntaxTree.OutPort THEN
  6904. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6905. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6906. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6907. ELSE
  6908. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6909. communication.SetRight(right)
  6910. END;
  6911. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6912. IF CheckVariable(left) THEN
  6913. IF inPort.direction # SyntaxTree.InPort THEN
  6914. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6915. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6916. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6917. END;
  6918. END;
  6919. ELSE
  6920. Error(communication.position, -1, "unsupported stream operation");
  6921. END;
  6922. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6923. IF outPort.direction # SyntaxTree.OutPort THEN
  6924. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6925. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6926. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6927. ELSE
  6928. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6929. communication.SetRight(right)
  6930. END;
  6931. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6932. IF CheckVariable(right) THEN
  6933. IF inPort.direction # SyntaxTree.InPort THEN
  6934. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6935. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6936. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6937. END;
  6938. END;
  6939. ELSE
  6940. Error(communication.position, -1, "unsupported operation");
  6941. END;
  6942. END;
  6943. END VisitCommunicationStatement;
  6944. (** check and resolve if/eslif part
  6945. - check condition
  6946. - check statement sequence
  6947. **)
  6948. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6949. VAR prevUnreachable, b: BOOLEAN;
  6950. BEGIN
  6951. prevUnreachable := currentIsUnreachable;
  6952. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6953. IF IsBooleanValue(ifPart.condition,b) THEN
  6954. IF b=FALSE THEN
  6955. currentIsUnreachable := TRUE
  6956. ELSIF b=TRUE THEN
  6957. true := TRUE
  6958. END;
  6959. END;
  6960. StatementSequence(ifPart.statements);
  6961. currentIsUnreachable := prevUnreachable;
  6962. END IfPart;
  6963. (** check and resolve if statement
  6964. - check if parts and else part statement sequence
  6965. **)
  6966. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6967. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6968. BEGIN
  6969. prevUnreachable := currentIsUnreachable;
  6970. ifPartTrue := FALSE;
  6971. IfPart(ifStatement.ifPart,ifPartTrue);
  6972. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6973. elsif := ifStatement.GetElsifPart(i);
  6974. IfPart(elsif,ifPartTrue);
  6975. END;
  6976. IF ifStatement.elsePart # NIL THEN
  6977. IF ifPartTrue THEN
  6978. currentIsUnreachable := TRUE
  6979. END;
  6980. StatementSequence(ifStatement.elsePart)
  6981. END;
  6982. currentIsUnreachable := prevUnreachable;
  6983. END VisitIfStatement;
  6984. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6985. VAR variable: SyntaxTree.Designator;
  6986. type,variableType: SyntaxTree.Type;
  6987. withEntry: WithEntry;
  6988. BEGIN
  6989. variable := ResolveDesignator(withPart.variable);
  6990. variableType := variable.type.resolved;
  6991. withPart.SetVariable(variable);
  6992. type := ResolveType(withPart.type);
  6993. withPart.SetType(type);
  6994. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6995. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6996. END;
  6997. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6998. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6999. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7000. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7001. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7002. IF VerboseErrorMessage THEN
  7003. Printout.Info("variable",variable)
  7004. END;
  7005. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7006. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7007. IF VerboseErrorMessage THEN
  7008. Printout.Info("variable",variable);
  7009. Printout.Info("type",type);
  7010. END;
  7011. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7012. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7013. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7014. IF VerboseErrorMessage THEN
  7015. Printout.Info("variable",variable);
  7016. Printout.Info("type",type);
  7017. END;
  7018. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7019. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7020. ELSE
  7021. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7022. NEW(withEntry);
  7023. withEntry.previous := withEntries;
  7024. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7025. withEntry.type := type;
  7026. withEntries := withEntry;
  7027. StatementSequence(withPart.statements);
  7028. withEntries := withEntries.previous;
  7029. END;
  7030. END WithPart;
  7031. (** check and resolve with statement WITH variable: type DO ... END;
  7032. - check type and variable
  7033. - check that variable type is type extension of type
  7034. - check that variable is a variable
  7035. - enter new with scope and enter guardedVariable with same name and reference to variable
  7036. - create if statement:
  7037. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7038. **)
  7039. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7040. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7041. BEGIN
  7042. prevScope := currentScope; symbol := NIL;
  7043. FOR i := 0 TO withStatement.WithParts()-1 DO
  7044. WithPart(withStatement.GetWithPart(i),symbol);
  7045. END;
  7046. IF withStatement.elsePart # NIL THEN
  7047. StatementSequence(withStatement.elsePart)
  7048. END;
  7049. currentScope := prevScope;
  7050. END VisitWithStatement;
  7051. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7052. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7053. - check 'first' < 'last' and no overlaps between different case labels
  7054. - check statement sequence
  7055. **)
  7056. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7057. VAR
  7058. i: LONGINT;
  7059. position: LONGINT;
  7060. expression, left, right: SyntaxTree.Expression;
  7061. expressionType: SyntaxTree.Type;
  7062. l, r: LONGINT;
  7063. cl, cr: CHAR;
  7064. thiscases: SyntaxTree.CaseConstant;
  7065. BEGIN
  7066. thiscases := NIL;
  7067. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7068. expression := casePart.elements.GetExpression(i);
  7069. position := expression.position;
  7070. (* set context of range *)
  7071. IF expression IS SyntaxTree.RangeExpression THEN
  7072. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7073. END;
  7074. expression := ResolveExpression(expression);
  7075. IF expression = SyntaxTree.invalidExpression THEN
  7076. (* error already reported *)
  7077. expressionType := SyntaxTree.invalidType;
  7078. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7079. (* read out 'first' and 'last' *)
  7080. left := expression(SyntaxTree.RangeExpression).first;
  7081. right := expression(SyntaxTree.RangeExpression).last;
  7082. (* guaranteed by VisitRangeExpression: *)
  7083. ASSERT((left # NIL) & (right # NIL));
  7084. ASSERT(left.type.resolved = right.type.resolved);
  7085. left := CompatibleConversion(left.position, left, type);
  7086. right := CompatibleConversion(right.position, right, type);
  7087. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7088. expression(SyntaxTree.RangeExpression).SetLast(right);
  7089. expressionType := RegularType(position,left.type);
  7090. ELSE
  7091. expression := ConstantExpression(expression);
  7092. expression := CompatibleConversion(expression.position, expression, type);
  7093. (*
  7094. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7095. left := Global.NewCharacterValue(system,expression.position,cl);
  7096. expression := casePart.elements.GetExpression(i);
  7097. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7098. expression := left
  7099. END;
  7100. *)
  7101. casePart.elements.SetExpression(i,expression);
  7102. left := expression; right := expression;
  7103. expressionType := RegularType(position,expression.type)
  7104. END;
  7105. IF (expressionType = SyntaxTree.invalidType) THEN
  7106. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7107. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7108. expression := SyntaxTree.invalidExpression;
  7109. ELSE
  7110. l := 0; r := 0;
  7111. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7112. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7113. l := ORD(cl); r := ORD(cr);
  7114. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7115. ELSE
  7116. expression := SyntaxTree.invalidExpression
  7117. END;
  7118. IF expression # SyntaxTree.invalidExpression THEN
  7119. IF l>r THEN
  7120. Error(position,Diagnostics.Invalid,"empty case label")
  7121. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7122. Error(position,Diagnostics.Invalid,"duplicate case label");
  7123. ELSE
  7124. IF l < min THEN min := l END;
  7125. IF r > max THEN max := r END;
  7126. END;
  7127. END;
  7128. END;
  7129. casePart.elements.SetExpression(i,expression);
  7130. END;
  7131. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7132. casePart.SetConstants(thiscases);
  7133. StatementSequence(casePart.statements);
  7134. END CasePart;
  7135. (** check and resolve case statement CASE variable OF ... END;
  7136. - check variable
  7137. - check case parts
  7138. **)
  7139. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7140. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7141. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7142. BEGIN
  7143. expression := ResolveExpression(caseStatement.variable);
  7144. type := RegularType(expression.position,expression.type);
  7145. IF type = SyntaxTree.invalidType THEN
  7146. expression := SyntaxTree.invalidExpression;
  7147. ELSIF IsIntegerType(type) THEN
  7148. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7149. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7150. (*
  7151. expression := Global.NewCharacterValue(system,expression.position,ch);
  7152. *)
  7153. type := expression.type;
  7154. ELSIF IsCharacterType(type) THEN
  7155. ELSIF IsEnumerationType(type) THEN
  7156. ELSE
  7157. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7158. expression := SyntaxTree.invalidExpression;
  7159. END;
  7160. caseStatement.SetVariable(expression);
  7161. caseList := NIL;
  7162. min := MAX(LONGINT); max := MIN(LONGINT);
  7163. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7164. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7165. END;
  7166. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7167. msg := "huge sparse case table ";
  7168. Strings.AppendInt(msg, max-min);
  7169. Strings.Append(msg,"/");
  7170. Strings.AppendInt(msg, caseStatement.CaseParts());
  7171. Warning(caseStatement.position,msg);
  7172. END;
  7173. caseStatement.SetMinMax(min,max);
  7174. StatementSequence(caseStatement.elsePart);
  7175. IF expression.resolved # NIL THEN
  7176. IF IsCharacterValue(expression,ch) THEN
  7177. l := ORD(ch)
  7178. ELSIF IsIntegerValue(expression,l) THEN
  7179. END;
  7180. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7181. END;
  7182. END VisitCaseStatement;
  7183. (** check and resolve while statement
  7184. - check condition
  7185. - check statement sequence
  7186. **)
  7187. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7188. VAR prevIsUnreachable,b: BOOLEAN;
  7189. BEGIN
  7190. prevIsUnreachable := currentIsUnreachable;
  7191. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7192. IF IsBooleanValue(whileStatement.condition,b) THEN
  7193. IF b=FALSE THEN
  7194. currentIsUnreachable := TRUE
  7195. END;
  7196. END;
  7197. StatementSequence(whileStatement.statements);
  7198. currentIsUnreachable := prevIsUnreachable
  7199. END VisitWhileStatement;
  7200. (** check and resolve repeat statement
  7201. - check condition
  7202. - check statement sequence
  7203. **)
  7204. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7205. BEGIN
  7206. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7207. StatementSequence(repeatStatement.statements);
  7208. END VisitRepeatStatement;
  7209. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7210. VAR withEntry: WithEntry;
  7211. BEGIN
  7212. withEntry := withEntries;
  7213. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7214. withEntry := withEntry.previous
  7215. END;
  7216. IF withEntry = NIL THEN RETURN FALSE
  7217. ELSE
  7218. type := withEntry.type;
  7219. RETURN TRUE
  7220. END;
  7221. END GetGuard;
  7222. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7223. - check that variable is an integer variable
  7224. - check that from is integer typed with compatible type
  7225. - check that to has compatible type
  7226. - check that by is constant integer with compatible type
  7227. **)
  7228. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7229. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7230. BEGIN
  7231. designator := ResolveDesignator(forStatement.variable);
  7232. type := SyntaxTree.invalidType;
  7233. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7234. designator := SyntaxTree.invalidDesignator;
  7235. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7236. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7237. designator := SyntaxTree.invalidDesignator;
  7238. ELSIF CheckVariable(designator) THEN
  7239. type := designator.type;
  7240. END;
  7241. forStatement.SetVariable(designator);
  7242. expression := ResolveExpression(forStatement.from);
  7243. IF expression = SyntaxTree.invalidExpression THEN
  7244. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7245. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7246. expression := SyntaxTree.invalidExpression;
  7247. ELSIF type # SyntaxTree.invalidType THEN
  7248. expression := NewConversion(expression.position,expression,type,NIL)
  7249. END;
  7250. forStatement.SetFrom(expression);
  7251. expression := ResolveExpression(forStatement.to);
  7252. IF expression = SyntaxTree.invalidExpression THEN
  7253. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7254. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7255. expression := SyntaxTree.invalidExpression;
  7256. ELSIF type # SyntaxTree.invalidType THEN
  7257. expression := NewConversion(expression.position,expression,type,NIL)
  7258. END;
  7259. forStatement.SetTo(expression);
  7260. IF forStatement.by # NIL THEN
  7261. expression := ConstantInteger(forStatement.by);
  7262. ELSE
  7263. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7264. END;
  7265. IF expression = SyntaxTree.invalidExpression THEN
  7266. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7267. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7268. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7269. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7270. ELSIF type # SyntaxTree.invalidType THEN
  7271. expression := NewConversion(expression.position,expression,type,NIL)
  7272. END;
  7273. forStatement.SetBy(expression);
  7274. StatementSequence(forStatement.statements);
  7275. END VisitForStatement;
  7276. (** check and resolve loop statement LOOP StatementSequence END
  7277. - check statement sequence
  7278. **)
  7279. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7280. BEGIN
  7281. StatementSequence(loopStatement.statements)
  7282. END VisitLoopStatement;
  7283. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7284. BEGIN
  7285. StatementSequence(exitableBlock.statements);
  7286. END VisitExitableBlock;
  7287. (** check and resolve exit statement EXIT
  7288. - check that exit is within LOOP statement block
  7289. **)
  7290. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7291. VAR outer: SyntaxTree.Statement;
  7292. BEGIN
  7293. outer := exitStatement.outer;
  7294. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7295. outer := outer.outer;
  7296. END;
  7297. IF outer = NIL THEN
  7298. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7299. END;
  7300. END VisitExitStatement;
  7301. (** check and resolve return statement RETURN [expression]
  7302. - check expression (if any)
  7303. - check if in procedure scope
  7304. - if in procedure scope then check expression compatibility
  7305. - if not in procecdure scope then check on return without expression
  7306. **)
  7307. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7308. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7309. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7310. BEGIN
  7311. position := returnStatement.position;
  7312. expression := returnStatement.returnValue;
  7313. IF expression # NIL THEN
  7314. expression := ResolveExpression(expression);
  7315. returnStatement.SetReturnValue(expression);
  7316. END;
  7317. outer := returnStatement.outer;
  7318. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7319. outer := outer.outer
  7320. END;
  7321. IF (outer # NIL) THEN
  7322. scope := outer(SyntaxTree.Body).inScope;
  7323. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7324. IF (expression # NIL) THEN
  7325. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7326. END;
  7327. ELSE
  7328. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7329. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7330. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7331. END;
  7332. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7333. IF returnType # NIL THEN
  7334. returnType := returnType.resolved;
  7335. IF expression = NIL THEN
  7336. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7337. ELSIF expression.type = NIL THEN
  7338. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7339. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7340. Error(position,Diagnostics.Invalid,"return type not compatible");
  7341. IF VerboseErrorMessage THEN
  7342. Printout.Info("returnType",returnType);
  7343. Printout.Info("expression",expression);
  7344. END;
  7345. ELSE
  7346. expression := NewConversion(expression.position,expression,returnType,NIL);
  7347. returnStatement.SetReturnValue(expression);
  7348. END;
  7349. ELSIF expression # NIL THEN
  7350. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7351. END;
  7352. END;
  7353. END;
  7354. END VisitReturnStatement;
  7355. (** check and resolve await statement AWAIT(condition: Expression)
  7356. - check await condition
  7357. **)
  7358. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7359. VAR condition: SyntaxTree.Expression;
  7360. BEGIN
  7361. condition := ResolveCondition(awaitStatement.condition);
  7362. IF currentIsRealtime THEN
  7363. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7364. END;
  7365. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7366. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7367. END;
  7368. awaitStatement.SetCondition(condition);
  7369. END VisitAwaitStatement;
  7370. PROCEDURE CheckSystemImport(position: LONGINT);
  7371. VAR import: SyntaxTree.Import;
  7372. BEGIN
  7373. import := currentScope.ownerModule.moduleScope.firstImport;
  7374. WHILE(import # NIL) DO
  7375. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7376. RETURN;
  7377. END;
  7378. import := import.nextImport;
  7379. END;
  7380. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7381. END CheckSystemImport;
  7382. (** check and resolve code statement: do nothing, must be done by assembler
  7383. **)
  7384. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7385. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7386. BEGIN
  7387. CheckSystemImport(code.position);
  7388. FOR i := 0 TO code.inRules.Length()-1 DO
  7389. statement := code.inRules.GetStatement(i);
  7390. IF statement IS SyntaxTree.Assignment THEN
  7391. WITH statement: SyntaxTree.Assignment DO
  7392. statement.SetRight(ResolveExpression(statement.right));
  7393. END;
  7394. ELSE
  7395. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7396. END;
  7397. END;
  7398. FOR i := 0 TO code.outRules.Length()-1 DO
  7399. statement := code.outRules.GetStatement(i);
  7400. IF statement IS SyntaxTree.Assignment THEN
  7401. WITH statement: SyntaxTree.Assignment DO
  7402. statement.SetLeft(ResolveDesignator(statement.left));
  7403. END;
  7404. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7405. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7406. ELSE
  7407. Printout.Info("out statement ", statement);
  7408. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7409. END;
  7410. END;
  7411. END VisitCode;
  7412. (** check and set flags of a statement block
  7413. - check for multiply occurence of a flag
  7414. - check and set priority only in bodies
  7415. - check for valid names
  7416. **)
  7417. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7418. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7419. flag: LONGINT; recordBody: SyntaxTree.Body;
  7420. PROCEDURE SetProtectedRecord;
  7421. VAR scope: SyntaxTree.Scope;
  7422. BEGIN
  7423. scope := currentScope;
  7424. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7425. scope := scope.outerScope
  7426. END;
  7427. IF scope # NIL THEN
  7428. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7429. END;
  7430. END SetProtectedRecord;
  7431. BEGIN
  7432. flags := {};
  7433. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7434. recordBody := block(SyntaxTree.Body)
  7435. ELSE
  7436. recordBody := NIL
  7437. END;
  7438. blockModifier := block.blockModifiers;
  7439. WHILE(blockModifier # NIL) DO
  7440. name := blockModifier.identifier;
  7441. expression := blockModifier.expression;
  7442. position := blockModifier.position;
  7443. flag := -1;
  7444. IF name=Global.NamePriority THEN
  7445. IF expression = NIL THEN
  7446. Error(position,Diagnostics.Invalid,"missing priority expression");
  7447. ELSIF recordBody = NIL THEN
  7448. Error(position,Diagnostics.Invalid,"priority not on record body");
  7449. ELSIF recordBody.priority # NIL THEN
  7450. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7451. ELSE
  7452. recordBody.SetPriority(expression);
  7453. END;
  7454. ELSIF expression # NIL THEN
  7455. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7456. ELSIF name=Global.NameExclusive THEN
  7457. IF block.isExclusive THEN
  7458. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7459. END;
  7460. block.SetExclusive(TRUE); SetProtectedRecord;
  7461. ELSIF name=Global.NameActive THEN
  7462. IF recordBody = NIL THEN
  7463. Error(position,Diagnostics.Invalid,"active not in record body");
  7464. ELSIF recordBody.isActive THEN
  7465. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7466. ELSE
  7467. recordBody.SetActive(TRUE); SetProtectedRecord;
  7468. END;
  7469. ELSIF name=Global.NameSafe THEN
  7470. IF recordBody = NIL THEN
  7471. Error(position,Diagnostics.Invalid,"safe not in record body");
  7472. ELSIF recordBody.isSafe THEN
  7473. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7474. ELSE
  7475. recordBody.SetSafe(TRUE);
  7476. SetProtectedRecord;
  7477. END;
  7478. ELSIF name=Global.NameRealtime THEN
  7479. IF recordBody = NIL THEN
  7480. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7481. ELSIF recordBody.isRealtime THEN
  7482. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7483. ELSE
  7484. recordBody.SetRealtime(TRUE);
  7485. block.SetRealtime(TRUE);
  7486. END;
  7487. ELSIF name=Global.NameUnchecked THEN
  7488. IF block.isUnchecked THEN
  7489. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7490. ELSE
  7491. block.SetUnchecked(TRUE);
  7492. END;
  7493. ELSIF (name=Global.NameUncooperative) THEN
  7494. IF block.isUncooperative THEN
  7495. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7496. ELSE
  7497. block.SetUncooperative(TRUE);
  7498. END;
  7499. ELSE
  7500. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7501. END;
  7502. blockModifier := blockModifier.nextModifier;
  7503. END;
  7504. END BlockFlags;
  7505. (** check and resolve statement block
  7506. - check flags (exclusive)
  7507. - check statement sequence
  7508. **)
  7509. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7510. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7511. BEGIN
  7512. BlockFlags(statementBlock);
  7513. IF statementBlock.isExclusive THEN
  7514. (* check that not in exclusive block *)
  7515. IF currentIsExclusive THEN
  7516. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7517. ELSIF currentIsRealtime THEN
  7518. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7519. END;
  7520. END;
  7521. recentExclusive := currentIsExclusive;
  7522. recentUnreachable := currentIsUnreachable;
  7523. recentRealtime := currentIsRealtime;
  7524. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7525. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7526. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7527. StatementSequence(statementBlock.statements);
  7528. currentIsRealtime := recentRealtime;
  7529. currentIsExclusive := recentExclusive;
  7530. currentIsUnreachable := recentUnreachable;
  7531. END VisitStatementBlock;
  7532. (** check and resolve body
  7533. - check flags (active, priority, safe)
  7534. - check body and finally part
  7535. **)
  7536. PROCEDURE Body(body: SyntaxTree.Body);
  7537. BEGIN
  7538. VisitStatementBlock(body);
  7539. IF body.isActive THEN
  7540. IF ~currentIsBodyProcedure THEN
  7541. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7542. ELSIF body.priority # NIL THEN
  7543. body.SetPriority(ConstantInteger(body.priority));
  7544. END;
  7545. ELSIF body.isSafe THEN
  7546. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7547. ELSIF body.priority # NIL THEN
  7548. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7549. END;
  7550. IF body.code # NIL THEN
  7551. CheckSystemImport(body.position);
  7552. END;
  7553. StatementSequence(body.finally)
  7554. END Body;
  7555. (*** scopes ***)
  7556. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7557. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7558. VAR duplicateSymbol: BOOLEAN;
  7559. BEGIN
  7560. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7561. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7562. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7563. END;
  7564. scope.EnterSymbol(symbol,duplicateSymbol);
  7565. IF ~allowDuplicate & duplicateSymbol THEN
  7566. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7567. IF VerboseErrorMessage THEN
  7568. Printout.Info("multiply defined identifier",symbol);
  7569. Printout.Info("in scope",scope);
  7570. END;
  7571. END;
  7572. END Register;
  7573. (**
  7574. implementation: check and resolve an implementation part
  7575. **)
  7576. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7577. move implementation checker to a separate object ? *)
  7578. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7579. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7580. BEGIN
  7581. prevIsRealtime := currentIsRealtime;
  7582. prevIsBodyProcedure := currentIsBodyProcedure;
  7583. prevIsCellNet := currentIsCellNet;
  7584. prevScope := currentScope;
  7585. currentScope := scope;
  7586. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7587. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7588. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7589. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7590. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7591. (*
  7592. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7593. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7594. END;
  7595. *)
  7596. END;
  7597. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7598. (* module body, record bodies are wrapped into an artifical procedure *)
  7599. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7600. Body(scope(SyntaxTree.ProcedureScope).body)
  7601. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7602. Body(scope(SyntaxTree.ProcedureScope).body)
  7603. END;
  7604. END;
  7605. currentScope := prevScope;
  7606. currentIsRealtime := prevIsRealtime;
  7607. currentIsBodyProcedure := prevIsBodyProcedure;
  7608. currentIsCellNet := prevIsCellNet;
  7609. END Implementation;
  7610. (** implementation phase:
  7611. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7612. **)
  7613. PROCEDURE Implementations(x: SyntaxTree.Module);
  7614. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7615. BEGIN
  7616. prevPhase := phase;
  7617. phase := InlinePhase;
  7618. scope := x.firstScope;
  7619. WHILE(scope # NIL) DO
  7620. Implementation(scope);
  7621. scope := scope.nextScope;
  7622. END;
  7623. phase := ImplementationPhase;
  7624. scope := x.firstScope;
  7625. WHILE(scope # NIL) DO
  7626. Implementation(scope);
  7627. scope := scope.nextScope;
  7628. END;
  7629. phase := prevPhase;
  7630. END Implementations;
  7631. (** declaration phase:
  7632. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7633. - import lists (for module scopes)
  7634. - parameter list (for procedure scopes)
  7635. - constant declarations
  7636. - type declarations
  7637. - variable declarations
  7638. - procedure declarations
  7639. preformed in two stages:
  7640. - first all symbols are entered into the symbol table (with uniqueness check),
  7641. - then all symbols are resolved
  7642. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7643. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7644. **)
  7645. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7646. VAR
  7647. constant: SyntaxTree.Constant;
  7648. typeDeclaration: SyntaxTree.TypeDeclaration;
  7649. variable: SyntaxTree.Variable;
  7650. procedure: SyntaxTree.Procedure;
  7651. prevScope: SyntaxTree.Scope;
  7652. parameter: SyntaxTree.Parameter;
  7653. import: SyntaxTree.Import;
  7654. symbol: SyntaxTree.Symbol;
  7655. prevPhase: LONGINT;
  7656. prevError : BOOLEAN;
  7657. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7658. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7659. BEGIN
  7660. IF type.baseType # NIL THEN
  7661. baseType := type.baseType.resolved;
  7662. IF baseType IS SyntaxTree.PointerType THEN
  7663. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7664. END;
  7665. IF baseType IS SyntaxTree.CellType THEN
  7666. DeclareCell(baseType(SyntaxTree.CellType));
  7667. END;
  7668. END;
  7669. parameter := type.firstParameter;
  7670. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7671. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7672. variable.SetType(parameter.type);
  7673. variable.SetAccess(SyntaxTree.Hidden);
  7674. variable.SetModifiers(parameter.modifiers);
  7675. currentScope.PushVariable(variable);
  7676. (*
  7677. Register(parameter,scope, FALSE);
  7678. *)
  7679. parameter := parameter.nextParameter;
  7680. END;
  7681. property := type.firstProperty;
  7682. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7683. variable := currentScope.FindVariable(property.name);
  7684. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7685. prop := variable(SyntaxTree.Property);
  7686. ELSE (* add, duplicate symbols detection later *)
  7687. prop := SyntaxTree.NewProperty(property.position, property.name);
  7688. currentScope.PushVariable(prop);
  7689. END;
  7690. prop.SetType(property.type);
  7691. prop.SetValue(property.value);
  7692. prop.SetAccess(SyntaxTree.Hidden);
  7693. property := property.nextProperty;
  7694. END;
  7695. END DeclareCell;
  7696. BEGIN
  7697. prevError := error;
  7698. prevPhase := phase;
  7699. phase := DeclarationPhase;
  7700. prevScope := currentScope;
  7701. currentScope := scope;
  7702. error := FALSE;
  7703. (* first enter all symbols in scope *)
  7704. IF scope IS SyntaxTree.ModuleScope THEN
  7705. (* treat imports first for a module scope, , set default context if necessary *)
  7706. import := scope(SyntaxTree.ModuleScope).firstImport;
  7707. WHILE(import # NIL) DO
  7708. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7709. Register(import, currentScope, FALSE);
  7710. import := import.nextImport;
  7711. END;
  7712. import := scope(SyntaxTree.ModuleScope).firstImport;
  7713. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7714. ResolveSymbol(import);
  7715. import := import.nextImport;
  7716. END;
  7717. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7718. (* enter parameters for a procedure scope *)
  7719. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7720. WHILE(parameter # NIL) DO
  7721. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7722. END;
  7723. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7724. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7725. ELSIF scope IS SyntaxTree.CellScope THEN
  7726. IF~skipImplementation THEN
  7727. import := scope(SyntaxTree.CellScope).firstImport;
  7728. WHILE(import # NIL) DO
  7729. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7730. Register(import, currentScope, FALSE);
  7731. import := import.nextImport;
  7732. END;
  7733. import := scope(SyntaxTree.CellScope).firstImport;
  7734. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7735. ResolveSymbol(import);
  7736. import := import.nextImport;
  7737. END;
  7738. END;
  7739. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7740. END;
  7741. IF error THEN RETURN END;
  7742. (* constants *)
  7743. constant := scope.firstConstant;
  7744. WHILE (constant # NIL) DO
  7745. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7746. END;
  7747. (* type declarations *)
  7748. typeDeclaration := scope.firstTypeDeclaration;
  7749. WHILE (typeDeclaration # NIL) DO
  7750. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7751. END;
  7752. (* variables *)
  7753. variable := scope.firstVariable;
  7754. WHILE (variable # NIL) DO
  7755. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7756. END;
  7757. (* procedures *)
  7758. procedure := scope.firstProcedure;
  7759. WHILE (procedure # NIL) DO
  7760. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7761. END;
  7762. IF ~skipImplementation THEN
  7763. (* now process all symbols without any presumption on the order *)
  7764. symbol := scope.firstSymbol;
  7765. WHILE(symbol # NIL) DO
  7766. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7767. ResolveSymbol(symbol);
  7768. END;
  7769. symbol := symbol.nextSymbol;
  7770. END;
  7771. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7772. symbol := scope.firstSymbol;
  7773. WHILE symbol # NIL DO
  7774. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7775. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7776. pointerFixes.Add(symbol, currentScope);
  7777. END;
  7778. IF ~symbol.type.resolved.isRealtime THEN
  7779. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7780. END;
  7781. END;
  7782. symbol := symbol.nextSymbol
  7783. END;
  7784. END;
  7785. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7786. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7787. END;
  7788. IF (scope.ownerModule # NIL) THEN
  7789. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7790. scope.ownerModule.AddScope(scope);
  7791. END;
  7792. END;
  7793. phase := prevPhase;
  7794. currentScope := prevScope;
  7795. error := error OR prevError;
  7796. END Declarations;
  7797. (* nopov *)
  7798. (** check if all operators from one module are compatible to the ones in the other module
  7799. - check if there are not multiple operators with the same signature
  7800. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7801. - check for all operators whose signatures are compatible, whether the return types are compatible
  7802. note that:
  7803. - the return type is not considered to be part of the signature
  7804. - two signatures are considered compatible, if all of the operands are compatible
  7805. **)
  7806. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7807. VAR
  7808. thisOperator, thatOperator: SyntaxTree.Operator;
  7809. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7810. thisParameter, thatParameter: SyntaxTree.Parameter;
  7811. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7812. i: LONGINT;
  7813. BEGIN
  7814. currentScope := thisModuleScope;
  7815. hasError := FALSE;
  7816. (* go through all operators in the other module *)
  7817. thatOperator := thatModuleScope.firstOperator;
  7818. WHILE (thatOperator # NIL) & ~hasError DO
  7819. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7820. (* the other operator is accessible *)
  7821. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7822. (* the other operator is not the conversion operator *)
  7823. (* go through all operators in this module *)
  7824. thisOperator := thisModuleScope.firstOperator;
  7825. WHILE (thisOperator # NIL) & ~hasError DO
  7826. IF thisOperator # thatOperator THEN
  7827. (* the operators are not the same *)
  7828. IF thisOperator.name = thatOperator.name THEN
  7829. (* the operators share the same identifier *)
  7830. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7831. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7832. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7833. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7834. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7835. (* both operators have the same paramter count *)
  7836. thisParameter := thisProcedureType.firstParameter;
  7837. thatParameter := thatProcedureType.firstParameter;
  7838. operandsAreEqual := TRUE;
  7839. operandsAreCompatible := TRUE;
  7840. (* go through all parameters *)
  7841. FOR i := 1 TO thisProcedureType.numberParameters DO
  7842. ASSERT(thatParameter # NIL);
  7843. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7844. operandsAreEqual := FALSE;
  7845. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7846. operandsAreCompatible := FALSE
  7847. END
  7848. END;
  7849. thisParameter := thisParameter.nextParameter;
  7850. thatParameter := thatParameter.nextParameter
  7851. END;
  7852. IF operandsAreEqual THEN
  7853. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7854. hasError := TRUE
  7855. ELSIF operandsAreCompatible THEN
  7856. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7857. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7858. hasError := TRUE
  7859. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7860. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7861. hasError := TRUE
  7862. END
  7863. END
  7864. END
  7865. END
  7866. END;
  7867. thisOperator := thisOperator.nextOperator
  7868. END
  7869. END
  7870. END;
  7871. thatOperator := thatOperator.nextOperator
  7872. END
  7873. END CheckInterOperatorConformity;
  7874. (** check module:
  7875. - check module declaration
  7876. - add context, if necessary
  7877. - remove module from import cache, if necessary
  7878. - check declarations
  7879. - resolve all type fixes
  7880. - check implementation (bodies)
  7881. **)
  7882. PROCEDURE Module*(x: SyntaxTree.Module);
  7883. VAR (* nopov *)
  7884. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7885. BEGIN
  7886. prevScope := currentScope;
  7887. prevIsCellNet := currentIsCellNet;
  7888. module := x;
  7889. ASSERT(x # NIL);
  7890. global := system.globalScope[x.case];
  7891. x.moduleScope.SetGlobalScope(global);
  7892. currentScope := global;
  7893. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7894. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7895. RemoveModuleFromCache(importCache,x);
  7896. Declarations(x.moduleScope, FALSE);
  7897. FixTypes();
  7898. IF module.isCellNet THEN
  7899. currentIsCellNet := TRUE;
  7900. modifier := x.modifiers;
  7901. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7902. CheckModifiers(modifier, FALSE);
  7903. END;
  7904. (* nopov *)
  7905. IF ~error THEN
  7906. (* check if operators conform to each other within this module *)
  7907. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7908. (* go through all imports *)
  7909. import := x.moduleScope.firstImport;
  7910. WHILE import # NIL DO
  7911. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7912. (* check if all operators in this module conform to the ones of the imported module *)
  7913. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7914. END;
  7915. import := import.nextImport
  7916. END;
  7917. END;
  7918. Implementations(x);
  7919. module := NIL;
  7920. currentIsCellNet := prevIsCellNet;
  7921. currentScope := prevScope;
  7922. END Module;
  7923. END Checker;
  7924. Warnings*=OBJECT (SyntaxTree.Visitor)
  7925. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7926. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7927. BEGIN
  7928. SELF.diagnostics := diagnostics
  7929. END InitWarnings;
  7930. (** types *)
  7931. PROCEDURE Type(x: SyntaxTree.Type);
  7932. BEGIN x.Accept(SELF)
  7933. END Type;
  7934. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7935. BEGIN END VisitType;
  7936. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7937. BEGIN END VisitBasicType;
  7938. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7939. BEGIN END VisitCharacterType;
  7940. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7941. BEGIN END VisitIntegerType;
  7942. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7943. BEGIN END VisitFloatType;
  7944. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7945. BEGIN END VisitQualifiedType;
  7946. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7947. BEGIN END VisitStringType;
  7948. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7949. BEGIN END VisitEnumerationType;
  7950. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7951. BEGIN END VisitRangeType;
  7952. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7953. BEGIN
  7954. IF ~(SyntaxTree.Warned IN x.state) THEN
  7955. x.SetState(SyntaxTree.Warned);
  7956. Type(x.arrayBase);
  7957. END;
  7958. END VisitArrayType;
  7959. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7960. BEGIN
  7961. IF ~(SyntaxTree.Warned IN x.state) THEN
  7962. x.SetState(SyntaxTree.Warned);
  7963. Type(x.arrayBase);
  7964. END;
  7965. END VisitMathArrayType;
  7966. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7967. BEGIN
  7968. IF ~(SyntaxTree.Warned IN x.state) THEN
  7969. x.SetState(SyntaxTree.Warned);
  7970. Type(x.pointerBase);
  7971. END;
  7972. END VisitPointerType;
  7973. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7974. BEGIN Scope(x.recordScope) END VisitRecordType;
  7975. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7976. BEGIN Scope(x.cellScope) END VisitCellType;
  7977. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7978. BEGIN END VisitProcedureType;
  7979. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7980. VAR msg: ARRAY 256 OF CHAR;
  7981. BEGIN
  7982. Global.GetSymbolName(x,msg);
  7983. Strings.Append(msg," ");
  7984. Strings.Append(msg,text);
  7985. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7986. END Warning;
  7987. (** symbols *)
  7988. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7989. BEGIN
  7990. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7991. IF ~(x IS SyntaxTree.Parameter) THEN
  7992. Warning(x,"never used");
  7993. END;
  7994. END;
  7995. x.Accept(SELF);
  7996. END Symbol;
  7997. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7998. BEGIN END VisitSymbol;
  7999. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8000. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8001. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8002. BEGIN END VisitConstant;
  8003. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8004. BEGIN END VisitVariable;
  8005. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8006. BEGIN END VisitProperty;
  8007. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8008. BEGIN END VisitParameter;
  8009. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8010. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8011. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8012. BEGIN END VisitOperator;
  8013. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8014. BEGIN END VisitImport;
  8015. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8016. VAR
  8017. symbol: SyntaxTree.Symbol;
  8018. BEGIN
  8019. symbol := scope.firstSymbol;
  8020. WHILE(symbol # NIL) DO
  8021. Symbol(symbol);
  8022. symbol := symbol.nextSymbol;
  8023. END;
  8024. END Scope;
  8025. PROCEDURE Module*(x: SyntaxTree.Module);
  8026. BEGIN
  8027. SELF.module := x;
  8028. Scope(x.moduleScope);
  8029. END Module;
  8030. END Warnings;
  8031. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8032. BEGIN
  8033. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8034. END IsOberonInline;
  8035. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8036. BEGIN
  8037. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8038. END Resolved;
  8039. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8040. VAR i: LONGINT;
  8041. BEGIN
  8042. i := 1;
  8043. WHILE i < x DO
  8044. i := i *2
  8045. END;
  8046. RETURN i=x
  8047. END PowerOf2;
  8048. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8049. BEGIN
  8050. RETURN
  8051. (scope # NIL) &
  8052. (scope IS SyntaxTree.ModuleScope)
  8053. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8054. OR
  8055. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8056. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8057. END IsCellNetScope;
  8058. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8059. BEGIN
  8060. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8061. END IsCellScope;
  8062. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8063. BEGIN
  8064. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8065. RETURN (scope # NIL) & IsCellNetScope(scope)
  8066. END InCellNetScope;
  8067. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8068. BEGIN
  8069. ASSERT(size MOD system.dataUnit = 0);
  8070. RETURN size DIV system.dataUnit
  8071. END ToMemoryUnits;
  8072. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8073. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8074. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8075. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8076. BEGIN
  8077. IF t = NIL THEN
  8078. RETURN TRUE
  8079. ELSE
  8080. t := t.resolved;
  8081. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8082. END;
  8083. END TypeAllowed;
  8084. BEGIN
  8085. type := type.resolved;
  8086. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8087. RETURN FALSE
  8088. ELSE
  8089. procedureType := type(SyntaxTree.ProcedureType);
  8090. numberParameters := procedureType.numberParameters;
  8091. RETURN
  8092. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8093. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8094. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8095. END;
  8096. END GetProcedureAllowed;
  8097. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8098. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8099. VAR import: SyntaxTree.Import;
  8100. BEGIN
  8101. import := importCache.ImportByModuleName(x.name,x.context);
  8102. IF import # NIL THEN
  8103. importCache.RemoveImporters(x.name,x.context);
  8104. END;
  8105. END RemoveModuleFromCache;
  8106. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8107. (* to <- this assignment compatibility *)
  8108. VAR result: BOOLEAN;
  8109. BEGIN
  8110. IF this= NIL THEN result := (to=NIL)
  8111. ELSIF to=NIL THEN result := FALSE
  8112. ELSE
  8113. (*! will be replaced by this:
  8114. ELSE result := this.CompatibleTo(to.resolved);
  8115. *)
  8116. this := this.resolved; to := to.resolved;
  8117. IF to=SyntaxTree.invalidType THEN result := FALSE
  8118. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8119. ELSIF to = this THEN
  8120. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8121. ELSIF to IS SyntaxTree.BasicType THEN
  8122. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8123. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8124. result := this.CompatibleTo(to.resolved)
  8125. ELSE
  8126. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8127. END
  8128. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8129. result := to.sizeInBits = this.sizeInBits;
  8130. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8131. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8132. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8133. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8134. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8135. result := TRUE;
  8136. ELSIF to IS SyntaxTree.AnyType THEN
  8137. 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);
  8138. ELSIF to IS SyntaxTree.ObjectType THEN
  8139. 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 *) ;
  8140. ELSIF to IS SyntaxTree.ByteType THEN
  8141. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8142. ELSIF to IS SyntaxTree.CharacterType THEN
  8143. result := IsCharacterType(this)
  8144. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8145. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8146. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8147. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8148. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8149. result := TRUE;
  8150. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8151. result := TRUE;
  8152. ELSE
  8153. result := FALSE
  8154. END;
  8155. ELSIF to IS SyntaxTree.PointerType THEN
  8156. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8157. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8158. & (~to.isRealtime OR this.isRealtime);
  8159. ELSIF to IS SyntaxTree.ProcedureType THEN
  8160. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8161. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8162. & (~to.isRealtime OR this.isRealtime)
  8163. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8164. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8165. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8166. ELSIF to IS SyntaxTree.RecordType THEN
  8167. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8168. ELSIF to IS SyntaxTree.ArrayType THEN
  8169. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8170. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8171. ELSIF StaticArrayCompatible(to, this) THEN
  8172. result := TRUE
  8173. ELSE
  8174. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8175. END;
  8176. ELSIF to IS SyntaxTree.MathArrayType THEN
  8177. IF this IS SyntaxTree.MathArrayType THEN
  8178. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8179. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8180. result := TRUE;
  8181. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8182. result := TRUE;
  8183. ELSE
  8184. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8185. END;
  8186. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8187. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8188. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8189. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8190. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8191. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8192. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8193. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8194. ELSE
  8195. result := FALSE
  8196. END;
  8197. (* an array-structured object type is compatible to the type of its array structure *)
  8198. ELSIF IsArrayStructuredObjectType(this) THEN
  8199. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8200. ELSE
  8201. result := FALSE;
  8202. END;
  8203. ELSIF to IS SyntaxTree.StringType THEN
  8204. result := FALSE;
  8205. ELSIF to IS SyntaxTree.EnumerationType THEN
  8206. result := IsEnumerationExtension(this,to);
  8207. ELSIF to IS SyntaxTree.PortType THEN
  8208. result := SameType(to, this)
  8209. ELSE
  8210. Printout.Info("CompatibleTo",to);
  8211. HALT(100); (* implement missing type check *)
  8212. END;
  8213. END;
  8214. RETURN result
  8215. END CompatibleTo;
  8216. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8217. VAR actualBase, formalBase: SyntaxTree.Type;
  8218. BEGIN
  8219. IF SameType(formal,actual) THEN
  8220. RETURN TRUE
  8221. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8222. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8223. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8224. RETURN
  8225. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8226. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8227. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8228. & StaticArrayCompatible(formalBase,actualBase)
  8229. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8230. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8231. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8232. RETURN
  8233. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8234. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8235. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8236. & StaticArrayCompatible(formalBase,actualBase)
  8237. ELSE RETURN FALSE
  8238. END;
  8239. END StaticArrayCompatible;
  8240. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8241. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8242. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8243. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8244. BEGIN
  8245. result := SameType(formal,actual);
  8246. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8247. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8248. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8249. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8250. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8251. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8252. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8253. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8254. & TC(formalBase, actualBase);
  8255. END;
  8256. RETURN result
  8257. END TC;
  8258. BEGIN
  8259. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8260. ELSE
  8261. arrayBase := formalType.arrayBase.resolved;
  8262. IF (actualType IS SyntaxTree.StringType) THEN
  8263. result := arrayBase IS SyntaxTree.CharacterType
  8264. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8265. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8266. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8267. result := TC(formalType, actualType);
  8268. ELSE
  8269. result := (arrayBase IS SyntaxTree.ByteType)
  8270. END;
  8271. END;
  8272. RETURN result
  8273. END OpenArrayCompatible;
  8274. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8275. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8276. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8277. BEGIN
  8278. IF actualType IS SyntaxTree.MathArrayType THEN
  8279. actualArray := actualType(SyntaxTree.MathArrayType);
  8280. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8281. (*
  8282. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8283. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8284. *)
  8285. actualBase := ArrayBase(actualType,Infinity);
  8286. formalBase := ArrayBase(formalType,Infinity);
  8287. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8288. ELSE
  8289. (*
  8290. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8291. *)
  8292. formalBase := Resolved(formalType.arrayBase);
  8293. actualBase := Resolved(actualArray.arrayBase);
  8294. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8295. (*
  8296. ARRAY [k] -> ARRAY [n]
  8297. *)
  8298. result := (formalType.staticLength = actualArray.staticLength)
  8299. ELSE
  8300. result := TRUE
  8301. END;
  8302. IF ~result THEN
  8303. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8304. ELSIF actualBase = NIL THEN result := FALSE
  8305. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8306. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8307. ELSE
  8308. result := SameType(formalBase,actualBase)
  8309. END;
  8310. END;
  8311. ELSE
  8312. result := FALSE
  8313. END;
  8314. RETURN result
  8315. END MathArrayCompatible;
  8316. (**
  8317. Math Array Type distance for assignments / parameter passings of the form
  8318. from -> to
  8319. variants:
  8320. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8321. allowed:
  8322. static -> static (& size match)
  8323. static -> open
  8324. static -> tensor
  8325. open -> open
  8326. open -> tensor
  8327. open -> static
  8328. tensor -> tensor
  8329. tensor -> open
  8330. tensor -> static
  8331. **)
  8332. (*! think about the metric here: is form matching more important than element type matching? *)
  8333. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8334. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8335. BEGIN
  8336. fromBase := Resolved(from.arrayBase);
  8337. toBase := Resolved(to.arrayBase);
  8338. i := Infinity;
  8339. IF from = to THEN
  8340. i := 0;
  8341. ELSIF (from.form = to.form) THEN
  8342. (* static -> static, open -> open, tensor -> tensor *)
  8343. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8344. IF fromBase = toBase THEN i := 0
  8345. ELSIF toBase = NIL THEN i := 1
  8346. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8347. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8348. ELSE
  8349. i := TypeDistance(system,fromBase, toBase, varpar);
  8350. END;
  8351. END;
  8352. ELSIF (to.form = SyntaxTree.Static) THEN
  8353. (* forbidden *)
  8354. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8355. (* static -> tensor, open -> tensor, tensor -> open *)
  8356. IF toBase=fromBase THEN i := 0;
  8357. ELSIF toBase = NIL THEN i := 1;
  8358. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8359. toBase := ArrayBase(toBase,Infinity);
  8360. IF (fromBase=toBase) THEN i := 0
  8361. ELSIF (toBase = NIL) THEN i:= 1
  8362. ELSIF (fromBase = NIL) THEN i := Infinity;
  8363. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8364. END;
  8365. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8366. fromBase := ArrayBase(fromBase,Infinity);
  8367. IF (fromBase=toBase) THEN i := 0
  8368. ELSIF (toBase = NIL) THEN i := 1
  8369. ELSIF (fromBase = NIL) THEN i := Infinity;
  8370. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8371. END;
  8372. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8373. END;
  8374. IF i # Infinity THEN INC(i,2) END;
  8375. ELSIF (from.form = SyntaxTree.Static) THEN
  8376. (* static -> open *)
  8377. IF toBase=fromBase THEN i := 0
  8378. ELSIF toBase = NIL THEN i := 1
  8379. ELSIF fromBase = NIL THEN i := Infinity
  8380. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8381. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8382. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8383. END;
  8384. IF i # Infinity THEN INC(i,1) END;
  8385. ELSE HALT(100); (* unknown case *)
  8386. END;
  8387. RETURN i;
  8388. END MathArrayTypeDistance;
  8389. (** compute and return the distance of two array types
  8390. - return the distance of the base types
  8391. **)
  8392. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8393. VAR i: LONGINT;
  8394. BEGIN
  8395. i := Infinity;
  8396. IF from = to THEN
  8397. i := 0
  8398. ELSE
  8399. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8400. (*
  8401. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8402. i := TypeDistance(from.base, to.base);
  8403. IF i >= 0 THEN INC(i) END
  8404. ELSIF (from.mode = open) & (to.mode = open) THEN
  8405. i := TypeDistance(from.base, to.base);
  8406. *)
  8407. END;
  8408. RETURN i
  8409. END ArrayTypeDistance;
  8410. (** compute the signature distance of a procedure and an actual parameter list
  8411. - if any of the parameters are not compatible, the result is infinite
  8412. - add up and return the distance over all parameters
  8413. **)
  8414. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8415. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8416. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8417. BEGIN
  8418. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8419. result := Infinity
  8420. ELSE
  8421. formalParameter := procedureType.firstParameter;
  8422. i := 0;
  8423. result := 0;
  8424. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8425. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8426. actualParameter := actualParameters.GetExpression(i);
  8427. ASSERT(formalParameter.type # NIL);
  8428. IF (actualParameter.type = NIL) THEN distance := Infinity
  8429. ELSE
  8430. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8431. END;
  8432. IF distance = Infinity THEN
  8433. result := Infinity;
  8434. ELSE
  8435. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8436. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8437. (* already handled varpar *)
  8438. (*
  8439. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8440. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8442. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8443. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8444. END;
  8445. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8446. result := Infinity
  8447. END;
  8448. *)
  8449. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8450. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8451. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8452. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8453. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8454. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8455. END;
  8456. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8457. result := Infinity
  8458. END;
  8459. ELSE
  8460. result := Infinity
  8461. END;
  8462. ELSE
  8463. INC(result,distance);
  8464. END;
  8465. END;
  8466. formalParameter := formalParameter.nextParameter; INC(i);
  8467. END;
  8468. END;
  8469. ASSERT(result >= 0);
  8470. RETURN result
  8471. END Distance;
  8472. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8473. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8474. BEGIN
  8475. IF right.numberParameters # (procedureType.numberParameters) THEN
  8476. result := Infinity
  8477. ELSE
  8478. formalParameter := procedureType.firstParameter;
  8479. rightParameter := right.firstParameter;
  8480. i := 0;
  8481. result := 0;
  8482. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8483. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8484. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8485. IF distance = Infinity THEN
  8486. result := Infinity;
  8487. ELSE
  8488. INC(result,distance);
  8489. END;
  8490. formalParameter := formalParameter.nextParameter;
  8491. rightParameter := rightParameter.nextParameter;
  8492. END;
  8493. END;
  8494. ASSERT(result >= 0);
  8495. RETURN result
  8496. END ProcedureTypeDistance;
  8497. (** compute and return the distance between two types, used for computation of signature distance
  8498. from -> to
  8499. **)
  8500. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8501. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8502. BEGIN
  8503. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8504. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8505. END;
  8506. i := Infinity;
  8507. IF from = to THEN
  8508. i := 0
  8509. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8510. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8511. i := Infinity;
  8512. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8513. i := 1
  8514. ELSIF (from IS SyntaxTree.StringType) THEN
  8515. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8516. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8517. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8518. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8519. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8520. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8521. i := 1
  8522. ELSIF (from IS SyntaxTree.NilType) THEN
  8523. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8524. (*
  8525. ELSIF (from = NoType) THEN
  8526. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8527. *)
  8528. ELSIF (from IS SyntaxTree.BasicType) THEN
  8529. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8530. IF varpar & (i # 0) THEN i := Infinity END;
  8531. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8532. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8533. ELSIF (from IS SyntaxTree.RecordType) THEN
  8534. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8535. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8536. IF to IS SyntaxTree.MathArrayType THEN
  8537. (*
  8538. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8539. i := Infinity;
  8540. ELSE
  8541. *)
  8542. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8543. (*
  8544. END;
  8545. *)
  8546. END
  8547. ELSIF (from IS SyntaxTree.PointerType) THEN
  8548. ptr := from(SyntaxTree.PointerType);
  8549. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8550. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8551. (* ELSE i := TypeDistance(ptr.base, to); *)
  8552. END
  8553. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8554. IF (to IS SyntaxTree.ProcedureType) THEN
  8555. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8556. END;
  8557. ELSIF (from IS SyntaxTree.PortType) THEN
  8558. IF (to IS SyntaxTree.PortType) THEN
  8559. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8560. i := 0;
  8561. END;
  8562. END;
  8563. (*no procedure test, procedure must be the same*)
  8564. END;
  8565. RETURN i
  8566. END TypeDistance;
  8567. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8568. BEGIN
  8569. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8570. END IsIntegerType;
  8571. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8572. BEGIN
  8573. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8574. END IsAddressType;
  8575. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8576. BEGIN
  8577. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8578. END IsSizeType;
  8579. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8580. BEGIN
  8581. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8582. END IsSignedIntegerType;
  8583. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8584. BEGIN
  8585. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8586. END IsUnsignedIntegerType;
  8587. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8588. VAR result: BOOLEAN;
  8589. BEGIN
  8590. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8591. value := x.resolved(SyntaxTree.IntegerValue).value;
  8592. result := TRUE
  8593. ELSE
  8594. result := FALSE
  8595. END;
  8596. RETURN result
  8597. END IsIntegerValue;
  8598. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8599. VAR result: BOOLEAN;
  8600. BEGIN
  8601. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8602. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8603. result := TRUE
  8604. ELSE
  8605. result := FALSE
  8606. END;
  8607. RETURN result
  8608. END IsEnumerationValue;
  8609. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8610. VAR result: BOOLEAN;
  8611. BEGIN
  8612. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8613. value := x.resolved(SyntaxTree.RealValue).value;
  8614. result := TRUE
  8615. ELSE
  8616. result := FALSE
  8617. END;
  8618. RETURN result
  8619. END IsRealValue;
  8620. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8621. VAR result: BOOLEAN;
  8622. BEGIN
  8623. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8624. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8625. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8626. result := TRUE
  8627. ELSE
  8628. result := FALSE
  8629. END;
  8630. RETURN result
  8631. END IsComplexValue;
  8632. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8633. VAR result: BOOLEAN;
  8634. BEGIN
  8635. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8636. value := x.resolved(SyntaxTree.CharacterValue).value;
  8637. result := TRUE
  8638. ELSE
  8639. result := FALSE
  8640. END;
  8641. RETURN result
  8642. END IsCharacterValue;
  8643. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8644. VAR result: BOOLEAN;
  8645. BEGIN
  8646. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8647. value := x.resolved(SyntaxTree.BooleanValue).value;
  8648. result := TRUE
  8649. ELSE
  8650. result := FALSE
  8651. END;
  8652. RETURN result
  8653. END IsBooleanValue;
  8654. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8655. VAR result: BOOLEAN;
  8656. BEGIN
  8657. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8658. value := x.resolved(SyntaxTree.SetValue).value;
  8659. result := TRUE
  8660. ELSE
  8661. result := FALSE
  8662. END;
  8663. RETURN result
  8664. END IsSetValue;
  8665. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8666. VAR result: BOOLEAN;
  8667. BEGIN
  8668. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8669. value := x.resolved(SyntaxTree.StringValue).value;
  8670. result := TRUE
  8671. ELSE
  8672. result := FALSE
  8673. END;
  8674. RETURN result
  8675. END IsStringValue;
  8676. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8677. BEGIN
  8678. x := x.resolved;
  8679. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8680. END Indexable;
  8681. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8682. BEGIN
  8683. RETURN t1.SameType(t2.resolved);
  8684. END SameType;
  8685. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8686. BEGIN
  8687. IF t IS SyntaxTree.MathArrayType THEN
  8688. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8689. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8690. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8691. DEC(max);
  8692. END;
  8693. ELSIF t IS SyntaxTree.ArrayType THEN
  8694. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8695. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8696. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8697. END;
  8698. END;
  8699. RETURN t;
  8700. END ArrayBase;
  8701. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8702. BEGIN
  8703. type := type.resolved;
  8704. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8705. base := type(SyntaxTree.ArrayType).arrayBase;
  8706. RETURN TRUE;
  8707. END;
  8708. RETURN FALSE;
  8709. END IsOpenArray;
  8710. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8711. BEGIN
  8712. type := type.resolved;
  8713. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8714. base := type(SyntaxTree.ArrayType).arrayBase;
  8715. dim := type(SyntaxTree.ArrayType).staticLength;
  8716. RETURN TRUE
  8717. ELSE
  8718. RETURN FALSE
  8719. END;
  8720. END IsStaticArray;
  8721. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8722. BEGIN
  8723. type := type.resolved;
  8724. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8725. base := type(SyntaxTree.ArrayType).arrayBase;
  8726. RETURN TRUE
  8727. ELSE
  8728. RETURN FALSE
  8729. END;
  8730. END IsDynamicArray;
  8731. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8732. VAR i: LONGINT;
  8733. BEGIN
  8734. i := 0;
  8735. t := t.resolved;
  8736. IF t IS SyntaxTree.MathArrayType THEN
  8737. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8738. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8739. END;
  8740. ELSIF t IS SyntaxTree.ArrayType THEN
  8741. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8742. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8743. END;
  8744. END;
  8745. RETURN i
  8746. END Dimension;
  8747. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8748. BEGIN
  8749. RETURN expression.assignable;
  8750. END IsVariable;
  8751. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8752. BEGIN
  8753. IF (symbol IS SyntaxTree.Parameter) THEN
  8754. WITH symbol: SyntaxTree.Parameter DO
  8755. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8756. END;
  8757. ELSE
  8758. RETURN FALSE
  8759. END;
  8760. END IsVariableParameter;
  8761. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8762. VAR result: BOOLEAN;
  8763. BEGIN
  8764. IF type = NIL THEN result := FALSE
  8765. ELSE
  8766. type := type.resolved;
  8767. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8768. END;
  8769. RETURN result
  8770. END IsPointerType;
  8771. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8772. VAR result: BOOLEAN;
  8773. BEGIN
  8774. IF type = NIL THEN result := FALSE
  8775. ELSE
  8776. type := type.resolved;
  8777. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8778. END;
  8779. RETURN result
  8780. END IsUnsafePointer;
  8781. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8782. BEGIN
  8783. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8784. END IsDisposable;
  8785. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8786. VAR result: BOOLEAN;
  8787. BEGIN
  8788. IF type = NIL THEN result := FALSE
  8789. ELSE
  8790. type := type.resolved;
  8791. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8792. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8793. result := result OR (type IS SyntaxTree.ObjectType);
  8794. END;
  8795. RETURN result
  8796. END IsPointerToRecord;
  8797. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8798. VAR result: BOOLEAN;
  8799. BEGIN
  8800. IF type = NIL THEN result := FALSE
  8801. ELSE
  8802. type := type.resolved;
  8803. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8804. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8805. ;
  8806. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8807. result := result OR (type IS SyntaxTree.ObjectType);
  8808. END;
  8809. RETURN result
  8810. END IsPointerToObject;
  8811. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8812. BEGIN
  8813. IF type # NIL THEN
  8814. RETURN type.resolved.hasPointers
  8815. ELSE
  8816. RETURN FALSE
  8817. END;
  8818. END ContainsPointer;
  8819. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8820. BEGIN
  8821. IF type = NIL THEN RETURN FALSE END;
  8822. type := type.resolved;
  8823. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8824. END IsStringType;
  8825. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8826. BEGIN
  8827. IF type = NIL THEN RETURN FALSE END;
  8828. type := type.resolved;
  8829. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8830. END IsCharacterType;
  8831. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8832. BEGIN
  8833. IF type = NIL THEN RETURN FALSE END;
  8834. type := type.resolved;
  8835. RETURN (type IS SyntaxTree.EnumerationType)
  8836. END IsEnumerationType;
  8837. (** cf. section "Type extension (base type)" in the language report **)
  8838. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8839. VAR result: BOOLEAN;
  8840. BEGIN
  8841. ASSERT(base # NIL); ASSERT(extension # NIL);
  8842. base := base.resolved; extension := extension.resolved;
  8843. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8844. result := TRUE;
  8845. ELSE
  8846. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8847. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8848. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8849. END;
  8850. WHILE (extension # NIL) & (extension # base) DO
  8851. IF extension IS SyntaxTree.RecordType THEN
  8852. extension := extension(SyntaxTree.RecordType).baseType;
  8853. IF (extension # NIL) THEN extension := extension.resolved END;
  8854. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8855. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8856. END;
  8857. ELSE extension := NIL;
  8858. END;
  8859. END;
  8860. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8861. END;
  8862. RETURN result
  8863. END IsTypeExtension;
  8864. (** check if base is the base enumeration type of extension **)
  8865. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8866. BEGIN
  8867. base := base.resolved; extension := extension.resolved;
  8868. WHILE (extension # NIL) & (extension # base) DO
  8869. IF extension IS SyntaxTree.EnumerationType THEN
  8870. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8871. IF extension # NIL THEN extension := extension.resolved END;
  8872. ELSE
  8873. extension := NIL
  8874. END;
  8875. END;
  8876. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8877. END IsEnumerationExtension;
  8878. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8879. BEGIN
  8880. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8881. RETURN TRUE
  8882. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8883. RETURN TRUE
  8884. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8885. RETURN TRUE
  8886. ELSE
  8887. RETURN FALSE
  8888. END
  8889. END IsCallable;
  8890. (** compute and return the distance of two record types
  8891. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8892. **)
  8893. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8894. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8895. BEGIN
  8896. i := 0;
  8897. WHILE (from # NIL) & (from # to) DO
  8898. baseType := from.baseType;
  8899. IF (baseType # NIL) THEN
  8900. baseType := baseType.resolved;
  8901. IF baseType IS SyntaxTree.PointerType THEN
  8902. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8903. END;
  8904. IF baseType IS SyntaxTree.RecordType THEN
  8905. from := baseType(SyntaxTree.RecordType);
  8906. ELSE
  8907. from := NIL;
  8908. END;
  8909. ELSE
  8910. from := NIL
  8911. END;
  8912. INC(i)
  8913. END;
  8914. IF from = NIL THEN i := Infinity END;
  8915. RETURN i
  8916. END RecordTypeDistance;
  8917. (** compute and return the distance of two pointer types **)
  8918. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8919. BEGIN
  8920. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8921. RETURN Infinity;
  8922. ELSE
  8923. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8924. END;
  8925. END PointerTypeDistance;
  8926. (** check if expression contains a symbol designator pointing to a type declaration.
  8927. - if so then enter type declaration into typeDeclaration and return true else return false
  8928. **)
  8929. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8930. VAR result: BOOLEAN;
  8931. BEGIN
  8932. result := FALSE;
  8933. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8934. result := TRUE;
  8935. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8936. END;
  8937. RETURN result
  8938. END IsTypeDesignator;
  8939. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8940. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8941. VAR result: BOOLEAN;
  8942. BEGIN
  8943. type := type.resolved;
  8944. IF type IS SyntaxTree.PointerType THEN
  8945. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8946. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8947. result := TRUE
  8948. ELSE
  8949. result := type IS SyntaxTree.RecordType
  8950. END;
  8951. RETURN result
  8952. END IsExtensibleType;
  8953. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8954. BEGIN
  8955. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8956. (d IS SyntaxTree.SymbolDesignator) &
  8957. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8958. OR
  8959. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8960. END IsUnextensibleRecord;
  8961. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8962. BEGIN
  8963. IF IsUnextensibleRecord(d) THEN
  8964. RETURN FALSE
  8965. ELSE RETURN IsExtensibleType(d.type.resolved)
  8966. END;
  8967. END IsExtensibleDesignator;
  8968. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8969. BEGIN
  8970. type := type.resolved;
  8971. IF (type IS SyntaxTree.PointerType) THEN
  8972. RETURN TRUE
  8973. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8974. RETURN TRUE
  8975. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8976. RETURN TRUE
  8977. ELSIF (type IS SyntaxTree.BasicType) THEN
  8978. RETURN TRUE
  8979. END;
  8980. RETURN FALSE
  8981. END IsBasicType;
  8982. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8983. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8984. BEGIN
  8985. baseType := record.baseType;
  8986. IF (baseType # NIL) THEN
  8987. baseType := baseType.resolved;
  8988. IF (baseType IS SyntaxTree.PointerType) THEN
  8989. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8990. END;
  8991. END;
  8992. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8993. recordType := baseType(SyntaxTree.RecordType);
  8994. ELSE
  8995. recordType := NIL;
  8996. END;
  8997. RETURN recordType
  8998. END RecordBase;
  8999. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9000. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9001. BEGIN
  9002. baseRecord := RecordBase(scope.ownerRecord);
  9003. IF baseRecord = NIL THEN RETURN NIL END;
  9004. scope := baseRecord.recordScope;
  9005. procedureType := procedure.type.resolved;
  9006. IF procedure IS SyntaxTree.Operator THEN
  9007. operator := scope.firstOperator;
  9008. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9009. (*
  9010. Printout.Info("not same ",procedureType);
  9011. Printout.Info("with ",operator.type);
  9012. *)
  9013. operator := operator.nextOperator;
  9014. END;
  9015. super := operator;
  9016. ELSE
  9017. super := scope.firstProcedure;
  9018. WHILE (super # NIL) & (super.name # procedure.name) DO
  9019. super := super.nextProcedure;
  9020. END;
  9021. END;
  9022. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9023. RETURN super
  9024. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9025. RETURN super
  9026. ELSE
  9027. RETURN FindSuperProcedure(scope,procedure);
  9028. END;
  9029. END FindSuperProcedure;
  9030. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9031. VAR procedure: SyntaxTree.Procedure;
  9032. BEGIN
  9033. procedure := record.recordScope.constructor;
  9034. IF procedure = NIL THEN
  9035. record := RecordBase(record);
  9036. IF record # NIL THEN
  9037. procedure := GetConstructor(record)
  9038. END;
  9039. END;
  9040. RETURN procedure;
  9041. END GetConstructor;
  9042. (* enter a case into a list of cases in a sorted way and check for collision *)
  9043. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9044. VAR prev,this,new: SyntaxTree.CaseConstant;
  9045. BEGIN
  9046. this := root;
  9047. prev := NIL;
  9048. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9049. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9050. RETURN FALSE
  9051. ELSE
  9052. IF (this # NIL) & (this.min = max+1) THEN
  9053. this.min := min
  9054. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9055. prev.max := min
  9056. ELSE
  9057. NEW(new); new.min := min; new.max := max;
  9058. new.next := this;
  9059. IF prev = NIL THEN
  9060. root := new;
  9061. ELSE
  9062. prev.next := new
  9063. END
  9064. END;
  9065. RETURN TRUE
  9066. END;
  9067. END EnterCase;
  9068. (** generate and return a new checker object, errors are entered into diagnostics **)
  9069. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9070. VAR checker: Checker;
  9071. BEGIN
  9072. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9073. RETURN checker
  9074. END NewChecker;
  9075. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9076. VAR warnings: Warnings;
  9077. BEGIN
  9078. NEW(warnings, diagnostics); RETURN warnings;
  9079. END NewWarnings;
  9080. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9081. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9082. END IsRangeType;
  9083. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9084. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9085. END IsMathArrayType;
  9086. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9087. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9088. END IsArrayType;
  9089. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9090. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9091. END IsComplexType;
  9092. (** if a type is an array-structured object type *)
  9093. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9094. VAR recordType: SyntaxTree.RecordType;
  9095. BEGIN
  9096. IF type = NIL THEN
  9097. RETURN FALSE
  9098. ELSE
  9099. type := type.resolved;
  9100. IF type IS SyntaxTree.PointerType THEN
  9101. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9102. IF type IS SyntaxTree.RecordType THEN
  9103. recordType := type(SyntaxTree.RecordType);
  9104. RETURN recordType.isObject & recordType.HasArrayStructure()
  9105. ELSE
  9106. RETURN FALSE
  9107. END
  9108. ELSE
  9109. RETURN FALSE
  9110. END
  9111. END
  9112. END IsArrayStructuredObjectType;
  9113. (** the math array structure of a type
  9114. - for math arrays: the array itself
  9115. - for pointers: the math array structure of the pointer base
  9116. - for array-structured object types: the underlying structure
  9117. - for non-math arrays and all other types: NIL
  9118. **)
  9119. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9120. VAR
  9121. result: SyntaxTree.MathArrayType;
  9122. BEGIN
  9123. IF type = NIL THEN
  9124. result := NIL
  9125. ELSE
  9126. type := type.resolved;
  9127. IF type IS SyntaxTree.PointerType THEN
  9128. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9129. END;
  9130. IF type IS SyntaxTree.MathArrayType THEN
  9131. result := type(SyntaxTree.MathArrayType)
  9132. ELSIF type IS SyntaxTree.RecordType THEN
  9133. result := type(SyntaxTree.RecordType).arrayStructure
  9134. ELSE
  9135. result := NIL
  9136. END
  9137. END;
  9138. RETURN result
  9139. END MathArrayStructureOfType;
  9140. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9141. VAR
  9142. result: BOOLEAN;
  9143. rangeExpression: SyntaxTree.RangeExpression;
  9144. BEGIN
  9145. IF x IS SyntaxTree.RangeExpression THEN
  9146. rangeExpression := x(SyntaxTree.RangeExpression);
  9147. result := TRUE;
  9148. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9149. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9150. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9151. ELSE
  9152. result := FALSE
  9153. END;
  9154. RETURN result
  9155. END IsStaticRange;
  9156. (** whether a type is a math array of tensor form **)
  9157. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9158. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9159. END IsTensor;
  9160. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9161. BEGIN
  9162. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9163. length := type(SyntaxTree.MathArrayType).staticLength;
  9164. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9165. RETURN TRUE
  9166. ELSE
  9167. RETURN FALSE
  9168. END;
  9169. END IsStaticMathArray;
  9170. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9171. BEGIN
  9172. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9173. END SymbolHasAddress;
  9174. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9175. BEGIN
  9176. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9177. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9178. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9179. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9180. END HasAddress;
  9181. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9182. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9183. BEGIN
  9184. IF (e IS SyntaxTree.Designator) THEN
  9185. d := e(SyntaxTree.Designator);
  9186. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9187. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9188. e := d.left;
  9189. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9190. END;
  9191. IF d # NIL THEN
  9192. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9193. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9194. END;
  9195. END;
  9196. RETURN FALSE;
  9197. END IsLocalVariable;
  9198. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9199. BEGIN
  9200. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9201. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9202. ELSE
  9203. RETURN TRUE
  9204. END;
  9205. END IsStaticProcedure;
  9206. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9207. CONST OptimizeMethodTable = FALSE;
  9208. BEGIN
  9209. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9210. END InMethodTable;
  9211. END FoxSemanticChecker.
  9212. SystemTools.FreeDownTo FoxSemanticChecker ~