FoxSemanticChecker.Mod 402 KB

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