FoxSemanticChecker.Mod 401 KB

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