FoxSemanticChecker.Mod 399 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  83. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  84. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  85. currentScope-: SyntaxTree.Scope;
  86. currentIsRealtime: BOOLEAN;
  87. currentIsUnreachable: BOOLEAN;
  88. currentIsCellNet: BOOLEAN;
  89. currentIsBodyProcedure: BOOLEAN;
  90. currentIsExclusive: BOOLEAN;
  91. global: SyntaxTree.ModuleScope;
  92. withEntries: WithEntry;
  93. activeCellsStatement: BOOLEAN;
  94. replacements*: Replacement;
  95. cellsAreObjects: BOOLEAN;
  96. variableAccessed: BOOLEAN;
  97. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  98. BEGIN
  99. SELF.diagnostics := diagnostics;
  100. SELF.useDarwinCCalls := useDarwinCCalls;
  101. SELF.cooperative := cooperative;
  102. SELF.system := system;
  103. SELF.symbolFileFormat := symbolFileFormat;
  104. error := FALSE;
  105. NEW(typeFixes);
  106. NEW(pointerFixes);
  107. resolvedType := NIL;
  108. resolvedExpression := NIL;
  109. resolvedStatement := NIL;
  110. currentScope := NIL;
  111. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  112. SELF.importCache := importCache;
  113. arrayBaseImported := FALSE;
  114. complexNumbersImported := FALSE;
  115. SELF.VerboseErrorMessage := verboseErrorMessage;
  116. global := NIL;
  117. phase := UndefinedPhase;
  118. currentIsRealtime := FALSE;
  119. currentIsUnreachable := FALSE;
  120. currentIsCellNet := FALSE;
  121. currentIsBodyProcedure := FALSE;
  122. currentIsExclusive := FALSE;
  123. withEntries := NIL;
  124. SELF.cellsAreObjects := system.cellsAreObjects;
  125. COPY(backend, backendName);
  126. END InitChecker;
  127. (** report error **)
  128. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  129. VAR errModule: SyntaxTree.Module;
  130. BEGIN
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  141. END Warning;
  142. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  143. VAR errorMessage: ARRAY 256 OF CHAR;
  144. BEGIN
  145. Basic.Concat(errorMessage,msg," ", msg2);
  146. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  147. error := TRUE;
  148. END ErrorSS;
  149. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  150. VAR msg, msg2: ARRAY 256 OF CHAR;
  151. BEGIN
  152. COPY(msg1, msg);
  153. Strings.Append(msg, " = ");
  154. Basic.GetString(s, msg2);
  155. Strings.Append(msg, msg2);
  156. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  157. END InfoSS;
  158. (*** symbol lookup ***)
  159. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  160. **)
  161. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  162. VAR
  163. scope,baseScope: SyntaxTree.Scope;
  164. symbol, s: SyntaxTree.Symbol;
  165. ownerRecord,base: SyntaxTree.RecordType;
  166. BEGIN
  167. scope := inScope;
  168. symbol := NIL;
  169. WHILE (scope # NIL) & (symbol = NIL) DO
  170. symbol := scope.FindSymbol(name);
  171. s := NIL;
  172. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  173. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  175. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. END;
  177. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  178. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  179. ELSE
  180. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  181. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  182. symbol.MarkUsed;
  183. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  184. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  185. base := RecordBase(ownerRecord);
  186. IF (base # NIL) THEN
  187. baseScope := base.recordScope;
  188. symbol := Find(baseScope,name,FALSE);
  189. ELSE
  190. symbol := NIL;
  191. END;
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. END;
  196. END;
  197. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  198. END;
  199. IF (symbol # NIL) THEN
  200. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  201. ASSERT(phase = DeclarationPhase);
  202. ResolveSymbol(symbol)
  203. END;
  204. symbol.MarkUsed;
  205. END;
  206. RETURN symbol
  207. END Find;
  208. (*** types ***)
  209. (** find type declaration with name qualifiedIdentifier and return resolved type
  210. - check qualified identifier prefix, set scope to module scope if appropriate
  211. - check suffix in scope
  212. **)
  213. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  214. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  215. BEGIN
  216. result := NIL;
  217. prevScope := currentScope;
  218. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  219. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  220. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  221. IF symbol(SyntaxTree.Import).module = NIL THEN
  222. Error(qualifiedIdentifier.position,"module not loaded");
  223. result := SyntaxTree.invalidType;
  224. symbol := NIL;
  225. ELSE
  226. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  227. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  228. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  229. IF VerboseErrorMessage THEN
  230. Printout.Info("scope", currentScope);
  231. Printout.Info("symbol", symbol);
  232. END;
  233. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  234. END;
  235. END;
  236. ELSE
  237. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  238. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  239. symbol := NIL;
  240. END;
  241. ELSE
  242. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  243. IF symbol = NIL THEN
  244. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  245. IF VerboseErrorMessage THEN
  246. Printout.Info("Qualident",qualifiedIdentifier);
  247. Printout.Info("in scope",currentScope) ;
  248. END;
  249. END;
  250. END;
  251. IF symbol = NIL THEN (* error already handled *)
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  255. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  256. typeDeclaration := NIL;
  257. result := SyntaxTree.invalidType;
  258. ELSE
  259. currentScope := symbol.scope;
  260. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  261. result := ResolveType(typeDeclaration.declaredType);
  262. symbol.MarkUsed;
  263. ASSERT(result # NIL);
  264. END;
  265. currentScope := prevScope;
  266. RETURN result
  267. END ResolveNamedType;
  268. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  269. If node is currently being resolved then emit a cyclic definition error.
  270. Return TRUE only if node is fully resolved.
  271. **)
  272. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  273. VAR result: BOOLEAN;
  274. BEGIN
  275. IF SyntaxTree.Resolved IN x.state THEN
  276. result := FALSE
  277. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  278. Error(x.position,"cyclic definition");
  279. result := FALSE;
  280. ELSE
  281. result := TRUE;
  282. x.SetState(SyntaxTree.BeingResolved)
  283. END;
  284. RETURN result
  285. END TypeNeedsResolution;
  286. (** Return invalid type if x is currently being resolved, return x otherwise**)
  287. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  288. BEGIN
  289. IF SyntaxTree.Resolved IN x.state THEN
  290. RETURN x
  291. ELSE
  292. RETURN SyntaxTree.invalidType
  293. END;
  294. END ResolvedType;
  295. PROCEDURE VisitType(x: SyntaxTree.Type);
  296. BEGIN
  297. ASSERT(x = SyntaxTree.invalidType);
  298. END VisitType;
  299. (** resolve basic type **)
  300. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  301. BEGIN
  302. IF TypeNeedsResolution(x) THEN
  303. x.SetState(SyntaxTree.Resolved);
  304. END;
  305. resolvedType := ResolvedType(x)
  306. END VisitBasicType;
  307. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitByteType;
  311. (** resolve character type **)
  312. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  313. BEGIN
  314. VisitBasicType(x);
  315. END VisitCharacterType;
  316. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitBooleanType;
  320. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  321. BEGIN
  322. VisitBasicType(x);
  323. END VisitSetType;
  324. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  325. BEGIN
  326. VisitBasicType(x);
  327. END VisitAddressType;
  328. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  329. BEGIN
  330. VisitBasicType(x);
  331. END VisitSizeType;
  332. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  333. BEGIN
  334. VisitBasicType(x);
  335. END VisitAnyType;
  336. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitObjectType;
  340. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  341. BEGIN
  342. VisitBasicType(x);
  343. END VisitNilType;
  344. (** resolve integer type **)
  345. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitIntegerType;
  349. (** resolve real type **)
  350. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  351. BEGIN
  352. VisitBasicType(x);
  353. END VisitFloatType;
  354. (** resolve complex type **)
  355. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  356. BEGIN
  357. VisitBasicType(x);
  358. END VisitComplexType;
  359. (**
  360. resolve string type: nothing to be done
  361. **)
  362. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  363. BEGIN
  364. IF TypeNeedsResolution(x) THEN
  365. x.SetState(SyntaxTree.Resolved);
  366. END;
  367. resolvedType := ResolvedType(x)
  368. END VisitStringType;
  369. (**
  370. check enumeration scope: enter symbols and check for duplicate names
  371. **)
  372. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  373. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  374. BEGIN
  375. prevScope := currentScope;
  376. currentScope := x;
  377. e := x.firstConstant;
  378. WHILE (e # NIL) DO
  379. Register(e,x,FALSE);
  380. IF SymbolNeedsResolution(e) THEN
  381. IF e.value # NIL THEN
  382. value := ConstantExpression(e.value);
  383. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  384. ELSE
  385. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  386. value.SetType(x.ownerEnumeration);
  387. END;
  388. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  389. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  390. IF nextHighest > highest THEN highest := nextHighest END;
  391. END;
  392. e.SetValue(value);
  393. CheckSymbolVisibility(e);
  394. e.SetType(x.ownerEnumeration);
  395. e.SetState(SyntaxTree.Resolved);
  396. END;
  397. e := e.nextConstant;
  398. END;
  399. currentScope := prevScope;
  400. END CheckEnumerationScope;
  401. (**
  402. resolve enumeration type: check enumeration scope
  403. **)
  404. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  405. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  406. lowest, highest: LONGINT;
  407. BEGIN
  408. IF TypeNeedsResolution(x) THEN
  409. IF x.enumerationBase # NIL THEN
  410. position := x.enumerationBase.position;
  411. baseType := ResolveType(x.enumerationBase);
  412. resolved := baseType.resolved;
  413. baseScope := NIL;
  414. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  415. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  416. Error(position, "base type is no enumeration type");
  417. ELSE
  418. enumerationBase := resolved(SyntaxTree.EnumerationType);
  419. lowest := enumerationBase.rangeHighest+1;
  420. END;
  421. x.SetEnumerationBase(baseType);
  422. ELSE lowest := 0;
  423. END;
  424. highest := lowest-1;
  425. CheckEnumerationScope(x.enumerationScope, highest);
  426. x.SetRange(lowest, highest);
  427. x.SetState(SyntaxTree.Resolved);
  428. END;
  429. resolvedType := ResolvedType(x);
  430. END VisitEnumerationType;
  431. (**
  432. resolve range type: nothing to be done
  433. **)
  434. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  435. BEGIN
  436. IF TypeNeedsResolution(x) THEN
  437. x.SetState(SyntaxTree.Resolved);
  438. END;
  439. resolvedType := ResolvedType(x)
  440. END VisitRangeType;
  441. (**
  442. resolve qualified type
  443. - find and resolve named type and set resolved type
  444. **)
  445. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  446. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  447. BEGIN
  448. IF TypeNeedsResolution(x) THEN
  449. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  450. x.SetResolved(type.resolved);
  451. x.SetState(SyntaxTree.Resolved);
  452. x.SetTypeDeclaration (typeDeclaration);
  453. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  454. x.SetResolved(SyntaxTree.invalidType);
  455. END;
  456. resolvedType := x;
  457. END VisitQualifiedType;
  458. (**
  459. resolve array type
  460. - check base type
  461. - array of math array forbidden
  462. - static array of open array forbidden
  463. **)
  464. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  465. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  466. BEGIN
  467. IF TypeNeedsResolution(x) THEN
  468. x.SetArrayBase(ResolveType(x.arrayBase));
  469. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  470. arrayBase := x.arrayBase.resolved;
  471. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  472. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  473. pointerType.SetPointerBase(arrayBase);
  474. pointerType.SetHidden(TRUE);
  475. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  476. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  477. ELSE
  478. x.SetArrayBase(pointerType);
  479. END;
  480. END;
  481. IF x.length # NIL THEN
  482. variableAccessed := FALSE;
  483. e := ResolveExpression(x.length);
  484. IF (e.resolved = NIL) THEN
  485. IF variableAccessed THEN
  486. Error(e.position, "forbidden variable access");
  487. END;
  488. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  489. ELSE
  490. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  491. END;
  492. END;
  493. IF arrayBase IS SyntaxTree.ArrayType THEN
  494. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  495. Error(x.position,"forbidden static array of dynamic array");
  496. END;
  497. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  498. Error(x.position,"forbidden array mixed form");
  499. END;
  500. x.SetHasPointers(arrayBase.hasPointers);
  501. x.SetState(SyntaxTree.Resolved);
  502. END;
  503. resolvedType := ResolvedType(x);
  504. END VisitArrayType;
  505. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  506. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  507. BEGIN
  508. module := currentScope.ownerModule;
  509. IF module.name=name THEN
  510. (* do nothing *)
  511. ELSE
  512. moduleScope := module.moduleScope;
  513. import := moduleScope.FindImport(name);
  514. IF import = NIL THEN
  515. import := SyntaxTree.NewImport(position,name,name,TRUE);
  516. moduleScope.AddImport(import);
  517. Register(import,moduleScope,FALSE);
  518. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  519. VisitImport(import);
  520. ELSIF import.direct=FALSE THEN
  521. import.SetScope(module.moduleScope);
  522. import.SetDirect(TRUE);
  523. IF moduleScope.FindSymbol(import.name) = NIL THEN
  524. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  525. duplicate.SetContext(import.context);
  526. duplicate.SetModule(import.module);
  527. Register(duplicate,moduleScope,TRUE);
  528. VisitImport(duplicate);
  529. END;
  530. END;
  531. import.MarkUsed
  532. END;
  533. END ImportModule;
  534. (**
  535. resolve math array type
  536. - check base type
  537. - open math array of array forbidden
  538. - math array of tensor forbidden
  539. - static array of open array forbidden
  540. **)
  541. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  542. VAR arrayBase: SyntaxTree.Type;
  543. BEGIN
  544. IF TypeNeedsResolution(x) THEN
  545. x.SetArrayBase(ResolveType(x.arrayBase));
  546. IF x.length # NIL THEN
  547. x.SetLength(ConstantIntegerGeq0(x.length));
  548. END;
  549. arrayBase := x.arrayBase;
  550. IF arrayBase # NIL THEN
  551. arrayBase := arrayBase.resolved;
  552. IF arrayBase = SyntaxTree.invalidType THEN
  553. (* error already handled *)
  554. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  555. Error(x.position,"forbidden array mixed form");
  556. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  557. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  558. Error(x.position,"forbidden Tensor Array mix")
  559. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  560. Error(x.position,"forbidden static array of dynamic array")
  561. END;
  562. END;
  563. IF x.form = SyntaxTree.Static THEN
  564. x.SetIncrement(system.SizeOf(arrayBase));
  565. END;
  566. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  567. END;
  568. x.SetState(SyntaxTree.Resolved);
  569. END;
  570. resolvedType := ResolvedType(x);
  571. END VisitMathArrayType;
  572. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  573. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  574. (1) Rec = RECORD ... END; Ptr <---> Rec
  575. Ptr = POINTER TO Rec; ^ |
  576. | |
  577. TypeDesc TypeDesc
  578. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  579. ^ /
  580. | /
  581. TypeDesc <-- /
  582. *)
  583. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  584. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  585. BEGIN
  586. Strings.IntToStr(x.position.start,number);
  587. COPY(prefix,name);
  588. Strings.Append(name,"@");
  589. Strings.Append(name,number);
  590. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  591. typeDeclaration.SetDeclaredType(x);
  592. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  593. x.SetTypeDeclaration(typeDeclaration);
  594. currentScope.AddTypeDeclaration(typeDeclaration);
  595. typeDeclaration.SetScope(currentScope);
  596. END AnonymousTypeDeclaration;
  597. (**
  598. deferred pointer type resolving
  599. - resolve base type
  600. - check that base type is a record or array type
  601. - if error then set base type to invalid type
  602. **)
  603. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  604. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  605. BEGIN
  606. ASSERT(type.pointerBase # NIL);
  607. position := type.pointerBase.position;
  608. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  609. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  610. (* not for pointers, a type is needed for the records only
  611. IF type.typeDeclaration = NIL THEN
  612. AnonymousTypeDeclaration(type);
  613. END;
  614. *)
  615. END;
  616. resolved := ResolveType(type.pointerBase);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  618. type.SetPointerBase(resolved);
  619. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  620. recordType := resolved.resolved(SyntaxTree.RecordType);
  621. IF recordType.isObject & (recordType.baseType # NIL) THEN
  622. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "base type of object must be a realtime object");
  624. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  625. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  626. END;
  627. END;
  628. END;
  629. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  630. Error(position, "realtime object contains references to non-realtime objects");
  631. END;
  632. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  633. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  634. Error(position, "forbidden unsafe at static array");
  635. ELS
  636. *)
  637. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  638. Error(position, "forbidden unsafe at multidimensional array");
  639. END;
  640. END;
  641. ELSE
  642. Error(position, "forbidden pointer base type");
  643. type.SetPointerBase(SyntaxTree.invalidType)
  644. END
  645. END FixPointerType;
  646. (**
  647. resolve pointer type
  648. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  649. **)
  650. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  651. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  652. modifiers: SyntaxTree.Modifier; position: Position;
  653. BEGIN
  654. IF TypeNeedsResolution(x) THEN
  655. modifiers := x.modifiers;
  656. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  657. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  658. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  659. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  660. (* inheritance cycle check
  661. example:
  662. A=POINTER TO RECORD(B) END;
  663. B=POINTER TO RECORD(A) END;
  664. *)
  665. IF x.pointerBase IS SyntaxTree.RecordType THEN
  666. recordType := x.pointerBase(SyntaxTree.RecordType);
  667. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  668. recordBaseType := ResolveType(recordType.baseType);
  669. recordType.SetBaseType(recordBaseType);
  670. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  671. END;
  672. CheckModifiers(modifiers, TRUE);
  673. typeFixes.Add(x,currentScope);
  674. x.SetState(SyntaxTree.Resolved);
  675. END;
  676. resolvedType := ResolvedType(x)
  677. END VisitPointerType;
  678. (**
  679. resolve port type
  680. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  681. **)
  682. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  683. VAR value: LONGINT;
  684. BEGIN
  685. IF TypeNeedsResolution(x) THEN
  686. x.SetCellsAreObjects(cellsAreObjects);
  687. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  688. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  689. x.SetSize(value)
  690. ELSE
  691. x.SetSize(system.SizeOf(system.longintType));
  692. END;
  693. x.SetState(SyntaxTree.Resolved);
  694. END;
  695. resolvedType := ResolvedType(x)
  696. END VisitPortType;
  697. (**
  698. deferred procedure type resolving
  699. - resolve return type
  700. - traverse and resolve parameters
  701. **)
  702. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  703. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  704. BEGIN
  705. resolved := ResolveType(procedureType.returnType);
  706. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  707. Error(procedureType.position,"forbidden open array return type");
  708. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  709. Error(procedureType.position,"procedure with return type does not return");
  710. END;
  711. procedureType.SetReturnType(resolved);
  712. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  713. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  714. parameter.SetType(procedureType.returnType);
  715. parameter.SetAccess(SyntaxTree.Hidden);
  716. parameter.SetUntraced(procedureType.hasUntracedReturn);
  717. VisitParameter(parameter);
  718. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  719. END;
  720. (* process parameters *)
  721. parameter :=procedureType.firstParameter;
  722. WHILE (parameter # NIL) DO
  723. VisitParameter(parameter);
  724. parameter := parameter.nextParameter;
  725. END;
  726. parameter := procedureType.selfParameter;
  727. IF parameter # NIL THEN
  728. VisitParameter(parameter)
  729. END;
  730. END FixProcedureType;
  731. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  732. VAR prev,this: SyntaxTree.Modifier;
  733. BEGIN
  734. this := modifiers;prev := NIL;
  735. WHILE (this # NIL) & (this.identifier # name) DO
  736. prev := this; this := this.nextModifier;
  737. END;
  738. IF this # NIL THEN
  739. IF this.expression # NIL THEN
  740. Error(this.position,"unexpected expression");
  741. END;
  742. this.Resolved;
  743. position := this.position;
  744. RETURN TRUE
  745. ELSE
  746. RETURN FALSE
  747. END;
  748. END HasFlag;
  749. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  750. VAR prev,this: SyntaxTree.Modifier;
  751. BEGIN
  752. this := modifiers;prev := NIL;
  753. WHILE (this # NIL) & (this.identifier # name) DO
  754. prev := this; this := this.nextModifier;
  755. END;
  756. IF this # NIL THEN
  757. IF this.expression = NIL THEN
  758. Error(this.position,"expected expression value");
  759. ELSE
  760. this.SetExpression(ConstantExpression(this.expression));
  761. IF CheckIntegerValue(this.expression,value) THEN END;
  762. END;
  763. this.Resolved;
  764. position := this.position;
  765. RETURN TRUE
  766. ELSE RETURN FALSE
  767. END;
  768. END HasValue;
  769. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  770. VAR prev,this: SyntaxTree.Modifier;
  771. BEGIN
  772. this := modifiers;prev := NIL;
  773. WHILE (this # NIL) & (this.identifier # name) DO
  774. prev := this; this := this.nextModifier;
  775. END;
  776. IF this # NIL THEN
  777. IF this.expression = NIL THEN
  778. Error(this.position,"expected expression value");
  779. ELSE
  780. this.SetExpression(ConstantExpression(this.expression));
  781. IF CheckStringValue(this.expression,value) THEN END;
  782. END;
  783. this.Resolved;
  784. position := this.position;
  785. RETURN TRUE
  786. ELSE RETURN FALSE
  787. END;
  788. END HasStringValue;
  789. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  790. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  791. BEGIN
  792. IF cellsAreObjects THEN RETURN FALSE END;
  793. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  794. IF svalue = "A2" THEN
  795. RETURN TRUE
  796. END;
  797. END;
  798. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  799. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  800. END;
  801. RETURN FALSE;
  802. (*
  803. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  804. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  805. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  806. IF svalue[0] = "~" THEN
  807. Strings.TrimLeft(svalue, "~");
  808. IF svalue = backendName THEN
  809. RETURN TRUE;
  810. END;
  811. ELSIF svalue # backendName THEN
  812. RETURN TRUE;
  813. END;
  814. END;
  815. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  816. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  817. END;
  818. RETURN FALSE;
  819. *)
  820. END SkipImplementation;
  821. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  822. VAR this: SyntaxTree.Modifier;
  823. BEGIN
  824. this := modifiers;
  825. WHILE this # NIL DO
  826. IF ~this.resolved THEN
  827. IF checkUse THEN
  828. Error(this.position,"unexpected modifier");
  829. ELSE
  830. this.SetExpression(ResolveExpression(this.expression));
  831. this.Resolved;
  832. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  833. END;
  834. END;
  835. this := this.nextModifier
  836. END;
  837. END CheckModifiers;
  838. (**
  839. resolve procedure type
  840. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  841. **)
  842. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  843. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  844. BEGIN
  845. IF TypeNeedsResolution(procedureType) THEN
  846. modifiers := procedureType.modifiers;
  847. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  848. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  849. procedureType.SetInterrupt(TRUE);
  850. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  851. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  852. IF useDarwinCCalls THEN (*fld*)
  853. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  854. ELSE
  855. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  856. END
  857. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  858. procedureType.SetNoReturn(TRUE);
  859. END;
  860. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  861. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  862. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  863. CheckModifiers(modifiers, TRUE);
  864. modifiers := procedureType.returnTypeModifiers;
  865. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  866. CheckModifiers(modifiers, TRUE);
  867. typeFixes.Add(procedureType,currentScope);
  868. procedureType.SetHasPointers(procedureType.isDelegate);
  869. procedureType.SetState(SyntaxTree.Resolved);
  870. END;
  871. resolvedType := ResolvedType(procedureType)
  872. END VisitProcedureType;
  873. (** check and resolve record type
  874. - check base type: must be record, math array or array-structured object type
  875. - check declarations
  876. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  877. **)
  878. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  879. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  880. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  881. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  882. hasPointers: BOOLEAN;
  883. modifiers: SyntaxTree.Modifier;
  884. value: LONGINT;
  885. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  886. BEGIN
  887. type := type.resolved;
  888. IF (type IS SyntaxTree.PointerType) &
  889. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  890. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  891. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  892. RETURN TRUE
  893. ELSE
  894. RETURN FALSE
  895. END;
  896. END IsPointerToRecord;
  897. BEGIN
  898. IF TypeNeedsResolution(x) THEN
  899. hasPointers := FALSE;
  900. modifiers := x.modifiers;
  901. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  902. CheckModifiers(modifiers, TRUE);
  903. IF x.baseType # NIL THEN
  904. position := x.baseType.position;
  905. baseType := ResolveType(x.baseType);
  906. resolved := baseType.resolved;
  907. hasPointers := hasPointers OR resolved.hasPointers;
  908. IF x.isObject THEN (* object *)
  909. ASSERT(x.pointerType # NIL);
  910. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  911. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  912. baseType := NIL
  913. ELSIF IsPointerToRecord(resolved,recordType) THEN
  914. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  915. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  916. ELSE
  917. Error(position,"object does not extend pointer to record, object or math array ")
  918. END;
  919. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  920. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  921. ELSIF IsPointerToRecord(resolved,recordType) THEN
  922. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  923. ELSIF resolved IS SyntaxTree.RecordType THEN
  924. ELSE
  925. Error(position,"pointer to record does not extend pointer to record or record")
  926. END;
  927. ELSE
  928. IF resolved IS SyntaxTree.RecordType THEN
  929. ELSE
  930. Error(position,"record does not extend record")
  931. END;
  932. END;
  933. x.SetBaseType(baseType);
  934. IF x.Level() > 15 THEN
  935. Error(position, "record/object inheritance level too high");
  936. (* note:
  937. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  938. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  939. inheritance history of a type.
  940. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  941. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  942. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  943. *)
  944. END;
  945. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  946. Error(position, "invalid inheritance of disposable types");
  947. END;
  948. END;
  949. Declarations(x.recordScope, FALSE, {0});
  950. x.SetState(SyntaxTree.Resolved);
  951. Declarations(x.recordScope, FALSE, {1});
  952. ResolveArrayStructure(x);
  953. (* computation of sizes and offsets skipped -> done in backend / system *)
  954. recordBase := x.GetBaseRecord();
  955. IF recordBase = NIL THEN numberMethods := 0
  956. ELSE numberMethods := recordBase.recordScope.numberMethods
  957. END;
  958. isRealtime := TRUE;
  959. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  960. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  961. END;
  962. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  963. WHILE symbol # NIL DO
  964. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  965. IF symbol IS SyntaxTree.Variable THEN
  966. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  967. END;
  968. IF symbol IS SyntaxTree.Procedure THEN
  969. procedure := symbol(SyntaxTree.Procedure);
  970. IF procedure.super # NIL THEN
  971. procedure.SetMethodNumber(procedure.super.methodNumber)
  972. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  973. procedure.SetMethodNumber(numberMethods);
  974. INC(numberMethods);
  975. END;
  976. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  977. Error(procedure.position,"realtime procedure in non-realtime object")
  978. END;
  979. END;
  980. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  981. Error(symbol.position,"non-realtime symbol in realtime object")
  982. END;
  983. symbol := symbol.nextSymbol;
  984. END;
  985. IF isRealtime THEN x.SetRealtime(TRUE) END;
  986. x.recordScope.SetNumberMethods(numberMethods);
  987. (* TODO: is this needed anymore? *)
  988. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  989. Error(x.position,"object extends a record")
  990. END;
  991. IF (x.typeDeclaration = NIL) THEN
  992. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  993. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  994. (*
  995. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  996. AnonymousTypeDeclaration(x,name);
  997. *)
  998. ELSE
  999. AnonymousTypeDeclaration(x,"Anonymous");
  1000. END;
  1001. END;
  1002. x.SetHasPointers(hasPointers);
  1003. x.SetState(SyntaxTree.Resolved);
  1004. END;
  1005. resolvedType := ResolvedType(x);
  1006. END VisitRecordType;
  1007. (** check and resolve cell type
  1008. - check base type: must be cell
  1009. - check declarations
  1010. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1011. **)
  1012. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1013. VAR
  1014. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1015. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1016. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1017. recordBase: SyntaxTree.RecordType;
  1018. numberMethods, int: LONGINT;
  1019. real: LONGREAL;
  1020. bool: BOOLEAN;
  1021. set: SET;
  1022. v: SyntaxTree.Expression;
  1023. str: Scanner.StringType;
  1024. atype: SyntaxTree.ArrayType;
  1025. prev: SyntaxTree.Scope;
  1026. skip: BOOLEAN;
  1027. svalue: ARRAY 32 OF CHAR;
  1028. BEGIN
  1029. IF TypeNeedsResolution(x) THEN
  1030. recordBase := NIL;
  1031. IF cellsAreObjects THEN
  1032. IF x.baseType = NIL THEN
  1033. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1034. ImportModule(qualifiedIdentifier.prefix, x.position);
  1035. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1036. x.SetBaseType(ResolveType(x.baseType));
  1037. recordBase := x.GetBaseRecord();
  1038. IF recordBase = NIL THEN
  1039. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1040. END;
  1041. ELSE
  1042. x.SetBaseType(ResolveType(x.baseType));
  1043. END;
  1044. ELSE
  1045. x.SetBaseType(ResolveType(x.baseType));
  1046. END;
  1047. IF recordBase = NIL THEN numberMethods := 0
  1048. ELSE numberMethods := recordBase.recordScope.numberMethods
  1049. END;
  1050. modifier := x.modifiers;
  1051. (*IF ~x.isCellNet THEN*)
  1052. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1053. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1054. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1055. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1056. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1057. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1058. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1059. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1060. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1061. symbol := system.activeCellsCapabilities;
  1062. WHILE symbol # NIL DO
  1063. IF HasFlag(modifier, symbol.name, position) THEN END;
  1064. symbol := symbol.nextSymbol;
  1065. END;
  1066. modifier := x.modifiers;
  1067. WHILE (modifier # NIL) DO
  1068. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1069. IF modifier.expression # NIL THEN
  1070. v := ConstantExpression(modifier.expression);
  1071. property.SetValue(v);
  1072. IF IsIntegerValue(modifier.expression, int) THEN
  1073. (*property.SetValue(modifier.expression);*)
  1074. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1075. property.SetType(system.longintType);
  1076. ELSIF IsRealValue(modifier.expression, real) THEN
  1077. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1078. property.SetType(system.longrealType);
  1079. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1080. property.SetType(system.booleanType);
  1081. ELSIF IsSetValue(modifier.expression, set) THEN
  1082. property.SetType(system.setType);
  1083. ELSIF IsStringValue(modifier.expression, str) THEN
  1084. (*property.SetValue(modifier.expression);*)
  1085. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1086. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1087. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1088. property.SetType(atype);
  1089. ELSE
  1090. Error(modifier.position, "unsupported property type");
  1091. END;
  1092. ELSE (* flag property *)
  1093. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1094. property.SetType(system.booleanType);
  1095. END;
  1096. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1097. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1098. x.AddProperty(property);
  1099. modifier := modifier.nextModifier;
  1100. END;
  1101. CheckModifiers(modifier, FALSE);
  1102. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1103. (* process parameters *)
  1104. prev := currentScope;
  1105. currentScope := x.cellScope;
  1106. parameter :=x.firstParameter;
  1107. WHILE (parameter # NIL) DO
  1108. VisitParameter(parameter);
  1109. type := parameter.type.resolved;
  1110. IF ~(type IS SyntaxTree.PortType) THEN
  1111. WHILE IsStaticArray(type, type, len) DO
  1112. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1113. END;
  1114. WHILE IsDynamicArray(type, type) DO
  1115. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1116. END;
  1117. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1118. Error(parameter.position, "invalid type, must be port or static array of port ");
  1119. END;
  1120. END;
  1121. parameter := parameter.nextParameter;
  1122. END;
  1123. currentScope := prev;
  1124. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1125. WHILE symbol # NIL DO
  1126. IF symbol IS SyntaxTree.Variable THEN
  1127. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1128. END;
  1129. symbol := symbol.nextSymbol;
  1130. END;
  1131. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1132. IF (x.typeDeclaration = NIL) THEN
  1133. AnonymousTypeDeclaration(x,"Anonymous");
  1134. END;
  1135. x.SetState(SyntaxTree.Resolved);
  1136. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1137. Warning(x.position, "Forbidden empty Body.");
  1138. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1139. Warning(x.position, "Non-empty body for an engine?");
  1140. END;
  1141. END;
  1142. resolvedType := ResolvedType(x);
  1143. END VisitCellType;
  1144. (* check if an object is an array-structured object type
  1145. - determine the array structure
  1146. - collect operators from top to bottom in the inheritance hierarchy
  1147. - check if LEN operator is declared
  1148. - determine number of possible index operators
  1149. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1150. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1151. *)
  1152. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1153. VAR
  1154. indexOperatorCount, i: LONGINT;
  1155. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1156. isTensor: BOOLEAN;
  1157. BEGIN
  1158. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1159. (* determine array structure *)
  1160. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1161. END;
  1162. IF recordType.HasArrayStructure() THEN
  1163. (* the object is an ASOT *)
  1164. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1165. (* reset array access operators *)
  1166. arrayAccessOperators.len := NIL;
  1167. arrayAccessOperators.generalRead := NIL;
  1168. arrayAccessOperators.generalWrite := NIL;
  1169. IF isTensor THEN
  1170. (* all operators of dimensionalities 1 to max *)
  1171. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1172. ELSE
  1173. (* all operators of certain dimensionality *)
  1174. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1175. END;
  1176. NEW(arrayAccessOperators.read, indexOperatorCount);
  1177. NEW(arrayAccessOperators.write, indexOperatorCount);
  1178. FOR i := 0 TO indexOperatorCount - 1 DO
  1179. arrayAccessOperators.read[i] := NIL;
  1180. arrayAccessOperators.write[i] := NIL
  1181. END;
  1182. (* collect access operators in the record scope *)
  1183. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1184. IF arrayAccessOperators.len = NIL THEN
  1185. (* TODO: think about making this operator optional for static array structures *)
  1186. Error(recordType.position, "LEN operator missing")
  1187. END;
  1188. (* show error messages *)
  1189. IF isTensor THEN
  1190. (* require ARRAY [*] OF RANGE *)
  1191. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1192. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1193. ELSE
  1194. (* forbid ARRAY [*] OF RANGE *)
  1195. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1196. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1197. (* require RANGE, RANGE, ... RANGE *)
  1198. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1199. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1200. END;
  1201. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1202. ELSE
  1203. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1204. IF recordType.recordScope.firstOperator # NIL THEN
  1205. RETURN;
  1206. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1207. END
  1208. END
  1209. END ResolveArrayStructure;
  1210. (** collect array access operators in a record scope **)
  1211. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1212. VAR
  1213. baseType: SyntaxTree.Type;
  1214. operator: SyntaxTree.Operator;
  1215. isReadOperator, isGeneralOperator: BOOLEAN;
  1216. indexListSize, indexListKind, hashValue: LONGINT;
  1217. BEGIN
  1218. (* if a parent record scope exists, collect the operators there first *)
  1219. baseType := recordScope.ownerRecord.baseType;
  1220. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1221. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1222. END;
  1223. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1224. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1225. END;
  1226. (* go through all operators in the current record scope *)
  1227. operator := recordScope.firstOperator;
  1228. WHILE operator # NIL DO
  1229. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1230. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1231. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1232. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1233. IF isGeneralOperator THEN
  1234. IF isReadOperator THEN
  1235. arrayAccessOperators.generalRead := operator
  1236. ELSE
  1237. arrayAccessOperators.generalWrite := operator
  1238. END
  1239. ELSE
  1240. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1241. IF isReadOperator THEN
  1242. arrayAccessOperators.read[hashValue] := operator
  1243. ELSE
  1244. arrayAccessOperators.write[hashValue] := operator
  1245. END
  1246. END
  1247. END
  1248. ELSE
  1249. Error(operator.position, 'invalid operator')
  1250. END;
  1251. operator := operator.nextOperator
  1252. END
  1253. END CollectArrayAccessOperators;
  1254. (** the hash value of an index operator **)
  1255. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1256. VAR result: LONGINT;
  1257. BEGIN
  1258. IF isTensor THEN
  1259. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1260. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1261. ELSE
  1262. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1263. END
  1264. ELSE
  1265. result := indexListKind
  1266. END;
  1267. RETURN result
  1268. END IndexOperatorHash;
  1269. (** 2 to the power of exponent **)
  1270. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1271. VAR result, i: LONGINT;
  1272. BEGIN
  1273. result := 1;
  1274. FOR i := 1 TO exponent DO
  1275. result := result * 2;
  1276. END;
  1277. RETURN result
  1278. END TwoToThePowerOf;
  1279. (** check if a LEN operator has a correct signature. i.e.
  1280. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1281. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1282. **)
  1283. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1284. VAR
  1285. procedureType: SyntaxTree.ProcedureType;
  1286. returnedArrayType: SyntaxTree.MathArrayType;
  1287. result: BOOLEAN;
  1288. BEGIN
  1289. result := FALSE;
  1290. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1291. IF (procedureType.numberParameters = 0) THEN
  1292. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1293. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1294. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1295. IF returnedArrayType.form = SyntaxTree.Open THEN
  1296. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1297. result := TRUE
  1298. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1299. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1300. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1301. result := TRUE
  1302. END
  1303. END
  1304. END
  1305. END
  1306. END;
  1307. IF result THEN
  1308. (* export symbol automatically *)
  1309. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1310. ELSE
  1311. Error(operator.position, "LEN operator with invalid signature");
  1312. END;
  1313. RETURN result
  1314. END CheckLenOperator;
  1315. (** check if an index operator has a correct signature. i.e.
  1316. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1317. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1318. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1319. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1320. - determine if it is a read or write operator (existance of return type)
  1321. - check index parameters
  1322. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1323. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1324. [LONGINT] -> binary 0 -> 0
  1325. [RANGE] -> binary 1 -> 1
  1326. [LONGINT, LONGINT] -> binary 00 -> 0
  1327. [LONGINT, RANGE] -> binary 01 -> 1
  1328. [RANGE, LONGINT] -> binary 10 -> 2
  1329. [RANGE, RANGE] -> binary 11 -> 3
  1330. etc.
  1331. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1332. - for read operators, check if return type matches the type of data that is read
  1333. - for write operators, check if last parameter type matches the type of data that is written
  1334. **)
  1335. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1336. VAR
  1337. elementType, otherElementType, dataType: SyntaxTree.Type;
  1338. procedureType: SyntaxTree.ProcedureType;
  1339. mathArrayType: SyntaxTree.MathArrayType;
  1340. parameter: SyntaxTree.Parameter;
  1341. parameterCount, rangeCount, i: LONGINT;
  1342. hasTypeError: BOOLEAN;
  1343. BEGIN
  1344. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1345. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1346. (* determine if it is a read or write operator *)
  1347. isReadOperator := (procedureType.returnType # NIL);
  1348. IF isReadOperator THEN
  1349. indexListSize := parameterCount;
  1350. ELSE
  1351. indexListSize := parameterCount - 1;
  1352. END;
  1353. IF indexListSize < 1 THEN
  1354. Error(operator.position, "index operator with too few parameters");
  1355. RETURN FALSE
  1356. END;
  1357. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1358. (* general operator *)
  1359. isGeneralOperator := TRUE;
  1360. IF indexListSize > 1 THEN
  1361. Error(operator.position, "index operator with too many parameters");
  1362. RETURN FALSE
  1363. END;
  1364. (* ARRAY [*] OF RANGE*)
  1365. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1366. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1367. Error(operator.position, "index parameter not dynamic math array of range");
  1368. RETURN FALSE
  1369. END;
  1370. parameter := procedureType.firstParameter.nextParameter
  1371. ELSE
  1372. (* fixed-dim. operator *)
  1373. isGeneralOperator := FALSE;
  1374. (* check number of index parameters *)
  1375. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1376. (* for tensors, limited to a certain size *)
  1377. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1378. Error(operator.position, "too many index parameters for tensor");
  1379. RETURN FALSE
  1380. END
  1381. ELSE
  1382. (* for non-tensors, depends on dimensionality *)
  1383. IF indexListSize # arrayStructure.Dimensionality() THEN
  1384. Error(operator.position, "index parameter count does not match dimensionality");
  1385. RETURN FALSE
  1386. END
  1387. END;
  1388. (* go through all index parameters
  1389. - count the number of ranges
  1390. - determine the index list kind number
  1391. *)
  1392. indexListKind := 0;
  1393. rangeCount := 0;
  1394. parameter := procedureType.firstParameter;
  1395. FOR i := 1 TO indexListSize DO
  1396. indexListKind := indexListKind * 2;
  1397. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1398. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1399. INC(indexListKind);
  1400. INC(rangeCount)
  1401. ELSE
  1402. Error(parameter.position, "integer or range expected");
  1403. RETURN FALSE
  1404. END;
  1405. parameter := parameter.nextParameter
  1406. END;
  1407. END;
  1408. (*
  1409. - for read operators: check type of last parameter
  1410. - for write operators: check return type
  1411. *)
  1412. IF isReadOperator THEN
  1413. dataType := procedureType.returnType (* the return type *)
  1414. ELSE
  1415. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1416. END;
  1417. elementType := arrayStructure.ElementType();
  1418. hasTypeError := FALSE;
  1419. IF isGeneralOperator THEN
  1420. (* ARRAY [?] OF <Element> *)
  1421. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1422. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1423. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1424. hasTypeError := TRUE
  1425. END
  1426. ELSE
  1427. hasTypeError := TRUE
  1428. END
  1429. ELSE
  1430. IF rangeCount = 0 THEN
  1431. (* <Element> *)
  1432. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1433. ELSE
  1434. (* ARRAY [*, *, ..., *] OF <Element> *)
  1435. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1436. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1437. IF mathArrayType.IsFullyDynamic() THEN
  1438. IF mathArrayType.Dimensionality() = rangeCount THEN
  1439. otherElementType := mathArrayType.ElementType();
  1440. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1441. ELSE
  1442. hasTypeError := TRUE
  1443. END
  1444. ELSE
  1445. hasTypeError := TRUE
  1446. END
  1447. ELSE
  1448. hasTypeError := TRUE
  1449. END
  1450. END
  1451. END;
  1452. IF hasTypeError THEN
  1453. IF isReadOperator THEN
  1454. Error(operator.position, "return type does not match")
  1455. ELSE
  1456. Error(parameter.position, "type of last parameter does not match")
  1457. END;
  1458. RETURN FALSE
  1459. END;
  1460. (* export symbol automatically *)
  1461. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1462. RETURN TRUE
  1463. END CheckIndexOperator;
  1464. (** resolve all pending types (late resolving).
  1465. - type fixes are resolved at the end of the declaration phase
  1466. - type fixes may imply new type fixes that are also entered at the end of the list
  1467. **)
  1468. PROCEDURE FixTypes;
  1469. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1470. BEGIN
  1471. prevScope := currentScope;
  1472. p := typeFixes.Get(currentScope);
  1473. WHILE p # NIL DO
  1474. ASSERT(currentScope # NIL);
  1475. ASSERT(p IS SyntaxTree.Type);
  1476. IF p IS SyntaxTree.PointerType THEN
  1477. FixPointerType(p(SyntaxTree.PointerType))
  1478. ELSIF p IS SyntaxTree.ProcedureType THEN
  1479. FixProcedureType(p(SyntaxTree.ProcedureType))
  1480. ELSE
  1481. HALT(100);
  1482. END;
  1483. p := typeFixes.Get(currentScope);
  1484. END;
  1485. currentScope :=prevScope;
  1486. END FixTypes;
  1487. (**
  1488. resolve type x
  1489. - if x is nil then return nil
  1490. - if x cannot be resolved then the result is invalidType else the result is x
  1491. - the resolved type is entered into x.resolved
  1492. **)
  1493. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1494. VAR prev,resolved: SyntaxTree.Type;
  1495. BEGIN
  1496. prev := resolvedType;
  1497. resolvedType := SyntaxTree.invalidType;
  1498. IF x = NIL THEN resolvedType := NIL
  1499. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1500. END;
  1501. resolved := resolvedType;
  1502. resolvedType := prev;
  1503. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1504. RETURN resolved
  1505. END ResolveType;
  1506. (*** compatibility rules ***)
  1507. (**
  1508. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1509. **)
  1510. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1511. VAR result: SyntaxTree.Type;
  1512. BEGIN
  1513. result := SyntaxTree.invalidType;
  1514. IF type = NIL THEN Error(position, "expression of type NIL");
  1515. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1516. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1517. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1518. ELSE result := type.resolved
  1519. END;
  1520. RETURN result
  1521. END RegularType;
  1522. (** returns signature compatibility of procedure types this and to
  1523. - if not compatible then error is reported
  1524. - compatibility means type equality
  1525. **)
  1526. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1527. VAR result: BOOLEAN;
  1528. BEGIN
  1529. result := SameType(to,this);
  1530. IF ~result THEN
  1531. Error(position, "signature incompatible");
  1532. IF VerboseErrorMessage THEN
  1533. Printout.Info("this",this);
  1534. Printout.Info("to",to);
  1535. END;
  1536. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1537. Error(position, "signature incompatible: realtime flag must be inherited");
  1538. END;
  1539. RETURN result
  1540. END SignatureCompatible;
  1541. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1542. - for var parameters compatibility means same type except for
  1543. - formal is of open array of system byte
  1544. - formal is of record type
  1545. - formal is of open array type
  1546. - formal is of open math array type
  1547. - for value parameters compatibllity means assignment compatibility except for
  1548. - formal is of open array type
  1549. if compatible the return true else report error and return false
  1550. **)
  1551. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1552. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1553. BEGIN
  1554. formalType := RegularType(formal.position,formal.type);
  1555. actualType := RegularType(actual.position,actual.type);
  1556. error := FALSE;
  1557. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1558. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1559. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1560. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1561. END;
  1562. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1563. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1564. ELSIF ~IsVariable(actual) THEN
  1565. result := FALSE; error := TRUE;
  1566. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1567. Error(actual.position,"not a variable: no operator for writing");
  1568. ELSE
  1569. Error(actual.position,"is not a variable");
  1570. END;
  1571. IF VerboseErrorMessage THEN
  1572. Printout.Info("actual",actual);
  1573. Printout.Info("formal",formal);
  1574. END;
  1575. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1576. result := CompatibleTo(system,actualType,formalType);
  1577. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1578. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1579. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1580. IF IsArrayStructuredObjectType(actualType) THEN
  1581. actualType := MathArrayStructureOfType(actualType)
  1582. END;
  1583. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1584. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1585. Error(actual.position,"incompatible non-static actual type");
  1586. END;
  1587. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1588. Error(actual.position,"incompatible tensor (use a range expression)");
  1589. END;
  1590. ELSE
  1591. result := SameType(actualType,formalType)
  1592. END
  1593. ELSE
  1594. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1595. actualType := system.characterType;
  1596. END;
  1597. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1598. result := TRUE; (* special rule for WINAPI parameters *)
  1599. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1600. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1601. ELSE
  1602. result := CompatibleTo(system,actualType,formalType);
  1603. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1604. Error(actual.position,"incompatible non-static actual type");
  1605. END;
  1606. END;
  1607. END;
  1608. IF ~result & ~error THEN
  1609. Error(actual.position,"incompatible parameter");
  1610. IF VerboseErrorMessage THEN
  1611. Printout.Info("actual",actual);
  1612. Printout.Info("formal",formal);
  1613. END;
  1614. END;
  1615. RETURN result
  1616. END ParameterCompatible;
  1617. (** check compatibility for expressions of the form left := right
  1618. - if compatible then return true else error report and return false
  1619. - check if left is variable
  1620. - check compatiblity
  1621. **)
  1622. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1623. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1624. BEGIN
  1625. result := FALSE;
  1626. leftType := RegularType(left.position,left.type);
  1627. rightType := RegularType(right.position,right.type);
  1628. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1629. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1630. END;
  1631. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1632. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1633. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1634. (* error already handled *)
  1635. result := TRUE;
  1636. ELSIF ~IsVariable(left) THEN
  1637. Error(left.position,"is not a variable");
  1638. IF VerboseErrorMessage THEN
  1639. Printout.Info("left",left);
  1640. Printout.Info("right",right);
  1641. END;
  1642. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1643. result := TRUE;
  1644. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1645. result := TRUE
  1646. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1647. Error(left.position,"incompatible assignment");
  1648. IF VerboseErrorMessage THEN
  1649. Printout.Info("left",left);
  1650. Printout.Info("right",right);
  1651. END;
  1652. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1653. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1654. Error(right.position,"forbidden assignment of a nested procedure");
  1655. ELSE
  1656. result := TRUE
  1657. END;
  1658. RETURN result
  1659. END AssignmentCompatible;
  1660. (*** values ***)
  1661. (** check and resolve integer value **)
  1662. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1663. VAR hugeint: HUGEINT;
  1664. BEGIN
  1665. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1666. value.SetType(Global.GetIntegerType(system,hugeint));
  1667. resolvedExpression := value
  1668. END VisitIntegerValue;
  1669. (** check and resolve real value **)
  1670. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1671. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1672. BEGIN
  1673. subtype := value(SyntaxTree.RealValue).subtype;
  1674. IF subtype = Scanner.Real THEN
  1675. type := system.realType
  1676. ELSIF subtype = Scanner.Longreal THEN
  1677. type := system.longrealType
  1678. ELSE
  1679. HALT(100)
  1680. END;
  1681. value.SetType(type);
  1682. resolvedExpression := value
  1683. END VisitRealValue;
  1684. (** check and resolve complex value **)
  1685. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1686. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1687. BEGIN
  1688. subtype := value(SyntaxTree.ComplexValue).subtype;
  1689. IF subtype = Scanner.Real THEN
  1690. type := system.complexType
  1691. ELSIF subtype = Scanner.Longreal THEN
  1692. type := system.longcomplexType
  1693. ELSE
  1694. HALT(100)
  1695. END;
  1696. value.SetType(type);
  1697. resolvedExpression := value
  1698. END VisitComplexValue;
  1699. (** check and resolve set value **)
  1700. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1701. BEGIN
  1702. value.SetType(system.setType);
  1703. resolvedExpression := value
  1704. END VisitSetValue;
  1705. (** check and resolve set value **)
  1706. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1707. BEGIN
  1708. value.SetType(SyntaxTree.invalidType);
  1709. resolvedExpression := value
  1710. END VisitMathArrayValue;
  1711. (** check and resolve boolean value **)
  1712. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1713. BEGIN
  1714. value.SetType(system.booleanType);
  1715. resolvedExpression := value
  1716. END VisitBooleanValue;
  1717. (** check and resolve string value **)
  1718. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1719. BEGIN
  1720. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1721. resolvedExpression := value
  1722. END VisitStringValue;
  1723. (** check and resolve character value **)
  1724. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1725. BEGIN
  1726. value.SetType(system.characterType);
  1727. resolvedExpression := value
  1728. END VisitCharacterValue;
  1729. (** check and resolve nil value **)
  1730. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1731. BEGIN
  1732. value.SetType(system.nilType);
  1733. resolvedExpression := value
  1734. END VisitNilValue;
  1735. (** check and resolve enumerator value **)
  1736. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1737. BEGIN
  1738. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1739. ASSERT(value.type # NIL);
  1740. resolvedExpression := value
  1741. END VisitEnumerationValue;
  1742. (*** expressions ***)
  1743. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1744. - check all elements on integer type
  1745. - if element range is constant, then check lower and upper bound
  1746. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1747. if an error occurs then report error and return invalidExpression
  1748. **)
  1749. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1750. VAR
  1751. i: LONGINT;
  1752. element: SyntaxTree.Expression;
  1753. constant: BOOLEAN;
  1754. elements: SyntaxTree.ExpressionList;
  1755. s: SET;
  1756. result: SyntaxTree.Expression;
  1757. value: SyntaxTree.Value;
  1758. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1759. VAR
  1760. left, right: SyntaxTree.Expression;
  1761. elementResult: SyntaxTree.Expression;
  1762. leftInteger, rightInteger, temp: LONGINT;
  1763. BEGIN
  1764. (* set context of range *)
  1765. IF element IS SyntaxTree.RangeExpression THEN
  1766. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1767. END;
  1768. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1769. IF elementResult = SyntaxTree.invalidExpression THEN
  1770. (* error already reported *)
  1771. constant := FALSE
  1772. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1773. (* the element is a range expression *)
  1774. (* extract left and right hand side of range *)
  1775. left := elementResult(SyntaxTree.RangeExpression).first;
  1776. right := elementResult(SyntaxTree.RangeExpression).last;
  1777. (* guaranteed by VisitRangeExpression: *)
  1778. ASSERT((left # NIL) & (right # NIL));
  1779. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1780. ELSE
  1781. (* the element is not a range expression *)
  1782. (* check type and add conversion if needed *)
  1783. IF IsIntegerType(elementResult.type.resolved) THEN
  1784. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1785. ELSE
  1786. Error(elementResult.position, "non integer element in set");
  1787. elementResult := SyntaxTree.invalidExpression;
  1788. constant := FALSE
  1789. END;
  1790. left := elementResult;
  1791. right := elementResult
  1792. END;
  1793. IF elementResult # SyntaxTree.invalidExpression THEN
  1794. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1795. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1796. Error(left.position,"not allowed set integer value");
  1797. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1798. Error(right.position,"not allowed set integer value");
  1799. END
  1800. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1801. Error(right.position,"not allowed set integer value");
  1802. ELSE
  1803. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1804. s := {};
  1805. ELSE
  1806. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1807. IF leftInteger < 0 THEN leftInteger := 0 END;
  1808. (*!!!!!!!!! this is a hack !!!!!!! *)
  1809. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1810. extends the range x..31 to x..63 !!!!!! *)
  1811. s := s + {leftInteger..rightInteger};
  1812. END;
  1813. END;
  1814. ELSE
  1815. constant := FALSE;
  1816. END
  1817. END;
  1818. RETURN elementResult
  1819. END CheckElement;
  1820. BEGIN
  1821. result := set; constant := TRUE; s := {}; elements := set.elements;
  1822. IF elements # NIL THEN
  1823. FOR i := 0 TO elements.Length()-1 DO
  1824. element := elements.GetExpression(i);
  1825. element := CheckElement(element);
  1826. IF element = SyntaxTree.invalidExpression THEN
  1827. result := SyntaxTree.invalidExpression
  1828. END;
  1829. elements.SetExpression(i,element);
  1830. END;
  1831. END;
  1832. IF constant THEN
  1833. value := SyntaxTree.NewSetValue(set.position,s);
  1834. value.SetType(system.setType);
  1835. result.SetResolved(value);
  1836. END;
  1837. (* optimization possible
  1838. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1839. left this to the programmer...
  1840. *)
  1841. result.SetType(system.setType);
  1842. resolvedExpression := result;
  1843. END VisitSet;
  1844. (*
  1845. old variant: quite generic but needs better conversion handling, do this?
  1846. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1847. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1848. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1849. BEGIN
  1850. type := NIL;
  1851. numberElements := x.elements.Length();
  1852. FOR i := 0 TO numberElements-1 DO
  1853. expression := x.elements.GetExpression(i);
  1854. position := expression.position;
  1855. expression := ResolveExpression(x.elements.GetExpression(i));
  1856. x.elements.SetExpression(i,de);
  1857. IF type = NIL THEN
  1858. type := expression.type;
  1859. ELSIF CompatibleTo(system,expression.type,type) THEN
  1860. (* ok *)
  1861. ELSIF CompatibleTo(system,type,expression.type) THEN
  1862. type := expression.type
  1863. ELSE
  1864. Error(expression.position, "incompatible element types");
  1865. type := SyntaxTree.invalidType;
  1866. END;
  1867. END;
  1868. isValue := TRUE;
  1869. FOR i := 0 TO numberElements-1 DO
  1870. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1871. x.elements.SetExpression(i,expression);
  1872. isValue := isValue & (expression.resolved # NIL);
  1873. END;
  1874. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1875. arrayType.SetArrayBase(type);
  1876. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1877. IF isValue THEN
  1878. value := SyntaxTree.NewMathArrayValue(position);
  1879. value.SetElements(x.elements);
  1880. x.SetResolved(value);
  1881. END;
  1882. x.SetType(arrayType);
  1883. resolvedExpression := x;
  1884. END VisitMathArrayExpression;
  1885. *)
  1886. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1887. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1888. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1889. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1890. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1891. BEGIN
  1892. numberElements := x.elements.Length();
  1893. FOR i := 0 TO numberElements-1 DO
  1894. expression := x.elements.GetExpression(i);
  1895. IF expression IS SyntaxTree.MathArrayExpression THEN
  1896. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1897. ELSE
  1898. position := expression.position;
  1899. expression := ResolveExpression(x.elements.GetExpression(i));
  1900. x.elements.SetExpression(i,expression);
  1901. IF type = NIL THEN
  1902. type := expression.type;
  1903. ELSIF CompatibleTo(system,expression.type,type) THEN
  1904. (* ok *)
  1905. ELSIF CompatibleTo(system,type,expression.type) THEN
  1906. type := expression.type
  1907. ELSE
  1908. Error(expression.position, "incompatible element types");
  1909. type := SyntaxTree.invalidType;
  1910. END;
  1911. END;
  1912. END;
  1913. END RecursivelyFindType;
  1914. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1915. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1916. BEGIN
  1917. numberElements := x.elements.Length();
  1918. FOR i := 0 TO numberElements-1 DO
  1919. expression := x.elements.GetExpression(i);
  1920. IF expression IS SyntaxTree.MathArrayExpression THEN
  1921. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1922. ELSE
  1923. position := expression.position;
  1924. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1925. x.elements.SetExpression(i,expression);
  1926. isValue := isValue & (expression.resolved # NIL);
  1927. END;
  1928. END;
  1929. END RecursivelySetExpression;
  1930. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1931. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1932. arrayType: SyntaxTree.MathArrayType;
  1933. BEGIN
  1934. numberElements := x.elements.Length();
  1935. baseType := NIL;
  1936. gsize := 0;
  1937. FOR i := 0 TO numberElements-1 DO
  1938. expression := x.elements.GetExpression(i);
  1939. IF expression IS SyntaxTree.MathArrayExpression THEN
  1940. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1941. IF i=0 THEN
  1942. gsize := size;
  1943. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1944. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1945. ELSE expression.SetType(baseType)
  1946. END;
  1947. ELSIF baseType = NIL THEN baseType := type;
  1948. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1949. END;
  1950. END;
  1951. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1952. arrayType.SetArrayBase(baseType);
  1953. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1954. RETURN ResolveType(arrayType);
  1955. END RecursivelySetType;
  1956. BEGIN
  1957. type := NIL;
  1958. RecursivelyFindType(x);
  1959. isValue := TRUE;
  1960. RecursivelySetExpression(x);
  1961. arrayType := RecursivelySetType(x);
  1962. x.SetType(arrayType);
  1963. IF isValue THEN
  1964. value := SyntaxTree.NewMathArrayValue(x.position);
  1965. value.SetArray(x);
  1966. x.SetResolved(value);
  1967. value.SetType(arrayType);
  1968. END;
  1969. x.SetType(arrayType);
  1970. resolvedExpression := x;
  1971. END VisitMathArrayExpression;
  1972. (** check and resolve unary expression **)
  1973. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1974. VAR
  1975. left: SyntaxTree.Expression;
  1976. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1977. bool: BOOLEAN;
  1978. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1979. value: SyntaxTree.Value;
  1980. BEGIN
  1981. type := SyntaxTree.invalidType;
  1982. left := ResolveExpression(unaryExpression.left);
  1983. unaryExpression.SetLeft(left);
  1984. operator := unaryExpression.operator;
  1985. result := unaryExpression;
  1986. IF ~system.operatorDefined[operator] THEN
  1987. Error(left.position,"Operator Not Defined");
  1988. RETURN
  1989. ELSIF left.type = NIL THEN
  1990. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1991. resolvedExpression := SyntaxTree.invalidExpression;
  1992. RETURN
  1993. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1994. RETURN
  1995. END;
  1996. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1997. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1998. END;
  1999. IF operatorCall # NIL THEN
  2000. result := operatorCall;
  2001. type := operatorCall.type;
  2002. (* admissible operators
  2003. Minus number, set
  2004. Not boolean
  2005. *)
  2006. ELSE
  2007. CASE unaryExpression.operator OF
  2008. |Scanner.Minus:
  2009. IF IsIntegerType(left.type.resolved) THEN
  2010. IF left.resolved # NIL THEN
  2011. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2012. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2013. result.SetResolved(value);
  2014. type := Global.GetIntegerType(system,int);
  2015. value.SetType(type);
  2016. ELSE
  2017. type := left.type
  2018. END
  2019. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2020. IF IsRealValue(left,real) THEN
  2021. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2022. result.SetResolved(value);
  2023. type := left.type;
  2024. value.SetType(type);
  2025. ELSE
  2026. type := left.type;
  2027. END;
  2028. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2029. IF IsSetValue(left,set) THEN
  2030. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2031. result.SetResolved(value);
  2032. type := left.type;
  2033. value.SetType(type);
  2034. ELSE
  2035. type := left.type;
  2036. END;
  2037. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2038. IF IsComplexValue(left, real, imaginary) THEN
  2039. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2040. result.SetResolved(value);
  2041. type := left.type;
  2042. value.SetType(type);
  2043. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2044. ELSE
  2045. type := left.type;
  2046. END
  2047. ELSE
  2048. Error(left.position,"unary operator not applicable");
  2049. END;
  2050. |Scanner.Not:
  2051. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2052. IF IsBooleanValue(left,bool) THEN
  2053. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2054. result.SetResolved(value);
  2055. type := system.booleanType;
  2056. value.SetType(type);
  2057. ELSE
  2058. type := system.booleanType;
  2059. END;
  2060. ELSE
  2061. Error(left.position,"unary operator not applicable");
  2062. END;
  2063. |Scanner.Plus:
  2064. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2065. result := left; type := left.type;
  2066. ELSE
  2067. Error(left.position,"unary operator not applicable");
  2068. END;
  2069. (* ADDRESS OF *)
  2070. |Scanner.Address:
  2071. IF HasAddress(left) THEN
  2072. type := system.addressType;
  2073. ELSE
  2074. type := SyntaxTree.invalidType;
  2075. Error(left.position,"has no address");
  2076. Printout.Info("par", left);
  2077. END;
  2078. (* SIZE OF *)
  2079. |Scanner.Size:
  2080. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2081. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2082. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2083. value := SyntaxTree.NewIntegerValue(left.position, int);
  2084. result.SetResolved(value);
  2085. type := Global.GetIntegerType(system,int);
  2086. value.SetType(type)
  2087. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2088. ELSE
  2089. (* for variables, system sizeof could represent the physically occupied size
  2090. determined via the type descriptor, implement that ? *)
  2091. Error(left.position,"is not a type symbol");
  2092. END
  2093. (* ALIAS OF *)
  2094. |Scanner.Alias:
  2095. type := left.type.resolved;
  2096. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2097. type := SyntaxTree.invalidType;
  2098. Error(left.position,"alias on non math array type");
  2099. END;
  2100. ELSE
  2101. Error(left.position,"unary operator not defined");
  2102. END;
  2103. END;
  2104. result.SetType(type);
  2105. resolvedExpression := result
  2106. END VisitUnaryExpression;
  2107. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2108. VAR
  2109. result: SyntaxTree.Expression;
  2110. array: SyntaxTree.MathArrayExpression;
  2111. value: SyntaxTree.MathArrayValue;
  2112. isValue: BOOLEAN;
  2113. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2114. BEGIN
  2115. type := type.resolved;
  2116. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2117. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2118. END;
  2119. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2120. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2121. END;
  2122. RETURN type
  2123. END BaseType;
  2124. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2125. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2126. BEGIN
  2127. numberElements := x.elements.Length();
  2128. FOR i := 0 TO numberElements-1 DO
  2129. expression := x.elements.GetExpression(i);
  2130. IF expression IS SyntaxTree.MathArrayExpression THEN
  2131. array := SyntaxTree.NewMathArrayExpression(position);
  2132. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2133. expression := array;
  2134. ELSE
  2135. position := expression.position;
  2136. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2137. isValue := isValue & (expression.resolved # NIL);
  2138. END;
  2139. to.elements.AddExpression(expression);
  2140. END;
  2141. END RecursivelyConvert;
  2142. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2143. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2144. arrayType: SyntaxTree.MathArrayType;
  2145. BEGIN
  2146. numberElements := x.elements.Length();
  2147. baseType := NIL;
  2148. gsize := 0;
  2149. FOR i := 0 TO numberElements-1 DO
  2150. expression := x.elements.GetExpression(i);
  2151. IF expression IS SyntaxTree.MathArrayExpression THEN
  2152. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2153. IF i=0 THEN
  2154. gsize := size;
  2155. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2156. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2157. ELSE expression.SetType(baseType)
  2158. END;
  2159. ELSIF baseType = NIL THEN baseType := type;
  2160. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2161. END;
  2162. END;
  2163. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2164. arrayType.SetArrayBase(baseType);
  2165. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2166. RETURN ResolveType(arrayType);
  2167. END RecursivelySetType;
  2168. BEGIN
  2169. result := SyntaxTree.invalidExpression;
  2170. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2171. result := expression (* do not convert *)
  2172. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2173. isValue := TRUE;
  2174. type := BaseType(type);
  2175. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2176. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2177. value := SyntaxTree.NewMathArrayValue(array.position);
  2178. value.SetArray(array);
  2179. value.SetType(RecursivelySetType(array));
  2180. result := value;
  2181. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2182. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2183. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2184. IF result = NIL THEN
  2185. result := SyntaxTree.invalidExpression;
  2186. Error(position, "incompatible conversion");
  2187. IF VerboseErrorMessage THEN
  2188. Printout.Info("expression",expression);
  2189. Printout.Info("type",type);
  2190. END;
  2191. END;
  2192. END;
  2193. RETURN result
  2194. END MathArrayConversion;
  2195. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2196. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2197. BEGIN
  2198. result := expression; type := type.resolved;
  2199. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2200. (* skip, no conversion *)
  2201. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2202. int := expression(SyntaxTree.IntegerValue).hvalue;
  2203. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2204. int := Global.ConvertSigned(int,system.SizeOf(type));
  2205. result := SyntaxTree.NewIntegerValue(position,int);
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2208. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2209. result := SyntaxTree.NewIntegerValue(position,int);
  2210. result.SetType(type);
  2211. ELSIF (type IS SyntaxTree.FloatType) THEN
  2212. result := SyntaxTree.NewRealValue(expression.position,int);
  2213. result.SetType(type);
  2214. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2215. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2216. result.SetType(type);
  2217. ELSIF (type IS SyntaxTree.SetType) THEN
  2218. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2219. result.SetType(type);
  2220. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2221. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2222. result.SetType(type);
  2223. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2224. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2225. Error(position, "huge integer value incompatible to enumeration");
  2226. END;
  2227. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.PortType) THEN
  2230. result := ConvertValue(position, expression, system.integerType);
  2231. ELSE
  2232. Error(position, "integer value cannot be converted");
  2233. result := SyntaxTree.invalidExpression;
  2234. IF VerboseErrorMessage THEN
  2235. Printout.Info("expression",expression);
  2236. Printout.Info("type",type);
  2237. END;
  2238. END;
  2239. ELSIF IsRealValue(expression,real) THEN
  2240. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2241. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2242. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2245. int := ENTIERH(real);
  2246. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.FloatType) THEN
  2249. result := SyntaxTree.NewRealValue(position,real);
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2252. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2253. result.SetType(type);
  2254. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2255. ELSIF (type IS SyntaxTree.PortType) THEN
  2256. result := ConvertValue(position, expression, system.integerType);
  2257. ELSE
  2258. Error(position, "real value cannot be converted");
  2259. result := SyntaxTree.invalidExpression;
  2260. END
  2261. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2262. IF (type IS SyntaxTree.ComplexType) THEN
  2263. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2264. result.SetType(type);
  2265. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2266. ELSE
  2267. Error(position, "complex value cannot be converted");
  2268. result := SyntaxTree.invalidExpression;
  2269. END
  2270. ELSIF IsSetValue(expression,set) THEN
  2271. IF (type IS SyntaxTree.IntegerType) THEN
  2272. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2273. result.SetType(type);
  2274. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2275. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2276. result.SetType(type);
  2277. ELSIF (type IS SyntaxTree.PortType) THEN
  2278. result := ConvertValue(position, expression, system.integerType);
  2279. ELSE
  2280. Error(position, "set value cannot be converted");
  2281. result := SyntaxTree.invalidExpression;
  2282. END;
  2283. ELSIF IsStringValue(expression,string) THEN
  2284. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2285. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2286. result.SetType(type);
  2287. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2288. ELSE
  2289. Error(position, "string value cannot be converted");
  2290. result := SyntaxTree.invalidExpression;
  2291. END;
  2292. ELSIF IsCharacterValue(expression,char) THEN
  2293. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2294. string[0] := char; string[1] := 0X;
  2295. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2296. result := SyntaxTree.NewStringValue(expression.position,string);
  2297. result.SetType(type);
  2298. ELSIF (type IS SyntaxTree.ByteType) THEN
  2299. (* do not simply set the new type as this could invalidate types of constants *)
  2300. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2301. result.SetType(type)
  2302. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2303. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2304. result.SetType(type);
  2305. ELSIF (type IS SyntaxTree.SetType) THEN
  2306. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2307. result.SetType(type);
  2308. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2309. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2310. result.SetType(type);
  2311. ELSIF (type IS SyntaxTree.PortType) THEN
  2312. result := ConvertValue(position, expression, system.integerType);
  2313. ELSE
  2314. Error(position, "character value cannot be converted");
  2315. result := SyntaxTree.invalidExpression;
  2316. END;
  2317. ELSIF expression IS SyntaxTree.NilValue THEN
  2318. IF type IS SyntaxTree.AddressType THEN
  2319. result := SyntaxTree.NewIntegerValue(position,0);
  2320. result.SetType(type);
  2321. ELSE
  2322. result := expression;
  2323. END;
  2324. (* nothing to be done *)
  2325. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2326. result := MathArrayConversion(position, expression,type);
  2327. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2328. int := expression(SyntaxTree.EnumerationValue).value;
  2329. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2330. int := Global.ConvertSigned(int,system.SizeOf(type));
  2331. result := SyntaxTree.NewIntegerValue(position,int);
  2332. result.SetType(type);
  2333. ELSE
  2334. result := expression;
  2335. END;
  2336. (* nothing to be done *)
  2337. ELSE
  2338. Error(position, "expression cannot be converted");
  2339. IF VerboseErrorMessage THEN
  2340. Printout.Info("expression",expression);
  2341. Printout.Info("type",type);
  2342. END;
  2343. result := SyntaxTree.invalidExpression;
  2344. END;
  2345. RETURN result
  2346. END ConvertValue;
  2347. (**
  2348. return a conversion of an expression to a given type
  2349. - if expression is already of same type then return expression
  2350. - if incompatible conversion then report error and return invalidExpression
  2351. **)
  2352. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2353. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2354. BEGIN
  2355. type := type.resolved;
  2356. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2357. result := expression;
  2358. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2359. ELSIF expression = NIL THEN (* NIL expression *)
  2360. ELSIF expression.type = NIL THEN
  2361. Error(position, "expression of type NIL cannot be converted");
  2362. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2363. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2364. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2365. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2366. ELSIF expression.resolved # NIL THEN (* value *)
  2367. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2368. IF value IS SyntaxTree.Value THEN
  2369. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2370. result.SetResolved(value(SyntaxTree.Value));
  2371. result.SetType(value.type);
  2372. ELSE
  2373. result := value
  2374. END;
  2375. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2376. expressionList := SyntaxTree.NewExpressionList();
  2377. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2378. typeDeclaration.SetDeclaredType(type);
  2379. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2380. typeSymbol.SetType(typeDeclaration.type);
  2381. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2382. expressionList.AddExpression(expression);
  2383. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2384. result.SetType(type);
  2385. ELSIF IsArrayStructuredObjectType(type) THEN
  2386. (* no type can be converted to an array-structured object type *)
  2387. HALT(100)
  2388. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2389. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2390. result := MathArrayConversion(position, expression,type);
  2391. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2392. expression := ConvertToMathArray(expression);
  2393. type := MathArrayStructureOfType(type);
  2394. result := MathArrayConversion(position, expression, type)
  2395. ELSE
  2396. Error(expression.position,"cannot convert non array type to array type")
  2397. END;
  2398. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2399. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2400. OR ~(type IS SyntaxTree.ArrayType) THEN
  2401. Error(expression.position,"cannot convert array type to non-array type")
  2402. END;
  2403. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2404. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2405. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2406. (*skip, no conversion*)
  2407. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2408. (* skip, no conversion *)
  2409. ELSE
  2410. ASSERT(~(type IS SyntaxTree.RangeType));
  2411. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2412. ASSERT(type # NIL);
  2413. END;
  2414. RETURN result
  2415. END NewConversion;
  2416. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2417. BEGIN
  2418. IF CompatibleTo(system,expression.type, type) THEN
  2419. RETURN NewConversion(position, expression, type, NIL);
  2420. ELSE
  2421. Error(expression.position, "incompatible expression");
  2422. RETURN SyntaxTree.invalidExpression
  2423. END;
  2424. END CompatibleConversion;
  2425. (**
  2426. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2427. **)
  2428. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2429. VAR leftType,rightType: SyntaxTree.Type;
  2430. BEGIN
  2431. IF left.type = NIL THEN Error(left.position,"no type")
  2432. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2433. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2434. (* error already handled *)
  2435. ELSE
  2436. leftType := left.type.resolved; rightType := right.type.resolved;
  2437. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2438. right := NewConversion(right.position, right, leftType, NIL);
  2439. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2440. left := NewConversion(left.position,left,rightType,NIL);
  2441. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2442. right := NewConversion(right.position, right, leftType, NIL);
  2443. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2444. left := NewConversion(left.position,left,rightType,NIL);
  2445. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2446. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2447. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2448. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2449. ELSIF
  2450. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2451. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2452. (* must be the case LONGREAL / COMPLEX ) *)
  2453. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2454. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2455. ELSE
  2456. Error(left.position,"incompatible operands");
  2457. END;
  2458. END;
  2459. END ConvertOperands;
  2460. (** find and return best operator matching to parameter list (nil, if none)
  2461. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2462. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2463. **)
  2464. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2465. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2466. identifier: SyntaxTree.Identifier;
  2467. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2468. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2469. BEGIN
  2470. operator := scope.firstOperator;
  2471. WHILE(operator # NIL) DO
  2472. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2473. procedureType := operator.type(SyntaxTree.ProcedureType);
  2474. distance := Distance(system, procedureType,actualParameters);
  2475. IF (distance < Infinity) THEN
  2476. IF returnType # NIL THEN
  2477. IF procedureType.returnType = NIL THEN
  2478. distance := Infinity
  2479. ELSE
  2480. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2481. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2482. END;
  2483. END;
  2484. END;
  2485. (*
  2486. IF distance < Infinity THEN
  2487. TRACE(distance, operator);
  2488. Printout.Info("potential operator",operator);
  2489. ELSE
  2490. Printout.Info("operator not possible",operator);
  2491. END;
  2492. *)
  2493. IF distance < bestDistance THEN
  2494. bestDistance := distance;
  2495. bestOperator := operator;
  2496. END;
  2497. END;
  2498. operator := operator.nextOperator;
  2499. END;
  2500. (*
  2501. Printout.Info("taken operator",bestOperator);
  2502. *)
  2503. END FindInScope;
  2504. BEGIN
  2505. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2506. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2507. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2508. import := currentScope.ownerModule.moduleScope.firstImport;
  2509. WHILE (bestDistance > 0) & (import # NIL) DO
  2510. IF import.module # NIL THEN
  2511. identifier := Global.GetIdentifier(operator,import.module.case);
  2512. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2513. END;
  2514. import := import.nextImport;
  2515. END;
  2516. RETURN bestOperator
  2517. END FindOperator;
  2518. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2519. BEGIN
  2520. currentScope := scope;
  2521. END SetCurrentScope;
  2522. (**
  2523. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2524. - handle LEN and DIM operator for array-structured object types
  2525. - find operator, if found then
  2526. - if in other module then add import designator
  2527. - create symbol designator for operator
  2528. - if error then return invalidExpression, if no operator then return NIL
  2529. **)
  2530. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2531. VAR
  2532. operator: SyntaxTree.Operator;
  2533. import: SyntaxTree.Import;
  2534. expression, result: SyntaxTree.Expression;
  2535. designator: SyntaxTree.Designator;
  2536. actualParameters, tempList: SyntaxTree.ExpressionList;
  2537. recordType: SyntaxTree.RecordType;
  2538. castReturnType : SyntaxTree.MathArrayType;
  2539. BEGIN
  2540. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2541. result := SyntaxTree.invalidExpression
  2542. ELSIF leftExpression = NIL THEN
  2543. result := NIL
  2544. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2545. (* LEN or DIM operator on array-structured object type *)
  2546. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2547. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2548. IF recordType.arrayAccessOperators.len = NIL THEN
  2549. Error(position, "call of undeclared LEN operator");
  2550. result := SyntaxTree.invalidExpression
  2551. ELSE
  2552. ASSERT(leftExpression IS SyntaxTree.Designator);
  2553. designator := leftExpression(SyntaxTree.Designator);
  2554. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2555. ASSERT(expression IS SyntaxTree.Designator);
  2556. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2557. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2558. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2559. result := designator
  2560. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2561. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2562. tempList := SyntaxTree.NewExpressionList();
  2563. tempList.AddExpression(rightExpression);
  2564. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2565. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2566. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2567. tempList := SyntaxTree.NewExpressionList();
  2568. tempList.AddExpression(designator);
  2569. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2570. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2571. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2572. END
  2573. END;
  2574. ELSE
  2575. IF ~complexNumbersImported THEN
  2576. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2577. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2578. THEN
  2579. (* operators on complex numbers *)
  2580. ImportModule(Global.ComplexNumbersName,position);
  2581. complexNumbersImported := TRUE;
  2582. END;
  2583. END;
  2584. (* import OCArrayBase if needed *)
  2585. IF ~arrayBaseImported THEN
  2586. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2587. IF op = Global.Dim THEN
  2588. (* not existing in OCArrayBase *)
  2589. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2590. (* not existing in OCArrayBase *)
  2591. ELSE
  2592. ImportModule(Global.ArrayBaseName,position);
  2593. arrayBaseImported := TRUE;
  2594. END
  2595. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2596. ImportModule(Global.ArrayBaseName,position);
  2597. arrayBaseImported := TRUE
  2598. END;
  2599. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2600. (* LEN(RANGE) *)
  2601. ImportModule(Global.ArrayBaseName,position);
  2602. arrayBaseImported := TRUE;
  2603. END;
  2604. END;
  2605. actualParameters := SyntaxTree.NewExpressionList();
  2606. actualParameters.AddExpression(leftExpression);
  2607. IF rightExpression # NIL THEN
  2608. actualParameters.AddExpression(rightExpression)
  2609. END;
  2610. operator := FindOperator(system,op,actualParameters,resultType);
  2611. IF operator # NIL THEN
  2612. designator := NIL;
  2613. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2614. import := currentScope.ownerModule.moduleScope.firstImport;
  2615. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2616. import := import.nextImport;
  2617. END;
  2618. expression := NewSymbolDesignator(position,NIL,import);
  2619. designator := expression(SyntaxTree.Designator);
  2620. END;
  2621. expression := NewSymbolDesignator(position,designator,operator);
  2622. designator := expression(SyntaxTree.Designator);
  2623. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2624. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2625. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2626. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2627. result.SetType(castReturnType);
  2628. END;
  2629. ELSE
  2630. result := NIL;
  2631. END;
  2632. END;
  2633. RETURN result
  2634. END NewOperatorCall;
  2635. (** check and resolve binary expression **)
  2636. (*! clean up *)
  2637. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2638. VAR left,right,result: SyntaxTree.Expression;
  2639. leftType, rightType: SyntaxTree.Type;
  2640. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2641. cl,cr: CHAR;
  2642. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2643. type: SyntaxTree.Type;
  2644. value: SyntaxTree.Value;
  2645. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2646. integerConstantFolding: BOOLEAN;
  2647. list: SyntaxTree.ExpressionList;
  2648. PROCEDURE NewBool(v: BOOLEAN);
  2649. BEGIN
  2650. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2651. value.SetType(system.booleanType);
  2652. result.SetResolved(value);
  2653. type := system.booleanType
  2654. END NewBool;
  2655. PROCEDURE NewSet(v: SET);
  2656. BEGIN
  2657. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2658. value.SetType(system.setType);
  2659. result.SetResolved(value);
  2660. type := system.setType;
  2661. END NewSet;
  2662. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2663. BEGIN
  2664. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2665. (* type cast to "larger" type only if the value is still in the range *)
  2666. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2667. value.SetType(t);
  2668. END;
  2669. result.SetResolved(value);
  2670. type := value.type;
  2671. END NewInteger;
  2672. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2673. BEGIN
  2674. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2675. value.SetType(t);
  2676. result.SetResolved(value);
  2677. type := t;
  2678. END NewReal;
  2679. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2680. BEGIN
  2681. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2682. value.SetType(t);
  2683. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2684. result.SetResolved(value);
  2685. type := t;
  2686. END NewComplex;
  2687. BEGIN
  2688. type := SyntaxTree.invalidType;
  2689. left := ResolveExpression(binaryExpression.left);
  2690. right := ResolveExpression(binaryExpression.right);
  2691. binaryExpression.SetLeft(left);
  2692. binaryExpression.SetRight(right);
  2693. result := binaryExpression;
  2694. operator := binaryExpression.operator;
  2695. IF ~system.operatorDefined[operator] THEN
  2696. Error(left.position,"Operator Not Defined");
  2697. result := SyntaxTree.invalidExpression;
  2698. RETURN
  2699. END;
  2700. IF left.type = NIL THEN
  2701. Error(left.position,"Expression has no result type");
  2702. result := SyntaxTree.invalidExpression;
  2703. RETURN;
  2704. END;
  2705. IF right.type = NIL THEN
  2706. Error(right.position,"Expression has no result type");
  2707. result := SyntaxTree.invalidExpression;
  2708. RETURN;
  2709. END;
  2710. leftType := left.type.resolved; rightType := right.type.resolved;
  2711. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2712. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2713. END;
  2714. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2715. list := SyntaxTree.NewExpressionList();
  2716. list.AddExpression(right);
  2717. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2718. END;
  2719. IF operatorCall # NIL THEN
  2720. result := operatorCall;
  2721. type := operatorCall.type;
  2722. (* admissible operators:
  2723. Times, Plus, Minus numeric numeric numeric
  2724. set set set
  2725. Slash numeric numeric real /complex
  2726. set set set
  2727. Div , Mod integer integer integer
  2728. And, Or bool bool bool
  2729. Equal, Unequal basic basic bool
  2730. pointer pointer bool
  2731. object object bool
  2732. record record bool
  2733. string string bool
  2734. enumerator enumerator bool
  2735. Less, LessEqual,
  2736. Greater, GreaterEqual integer/real integer/real bool
  2737. enumerator enumerator bool
  2738. In integer set bool
  2739. Is pointer type bool
  2740. object type bool
  2741. record type bool
  2742. Upto: special abbreviation for a..b
  2743. *)
  2744. ELSIF (left.type = NIL) THEN
  2745. Error(left.position,"type (left operand) = NIL in binary expression");
  2746. D.Str("nil type in "); D.Type(left); D.Ln;
  2747. result := SyntaxTree.invalidExpression;
  2748. ELSIF (right.type = NIL) THEN
  2749. Error(right.position,"type (right operand) = NIL in binary expression");
  2750. result := SyntaxTree.invalidExpression;
  2751. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2752. result := SyntaxTree.invalidExpression;
  2753. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2754. HALT(100);
  2755. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2756. type := system.booleanType;
  2757. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2758. Error(right.position,"is not a type ");
  2759. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2760. Error(binaryExpression.position,"is not a type extension of ");
  2761. IF VerboseErrorMessage THEN
  2762. Printout.Info("left",left);
  2763. Printout.Info("right",right);
  2764. END;
  2765. ELSIF IsUnsafePointer(left.type) THEN
  2766. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2767. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2768. NewBool(TRUE)
  2769. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2770. NewBool(TRUE);
  2771. ELSIF IsUnextensibleRecord(left) THEN
  2772. NewBool(FALSE)
  2773. END
  2774. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2775. Error(right.position,"must not be a type");
  2776. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2777. Error(left.position,"must not be a type");
  2778. ELSIF operator = Scanner.In THEN (* left IN right *)
  2779. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2780. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2781. NewBool(il IN sr);
  2782. ELSE
  2783. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2784. left := NewConversion(left.position, left, system.longintType,NIL);
  2785. binaryExpression.SetLeft(left)
  2786. END;
  2787. type := system.booleanType;
  2788. END
  2789. ELSE
  2790. Error(binaryExpression.position, "incompatible operands");
  2791. END
  2792. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2793. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2794. Error(binaryExpression.position,"incompatible operands");
  2795. END;
  2796. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2797. ELSE Error(binaryExpression.position,"operator not defined 1")
  2798. END
  2799. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2800. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2801. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2802. Error(binaryExpression.position,"incompatible operands");
  2803. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2804. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2805. left := NewConversion(left.position, left, system.addressType, NIL);
  2806. right := NewConversion(right.position, right, system.addressType, NIL);
  2807. binaryExpression.SetLeft(left);
  2808. binaryExpression.SetRight(right);
  2809. type := system.addressType;
  2810. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2811. ConvertOperands(left, right);
  2812. binaryExpression.SetLeft(left);
  2813. binaryExpression.SetRight(right);
  2814. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2815. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2816. END;
  2817. type := system.booleanType;
  2818. ELSE
  2819. Error(binaryExpression.position,"operator not defined 3");
  2820. END
  2821. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2822. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2823. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2824. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2825. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2826. CASE operator OF
  2827. |Scanner.Equal: NewBool(strl^=strr^);
  2828. |Scanner.Unequal:NewBool(strl^#strr^);
  2829. |Scanner.Less: NewBool(strl^<strr^);
  2830. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2831. |Scanner.Greater: NewBool(strl^>strr^);
  2832. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2833. ELSE
  2834. Error(binaryExpression.position,"operator not defined 4");
  2835. END;
  2836. END;
  2837. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2838. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2839. type := system.booleanType
  2840. ELSE
  2841. Error(binaryExpression.position,"operator not defined 5");
  2842. END;
  2843. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2844. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2845. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2846. type := system.booleanType;
  2847. ELSE
  2848. Error(binaryExpression.position,"operator not defined 6");
  2849. END
  2850. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2851. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2852. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2853. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2854. type := system.booleanType
  2855. ELSE
  2856. Error(binaryExpression.position,"operator not defined for enumerators");
  2857. END;
  2858. ELSE
  2859. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2860. END;
  2861. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2862. type := system.booleanType;
  2863. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2864. type := system.booleanType;
  2865. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2866. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2867. THEN
  2868. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2869. IF (leftType # rightType) THEN
  2870. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2871. ConvertOperands(left,right); (* operands must be of the same type here *)
  2872. END;
  2873. binaryExpression.SetLeft(left);
  2874. binaryExpression.SetRight(right);
  2875. leftType := left.type.resolved;
  2876. rightType := right.type.resolved;
  2877. END;
  2878. type := leftType;
  2879. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2880. Error(binaryExpression.position,"conversion failed ?");
  2881. IF VerboseErrorMessage THEN
  2882. Printout.Info("left",left);
  2883. Printout.Info("right",right);
  2884. END;
  2885. ELSIF IsIntegerType(leftType) THEN
  2886. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2887. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2888. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2889. Error(binaryExpression.position,"division by zero");
  2890. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2891. Error(binaryExpression.position,"integer division by negative number");
  2892. END;
  2893. END;
  2894. (* constant folding *)
  2895. (* bootstrap64
  2896. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2897. CASE operator OF
  2898. |Scanner.Plus: NewInteger(il+ir,left.type);
  2899. |Scanner.Minus: NewInteger(il-ir,left.type);
  2900. |Scanner.Times: NewInteger(il*ir,left.type);
  2901. |Scanner.Slash:
  2902. IF ir # 0 THEN
  2903. NewReal(il/ir, system.realType);
  2904. END;
  2905. |Scanner.Mod:
  2906. IF ir > 0 THEN
  2907. NewInteger(il MOD ir,left.type);
  2908. END;
  2909. |Scanner.Div:
  2910. IF ir > 0 THEN
  2911. NewInteger(il DIV ir,left.type);
  2912. END;
  2913. |Scanner.Equal: NewBool(il=ir);
  2914. |Scanner.Unequal:NewBool(il#ir);
  2915. |Scanner.Less: NewBool(il<ir);
  2916. |Scanner.LessEqual: NewBool(il<=ir);
  2917. |Scanner.Greater: NewBool(il>ir);
  2918. |Scanner.GreaterEqual: NewBool(il>=ir);
  2919. ELSE Error(binaryExpression.position,"operator not defined 7");
  2920. END;
  2921. ELS*)
  2922. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2923. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2924. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2925. CASE operator OF
  2926. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2927. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2928. |Scanner.Times: NewInteger(hl*hr,left.type);
  2929. |Scanner.Slash:
  2930. IF hr = 0 THEN
  2931. Error(binaryExpression.position,"division by zero");
  2932. ELSE
  2933. IF type.sizeInBits = 64 THEN
  2934. NewReal(hl/hr,system.longrealType);
  2935. ELSE
  2936. NewReal(hl/hr,system.realType)
  2937. END
  2938. END;
  2939. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2940. |Scanner.Mod:
  2941. IF hr = 0 THEN
  2942. Error(binaryExpression.position,"division by zero");
  2943. ELSE
  2944. NewInteger(hl MOD hr, left.type);
  2945. (* bootstrap64
  2946. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2947. *)
  2948. END;
  2949. |Scanner.Div:
  2950. IF hr = 0 THEN
  2951. Error(binaryExpression.position,"division by zero");
  2952. ELSE
  2953. NewInteger(hl DIV hr, left.type);
  2954. (* bootstrap64
  2955. NewInteger(Machine.DivH(hl,hr),left.type);
  2956. *)
  2957. END;
  2958. (* *)
  2959. |Scanner.Equal: NewBool(hl=hr);
  2960. |Scanner.Unequal: NewBool(hl#hr);
  2961. |Scanner.Less: NewBool(hl<hr);
  2962. |Scanner.LessEqual: NewBool(hl<=hr);
  2963. |Scanner.Greater: NewBool(hl>hr);
  2964. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2965. ELSE Error(binaryExpression.position,"operator not defined 8");
  2966. END;
  2967. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2968. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2969. type := left.type
  2970. ELSIF (operator = Scanner.Slash) THEN
  2971. left := NewConversion(left.position,left,system.realType,NIL);
  2972. right := NewConversion(right.position,right,system.realType,NIL);
  2973. binaryExpression.SetLeft(left);
  2974. binaryExpression.SetRight(right);
  2975. type := system.realType
  2976. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2977. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2978. type := system.booleanType
  2979. ELSE
  2980. Error(binaryExpression.position,"operator not defined 9");
  2981. END;
  2982. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2983. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2984. CASE operator OF
  2985. |Scanner.Plus: NewReal(rl+rr,leftType);
  2986. |Scanner.Minus: NewReal(rl-rr,leftType);
  2987. |Scanner.Times:NewReal(rl*rr,leftType);
  2988. |Scanner.Slash:
  2989. IF rr = 0 THEN
  2990. Error(binaryExpression.position,"division by zero");
  2991. ELSE
  2992. NewReal(rl/rr,leftType);
  2993. END
  2994. |Scanner.Equal: NewBool(rl=rr);
  2995. |Scanner.Unequal: NewBool(rl#rr);
  2996. |Scanner.Less: NewBool(rl<rr);
  2997. |Scanner.LessEqual: NewBool(rl<=rr);
  2998. |Scanner.Greater: NewBool(rl>rr);
  2999. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3000. ELSE Error(binaryExpression.position,"operator not defined 10");
  3001. END;
  3002. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3003. type := left.type
  3004. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3005. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3006. type := system.booleanType
  3007. ELSE
  3008. Error(binaryExpression.position,"operator not defined 11");
  3009. IF VerboseErrorMessage THEN
  3010. Printout.Info("left",left);
  3011. Printout.Info("right",right);
  3012. END;
  3013. END;
  3014. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3015. CASE operator OF
  3016. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3017. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3018. ELSE
  3019. Error(binaryExpression.position,"operator not defined");
  3020. IF VerboseErrorMessage THEN
  3021. Printout.Info("left", left);
  3022. Printout.Info("right", right)
  3023. END;
  3024. END;
  3025. IF ~error THEN
  3026. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3027. Error(binaryExpression.position,"division by zero")
  3028. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3029. (* do constant folding *)
  3030. CASE operator OF
  3031. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3032. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3033. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3034. |Scanner.Slash:
  3035. divisor := c * c + d * d;
  3036. ASSERT(divisor # 0);
  3037. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3038. |Scanner.Equal: NewBool((a = c) & (b = d))
  3039. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3040. END
  3041. END
  3042. END
  3043. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3044. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3045. CASE operator OF
  3046. |Scanner.And: NewBool(bl & br);
  3047. |Scanner.Or: NewBool(bl OR br);
  3048. |Scanner.Equal: NewBool(bl = br);
  3049. |Scanner.Unequal: NewBool(bl # br);
  3050. ELSE Error(binaryExpression.position,"operator not defined 12");
  3051. END;
  3052. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3053. type := system.booleanType
  3054. ELSE
  3055. Error(binaryExpression.position,"operator not defined 13");
  3056. END;
  3057. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3058. (* constant folding *)
  3059. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3060. IF operator = Scanner.Equal THEN
  3061. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3062. ELSIF operator = Scanner.Unequal THEN
  3063. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3064. END;
  3065. END;
  3066. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3067. type := system.booleanType;
  3068. ELSE
  3069. Error(binaryExpression.position, "operator not defined");
  3070. END;
  3071. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3072. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3073. CASE operator OF
  3074. |Scanner.Plus: NewSet(sl + sr);
  3075. |Scanner.Minus: NewSet(sl - sr);
  3076. |Scanner.Times: NewSet(sl * sr);
  3077. |Scanner.Slash: NewSet(sl / sr);
  3078. |Scanner.Equal: NewBool(sl=sr);
  3079. |Scanner.Unequal: NewBool(sl#sr);
  3080. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3081. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3082. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3083. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3084. ELSE Error(binaryExpression.position,"operator not defined 14");
  3085. END;
  3086. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3087. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3088. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3089. THEN
  3090. type := system.booleanType
  3091. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3092. type := left.type
  3093. ELSE
  3094. Error(binaryExpression.position,"operator not defined 15");
  3095. END;
  3096. ELSIF IsCharacterType(left.type) THEN
  3097. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3098. CASE operator OF
  3099. |Scanner.Equal: NewBool(cl=cr);
  3100. |Scanner.Unequal: NewBool(cl#cr);
  3101. |Scanner.Less: NewBool(cl<cr);
  3102. |Scanner.LessEqual: NewBool(cl<=cr);
  3103. |Scanner.Greater: NewBool(cl>cr);
  3104. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3105. ELSE Error(binaryExpression.position,"operator not defined 16");
  3106. END;
  3107. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3108. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3109. type := system.booleanType
  3110. ELSE
  3111. Error(binaryExpression.position,"operator not defined 17");
  3112. END;
  3113. ELSE
  3114. Error(binaryExpression.position,"operator not defined 18");
  3115. END;
  3116. ELSE
  3117. Error(binaryExpression.position,"operator not defined 19");
  3118. END;
  3119. IF type = SyntaxTree.invalidType THEN
  3120. result := SyntaxTree.invalidExpression
  3121. ELSE
  3122. result.SetType(type)
  3123. END;
  3124. resolvedExpression := result
  3125. END VisitBinaryExpression;
  3126. (** resolve a range expression of the from <<first .. last BY step>>
  3127. - depending on the context different things are checked:
  3128. ArrayIndex:
  3129. - components must be integers
  3130. - replace missing lower bound with 0
  3131. - replace missing upper bound with MAX(LONGINT)
  3132. - replace missing step size with 1
  3133. SetElement:
  3134. - components must be integers
  3135. - replace missing lower bound with 0
  3136. - replace missing upper bound with MAX(SET)
  3137. - must not have step size
  3138. CaseGuard:
  3139. - components must be constant
  3140. - components must be integers or characters
  3141. - must have lower and upper bound present
  3142. - components are made compatible
  3143. - must not have step size
  3144. - if error: return invalidExpression
  3145. **)
  3146. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3147. VAR
  3148. hasError: BOOLEAN;
  3149. first, last, step: SyntaxTree.Expression;
  3150. BEGIN
  3151. hasError := FALSE;
  3152. first := x.first;
  3153. last := x.last;
  3154. step := x.step;
  3155. (* check lower bound *)
  3156. IF x.context = SyntaxTree.CaseGuard THEN
  3157. IF first = NIL THEN
  3158. Error(x.position, "missing lower bound");
  3159. hasError := TRUE
  3160. ELSE
  3161. first := ResolveExpression(first);
  3162. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3163. Error(first.position, "lower bound not integer or character");
  3164. hasError := TRUE
  3165. ELSE
  3166. IF first IS SyntaxTree.StringValue THEN
  3167. (* add conversion from string to character *)
  3168. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3169. END
  3170. END;
  3171. (* check if expression is constant *)
  3172. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3173. (* error already reported *)
  3174. hasError := TRUE
  3175. END
  3176. END
  3177. ELSE (* ArrayIndex, SetElement *)
  3178. IF first = NIL THEN
  3179. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3180. END;
  3181. first := ResolveExpression(first);
  3182. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3183. first := NewConversion(first.position, first, system.longintType, NIL)
  3184. ELSE
  3185. Error(first.position, "lower bound not integer");
  3186. hasError := TRUE
  3187. END
  3188. END;
  3189. (* check upper bound *)
  3190. IF x.context = SyntaxTree.CaseGuard THEN
  3191. IF last = NIL THEN
  3192. Error(x.position, "missing upper bound");
  3193. hasError := TRUE
  3194. ELSE
  3195. last := ResolveExpression(last);
  3196. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3197. Error(last.position, "lower bound not integer or character");
  3198. hasError := TRUE
  3199. ELSE
  3200. IF last IS SyntaxTree.StringValue THEN
  3201. (* add conversion from string to character *)
  3202. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3203. END
  3204. END;
  3205. (* check if expression is constant *)
  3206. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3207. (* error already reported *)
  3208. hasError := TRUE
  3209. ELSE
  3210. (* try to make lower and upper bound compatible *)
  3211. ConvertOperands(first, last);
  3212. IF first.type.resolved # last.type.resolved THEN
  3213. Error(x.position, "lower and upper bounds incompatible");
  3214. hasError := TRUE
  3215. END
  3216. END
  3217. END
  3218. ELSE (* ArrayIndex, SetElement *)
  3219. IF last = NIL THEN
  3220. IF x.context = SyntaxTree.ArrayIndex THEN
  3221. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3222. ELSE
  3223. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3224. END
  3225. END;
  3226. last := ResolveExpression(last);
  3227. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3228. last := NewConversion(last.position, last, system.longintType, NIL)
  3229. ELSE
  3230. Error(last.position, "upper bound not integer");
  3231. hasError := TRUE
  3232. END
  3233. END;
  3234. (* check step size *)
  3235. IF x.context = SyntaxTree.ArrayIndex THEN
  3236. IF step = NIL THEN
  3237. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3238. END;
  3239. step := ResolveExpression(step);
  3240. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3241. step := NewConversion(step.position, step, system.longintType, NIL)
  3242. ELSE
  3243. Error(step.position, "step size not integer");
  3244. hasError := TRUE
  3245. END
  3246. ELSE (* SetElement, CaseGuard *)
  3247. IF step # NIL THEN
  3248. Error(last.position, "step size not allowed in this context");
  3249. hasError := TRUE
  3250. END
  3251. END;
  3252. IF hasError THEN
  3253. resolvedExpression := SyntaxTree.invalidExpression
  3254. ELSE
  3255. x.SetFirst(first);
  3256. x.SetLast(last);
  3257. x.SetStep(step);
  3258. x.SetType(system.rangeType);
  3259. resolvedExpression := x;
  3260. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3261. END
  3262. END VisitRangeExpression;
  3263. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3264. BEGIN
  3265. x.SetType(NIL);
  3266. resolvedExpression := x;
  3267. END VisitTensorRangeExpression;
  3268. (** resolve the expression d and return result as designator
  3269. - resolve expression
  3270. - if expression is a designator then return designator else error message and return invalidDesignator
  3271. **)
  3272. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3273. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3274. BEGIN
  3275. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3276. resolved := ResolveExpression(d);
  3277. IF resolved = SyntaxTree.invalidExpression THEN
  3278. (* error should already have been reported *)
  3279. result := SyntaxTree.invalidDesignator;
  3280. ELSIF resolved IS SyntaxTree.Designator THEN
  3281. result := resolved(SyntaxTree.Designator);
  3282. ELSE
  3283. Error(d.position,"is no designator ! ");
  3284. result := SyntaxTree.invalidDesignator;
  3285. END;
  3286. (* result.type might be nil. *)
  3287. RETURN result
  3288. END ResolveDesignator;
  3289. (**
  3290. symbol designator generated in this module
  3291. nothing to be resolved
  3292. **)
  3293. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3294. BEGIN
  3295. resolvedExpression := x;
  3296. END VisitSymbolDesignator;
  3297. (**
  3298. self designator generated in this module
  3299. nothing to be resolved
  3300. **)
  3301. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3302. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3303. BEGIN
  3304. (* check if in record scope *)
  3305. scope := currentScope;
  3306. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3307. scope := scope.outerScope;
  3308. END;
  3309. IF scope = NIL THEN (* in module scope *)
  3310. x.SetType(system.anyType);
  3311. ELSIF scope IS SyntaxTree.CellScope THEN
  3312. cell := scope(SyntaxTree.CellScope).ownerCell;
  3313. x.SetType(cell);
  3314. ELSE (* in record scope *)
  3315. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3316. IF (record # NIL) & (record.pointerType # NIL) THEN
  3317. type := ResolveType(record.pointerType);
  3318. x.SetType(type);
  3319. ELSE
  3320. x.SetType(record);
  3321. END;
  3322. END;
  3323. resolvedExpression := x;
  3324. END VisitSelfDesignator;
  3325. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3326. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3327. BEGIN
  3328. scope := currentScope;
  3329. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3330. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3331. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3332. returnType := procedureType.returnType;
  3333. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3334. THEN
  3335. x.SetType(returnType);
  3336. ELSE
  3337. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3338. x.SetType(SyntaxTree.invalidType);
  3339. END;
  3340. ELSE
  3341. Error(x.position,"forbidden access to result designator");
  3342. x.SetType(SyntaxTree.invalidType);
  3343. END;
  3344. x.SetAssignable(TRUE);
  3345. resolvedExpression := x;
  3346. END VisitResultDesignator;
  3347. (**
  3348. return symbol designator as an expression
  3349. - if symbol is a constant then return the constant value expression
  3350. - else
  3351. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3352. - if symbol is a guarded variable then return a TypeGuardDesignator
  3353. - else return a symbol designator
  3354. **)
  3355. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3356. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3357. guardType: SyntaxTree.Type;
  3358. BEGIN
  3359. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3360. result := SyntaxTree.invalidExpression;
  3361. ASSERT(symbol # NIL);
  3362. (*
  3363. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3364. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3365. Error(position, "type not allowed here");
  3366. ELS *)
  3367. (* not needed any more as values are stored in the expression
  3368. IF symbol IS SyntaxTree.Constant THEN
  3369. result := symbol(SyntaxTree.Constant).value
  3370. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3371. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3372. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3373. ELSE
  3374. result := symbol(SyntaxTree.Constant).value
  3375. END;
  3376. ELSE
  3377. *)
  3378. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3379. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3380. THEN
  3381. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3382. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3383. left := NewDereferenceDesignator(position,left);
  3384. left.SetHidden(TRUE);
  3385. END;
  3386. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3387. scope := currentScope;
  3388. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3389. scope := scope.outerScope;
  3390. END;
  3391. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3392. Error(position, "forbidden access to symbol in parent procedure scope");
  3393. END;
  3394. END;
  3395. 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);
  3396. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3397. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3398. ELSE
  3399. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3400. END;
  3401. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3402. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3403. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3404. result.SetType(symbol.type);
  3405. result.SetAssignable(assignable);
  3406. symbol.MarkUsed;
  3407. IF symbol IS SyntaxTree.Constant THEN
  3408. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3409. END;
  3410. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3411. variableAccessed := TRUE
  3412. END;
  3413. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3414. IF GetGuard(symbol,guardType) THEN
  3415. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3416. END;
  3417. END;
  3418. ASSERT(result.type # NIL);
  3419. RETURN result
  3420. END NewSymbolDesignator;
  3421. (** check and resolve an identifier designator "identifier"
  3422. - if identifier = self then return SelfDesignator
  3423. - else find symbol in current scope
  3424. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3425. **)
  3426. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3427. VAR symbol: SyntaxTree.Symbol;
  3428. BEGIN
  3429. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3430. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3431. IF symbol # NIL THEN
  3432. ResolveSymbol(symbol);
  3433. ASSERT(symbol.type # NIL);
  3434. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3435. ELSE
  3436. Error(identifierDesignator.position,"Undeclared Identifier");
  3437. IF VerboseErrorMessage THEN
  3438. Printout.Info("undeclared identifier designator",identifierDesignator);
  3439. END;
  3440. resolvedExpression := SyntaxTree.invalidDesignator;
  3441. END;
  3442. END VisitIdentifierDesignator;
  3443. (** check and resolve a selector designator of the form left.designator
  3444. - if left is a pointer type then do auto dereferenciation
  3445. - left denotes a search scope:
  3446. - if left type is import type then set search scope to respective module
  3447. - if left type is enumeration type then set search scope to respective enumeration scope
  3448. - elsif left type is record type then set search scope to record scope
  3449. - search symbol in computed scope
  3450. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3451. **)
  3452. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3453. VAR
  3454. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3455. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3456. BEGIN
  3457. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3458. left := ResolveDesignator(selectorDesignator.left);
  3459. result := SyntaxTree.invalidDesignator;
  3460. IF left # NIL THEN
  3461. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3462. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3463. END;
  3464. scope := NIL;
  3465. IF left.type = NIL THEN
  3466. Error(selectorDesignator.position,"field on nil typed designator");
  3467. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3468. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3469. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3470. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3471. module := symbol(SyntaxTree.Import).module;
  3472. IF module # NIL THEN
  3473. scope := module.moduleScope
  3474. ELSE
  3475. Error(left.position,"module not loaded");
  3476. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3477. END;
  3478. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3479. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3480. ASSERT(scope # NIL)
  3481. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3482. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3483. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3484. IF type IS SyntaxTree.EnumerationType THEN
  3485. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3486. ELSE
  3487. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3488. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3489. END;
  3490. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3491. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3492. ELSE
  3493. Error(selectorDesignator.position,"field on non-record type designator");
  3494. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3495. END;
  3496. symbol := NIL;
  3497. IF scope # NIL THEN
  3498. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3499. IF symbol # NIL THEN
  3500. ResolveSymbol(symbol);
  3501. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3502. symbol.MarkUsed
  3503. ELSE
  3504. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3505. IF VerboseErrorMessage THEN
  3506. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3507. Printout.Info("scope", scope);
  3508. Printout.Info("left", left);
  3509. Printout.Info("undeclared identifier",selectorDesignator);
  3510. Printout.Info("left resolved designator",left);
  3511. END
  3512. END;
  3513. END;
  3514. END;
  3515. resolvedExpression := result;
  3516. END VisitSelectorDesignator;
  3517. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3518. VAR len,idx: LONGINT;
  3519. BEGIN
  3520. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3521. IF idx < 0 THEN
  3522. Error(index.position,"index out of bounds (too small)")
  3523. ELSE
  3524. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3525. Error(index.position,"index out of bounds (too large)");
  3526. END;
  3527. END;
  3528. END;
  3529. END IndexCheck;
  3530. (*
  3531. - if index designator has not type, use newBaseType as its type
  3532. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3533. - special rule: if static array of dynamic array occurs, make it all dynamic
  3534. index designator type: new base type: new index designator type:
  3535. NIL z z
  3536. ARRAY [x, y] z ARRAY [x, y] OF z
  3537. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3538. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3539. *)
  3540. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3541. VAR
  3542. mathArrayType: SyntaxTree.MathArrayType;
  3543. makeDynamic: BOOLEAN;
  3544. BEGIN
  3545. IF indexDesignator.type = NIL THEN
  3546. indexDesignator.SetType(newBaseType)
  3547. ELSE
  3548. (* index designator must be a of math array type *)
  3549. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3550. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3551. (* determine if all arrays have to be made dynamic *)
  3552. makeDynamic :=
  3553. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3554. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3555. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3556. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3557. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3558. END;
  3559. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3560. mathArrayType.SetArrayBase(newBaseType)
  3561. END
  3562. END SetIndexBaseType;
  3563. (** check and append index list element to index designator of math array
  3564. - check validity of single index or array range
  3565. - compute new type
  3566. - if range then create new array type (calculate length of resulting array)
  3567. - otherwise take sourceArray.arrayBase as new type
  3568. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3569. **)
  3570. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3571. VAR
  3572. targetArray: SyntaxTree.MathArrayType;
  3573. first, last, step: SyntaxTree.Expression;
  3574. firstValue, lastValue, stepValue, length: LONGINT;
  3575. rangeExpression: SyntaxTree.RangeExpression;
  3576. BEGIN
  3577. IF indexListItem.type = SyntaxTree.invalidType THEN
  3578. (* error already handled *)
  3579. indexDesignator.parameters.AddExpression(indexListItem)
  3580. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3581. indexDesignator.HasRange;
  3582. indexDesignator.HasTensorRange;
  3583. indexDesignator.parameters.AddExpression(indexListItem);
  3584. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3585. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3586. IndexCheck(indexListItem, sourceArray.length);
  3587. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3588. indexDesignator.parameters.AddExpression(indexListItem)
  3589. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3590. indexDesignator.HasRange;
  3591. (* if the range is given as an array range expression, check the validity of its components *)
  3592. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3593. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3594. first := rangeExpression.first;
  3595. last := rangeExpression.last;
  3596. step := rangeExpression.step;
  3597. (* perform static checks on range components *)
  3598. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3599. Error(indexListItem.position,"lower bound of array range too small")
  3600. END;
  3601. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3602. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3603. Error(indexListItem.position,"upper bound of array range too large")
  3604. END
  3605. END;
  3606. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3607. Error(indexListItem.position,"invalid step size")
  3608. END;
  3609. (* add conversions to size type *)
  3610. (* TODO: needed? *)
  3611. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3612. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3613. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3614. END;
  3615. IF indexDesignator.hasTensorRange THEN
  3616. (* the index designator's base type is a tensor: leave it as is *)
  3617. ELSE
  3618. (* append a new math array to the index designator's base type *)
  3619. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3620. IF ~error THEN
  3621. (*
  3622. (* optimization: calculate length of target array for static ranges *)
  3623. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3624. IF IsStaticallyOpenRange(rangeExpression) THEN
  3625. (* range is open ('*'): reuse source array length as target array length *)
  3626. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3627. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3628. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3629. IF lastValue = MAX(LONGINT) THEN
  3630. IF IsIntegerValue(sourceArray.length, length) THEN
  3631. lastValue := length - 1;
  3632. isStaticTargetArrayLength := TRUE
  3633. ELSE
  3634. isStaticTargetArrayLength := FALSE
  3635. END
  3636. ELSE
  3637. isStaticTargetArrayLength := TRUE
  3638. END;
  3639. IF isStaticTargetArrayLength THEN
  3640. (* calculate static target array length *)
  3641. IF firstValue > lastValue THEN
  3642. length := 0
  3643. ELSE
  3644. length := 1 + lastValue - firstValue;
  3645. IF length MOD stepValue = 0 THEN
  3646. length := length DIV stepValue
  3647. ELSE
  3648. length := length DIV stepValue + 1
  3649. END
  3650. END;
  3651. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3652. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3653. ASSERT(targetArray.form = SyntaxTree.Static)
  3654. END
  3655. END
  3656. END
  3657. *)
  3658. END;
  3659. SetIndexBaseType(indexDesignator, targetArray)
  3660. END;
  3661. indexDesignator.parameters.AddExpression(indexListItem)
  3662. ELSE
  3663. Error(position,"invalid index list item");
  3664. END;
  3665. END AppendMathIndex;
  3666. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3667. VAR parameters: SyntaxTree.ExpressionList;
  3668. BEGIN
  3669. parameters := index.parameters;
  3670. IF (expression.type = NIL) THEN
  3671. Error(position, "invalid index");
  3672. ELSIF IsIntegerType(expression.type.resolved) THEN
  3673. IF over IS SyntaxTree.ArrayType THEN
  3674. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3675. ELSIF over IS SyntaxTree.StringType THEN
  3676. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3677. END;
  3678. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3679. parameters.AddExpression(expression);
  3680. ELSE
  3681. Error(position, "invalid index");
  3682. END;
  3683. END AppendIndex;
  3684. (** convert an expression to math array type
  3685. if expression is of math array type: return expression itself
  3686. if expression is of array-structured object type: return an index operator call on it
  3687. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3688. otherwise: return invalid expression
  3689. **)
  3690. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3691. VAR
  3692. result: SyntaxTree.Expression;
  3693. mathArrayType: SyntaxTree.MathArrayType;
  3694. BEGIN
  3695. IF expression.type = NIL THEN
  3696. result := SyntaxTree.invalidExpression
  3697. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3698. (* expression of math array type *)
  3699. result := expression
  3700. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3701. (* expression of array-structured object type *)
  3702. mathArrayType := MathArrayStructureOfType(expression.type);
  3703. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3704. ELSE
  3705. result := SyntaxTree.invalidExpression
  3706. END;
  3707. RETURN result
  3708. END ConvertToMathArray;
  3709. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3710. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3711. VAR
  3712. result: SyntaxTree.ExpressionList;
  3713. i: LONGINT;
  3714. BEGIN
  3715. result := SyntaxTree.NewExpressionList();
  3716. FOR i := 1 TO itemCount DO
  3717. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3718. END;
  3719. RETURN result
  3720. END ListOfOpenRanges;
  3721. (** create a procedure call designator for an index operator call on an array-structured object type
  3722. - use given index list as actual parameters
  3723. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3724. **)
  3725. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3726. VAR
  3727. operator: SyntaxTree.Operator;
  3728. expression: SyntaxTree.Expression;
  3729. actualParameters, tempList: SyntaxTree.ExpressionList;
  3730. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3731. result, tempDesignator: SyntaxTree.Designator;
  3732. recordType: SyntaxTree.RecordType;
  3733. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3734. i, hashValue, indexListSize, indexListKind: LONGINT;
  3735. castReturnType: SyntaxTree.MathArrayType;
  3736. BEGIN
  3737. ASSERT(IsArrayStructuredObjectType(left.type));
  3738. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3739. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3740. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3741. indexListSize := indexList.Length();
  3742. indexListKind := 0;
  3743. containsNonRange := FALSE;
  3744. FOR i := 0 TO indexList.Length() - 1 DO
  3745. indexListKind := indexListKind * 2;
  3746. expression := indexList.GetExpression(i);
  3747. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3748. INC(indexListKind)
  3749. ELSE
  3750. containsNonRange := TRUE
  3751. END
  3752. END;
  3753. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3754. (* select applicable index operator
  3755. - try to look up optimal index operator
  3756. - if not present, use operator on ranges
  3757. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3758. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3759. *)
  3760. usesGeneralOperator := FALSE;
  3761. IF rhs # NIL THEN
  3762. (* write operator *)
  3763. IF hashValue = -1 THEN
  3764. operator := NIL
  3765. ELSE
  3766. operator := recordType.arrayAccessOperators.write[hashValue];
  3767. END;
  3768. IF operator = NIL THEN
  3769. usesPureRangeOperator := TRUE;
  3770. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3771. operator := recordType.arrayAccessOperators.generalWrite;
  3772. usesGeneralOperator := TRUE
  3773. ELSE
  3774. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3775. operator := recordType.arrayAccessOperators.write[hashValue];
  3776. END
  3777. END
  3778. ELSE
  3779. (* read operator *)
  3780. IF hashValue = -1 THEN
  3781. operator := NIL
  3782. ELSE
  3783. operator := recordType.arrayAccessOperators.read[hashValue];
  3784. END;
  3785. IF operator = NIL THEN
  3786. usesPureRangeOperator := TRUE;
  3787. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3788. operator := recordType.arrayAccessOperators.generalRead;
  3789. usesGeneralOperator := TRUE
  3790. ELSE
  3791. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3792. operator := recordType.arrayAccessOperators.read[hashValue];
  3793. END
  3794. END
  3795. END;
  3796. IF operator = NIL THEN
  3797. Error(position, "call of undeclared [] operator");
  3798. result := SyntaxTree.invalidDesignator;
  3799. ELSE
  3800. (* determine if reshaping is needed *)
  3801. needsReshaping := containsNonRange & usesPureRangeOperator;
  3802. (* import OCArrayBase if reshaping is needed *)
  3803. IF needsReshaping & ~arrayBaseImported THEN
  3804. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3805. arrayBaseImported := TRUE
  3806. END;
  3807. (* add the index list item to the list of actual parameters
  3808. - for general operators: add a single inline array containing the index list items as parameter
  3809. - otherwise: add all index list items as individual parameters
  3810. *)
  3811. actualParameters := SyntaxTree.NewExpressionList();
  3812. IF usesGeneralOperator THEN
  3813. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3814. END;
  3815. FOR i := 0 TO indexListSize - 1 DO
  3816. expression := indexList.GetExpression(i);
  3817. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3818. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3819. tempList := SyntaxTree.NewExpressionList();
  3820. tempList.AddExpression(expression);
  3821. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3822. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3823. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3824. END;
  3825. IF usesGeneralOperator THEN
  3826. tempMathArrayExpression.elements.AddExpression(expression);
  3827. ELSE
  3828. actualParameters.AddExpression(expression)
  3829. END
  3830. END;
  3831. IF usesGeneralOperator THEN
  3832. actualParameters.AddExpression(tempMathArrayExpression)
  3833. END;
  3834. IF rhs # NIL THEN
  3835. (* add actual parameter for RHS *)
  3836. IF needsReshaping THEN
  3837. (* reshape using OCArrayBase.ExpandDimensions *)
  3838. tempList := SyntaxTree.NewExpressionList();
  3839. (* source array *)
  3840. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3841. tempList.AddExpression(rhs);
  3842. ELSE
  3843. (* convert scalar to one-dimensional array *)
  3844. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3845. tempMathArrayExpression.elements.AddExpression(rhs);
  3846. tempList.AddExpression(tempMathArrayExpression)
  3847. END;
  3848. (* list of kept dimensions *)
  3849. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3850. FOR i := 0 TO indexListSize - 1 DO
  3851. expression := indexList.GetExpression(i);
  3852. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3853. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3854. ELSE
  3855. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3856. END
  3857. END;
  3858. tempList.AddExpression(tempMathArrayExpression);
  3859. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3860. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3861. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3862. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3863. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3864. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3865. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3866. expression.SetType(castReturnType);
  3867. ELSE
  3868. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3869. END;
  3870. actualParameters.AddExpression(expression)
  3871. ELSE
  3872. actualParameters.AddExpression(rhs)
  3873. END
  3874. END;
  3875. (* add dereference operator and create procedure call designator *)
  3876. ASSERT(left IS SyntaxTree.Designator);
  3877. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3878. ASSERT(expression IS SyntaxTree.Designator);
  3879. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3880. IF (rhs = NIL) & needsReshaping THEN
  3881. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3882. tempList := SyntaxTree.NewExpressionList();
  3883. FOR i := 0 TO indexList.Length() - 1 DO
  3884. expression := indexList.GetExpression(i);
  3885. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3886. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3887. ELSE
  3888. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3889. END
  3890. END;
  3891. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3892. END;
  3893. IF rhs = NIL THEN
  3894. (* special rule: index read operator calls are considered to be assignable *)
  3895. result.SetAssignable(TRUE)
  3896. END;
  3897. (* put information about this index operator call into the resulting designator *)
  3898. result.SetRelatedAsot(left);
  3899. result.SetRelatedIndexList(indexList)
  3900. END;
  3901. RETURN result
  3902. END NewIndexOperatorCall;
  3903. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3904. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3905. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3906. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3907. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3908. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3909. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3910. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3911. CONST trace = FALSE;
  3912. BEGIN
  3913. IF trace THEN
  3914. FOR i := 0 TO actualParameters.Length()-1 DO
  3915. Printout.Info("par", actualParameters.GetExpression(i));
  3916. END;
  3917. END;
  3918. operator := scope.firstOperator;
  3919. WHILE(operator # NIL) DO
  3920. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3921. procedureType := operator.type(SyntaxTree.ProcedureType);
  3922. distance := Distance(system, procedureType,actualParameters);
  3923. IF trace THEN Printout.Info("check op ",operator) END;
  3924. IF distance < bestDistance THEN
  3925. IF trace THEN Printout.Info("taken op",operator) END;
  3926. bestDistance := distance;
  3927. bestOperator := operator;
  3928. END;
  3929. END;
  3930. operator := operator.nextOperator;
  3931. END;
  3932. END FindInScope;
  3933. BEGIN
  3934. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3935. IF oper = 0 THEN (* index *)
  3936. identifier := SyntaxTree.NewIdentifier("[]");
  3937. ELSE
  3938. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3939. END;
  3940. WHILE (recordType # NIL) DO
  3941. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3942. recordType := recordType.GetBaseRecord();
  3943. END;
  3944. RETURN bestOperator
  3945. END FindOperator;
  3946. BEGIN
  3947. type := left.type.resolved;
  3948. IF type IS SyntaxTree.RecordType THEN
  3949. pointer := FALSE;
  3950. recordType := type(SyntaxTree.RecordType);
  3951. ELSE
  3952. pointer := TRUE;
  3953. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3954. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3955. END;
  3956. actualParameters := SyntaxTree.NewExpressionList();
  3957. IF parameters # NIL THEN
  3958. FOR i := 0 TO parameters.Length()-1 DO
  3959. expression := ResolveExpression(parameters.GetExpression(i));
  3960. actualParameters.AddExpression(expression);
  3961. END;
  3962. END;
  3963. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3964. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3965. IF op # NIL THEN
  3966. designator := left(SyntaxTree.Designator);
  3967. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3968. expression := NewSymbolDesignator(position, designator , op);
  3969. ASSERT(expression IS SyntaxTree.Designator);
  3970. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3971. result.SetRelatedAsot(left);
  3972. result.SetRelatedIndexList(parameters);
  3973. (* check if write operator exists, for var parameters *)
  3974. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3975. actualParameters := SyntaxTree.NewExpressionList();
  3976. FOR i := 0 TO parameters.Length()-1 DO
  3977. expression := ResolveExpression(parameters.GetExpression(i));
  3978. actualParameters.AddExpression(expression);
  3979. END;
  3980. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3981. actualParameters.AddExpression(rhs);
  3982. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3983. IF op = NIL THEN rhs := NIL END;
  3984. END;
  3985. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3986. ELSE
  3987. result := NIL;
  3988. END;
  3989. RETURN result;
  3990. END NewObjectOperatorCall;
  3991. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3992. 1. convert bracket designator chains into a single one that contains separators
  3993. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3994. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3995. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3996. - if an array or math array is indexed over, create index designator
  3997. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3998. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3999. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4000. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4001. - if an array-structured object type is indexed over, create procedure call designator
  4002. e.g.: a[x, y] -> a^."[]"(x, y)
  4003. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4004. - a[i, *] = a[i][*]
  4005. - a[*, i] # a[*][i]
  4006. Because:
  4007. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4008. - 'i-th column' = a[*, i]
  4009. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4010. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4011. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4012. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4013. **)
  4014. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  4015. VAR
  4016. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4017. indexDesignator: SyntaxTree.IndexDesignator;
  4018. designator: SyntaxTree.Designator;
  4019. type: SyntaxTree.Type;
  4020. recordType: SyntaxTree.RecordType;
  4021. expression, rhs: SyntaxTree.Expression;
  4022. indexList: SyntaxTree.ExpressionList;
  4023. i: LONGINT;
  4024. hasError, done: BOOLEAN;
  4025. PROCEDURE FinalizeIndexDesignator;
  4026. BEGIN
  4027. IF indexDesignator # NIL THEN
  4028. (* the end of a tensor has been reached: *)
  4029. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4030. SetIndexBaseType(indexDesignator, type);
  4031. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4032. designator := indexDesignator;
  4033. type := designator.type.resolved;
  4034. indexDesignator := NIL;
  4035. ASSERT(SyntaxTree.Resolved IN type.state)
  4036. END
  4037. END FinalizeIndexDesignator;
  4038. BEGIN
  4039. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4040. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4041. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4042. (* copy all index list entries including a separator to the left bracket designator *)
  4043. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4044. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4045. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4046. END;
  4047. (* propagate the related RHS *)
  4048. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4049. (* only resolve left bracket designator and use as final result *)
  4050. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4051. ELSE
  4052. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4053. designator := ResolveDesignator(bracketDesignator.left);
  4054. type := designator.type.resolved;
  4055. indexDesignator := NIL;
  4056. (*!!! clean up *)
  4057. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4058. OR (type IS SyntaxTree.RecordType)
  4059. THEN
  4060. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4061. IF resolvedExpression = NIL THEN
  4062. Error(bracketDesignator.position,"undefined operator");
  4063. resolvedExpression := SyntaxTree.invalidDesignator
  4064. END;
  4065. RETURN;
  4066. END;
  4067. i := 0;
  4068. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4069. expression := bracketDesignator.parameters.GetExpression(i);
  4070. expression := ResolveExpression(expression);
  4071. bracketDesignator.parameters.SetExpression(i, expression);
  4072. IF expression = SyntaxTree.indexListSeparator THEN
  4073. (* finalize an existing index designator if needed *)
  4074. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4075. INC(i)
  4076. ELSE
  4077. (* do auto-dereferencing if needed *)
  4078. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4079. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4080. & (i=0)*)
  4081. THEN
  4082. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4083. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4084. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4085. designator := SyntaxTree.invalidDesignator;
  4086. type := SyntaxTree.invalidType
  4087. ELSE
  4088. FinalizeIndexDesignator;
  4089. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4090. type := designator.type.resolved
  4091. END
  4092. END;
  4093. (* create a new index designator, if needed *)
  4094. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4095. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4096. indexDesignator.SetAssignable(designator.assignable);
  4097. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4098. (* designator := indexDesignator *)
  4099. END;
  4100. IF type = SyntaxTree.invalidType THEN
  4101. (* error already handled *)
  4102. INC(i)
  4103. ELSIF type IS SyntaxTree.ArrayType THEN
  4104. (* indexing over an array *)
  4105. ASSERT(indexDesignator # NIL);
  4106. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4107. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4108. INC(i)
  4109. ELSIF type IS SyntaxTree.StringType THEN
  4110. (* indexing over an array *)
  4111. ASSERT(indexDesignator # NIL);
  4112. AppendIndex(expression.position, indexDesignator, expression, type);
  4113. type := type(SyntaxTree.StringType).baseType.resolved;
  4114. INC(i)
  4115. ELSIF type IS SyntaxTree.MathArrayType THEN
  4116. (* indexing over a math array *)
  4117. ASSERT(indexDesignator # NIL);
  4118. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4119. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4120. INC(i)
  4121. ELSIF IsArrayStructuredObjectType(type) THEN
  4122. (* indexing over ASOTs *)
  4123. FinalizeIndexDesignator;
  4124. ASSERT(type IS SyntaxTree.PointerType);
  4125. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4126. (*
  4127. - collect index list items from bracket designator that belong to ASOT
  4128. - check for errors
  4129. *)
  4130. indexList := SyntaxTree.NewExpressionList();
  4131. hasError := FALSE;
  4132. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4133. (* indexing over tensor ASOT:
  4134. - stop at index list end or separator
  4135. - dimensionality is given by number of index list items
  4136. *)
  4137. done := FALSE;
  4138. WHILE ~done DO
  4139. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4140. done := TRUE;
  4141. ELSE
  4142. expression := bracketDesignator.parameters.GetExpression(i);
  4143. IF expression = SyntaxTree.indexListSeparator THEN
  4144. done := TRUE;
  4145. ELSE
  4146. expression := ResolveExpression(expression);
  4147. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4148. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4149. hasError := TRUE
  4150. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4151. Error(expression.position, "integer or range expected");
  4152. expression := SyntaxTree.invalidExpression;
  4153. hasError := TRUE
  4154. END;
  4155. indexList.AddExpression(expression)
  4156. END;
  4157. INC(i)
  4158. END
  4159. END
  4160. ELSE
  4161. (* indexing over non-tensor ASOT:
  4162. - ignore separators
  4163. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4164. *)
  4165. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4166. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4167. expression := bracketDesignator.parameters.GetExpression(i);
  4168. ELSE
  4169. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4170. END;
  4171. IF expression # SyntaxTree.indexListSeparator THEN
  4172. expression := ResolveExpression(expression);
  4173. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4174. Error(expression.position, "integer or range expected");
  4175. expression := SyntaxTree.invalidExpression;
  4176. hasError := TRUE
  4177. END;
  4178. indexList.AddExpression(expression)
  4179. END;
  4180. INC(i)
  4181. END;
  4182. END;
  4183. IF hasError THEN
  4184. designator := SyntaxTree.invalidDesignator;
  4185. type := SyntaxTree.invalidType;
  4186. ELSE
  4187. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4188. and the last entry in the index list belongs to the array-structured object type in question.
  4189. E.g.: for a 2-dimensional array-structured object type:
  4190. - 'lhs := asot[1, 2]' -> read mode
  4191. - 'asot[1, 2] := rhs' -> write mode
  4192. - 'asot[1, 2, 3] := rhs' -> read mode
  4193. *)
  4194. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4195. rhs := bracketDesignator.relatedRhs
  4196. ELSE
  4197. rhs := NIL
  4198. END;
  4199. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4200. type := designator.type
  4201. END
  4202. ELSE
  4203. Error(expression.position,"indexing over non-array type");
  4204. designator := SyntaxTree.invalidDesignator;
  4205. type := SyntaxTree.invalidType;
  4206. INC(i)
  4207. END
  4208. END
  4209. END;
  4210. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4211. resolvedExpression := designator
  4212. END
  4213. END VisitBracketDesignator;
  4214. (** check and resolve expression list
  4215. - resolve each expression in an expression list
  4216. - returns true if and only if all statements could have successfully been resolved
  4217. **)
  4218. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4219. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4220. BEGIN
  4221. result := TRUE;
  4222. FOR i := 0 TO expressionList.Length()-1 DO
  4223. expression := ResolveExpression(expressionList.GetExpression(i));
  4224. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4225. expressionList.SetExpression(i,expression);
  4226. END;
  4227. RETURN result
  4228. END ExpressionList;
  4229. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4230. BEGIN
  4231. type := type.resolved;
  4232. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4233. RETURN TRUE
  4234. ELSIF system.CanPassInRegister # NIL THEN
  4235. RETURN system.CanPassInRegister(type);
  4236. ELSE
  4237. RETURN FALSE
  4238. END;
  4239. END CanPassInRegister;
  4240. (** return procedure call designator left(actualParameters)
  4241. - check realtime procedure call in realtime procedure
  4242. - check number of parameters
  4243. - check parameter compatibility
  4244. return invalidDesignator if error
  4245. **)
  4246. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4247. VAR result: SyntaxTree.Designator;
  4248. numberFormalParameters, numberActualParameters: LONGINT;
  4249. formalType: SyntaxTree.ProcedureType;
  4250. formalParameter: SyntaxTree.Parameter;
  4251. actualParameter: SyntaxTree.Expression;
  4252. i: LONGINT;
  4253. self: SyntaxTree.Expression;
  4254. BEGIN
  4255. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4256. result := SyntaxTree.invalidDesignator;
  4257. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4258. numberFormalParameters := formalType.numberParameters;
  4259. numberActualParameters := actualParameters.Length();
  4260. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4261. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4262. END;
  4263. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4264. self := left.left;
  4265. IF (self # NIL) & ~IsVariable(self) THEN
  4266. Error(self.position, "Non-variable expression on variable receiver");
  4267. END;
  4268. END;
  4269. IF ~ExpressionList(actualParameters) THEN
  4270. result := SyntaxTree.invalidDesignator
  4271. ELSE
  4272. IF numberActualParameters <= numberFormalParameters THEN
  4273. formalParameter := formalType.firstParameter;
  4274. FOR i := 0 TO numberActualParameters-1 DO
  4275. actualParameter := actualParameters.GetExpression(i);
  4276. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4277. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4278. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4279. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4280. ELSE
  4281. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4282. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4283. END;
  4284. actualParameters.SetExpression(i,actualParameter);
  4285. END;
  4286. formalParameter := formalParameter.nextParameter;
  4287. END;
  4288. WHILE (formalParameter # NIL) DO
  4289. IF formalParameter.defaultValue # NIL THEN
  4290. actualParameters.AddExpression(formalParameter.defaultValue);
  4291. formalParameter := formalParameter.nextParameter
  4292. ELSE
  4293. Error(position, "less actual than formal parameters");
  4294. formalParameter := NIL;
  4295. END;
  4296. END;
  4297. ELSE
  4298. Error(position, "more actual than formal parameters")
  4299. END;
  4300. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4301. result.SetAssignable(FALSE);
  4302. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4303. END;
  4304. RETURN result
  4305. END NewProcedureCallDesignator;
  4306. (**
  4307. builtin call designator generated in VisitParameterDesignator
  4308. -> nothing to be resolved
  4309. **)
  4310. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4311. BEGIN
  4312. resolvedExpression := x;
  4313. END VisitTypeGuardDesignator;
  4314. (**
  4315. builtin call designator generated in VisitParameterDesignator
  4316. -> nothing to be resolved
  4317. **)
  4318. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4319. BEGIN
  4320. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4321. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4322. ASSERT(resolvedExpression.type # NIL);
  4323. ELSIF ExpressionList(x.parameters) THEN
  4324. resolvedExpression := x;
  4325. END;
  4326. END VisitBuiltinCallDesignator;
  4327. (**
  4328. procedure call designator generated in VisitParameterDesignator
  4329. -> nothing to be resolved
  4330. **)
  4331. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4332. BEGIN
  4333. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4334. resolvedExpression := x;
  4335. END VisitProcedureCallDesignator;
  4336. (** return true if x is a variable else return false and report error **)
  4337. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4338. VAR result: BOOLEAN;
  4339. BEGIN
  4340. result := TRUE;
  4341. IF x = SyntaxTree.invalidExpression THEN
  4342. result := FALSE;
  4343. ELSIF ~IsVariable(x) THEN
  4344. Error(x.position,"non variable expression");
  4345. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4346. result := FALSE;
  4347. END;
  4348. RETURN result
  4349. END CheckVariable;
  4350. (**
  4351. if expression x is of basic type then return true else report error and return false
  4352. **)
  4353. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4354. VAR result: BOOLEAN;
  4355. BEGIN
  4356. result := FALSE;
  4357. IF x = SyntaxTree.invalidExpression THEN
  4358. ELSIF ~IsBasicType(x.type) THEN
  4359. Error(x.position,"is no basic type");
  4360. result := FALSE
  4361. ELSE result := TRUE
  4362. END;
  4363. RETURN result
  4364. END CheckBasicType;
  4365. (**
  4366. if expression x is of number type then return true else report error and return false
  4367. **)
  4368. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4369. VAR result: BOOLEAN;
  4370. BEGIN
  4371. result := FALSE;
  4372. IF x = SyntaxTree.invalidExpression THEN
  4373. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4374. Error(x.position,"is non number type");
  4375. ELSE result := TRUE
  4376. END;
  4377. RETURN result
  4378. END CheckNumberType;
  4379. (**
  4380. if expression x is of number or size type but not complex then return true else report error and return false
  4381. **)
  4382. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN;
  4384. BEGIN
  4385. result := FALSE;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4388. Error(x.position,"is complex type");
  4389. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4390. Error(x.position,"is non number type");
  4391. ELSE result := TRUE
  4392. END;
  4393. RETURN result
  4394. END CheckNonComplexNumberSizeType;
  4395. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4397. BEGIN
  4398. result := FALSE; type := x.type.resolved;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. 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
  4401. TRACE(type.sizeInBits);
  4402. TRACE(system.addressType.sizeInBits);
  4403. Error(x.position,"is no address type");
  4404. ELSE result := TRUE
  4405. END;
  4406. RETURN result
  4407. END CheckAddressType;
  4408. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4409. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4410. BEGIN
  4411. result := FALSE; type := x.type.resolved;
  4412. IF x = SyntaxTree.invalidExpression THEN
  4413. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4414. Error(x.position,"is no size type");
  4415. ELSE result := TRUE
  4416. END;
  4417. RETURN result
  4418. END CheckSizeType;
  4419. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4420. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4421. BEGIN
  4422. result := FALSE; type := x.type.resolved;
  4423. IF x = SyntaxTree.invalidExpression THEN
  4424. 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
  4425. Error(x.position,"is no object type");
  4426. ELSE result := TRUE
  4427. END;
  4428. RETURN result
  4429. END CheckObjectType;
  4430. (**
  4431. if expression x is of integer type then return true else report error and return false
  4432. **)
  4433. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4434. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4435. BEGIN
  4436. result := FALSE; type := x.type.resolved;
  4437. IF x = SyntaxTree.invalidExpression THEN
  4438. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4439. Error(x.position,"is no integer type");
  4440. ELSE result := TRUE
  4441. END;
  4442. RETURN result
  4443. END CheckIntegerType;
  4444. (**
  4445. if expression x is of character type then return true else report error and return false
  4446. **)
  4447. PROCEDURE CheckCharacterType(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.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4453. Error(x.position,"is no character type");
  4454. ELSE result := TRUE
  4455. END;
  4456. RETURN result
  4457. END CheckCharacterType;
  4458. (**
  4459. if expression x is of real type then return true else report error and return false
  4460. **)
  4461. PROCEDURE CheckRealType(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.FloatType) THEN
  4467. Error(x.position,"is no float type");
  4468. ELSE result := TRUE
  4469. END;
  4470. RETURN result
  4471. END CheckRealType;
  4472. (**
  4473. if expression x is of range type then return true else report error and return false
  4474. **)
  4475. PROCEDURE CheckRangeType(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.RangeType) THEN
  4481. Error(x.position,"is no range type");
  4482. ELSE result := TRUE
  4483. END;
  4484. RETURN result
  4485. END CheckRangeType;
  4486. (**
  4487. if expression x is of boolean type then return true else report error and return false
  4488. **)
  4489. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4490. VAR result: BOOLEAN;
  4491. BEGIN
  4492. result := FALSE;
  4493. IF x = SyntaxTree.invalidExpression THEN
  4494. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4495. Error(x.position,"is no boolean type");
  4496. ELSE result := TRUE
  4497. END;
  4498. RETURN result
  4499. END CheckBooleanType;
  4500. (**
  4501. if expression x is of set type then return true else report error and return false
  4502. **)
  4503. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4509. Error(x.position,"is no set type");
  4510. ELSE result := TRUE
  4511. END;
  4512. RETURN result
  4513. END CheckSetType;
  4514. (**
  4515. if expression x is of string or array of character type then return true else report error and return false
  4516. **)
  4517. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4518. VAR result: BOOLEAN;
  4519. BEGIN
  4520. result := FALSE;
  4521. IF x = SyntaxTree.invalidExpression THEN
  4522. ELSIF ~IsStringType(x.type.resolved) THEN
  4523. Error(x.position,"is no string type");
  4524. ELSE result := TRUE
  4525. END;
  4526. RETURN result
  4527. END CheckStringType;
  4528. (**
  4529. if expression x is a type declaration type return true else report error and return false
  4530. **)
  4531. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4532. VAR result: BOOLEAN;
  4533. BEGIN
  4534. result := FALSE;
  4535. IF x = SyntaxTree.invalidExpression THEN
  4536. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4537. Error(x.position,"is not a type declaration");
  4538. ELSE result := TRUE
  4539. END;
  4540. RETURN result
  4541. END CheckTypeDeclarationType;
  4542. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4543. VAR result: BOOLEAN;
  4544. BEGIN
  4545. result := FALSE;
  4546. IF x = SyntaxTree.invalidExpression THEN
  4547. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4548. result := TRUE;
  4549. value := x.resolved(SyntaxTree.IntegerValue).value;
  4550. ELSE
  4551. Error(x.position,"expression is not an integer constant");
  4552. END;
  4553. RETURN result;
  4554. END CheckIntegerValue;
  4555. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4556. VAR result: BOOLEAN;
  4557. BEGIN
  4558. result := FALSE;
  4559. IF x = SyntaxTree.invalidExpression THEN
  4560. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4561. result := TRUE;
  4562. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4563. ELSE
  4564. Error(x.position,"expression is not an integer constant");
  4565. END;
  4566. RETURN result;
  4567. END CheckStringValue;
  4568. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4569. BEGIN
  4570. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4571. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4572. ELSE
  4573. RETURN FALSE
  4574. END;
  4575. END IsUnsignedValue;
  4576. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4577. BEGIN
  4578. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4579. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4580. ELSE
  4581. RETURN FALSE
  4582. END
  4583. END IsAddressValue;
  4584. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4585. BEGIN
  4586. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4587. END IsAddressExpression;
  4588. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4589. BEGIN
  4590. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4591. END IsSizeExpression;
  4592. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4593. VAR result: BOOLEAN;
  4594. BEGIN
  4595. result := FALSE;
  4596. IF x = SyntaxTree.invalidExpression THEN
  4597. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4598. result := TRUE;
  4599. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4600. ELSE
  4601. Error(x.position,"expression is not an integer constant");
  4602. END;
  4603. RETURN result;
  4604. END CheckEnumerationValue;
  4605. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4606. VAR result: BOOLEAN;
  4607. BEGIN
  4608. result := FALSE;
  4609. IF x = SyntaxTree.invalidExpression THEN
  4610. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4611. result := TRUE;
  4612. value := x.resolved(SyntaxTree.CharacterValue).value;
  4613. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4614. result := TRUE;
  4615. value := x.resolved(SyntaxTree.StringValue).value[0];
  4616. ELSE
  4617. Error(x.position,"expression is not a character constant");
  4618. END;
  4619. RETURN result;
  4620. END CheckCharacterValue;
  4621. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4622. VAR result: BOOLEAN;
  4623. BEGIN
  4624. result := FALSE;
  4625. IF x = SyntaxTree.invalidExpression THEN
  4626. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4627. value := x.resolved(SyntaxTree.IntegerValue).value;
  4628. IF (value > 0) OR includeZero & (value = 0) THEN
  4629. result := TRUE;
  4630. ELSE
  4631. Error(x.position,"integer is not positive");
  4632. END
  4633. ELSE
  4634. Error(x.position,"expression is not an integer constant");
  4635. END;
  4636. RETURN result;
  4637. END CheckPositiveIntegerValue;
  4638. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4639. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4640. BEGIN
  4641. result := FALSE;
  4642. IF x = SyntaxTree.invalidExpression THEN
  4643. ELSE
  4644. type := x.type.resolved;
  4645. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4646. portType := type(SyntaxTree.PortType);
  4647. result := TRUE
  4648. ELSE
  4649. Error(x.position,"no port type");
  4650. END;
  4651. END;
  4652. RETURN result
  4653. END CheckPortType;
  4654. (* move to builtin procedure call statement ?
  4655. remove builtin procedure call designator ?
  4656. *)
  4657. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4658. VAR
  4659. numberActualParameters,numberFormalParameters: LONGINT;
  4660. formalParameter: SyntaxTree.Parameter;
  4661. actualParameter: SyntaxTree.Expression;
  4662. procedureType: SyntaxTree.ProcedureType;
  4663. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4664. inPort, outPort: SyntaxTree.PortType;
  4665. constructor: SyntaxTree.Procedure;
  4666. type0,type1,type2: SyntaxTree.Type;
  4667. type,base,parameterType: SyntaxTree.Type;
  4668. arrayType: SyntaxTree.ArrayType;
  4669. i,i0,i1: LONGINT;
  4670. r,r0,r1,im: LONGREAL;
  4671. c: CHAR;
  4672. id: LONGINT;
  4673. b: BOOLEAN;
  4674. first: LONGINT;
  4675. mathArrayType: SyntaxTree.MathArrayType;
  4676. customBuiltin: SyntaxTree.CustomBuiltin;
  4677. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4678. VAR resultB: BOOLEAN;
  4679. BEGIN
  4680. IF numberActualParameters < from THEN
  4681. Error(position, "less actual than formal parameters");
  4682. result := SyntaxTree.invalidExpression;
  4683. resultB := FALSE;
  4684. ELSIF numberActualParameters > to THEN
  4685. Error(position, "more actual than formal parameters");
  4686. result := SyntaxTree.invalidExpression;
  4687. resultB := FALSE;
  4688. ELSE
  4689. resultB := TRUE;
  4690. END;
  4691. RETURN resultB
  4692. END CheckArity;
  4693. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4694. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4695. BEGIN
  4696. WHILE modifier # NIL DO
  4697. symbol := cellType.FindProperty(modifier.identifier);
  4698. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4699. propertyType := symbol.type.resolved;
  4700. modifierType := modifier.expression.type.resolved;
  4701. IF ~CompatibleTo(system, modifierType, propertyType) &
  4702. ~(
  4703. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4704. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4705. Error(modifier.position,"incompatible to cell property");
  4706. END;
  4707. ELSE
  4708. Error(modifier.position, "undefined property");
  4709. END;
  4710. modifier := modifier.nextModifier;
  4711. END;
  4712. END CheckModifiers;
  4713. BEGIN
  4714. type := NIL; result := NIL;
  4715. type0 := NIL; type1 := NIL; type2 := NIL;
  4716. numberActualParameters := actualParameters.Length();
  4717. IF numberActualParameters>0 THEN
  4718. parameter0 := actualParameters.GetExpression(0);
  4719. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4720. Error(parameter0.position,"forbidden type-less argument");
  4721. result := SyntaxTree.invalidExpression
  4722. END
  4723. END;
  4724. IF numberActualParameters >1 THEN
  4725. parameter1 := actualParameters.GetExpression(1);
  4726. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4727. ELSE
  4728. Error(parameter1.position,"forbidden type-less argument");
  4729. result := SyntaxTree.invalidExpression
  4730. END
  4731. END;
  4732. IF numberActualParameters >2 THEN
  4733. parameter2 := actualParameters.GetExpression(2);
  4734. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4735. ELSE
  4736. Error(parameter2.position,"forbidden type-less argument");
  4737. result := SyntaxTree.invalidExpression
  4738. END
  4739. END;
  4740. IF returnType # NIL THEN
  4741. id := Global.New;
  4742. result := NIL;
  4743. ELSE
  4744. id := builtin.id;
  4745. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4746. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4747. END;
  4748. END;
  4749. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4750. ELSIF result # NIL THEN type := result.type (* operator *)
  4751. ELSE
  4752. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4753. result(SyntaxTree.Designator).SetLeft(left);
  4754. IF returnType # NIL THEN
  4755. type := returnType;
  4756. END;
  4757. (* ---- ASSERT ----- *)
  4758. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4759. IF CheckBooleanType(parameter0) THEN
  4760. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4761. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4762. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4763. *)
  4764. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4765. Error(position, "assert failed");
  4766. END;
  4767. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4768. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4769. rules imposed by the architecture / current runtime
  4770. *)
  4771. END;
  4772. END;
  4773. (* ---- COPY ----- *)
  4774. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4775. IF~IsStringType(type0) THEN
  4776. Error(parameter0.position,"no string type");
  4777. END;
  4778. IF ~IsStringType(type1) THEN
  4779. Error(parameter1.position,"no string type");
  4780. ELSIF CheckVariable(parameter1) THEN
  4781. IF (type0 IS SyntaxTree.StringType) THEN
  4782. arrayType := type1(SyntaxTree.ArrayType);
  4783. IF arrayType.form = SyntaxTree.Static THEN
  4784. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4785. Error(position, "destination length smaller than source length")
  4786. END;
  4787. END;
  4788. END;
  4789. END;
  4790. (* ---- INC, DEC----- *)
  4791. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4792. IF numberActualParameters = 1 THEN
  4793. parameter1 :=Global.NewIntegerValue(system,position,1);
  4794. actualParameters.AddExpression(parameter1);
  4795. END;
  4796. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4797. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4798. Error(position, "incompatible increment");
  4799. ELSE
  4800. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4801. actualParameters.SetExpression(1,parameter1);
  4802. END;
  4803. END;
  4804. (* ---- EXCL, INCL----- *)
  4805. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4806. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4807. IF IsIntegerValue(parameter1,i0) THEN
  4808. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4809. Error(position, "parameter out of SET range")
  4810. END;
  4811. END;
  4812. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4813. actualParameters.SetExpression(1,parameter1);
  4814. END;
  4815. (* ---- HALT, SYSTEM.HALT ----- *)
  4816. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4817. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4818. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4819. rules imposed by the architecture / current runtime
  4820. *)
  4821. END;
  4822. (* ---- WAIT ----- *)
  4823. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4824. IF CheckObjectType(parameter0) THEN
  4825. END;
  4826. (* ---- NEW ----- *)
  4827. ELSIF (id = Global.New) THEN
  4828. IF returnType # NIL THEN
  4829. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4830. ELSE
  4831. first := 1;
  4832. END;
  4833. IF CheckArity(first,Infinity) THEN
  4834. IF currentIsRealtime THEN
  4835. Error(position, "forbidden new in realtime block");
  4836. END;
  4837. (* check constructor *)
  4838. IF (first =0) OR CheckVariable(parameter0) THEN
  4839. IF type0 IS SyntaxTree.PointerType THEN
  4840. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4841. ELSIF type0 IS SyntaxTree.CellType THEN
  4842. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4843. ELSE
  4844. Error(position, "forbidden new on value type");
  4845. END;
  4846. IF type0 IS SyntaxTree.ArrayType THEN
  4847. arrayType := type0(SyntaxTree.ArrayType);
  4848. IF arrayType.form = SyntaxTree.Static THEN
  4849. i := first
  4850. ELSIF arrayType.form = SyntaxTree.Open THEN
  4851. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4852. ELSE HALT(100)
  4853. END;
  4854. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4855. i := first;
  4856. REPEAT
  4857. actualParameter := actualParameters.GetExpression(i);
  4858. IF CheckSizeType(actualParameter) THEN
  4859. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4860. actualParameters.SetExpression(i,actualParameter);
  4861. END;
  4862. INC(i);
  4863. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4864. END;
  4865. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4866. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4867. IF constructor = NIL THEN
  4868. IF CheckArity(first,first) THEN END;
  4869. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4870. Error(position, "new on object with hidden constructor");
  4871. ELSE
  4872. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4873. numberFormalParameters := procedureType.numberParameters;
  4874. IF numberActualParameters-first <= numberFormalParameters THEN
  4875. formalParameter := procedureType.firstParameter;
  4876. FOR i := first TO numberActualParameters-1 DO
  4877. actualParameter := actualParameters.GetExpression(i);
  4878. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4879. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4880. ELSE
  4881. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4882. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4883. END;
  4884. actualParameters.SetExpression(i,actualParameter);
  4885. END;
  4886. formalParameter := formalParameter.nextParameter;
  4887. END;
  4888. WHILE (formalParameter # NIL) DO
  4889. IF formalParameter.defaultValue # NIL THEN
  4890. actualParameters.AddExpression(formalParameter.defaultValue);
  4891. formalParameter := formalParameter.nextParameter
  4892. ELSE
  4893. Error(position, "less actual than formal parameters");
  4894. formalParameter := NIL;
  4895. END;
  4896. END;
  4897. ELSE
  4898. Error(position, "more actual than formal parameters")
  4899. END;
  4900. END;
  4901. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4902. mathArrayType := type0(SyntaxTree.MathArrayType);
  4903. IF mathArrayType.form = SyntaxTree.Static THEN
  4904. Error(position, "new on static array");
  4905. ELSE
  4906. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4907. i0 := first+1; i1 := Infinity;
  4908. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4909. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4910. i1 := i0;
  4911. ELSE HALT(100);
  4912. END;
  4913. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4914. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4915. base := ArrayBase(type0,MAX(LONGINT));
  4916. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4917. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4918. IF ~CompatibleTo(system,type0,parameterType) THEN
  4919. Error(parameter0.position,"incompatible parameter in new");
  4920. result := SyntaxTree.invalidExpression;
  4921. ELSE
  4922. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4923. END;
  4924. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4925. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4926. IF ~CompatibleTo(system,type1,parameterType) THEN
  4927. Error(parameter1.position,"parameter incompatible to math array of longint");
  4928. result := SyntaxTree.invalidExpression;
  4929. ELSE
  4930. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4931. END;
  4932. ELSE
  4933. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4934. i := first;
  4935. REPEAT
  4936. actualParameter := actualParameters.GetExpression(i);
  4937. IF CheckSizeType(actualParameter) THEN
  4938. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4939. actualParameters.SetExpression(i,actualParameter);
  4940. END;
  4941. INC(i);
  4942. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4943. END;
  4944. END;
  4945. END;
  4946. ELSIF type0 IS SyntaxTree.CellType THEN
  4947. IF ~(currentIsCellNet) THEN
  4948. Error(position, "cell allocation outside activeCells ");
  4949. ELSE
  4950. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4951. IF (constructor = NIL) & CheckArity(1,1) THEN
  4952. (* ok *)
  4953. ELSE
  4954. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4955. numberFormalParameters := procedureType.numberParameters;
  4956. DEC(numberActualParameters);
  4957. IF numberActualParameters <= numberFormalParameters THEN
  4958. formalParameter := procedureType.firstParameter;
  4959. FOR i := first TO numberActualParameters DO
  4960. actualParameter := actualParameters.GetExpression(i);
  4961. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4962. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4963. ELSE
  4964. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4965. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4966. END;
  4967. actualParameters.SetExpression(i,actualParameter);
  4968. END;
  4969. formalParameter := formalParameter.nextParameter;
  4970. END;
  4971. WHILE (formalParameter # NIL) DO
  4972. IF formalParameter.defaultValue # NIL THEN
  4973. actualParameters.AddExpression(formalParameter.defaultValue);
  4974. formalParameter := formalParameter.nextParameter
  4975. ELSE
  4976. Error(position, "less actual than formal parameters");
  4977. formalParameter := NIL;
  4978. END;
  4979. END;
  4980. ELSE
  4981. Error(position, "more actual than formal parameters")
  4982. END;
  4983. END;
  4984. END;
  4985. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4986. activeCellsStatement := TRUE;
  4987. ELSE
  4988. Error(position, "cannot be allocated");
  4989. END;
  4990. END;
  4991. END;
  4992. (* ---- DISPOSE ----- *)
  4993. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4994. IF ~IsPointerType(parameter0.type) THEN
  4995. Error(parameter0.position,"is not a pointer")
  4996. ELSIF ~IsDisposable(parameter0.type) THEN
  4997. Error(parameter0.position,"is not disposable")
  4998. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4999. END
  5000. (* ---- GETPROCEDURE ----- *)
  5001. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5002. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5003. IF CheckVariable(parameter2) THEN
  5004. IF ~GetProcedureAllowed(parameter2.type) THEN
  5005. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5006. END;
  5007. END;
  5008. END;
  5009. (* ---- ABS ----- *)
  5010. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5011. (* note: ABS on complex numbers is done using overloading *)
  5012. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5013. type := type0;
  5014. IF IsIntegerValue(parameter0,i0) THEN
  5015. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5016. type := Global.GetIntegerType(system,ABS(i0));
  5017. ELSIF IsRealValue(parameter0,r) THEN
  5018. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5019. END;
  5020. ELSE
  5021. type := SyntaxTree.invalidType;
  5022. END;
  5023. (* ---- ASH, ASR ----- *)
  5024. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5025. type := type0;
  5026. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5027. (*
  5028. ConvertOperands(parameter0,parameter1); (* same type *)
  5029. *)
  5030. type := parameter0.type;
  5031. IF IsIntegerValue(parameter0,i0) THEN
  5032. IF IsIntegerValue(parameter1,i1) THEN
  5033. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5034. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5035. result := ResolveExpression(result);
  5036. type := Global.GetIntegerType(system,i0);
  5037. END;
  5038. END;
  5039. IF type.resolved.sizeInBits < 32 THEN
  5040. type := system.longintType;
  5041. END;
  5042. (*!compatibility with release, remove when resolved
  5043. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5044. *)
  5045. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5046. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5047. actualParameters.SetExpression(0,parameter0);
  5048. actualParameters.SetExpression(1,parameter1);
  5049. END;
  5050. (* ---- CAP ----- *)
  5051. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5052. type := system.characterType;
  5053. IF CheckCharacterType (parameter0) THEN
  5054. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5055. actualParameters.SetExpression(0,parameter0);
  5056. IF IsCharacterValue(parameter0,c) THEN
  5057. IF (c <= "z") & (c >= "a") THEN
  5058. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5059. ELSE
  5060. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5061. END;
  5062. END;
  5063. END;
  5064. (* ---- CHR ----- *)
  5065. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5066. IF id = Global.Chr THEN
  5067. type := system.characterType
  5068. ELSE
  5069. type := system.characterType32
  5070. END;
  5071. IF CheckIntegerType(parameter0) THEN
  5072. IF IsIntegerValue(parameter0,i0) THEN
  5073. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5074. result := ResolveExpression(result);
  5075. ELSE
  5076. (*
  5077. result := NewConversion(parameter0.position,parameter0,type);
  5078. *)
  5079. END;
  5080. END
  5081. (* ---- ENTIER ----- *)
  5082. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5083. type := system.longintType;
  5084. IF CheckRealType(parameter0) THEN
  5085. IF IsRealValue(parameter0,r) THEN
  5086. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5087. type := Global.GetIntegerType(system,ENTIER(r));
  5088. END
  5089. END;
  5090. (* ---- ENTIERH ----- *)
  5091. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5092. type := system.hugeintType;
  5093. IF CheckRealType(parameter0) THEN
  5094. IF IsRealValue(parameter0,r) THEN
  5095. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5096. END
  5097. END;
  5098. (* ---- LEN ----- *)
  5099. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5100. type := system.longintType;
  5101. base := type0;
  5102. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5103. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5104. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5105. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5106. Error(position, "forbidden len on unsafe pointer");
  5107. END;
  5108. type0 := base;
  5109. ELSE
  5110. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5111. type0 := parameter0.type.resolved;
  5112. actualParameters.SetExpression(0,parameter0);
  5113. base := type0;
  5114. END;
  5115. END;
  5116. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5117. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5118. IF i1 < 0 THEN
  5119. Error(position, "invalid dimension");
  5120. base := SyntaxTree.invalidType;
  5121. ELSE
  5122. base := ArrayBase(base,i1);
  5123. IF (base # NIL) & Indexable(base) THEN
  5124. ELSE
  5125. Error(position, "len on no array");
  5126. IF VerboseErrorMessage THEN
  5127. Printout.Info("base",base);
  5128. END;
  5129. base := SyntaxTree.invalidType;
  5130. END;
  5131. END;
  5132. IF numberActualParameters=2 THEN
  5133. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5134. actualParameters.SetExpression(1,parameter1);
  5135. ELSIF base IS SyntaxTree.MathArrayType THEN
  5136. Error(position, "missing dimension specification");
  5137. END;
  5138. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5139. IF base IS SyntaxTree.ArrayType THEN
  5140. arrayType := base(SyntaxTree.ArrayType);
  5141. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5142. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5143. result := Global.NewIntegerValue(system,position,i);
  5144. type := result.type;(* arrayType.length.type;*)
  5145. ASSERT(type # NIL);
  5146. END;
  5147. ELSIF base IS SyntaxTree.MathArrayType THEN
  5148. mathArrayType := base(SyntaxTree.MathArrayType);
  5149. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5150. result := Global.NewIntegerValue(system,position,i);
  5151. type := result.type;
  5152. (*
  5153. type := mathArrayType.length.type;
  5154. *)
  5155. ASSERT(type # NIL);
  5156. END;
  5157. END;
  5158. END;
  5159. ELSE
  5160. type := system.longintType;
  5161. END;
  5162. (* ---- FIRST ---- *)
  5163. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5164. type := system.longintType;
  5165. IF CheckRangeType(parameter0) THEN END;
  5166. result.SetAssignable(parameter0.assignable)
  5167. (* ---- LAST ---- *)
  5168. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5169. type := system.longintType;
  5170. IF CheckRangeType(parameter0) THEN END;
  5171. result.SetAssignable(parameter0.assignable)
  5172. (* ---- STEP ---- *)
  5173. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5174. type := system.longintType;
  5175. IF CheckRangeType(parameter0) THEN END;
  5176. result.SetAssignable(parameter0.assignable)
  5177. (* ---- RE ---- *)
  5178. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5179. IF CheckNumberType(parameter0) THEN
  5180. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5181. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5182. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5183. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5184. type := parameter0.type
  5185. ELSE
  5186. type := system.realType
  5187. END
  5188. END;
  5189. result.SetAssignable(parameter0.assignable)
  5190. (* ---- IM ---- *)
  5191. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5192. IF CheckNumberType(parameter0) THEN
  5193. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5194. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5195. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5196. ELSE
  5197. type := system.realType;
  5198. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5199. END
  5200. END;
  5201. result.SetAssignable(parameter0.assignable)
  5202. (* ---- MAX ----- *)
  5203. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5204. IF numberActualParameters = 1 THEN
  5205. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5206. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5207. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5208. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5209. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5210. *)
  5211. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5212. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5213. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5214. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5215. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5216. END;
  5217. ELSE
  5218. Error(parameter0.position,"is not a type symbol");
  5219. END
  5220. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5221. ConvertOperands(parameter0,parameter1);
  5222. actualParameters.SetExpression(0,parameter0);
  5223. actualParameters.SetExpression(1,parameter1);
  5224. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5225. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5226. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5227. END;
  5228. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5229. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5230. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5231. END;
  5232. END;
  5233. type := parameter0.type;
  5234. ELSE type := SyntaxTree.invalidType;
  5235. END;
  5236. (* ---- MIN ----- *)
  5237. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5238. IF numberActualParameters = 1 THEN
  5239. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5240. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5241. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5242. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5243. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5244. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5245. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5246. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5247. END;
  5248. ELSE
  5249. Error(parameter0.position,"is not a type symbol");
  5250. END
  5251. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5252. ConvertOperands(parameter0,parameter1);
  5253. actualParameters.SetExpression(0,parameter0);
  5254. actualParameters.SetExpression(1,parameter1);
  5255. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5256. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5257. ELSE result.SetResolved(parameter1.resolved)
  5258. END;
  5259. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5260. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5261. ELSE result.SetResolved(parameter1.resolved)
  5262. END;
  5263. END;
  5264. type := parameter0.type;
  5265. ELSE type := SyntaxTree.invalidType;
  5266. END;
  5267. (* ---- ODD ----- *)
  5268. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5269. type := system.booleanType;
  5270. IF CheckIntegerType(parameter0) THEN
  5271. IF IsIntegerValue(parameter0,i0) THEN
  5272. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5273. type := system.booleanType;
  5274. END;
  5275. END;
  5276. (* ---- ORD ----- *)
  5277. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5278. IF id = Global.Ord THEN
  5279. type := system.integerType;
  5280. ELSE
  5281. type := system.longintType;
  5282. END;
  5283. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5284. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5285. actualParameters.SetExpression(0,parameter0);
  5286. (* IF CheckCharacterType(parameter0) THEN*)
  5287. IF IsCharacterValue(parameter0,c)THEN
  5288. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5289. type := Global.GetSignedIntegerType(system,ORD(c));
  5290. END;
  5291. ELSE Error(parameter0.position, "incompatible parameter");
  5292. END;
  5293. (* ---- SHORT ----- *)
  5294. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5295. type := type0;
  5296. IF IsSignedIntegerType(type) THEN
  5297. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5298. ELSIF type = system.integerType THEN type := system.shortintType
  5299. ELSIF type = system.longintType THEN type := system.integerType
  5300. ELSIF type = system.hugeintType THEN type:= system.longintType
  5301. ELSE
  5302. CASE type.sizeInBits OF
  5303. 16: type := Global.Integer8
  5304. |32: type := Global.Integer16
  5305. |64: type := Global.Integer32
  5306. END;
  5307. END;
  5308. ELSIF type IS SyntaxTree.FloatType THEN
  5309. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5310. ELSIF type = system.longrealType THEN type := system.realType
  5311. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5312. END;
  5313. ELSIF type IS SyntaxTree.ComplexType THEN
  5314. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5315. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5316. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5317. END;
  5318. ELSE
  5319. Error(parameter0.position,"short not applicable")
  5320. END;
  5321. IF (parameter0.resolved # NIL) THEN
  5322. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5323. IF parameter0 IS SyntaxTree.Value THEN
  5324. result.SetResolved(parameter0(SyntaxTree.Value));
  5325. END;
  5326. END;
  5327. (* ---- LONG ----- *)
  5328. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5329. type := type0;
  5330. IF IsSignedIntegerType(type) THEN
  5331. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5332. ELSIF type = system.longintType THEN type := system.hugeintType
  5333. ELSIF type = system.integerType THEN type := system.longintType
  5334. ELSIF type = system.shortintType THEN type := system.integerType
  5335. ELSE
  5336. CASE type.sizeInBits OF
  5337. 8: type := Global.Integer16
  5338. |16: type := Global.Integer32
  5339. |32: type := Global.Integer64
  5340. END;
  5341. END;
  5342. ELSIF type IS SyntaxTree.FloatType THEN
  5343. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5344. ELSIF type= system.realType THEN type := system.longrealType
  5345. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5346. END;
  5347. ELSIF type IS SyntaxTree.ComplexType THEN
  5348. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5349. ELSIF type = system.complexType THEN type := system.longcomplexType
  5350. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5351. END;
  5352. ELSE
  5353. Error(parameter0.position,"long not applicable")
  5354. END;
  5355. IF (parameter0.resolved # NIL) THEN
  5356. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5357. IF parameter0 IS SyntaxTree.Value THEN
  5358. result.SetResolved(parameter0(SyntaxTree.Value));
  5359. END;
  5360. END;
  5361. (* ---- SIZE OF ----- *)
  5362. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5363. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5364. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5365. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5366. type := system.integerType;
  5367. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5368. ELSE
  5369. (* for variables, system sizeof could represent the physically occupied size
  5370. determined via the type descriptor, implement that ? *)
  5371. Error(parameter0.position,"is not a type symbol");
  5372. END
  5373. (* ---- SYSTEM.TRACE -----*)
  5374. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5375. FOR i := 0 TO numberActualParameters-1 DO
  5376. parameter0 := actualParameters.GetExpression(i);
  5377. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5378. Error(parameter0.position,"incompatible parameter");
  5379. END;
  5380. END;
  5381. (* remaining issues can only be tested in backend *)
  5382. (* ---- ADDRESSOF----- *)
  5383. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5384. IF HasAddress(parameter0) THEN
  5385. type := system.addressType;
  5386. ELSE
  5387. type := SyntaxTree.invalidType;
  5388. Error(parameter0.position,"has no address");
  5389. END;
  5390. (* ---- BIT ----- *)
  5391. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5392. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5393. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5394. actualParameters.SetExpression(0,parameter0);
  5395. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5396. actualParameters.SetExpression(1,parameter1);
  5397. END;
  5398. type := system.booleanType;
  5399. (* ----- MSK ---- *)
  5400. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5401. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5402. ConvertOperands(parameter0,parameter1);
  5403. actualParameters.SetExpression(0,parameter0);
  5404. actualParameters.SetExpression(1,parameter1);
  5405. END;
  5406. type := parameter0.type;
  5407. (* ---- SYSTEM.GET64 ----- *)
  5408. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5409. IF CheckAddressType(parameter0) THEN
  5410. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5411. actualParameters.SetExpression(0,parameter0);
  5412. END;
  5413. type := system.hugeintType;
  5414. (* ---- SYSTEM.GET32 ----- *)
  5415. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5416. IF CheckAddressType(parameter0) THEN
  5417. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5418. actualParameters.SetExpression(0,parameter0);
  5419. END;
  5420. type := system.longintType;
  5421. (* ---- SYSTEM.GET16 ----- *)
  5422. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5423. IF CheckAddressType(parameter0) THEN
  5424. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5425. actualParameters.SetExpression(0,parameter0);
  5426. END;
  5427. type := system.integerType;
  5428. (* ---- SYSTEM.GET8 ----- *)
  5429. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5430. IF CheckAddressType(parameter0) THEN
  5431. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5432. actualParameters.SetExpression(0,parameter0);
  5433. END;
  5434. type := system.shortintType;
  5435. (* ---- SYSTEM.GetStackPointer ----- *)
  5436. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5437. type := system.addressType;
  5438. (* ---- SYSTEM.GetFramePointer ----- *)
  5439. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5440. type := system.addressType;
  5441. (* ---- SYSTEM.GetActivity ----- *)
  5442. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5443. type := system.objectType;
  5444. (* ---- SYSTEM.SetStackPointer ----- *)
  5445. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5446. IF CheckAddressType(parameter0) THEN
  5447. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5448. actualParameters.SetExpression(0,parameter0);
  5449. END;
  5450. (* ---- SYSTEM.SetFramePointer ----- *)
  5451. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5452. IF CheckAddressType(parameter0) THEN
  5453. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5454. actualParameters.SetExpression(0,parameter0);
  5455. END;
  5456. (* ---- SYSTEM.SetActivity ----- *)
  5457. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5458. IF CheckObjectType(parameter0) THEN
  5459. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5460. actualParameters.SetExpression(0,parameter0);
  5461. END;
  5462. (* ---- LSH, LSL, ROT, ROR ----- *)
  5463. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5464. type := type0;
  5465. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5466. actualParameters.SetExpression(1, parameter1);
  5467. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5468. IF id = Global.Lsh THEN
  5469. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5470. ELSIF id = Global.Rot THEN
  5471. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5472. ELSIF id = Global.Ror THEN
  5473. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5474. END;
  5475. END;
  5476. (* ---- SYSTEM.VAL ----- *)
  5477. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5478. IF CheckTypeDeclarationType(parameter0) THEN
  5479. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5480. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5481. result := SyntaxTree.invalidExpression;
  5482. Error(parameter0.position,"is no basic type");
  5483. ELSE
  5484. IF (parameter1.resolved # NIL) THEN
  5485. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5486. IF parameter0 IS SyntaxTree.Value THEN
  5487. result.SetResolved(parameter0(SyntaxTree.Value));
  5488. END;
  5489. END;
  5490. result.SetAssignable(parameter1.assignable);
  5491. END;
  5492. END;
  5493. (* ---- SYSTEM.GET ----- *)
  5494. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5495. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5496. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5497. actualParameters.SetExpression(0,parameter0);
  5498. END;
  5499. (* ---- SYSTEM.PUT ----- *)
  5500. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5501. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5502. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5503. actualParameters.SetExpression(0,parameter0);
  5504. END;
  5505. (* ---- SYSTEM.PUT64 ----- *)
  5506. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5507. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5508. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5509. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5510. actualParameters.SetExpression(0,parameter0);
  5511. actualParameters.SetExpression(1,parameter1);
  5512. END;
  5513. (* ---- SYSTEM.PUT32 ----- *)
  5514. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5515. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5516. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5517. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5518. actualParameters.SetExpression(0,parameter0);
  5519. actualParameters.SetExpression(1,parameter1);
  5520. END;
  5521. (* ---- SYSTEM.PUT16 ----- *)
  5522. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5523. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5524. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5525. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5526. actualParameters.SetExpression(0,parameter0);
  5527. actualParameters.SetExpression(1,parameter1);
  5528. END;
  5529. (* ---- SYSTEM.PUT8 ----- *)
  5530. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5531. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5532. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5533. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5534. actualParameters.SetExpression(0,parameter0);
  5535. actualParameters.SetExpression(1,parameter1);
  5536. END;
  5537. (* ---- SYSTEM.MOVE ----- *)
  5538. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5539. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5540. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5541. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5542. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5543. actualParameters.SetExpression(0,parameter0);
  5544. actualParameters.SetExpression(1,parameter1);
  5545. actualParameters.SetExpression(2,parameter2);
  5546. END;
  5547. (* ---- SYSTEM.NEW ----- *)
  5548. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5549. IF ~IsPointerType(parameter0.type) THEN
  5550. Error(parameter0.position,"is not a pointer")
  5551. ELSIF CheckSizeType(parameter1) THEN
  5552. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5553. actualParameters.SetExpression(1,parameter1);
  5554. END;
  5555. (* ----SYSTEM.REF ---- *)
  5556. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5557. type := system.addressType
  5558. (* ---- INCR ----- *)
  5559. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5560. type := system.sizeType;
  5561. base := type0;
  5562. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5563. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5564. IF i1 < 0 THEN
  5565. Error(position, "invalid dimension");
  5566. base := SyntaxTree.invalidType;
  5567. ELSE
  5568. base := ArrayBase(base,i1);
  5569. IF (base # NIL) & Indexable(base) THEN
  5570. ELSE
  5571. Error(position, "len on no array");
  5572. IF VerboseErrorMessage THEN
  5573. Printout.Info("base",base);
  5574. END;
  5575. base := SyntaxTree.invalidType;
  5576. END;
  5577. END;
  5578. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5579. actualParameters.SetExpression(1,parameter1);
  5580. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5581. mathArrayType := base(SyntaxTree.MathArrayType);
  5582. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5583. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5584. type := system.longintType;
  5585. END;
  5586. END;
  5587. ELSE
  5588. type := system.longintType;
  5589. END;
  5590. (* ---- SUM ----- *)
  5591. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5592. Error(position, "sum operator not applicable");
  5593. (* ---- ALL ----- *)
  5594. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5595. Error(position, "all operator not applicable");
  5596. (* ---- DIM ----- *)
  5597. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5598. type := system.sizeType;
  5599. IF type0 IS SyntaxTree.MathArrayType THEN
  5600. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5601. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5602. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5603. END;
  5604. ELSE
  5605. Error(position, "dimension on non math array type");
  5606. END;
  5607. (* ---- CAS ----- *)
  5608. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5609. IF type0.IsComposite () THEN
  5610. Error(position, "first parameter of composite type");
  5611. result := SyntaxTree.invalidExpression;
  5612. ELSIF ~IsVariable (parameter0) THEN
  5613. Error(position, "first parameter not assignable");
  5614. result := SyntaxTree.invalidExpression;
  5615. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5616. Error(position, "second parameter incompatible");
  5617. result := SyntaxTree.invalidExpression;
  5618. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5619. Error(position, "third parameter incompatible");
  5620. result := SyntaxTree.invalidExpression;
  5621. ELSE
  5622. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5623. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5624. type := type0;
  5625. END;
  5626. (* ---- RESHAPE ----- *)
  5627. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5628. IF type0 IS SyntaxTree.MathArrayType THEN
  5629. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5630. base := ArrayBase(type0,MAX(LONGINT));
  5631. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5632. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5633. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5634. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5635. IF ~CompatibleTo(system,type0,parameterType) THEN
  5636. Error(parameter0.position,"incompatible parameter in reshape");
  5637. result := SyntaxTree.invalidExpression;
  5638. ELSE
  5639. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5640. END;
  5641. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5642. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5643. IF ~CompatibleTo(system,type1,parameterType) THEN
  5644. Error(parameter1.position,"parameter incompatible to math array of longint");
  5645. result := SyntaxTree.invalidExpression;
  5646. ELSE
  5647. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5648. END;
  5649. ELSE
  5650. Error(position,"reshape on non math array type");
  5651. result := SyntaxTree.invalidExpression;
  5652. END;
  5653. (* ---- SYSTEM.TYPECODE ----- *)
  5654. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5655. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5656. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5657. type := type.resolved;
  5658. IF type IS SyntaxTree.PointerType THEN
  5659. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5660. END;
  5661. IF ~(type IS SyntaxTree.RecordType) THEN
  5662. Error(parameter0.position,"must be type with type descriptor");
  5663. END;
  5664. ELSE
  5665. Error(parameter0.position,"is not a type symbol");
  5666. END;
  5667. type := system.addressType;
  5668. (* -------- FLT --------- *)
  5669. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5670. type := system.realType;
  5671. IF IsRealValue(parameter0, r) THEN
  5672. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5673. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5674. i0 := i; i := ABS(i);
  5675. IF i # 0 THEN
  5676. i1 := 23;
  5677. IF i >= 2*800000H THEN
  5678. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5679. ELSIF i < 800000H THEN
  5680. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5681. END;
  5682. i := (i1 + 127)*800000H - 800000H + i;
  5683. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5684. END;
  5685. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5686. END;
  5687. (* ------- CONNECT -------*)
  5688. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5689. (*IF ~(currentIsCellNet) THEN
  5690. Error(position, "connection outside activeCells body block");
  5691. END;*)
  5692. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5693. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5694. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5695. END;
  5696. IF numberActualParameters = 3 THEN
  5697. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5698. Error(position, "incompatible channel size parameter");
  5699. END;
  5700. *)
  5701. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5702. actualParameters.SetExpression(2,parameter2);
  5703. END;
  5704. activeCellsStatement := TRUE;
  5705. (* ---------- DELEGATE --------*)
  5706. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5707. (*
  5708. IF ~(currentIsCellNet) THEN
  5709. Error(position, "connection delegation outside activeCells body block");
  5710. END;
  5711. *)
  5712. IF ~CheckPortType(parameter1, inPort) THEN
  5713. Error(parameter0.position,"not a port")
  5714. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5715. Error(parameter1.position,"not a port")
  5716. ELSIF (outPort.direction # inPort.direction) THEN
  5717. Error(parameter0.position,"invalid port direction");
  5718. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5719. Error(position, "incompatible port sizes");
  5720. END;
  5721. activeCellsStatement := TRUE;
  5722. (* --------- RECEIVE ---------*)
  5723. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5724. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5725. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5726. IF inPort.direction # SyntaxTree.InPort THEN
  5727. Error(parameter0.position,"not an in-port")
  5728. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5729. Error(parameter1.position,"incompatible to port type");
  5730. END;
  5731. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5732. IF ~SameType(parameter2.type, system.integerType) THEN
  5733. Error(parameter2.position,"incompatible to integer type");
  5734. END;
  5735. END;
  5736. END;
  5737. (* --------- SEND ---------*)
  5738. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5739. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5740. IF CheckPortType(parameter0,outPort) THEN
  5741. IF outPort.direction # SyntaxTree.OutPort THEN
  5742. Error(parameter1.position,"not an out-port")
  5743. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5744. Error(parameter1.position,"incompatible to port type");
  5745. ELSE
  5746. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5747. actualParameters.SetExpression(1,parameter1);
  5748. END;
  5749. END;
  5750. (* ------- custom builtins ----- *)
  5751. ELSIF id = Global.systemSpecial THEN
  5752. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5753. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5754. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5755. type := procedureType.returnType;
  5756. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5757. (* go through all formal parameters *)
  5758. formalParameter := procedureType.firstParameter;
  5759. FOR i := 0 TO actualParameters.Length() - 1 DO
  5760. actualParameter := actualParameters.GetExpression(i);
  5761. IF actualParameter = SyntaxTree.invalidExpression THEN
  5762. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5763. Error(position, "incompatible parameter")
  5764. ELSE
  5765. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5766. END;
  5767. actualParameters.SetExpression(i, actualParameter);
  5768. formalParameter := formalParameter.nextParameter
  5769. END
  5770. END
  5771. ELSE
  5772. Error(position, "builtin not implemented");
  5773. result := SyntaxTree.invalidExpression;
  5774. END;
  5775. END;
  5776. IF result # SyntaxTree.invalidExpression THEN
  5777. type := ResolveType(type);
  5778. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5779. result.SetType(type);
  5780. END;
  5781. RETURN result
  5782. END NewBuiltinCallDesignator;
  5783. (** return type guard designator left(type)
  5784. - check if type can be extended (i.e. is no static record)
  5785. - check if type is a type extension of left.type
  5786. - returns new type guard designator
  5787. returns invalidDesignator = invalidExpression if error
  5788. **)
  5789. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5790. VAR result: SyntaxTree.Designator;
  5791. BEGIN
  5792. result := SyntaxTree.invalidDesignator;
  5793. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5794. Error(position, "no type extension of type");
  5795. IF VerboseErrorMessage THEN
  5796. Printout.Info("left",left);
  5797. Printout.Info("type",type);
  5798. END;
  5799. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5800. Error(position, "variable cannot be extended");
  5801. ELSIF IsUnsafePointer(left.type) THEN
  5802. Error(position, "forbidden type guard on unsafe pointer");
  5803. ELSE
  5804. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5805. result.SetType(type);
  5806. result.SetAssignable(left.assignable);
  5807. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5808. END;
  5809. RETURN result
  5810. END NewTypeGuardDesignator;
  5811. (** check and resolve parameter designator left(expression list)
  5812. - check expression list
  5813. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5814. - elsif left is a procedure type then
  5815. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5816. - else return is a procedure call then return ProcedureCallDesignator
  5817. returns invalidDesignator = invalidExpression if error
  5818. **)
  5819. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5820. VAR
  5821. parameters: SyntaxTree.ExpressionList;
  5822. left: SyntaxTree.Designator;
  5823. result,expression: SyntaxTree.Expression;
  5824. typeDeclaration: SyntaxTree.TypeDeclaration;
  5825. type, expressionType: SyntaxTree.Type;
  5826. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5827. BEGIN
  5828. type := type.resolved;
  5829. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5830. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5831. END;
  5832. RETURN type
  5833. END BaseType;
  5834. BEGIN
  5835. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5836. result := SyntaxTree.invalidDesignator;
  5837. left := ResolveDesignator(designator.left);
  5838. IF left # SyntaxTree.invalidDesignator THEN
  5839. parameters := designator.parameters;
  5840. IF ExpressionList(parameters) THEN
  5841. IF (left.type = NIL) THEN
  5842. Error(left.position,"object is not a procedure or cannot be extended");
  5843. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5844. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5845. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5846. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5847. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5848. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5849. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5850. ELSE
  5851. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5852. END
  5853. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5854. expression := parameters.GetExpression(0);
  5855. type := typeDeclaration.declaredType.resolved;
  5856. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5857. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5858. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5859. OR (expressionType IS SyntaxTree.EnumerationType)
  5860. ) THEN
  5861. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5862. ELSE
  5863. Error(left.position,"invalid type in explicit conversion");
  5864. END;
  5865. ELSE
  5866. Error(left.position,"called object is not a procedure or cannot be extended");
  5867. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5868. result := SyntaxTree.invalidDesignator;
  5869. END;
  5870. ELSE
  5871. result := SyntaxTree.invalidDesignator
  5872. END;
  5873. END;
  5874. resolvedExpression := result;
  5875. END VisitParameterDesignator;
  5876. (** check dereference designator left^
  5877. - check if left is pointer type or left is object type
  5878. - return new dereference designator with type = left.baseType.type (if appropriate)
  5879. with error handling
  5880. returns invalidDesignator = invalidExpression if error
  5881. **)
  5882. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5883. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5884. BEGIN
  5885. result := SyntaxTree.invalidDesignator;
  5886. type := left.type;
  5887. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5888. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5889. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5890. result.SetAssignable(TRUE);
  5891. result.SetType(type);
  5892. result.SetHidden(left.isHidden);
  5893. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5894. type := type.resolved;
  5895. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5896. result.SetAssignable(TRUE);
  5897. result.SetType(type);
  5898. result.SetHidden(left.isHidden);
  5899. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5900. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5901. result.SetAssignable(TRUE);
  5902. result.SetType(type);
  5903. result.SetHidden(left.isHidden);
  5904. ELSE
  5905. Error(position, "dereference on no pointer");
  5906. IF VerboseErrorMessage THEN
  5907. Printout.Info("pointer", type);
  5908. Printout.Info("scope", currentScope);
  5909. END;
  5910. END;
  5911. RETURN result
  5912. END NewDereferenceDesignator;
  5913. (** check supercall designator left^
  5914. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5915. - return new supercall designator with type = left.type
  5916. with error handling
  5917. **)
  5918. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5919. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5920. objectScope: SyntaxTree.Scope;
  5921. BEGIN
  5922. result := SyntaxTree.invalidDesignator;
  5923. IF left = SyntaxTree.invalidDesignator THEN
  5924. (* error already handled *)
  5925. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5926. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5927. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5928. IF symbol IS SyntaxTree.Procedure THEN
  5929. procedure := symbol(SyntaxTree.Procedure);
  5930. objectScope := currentScope;
  5931. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5932. objectScope := objectScope.outerScope;
  5933. END;
  5934. IF (left.left = NIL) OR ~
  5935. (
  5936. (left.left IS SyntaxTree.SelfDesignator) OR
  5937. (left.left IS SyntaxTree.DereferenceDesignator)
  5938. & (left.left(SyntaxTree.Designator).left # NIL)
  5939. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5940. Error(position, "procedure not in immediate object scope");
  5941. IF VerboseErrorMessage THEN
  5942. Printout.Info("left.left",left.left);
  5943. END;
  5944. ELSIF procedure.super # NIL THEN
  5945. result := SyntaxTree.NewSupercallDesignator(position,left);
  5946. result.SetType(left.type.resolved)
  5947. ELSE
  5948. Error(position, "no supermethod for this procedure");
  5949. END;
  5950. ELSE
  5951. Error(position, "symbol is not a procedure");
  5952. END;
  5953. ELSE
  5954. Error(position, "is no symbol designator");
  5955. END;
  5956. RETURN result
  5957. END NewSupercallDesignator;
  5958. (** check and semantically resolve arrow designator left^
  5959. - if left is procedure type -> result := SupercallDesignator
  5960. - else result := DereferenceDesignator
  5961. returns result via global variable resolvedExpression
  5962. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5963. **)
  5964. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5965. VAR left: SyntaxTree.Designator;
  5966. BEGIN
  5967. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5968. left := ResolveDesignator(arrowDesignator.left);
  5969. IF left # NIL THEN
  5970. IF (left.type = NIL) THEN
  5971. Error(arrowDesignator.position,"Invalid arrow designator");
  5972. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5973. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5974. ELSE
  5975. IF IsPointerToObject(left.type) THEN
  5976. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5977. END;
  5978. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5979. END
  5980. END
  5981. END VisitArrowDesignator;
  5982. (** check and return expression
  5983. - if expression has no type then resolve expression
  5984. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5985. - return result
  5986. **)
  5987. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5988. VAR result,prev: SyntaxTree.Expression;
  5989. BEGIN
  5990. IF expression = NIL THEN result := NIL
  5991. ELSIF (expression.type = NIL) THEN
  5992. prev := resolvedExpression;
  5993. resolvedExpression := SyntaxTree.invalidExpression;
  5994. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5995. expression.SetType(SyntaxTree.invalidType);
  5996. END;
  5997. expression.Accept(SELF);
  5998. result := resolvedExpression;
  5999. IF currentIsRealtime THEN
  6000. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6001. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6002. END;
  6003. END;
  6004. (* designator modifiers for backends if they support it ...*)
  6005. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6006. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6007. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6008. END;
  6009. resolvedExpression := prev
  6010. ELSE
  6011. result := expression
  6012. END;
  6013. RETURN result
  6014. END ResolveExpression;
  6015. (**
  6016. check expression to be constant expression
  6017. - resolve expression
  6018. - if valid then check that of value type
  6019. report error and return invalidExpression if anything fails
  6020. **)
  6021. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6022. VAR position: Position;
  6023. BEGIN
  6024. position := expression.position;
  6025. expression := ResolveExpression(expression);
  6026. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6027. ELSIF (expression.resolved = NIL) THEN
  6028. Error(position, "expression is not constant");
  6029. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6030. expression := SyntaxTree.invalidExpression;
  6031. END;
  6032. RETURN expression
  6033. END ConstantExpression;
  6034. (** check expression to be constant integer
  6035. - resolve expresssion
  6036. - if valid then check that of integer value type
  6037. report error and return invalidExpression if anything fails
  6038. **)
  6039. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6040. VAR position: Position;
  6041. BEGIN
  6042. position := expression.position;
  6043. expression := ResolveExpression(expression);
  6044. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6045. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6046. expression := SyntaxTree.invalidExpression;
  6047. Error(position, "expression is not a constant integer");
  6048. END;
  6049. RETURN expression
  6050. END ConstantInteger;
  6051. (** check expression as positive (>=0) constant integer
  6052. - resolve expression
  6053. - if valid then check that integer value
  6054. - if integer value then check that value >= 0
  6055. report error and return invalidExpression if anything fails
  6056. **)
  6057. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6058. VAR position: Position;
  6059. BEGIN
  6060. position := expression.position;
  6061. expression := ConstantExpression(expression);
  6062. IF expression = SyntaxTree.invalidExpression THEN
  6063. (* error already reported *)
  6064. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6065. Error(position, "expression is not integer valued");
  6066. expression := SyntaxTree.invalidExpression
  6067. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6068. Error(position, "integer is not greater or equal zero");
  6069. END;
  6070. RETURN expression
  6071. END ConstantIntegerGeq0;
  6072. (** check expression as condition
  6073. - resolve expression
  6074. - if valid expression then check that result type is boolean
  6075. report error and return invalidExpression if anything fails
  6076. **)
  6077. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6078. VAR position: Position;
  6079. BEGIN
  6080. position := expression.position;
  6081. expression := ResolveExpression(expression);
  6082. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6083. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6084. expression := SyntaxTree.invalidExpression;
  6085. Error(position, "expression is not boolean");
  6086. END;
  6087. RETURN expression
  6088. END ResolveCondition;
  6089. (*** symbols ***)
  6090. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6091. BEGIN
  6092. x.Accept(SELF);
  6093. END ResolveSymbol;
  6094. (** check a symbol
  6095. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6096. **)
  6097. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6098. VAR scope: SyntaxTree.Scope;
  6099. BEGIN
  6100. (* visibility *)
  6101. scope := symbol.scope;
  6102. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6103. scope := scope.outerScope;
  6104. END;
  6105. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6106. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6107. Error(symbol.position,"cannot be exported");
  6108. IF VerboseErrorMessage THEN
  6109. Printout.Info("symbol",symbol);
  6110. END;
  6111. END;
  6112. END;
  6113. END CheckSymbolVisibility;
  6114. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6115. If node is currently being resolved then emit a cyclic definition error.
  6116. Return TRUE only if node is fully resolved.
  6117. **)
  6118. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6119. VAR result: BOOLEAN;
  6120. BEGIN
  6121. IF SyntaxTree.Resolved IN x.state THEN
  6122. result := FALSE
  6123. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6124. Error(x.position,"cyclic definition");
  6125. result := FALSE;
  6126. ELSE
  6127. result := TRUE;
  6128. x.SetState(SyntaxTree.BeingResolved)
  6129. END;
  6130. RETURN result
  6131. END SymbolNeedsResolution;
  6132. (** check and resolve a type declaration symbol = Type
  6133. - set type to declaration type
  6134. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6135. This is so because the type declaration itself does not have a type but it only stands for a type.
  6136. In the implementation of the compiler this made a lot much easier.
  6137. - resolve and set declared type
  6138. - check symbol
  6139. **)
  6140. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6141. VAR prevScope: SyntaxTree.Scope;
  6142. BEGIN
  6143. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6144. IF SymbolNeedsResolution(typeDeclaration) THEN
  6145. typeDeclaration.SetState(SyntaxTree.Resolved);
  6146. prevScope := currentScope;
  6147. currentScope := typeDeclaration.scope;
  6148. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6149. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6150. CheckSymbolVisibility(typeDeclaration);
  6151. typeDeclaration.SetState(SyntaxTree.Resolved);
  6152. currentScope := prevScope;
  6153. END;
  6154. END VisitTypeDeclaration;
  6155. (** check and resolve a constant declaration symbol = (constant) expression
  6156. - check expression
  6157. - set type and value
  6158. - check symbol
  6159. **)
  6160. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6161. VAR
  6162. expression: SyntaxTree.Expression;
  6163. type: SyntaxTree.Type;
  6164. name: Basic.SegmentedName;
  6165. replacement: Replacement;
  6166. BEGIN
  6167. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6168. IF SymbolNeedsResolution(constant) THEN
  6169. expression := constant.value;
  6170. IF replacements # NIL THEN
  6171. Global.GetSymbolSegmentedName(constant, name);
  6172. replacement := replacements;
  6173. WHILE (replacement # NIL) & (replacement.name # name) DO
  6174. replacement := replacement.next;
  6175. END;
  6176. IF replacement # NIL THEN
  6177. InfoSS(constant.position, "replacing constant", constant.name);
  6178. (*
  6179. NEW(stringReader, Strings.Length(replacement.string^));
  6180. stringReader.Set(replacement.string^);
  6181. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6182. NEW(parser, scanner, diagnostics);
  6183. expression := parser.Expression();
  6184. *)
  6185. expression := replacement.expression;
  6186. replacement.used := TRUE;
  6187. END;
  6188. END;
  6189. constant.SetType(SyntaxTree.invalidType);
  6190. expression := ConstantExpression(expression);
  6191. ASSERT(expression.type # NIL);
  6192. type := expression.type.resolved;
  6193. constant.SetType(type);
  6194. constant.SetValue(expression);
  6195. CheckSymbolVisibility(constant);
  6196. constant.SetState(SyntaxTree.Resolved);
  6197. END;
  6198. END VisitConstant;
  6199. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6200. VAR procedureAlignment: LONGINT;
  6201. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6202. (* least common multiple *)
  6203. VAR a,b: LONGINT;
  6204. BEGIN
  6205. a := a0; b := b0;
  6206. WHILE (a # b) DO
  6207. IF a < b THEN a := a+a0
  6208. ELSE b := b + b0
  6209. END;
  6210. END;
  6211. RETURN a
  6212. END LCM;
  6213. BEGIN
  6214. IF alignment > 1 THEN
  6215. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6216. IF (procedureAlignment > 1) THEN
  6217. alignment := LCM(alignment, procedureAlignment);
  6218. END;
  6219. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6220. END;
  6221. END AdaptStackAlignment;
  6222. (** check and resolve a variable / field
  6223. - check and set type
  6224. - negative check on open array type
  6225. - check symbol
  6226. **)
  6227. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6228. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6229. BEGIN
  6230. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6231. IF SymbolNeedsResolution(variable) THEN
  6232. modifiers := variable.modifiers;
  6233. (*
  6234. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6235. variable.AddFlags(flags);
  6236. *)
  6237. variable.SetType(ResolveType(variable.type));
  6238. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6239. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6240. Error(variable.position,"forbidden open array variable");
  6241. END;
  6242. END;
  6243. CheckSymbolVisibility(variable);
  6244. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6245. variable.SetUntraced(TRUE);
  6246. IF ~ContainsPointer(variable.type) THEN
  6247. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6248. Error(position, "untraced flag on non-pointer variable");
  6249. END;
  6250. END;
  6251. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6252. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6253. IF ~PowerOf2(value) THEN
  6254. Error(position, "forbidden alignment - must be power of two");
  6255. ELSE
  6256. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6257. END;
  6258. END;
  6259. variable.SetAlignment(FALSE,value);
  6260. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6261. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6262. Error(position, "fixed position not possible in procedure");
  6263. END;
  6264. variable.SetAlignment(TRUE, value);
  6265. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6266. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6267. Error(position,"fictive offset not possible in procedure");
  6268. END;
  6269. variable.SetFictive(value);
  6270. variable.SetOffset(value*system.dataUnit);
  6271. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6272. END;
  6273. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6274. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6275. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6276. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6277. END;
  6278. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6279. IF variable.initializer # NIL THEN
  6280. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6281. END;
  6282. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6283. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6284. pointerType.SetPointerBase(variable.type);
  6285. pointerType.SetHidden(TRUE);
  6286. variable.SetType(ResolveType(pointerType));
  6287. END;
  6288. variable.SetState(SyntaxTree.Resolved);
  6289. END;
  6290. END VisitVariable;
  6291. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6292. BEGIN
  6293. VisitVariable(property)
  6294. END VisitProperty;
  6295. (** check and resolve a (procedure) parameter
  6296. - check and set type
  6297. - check symbol
  6298. - check parameter kind and set read-only flags if appropriate
  6299. **)
  6300. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6301. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6302. BEGIN
  6303. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6304. IF SymbolNeedsResolution(parameter) THEN
  6305. modifiers := parameter.modifiers;
  6306. parameter.SetType(ResolveType(parameter.type));
  6307. ASSERT(parameter.type.resolved # NIL);
  6308. CheckSymbolVisibility(parameter);
  6309. IF parameter.defaultValue # NIL THEN
  6310. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6311. Error(parameter.position,"forbidden default value on non-value parameter");
  6312. ELSE
  6313. expression := ConstantExpression(parameter.defaultValue);
  6314. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6315. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6316. parameter.SetDefaultValue(expression);
  6317. END;
  6318. END;
  6319. END;
  6320. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6321. parameter.SetUntraced(TRUE);
  6322. IF ~ContainsPointer(parameter.type) THEN
  6323. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6324. Error(position, "untraced flag on non-pointer variable");
  6325. END;
  6326. END;
  6327. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6328. parameter.SetMoveable(TRUE);
  6329. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6330. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6331. Error(position, "illegal movable flag on non-address variable");
  6332. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6333. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6334. Error(position, "unnecessary movable flag on variable variable");
  6335. END;
  6336. END;
  6337. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6338. parameter.SetState(SyntaxTree.Resolved);
  6339. END;
  6340. END VisitParameter;
  6341. (** check and resolve a procedure (with declaration and implementation scope)
  6342. - check the procedure type
  6343. - check if method (i.e. in record scope), if so then
  6344. - check if (unique) constructor
  6345. - check if (unique) finalizer
  6346. - check if super method available, if so then check signature
  6347. - of not in record scope then negative check on constructor flag
  6348. - of not in record scope then negative check on finalizer flag
  6349. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6350. - check procedure symbol
  6351. **)
  6352. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6353. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6354. procedureType: SyntaxTree.ProcedureType;
  6355. type: SyntaxTree.Type;
  6356. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6357. qualifiedType: SyntaxTree.QualifiedType;
  6358. value: LONGINT;
  6359. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6360. position: Position;
  6361. fp: SyntaxTree.FingerPrint;
  6362. BEGIN
  6363. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6364. IF IsOberonInline(procedure) THEN
  6365. IF SyntaxTree.Public * procedure.access # {} THEN
  6366. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6367. END;
  6368. procedure.SetInline(FALSE);
  6369. procedure.SetOberonInline(TRUE);
  6370. END;
  6371. IF SymbolNeedsResolution(procedure) THEN
  6372. recentIsRealtime := currentIsRealtime;
  6373. recentIsBodyProcedure := currentIsBodyProcedure;
  6374. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6375. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6376. modifiers := procedureType.modifiers;
  6377. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6378. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6379. IF useDarwinCCalls THEN (*fld*)
  6380. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6381. ELSE
  6382. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6383. END
  6384. END;
  6385. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6386. procedureType.SetInterrupt(TRUE);
  6387. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6388. END;
  6389. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6390. procedureType.SetNoReturn(TRUE);
  6391. END;
  6392. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6393. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6394. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6395. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6396. END;
  6397. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6398. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6399. END;
  6400. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6401. IF ~PowerOf2(value) THEN
  6402. Error(position, "forbidden stack alignment - must be power of two");
  6403. ELSE
  6404. procedureType.SetStackAlignment(value)
  6405. END;
  6406. END;
  6407. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6408. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6409. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6410. END;
  6411. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6412. SyntaxTree.InitFingerPrint(fp);
  6413. fp.shallow := value;
  6414. fp.shallowAvailable := TRUE;
  6415. procedure.SetFingerPrint(fp);
  6416. END;
  6417. CheckModifiers(modifiers, TRUE);
  6418. modifiers := procedureType.returnTypeModifiers;
  6419. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6420. CheckModifiers(modifiers, TRUE);
  6421. procedure.SetState(SyntaxTree.Resolved);
  6422. FixProcedureType(procedureType);
  6423. currentIsRealtime := procedureType.isRealtime;
  6424. currentIsBodyProcedure := procedure.isBodyProcedure;
  6425. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6426. THEN
  6427. Error(procedure.position,"problems during parameter offset computation");
  6428. END;
  6429. CheckSymbolVisibility(procedure);
  6430. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6431. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6432. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6433. Error(procedure.position,"problem during parameter offset generation");
  6434. END;
  6435. END;
  6436. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6437. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6438. procedureType.SetDelegate(TRUE);
  6439. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6440. (* add auto-self *)
  6441. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6442. IF (record.pointerType.typeDeclaration = NIL) THEN
  6443. selfParameter.SetType(record.pointerType);
  6444. ELSE
  6445. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6446. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6447. qualifiedType.SetResolved(record.pointerType);
  6448. selfParameter.SetType(qualifiedType);
  6449. END;
  6450. selfParameter.SetAccess(SyntaxTree.Hidden);
  6451. END;
  6452. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6453. IF procedure.isConstructor THEN
  6454. (*! constructor is always visible, compatibility to paco
  6455. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6456. *)
  6457. procedure.MarkUsed;
  6458. IF procedureType.returnType # NIL THEN
  6459. Error(procedure.position,"constructor with forbidden return type");
  6460. END;
  6461. proc := procedure.scope.firstProcedure;
  6462. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6463. proc := proc.nextProcedure;
  6464. END;
  6465. IF proc # NIL THEN
  6466. Error(procedure.position,"duplicate constructor")
  6467. ELSE
  6468. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6469. END;
  6470. END;
  6471. IF procedure.isFinalizer THEN
  6472. procedure.MarkUsed;
  6473. IF procedureType.returnType # NIL THEN
  6474. Error(procedure.position,"finalizer with forbidden return type");
  6475. END;
  6476. IF procedureType.numberParameters # 0 THEN
  6477. Error(procedure.position,"finalizer with formal parameters");
  6478. END;
  6479. proc := procedure.scope.firstProcedure;
  6480. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6481. proc := proc.nextProcedure;
  6482. END;
  6483. IF proc # NIL THEN
  6484. Error(procedure.position,"duplicate finalizer")
  6485. ELSE
  6486. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6487. END;
  6488. END;
  6489. super := FindSuperProcedure(record.recordScope, procedure);
  6490. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6491. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6492. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6493. END;
  6494. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6495. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6496. END;
  6497. IF super.isFinal THEN
  6498. Error(procedure.position,"forbidden method extending final method");
  6499. END;
  6500. procedure.SetSuper(super);
  6501. super.SetOverwritten(TRUE);
  6502. procedure.SetAccess(procedure.access+super.access);
  6503. procedure.MarkUsed;
  6504. END;
  6505. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6506. THEN
  6507. Error(procedure.position,"problems during parameter offset computation");
  6508. END;
  6509. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6510. IF cellsAreObjects THEN
  6511. procedureType.SetDelegate(TRUE);
  6512. END;
  6513. IF procedure.isConstructor THEN
  6514. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6515. END;
  6516. ELSIF procedure.isConstructor THEN
  6517. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6518. END;
  6519. Declarations(procedure.procedureScope, FALSE, {0,1});
  6520. (* body resolution part done as late fix of the procedure type *)
  6521. procedure.SetState(SyntaxTree.Resolved);
  6522. currentIsRealtime := recentIsRealtime;
  6523. currentIsBodyProcedure := recentIsBodyProcedure;
  6524. END;
  6525. END VisitProcedure;
  6526. (**
  6527. a builtin procedure is a global item that may not be modified locally
  6528. instead the resolving of builtin procedure calls are done in the esignator
  6529. **)
  6530. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6531. VAR type: SyntaxTree.Type;
  6532. BEGIN
  6533. type := ResolveType(builtinProcedure.type);
  6534. END VisitBuiltin;
  6535. (* nopov *)
  6536. (** check and resolve operator
  6537. - operators are first checked as procedures
  6538. - then additional operator-specific checks are done
  6539. - note that only module-scope operators are checked here
  6540. (operators in a record scope are only allowed in the context of
  6541. array-structured object types and checked in 'ResolveArrayStructure')
  6542. - also note that inter-operator conformity is not checked here
  6543. **)
  6544. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6545. VAR
  6546. procedureType: SyntaxTree.ProcedureType;
  6547. leftType, rightType: SyntaxTree.Type;
  6548. identifierNumber: LONGINT; position: Position;
  6549. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6550. modifiers: SyntaxTree.Modifier;
  6551. (** whether a type is locally defined in the current module scope
  6552. for arrays, the base type must be locally defined **)
  6553. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6554. BEGIN
  6555. IF type = NIL THEN
  6556. RETURN FALSE
  6557. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6558. RETURN TRUE
  6559. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6560. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6561. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6562. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6563. ELSE
  6564. RETURN FALSE
  6565. END
  6566. END IsLocallyDefined;
  6567. BEGIN
  6568. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6569. procedureType := operator.type(SyntaxTree.ProcedureType);
  6570. modifiers := procedureType.modifiers;
  6571. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6572. CheckModifiers(modifiers, TRUE);
  6573. VisitProcedure(operator);
  6574. IF operator.scope IS SyntaxTree.RecordScope THEN
  6575. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6576. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6577. IF identifierNumber = -1 THEN
  6578. Error(operator.position, "operator with unknown identifier")
  6579. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6580. Error(operator.position, "identifier may not be used for operator")
  6581. ELSE
  6582. IF procedureType.numberParameters < 1 THEN
  6583. Error(operator.position, "operator without operand");
  6584. ELSIF procedureType.numberParameters > 2 THEN
  6585. Error(operator.position, "operator with more than two operands");
  6586. ELSE
  6587. (* determine operand types *)
  6588. leftType := procedureType.firstParameter.type;
  6589. IF procedureType.numberParameters > 1 THEN
  6590. rightType := procedureType.firstParameter.nextParameter.type
  6591. ELSE
  6592. rightType := NIL
  6593. END;
  6594. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6595. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6596. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6597. Error(operator.position, "none of the operands is declared in the same module")
  6598. END
  6599. END;
  6600. (* TODO: refine the checks, think about how restrictive the checks should be
  6601. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6602. They might be used for intersection, union, complement of custom object types *)
  6603. (* defaults *)
  6604. hasReturnType := TRUE;
  6605. mustBeUnary := FALSE;
  6606. mustBeBinary := FALSE;
  6607. mustReturnBoolean := FALSE;
  6608. mustReturnInteger := FALSE;
  6609. mustHaveEquitypedOperands := FALSE;
  6610. (* operator-specific exceptions *)
  6611. CASE identifierNumber OF
  6612. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6613. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6614. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6615. mustBeBinary := TRUE
  6616. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6617. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6618. | Scanner.Times: mustBeBinary := TRUE
  6619. | Scanner.TimesTimes: mustBeBinary := TRUE
  6620. | Scanner.DotTimes: mustBeBinary := TRUE
  6621. | Scanner.PlusTimes: mustBeBinary := TRUE
  6622. | Scanner.Slash: mustBeBinary := TRUE
  6623. | Scanner.Backslash: mustBeBinary := TRUE
  6624. | Scanner.DotSlash: mustBeBinary := TRUE
  6625. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6626. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6627. | Scanner.Not: mustBeUnary := TRUE
  6628. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6629. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6630. | Scanner.Transpose: mustBeUnary := TRUE;
  6631. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6632. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6633. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6634. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6635. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6636. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6637. | Global.Abs: mustBeUnary := TRUE;
  6638. | Global.Ash: (* TODO: arity? *)
  6639. | Global.Cap: (* TODO: arity? *)
  6640. | Global.Chr: mustBeUnary := TRUE;
  6641. | Global.Entier: (* TODO: arity? *)
  6642. | Global.EntierH: (* TODO: arity? *)
  6643. | Global.Len: (* unary and binary *)
  6644. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6645. | Global.Max, Global.Min: (* unary and binary *)
  6646. | Global.Odd: (* TODO: arity? *)
  6647. | Global.Sum: (* TODO: arity? *)
  6648. | Global.All: (* TODO: arity? *)
  6649. | Global.Re, Global.Im:
  6650. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6651. | Scanner.Alias:
  6652. | Scanner.GreaterGreater, Scanner.LessLess:
  6653. mustBeBinary := TRUE; hasReturnType := FALSE;
  6654. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6655. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6656. END;
  6657. (* check parameter count *)
  6658. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6659. Error(operator.position,"operator is not unary")
  6660. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6661. Error(operator.position,"operator is not binary")
  6662. END;
  6663. (* check parameter types *)
  6664. (* TODO: is this used at all? *)
  6665. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6666. leftType := procedureType.firstParameter.type;
  6667. rightType := procedureType.firstParameter.nextParameter.type;
  6668. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6669. Error(operator.position, "the two operands are not of the same type")
  6670. END
  6671. END;
  6672. (* check return type *)
  6673. IF hasReturnType THEN
  6674. IF procedureType.returnType = NIL THEN
  6675. Error(operator.position, "return type required")
  6676. ELSIF mustReturnBoolean THEN
  6677. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6678. Error(operator.position,"return type is not Boolean")
  6679. END
  6680. ELSIF mustReturnInteger THEN
  6681. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6682. Error(operator.position,"return type is not integer")
  6683. END
  6684. END
  6685. ELSIF procedureType.returnType # NIL THEN
  6686. Error(operator.position, "return type not allowed")
  6687. END
  6688. END
  6689. END
  6690. END
  6691. END VisitOperator;
  6692. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6693. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6694. BEGIN
  6695. IF error THEN RETURN FALSE END;
  6696. prevScope := currentScope;
  6697. prevDiagnostics := diagnostics;
  6698. diagnostics := NIL; (* suppress error output *)
  6699. currentScope := module.moduleScope;
  6700. VisitImport(x);
  6701. IF ~error THEN
  6702. module.moduleScope.AddImport(x);
  6703. x.SetScope(module.moduleScope);
  6704. END;
  6705. currentScope := prevScope;
  6706. diagnostics := prevDiagnostics;
  6707. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6708. END AddImport;
  6709. (** check and resolve import
  6710. - check for name = SYSTEM
  6711. - check for forbidden self import
  6712. - search through global import cache: already imported?
  6713. - check if already imported indirectly
  6714. - import if necessary -> set module and enter into import cache
  6715. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6716. - after this import this direct import and all indirect imports are stored in the current module's import list
  6717. **)
  6718. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6719. VAR
  6720. module: SyntaxTree.Module;
  6721. moduleScope: SyntaxTree.ModuleScope;
  6722. import,reimport: SyntaxTree.Import;
  6723. filename: FileName;
  6724. prevScope: SyntaxTree.Scope;
  6725. BEGIN
  6726. IF SymbolNeedsResolution(x) THEN
  6727. prevScope := currentScope;
  6728. x.SetType(SyntaxTree.importType);
  6729. moduleScope := currentScope.ownerModule.moduleScope;
  6730. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6731. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6732. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6733. Error(x.position,"forbidden self import");
  6734. ELSE
  6735. (* search through global import list: already imported ? *)
  6736. IF (x.module = NIL) & (importCache # NIL) THEN
  6737. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6738. ELSE import := NIL
  6739. END;
  6740. IF x.module # NIL THEN (* already imported indirectly *)
  6741. module := x.module;
  6742. ELSIF import # NIL THEN (* already in module list *)
  6743. module := import.module;
  6744. ASSERT(module # NIL);
  6745. x.SetModule(module);
  6746. ELSE (* must be imported *)
  6747. Global.ModuleFileName(x.moduleName,x.context,filename);
  6748. IF symbolFileFormat # NIL THEN
  6749. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6750. IF module = NIL THEN
  6751. ErrorSS(x.position,"could not import",filename);
  6752. IF VerboseErrorMessage THEN
  6753. Printout.Info("import",x)
  6754. END
  6755. ELSE
  6756. (*
  6757. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6758. (*! should rather be done by importer *)
  6759. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6760. checker.importCache := importCache;
  6761. checker.arrayBaseImported := arrayBaseImported;
  6762. checker.global := global;
  6763. checker.Module(module); (* semantic check *)
  6764. error := error OR checker.error;
  6765. END;
  6766. *)
  6767. (*
  6768. ASSERT(SyntaxTree.Resolved IN module.state);
  6769. *)
  6770. x.SetModule(module);
  6771. IF importCache # NIL THEN
  6772. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6773. import.SetContext(x.context);
  6774. import.SetModule(module);
  6775. importCache.AddImport(import);
  6776. END;
  6777. END;
  6778. ELSE
  6779. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6780. END;
  6781. END;
  6782. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6783. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6784. END;
  6785. import := module.moduleScope.firstImport;
  6786. WHILE(import # NIL) DO
  6787. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6788. ASSERT(currentScope # NIL);
  6789. ASSERT(currentScope.ownerModule # NIL);
  6790. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6791. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6792. Error(x.position,"recursive import");
  6793. ELSE
  6794. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6795. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6796. IF reimport = NIL THEN (* indirect import *)
  6797. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6798. reimport.SetContext(import.context);
  6799. reimport.SetModule(import.module);
  6800. moduleScope.AddImport(reimport);
  6801. reimport.SetScope(moduleScope);
  6802. ELSE
  6803. ASSERT(import.module # NIL);
  6804. reimport.SetModule(import.module); (* direct or indirect import *)
  6805. END;
  6806. END;
  6807. import := import.nextImport;
  6808. END;
  6809. END;
  6810. END;
  6811. currentScope := prevScope;
  6812. (* ELSE nothing to be done *)
  6813. x.SetState(SyntaxTree.Resolved);
  6814. END;
  6815. END VisitImport;
  6816. (*** statements ***)
  6817. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6818. VAR prev,resolved: SyntaxTree.Statement;
  6819. BEGIN
  6820. prev := resolvedStatement;
  6821. resolvedStatement := x;
  6822. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6823. activeCellsStatement := FALSE;
  6824. x.Accept(SELF);
  6825. (* removed this, implementation restriction should be resolved by backend
  6826. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6827. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6828. END;
  6829. *)
  6830. resolved := resolvedStatement;
  6831. resolvedStatement := prev;
  6832. RETURN resolved
  6833. END ResolveStatement;
  6834. (** check and resolve statement sequence
  6835. - check all statements, replace if necessary
  6836. **)
  6837. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6838. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6839. BEGIN
  6840. IF statementSequence # NIL THEN (* else empty *)
  6841. FOR i := 0 TO statementSequence.Length()-1 DO
  6842. statement := statementSequence.GetStatement(i);
  6843. resolved := ResolveStatement(statement);
  6844. IF (resolved # statement) THEN
  6845. statementSequence.SetStatement(i,resolved);
  6846. END;
  6847. END;
  6848. END;
  6849. END StatementSequence;
  6850. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6851. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6852. - check if procedure is callable
  6853. - check return type = NIL (otherwise must be assignment statement)
  6854. **)
  6855. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6856. VAR call: SyntaxTree.Designator;
  6857. BEGIN
  6858. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6859. call := procedureCall.call;
  6860. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6861. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6862. END;
  6863. call := ResolveDesignator(call);
  6864. IF call = SyntaxTree.invalidDesignator THEN
  6865. (* error already handled *)
  6866. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6867. (* inline call in a statement *)
  6868. ELSIF ~IsCallable(call) THEN
  6869. Error(procedureCall.position,"called object is not a procedure");
  6870. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6871. Error(procedureCall.position,"calling procedure with non-void return type");
  6872. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6873. END;
  6874. procedureCall.SetCall(call);
  6875. (*
  6876. IF call = SyntaxTree.invalidDesignator THEN
  6877. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6878. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6879. IF IsOberonInline(procedure) THEN
  6880. Warning(procedure.position,"call to inline proc");
  6881. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6882. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6883. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6884. resolvedStatement := block;
  6885. RETURN;
  6886. END;
  6887. END;
  6888. *)
  6889. END VisitProcedureCallStatement;
  6890. (** check and resolve assignment LHS := RHS
  6891. - resolve LHS and RHS
  6892. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6893. - check if assignment is compatible
  6894. - check if LHS is variable (i.e. assignable)
  6895. - convert RHS if necessary
  6896. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6897. - assignment between different ASOTs
  6898. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6899. - assignment to ASOT elements:
  6900. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6901. **)
  6902. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6903. VAR
  6904. left: SyntaxTree.Designator;
  6905. right, expression: SyntaxTree.Expression;
  6906. designator: SyntaxTree.Designator;
  6907. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6908. mathArrayType: SyntaxTree.MathArrayType;
  6909. BEGIN
  6910. right := ResolveExpression(assignment.right);
  6911. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6912. left := ResolveDesignator(assignment.left);
  6913. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6914. (* error already handled *)
  6915. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6916. (* LHS is index write operator call on ASOT *)
  6917. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6918. (* necessary ?
  6919. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6920. type := procedureType.firstParameter.type;
  6921. expression := procedureCallDesignator.parameters.GetExpression(0);
  6922. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6923. *)
  6924. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6925. ELSIF CheckVariable(left) THEN
  6926. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6927. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6928. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6929. (* conversion done by procedure call
  6930. (* try to convert to left argument *)
  6931. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6932. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6933. procedureCallDesignator.parameters.SetExpression(1, right);
  6934. END;
  6935. *)
  6936. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6937. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6938. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6939. ELSIF AssignmentCompatible(left, right) THEN
  6940. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6941. mathArrayType := MathArrayStructureOfType(left.type);
  6942. right := NewConversion(right.position, right, mathArrayType, NIL);
  6943. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6944. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6945. ELSE
  6946. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6947. assignment.SetLeft(left);
  6948. assignment.SetRight(right);
  6949. resolvedStatement := assignment
  6950. END
  6951. END
  6952. END
  6953. END VisitAssignment;
  6954. (** check and resolve assignment LHS := RHS
  6955. - resolve LHS and RHS
  6956. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6957. - check if assignment is compatible
  6958. - check if LHS is variable (i.e. assignable)
  6959. - convert RHS if necessary
  6960. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6961. - assignment between different ASOTs
  6962. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6963. - assignment to ASOT elements:
  6964. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6965. **)
  6966. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6967. VAR
  6968. left: SyntaxTree.Designator;
  6969. right: SyntaxTree.Expression;
  6970. inPort, outPort: SyntaxTree.PortType;
  6971. expression: SyntaxTree.Expression;
  6972. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6973. BEGIN
  6974. right := ResolveExpression(communication.right);
  6975. left := ResolveDesignator(communication.left);
  6976. communication.SetLeft(left);
  6977. communication.SetRight(right);
  6978. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6979. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6980. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6981. (* conversion done by procedure call
  6982. (* try to convert to left argument *)
  6983. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6984. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6985. procedureCallDesignator.parameters.SetExpression(1, right);
  6986. END;
  6987. *)
  6988. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6989. ELSE
  6990. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6991. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6992. (* error already handled *)
  6993. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6994. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6995. IF outPort.direction # SyntaxTree.OutPort THEN
  6996. Error(left.position,"not an out-port")
  6997. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6998. Error(left.position,"incompatible to port type");
  6999. ELSE
  7000. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7001. communication.SetRight(right)
  7002. END;
  7003. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7004. IF CheckVariable(left) THEN
  7005. IF inPort.direction # SyntaxTree.InPort THEN
  7006. Error(left.position,"not an in-port")
  7007. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7008. Error(right.position,"incompatible to port type");
  7009. END;
  7010. END;
  7011. ELSE
  7012. Error(communication.position,"unsupported stream operation");
  7013. END;
  7014. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7015. IF outPort.direction # SyntaxTree.OutPort THEN
  7016. Error(left.position,"not an out-port")
  7017. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7018. Error(left.position,"incompatible to port type");
  7019. ELSE
  7020. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7021. communication.SetRight(right)
  7022. END;
  7023. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7024. IF CheckVariable(right) THEN
  7025. IF inPort.direction # SyntaxTree.InPort THEN
  7026. Error(left.position,"not an in-port")
  7027. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7028. Error(right.position,"incompatible to port type");
  7029. END;
  7030. END;
  7031. ELSE
  7032. Error(communication.position, "unsupported operation");
  7033. END;
  7034. END;
  7035. END VisitCommunicationStatement;
  7036. (** check and resolve if/eslif part
  7037. - check condition
  7038. - check statement sequence
  7039. **)
  7040. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7041. VAR prevUnreachable, b: BOOLEAN;
  7042. BEGIN
  7043. prevUnreachable := currentIsUnreachable;
  7044. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7045. IF IsBooleanValue(ifPart.condition,b) THEN
  7046. IF b=FALSE THEN
  7047. currentIsUnreachable := TRUE
  7048. ELSIF b=TRUE THEN
  7049. true := TRUE
  7050. END;
  7051. END;
  7052. StatementSequence(ifPart.statements);
  7053. currentIsUnreachable := prevUnreachable;
  7054. END IfPart;
  7055. (** check and resolve if statement
  7056. - check if parts and else part statement sequence
  7057. **)
  7058. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7059. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7060. BEGIN
  7061. prevUnreachable := currentIsUnreachable;
  7062. ifPartTrue := FALSE;
  7063. IfPart(ifStatement.ifPart,ifPartTrue);
  7064. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7065. elsif := ifStatement.GetElsifPart(i);
  7066. IfPart(elsif,ifPartTrue);
  7067. END;
  7068. IF ifStatement.elsePart # NIL THEN
  7069. IF ifPartTrue THEN
  7070. currentIsUnreachable := TRUE
  7071. END;
  7072. StatementSequence(ifStatement.elsePart)
  7073. END;
  7074. currentIsUnreachable := prevUnreachable;
  7075. END VisitIfStatement;
  7076. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7077. VAR variable: SyntaxTree.Designator;
  7078. type,variableType: SyntaxTree.Type;
  7079. withEntry: WithEntry;
  7080. BEGIN
  7081. variable := ResolveDesignator(withPart.variable);
  7082. variableType := variable.type.resolved;
  7083. withPart.SetVariable(variable);
  7084. type := ResolveType(withPart.type);
  7085. withPart.SetType(type);
  7086. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7087. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7088. END;
  7089. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7090. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7091. Error(variable.position,"is not extensible designator");
  7092. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7093. Error(variable.position,"is no local variable ");
  7094. IF VerboseErrorMessage THEN
  7095. Printout.Info("variable",variable)
  7096. END;
  7097. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7098. Error(variable.position,"withguarded symbol is no type extension of ");
  7099. IF VerboseErrorMessage THEN
  7100. Printout.Info("variable",variable);
  7101. Printout.Info("type",type);
  7102. END;
  7103. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7104. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7105. Error(variable.position,"withguarded symbol is no variable ");
  7106. IF VerboseErrorMessage THEN
  7107. Printout.Info("variable",variable);
  7108. Printout.Info("type",type);
  7109. END;
  7110. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7111. Error(variable.position,"invalid change of withguarded symbol");
  7112. ELSE
  7113. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7114. NEW(withEntry);
  7115. withEntry.previous := withEntries;
  7116. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7117. withEntry.type := type;
  7118. withEntries := withEntry;
  7119. StatementSequence(withPart.statements);
  7120. withEntries := withEntries.previous;
  7121. END;
  7122. END WithPart;
  7123. (** check and resolve with statement WITH variable: type DO ... END;
  7124. - check type and variable
  7125. - check that variable type is type extension of type
  7126. - check that variable is a variable
  7127. - enter new with scope and enter guardedVariable with same name and reference to variable
  7128. - create if statement:
  7129. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7130. **)
  7131. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7132. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7133. BEGIN
  7134. prevScope := currentScope; symbol := NIL;
  7135. FOR i := 0 TO withStatement.WithParts()-1 DO
  7136. WithPart(withStatement.GetWithPart(i),symbol);
  7137. END;
  7138. IF withStatement.elsePart # NIL THEN
  7139. StatementSequence(withStatement.elsePart)
  7140. END;
  7141. currentScope := prevScope;
  7142. END VisitWithStatement;
  7143. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7144. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7145. - check 'first' < 'last' and no overlaps between different case labels
  7146. - check statement sequence
  7147. **)
  7148. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7149. VAR
  7150. i: LONGINT;
  7151. position: Position;
  7152. expression, left, right: SyntaxTree.Expression;
  7153. expressionType: SyntaxTree.Type;
  7154. l, r: LONGINT;
  7155. cl, cr: CHAR;
  7156. thiscases: SyntaxTree.CaseConstant;
  7157. BEGIN
  7158. thiscases := NIL;
  7159. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7160. expression := casePart.elements.GetExpression(i);
  7161. position := expression.position;
  7162. (* set context of range *)
  7163. IF expression IS SyntaxTree.RangeExpression THEN
  7164. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7165. END;
  7166. expression := ResolveExpression(expression);
  7167. IF expression = SyntaxTree.invalidExpression THEN
  7168. (* error already reported *)
  7169. expressionType := SyntaxTree.invalidType;
  7170. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7171. (* read out 'first' and 'last' *)
  7172. left := expression(SyntaxTree.RangeExpression).first;
  7173. right := expression(SyntaxTree.RangeExpression).last;
  7174. (* guaranteed by VisitRangeExpression: *)
  7175. ASSERT((left # NIL) & (right # NIL));
  7176. ASSERT(left.type.resolved = right.type.resolved);
  7177. left := CompatibleConversion(left.position, left, type);
  7178. right := CompatibleConversion(right.position, right, type);
  7179. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7180. expression(SyntaxTree.RangeExpression).SetLast(right);
  7181. expressionType := RegularType(position,left.type);
  7182. ELSE
  7183. expression := ConstantExpression(expression);
  7184. expression := CompatibleConversion(expression.position, expression, type);
  7185. (*
  7186. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7187. left := Global.NewCharacterValue(system,expression.position,cl);
  7188. expression := casePart.elements.GetExpression(i);
  7189. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7190. expression := left
  7191. END;
  7192. *)
  7193. casePart.elements.SetExpression(i,expression);
  7194. left := expression; right := expression;
  7195. expressionType := RegularType(position,expression.type)
  7196. END;
  7197. IF (expressionType = SyntaxTree.invalidType) THEN
  7198. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7199. Error(position, "inadmissible case label");
  7200. expression := SyntaxTree.invalidExpression;
  7201. ELSE
  7202. l := 0; r := 0;
  7203. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7204. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7205. l := ORD(cl); r := ORD(cr);
  7206. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7207. ELSE
  7208. expression := SyntaxTree.invalidExpression
  7209. END;
  7210. IF expression # SyntaxTree.invalidExpression THEN
  7211. IF l>r THEN
  7212. Error(position, "empty case label")
  7213. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7214. Error(position, "duplicate case label");
  7215. ELSE
  7216. IF l < min THEN min := l END;
  7217. IF r > max THEN max := r END;
  7218. END;
  7219. END;
  7220. END;
  7221. casePart.elements.SetExpression(i,expression);
  7222. END;
  7223. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7224. casePart.SetConstants(thiscases);
  7225. StatementSequence(casePart.statements);
  7226. END CasePart;
  7227. (** check and resolve case statement CASE variable OF ... END;
  7228. - check variable
  7229. - check case parts
  7230. **)
  7231. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7232. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7233. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7234. BEGIN
  7235. expression := ResolveExpression(caseStatement.variable);
  7236. type := RegularType(expression.position,expression.type);
  7237. IF type = SyntaxTree.invalidType THEN
  7238. expression := SyntaxTree.invalidExpression;
  7239. ELSIF IsIntegerType(type) THEN
  7240. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7241. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7242. (*
  7243. expression := Global.NewCharacterValue(system,expression.position,ch);
  7244. *)
  7245. type := expression.type;
  7246. ELSIF IsCharacterType(type) THEN
  7247. ELSIF IsEnumerationType(type) THEN
  7248. ELSE
  7249. Error(caseStatement.variable.position,"variable must be integer or character type");
  7250. expression := SyntaxTree.invalidExpression;
  7251. END;
  7252. caseStatement.SetVariable(expression);
  7253. caseList := NIL;
  7254. min := MAX(LONGINT); max := MIN(LONGINT);
  7255. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7256. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7257. END;
  7258. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7259. msg := "huge sparse case table ";
  7260. Strings.AppendInt(msg, max-min);
  7261. Strings.Append(msg,"/");
  7262. Strings.AppendInt(msg, caseStatement.CaseParts());
  7263. Warning(caseStatement.position,msg);
  7264. END;
  7265. caseStatement.SetMinMax(min,max);
  7266. StatementSequence(caseStatement.elsePart);
  7267. IF expression.resolved # NIL THEN
  7268. IF IsCharacterValue(expression,ch) THEN
  7269. l := ORD(ch)
  7270. ELSIF IsIntegerValue(expression,l) THEN
  7271. END;
  7272. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7273. END;
  7274. END VisitCaseStatement;
  7275. (** check and resolve while statement
  7276. - check condition
  7277. - check statement sequence
  7278. **)
  7279. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7280. VAR prevIsUnreachable,b: BOOLEAN;
  7281. BEGIN
  7282. prevIsUnreachable := currentIsUnreachable;
  7283. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7284. IF IsBooleanValue(whileStatement.condition,b) THEN
  7285. IF b=FALSE THEN
  7286. currentIsUnreachable := TRUE
  7287. END;
  7288. END;
  7289. StatementSequence(whileStatement.statements);
  7290. currentIsUnreachable := prevIsUnreachable
  7291. END VisitWhileStatement;
  7292. (** check and resolve repeat statement
  7293. - check condition
  7294. - check statement sequence
  7295. **)
  7296. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7297. BEGIN
  7298. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7299. StatementSequence(repeatStatement.statements);
  7300. END VisitRepeatStatement;
  7301. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7302. VAR withEntry: WithEntry;
  7303. BEGIN
  7304. withEntry := withEntries;
  7305. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7306. withEntry := withEntry.previous
  7307. END;
  7308. IF withEntry = NIL THEN RETURN FALSE
  7309. ELSE
  7310. type := withEntry.type;
  7311. RETURN TRUE
  7312. END;
  7313. END GetGuard;
  7314. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7315. - check that variable is an integer variable
  7316. - check that from is integer typed with compatible type
  7317. - check that to has compatible type
  7318. - check that by is constant integer with compatible type
  7319. **)
  7320. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7321. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7322. BEGIN
  7323. designator := ResolveDesignator(forStatement.variable);
  7324. type := SyntaxTree.invalidType;
  7325. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7326. designator := SyntaxTree.invalidDesignator;
  7327. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7328. Error(designator.position,"control variable of non-integer type");
  7329. designator := SyntaxTree.invalidDesignator;
  7330. ELSIF CheckVariable(designator) THEN
  7331. type := designator.type;
  7332. END;
  7333. forStatement.SetVariable(designator);
  7334. expression := ResolveExpression(forStatement.from);
  7335. IF expression = SyntaxTree.invalidExpression THEN
  7336. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7337. Error(expression.position,"start value of incompatible type");
  7338. expression := SyntaxTree.invalidExpression;
  7339. ELSIF type # SyntaxTree.invalidType THEN
  7340. expression := NewConversion(expression.position,expression,type,NIL)
  7341. END;
  7342. forStatement.SetFrom(expression);
  7343. expression := ResolveExpression(forStatement.to);
  7344. IF expression = SyntaxTree.invalidExpression THEN
  7345. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7346. Error(expression.position,"end value of incompatible type");
  7347. expression := SyntaxTree.invalidExpression;
  7348. ELSIF type # SyntaxTree.invalidType THEN
  7349. expression := NewConversion(expression.position,expression,type,NIL)
  7350. END;
  7351. forStatement.SetTo(expression);
  7352. IF forStatement.by # NIL THEN
  7353. expression := ConstantInteger(forStatement.by);
  7354. ELSE
  7355. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7356. END;
  7357. IF expression = SyntaxTree.invalidExpression THEN
  7358. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7359. Error(expression.position,"step value of incompatible type");
  7360. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7361. Error(expression.position,"invalid step value");
  7362. ELSIF type # SyntaxTree.invalidType THEN
  7363. expression := NewConversion(expression.position,expression,type,NIL)
  7364. END;
  7365. forStatement.SetBy(expression);
  7366. StatementSequence(forStatement.statements);
  7367. END VisitForStatement;
  7368. (** check and resolve loop statement LOOP StatementSequence END
  7369. - check statement sequence
  7370. **)
  7371. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7372. BEGIN
  7373. StatementSequence(loopStatement.statements)
  7374. END VisitLoopStatement;
  7375. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7376. BEGIN
  7377. StatementSequence(exitableBlock.statements);
  7378. END VisitExitableBlock;
  7379. (** check and resolve exit statement EXIT
  7380. - check that exit is within LOOP statement block
  7381. **)
  7382. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7383. VAR outer: SyntaxTree.Statement;
  7384. BEGIN
  7385. outer := exitStatement.outer;
  7386. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7387. outer := outer.outer;
  7388. END;
  7389. IF outer = NIL THEN
  7390. Error(exitStatement.position,"exit statement not within loop statement");
  7391. END;
  7392. END VisitExitStatement;
  7393. (** check and resolve return statement RETURN [expression]
  7394. - check expression (if any)
  7395. - check if in procedure scope
  7396. - if in procedure scope then check expression compatibility
  7397. - if not in procecdure scope then check on return without expression
  7398. **)
  7399. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7400. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7401. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7402. BEGIN
  7403. position := returnStatement.position;
  7404. expression := returnStatement.returnValue;
  7405. IF expression # NIL THEN
  7406. expression := ResolveExpression(expression);
  7407. returnStatement.SetReturnValue(expression);
  7408. END;
  7409. outer := returnStatement.outer;
  7410. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7411. outer := outer.outer
  7412. END;
  7413. IF (outer # NIL) THEN
  7414. scope := outer(SyntaxTree.Body).inScope;
  7415. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7416. IF (expression # NIL) THEN
  7417. Error(position, "return statement with parameter not in procedure scope");
  7418. END;
  7419. ELSE
  7420. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7421. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7422. Error(position, "return statement in procedure that does not return");
  7423. END;
  7424. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7425. IF returnType # NIL THEN
  7426. returnType := returnType.resolved;
  7427. IF expression = NIL THEN
  7428. Error(position, "empty return type in procedure providing a return type")
  7429. ELSIF expression.type = NIL THEN
  7430. Error(position,"returned type incompatible: expression has no type");
  7431. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7432. Error(position, "return type not compatible");
  7433. IF VerboseErrorMessage THEN
  7434. Printout.Info("returnType",returnType);
  7435. Printout.Info("expression",expression);
  7436. END;
  7437. ELSE
  7438. expression := NewConversion(expression.position,expression,returnType,NIL);
  7439. returnStatement.SetReturnValue(expression);
  7440. END;
  7441. ELSIF expression # NIL THEN
  7442. Error(position, "non-empty return type in procedure providing no return type");
  7443. END;
  7444. END;
  7445. END;
  7446. END VisitReturnStatement;
  7447. (** check and resolve await statement AWAIT(condition: Expression)
  7448. - check await condition
  7449. **)
  7450. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7451. VAR condition: SyntaxTree.Expression;
  7452. BEGIN
  7453. condition := ResolveCondition(awaitStatement.condition);
  7454. IF currentIsRealtime THEN
  7455. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7456. END;
  7457. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7458. Error(awaitStatement.position,"senseless await statement with constant condition");
  7459. END;
  7460. awaitStatement.SetCondition(condition);
  7461. END VisitAwaitStatement;
  7462. PROCEDURE CheckSystemImport(position: Position);
  7463. VAR import: SyntaxTree.Import;
  7464. BEGIN
  7465. import := currentScope.ownerModule.moduleScope.firstImport;
  7466. WHILE(import # NIL) DO
  7467. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7468. RETURN;
  7469. END;
  7470. import := import.nextImport;
  7471. END;
  7472. Error(position, "forbidden code without system import");
  7473. END CheckSystemImport;
  7474. (** check and resolve code statement: do nothing, must be done by assembler
  7475. **)
  7476. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7477. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7478. BEGIN
  7479. CheckSystemImport(code.position);
  7480. FOR i := 0 TO code.inRules.Length()-1 DO
  7481. statement := code.inRules.GetStatement(i);
  7482. IF statement IS SyntaxTree.Assignment THEN
  7483. WITH statement: SyntaxTree.Assignment DO
  7484. statement.SetRight(ResolveExpression(statement.right));
  7485. END;
  7486. ELSE
  7487. Error(statement.position, "can only be assignment")
  7488. END;
  7489. END;
  7490. FOR i := 0 TO code.outRules.Length()-1 DO
  7491. statement := code.outRules.GetStatement(i);
  7492. IF statement IS SyntaxTree.Assignment THEN
  7493. WITH statement: SyntaxTree.Assignment DO
  7494. statement.SetLeft(ResolveDesignator(statement.left));
  7495. END;
  7496. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7497. (* must be a reference to some register *)
  7498. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7499. ELSE
  7500. Printout.Info("out statement ", statement);
  7501. Error(statement.position, "(out) can only be assignment")
  7502. END;
  7503. END;
  7504. END VisitCode;
  7505. (** check and set flags of a statement block
  7506. - check for multiply occurence of a flag
  7507. - check and set priority only in bodies
  7508. - check for valid names
  7509. **)
  7510. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7511. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7512. flag: LONGINT; recordBody: SyntaxTree.Body;
  7513. PROCEDURE SetProtectedRecord;
  7514. VAR scope: SyntaxTree.Scope;
  7515. BEGIN
  7516. scope := currentScope;
  7517. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7518. scope := scope.outerScope
  7519. END;
  7520. IF scope # NIL THEN
  7521. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7522. END;
  7523. END SetProtectedRecord;
  7524. BEGIN
  7525. flags := {};
  7526. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7527. recordBody := block(SyntaxTree.Body)
  7528. ELSE
  7529. recordBody := NIL
  7530. END;
  7531. blockModifier := block.blockModifiers;
  7532. WHILE(blockModifier # NIL) DO
  7533. name := blockModifier.identifier;
  7534. expression := blockModifier.expression;
  7535. position := blockModifier.position;
  7536. flag := -1;
  7537. IF name=Global.NamePriority THEN
  7538. IF expression = NIL THEN
  7539. Error(position, "missing priority expression");
  7540. ELSIF recordBody = NIL THEN
  7541. Error(position, "priority not on record body");
  7542. ELSIF recordBody.priority # NIL THEN
  7543. Error(position, "duplicate priority expression");
  7544. ELSE
  7545. recordBody.SetPriority(expression);
  7546. END;
  7547. ELSIF expression # NIL THEN
  7548. Error(expression.position,"expression not in connection with priority")
  7549. ELSIF name=Global.NameExclusive THEN
  7550. IF block.isExclusive THEN
  7551. Error(position, "duplicate exclusive flag")
  7552. END;
  7553. block.SetExclusive(TRUE); SetProtectedRecord;
  7554. ELSIF name=Global.NameActive THEN
  7555. IF recordBody = NIL THEN
  7556. Error(position, "active not in record body");
  7557. ELSIF recordBody.isActive THEN
  7558. Error(position, "duplicate active flag")
  7559. ELSE
  7560. recordBody.SetActive(TRUE); SetProtectedRecord;
  7561. END;
  7562. ELSIF name=Global.NameSafe THEN
  7563. IF recordBody = NIL THEN
  7564. Error(position, "safe not in record body");
  7565. ELSIF recordBody.isSafe THEN
  7566. Error(position, "duplicate safe flag")
  7567. ELSE
  7568. recordBody.SetSafe(TRUE);
  7569. SetProtectedRecord;
  7570. END;
  7571. ELSIF name=Global.NameRealtime THEN
  7572. IF recordBody = NIL THEN
  7573. Error(position, "realtime not in record body");
  7574. ELSIF recordBody.isRealtime THEN
  7575. Error(position, "duplicate realtime flag")
  7576. ELSE
  7577. recordBody.SetRealtime(TRUE);
  7578. block.SetRealtime(TRUE);
  7579. END;
  7580. ELSIF name=Global.NameUnchecked THEN
  7581. IF block.isUnchecked THEN
  7582. Error(position, "duplicate unchecked flag")
  7583. ELSE
  7584. block.SetUnchecked(TRUE);
  7585. END;
  7586. ELSIF (name=Global.NameUncooperative) THEN
  7587. IF block.isUncooperative THEN
  7588. Error(position, "duplicate uncooperative flag")
  7589. ELSE
  7590. block.SetUncooperative(TRUE);
  7591. END;
  7592. ELSE
  7593. Error(position, "unknown block modifier");
  7594. END;
  7595. blockModifier := blockModifier.nextModifier;
  7596. END;
  7597. END BlockFlags;
  7598. (** check and resolve statement block
  7599. - check flags (exclusive)
  7600. - check statement sequence
  7601. **)
  7602. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7603. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7604. BEGIN
  7605. BlockFlags(statementBlock);
  7606. IF statementBlock.isExclusive THEN
  7607. (* check that not in exclusive block *)
  7608. IF currentIsExclusive THEN
  7609. Error (statementBlock.position,"forbidden recursive exclusive")
  7610. ELSIF currentIsRealtime THEN
  7611. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7612. END;
  7613. END;
  7614. recentExclusive := currentIsExclusive;
  7615. recentUnreachable := currentIsUnreachable;
  7616. recentRealtime := currentIsRealtime;
  7617. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7618. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7619. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7620. StatementSequence(statementBlock.statements);
  7621. currentIsRealtime := recentRealtime;
  7622. currentIsExclusive := recentExclusive;
  7623. currentIsUnreachable := recentUnreachable;
  7624. END VisitStatementBlock;
  7625. (** check and resolve body
  7626. - check flags (active, priority, safe)
  7627. - check body and finally part
  7628. **)
  7629. PROCEDURE Body(body: SyntaxTree.Body);
  7630. BEGIN
  7631. VisitStatementBlock(body);
  7632. IF body.isActive THEN
  7633. IF ~currentIsBodyProcedure THEN
  7634. Error(body.position,"active flag not in object body");
  7635. ELSIF body.priority # NIL THEN
  7636. body.SetPriority(ConstantInteger(body.priority));
  7637. END;
  7638. ELSIF body.isSafe THEN
  7639. Error(body.position,"safe flag not in active body");
  7640. ELSIF body.priority # NIL THEN
  7641. Error(body.position,"priority flag not in active body");
  7642. END;
  7643. IF body.code # NIL THEN
  7644. CheckSystemImport(body.position);
  7645. END;
  7646. StatementSequence(body.finally)
  7647. END Body;
  7648. (*** scopes ***)
  7649. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7650. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7651. VAR duplicateSymbol: BOOLEAN;
  7652. BEGIN
  7653. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7654. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7655. Error(symbol.position,"globally defined keyword")
  7656. END;
  7657. scope.EnterSymbol(symbol,duplicateSymbol);
  7658. IF ~allowDuplicate & duplicateSymbol THEN
  7659. Error(symbol.position,"Multiply defined identifier.");
  7660. IF VerboseErrorMessage THEN
  7661. Printout.Info("multiply defined identifier",symbol);
  7662. Printout.Info("in scope",scope);
  7663. END;
  7664. END;
  7665. END Register;
  7666. (**
  7667. implementation: check and resolve an implementation part
  7668. **)
  7669. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7670. move implementation checker to a separate object ? *)
  7671. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7672. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7673. BEGIN
  7674. prevIsRealtime := currentIsRealtime;
  7675. prevIsBodyProcedure := currentIsBodyProcedure;
  7676. prevIsCellNet := currentIsCellNet;
  7677. prevScope := currentScope;
  7678. currentScope := scope;
  7679. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7680. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7681. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7682. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7683. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7684. (*
  7685. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7686. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7687. END;
  7688. *)
  7689. END;
  7690. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7691. (* module body, record bodies are wrapped into an artifical procedure *)
  7692. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7693. Body(scope(SyntaxTree.ProcedureScope).body)
  7694. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7695. Body(scope(SyntaxTree.ProcedureScope).body)
  7696. END;
  7697. END;
  7698. currentScope := prevScope;
  7699. currentIsRealtime := prevIsRealtime;
  7700. currentIsBodyProcedure := prevIsBodyProcedure;
  7701. currentIsCellNet := prevIsCellNet;
  7702. END Implementation;
  7703. (** implementation phase:
  7704. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7705. **)
  7706. PROCEDURE Implementations(x: SyntaxTree.Module);
  7707. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7708. BEGIN
  7709. prevPhase := phase;
  7710. phase := InlinePhase;
  7711. scope := x.firstScope;
  7712. WHILE(scope # NIL) DO
  7713. Implementation(scope);
  7714. scope := scope.nextScope;
  7715. END;
  7716. phase := ImplementationPhase;
  7717. scope := x.firstScope;
  7718. WHILE(scope # NIL) DO
  7719. Implementation(scope);
  7720. scope := scope.nextScope;
  7721. END;
  7722. phase := prevPhase;
  7723. END Implementations;
  7724. (** declaration phase:
  7725. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7726. - import lists (for module scopes)
  7727. - parameter list (for procedure scopes)
  7728. - constant declarations
  7729. - type declarations
  7730. - variable declarations
  7731. - procedure declarations
  7732. preformed in two stages:
  7733. - first all symbols are entered into the symbol table (with uniqueness check),
  7734. - then all symbols are resolved
  7735. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7736. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7737. phases :
  7738. 0 = before procedures
  7739. 1 = procedures and later
  7740. **)
  7741. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7742. VAR
  7743. constant: SyntaxTree.Constant;
  7744. typeDeclaration: SyntaxTree.TypeDeclaration;
  7745. variable: SyntaxTree.Variable;
  7746. procedure: SyntaxTree.Procedure;
  7747. procedureType : SyntaxTree.ProcedureType;
  7748. prevScope: SyntaxTree.Scope;
  7749. parameter: SyntaxTree.Parameter;
  7750. import: SyntaxTree.Import;
  7751. symbol: SyntaxTree.Symbol;
  7752. prevPhase: LONGINT;
  7753. prevError : BOOLEAN;
  7754. i: LONGINT;
  7755. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7756. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7757. BEGIN
  7758. IF type.baseType # NIL THEN
  7759. baseType := type.baseType.resolved;
  7760. IF baseType IS SyntaxTree.PointerType THEN
  7761. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7762. END;
  7763. (*
  7764. IF baseType IS SyntaxTree.CellType THEN
  7765. DeclareCell(baseType(SyntaxTree.CellType));
  7766. END;
  7767. *)
  7768. END;
  7769. parameter := type.firstParameter;
  7770. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7771. (*
  7772. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7773. variable.SetType(parameter.type);
  7774. variable.SetAccess(SyntaxTree.Hidden);
  7775. variable.SetModifiers(parameter.modifiers);
  7776. currentScope.PushVariable(variable);
  7777. *)
  7778. Register(parameter,scope, FALSE);
  7779. parameter := parameter.nextParameter;
  7780. END;
  7781. property := type.firstProperty;
  7782. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7783. (*
  7784. variable := currentScope.FindVariable(property.name);
  7785. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7786. prop := variable(SyntaxTree.Property);
  7787. ELSE (* add, duplicate symbols detection later *)
  7788. prop := SyntaxTree.NewProperty(property.position, property.name);
  7789. currentScope.PushVariable(prop);
  7790. END;
  7791. prop.SetType(property.type);
  7792. prop.SetValue(property.value);
  7793. prop.SetAccess(SyntaxTree.Hidden);
  7794. *)
  7795. Register(property, scope, FALSE);
  7796. property := property.nextProperty;
  7797. END;
  7798. END DeclareCell;
  7799. BEGIN
  7800. prevError := error;
  7801. prevPhase := phase;
  7802. phase := DeclarationPhase;
  7803. prevScope := currentScope;
  7804. currentScope := scope;
  7805. error := FALSE;
  7806. IF 0 IN phases THEN
  7807. (* first enter all symbols in scope *)
  7808. IF scope IS SyntaxTree.ModuleScope THEN
  7809. (* treat imports first for a module scope, , set default context if necessary *)
  7810. import := scope(SyntaxTree.ModuleScope).firstImport;
  7811. WHILE(import # NIL) DO
  7812. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7813. Register(import, currentScope, FALSE);
  7814. import := import.nextImport;
  7815. END;
  7816. import := scope(SyntaxTree.ModuleScope).firstImport;
  7817. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7818. ResolveSymbol(import);
  7819. import := import.nextImport;
  7820. END;
  7821. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7822. (* enter parameters for a procedure scope *)
  7823. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7824. parameter := procedureType.firstParameter;
  7825. WHILE(parameter # NIL) DO
  7826. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7827. END;
  7828. parameter := procedureType.returnParameter;
  7829. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7830. parameter := procedureType.selfParameter;
  7831. IF parameter # NIL THEN
  7832. Register(parameter, currentScope, FALSE);
  7833. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7834. END;
  7835. ELSIF scope IS SyntaxTree.CellScope THEN
  7836. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7837. IF~skipImplementation THEN
  7838. import := scope(SyntaxTree.CellScope).firstImport;
  7839. WHILE(import # NIL) DO
  7840. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7841. Register(import, currentScope, FALSE);
  7842. import := import.nextImport;
  7843. END;
  7844. import := scope(SyntaxTree.CellScope).firstImport;
  7845. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7846. ResolveSymbol(import);
  7847. import := import.nextImport;
  7848. END;
  7849. END;
  7850. END;
  7851. IF error THEN RETURN END;
  7852. IF skipImplementation THEN
  7853. scope.Clear;
  7854. END;
  7855. (* constants *)
  7856. constant := scope.firstConstant;
  7857. WHILE (constant # NIL) DO
  7858. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7859. END;
  7860. (* type declarations *)
  7861. typeDeclaration := scope.firstTypeDeclaration;
  7862. WHILE (typeDeclaration # NIL) DO
  7863. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7864. END;
  7865. (* variables *)
  7866. variable := scope.firstVariable;
  7867. WHILE (variable # NIL) DO
  7868. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7869. END;
  7870. (* procedures *)
  7871. IF scope.procedures # NIL THEN
  7872. FOR i := 0 TO scope.procedures.Length()-1 DO
  7873. procedure := scope.procedures.GetProcedure(i);
  7874. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7875. IF procedureType.selfParameter = NIL THEN
  7876. scope.AddProcedure(procedure);
  7877. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7878. ELSE
  7879. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7880. IF typeDeclaration = NIL THEN
  7881. Error(procedureType.selfParameter.position, "No such type declaration");
  7882. ELSE
  7883. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7884. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7885. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7886. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7887. END;
  7888. END;
  7889. END;
  7890. END;
  7891. END;
  7892. (* now process all symbols without any presumption on the order *)
  7893. symbol := scope.firstSymbol;
  7894. WHILE(symbol # NIL) DO
  7895. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7896. IF (symbol IS SyntaxTree.Procedure) THEN
  7897. IF 1 IN phases THEN
  7898. ResolveSymbol(symbol);
  7899. END;
  7900. ELSE
  7901. IF 0 IN phases THEN
  7902. ResolveSymbol(symbol);
  7903. END;
  7904. END;
  7905. END;
  7906. symbol := symbol.nextSymbol;
  7907. END;
  7908. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7909. symbol := scope.firstSymbol;
  7910. WHILE symbol # NIL DO
  7911. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7912. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7913. pointerFixes.Add(symbol, currentScope);
  7914. END;
  7915. IF ~symbol.type.resolved.isRealtime THEN
  7916. Error(symbol.position,"symbol has no realtime type");
  7917. END;
  7918. END;
  7919. symbol := symbol.nextSymbol
  7920. END;
  7921. END;
  7922. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7923. Error(Basic.invalidPosition,"problems during offset computation in module");
  7924. END;
  7925. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7926. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7927. scope.ownerModule.AddScope(scope);
  7928. END;
  7929. phase := prevPhase;
  7930. currentScope := prevScope;
  7931. error := error OR prevError;
  7932. END Declarations;
  7933. (* nopov *)
  7934. (** check if all operators from one module are compatible to the ones in the other module
  7935. - check if there are not multiple operators with the same signature
  7936. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7937. - check for all operators whose signatures are compatible, whether the return types are compatible
  7938. note that:
  7939. - the return type is not considered to be part of the signature
  7940. - two signatures are considered compatible, if all of the operands are compatible
  7941. **)
  7942. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7943. VAR
  7944. thisOperator, thatOperator: SyntaxTree.Operator;
  7945. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7946. thisParameter, thatParameter: SyntaxTree.Parameter;
  7947. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7948. i: LONGINT;
  7949. BEGIN
  7950. currentScope := thisModuleScope;
  7951. hasError := FALSE;
  7952. (* go through all operators in the other module *)
  7953. thatOperator := thatModuleScope.firstOperator;
  7954. WHILE (thatOperator # NIL) & ~hasError DO
  7955. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7956. (* the other operator is accessible *)
  7957. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7958. (* the other operator is not the conversion operator *)
  7959. (* go through all operators in this module *)
  7960. thisOperator := thisModuleScope.firstOperator;
  7961. WHILE (thisOperator # NIL) & ~hasError DO
  7962. IF thisOperator # thatOperator THEN
  7963. (* the operators are not the same *)
  7964. IF thisOperator.name = thatOperator.name THEN
  7965. (* the operators share the same identifier *)
  7966. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7967. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7968. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7969. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7970. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7971. (* both operators have the same paramter count *)
  7972. thisParameter := thisProcedureType.firstParameter;
  7973. thatParameter := thatProcedureType.firstParameter;
  7974. operandsAreEqual := TRUE;
  7975. operandsAreCompatible := TRUE;
  7976. (* go through all parameters *)
  7977. FOR i := 1 TO thisProcedureType.numberParameters DO
  7978. ASSERT(thatParameter # NIL);
  7979. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7980. operandsAreEqual := FALSE;
  7981. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7982. operandsAreCompatible := FALSE
  7983. END
  7984. END;
  7985. thisParameter := thisParameter.nextParameter;
  7986. thatParameter := thatParameter.nextParameter
  7987. END;
  7988. IF operandsAreEqual THEN
  7989. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7990. hasError := TRUE
  7991. ELSIF operandsAreCompatible THEN
  7992. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7993. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7994. hasError := TRUE
  7995. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7996. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7997. hasError := TRUE
  7998. END
  7999. END
  8000. END
  8001. END
  8002. END;
  8003. thisOperator := thisOperator.nextOperator
  8004. END
  8005. END
  8006. END;
  8007. thatOperator := thatOperator.nextOperator
  8008. END
  8009. END CheckInterOperatorConformity;
  8010. (** check module:
  8011. - check module declaration
  8012. - add context, if necessary
  8013. - remove module from import cache, if necessary
  8014. - check declarations
  8015. - resolve all type fixes
  8016. - check implementation (bodies)
  8017. **)
  8018. PROCEDURE Module*(x: SyntaxTree.Module);
  8019. VAR (* nopov *)
  8020. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8021. BEGIN
  8022. prevScope := currentScope;
  8023. prevIsCellNet := currentIsCellNet;
  8024. module := x;
  8025. ASSERT(x # NIL);
  8026. global := system.globalScope[x.case];
  8027. x.moduleScope.SetGlobalScope(global);
  8028. currentScope := global;
  8029. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8030. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8031. RemoveModuleFromCache(importCache,x);
  8032. Declarations(x.moduleScope, FALSE, {0,1});
  8033. FixTypes();
  8034. IF module.isCellNet THEN
  8035. currentIsCellNet := TRUE;
  8036. modifier := x.modifiers;
  8037. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8038. CheckModifiers(modifier, FALSE);
  8039. END;
  8040. (* nopov *)
  8041. IF ~error THEN
  8042. (* check if operators conform to each other within this module *)
  8043. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8044. (* go through all imports *)
  8045. import := x.moduleScope.firstImport;
  8046. WHILE import # NIL DO
  8047. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8048. (* check if all operators in this module conform to the ones of the imported module *)
  8049. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8050. END;
  8051. import := import.nextImport
  8052. END;
  8053. END;
  8054. Implementations(x);
  8055. module := NIL;
  8056. currentIsCellNet := prevIsCellNet;
  8057. currentScope := prevScope;
  8058. END Module;
  8059. END Checker;
  8060. Warnings*=OBJECT (SyntaxTree.Visitor)
  8061. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8062. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8063. BEGIN
  8064. SELF.diagnostics := diagnostics
  8065. END InitWarnings;
  8066. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8067. BEGIN END VisitPortType;
  8068. (** types *)
  8069. PROCEDURE Type(x: SyntaxTree.Type);
  8070. BEGIN x.Accept(SELF)
  8071. END Type;
  8072. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8073. BEGIN END VisitType;
  8074. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8075. BEGIN END VisitBasicType;
  8076. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8077. BEGIN END VisitCharacterType;
  8078. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8079. BEGIN END VisitIntegerType;
  8080. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8081. BEGIN END VisitFloatType;
  8082. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8083. BEGIN END VisitQualifiedType;
  8084. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8085. BEGIN END VisitStringType;
  8086. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8087. BEGIN END VisitEnumerationType;
  8088. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8089. BEGIN END VisitRangeType;
  8090. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8091. BEGIN
  8092. IF ~(SyntaxTree.Warned IN x.state) THEN
  8093. x.SetState(SyntaxTree.Warned);
  8094. Type(x.arrayBase);
  8095. END;
  8096. END VisitArrayType;
  8097. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8098. BEGIN
  8099. IF ~(SyntaxTree.Warned IN x.state) THEN
  8100. x.SetState(SyntaxTree.Warned);
  8101. Type(x.arrayBase);
  8102. END;
  8103. END VisitMathArrayType;
  8104. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8105. BEGIN
  8106. IF ~(SyntaxTree.Warned IN x.state) THEN
  8107. x.SetState(SyntaxTree.Warned);
  8108. Type(x.pointerBase);
  8109. END;
  8110. END VisitPointerType;
  8111. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8112. BEGIN Scope(x.recordScope) END VisitRecordType;
  8113. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8114. BEGIN Scope(x.cellScope) END VisitCellType;
  8115. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8116. BEGIN END VisitProcedureType;
  8117. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8118. VAR msg: ARRAY 256 OF CHAR;
  8119. BEGIN
  8120. Global.GetSymbolName(x,msg);
  8121. Strings.Append(msg," ");
  8122. Strings.Append(msg,text);
  8123. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8124. END Warning;
  8125. (** symbols *)
  8126. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8127. BEGIN
  8128. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8129. IF ~(x IS SyntaxTree.Parameter) THEN
  8130. Warning(x,"never used");
  8131. END;
  8132. END;
  8133. x.Accept(SELF);
  8134. END Symbol;
  8135. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8136. BEGIN END VisitSymbol;
  8137. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8138. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8139. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8140. BEGIN END VisitConstant;
  8141. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8142. BEGIN END VisitVariable;
  8143. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8144. BEGIN END VisitProperty;
  8145. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8146. BEGIN END VisitParameter;
  8147. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8148. BEGIN
  8149. Scope(x.procedureScope)
  8150. END VisitProcedure;
  8151. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8152. BEGIN END VisitOperator;
  8153. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8154. BEGIN END VisitImport;
  8155. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8156. VAR
  8157. symbol: SyntaxTree.Symbol;
  8158. BEGIN
  8159. symbol := scope.firstSymbol;
  8160. WHILE(symbol # NIL) DO
  8161. Symbol(symbol);
  8162. symbol := symbol.nextSymbol;
  8163. END;
  8164. END Scope;
  8165. PROCEDURE Module*(x: SyntaxTree.Module);
  8166. BEGIN
  8167. SELF.module := x;
  8168. Scope(x.moduleScope);
  8169. END Module;
  8170. END Warnings;
  8171. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8172. BEGIN
  8173. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8174. END IsOberonInline;
  8175. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8176. BEGIN
  8177. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8178. END Resolved;
  8179. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8180. VAR i: LONGINT;
  8181. BEGIN
  8182. i := 1;
  8183. WHILE i < x DO
  8184. i := i *2
  8185. END;
  8186. RETURN i=x
  8187. END PowerOf2;
  8188. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8189. BEGIN
  8190. RETURN
  8191. (scope # NIL) &
  8192. (scope IS SyntaxTree.ModuleScope)
  8193. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8194. OR
  8195. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8196. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8197. END IsCellNetScope;
  8198. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8199. BEGIN
  8200. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8201. END IsCellScope;
  8202. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8203. BEGIN
  8204. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8205. RETURN (scope # NIL) & IsCellNetScope(scope)
  8206. END InCellNetScope;
  8207. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8208. BEGIN
  8209. ASSERT(size MOD system.dataUnit = 0);
  8210. RETURN size DIV system.dataUnit
  8211. END ToMemoryUnits;
  8212. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8213. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8214. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8215. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8216. BEGIN
  8217. IF t = NIL THEN
  8218. RETURN TRUE
  8219. ELSE
  8220. t := t.resolved;
  8221. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8222. END;
  8223. END TypeAllowed;
  8224. BEGIN
  8225. type := type.resolved;
  8226. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8227. RETURN FALSE
  8228. ELSE
  8229. procedureType := type(SyntaxTree.ProcedureType);
  8230. numberParameters := procedureType.numberParameters;
  8231. RETURN
  8232. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8233. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8234. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8235. END;
  8236. END GetProcedureAllowed;
  8237. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8238. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8239. VAR import: SyntaxTree.Import;
  8240. BEGIN
  8241. import := importCache.ImportByModuleName(x.name,x.context);
  8242. IF import # NIL THEN
  8243. importCache.RemoveImporters(x.name,x.context);
  8244. END;
  8245. END RemoveModuleFromCache;
  8246. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8247. (* to <- this assignment compatibility *)
  8248. VAR result: BOOLEAN;
  8249. BEGIN
  8250. IF this= NIL THEN result := (to=NIL)
  8251. ELSIF to=NIL THEN result := FALSE
  8252. ELSE
  8253. (*! will be replaced by this:
  8254. ELSE result := this.CompatibleTo(to.resolved);
  8255. *)
  8256. this := this.resolved; to := to.resolved;
  8257. IF to=SyntaxTree.invalidType THEN result := FALSE
  8258. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8259. ELSIF to = this THEN
  8260. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8261. ELSIF to IS SyntaxTree.BasicType THEN
  8262. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8263. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8264. result := this.CompatibleTo(to.resolved)
  8265. ELSE
  8266. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8267. END
  8268. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8269. result := to.sizeInBits = this.sizeInBits;
  8270. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8271. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8272. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8273. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8274. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8275. result := TRUE;
  8276. ELSIF to IS SyntaxTree.AnyType THEN
  8277. 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);
  8278. ELSIF to IS SyntaxTree.ObjectType THEN
  8279. 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 *) ;
  8280. ELSIF to IS SyntaxTree.ByteType THEN
  8281. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8282. ELSIF to IS SyntaxTree.CharacterType THEN
  8283. result := IsCharacterType(this)
  8284. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8285. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8286. 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
  8287. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8288. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8289. result := TRUE;
  8290. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8291. result := TRUE;
  8292. ELSE
  8293. result := FALSE
  8294. END;
  8295. ELSIF to IS SyntaxTree.PointerType THEN
  8296. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8297. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8298. & (~to.isRealtime OR this.isRealtime);
  8299. ELSIF to IS SyntaxTree.ProcedureType THEN
  8300. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8301. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8302. & (~to.isRealtime OR this.isRealtime)
  8303. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8304. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8305. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8306. ELSIF to IS SyntaxTree.RecordType THEN
  8307. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8308. ELSIF to IS SyntaxTree.ArrayType THEN
  8309. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8310. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8311. ELSIF StaticArrayCompatible(to, this) THEN
  8312. result := TRUE
  8313. ELSE
  8314. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8315. END;
  8316. ELSIF to IS SyntaxTree.MathArrayType THEN
  8317. IF this IS SyntaxTree.MathArrayType THEN
  8318. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8319. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8320. result := TRUE;
  8321. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8322. result := TRUE;
  8323. ELSE
  8324. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8325. END;
  8326. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8327. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8328. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8329. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8330. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8331. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8332. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8333. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8334. ELSE
  8335. result := FALSE
  8336. END;
  8337. (* an array-structured object type is compatible to the type of its array structure *)
  8338. ELSIF IsArrayStructuredObjectType(this) THEN
  8339. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8340. ELSE
  8341. result := FALSE;
  8342. END;
  8343. ELSIF to IS SyntaxTree.StringType THEN
  8344. result := FALSE;
  8345. ELSIF to IS SyntaxTree.EnumerationType THEN
  8346. result := IsEnumerationExtension(this,to);
  8347. ELSIF to IS SyntaxTree.PortType THEN
  8348. result := SameType(to, this)
  8349. ELSE
  8350. Printout.Info("CompatibleTo",to);
  8351. HALT(100); (* implement missing type check *)
  8352. END;
  8353. END;
  8354. RETURN result
  8355. END CompatibleTo;
  8356. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8357. VAR actualBase, formalBase: SyntaxTree.Type;
  8358. BEGIN
  8359. IF SameType(formal,actual) THEN
  8360. RETURN TRUE
  8361. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8362. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8363. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8364. RETURN
  8365. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8366. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8367. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8368. & StaticArrayCompatible(formalBase,actualBase)
  8369. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8370. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8371. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8372. RETURN
  8373. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8374. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8375. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8376. & StaticArrayCompatible(formalBase,actualBase)
  8377. ELSE RETURN FALSE
  8378. END;
  8379. END StaticArrayCompatible;
  8380. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8381. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8382. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8383. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8384. BEGIN
  8385. result := SameType(formal,actual);
  8386. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8387. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8388. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8389. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8390. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8391. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8392. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8393. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8394. & TC(formalBase, actualBase);
  8395. END;
  8396. RETURN result
  8397. END TC;
  8398. BEGIN
  8399. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8400. ELSE
  8401. arrayBase := formalType.arrayBase.resolved;
  8402. IF (actualType IS SyntaxTree.StringType) THEN
  8403. result := arrayBase IS SyntaxTree.CharacterType
  8404. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8405. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8406. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8407. result := TC(formalType, actualType);
  8408. ELSE
  8409. result := (arrayBase IS SyntaxTree.ByteType)
  8410. END;
  8411. END;
  8412. RETURN result
  8413. END OpenArrayCompatible;
  8414. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8415. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8416. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8417. BEGIN
  8418. IF actualType IS SyntaxTree.MathArrayType THEN
  8419. actualArray := actualType(SyntaxTree.MathArrayType);
  8420. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8421. (*
  8422. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8423. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8424. *)
  8425. actualBase := ArrayBase(actualType,Infinity);
  8426. formalBase := ArrayBase(formalType,Infinity);
  8427. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8428. ELSE
  8429. (*
  8430. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8431. *)
  8432. formalBase := Resolved(formalType.arrayBase);
  8433. actualBase := Resolved(actualArray.arrayBase);
  8434. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8435. (*
  8436. ARRAY [k] -> ARRAY [n]
  8437. *)
  8438. result := (formalType.staticLength = actualArray.staticLength)
  8439. ELSE
  8440. result := TRUE
  8441. END;
  8442. IF ~result THEN
  8443. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8444. ELSIF actualBase = NIL THEN result := FALSE
  8445. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8446. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8447. ELSE
  8448. result := SameType(formalBase,actualBase)
  8449. END;
  8450. END;
  8451. ELSE
  8452. result := FALSE
  8453. END;
  8454. RETURN result
  8455. END MathArrayCompatible;
  8456. (**
  8457. Math Array Type distance for assignments / parameter passings of the form
  8458. from -> to
  8459. variants:
  8460. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8461. allowed:
  8462. static -> static (& size match)
  8463. static -> open
  8464. static -> tensor
  8465. open -> open
  8466. open -> tensor
  8467. open -> static
  8468. tensor -> tensor
  8469. tensor -> open
  8470. tensor -> static
  8471. **)
  8472. (*! think about the metric here: is form matching more important than element type matching? *)
  8473. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8474. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8475. BEGIN
  8476. fromBase := Resolved(from.arrayBase);
  8477. toBase := Resolved(to.arrayBase);
  8478. i := Infinity;
  8479. IF from = to THEN
  8480. i := 0;
  8481. ELSIF (from.form = to.form) THEN
  8482. (* static -> static, open -> open, tensor -> tensor *)
  8483. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8484. IF fromBase = toBase THEN i := 0
  8485. ELSIF toBase = NIL THEN i := 1
  8486. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8487. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8488. ELSE
  8489. i := TypeDistance(system,fromBase, toBase, varpar);
  8490. END;
  8491. END;
  8492. ELSIF (to.form = SyntaxTree.Static) THEN
  8493. (* forbidden *)
  8494. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8495. (* static -> tensor, open -> tensor, tensor -> open *)
  8496. IF toBase=fromBase THEN i := 0;
  8497. ELSIF toBase = NIL THEN i := 1;
  8498. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8499. toBase := ArrayBase(toBase,Infinity);
  8500. IF (fromBase=toBase) THEN i := 0
  8501. ELSIF (toBase = NIL) THEN i:= 1
  8502. ELSIF (fromBase = NIL) THEN i := Infinity;
  8503. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8504. END;
  8505. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8506. fromBase := ArrayBase(fromBase,Infinity);
  8507. IF (fromBase=toBase) THEN i := 0
  8508. ELSIF (toBase = NIL) THEN i := 1
  8509. ELSIF (fromBase = NIL) THEN i := Infinity;
  8510. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8511. END;
  8512. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8513. END;
  8514. IF i # Infinity THEN INC(i,2) END;
  8515. ELSIF (from.form = SyntaxTree.Static) THEN
  8516. (* static -> open *)
  8517. IF toBase=fromBase THEN i := 0
  8518. ELSIF toBase = NIL THEN i := 1
  8519. ELSIF fromBase = NIL THEN i := Infinity
  8520. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8521. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8522. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8523. END;
  8524. IF i # Infinity THEN INC(i,1) END;
  8525. ELSE HALT(100); (* unknown case *)
  8526. END;
  8527. RETURN i;
  8528. END MathArrayTypeDistance;
  8529. (** compute and return the distance of two array types
  8530. - return the distance of the base types
  8531. **)
  8532. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8533. VAR i: LONGINT;
  8534. BEGIN
  8535. i := Infinity;
  8536. IF from = to THEN
  8537. i := 0
  8538. ELSE
  8539. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8540. (*
  8541. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8542. i := TypeDistance(from.base, to.base);
  8543. IF i >= 0 THEN INC(i) END
  8544. ELSIF (from.mode = open) & (to.mode = open) THEN
  8545. i := TypeDistance(from.base, to.base);
  8546. *)
  8547. END;
  8548. RETURN i
  8549. END ArrayTypeDistance;
  8550. (** compute the signature distance of a procedure and an actual parameter list
  8551. - if any of the parameters are not compatible, the result is infinite
  8552. - add up and return the distance over all parameters
  8553. **)
  8554. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8555. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8556. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8557. BEGIN
  8558. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8559. result := Infinity
  8560. ELSE
  8561. formalParameter := procedureType.firstParameter;
  8562. i := 0;
  8563. result := 0;
  8564. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8565. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8566. actualParameter := actualParameters.GetExpression(i);
  8567. ASSERT(formalParameter.type # NIL);
  8568. IF (actualParameter.type = NIL) THEN distance := Infinity
  8569. ELSE
  8570. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8571. END;
  8572. IF distance = Infinity THEN
  8573. result := Infinity;
  8574. ELSE
  8575. to := formalParameter.type.resolved;
  8576. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8577. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8578. (* already handled varpar *)
  8579. (*
  8580. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8581. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8582. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8583. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8584. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8585. END;
  8586. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8587. result := Infinity
  8588. END;
  8589. *)
  8590. INC(result, distance);
  8591. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8592. INC(result, distance);
  8593. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8594. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8595. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8596. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8597. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8598. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8599. END;
  8600. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8601. result := Infinity
  8602. END;
  8603. ELSE
  8604. result := Infinity
  8605. END;
  8606. ELSE
  8607. INC(result,distance);
  8608. END;
  8609. END;
  8610. (*
  8611. Printout.Info("actual=", actualParameter);
  8612. Printout.Info("formal=", formalParameter);
  8613. TRACE(result);
  8614. *)
  8615. formalParameter := formalParameter.nextParameter; INC(i);
  8616. END;
  8617. END;
  8618. ASSERT(result >= 0);
  8619. RETURN result
  8620. END Distance;
  8621. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8622. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8623. BEGIN
  8624. IF right.numberParameters # (procedureType.numberParameters) THEN
  8625. result := Infinity
  8626. ELSE
  8627. formalParameter := procedureType.firstParameter;
  8628. rightParameter := right.firstParameter;
  8629. i := 0;
  8630. result := 0;
  8631. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8632. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8633. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8634. IF distance = Infinity THEN
  8635. result := Infinity;
  8636. ELSE
  8637. INC(result,distance);
  8638. END;
  8639. formalParameter := formalParameter.nextParameter;
  8640. rightParameter := rightParameter.nextParameter;
  8641. END;
  8642. END;
  8643. ASSERT(result >= 0);
  8644. RETURN result
  8645. END ProcedureTypeDistance;
  8646. (** compute and return the distance between two types, used for computation of signature distance
  8647. from -> to
  8648. **)
  8649. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8650. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8651. BEGIN
  8652. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8653. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8654. END;
  8655. i := Infinity;
  8656. IF from = to THEN
  8657. i := 0
  8658. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8659. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8660. i := Infinity;
  8661. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8662. i := 10;
  8663. ELSIF (from IS SyntaxTree.StringType) THEN
  8664. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8665. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8666. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8667. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8668. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8669. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8670. i := 1
  8671. ELSIF (from IS SyntaxTree.NilType) THEN
  8672. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8673. (*
  8674. ELSIF (from = NoType) THEN
  8675. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8676. *)
  8677. ELSIF (from IS SyntaxTree.BasicType) THEN
  8678. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8679. IF varpar & (i # 0) THEN i := Infinity END;
  8680. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8681. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8682. ELSIF (from IS SyntaxTree.RecordType) THEN
  8683. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8684. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8685. IF to IS SyntaxTree.MathArrayType THEN
  8686. (*
  8687. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8688. i := Infinity;
  8689. ELSE
  8690. *)
  8691. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8692. (*
  8693. END;
  8694. *)
  8695. END
  8696. ELSIF (from IS SyntaxTree.PointerType) THEN
  8697. ptr := from(SyntaxTree.PointerType);
  8698. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8699. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8700. (* ELSE i := TypeDistance(ptr.base, to); *)
  8701. END
  8702. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8703. IF (to IS SyntaxTree.ProcedureType) THEN
  8704. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8705. END;
  8706. ELSIF (from IS SyntaxTree.PortType) THEN
  8707. IF (to IS SyntaxTree.PortType) THEN
  8708. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8709. i := 0;
  8710. END;
  8711. END;
  8712. (*no procedure test, procedure must be the same*)
  8713. END;
  8714. RETURN i
  8715. END TypeDistance;
  8716. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8717. BEGIN
  8718. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8719. END IsIntegerType;
  8720. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8721. BEGIN
  8722. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8723. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8724. OR IsPointerType(type)
  8725. )
  8726. END IsAddressType;
  8727. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8728. BEGIN
  8729. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8730. END IsSizeType;
  8731. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8732. BEGIN
  8733. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8734. END IsSignedIntegerType;
  8735. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8736. BEGIN
  8737. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8738. END IsUnsignedIntegerType;
  8739. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8740. VAR result: BOOLEAN;
  8741. BEGIN
  8742. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8743. value := x.resolved(SyntaxTree.IntegerValue).value;
  8744. result := TRUE
  8745. ELSE
  8746. result := FALSE
  8747. END;
  8748. RETURN result
  8749. END IsIntegerValue;
  8750. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8751. VAR result: BOOLEAN;
  8752. BEGIN
  8753. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8754. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8755. result := TRUE
  8756. ELSE
  8757. result := FALSE
  8758. END;
  8759. RETURN result
  8760. END IsEnumerationValue;
  8761. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8762. VAR result: BOOLEAN;
  8763. BEGIN
  8764. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8765. value := x.resolved(SyntaxTree.RealValue).value;
  8766. result := TRUE
  8767. ELSE
  8768. result := FALSE
  8769. END;
  8770. RETURN result
  8771. END IsRealValue;
  8772. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8773. VAR result: BOOLEAN;
  8774. BEGIN
  8775. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8776. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8777. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8778. result := TRUE
  8779. ELSE
  8780. result := FALSE
  8781. END;
  8782. RETURN result
  8783. END IsComplexValue;
  8784. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8785. VAR result: BOOLEAN;
  8786. BEGIN
  8787. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8788. value := x.resolved(SyntaxTree.CharacterValue).value;
  8789. result := TRUE
  8790. ELSE
  8791. result := FALSE
  8792. END;
  8793. RETURN result
  8794. END IsCharacterValue;
  8795. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8796. VAR result: BOOLEAN;
  8797. BEGIN
  8798. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8799. value := x.resolved(SyntaxTree.BooleanValue).value;
  8800. result := TRUE
  8801. ELSE
  8802. result := FALSE
  8803. END;
  8804. RETURN result
  8805. END IsBooleanValue;
  8806. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8807. VAR result: BOOLEAN;
  8808. BEGIN
  8809. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8810. value := x.resolved(SyntaxTree.SetValue).value;
  8811. result := TRUE
  8812. ELSE
  8813. result := FALSE
  8814. END;
  8815. RETURN result
  8816. END IsSetValue;
  8817. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8818. VAR result: BOOLEAN;
  8819. BEGIN
  8820. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8821. value := x.resolved(SyntaxTree.StringValue).value;
  8822. result := TRUE
  8823. ELSE
  8824. result := FALSE
  8825. END;
  8826. RETURN result
  8827. END IsStringValue;
  8828. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8829. BEGIN
  8830. x := x.resolved;
  8831. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8832. END Indexable;
  8833. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8834. BEGIN
  8835. RETURN t1.SameType(t2.resolved);
  8836. END SameType;
  8837. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8838. BEGIN
  8839. IF t IS SyntaxTree.MathArrayType THEN
  8840. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8841. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8842. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8843. DEC(max);
  8844. END;
  8845. ELSIF t IS SyntaxTree.ArrayType THEN
  8846. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8847. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8848. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8849. END;
  8850. END;
  8851. RETURN t;
  8852. END ArrayBase;
  8853. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8854. BEGIN
  8855. type := type.resolved;
  8856. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8857. base := type(SyntaxTree.ArrayType).arrayBase;
  8858. RETURN TRUE;
  8859. END;
  8860. RETURN FALSE;
  8861. END IsOpenArray;
  8862. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8863. BEGIN
  8864. type := type.resolved;
  8865. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8866. base := type(SyntaxTree.ArrayType).arrayBase;
  8867. dim := type(SyntaxTree.ArrayType).staticLength;
  8868. RETURN TRUE
  8869. ELSE
  8870. RETURN FALSE
  8871. END;
  8872. END IsStaticArray;
  8873. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8874. BEGIN
  8875. type := type.resolved;
  8876. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8877. base := type(SyntaxTree.ArrayType).arrayBase;
  8878. RETURN TRUE
  8879. ELSE
  8880. RETURN FALSE
  8881. END;
  8882. END IsDynamicArray;
  8883. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8884. VAR i: LONGINT;
  8885. BEGIN
  8886. i := 0;
  8887. t := t.resolved;
  8888. IF t IS SyntaxTree.MathArrayType THEN
  8889. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8890. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8891. END;
  8892. ELSIF t IS SyntaxTree.ArrayType THEN
  8893. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8894. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8895. END;
  8896. END;
  8897. RETURN i
  8898. END Dimension;
  8899. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8900. BEGIN
  8901. RETURN expression.assignable;
  8902. END IsVariable;
  8903. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8904. BEGIN
  8905. IF (symbol IS SyntaxTree.Parameter) THEN
  8906. WITH symbol: SyntaxTree.Parameter DO
  8907. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8908. END;
  8909. ELSE
  8910. RETURN FALSE
  8911. END;
  8912. END IsVariableParameter;
  8913. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8914. VAR result: BOOLEAN;
  8915. BEGIN
  8916. IF type = NIL THEN result := FALSE
  8917. ELSE
  8918. type := type.resolved;
  8919. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8920. END;
  8921. RETURN result
  8922. END IsPointerType;
  8923. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8924. VAR result: BOOLEAN;
  8925. BEGIN
  8926. IF type = NIL THEN result := FALSE
  8927. ELSE
  8928. type := type.resolved;
  8929. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8930. END;
  8931. RETURN result
  8932. END IsUnsafePointer;
  8933. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8934. BEGIN
  8935. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8936. END IsDisposable;
  8937. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8938. VAR result: BOOLEAN;
  8939. BEGIN
  8940. IF type = NIL THEN result := FALSE
  8941. ELSE
  8942. type := type.resolved;
  8943. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8944. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8945. result := result OR (type IS SyntaxTree.ObjectType);
  8946. END;
  8947. RETURN result
  8948. END IsPointerToRecord;
  8949. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8950. VAR result: BOOLEAN;
  8951. BEGIN
  8952. IF type = NIL THEN result := FALSE
  8953. ELSE
  8954. type := type.resolved;
  8955. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8956. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8957. ;
  8958. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8959. result := result OR (type IS SyntaxTree.ObjectType);
  8960. END;
  8961. RETURN result
  8962. END IsPointerToObject;
  8963. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8964. BEGIN
  8965. IF type # NIL THEN
  8966. RETURN type.resolved.hasPointers
  8967. ELSE
  8968. RETURN FALSE
  8969. END;
  8970. END ContainsPointer;
  8971. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8972. BEGIN
  8973. IF type = NIL THEN RETURN FALSE END;
  8974. type := type.resolved;
  8975. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8976. END IsStringType;
  8977. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8978. BEGIN
  8979. IF type = NIL THEN RETURN FALSE END;
  8980. type := type.resolved;
  8981. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8982. END IsCharacterType;
  8983. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8984. BEGIN
  8985. IF type = NIL THEN RETURN FALSE END;
  8986. type := type.resolved;
  8987. RETURN (type IS SyntaxTree.EnumerationType)
  8988. END IsEnumerationType;
  8989. (** cf. section "Type extension (base type)" in the language report **)
  8990. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8991. VAR result: BOOLEAN;
  8992. BEGIN
  8993. ASSERT(base # NIL); ASSERT(extension # NIL);
  8994. base := base.resolved; extension := extension.resolved;
  8995. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8996. result := TRUE;
  8997. ELSE
  8998. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8999. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9000. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9001. END;
  9002. WHILE (extension # NIL) & (extension # base) DO
  9003. IF extension IS SyntaxTree.RecordType THEN
  9004. extension := extension(SyntaxTree.RecordType).baseType;
  9005. IF (extension # NIL) THEN extension := extension.resolved END;
  9006. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9007. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9008. END;
  9009. ELSE extension := NIL;
  9010. END;
  9011. END;
  9012. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9013. END;
  9014. RETURN result
  9015. END IsTypeExtension;
  9016. (** check if base is the base enumeration type of extension **)
  9017. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9018. BEGIN
  9019. base := base.resolved; extension := extension.resolved;
  9020. WHILE (extension # NIL) & (extension # base) DO
  9021. IF extension IS SyntaxTree.EnumerationType THEN
  9022. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9023. IF extension # NIL THEN extension := extension.resolved END;
  9024. ELSE
  9025. extension := NIL
  9026. END;
  9027. END;
  9028. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9029. END IsEnumerationExtension;
  9030. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9031. BEGIN
  9032. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9033. RETURN TRUE
  9034. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9035. RETURN TRUE
  9036. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9037. RETURN TRUE
  9038. ELSE
  9039. RETURN FALSE
  9040. END
  9041. END IsCallable;
  9042. (** compute and return the distance of two record types
  9043. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9044. **)
  9045. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9046. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9047. BEGIN
  9048. i := 0;
  9049. WHILE (from # NIL) & (from # to) DO
  9050. baseType := from.baseType;
  9051. IF (baseType # NIL) THEN
  9052. baseType := baseType.resolved;
  9053. IF baseType IS SyntaxTree.PointerType THEN
  9054. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9055. END;
  9056. IF baseType IS SyntaxTree.RecordType THEN
  9057. from := baseType(SyntaxTree.RecordType);
  9058. ELSE
  9059. from := NIL;
  9060. END;
  9061. ELSE
  9062. from := NIL
  9063. END;
  9064. INC(i)
  9065. END;
  9066. IF from = NIL THEN i := Infinity END;
  9067. RETURN i
  9068. END RecordTypeDistance;
  9069. (** compute and return the distance of two pointer types **)
  9070. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9071. BEGIN
  9072. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9073. RETURN Infinity;
  9074. ELSE
  9075. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9076. END;
  9077. END PointerTypeDistance;
  9078. (** check if expression contains a symbol designator pointing to a type declaration.
  9079. - if so then enter type declaration into typeDeclaration and return true else return false
  9080. **)
  9081. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9082. VAR result: BOOLEAN;
  9083. BEGIN
  9084. result := FALSE;
  9085. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9086. result := TRUE;
  9087. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9088. END;
  9089. RETURN result
  9090. END IsTypeDesignator;
  9091. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9092. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9093. VAR result: BOOLEAN;
  9094. BEGIN
  9095. type := type.resolved;
  9096. IF type IS SyntaxTree.PointerType THEN
  9097. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9098. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9099. result := TRUE
  9100. ELSE
  9101. result := type IS SyntaxTree.RecordType
  9102. END;
  9103. RETURN result
  9104. END IsExtensibleType;
  9105. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9106. BEGIN
  9107. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9108. (d IS SyntaxTree.SymbolDesignator) &
  9109. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9110. OR
  9111. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9112. END IsUnextensibleRecord;
  9113. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9114. BEGIN
  9115. IF IsUnextensibleRecord(d) THEN
  9116. RETURN FALSE
  9117. ELSE RETURN IsExtensibleType(d.type.resolved)
  9118. END;
  9119. END IsExtensibleDesignator;
  9120. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9121. BEGIN
  9122. type := type.resolved;
  9123. IF (type IS SyntaxTree.PointerType) THEN
  9124. RETURN TRUE
  9125. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9126. RETURN TRUE
  9127. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9128. RETURN TRUE
  9129. ELSIF (type IS SyntaxTree.BasicType) THEN
  9130. RETURN TRUE
  9131. END;
  9132. RETURN FALSE
  9133. END IsBasicType;
  9134. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9135. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9136. BEGIN
  9137. baseType := record.baseType;
  9138. IF (baseType # NIL) THEN
  9139. baseType := baseType.resolved;
  9140. IF (baseType IS SyntaxTree.PointerType) THEN
  9141. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9142. END;
  9143. END;
  9144. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9145. recordType := baseType(SyntaxTree.RecordType);
  9146. ELSE
  9147. recordType := NIL;
  9148. END;
  9149. RETURN recordType
  9150. END RecordBase;
  9151. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9152. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9153. BEGIN
  9154. baseRecord := RecordBase(scope.ownerRecord);
  9155. IF baseRecord = NIL THEN RETURN NIL END;
  9156. scope := baseRecord.recordScope;
  9157. procedureType := procedure.type.resolved;
  9158. IF procedure IS SyntaxTree.Operator THEN
  9159. operator := scope.firstOperator;
  9160. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9161. (*
  9162. Printout.Info("not same ",procedureType);
  9163. Printout.Info("with ",operator.type);
  9164. *)
  9165. operator := operator.nextOperator;
  9166. END;
  9167. super := operator;
  9168. ELSE
  9169. super := scope.firstProcedure;
  9170. WHILE (super # NIL) & (super.name # procedure.name) DO
  9171. super := super.nextProcedure;
  9172. END;
  9173. END;
  9174. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9175. RETURN super
  9176. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9177. RETURN super
  9178. ELSE
  9179. RETURN FindSuperProcedure(scope,procedure);
  9180. END;
  9181. END FindSuperProcedure;
  9182. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9183. VAR procedure: SyntaxTree.Procedure;
  9184. BEGIN
  9185. procedure := record.recordScope.constructor;
  9186. IF procedure = NIL THEN
  9187. record := RecordBase(record);
  9188. IF record # NIL THEN
  9189. procedure := GetConstructor(record)
  9190. END;
  9191. END;
  9192. RETURN procedure;
  9193. END GetConstructor;
  9194. (* enter a case into a list of cases in a sorted way and check for collision *)
  9195. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9196. VAR prev,this,new: SyntaxTree.CaseConstant;
  9197. BEGIN
  9198. this := root;
  9199. prev := NIL;
  9200. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9201. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9202. RETURN FALSE
  9203. ELSE
  9204. IF (this # NIL) & (this.min = max+1) THEN
  9205. this.min := min
  9206. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9207. prev.max := min
  9208. ELSE
  9209. NEW(new); new.min := min; new.max := max;
  9210. new.next := this;
  9211. IF prev = NIL THEN
  9212. root := new;
  9213. ELSE
  9214. prev.next := new
  9215. END
  9216. END;
  9217. RETURN TRUE
  9218. END;
  9219. END EnterCase;
  9220. (** generate and return a new checker object, errors are entered into diagnostics **)
  9221. 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;
  9222. VAR checker: Checker;
  9223. BEGIN
  9224. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9225. RETURN checker
  9226. END NewChecker;
  9227. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9228. VAR warnings: Warnings;
  9229. BEGIN
  9230. NEW(warnings, diagnostics); RETURN warnings;
  9231. END NewWarnings;
  9232. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9233. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9234. END IsRangeType;
  9235. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9236. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9237. END IsMathArrayType;
  9238. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9239. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9240. END IsArrayType;
  9241. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9242. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9243. END IsComplexType;
  9244. (** if a type is an array-structured object type *)
  9245. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9246. VAR recordType: SyntaxTree.RecordType;
  9247. BEGIN
  9248. IF type = NIL THEN
  9249. RETURN FALSE
  9250. ELSE
  9251. type := type.resolved;
  9252. IF type IS SyntaxTree.PointerType THEN
  9253. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9254. IF type IS SyntaxTree.RecordType THEN
  9255. recordType := type(SyntaxTree.RecordType);
  9256. RETURN recordType.isObject & recordType.HasArrayStructure()
  9257. ELSE
  9258. RETURN FALSE
  9259. END
  9260. ELSE
  9261. RETURN FALSE
  9262. END
  9263. END
  9264. END IsArrayStructuredObjectType;
  9265. (** the math array structure of a type
  9266. - for math arrays: the array itself
  9267. - for pointers: the math array structure of the pointer base
  9268. - for array-structured object types: the underlying structure
  9269. - for non-math arrays and all other types: NIL
  9270. **)
  9271. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9272. VAR
  9273. result: SyntaxTree.MathArrayType;
  9274. BEGIN
  9275. IF type = NIL THEN
  9276. result := NIL
  9277. ELSE
  9278. type := type.resolved;
  9279. IF type IS SyntaxTree.PointerType THEN
  9280. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9281. END;
  9282. IF type IS SyntaxTree.MathArrayType THEN
  9283. result := type(SyntaxTree.MathArrayType)
  9284. ELSIF type IS SyntaxTree.RecordType THEN
  9285. result := type(SyntaxTree.RecordType).arrayStructure
  9286. ELSE
  9287. result := NIL
  9288. END
  9289. END;
  9290. RETURN result
  9291. END MathArrayStructureOfType;
  9292. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9293. VAR
  9294. result: BOOLEAN;
  9295. rangeExpression: SyntaxTree.RangeExpression;
  9296. BEGIN
  9297. IF x IS SyntaxTree.RangeExpression THEN
  9298. rangeExpression := x(SyntaxTree.RangeExpression);
  9299. result := TRUE;
  9300. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9301. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9302. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9303. ELSE
  9304. result := FALSE
  9305. END;
  9306. RETURN result
  9307. END IsStaticRange;
  9308. (** whether a type is a math array of tensor form **)
  9309. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9310. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9311. END IsTensor;
  9312. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9313. BEGIN
  9314. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9315. length := type(SyntaxTree.MathArrayType).staticLength;
  9316. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9317. RETURN TRUE
  9318. ELSE
  9319. RETURN FALSE
  9320. END;
  9321. END IsStaticMathArray;
  9322. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9323. BEGIN
  9324. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9325. END SymbolHasAddress;
  9326. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9327. BEGIN
  9328. RETURN
  9329. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9330. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9331. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9332. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9333. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9334. ;
  9335. END HasAddress;
  9336. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9337. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9338. BEGIN
  9339. IF (e IS SyntaxTree.Designator) THEN
  9340. d := e(SyntaxTree.Designator);
  9341. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9342. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9343. e := d.left;
  9344. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9345. END;
  9346. IF d # NIL THEN
  9347. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9348. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9349. END;
  9350. END;
  9351. RETURN FALSE;
  9352. END IsLocalVariable;
  9353. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9354. BEGIN
  9355. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9356. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9357. ELSE
  9358. RETURN TRUE
  9359. END;
  9360. END IsStaticProcedure;
  9361. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9362. CONST OptimizeMethodTable = FALSE;
  9363. BEGIN
  9364. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9365. END InMethodTable;
  9366. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9367. BEGIN
  9368. IF type = NIL THEN RETURN FALSE
  9369. ELSE
  9370. type := type.resolved;
  9371. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9372. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9373. END
  9374. END ReturnedAsParameter;
  9375. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9376. BEGIN
  9377. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9378. END StructuredReturnType;
  9379. END FoxSemanticChecker.
  9380. SystemTools.FreeDownTo FoxSemanticChecker ~